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).
58 #include <openssl/crypto.h>
59 /* #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 "e_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 */
186 NULL /* rsa_keygen */
190 static RAND_METHOD ibm_4758_cca_rand =
192 /* "IBM 4758 RAND method", */
194 cca_get_random_bytes, /* get random bytes from the card */
197 cca_get_random_bytes, /* pseudo rand */
198 cca_random_status, /* status */
201 static const char *engine_4758_cca_id = "4758cca";
202 static const char *engine_4758_cca_name = "IBM 4758 CCA hardware engine support";
204 /* engine implementation */
205 /*-----------------------*/
206 static int bind_helper(ENGINE *e)
208 if(!ENGINE_set_id(e, engine_4758_cca_id) ||
209 !ENGINE_set_name(e, engine_4758_cca_name) ||
210 #ifndef OPENSSL_NO_RSA
211 !ENGINE_set_RSA(e, &ibm_4758_cca_rsa) ||
213 !ENGINE_set_RAND(e, &ibm_4758_cca_rand) ||
214 !ENGINE_set_destroy_function(e, ibm_4758_cca_destroy) ||
215 !ENGINE_set_init_function(e, ibm_4758_cca_init) ||
216 !ENGINE_set_finish_function(e, ibm_4758_cca_finish) ||
217 !ENGINE_set_ctrl_function(e, ibm_4758_cca_ctrl) ||
218 !ENGINE_set_load_privkey_function(e, ibm_4758_load_privkey) ||
219 !ENGINE_set_load_pubkey_function(e, ibm_4758_load_pubkey) ||
220 !ENGINE_set_cmd_defns(e, cca4758_cmd_defns))
222 /* Ensure the error handling is set up */
223 ERR_load_CCA4758_strings();
227 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
228 static ENGINE *engine_4758_cca(void)
230 ENGINE *ret = ENGINE_new();
233 if(!bind_helper(ret))
241 void ENGINE_load_4758cca(void)
243 ENGINE *e_4758 = engine_4758_cca();
251 static int ibm_4758_cca_destroy(ENGINE *e)
253 ERR_unload_CCA4758_strings();
254 free_CCA4758_LIB_NAME();
258 static int ibm_4758_cca_init(ENGINE *e)
262 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT,CCA4758_R_ALREADY_LOADED);
266 dso = DSO_load(NULL, get_CCA4758_LIB_NAME(), NULL, 0);
269 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT,CCA4758_R_DSO_FAILURE);
273 #ifndef OPENSSL_NO_RSA
274 if(!(keyRecordRead = (F_KEYRECORDREAD)
275 DSO_bind_func(dso, n_keyRecordRead)) ||
276 !(randomNumberGenerate = (F_RANDOMNUMBERGENERATE)
277 DSO_bind_func(dso, n_randomNumberGenerate)) ||
278 !(digitalSignatureGenerate = (F_DIGITALSIGNATUREGENERATE)
279 DSO_bind_func(dso, n_digitalSignatureGenerate)) ||
280 !(digitalSignatureVerify = (F_DIGITALSIGNATUREVERIFY)
281 DSO_bind_func(dso, n_digitalSignatureVerify)) ||
282 !(publicKeyExtract = (F_PUBLICKEYEXTRACT)
283 DSO_bind_func(dso, n_publicKeyExtract)) ||
284 !(pkaEncrypt = (F_PKAENCRYPT)
285 DSO_bind_func(dso, n_pkaEncrypt)) ||
286 !(pkaDecrypt = (F_PKADECRYPT)
287 DSO_bind_func(dso, n_pkaDecrypt)))
289 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT,CCA4758_R_DSO_FAILURE);
293 if(!(randomNumberGenerate = (F_RANDOMNUMBERGENERATE)
294 DSO_bind_func(dso, n_randomNumberGenerate)))
296 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT,CCA4758_R_DSO_FAILURE);
301 hndidx = RSA_get_ex_new_index(0, "IBM 4758 CCA RSA key handle",
302 NULL, NULL, cca_ex_free);
310 keyRecordRead = (F_KEYRECORDREAD)0;
311 randomNumberGenerate = (F_RANDOMNUMBERGENERATE)0;
312 digitalSignatureGenerate = (F_DIGITALSIGNATUREGENERATE)0;
313 digitalSignatureVerify = (F_DIGITALSIGNATUREVERIFY)0;
314 publicKeyExtract = (F_PUBLICKEYEXTRACT)0;
315 pkaEncrypt = (F_PKAENCRYPT)0;
316 pkaDecrypt = (F_PKADECRYPT)0;
320 static int ibm_4758_cca_finish(ENGINE *e)
322 free_CCA4758_LIB_NAME();
325 CCA4758err(CCA4758_F_IBM_4758_CCA_FINISH,
326 CCA4758_R_NOT_LOADED);
331 CCA4758err(CCA4758_F_IBM_4758_CCA_FINISH,
332 CCA4758_R_UNIT_FAILURE);
336 keyRecordRead = (F_KEYRECORDREAD)0;
337 randomNumberGenerate = (F_RANDOMNUMBERGENERATE)0;
338 digitalSignatureGenerate = (F_DIGITALSIGNATUREGENERATE)0;
339 digitalSignatureVerify = (F_DIGITALSIGNATUREVERIFY)0;
340 publicKeyExtract = (F_PUBLICKEYEXTRACT)0;
341 pkaEncrypt = (F_PKAENCRYPT)0;
342 pkaDecrypt = (F_PKADECRYPT)0;
346 static int ibm_4758_cca_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
348 int initialised = ((dso == NULL) ? 0 : 1);
351 case CCA4758_CMD_SO_PATH:
354 CCA4758err(CCA4758_F_IBM_4758_CCA_CTRL,
355 ERR_R_PASSED_NULL_PARAMETER);
360 CCA4758err(CCA4758_F_IBM_4758_CCA_CTRL,
361 CCA4758_R_ALREADY_LOADED);
364 return set_CCA4758_LIB_NAME((const char *)p);
368 CCA4758err(CCA4758_F_IBM_4758_CCA_CTRL,
369 CCA4758_R_COMMAND_NOT_IMPLEMENTED);
373 #ifndef OPENSSL_NO_RSA
375 #define MAX_CCA_PKA_TOKEN_SIZE 2500
377 static EVP_PKEY *ibm_4758_load_privkey(ENGINE* e, const char* key_id,
378 UI_METHOD *ui_method, void *callback_data)
381 EVP_PKEY *res = NULL;
382 unsigned char* keyToken = NULL;
383 unsigned char pubKeyToken[MAX_CCA_PKA_TOKEN_SIZE];
384 long pubKeyTokenLength = MAX_CCA_PKA_TOKEN_SIZE;
385 long keyTokenLength = MAX_CCA_PKA_TOKEN_SIZE;
388 long exitDataLength = 0;
389 long ruleArrayLength = 0;
390 unsigned char exitData[8];
391 unsigned char ruleArray[8];
392 unsigned char keyLabel[64];
393 long keyLabelLength = strlen(key_id);
394 unsigned char modulus[256];
395 long modulusFieldLength = sizeof(modulus);
396 long modulusLength = 0;
397 unsigned char exponent[256];
398 long exponentLength = sizeof(exponent);
400 if (keyLabelLength > sizeof(keyLabel))
402 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
403 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
407 memset(keyLabel,' ', sizeof(keyLabel));
408 memcpy(keyLabel, key_id, keyLabelLength);
410 keyToken = OPENSSL_malloc(MAX_CCA_PKA_TOKEN_SIZE + sizeof(long));
413 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
414 ERR_R_MALLOC_FAILURE);
418 keyRecordRead(&returnCode, &reasonCode, &exitDataLength,
419 exitData, &ruleArrayLength, ruleArray, keyLabel,
420 &keyTokenLength, keyToken+sizeof(long));
424 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
425 CCA4758_R_FAILED_LOADING_PRIVATE_KEY);
429 publicKeyExtract(&returnCode, &reasonCode, &exitDataLength,
430 exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
431 keyToken+sizeof(long), &pubKeyTokenLength, pubKeyToken);
435 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
436 CCA4758_R_FAILED_LOADING_PRIVATE_KEY);
440 if (!getModulusAndExponent(pubKeyToken, &exponentLength,
441 exponent, &modulusLength, &modulusFieldLength,
444 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
445 CCA4758_R_FAILED_LOADING_PRIVATE_KEY);
449 (*(long*)keyToken) = keyTokenLength;
450 rtmp = RSA_new_method(e);
451 RSA_set_ex_data(rtmp, hndidx, (char *)keyToken);
453 rtmp->e = BN_bin2bn(exponent, exponentLength, NULL);
454 rtmp->n = BN_bin2bn(modulus, modulusFieldLength, NULL);
455 rtmp->flags |= RSA_FLAG_EXT_PKEY;
457 res = EVP_PKEY_new();
458 EVP_PKEY_assign_RSA(res, rtmp);
463 OPENSSL_free(keyToken);
471 static EVP_PKEY *ibm_4758_load_pubkey(ENGINE* e, const char* key_id,
472 UI_METHOD *ui_method, void *callback_data)
475 EVP_PKEY *res = NULL;
476 unsigned char* keyToken = NULL;
477 long keyTokenLength = MAX_CCA_PKA_TOKEN_SIZE;
480 long exitDataLength = 0;
481 long ruleArrayLength = 0;
482 unsigned char exitData[8];
483 unsigned char ruleArray[8];
484 unsigned char keyLabel[64];
485 long keyLabelLength = strlen(key_id);
486 unsigned char modulus[512];
487 long modulusFieldLength = sizeof(modulus);
488 long modulusLength = 0;
489 unsigned char exponent[512];
490 long exponentLength = sizeof(exponent);
492 if (keyLabelLength > sizeof(keyLabel))
494 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
495 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
499 memset(keyLabel,' ', sizeof(keyLabel));
500 memcpy(keyLabel, key_id, keyLabelLength);
502 keyToken = OPENSSL_malloc(MAX_CCA_PKA_TOKEN_SIZE + sizeof(long));
505 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PUBKEY,
506 ERR_R_MALLOC_FAILURE);
510 keyRecordRead(&returnCode, &reasonCode, &exitDataLength, exitData,
511 &ruleArrayLength, ruleArray, keyLabel, &keyTokenLength,
512 keyToken+sizeof(long));
516 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
517 ERR_R_MALLOC_FAILURE);
521 if (!getModulusAndExponent(keyToken+sizeof(long), &exponentLength,
522 exponent, &modulusLength, &modulusFieldLength, modulus))
524 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY,
525 CCA4758_R_FAILED_LOADING_PUBLIC_KEY);
529 (*(long*)keyToken) = keyTokenLength;
530 rtmp = RSA_new_method(e);
531 RSA_set_ex_data(rtmp, hndidx, (char *)keyToken);
532 rtmp->e = BN_bin2bn(exponent, exponentLength, NULL);
533 rtmp->n = BN_bin2bn(modulus, modulusFieldLength, NULL);
534 rtmp->flags |= RSA_FLAG_EXT_PKEY;
535 res = EVP_PKEY_new();
536 EVP_PKEY_assign_RSA(res, rtmp);
541 OPENSSL_free(keyToken);
549 static int cca_rsa_pub_enc(int flen, const unsigned char *from,
550 unsigned char *to, RSA *rsa,int padding)
555 long exitDataLength = 0;
556 unsigned char exitData[8];
557 long ruleArrayLength = 1;
558 unsigned char ruleArray[8] = "PKCS-1.2";
559 long dataStructureLength = 0;
560 unsigned char dataStructure[8];
561 long outputLength = RSA_size(rsa);
563 unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
565 keyTokenLength = *(long*)keyToken;
566 keyToken+=sizeof(long);
568 pkaEncrypt(&returnCode, &reasonCode, &exitDataLength, exitData,
569 &ruleArrayLength, ruleArray, &lflen, (unsigned char*)from,
570 &dataStructureLength, dataStructure, &keyTokenLength,
571 keyToken, &outputLength, to);
573 if (returnCode || reasonCode)
574 return -(returnCode << 16 | reasonCode);
578 static int cca_rsa_priv_dec(int flen, const unsigned char *from,
579 unsigned char *to, RSA *rsa,int padding)
584 long exitDataLength = 0;
585 unsigned char exitData[8];
586 long ruleArrayLength = 1;
587 unsigned char ruleArray[8] = "PKCS-1.2";
588 long dataStructureLength = 0;
589 unsigned char dataStructure[8];
590 long outputLength = RSA_size(rsa);
592 unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
594 keyTokenLength = *(long*)keyToken;
595 keyToken+=sizeof(long);
597 pkaDecrypt(&returnCode, &reasonCode, &exitDataLength, exitData,
598 &ruleArrayLength, ruleArray, &lflen, (unsigned char*)from,
599 &dataStructureLength, dataStructure, &keyTokenLength,
600 keyToken, &outputLength, to);
602 return (returnCode | reasonCode) ? 0 : 1;
605 #define SSL_SIG_LEN 36
607 static int cca_rsa_verify(int type, const unsigned char *m, unsigned int m_len,
608 unsigned char *sigbuf, unsigned int siglen, const RSA *rsa)
612 long lsiglen = siglen;
613 long exitDataLength = 0;
614 unsigned char exitData[8];
615 long ruleArrayLength = 1;
616 unsigned char ruleArray[8] = "PKCS-1.1";
618 unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
619 long length = SSL_SIG_LEN;
621 unsigned char *hashBuffer = NULL;
624 X509_ALGOR algorithm;
625 ASN1_OCTET_STRING digest;
627 keyTokenLength = *(long*)keyToken;
628 keyToken+=sizeof(long);
630 if (type == NID_md5 || type == NID_sha1)
632 sig.algor = &algorithm;
633 algorithm.algorithm = OBJ_nid2obj(type);
635 if (!algorithm.algorithm)
637 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
638 CCA4758_R_UNKNOWN_ALGORITHM_TYPE);
642 if (!algorithm.algorithm->length)
644 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
645 CCA4758_R_ASN1_OID_UNKNOWN_FOR_MD);
649 parameter.type = V_ASN1_NULL;
650 parameter.value.ptr = NULL;
651 algorithm.parameter = ¶meter;
653 sig.digest = &digest;
654 sig.digest->data = (unsigned char*)m;
655 sig.digest->length = m_len;
657 length = i2d_X509_SIG(&sig, NULL);
660 keyLength = RSA_size(rsa);
662 if (length - RSA_PKCS1_PADDING > keyLength)
664 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
665 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
672 if (m_len != SSL_SIG_LEN)
674 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
675 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
679 hashBuffer = (unsigned char *)m;
685 ptr = hashBuffer = OPENSSL_malloc(
686 (unsigned int)keyLength+1);
689 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
690 ERR_R_MALLOC_FAILURE);
694 i2d_X509_SIG(&sig, &ptr);
700 ptr = hashBuffer = OPENSSL_malloc(
701 (unsigned int)keyLength+1);
704 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
705 ERR_R_MALLOC_FAILURE);
708 i2d_X509_SIG(&sig, &ptr);
715 digitalSignatureVerify(&returnCode, &reasonCode, &exitDataLength,
716 exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
717 keyToken, &length, hashBuffer, &lsiglen, sigbuf);
719 if (type == NID_sha1 || type == NID_md5)
721 OPENSSL_cleanse(hashBuffer, keyLength+1);
722 OPENSSL_free(hashBuffer);
725 return ((returnCode || reasonCode) ? 0 : 1);
728 #define SSL_SIG_LEN 36
730 static int cca_rsa_sign(int type, const unsigned char *m, unsigned int m_len,
731 unsigned char *sigret, unsigned int *siglen, const RSA *rsa)
735 long exitDataLength = 0;
736 unsigned char exitData[8];
737 long ruleArrayLength = 1;
738 unsigned char ruleArray[8] = "PKCS-1.1";
739 long outputLength=256;
740 long outputBitLength;
742 unsigned char *hashBuffer = NULL;
743 unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
744 long length = SSL_SIG_LEN;
748 X509_ALGOR algorithm;
749 ASN1_OCTET_STRING digest;
751 keyTokenLength = *(long*)keyToken;
752 keyToken+=sizeof(long);
754 if (type == NID_md5 || type == NID_sha1)
756 sig.algor = &algorithm;
757 algorithm.algorithm = OBJ_nid2obj(type);
759 if (!algorithm.algorithm)
761 CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,
762 CCA4758_R_UNKNOWN_ALGORITHM_TYPE);
766 if (!algorithm.algorithm->length)
768 CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,
769 CCA4758_R_ASN1_OID_UNKNOWN_FOR_MD);
773 parameter.type = V_ASN1_NULL;
774 parameter.value.ptr = NULL;
775 algorithm.parameter = ¶meter;
777 sig.digest = &digest;
778 sig.digest->data = (unsigned char*)m;
779 sig.digest->length = m_len;
781 length = i2d_X509_SIG(&sig, NULL);
784 keyLength = RSA_size(rsa);
786 if (length - RSA_PKCS1_PADDING > keyLength)
788 CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,
789 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
796 if (m_len != SSL_SIG_LEN)
798 CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,
799 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
802 hashBuffer = (unsigned char*)m;
808 ptr = hashBuffer = OPENSSL_malloc(
809 (unsigned int)keyLength+1);
812 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
813 ERR_R_MALLOC_FAILURE);
816 i2d_X509_SIG(&sig, &ptr);
822 ptr = hashBuffer = OPENSSL_malloc(
823 (unsigned int)keyLength+1);
826 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
827 ERR_R_MALLOC_FAILURE);
830 i2d_X509_SIG(&sig, &ptr);
837 digitalSignatureGenerate(&returnCode, &reasonCode, &exitDataLength,
838 exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
839 keyToken, &length, hashBuffer, &outputLength, &outputBitLength,
842 if (type == NID_sha1 || type == NID_md5)
844 OPENSSL_cleanse(hashBuffer, keyLength+1);
845 OPENSSL_free(hashBuffer);
848 *siglen = outputLength;
850 return ((returnCode || reasonCode) ? 0 : 1);
853 static int getModulusAndExponent(const unsigned char*token, long *exponentLength,
854 unsigned char *exponent, long *modulusLength, long *modulusFieldLength,
855 unsigned char *modulus)
859 if (*token++ != (char)0x1E) /* internal PKA token? */
862 if (*token++) /* token version must be zero */
867 len |= (unsigned char)*token++;
869 token += 4; /* skip reserved bytes */
871 if (*token++ == (char)0x04)
873 if (*token++) /* token version must be zero */
878 len |= (unsigned char)*token++;
880 token+=2; /* skip reserved section */
884 len |= (unsigned char)*token++;
886 *exponentLength = len;
890 len |= (unsigned char)*token++;
892 *modulusLength = len;
896 len |= (unsigned char)*token++;
898 *modulusFieldLength = len;
900 memcpy(exponent, token, *exponentLength);
901 token+= *exponentLength;
903 memcpy(modulus, token, *modulusFieldLength);
909 #endif /* OPENSSL_NO_RSA */
911 static int cca_random_status(void)
916 static int cca_get_random_bytes(unsigned char* buf, int num)
920 long exit_data_length;
921 unsigned char exit_data[4];
922 unsigned char form[] = "RANDOM ";
923 unsigned char rand_buf[8];
925 while(num >= sizeof(rand_buf))
927 randomNumberGenerate(&ret_code, &reason_code, &exit_data_length,
928 exit_data, form, rand_buf);
931 num -= sizeof(rand_buf);
932 memcpy(buf, rand_buf, sizeof(rand_buf));
933 buf += sizeof(rand_buf);
938 randomNumberGenerate(&ret_code, &reason_code, NULL, NULL,
942 memcpy(buf, rand_buf, num);
948 static void cca_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad, int idx,
949 long argl, void *argp)
955 /* Goo to handle building as a dynamic engine */
956 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
957 static int bind_fn(ENGINE *e, const char *id)
959 if(id && (strcmp(id, engine_4758_cca_id) != 0))
965 IMPLEMENT_DYNAMIC_CHECK_FN()
966 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
967 #endif /* OPENSSL_NO_DYNAMIC_ENGINE */
969 #endif /* !OPENSSL_NO_HW_4758_CCA */
970 #endif /* !OPENSSL_NO_HW */