68 #ifndef OPENSSL_NO_RSA
77 #ifndef OPENSSL_NO_HW_CHIL
93 #define HWCRHK_LIB_NAME "CHIL engine"
96 static int hwcrhk_destroy(
ENGINE *
e);
97 static int hwcrhk_init(
ENGINE *
e);
98 static int hwcrhk_finish(
ENGINE *
e);
99 static int hwcrhk_ctrl(
ENGINE *
e,
int cmd,
long i,
void *
p,
void (*
f)(
void));
111 #ifndef OPENSSL_NO_RSA
117 static int hwcrhk_rsa_finish(
RSA *rsa);
120 #ifndef OPENSSL_NO_DH
123 static int hwcrhk_mod_exp_dh(
const DH *dh,
BIGNUM *r,
129 static int hwcrhk_rand_bytes(
unsigned char *buf,
int num);
130 static int hwcrhk_rand_status(
void);
133 static EVP_PKEY *hwcrhk_load_privkey(
ENGINE *eng,
const char *key_id,
134 UI_METHOD *ui_method,
void *callback_data);
135 static EVP_PKEY *hwcrhk_load_pubkey(
ENGINE *eng,
const char *key_id,
136 UI_METHOD *ui_method,
void *callback_data);
139 static int hwcrhk_insert_card(
const char *prompt_info,
140 const char *wrong_info,
143 static int hwcrhk_get_pass(
const char *prompt_info,
144 int *len_io,
char *buf,
147 static void hwcrhk_log_message(
void *logstr,
const char *message);
150 #define HWCRHK_CMD_SO_PATH ENGINE_CMD_BASE
151 #define HWCRHK_CMD_FORK_CHECK (ENGINE_CMD_BASE + 1)
152 #define HWCRHK_CMD_THREAD_LOCKING (ENGINE_CMD_BASE + 2)
153 #define HWCRHK_CMD_SET_USER_INTERFACE (ENGINE_CMD_BASE + 3)
154 #define HWCRHK_CMD_SET_CALLBACK_DATA (ENGINE_CMD_BASE + 4)
158 "Specifies the path to the 'hwcrhk' shared library",
162 "Turns fork() checking on (non-zero) or off (zero)",
166 "Turns thread-safe locking on (zero) or off (non-zero)",
169 "SET_USER_INTERFACE",
170 "Set the global user interface (internal)",
174 "Set the global user interface extra data (internal)",
179 #ifndef OPENSSL_NO_RSA
200 #ifndef OPENSSL_NO_DH
228 static const char *engine_hwcrhk_id =
"chil";
229 static const char *engine_hwcrhk_name =
"CHIL hardware engine support";
230 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
232 static const char *engine_hwcrhk_id_alt =
"ncipher";
267 #define BN2MPI(mp, bn) \
268 {mp.size = bn->top * sizeof(BN_ULONG); mp.buf = (unsigned char *)bn->d;}
269 #define MPI2BN(bn, mp) \
270 {mp.size = bn->dmax * sizeof(BN_ULONG); mp.buf = (unsigned char *)bn->d;}
272 static BIO *logstream = NULL;
273 static int disable_mutex_callbacks = 0;
335 static int bind_helper(
ENGINE *e)
337 #ifndef OPENSSL_NO_RSA
340 #ifndef OPENSSL_NO_DH
345 #ifndef OPENSSL_NO_RSA
348 #ifndef OPENSSL_NO_DH
361 #ifndef OPENSSL_NO_RSA
376 #ifndef OPENSSL_NO_DH
384 ERR_load_HWCRHK_strings();
388 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
389 static ENGINE *engine_chil(
void)
394 if(!bind_helper(ret))
405 ENGINE *toadd = engine_chil();
418 static DSO *hwcrhk_dso = NULL;
420 #ifndef OPENSSL_NO_RSA
421 static int hndidx_rsa = -1;
429 #ifndef OPENSSL_NO_RSA
433 #ifndef OPENSSL_NO_RSA
441 static const char *HWCRHK_LIBNAME = NULL;
442 static void free_HWCRHK_LIBNAME(
void)
446 HWCRHK_LIBNAME = NULL;
448 static const char *get_HWCRHK_LIBNAME(
void)
451 return HWCRHK_LIBNAME;
454 static long set_HWCRHK_LIBNAME(
const char *name)
456 free_HWCRHK_LIBNAME();
457 return (((HWCRHK_LIBNAME =
BUF_strdup(name)) != NULL) ? 1 : 0);
459 static const char *n_hwcrhk_Init =
"HWCryptoHook_Init";
460 static const char *n_hwcrhk_Finish =
"HWCryptoHook_Finish";
461 static const char *n_hwcrhk_ModExp =
"HWCryptoHook_ModExp";
462 #ifndef OPENSSL_NO_RSA
463 static const char *n_hwcrhk_RSA =
"HWCryptoHook_RSA";
465 static const char *n_hwcrhk_RandomBytes =
"HWCryptoHook_RandomBytes";
466 #ifndef OPENSSL_NO_RSA
467 static const char *n_hwcrhk_RSALoadKey =
"HWCryptoHook_RSALoadKey";
468 static const char *n_hwcrhk_RSAGetPublicKey =
"HWCryptoHook_RSAGetPublicKey";
469 static const char *n_hwcrhk_RSAUnloadKey =
"HWCryptoHook_RSAUnloadKey";
471 static const char *n_hwcrhk_ModExpCRT =
"HWCryptoHook_ModExpCRT";
486 rmsg.
size =
sizeof(tempbuf);
488 *hac = p_hwcrhk_Init(&hwcrhk_globals,
sizeof(hwcrhk_globals), &rmsg,
498 p_hwcrhk_Finish(hac);
502 static int hwcrhk_destroy(
ENGINE *e)
504 free_HWCRHK_LIBNAME();
505 ERR_unload_HWCRHK_strings();
510 static int hwcrhk_init(
ENGINE *e)
515 #ifndef OPENSSL_NO_RSA
524 if(hwcrhk_dso != NULL)
530 hwcrhk_dso =
DSO_load(NULL, get_HWCRHK_LIBNAME(), NULL, 0);
531 if(hwcrhk_dso == NULL)
542 #ifndef OPENSSL_NO_RSA
562 p_hwcrhk_Finish = p2;
563 p_hwcrhk_ModExp = p3;
564 #ifndef OPENSSL_NO_RSA
566 p_hwcrhk_RSALoadKey = p5;
567 p_hwcrhk_RSAGetPublicKey = p6;
568 p_hwcrhk_RSAUnloadKey = p7;
570 p_hwcrhk_RandomBytes = p8;
571 p_hwcrhk_ModExpCRT = p9;
575 if (disable_mutex_callbacks == 0)
581 hwcrhk_globals.
mutex_init = hwcrhk_mutex_init;
590 if(!get_context(&hwcrhk_context, &password_context))
596 #ifndef OPENSSL_NO_RSA
597 if (hndidx_rsa == -1)
599 "nFast HWCryptoHook RSA key handle",
607 p_hwcrhk_Init = NULL;
608 p_hwcrhk_Finish = NULL;
609 p_hwcrhk_ModExp = NULL;
610 #ifndef OPENSSL_NO_RSA
612 p_hwcrhk_RSALoadKey = NULL;
613 p_hwcrhk_RSAGetPublicKey = NULL;
614 p_hwcrhk_RSAUnloadKey = NULL;
616 p_hwcrhk_ModExpCRT = NULL;
617 p_hwcrhk_RandomBytes = NULL;
621 static int hwcrhk_finish(
ENGINE *e)
624 free_HWCRHK_LIBNAME();
625 if(hwcrhk_dso == NULL)
631 release_context(hwcrhk_context);
642 p_hwcrhk_Init = NULL;
643 p_hwcrhk_Finish = NULL;
644 p_hwcrhk_ModExp = NULL;
645 #ifndef OPENSSL_NO_RSA
647 p_hwcrhk_RSALoadKey = NULL;
648 p_hwcrhk_RSAGetPublicKey = NULL;
649 p_hwcrhk_RSAUnloadKey = NULL;
651 p_hwcrhk_ModExpCRT = NULL;
652 p_hwcrhk_RandomBytes = NULL;
656 static int hwcrhk_ctrl(
ENGINE *e,
int cmd,
long i,
void *p,
void (*
f)(
void))
673 return set_HWCRHK_LIBNAME((
const char *)p);
714 hwcrhk_globals.
flags |=
717 hwcrhk_globals.
flags &=
728 disable_mutex_callbacks = 1;
733 disable_mutex_callbacks = ((i == 0) ? 0 : 1);
748 static EVP_PKEY *hwcrhk_load_privkey(
ENGINE *eng,
const char *key_id,
749 UI_METHOD *ui_method,
void *callback_data)
751 #ifndef OPENSSL_NO_RSA
755 #ifndef OPENSSL_NO_RSA
759 #if !defined(OPENSSL_NO_RSA)
765 #if !defined(OPENSSL_NO_RSA)
767 rmsg.
size =
sizeof(tempbuf);
776 #ifndef OPENSSL_NO_RSA
786 if (p_hwcrhk_RSALoadKey(hwcrhk_context, key_id, hptr,
801 #ifndef OPENSSL_NO_RSA
809 if (p_hwcrhk_RSAGetPublicKey(*hptr, &n, &e, &rmsg)
822 if (p_hwcrhk_RSAGetPublicKey(*hptr, &n, &e, &rmsg))
829 rtmp->
e->
top = e.
size /
sizeof(BN_ULONG);
831 rtmp->
n->
top = n.
size /
sizeof(BN_ULONG);
844 #ifndef OPENSSL_NO_RSA
851 static EVP_PKEY *hwcrhk_load_pubkey(
ENGINE *eng,
const char *key_id,
852 UI_METHOD *ui_method,
void *callback_data)
856 #ifndef OPENSSL_NO_RSA
857 res = hwcrhk_load_privkey(eng, key_id,
858 ui_method, callback_data);
864 #ifndef OPENSSL_NO_RSA
908 rmsg.
size =
sizeof(tempbuf);
923 ret = p_hwcrhk_ModExp(hwcrhk_context, m_a, m_p, m_n, &m_r, &rmsg);
926 r->
top = m_r.
size /
sizeof(BN_ULONG);
951 #ifndef OPENSSL_NO_RSA
957 int to_return = 0, ret;
960 rmsg.
size =
sizeof(tempbuf);
989 ret = p_hwcrhk_RSA(m_a, *hptr, &m_r, &rmsg);
992 r->
top = m_r.
size /
sizeof(BN_ULONG);
1036 ret = p_hwcrhk_ModExpCRT(hwcrhk_context, m_a, m_p, m_q,
1037 m_dmp1, m_dmq1, m_iqmp, &m_r, &rmsg);
1040 r->
top = m_r.
size /
sizeof(BN_ULONG);
1069 #ifndef OPENSSL_NO_RSA
1074 return hwcrhk_mod_exp(r, a, p, m, ctx);
1077 static int hwcrhk_rsa_finish(
RSA *rsa)
1084 p_hwcrhk_RSAUnloadKey(*hptr, NULL);
1093 #ifndef OPENSSL_NO_DH
1095 static int hwcrhk_mod_exp_dh(
const DH *dh,
BIGNUM *r,
1099 return hwcrhk_mod_exp(r, a, p, m, ctx);
1104 static int hwcrhk_rand_bytes(
unsigned char *buf,
int num)
1112 rmsg.
size =
sizeof(tempbuf);
1120 ret = p_hwcrhk_RandomBytes(hwcrhk_context, buf, num, &rmsg);
1144 static int hwcrhk_rand_status(
void)
1178 static int hwcrhk_get_pass(
const char *prompt_info,
1179 int *len_io,
char *buf,
1184 void *callback_data = NULL;
1189 if (prompt_info && !*prompt_info)
1211 if (callback == NULL && ui_method == NULL)
1224 "pass phrase", prompt_info);
1228 buf,0,(*len_io) - 1);
1240 *len_io = strlen(buf);
1248 *len_io =
callback(buf, *len_io, 0, callback_data);
1255 static int hwcrhk_insert_card(
const char *prompt_info,
1256 const char *wrong_info,
1262 void *callback_data = NULL;
1279 if (ui_method == NULL)
1295 if (wrong_info && *wrong_info)
1297 "Current card: \"%s\"\n", wrong_info);
1301 if (ok >= 0 && prompt_info)
1304 "Insert card \"%s\"", prompt_info);
1306 "\n then hit <enter> or C<enter> to cancel\n",
1315 if (ok == -2 || (ok >= 0 && answer ==
'C'))
1325 static void hwcrhk_log_message(
void *logstr,
const char *message)
1327 BIO *lstream = NULL;
1331 lstream=*(
BIO **)logstr;
1341 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
1342 static int bind_fn(
ENGINE *e,
const char *
id)
1344 if(
id && (strcmp(
id, engine_hwcrhk_id) != 0) &&
1345 (strcmp(
id, engine_hwcrhk_id_alt) != 0))