2
0

e_sureware.c 30 KB

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