2
0

e_4758cca.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. /* Author: Maurice Gittens <maurice@gittens.nl> */
  2. /* ====================================================================
  3. * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  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
  15. * distribution.
  16. *
  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/)"
  21. *
  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.
  26. *
  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.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  35. *
  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. * ====================================================================
  49. *
  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).
  53. *
  54. */
  55. #include <stdio.h>
  56. #include <string.h>
  57. #include <openssl/crypto.h>
  58. #include <openssl/dso.h>
  59. #include <openssl/x509.h>
  60. #include <openssl/objects.h>
  61. #include <openssl/engine.h>
  62. #include <openssl/rand.h>
  63. #ifndef OPENSSL_NO_RSA
  64. #include <openssl/rsa.h>
  65. #endif
  66. #include <openssl/bn.h>
  67. #ifndef OPENSSL_NO_HW
  68. #ifndef OPENSSL_NO_HW_4758_CCA
  69. #ifdef FLAT_INC
  70. #include "hw_4758_cca.h"
  71. #else
  72. #include "vendor_defns/hw_4758_cca.h"
  73. #endif
  74. #include "e_4758cca_err.c"
  75. static int ibm_4758_cca_destroy(ENGINE *e);
  76. static int ibm_4758_cca_init(ENGINE *e);
  77. static int ibm_4758_cca_finish(ENGINE *e);
  78. static int ibm_4758_cca_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
  79. /* rsa functions */
  80. /*---------------*/
  81. #ifndef OPENSSL_NO_RSA
  82. static int cca_rsa_pub_enc(int flen, const unsigned char *from,
  83. unsigned char *to, RSA *rsa,int padding);
  84. static int cca_rsa_priv_dec(int flen, const unsigned char *from,
  85. unsigned char *to, RSA *rsa,int padding);
  86. static int cca_rsa_sign(int type, const unsigned char *m, unsigned int m_len,
  87. unsigned char *sigret, unsigned int *siglen, const RSA *rsa);
  88. static int cca_rsa_verify(int dtype, const unsigned char *m, unsigned int m_len,
  89. const unsigned char *sigbuf, unsigned int siglen, const RSA *rsa);
  90. /* utility functions */
  91. /*-----------------------*/
  92. static EVP_PKEY *ibm_4758_load_privkey(ENGINE*, const char*,
  93. UI_METHOD *ui_method, void *callback_data);
  94. static EVP_PKEY *ibm_4758_load_pubkey(ENGINE*, const char*,
  95. UI_METHOD *ui_method, void *callback_data);
  96. static int getModulusAndExponent(const unsigned char *token, long *exponentLength,
  97. unsigned char *exponent, long *modulusLength,
  98. long *modulusFieldLength, unsigned char *modulus);
  99. #endif
  100. /* RAND number functions */
  101. /*-----------------------*/
  102. static int cca_get_random_bytes(unsigned char*, int);
  103. static int cca_random_status(void);
  104. #ifndef OPENSSL_NO_RSA
  105. static void cca_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
  106. int idx,long argl, void *argp);
  107. #endif
  108. /* Function pointers for CCA verbs */
  109. /*---------------------------------*/
  110. #ifndef OPENSSL_NO_RSA
  111. static F_KEYRECORDREAD keyRecordRead;
  112. static F_DIGITALSIGNATUREGENERATE digitalSignatureGenerate;
  113. static F_DIGITALSIGNATUREVERIFY digitalSignatureVerify;
  114. static F_PUBLICKEYEXTRACT publicKeyExtract;
  115. static F_PKAENCRYPT pkaEncrypt;
  116. static F_PKADECRYPT pkaDecrypt;
  117. #endif
  118. static F_RANDOMNUMBERGENERATE randomNumberGenerate;
  119. /* static variables */
  120. /*------------------*/
  121. static const char *CCA4758_LIB_NAME = NULL;
  122. static const char *get_CCA4758_LIB_NAME(void)
  123. {
  124. if(CCA4758_LIB_NAME)
  125. return CCA4758_LIB_NAME;
  126. return CCA_LIB_NAME;
  127. }
  128. static void free_CCA4758_LIB_NAME(void)
  129. {
  130. if(CCA4758_LIB_NAME)
  131. OPENSSL_free((void*)CCA4758_LIB_NAME);
  132. CCA4758_LIB_NAME = NULL;
  133. }
  134. static long set_CCA4758_LIB_NAME(const char *name)
  135. {
  136. free_CCA4758_LIB_NAME();
  137. return (((CCA4758_LIB_NAME = BUF_strdup(name)) != NULL) ? 1 : 0);
  138. }
  139. #ifndef OPENSSL_NO_RSA
  140. static const char* n_keyRecordRead = CSNDKRR;
  141. static const char* n_digitalSignatureGenerate = CSNDDSG;
  142. static const char* n_digitalSignatureVerify = CSNDDSV;
  143. static const char* n_publicKeyExtract = CSNDPKX;
  144. static const char* n_pkaEncrypt = CSNDPKE;
  145. static const char* n_pkaDecrypt = CSNDPKD;
  146. #endif
  147. static const char* n_randomNumberGenerate = CSNBRNG;
  148. #ifndef OPENSSL_NO_RSA
  149. static int hndidx = -1;
  150. #endif
  151. static DSO *dso = NULL;
  152. /* openssl engine initialization structures */
  153. /*------------------------------------------*/
  154. #define CCA4758_CMD_SO_PATH ENGINE_CMD_BASE
  155. static const ENGINE_CMD_DEFN cca4758_cmd_defns[] = {
  156. {CCA4758_CMD_SO_PATH,
  157. "SO_PATH",
  158. "Specifies the path to the '4758cca' shared library",
  159. ENGINE_CMD_FLAG_STRING},
  160. {0, NULL, NULL, 0}
  161. };
  162. #ifndef OPENSSL_NO_RSA
  163. static RSA_METHOD ibm_4758_cca_rsa =
  164. {
  165. "IBM 4758 CCA RSA method",
  166. cca_rsa_pub_enc,
  167. NULL,
  168. NULL,
  169. cca_rsa_priv_dec,
  170. NULL, /*rsa_mod_exp,*/
  171. NULL, /*mod_exp_mont,*/
  172. NULL, /* init */
  173. NULL, /* finish */
  174. RSA_FLAG_SIGN_VER, /* flags */
  175. NULL, /* app_data */
  176. cca_rsa_sign, /* rsa_sign */
  177. cca_rsa_verify, /* rsa_verify */
  178. NULL /* rsa_keygen */
  179. };
  180. #endif
  181. static RAND_METHOD ibm_4758_cca_rand =
  182. {
  183. /* "IBM 4758 RAND method", */
  184. NULL, /* seed */
  185. cca_get_random_bytes, /* get random bytes from the card */
  186. NULL, /* cleanup */
  187. NULL, /* add */
  188. cca_get_random_bytes, /* pseudo rand */
  189. cca_random_status, /* status */
  190. };
  191. static const char *engine_4758_cca_id = "4758cca";
  192. static const char *engine_4758_cca_name = "IBM 4758 CCA hardware engine support";
  193. #ifndef OPENSSL_NO_DYNAMIC_ENGINE
  194. /* Compatibility hack, the dynamic library uses this form in the path */
  195. static const char *engine_4758_cca_id_alt = "4758_cca";
  196. #endif
  197. /* engine implementation */
  198. /*-----------------------*/
  199. static int bind_helper(ENGINE *e)
  200. {
  201. if(!ENGINE_set_id(e, engine_4758_cca_id) ||
  202. !ENGINE_set_name(e, engine_4758_cca_name) ||
  203. #ifndef OPENSSL_NO_RSA
  204. !ENGINE_set_RSA(e, &ibm_4758_cca_rsa) ||
  205. #endif
  206. !ENGINE_set_RAND(e, &ibm_4758_cca_rand) ||
  207. !ENGINE_set_destroy_function(e, ibm_4758_cca_destroy) ||
  208. !ENGINE_set_init_function(e, ibm_4758_cca_init) ||
  209. !ENGINE_set_finish_function(e, ibm_4758_cca_finish) ||
  210. !ENGINE_set_ctrl_function(e, ibm_4758_cca_ctrl) ||
  211. #ifndef OPENSSL_NO_RSA
  212. !ENGINE_set_load_privkey_function(e, ibm_4758_load_privkey) ||
  213. !ENGINE_set_load_pubkey_function(e, ibm_4758_load_pubkey) ||
  214. #endif
  215. !ENGINE_set_cmd_defns(e, cca4758_cmd_defns))
  216. return 0;
  217. /* Ensure the error handling is set up */
  218. ERR_load_CCA4758_strings();
  219. return 1;
  220. }
  221. #ifdef OPENSSL_NO_DYNAMIC_ENGINE
  222. static ENGINE *engine_4758_cca(void)
  223. {
  224. ENGINE *ret = ENGINE_new();
  225. if(!ret)
  226. return NULL;
  227. if(!bind_helper(ret))
  228. {
  229. ENGINE_free(ret);
  230. return NULL;
  231. }
  232. return ret;
  233. }
  234. void ENGINE_load_4758cca(void)
  235. {
  236. ENGINE *e_4758 = engine_4758_cca();
  237. if (!e_4758) return;
  238. ENGINE_add(e_4758);
  239. ENGINE_free(e_4758);
  240. ERR_clear_error();
  241. }
  242. #endif
  243. static int ibm_4758_cca_destroy(ENGINE *e)
  244. {
  245. ERR_unload_CCA4758_strings();
  246. free_CCA4758_LIB_NAME();
  247. return 1;
  248. }
  249. static int ibm_4758_cca_init(ENGINE *e)
  250. {
  251. if(dso)
  252. {
  253. CCA4758err(CCA4758_F_IBM_4758_CCA_INIT,CCA4758_R_ALREADY_LOADED);
  254. goto err;
  255. }
  256. dso = DSO_load(NULL, get_CCA4758_LIB_NAME(), NULL, 0);
  257. if(!dso)
  258. {
  259. CCA4758err(CCA4758_F_IBM_4758_CCA_INIT,CCA4758_R_DSO_FAILURE);
  260. goto err;
  261. }
  262. #ifndef OPENSSL_NO_RSA
  263. if(!(keyRecordRead = (F_KEYRECORDREAD)
  264. DSO_bind_func(dso, n_keyRecordRead)) ||
  265. !(randomNumberGenerate = (F_RANDOMNUMBERGENERATE)
  266. DSO_bind_func(dso, n_randomNumberGenerate)) ||
  267. !(digitalSignatureGenerate = (F_DIGITALSIGNATUREGENERATE)
  268. DSO_bind_func(dso, n_digitalSignatureGenerate)) ||
  269. !(digitalSignatureVerify = (F_DIGITALSIGNATUREVERIFY)
  270. DSO_bind_func(dso, n_digitalSignatureVerify)) ||
  271. !(publicKeyExtract = (F_PUBLICKEYEXTRACT)
  272. DSO_bind_func(dso, n_publicKeyExtract)) ||
  273. !(pkaEncrypt = (F_PKAENCRYPT)
  274. DSO_bind_func(dso, n_pkaEncrypt)) ||
  275. !(pkaDecrypt = (F_PKADECRYPT)
  276. DSO_bind_func(dso, n_pkaDecrypt)))
  277. {
  278. CCA4758err(CCA4758_F_IBM_4758_CCA_INIT,CCA4758_R_DSO_FAILURE);
  279. goto err;
  280. }
  281. #else
  282. if(!(randomNumberGenerate = (F_RANDOMNUMBERGENERATE)
  283. DSO_bind_func(dso, n_randomNumberGenerate)))
  284. {
  285. CCA4758err(CCA4758_F_IBM_4758_CCA_INIT,CCA4758_R_DSO_FAILURE);
  286. goto err;
  287. }
  288. #endif
  289. #ifndef OPENSSL_NO_RSA
  290. hndidx = RSA_get_ex_new_index(0, "IBM 4758 CCA RSA key handle",
  291. NULL, NULL, cca_ex_free);
  292. #endif
  293. return 1;
  294. err:
  295. if(dso)
  296. DSO_free(dso);
  297. dso = NULL;
  298. #ifndef OPENSSL_NO_RSA
  299. keyRecordRead = (F_KEYRECORDREAD)0;
  300. digitalSignatureGenerate = (F_DIGITALSIGNATUREGENERATE)0;
  301. digitalSignatureVerify = (F_DIGITALSIGNATUREVERIFY)0;
  302. publicKeyExtract = (F_PUBLICKEYEXTRACT)0;
  303. pkaEncrypt = (F_PKAENCRYPT)0;
  304. pkaDecrypt = (F_PKADECRYPT)0;
  305. #endif
  306. randomNumberGenerate = (F_RANDOMNUMBERGENERATE)0;
  307. return 0;
  308. }
  309. static int ibm_4758_cca_finish(ENGINE *e)
  310. {
  311. free_CCA4758_LIB_NAME();
  312. if(!dso)
  313. {
  314. CCA4758err(CCA4758_F_IBM_4758_CCA_FINISH,
  315. CCA4758_R_NOT_LOADED);
  316. return 0;
  317. }
  318. if(!DSO_free(dso))
  319. {
  320. CCA4758err(CCA4758_F_IBM_4758_CCA_FINISH,
  321. CCA4758_R_UNIT_FAILURE);
  322. return 0;
  323. }
  324. dso = NULL;
  325. #ifndef OPENSSL_NO_RSA
  326. keyRecordRead = (F_KEYRECORDREAD)0;
  327. randomNumberGenerate = (F_RANDOMNUMBERGENERATE)0;
  328. digitalSignatureGenerate = (F_DIGITALSIGNATUREGENERATE)0;
  329. digitalSignatureVerify = (F_DIGITALSIGNATUREVERIFY)0;
  330. publicKeyExtract = (F_PUBLICKEYEXTRACT)0;
  331. pkaEncrypt = (F_PKAENCRYPT)0;
  332. pkaDecrypt = (F_PKADECRYPT)0;
  333. #endif
  334. randomNumberGenerate = (F_RANDOMNUMBERGENERATE)0;
  335. return 1;
  336. }
  337. static int ibm_4758_cca_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
  338. {
  339. int initialised = ((dso == NULL) ? 0 : 1);
  340. switch(cmd)
  341. {
  342. case CCA4758_CMD_SO_PATH:
  343. if(p == NULL)
  344. {
  345. CCA4758err(CCA4758_F_IBM_4758_CCA_CTRL,
  346. ERR_R_PASSED_NULL_PARAMETER);
  347. return 0;
  348. }
  349. if(initialised)
  350. {
  351. CCA4758err(CCA4758_F_IBM_4758_CCA_CTRL,
  352. CCA4758_R_ALREADY_LOADED);
  353. return 0;
  354. }
  355. return set_CCA4758_LIB_NAME((const char *)p);
  356. default:
  357. break;
  358. }
  359. CCA4758err(CCA4758_F_IBM_4758_CCA_CTRL,
  360. CCA4758_R_COMMAND_NOT_IMPLEMENTED);
  361. return 0;
  362. }
  363. #ifndef OPENSSL_NO_RSA
  364. #define MAX_CCA_PKA_TOKEN_SIZE 2500
  365. static EVP_PKEY *ibm_4758_load_privkey(ENGINE* e, const char* key_id,
  366. UI_METHOD *ui_method, void *callback_data)
  367. {
  368. RSA *rtmp = NULL;
  369. EVP_PKEY *res = NULL;
  370. unsigned char* keyToken = NULL;
  371. unsigned char pubKeyToken[MAX_CCA_PKA_TOKEN_SIZE];
  372. long pubKeyTokenLength = MAX_CCA_PKA_TOKEN_SIZE;
  373. long keyTokenLength = MAX_CCA_PKA_TOKEN_SIZE;
  374. long returnCode;
  375. long reasonCode;
  376. long exitDataLength = 0;
  377. long ruleArrayLength = 0;
  378. unsigned char exitData[8];
  379. unsigned char ruleArray[8];
  380. unsigned char keyLabel[64];
  381. unsigned long keyLabelLength = strlen(key_id);
  382. unsigned char modulus[256];
  383. long modulusFieldLength = sizeof(modulus);
  384. long modulusLength = 0;
  385. unsigned char exponent[256];
  386. long exponentLength = sizeof(exponent);
  387. if (keyLabelLength > sizeof(keyLabel))
  388. {
  389. CCA4758err(CCA4758_F_IBM_4758_LOAD_PRIVKEY,
  390. CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
  391. return NULL;
  392. }
  393. memset(keyLabel,' ', sizeof(keyLabel));
  394. memcpy(keyLabel, key_id, keyLabelLength);
  395. keyToken = OPENSSL_malloc(MAX_CCA_PKA_TOKEN_SIZE + sizeof(long));
  396. if (!keyToken)
  397. {
  398. CCA4758err(CCA4758_F_IBM_4758_LOAD_PRIVKEY,
  399. ERR_R_MALLOC_FAILURE);
  400. goto err;
  401. }
  402. keyRecordRead(&returnCode, &reasonCode, &exitDataLength,
  403. exitData, &ruleArrayLength, ruleArray, keyLabel,
  404. &keyTokenLength, keyToken+sizeof(long));
  405. if (returnCode)
  406. {
  407. CCA4758err(CCA4758_F_IBM_4758_LOAD_PRIVKEY,
  408. CCA4758_R_FAILED_LOADING_PRIVATE_KEY);
  409. goto err;
  410. }
  411. publicKeyExtract(&returnCode, &reasonCode, &exitDataLength,
  412. exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
  413. keyToken+sizeof(long), &pubKeyTokenLength, pubKeyToken);
  414. if (returnCode)
  415. {
  416. CCA4758err(CCA4758_F_IBM_4758_LOAD_PRIVKEY,
  417. CCA4758_R_FAILED_LOADING_PRIVATE_KEY);
  418. goto err;
  419. }
  420. if (!getModulusAndExponent(pubKeyToken, &exponentLength,
  421. exponent, &modulusLength, &modulusFieldLength,
  422. modulus))
  423. {
  424. CCA4758err(CCA4758_F_IBM_4758_LOAD_PRIVKEY,
  425. CCA4758_R_FAILED_LOADING_PRIVATE_KEY);
  426. goto err;
  427. }
  428. (*(long*)keyToken) = keyTokenLength;
  429. rtmp = RSA_new_method(e);
  430. RSA_set_ex_data(rtmp, hndidx, (char *)keyToken);
  431. rtmp->e = BN_bin2bn(exponent, exponentLength, NULL);
  432. rtmp->n = BN_bin2bn(modulus, modulusFieldLength, NULL);
  433. rtmp->flags |= RSA_FLAG_EXT_PKEY;
  434. res = EVP_PKEY_new();
  435. EVP_PKEY_assign_RSA(res, rtmp);
  436. return res;
  437. err:
  438. if (keyToken)
  439. OPENSSL_free(keyToken);
  440. return NULL;
  441. }
  442. static EVP_PKEY *ibm_4758_load_pubkey(ENGINE* e, const char* key_id,
  443. UI_METHOD *ui_method, void *callback_data)
  444. {
  445. RSA *rtmp = NULL;
  446. EVP_PKEY *res = NULL;
  447. unsigned char* keyToken = NULL;
  448. long keyTokenLength = MAX_CCA_PKA_TOKEN_SIZE;
  449. long returnCode;
  450. long reasonCode;
  451. long exitDataLength = 0;
  452. long ruleArrayLength = 0;
  453. unsigned char exitData[8];
  454. unsigned char ruleArray[8];
  455. unsigned char keyLabel[64];
  456. unsigned long keyLabelLength = strlen(key_id);
  457. unsigned char modulus[512];
  458. long modulusFieldLength = sizeof(modulus);
  459. long modulusLength = 0;
  460. unsigned char exponent[512];
  461. long exponentLength = sizeof(exponent);
  462. if (keyLabelLength > sizeof(keyLabel))
  463. {
  464. CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY,
  465. CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
  466. return NULL;
  467. }
  468. memset(keyLabel,' ', sizeof(keyLabel));
  469. memcpy(keyLabel, key_id, keyLabelLength);
  470. keyToken = OPENSSL_malloc(MAX_CCA_PKA_TOKEN_SIZE + sizeof(long));
  471. if (!keyToken)
  472. {
  473. CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY,
  474. ERR_R_MALLOC_FAILURE);
  475. goto err;
  476. }
  477. keyRecordRead(&returnCode, &reasonCode, &exitDataLength, exitData,
  478. &ruleArrayLength, ruleArray, keyLabel, &keyTokenLength,
  479. keyToken+sizeof(long));
  480. if (returnCode)
  481. {
  482. CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY,
  483. ERR_R_MALLOC_FAILURE);
  484. goto err;
  485. }
  486. if (!getModulusAndExponent(keyToken+sizeof(long), &exponentLength,
  487. exponent, &modulusLength, &modulusFieldLength, modulus))
  488. {
  489. CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY,
  490. CCA4758_R_FAILED_LOADING_PUBLIC_KEY);
  491. goto err;
  492. }
  493. (*(long*)keyToken) = keyTokenLength;
  494. rtmp = RSA_new_method(e);
  495. RSA_set_ex_data(rtmp, hndidx, (char *)keyToken);
  496. rtmp->e = BN_bin2bn(exponent, exponentLength, NULL);
  497. rtmp->n = BN_bin2bn(modulus, modulusFieldLength, NULL);
  498. rtmp->flags |= RSA_FLAG_EXT_PKEY;
  499. res = EVP_PKEY_new();
  500. EVP_PKEY_assign_RSA(res, rtmp);
  501. return res;
  502. err:
  503. if (keyToken)
  504. OPENSSL_free(keyToken);
  505. return NULL;
  506. }
  507. static int cca_rsa_pub_enc(int flen, const unsigned char *from,
  508. unsigned char *to, RSA *rsa,int padding)
  509. {
  510. long returnCode;
  511. long reasonCode;
  512. long lflen = flen;
  513. long exitDataLength = 0;
  514. unsigned char exitData[8];
  515. long ruleArrayLength = 1;
  516. unsigned char ruleArray[8] = "PKCS-1.2";
  517. long dataStructureLength = 0;
  518. unsigned char dataStructure[8];
  519. long outputLength = RSA_size(rsa);
  520. long keyTokenLength;
  521. unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
  522. keyTokenLength = *(long*)keyToken;
  523. keyToken+=sizeof(long);
  524. pkaEncrypt(&returnCode, &reasonCode, &exitDataLength, exitData,
  525. &ruleArrayLength, ruleArray, &lflen, (unsigned char*)from,
  526. &dataStructureLength, dataStructure, &keyTokenLength,
  527. keyToken, &outputLength, to);
  528. if (returnCode || reasonCode)
  529. return -(returnCode << 16 | reasonCode);
  530. return outputLength;
  531. }
  532. static int cca_rsa_priv_dec(int flen, const unsigned char *from,
  533. unsigned char *to, RSA *rsa,int padding)
  534. {
  535. long returnCode;
  536. long reasonCode;
  537. long lflen = flen;
  538. long exitDataLength = 0;
  539. unsigned char exitData[8];
  540. long ruleArrayLength = 1;
  541. unsigned char ruleArray[8] = "PKCS-1.2";
  542. long dataStructureLength = 0;
  543. unsigned char dataStructure[8];
  544. long outputLength = RSA_size(rsa);
  545. long keyTokenLength;
  546. unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
  547. keyTokenLength = *(long*)keyToken;
  548. keyToken+=sizeof(long);
  549. pkaDecrypt(&returnCode, &reasonCode, &exitDataLength, exitData,
  550. &ruleArrayLength, ruleArray, &lflen, (unsigned char*)from,
  551. &dataStructureLength, dataStructure, &keyTokenLength,
  552. keyToken, &outputLength, to);
  553. return (returnCode | reasonCode) ? 0 : 1;
  554. }
  555. #define SSL_SIG_LEN 36
  556. static int cca_rsa_verify(int type, const unsigned char *m, unsigned int m_len,
  557. const unsigned char *sigbuf, unsigned int siglen, const RSA *rsa)
  558. {
  559. long returnCode;
  560. long reasonCode;
  561. long lsiglen = siglen;
  562. long exitDataLength = 0;
  563. unsigned char exitData[8];
  564. long ruleArrayLength = 1;
  565. unsigned char ruleArray[8] = "PKCS-1.1";
  566. long keyTokenLength;
  567. unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
  568. long length = SSL_SIG_LEN;
  569. long keyLength ;
  570. unsigned char *hashBuffer = NULL;
  571. X509_SIG sig;
  572. ASN1_TYPE parameter;
  573. X509_ALGOR algorithm;
  574. ASN1_OCTET_STRING digest;
  575. keyTokenLength = *(long*)keyToken;
  576. keyToken+=sizeof(long);
  577. if (type == NID_md5 || type == NID_sha1)
  578. {
  579. sig.algor = &algorithm;
  580. algorithm.algorithm = OBJ_nid2obj(type);
  581. if (!algorithm.algorithm)
  582. {
  583. CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
  584. CCA4758_R_UNKNOWN_ALGORITHM_TYPE);
  585. return 0;
  586. }
  587. if (!algorithm.algorithm->length)
  588. {
  589. CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
  590. CCA4758_R_ASN1_OID_UNKNOWN_FOR_MD);
  591. return 0;
  592. }
  593. parameter.type = V_ASN1_NULL;
  594. parameter.value.ptr = NULL;
  595. algorithm.parameter = &parameter;
  596. sig.digest = &digest;
  597. sig.digest->data = (unsigned char*)m;
  598. sig.digest->length = m_len;
  599. length = i2d_X509_SIG(&sig, NULL);
  600. }
  601. keyLength = RSA_size(rsa);
  602. if (length - RSA_PKCS1_PADDING > keyLength)
  603. {
  604. CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
  605. CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
  606. return 0;
  607. }
  608. switch (type)
  609. {
  610. case NID_md5_sha1 :
  611. if (m_len != SSL_SIG_LEN)
  612. {
  613. CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
  614. CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
  615. return 0;
  616. }
  617. hashBuffer = (unsigned char *)m;
  618. length = m_len;
  619. break;
  620. case NID_md5 :
  621. {
  622. unsigned char *ptr;
  623. ptr = hashBuffer = OPENSSL_malloc(
  624. (unsigned int)keyLength+1);
  625. if (!hashBuffer)
  626. {
  627. CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
  628. ERR_R_MALLOC_FAILURE);
  629. return 0;
  630. }
  631. i2d_X509_SIG(&sig, &ptr);
  632. }
  633. break;
  634. case NID_sha1 :
  635. {
  636. unsigned char *ptr;
  637. ptr = hashBuffer = OPENSSL_malloc(
  638. (unsigned int)keyLength+1);
  639. if (!hashBuffer)
  640. {
  641. CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
  642. ERR_R_MALLOC_FAILURE);
  643. return 0;
  644. }
  645. i2d_X509_SIG(&sig, &ptr);
  646. }
  647. break;
  648. default:
  649. return 0;
  650. }
  651. digitalSignatureVerify(&returnCode, &reasonCode, &exitDataLength,
  652. exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
  653. keyToken, &length, hashBuffer, &lsiglen,
  654. (unsigned char *)sigbuf);
  655. if (type == NID_sha1 || type == NID_md5)
  656. {
  657. OPENSSL_cleanse(hashBuffer, keyLength+1);
  658. OPENSSL_free(hashBuffer);
  659. }
  660. return ((returnCode || reasonCode) ? 0 : 1);
  661. }
  662. #define SSL_SIG_LEN 36
  663. static int cca_rsa_sign(int type, const unsigned char *m, unsigned int m_len,
  664. unsigned char *sigret, unsigned int *siglen, const RSA *rsa)
  665. {
  666. long returnCode;
  667. long reasonCode;
  668. long exitDataLength = 0;
  669. unsigned char exitData[8];
  670. long ruleArrayLength = 1;
  671. unsigned char ruleArray[8] = "PKCS-1.1";
  672. long outputLength=256;
  673. long outputBitLength;
  674. long keyTokenLength;
  675. unsigned char *hashBuffer = NULL;
  676. unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
  677. long length = SSL_SIG_LEN;
  678. long keyLength ;
  679. X509_SIG sig;
  680. ASN1_TYPE parameter;
  681. X509_ALGOR algorithm;
  682. ASN1_OCTET_STRING digest;
  683. keyTokenLength = *(long*)keyToken;
  684. keyToken+=sizeof(long);
  685. if (type == NID_md5 || type == NID_sha1)
  686. {
  687. sig.algor = &algorithm;
  688. algorithm.algorithm = OBJ_nid2obj(type);
  689. if (!algorithm.algorithm)
  690. {
  691. CCA4758err(CCA4758_F_CCA_RSA_SIGN,
  692. CCA4758_R_UNKNOWN_ALGORITHM_TYPE);
  693. return 0;
  694. }
  695. if (!algorithm.algorithm->length)
  696. {
  697. CCA4758err(CCA4758_F_CCA_RSA_SIGN,
  698. CCA4758_R_ASN1_OID_UNKNOWN_FOR_MD);
  699. return 0;
  700. }
  701. parameter.type = V_ASN1_NULL;
  702. parameter.value.ptr = NULL;
  703. algorithm.parameter = &parameter;
  704. sig.digest = &digest;
  705. sig.digest->data = (unsigned char*)m;
  706. sig.digest->length = m_len;
  707. length = i2d_X509_SIG(&sig, NULL);
  708. }
  709. keyLength = RSA_size(rsa);
  710. if (length - RSA_PKCS1_PADDING > keyLength)
  711. {
  712. CCA4758err(CCA4758_F_CCA_RSA_SIGN,
  713. CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
  714. return 0;
  715. }
  716. switch (type)
  717. {
  718. case NID_md5_sha1 :
  719. if (m_len != SSL_SIG_LEN)
  720. {
  721. CCA4758err(CCA4758_F_CCA_RSA_SIGN,
  722. CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
  723. return 0;
  724. }
  725. hashBuffer = (unsigned char*)m;
  726. length = m_len;
  727. break;
  728. case NID_md5 :
  729. {
  730. unsigned char *ptr;
  731. ptr = hashBuffer = OPENSSL_malloc(
  732. (unsigned int)keyLength+1);
  733. if (!hashBuffer)
  734. {
  735. CCA4758err(CCA4758_F_CCA_RSA_SIGN,
  736. ERR_R_MALLOC_FAILURE);
  737. return 0;
  738. }
  739. i2d_X509_SIG(&sig, &ptr);
  740. }
  741. break;
  742. case NID_sha1 :
  743. {
  744. unsigned char *ptr;
  745. ptr = hashBuffer = OPENSSL_malloc(
  746. (unsigned int)keyLength+1);
  747. if (!hashBuffer)
  748. {
  749. CCA4758err(CCA4758_F_CCA_RSA_SIGN,
  750. ERR_R_MALLOC_FAILURE);
  751. return 0;
  752. }
  753. i2d_X509_SIG(&sig, &ptr);
  754. }
  755. break;
  756. default:
  757. return 0;
  758. }
  759. digitalSignatureGenerate(&returnCode, &reasonCode, &exitDataLength,
  760. exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
  761. keyToken, &length, hashBuffer, &outputLength, &outputBitLength,
  762. sigret);
  763. if (type == NID_sha1 || type == NID_md5)
  764. {
  765. OPENSSL_cleanse(hashBuffer, keyLength+1);
  766. OPENSSL_free(hashBuffer);
  767. }
  768. *siglen = outputLength;
  769. return ((returnCode || reasonCode) ? 0 : 1);
  770. }
  771. static int getModulusAndExponent(const unsigned char*token, long *exponentLength,
  772. unsigned char *exponent, long *modulusLength, long *modulusFieldLength,
  773. unsigned char *modulus)
  774. {
  775. unsigned long len;
  776. if (*token++ != (char)0x1E) /* internal PKA token? */
  777. return 0;
  778. if (*token++) /* token version must be zero */
  779. return 0;
  780. len = *token++;
  781. len = len << 8;
  782. len |= (unsigned char)*token++;
  783. token += 4; /* skip reserved bytes */
  784. if (*token++ == (char)0x04)
  785. {
  786. if (*token++) /* token version must be zero */
  787. return 0;
  788. len = *token++;
  789. len = len << 8;
  790. len |= (unsigned char)*token++;
  791. token+=2; /* skip reserved section */
  792. len = *token++;
  793. len = len << 8;
  794. len |= (unsigned char)*token++;
  795. *exponentLength = len;
  796. len = *token++;
  797. len = len << 8;
  798. len |= (unsigned char)*token++;
  799. *modulusLength = len;
  800. len = *token++;
  801. len = len << 8;
  802. len |= (unsigned char)*token++;
  803. *modulusFieldLength = len;
  804. memcpy(exponent, token, *exponentLength);
  805. token+= *exponentLength;
  806. memcpy(modulus, token, *modulusFieldLength);
  807. return 1;
  808. }
  809. return 0;
  810. }
  811. #endif /* OPENSSL_NO_RSA */
  812. static int cca_random_status(void)
  813. {
  814. return 1;
  815. }
  816. static int cca_get_random_bytes(unsigned char* buf, int num)
  817. {
  818. long ret_code;
  819. long reason_code;
  820. long exit_data_length;
  821. unsigned char exit_data[4];
  822. unsigned char form[] = "RANDOM ";
  823. unsigned char rand_buf[8];
  824. while(num >= (int)sizeof(rand_buf))
  825. {
  826. randomNumberGenerate(&ret_code, &reason_code, &exit_data_length,
  827. exit_data, form, rand_buf);
  828. if (ret_code)
  829. return 0;
  830. num -= sizeof(rand_buf);
  831. memcpy(buf, rand_buf, sizeof(rand_buf));
  832. buf += sizeof(rand_buf);
  833. }
  834. if (num)
  835. {
  836. randomNumberGenerate(&ret_code, &reason_code, NULL, NULL,
  837. form, rand_buf);
  838. if (ret_code)
  839. return 0;
  840. memcpy(buf, rand_buf, num);
  841. }
  842. return 1;
  843. }
  844. #ifndef OPENSSL_NO_RSA
  845. static void cca_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad, int idx,
  846. long argl, void *argp)
  847. {
  848. if (item)
  849. OPENSSL_free(item);
  850. }
  851. #endif
  852. /* Goo to handle building as a dynamic engine */
  853. #ifndef OPENSSL_NO_DYNAMIC_ENGINE
  854. static int bind_fn(ENGINE *e, const char *id)
  855. {
  856. if(id && (strcmp(id, engine_4758_cca_id) != 0) &&
  857. (strcmp(id, engine_4758_cca_id_alt) != 0))
  858. return 0;
  859. if(!bind_helper(e))
  860. return 0;
  861. return 1;
  862. }
  863. IMPLEMENT_DYNAMIC_CHECK_FN()
  864. IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
  865. #endif /* OPENSSL_NO_DYNAMIC_ENGINE */
  866. #endif /* !OPENSSL_NO_HW_4758_CCA */
  867. #endif /* !OPENSSL_NO_HW */