hw_pk11_pub.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/* crypto/engine/hw_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 <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <cryptlib.h>
#ifndef OPENSSL_NO_HW
#ifndef OPENSSL_NO_HW_PK11
#include "security/cryptoki.h"
#include "hw_pk11_err.c"
#ifndef OPENSSL_NO_RSA
/* RSA stuff */
static int pk11_RSA_verify(int dtype, const unsigned char *m,
PK11_SESSION *sp);
#endif
/* DSA stuff */
#ifndef OPENSSL_NO_RSA
PK11_SESSION *sp);
PK11_SESSION *sp);
#endif
/* DH stuff */
#ifndef OPENSSL_NO_DH
static int pk11_DH_compute_key(unsigned char *key,
#endif
#ifndef OPENSSL_NO_RSA
/* Our internal RSA_METHOD that we provide pointers to */
static RSA_METHOD pk11_rsa =
{
"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 */
};
RSA_METHOD *PK11_RSA(void)
{
return(&pk11_rsa);
}
#endif
#ifndef OPENSSL_NO_DSA
/* Our internal DSA_METHOD that we provide pointers to */
static DSA_METHOD pk11_dsa =
{
"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 */
};
DSA_METHOD *PK11_DSA(void)
{
return(&pk11_dsa);
}
#endif
#ifndef OPENSSL_NO_DH
/* 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 */
};
{
return(&pk11_dh);
}
#endif
/* Size of an SSL signature: MD5+SHA1
*/
#define SSL_SIG_LENGTH 36
/* Lengths of DSA data and signature
*/
#define DSA_DATA_LEN 20
#define DSA_SIGNATURE_LEN 40
#ifndef OPENSSL_NO_RSA
/* Similiar 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
*/
{
int i,num=0,r= -1;
{
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 catched in the padding functions
*/
{
int i,num=0,r= -1;
{
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. Input errors are also checked here
*/
{
BIGNUM f;
int j,num=0,r= -1;
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;
}
/* 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. Input errors are also checked here
*/
{
BIGNUM f;
int i,num=0,r= -1;
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;
}
/* 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 pk11_RSA_public_encrypt_low(int flen,
{
int retval = -1;
char tmp_buf[20];
return -1;
if (h_pub_key == CK_INVALID_HANDLE)
if (h_pub_key != CK_INVALID_HANDLE)
{
{
return -1;
}
{
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 pk11_RSA_private_encrypt_low(int flen,
{
int retval = -1;
char tmp_buf[20];
return -1;
if (h_priv_key == CK_INVALID_HANDLE)
if (h_priv_key != CK_INVALID_HANDLE)
{
{
return -1;
}
{
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 pk11_RSA_private_decrypt_low(int flen,
{
int retval = -1;
char tmp_buf[20];
return -1;
if (h_priv_key == CK_INVALID_HANDLE)
if (h_priv_key != CK_INVALID_HANDLE)
{
{
return -1;
}
{
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 pk11_RSA_public_decrypt_low(int flen,
{
int retval = -1;
char tmp_buf[20];
return -1;
if (h_pub_key == CK_INVALID_HANDLE)
if (h_pub_key != CK_INVALID_HANDLE)
{
{
return -1;
}
{
return -1;
}
}
return retval;
}
{
/* This flag in the RSA_METHOD enables the new rsa_sign,
* rsa_verify functions. See rsa.h for details. */
return 1;
}
{
return pk11_destroy_rsa_key_objects(NULL);
}
/* 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 */
{
int i,j;
unsigned char *p,*s = NULL;
int ret = 0;
char tmp_buf[20];
/* 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;
i2d_X509_SIG(&sig,&p);
}
goto err;
if (h_priv_key == CK_INVALID_HANDLE)
if (h_priv_key != CK_INVALID_HANDLE)
{
{
goto err;
}
*siglen = j;
{
goto err;
}
ret = 1;
}
err:
if (type != NID_md5_sha1)
{
memset(s,0,(unsigned int)j+1);
OPENSSL_free(s);
}
return ret;
}
static int pk11_RSA_verify(int type, const unsigned char *m,
{
int i,j;
unsigned char *p,*s = NULL;
int ret = 0;
char tmp_buf[20];
/* 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;
i2d_X509_SIG(&sig,&p);
}
goto err;
if (h_pub_key == CK_INVALID_HANDLE)
if (h_pub_key != CK_INVALID_HANDLE)
{
{
goto err;
}
{
goto err;
}
ret = 1;
}
err:
if (type != NID_md5_sha1)
{
OPENSSL_free(s);
}
return ret;
}
{
return NULL;
{
if (pkey)
{
if (rsa)
{
sp);
if (h_priv_key == CK_INVALID_HANDLE)
{
}
}
else
{
}
}
}
return(pkey);
}
{
return NULL;
{
if (pkey)
{
if (rsa)
{
if (h_pub_key == CK_INVALID_HANDLE)
{
}
}
else
{
}
}
}
return(pkey);
}
/* Create a public key object in a session from a given rsa structure.
*/
{
char tmp_buf[20];
{
{CKA_TOKEN, &false, sizeof(true)},
{CKA_ENCRYPT, &true, sizeof(true)},
{CKA_VERIFY_RECOVER, &true, sizeof(true)},
{CKA_MODULUS, (void *)NULL, 0},
{CKA_PUBLIC_EXPONENT, (void *)NULL, 0}
};
int i;
{
goto err;
}
{
goto err;
}
{
goto err;
}
{
goto err;
}
{
goto err;
}
if (found == 0)
{
{
goto err;
}
}
err:
for (i = 5; i <= 6; i++)
{
{
}
}
return h_key;
}
/* Create a private key object in the session from a given rsa structure
*/
{
int i;
char tmp_buf[20];
/* Both CKA_TOKEN and CKA_SENSITIVE have to be FALSE for session keys
*/
{
{CKA_TOKEN, &false, sizeof(true)},
{CKA_SENSITIVE, &false, sizeof(true)},
{CKA_DECRYPT, &true, sizeof(true)},
{CKA_SIGN, &true, sizeof(true)},
{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 err;
}
{
goto err;
}
{
goto err;
}
{
goto err;
}
if (found == 0)
{
{
goto err;
}
}
err:
/* 6 to 13 entries in the key template are key components
* They need to be freed apon exit or error.
*/
for (i = 6; i <= 13; i++)
{
{
a_key_template[i].ulValueLen);
}
}
return h_key;
}
#endif
#ifndef OPENSSL_NO_DSA
/* The DSA function implementation
*/
{
return 1;
}
{
return pk11_destroy_dsa_key_objects(NULL);
}
static DSA_SIG *
{
int i;
/* The signature is the concatenation of r and s,
* each is 20 bytes long
*/
unsigned char sigret[DSA_SIGNATURE_LEN];
unsigned int siglen = DSA_SIGNATURE_LEN;
char tmp_buf[20];
{
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;
}
(CK_ULONG_PTR) &siglen);
{
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;
unsigned char sigbuf[DSA_SIGNATURE_LEN];
unsigned long siglen = DSA_SIGNATURE_LEN;
char tmp_buf[20];
{
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)
{
{
goto ret;
}
{
goto ret;
}
}
retval = 1;
ret:
return retval;
}
/* Create a public key object in a session from a given dsa structure.
*/
{
int i;
char tmp_buf[20];
{
{CKA_TOKEN, &false, sizeof(true)},
{CKA_VERIFY, &true, sizeof(true)},
};
{
goto err;
}
{
goto err;
}
{
goto err;
}
{
goto err;
}
if (found == 0)
{
{
goto err;
}
}
err:
for (i = 4; i <= 7; i++)
{
{
}
}
return h_key;
}
/* Create a private key object in the session from a given dsa structure
*/
{
int i;
char tmp_buf[20];
/* Both CKA_TOKEN and CKA_SENSITIVE have to be FALSE for session keys
*/
{
{CKA_TOKEN, &false, sizeof(true)},
{CKA_SENSITIVE, &false, sizeof(true)},
{CKA_SIGN, &true, sizeof(true)},
};
/* Put the private key components into the template
*/
{
goto err;
}
{
goto err;
}
{
goto err;
}
{
goto err;
}
if (found == 0)
{
{
goto err;
}
}
err:
/* 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;
}
#endif
#ifndef OPENSSL_NO_DH
/* The DH function implementation
*/
{
return 1;
}
{
return pk11_destroy_dh_key_objects(NULL);
}
{
CK_ULONG i;
int ret = 0;
char tmp_buf[20];
{
{CKA_PRIVATE, &false, sizeof(false)},
};
{
{CKA_PRIVATE, &false, sizeof(false)},
{CKA_SENSITIVE, &false, sizeof(false)},
{CKA_DERIVE, &true, sizeof(true)}
};
{
};
{
};
{
{
goto err;
}
}
else
goto err;
{
{
goto err;
}
}
else
goto err;
goto err;
&h_priv_key);
{
goto err;
}
/* Reuse the larger memory allocated. We know the larger memory
* is sufficient for reuse */
else
{
goto err;
}
{
goto err;
}
/* Reuse the memory allocated */
{
goto err;
}
{
}
/* Reuse the memory allocated */
{
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;
}
{
CK_ULONG i;
{
{CKA_VALUE_LEN, (void*) NULL, sizeof(i)},
};
{
};
int ret = 0;
char tmp_buf[20];
goto err;
{
goto err;
}
if (h_key == CK_INVALID_HANDLE)
if (h_key == CK_INVALID_HANDLE)
{
goto err;
}
i = mechanism.ulParameterLen;
{
goto err;
}
{
goto err;
}
{
goto err;
}
priv_key_result[0].pValue =
if (!priv_key_result[0].pValue)
{
goto err;
}
{
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 */
{
}
err:
if (h_derived_key != CK_INVALID_HANDLE)
{
{
}
}
if (priv_key_result[0].pValue)
{
}
if (mechanism.pParameter)
{
}
return ret;
}
{
int i;
char tmp_buf[20];
{
{CKA_DERIVE, &true, sizeof(true)},
{CKA_PRIVATE, &false, sizeof(false)},
};
{
goto err;
}
{
goto err;
}
{
goto err;
}
{
goto err;
}
{
goto err;
}
{
goto err;
}
if (found == 0)
{
{
goto err;
}
}
err:
for (i = 4; i <= 6; i++)
{
{
}
}
return h_key;
}
#endif
/* Local function to simplify key template population
* Return 0 -- error, 1 -- no error
*/
{
if (len == 0)
return 1;
*ul_value_len = len;
return 0;
return 1;
}
{
}
{
}
{
}
#endif
#endif