e_sureware.c 31 KB

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