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> */
59 #include <openssl/dso.h>
60 #include <openssl/x509.h>
61 #include <openssl/objects.h>
62 #include <openssl/engine.h>
65 #ifndef OPENSSL_NO_HW_4758_CCA
68 #include "hw_4758_cca.h"
70 #include "vendor_defns/hw_4758_cca.h"
73 #include "e_4758_cca_err.c"
75 static int ibm_4758_cca_destroy(ENGINE *e);
76 static int ibm_4758_cca_init(ENGINE *e);
77 static int ibm_4758_cca_finish(ENGINE *e);
78 static int ibm_4758_cca_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
82 #ifndef OPENSSL_NO_RSA
83 static int cca_rsa_pub_enc(int flen, const unsigned char *from,
84 unsigned char *to, RSA *rsa,int padding);
85 static int cca_rsa_priv_dec(int flen, const unsigned char *from,
86 unsigned char *to, RSA *rsa,int padding);
87 static int cca_rsa_sign(int type, const unsigned char *m, unsigned int m_len,
88 unsigned char *sigret, unsigned int *siglen, const RSA *rsa);
89 static int cca_rsa_verify(int dtype, const unsigned char *m, unsigned int m_len,
90 unsigned char *sigbuf, unsigned int siglen, const RSA *rsa);
92 /* utility functions */
93 /*-----------------------*/
94 static EVP_PKEY *ibm_4758_load_privkey(ENGINE*, const char*,
95 UI_METHOD *ui_method, void *callback_data);
96 static EVP_PKEY *ibm_4758_load_pubkey(ENGINE*, const char*,
97 UI_METHOD *ui_method, void *callback_data);
99 static int getModulusAndExponent(const unsigned char *token, long *exponentLength,
100 unsigned char *exponent, long *modulusLength,
101 long *modulusFieldLength, unsigned char *modulus);
104 /* RAND number functions */
105 /*-----------------------*/
106 static int cca_get_random_bytes(unsigned char*, int );
107 static int cca_random_status(void);
109 static void cca_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
110 int idx,long argl, void *argp);
112 /* Function pointers for CCA verbs */
113 /*---------------------------------*/
114 #ifndef OPENSSL_NO_RSA
115 static F_KEYRECORDREAD keyRecordRead;
116 static F_DIGITALSIGNATUREGENERATE digitalSignatureGenerate;
117 static F_DIGITALSIGNATUREVERIFY digitalSignatureVerify;
118 static F_PUBLICKEYEXTRACT publicKeyExtract;
119 static F_PKAENCRYPT pkaEncrypt;
120 static F_PKADECRYPT pkaDecrypt;
122 static F_RANDOMNUMBERGENERATE randomNumberGenerate;
124 /* static variables */
125 /*------------------*/
126 static const char *CCA4758_LIB_NAME = NULL;
127 static const char *get_CCA4758_LIB_NAME(void)
130 return CCA4758_LIB_NAME;
133 static void free_CCA4758_LIB_NAME(void)
136 OPENSSL_free((void*)CCA4758_LIB_NAME);
137 CCA4758_LIB_NAME = NULL;
139 static long set_CCA4758_LIB_NAME(const char *name)
141 free_CCA4758_LIB_NAME();
142 return (((CCA4758_LIB_NAME = BUF_strdup(name)) != NULL) ? 1 : 0);
144 #ifndef OPENSSL_NO_RSA
145 static const char* n_keyRecordRead = CSNDKRR;
146 static const char* n_digitalSignatureGenerate = CSNDDSG;
147 static const char* n_digitalSignatureVerify = CSNDDSV;
148 static const char* n_publicKeyExtract = CSNDPKX;
149 static const char* n_pkaEncrypt = CSNDPKE;
150 static const char* n_pkaDecrypt = CSNDPKD;
152 static const char* n_randomNumberGenerate = CSNBRNG;
154 static int hndidx = -1;
155 static DSO *dso = NULL;
157 /* openssl engine initialization structures */
158 /*------------------------------------------*/
160 #define CCA4758_CMD_SO_PATH ENGINE_CMD_BASE
161 static const ENGINE_CMD_DEFN cca4758_cmd_defns[] = {
162 {CCA4758_CMD_SO_PATH,
164 "Specifies the path to the '4758cca' shared library",
165 ENGINE_CMD_FLAG_STRING},
169 #ifndef OPENSSL_NO_RSA
170 static RSA_METHOD ibm_4758_cca_rsa =
172 "IBM 4758 CCA RSA method",
177 NULL, /*rsa_mod_exp,*/
178 NULL, /*mod_exp_mont,*/
181 RSA_FLAG_SIGN_VER, /* flags */
183 cca_rsa_sign, /* rsa_sign */
184 cca_rsa_verify /* rsa_verify */
188 static RAND_METHOD ibm_4758_cca_rand =
190 /* "IBM 4758 RAND method", */
192 cca_get_random_bytes, /* get random bytes from the card */
195 cca_get_random_bytes, /* pseudo rand */
196 cca_random_status, /* status */
199 static const char *engine_4758_cca_id = "4758cca";
200 static const char *engine_4758_cca_name = "IBM 4758 CCA hardware engine support";
202 /* engine implementation */
203 /*-----------------------*/
204 static int bind_helper(ENGINE *e)
206 if(!ENGINE_set_id(e, engine_4758_cca_id) ||
207 !ENGINE_set_name(e, engine_4758_cca_name) ||
208 #ifndef OPENSSL_NO_RSA
209 !ENGINE_set_RSA(e, &ibm_4758_cca_rsa) ||
211 !ENGINE_set_RAND(e, &ibm_4758_cca_rand) ||
212 !ENGINE_set_destroy_function(e, ibm_4758_cca_destroy) ||
213 !ENGINE_set_init_function(e, ibm_4758_cca_init) ||
214 !ENGINE_set_finish_function(e, ibm_4758_cca_finish) ||
215 !ENGINE_set_ctrl_function(e, ibm_4758_cca_ctrl) ||
216 !ENGINE_set_load_privkey_function(e, ibm_4758_load_privkey) ||
217 !ENGINE_set_load_pubkey_function(e, ibm_4758_load_pubkey) ||
218 !ENGINE_set_cmd_defns(e, cca4758_cmd_defns))
220 /* Ensure the error handling is set up */
221 ERR_load_CCA4758_strings();
225 static ENGINE *engine_4758_cca(void)
227 ENGINE *ret = ENGINE_new();
230 if(!bind_helper(ret))
238 void ENGINE_load_4758cca(void)
240 ENGINE *e_4758 = engine_4758_cca();
247 static int ibm_4758_cca_destroy(ENGINE *e)
249 ERR_unload_CCA4758_strings();
250 free_CCA4758_LIB_NAME();
254 static int ibm_4758_cca_init(ENGINE *e)
258 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT,CCA4758_R_ALREADY_LOADED);
262 dso = DSO_load(NULL, get_CCA4758_LIB_NAME(), NULL, 0);
265 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT,CCA4758_R_DSO_FAILURE);
269 #ifndef OPENSSL_NO_RSA
270 if(!(keyRecordRead = (F_KEYRECORDREAD)
271 DSO_bind_func(dso, n_keyRecordRead)) ||
272 !(randomNumberGenerate = (F_RANDOMNUMBERGENERATE)
273 DSO_bind_func(dso, n_randomNumberGenerate)) ||
274 !(digitalSignatureGenerate = (F_DIGITALSIGNATUREGENERATE)
275 DSO_bind_func(dso, n_digitalSignatureGenerate)) ||
276 !(digitalSignatureVerify = (F_DIGITALSIGNATUREVERIFY)
277 DSO_bind_func(dso, n_digitalSignatureVerify)) ||
278 !(publicKeyExtract = (F_PUBLICKEYEXTRACT)
279 DSO_bind_func(dso, n_publicKeyExtract)) ||
280 !(pkaEncrypt = (F_PKAENCRYPT)
281 DSO_bind_func(dso, n_pkaEncrypt)) ||
282 !(pkaDecrypt = (F_PKADECRYPT)
283 DSO_bind_func(dso, n_pkaDecrypt)))
285 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT,CCA4758_R_DSO_FAILURE);
289 if(!(randomNumberGenerate = (F_RANDOMNUMBERGENERATE)
290 DSO_bind_func(dso, n_randomNumberGenerate)))
292 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT,CCA4758_R_DSO_FAILURE);
297 hndidx = RSA_get_ex_new_index(0, "IBM 4758 CCA RSA key handle",
298 NULL, NULL, cca_ex_free);
306 keyRecordRead = (F_KEYRECORDREAD)0;
307 randomNumberGenerate = (F_RANDOMNUMBERGENERATE)0;
308 digitalSignatureGenerate = (F_DIGITALSIGNATUREGENERATE)0;
309 digitalSignatureVerify = (F_DIGITALSIGNATUREVERIFY)0;
310 publicKeyExtract = (F_PUBLICKEYEXTRACT)0;
311 pkaEncrypt = (F_PKAENCRYPT)0;
312 pkaDecrypt = (F_PKADECRYPT)0;
316 static int ibm_4758_cca_finish(ENGINE *e)
318 free_CCA4758_LIB_NAME();
321 CCA4758err(CCA4758_F_IBM_4758_CCA_FINISH,
322 CCA4758_R_NOT_LOADED);
327 CCA4758err(CCA4758_F_IBM_4758_CCA_FINISH,
328 CCA4758_R_UNIT_FAILURE);
332 keyRecordRead = (F_KEYRECORDREAD)0;
333 randomNumberGenerate = (F_RANDOMNUMBERGENERATE)0;
334 digitalSignatureGenerate = (F_DIGITALSIGNATUREGENERATE)0;
335 digitalSignatureVerify = (F_DIGITALSIGNATUREVERIFY)0;
336 publicKeyExtract = (F_PUBLICKEYEXTRACT)0;
337 pkaEncrypt = (F_PKAENCRYPT)0;
338 pkaDecrypt = (F_PKADECRYPT)0;
342 static int ibm_4758_cca_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
344 int initialised = ((dso == NULL) ? 0 : 1);
347 case CCA4758_CMD_SO_PATH:
350 CCA4758err(CCA4758_F_IBM_4758_CCA_CTRL,
351 ERR_R_PASSED_NULL_PARAMETER);
356 CCA4758err(CCA4758_F_IBM_4758_CCA_CTRL,
357 CCA4758_R_ALREADY_LOADED);
360 return set_CCA4758_LIB_NAME((const char *)p);
364 CCA4758err(CCA4758_F_IBM_4758_CCA_CTRL,
365 CCA4758_R_COMMAND_NOT_IMPLEMENTED);
369 #ifndef OPENSSL_NO_RSA
371 #define MAX_CCA_PKA_TOKEN_SIZE 2500
373 static EVP_PKEY *ibm_4758_load_privkey(ENGINE* e, const char* key_id,
374 UI_METHOD *ui_method, void *callback_data)
377 EVP_PKEY *res = NULL;
378 unsigned char* keyToken = NULL;
379 unsigned char pubKeyToken[MAX_CCA_PKA_TOKEN_SIZE];
380 long pubKeyTokenLength = MAX_CCA_PKA_TOKEN_SIZE;
381 long keyTokenLength = MAX_CCA_PKA_TOKEN_SIZE;
384 long exitDataLength = 0;
385 long ruleArrayLength = 0;
386 unsigned char exitData[8];
387 unsigned char ruleArray[8];
388 unsigned char keyLabel[64];
389 long keyLabelLength = strlen(key_id);
390 unsigned char modulus[256];
391 long modulusFieldLength = sizeof(modulus);
392 long modulusLength = 0;
393 unsigned char exponent[256];
394 long exponentLength = sizeof(exponent);
396 if (keyLabelLength > sizeof(keyLabel))
398 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
399 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
403 memset(keyLabel,' ', sizeof(keyLabel));
404 memcpy(keyLabel, key_id, keyLabelLength);
406 keyToken = OPENSSL_malloc(MAX_CCA_PKA_TOKEN_SIZE + sizeof(long));
409 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
410 ERR_R_MALLOC_FAILURE);
414 keyRecordRead(&returnCode, &reasonCode, &exitDataLength,
415 exitData, &ruleArrayLength, ruleArray, keyLabel,
416 &keyTokenLength, keyToken+sizeof(long));
420 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
421 CCA4758_R_FAILED_LOADING_PRIVATE_KEY);
425 publicKeyExtract(&returnCode, &reasonCode, &exitDataLength,
426 exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
427 keyToken+sizeof(long), &pubKeyTokenLength, pubKeyToken);
431 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
432 CCA4758_R_FAILED_LOADING_PRIVATE_KEY);
436 if (!getModulusAndExponent(pubKeyToken, &exponentLength,
437 exponent, &modulusLength, &modulusFieldLength,
440 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
441 CCA4758_R_FAILED_LOADING_PRIVATE_KEY);
445 (*(long*)keyToken) = keyTokenLength;
446 rtmp = RSA_new_method(e);
447 RSA_set_ex_data(rtmp, hndidx, (char *)keyToken);
449 rtmp->e = BN_bin2bn(exponent, exponentLength, NULL);
450 rtmp->n = BN_bin2bn(modulus, modulusFieldLength, NULL);
451 rtmp->flags |= RSA_FLAG_EXT_PKEY;
453 res = EVP_PKEY_new();
454 EVP_PKEY_assign_RSA(res, rtmp);
459 OPENSSL_free(keyToken);
467 static EVP_PKEY *ibm_4758_load_pubkey(ENGINE* e, const char* key_id,
468 UI_METHOD *ui_method, void *callback_data)
471 EVP_PKEY *res = NULL;
472 unsigned char* keyToken = NULL;
473 long keyTokenLength = MAX_CCA_PKA_TOKEN_SIZE;
476 long exitDataLength = 0;
477 long ruleArrayLength = 0;
478 unsigned char exitData[8];
479 unsigned char ruleArray[8];
480 unsigned char keyLabel[64];
481 long keyLabelLength = strlen(key_id);
482 unsigned char modulus[512];
483 long modulusFieldLength = sizeof(modulus);
484 long modulusLength = 0;
485 unsigned char exponent[512];
486 long exponentLength = sizeof(exponent);
488 if (keyLabelLength > sizeof(keyLabel))
490 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
491 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
495 memset(keyLabel,' ', sizeof(keyLabel));
496 memcpy(keyLabel, key_id, keyLabelLength);
498 keyToken = OPENSSL_malloc(MAX_CCA_PKA_TOKEN_SIZE + sizeof(long));
501 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PUBKEY,
502 ERR_R_MALLOC_FAILURE);
506 keyRecordRead(&returnCode, &reasonCode, &exitDataLength, exitData,
507 &ruleArrayLength, ruleArray, keyLabel, &keyTokenLength,
508 keyToken+sizeof(long));
512 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
513 ERR_R_MALLOC_FAILURE);
517 if (!getModulusAndExponent(keyToken+sizeof(long), &exponentLength,
518 exponent, &modulusLength, &modulusFieldLength, modulus))
520 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
521 CCA4758_R_FAILED_LOADING_PUBLIC_KEY);
525 (*(long*)keyToken) = keyTokenLength;
526 rtmp = RSA_new_method(e);
527 RSA_set_ex_data(rtmp, hndidx, (char *)keyToken);
528 rtmp->e = BN_bin2bn(exponent, exponentLength, NULL);
529 rtmp->n = BN_bin2bn(modulus, modulusFieldLength, NULL);
530 rtmp->flags |= RSA_FLAG_EXT_PKEY;
531 res = EVP_PKEY_new();
532 EVP_PKEY_assign_RSA(res, rtmp);
537 OPENSSL_free(keyToken);
545 static int cca_rsa_pub_enc(int flen, const unsigned char *from,
546 unsigned char *to, RSA *rsa,int padding)
551 long exitDataLength = 0;
552 unsigned char exitData[8];
553 long ruleArrayLength = 1;
554 unsigned char ruleArray[8] = "PKCS-1.2";
555 long dataStructureLength = 0;
556 unsigned char dataStructure[8];
557 long outputLength = RSA_size(rsa);
559 unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
561 keyTokenLength = *(long*)keyToken;
562 keyToken+=sizeof(long);
564 pkaEncrypt(&returnCode, &reasonCode, &exitDataLength, exitData,
565 &ruleArrayLength, ruleArray, &lflen, (unsigned char*)from,
566 &dataStructureLength, dataStructure, &keyTokenLength,
567 keyToken, &outputLength, to);
569 if (returnCode || reasonCode)
570 return -(returnCode << 16 | reasonCode);
574 static int cca_rsa_priv_dec(int flen, const unsigned char *from,
575 unsigned char *to, RSA *rsa,int padding)
580 long exitDataLength = 0;
581 unsigned char exitData[8];
582 long ruleArrayLength = 1;
583 unsigned char ruleArray[8] = "PKCS-1.2";
584 long dataStructureLength = 0;
585 unsigned char dataStructure[8];
586 long outputLength = RSA_size(rsa);
588 unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
590 keyTokenLength = *(long*)keyToken;
591 keyToken+=sizeof(long);
593 pkaDecrypt(&returnCode, &reasonCode, &exitDataLength, exitData,
594 &ruleArrayLength, ruleArray, &lflen, (unsigned char*)from,
595 &dataStructureLength, dataStructure, &keyTokenLength,
596 keyToken, &outputLength, to);
598 return (returnCode | reasonCode) ? 0 : 1;
601 #define SSL_SIG_LEN 36
603 static int cca_rsa_verify(int type, const unsigned char *m, unsigned int m_len,
604 unsigned char *sigbuf, unsigned int siglen, const RSA *rsa)
608 long lsiglen = siglen;
609 long exitDataLength = 0;
610 unsigned char exitData[8];
611 long ruleArrayLength = 1;
612 unsigned char ruleArray[8] = "PKCS-1.1";
614 unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
615 long length = SSL_SIG_LEN;
617 unsigned char *hashBuffer = NULL;
620 X509_ALGOR algorithm;
621 ASN1_OCTET_STRING digest;
623 keyTokenLength = *(long*)keyToken;
624 keyToken+=sizeof(long);
626 if (type == NID_md5 || type == NID_sha1)
628 sig.algor = &algorithm;
629 algorithm.algorithm = OBJ_nid2obj(type);
631 if (!algorithm.algorithm)
633 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
634 CCA4758_R_UNKNOWN_ALGORITHM_TYPE);
638 if (!algorithm.algorithm->length)
640 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
641 CCA4758_R_ASN1_OID_UNKNOWN_FOR_MD);
645 parameter.type = V_ASN1_NULL;
646 parameter.value.ptr = NULL;
647 algorithm.parameter = ¶meter;
649 sig.digest = &digest;
650 sig.digest->data = (unsigned char*)m;
651 sig.digest->length = m_len;
653 length = i2d_X509_SIG(&sig, NULL);
656 keyLength = RSA_size(rsa);
658 if (length - RSA_PKCS1_PADDING > keyLength)
660 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
661 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
668 if (m_len != SSL_SIG_LEN)
670 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
671 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
675 hashBuffer = (unsigned char *)m;
681 ptr = hashBuffer = OPENSSL_malloc(
682 (unsigned int)keyLength+1);
685 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
686 ERR_R_MALLOC_FAILURE);
690 i2d_X509_SIG(&sig, &ptr);
696 ptr = hashBuffer = OPENSSL_malloc(
697 (unsigned int)keyLength+1);
700 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
701 ERR_R_MALLOC_FAILURE);
704 i2d_X509_SIG(&sig, &ptr);
711 digitalSignatureVerify(&returnCode, &reasonCode, &exitDataLength,
712 exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
713 keyToken, &length, hashBuffer, &lsiglen, sigbuf);
715 if (type == NID_sha1 || type == NID_md5)
717 memset(hashBuffer, 0, keyLength+1);
718 OPENSSL_free(hashBuffer);
721 return ((returnCode || reasonCode) ? 0 : 1);
724 #define SSL_SIG_LEN 36
726 static int cca_rsa_sign(int type, const unsigned char *m, unsigned int m_len,
727 unsigned char *sigret, unsigned int *siglen, const RSA *rsa)
731 long exitDataLength = 0;
732 unsigned char exitData[8];
733 long ruleArrayLength = 1;
734 unsigned char ruleArray[8] = "PKCS-1.1";
735 long outputLength=256;
736 long outputBitLength;
738 unsigned char *hashBuffer = NULL;
739 unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
740 long length = SSL_SIG_LEN;
744 X509_ALGOR algorithm;
745 ASN1_OCTET_STRING digest;
747 keyTokenLength = *(long*)keyToken;
748 keyToken+=sizeof(long);
750 if (type == NID_md5 || type == NID_sha1)
752 sig.algor = &algorithm;
753 algorithm.algorithm = OBJ_nid2obj(type);
755 if (!algorithm.algorithm)
757 CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,
758 CCA4758_R_UNKNOWN_ALGORITHM_TYPE);
762 if (!algorithm.algorithm->length)
764 CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,
765 CCA4758_R_ASN1_OID_UNKNOWN_FOR_MD);
769 parameter.type = V_ASN1_NULL;
770 parameter.value.ptr = NULL;
771 algorithm.parameter = ¶meter;
773 sig.digest = &digest;
774 sig.digest->data = (unsigned char*)m;
775 sig.digest->length = m_len;
777 length = i2d_X509_SIG(&sig, NULL);
780 keyLength = RSA_size(rsa);
782 if (length - RSA_PKCS1_PADDING > keyLength)
784 CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,
785 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
792 if (m_len != SSL_SIG_LEN)
794 CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,
795 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
798 hashBuffer = (unsigned char*)m;
804 ptr = hashBuffer = OPENSSL_malloc(
805 (unsigned int)keyLength+1);
808 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
809 ERR_R_MALLOC_FAILURE);
812 i2d_X509_SIG(&sig, &ptr);
818 ptr = hashBuffer = OPENSSL_malloc(
819 (unsigned int)keyLength+1);
822 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
823 ERR_R_MALLOC_FAILURE);
826 i2d_X509_SIG(&sig, &ptr);
833 digitalSignatureGenerate(&returnCode, &reasonCode, &exitDataLength,
834 exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
835 keyToken, &length, hashBuffer, &outputLength, &outputBitLength,
838 if (type == NID_sha1 || type == NID_md5)
840 memset(hashBuffer, 0, keyLength+1);
841 OPENSSL_free(hashBuffer);
844 *siglen = outputLength;
846 return ((returnCode || reasonCode) ? 0 : 1);
849 static int getModulusAndExponent(const unsigned char*token, long *exponentLength,
850 unsigned char *exponent, long *modulusLength, long *modulusFieldLength,
851 unsigned char *modulus)
855 if (*token++ != (char)0x1E) /* internal PKA token? */
858 if (*token++) /* token version must be zero */
863 len |= (unsigned char)*token++;
865 token += 4; /* skip reserved bytes */
867 if (*token++ == (char)0x04)
869 if (*token++) /* token version must be zero */
874 len |= (unsigned char)*token++;
876 token+=2; /* skip reserved section */
880 len |= (unsigned char)*token++;
882 *exponentLength = len;
886 len |= (unsigned char)*token++;
888 *modulusLength = len;
892 len |= (unsigned char)*token++;
894 *modulusFieldLength = len;
896 memcpy(exponent, token, *exponentLength);
897 token+= *exponentLength;
899 memcpy(modulus, token, *modulusFieldLength);
905 #endif /* OPENSSL_NO_RSA */
907 static int cca_random_status(void)
912 static int cca_get_random_bytes(unsigned char* buf, int num)
916 long exit_data_length;
917 unsigned char exit_data[4];
918 unsigned char form[] = "RANDOM ";
919 unsigned char rand_buf[8];
921 while(num >= sizeof(rand_buf))
923 randomNumberGenerate(&ret_code, &reason_code, &exit_data_length,
924 exit_data, form, rand_buf);
927 num -= sizeof(rand_buf);
928 memcpy(buf, rand_buf, sizeof(rand_buf));
929 buf += sizeof(rand_buf);
934 randomNumberGenerate(&ret_code, &reason_code, NULL, NULL,
938 memcpy(buf, rand_buf, num);
944 static void cca_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad, int idx,
945 long argl, void *argp)
951 /* Goo to handle building as a dynamic engine */
952 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
953 static int bind_fn(ENGINE *e, const char *id)
955 if(id && (strcmp(id, engine_4758_cca_id) != 0))
961 IMPLEMENT_DYNAMIC_CHECK_FN()
962 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
963 #endif /* OPENSSL_NO_DYNAMIC_ENGINE */
965 #endif /* !OPENSSL_NO_HW_4758_CCA */
966 #endif /* !OPENSSL_NO_HW */