e_4758cca.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. 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. if (res)
  441. EVP_PKEY_free(res);
  442. if (rtmp)
  443. RSA_free(rtmp);
  444. return NULL;
  445. }
  446. static EVP_PKEY *ibm_4758_load_pubkey(ENGINE* e, const char* key_id,
  447. UI_METHOD *ui_method, void *callback_data)
  448. {
  449. RSA *rtmp = NULL;
  450. EVP_PKEY *res = NULL;
  451. unsigned char* keyToken = NULL;
  452. long keyTokenLength = MAX_CCA_PKA_TOKEN_SIZE;
  453. long returnCode;
  454. long reasonCode;
  455. long exitDataLength = 0;
  456. long ruleArrayLength = 0;
  457. unsigned char exitData[8];
  458. unsigned char ruleArray[8];
  459. unsigned char keyLabel[64];
  460. unsigned long keyLabelLength = strlen(key_id);
  461. unsigned char modulus[512];
  462. long modulusFieldLength = sizeof(modulus);
  463. long modulusLength = 0;
  464. unsigned char exponent[512];
  465. long exponentLength = sizeof(exponent);
  466. if (keyLabelLength > sizeof(keyLabel))
  467. {
  468. CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY,
  469. CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
  470. return NULL;
  471. }
  472. memset(keyLabel,' ', sizeof(keyLabel));
  473. memcpy(keyLabel, key_id, keyLabelLength);
  474. keyToken = OPENSSL_malloc(MAX_CCA_PKA_TOKEN_SIZE + sizeof(long));
  475. if (!keyToken)
  476. {
  477. CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY,
  478. ERR_R_MALLOC_FAILURE);
  479. goto err;
  480. }
  481. keyRecordRead(&returnCode, &reasonCode, &exitDataLength, exitData,
  482. &ruleArrayLength, ruleArray, keyLabel, &keyTokenLength,
  483. keyToken+sizeof(long));
  484. if (returnCode)
  485. {
  486. CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY,
  487. ERR_R_MALLOC_FAILURE);
  488. goto err;
  489. }
  490. if (!getModulusAndExponent(keyToken+sizeof(long), &exponentLength,
  491. exponent, &modulusLength, &modulusFieldLength, modulus))
  492. {
  493. CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY,
  494. CCA4758_R_FAILED_LOADING_PUBLIC_KEY);
  495. goto err;
  496. }
  497. (*(long*)keyToken) = keyTokenLength;
  498. rtmp = RSA_new_method(e);
  499. RSA_set_ex_data(rtmp, hndidx, (char *)keyToken);
  500. rtmp->e = BN_bin2bn(exponent, exponentLength, NULL);
  501. rtmp->n = BN_bin2bn(modulus, modulusFieldLength, NULL);
  502. rtmp->flags |= RSA_FLAG_EXT_PKEY;
  503. res = EVP_PKEY_new();
  504. EVP_PKEY_assign_RSA(res, rtmp);
  505. return res;
  506. err:
  507. if (keyToken)
  508. OPENSSL_free(keyToken);
  509. if (res)
  510. EVP_PKEY_free(res);
  511. if (rtmp)
  512. RSA_free(rtmp);
  513. return NULL;
  514. }
  515. static int cca_rsa_pub_enc(int flen, const unsigned char *from,
  516. unsigned char *to, RSA *rsa,int padding)
  517. {
  518. long returnCode;
  519. long reasonCode;
  520. long lflen = flen;
  521. long exitDataLength = 0;
  522. unsigned char exitData[8];
  523. long ruleArrayLength = 1;
  524. unsigned char ruleArray[8] = "PKCS-1.2";
  525. long dataStructureLength = 0;
  526. unsigned char dataStructure[8];
  527. long outputLength = RSA_size(rsa);
  528. long keyTokenLength;
  529. unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
  530. keyTokenLength = *(long*)keyToken;
  531. keyToken+=sizeof(long);
  532. pkaEncrypt(&returnCode, &reasonCode, &exitDataLength, exitData,
  533. &ruleArrayLength, ruleArray, &lflen, (unsigned char*)from,
  534. &dataStructureLength, dataStructure, &keyTokenLength,
  535. keyToken, &outputLength, to);
  536. if (returnCode || reasonCode)
  537. return -(returnCode << 16 | reasonCode);
  538. return outputLength;
  539. }
  540. static int cca_rsa_priv_dec(int flen, const unsigned char *from,
  541. unsigned char *to, RSA *rsa,int padding)
  542. {
  543. long returnCode;
  544. long reasonCode;
  545. long lflen = flen;
  546. long exitDataLength = 0;
  547. unsigned char exitData[8];
  548. long ruleArrayLength = 1;
  549. unsigned char ruleArray[8] = "PKCS-1.2";
  550. long dataStructureLength = 0;
  551. unsigned char dataStructure[8];
  552. long outputLength = RSA_size(rsa);
  553. long keyTokenLength;
  554. unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
  555. keyTokenLength = *(long*)keyToken;
  556. keyToken+=sizeof(long);
  557. pkaDecrypt(&returnCode, &reasonCode, &exitDataLength, exitData,
  558. &ruleArrayLength, ruleArray, &lflen, (unsigned char*)from,
  559. &dataStructureLength, dataStructure, &keyTokenLength,
  560. keyToken, &outputLength, to);
  561. return (returnCode | reasonCode) ? 0 : 1;
  562. }
  563. #define SSL_SIG_LEN 36
  564. static int cca_rsa_verify(int type, const unsigned char *m, unsigned int m_len,
  565. unsigned char *sigbuf, unsigned int siglen, const RSA *rsa)
  566. {
  567. long returnCode;
  568. long reasonCode;
  569. long lsiglen = siglen;
  570. long exitDataLength = 0;
  571. unsigned char exitData[8];
  572. long ruleArrayLength = 1;
  573. unsigned char ruleArray[8] = "PKCS-1.1";
  574. long keyTokenLength;
  575. unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
  576. long length = SSL_SIG_LEN;
  577. long keyLength ;
  578. unsigned char *hashBuffer = NULL;
  579. X509_SIG sig;
  580. ASN1_TYPE parameter;
  581. X509_ALGOR algorithm;
  582. ASN1_OCTET_STRING digest;
  583. keyTokenLength = *(long*)keyToken;
  584. keyToken+=sizeof(long);
  585. if (type == NID_md5 || type == NID_sha1)
  586. {
  587. sig.algor = &algorithm;
  588. algorithm.algorithm = OBJ_nid2obj(type);
  589. if (!algorithm.algorithm)
  590. {
  591. CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
  592. CCA4758_R_UNKNOWN_ALGORITHM_TYPE);
  593. return 0;
  594. }
  595. if (!algorithm.algorithm->length)
  596. {
  597. CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
  598. CCA4758_R_ASN1_OID_UNKNOWN_FOR_MD);
  599. return 0;
  600. }
  601. parameter.type = V_ASN1_NULL;
  602. parameter.value.ptr = NULL;
  603. algorithm.parameter = &parameter;
  604. sig.digest = &digest;
  605. sig.digest->data = (unsigned char*)m;
  606. sig.digest->length = m_len;
  607. length = i2d_X509_SIG(&sig, NULL);
  608. }
  609. keyLength = RSA_size(rsa);
  610. if (length - RSA_PKCS1_PADDING > keyLength)
  611. {
  612. CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
  613. CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
  614. return 0;
  615. }
  616. switch (type)
  617. {
  618. case NID_md5_sha1 :
  619. if (m_len != SSL_SIG_LEN)
  620. {
  621. CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
  622. CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
  623. return 0;
  624. }
  625. hashBuffer = (unsigned char *)m;
  626. length = m_len;
  627. break;
  628. case NID_md5 :
  629. {
  630. unsigned char *ptr;
  631. ptr = hashBuffer = OPENSSL_malloc(
  632. (unsigned int)keyLength+1);
  633. if (!hashBuffer)
  634. {
  635. CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
  636. ERR_R_MALLOC_FAILURE);
  637. return 0;
  638. }
  639. i2d_X509_SIG(&sig, &ptr);
  640. }
  641. break;
  642. case NID_sha1 :
  643. {
  644. unsigned char *ptr;
  645. ptr = hashBuffer = OPENSSL_malloc(
  646. (unsigned int)keyLength+1);
  647. if (!hashBuffer)
  648. {
  649. CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
  650. ERR_R_MALLOC_FAILURE);
  651. return 0;
  652. }
  653. i2d_X509_SIG(&sig, &ptr);
  654. }
  655. break;
  656. default:
  657. return 0;
  658. }
  659. digitalSignatureVerify(&returnCode, &reasonCode, &exitDataLength,
  660. exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
  661. keyToken, &length, hashBuffer, &lsiglen, sigbuf);
  662. if (type == NID_sha1 || type == NID_md5)
  663. {
  664. OPENSSL_cleanse(hashBuffer, keyLength+1);
  665. OPENSSL_free(hashBuffer);
  666. }
  667. return ((returnCode || reasonCode) ? 0 : 1);
  668. }
  669. #define SSL_SIG_LEN 36
  670. static int cca_rsa_sign(int type, const unsigned char *m, unsigned int m_len,
  671. unsigned char *sigret, unsigned int *siglen, const RSA *rsa)
  672. {
  673. long returnCode;
  674. long reasonCode;
  675. long exitDataLength = 0;
  676. unsigned char exitData[8];
  677. long ruleArrayLength = 1;
  678. unsigned char ruleArray[8] = "PKCS-1.1";
  679. long outputLength=256;
  680. long outputBitLength;
  681. long keyTokenLength;
  682. unsigned char *hashBuffer = NULL;
  683. unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
  684. long length = SSL_SIG_LEN;
  685. long keyLength ;
  686. X509_SIG sig;
  687. ASN1_TYPE parameter;
  688. X509_ALGOR algorithm;
  689. ASN1_OCTET_STRING digest;
  690. keyTokenLength = *(long*)keyToken;
  691. keyToken+=sizeof(long);
  692. if (type == NID_md5 || type == NID_sha1)
  693. {
  694. sig.algor = &algorithm;
  695. algorithm.algorithm = OBJ_nid2obj(type);
  696. if (!algorithm.algorithm)
  697. {
  698. CCA4758err(CCA4758_F_CCA_RSA_SIGN,
  699. CCA4758_R_UNKNOWN_ALGORITHM_TYPE);
  700. return 0;
  701. }
  702. if (!algorithm.algorithm->length)
  703. {
  704. CCA4758err(CCA4758_F_CCA_RSA_SIGN,
  705. CCA4758_R_ASN1_OID_UNKNOWN_FOR_MD);
  706. return 0;
  707. }
  708. parameter.type = V_ASN1_NULL;
  709. parameter.value.ptr = NULL;
  710. algorithm.parameter = &parameter;
  711. sig.digest = &digest;
  712. sig.digest->data = (unsigned char*)m;
  713. sig.digest->length = m_len;
  714. length = i2d_X509_SIG(&sig, NULL);
  715. }
  716. keyLength = RSA_size(rsa);
  717. if (length - RSA_PKCS1_PADDING > keyLength)
  718. {
  719. CCA4758err(CCA4758_F_CCA_RSA_SIGN,
  720. CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
  721. return 0;
  722. }
  723. switch (type)
  724. {
  725. case NID_md5_sha1 :
  726. if (m_len != SSL_SIG_LEN)
  727. {
  728. CCA4758err(CCA4758_F_CCA_RSA_SIGN,
  729. CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
  730. return 0;
  731. }
  732. hashBuffer = (unsigned char*)m;
  733. length = m_len;
  734. break;
  735. case NID_md5 :
  736. {
  737. unsigned char *ptr;
  738. ptr = hashBuffer = OPENSSL_malloc(
  739. (unsigned int)keyLength+1);
  740. if (!hashBuffer)
  741. {
  742. CCA4758err(CCA4758_F_CCA_RSA_SIGN,
  743. ERR_R_MALLOC_FAILURE);
  744. return 0;
  745. }
  746. i2d_X509_SIG(&sig, &ptr);
  747. }
  748. break;
  749. case NID_sha1 :
  750. {
  751. unsigned char *ptr;
  752. ptr = hashBuffer = OPENSSL_malloc(
  753. (unsigned int)keyLength+1);
  754. if (!hashBuffer)
  755. {
  756. CCA4758err(CCA4758_F_CCA_RSA_SIGN,
  757. ERR_R_MALLOC_FAILURE);
  758. return 0;
  759. }
  760. i2d_X509_SIG(&sig, &ptr);
  761. }
  762. break;
  763. default:
  764. return 0;
  765. }
  766. digitalSignatureGenerate(&returnCode, &reasonCode, &exitDataLength,
  767. exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
  768. keyToken, &length, hashBuffer, &outputLength, &outputBitLength,
  769. sigret);
  770. if (type == NID_sha1 || type == NID_md5)
  771. {
  772. OPENSSL_cleanse(hashBuffer, keyLength+1);
  773. OPENSSL_free(hashBuffer);
  774. }
  775. *siglen = outputLength;
  776. return ((returnCode || reasonCode) ? 0 : 1);
  777. }
  778. static int getModulusAndExponent(const unsigned char*token, long *exponentLength,
  779. unsigned char *exponent, long *modulusLength, long *modulusFieldLength,
  780. unsigned char *modulus)
  781. {
  782. unsigned long len;
  783. if (*token++ != (char)0x1E) /* internal PKA token? */
  784. return 0;
  785. if (*token++) /* token version must be zero */
  786. return 0;
  787. len = *token++;
  788. len = len << 8;
  789. len |= (unsigned char)*token++;
  790. token += 4; /* skip reserved bytes */
  791. if (*token++ == (char)0x04)
  792. {
  793. if (*token++) /* token version must be zero */
  794. return 0;
  795. len = *token++;
  796. len = len << 8;
  797. len |= (unsigned char)*token++;
  798. token+=2; /* skip reserved section */
  799. len = *token++;
  800. len = len << 8;
  801. len |= (unsigned char)*token++;
  802. *exponentLength = len;
  803. len = *token++;
  804. len = len << 8;
  805. len |= (unsigned char)*token++;
  806. *modulusLength = len;
  807. len = *token++;
  808. len = len << 8;
  809. len |= (unsigned char)*token++;
  810. *modulusFieldLength = len;
  811. memcpy(exponent, token, *exponentLength);
  812. token+= *exponentLength;
  813. memcpy(modulus, token, *modulusFieldLength);
  814. return 1;
  815. }
  816. return 0;
  817. }
  818. #endif /* OPENSSL_NO_RSA */
  819. static int cca_random_status(void)
  820. {
  821. return 1;
  822. }
  823. static int cca_get_random_bytes(unsigned char* buf, int num)
  824. {
  825. long ret_code;
  826. long reason_code;
  827. long exit_data_length;
  828. unsigned char exit_data[4];
  829. unsigned char form[] = "RANDOM ";
  830. unsigned char rand_buf[8];
  831. while(num >= (int)sizeof(rand_buf))
  832. {
  833. randomNumberGenerate(&ret_code, &reason_code, &exit_data_length,
  834. exit_data, form, rand_buf);
  835. if (ret_code)
  836. return 0;
  837. num -= sizeof(rand_buf);
  838. memcpy(buf, rand_buf, sizeof(rand_buf));
  839. buf += sizeof(rand_buf);
  840. }
  841. if (num)
  842. {
  843. randomNumberGenerate(&ret_code, &reason_code, NULL, NULL,
  844. form, rand_buf);
  845. if (ret_code)
  846. return 0;
  847. memcpy(buf, rand_buf, num);
  848. }
  849. return 1;
  850. }
  851. #ifndef OPENSSL_NO_RSA
  852. static void cca_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad, int idx,
  853. long argl, void *argp)
  854. {
  855. if (item)
  856. OPENSSL_free(item);
  857. }
  858. #endif
  859. /* Goo to handle building as a dynamic engine */
  860. #ifndef OPENSSL_NO_DYNAMIC_ENGINE
  861. static int bind_fn(ENGINE *e, const char *id)
  862. {
  863. if(id && (strcmp(id, engine_4758_cca_id) != 0) &&
  864. (strcmp(id, engine_4758_cca_id_alt) != 0))
  865. return 0;
  866. if(!bind_helper(e))
  867. return 0;
  868. return 1;
  869. }
  870. IMPLEMENT_DYNAMIC_CHECK_FN()
  871. IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
  872. #endif /* OPENSSL_NO_DYNAMIC_ENGINE */
  873. #endif /* !OPENSSL_NO_HW_4758_CCA */
  874. #endif /* !OPENSSL_NO_HW */