/*
*/
/* crypto/engine/e_pk11_pub.c */
/*
* This product includes software developed by the OpenSSL Project for
* use in the OpenSSL Toolkit (http://www.openssl.org/).
*
* This project also referenced hw_pkcs11-0.9.7b.patch written by
* Afchine Madjlessi.
*/
/*
* ====================================================================
* Copyright (c) 2000-2001 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <strings.h>
#ifndef OPENSSL_NO_RSA
#endif /* OPENSSL_NO_RSA */
#ifndef OPENSSL_NO_DSA
#endif /* OPENSSL_NO_DSA */
#ifndef OPENSSL_NO_DH
#endif /* OPENSSL_NO_DH */
#include <pthread.h>
#include <libgen.h>
#ifndef OPENSSL_NO_HW
#ifndef OPENSSL_NO_HW_PK11
#include <security/cryptoki.h>
#include "e_pk11.h"
#include "e_pk11_uri.h"
extern CK_SLOT_ID pubkey_SLOTID;
/*
* During the reinitialization after a detected fork we will try to login to the
* token using the passphrasedialog keyword that we inherit from the parent.
*/
char *passphrasedialog;
#ifndef OPENSSL_NO_RSA
/* RSA stuff */
static int pk11_RSA_verify(int dtype, const unsigned char *m,
#endif
/* DSA stuff */
#ifndef OPENSSL_NO_DSA
#endif
/* DH stuff */
#ifndef OPENSSL_NO_DH
static int pk11_DH_compute_key(unsigned char *key,
#endif
/* Read mode string to be used for fopen() */
#if SOLARIS_OPENSSL
#else
#endif
/*
* Increment existing or create a new reference for an asymmetric key PKCS#11
* object handle in the active object list. If the operation fails, unlock (if
* locked), set error variable and jump to the specified label. We use this list
* so that we can track how many references to the PKCS#11 objects are used from
* all our sessions structures. If we are replacing an object reference in the
* session structure and the ref count for the reference being replaced gets to
* 0 we know that we can safely free the object itself via C_ObjectDestroy().
* See also TRY_OBJ_DESTROY.
*/
if (unlock) { \
} \
goto label; \
}
/*
* Find active list entry according to object handle and return pointer to the
* entry otherwise return NULL.
*
* This function presumes it is called with lock protecting the active list
* held.
*/
static PK11_active *
{
if (entry->h == h) {
return (entry);
}
}
return (NULL);
}
/*
* Search for an entry in the active list using PKCS#11 object handle as a
* failure.
*
* This function presumes it is called with lock protecting the active list
* held.
*/
int
{
if (h == CK_INVALID_HANDLE) {
return (-1);
}
/* search for entry in the active list */
} else {
/* not found, create new entry and add it to the list */
return (-1);
}
entry->h = h;
/* connect the newly created entry to the list */
} else { /* make the entry first in the list */
}
}
}
/*
* Remove active list entry from the list and free it.
*
* This function presumes it is called with lock protecting the active list
* held.
*/
void
{
/* remove the entry from the list and free it */
}
} else {
/* we were the first but not the only one */
}
}
/* sanitization */
entry->h = CK_INVALID_HANDLE;
}
/* Free all entries from the active list. */
void
{
/* only for asymmetric types since only they have C_Find* locks. */
switch (type) {
case OP_RSA:
case OP_DSA:
case OP_DH:
break;
default:
return;
}
/* see find_lock array definition for more info on object locking */
}
}
/*
* Search for active list entry associated with given PKCS#11 object handle,
* decrement its refcnt and if it drops to 0, disconnect the entry and free it.
*
* Return 1 if the PKCS#11 object associated with the entry has no references,
* return 0 if there is at least one reference, -1 on error.
*
* This function presumes it is called with lock protecting the active list
* held.
*/
int
{
return (-1);
}
return (1);
}
return (0);
}
#ifndef OPENSSL_NO_RSA
/* Our internal RSA_METHOD that we provide pointers to */
"PKCS#11 RSA method",
pk11_RSA_public_encrypt, /* rsa_pub_encrypt */
pk11_RSA_public_decrypt, /* rsa_pub_decrypt */
pk11_RSA_private_encrypt, /* rsa_priv_encrypt */
pk11_RSA_private_decrypt, /* rsa_priv_decrypt */
NULL, /* rsa_mod_exp */
NULL, /* bn_mod_exp */
pk11_RSA_init, /* init */
pk11_RSA_finish, /* finish */
RSA_FLAG_SIGN_VER, /* flags */
NULL, /* app_data */
pk11_RSA_sign, /* rsa_sign */
pk11_RSA_verify, /* rsa_verify */
/* Internal rsa_keygen will be used if this is NULL. */
NULL /* rsa_keygen */
};
PK11_RSA(void)
{
return (&pk11_rsa);
}
#endif
#ifndef OPENSSL_NO_DSA
/* Our internal DSA_METHOD that we provide pointers to */
"PKCS#11 DSA method",
pk11_dsa_do_sign, /* dsa_do_sign */
NULL, /* dsa_sign_setup */
pk11_dsa_do_verify, /* dsa_do_verify */
NULL, /* dsa_mod_exp */
NULL, /* bn_mod_exp */
pk11_DSA_init, /* init */
pk11_DSA_finish, /* finish */
0, /* flags */
NULL /* app_data */
};
PK11_DSA(void)
{
return (&pk11_dsa);
}
#endif
#ifndef OPENSSL_NO_DH
/*
* PKCS #11 V2.20, section 11.2 specifies that the number of bytes needed for
* output buffer may somewhat exceed the precise number of bytes needed, but
* should not exceed it by a large amount. That may be caused, for example, by
* rounding it up to multiple of X in the underlying bignum library. 8 should be
* enough.
*/
/* Our internal DH_METHOD that we provide pointers to */
"PKCS#11 DH method",
pk11_DH_generate_key, /* generate_key */
pk11_DH_compute_key, /* compute_key */
NULL, /* bn_mod_exp */
pk11_DH_init, /* init */
pk11_DH_finish, /* finish */
0, /* flags */
NULL, /* app_data */
NULL /* generate_params */
};
PK11_DH(void)
{
return (&pk11_dh);
}
#endif
/* Size of an SSL signature: MD5+SHA1 */
/* Lengths of DSA data and signature */
#ifndef OPENSSL_NO_RSA
/*
* Similar to OpenSSL to take advantage of the paddings. The goal is to
* support all paddings in this engine although PK11 library does not
* support all the paddings used in OpenSSL.
* The input errors should have been checked in the padding functions.
*/
static int
{
goto err;
}
switch (padding) {
case RSA_PKCS1_PADDING:
break;
#ifndef OPENSSL_NO_SHA
case RSA_PKCS1_OAEP_PADDING:
break;
#endif
case RSA_SSLV23_PADDING:
break;
case RSA_NO_PADDING:
break;
default:
goto err;
}
if (i <= 0) {
goto err;
}
/* PK11 functions are called here */
err:
}
return (r);
}
/*
* Similar to Openssl to take advantage of the paddings. The input errors
* should be caught in the padding functions
*/
static int
{
goto err;
}
switch (padding) {
case RSA_PKCS1_PADDING:
break;
case RSA_NO_PADDING:
break;
case RSA_SSLV23_PADDING:
default:
goto err;
}
if (i <= 0) {
goto err;
}
/* PK11 functions are called here */
err:
}
return (r);
}
/* Similar to OpenSSL code. Input errors are also checked here */
static int
{
BIGNUM f;
unsigned char *p;
BN_init(&f);
goto err;
}
/*
* This check was for equality but PGP does evil things
* and chops off the top '0' bytes
*/
goto err;
}
/* make data into a big number */
goto err;
}
goto err;
}
/* PK11 functions are called here */
/*
* PK11 CKM_RSA_X_509 mechanism pads 0's at the beginning.
* Needs to skip these 0's paddings here.
*/
for (j = 0; j < r; j++) {
if (buf[j] != 0) {
break;
}
}
p = buf + j;
j = r - j; /* j is only used with no-padding mode */
switch (padding) {
case RSA_PKCS1_PADDING:
break;
#ifndef OPENSSL_NO_SHA
case RSA_PKCS1_OAEP_PADDING:
break;
#endif
case RSA_SSLV23_PADDING:
break;
case RSA_NO_PADDING:
break;
default:
goto err;
}
if (r < 0) {
}
err:
BN_clear_free(&f);
}
return (r);
}
/* Similar to OpenSSL code. Input errors are also checked here */
static int
{
BIGNUM f;
unsigned char *p;
BN_init(&f);
goto err;
}
/*
* This check was for equality but PGP does evil things
* and chops off the top '0' bytes
*/
goto err;
}
goto err;
}
goto err;
}
/* PK11 functions are called here */
/*
* PK11 CKM_RSA_X_509 mechanism pads 0's at the beginning.
* Needs to skip these 0's here
*/
for (i = 0; i < r; i++) {
if (buf[i] != 0) {
break;
}
}
p = buf + i;
i = r - i; /* i is only used with no-padding mode */
switch (padding) {
case RSA_PKCS1_PADDING:
break;
case RSA_NO_PADDING:
break;
default:
goto err;
}
if (r < 0) {
}
err:
BN_clear_free(&f);
}
return (r);
}
/*
* This function implements RSA public encryption using C_EncryptInit and
* C_Encrypt pk11 interfaces. Note that the CKM_RSA_X_509 is used here.
* The calling function allocated sufficient memory in "to" to store results.
*/
static int
{
return (-1);
}
if (h_pub_key == CK_INVALID_HANDLE) {
}
if (h_pub_key != CK_INVALID_HANDLE) {
return (-1);
}
PK11_R_ENCRYPT, rv);
return (-1);
}
}
return (retval);
}
/*
* This function implements RSA private encryption using C_SignInit and
* C_Sign pk11 APIs. Note that CKM_RSA_X_509 is used here.
* The calling function allocated sufficient memory in "to" to store results.
*/
static int
{
return (-1);
}
if (h_priv_key == CK_INVALID_HANDLE) {
}
if (h_priv_key != CK_INVALID_HANDLE) {
return (-1);
}
rv);
return (-1);
}
retval = ul_sig_len;
}
return (retval);
}
/*
* This function implements RSA private decryption using C_DecryptInit and
* C_Decrypt pk11 APIs. Note that CKM_RSA_X_509 mechanism is used here.
* The calling function allocated sufficient memory in "to" to store results.
*/
static int
{
return (-1);
}
if (h_priv_key == CK_INVALID_HANDLE) {
}
if (h_priv_key != CK_INVALID_HANDLE) {
return (-1);
}
PK11_R_DECRYPT, rv);
return (-1);
}
}
return (retval);
}
/*
* This function implements RSA public decryption using C_VerifyRecoverInit
* and C_VerifyRecover pk11 APIs. Note that CKM_RSA_X_509 is used here.
* The calling function allocated sufficient memory in "to" to store results.
*/
static int
{
return (-1);
}
if (h_pub_key == CK_INVALID_HANDLE) {
}
if (h_pub_key != CK_INVALID_HANDLE) {
return (-1);
}
return (-1);
}
}
return (retval);
}
static int
{
/*
* This flag in the RSA_METHOD enables the new rsa_sign,
* rsa_verify functions. See rsa.h for details.
*/
return (1);
}
static int
{
/*
* Since we are overloading OpenSSL's native RSA_eay_finish() we need
* to do the same as in the original function, i.e. to free bignum
* structures.
*/
}
}
}
return (1);
}
/*
* Standard engine interface function. Majority codes here are from
* rsa/rsa_sign.c. We replaced the decrypt function call by C_Sign of PKCS#11.
* See more details in rsa/rsa_sign.c
*/
static int
{
int i, j;
unsigned char *p, *s = NULL;
int ret = 0;
unsigned long ulsiglen;
/* Encode the digest */
/* Special case: SSL signature, just check the length */
if (type == NID_md5_sha1) {
if (m_len != SSL_SIG_LENGTH) {
goto err;
}
i = SSL_SIG_LENGTH;
s = (unsigned char *)m;
} else {
goto err;
}
goto err;
}
}
if ((i - RSA_PKCS1_PADDING) > j) {
goto err;
}
if (type != NID_md5_sha1) {
s = (unsigned char *)OPENSSL_malloc((unsigned int)(j + 1));
if (s == NULL) {
goto err;
}
p = s;
(void) i2d_X509_SIG(&sig, &p);
}
goto err;
}
if (h_priv_key == CK_INVALID_HANDLE) {
}
if (h_priv_key != CK_INVALID_HANDLE) {
goto err;
}
ulsiglen = j;
(CK_ULONG_PTR) &ulsiglen);
goto err;
}
ret = 1;
}
err:
if (type != NID_md5_sha1) {
(void) memset(s, 0, (unsigned int)(j + 1));
OPENSSL_free(s);
}
return (ret);
}
static int
{
int i, j;
unsigned char *p, *s = NULL;
int ret = 0;
/* Encode the digest */
/* Special case: SSL signature, just check the length */
if (type == NID_md5_sha1) {
if (m_len != SSL_SIG_LENGTH) {
goto err;
}
i = SSL_SIG_LENGTH;
s = (unsigned char *)m;
} else {
goto err;
}
goto err;
}
}
if ((i - RSA_PKCS1_PADDING) > j) {
goto err;
}
if (type != NID_md5_sha1) {
s = (unsigned char *)OPENSSL_malloc((unsigned int)(j + 1));
if (s == NULL) {
goto err;
}
p = s;
(void) i2d_X509_SIG(&sig, &p);
}
goto err;
}
if (h_pub_key == CK_INVALID_HANDLE) {
}
if (h_pub_key != CK_INVALID_HANDLE) {
rv);
goto err;
}
goto err;
}
ret = 1;
}
err:
if (type != NID_md5_sha1) {
OPENSSL_free(s);
}
return (ret);
}
/*
* Get a key from a file whose name is 'keyid'.
* 'keyid' may or may not contain the file URI prefix, "file://".
*/
static EVP_PKEY *
{
const char *filename;
/* file URI: skip the "file://" prefix */
} else {
/* assume keyid is a filename without file URI prefix */
}
/* 'keyid' was not a file */
return (NULL);
}
if (is_private) {
} else {
}
return (NULL);
}
goto err;
}
/*
* This will always destroy the RSA object since we have a new
* RSA structure here.
*/
if (is_private) {
} else {
}
if (h_key == CK_INVALID_HANDLE) {
goto err;
}
return (pkey);
err:
}
}
return (NULL);
}
/*
* Get a key from a token.
* uri_struct->object is required.
*/
static EVP_PKEY *
{
};
/*
* These public attributes are needed to initialize the OpenSSL RSA
* structure with something we can use to look up the key. Note that we
* never ask for private components.
*/
};
/* make sure that the correct token was specified */
if (pk11_check_token_attrs(uri_struct) == 0) {
return (NULL);
}
/* The "object" token is mandatory in the PKCS#11 URI. */
return (NULL);
}
/* login to the session */
uri_struct, is_private) == 0) {
return (NULL);
}
&ks_key) == 0) {
goto err;
}
/*
* Free the structure now. Note that we use uri_struct's field
* directly in the template so we can't free until find is done.
*/
/*
* We might have a cache hit which we could confirm according to
* the 'n'/'e' params. However, it is easier just to recreate
* everything. We expect the keys to be loaded once and used
* many times. We do not check the return value because even in
* case of failure the sp structure will have both key pointer
* and object handle cleaned and pk11_destroy_object() reports
* the failure to the OpenSSL error message buffer.
*/
if (is_private) {
/* This object should not be deleted on a cache miss. */
} else {
}
goto err;
}
goto err;
}
/* Cache the RSA structure pointer. */
if (is_private) {
} else {
}
/* We only export the non-sensitive key components: n and e */
if (is_private) {
/*
* Must have 'n'/'e' components in the session structure as
* well. They serve as a public look-up key for the private key
* in the keystore.
*/
}
goto err;
}
goto err;
}
if (is_private) {
/*
* We do not use pk11_get_private_rsa_key() here so we must
* take care of handle management ourselves.
*/
} else {
/*
* Create a session object from it so that when calling
* pk11_get_public_rsa_key() the next time, we can find it. The
* reason why we do that is that we cannot tell from the RSA
* structure (OpenSSL RSA structure does not have any room for
* additional data used by the engine, for example) if it bears
* a public key stored in the keystore or not so it's better if
* we always have a session key. Note that this is different
* from what we do for the private keystore objects but in that
* case, we can tell from the RSA structure that the keystore
* object is in play - the 'd' component is NULL in that case.
*/
if (h_pub_key == CK_INVALID_HANDLE) {
goto err;
}
}
return (pkey);
err:
}
}
return (NULL);
}
/*
* Load an RSA key from a file or get its PKCS#11 handle if stored in the
* PKCS#11 token.
*/
static EVP_PKEY *
{
int ret;
return (NULL);
}
/*
* pkcs11_parse_uri() returns
* - PK11_URI_OK if keyid is a valid PKCS#11 URI
* - PK11_NOT_PKCS11_URI if keyid is not a PKCS#11 URI
* - other return code if keyid is badly formatted
*/
if (ret == PK11_URI_OK) {
/* PKCS#11 URI: try to find a key from a PKCS#11 token */
} else if (ret == PK11_NOT_PKCS11_URI) {
/* Not PKCS#11 URI: assume it is a file */
} else {
/* hard failure */
}
return (pkey);
}
/*
* Load RSA private key from a file or get its PKCS#11 handle if stored in the
* PKCS#11 token.
*/
/* ARGSUSED */
EVP_PKEY *
{
}
/* Load RSA public key from a file or load it from the PKCS#11 token. */
/* ARGSUSED */
EVP_PKEY *
{
}
/*
* Get a public key object in a session from a given rsa structure. If the
* PKCS#11 session object already exists it is found, reused, and
* the counter in the active object list incremented. If not found, a new
* session object is created and put also onto the active object list.
*
* We use the session field from sp, and we cache rsa->(n|e) in
* opdata_rsa_(n|e|d)_num, respectively.
*/
static CK_OBJECT_HANDLE
{
{CKA_MODULUS, (void *)NULL, 0},
{CKA_PUBLIC_EXPONENT, (void *)NULL, 0}
};
int i;
goto malloc_err;
}
goto malloc_err;
}
/* see find_lock array definition for more info on object locking */
goto err;
}
goto err;
}
goto err;
}
if (found == 0) {
goto err;
}
}
goto err;
}
goto err;
}
/* LINTED: E_CONSTANT_CONDITION */
err:
if (rollback) {
/*
* We do not care about the return value from C_DestroyObject()
* since we are doing rollback.
*/
if (found == 0) {
}
}
for (i = 5; i <= 6; i++) {
}
}
return (h_key);
}
/*
* Function similar to pk11_get_public_rsa_key(). In addition to 'n' and 'e'
* components, it also caches 'd' if present. Note that if RSA keys by reference
* are used, 'd' is never extracted from the token in which case it would be
* NULL here.
*/
static CK_OBJECT_HANDLE
{
int i;
/*
* Both CKA_TOKEN and CKA_SENSITIVE have to be CK_FALSE for session keys
*/
{CKA_MODULUS, (void *)NULL, 0},
{CKA_PUBLIC_EXPONENT, (void *)NULL, 0},
{CKA_PRIVATE_EXPONENT, (void *)NULL, 0},
{CKA_PRIME_1, (void *)NULL, 0},
{CKA_PRIME_2, (void *)NULL, 0},
{CKA_EXPONENT_1, (void *)NULL, 0},
{CKA_EXPONENT_2, (void *)NULL, 0},
{CKA_COEFFICIENT, (void *)NULL, 0},
};
/* Put the private key components into the template */
goto malloc_err;
}
/* see find_lock array definition for more info on object locking */
/*
* We are getting the private key but the private 'd' component is NULL.
* That means this is key by reference RSA key. In that case, we can
* use only public components for searching for the private key handle.
*/
ul_key_attr_count = 8;
/*
* We will perform the search in the token, not in the existing
* session keys.
*/
}
goto err;
}
goto err;
}
goto err;
}
if (found == 0) {
/*
* We have an RSA structure with 'n'/'e' components only so we
* tried to find the private key in the keystore. If it was
* really a token key we have a problem. Note that for other key
* types we just create a new session key using the private
* components from the RSA structure.
*/
goto err;
}
goto err;
}
}
/*
* When RSA keys by reference code is used, we never extract private
* components from the keystore. In that case 'd' was set to NULL and we
* expect the application to properly cope with that. It is documented
* in openssl(5). In general, if keys by reference are used we expect it
* to be used exclusively using the high level API and then there is no
* problem. If the application expects the private components to be read
* from the keystore then that is not a supported way of usage.
*/
goto err;
}
} else {
}
/*
* For the key by reference code, we need public components as well
* since 'd' component is always NULL. For that reason, we always cache
* 'n'/'e' components as well.
*/
goto err;
}
goto err;
}
/* LINTED: E_CONSTANT_CONDITION */
err:
if (rollback) {
/*
* We do not care about the return value from C_DestroyObject()
* since we are doing rollback.
*/
if (found == 0) {
}
}
/*
* 6 to 13 entries in the key template are key components.
* They need to be freed upon exit or error.
*/
for (i = 6; i <= 13; i++) {
a_key_template[i].ulValueLen);
}
}
return (h_key);
}
/*
* Check for cache miss. Objects are cleaned only if we have a full cache miss,
* meaning that it's a different RSA key pair. Return 1 for cache hit, 0 for
* cache miss.
*/
static int
{
/*
* Provide protection against RSA structure reuse by making the
* check for cache hit stronger. Only public components of RSA
* key matter here so it is sufficient to compare them with values
* cached in PK11_SESSION structure.
*
* We must check the handle as well since with key by reference, public
* components 'n'/'e' are cached in private keys as well. That means we
* could have a cache hit in a private key when looking for a public
* key. That would not work, you cannot have one PKCS#11 object for
* both data signing and verifying.
*/
return (1);
} else {
/*
* No public key object yet but we have the right RSA
* structure with potentially existing private key
* object. We can just create a public object and move
* on with this session structure.
*/
return (0);
}
}
/*
* A different RSA key pair was using this session structure previously
* or it's an empty structure. Destroy what we can.
*/
return (0);
}
/*
* Check for cache miss. Objects are cleaned only if we have a full cache miss,
* meaning that it's a different RSA key pair. Return 1 for cache hit, 0 for
* cache miss.
*/
static int
{
/*
* Provide protection against RSA structure reuse by making the
* check for cache hit stronger. Comparing public exponent of RSA
* key with value cached in PK11_SESSION structure should
* be sufficient. Note that we want to compare the public component
* since with the keys by reference mechanism, private components are
* not in the RSA structure. Also, see pk11_check_new_rsa_key_pub()
* about why we compare the handle as well.
*/
return (1);
} else {
/*
* No private key object yet but we have the right RSA
* structure with potentially existing public key
* object. We can just create a private object and move
* on with this session structure.
*/
return (0);
}
}
/*
* A different RSA key pair was using this session structure previously
* or it's an empty structure. Destroy what we can.
*/
return (0);
}
#endif
#ifndef OPENSSL_NO_DSA
/* The DSA function implementation */
/* ARGSUSED */
static int
{
return (1);
}
/* ARGSUSED */
static int
{
return (1);
}
static DSA_SIG *
{
int i;
/*
* The signature is the concatenation of r and s,
* each is 20 bytes long
*/
goto ret;
}
if (dlen > i) {
goto ret;
}
goto ret;
}
if (h_priv_key == CK_INVALID_HANDLE) {
}
if (h_priv_key != CK_INVALID_HANDLE) {
goto ret;
}
goto ret;
}
}
goto ret;
}
goto ret;
}
goto ret;
}
goto ret;
}
dsa_sig->r = r;
dsa_sig->s = s;
ret:
if (r != NULL) {
BN_free(r);
}
if (s != NULL) {
BN_free(s);
}
}
return (dsa_sig);
}
static int
{
int i;
int retval = 0;
goto ret;
}
goto ret;
}
if (dlen > i) {
goto ret;
}
goto ret;
}
if (h_pub_key == CK_INVALID_HANDLE) {
}
if (h_pub_key != CK_INVALID_HANDLE) {
rv);
goto ret;
}
/*
* The representation of each of the two big numbers could
* be shorter than DSA_SIGNATURE_LEN/2 bytes so we need
* to act accordingly and shift if necessary.
*/
BN_num_bytes(sig->s));
goto ret;
}
}
retval = 1;
ret:
return (retval);
}
/*
* Create a public key object in a session from a given dsa structure.
* The *dsa_pub_num pointer is non-NULL for DSA public keys.
*/
{
int i;
};
goto malloc_err;
}
/* see find_lock array definition for more info on object locking */
goto err;
}
goto err;
}
goto err;
}
if (found == 0) {
goto err;
}
}
if (dsa_pub_num != NULL) {
goto err;
}
}
/* LINTED: E_CONSTANT_CONDITION */
}
err:
if (rollback) {
/*
* We do not care about the return value from C_DestroyObject()
* since we are doing rollback.
*/
if (found == 0) {
}
}
for (i = 4; i <= 7; i++) {
}
}
return (h_key);
}
/*
* Create a private key object in the session from a given dsa structure
* The *dsa_priv_num pointer is non-NULL for DSA private keys.
*/
{
int i;
/*
* Both CKA_TOKEN and CKA_SENSITIVE have to be CK_FALSE for session keys
*/
};
/* Put the private key components into the template */
goto malloc_err;
}
/* see find_lock array definition for more info on object locking */
goto err;
}
goto err;
}
goto err;
}
if (found == 0) {
goto err;
}
}
if (dsa_priv_num != NULL) {
goto err;
}
}
/* LINTED: E_CONSTANT_CONDITION */
}
err:
if (rollback) {
/*
* We do not care about the return value from C_DestroyObject()
* since we are doing rollback.
*/
if (found == 0) {
}
}
/*
* 5 to 8 entries in the key template are key components.
* They need to be freed apon exit or error.
*/
for (i = 5; i <= 8; i++) {
a_key_template[i].ulValueLen);
}
}
return (h_key);
}
/*
* Check for cache miss and clean the object pointer and handle
* in such case. Return 1 for cache hit, 0 for cache miss.
*/
static int
{
/*
* Provide protection against DSA structure reuse by making the
* check for cache hit stronger. Only public key component of DSA
* key matters here so it is sufficient to compare it with value
* cached in PK11_SESSION structure.
*/
/*
* We do not check the return value because even in case of
* failure the sp structure will have both key pointer
* and object handle cleaned and pk11_destroy_object()
* reports the failure to the OpenSSL error message buffer.
*/
return (0);
}
return (1);
}
/*
* Check for cache miss and clean the object pointer and handle
* in such case. Return 1 for cache hit, 0 for cache miss.
*/
static int
{
/*
* Provide protection against DSA structure reuse by making the
* check for cache hit stronger. Only private key component of DSA
* key matters here so it is sufficient to compare it with value
* cached in PK11_SESSION structure.
*/
/*
* We do not check the return value because even in case of
* failure the sp structure will have both key pointer
* and object handle cleaned and pk11_destroy_object()
* reports the failure to the OpenSSL error message buffer.
*/
return (0);
}
return (1);
}
#endif
#ifndef OPENSSL_NO_DH
/* The DH function implementation */
/* ARGSUSED */
static int
{
return (1);
}
/* ARGSUSED */
static int
{
return (1);
}
/*
* Generate DH key-pair.
*
* Warning: Unlike OpenSSL's DH_generate_key(3) we ignore dh->priv_key
* and override it even if it is set. OpenSSL does not touch dh->priv_key
* if set and just computes dh->pub_key. It looks like PKCS#11 standard
* is not capable of providing this functionality. This could be a problem
* for applications relying on OpenSSL's semantics.
*/
static int
{
CK_ULONG i;
};
};
};
};
/*
* We must not increase ulValueLen by DH_BUF_RESERVE since that
* could cause the same rounding problem. See definition of
* DH_BUF_RESERVE above.
*/
goto err;
}
} else {
goto err;
}
goto err;
}
} else {
goto err;
}
/*
* Note: we are only using PK11_SESSION structure for getting
* a session handle. The objects created in this function are
* destroyed before return and thus not cached.
*/
goto err;
}
&h_priv_key);
goto err;
}
/*
* Reuse the larger memory allocated. We know the larger memory
* should be sufficient for reuse.
*/
} else {
}
goto err;
}
goto err;
}
/* Reuse the memory allocated */
goto err;
}
goto err;
}
}
goto err;
}
}
/* Reuse the memory allocated */
goto err;
}
goto err;
}
}
goto err;
}
}
ret = 1;
err:
if (h_pub_key != CK_INVALID_HANDLE) {
}
}
if (h_priv_key != CK_INVALID_HANDLE) {
}
}
for (i = 1; i <= 2; i++) {
}
}
return (ret);
}
static int
{
int i;
};
};
goto err;
}
goto err;
}
goto err;
}
if (h_key == CK_INVALID_HANDLE) {
}
if (h_key == CK_INVALID_HANDLE) {
goto err;
}
goto err;
}
rv);
goto err;
}
goto err;
}
priv_key_result[0].pValue =
if (!priv_key_result[0].pValue) {
goto err;
}
rv);
goto err;
}
/*
* OpenSSL allocates the output buffer 'key' which is the same
* length of the public key. It is long enough for the derived key
*/
/*
* CKM_DH_PKCS_DERIVE mechanism is not supposed to strip
* leading zeros from a computed shared secret. However,
* OpenSSL always did it so we must do the same here. The
* vagueness of the spec regarding leading zero bytes was
* finally cleared with TLS 1.1 (RFC 4346) saying that leading
* zeros are stripped before the computed data is used as the
* pre-master secret.
*/
for (i = 0; i < priv_key_result[0].ulValueLen; ++i) {
if (((char *)priv_key_result[0].pValue)[i] != 0) {
break;
}
}
priv_key_result[0].ulValueLen - i);
}
err:
if (h_derived_key != CK_INVALID_HANDLE) {
}
}
if (priv_key_result[0].pValue) {
}
if (mechanism.pParameter) {
}
return (ret);
}
static CK_OBJECT_HANDLE
{
int i;
{
};
goto malloc_err;
}
goto malloc_err;
}
goto malloc_err;
}
/* see find_lock array definition for more info on object locking */
goto err;
}
goto err;
}
rv);
goto err;
}
if (found == 0) {
rv);
goto err;
}
}
if (dh_priv_num != NULL) {
goto err;
}
}
/* LINTED: E_CONSTANT_CONDITION */
}
err:
if (rollback) {
/*
* We do not care about the return value from C_DestroyObject()
* since we are doing rollback.
*/
if (found == 0) {
}
}
for (i = 4; i <= 6; i++) {
}
}
return (h_key);
}
/*
* Check for cache miss and clean the object pointer and handle
* in such case. Return 1 for cache hit, 0 for cache miss.
*
* Note: we rely on pk11_destroy_dh_key_objects() to set sp->opdata_dh
* to CK_INVALID_HANDLE even when it fails to destroy the object.
*/
static int
{
/*
* Provide protection against DH structure reuse by making the
* check for cache hit stronger. Private key component of DH key
* is unique so it is sufficient to compare it with value cached
* in PK11_SESSION structure.
*/
/*
* We do not check the return value because even in case of
* failure the sp structure will have both key pointer
* and object handle cleaned and pk11_destroy_object()
* reports the failure to the OpenSSL error message buffer.
*/
return (0);
}
return (1);
}
#endif
/*
* Local function to simplify key template population
* Return 0 -- error, 1 -- no error
*/
static int
{
/*
* This function can be used on non-initialized BIGNUMs. It is easier to
* check that here than individually in the callers.
*/
}
return (1);
}
*ul_value_len = len;
return (0);
}
return (1);
}
static void
{
if (attr->ulValueLen > 0) {
}
}
/*
* Find one object in the token. It is an error if we can not find the object or
* if we find more objects based on the template we got.
*
* Returns:
* 1 OK
* 0 no object or more than 1 object found
*/
static int
{
goto err;
}
rv);
goto err;
}
if (objcnt > 1) {
goto err;
} else if (objcnt == 0) {
goto err;
}
(void) pFuncList->C_FindObjectsFinal(s);
return (1);
err:
return (0);
}
/*
* OpenSSL 1.0.0 introduced ENGINE API for the PKEY EVP functions. Sadly,
* "openssl dgst -dss1 ..." now uses a new function EVP_DigestSignInit() which
* internally needs a PKEY method for DSA even when in the engine. So, to avoid
* a regression when moving from 0.9.8 to 1.0.0, we use an internal OpenSSL
* structure for the DSA PKEY methods to make it work. It is a future project to
* make it work with HW acceleration.
*
* Note that at the time of 1.0.0d release there is no documentation as to how
* the PKEY EVP functions are to be implemented in an engine. There is only one
* engine shipped with 1.0.0d that uses the PKEY EVP methods, the GOST engine.
* It was used as an example when fixing the above mentioned regression problem.
*/
int
int nid)
{
return (1);
}
switch (nid) {
case NID_dsa:
return (1);
}
/* Error branch. */
return (0);
}
#endif /* OPENSSL_NO_HW_PK11 */
#endif /* OPENSSL_NO_HW */