2 * Written by Corinne Dive-Reclus(cdive@baltimore.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
36 * Written by Corinne Dive-Reclus(cdive@baltimore.com)
38 * Copyright@2001 Baltimore Technologies Ltd.
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 *
52 ====================================================================*/
56 #include <openssl/crypto.h>
57 #include <openssl/pem.h>
58 #include <openssl/dso.h>
59 #include <openssl/engine.h>
60 #include <openssl/rand.h>
61 #ifndef OPENSSL_NO_RSA
62 # include <openssl/rsa.h>
64 #ifndef OPENSSL_NO_DSA
65 # include <openssl/dsa.h>
68 # include <openssl/dh.h>
70 #include <openssl/bn.h>
73 # ifndef OPENSSL_NO_HW_SUREWARE
76 # include "sureware.h"
78 # include "vendor_defns/sureware.h"
81 # define SUREWARE_LIB_NAME "sureware engine"
82 # include "e_sureware_err.c"
84 static int surewarehk_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
,
86 static int surewarehk_destroy(ENGINE
*e
);
87 static int surewarehk_init(ENGINE
*e
);
88 static int surewarehk_finish(ENGINE
*e
);
89 static int surewarehk_modexp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
90 const BIGNUM
*m
, BN_CTX
*ctx
);
93 # ifndef OPENSSL_NO_RSA
94 static int surewarehk_rsa_priv_dec(int flen
, const unsigned char *from
,
95 unsigned char *to
, RSA
*rsa
, int padding
);
96 static int surewarehk_rsa_sign(int flen
, const unsigned char *from
,
97 unsigned char *to
, RSA
*rsa
, int padding
);
101 static int surewarehk_rand_bytes(unsigned char *buf
, int num
);
102 static int surewarehk_rand_seed(const void *buf
, int num
);
103 static int surewarehk_rand_add(const void *buf
, int num
, double entropy
);
106 static EVP_PKEY
*surewarehk_load_privkey(ENGINE
*e
, const char *key_id
,
107 UI_METHOD
*ui_method
,
108 void *callback_data
);
109 static EVP_PKEY
*surewarehk_load_pubkey(ENGINE
*e
, const char *key_id
,
110 UI_METHOD
*ui_method
,
111 void *callback_data
);
112 static void surewarehk_ex_free(void *obj
, void *item
, CRYPTO_EX_DATA
*ad
,
113 int idx
, long argl
, void *argp
);
115 # ifndef OPENSSL_NO_RSA
116 /* This function is aliased to mod_exp (with the mont stuff dropped). */
117 static int surewarehk_mod_exp_mont(BIGNUM
*r
, const BIGNUM
*a
,
118 const BIGNUM
*p
, const BIGNUM
*m
,
119 BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
)
121 return surewarehk_modexp(r
, a
, p
, m
, ctx
);
124 /* Our internal RSA_METHOD that we provide pointers to */
125 static RSA_METHOD surewarehk_rsa
= {
126 "SureWare RSA method",
129 surewarehk_rsa_sign
, /* our rsa_sign is OpenSSL priv_enc */
130 surewarehk_rsa_priv_dec
, /* priv_dec */
132 surewarehk_mod_exp_mont
, /* mod_exp_mongomery */
137 NULL
, /* OpenSSL sign */
138 NULL
, /* OpenSSL verify */
143 # ifndef OPENSSL_NO_DH
144 /* Our internal DH_METHOD that we provide pointers to */
145 /* This function is aliased to mod_exp (with the dh and mont dropped). */
146 static int surewarehk_modexp_dh(const DH
*dh
, BIGNUM
*r
, const BIGNUM
*a
,
147 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
150 return surewarehk_modexp(r
, a
, p
, m
, ctx
);
153 static DH_METHOD surewarehk_dh
= {
154 "SureWare DH method",
157 surewarehk_modexp_dh
, /* dh mod exp */
166 static RAND_METHOD surewarehk_rand
= {
167 /* "SureWare RAND method", */
168 surewarehk_rand_seed
,
169 surewarehk_rand_bytes
,
172 surewarehk_rand_bytes
,
173 NULL
, /* rand_status */
176 # ifndef OPENSSL_NO_DSA
178 static DSA_SIG
*surewarehk_dsa_do_sign(const unsigned char *dgst
, int dlen
,
180 static int surewarehk_dsa_mod_exp(DSA
*dsa
, BIGNUM
*rr
, BIGNUM
*a1
,
181 BIGNUM
*p1
, BIGNUM
*a2
, BIGNUM
*p2
,
182 BIGNUM
*m
, BN_CTX
*ctx
,
183 BN_MONT_CTX
*in_mont
)
188 /* let rr = a1 ^ p1 mod m */
189 if (!surewarehk_modexp(rr
, a1
, p1
, m
, ctx
))
191 /* let t = a2 ^ p2 mod m */
192 if (!surewarehk_modexp(&t
, a2
, p2
, m
, ctx
))
194 /* let rr = rr * t mod m */
195 if (!BN_mod_mul(rr
, rr
, &t
, m
, ctx
))
203 static DSA_METHOD surewarehk_dsa
= {
204 "SureWare DSA method",
205 surewarehk_dsa_do_sign
,
206 NULL
, /* sign setup */
208 surewarehk_dsa_mod_exp
, /* mod exp */
209 NULL
, /* bn mod exp */
219 static const char *engine_sureware_id
= "sureware";
220 static const char *engine_sureware_name
= "SureWare hardware engine support";
222 /* Now, to our own code */
225 * As this is only ever called once, there's no need for locking (indeed -
226 * the lock will already be held by our caller!!!)
228 static int bind_sureware(ENGINE
*e
)
230 # ifndef OPENSSL_NO_RSA
231 const RSA_METHOD
*meth1
;
233 # ifndef OPENSSL_NO_DSA
234 const DSA_METHOD
*meth2
;
236 # ifndef OPENSSL_NO_DH
237 const DH_METHOD
*meth3
;
240 if (!ENGINE_set_id(e
, engine_sureware_id
) ||
241 !ENGINE_set_name(e
, engine_sureware_name
) ||
242 # ifndef OPENSSL_NO_RSA
243 !ENGINE_set_RSA(e
, &surewarehk_rsa
) ||
245 # ifndef OPENSSL_NO_DSA
246 !ENGINE_set_DSA(e
, &surewarehk_dsa
) ||
248 # ifndef OPENSSL_NO_DH
249 !ENGINE_set_DH(e
, &surewarehk_dh
) ||
251 !ENGINE_set_RAND(e
, &surewarehk_rand
) ||
252 !ENGINE_set_destroy_function(e
, surewarehk_destroy
) ||
253 !ENGINE_set_init_function(e
, surewarehk_init
) ||
254 !ENGINE_set_finish_function(e
, surewarehk_finish
) ||
255 !ENGINE_set_ctrl_function(e
, surewarehk_ctrl
) ||
256 !ENGINE_set_load_privkey_function(e
, surewarehk_load_privkey
) ||
257 !ENGINE_set_load_pubkey_function(e
, surewarehk_load_pubkey
))
260 # ifndef OPENSSL_NO_RSA
262 * We know that the "PKCS1_SSLeay()" functions hook properly to the
263 * cswift-specific mod_exp and mod_exp_crt so we use those functions. NB:
264 * We don't use ENGINE_openssl() or anything "more generic" because
265 * something like the RSAref code may not hook properly, and if you own
266 * one of these cards then you have the right to do RSA operations on it
269 meth1
= RSA_PKCS1_SSLeay();
271 surewarehk_rsa
.rsa_pub_enc
= meth1
->rsa_pub_enc
;
272 surewarehk_rsa
.rsa_pub_dec
= meth1
->rsa_pub_dec
;
276 # ifndef OPENSSL_NO_DSA
278 * Use the DSA_OpenSSL() method and just hook the mod_exp-ish bits.
280 meth2
= DSA_OpenSSL();
282 surewarehk_dsa
.dsa_do_verify
= meth2
->dsa_do_verify
;
286 # ifndef OPENSSL_NO_DH
287 /* Much the same for Diffie-Hellman */
288 meth3
= DH_OpenSSL();
290 surewarehk_dh
.generate_key
= meth3
->generate_key
;
291 surewarehk_dh
.compute_key
= meth3
->compute_key
;
295 /* Ensure the sureware error handling is set up */
296 ERR_load_SUREWARE_strings();
300 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
301 static int bind_helper(ENGINE
*e
, const char *id
)
303 if (id
&& (strcmp(id
, engine_sureware_id
) != 0))
305 if (!bind_sureware(e
))
310 IMPLEMENT_DYNAMIC_CHECK_FN()
311 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper
)
313 static ENGINE
*engine_sureware(void)
315 ENGINE
*ret
= ENGINE_new();
318 if (!bind_sureware(ret
)) {
325 void ENGINE_load_sureware(void)
327 /* Copied from eng_[openssl|dyn].c */
328 ENGINE
*toadd
= engine_sureware();
338 * This is a process-global DSO handle used for loading and unloading the
339 * SureWareHook library. NB: This is only set (or unset) during an init() or
340 * finish() call (reference counts permitting) and they're operating with
341 * global locks, so this should be thread-safe implicitly.
343 static DSO
*surewarehk_dso
= NULL
;
344 # ifndef OPENSSL_NO_RSA
345 /* Index for KM handle. Not really used yet. */
346 static int rsaHndidx
= -1;
348 # ifndef OPENSSL_NO_DSA
349 /* Index for KM handle. Not really used yet. */
350 static int dsaHndidx
= -1;
354 * These are the function pointers that are (un)set when the library has
355 * successfully (un)loaded.
357 static SureWareHook_Init_t
*p_surewarehk_Init
= NULL
;
358 static SureWareHook_Finish_t
*p_surewarehk_Finish
= NULL
;
359 static SureWareHook_Rand_Bytes_t
*p_surewarehk_Rand_Bytes
= NULL
;
360 static SureWareHook_Rand_Seed_t
*p_surewarehk_Rand_Seed
= NULL
;
361 static SureWareHook_Load_Privkey_t
*p_surewarehk_Load_Privkey
= NULL
;
362 static SureWareHook_Info_Pubkey_t
*p_surewarehk_Info_Pubkey
= NULL
;
363 static SureWareHook_Load_Rsa_Pubkey_t
*p_surewarehk_Load_Rsa_Pubkey
= NULL
;
364 static SureWareHook_Load_Dsa_Pubkey_t
*p_surewarehk_Load_Dsa_Pubkey
= NULL
;
365 static SureWareHook_Free_t
*p_surewarehk_Free
= NULL
;
366 static SureWareHook_Rsa_Priv_Dec_t
*p_surewarehk_Rsa_Priv_Dec
= NULL
;
367 static SureWareHook_Rsa_Sign_t
*p_surewarehk_Rsa_Sign
= NULL
;
368 static SureWareHook_Dsa_Sign_t
*p_surewarehk_Dsa_Sign
= NULL
;
369 static SureWareHook_Mod_Exp_t
*p_surewarehk_Mod_Exp
= NULL
;
371 /* Used in the DSO operations. */
372 static const char *surewarehk_LIBNAME
= "SureWareHook";
373 static const char *n_surewarehk_Init
= "SureWareHook_Init";
374 static const char *n_surewarehk_Finish
= "SureWareHook_Finish";
375 static const char *n_surewarehk_Rand_Bytes
= "SureWareHook_Rand_Bytes";
376 static const char *n_surewarehk_Rand_Seed
= "SureWareHook_Rand_Seed";
377 static const char *n_surewarehk_Load_Privkey
= "SureWareHook_Load_Privkey";
378 static const char *n_surewarehk_Info_Pubkey
= "SureWareHook_Info_Pubkey";
379 static const char *n_surewarehk_Load_Rsa_Pubkey
=
380 "SureWareHook_Load_Rsa_Pubkey";
381 static const char *n_surewarehk_Load_Dsa_Pubkey
=
382 "SureWareHook_Load_Dsa_Pubkey";
383 static const char *n_surewarehk_Free
= "SureWareHook_Free";
384 static const char *n_surewarehk_Rsa_Priv_Dec
= "SureWareHook_Rsa_Priv_Dec";
385 static const char *n_surewarehk_Rsa_Sign
= "SureWareHook_Rsa_Sign";
386 static const char *n_surewarehk_Dsa_Sign
= "SureWareHook_Dsa_Sign";
387 static const char *n_surewarehk_Mod_Exp
= "SureWareHook_Mod_Exp";
388 static BIO
*logstream
= NULL
;
391 * SureWareHook library functions and mechanics - these are used by the
392 * higher-level functions further down. NB: As and where there's no error
393 * checking, take a look lower down where these functions are called, the
394 * checking and error handling is probably down there.
396 static int threadsafe
= 1;
397 static int surewarehk_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
,
403 case ENGINE_CTRL_SET_LOGSTREAM
:
406 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
409 if (CRYPTO_add(&bio
->references
, 1, CRYPTO_LOCK_BIO
) > 1)
412 SUREWAREerr(SUREWARE_F_SUREWAREHK_CTRL
,
413 SUREWARE_R_BIO_WAS_FREED
);
415 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
418 * This will prevent the initialisation function from "installing"
419 * the mutex-handling callbacks, even if they are available from
420 * within the library (or were provided to the library from the
421 * calling application). This is to remove any baggage for
422 * applications not using multithreading.
424 case ENGINE_CTRL_CHIL_NO_LOCKING
:
425 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
427 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
430 /* The command isn't understood by this engine */
432 SUREWAREerr(SUREWARE_F_SUREWAREHK_CTRL
,
433 ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED
);
441 /* Destructor (complements the "ENGINE_surewarehk()" constructor) */
442 static int surewarehk_destroy(ENGINE
*e
)
444 ERR_unload_SUREWARE_strings();
448 /* (de)initialisation functions. */
449 static int surewarehk_init(ENGINE
*e
)
451 char msg
[64] = "ENGINE_init";
452 SureWareHook_Init_t
*p1
= NULL
;
453 SureWareHook_Finish_t
*p2
= NULL
;
454 SureWareHook_Rand_Bytes_t
*p3
= NULL
;
455 SureWareHook_Rand_Seed_t
*p4
= NULL
;
456 SureWareHook_Load_Privkey_t
*p5
= NULL
;
457 SureWareHook_Load_Rsa_Pubkey_t
*p6
= NULL
;
458 SureWareHook_Free_t
*p7
= NULL
;
459 SureWareHook_Rsa_Priv_Dec_t
*p8
= NULL
;
460 SureWareHook_Rsa_Sign_t
*p9
= NULL
;
461 SureWareHook_Dsa_Sign_t
*p12
= NULL
;
462 SureWareHook_Info_Pubkey_t
*p13
= NULL
;
463 SureWareHook_Load_Dsa_Pubkey_t
*p14
= NULL
;
464 SureWareHook_Mod_Exp_t
*p15
= NULL
;
466 if (surewarehk_dso
!= NULL
) {
467 SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT
, ENGINE_R_ALREADY_LOADED
);
470 /* Attempt to load libsurewarehk.so/surewarehk.dll/whatever. */
471 surewarehk_dso
= DSO_load(NULL
, surewarehk_LIBNAME
, NULL
, 0);
472 if (surewarehk_dso
== NULL
) {
473 SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT
, ENGINE_R_DSO_FAILURE
);
478 (SureWareHook_Init_t
*) DSO_bind_func(surewarehk_dso
,
481 (SureWareHook_Finish_t
*) DSO_bind_func(surewarehk_dso
,
482 n_surewarehk_Finish
))
484 (SureWareHook_Rand_Bytes_t
*) DSO_bind_func(surewarehk_dso
,
485 n_surewarehk_Rand_Bytes
))
487 (SureWareHook_Rand_Seed_t
*) DSO_bind_func(surewarehk_dso
,
488 n_surewarehk_Rand_Seed
))
490 (SureWareHook_Load_Privkey_t
*) DSO_bind_func(surewarehk_dso
,
491 n_surewarehk_Load_Privkey
))
493 (SureWareHook_Load_Rsa_Pubkey_t
*) DSO_bind_func(surewarehk_dso
,
494 n_surewarehk_Load_Rsa_Pubkey
))
496 (SureWareHook_Free_t
*) DSO_bind_func(surewarehk_dso
, n_surewarehk_Free
))
498 (SureWareHook_Rsa_Priv_Dec_t
*) DSO_bind_func(surewarehk_dso
,
499 n_surewarehk_Rsa_Priv_Dec
))
501 (SureWareHook_Rsa_Sign_t
*) DSO_bind_func(surewarehk_dso
,
502 n_surewarehk_Rsa_Sign
))
504 (SureWareHook_Dsa_Sign_t
*) DSO_bind_func(surewarehk_dso
,
505 n_surewarehk_Dsa_Sign
))
507 (SureWareHook_Info_Pubkey_t
*) DSO_bind_func(surewarehk_dso
,
508 n_surewarehk_Info_Pubkey
))
510 (SureWareHook_Load_Dsa_Pubkey_t
*) DSO_bind_func(surewarehk_dso
,
511 n_surewarehk_Load_Dsa_Pubkey
))
513 (SureWareHook_Mod_Exp_t
*) DSO_bind_func(surewarehk_dso
,
514 n_surewarehk_Mod_Exp
))) {
515 SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT
, ENGINE_R_DSO_FAILURE
);
518 /* Copy the pointers */
519 p_surewarehk_Init
= p1
;
520 p_surewarehk_Finish
= p2
;
521 p_surewarehk_Rand_Bytes
= p3
;
522 p_surewarehk_Rand_Seed
= p4
;
523 p_surewarehk_Load_Privkey
= p5
;
524 p_surewarehk_Load_Rsa_Pubkey
= p6
;
525 p_surewarehk_Free
= p7
;
526 p_surewarehk_Rsa_Priv_Dec
= p8
;
527 p_surewarehk_Rsa_Sign
= p9
;
528 p_surewarehk_Dsa_Sign
= p12
;
529 p_surewarehk_Info_Pubkey
= p13
;
530 p_surewarehk_Load_Dsa_Pubkey
= p14
;
531 p_surewarehk_Mod_Exp
= p15
;
532 /* Contact the hardware and initialises it. */
533 if (p_surewarehk_Init(msg
, threadsafe
) == SUREWAREHOOK_ERROR_UNIT_FAILURE
) {
534 SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT
, SUREWARE_R_UNIT_FAILURE
);
537 if (p_surewarehk_Init(msg
, threadsafe
) == SUREWAREHOOK_ERROR_UNIT_FAILURE
) {
538 SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT
, SUREWARE_R_UNIT_FAILURE
);
542 * try to load the default private key, if failed does not return a
543 * failure but wait for an explicit ENGINE_load_privakey
545 surewarehk_load_privkey(e
, NULL
, NULL
, NULL
);
547 /* Everything's fine. */
548 # ifndef OPENSSL_NO_RSA
550 rsaHndidx
= RSA_get_ex_new_index(0,
551 "SureWareHook RSA key handle",
552 NULL
, NULL
, surewarehk_ex_free
);
554 # ifndef OPENSSL_NO_DSA
556 dsaHndidx
= DSA_get_ex_new_index(0,
557 "SureWareHook DSA key handle",
558 NULL
, NULL
, surewarehk_ex_free
);
563 DSO_free(surewarehk_dso
);
564 surewarehk_dso
= NULL
;
565 p_surewarehk_Init
= NULL
;
566 p_surewarehk_Finish
= NULL
;
567 p_surewarehk_Rand_Bytes
= NULL
;
568 p_surewarehk_Rand_Seed
= NULL
;
569 p_surewarehk_Load_Privkey
= NULL
;
570 p_surewarehk_Load_Rsa_Pubkey
= NULL
;
571 p_surewarehk_Free
= NULL
;
572 p_surewarehk_Rsa_Priv_Dec
= NULL
;
573 p_surewarehk_Rsa_Sign
= NULL
;
574 p_surewarehk_Dsa_Sign
= NULL
;
575 p_surewarehk_Info_Pubkey
= NULL
;
576 p_surewarehk_Load_Dsa_Pubkey
= NULL
;
577 p_surewarehk_Mod_Exp
= NULL
;
581 static int surewarehk_finish(ENGINE
*e
)
584 if (surewarehk_dso
== NULL
) {
585 SUREWAREerr(SUREWARE_F_SUREWAREHK_FINISH
, ENGINE_R_NOT_LOADED
);
589 p_surewarehk_Finish();
590 if (!DSO_free(surewarehk_dso
)) {
591 SUREWAREerr(SUREWARE_F_SUREWAREHK_FINISH
, ENGINE_R_DSO_FAILURE
);
597 surewarehk_dso
= NULL
;
598 p_surewarehk_Init
= NULL
;
599 p_surewarehk_Finish
= NULL
;
600 p_surewarehk_Rand_Bytes
= NULL
;
601 p_surewarehk_Rand_Seed
= NULL
;
602 p_surewarehk_Load_Privkey
= NULL
;
603 p_surewarehk_Load_Rsa_Pubkey
= NULL
;
604 p_surewarehk_Free
= NULL
;
605 p_surewarehk_Rsa_Priv_Dec
= NULL
;
606 p_surewarehk_Rsa_Sign
= NULL
;
607 p_surewarehk_Dsa_Sign
= NULL
;
608 p_surewarehk_Info_Pubkey
= NULL
;
609 p_surewarehk_Load_Dsa_Pubkey
= NULL
;
610 p_surewarehk_Mod_Exp
= NULL
;
614 static void surewarehk_error_handling(char *const msg
, int func
, int ret
)
617 case SUREWAREHOOK_ERROR_UNIT_FAILURE
:
618 ENGINEerr(func
, SUREWARE_R_UNIT_FAILURE
);
620 case SUREWAREHOOK_ERROR_FALLBACK
:
621 ENGINEerr(func
, SUREWARE_R_REQUEST_FALLBACK
);
623 case SUREWAREHOOK_ERROR_DATA_SIZE
:
624 ENGINEerr(func
, SUREWARE_R_SIZE_TOO_LARGE_OR_TOO_SMALL
);
626 case SUREWAREHOOK_ERROR_INVALID_PAD
:
627 ENGINEerr(func
, SUREWARE_R_PADDING_CHECK_FAILED
);
630 ENGINEerr(func
, SUREWARE_R_REQUEST_FAILED
);
632 case 1: /* nothing */
636 ERR_add_error_data(1, msg
);
638 CRYPTO_w_lock(CRYPTO_LOCK_BIO
);
639 BIO_write(logstream
, msg
, strlen(msg
));
640 CRYPTO_w_unlock(CRYPTO_LOCK_BIO
);
645 static int surewarehk_rand_bytes(unsigned char *buf
, int num
)
648 char msg
[64] = "ENGINE_rand_bytes";
649 if (!p_surewarehk_Rand_Bytes
) {
650 SUREWAREerr(SUREWARE_F_SUREWAREHK_RAND_BYTES
,
651 ENGINE_R_NOT_INITIALISED
);
653 ret
= p_surewarehk_Rand_Bytes(msg
, buf
, num
);
654 surewarehk_error_handling(msg
, SUREWARE_F_SUREWAREHK_RAND_BYTES
, ret
);
656 return ret
== 1 ? 1 : 0;
659 static int surewarehk_rand_seed(const void *buf
, int num
)
662 char msg
[64] = "ENGINE_rand_seed";
663 if (!p_surewarehk_Rand_Seed
) {
664 SUREWAREerr(SUREWARE_F_SUREWAREHK_RAND_SEED
,
665 ENGINE_R_NOT_INITIALISED
);
668 ret
= p_surewarehk_Rand_Seed(msg
, buf
, num
);
669 surewarehk_error_handling(msg
, SUREWARE_F_SUREWAREHK_RAND_SEED
, ret
);
677 static int surewarehk_rand_add(const void *buf
, int num
, double entropy
)
679 return surewarehk_rand_seed(buf
, num
);
682 static EVP_PKEY
*sureware_load_public(ENGINE
*e
, const char *key_id
,
683 char *hptr
, unsigned long el
,
686 EVP_PKEY
*res
= NULL
;
687 # ifndef OPENSSL_NO_RSA
690 # ifndef OPENSSL_NO_DSA
693 char msg
[64] = "sureware_load_public";
695 if (!p_surewarehk_Load_Rsa_Pubkey
|| !p_surewarehk_Load_Dsa_Pubkey
) {
696 SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC
,
697 ENGINE_R_NOT_INITIALISED
);
701 # ifndef OPENSSL_NO_RSA
704 /* set private external reference */
705 rsatmp
= RSA_new_method(e
);
706 RSA_set_ex_data(rsatmp
, rsaHndidx
, hptr
);
707 rsatmp
->flags
|= RSA_FLAG_EXT_PKEY
;
709 /* set public big nums */
710 rsatmp
->e
= BN_new();
711 rsatmp
->n
= BN_new();
712 if (!rsatmp
->e
|| !rsatmp
->n
)
714 bn_expand2(rsatmp
->e
, el
/ sizeof(BN_ULONG
));
715 bn_expand2(rsatmp
->n
, el
/ sizeof(BN_ULONG
));
716 if (rsatmp
->e
->dmax
!= (int)(el
/ sizeof(BN_ULONG
)) ||
717 rsatmp
->n
->dmax
!= (int)(el
/ sizeof(BN_ULONG
)))
719 ret
= p_surewarehk_Load_Rsa_Pubkey(msg
, key_id
, el
,
720 (unsigned long *)rsatmp
->n
->d
,
721 (unsigned long *)rsatmp
->e
->d
);
722 surewarehk_error_handling(msg
, SUREWARE_F_SUREWARE_LOAD_PUBLIC
, ret
);
724 SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC
,
725 ENGINE_R_FAILED_LOADING_PUBLIC_KEY
);
728 /* normalise pub e and pub n */
729 rsatmp
->e
->top
= el
/ sizeof(BN_ULONG
);
730 bn_fix_top(rsatmp
->e
);
731 rsatmp
->n
->top
= el
/ sizeof(BN_ULONG
);
732 bn_fix_top(rsatmp
->n
);
733 /* create an EVP object: engine + rsa key */
734 res
= EVP_PKEY_new();
735 EVP_PKEY_assign_RSA(res
, rsatmp
);
739 # ifndef OPENSSL_NO_DSA
742 /* set private/public external reference */
743 dsatmp
= DSA_new_method(e
);
744 DSA_set_ex_data(dsatmp
, dsaHndidx
, hptr
);
746 * dsatmp->flags |= DSA_FLAG_EXT_PKEY;
750 dsatmp
->pub_key
= BN_new();
751 dsatmp
->p
= BN_new();
752 dsatmp
->q
= BN_new();
753 dsatmp
->g
= BN_new();
754 if (!dsatmp
->pub_key
|| !dsatmp
->p
|| !dsatmp
->q
|| !dsatmp
->g
)
756 bn_expand2(dsatmp
->pub_key
, el
/ sizeof(BN_ULONG
));
757 bn_expand2(dsatmp
->p
, el
/ sizeof(BN_ULONG
));
758 bn_expand2(dsatmp
->q
, 20 / sizeof(BN_ULONG
));
759 bn_expand2(dsatmp
->g
, el
/ sizeof(BN_ULONG
));
760 if (dsatmp
->pub_key
->dmax
!= (int)(el
/ sizeof(BN_ULONG
))
761 || dsatmp
->p
->dmax
!= (int)(el
/ sizeof(BN_ULONG
))
762 || dsatmp
->q
->dmax
!= 20 / sizeof(BN_ULONG
)
763 || dsatmp
->g
->dmax
!= (int)(el
/ sizeof(BN_ULONG
)))
766 ret
= p_surewarehk_Load_Dsa_Pubkey(msg
, key_id
, el
,
767 (unsigned long *)dsatmp
->
769 (unsigned long *)dsatmp
->p
->d
,
770 (unsigned long *)dsatmp
->q
->d
,
771 (unsigned long *)dsatmp
->g
->d
);
772 surewarehk_error_handling(msg
, SUREWARE_F_SUREWARE_LOAD_PUBLIC
, ret
);
774 SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC
,
775 ENGINE_R_FAILED_LOADING_PUBLIC_KEY
);
779 /* normalise pubkey and parameters in case of */
780 dsatmp
->pub_key
->top
= el
/ sizeof(BN_ULONG
);
781 bn_fix_top(dsatmp
->pub_key
);
782 dsatmp
->p
->top
= el
/ sizeof(BN_ULONG
);
783 bn_fix_top(dsatmp
->p
);
784 dsatmp
->q
->top
= 20 / sizeof(BN_ULONG
);
785 bn_fix_top(dsatmp
->q
);
786 dsatmp
->g
->top
= el
/ sizeof(BN_ULONG
);
787 bn_fix_top(dsatmp
->g
);
789 /* create an EVP object: engine + rsa key */
790 res
= EVP_PKEY_new();
791 EVP_PKEY_assign_DSA(res
, dsatmp
);
796 SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC
,
797 ENGINE_R_FAILED_LOADING_PRIVATE_KEY
);
802 # ifndef OPENSSL_NO_RSA
805 # ifndef OPENSSL_NO_DSA
811 static EVP_PKEY
*surewarehk_load_privkey(ENGINE
*e
, const char *key_id
,
812 UI_METHOD
*ui_method
,
815 EVP_PKEY
*res
= NULL
;
817 unsigned long el
= 0;
820 char msg
[64] = "ENGINE_load_privkey";
822 if (!p_surewarehk_Load_Privkey
) {
823 SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PRIVKEY
,
824 ENGINE_R_NOT_INITIALISED
);
826 ret
= p_surewarehk_Load_Privkey(msg
, key_id
, &hptr
, &el
, &keytype
);
828 SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PRIVKEY
,
829 ENGINE_R_FAILED_LOADING_PRIVATE_KEY
);
830 ERR_add_error_data(1, msg
);
832 res
= sureware_load_public(e
, key_id
, hptr
, el
, keytype
);
837 static EVP_PKEY
*surewarehk_load_pubkey(ENGINE
*e
, const char *key_id
,
838 UI_METHOD
*ui_method
,
841 EVP_PKEY
*res
= NULL
;
843 unsigned long el
= 0;
846 char msg
[64] = "ENGINE_load_pubkey";
848 if (!p_surewarehk_Info_Pubkey
) {
849 SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PUBKEY
,
850 ENGINE_R_NOT_INITIALISED
);
852 /* call once to identify if DSA or RSA */
853 ret
= p_surewarehk_Info_Pubkey(msg
, key_id
, &el
, &keytype
);
855 SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PUBKEY
,
856 ENGINE_R_FAILED_LOADING_PUBLIC_KEY
);
857 ERR_add_error_data(1, msg
);
859 res
= sureware_load_public(e
, key_id
, hptr
, el
, keytype
);
865 * This cleans up an RSA/DSA KM key(do not destroy the key into the hardware)
866 * , called when ex_data is freed
868 static void surewarehk_ex_free(void *obj
, void *item
, CRYPTO_EX_DATA
*ad
,
869 int idx
, long argl
, void *argp
)
871 if (!p_surewarehk_Free
) {
872 SUREWAREerr(SUREWARE_F_SUREWAREHK_EX_FREE
, ENGINE_R_NOT_INITIALISED
);
874 p_surewarehk_Free((char *)item
, 0);
878 * return number of decrypted bytes
880 # ifndef OPENSSL_NO_RSA
881 static int surewarehk_rsa_priv_dec(int flen
, const unsigned char *from
,
882 unsigned char *to
, RSA
*rsa
, int padding
)
885 char *buf
= NULL
, *hptr
= NULL
;
886 char msg
[64] = "ENGINE_rsa_priv_dec";
887 if (!p_surewarehk_Rsa_Priv_Dec
) {
888 SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC
,
889 ENGINE_R_NOT_INITIALISED
);
891 /* extract ref to private key */
892 else if ((hptr
= RSA_get_ex_data(rsa
, rsaHndidx
)) == NULL
) {
893 SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC
,
894 SUREWARE_R_MISSING_KEY_COMPONENTS
);
897 /* analyse what padding we can do into the hardware */
898 if (padding
== RSA_PKCS1_PADDING
) {
901 p_surewarehk_Rsa_Priv_Dec(msg
, flen
, (unsigned char *)from
, &tlen
,
902 to
, hptr
, SUREWARE_PKCS1_PAD
);
903 surewarehk_error_handling(msg
, SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC
,
908 } else { /* do with no padding into hardware */
911 p_surewarehk_Rsa_Priv_Dec(msg
, flen
, (unsigned char *)from
, &tlen
,
912 to
, hptr
, SUREWARE_NO_PAD
);
913 surewarehk_error_handling(msg
, SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC
,
917 /* intermediate buffer for padding */
918 if ((buf
= OPENSSL_malloc(tlen
)) == NULL
) {
919 SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC
,
920 ERR_R_MALLOC_FAILURE
);
923 memcpy(buf
, to
, tlen
); /* transfert to into buf */
924 switch (padding
) { /* check padding in software */
925 case RSA_PKCS1_OAEP_PADDING
:
927 RSA_padding_check_PKCS1_OAEP(to
, tlen
, (unsigned char *)buf
,
928 tlen
, tlen
, NULL
, 0);
930 case RSA_SSLV23_PADDING
:
932 RSA_padding_check_SSLv23(to
, tlen
, (unsigned char *)buf
, flen
,
937 RSA_padding_check_none(to
, tlen
, (unsigned char *)buf
, flen
,
941 SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC
,
942 SUREWARE_R_UNKNOWN_PADDING_TYPE
);
946 SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC
,
947 SUREWARE_R_PADDING_CHECK_FAILED
);
950 OPENSSL_clear_free(buf
, tlen
);
955 * Does what OpenSSL rsa_priv_enc does.
957 static int surewarehk_rsa_sign(int flen
, const unsigned char *from
,
958 unsigned char *to
, RSA
*rsa
, int padding
)
962 char msg
[64] = "ENGINE_rsa_sign";
963 if (!p_surewarehk_Rsa_Sign
) {
964 SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN
, ENGINE_R_NOT_INITIALISED
);
966 /* extract ref to private key */
967 else if ((hptr
= RSA_get_ex_data(rsa
, rsaHndidx
)) == NULL
) {
968 SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN
,
969 SUREWARE_R_MISSING_KEY_COMPONENTS
);
972 case RSA_PKCS1_PADDING
: /* do it in one shot */
974 p_surewarehk_Rsa_Sign(msg
, flen
, (unsigned char *)from
, &tlen
,
975 to
, hptr
, SUREWARE_PKCS1_PAD
);
976 surewarehk_error_handling(msg
, SUREWARE_F_SUREWAREHK_RSA_SIGN
,
981 SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN
,
982 SUREWARE_R_UNKNOWN_PADDING_TYPE
);
985 return ret
== 1 ? tlen
: ret
;
990 # ifndef OPENSSL_NO_DSA
991 /* DSA sign and verify */
992 static DSA_SIG
*surewarehk_dsa_do_sign(const unsigned char *from
, int flen
,
997 DSA_SIG
*psign
= NULL
;
998 char msg
[64] = "ENGINE_dsa_do_sign";
999 if (!p_surewarehk_Dsa_Sign
) {
1000 SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN
,
1001 ENGINE_R_NOT_INITIALISED
);
1004 /* extract ref to private key */
1005 else if ((hptr
= DSA_get_ex_data(dsa
, dsaHndidx
)) == NULL
) {
1006 SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN
,
1007 SUREWARE_R_MISSING_KEY_COMPONENTS
);
1010 if ((psign
= DSA_SIG_new()) == NULL
) {
1011 SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN
,
1012 ERR_R_MALLOC_FAILURE
);
1015 psign
->r
= BN_new();
1016 psign
->s
= BN_new();
1017 if (!psign
->r
|| !psign
->s
)
1019 bn_expand2(psign
->r
, 20 / sizeof(BN_ULONG
));
1020 bn_expand2(psign
->s
, 20 / sizeof(BN_ULONG
));
1021 if (psign
->r
->dmax
!= 20 / sizeof(BN_ULONG
) ||
1022 psign
->s
->dmax
!= 20 / sizeof(BN_ULONG
))
1024 ret
= p_surewarehk_Dsa_Sign(msg
, flen
, from
,
1025 (unsigned long *)psign
->r
->d
,
1026 (unsigned long *)psign
->s
->d
, hptr
);
1027 surewarehk_error_handling(msg
, SUREWARE_F_SUREWAREHK_DSA_DO_SIGN
,
1030 psign
->r
->top
= 20 / sizeof(BN_ULONG
);
1031 bn_fix_top(psign
->r
);
1032 psign
->s
->top
= 20 / sizeof(BN_ULONG
);
1033 bn_fix_top(psign
->s
);
1037 DSA_SIG_free(psign
);
1044 static int surewarehk_modexp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
1045 const BIGNUM
*m
, BN_CTX
*ctx
)
1048 char msg
[64] = "ENGINE_modexp";
1049 if (!p_surewarehk_Mod_Exp
) {
1050 SUREWAREerr(SUREWARE_F_SUREWAREHK_MODEXP
, ENGINE_R_NOT_INITIALISED
);
1052 bn_expand2(r
, m
->top
);
1053 if (r
->dmax
== m
->top
) {
1055 ret
= p_surewarehk_Mod_Exp(msg
,
1056 m
->top
* sizeof(BN_ULONG
),
1057 (unsigned long *)m
->d
,
1058 p
->top
* sizeof(BN_ULONG
),
1059 (unsigned long *)p
->d
,
1060 a
->top
* sizeof(BN_ULONG
),
1061 (unsigned long *)a
->d
,
1062 (unsigned long *)r
->d
);
1063 surewarehk_error_handling(msg
, SUREWARE_F_SUREWAREHK_MODEXP
, ret
);
1065 /* normalise result */
1073 # endif /* !OPENSSL_NO_HW_SUREWARE */
1074 #endif /* !OPENSSL_NO_HW */