e_sureware.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. /*-
  2. * Written by Corinne Dive-Reclus(cdive@baltimore.com)
  3. *
  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. * Written by Corinne Dive-Reclus(cdive@baltimore.com)
  37. *
  38. * Copyright@2001 Baltimore Technologies Ltd.
  39. * All right Reserved.
  40. * *
  41. * THIS FILE IS PROVIDED BY BALTIMORE TECHNOLOGIES ``AS IS'' AND *
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE *
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
  44. * ARE DISCLAIMED. IN NO EVENT SHALL BALTIMORE TECHNOLOGIES BE LIABLE *
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS *
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) *
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY *
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF *
  51. * SUCH DAMAGE. *
  52. ====================================================================*/
  53. #include <stdio.h>
  54. #include <string.h>
  55. #include <openssl/crypto.h>
  56. #include <openssl/pem.h>
  57. #include <openssl/dso.h>
  58. #include <openssl/engine.h>
  59. #include <openssl/rand.h>
  60. #ifndef OPENSSL_NO_RSA
  61. # include <openssl/rsa.h>
  62. #endif
  63. #ifndef OPENSSL_NO_DSA
  64. # include <openssl/dsa.h>
  65. #endif
  66. #ifndef OPENSSL_NO_DH
  67. # include <openssl/dh.h>
  68. #endif
  69. #include <openssl/bn.h>
  70. #ifndef OPENSSL_NO_HW
  71. # ifndef OPENSSL_NO_HW_SUREWARE
  72. # ifdef FLAT_INC
  73. # include "sureware.h"
  74. # else
  75. # include "vendor_defns/sureware.h"
  76. # endif
  77. # define SUREWARE_LIB_NAME "sureware engine"
  78. # include "e_sureware_err.c"
  79. static int surewarehk_ctrl(ENGINE *e, int cmd, long i, void *p,
  80. void (*f) (void));
  81. static int surewarehk_destroy(ENGINE *e);
  82. static int surewarehk_init(ENGINE *e);
  83. static int surewarehk_finish(ENGINE *e);
  84. static int surewarehk_modexp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  85. const BIGNUM *m, BN_CTX *ctx);
  86. /* RSA stuff */
  87. # ifndef OPENSSL_NO_RSA
  88. static int surewarehk_rsa_priv_dec(int flen, const unsigned char *from,
  89. unsigned char *to, RSA *rsa, int padding);
  90. static int surewarehk_rsa_sign(int flen, const unsigned char *from,
  91. unsigned char *to, RSA *rsa, int padding);
  92. # endif
  93. /* RAND stuff */
  94. static int surewarehk_rand_bytes(unsigned char *buf, int num);
  95. static void surewarehk_rand_seed(const void *buf, int num);
  96. static void surewarehk_rand_add(const void *buf, int num, double entropy);
  97. /* KM stuff */
  98. static EVP_PKEY *surewarehk_load_privkey(ENGINE *e, const char *key_id,
  99. UI_METHOD *ui_method,
  100. void *callback_data);
  101. static EVP_PKEY *surewarehk_load_pubkey(ENGINE *e, const char *key_id,
  102. UI_METHOD *ui_method,
  103. void *callback_data);
  104. static void surewarehk_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
  105. int idx, long argl, void *argp);
  106. # if 0
  107. static void surewarehk_dh_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
  108. int idx, long argl, void *argp);
  109. # endif
  110. # ifndef OPENSSL_NO_RSA
  111. /* This function is aliased to mod_exp (with the mont stuff dropped). */
  112. static int surewarehk_mod_exp_mont(BIGNUM *r, const BIGNUM *a,
  113. const BIGNUM *p, const BIGNUM *m,
  114. BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  115. {
  116. return surewarehk_modexp(r, a, p, m, ctx);
  117. }
  118. /* Our internal RSA_METHOD that we provide pointers to */
  119. static RSA_METHOD surewarehk_rsa = {
  120. "SureWare RSA method",
  121. NULL, /* pub_enc */
  122. NULL, /* pub_dec */
  123. surewarehk_rsa_sign, /* our rsa_sign is OpenSSL priv_enc */
  124. surewarehk_rsa_priv_dec, /* priv_dec */
  125. NULL, /* mod_exp */
  126. surewarehk_mod_exp_mont, /* mod_exp_mongomery */
  127. NULL, /* init */
  128. NULL, /* finish */
  129. 0, /* RSA flag */
  130. NULL,
  131. NULL, /* OpenSSL sign */
  132. NULL, /* OpenSSL verify */
  133. NULL /* keygen */
  134. };
  135. # endif
  136. # ifndef OPENSSL_NO_DH
  137. /* Our internal DH_METHOD that we provide pointers to */
  138. /* This function is aliased to mod_exp (with the dh and mont dropped). */
  139. static int surewarehk_modexp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
  140. const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  141. BN_MONT_CTX *m_ctx)
  142. {
  143. return surewarehk_modexp(r, a, p, m, ctx);
  144. }
  145. static DH_METHOD surewarehk_dh = {
  146. "SureWare DH method",
  147. NULL, /* gen_key */
  148. NULL, /* agree, */
  149. surewarehk_modexp_dh, /* dh mod exp */
  150. NULL, /* init */
  151. NULL, /* finish */
  152. 0, /* flags */
  153. NULL,
  154. NULL
  155. };
  156. # endif
  157. static RAND_METHOD surewarehk_rand = {
  158. /* "SureWare RAND method", */
  159. surewarehk_rand_seed,
  160. surewarehk_rand_bytes,
  161. NULL, /* cleanup */
  162. surewarehk_rand_add,
  163. surewarehk_rand_bytes,
  164. NULL, /* rand_status */
  165. };
  166. # ifndef OPENSSL_NO_DSA
  167. /* DSA stuff */
  168. static DSA_SIG *surewarehk_dsa_do_sign(const unsigned char *dgst, int dlen,
  169. DSA *dsa);
  170. static int surewarehk_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
  171. BIGNUM *p1, BIGNUM *a2, BIGNUM *p2,
  172. BIGNUM *m, BN_CTX *ctx,
  173. BN_MONT_CTX *in_mont)
  174. {
  175. BIGNUM t;
  176. int to_return = 0;
  177. BN_init(&t);
  178. /* let rr = a1 ^ p1 mod m */
  179. if (!surewarehk_modexp(rr, a1, p1, m, ctx))
  180. goto end;
  181. /* let t = a2 ^ p2 mod m */
  182. if (!surewarehk_modexp(&t, a2, p2, m, ctx))
  183. goto end;
  184. /* let rr = rr * t mod m */
  185. if (!BN_mod_mul(rr, rr, &t, m, ctx))
  186. goto end;
  187. to_return = 1;
  188. end:
  189. BN_free(&t);
  190. return to_return;
  191. }
  192. static DSA_METHOD surewarehk_dsa = {
  193. "SureWare DSA method",
  194. surewarehk_dsa_do_sign,
  195. NULL, /* sign setup */
  196. NULL, /* verify, */
  197. surewarehk_dsa_mod_exp, /* mod exp */
  198. NULL, /* bn mod exp */
  199. NULL, /* init */
  200. NULL, /* finish */
  201. 0,
  202. NULL,
  203. NULL,
  204. NULL
  205. };
  206. # endif
  207. static const char *engine_sureware_id = "sureware";
  208. static const char *engine_sureware_name = "SureWare hardware engine support";
  209. /* Now, to our own code */
  210. /*
  211. * As this is only ever called once, there's no need for locking (indeed -
  212. * the lock will already be held by our caller!!!)
  213. */
  214. static int bind_sureware(ENGINE *e)
  215. {
  216. # ifndef OPENSSL_NO_RSA
  217. const RSA_METHOD *meth1;
  218. # endif
  219. # ifndef OPENSSL_NO_DSA
  220. const DSA_METHOD *meth2;
  221. # endif
  222. # ifndef OPENSSL_NO_DH
  223. const DH_METHOD *meth3;
  224. # endif
  225. if (!ENGINE_set_id(e, engine_sureware_id) ||
  226. !ENGINE_set_name(e, engine_sureware_name) ||
  227. # ifndef OPENSSL_NO_RSA
  228. !ENGINE_set_RSA(e, &surewarehk_rsa) ||
  229. # endif
  230. # ifndef OPENSSL_NO_DSA
  231. !ENGINE_set_DSA(e, &surewarehk_dsa) ||
  232. # endif
  233. # ifndef OPENSSL_NO_DH
  234. !ENGINE_set_DH(e, &surewarehk_dh) ||
  235. # endif
  236. !ENGINE_set_RAND(e, &surewarehk_rand) ||
  237. !ENGINE_set_destroy_function(e, surewarehk_destroy) ||
  238. !ENGINE_set_init_function(e, surewarehk_init) ||
  239. !ENGINE_set_finish_function(e, surewarehk_finish) ||
  240. !ENGINE_set_ctrl_function(e, surewarehk_ctrl) ||
  241. !ENGINE_set_load_privkey_function(e, surewarehk_load_privkey) ||
  242. !ENGINE_set_load_pubkey_function(e, surewarehk_load_pubkey))
  243. return 0;
  244. # ifndef OPENSSL_NO_RSA
  245. /*
  246. * We know that the "PKCS1_SSLeay()" functions hook properly to the
  247. * cswift-specific mod_exp and mod_exp_crt so we use those functions. NB:
  248. * We don't use ENGINE_openssl() or anything "more generic" because
  249. * something like the RSAref code may not hook properly, and if you own
  250. * one of these cards then you have the right to do RSA operations on it
  251. * anyway!
  252. */
  253. meth1 = RSA_PKCS1_SSLeay();
  254. if (meth1) {
  255. surewarehk_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
  256. surewarehk_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
  257. }
  258. # endif
  259. # ifndef OPENSSL_NO_DSA
  260. /*
  261. * Use the DSA_OpenSSL() method and just hook the mod_exp-ish bits.
  262. */
  263. meth2 = DSA_OpenSSL();
  264. if (meth2) {
  265. surewarehk_dsa.dsa_do_verify = meth2->dsa_do_verify;
  266. }
  267. # endif
  268. # ifndef OPENSSL_NO_DH
  269. /* Much the same for Diffie-Hellman */
  270. meth3 = DH_OpenSSL();
  271. if (meth3) {
  272. surewarehk_dh.generate_key = meth3->generate_key;
  273. surewarehk_dh.compute_key = meth3->compute_key;
  274. }
  275. # endif
  276. /* Ensure the sureware error handling is set up */
  277. ERR_load_SUREWARE_strings();
  278. return 1;
  279. }
  280. # ifndef OPENSSL_NO_DYNAMIC_ENGINE
  281. static int bind_helper(ENGINE *e, const char *id)
  282. {
  283. if (id && (strcmp(id, engine_sureware_id) != 0))
  284. return 0;
  285. if (!bind_sureware(e))
  286. return 0;
  287. return 1;
  288. }
  289. IMPLEMENT_DYNAMIC_CHECK_FN()
  290. IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
  291. # else
  292. static ENGINE *engine_sureware(void)
  293. {
  294. ENGINE *ret = ENGINE_new();
  295. if (!ret)
  296. return NULL;
  297. if (!bind_sureware(ret)) {
  298. ENGINE_free(ret);
  299. return NULL;
  300. }
  301. return ret;
  302. }
  303. void ENGINE_load_sureware(void)
  304. {
  305. /* Copied from eng_[openssl|dyn].c */
  306. ENGINE *toadd = engine_sureware();
  307. if (!toadd)
  308. return;
  309. ENGINE_add(toadd);
  310. ENGINE_free(toadd);
  311. ERR_clear_error();
  312. }
  313. # endif
  314. /*
  315. * This is a process-global DSO handle used for loading and unloading the
  316. * SureWareHook library. NB: This is only set (or unset) during an init() or
  317. * finish() call (reference counts permitting) and they're operating with
  318. * global locks, so this should be thread-safe implicitly.
  319. */
  320. static DSO *surewarehk_dso = NULL;
  321. # ifndef OPENSSL_NO_RSA
  322. /* Index for KM handle. Not really used yet. */
  323. static int rsaHndidx = -1;
  324. # endif
  325. # ifndef OPENSSL_NO_DSA
  326. /* Index for KM handle. Not really used yet. */
  327. static int dsaHndidx = -1;
  328. # endif
  329. /*
  330. * These are the function pointers that are (un)set when the library has
  331. * successfully (un)loaded.
  332. */
  333. static SureWareHook_Init_t *p_surewarehk_Init = NULL;
  334. static SureWareHook_Finish_t *p_surewarehk_Finish = NULL;
  335. static SureWareHook_Rand_Bytes_t *p_surewarehk_Rand_Bytes = NULL;
  336. static SureWareHook_Rand_Seed_t *p_surewarehk_Rand_Seed = NULL;
  337. static SureWareHook_Load_Privkey_t *p_surewarehk_Load_Privkey = NULL;
  338. static SureWareHook_Info_Pubkey_t *p_surewarehk_Info_Pubkey = NULL;
  339. static SureWareHook_Load_Rsa_Pubkey_t *p_surewarehk_Load_Rsa_Pubkey = NULL;
  340. static SureWareHook_Load_Dsa_Pubkey_t *p_surewarehk_Load_Dsa_Pubkey = NULL;
  341. static SureWareHook_Free_t *p_surewarehk_Free = NULL;
  342. static SureWareHook_Rsa_Priv_Dec_t *p_surewarehk_Rsa_Priv_Dec = NULL;
  343. static SureWareHook_Rsa_Sign_t *p_surewarehk_Rsa_Sign = NULL;
  344. static SureWareHook_Dsa_Sign_t *p_surewarehk_Dsa_Sign = NULL;
  345. static SureWareHook_Mod_Exp_t *p_surewarehk_Mod_Exp = NULL;
  346. /* Used in the DSO operations. */
  347. static const char *surewarehk_LIBNAME = "SureWareHook";
  348. static const char *n_surewarehk_Init = "SureWareHook_Init";
  349. static const char *n_surewarehk_Finish = "SureWareHook_Finish";
  350. static const char *n_surewarehk_Rand_Bytes = "SureWareHook_Rand_Bytes";
  351. static const char *n_surewarehk_Rand_Seed = "SureWareHook_Rand_Seed";
  352. static const char *n_surewarehk_Load_Privkey = "SureWareHook_Load_Privkey";
  353. static const char *n_surewarehk_Info_Pubkey = "SureWareHook_Info_Pubkey";
  354. static const char *n_surewarehk_Load_Rsa_Pubkey =
  355. "SureWareHook_Load_Rsa_Pubkey";
  356. static const char *n_surewarehk_Load_Dsa_Pubkey =
  357. "SureWareHook_Load_Dsa_Pubkey";
  358. static const char *n_surewarehk_Free = "SureWareHook_Free";
  359. static const char *n_surewarehk_Rsa_Priv_Dec = "SureWareHook_Rsa_Priv_Dec";
  360. static const char *n_surewarehk_Rsa_Sign = "SureWareHook_Rsa_Sign";
  361. static const char *n_surewarehk_Dsa_Sign = "SureWareHook_Dsa_Sign";
  362. static const char *n_surewarehk_Mod_Exp = "SureWareHook_Mod_Exp";
  363. static BIO *logstream = NULL;
  364. /*
  365. * SureWareHook library functions and mechanics - these are used by the
  366. * higher-level functions further down. NB: As and where there's no error
  367. * checking, take a look lower down where these functions are called, the
  368. * checking and error handling is probably down there.
  369. */
  370. static int threadsafe = 1;
  371. static int surewarehk_ctrl(ENGINE *e, int cmd, long i, void *p,
  372. void (*f) (void))
  373. {
  374. int to_return = 1;
  375. switch (cmd) {
  376. case ENGINE_CTRL_SET_LOGSTREAM:
  377. {
  378. BIO *bio = (BIO *)p;
  379. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  380. if (logstream) {
  381. BIO_free(logstream);
  382. logstream = NULL;
  383. }
  384. if (CRYPTO_add(&bio->references, 1, CRYPTO_LOCK_BIO) > 1)
  385. logstream = bio;
  386. else
  387. SUREWAREerr(SUREWARE_F_SUREWAREHK_CTRL,
  388. SUREWARE_R_BIO_WAS_FREED);
  389. }
  390. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  391. break;
  392. /*
  393. * This will prevent the initialisation function from "installing"
  394. * the mutex-handling callbacks, even if they are available from
  395. * within the library (or were provided to the library from the
  396. * calling application). This is to remove any baggage for
  397. * applications not using multithreading.
  398. */
  399. case ENGINE_CTRL_CHIL_NO_LOCKING:
  400. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  401. threadsafe = 0;
  402. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  403. break;
  404. /* The command isn't understood by this engine */
  405. default:
  406. SUREWAREerr(SUREWARE_F_SUREWAREHK_CTRL,
  407. ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
  408. to_return = 0;
  409. break;
  410. }
  411. return to_return;
  412. }
  413. /* Destructor (complements the "ENGINE_surewarehk()" constructor) */
  414. static int surewarehk_destroy(ENGINE *e)
  415. {
  416. ERR_unload_SUREWARE_strings();
  417. return 1;
  418. }
  419. /* (de)initialisation functions. */
  420. static int surewarehk_init(ENGINE *e)
  421. {
  422. char msg[64] = "ENGINE_init";
  423. SureWareHook_Init_t *p1 = NULL;
  424. SureWareHook_Finish_t *p2 = NULL;
  425. SureWareHook_Rand_Bytes_t *p3 = NULL;
  426. SureWareHook_Rand_Seed_t *p4 = NULL;
  427. SureWareHook_Load_Privkey_t *p5 = NULL;
  428. SureWareHook_Load_Rsa_Pubkey_t *p6 = NULL;
  429. SureWareHook_Free_t *p7 = NULL;
  430. SureWareHook_Rsa_Priv_Dec_t *p8 = NULL;
  431. SureWareHook_Rsa_Sign_t *p9 = NULL;
  432. SureWareHook_Dsa_Sign_t *p12 = NULL;
  433. SureWareHook_Info_Pubkey_t *p13 = NULL;
  434. SureWareHook_Load_Dsa_Pubkey_t *p14 = NULL;
  435. SureWareHook_Mod_Exp_t *p15 = NULL;
  436. if (surewarehk_dso != NULL) {
  437. SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT, ENGINE_R_ALREADY_LOADED);
  438. goto err;
  439. }
  440. /* Attempt to load libsurewarehk.so/surewarehk.dll/whatever. */
  441. surewarehk_dso = DSO_load(NULL, surewarehk_LIBNAME, NULL, 0);
  442. if (surewarehk_dso == NULL) {
  443. SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT, ENGINE_R_DSO_FAILURE);
  444. goto err;
  445. }
  446. if (!
  447. (p1 =
  448. (SureWareHook_Init_t *) DSO_bind_func(surewarehk_dso,
  449. n_surewarehk_Init))
  450. || !(p2 =
  451. (SureWareHook_Finish_t *) DSO_bind_func(surewarehk_dso,
  452. n_surewarehk_Finish))
  453. || !(p3 =
  454. (SureWareHook_Rand_Bytes_t *) DSO_bind_func(surewarehk_dso,
  455. n_surewarehk_Rand_Bytes))
  456. || !(p4 =
  457. (SureWareHook_Rand_Seed_t *) DSO_bind_func(surewarehk_dso,
  458. n_surewarehk_Rand_Seed))
  459. || !(p5 =
  460. (SureWareHook_Load_Privkey_t *) DSO_bind_func(surewarehk_dso,
  461. n_surewarehk_Load_Privkey))
  462. || !(p6 =
  463. (SureWareHook_Load_Rsa_Pubkey_t *) DSO_bind_func(surewarehk_dso,
  464. n_surewarehk_Load_Rsa_Pubkey))
  465. || !(p7 =
  466. (SureWareHook_Free_t *) DSO_bind_func(surewarehk_dso, n_surewarehk_Free))
  467. || !(p8 =
  468. (SureWareHook_Rsa_Priv_Dec_t *) DSO_bind_func(surewarehk_dso,
  469. n_surewarehk_Rsa_Priv_Dec))
  470. || !(p9 =
  471. (SureWareHook_Rsa_Sign_t *) DSO_bind_func(surewarehk_dso,
  472. n_surewarehk_Rsa_Sign))
  473. || !(p12 =
  474. (SureWareHook_Dsa_Sign_t *) DSO_bind_func(surewarehk_dso,
  475. n_surewarehk_Dsa_Sign))
  476. || !(p13 =
  477. (SureWareHook_Info_Pubkey_t *) DSO_bind_func(surewarehk_dso,
  478. n_surewarehk_Info_Pubkey))
  479. || !(p14 =
  480. (SureWareHook_Load_Dsa_Pubkey_t *) DSO_bind_func(surewarehk_dso,
  481. n_surewarehk_Load_Dsa_Pubkey))
  482. || !(p15 =
  483. (SureWareHook_Mod_Exp_t *) DSO_bind_func(surewarehk_dso,
  484. n_surewarehk_Mod_Exp))) {
  485. SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT, ENGINE_R_DSO_FAILURE);
  486. goto err;
  487. }
  488. /* Copy the pointers */
  489. p_surewarehk_Init = p1;
  490. p_surewarehk_Finish = p2;
  491. p_surewarehk_Rand_Bytes = p3;
  492. p_surewarehk_Rand_Seed = p4;
  493. p_surewarehk_Load_Privkey = p5;
  494. p_surewarehk_Load_Rsa_Pubkey = p6;
  495. p_surewarehk_Free = p7;
  496. p_surewarehk_Rsa_Priv_Dec = p8;
  497. p_surewarehk_Rsa_Sign = p9;
  498. p_surewarehk_Dsa_Sign = p12;
  499. p_surewarehk_Info_Pubkey = p13;
  500. p_surewarehk_Load_Dsa_Pubkey = p14;
  501. p_surewarehk_Mod_Exp = p15;
  502. /* Contact the hardware and initialises it. */
  503. if (p_surewarehk_Init(msg, threadsafe) == SUREWAREHOOK_ERROR_UNIT_FAILURE) {
  504. SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT, SUREWARE_R_UNIT_FAILURE);
  505. goto err;
  506. }
  507. if (p_surewarehk_Init(msg, threadsafe) == SUREWAREHOOK_ERROR_UNIT_FAILURE) {
  508. SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT, SUREWARE_R_UNIT_FAILURE);
  509. goto err;
  510. }
  511. /*
  512. * try to load the default private key, if failed does not return a
  513. * failure but wait for an explicit ENGINE_load_privakey
  514. */
  515. surewarehk_load_privkey(e, NULL, NULL, NULL);
  516. /* Everything's fine. */
  517. # ifndef OPENSSL_NO_RSA
  518. if (rsaHndidx == -1)
  519. rsaHndidx = RSA_get_ex_new_index(0,
  520. "SureWareHook RSA key handle",
  521. NULL, NULL, surewarehk_ex_free);
  522. # endif
  523. # ifndef OPENSSL_NO_DSA
  524. if (dsaHndidx == -1)
  525. dsaHndidx = DSA_get_ex_new_index(0,
  526. "SureWareHook DSA key handle",
  527. NULL, NULL, surewarehk_ex_free);
  528. # endif
  529. return 1;
  530. err:
  531. if (surewarehk_dso)
  532. DSO_free(surewarehk_dso);
  533. surewarehk_dso = NULL;
  534. p_surewarehk_Init = NULL;
  535. p_surewarehk_Finish = NULL;
  536. p_surewarehk_Rand_Bytes = NULL;
  537. p_surewarehk_Rand_Seed = NULL;
  538. p_surewarehk_Load_Privkey = NULL;
  539. p_surewarehk_Load_Rsa_Pubkey = NULL;
  540. p_surewarehk_Free = NULL;
  541. p_surewarehk_Rsa_Priv_Dec = NULL;
  542. p_surewarehk_Rsa_Sign = NULL;
  543. p_surewarehk_Dsa_Sign = NULL;
  544. p_surewarehk_Info_Pubkey = NULL;
  545. p_surewarehk_Load_Dsa_Pubkey = NULL;
  546. p_surewarehk_Mod_Exp = NULL;
  547. return 0;
  548. }
  549. static int surewarehk_finish(ENGINE *e)
  550. {
  551. int to_return = 1;
  552. if (surewarehk_dso == NULL) {
  553. SUREWAREerr(SUREWARE_F_SUREWAREHK_FINISH, ENGINE_R_NOT_LOADED);
  554. to_return = 0;
  555. goto err;
  556. }
  557. p_surewarehk_Finish();
  558. if (!DSO_free(surewarehk_dso)) {
  559. SUREWAREerr(SUREWARE_F_SUREWAREHK_FINISH, ENGINE_R_DSO_FAILURE);
  560. to_return = 0;
  561. goto err;
  562. }
  563. err:
  564. if (logstream)
  565. BIO_free(logstream);
  566. surewarehk_dso = NULL;
  567. p_surewarehk_Init = NULL;
  568. p_surewarehk_Finish = NULL;
  569. p_surewarehk_Rand_Bytes = NULL;
  570. p_surewarehk_Rand_Seed = NULL;
  571. p_surewarehk_Load_Privkey = NULL;
  572. p_surewarehk_Load_Rsa_Pubkey = NULL;
  573. p_surewarehk_Free = NULL;
  574. p_surewarehk_Rsa_Priv_Dec = NULL;
  575. p_surewarehk_Rsa_Sign = NULL;
  576. p_surewarehk_Dsa_Sign = NULL;
  577. p_surewarehk_Info_Pubkey = NULL;
  578. p_surewarehk_Load_Dsa_Pubkey = NULL;
  579. p_surewarehk_Mod_Exp = NULL;
  580. return to_return;
  581. }
  582. static void surewarehk_error_handling(char *const msg, int func, int ret)
  583. {
  584. switch (ret) {
  585. case SUREWAREHOOK_ERROR_UNIT_FAILURE:
  586. ENGINEerr(func, SUREWARE_R_UNIT_FAILURE);
  587. break;
  588. case SUREWAREHOOK_ERROR_FALLBACK:
  589. ENGINEerr(func, SUREWARE_R_REQUEST_FALLBACK);
  590. break;
  591. case SUREWAREHOOK_ERROR_DATA_SIZE:
  592. ENGINEerr(func, SUREWARE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
  593. break;
  594. case SUREWAREHOOK_ERROR_INVALID_PAD:
  595. ENGINEerr(func, SUREWARE_R_PADDING_CHECK_FAILED);
  596. break;
  597. default:
  598. ENGINEerr(func, SUREWARE_R_REQUEST_FAILED);
  599. break;
  600. case 1: /* nothing */
  601. msg[0] = '\0';
  602. }
  603. if (*msg) {
  604. ERR_add_error_data(1, msg);
  605. if (logstream) {
  606. CRYPTO_w_lock(CRYPTO_LOCK_BIO);
  607. BIO_write(logstream, msg, strlen(msg));
  608. CRYPTO_w_unlock(CRYPTO_LOCK_BIO);
  609. }
  610. }
  611. }
  612. static int surewarehk_rand_bytes(unsigned char *buf, int num)
  613. {
  614. int ret = 0;
  615. char msg[64] = "ENGINE_rand_bytes";
  616. if (!p_surewarehk_Rand_Bytes) {
  617. SUREWAREerr(SUREWARE_F_SUREWAREHK_RAND_BYTES,
  618. ENGINE_R_NOT_INITIALISED);
  619. } else {
  620. ret = p_surewarehk_Rand_Bytes(msg, buf, num);
  621. surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_RAND_BYTES, ret);
  622. }
  623. return ret == 1 ? 1 : 0;
  624. }
  625. static void surewarehk_rand_seed(const void *buf, int num)
  626. {
  627. int ret = 0;
  628. char msg[64] = "ENGINE_rand_seed";
  629. if (!p_surewarehk_Rand_Seed) {
  630. SUREWAREerr(SUREWARE_F_SUREWAREHK_RAND_SEED,
  631. ENGINE_R_NOT_INITIALISED);
  632. } else {
  633. ret = p_surewarehk_Rand_Seed(msg, buf, num);
  634. surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_RAND_SEED, ret);
  635. }
  636. }
  637. static void surewarehk_rand_add(const void *buf, int num, double entropy)
  638. {
  639. surewarehk_rand_seed(buf, num);
  640. }
  641. static EVP_PKEY *sureware_load_public(ENGINE *e, const char *key_id,
  642. char *hptr, unsigned long el,
  643. char keytype)
  644. {
  645. EVP_PKEY *res = NULL;
  646. # ifndef OPENSSL_NO_RSA
  647. RSA *rsatmp = NULL;
  648. # endif
  649. # ifndef OPENSSL_NO_DSA
  650. DSA *dsatmp = NULL;
  651. # endif
  652. char msg[64] = "sureware_load_public";
  653. int ret = 0;
  654. if (!p_surewarehk_Load_Rsa_Pubkey || !p_surewarehk_Load_Dsa_Pubkey) {
  655. SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,
  656. ENGINE_R_NOT_INITIALISED);
  657. goto err;
  658. }
  659. switch (keytype) {
  660. # ifndef OPENSSL_NO_RSA
  661. case 1:
  662. /*RSA*/
  663. /* set private external reference */
  664. rsatmp = RSA_new_method(e);
  665. RSA_set_ex_data(rsatmp, rsaHndidx, hptr);
  666. rsatmp->flags |= RSA_FLAG_EXT_PKEY;
  667. /* set public big nums */
  668. rsatmp->e = BN_new();
  669. rsatmp->n = BN_new();
  670. bn_expand2(rsatmp->e, el / sizeof(BN_ULONG));
  671. bn_expand2(rsatmp->n, el / sizeof(BN_ULONG));
  672. if (!rsatmp->e || rsatmp->e->dmax != (int)(el / sizeof(BN_ULONG)) ||
  673. !rsatmp->n || rsatmp->n->dmax != (int)(el / sizeof(BN_ULONG)))
  674. goto err;
  675. ret = p_surewarehk_Load_Rsa_Pubkey(msg, key_id, el,
  676. (unsigned long *)rsatmp->n->d,
  677. (unsigned long *)rsatmp->e->d);
  678. surewarehk_error_handling(msg, SUREWARE_F_SUREWARE_LOAD_PUBLIC, ret);
  679. if (ret != 1) {
  680. SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,
  681. ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
  682. goto err;
  683. }
  684. /* normalise pub e and pub n */
  685. rsatmp->e->top = el / sizeof(BN_ULONG);
  686. bn_fix_top(rsatmp->e);
  687. rsatmp->n->top = el / sizeof(BN_ULONG);
  688. bn_fix_top(rsatmp->n);
  689. /* create an EVP object: engine + rsa key */
  690. res = EVP_PKEY_new();
  691. EVP_PKEY_assign_RSA(res, rsatmp);
  692. break;
  693. # endif
  694. # ifndef OPENSSL_NO_DSA
  695. case 2:
  696. /*DSA*/
  697. /* set private/public external reference */
  698. dsatmp = DSA_new_method(e);
  699. DSA_set_ex_data(dsatmp, dsaHndidx, hptr);
  700. /*
  701. * dsatmp->flags |= DSA_FLAG_EXT_PKEY;
  702. */
  703. /* set public key */
  704. dsatmp->pub_key = BN_new();
  705. dsatmp->p = BN_new();
  706. dsatmp->q = BN_new();
  707. dsatmp->g = BN_new();
  708. bn_expand2(dsatmp->pub_key, el / sizeof(BN_ULONG));
  709. bn_expand2(dsatmp->p, el / sizeof(BN_ULONG));
  710. bn_expand2(dsatmp->q, 20 / sizeof(BN_ULONG));
  711. bn_expand2(dsatmp->g, el / sizeof(BN_ULONG));
  712. if (!dsatmp->pub_key
  713. || dsatmp->pub_key->dmax != (int)(el / sizeof(BN_ULONG))
  714. || !dsatmp->p || dsatmp->p->dmax != (int)(el / sizeof(BN_ULONG))
  715. || !dsatmp->q || dsatmp->q->dmax != 20 / sizeof(BN_ULONG)
  716. || !dsatmp->g || dsatmp->g->dmax != (int)(el / sizeof(BN_ULONG)))
  717. goto err;
  718. ret = p_surewarehk_Load_Dsa_Pubkey(msg, key_id, el,
  719. (unsigned long *)dsatmp->
  720. pub_key->d,
  721. (unsigned long *)dsatmp->p->d,
  722. (unsigned long *)dsatmp->q->d,
  723. (unsigned long *)dsatmp->g->d);
  724. surewarehk_error_handling(msg, SUREWARE_F_SUREWARE_LOAD_PUBLIC, ret);
  725. if (ret != 1) {
  726. SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,
  727. ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
  728. goto err;
  729. }
  730. /* set parameters */
  731. /* normalise pubkey and parameters in case of */
  732. dsatmp->pub_key->top = el / sizeof(BN_ULONG);
  733. bn_fix_top(dsatmp->pub_key);
  734. dsatmp->p->top = el / sizeof(BN_ULONG);
  735. bn_fix_top(dsatmp->p);
  736. dsatmp->q->top = 20 / sizeof(BN_ULONG);
  737. bn_fix_top(dsatmp->q);
  738. dsatmp->g->top = el / sizeof(BN_ULONG);
  739. bn_fix_top(dsatmp->g);
  740. /* create an EVP object: engine + rsa key */
  741. res = EVP_PKEY_new();
  742. EVP_PKEY_assign_DSA(res, dsatmp);
  743. break;
  744. # endif
  745. default:
  746. SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,
  747. ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
  748. goto err;
  749. }
  750. return res;
  751. err:
  752. if (res)
  753. EVP_PKEY_free(res);
  754. # ifndef OPENSSL_NO_RSA
  755. if (rsatmp)
  756. RSA_free(rsatmp);
  757. # endif
  758. # ifndef OPENSSL_NO_DSA
  759. if (dsatmp)
  760. DSA_free(dsatmp);
  761. # endif
  762. return NULL;
  763. }
  764. static EVP_PKEY *surewarehk_load_privkey(ENGINE *e, const char *key_id,
  765. UI_METHOD *ui_method,
  766. void *callback_data)
  767. {
  768. EVP_PKEY *res = NULL;
  769. int ret = 0;
  770. unsigned long el = 0;
  771. char *hptr = NULL;
  772. char keytype = 0;
  773. char msg[64] = "ENGINE_load_privkey";
  774. if (!p_surewarehk_Load_Privkey) {
  775. SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PRIVKEY,
  776. ENGINE_R_NOT_INITIALISED);
  777. } else {
  778. ret = p_surewarehk_Load_Privkey(msg, key_id, &hptr, &el, &keytype);
  779. if (ret != 1) {
  780. SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PRIVKEY,
  781. ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
  782. ERR_add_error_data(1, msg);
  783. } else
  784. res = sureware_load_public(e, key_id, hptr, el, keytype);
  785. }
  786. return res;
  787. }
  788. static EVP_PKEY *surewarehk_load_pubkey(ENGINE *e, const char *key_id,
  789. UI_METHOD *ui_method,
  790. void *callback_data)
  791. {
  792. EVP_PKEY *res = NULL;
  793. int ret = 0;
  794. unsigned long el = 0;
  795. char *hptr = NULL;
  796. char keytype = 0;
  797. char msg[64] = "ENGINE_load_pubkey";
  798. if (!p_surewarehk_Info_Pubkey) {
  799. SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PUBKEY,
  800. ENGINE_R_NOT_INITIALISED);
  801. } else {
  802. /* call once to identify if DSA or RSA */
  803. ret = p_surewarehk_Info_Pubkey(msg, key_id, &el, &keytype);
  804. if (ret != 1) {
  805. SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PUBKEY,
  806. ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
  807. ERR_add_error_data(1, msg);
  808. } else
  809. res = sureware_load_public(e, key_id, hptr, el, keytype);
  810. }
  811. return res;
  812. }
  813. /*
  814. * This cleans up an RSA/DSA KM key(do not destroy the key into the hardware)
  815. * , called when ex_data is freed
  816. */
  817. static void surewarehk_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
  818. int idx, long argl, void *argp)
  819. {
  820. if (!p_surewarehk_Free) {
  821. SUREWAREerr(SUREWARE_F_SUREWAREHK_EX_FREE, ENGINE_R_NOT_INITIALISED);
  822. } else
  823. p_surewarehk_Free((char *)item, 0);
  824. }
  825. # if 0
  826. /* not currently used (bug?) */
  827. /*
  828. * This cleans up an DH KM key (destroys the key into hardware), called when
  829. * ex_data is freed
  830. */
  831. static void surewarehk_dh_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
  832. int idx, long argl, void *argp)
  833. {
  834. if (!p_surewarehk_Free) {
  835. SUREWAREerr(SUREWARE_F_SUREWAREHK_DH_EX_FREE,
  836. ENGINE_R_NOT_INITIALISED);
  837. } else
  838. p_surewarehk_Free((char *)item, 1);
  839. }
  840. # endif
  841. /*
  842. * return number of decrypted bytes
  843. */
  844. # ifndef OPENSSL_NO_RSA
  845. static int surewarehk_rsa_priv_dec(int flen, const unsigned char *from,
  846. unsigned char *to, RSA *rsa, int padding)
  847. {
  848. int ret = 0, tlen;
  849. char *buf = NULL, *hptr = NULL;
  850. char msg[64] = "ENGINE_rsa_priv_dec";
  851. if (!p_surewarehk_Rsa_Priv_Dec) {
  852. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
  853. ENGINE_R_NOT_INITIALISED);
  854. }
  855. /* extract ref to private key */
  856. else if (!(hptr = RSA_get_ex_data(rsa, rsaHndidx))) {
  857. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
  858. SUREWARE_R_MISSING_KEY_COMPONENTS);
  859. goto err;
  860. }
  861. /* analyse what padding we can do into the hardware */
  862. if (padding == RSA_PKCS1_PADDING) {
  863. /* do it one shot */
  864. ret =
  865. p_surewarehk_Rsa_Priv_Dec(msg, flen, (unsigned char *)from, &tlen,
  866. to, hptr, SUREWARE_PKCS1_PAD);
  867. surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
  868. ret);
  869. if (ret != 1)
  870. goto err;
  871. ret = tlen;
  872. } else { /* do with no padding into hardware */
  873. ret =
  874. p_surewarehk_Rsa_Priv_Dec(msg, flen, (unsigned char *)from, &tlen,
  875. to, hptr, SUREWARE_NO_PAD);
  876. surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
  877. ret);
  878. if (ret != 1)
  879. goto err;
  880. /* intermediate buffer for padding */
  881. if ((buf = OPENSSL_malloc(tlen)) == NULL) {
  882. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
  883. ERR_R_MALLOC_FAILURE);
  884. goto err;
  885. }
  886. memcpy(buf, to, tlen); /* transfert to into buf */
  887. switch (padding) { /* check padding in software */
  888. # ifndef OPENSSL_NO_SHA
  889. case RSA_PKCS1_OAEP_PADDING:
  890. ret =
  891. RSA_padding_check_PKCS1_OAEP(to, tlen, (unsigned char *)buf,
  892. tlen, tlen, NULL, 0);
  893. break;
  894. # endif
  895. case RSA_SSLV23_PADDING:
  896. ret =
  897. RSA_padding_check_SSLv23(to, tlen, (unsigned char *)buf, flen,
  898. tlen);
  899. break;
  900. case RSA_NO_PADDING:
  901. ret =
  902. RSA_padding_check_none(to, tlen, (unsigned char *)buf, flen,
  903. tlen);
  904. break;
  905. default:
  906. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
  907. SUREWARE_R_UNKNOWN_PADDING_TYPE);
  908. goto err;
  909. }
  910. if (ret < 0)
  911. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
  912. SUREWARE_R_PADDING_CHECK_FAILED);
  913. }
  914. err:
  915. if (buf) {
  916. OPENSSL_cleanse(buf, tlen);
  917. OPENSSL_free(buf);
  918. }
  919. return ret;
  920. }
  921. /*
  922. * Does what OpenSSL rsa_priv_enc does.
  923. */
  924. static int surewarehk_rsa_sign(int flen, const unsigned char *from,
  925. unsigned char *to, RSA *rsa, int padding)
  926. {
  927. int ret = 0, tlen;
  928. char *hptr = NULL;
  929. char msg[64] = "ENGINE_rsa_sign";
  930. if (!p_surewarehk_Rsa_Sign) {
  931. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN, ENGINE_R_NOT_INITIALISED);
  932. }
  933. /* extract ref to private key */
  934. else if (!(hptr = RSA_get_ex_data(rsa, rsaHndidx))) {
  935. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN,
  936. SUREWARE_R_MISSING_KEY_COMPONENTS);
  937. } else {
  938. switch (padding) {
  939. case RSA_PKCS1_PADDING: /* do it in one shot */
  940. ret =
  941. p_surewarehk_Rsa_Sign(msg, flen, (unsigned char *)from, &tlen,
  942. to, hptr, SUREWARE_PKCS1_PAD);
  943. surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_RSA_SIGN,
  944. ret);
  945. break;
  946. case RSA_NO_PADDING:
  947. default:
  948. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN,
  949. SUREWARE_R_UNKNOWN_PADDING_TYPE);
  950. }
  951. }
  952. return ret == 1 ? tlen : ret;
  953. }
  954. # endif
  955. # ifndef OPENSSL_NO_DSA
  956. /* DSA sign and verify */
  957. static DSA_SIG *surewarehk_dsa_do_sign(const unsigned char *from, int flen,
  958. DSA *dsa)
  959. {
  960. int ret = 0;
  961. char *hptr = NULL;
  962. DSA_SIG *psign = NULL;
  963. char msg[64] = "ENGINE_dsa_do_sign";
  964. if (!p_surewarehk_Dsa_Sign) {
  965. SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,
  966. ENGINE_R_NOT_INITIALISED);
  967. goto err;
  968. }
  969. /* extract ref to private key */
  970. else if (!(hptr = DSA_get_ex_data(dsa, dsaHndidx))) {
  971. SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,
  972. SUREWARE_R_MISSING_KEY_COMPONENTS);
  973. goto err;
  974. } else {
  975. if ((psign = DSA_SIG_new()) == NULL) {
  976. SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,
  977. ERR_R_MALLOC_FAILURE);
  978. goto err;
  979. }
  980. psign->r = BN_new();
  981. psign->s = BN_new();
  982. bn_expand2(psign->r, 20 / sizeof(BN_ULONG));
  983. bn_expand2(psign->s, 20 / sizeof(BN_ULONG));
  984. if (!psign->r || psign->r->dmax != 20 / sizeof(BN_ULONG) ||
  985. !psign->s || psign->s->dmax != 20 / sizeof(BN_ULONG))
  986. goto err;
  987. ret = p_surewarehk_Dsa_Sign(msg, flen, from,
  988. (unsigned long *)psign->r->d,
  989. (unsigned long *)psign->s->d, hptr);
  990. surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,
  991. ret);
  992. }
  993. psign->r->top = 20 / sizeof(BN_ULONG);
  994. bn_fix_top(psign->r);
  995. psign->s->top = 20 / sizeof(BN_ULONG);
  996. bn_fix_top(psign->s);
  997. err:
  998. if (psign) {
  999. DSA_SIG_free(psign);
  1000. psign = NULL;
  1001. }
  1002. return psign;
  1003. }
  1004. # endif
  1005. static int surewarehk_modexp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  1006. const BIGNUM *m, BN_CTX *ctx)
  1007. {
  1008. int ret = 0;
  1009. char msg[64] = "ENGINE_modexp";
  1010. if (!p_surewarehk_Mod_Exp) {
  1011. SUREWAREerr(SUREWARE_F_SUREWAREHK_MODEXP, ENGINE_R_NOT_INITIALISED);
  1012. } else {
  1013. bn_expand2(r, m->top);
  1014. if (r && r->dmax == m->top) {
  1015. /* do it */
  1016. ret = p_surewarehk_Mod_Exp(msg,
  1017. m->top * sizeof(BN_ULONG),
  1018. (unsigned long *)m->d,
  1019. p->top * sizeof(BN_ULONG),
  1020. (unsigned long *)p->d,
  1021. a->top * sizeof(BN_ULONG),
  1022. (unsigned long *)a->d,
  1023. (unsigned long *)r->d);
  1024. surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_MODEXP, ret);
  1025. if (ret == 1) {
  1026. /* normalise result */
  1027. r->top = m->top;
  1028. bn_fix_top(r);
  1029. }
  1030. }
  1031. }
  1032. return ret;
  1033. }
  1034. # endif /* !OPENSSL_NO_HW_SureWare */
  1035. #endif /* !OPENSSL_NO_HW */