1 /* Author: Maurice Gittens <maurice@gittens.nl> */
2 /* ====================================================================
3 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * licensing@OpenSSL.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
57 #include <openssl/crypto.h>
58 /* #include <openssl/pem.h> */
60 #include <openssl/dso.h>
61 #include <openssl/x509.h>
62 #include <openssl/objects.h>
63 #include <openssl/engine.h>
66 #ifndef OPENSSL_NO_HW_4758_CCA
69 #include "hw_4758_cca.h"
71 #include "vendor_defns/hw_4758_cca.h"
74 #include "hw_4758_cca_err.c"
76 static int ibm_4758_cca_destroy(ENGINE *e);
77 static int ibm_4758_cca_init(ENGINE *e);
78 static int ibm_4758_cca_finish(ENGINE *e);
79 static int ibm_4758_cca_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
83 #ifndef OPENSSL_NO_RSA
84 static int cca_rsa_pub_enc(int flen, const unsigned char *from,
85 unsigned char *to, RSA *rsa,int padding);
86 static int cca_rsa_priv_dec(int flen, const unsigned char *from,
87 unsigned char *to, RSA *rsa,int padding);
88 static int cca_rsa_sign(int type, const unsigned char *m, unsigned int m_len,
89 unsigned char *sigret, unsigned int *siglen, const RSA *rsa);
90 static int cca_rsa_verify(int dtype, const unsigned char *m, unsigned int m_len,
91 unsigned char *sigbuf, unsigned int siglen, const RSA *rsa);
93 /* utility functions */
94 /*-----------------------*/
95 static EVP_PKEY *ibm_4758_load_privkey(ENGINE*, const char*,
96 UI_METHOD *ui_method, void *callback_data);
97 static EVP_PKEY *ibm_4758_load_pubkey(ENGINE*, const char*,
98 UI_METHOD *ui_method, void *callback_data);
100 static int getModulusAndExponent(const unsigned char *token, long *exponentLength,
101 unsigned char *exponent, long *modulusLength,
102 long *modulusFieldLength, unsigned char *modulus);
105 /* RAND number functions */
106 /*-----------------------*/
107 static int cca_get_random_bytes(unsigned char*, int );
108 static int cca_random_status(void);
110 static void cca_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
111 int idx,long argl, void *argp);
113 /* Function pointers for CCA verbs */
114 /*---------------------------------*/
115 #ifndef OPENSSL_NO_RSA
116 static F_KEYRECORDREAD keyRecordRead;
117 static F_DIGITALSIGNATUREGENERATE digitalSignatureGenerate;
118 static F_DIGITALSIGNATUREVERIFY digitalSignatureVerify;
119 static F_PUBLICKEYEXTRACT publicKeyExtract;
120 static F_PKAENCRYPT pkaEncrypt;
121 static F_PKADECRYPT pkaDecrypt;
123 static F_RANDOMNUMBERGENERATE randomNumberGenerate;
125 /* static variables */
126 /*------------------*/
127 static const char *CCA4758_LIB_NAME = NULL;
128 static const char *get_CCA4758_LIB_NAME(void)
131 return CCA4758_LIB_NAME;
134 static void free_CCA4758_LIB_NAME(void)
137 OPENSSL_free((void*)CCA4758_LIB_NAME);
138 CCA4758_LIB_NAME = NULL;
140 static long set_CCA4758_LIB_NAME(const char *name)
142 free_CCA4758_LIB_NAME();
143 return (((CCA4758_LIB_NAME = BUF_strdup(name)) != NULL) ? 1 : 0);
145 #ifndef OPENSSL_NO_RSA
146 static const char* n_keyRecordRead = CSNDKRR;
147 static const char* n_digitalSignatureGenerate = CSNDDSG;
148 static const char* n_digitalSignatureVerify = CSNDDSV;
149 static const char* n_publicKeyExtract = CSNDPKX;
150 static const char* n_pkaEncrypt = CSNDPKE;
151 static const char* n_pkaDecrypt = CSNDPKD;
153 static const char* n_randomNumberGenerate = CSNBRNG;
155 static int hndidx = -1;
156 static DSO *dso = NULL;
158 /* openssl engine initialization structures */
159 /*------------------------------------------*/
161 #define CCA4758_CMD_SO_PATH ENGINE_CMD_BASE
162 static const ENGINE_CMD_DEFN cca4758_cmd_defns[] = {
163 {CCA4758_CMD_SO_PATH,
165 "Specifies the path to the '4758cca' shared library",
166 ENGINE_CMD_FLAG_STRING},
170 #ifndef OPENSSL_NO_RSA
171 static RSA_METHOD ibm_4758_cca_rsa =
173 "IBM 4758 CCA RSA method",
178 NULL, /*rsa_mod_exp,*/
179 NULL, /*mod_exp_mont,*/
182 RSA_FLAG_SIGN_VER, /* flags */
184 cca_rsa_sign, /* rsa_sign */
185 cca_rsa_verify /* rsa_verify */
189 static RAND_METHOD ibm_4758_cca_rand =
191 /* "IBM 4758 RAND method", */
193 cca_get_random_bytes, /* get random bytes from the card */
196 cca_get_random_bytes, /* pseudo rand */
197 cca_random_status, /* status */
200 static const char *engine_4758_cca_id = "4758cca";
201 static const char *engine_4758_cca_name = "IBM 4758 CCA hardware engine support";
203 /* engine implementation */
204 /*-----------------------*/
205 static int bind_helper(ENGINE *e)
207 if(!ENGINE_set_id(e, engine_4758_cca_id) ||
208 !ENGINE_set_name(e, engine_4758_cca_name) ||
209 #ifndef OPENSSL_NO_RSA
210 !ENGINE_set_RSA(e, &ibm_4758_cca_rsa) ||
212 !ENGINE_set_RAND(e, &ibm_4758_cca_rand) ||
213 !ENGINE_set_destroy_function(e, ibm_4758_cca_destroy) ||
214 !ENGINE_set_init_function(e, ibm_4758_cca_init) ||
215 !ENGINE_set_finish_function(e, ibm_4758_cca_finish) ||
216 !ENGINE_set_ctrl_function(e, ibm_4758_cca_ctrl) ||
217 !ENGINE_set_load_privkey_function(e, ibm_4758_load_privkey) ||
218 !ENGINE_set_load_pubkey_function(e, ibm_4758_load_pubkey) ||
219 !ENGINE_set_cmd_defns(e, cca4758_cmd_defns))
221 /* Ensure the error handling is set up */
222 ERR_load_CCA4758_strings();
226 static ENGINE *engine_4758_cca(void)
228 ENGINE *ret = ENGINE_new();
231 if(!bind_helper(ret))
239 void ENGINE_load_4758cca(void)
241 ENGINE *e_4758 = engine_4758_cca();
248 static int ibm_4758_cca_destroy(ENGINE *e)
250 ERR_unload_CCA4758_strings();
251 free_CCA4758_LIB_NAME();
255 static int ibm_4758_cca_init(ENGINE *e)
259 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT,CCA4758_R_ALREADY_LOADED);
263 dso = DSO_load(NULL, get_CCA4758_LIB_NAME(), NULL, 0);
266 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT,CCA4758_R_DSO_FAILURE);
270 #ifndef OPENSSL_NO_RSA
271 if(!(keyRecordRead = (F_KEYRECORDREAD)
272 DSO_bind_func(dso, n_keyRecordRead)) ||
273 !(randomNumberGenerate = (F_RANDOMNUMBERGENERATE)
274 DSO_bind_func(dso, n_randomNumberGenerate)) ||
275 !(digitalSignatureGenerate = (F_DIGITALSIGNATUREGENERATE)
276 DSO_bind_func(dso, n_digitalSignatureGenerate)) ||
277 !(digitalSignatureVerify = (F_DIGITALSIGNATUREVERIFY)
278 DSO_bind_func(dso, n_digitalSignatureVerify)) ||
279 !(publicKeyExtract = (F_PUBLICKEYEXTRACT)
280 DSO_bind_func(dso, n_publicKeyExtract)) ||
281 !(pkaEncrypt = (F_PKAENCRYPT)
282 DSO_bind_func(dso, n_pkaEncrypt)) ||
283 !(pkaDecrypt = (F_PKADECRYPT)
284 DSO_bind_func(dso, n_pkaDecrypt)))
286 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT,CCA4758_R_DSO_FAILURE);
290 if(!(randomNumberGenerate = (F_RANDOMNUMBERGENERATE)
291 DSO_bind_func(dso, n_randomNumberGenerate)))
293 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT,CCA4758_R_DSO_FAILURE);
298 hndidx = RSA_get_ex_new_index(0, "IBM 4758 CCA RSA key handle",
299 NULL, NULL, cca_ex_free);
307 keyRecordRead = (F_KEYRECORDREAD)0;
308 randomNumberGenerate = (F_RANDOMNUMBERGENERATE)0;
309 digitalSignatureGenerate = (F_DIGITALSIGNATUREGENERATE)0;
310 digitalSignatureVerify = (F_DIGITALSIGNATUREVERIFY)0;
311 publicKeyExtract = (F_PUBLICKEYEXTRACT)0;
312 pkaEncrypt = (F_PKAENCRYPT)0;
313 pkaDecrypt = (F_PKADECRYPT)0;
317 static int ibm_4758_cca_finish(ENGINE *e)
319 free_CCA4758_LIB_NAME();
322 CCA4758err(CCA4758_F_IBM_4758_CCA_FINISH,
323 CCA4758_R_NOT_LOADED);
328 CCA4758err(CCA4758_F_IBM_4758_CCA_FINISH,
329 CCA4758_R_UNIT_FAILURE);
333 keyRecordRead = (F_KEYRECORDREAD)0;
334 randomNumberGenerate = (F_RANDOMNUMBERGENERATE)0;
335 digitalSignatureGenerate = (F_DIGITALSIGNATUREGENERATE)0;
336 digitalSignatureVerify = (F_DIGITALSIGNATUREVERIFY)0;
337 publicKeyExtract = (F_PUBLICKEYEXTRACT)0;
338 pkaEncrypt = (F_PKAENCRYPT)0;
339 pkaDecrypt = (F_PKADECRYPT)0;
343 static int ibm_4758_cca_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
345 int initialised = ((dso == NULL) ? 0 : 1);
348 case CCA4758_CMD_SO_PATH:
351 CCA4758err(CCA4758_F_IBM_4758_CCA_CTRL,
352 ERR_R_PASSED_NULL_PARAMETER);
357 CCA4758err(CCA4758_F_IBM_4758_CCA_CTRL,
358 CCA4758_R_ALREADY_LOADED);
361 return set_CCA4758_LIB_NAME((const char *)p);
365 CCA4758err(CCA4758_F_IBM_4758_CCA_CTRL,
366 CCA4758_R_COMMAND_NOT_IMPLEMENTED);
370 #ifndef OPENSSL_NO_RSA
372 #define MAX_CCA_PKA_TOKEN_SIZE 2500
374 static EVP_PKEY *ibm_4758_load_privkey(ENGINE* e, const char* key_id,
375 UI_METHOD *ui_method, void *callback_data)
378 EVP_PKEY *res = NULL;
379 unsigned char* keyToken = NULL;
380 unsigned char pubKeyToken[MAX_CCA_PKA_TOKEN_SIZE];
381 long pubKeyTokenLength = MAX_CCA_PKA_TOKEN_SIZE;
382 long keyTokenLength = MAX_CCA_PKA_TOKEN_SIZE;
385 long exitDataLength = 0;
386 long ruleArrayLength = 0;
387 unsigned char exitData[8];
388 unsigned char ruleArray[8];
389 unsigned char keyLabel[64];
390 long keyLabelLength = strlen(key_id);
391 unsigned char modulus[256];
392 long modulusFieldLength = sizeof(modulus);
393 long modulusLength = 0;
394 unsigned char exponent[256];
395 long exponentLength = sizeof(exponent);
397 if (keyLabelLength > sizeof(keyLabel))
399 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
400 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
404 memset(keyLabel,' ', sizeof(keyLabel));
405 memcpy(keyLabel, key_id, keyLabelLength);
407 keyToken = OPENSSL_malloc(MAX_CCA_PKA_TOKEN_SIZE + sizeof(long));
410 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
411 ERR_R_MALLOC_FAILURE);
415 keyRecordRead(&returnCode, &reasonCode, &exitDataLength,
416 exitData, &ruleArrayLength, ruleArray, keyLabel,
417 &keyTokenLength, keyToken+sizeof(long));
421 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
422 CCA4758_R_FAILED_LOADING_PRIVATE_KEY);
426 publicKeyExtract(&returnCode, &reasonCode, &exitDataLength,
427 exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
428 keyToken+sizeof(long), &pubKeyTokenLength, pubKeyToken);
432 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
433 CCA4758_R_FAILED_LOADING_PRIVATE_KEY);
437 if (!getModulusAndExponent(pubKeyToken, &exponentLength,
438 exponent, &modulusLength, &modulusFieldLength,
441 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
442 CCA4758_R_FAILED_LOADING_PRIVATE_KEY);
446 (*(long*)keyToken) = keyTokenLength;
447 rtmp = RSA_new_method(e);
448 RSA_set_ex_data(rtmp, hndidx, (char *)keyToken);
450 rtmp->e = BN_bin2bn(exponent, exponentLength, NULL);
451 rtmp->n = BN_bin2bn(modulus, modulusFieldLength, NULL);
452 rtmp->flags |= RSA_FLAG_EXT_PKEY;
454 res = EVP_PKEY_new();
455 EVP_PKEY_assign_RSA(res, rtmp);
460 OPENSSL_free(keyToken);
468 static EVP_PKEY *ibm_4758_load_pubkey(ENGINE* e, const char* key_id,
469 UI_METHOD *ui_method, void *callback_data)
472 EVP_PKEY *res = NULL;
473 unsigned char* keyToken = NULL;
474 long keyTokenLength = MAX_CCA_PKA_TOKEN_SIZE;
477 long exitDataLength = 0;
478 long ruleArrayLength = 0;
479 unsigned char exitData[8];
480 unsigned char ruleArray[8];
481 unsigned char keyLabel[64];
482 long keyLabelLength = strlen(key_id);
483 unsigned char modulus[512];
484 long modulusFieldLength = sizeof(modulus);
485 long modulusLength = 0;
486 unsigned char exponent[512];
487 long exponentLength = sizeof(exponent);
489 if (keyLabelLength > sizeof(keyLabel))
491 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
492 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
496 memset(keyLabel,' ', sizeof(keyLabel));
497 memcpy(keyLabel, key_id, keyLabelLength);
499 keyToken = OPENSSL_malloc(MAX_CCA_PKA_TOKEN_SIZE + sizeof(long));
502 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PUBKEY,
503 ERR_R_MALLOC_FAILURE);
507 keyRecordRead(&returnCode, &reasonCode, &exitDataLength, exitData,
508 &ruleArrayLength, ruleArray, keyLabel, &keyTokenLength,
509 keyToken+sizeof(long));
513 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
514 ERR_R_MALLOC_FAILURE);
518 if (!getModulusAndExponent(keyToken+sizeof(long), &exponentLength,
519 exponent, &modulusLength, &modulusFieldLength, modulus))
521 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
522 CCA4758_R_FAILED_LOADING_PUBLIC_KEY);
526 (*(long*)keyToken) = keyTokenLength;
527 rtmp = RSA_new_method(e);
528 RSA_set_ex_data(rtmp, hndidx, (char *)keyToken);
529 rtmp->e = BN_bin2bn(exponent, exponentLength, NULL);
530 rtmp->n = BN_bin2bn(modulus, modulusFieldLength, NULL);
531 rtmp->flags |= RSA_FLAG_EXT_PKEY;
532 res = EVP_PKEY_new();
533 EVP_PKEY_assign_RSA(res, rtmp);
538 OPENSSL_free(keyToken);
546 static int cca_rsa_pub_enc(int flen, const unsigned char *from,
547 unsigned char *to, RSA *rsa,int padding)
552 long exitDataLength = 0;
553 unsigned char exitData[8];
554 long ruleArrayLength = 1;
555 unsigned char ruleArray[8] = "PKCS-1.2";
556 long dataStructureLength = 0;
557 unsigned char dataStructure[8];
558 long outputLength = RSA_size(rsa);
560 unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
562 keyTokenLength = *(long*)keyToken;
563 keyToken+=sizeof(long);
565 pkaEncrypt(&returnCode, &reasonCode, &exitDataLength, exitData,
566 &ruleArrayLength, ruleArray, &lflen, (unsigned char*)from,
567 &dataStructureLength, dataStructure, &keyTokenLength,
568 keyToken, &outputLength, to);
570 if (returnCode || reasonCode)
571 return -(returnCode << 16 | reasonCode);
575 static int cca_rsa_priv_dec(int flen, const unsigned char *from,
576 unsigned char *to, RSA *rsa,int padding)
581 long exitDataLength = 0;
582 unsigned char exitData[8];
583 long ruleArrayLength = 1;
584 unsigned char ruleArray[8] = "PKCS-1.2";
585 long dataStructureLength = 0;
586 unsigned char dataStructure[8];
587 long outputLength = RSA_size(rsa);
589 unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
591 keyTokenLength = *(long*)keyToken;
592 keyToken+=sizeof(long);
594 pkaDecrypt(&returnCode, &reasonCode, &exitDataLength, exitData,
595 &ruleArrayLength, ruleArray, &lflen, (unsigned char*)from,
596 &dataStructureLength, dataStructure, &keyTokenLength,
597 keyToken, &outputLength, to);
599 return (returnCode | reasonCode) ? 0 : 1;
602 #define SSL_SIG_LEN 36
604 static int cca_rsa_verify(int type, const unsigned char *m, unsigned int m_len,
605 unsigned char *sigbuf, unsigned int siglen, const RSA *rsa)
609 long lsiglen = siglen;
610 long exitDataLength = 0;
611 unsigned char exitData[8];
612 long ruleArrayLength = 1;
613 unsigned char ruleArray[8] = "PKCS-1.1";
615 unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
616 long length = SSL_SIG_LEN;
618 unsigned char *hashBuffer = NULL;
621 X509_ALGOR algorithm;
622 ASN1_OCTET_STRING digest;
624 keyTokenLength = *(long*)keyToken;
625 keyToken+=sizeof(long);
627 if (type == NID_md5 || type == NID_sha1)
629 sig.algor = &algorithm;
630 algorithm.algorithm = OBJ_nid2obj(type);
632 if (!algorithm.algorithm)
634 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
635 CCA4758_R_UNKNOWN_ALGORITHM_TYPE);
639 if (!algorithm.algorithm->length)
641 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
642 CCA4758_R_ASN1_OID_UNKNOWN_FOR_MD);
646 parameter.type = V_ASN1_NULL;
647 parameter.value.ptr = NULL;
648 algorithm.parameter = ¶meter;
650 sig.digest = &digest;
651 sig.digest->data = (unsigned char*)m;
652 sig.digest->length = m_len;
654 length = i2d_X509_SIG(&sig, NULL);
657 keyLength = RSA_size(rsa);
659 if (length - RSA_PKCS1_PADDING > keyLength)
661 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
662 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
669 if (m_len != SSL_SIG_LEN)
671 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
672 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
676 hashBuffer = (unsigned char *)m;
682 ptr = hashBuffer = OPENSSL_malloc(
683 (unsigned int)keyLength+1);
686 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
687 ERR_R_MALLOC_FAILURE);
691 i2d_X509_SIG(&sig, &ptr);
697 ptr = hashBuffer = OPENSSL_malloc(
698 (unsigned int)keyLength+1);
701 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
702 ERR_R_MALLOC_FAILURE);
705 i2d_X509_SIG(&sig, &ptr);
712 digitalSignatureVerify(&returnCode, &reasonCode, &exitDataLength,
713 exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
714 keyToken, &length, hashBuffer, &lsiglen, sigbuf);
716 if (type == NID_sha1 || type == NID_md5)
718 memset(hashBuffer, keyLength+1, 0);
719 OPENSSL_free(hashBuffer);
722 return ((returnCode || reasonCode) ? 0 : 1);
725 #define SSL_SIG_LEN 36
727 static int cca_rsa_sign(int type, const unsigned char *m, unsigned int m_len,
728 unsigned char *sigret, unsigned int *siglen, const RSA *rsa)
732 long exitDataLength = 0;
733 unsigned char exitData[8];
734 long ruleArrayLength = 1;
735 unsigned char ruleArray[8] = "PKCS-1.1";
736 long outputLength=256;
737 long outputBitLength;
739 unsigned char *hashBuffer = NULL;
740 unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
741 long length = SSL_SIG_LEN;
745 X509_ALGOR algorithm;
746 ASN1_OCTET_STRING digest;
748 keyTokenLength = *(long*)keyToken;
749 keyToken+=sizeof(long);
751 if (type == NID_md5 || type == NID_sha1)
753 sig.algor = &algorithm;
754 algorithm.algorithm = OBJ_nid2obj(type);
756 if (!algorithm.algorithm)
758 CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,
759 CCA4758_R_UNKNOWN_ALGORITHM_TYPE);
763 if (!algorithm.algorithm->length)
765 CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,
766 CCA4758_R_ASN1_OID_UNKNOWN_FOR_MD);
770 parameter.type = V_ASN1_NULL;
771 parameter.value.ptr = NULL;
772 algorithm.parameter = ¶meter;
774 sig.digest = &digest;
775 sig.digest->data = (unsigned char*)m;
776 sig.digest->length = m_len;
778 length = i2d_X509_SIG(&sig, NULL);
781 keyLength = RSA_size(rsa);
783 if (length - RSA_PKCS1_PADDING > keyLength)
785 CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,
786 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
793 if (m_len != SSL_SIG_LEN)
795 CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,
796 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
799 hashBuffer = (unsigned char*)m;
805 ptr = hashBuffer = OPENSSL_malloc(
806 (unsigned int)keyLength+1);
809 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
810 ERR_R_MALLOC_FAILURE);
813 i2d_X509_SIG(&sig, &ptr);
819 ptr = hashBuffer = OPENSSL_malloc(
820 (unsigned int)keyLength+1);
823 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
824 ERR_R_MALLOC_FAILURE);
827 i2d_X509_SIG(&sig, &ptr);
834 digitalSignatureGenerate(&returnCode, &reasonCode, &exitDataLength,
835 exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
836 keyToken, &length, hashBuffer, &outputLength, &outputBitLength,
839 if (type == NID_sha1 || type == NID_md5)
841 memset(hashBuffer, keyLength+1, 0);
842 OPENSSL_free(hashBuffer);
845 *siglen = outputLength;
847 return ((returnCode || reasonCode) ? 0 : 1);
850 static int getModulusAndExponent(const unsigned char*token, long *exponentLength,
851 unsigned char *exponent, long *modulusLength, long *modulusFieldLength,
852 unsigned char *modulus)
856 if (*token++ != (char)0x1E) /* internal PKA token? */
859 if (*token++) /* token version must be zero */
864 len |= (unsigned char)*token++;
866 token += 4; /* skip reserved bytes */
868 if (*token++ == (char)0x04)
870 if (*token++) /* token version must be zero */
875 len |= (unsigned char)*token++;
877 token+=2; /* skip reserved section */
881 len |= (unsigned char)*token++;
883 *exponentLength = len;
887 len |= (unsigned char)*token++;
889 *modulusLength = len;
893 len |= (unsigned char)*token++;
895 *modulusFieldLength = len;
897 memcpy(exponent, token, *exponentLength);
898 token+= *exponentLength;
900 memcpy(modulus, token, *modulusFieldLength);
906 #endif /* OPENSSL_NO_RSA */
908 static int cca_random_status(void)
913 static int cca_get_random_bytes(unsigned char* buf, int num)
917 long exit_data_length;
918 unsigned char exit_data[4];
919 unsigned char form[] = "RANDOM ";
920 unsigned char rand_buf[8];
922 while(num >= sizeof(rand_buf))
924 randomNumberGenerate(&ret_code, &reason_code, &exit_data_length,
925 exit_data, form, rand_buf);
928 num -= sizeof(rand_buf);
929 memcpy(buf, rand_buf, sizeof(rand_buf));
930 buf += sizeof(rand_buf);
935 randomNumberGenerate(&ret_code, &reason_code, NULL, NULL,
939 memcpy(buf, rand_buf, num);
945 static void cca_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad, int idx,
946 long argl, void *argp)
952 /* Goo to handle building as a dynamic engine */
953 #ifdef ENGINE_DYNAMIC_SUPPORT
954 static int bind_fn(ENGINE *e, const char *id)
956 if(id && (strcmp(id, engine_4758_cca_id) != 0))
962 IMPLEMENT_DYNAMIC_CHECK_FN()
963 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
964 #endif /* ENGINE_DYNAMIC_SUPPORT */
966 #endif /* !OPENSSL_NO_HW_4758_CCA */
967 #endif /* !OPENSSL_NO_HW */