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/dso.h>
60 #include <openssl/x509.h>
61 #include <openssl/objects.h>
62 #include <openssl/engine.h>
63 #include <openssl/rand.h>
64 #ifndef OPENSSL_NO_RSA
65 # include <openssl/rsa.h>
67 #include <openssl/bn.h>
70 # ifndef OPENSSL_NO_HW_4758_CCA
73 # include "hw_4758_cca.h"
75 # include "vendor_defns/hw_4758_cca.h"
78 # include "e_4758cca_err.c"
80 static int ibm_4758_cca_destroy(ENGINE *e);
81 static int ibm_4758_cca_init(ENGINE *e);
82 static int ibm_4758_cca_finish(ENGINE *e);
83 static int ibm_4758_cca_ctrl(ENGINE *e, int cmd, long i, void *p,
88 # ifndef OPENSSL_NO_RSA
89 static int cca_rsa_pub_enc(int flen, const unsigned char *from,
90 unsigned char *to, RSA *rsa, int padding);
91 static int cca_rsa_priv_dec(int flen, const unsigned char *from,
92 unsigned char *to, RSA *rsa, int padding);
93 static int cca_rsa_sign(int type, const unsigned char *m, unsigned int m_len,
94 unsigned char *sigret, unsigned int *siglen,
96 static int cca_rsa_verify(int dtype, const unsigned char *m,
97 unsigned int m_len, const unsigned char *sigbuf,
98 unsigned int siglen, const RSA *rsa);
100 /* utility functions */
101 /* ---------------------*/
102 static EVP_PKEY *ibm_4758_load_privkey(ENGINE *, const char *,
103 UI_METHOD *ui_method,
104 void *callback_data);
105 static EVP_PKEY *ibm_4758_load_pubkey(ENGINE *, const char *,
106 UI_METHOD *ui_method,
107 void *callback_data);
109 static int getModulusAndExponent(const unsigned char *token,
110 long *exponentLength,
111 unsigned char *exponent, long *modulusLength,
112 long *modulusFieldLength,
113 unsigned char *modulus);
116 /* RAND number functions */
117 /* ---------------------*/
118 static int cca_get_random_bytes(unsigned char *, int);
119 static int cca_random_status(void);
121 # ifndef OPENSSL_NO_RSA
122 static void cca_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
123 int idx, long argl, void *argp);
126 /* Function pointers for CCA verbs */
127 /* -------------------------------*/
128 # ifndef OPENSSL_NO_RSA
129 static F_KEYRECORDREAD keyRecordRead;
130 static F_DIGITALSIGNATUREGENERATE digitalSignatureGenerate;
131 static F_DIGITALSIGNATUREVERIFY digitalSignatureVerify;
132 static F_PUBLICKEYEXTRACT publicKeyExtract;
133 static F_PKAENCRYPT pkaEncrypt;
134 static F_PKADECRYPT pkaDecrypt;
136 static F_RANDOMNUMBERGENERATE randomNumberGenerate;
138 /* static variables */
139 /* ----------------*/
140 static const char *CCA4758_LIB_NAME = NULL;
141 static const char *get_CCA4758_LIB_NAME(void)
143 if (CCA4758_LIB_NAME)
144 return CCA4758_LIB_NAME;
148 static void free_CCA4758_LIB_NAME(void)
150 if (CCA4758_LIB_NAME)
151 OPENSSL_free((void *)CCA4758_LIB_NAME);
152 CCA4758_LIB_NAME = NULL;
155 static long set_CCA4758_LIB_NAME(const char *name)
157 free_CCA4758_LIB_NAME();
158 return (((CCA4758_LIB_NAME = BUF_strdup(name)) != NULL) ? 1 : 0);
161 # ifndef OPENSSL_NO_RSA
162 static const char *n_keyRecordRead = CSNDKRR;
163 static const char *n_digitalSignatureGenerate = CSNDDSG;
164 static const char *n_digitalSignatureVerify = CSNDDSV;
165 static const char *n_publicKeyExtract = CSNDPKX;
166 static const char *n_pkaEncrypt = CSNDPKE;
167 static const char *n_pkaDecrypt = CSNDPKD;
169 static const char *n_randomNumberGenerate = CSNBRNG;
171 # ifndef OPENSSL_NO_RSA
172 static int hndidx = -1;
174 static DSO *dso = NULL;
176 /* openssl engine initialization structures */
177 /* ----------------------------------------*/
179 # define CCA4758_CMD_SO_PATH ENGINE_CMD_BASE
180 static const ENGINE_CMD_DEFN cca4758_cmd_defns[] = {
181 {CCA4758_CMD_SO_PATH,
183 "Specifies the path to the '4758cca' shared library",
184 ENGINE_CMD_FLAG_STRING},
188 # ifndef OPENSSL_NO_RSA
189 static RSA_METHOD ibm_4758_cca_rsa = {
190 "IBM 4758 CCA RSA method",
195 NULL, /* rsa_mod_exp, */
196 NULL, /* mod_exp_mont, */
199 RSA_FLAG_SIGN_VER, /* flags */
201 cca_rsa_sign, /* rsa_sign */
202 cca_rsa_verify, /* rsa_verify */
203 NULL /* rsa_keygen */
207 static RAND_METHOD ibm_4758_cca_rand = {
208 /* "IBM 4758 RAND method", */
210 cca_get_random_bytes, /* get random bytes from the card */
213 cca_get_random_bytes, /* pseudo rand */
214 cca_random_status, /* status */
217 static const char *engine_4758_cca_id = "4758cca";
218 static const char *engine_4758_cca_name =
219 "IBM 4758 CCA hardware engine support";
220 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
221 /* Compatibility hack, the dynamic library uses this form in the path */
222 static const char *engine_4758_cca_id_alt = "4758_cca";
225 /* engine implementation */
226 /* ---------------------*/
227 static int bind_helper(ENGINE *e)
229 if (!ENGINE_set_id(e, engine_4758_cca_id) ||
230 !ENGINE_set_name(e, engine_4758_cca_name) ||
231 # ifndef OPENSSL_NO_RSA
232 !ENGINE_set_RSA(e, &ibm_4758_cca_rsa) ||
234 !ENGINE_set_RAND(e, &ibm_4758_cca_rand) ||
235 !ENGINE_set_destroy_function(e, ibm_4758_cca_destroy) ||
236 !ENGINE_set_init_function(e, ibm_4758_cca_init) ||
237 !ENGINE_set_finish_function(e, ibm_4758_cca_finish) ||
238 !ENGINE_set_ctrl_function(e, ibm_4758_cca_ctrl) ||
239 # ifndef OPENSSL_NO_RSA
240 !ENGINE_set_load_privkey_function(e, ibm_4758_load_privkey) ||
241 !ENGINE_set_load_pubkey_function(e, ibm_4758_load_pubkey) ||
243 !ENGINE_set_cmd_defns(e, cca4758_cmd_defns))
245 /* Ensure the error handling is set up */
246 ERR_load_CCA4758_strings();
250 # ifdef OPENSSL_NO_DYNAMIC_ENGINE
251 static ENGINE *engine_4758_cca(void)
253 ENGINE *ret = ENGINE_new();
256 if (!bind_helper(ret)) {
263 void ENGINE_load_4758cca(void)
265 ENGINE *e_4758 = engine_4758_cca();
274 static int ibm_4758_cca_destroy(ENGINE *e)
276 ERR_unload_CCA4758_strings();
277 free_CCA4758_LIB_NAME();
281 static int ibm_4758_cca_init(ENGINE *e)
284 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT, CCA4758_R_ALREADY_LOADED);
288 dso = DSO_load(NULL, get_CCA4758_LIB_NAME(), NULL, 0);
290 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT, CCA4758_R_DSO_FAILURE);
293 # ifndef OPENSSL_NO_RSA
294 if (!(keyRecordRead = (F_KEYRECORDREAD)
295 DSO_bind_func(dso, n_keyRecordRead)) ||
296 !(randomNumberGenerate = (F_RANDOMNUMBERGENERATE)
297 DSO_bind_func(dso, n_randomNumberGenerate)) ||
298 !(digitalSignatureGenerate = (F_DIGITALSIGNATUREGENERATE)
299 DSO_bind_func(dso, n_digitalSignatureGenerate)) ||
300 !(digitalSignatureVerify = (F_DIGITALSIGNATUREVERIFY)
301 DSO_bind_func(dso, n_digitalSignatureVerify)) ||
302 !(publicKeyExtract = (F_PUBLICKEYEXTRACT)
303 DSO_bind_func(dso, n_publicKeyExtract)) ||
304 !(pkaEncrypt = (F_PKAENCRYPT)
305 DSO_bind_func(dso, n_pkaEncrypt)) || !(pkaDecrypt = (F_PKADECRYPT)
309 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT, CCA4758_R_DSO_FAILURE);
313 if (!(randomNumberGenerate = (F_RANDOMNUMBERGENERATE)
314 DSO_bind_func(dso, n_randomNumberGenerate))) {
315 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT, CCA4758_R_DSO_FAILURE);
320 # ifndef OPENSSL_NO_RSA
321 hndidx = RSA_get_ex_new_index(0, "IBM 4758 CCA RSA key handle",
322 NULL, NULL, cca_ex_free);
331 # ifndef OPENSSL_NO_RSA
332 keyRecordRead = (F_KEYRECORDREAD) 0;
333 digitalSignatureGenerate = (F_DIGITALSIGNATUREGENERATE) 0;
334 digitalSignatureVerify = (F_DIGITALSIGNATUREVERIFY)0;
335 publicKeyExtract = (F_PUBLICKEYEXTRACT)0;
336 pkaEncrypt = (F_PKAENCRYPT) 0;
337 pkaDecrypt = (F_PKADECRYPT) 0;
339 randomNumberGenerate = (F_RANDOMNUMBERGENERATE) 0;
343 static int ibm_4758_cca_finish(ENGINE *e)
345 free_CCA4758_LIB_NAME();
347 CCA4758err(CCA4758_F_IBM_4758_CCA_FINISH, CCA4758_R_NOT_LOADED);
350 if (!DSO_free(dso)) {
351 CCA4758err(CCA4758_F_IBM_4758_CCA_FINISH, CCA4758_R_UNIT_FAILURE);
355 # ifndef OPENSSL_NO_RSA
356 keyRecordRead = (F_KEYRECORDREAD) 0;
357 randomNumberGenerate = (F_RANDOMNUMBERGENERATE) 0;
358 digitalSignatureGenerate = (F_DIGITALSIGNATUREGENERATE) 0;
359 digitalSignatureVerify = (F_DIGITALSIGNATUREVERIFY)0;
360 publicKeyExtract = (F_PUBLICKEYEXTRACT)0;
361 pkaEncrypt = (F_PKAENCRYPT) 0;
362 pkaDecrypt = (F_PKADECRYPT) 0;
364 randomNumberGenerate = (F_RANDOMNUMBERGENERATE) 0;
368 static int ibm_4758_cca_ctrl(ENGINE *e, int cmd, long i, void *p,
371 int initialised = ((dso == NULL) ? 0 : 1);
373 case CCA4758_CMD_SO_PATH:
375 CCA4758err(CCA4758_F_IBM_4758_CCA_CTRL,
376 ERR_R_PASSED_NULL_PARAMETER);
380 CCA4758err(CCA4758_F_IBM_4758_CCA_CTRL, CCA4758_R_ALREADY_LOADED);
383 return set_CCA4758_LIB_NAME((const char *)p);
387 CCA4758err(CCA4758_F_IBM_4758_CCA_CTRL,
388 CCA4758_R_COMMAND_NOT_IMPLEMENTED);
392 # ifndef OPENSSL_NO_RSA
394 # define MAX_CCA_PKA_TOKEN_SIZE 2500
396 static EVP_PKEY *ibm_4758_load_privkey(ENGINE *e, const char *key_id,
397 UI_METHOD *ui_method,
401 EVP_PKEY *res = NULL;
402 unsigned char *keyToken = NULL;
403 unsigned char pubKeyToken[MAX_CCA_PKA_TOKEN_SIZE];
404 long pubKeyTokenLength = MAX_CCA_PKA_TOKEN_SIZE;
405 long keyTokenLength = MAX_CCA_PKA_TOKEN_SIZE;
408 long exitDataLength = 0;
409 long ruleArrayLength = 0;
410 unsigned char exitData[8];
411 unsigned char ruleArray[8];
412 unsigned char keyLabel[64];
413 unsigned long keyLabelLength = strlen(key_id);
414 unsigned char modulus[256];
415 long modulusFieldLength = sizeof(modulus);
416 long modulusLength = 0;
417 unsigned char exponent[256];
418 long exponentLength = sizeof(exponent);
420 if (keyLabelLength > sizeof(keyLabel)) {
421 CCA4758err(CCA4758_F_IBM_4758_LOAD_PRIVKEY,
422 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
426 memset(keyLabel, ' ', sizeof(keyLabel));
427 memcpy(keyLabel, key_id, keyLabelLength);
429 keyToken = OPENSSL_malloc(MAX_CCA_PKA_TOKEN_SIZE + sizeof(long));
431 CCA4758err(CCA4758_F_IBM_4758_LOAD_PRIVKEY, ERR_R_MALLOC_FAILURE);
435 keyRecordRead(&returnCode, &reasonCode, &exitDataLength,
436 exitData, &ruleArrayLength, ruleArray, keyLabel,
437 &keyTokenLength, keyToken + sizeof(long));
440 CCA4758err(CCA4758_F_IBM_4758_LOAD_PRIVKEY,
441 CCA4758_R_FAILED_LOADING_PRIVATE_KEY);
445 publicKeyExtract(&returnCode, &reasonCode, &exitDataLength,
446 exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
447 keyToken + sizeof(long), &pubKeyTokenLength,
451 CCA4758err(CCA4758_F_IBM_4758_LOAD_PRIVKEY,
452 CCA4758_R_FAILED_LOADING_PRIVATE_KEY);
456 if (!getModulusAndExponent(pubKeyToken, &exponentLength,
457 exponent, &modulusLength, &modulusFieldLength,
459 CCA4758err(CCA4758_F_IBM_4758_LOAD_PRIVKEY,
460 CCA4758_R_FAILED_LOADING_PRIVATE_KEY);
464 (*(long *)keyToken) = keyTokenLength;
465 rtmp = RSA_new_method(e);
466 RSA_set_ex_data(rtmp, hndidx, (char *)keyToken);
468 rtmp->e = BN_bin2bn(exponent, exponentLength, NULL);
469 rtmp->n = BN_bin2bn(modulus, modulusFieldLength, NULL);
470 rtmp->flags |= RSA_FLAG_EXT_PKEY;
472 res = EVP_PKEY_new();
473 EVP_PKEY_assign_RSA(res, rtmp);
478 OPENSSL_free(keyToken);
482 static EVP_PKEY *ibm_4758_load_pubkey(ENGINE *e, const char *key_id,
483 UI_METHOD *ui_method,
487 EVP_PKEY *res = NULL;
488 unsigned char *keyToken = NULL;
489 long keyTokenLength = MAX_CCA_PKA_TOKEN_SIZE;
492 long exitDataLength = 0;
493 long ruleArrayLength = 0;
494 unsigned char exitData[8];
495 unsigned char ruleArray[8];
496 unsigned char keyLabel[64];
497 unsigned long keyLabelLength = strlen(key_id);
498 unsigned char modulus[512];
499 long modulusFieldLength = sizeof(modulus);
500 long modulusLength = 0;
501 unsigned char exponent[512];
502 long exponentLength = sizeof(exponent);
504 if (keyLabelLength > sizeof(keyLabel)) {
505 CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY,
506 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
510 memset(keyLabel, ' ', sizeof(keyLabel));
511 memcpy(keyLabel, key_id, keyLabelLength);
513 keyToken = OPENSSL_malloc(MAX_CCA_PKA_TOKEN_SIZE + sizeof(long));
515 CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY, ERR_R_MALLOC_FAILURE);
519 keyRecordRead(&returnCode, &reasonCode, &exitDataLength, exitData,
520 &ruleArrayLength, ruleArray, keyLabel, &keyTokenLength,
521 keyToken + sizeof(long));
524 CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY, ERR_R_MALLOC_FAILURE);
528 if (!getModulusAndExponent(keyToken + sizeof(long), &exponentLength,
529 exponent, &modulusLength, &modulusFieldLength,
531 CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY,
532 CCA4758_R_FAILED_LOADING_PUBLIC_KEY);
536 (*(long *)keyToken) = keyTokenLength;
537 rtmp = RSA_new_method(e);
538 RSA_set_ex_data(rtmp, hndidx, (char *)keyToken);
539 rtmp->e = BN_bin2bn(exponent, exponentLength, NULL);
540 rtmp->n = BN_bin2bn(modulus, modulusFieldLength, NULL);
541 rtmp->flags |= RSA_FLAG_EXT_PKEY;
542 res = EVP_PKEY_new();
543 EVP_PKEY_assign_RSA(res, rtmp);
548 OPENSSL_free(keyToken);
552 static int cca_rsa_pub_enc(int flen, const unsigned char *from,
553 unsigned char *to, RSA *rsa, int padding)
558 long exitDataLength = 0;
559 unsigned char exitData[8];
560 long ruleArrayLength = 1;
561 unsigned char ruleArray[8] = "PKCS-1.2";
562 long dataStructureLength = 0;
563 unsigned char dataStructure[8];
564 long outputLength = RSA_size(rsa);
566 unsigned char *keyToken = (unsigned char *)RSA_get_ex_data(rsa, hndidx);
568 keyTokenLength = *(long *)keyToken;
569 keyToken += sizeof(long);
571 pkaEncrypt(&returnCode, &reasonCode, &exitDataLength, exitData,
572 &ruleArrayLength, ruleArray, &lflen, (unsigned char *)from,
573 &dataStructureLength, dataStructure, &keyTokenLength,
574 keyToken, &outputLength, to);
576 if (returnCode || reasonCode)
577 return -(returnCode << 16 | reasonCode);
581 static int cca_rsa_priv_dec(int flen, const unsigned char *from,
582 unsigned char *to, RSA *rsa, int padding)
587 long exitDataLength = 0;
588 unsigned char exitData[8];
589 long ruleArrayLength = 1;
590 unsigned char ruleArray[8] = "PKCS-1.2";
591 long dataStructureLength = 0;
592 unsigned char dataStructure[8];
593 long outputLength = RSA_size(rsa);
595 unsigned char *keyToken = (unsigned char *)RSA_get_ex_data(rsa, hndidx);
597 keyTokenLength = *(long *)keyToken;
598 keyToken += sizeof(long);
600 pkaDecrypt(&returnCode, &reasonCode, &exitDataLength, exitData,
601 &ruleArrayLength, ruleArray, &lflen, (unsigned char *)from,
602 &dataStructureLength, dataStructure, &keyTokenLength,
603 keyToken, &outputLength, to);
605 return (returnCode | reasonCode) ? 0 : 1;
608 # define SSL_SIG_LEN 36
610 static int cca_rsa_verify(int type, const unsigned char *m,
611 unsigned int m_len, const unsigned char *sigbuf,
612 unsigned int siglen, const RSA *rsa)
616 long lsiglen = siglen;
617 long exitDataLength = 0;
618 unsigned char exitData[8];
619 long ruleArrayLength = 1;
620 unsigned char ruleArray[8] = "PKCS-1.1";
622 unsigned char *keyToken = (unsigned char *)RSA_get_ex_data(rsa, hndidx);
623 long length = SSL_SIG_LEN;
625 unsigned char *hashBuffer = NULL;
628 X509_ALGOR algorithm;
629 ASN1_OCTET_STRING digest;
631 keyTokenLength = *(long *)keyToken;
632 keyToken += sizeof(long);
634 if (type == NID_md5 || type == NID_sha1) {
635 sig.algor = &algorithm;
636 algorithm.algorithm = OBJ_nid2obj(type);
638 if (!algorithm.algorithm) {
639 CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
640 CCA4758_R_UNKNOWN_ALGORITHM_TYPE);
644 if (!OBJ_length(algorithm.algorithm)) {
645 CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
646 CCA4758_R_ASN1_OID_UNKNOWN_FOR_MD);
650 parameter.type = V_ASN1_NULL;
651 parameter.value.ptr = NULL;
652 algorithm.parameter = ¶meter;
654 sig.digest = &digest;
655 sig.digest->data = (unsigned char *)m;
656 sig.digest->length = m_len;
658 length = i2d_X509_SIG(&sig, NULL);
661 keyLength = RSA_size(rsa);
663 if (length - RSA_PKCS1_PADDING > keyLength) {
664 CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
665 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
671 if (m_len != SSL_SIG_LEN) {
672 CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
673 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
677 hashBuffer = (unsigned char *)m;
683 ptr = hashBuffer = OPENSSL_malloc((unsigned int)keyLength + 1);
685 CCA4758err(CCA4758_F_CCA_RSA_VERIFY, ERR_R_MALLOC_FAILURE);
689 i2d_X509_SIG(&sig, &ptr);
695 ptr = hashBuffer = OPENSSL_malloc((unsigned int)keyLength + 1);
697 CCA4758err(CCA4758_F_CCA_RSA_VERIFY, ERR_R_MALLOC_FAILURE);
700 i2d_X509_SIG(&sig, &ptr);
707 digitalSignatureVerify(&returnCode, &reasonCode, &exitDataLength,
708 exitData, &ruleArrayLength, ruleArray,
709 &keyTokenLength, keyToken, &length, hashBuffer,
710 &lsiglen, (unsigned char *)sigbuf);
712 if (type == NID_sha1 || type == NID_md5)
713 OPENSSL_clear_free(hashBuffer, keyLength + 1);
715 return ((returnCode || reasonCode) ? 0 : 1);
718 # define SSL_SIG_LEN 36
720 static int cca_rsa_sign(int type, const unsigned char *m, unsigned int m_len,
721 unsigned char *sigret, unsigned int *siglen,
726 long exitDataLength = 0;
727 unsigned char exitData[8];
728 long ruleArrayLength = 1;
729 unsigned char ruleArray[8] = "PKCS-1.1";
730 long outputLength = 256;
731 long outputBitLength;
733 unsigned char *hashBuffer = NULL;
734 unsigned char *keyToken = (unsigned char *)RSA_get_ex_data(rsa, hndidx);
735 long length = SSL_SIG_LEN;
739 X509_ALGOR algorithm;
740 ASN1_OCTET_STRING digest;
742 keyTokenLength = *(long *)keyToken;
743 keyToken += sizeof(long);
745 if (type == NID_md5 || type == NID_sha1) {
746 sig.algor = &algorithm;
747 algorithm.algorithm = OBJ_nid2obj(type);
749 if (!algorithm.algorithm) {
750 CCA4758err(CCA4758_F_CCA_RSA_SIGN,
751 CCA4758_R_UNKNOWN_ALGORITHM_TYPE);
755 if (!OBJ_length(algorithm.algorithm)) {
756 CCA4758err(CCA4758_F_CCA_RSA_SIGN,
757 CCA4758_R_ASN1_OID_UNKNOWN_FOR_MD);
761 parameter.type = V_ASN1_NULL;
762 parameter.value.ptr = NULL;
763 algorithm.parameter = ¶meter;
765 sig.digest = &digest;
766 sig.digest->data = (unsigned char *)m;
767 sig.digest->length = m_len;
769 length = i2d_X509_SIG(&sig, NULL);
772 keyLength = RSA_size(rsa);
774 if (length - RSA_PKCS1_PADDING > keyLength) {
775 CCA4758err(CCA4758_F_CCA_RSA_SIGN,
776 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
782 if (m_len != SSL_SIG_LEN) {
783 CCA4758err(CCA4758_F_CCA_RSA_SIGN,
784 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
787 hashBuffer = (unsigned char *)m;
793 ptr = hashBuffer = OPENSSL_malloc((unsigned int)keyLength + 1);
795 CCA4758err(CCA4758_F_CCA_RSA_SIGN, ERR_R_MALLOC_FAILURE);
798 i2d_X509_SIG(&sig, &ptr);
804 ptr = hashBuffer = OPENSSL_malloc((unsigned int)keyLength + 1);
806 CCA4758err(CCA4758_F_CCA_RSA_SIGN, ERR_R_MALLOC_FAILURE);
809 i2d_X509_SIG(&sig, &ptr);
816 digitalSignatureGenerate(&returnCode, &reasonCode, &exitDataLength,
817 exitData, &ruleArrayLength, ruleArray,
818 &keyTokenLength, keyToken, &length, hashBuffer,
819 &outputLength, &outputBitLength, sigret);
821 if (type == NID_sha1 || type == NID_md5)
822 OPENSSL_clear_free(hashBuffer, keyLength + 1);
824 *siglen = outputLength;
826 return ((returnCode || reasonCode) ? 0 : 1);
829 static int getModulusAndExponent(const unsigned char *token,
830 long *exponentLength,
831 unsigned char *exponent, long *modulusLength,
832 long *modulusFieldLength,
833 unsigned char *modulus)
837 if (*token++ != (char)0x1E) /* internal PKA token? */
840 if (*token++) /* token version must be zero */
845 len |= (unsigned char)*token++;
847 token += 4; /* skip reserved bytes */
849 if (*token++ == (char)0x04) {
850 if (*token++) /* token version must be zero */
855 len |= (unsigned char)*token++;
857 token += 2; /* skip reserved section */
861 len |= (unsigned char)*token++;
863 *exponentLength = len;
867 len |= (unsigned char)*token++;
869 *modulusLength = len;
873 len |= (unsigned char)*token++;
875 *modulusFieldLength = len;
877 memcpy(exponent, token, *exponentLength);
878 token += *exponentLength;
880 memcpy(modulus, token, *modulusFieldLength);
886 # endif /* OPENSSL_NO_RSA */
888 static int cca_random_status(void)
893 static int cca_get_random_bytes(unsigned char *buf, int num)
897 long exit_data_length;
898 unsigned char exit_data[4];
899 unsigned char form[] = "RANDOM ";
900 unsigned char rand_buf[8];
902 while (num >= (int)sizeof(rand_buf)) {
903 randomNumberGenerate(&ret_code, &reason_code, &exit_data_length,
904 exit_data, form, rand_buf);
907 num -= sizeof(rand_buf);
908 memcpy(buf, rand_buf, sizeof(rand_buf));
909 buf += sizeof(rand_buf);
913 randomNumberGenerate(&ret_code, &reason_code, NULL, NULL,
917 memcpy(buf, rand_buf, num);
923 # ifndef OPENSSL_NO_RSA
924 static void cca_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad, int idx,
925 long argl, void *argp)
932 /* Goo to handle building as a dynamic engine */
933 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
934 static int bind_fn(ENGINE *e, const char *id)
936 if (id && (strcmp(id, engine_4758_cca_id) != 0) &&
937 (strcmp(id, engine_4758_cca_id_alt) != 0))
944 IMPLEMENT_DYNAMIC_CHECK_FN()
945 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
946 # endif /* OPENSSL_NO_DYNAMIC_ENGINE */
947 # endif /* !OPENSSL_NO_HW_4758_CCA */
948 #endif /* !OPENSSL_NO_HW */