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
, 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
);
486 static EVP_PKEY
*ibm_4758_load_pubkey(ENGINE
*e
, const char *key_id
,
487 UI_METHOD
*ui_method
,
491 EVP_PKEY
*res
= NULL
;
492 unsigned char *keyToken
= NULL
;
493 long keyTokenLength
= MAX_CCA_PKA_TOKEN_SIZE
;
496 long exitDataLength
= 0;
497 long ruleArrayLength
= 0;
498 unsigned char exitData
[8];
499 unsigned char ruleArray
[8];
500 unsigned char keyLabel
[64];
501 unsigned long keyLabelLength
= strlen(key_id
);
502 unsigned char modulus
[512];
503 long modulusFieldLength
= sizeof(modulus
);
504 long modulusLength
= 0;
505 unsigned char exponent
[512];
506 long exponentLength
= sizeof(exponent
);
508 if (keyLabelLength
> sizeof(keyLabel
)) {
509 CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY
,
510 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL
);
514 memset(keyLabel
, ' ', sizeof(keyLabel
));
515 memcpy(keyLabel
, key_id
, keyLabelLength
);
517 keyToken
= OPENSSL_malloc(MAX_CCA_PKA_TOKEN_SIZE
+ sizeof(long));
519 CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY
, ERR_R_MALLOC_FAILURE
);
523 keyRecordRead(&returnCode
, &reasonCode
, &exitDataLength
, exitData
,
524 &ruleArrayLength
, ruleArray
, keyLabel
, &keyTokenLength
,
525 keyToken
+ sizeof(long));
528 CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY
, ERR_R_MALLOC_FAILURE
);
532 if (!getModulusAndExponent(keyToken
+ sizeof(long), &exponentLength
,
533 exponent
, &modulusLength
, &modulusFieldLength
,
535 CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY
,
536 CCA4758_R_FAILED_LOADING_PUBLIC_KEY
);
540 (*(long *)keyToken
) = keyTokenLength
;
541 rtmp
= RSA_new_method(e
);
542 RSA_set_ex_data(rtmp
, hndidx
, (char *)keyToken
);
543 rtmp
->e
= BN_bin2bn(exponent
, exponentLength
, NULL
);
544 rtmp
->n
= BN_bin2bn(modulus
, modulusFieldLength
, NULL
);
545 rtmp
->flags
|= RSA_FLAG_EXT_PKEY
;
546 res
= EVP_PKEY_new();
547 EVP_PKEY_assign_RSA(res
, rtmp
);
552 OPENSSL_free(keyToken
);
560 static int cca_rsa_pub_enc(int flen
, const unsigned char *from
,
561 unsigned char *to
, RSA
*rsa
, int padding
)
566 long exitDataLength
= 0;
567 unsigned char exitData
[8];
568 long ruleArrayLength
= 1;
569 unsigned char ruleArray
[8] = "PKCS-1.2";
570 long dataStructureLength
= 0;
571 unsigned char dataStructure
[8];
572 long outputLength
= RSA_size(rsa
);
574 unsigned char *keyToken
= (unsigned char *)RSA_get_ex_data(rsa
, hndidx
);
576 keyTokenLength
= *(long *)keyToken
;
577 keyToken
+= sizeof(long);
579 pkaEncrypt(&returnCode
, &reasonCode
, &exitDataLength
, exitData
,
580 &ruleArrayLength
, ruleArray
, &lflen
, (unsigned char *)from
,
581 &dataStructureLength
, dataStructure
, &keyTokenLength
,
582 keyToken
, &outputLength
, to
);
584 if (returnCode
|| reasonCode
)
585 return -(returnCode
<< 16 | reasonCode
);
589 static int cca_rsa_priv_dec(int flen
, const unsigned char *from
,
590 unsigned char *to
, RSA
*rsa
, int padding
)
595 long exitDataLength
= 0;
596 unsigned char exitData
[8];
597 long ruleArrayLength
= 1;
598 unsigned char ruleArray
[8] = "PKCS-1.2";
599 long dataStructureLength
= 0;
600 unsigned char dataStructure
[8];
601 long outputLength
= RSA_size(rsa
);
603 unsigned char *keyToken
= (unsigned char *)RSA_get_ex_data(rsa
, hndidx
);
605 keyTokenLength
= *(long *)keyToken
;
606 keyToken
+= sizeof(long);
608 pkaDecrypt(&returnCode
, &reasonCode
, &exitDataLength
, exitData
,
609 &ruleArrayLength
, ruleArray
, &lflen
, (unsigned char *)from
,
610 &dataStructureLength
, dataStructure
, &keyTokenLength
,
611 keyToken
, &outputLength
, to
);
613 return (returnCode
| reasonCode
) ? 0 : 1;
616 # define SSL_SIG_LEN 36
618 static int cca_rsa_verify(int type
, const unsigned char *m
,
619 unsigned int m_len
, unsigned char *sigbuf
,
620 unsigned int siglen
, const RSA
*rsa
)
624 long lsiglen
= siglen
;
625 long exitDataLength
= 0;
626 unsigned char exitData
[8];
627 long ruleArrayLength
= 1;
628 unsigned char ruleArray
[8] = "PKCS-1.1";
630 unsigned char *keyToken
= (unsigned char *)RSA_get_ex_data(rsa
, hndidx
);
631 long length
= SSL_SIG_LEN
;
633 unsigned char *hashBuffer
= NULL
;
636 X509_ALGOR algorithm
;
637 ASN1_OCTET_STRING digest
;
639 keyTokenLength
= *(long *)keyToken
;
640 keyToken
+= sizeof(long);
642 if (type
== NID_md5
|| type
== NID_sha1
) {
643 sig
.algor
= &algorithm
;
644 algorithm
.algorithm
= OBJ_nid2obj(type
);
646 if (!algorithm
.algorithm
) {
647 CCA4758err(CCA4758_F_CCA_RSA_VERIFY
,
648 CCA4758_R_UNKNOWN_ALGORITHM_TYPE
);
652 if (!algorithm
.algorithm
->length
) {
653 CCA4758err(CCA4758_F_CCA_RSA_VERIFY
,
654 CCA4758_R_ASN1_OID_UNKNOWN_FOR_MD
);
658 parameter
.type
= V_ASN1_NULL
;
659 parameter
.value
.ptr
= NULL
;
660 algorithm
.parameter
= ¶meter
;
662 sig
.digest
= &digest
;
663 sig
.digest
->data
= (unsigned char *)m
;
664 sig
.digest
->length
= m_len
;
666 length
= i2d_X509_SIG(&sig
, NULL
);
669 keyLength
= RSA_size(rsa
);
671 if (length
- RSA_PKCS1_PADDING
> keyLength
) {
672 CCA4758err(CCA4758_F_CCA_RSA_VERIFY
,
673 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL
);
679 if (m_len
!= SSL_SIG_LEN
) {
680 CCA4758err(CCA4758_F_CCA_RSA_VERIFY
,
681 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL
);
685 hashBuffer
= (unsigned char *)m
;
691 ptr
= hashBuffer
= OPENSSL_malloc((unsigned int)keyLength
+ 1);
693 CCA4758err(CCA4758_F_CCA_RSA_VERIFY
, ERR_R_MALLOC_FAILURE
);
697 i2d_X509_SIG(&sig
, &ptr
);
703 ptr
= hashBuffer
= OPENSSL_malloc((unsigned int)keyLength
+ 1);
705 CCA4758err(CCA4758_F_CCA_RSA_VERIFY
, ERR_R_MALLOC_FAILURE
);
708 i2d_X509_SIG(&sig
, &ptr
);
715 digitalSignatureVerify(&returnCode
, &reasonCode
, &exitDataLength
,
716 exitData
, &ruleArrayLength
, ruleArray
,
717 &keyTokenLength
, keyToken
, &length
, hashBuffer
,
720 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
,
736 long exitDataLength
= 0;
737 unsigned char exitData
[8];
738 long ruleArrayLength
= 1;
739 unsigned char ruleArray
[8] = "PKCS-1.1";
740 long outputLength
= 256;
741 long outputBitLength
;
743 unsigned char *hashBuffer
= NULL
;
744 unsigned char *keyToken
= (unsigned char *)RSA_get_ex_data(rsa
, hndidx
);
745 long length
= SSL_SIG_LEN
;
749 X509_ALGOR algorithm
;
750 ASN1_OCTET_STRING digest
;
752 keyTokenLength
= *(long *)keyToken
;
753 keyToken
+= sizeof(long);
755 if (type
== NID_md5
|| type
== NID_sha1
) {
756 sig
.algor
= &algorithm
;
757 algorithm
.algorithm
= OBJ_nid2obj(type
);
759 if (!algorithm
.algorithm
) {
760 CCA4758err(CCA4758_F_CCA_RSA_SIGN
,
761 CCA4758_R_UNKNOWN_ALGORITHM_TYPE
);
765 if (!algorithm
.algorithm
->length
) {
766 CCA4758err(CCA4758_F_CCA_RSA_SIGN
,
767 CCA4758_R_ASN1_OID_UNKNOWN_FOR_MD
);
771 parameter
.type
= V_ASN1_NULL
;
772 parameter
.value
.ptr
= NULL
;
773 algorithm
.parameter
= ¶meter
;
775 sig
.digest
= &digest
;
776 sig
.digest
->data
= (unsigned char *)m
;
777 sig
.digest
->length
= m_len
;
779 length
= i2d_X509_SIG(&sig
, NULL
);
782 keyLength
= RSA_size(rsa
);
784 if (length
- RSA_PKCS1_PADDING
> keyLength
) {
785 CCA4758err(CCA4758_F_CCA_RSA_SIGN
,
786 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL
);
792 if (m_len
!= SSL_SIG_LEN
) {
793 CCA4758err(CCA4758_F_CCA_RSA_SIGN
,
794 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL
);
797 hashBuffer
= (unsigned char *)m
;
803 ptr
= hashBuffer
= OPENSSL_malloc((unsigned int)keyLength
+ 1);
805 CCA4758err(CCA4758_F_CCA_RSA_SIGN
, ERR_R_MALLOC_FAILURE
);
808 i2d_X509_SIG(&sig
, &ptr
);
814 ptr
= hashBuffer
= OPENSSL_malloc((unsigned int)keyLength
+ 1);
816 CCA4758err(CCA4758_F_CCA_RSA_SIGN
, ERR_R_MALLOC_FAILURE
);
819 i2d_X509_SIG(&sig
, &ptr
);
826 digitalSignatureGenerate(&returnCode
, &reasonCode
, &exitDataLength
,
827 exitData
, &ruleArrayLength
, ruleArray
,
828 &keyTokenLength
, keyToken
, &length
, hashBuffer
,
829 &outputLength
, &outputBitLength
, sigret
);
831 if (type
== NID_sha1
|| type
== NID_md5
) {
832 OPENSSL_cleanse(hashBuffer
, keyLength
+ 1);
833 OPENSSL_free(hashBuffer
);
836 *siglen
= outputLength
;
838 return ((returnCode
|| reasonCode
) ? 0 : 1);
841 static int getModulusAndExponent(const unsigned char *token
,
842 long *exponentLength
,
843 unsigned char *exponent
, long *modulusLength
,
844 long *modulusFieldLength
,
845 unsigned char *modulus
)
849 if (*token
++ != (char)0x1E) /* internal PKA token? */
852 if (*token
++) /* token version must be zero */
857 len
|= (unsigned char)*token
++;
859 token
+= 4; /* skip reserved bytes */
861 if (*token
++ == (char)0x04) {
862 if (*token
++) /* token version must be zero */
867 len
|= (unsigned char)*token
++;
869 token
+= 2; /* skip reserved section */
873 len
|= (unsigned char)*token
++;
875 *exponentLength
= len
;
879 len
|= (unsigned char)*token
++;
881 *modulusLength
= len
;
885 len
|= (unsigned char)*token
++;
887 *modulusFieldLength
= len
;
889 memcpy(exponent
, token
, *exponentLength
);
890 token
+= *exponentLength
;
892 memcpy(modulus
, token
, *modulusFieldLength
);
898 # endif /* OPENSSL_NO_RSA */
900 static int cca_random_status(void)
905 static int cca_get_random_bytes(unsigned char *buf
, int num
)
909 long exit_data_length
;
910 unsigned char exit_data
[4];
911 unsigned char form
[] = "RANDOM ";
912 unsigned char rand_buf
[8];
914 while (num
>= (int)sizeof(rand_buf
)) {
915 randomNumberGenerate(&ret_code
, &reason_code
, &exit_data_length
,
916 exit_data
, form
, rand_buf
);
919 num
-= sizeof(rand_buf
);
920 memcpy(buf
, rand_buf
, sizeof(rand_buf
));
921 buf
+= sizeof(rand_buf
);
925 randomNumberGenerate(&ret_code
, &reason_code
, NULL
, NULL
,
929 memcpy(buf
, rand_buf
, num
);
935 # ifndef OPENSSL_NO_RSA
936 static void cca_ex_free(void *obj
, void *item
, CRYPTO_EX_DATA
*ad
, int idx
,
937 long argl
, void *argp
)
944 /* Goo to handle building as a dynamic engine */
945 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
946 static int bind_fn(ENGINE
*e
, const char *id
)
948 if (id
&& (strcmp(id
, engine_4758_cca_id
) != 0) &&
949 (strcmp(id
, engine_4758_cca_id_alt
) != 0))
956 IMPLEMENT_DYNAMIC_CHECK_FN()
957 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn
)
958 # endif /* OPENSSL_NO_DYNAMIC_ENGINE */
959 # endif /* !OPENSSL_NO_HW_4758_CCA */
960 #endif /* !OPENSSL_NO_HW */