e_sureware.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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. if(!rsatmp->e || !rsatmp->n)
  671. goto err;
  672. bn_expand2(rsatmp->e, el / sizeof(BN_ULONG));
  673. bn_expand2(rsatmp->n, el / sizeof(BN_ULONG));
  674. if (rsatmp->e->dmax != (int)(el / sizeof(BN_ULONG)) ||
  675. rsatmp->n->dmax != (int)(el / sizeof(BN_ULONG)))
  676. goto err;
  677. ret = p_surewarehk_Load_Rsa_Pubkey(msg, key_id, el,
  678. (unsigned long *)rsatmp->n->d,
  679. (unsigned long *)rsatmp->e->d);
  680. surewarehk_error_handling(msg, SUREWARE_F_SUREWARE_LOAD_PUBLIC, ret);
  681. if (ret != 1) {
  682. SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,
  683. ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
  684. goto err;
  685. }
  686. /* normalise pub e and pub n */
  687. rsatmp->e->top = el / sizeof(BN_ULONG);
  688. bn_fix_top(rsatmp->e);
  689. rsatmp->n->top = el / sizeof(BN_ULONG);
  690. bn_fix_top(rsatmp->n);
  691. /* create an EVP object: engine + rsa key */
  692. res = EVP_PKEY_new();
  693. EVP_PKEY_assign_RSA(res, rsatmp);
  694. break;
  695. # endif
  696. # ifndef OPENSSL_NO_DSA
  697. case 2:
  698. /*DSA*/
  699. /* set private/public external reference */
  700. dsatmp = DSA_new_method(e);
  701. DSA_set_ex_data(dsatmp, dsaHndidx, hptr);
  702. /*
  703. * dsatmp->flags |= DSA_FLAG_EXT_PKEY;
  704. */
  705. /* set public key */
  706. dsatmp->pub_key = BN_new();
  707. dsatmp->p = BN_new();
  708. dsatmp->q = BN_new();
  709. dsatmp->g = BN_new();
  710. if(!dsatmp->pub_key || !dsatmp->p || !dsatmp->q || !dsatmp->g)
  711. goto err;
  712. bn_expand2(dsatmp->pub_key, el / sizeof(BN_ULONG));
  713. bn_expand2(dsatmp->p, el / sizeof(BN_ULONG));
  714. bn_expand2(dsatmp->q, 20 / sizeof(BN_ULONG));
  715. bn_expand2(dsatmp->g, el / sizeof(BN_ULONG));
  716. if (dsatmp->pub_key->dmax != (int)(el / sizeof(BN_ULONG))
  717. || dsatmp->p->dmax != (int)(el / sizeof(BN_ULONG))
  718. || dsatmp->q->dmax != 20 / sizeof(BN_ULONG)
  719. || dsatmp->g->dmax != (int)(el / sizeof(BN_ULONG)))
  720. goto err;
  721. ret = p_surewarehk_Load_Dsa_Pubkey(msg, key_id, el,
  722. (unsigned long *)dsatmp->
  723. pub_key->d,
  724. (unsigned long *)dsatmp->p->d,
  725. (unsigned long *)dsatmp->q->d,
  726. (unsigned long *)dsatmp->g->d);
  727. surewarehk_error_handling(msg, SUREWARE_F_SUREWARE_LOAD_PUBLIC, ret);
  728. if (ret != 1) {
  729. SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,
  730. ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
  731. goto err;
  732. }
  733. /* set parameters */
  734. /* normalise pubkey and parameters in case of */
  735. dsatmp->pub_key->top = el / sizeof(BN_ULONG);
  736. bn_fix_top(dsatmp->pub_key);
  737. dsatmp->p->top = el / sizeof(BN_ULONG);
  738. bn_fix_top(dsatmp->p);
  739. dsatmp->q->top = 20 / sizeof(BN_ULONG);
  740. bn_fix_top(dsatmp->q);
  741. dsatmp->g->top = el / sizeof(BN_ULONG);
  742. bn_fix_top(dsatmp->g);
  743. /* create an EVP object: engine + rsa key */
  744. res = EVP_PKEY_new();
  745. EVP_PKEY_assign_DSA(res, dsatmp);
  746. break;
  747. # endif
  748. default:
  749. SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,
  750. ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
  751. goto err;
  752. }
  753. return res;
  754. err:
  755. # ifndef OPENSSL_NO_RSA
  756. if (rsatmp)
  757. RSA_free(rsatmp);
  758. # endif
  759. # ifndef OPENSSL_NO_DSA
  760. if (dsatmp)
  761. DSA_free(dsatmp);
  762. # endif
  763. return NULL;
  764. }
  765. static EVP_PKEY *surewarehk_load_privkey(ENGINE *e, const char *key_id,
  766. UI_METHOD *ui_method,
  767. void *callback_data)
  768. {
  769. EVP_PKEY *res = NULL;
  770. int ret = 0;
  771. unsigned long el = 0;
  772. char *hptr = NULL;
  773. char keytype = 0;
  774. char msg[64] = "ENGINE_load_privkey";
  775. if (!p_surewarehk_Load_Privkey) {
  776. SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PRIVKEY,
  777. ENGINE_R_NOT_INITIALISED);
  778. } else {
  779. ret = p_surewarehk_Load_Privkey(msg, key_id, &hptr, &el, &keytype);
  780. if (ret != 1) {
  781. SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PRIVKEY,
  782. ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
  783. ERR_add_error_data(1, msg);
  784. } else
  785. res = sureware_load_public(e, key_id, hptr, el, keytype);
  786. }
  787. return res;
  788. }
  789. static EVP_PKEY *surewarehk_load_pubkey(ENGINE *e, const char *key_id,
  790. UI_METHOD *ui_method,
  791. void *callback_data)
  792. {
  793. EVP_PKEY *res = NULL;
  794. int ret = 0;
  795. unsigned long el = 0;
  796. char *hptr = NULL;
  797. char keytype = 0;
  798. char msg[64] = "ENGINE_load_pubkey";
  799. if (!p_surewarehk_Info_Pubkey) {
  800. SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PUBKEY,
  801. ENGINE_R_NOT_INITIALISED);
  802. } else {
  803. /* call once to identify if DSA or RSA */
  804. ret = p_surewarehk_Info_Pubkey(msg, key_id, &el, &keytype);
  805. if (ret != 1) {
  806. SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PUBKEY,
  807. ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
  808. ERR_add_error_data(1, msg);
  809. } else
  810. res = sureware_load_public(e, key_id, hptr, el, keytype);
  811. }
  812. return res;
  813. }
  814. /*
  815. * This cleans up an RSA/DSA KM key(do not destroy the key into the hardware)
  816. * , called when ex_data is freed
  817. */
  818. static void surewarehk_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
  819. int idx, long argl, void *argp)
  820. {
  821. if (!p_surewarehk_Free) {
  822. SUREWAREerr(SUREWARE_F_SUREWAREHK_EX_FREE, ENGINE_R_NOT_INITIALISED);
  823. } else
  824. p_surewarehk_Free((char *)item, 0);
  825. }
  826. # if 0
  827. /* not currently used (bug?) */
  828. /*
  829. * This cleans up an DH KM key (destroys the key into hardware), called when
  830. * ex_data is freed
  831. */
  832. static void surewarehk_dh_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
  833. int idx, long argl, void *argp)
  834. {
  835. if (!p_surewarehk_Free) {
  836. SUREWAREerr(SUREWARE_F_SUREWAREHK_DH_EX_FREE,
  837. ENGINE_R_NOT_INITIALISED);
  838. } else
  839. p_surewarehk_Free((char *)item, 1);
  840. }
  841. # endif
  842. /*
  843. * return number of decrypted bytes
  844. */
  845. # ifndef OPENSSL_NO_RSA
  846. static int surewarehk_rsa_priv_dec(int flen, const unsigned char *from,
  847. unsigned char *to, RSA *rsa, int padding)
  848. {
  849. int ret = 0, tlen;
  850. char *buf = NULL, *hptr = NULL;
  851. char msg[64] = "ENGINE_rsa_priv_dec";
  852. if (!p_surewarehk_Rsa_Priv_Dec) {
  853. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
  854. ENGINE_R_NOT_INITIALISED);
  855. }
  856. /* extract ref to private key */
  857. else if (!(hptr = RSA_get_ex_data(rsa, rsaHndidx))) {
  858. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
  859. SUREWARE_R_MISSING_KEY_COMPONENTS);
  860. goto err;
  861. }
  862. /* analyse what padding we can do into the hardware */
  863. if (padding == RSA_PKCS1_PADDING) {
  864. /* do it one shot */
  865. ret =
  866. p_surewarehk_Rsa_Priv_Dec(msg, flen, (unsigned char *)from, &tlen,
  867. to, hptr, SUREWARE_PKCS1_PAD);
  868. surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
  869. ret);
  870. if (ret != 1)
  871. goto err;
  872. ret = tlen;
  873. } else { /* do with no padding into hardware */
  874. ret =
  875. p_surewarehk_Rsa_Priv_Dec(msg, flen, (unsigned char *)from, &tlen,
  876. to, hptr, SUREWARE_NO_PAD);
  877. surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
  878. ret);
  879. if (ret != 1)
  880. goto err;
  881. /* intermediate buffer for padding */
  882. if ((buf = OPENSSL_malloc(tlen)) == NULL) {
  883. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
  884. ERR_R_MALLOC_FAILURE);
  885. goto err;
  886. }
  887. memcpy(buf, to, tlen); /* transfert to into buf */
  888. switch (padding) { /* check padding in software */
  889. # ifndef OPENSSL_NO_SHA
  890. case RSA_PKCS1_OAEP_PADDING:
  891. ret =
  892. RSA_padding_check_PKCS1_OAEP(to, tlen, (unsigned char *)buf,
  893. tlen, tlen, NULL, 0);
  894. break;
  895. # endif
  896. case RSA_SSLV23_PADDING:
  897. ret =
  898. RSA_padding_check_SSLv23(to, tlen, (unsigned char *)buf, flen,
  899. tlen);
  900. break;
  901. case RSA_NO_PADDING:
  902. ret =
  903. RSA_padding_check_none(to, tlen, (unsigned char *)buf, flen,
  904. tlen);
  905. break;
  906. default:
  907. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
  908. SUREWARE_R_UNKNOWN_PADDING_TYPE);
  909. goto err;
  910. }
  911. if (ret < 0)
  912. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
  913. SUREWARE_R_PADDING_CHECK_FAILED);
  914. }
  915. err:
  916. if (buf) {
  917. OPENSSL_cleanse(buf, tlen);
  918. OPENSSL_free(buf);
  919. }
  920. return ret;
  921. }
  922. /*
  923. * Does what OpenSSL rsa_priv_enc does.
  924. */
  925. static int surewarehk_rsa_sign(int flen, const unsigned char *from,
  926. unsigned char *to, RSA *rsa, int padding)
  927. {
  928. int ret = 0, tlen;
  929. char *hptr = NULL;
  930. char msg[64] = "ENGINE_rsa_sign";
  931. if (!p_surewarehk_Rsa_Sign) {
  932. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN, ENGINE_R_NOT_INITIALISED);
  933. }
  934. /* extract ref to private key */
  935. else if (!(hptr = RSA_get_ex_data(rsa, rsaHndidx))) {
  936. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN,
  937. SUREWARE_R_MISSING_KEY_COMPONENTS);
  938. } else {
  939. switch (padding) {
  940. case RSA_PKCS1_PADDING: /* do it in one shot */
  941. ret =
  942. p_surewarehk_Rsa_Sign(msg, flen, (unsigned char *)from, &tlen,
  943. to, hptr, SUREWARE_PKCS1_PAD);
  944. surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_RSA_SIGN,
  945. ret);
  946. break;
  947. case RSA_NO_PADDING:
  948. default:
  949. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN,
  950. SUREWARE_R_UNKNOWN_PADDING_TYPE);
  951. }
  952. }
  953. return ret == 1 ? tlen : ret;
  954. }
  955. # endif
  956. # ifndef OPENSSL_NO_DSA
  957. /* DSA sign and verify */
  958. static DSA_SIG *surewarehk_dsa_do_sign(const unsigned char *from, int flen,
  959. DSA *dsa)
  960. {
  961. int ret = 0;
  962. char *hptr = NULL;
  963. DSA_SIG *psign = NULL;
  964. char msg[64] = "ENGINE_dsa_do_sign";
  965. if (!p_surewarehk_Dsa_Sign) {
  966. SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,
  967. ENGINE_R_NOT_INITIALISED);
  968. goto err;
  969. }
  970. /* extract ref to private key */
  971. else if (!(hptr = DSA_get_ex_data(dsa, dsaHndidx))) {
  972. SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,
  973. SUREWARE_R_MISSING_KEY_COMPONENTS);
  974. goto err;
  975. } else {
  976. if ((psign = DSA_SIG_new()) == NULL) {
  977. SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,
  978. ERR_R_MALLOC_FAILURE);
  979. goto err;
  980. }
  981. psign->r = BN_new();
  982. psign->s = BN_new();
  983. if(!psign->r || !psign->s)
  984. goto err;
  985. bn_expand2(psign->r, 20 / sizeof(BN_ULONG));
  986. bn_expand2(psign->s, 20 / sizeof(BN_ULONG));
  987. if (psign->r->dmax != 20 / sizeof(BN_ULONG) ||
  988. psign->s->dmax != 20 / sizeof(BN_ULONG))
  989. goto err;
  990. ret = p_surewarehk_Dsa_Sign(msg, flen, from,
  991. (unsigned long *)psign->r->d,
  992. (unsigned long *)psign->s->d, hptr);
  993. surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,
  994. ret);
  995. }
  996. psign->r->top = 20 / sizeof(BN_ULONG);
  997. bn_fix_top(psign->r);
  998. psign->s->top = 20 / sizeof(BN_ULONG);
  999. bn_fix_top(psign->s);
  1000. err:
  1001. if (psign) {
  1002. DSA_SIG_free(psign);
  1003. psign = NULL;
  1004. }
  1005. return psign;
  1006. }
  1007. # endif
  1008. static int surewarehk_modexp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  1009. const BIGNUM *m, BN_CTX *ctx)
  1010. {
  1011. int ret = 0;
  1012. char msg[64] = "ENGINE_modexp";
  1013. if (!p_surewarehk_Mod_Exp) {
  1014. SUREWAREerr(SUREWARE_F_SUREWAREHK_MODEXP, ENGINE_R_NOT_INITIALISED);
  1015. } else if (r) {
  1016. bn_expand2(r, m->top);
  1017. if (r->dmax == m->top) {
  1018. /* do it */
  1019. ret = p_surewarehk_Mod_Exp(msg,
  1020. m->top * sizeof(BN_ULONG),
  1021. (unsigned long *)m->d,
  1022. p->top * sizeof(BN_ULONG),
  1023. (unsigned long *)p->d,
  1024. a->top * sizeof(BN_ULONG),
  1025. (unsigned long *)a->d,
  1026. (unsigned long *)r->d);
  1027. surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_MODEXP, ret);
  1028. if (ret == 1) {
  1029. /* normalise result */
  1030. r->top = m->top;
  1031. bn_fix_top(r);
  1032. }
  1033. }
  1034. }
  1035. return ret;
  1036. }
  1037. # endif /* !OPENSSL_NO_HW_SureWare */
  1038. #endif /* !OPENSSL_NO_HW */