64 #ifndef OPENSSL_NO_RSA
70 #ifndef OPENSSL_NO_HW_4758_CCA
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,
void (*
f)(
void));
87 #ifndef OPENSSL_NO_RSA
88 static int cca_rsa_pub_enc(
int flen,
const unsigned char *from,
89 unsigned char *to,
RSA *rsa,
int padding);
90 static int cca_rsa_priv_dec(
int flen,
const unsigned char *from,
91 unsigned char *to,
RSA *rsa,
int padding);
92 static int cca_rsa_sign(
int type,
const unsigned char *m,
unsigned int m_len,
93 unsigned char *sigret,
unsigned int *siglen,
const RSA *rsa);
94 static int cca_rsa_verify(
int dtype,
const unsigned char *m,
unsigned int m_len,
95 const unsigned char *sigbuf,
unsigned int siglen,
const RSA *rsa);
100 UI_METHOD *ui_method,
void *callback_data);
102 UI_METHOD *ui_method,
void *callback_data);
104 static int getModulusAndExponent(
const unsigned char *token,
long *exponentLength,
105 unsigned char *exponent,
long *modulusLength,
106 long *modulusFieldLength,
unsigned char *modulus);
111 static int cca_get_random_bytes(
unsigned char*,
int);
112 static int cca_random_status(
void);
114 #ifndef OPENSSL_NO_RSA
116 int idx,
long argl,
void *argp);
121 #ifndef OPENSSL_NO_RSA
122 static F_KEYRECORDREAD keyRecordRead;
123 static F_DIGITALSIGNATUREGENERATE digitalSignatureGenerate;
126 static F_PKAENCRYPT pkaEncrypt;
127 static F_PKADECRYPT pkaDecrypt;
129 static F_RANDOMNUMBERGENERATE randomNumberGenerate;
133 static const char *CCA4758_LIB_NAME = NULL;
134 static const char *get_CCA4758_LIB_NAME(
void)
137 return CCA4758_LIB_NAME;
140 static void free_CCA4758_LIB_NAME(
void)
144 CCA4758_LIB_NAME = NULL;
146 static long set_CCA4758_LIB_NAME(
const char *name)
148 free_CCA4758_LIB_NAME();
149 return (((CCA4758_LIB_NAME =
BUF_strdup(name)) != NULL) ? 1 : 0);
151 #ifndef OPENSSL_NO_RSA
152 static const char* n_keyRecordRead =
CSNDKRR;
153 static const char* n_digitalSignatureGenerate =
CSNDDSG;
154 static const char* n_digitalSignatureVerify =
CSNDDSV;
155 static const char* n_publicKeyExtract =
CSNDPKX;
156 static const char* n_pkaEncrypt =
CSNDPKE;
157 static const char* n_pkaDecrypt =
CSNDPKD;
159 static const char* n_randomNumberGenerate =
CSNBRNG;
161 #ifndef OPENSSL_NO_RSA
162 static int hndidx = -1;
164 static DSO *dso = NULL;
169 #define CCA4758_CMD_SO_PATH ENGINE_CMD_BASE
173 "Specifies the path to the '4758cca' shared library",
178 #ifndef OPENSSL_NO_RSA
181 "IBM 4758 CCA RSA method",
202 cca_get_random_bytes,
205 cca_get_random_bytes,
209 static const char *engine_4758_cca_id =
"4758cca";
210 static const char *engine_4758_cca_name =
"IBM 4758 CCA hardware engine support";
211 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
213 static const char *engine_4758_cca_id_alt =
"4758_cca";
218 static int bind_helper(
ENGINE *e)
222 #ifndef OPENSSL_NO_RSA
230 #ifndef OPENSSL_NO_RSA
237 ERR_load_CCA4758_strings();
241 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
242 static ENGINE *engine_4758_cca(
void)
247 if(!bind_helper(ret))
257 ENGINE *e_4758 = engine_4758_cca();
265 static int ibm_4758_cca_destroy(
ENGINE *e)
267 ERR_unload_CCA4758_strings();
268 free_CCA4758_LIB_NAME();
272 static int ibm_4758_cca_init(
ENGINE *e)
280 dso =
DSO_load(NULL, get_CCA4758_LIB_NAME(), NULL, 0);
287 #ifndef OPENSSL_NO_RSA
288 if(!(keyRecordRead = (F_KEYRECORDREAD)
290 !(randomNumberGenerate = (F_RANDOMNUMBERGENERATE)
292 !(digitalSignatureGenerate = (F_DIGITALSIGNATUREGENERATE)
298 !(pkaEncrypt = (F_PKAENCRYPT)
300 !(pkaDecrypt = (F_PKADECRYPT)
307 if(!(randomNumberGenerate = (F_RANDOMNUMBERGENERATE)
315 #ifndef OPENSSL_NO_RSA
317 NULL, NULL, cca_ex_free);
326 #ifndef OPENSSL_NO_RSA
327 keyRecordRead = (F_KEYRECORDREAD)0;
328 digitalSignatureGenerate = (F_DIGITALSIGNATUREGENERATE)0;
331 pkaEncrypt = (F_PKAENCRYPT)0;
332 pkaDecrypt = (F_PKADECRYPT)0;
334 randomNumberGenerate = (F_RANDOMNUMBERGENERATE)0;
338 static int ibm_4758_cca_finish(
ENGINE *e)
340 free_CCA4758_LIB_NAME();
354 #ifndef OPENSSL_NO_RSA
355 keyRecordRead = (F_KEYRECORDREAD)0;
356 randomNumberGenerate = (F_RANDOMNUMBERGENERATE)0;
357 digitalSignatureGenerate = (F_DIGITALSIGNATUREGENERATE)0;
360 pkaEncrypt = (F_PKAENCRYPT)0;
361 pkaDecrypt = (F_PKADECRYPT)0;
363 randomNumberGenerate = (F_RANDOMNUMBERGENERATE)0;
367 static int ibm_4758_cca_ctrl(
ENGINE *e,
int cmd,
long i,
void *p,
void (*
f)(
void))
369 int initialised = ((dso == NULL) ? 0 : 1);
385 return set_CCA4758_LIB_NAME((
const char *)p);
394 #ifndef OPENSSL_NO_RSA
396 #define MAX_CCA_PKA_TOKEN_SIZE 2500
398 static EVP_PKEY *ibm_4758_load_privkey(
ENGINE* e,
const char* key_id,
399 UI_METHOD *ui_method,
void *callback_data)
403 unsigned char* keyToken = NULL;
409 long exitDataLength = 0;
410 long ruleArrayLength = 0;
411 unsigned char exitData[8];
412 unsigned char ruleArray[8];
413 unsigned char keyLabel[64];
414 unsigned long keyLabelLength = strlen(key_id);
415 unsigned char modulus[256];
416 long modulusFieldLength =
sizeof(modulus);
417 long modulusLength = 0;
418 unsigned char exponent[256];
419 long exponentLength =
sizeof(exponent);
421 if (keyLabelLength >
sizeof(keyLabel))
428 memset(keyLabel,
' ',
sizeof(keyLabel));
429 memcpy(keyLabel, key_id, keyLabelLength);
439 keyRecordRead(&returnCode, &reasonCode, &exitDataLength,
440 exitData, &ruleArrayLength, ruleArray, keyLabel,
441 &keyTokenLength, keyToken+
sizeof(
long));
450 publicKeyExtract(&returnCode, &reasonCode, &exitDataLength,
451 exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
452 keyToken+
sizeof(
long), &pubKeyTokenLength, pubKeyToken);
461 if (!getModulusAndExponent(pubKeyToken, &exponentLength,
462 exponent, &modulusLength, &modulusFieldLength,
470 (*(
long*)keyToken) = keyTokenLength;
474 rtmp->
e =
BN_bin2bn(exponent, exponentLength, NULL);
475 rtmp->
n =
BN_bin2bn(modulus, modulusFieldLength, NULL);
488 static EVP_PKEY *ibm_4758_load_pubkey(
ENGINE* e,
const char* key_id,
489 UI_METHOD *ui_method,
void *callback_data)
493 unsigned char* keyToken = NULL;
497 long exitDataLength = 0;
498 long ruleArrayLength = 0;
499 unsigned char exitData[8];
500 unsigned char ruleArray[8];
501 unsigned char keyLabel[64];
502 unsigned long keyLabelLength = strlen(key_id);
503 unsigned char modulus[512];
504 long modulusFieldLength =
sizeof(modulus);
505 long modulusLength = 0;
506 unsigned char exponent[512];
507 long exponentLength =
sizeof(exponent);
509 if (keyLabelLength >
sizeof(keyLabel))
516 memset(keyLabel,
' ',
sizeof(keyLabel));
517 memcpy(keyLabel, key_id, keyLabelLength);
527 keyRecordRead(&returnCode, &reasonCode, &exitDataLength, exitData,
528 &ruleArrayLength, ruleArray, keyLabel, &keyTokenLength,
529 keyToken+
sizeof(
long));
538 if (!getModulusAndExponent(keyToken+
sizeof(
long), &exponentLength,
539 exponent, &modulusLength, &modulusFieldLength, modulus))
546 (*(
long*)keyToken) = keyTokenLength;
549 rtmp->
e =
BN_bin2bn(exponent, exponentLength, NULL);
550 rtmp->
n =
BN_bin2bn(modulus, modulusFieldLength, NULL);
562 static int cca_rsa_pub_enc(
int flen,
const unsigned char *from,
563 unsigned char *to,
RSA *rsa,
int padding)
568 long exitDataLength = 0;
569 unsigned char exitData[8];
570 long ruleArrayLength = 1;
571 unsigned char ruleArray[8] =
"PKCS-1.2";
572 long dataStructureLength = 0;
573 unsigned char dataStructure[8];
576 unsigned char* keyToken = (
unsigned char*)
RSA_get_ex_data(rsa, hndidx);
578 keyTokenLength = *(
long*)keyToken;
579 keyToken+=
sizeof(long);
581 pkaEncrypt(&returnCode, &reasonCode, &exitDataLength, exitData,
582 &ruleArrayLength, ruleArray, &lflen, (
unsigned char*)from,
583 &dataStructureLength, dataStructure, &keyTokenLength,
584 keyToken, &outputLength, to);
586 if (returnCode || reasonCode)
587 return -(returnCode << 16 | reasonCode);
591 static int cca_rsa_priv_dec(
int flen,
const unsigned char *from,
592 unsigned char *to,
RSA *rsa,
int padding)
597 long exitDataLength = 0;
598 unsigned char exitData[8];
599 long ruleArrayLength = 1;
600 unsigned char ruleArray[8] =
"PKCS-1.2";
601 long dataStructureLength = 0;
602 unsigned char dataStructure[8];
605 unsigned char* keyToken = (
unsigned char*)
RSA_get_ex_data(rsa, hndidx);
607 keyTokenLength = *(
long*)keyToken;
608 keyToken+=
sizeof(long);
610 pkaDecrypt(&returnCode, &reasonCode, &exitDataLength, exitData,
611 &ruleArrayLength, ruleArray, &lflen, (
unsigned char*)from,
612 &dataStructureLength, dataStructure, &keyTokenLength,
613 keyToken, &outputLength, to);
615 return (returnCode | reasonCode) ? 0 : 1;
618 #define SSL_SIG_LEN 36
620 static int cca_rsa_verify(
int type,
const unsigned char *m,
unsigned int m_len,
621 const unsigned char *sigbuf,
unsigned int siglen,
const RSA *rsa)
625 long lsiglen = siglen;
626 long exitDataLength = 0;
627 unsigned char exitData[8];
628 long ruleArrayLength = 1;
629 unsigned char ruleArray[8] =
"PKCS-1.1";
631 unsigned char* keyToken = (
unsigned char*)
RSA_get_ex_data(rsa, hndidx);
634 unsigned char *hashBuffer = NULL;
640 keyTokenLength = *(
long*)keyToken;
641 keyToken+=
sizeof(long);
645 sig.
algor = &algorithm;
670 length = i2d_X509_SIG(&sig, NULL);
692 hashBuffer = (
unsigned char *)m;
699 (
unsigned int)keyLength+1);
707 i2d_X509_SIG(&sig, &ptr);
714 (
unsigned int)keyLength+1);
721 i2d_X509_SIG(&sig, &ptr);
728 digitalSignatureVerify(&returnCode, &reasonCode, &exitDataLength,
729 exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
730 keyToken, &length, hashBuffer, &lsiglen,
731 (
unsigned char *)sigbuf);
739 return ((returnCode || reasonCode) ? 0 : 1);
742 #define SSL_SIG_LEN 36
744 static int cca_rsa_sign(
int type,
const unsigned char *m,
unsigned int m_len,
745 unsigned char *sigret,
unsigned int *siglen,
const RSA *rsa)
749 long exitDataLength = 0;
750 unsigned char exitData[8];
751 long ruleArrayLength = 1;
752 unsigned char ruleArray[8] =
"PKCS-1.1";
753 long outputLength=256;
754 long outputBitLength;
756 unsigned char *hashBuffer = NULL;
757 unsigned char* keyToken = (
unsigned char*)
RSA_get_ex_data(rsa, hndidx);
765 keyTokenLength = *(
long*)keyToken;
766 keyToken+=
sizeof(long);
770 sig.
algor = &algorithm;
795 length = i2d_X509_SIG(&sig, NULL);
816 hashBuffer = (
unsigned char*)m;
823 (
unsigned int)keyLength+1);
830 i2d_X509_SIG(&sig, &ptr);
837 (
unsigned int)keyLength+1);
844 i2d_X509_SIG(&sig, &ptr);
851 digitalSignatureGenerate(&returnCode, &reasonCode, &exitDataLength,
852 exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
853 keyToken, &length, hashBuffer, &outputLength, &outputBitLength,
862 *siglen = outputLength;
864 return ((returnCode || reasonCode) ? 0 : 1);
867 static int getModulusAndExponent(
const unsigned char*token,
long *exponentLength,
868 unsigned char *exponent,
long *modulusLength,
long *modulusFieldLength,
869 unsigned char *modulus)
873 if (*token++ != (
char)0x1E)
881 len |= (
unsigned char)*token++;
885 if (*token++ == (
char)0x04)
892 len |= (
unsigned char)*token++;
898 len |= (
unsigned char)*token++;
900 *exponentLength =
len;
904 len |= (
unsigned char)*token++;
906 *modulusLength =
len;
910 len |= (
unsigned char)*token++;
912 *modulusFieldLength =
len;
914 memcpy(exponent, token, *exponentLength);
915 token+= *exponentLength;
917 memcpy(modulus, token, *modulusFieldLength);
925 static int cca_random_status(
void)
930 static int cca_get_random_bytes(
unsigned char* buf,
int num)
936 unsigned char form[] =
"RANDOM ";
937 unsigned char rand_buf[8];
939 while(num >= (
int)
sizeof(rand_buf))
941 randomNumberGenerate(&ret_code, &reason_code, &exit_data_length,
942 exit_data, form, rand_buf);
945 num -=
sizeof(rand_buf);
946 memcpy(buf, rand_buf,
sizeof(rand_buf));
947 buf +=
sizeof(rand_buf);
952 randomNumberGenerate(&ret_code, &reason_code, NULL, NULL,
956 memcpy(buf, rand_buf, num);
962 #ifndef OPENSSL_NO_RSA
964 long argl,
void *argp)
972 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
973 static int bind_fn(
ENGINE *e,
const char *
id)
975 if(
id && (strcmp(
id, engine_4758_cca_id) != 0) &&
976 (strcmp(
id, engine_4758_cca_id_alt) != 0))