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 #ifndef ENGINE_DYNAMIC_SUPPORT
227 static ENGINE
*engine_4758_cca(void)
229 ENGINE
*ret
= ENGINE_new();
232 if(!bind_helper(ret
))
240 void ENGINE_load_4758cca(void)
242 ENGINE
*e_4758
= engine_4758_cca();
250 static int ibm_4758_cca_destroy(ENGINE
*e
)
252 ERR_unload_CCA4758_strings();
253 free_CCA4758_LIB_NAME();
257 static int ibm_4758_cca_init(ENGINE
*e
)
261 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT
,CCA4758_R_ALREADY_LOADED
);
265 dso
= DSO_load(NULL
, get_CCA4758_LIB_NAME(), NULL
, 0);
268 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT
,CCA4758_R_DSO_FAILURE
);
272 #ifndef OPENSSL_NO_RSA
273 if(!(keyRecordRead
= (F_KEYRECORDREAD
)
274 DSO_bind_func(dso
, n_keyRecordRead
)) ||
275 !(randomNumberGenerate
= (F_RANDOMNUMBERGENERATE
)
276 DSO_bind_func(dso
, n_randomNumberGenerate
)) ||
277 !(digitalSignatureGenerate
= (F_DIGITALSIGNATUREGENERATE
)
278 DSO_bind_func(dso
, n_digitalSignatureGenerate
)) ||
279 !(digitalSignatureVerify
= (F_DIGITALSIGNATUREVERIFY
)
280 DSO_bind_func(dso
, n_digitalSignatureVerify
)) ||
281 !(publicKeyExtract
= (F_PUBLICKEYEXTRACT
)
282 DSO_bind_func(dso
, n_publicKeyExtract
)) ||
283 !(pkaEncrypt
= (F_PKAENCRYPT
)
284 DSO_bind_func(dso
, n_pkaEncrypt
)) ||
285 !(pkaDecrypt
= (F_PKADECRYPT
)
286 DSO_bind_func(dso
, n_pkaDecrypt
)))
288 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT
,CCA4758_R_DSO_FAILURE
);
292 if(!(randomNumberGenerate
= (F_RANDOMNUMBERGENERATE
)
293 DSO_bind_func(dso
, n_randomNumberGenerate
)))
295 CCA4758err(CCA4758_F_IBM_4758_CCA_INIT
,CCA4758_R_DSO_FAILURE
);
300 hndidx
= RSA_get_ex_new_index(0, "IBM 4758 CCA RSA key handle",
301 NULL
, NULL
, cca_ex_free
);
309 keyRecordRead
= (F_KEYRECORDREAD
)0;
310 randomNumberGenerate
= (F_RANDOMNUMBERGENERATE
)0;
311 digitalSignatureGenerate
= (F_DIGITALSIGNATUREGENERATE
)0;
312 digitalSignatureVerify
= (F_DIGITALSIGNATUREVERIFY
)0;
313 publicKeyExtract
= (F_PUBLICKEYEXTRACT
)0;
314 pkaEncrypt
= (F_PKAENCRYPT
)0;
315 pkaDecrypt
= (F_PKADECRYPT
)0;
319 static int ibm_4758_cca_finish(ENGINE
*e
)
321 free_CCA4758_LIB_NAME();
324 CCA4758err(CCA4758_F_IBM_4758_CCA_FINISH
,
325 CCA4758_R_NOT_LOADED
);
330 CCA4758err(CCA4758_F_IBM_4758_CCA_FINISH
,
331 CCA4758_R_UNIT_FAILURE
);
335 keyRecordRead
= (F_KEYRECORDREAD
)0;
336 randomNumberGenerate
= (F_RANDOMNUMBERGENERATE
)0;
337 digitalSignatureGenerate
= (F_DIGITALSIGNATUREGENERATE
)0;
338 digitalSignatureVerify
= (F_DIGITALSIGNATUREVERIFY
)0;
339 publicKeyExtract
= (F_PUBLICKEYEXTRACT
)0;
340 pkaEncrypt
= (F_PKAENCRYPT
)0;
341 pkaDecrypt
= (F_PKADECRYPT
)0;
345 static int ibm_4758_cca_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
)())
347 int initialised
= ((dso
== NULL
) ? 0 : 1);
350 case CCA4758_CMD_SO_PATH
:
353 CCA4758err(CCA4758_F_IBM_4758_CCA_CTRL
,
354 ERR_R_PASSED_NULL_PARAMETER
);
359 CCA4758err(CCA4758_F_IBM_4758_CCA_CTRL
,
360 CCA4758_R_ALREADY_LOADED
);
363 return set_CCA4758_LIB_NAME((const char *)p
);
367 CCA4758err(CCA4758_F_IBM_4758_CCA_CTRL
,
368 CCA4758_R_COMMAND_NOT_IMPLEMENTED
);
372 #ifndef OPENSSL_NO_RSA
374 #define MAX_CCA_PKA_TOKEN_SIZE 2500
376 static EVP_PKEY
*ibm_4758_load_privkey(ENGINE
* e
, const char* key_id
,
377 UI_METHOD
*ui_method
, void *callback_data
)
380 EVP_PKEY
*res
= NULL
;
381 unsigned char* keyToken
= NULL
;
382 unsigned char pubKeyToken
[MAX_CCA_PKA_TOKEN_SIZE
];
383 long pubKeyTokenLength
= MAX_CCA_PKA_TOKEN_SIZE
;
384 long keyTokenLength
= MAX_CCA_PKA_TOKEN_SIZE
;
387 long exitDataLength
= 0;
388 long ruleArrayLength
= 0;
389 unsigned char exitData
[8];
390 unsigned char ruleArray
[8];
391 unsigned char keyLabel
[64];
392 long keyLabelLength
= strlen(key_id
);
393 unsigned char modulus
[256];
394 long modulusFieldLength
= sizeof(modulus
);
395 long modulusLength
= 0;
396 unsigned char exponent
[256];
397 long exponentLength
= sizeof(exponent
);
399 if (keyLabelLength
> sizeof(keyLabel
))
401 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY
,
402 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL
);
406 memset(keyLabel
,' ', sizeof(keyLabel
));
407 memcpy(keyLabel
, key_id
, keyLabelLength
);
409 keyToken
= OPENSSL_malloc(MAX_CCA_PKA_TOKEN_SIZE
+ sizeof(long));
412 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY
,
413 ERR_R_MALLOC_FAILURE
);
417 keyRecordRead(&returnCode
, &reasonCode
, &exitDataLength
,
418 exitData
, &ruleArrayLength
, ruleArray
, keyLabel
,
419 &keyTokenLength
, keyToken
+sizeof(long));
423 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY
,
424 CCA4758_R_FAILED_LOADING_PRIVATE_KEY
);
428 publicKeyExtract(&returnCode
, &reasonCode
, &exitDataLength
,
429 exitData
, &ruleArrayLength
, ruleArray
, &keyTokenLength
,
430 keyToken
+sizeof(long), &pubKeyTokenLength
, pubKeyToken
);
434 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY
,
435 CCA4758_R_FAILED_LOADING_PRIVATE_KEY
);
439 if (!getModulusAndExponent(pubKeyToken
, &exponentLength
,
440 exponent
, &modulusLength
, &modulusFieldLength
,
443 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY
,
444 CCA4758_R_FAILED_LOADING_PRIVATE_KEY
);
448 (*(long*)keyToken
) = keyTokenLength
;
449 rtmp
= RSA_new_method(e
);
450 RSA_set_ex_data(rtmp
, hndidx
, (char *)keyToken
);
452 rtmp
->e
= BN_bin2bn(exponent
, exponentLength
, NULL
);
453 rtmp
->n
= BN_bin2bn(modulus
, modulusFieldLength
, NULL
);
454 rtmp
->flags
|= RSA_FLAG_EXT_PKEY
;
456 res
= EVP_PKEY_new();
457 EVP_PKEY_assign_RSA(res
, rtmp
);
462 OPENSSL_free(keyToken
);
470 static EVP_PKEY
*ibm_4758_load_pubkey(ENGINE
* e
, const char* key_id
,
471 UI_METHOD
*ui_method
, void *callback_data
)
474 EVP_PKEY
*res
= NULL
;
475 unsigned char* keyToken
= NULL
;
476 long keyTokenLength
= MAX_CCA_PKA_TOKEN_SIZE
;
479 long exitDataLength
= 0;
480 long ruleArrayLength
= 0;
481 unsigned char exitData
[8];
482 unsigned char ruleArray
[8];
483 unsigned char keyLabel
[64];
484 long keyLabelLength
= strlen(key_id
);
485 unsigned char modulus
[512];
486 long modulusFieldLength
= sizeof(modulus
);
487 long modulusLength
= 0;
488 unsigned char exponent
[512];
489 long exponentLength
= sizeof(exponent
);
491 if (keyLabelLength
> sizeof(keyLabel
))
493 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY
,
494 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL
);
498 memset(keyLabel
,' ', sizeof(keyLabel
));
499 memcpy(keyLabel
, key_id
, keyLabelLength
);
501 keyToken
= OPENSSL_malloc(MAX_CCA_PKA_TOKEN_SIZE
+ sizeof(long));
504 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PUBKEY
,
505 ERR_R_MALLOC_FAILURE
);
509 keyRecordRead(&returnCode
, &reasonCode
, &exitDataLength
, exitData
,
510 &ruleArrayLength
, ruleArray
, keyLabel
, &keyTokenLength
,
511 keyToken
+sizeof(long));
515 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY
,
516 ERR_R_MALLOC_FAILURE
);
520 if (!getModulusAndExponent(keyToken
+sizeof(long), &exponentLength
,
521 exponent
, &modulusLength
, &modulusFieldLength
, modulus
))
523 CCA4758err(CCA4758_F_IBM_4758_CCA_LOAD_PRIVKEY
,
524 CCA4758_R_FAILED_LOADING_PUBLIC_KEY
);
528 (*(long*)keyToken
) = keyTokenLength
;
529 rtmp
= RSA_new_method(e
);
530 RSA_set_ex_data(rtmp
, hndidx
, (char *)keyToken
);
531 rtmp
->e
= BN_bin2bn(exponent
, exponentLength
, NULL
);
532 rtmp
->n
= BN_bin2bn(modulus
, modulusFieldLength
, NULL
);
533 rtmp
->flags
|= RSA_FLAG_EXT_PKEY
;
534 res
= EVP_PKEY_new();
535 EVP_PKEY_assign_RSA(res
, rtmp
);
540 OPENSSL_free(keyToken
);
548 static int cca_rsa_pub_enc(int flen
, const unsigned char *from
,
549 unsigned char *to
, RSA
*rsa
,int padding
)
554 long exitDataLength
= 0;
555 unsigned char exitData
[8];
556 long ruleArrayLength
= 1;
557 unsigned char ruleArray
[8] = "PKCS-1.2";
558 long dataStructureLength
= 0;
559 unsigned char dataStructure
[8];
560 long outputLength
= RSA_size(rsa
);
562 unsigned char* keyToken
= (unsigned char*)RSA_get_ex_data(rsa
, hndidx
);
564 keyTokenLength
= *(long*)keyToken
;
565 keyToken
+=sizeof(long);
567 pkaEncrypt(&returnCode
, &reasonCode
, &exitDataLength
, exitData
,
568 &ruleArrayLength
, ruleArray
, &lflen
, (unsigned char*)from
,
569 &dataStructureLength
, dataStructure
, &keyTokenLength
,
570 keyToken
, &outputLength
, to
);
572 if (returnCode
|| reasonCode
)
573 return -(returnCode
<< 16 | reasonCode
);
577 static int cca_rsa_priv_dec(int flen
, const unsigned char *from
,
578 unsigned char *to
, RSA
*rsa
,int padding
)
583 long exitDataLength
= 0;
584 unsigned char exitData
[8];
585 long ruleArrayLength
= 1;
586 unsigned char ruleArray
[8] = "PKCS-1.2";
587 long dataStructureLength
= 0;
588 unsigned char dataStructure
[8];
589 long outputLength
= RSA_size(rsa
);
591 unsigned char* keyToken
= (unsigned char*)RSA_get_ex_data(rsa
, hndidx
);
593 keyTokenLength
= *(long*)keyToken
;
594 keyToken
+=sizeof(long);
596 pkaDecrypt(&returnCode
, &reasonCode
, &exitDataLength
, exitData
,
597 &ruleArrayLength
, ruleArray
, &lflen
, (unsigned char*)from
,
598 &dataStructureLength
, dataStructure
, &keyTokenLength
,
599 keyToken
, &outputLength
, to
);
601 return (returnCode
| reasonCode
) ? 0 : 1;
604 #define SSL_SIG_LEN 36
606 static int cca_rsa_verify(int type
, const unsigned char *m
, unsigned int m_len
,
607 unsigned char *sigbuf
, unsigned int siglen
, const RSA
*rsa
)
611 long lsiglen
= siglen
;
612 long exitDataLength
= 0;
613 unsigned char exitData
[8];
614 long ruleArrayLength
= 1;
615 unsigned char ruleArray
[8] = "PKCS-1.1";
617 unsigned char* keyToken
= (unsigned char*)RSA_get_ex_data(rsa
, hndidx
);
618 long length
= SSL_SIG_LEN
;
620 unsigned char *hashBuffer
= NULL
;
623 X509_ALGOR algorithm
;
624 ASN1_OCTET_STRING digest
;
626 keyTokenLength
= *(long*)keyToken
;
627 keyToken
+=sizeof(long);
629 if (type
== NID_md5
|| type
== NID_sha1
)
631 sig
.algor
= &algorithm
;
632 algorithm
.algorithm
= OBJ_nid2obj(type
);
634 if (!algorithm
.algorithm
)
636 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY
,
637 CCA4758_R_UNKNOWN_ALGORITHM_TYPE
);
641 if (!algorithm
.algorithm
->length
)
643 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY
,
644 CCA4758_R_ASN1_OID_UNKNOWN_FOR_MD
);
648 parameter
.type
= V_ASN1_NULL
;
649 parameter
.value
.ptr
= NULL
;
650 algorithm
.parameter
= ¶meter
;
652 sig
.digest
= &digest
;
653 sig
.digest
->data
= (unsigned char*)m
;
654 sig
.digest
->length
= m_len
;
656 length
= i2d_X509_SIG(&sig
, NULL
);
659 keyLength
= RSA_size(rsa
);
661 if (length
- RSA_PKCS1_PADDING
> keyLength
)
663 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY
,
664 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL
);
671 if (m_len
!= SSL_SIG_LEN
)
673 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY
,
674 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL
);
678 hashBuffer
= (unsigned char *)m
;
684 ptr
= hashBuffer
= OPENSSL_malloc(
685 (unsigned int)keyLength
+1);
688 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY
,
689 ERR_R_MALLOC_FAILURE
);
693 i2d_X509_SIG(&sig
, &ptr
);
699 ptr
= hashBuffer
= OPENSSL_malloc(
700 (unsigned int)keyLength
+1);
703 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY
,
704 ERR_R_MALLOC_FAILURE
);
707 i2d_X509_SIG(&sig
, &ptr
);
714 digitalSignatureVerify(&returnCode
, &reasonCode
, &exitDataLength
,
715 exitData
, &ruleArrayLength
, ruleArray
, &keyTokenLength
,
716 keyToken
, &length
, hashBuffer
, &lsiglen
, sigbuf
);
718 if (type
== NID_sha1
|| type
== NID_md5
)
720 OPENSSL_cleanse(hashBuffer
, keyLength
+1);
721 OPENSSL_free(hashBuffer
);
724 return ((returnCode
|| reasonCode
) ? 0 : 1);
727 #define SSL_SIG_LEN 36
729 static int cca_rsa_sign(int type
, const unsigned char *m
, unsigned int m_len
,
730 unsigned char *sigret
, unsigned int *siglen
, const RSA
*rsa
)
734 long exitDataLength
= 0;
735 unsigned char exitData
[8];
736 long ruleArrayLength
= 1;
737 unsigned char ruleArray
[8] = "PKCS-1.1";
738 long outputLength
=256;
739 long outputBitLength
;
741 unsigned char *hashBuffer
= NULL
;
742 unsigned char* keyToken
= (unsigned char*)RSA_get_ex_data(rsa
, hndidx
);
743 long length
= SSL_SIG_LEN
;
747 X509_ALGOR algorithm
;
748 ASN1_OCTET_STRING digest
;
750 keyTokenLength
= *(long*)keyToken
;
751 keyToken
+=sizeof(long);
753 if (type
== NID_md5
|| type
== NID_sha1
)
755 sig
.algor
= &algorithm
;
756 algorithm
.algorithm
= OBJ_nid2obj(type
);
758 if (!algorithm
.algorithm
)
760 CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN
,
761 CCA4758_R_UNKNOWN_ALGORITHM_TYPE
);
765 if (!algorithm
.algorithm
->length
)
767 CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN
,
768 CCA4758_R_ASN1_OID_UNKNOWN_FOR_MD
);
772 parameter
.type
= V_ASN1_NULL
;
773 parameter
.value
.ptr
= NULL
;
774 algorithm
.parameter
= ¶meter
;
776 sig
.digest
= &digest
;
777 sig
.digest
->data
= (unsigned char*)m
;
778 sig
.digest
->length
= m_len
;
780 length
= i2d_X509_SIG(&sig
, NULL
);
783 keyLength
= RSA_size(rsa
);
785 if (length
- RSA_PKCS1_PADDING
> keyLength
)
787 CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN
,
788 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL
);
795 if (m_len
!= SSL_SIG_LEN
)
797 CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN
,
798 CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL
);
801 hashBuffer
= (unsigned char*)m
;
807 ptr
= hashBuffer
= OPENSSL_malloc(
808 (unsigned int)keyLength
+1);
811 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY
,
812 ERR_R_MALLOC_FAILURE
);
815 i2d_X509_SIG(&sig
, &ptr
);
821 ptr
= hashBuffer
= OPENSSL_malloc(
822 (unsigned int)keyLength
+1);
825 CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY
,
826 ERR_R_MALLOC_FAILURE
);
829 i2d_X509_SIG(&sig
, &ptr
);
836 digitalSignatureGenerate(&returnCode
, &reasonCode
, &exitDataLength
,
837 exitData
, &ruleArrayLength
, ruleArray
, &keyTokenLength
,
838 keyToken
, &length
, hashBuffer
, &outputLength
, &outputBitLength
,
841 if (type
== NID_sha1
|| type
== NID_md5
)
843 OPENSSL_cleanse(hashBuffer
, keyLength
+1);
844 OPENSSL_free(hashBuffer
);
847 *siglen
= outputLength
;
849 return ((returnCode
|| reasonCode
) ? 0 : 1);
852 static int getModulusAndExponent(const unsigned char*token
, long *exponentLength
,
853 unsigned char *exponent
, long *modulusLength
, long *modulusFieldLength
,
854 unsigned char *modulus
)
858 if (*token
++ != (char)0x1E) /* internal PKA token? */
861 if (*token
++) /* token version must be zero */
866 len
|= (unsigned char)*token
++;
868 token
+= 4; /* skip reserved bytes */
870 if (*token
++ == (char)0x04)
872 if (*token
++) /* token version must be zero */
877 len
|= (unsigned char)*token
++;
879 token
+=2; /* skip reserved section */
883 len
|= (unsigned char)*token
++;
885 *exponentLength
= len
;
889 len
|= (unsigned char)*token
++;
891 *modulusLength
= len
;
895 len
|= (unsigned char)*token
++;
897 *modulusFieldLength
= len
;
899 memcpy(exponent
, token
, *exponentLength
);
900 token
+= *exponentLength
;
902 memcpy(modulus
, token
, *modulusFieldLength
);
908 #endif /* OPENSSL_NO_RSA */
910 static int cca_random_status(void)
915 static int cca_get_random_bytes(unsigned char* buf
, int num
)
919 long exit_data_length
;
920 unsigned char exit_data
[4];
921 unsigned char form
[] = "RANDOM ";
922 unsigned char rand_buf
[8];
924 while(num
>= sizeof(rand_buf
))
926 randomNumberGenerate(&ret_code
, &reason_code
, &exit_data_length
,
927 exit_data
, form
, rand_buf
);
930 num
-= sizeof(rand_buf
);
931 memcpy(buf
, rand_buf
, sizeof(rand_buf
));
932 buf
+= sizeof(rand_buf
);
937 randomNumberGenerate(&ret_code
, &reason_code
, NULL
, NULL
,
941 memcpy(buf
, rand_buf
, num
);
947 static void cca_ex_free(void *obj
, void *item
, CRYPTO_EX_DATA
*ad
, int idx
,
948 long argl
, void *argp
)
954 /* Goo to handle building as a dynamic engine */
955 #ifdef ENGINE_DYNAMIC_SUPPORT
956 static int bind_fn(ENGINE
*e
, const char *id
)
958 if(id
&& (strcmp(id
, engine_4758_cca_id
) != 0))
964 IMPLEMENT_DYNAMIC_CHECK_FN()
965 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn
)
966 #endif /* ENGINE_DYNAMIC_SUPPORT */
968 #endif /* !OPENSSL_NO_HW_4758_CCA */
969 #endif /* !OPENSSL_NO_HW */