1 /* crypto/engine/e_chil.c -*- mode: C; c-file-style: "eay" -*- */
3 * Written by Richard Levitte (richard@levitte.org), Geoff Thorpe
4 * (geoff@geoffthorpe.net) and Dr Stephen N Henson (steve@openssl.org) for
5 * the OpenSSL project 2000.
7 /* ====================================================================
8 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
22 * 3. All advertising materials mentioning features or use of this
23 * software must display the following acknowledgment:
24 * "This product includes software developed by the OpenSSL Project
25 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
27 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28 * endorse or promote products derived from this software without
29 * prior written permission. For written permission, please contact
30 * licensing@OpenSSL.org.
32 * 5. Products derived from this software may not be called "OpenSSL"
33 * nor may "OpenSSL" appear in their names without prior written
34 * permission of the OpenSSL Project.
36 * 6. Redistributions of any form whatsoever must retain the following
38 * "This product includes software developed by the OpenSSL Project
39 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
41 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52 * OF THE POSSIBILITY OF SUCH DAMAGE.
53 * ====================================================================
55 * This product includes cryptographic software written by Eric Young
56 * (eay@cryptsoft.com). This product includes software written by Tim
57 * Hudson (tjh@cryptsoft.com).
63 #include <openssl/crypto.h>
64 #include <openssl/pem.h>
65 #include <openssl/dso.h>
66 #include <openssl/engine.h>
67 #include <openssl/ui.h>
68 #include <openssl/rand.h>
69 #ifndef OPENSSL_NO_RSA
70 # include <openssl/rsa.h>
73 # include <openssl/dh.h>
75 #include <openssl/bn.h>
78 # ifndef OPENSSL_NO_HW_CHIL
81 * Attribution notice: nCipher have said several times that it's OK for
82 * us to implement a general interface to their boxes, and recently declared
83 * their HWCryptoHook to be public, and therefore available for us to use.
86 * The hwcryptohook.h included here is from May 2000.
90 # include "hwcryptohook.h"
92 # include "vendor_defns/hwcryptohook.h"
95 # define HWCRHK_LIB_NAME "CHIL engine"
96 # include "e_chil_err.c"
98 static int hwcrhk_destroy(ENGINE
*e
);
99 static int hwcrhk_init(ENGINE
*e
);
100 static int hwcrhk_finish(ENGINE
*e
);
101 static int hwcrhk_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
) (void));
103 /* Functions to handle mutexes */
104 static int hwcrhk_mutex_init(HWCryptoHook_Mutex
*,
105 HWCryptoHook_CallerContext
*);
106 static int hwcrhk_mutex_lock(HWCryptoHook_Mutex
*);
107 static void hwcrhk_mutex_unlock(HWCryptoHook_Mutex
*);
108 static void hwcrhk_mutex_destroy(HWCryptoHook_Mutex
*);
111 static int hwcrhk_mod_exp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
112 const BIGNUM
*m
, BN_CTX
*ctx
);
114 # ifndef OPENSSL_NO_RSA
116 static int hwcrhk_rsa_mod_exp(BIGNUM
*r
, const BIGNUM
*I
, RSA
*rsa
,
118 /* This function is aliased to mod_exp (with the mont stuff dropped). */
119 static int hwcrhk_mod_exp_mont(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
120 const BIGNUM
*m
, BN_CTX
*ctx
,
122 static int hwcrhk_rsa_finish(RSA
*rsa
);
125 # ifndef OPENSSL_NO_DH
127 /* This function is alised to mod_exp (with the DH and mont dropped). */
128 static int hwcrhk_mod_exp_dh(const DH
*dh
, BIGNUM
*r
,
129 const BIGNUM
*a
, const BIGNUM
*p
,
130 const BIGNUM
*m
, BN_CTX
*ctx
,
135 static int hwcrhk_rand_bytes(unsigned char *buf
, int num
);
136 static int hwcrhk_rand_status(void);
139 static EVP_PKEY
*hwcrhk_load_privkey(ENGINE
*eng
, const char *key_id
,
140 UI_METHOD
*ui_method
,
141 void *callback_data
);
142 static EVP_PKEY
*hwcrhk_load_pubkey(ENGINE
*eng
, const char *key_id
,
143 UI_METHOD
*ui_method
,
144 void *callback_data
);
146 /* Interaction stuff */
147 static int hwcrhk_insert_card(const char *prompt_info
,
148 const char *wrong_info
,
149 HWCryptoHook_PassphraseContext
* ppctx
,
150 HWCryptoHook_CallerContext
* cactx
);
151 static int hwcrhk_get_pass(const char *prompt_info
,
152 int *len_io
, char *buf
,
153 HWCryptoHook_PassphraseContext
* ppctx
,
154 HWCryptoHook_CallerContext
* cactx
);
155 static void hwcrhk_log_message(void *logstr
, const char *message
);
157 /* The definitions for control commands specific to this engine */
158 # define HWCRHK_CMD_SO_PATH ENGINE_CMD_BASE
159 # define HWCRHK_CMD_FORK_CHECK (ENGINE_CMD_BASE + 1)
160 # define HWCRHK_CMD_THREAD_LOCKING (ENGINE_CMD_BASE + 2)
161 # define HWCRHK_CMD_SET_USER_INTERFACE (ENGINE_CMD_BASE + 3)
162 # define HWCRHK_CMD_SET_CALLBACK_DATA (ENGINE_CMD_BASE + 4)
163 static const ENGINE_CMD_DEFN hwcrhk_cmd_defns
[] = {
166 "Specifies the path to the 'hwcrhk' shared library",
167 ENGINE_CMD_FLAG_STRING
},
168 {HWCRHK_CMD_FORK_CHECK
,
170 "Turns fork() checking on (non-zero) or off (zero)",
171 ENGINE_CMD_FLAG_NUMERIC
},
172 {HWCRHK_CMD_THREAD_LOCKING
,
174 "Turns thread-safe locking on (zero) or off (non-zero)",
175 ENGINE_CMD_FLAG_NUMERIC
},
176 {HWCRHK_CMD_SET_USER_INTERFACE
,
177 "SET_USER_INTERFACE",
178 "Set the global user interface (internal)",
179 ENGINE_CMD_FLAG_INTERNAL
},
180 {HWCRHK_CMD_SET_CALLBACK_DATA
,
182 "Set the global user interface extra data (internal)",
183 ENGINE_CMD_FLAG_INTERNAL
},
187 # ifndef OPENSSL_NO_RSA
188 /* Our internal RSA_METHOD that we provide pointers to */
189 static RSA_METHOD hwcrhk_rsa
= {
207 # ifndef OPENSSL_NO_DH
208 /* Our internal DH_METHOD that we provide pointers to */
209 static DH_METHOD hwcrhk_dh
= {
222 static RAND_METHOD hwcrhk_rand
= {
223 /* "CHIL RAND method", */
232 /* Constants used when creating the ENGINE */
233 static const char *engine_hwcrhk_id
= "chil";
234 static const char *engine_hwcrhk_name
= "CHIL hardware engine support";
235 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
236 /* Compatibility hack, the dynamic library uses this form in the path */
237 static const char *engine_hwcrhk_id_alt
= "ncipher";
240 /* Internal stuff for HWCryptoHook */
242 /* Some structures needed for proper use of thread locks */
244 * hwcryptohook.h has some typedefs that turn struct HWCryptoHook_MutexValue
245 * into HWCryptoHook_Mutex
247 struct HWCryptoHook_MutexValue
{
252 * hwcryptohook.h has some typedefs that turn struct
253 * HWCryptoHook_PassphraseContextValue into HWCryptoHook_PassphraseContext
255 struct HWCryptoHook_PassphraseContextValue
{
256 UI_METHOD
*ui_method
;
261 * hwcryptohook.h has some typedefs that turn struct
262 * HWCryptoHook_CallerContextValue into HWCryptoHook_CallerContext
264 struct HWCryptoHook_CallerContextValue
{
265 pem_password_cb
*password_callback
; /* Deprecated! Only present for
266 * backward compatibility! */
267 UI_METHOD
*ui_method
;
272 * The MPI structure in HWCryptoHook is pretty compatible with OpenSSL
273 * BIGNUM's, so lets define a couple of conversion macros
275 # define BN2MPI(mp, bn) \
276 {mp.size = bn->top * sizeof(BN_ULONG); mp.buf = (unsigned char *)bn->d;}
277 # define MPI2BN(bn, mp) \
278 {mp.size = bn->dmax * sizeof(BN_ULONG); mp.buf = (unsigned char *)bn->d;}
280 static BIO
*logstream
= NULL
;
281 static int disable_mutex_callbacks
= 0;
284 * One might wonder why these are needed, since one can pass down at least a
285 * UI_METHOD and a pointer to callback data to the key-loading functions. The
286 * thing is that the ModExp and RSAImmed functions can load keys as well, if
287 * the data they get is in a special, nCipher-defined format (hint: if you
288 * look at the private exponent of the RSA data as a string, you'll see this
289 * string: "nCipher KM tool key id", followed by some bytes, followed a key
290 * identity string, followed by more bytes. This happens when you use
291 * "embed" keys instead of "hwcrhk" keys). Unfortunately, those functions do
292 * not take any passphrase or caller context, and our functions can't really
293 * take any callback data either. Still, the "insert_card" and
294 * "get_passphrase" callbacks may be called down the line, and will need to
295 * know what user interface callbacks to call, and having callback data from
296 * the application may be a nice thing as well, so we need to keep track of
299 static HWCryptoHook_CallerContext password_context
= { NULL
, NULL
, NULL
};
301 /* Stuff to pass to the HWCryptoHook library */
302 static HWCryptoHook_InitInfo hwcrhk_globals
= {
303 HWCryptoHook_InitFlags_SimpleForkCheck
, /* Flags */
304 &logstream
, /* logstream */
305 sizeof(BN_ULONG
), /* limbsize */
306 0, /* mslimb first: false for BNs */
307 -1, /* msbyte first: use native */
308 0, /* Max mutexes, 0 = no small limit */
309 0, /* Max simultaneous, 0 = default */
312 * The next few are mutex stuff: we write wrapper functions around the OS
313 * mutex functions. We initialise them to 0 here, and change that to
314 * actual function pointers in hwcrhk_init() if dynamic locks are
315 * supported (that is, if the application programmer has made sure of
316 * setting up callbacks bafore starting this engine) *and* if
317 * disable_mutex_callbacks hasn't been set by a call to
318 * ENGINE_ctrl(ENGINE_CTRL_CHIL_NO_LOCKING).
320 sizeof(HWCryptoHook_Mutex
),
327 * The next few are condvar stuff: we write wrapper functions round the
328 * OS functions. Currently not implemented and not and absolute
329 * necessity even in threaded programs, therefore 0'ed. Will hopefully
330 * be implemented some day, since it enhances the efficiency of
333 0, /* sizeof(HWCryptoHook_CondVar), */
334 0, /* hwcrhk_cv_init, */
335 0, /* hwcrhk_cv_wait, */
336 0, /* hwcrhk_cv_signal, */
337 0, /* hwcrhk_cv_broadcast, */
338 0, /* hwcrhk_cv_destroy, */
340 hwcrhk_get_pass
, /* pass phrase */
341 hwcrhk_insert_card
, /* insert a card */
342 hwcrhk_log_message
/* Log message */
345 /* Now, to our own code */
348 * This internal function is used by ENGINE_chil() and possibly by the
349 * "dynamic" ENGINE support too
351 static int bind_helper(ENGINE
*e
)
353 # ifndef OPENSSL_NO_RSA
354 const RSA_METHOD
*meth1
;
356 # ifndef OPENSSL_NO_DH
357 const DH_METHOD
*meth2
;
359 if (!ENGINE_set_id(e
, engine_hwcrhk_id
) ||
360 !ENGINE_set_name(e
, engine_hwcrhk_name
) ||
361 # ifndef OPENSSL_NO_RSA
362 !ENGINE_set_RSA(e
, &hwcrhk_rsa
) ||
364 # ifndef OPENSSL_NO_DH
365 !ENGINE_set_DH(e
, &hwcrhk_dh
) ||
367 !ENGINE_set_RAND(e
, &hwcrhk_rand
) ||
368 !ENGINE_set_destroy_function(e
, hwcrhk_destroy
) ||
369 !ENGINE_set_init_function(e
, hwcrhk_init
) ||
370 !ENGINE_set_finish_function(e
, hwcrhk_finish
) ||
371 !ENGINE_set_ctrl_function(e
, hwcrhk_ctrl
) ||
372 !ENGINE_set_load_privkey_function(e
, hwcrhk_load_privkey
) ||
373 !ENGINE_set_load_pubkey_function(e
, hwcrhk_load_pubkey
) ||
374 !ENGINE_set_cmd_defns(e
, hwcrhk_cmd_defns
))
377 # ifndef OPENSSL_NO_RSA
379 * We know that the "PKCS1_OpenSSL()" functions hook properly to the
380 * cswift-specific mod_exp and mod_exp_crt so we use those functions. NB:
381 * We don't use ENGINE_openssl() or anything "more generic" because
382 * something like the RSAref code may not hook properly, and if you own
383 * one of these cards then you have the right to do RSA operations on it
386 meth1
= RSA_PKCS1_OpenSSL();
387 hwcrhk_rsa
.rsa_pub_enc
= meth1
->rsa_pub_enc
;
388 hwcrhk_rsa
.rsa_pub_dec
= meth1
->rsa_pub_dec
;
389 hwcrhk_rsa
.rsa_priv_enc
= meth1
->rsa_priv_enc
;
390 hwcrhk_rsa
.rsa_priv_dec
= meth1
->rsa_priv_dec
;
393 # ifndef OPENSSL_NO_DH
394 /* Much the same for Diffie-Hellman */
395 meth2
= DH_OpenSSL();
396 hwcrhk_dh
.generate_key
= meth2
->generate_key
;
397 hwcrhk_dh
.compute_key
= meth2
->compute_key
;
400 /* Ensure the hwcrhk error handling is set up */
401 ERR_load_HWCRHK_strings();
405 # ifdef OPENSSL_NO_DYNAMIC_ENGINE
406 static ENGINE
*engine_chil(void)
408 ENGINE
*ret
= ENGINE_new();
411 if (!bind_helper(ret
)) {
418 void ENGINE_load_chil(void)
420 /* Copied from eng_[openssl|dyn].c */
421 ENGINE
*toadd
= engine_chil();
431 * This is a process-global DSO handle used for loading and unloading the
432 * HWCryptoHook library. NB: This is only set (or unset) during an init() or
433 * finish() call (reference counts permitting) and they're operating with
434 * global locks, so this should be thread-safe implicitly.
436 static DSO
*hwcrhk_dso
= NULL
;
437 static HWCryptoHook_ContextHandle hwcrhk_context
= 0;
438 # ifndef OPENSSL_NO_RSA
439 /* Index for KM handle. Not really used yet. */
440 static int hndidx_rsa
= -1;
444 * These are the function pointers that are (un)set when the library has
445 * successfully (un)loaded.
447 static HWCryptoHook_Init_t
*p_hwcrhk_Init
= NULL
;
448 static HWCryptoHook_Finish_t
*p_hwcrhk_Finish
= NULL
;
449 static HWCryptoHook_ModExp_t
*p_hwcrhk_ModExp
= NULL
;
450 # ifndef OPENSSL_NO_RSA
451 static HWCryptoHook_RSA_t
*p_hwcrhk_RSA
= NULL
;
453 static HWCryptoHook_RandomBytes_t
*p_hwcrhk_RandomBytes
= NULL
;
454 # ifndef OPENSSL_NO_RSA
455 static HWCryptoHook_RSALoadKey_t
*p_hwcrhk_RSALoadKey
= NULL
;
456 static HWCryptoHook_RSAGetPublicKey_t
*p_hwcrhk_RSAGetPublicKey
= NULL
;
457 static HWCryptoHook_RSAUnloadKey_t
*p_hwcrhk_RSAUnloadKey
= NULL
;
459 static HWCryptoHook_ModExpCRT_t
*p_hwcrhk_ModExpCRT
= NULL
;
461 /* Used in the DSO operations. */
462 static const char *HWCRHK_LIBNAME
= NULL
;
463 static void free_HWCRHK_LIBNAME(void)
465 OPENSSL_free(HWCRHK_LIBNAME
);
466 HWCRHK_LIBNAME
= NULL
;
469 static const char *get_HWCRHK_LIBNAME(void)
472 return HWCRHK_LIBNAME
;
476 static long set_HWCRHK_LIBNAME(const char *name
)
478 free_HWCRHK_LIBNAME();
479 return (((HWCRHK_LIBNAME
= OPENSSL_strdup(name
)) != NULL
) ? 1 : 0);
482 static const char *n_hwcrhk_Init
= "HWCryptoHook_Init";
483 static const char *n_hwcrhk_Finish
= "HWCryptoHook_Finish";
484 static const char *n_hwcrhk_ModExp
= "HWCryptoHook_ModExp";
485 # ifndef OPENSSL_NO_RSA
486 static const char *n_hwcrhk_RSA
= "HWCryptoHook_RSA";
488 static const char *n_hwcrhk_RandomBytes
= "HWCryptoHook_RandomBytes";
489 # ifndef OPENSSL_NO_RSA
490 static const char *n_hwcrhk_RSALoadKey
= "HWCryptoHook_RSALoadKey";
491 static const char *n_hwcrhk_RSAGetPublicKey
= "HWCryptoHook_RSAGetPublicKey";
492 static const char *n_hwcrhk_RSAUnloadKey
= "HWCryptoHook_RSAUnloadKey";
494 static const char *n_hwcrhk_ModExpCRT
= "HWCryptoHook_ModExpCRT";
497 * HWCryptoHook library functions and mechanics - these are used by the
498 * higher-level functions further down. NB: As and where there's no error
499 * checking, take a look lower down where these functions are called, the
500 * checking and error handling is probably down there.
503 /* utility function to obtain a context */
504 static int get_context(HWCryptoHook_ContextHandle
* hac
,
505 HWCryptoHook_CallerContext
* cac
)
508 HWCryptoHook_ErrMsgBuf rmsg
;
511 rmsg
.size
= sizeof(tempbuf
);
513 *hac
= p_hwcrhk_Init(&hwcrhk_globals
, sizeof(hwcrhk_globals
), &rmsg
, cac
);
519 /* similarly to release one. */
520 static void release_context(HWCryptoHook_ContextHandle hac
)
522 p_hwcrhk_Finish(hac
);
525 /* Destructor (complements the "ENGINE_chil()" constructor) */
526 static int hwcrhk_destroy(ENGINE
*e
)
528 free_HWCRHK_LIBNAME();
529 ERR_unload_HWCRHK_strings();
533 /* (de)initialisation functions. */
534 static int hwcrhk_init(ENGINE
*e
)
536 HWCryptoHook_Init_t
*p1
;
537 HWCryptoHook_Finish_t
*p2
;
538 HWCryptoHook_ModExp_t
*p3
;
539 # ifndef OPENSSL_NO_RSA
540 HWCryptoHook_RSA_t
*p4
;
541 HWCryptoHook_RSALoadKey_t
*p5
;
542 HWCryptoHook_RSAGetPublicKey_t
*p6
;
543 HWCryptoHook_RSAUnloadKey_t
*p7
;
545 HWCryptoHook_RandomBytes_t
*p8
;
546 HWCryptoHook_ModExpCRT_t
*p9
;
548 if (hwcrhk_dso
!= NULL
) {
549 HWCRHKerr(HWCRHK_F_HWCRHK_INIT
, HWCRHK_R_ALREADY_LOADED
);
552 /* Attempt to load libnfhwcrhk.so/nfhwcrhk.dll/whatever. */
553 hwcrhk_dso
= DSO_load(NULL
, get_HWCRHK_LIBNAME(), NULL
, 0);
554 if (hwcrhk_dso
== NULL
) {
555 HWCRHKerr(HWCRHK_F_HWCRHK_INIT
, HWCRHK_R_DSO_FAILURE
);
559 #define BINDIT(t, name) (t *)DSO_bind_func(hwcrhk_dso, name)
560 if ((p1
= BINDIT(HWCryptoHook_Init_t
, n_hwcrhk_Init
)) == NULL
561 || (p2
= BINDIT(HWCryptoHook_Finish_t
, n_hwcrhk_Finish
)) == NULL
562 || (p3
= BINDIT(HWCryptoHook_ModExp_t
, n_hwcrhk_ModExp
)) == NULL
563 # ifndef OPENSSL_NO_RSA
564 || (p4
= BINDIT(HWCryptoHook_RSA_t
, n_hwcrhk_RSA
)) == NULL
565 || (p5
= BINDIT(HWCryptoHook_RSALoadKey_t
, n_hwcrhk_RSALoadKey
)) == NULL
566 || (p6
= BINDIT(HWCryptoHook_RSAGetPublicKey_t
, n_hwcrhk_RSAGetPublicKey
)) == NULL
567 || (p7
= BINDIT(HWCryptoHook_RSAUnloadKey_t
, n_hwcrhk_RSAUnloadKey
)) == NULL
569 || (p8
= BINDIT(HWCryptoHook_RandomBytes_t
, n_hwcrhk_RandomBytes
)) == NULL
570 || (p9
= BINDIT(HWCryptoHook_ModExpCRT_t
, n_hwcrhk_ModExpCRT
)) == NULL
) {
571 HWCRHKerr(HWCRHK_F_HWCRHK_INIT
, HWCRHK_R_DSO_FAILURE
);
574 /* Copy the pointers */
576 p_hwcrhk_Finish
= p2
;
577 p_hwcrhk_ModExp
= p3
;
578 # ifndef OPENSSL_NO_RSA
580 p_hwcrhk_RSALoadKey
= p5
;
581 p_hwcrhk_RSAGetPublicKey
= p6
;
582 p_hwcrhk_RSAUnloadKey
= p7
;
584 p_hwcrhk_RandomBytes
= p8
;
585 p_hwcrhk_ModExpCRT
= p9
;
588 * Check if the application decided to support dynamic locks, and if it
591 if (disable_mutex_callbacks
== 0) {
592 if (CRYPTO_get_dynlock_create_callback() != NULL
&&
593 CRYPTO_get_dynlock_lock_callback() != NULL
&&
594 CRYPTO_get_dynlock_destroy_callback() != NULL
) {
595 hwcrhk_globals
.mutex_init
= hwcrhk_mutex_init
;
596 hwcrhk_globals
.mutex_acquire
= hwcrhk_mutex_lock
;
597 hwcrhk_globals
.mutex_release
= hwcrhk_mutex_unlock
;
598 hwcrhk_globals
.mutex_destroy
= hwcrhk_mutex_destroy
;
603 * Try and get a context - if not, we may have a DSO but no accelerator!
605 if (!get_context(&hwcrhk_context
, &password_context
)) {
606 HWCRHKerr(HWCRHK_F_HWCRHK_INIT
, HWCRHK_R_UNIT_FAILURE
);
609 /* Everything's fine. */
610 # ifndef OPENSSL_NO_RSA
611 if (hndidx_rsa
== -1)
612 hndidx_rsa
= RSA_get_ex_new_index(0,
613 "nFast HWCryptoHook RSA key handle",
618 DSO_free(hwcrhk_dso
);
620 p_hwcrhk_Init
= NULL
;
621 p_hwcrhk_Finish
= NULL
;
622 p_hwcrhk_ModExp
= NULL
;
623 # ifndef OPENSSL_NO_RSA
625 p_hwcrhk_RSALoadKey
= NULL
;
626 p_hwcrhk_RSAGetPublicKey
= NULL
;
627 p_hwcrhk_RSAUnloadKey
= NULL
;
629 p_hwcrhk_ModExpCRT
= NULL
;
630 p_hwcrhk_RandomBytes
= NULL
;
634 static int hwcrhk_finish(ENGINE
*e
)
637 free_HWCRHK_LIBNAME();
638 if (hwcrhk_dso
== NULL
) {
639 HWCRHKerr(HWCRHK_F_HWCRHK_FINISH
, HWCRHK_R_NOT_LOADED
);
643 release_context(hwcrhk_context
);
644 if (!DSO_free(hwcrhk_dso
)) {
645 HWCRHKerr(HWCRHK_F_HWCRHK_FINISH
, HWCRHK_R_DSO_FAILURE
);
652 p_hwcrhk_Init
= NULL
;
653 p_hwcrhk_Finish
= NULL
;
654 p_hwcrhk_ModExp
= NULL
;
655 # ifndef OPENSSL_NO_RSA
657 p_hwcrhk_RSALoadKey
= NULL
;
658 p_hwcrhk_RSAGetPublicKey
= NULL
;
659 p_hwcrhk_RSAUnloadKey
= NULL
;
661 p_hwcrhk_ModExpCRT
= NULL
;
662 p_hwcrhk_RandomBytes
= NULL
;
666 static int hwcrhk_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
) (void))
671 case HWCRHK_CMD_SO_PATH
:
673 HWCRHKerr(HWCRHK_F_HWCRHK_CTRL
, HWCRHK_R_ALREADY_LOADED
);
677 HWCRHKerr(HWCRHK_F_HWCRHK_CTRL
, ERR_R_PASSED_NULL_PARAMETER
);
680 return set_HWCRHK_LIBNAME((const char *)p
);
681 case ENGINE_CTRL_SET_LOGSTREAM
:
685 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
688 if (CRYPTO_add(&bio
->references
, 1, CRYPTO_LOCK_BIO
) > 1)
691 HWCRHKerr(HWCRHK_F_HWCRHK_CTRL
, HWCRHK_R_BIO_WAS_FREED
);
693 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
695 case ENGINE_CTRL_SET_PASSWORD_CALLBACK
:
696 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
697 password_context
.password_callback
= (pem_password_cb
*)f
;
698 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
700 case ENGINE_CTRL_SET_USER_INTERFACE
:
701 case HWCRHK_CMD_SET_USER_INTERFACE
:
702 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
703 password_context
.ui_method
= (UI_METHOD
*)p
;
704 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
706 case ENGINE_CTRL_SET_CALLBACK_DATA
:
707 case HWCRHK_CMD_SET_CALLBACK_DATA
:
708 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
709 password_context
.callback_data
= p
;
710 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
713 * this enables or disables the "SimpleForkCheck" flag used in the
714 * initialisation structure.
716 case ENGINE_CTRL_CHIL_SET_FORKCHECK
:
717 case HWCRHK_CMD_FORK_CHECK
:
718 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
720 hwcrhk_globals
.flags
|= HWCryptoHook_InitFlags_SimpleForkCheck
;
722 hwcrhk_globals
.flags
&= ~HWCryptoHook_InitFlags_SimpleForkCheck
;
723 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
726 * This will prevent the initialisation function from "installing"
727 * the mutex-handling callbacks, even if they are available from
728 * within the library (or were provided to the library from the
729 * calling application). This is to remove any baggage for
730 * applications not using multithreading.
732 case ENGINE_CTRL_CHIL_NO_LOCKING
:
733 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
734 disable_mutex_callbacks
= 1;
735 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
737 case HWCRHK_CMD_THREAD_LOCKING
:
738 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE
);
739 disable_mutex_callbacks
= ((i
== 0) ? 0 : 1);
740 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE
);
743 /* The command isn't understood by this engine */
745 HWCRHKerr(HWCRHK_F_HWCRHK_CTRL
,
746 HWCRHK_R_CTRL_COMMAND_NOT_IMPLEMENTED
);
754 static EVP_PKEY
*hwcrhk_load_privkey(ENGINE
*eng
, const char *key_id
,
755 UI_METHOD
*ui_method
,
758 # ifndef OPENSSL_NO_RSA
761 EVP_PKEY
*res
= NULL
;
762 # ifndef OPENSSL_NO_RSA
763 HWCryptoHook_MPI e
, n
;
764 HWCryptoHook_RSAKeyHandle
*hptr
;
766 # if !defined(OPENSSL_NO_RSA)
768 HWCryptoHook_ErrMsgBuf rmsg
;
769 HWCryptoHook_PassphraseContext ppctx
;
772 # if !defined(OPENSSL_NO_RSA)
774 rmsg
.size
= sizeof(tempbuf
);
777 if (!hwcrhk_context
) {
778 HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY
, HWCRHK_R_NOT_INITIALISED
);
781 # ifndef OPENSSL_NO_RSA
782 hptr
= OPENSSL_malloc(sizeof(*hptr
));
784 HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY
, ERR_R_MALLOC_FAILURE
);
787 ppctx
.ui_method
= ui_method
;
788 ppctx
.callback_data
= callback_data
;
789 if (p_hwcrhk_RSALoadKey(hwcrhk_context
, key_id
, hptr
, &rmsg
, &ppctx
)) {
790 HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY
, HWCRHK_R_CHIL_ERROR
);
791 ERR_add_error_data(1, rmsg
.buf
);
795 HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY
, HWCRHK_R_NO_KEY
);
799 # ifndef OPENSSL_NO_RSA
800 rtmp
= RSA_new_method(eng
);
801 RSA_set_ex_data(rtmp
, hndidx_rsa
, (char *)hptr
);
804 rtmp
->flags
|= RSA_FLAG_EXT_PKEY
;
807 if (p_hwcrhk_RSAGetPublicKey(*hptr
, &n
, &e
, &rmsg
)
808 != HWCRYPTOHOOK_ERROR_MPISIZE
) {
809 HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY
, HWCRHK_R_CHIL_ERROR
);
810 ERR_add_error_data(1, rmsg
.buf
);
814 bn_expand2(rtmp
->e
, e
.size
/ sizeof(BN_ULONG
));
815 bn_expand2(rtmp
->n
, n
.size
/ sizeof(BN_ULONG
));
819 if (p_hwcrhk_RSAGetPublicKey(*hptr
, &n
, &e
, &rmsg
)) {
820 HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY
, HWCRHK_R_CHIL_ERROR
);
821 ERR_add_error_data(1, rmsg
.buf
);
824 rtmp
->e
->top
= e
.size
/ sizeof(BN_ULONG
);
826 rtmp
->n
->top
= n
.size
/ sizeof(BN_ULONG
);
829 res
= EVP_PKEY_new();
831 HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY
, HWCRHK_R_CHIL_ERROR
);
834 EVP_PKEY_assign_RSA(res
, rtmp
);
838 HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY
,
839 HWCRHK_R_PRIVATE_KEY_ALGORITHMS_DISABLED
);
843 # ifndef OPENSSL_NO_RSA
849 static EVP_PKEY
*hwcrhk_load_pubkey(ENGINE
*eng
, const char *key_id
,
850 UI_METHOD
*ui_method
, void *callback_data
)
852 EVP_PKEY
*res
= NULL
;
854 # ifndef OPENSSL_NO_RSA
855 res
= hwcrhk_load_privkey(eng
, key_id
, ui_method
, callback_data
);
860 # ifndef OPENSSL_NO_RSA
865 CRYPTO_w_lock(CRYPTO_LOCK_EVP_PKEY
);
867 res
->pkey
.rsa
= RSA_new();
868 res
->pkey
.rsa
->n
= rsa
->n
;
869 res
->pkey
.rsa
->e
= rsa
->e
;
872 CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY
);
878 HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PUBKEY
,
879 HWCRHK_R_CTRL_COMMAND_NOT_IMPLEMENTED
);
889 /* A little mod_exp */
890 static int hwcrhk_mod_exp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
891 const BIGNUM
*m
, BN_CTX
*ctx
)
894 HWCryptoHook_ErrMsgBuf rmsg
;
896 * Since HWCryptoHook_MPI is pretty compatible with BIGNUM's, we use them
897 * directly, plus a little macro magic. We only thing we need to make
898 * sure of is that enough space is allocated.
900 HWCryptoHook_MPI m_a
, m_p
, m_n
, m_r
;
903 to_return
= 0; /* expect failure */
905 rmsg
.size
= sizeof(tempbuf
);
907 if (!hwcrhk_context
) {
908 HWCRHKerr(HWCRHK_F_HWCRHK_MOD_EXP
, HWCRHK_R_NOT_INITIALISED
);
911 /* Prepare the params */
912 bn_expand2(r
, m
->top
); /* Check for error !! */
918 /* Perform the operation */
919 ret
= p_hwcrhk_ModExp(hwcrhk_context
, m_a
, m_p
, m_n
, &m_r
, &rmsg
);
921 /* Convert the response */
922 r
->top
= m_r
.size
/ sizeof(BN_ULONG
);
927 * FIXME: When this error is returned, HWCryptoHook is telling us
928 * that falling back to software computation might be a good thing.
930 if (ret
== HWCRYPTOHOOK_ERROR_FALLBACK
) {
931 HWCRHKerr(HWCRHK_F_HWCRHK_MOD_EXP
, HWCRHK_R_REQUEST_FALLBACK
);
933 HWCRHKerr(HWCRHK_F_HWCRHK_MOD_EXP
, HWCRHK_R_REQUEST_FAILED
);
935 ERR_add_error_data(1, rmsg
.buf
);
944 # ifndef OPENSSL_NO_RSA
945 static int hwcrhk_rsa_mod_exp(BIGNUM
*r
, const BIGNUM
*I
, RSA
*rsa
,
949 HWCryptoHook_ErrMsgBuf rmsg
;
950 HWCryptoHook_RSAKeyHandle
*hptr
;
951 int to_return
= 0, ret
;
954 rmsg
.size
= sizeof(tempbuf
);
956 if (!hwcrhk_context
) {
957 HWCRHKerr(HWCRHK_F_HWCRHK_RSA_MOD_EXP
, HWCRHK_R_NOT_INITIALISED
);
962 * This provides support for nForce keys. Since that's opaque data all
963 * we do is provide a handle to the proper key and let HWCryptoHook take
967 (HWCryptoHook_RSAKeyHandle
*) RSA_get_ex_data(rsa
, hndidx_rsa
))
969 HWCryptoHook_MPI m_a
, m_r
;
972 HWCRHKerr(HWCRHK_F_HWCRHK_RSA_MOD_EXP
,
973 HWCRHK_R_MISSING_KEY_COMPONENTS
);
977 /* Prepare the params */
978 bn_expand2(r
, rsa
->n
->top
); /* Check for error !! */
982 /* Perform the operation */
983 ret
= p_hwcrhk_RSA(m_a
, *hptr
, &m_r
, &rmsg
);
985 /* Convert the response */
986 r
->top
= m_r
.size
/ sizeof(BN_ULONG
);
991 * FIXME: When this error is returned, HWCryptoHook is telling us
992 * that falling back to software computation might be a good
995 if (ret
== HWCRYPTOHOOK_ERROR_FALLBACK
) {
996 HWCRHKerr(HWCRHK_F_HWCRHK_RSA_MOD_EXP
,
997 HWCRHK_R_REQUEST_FALLBACK
);
999 HWCRHKerr(HWCRHK_F_HWCRHK_RSA_MOD_EXP
,
1000 HWCRHK_R_REQUEST_FAILED
);
1002 ERR_add_error_data(1, rmsg
.buf
);
1006 HWCryptoHook_MPI m_a
, m_p
, m_q
, m_dmp1
, m_dmq1
, m_iqmp
, m_r
;
1008 if (!rsa
->p
|| !rsa
->q
|| !rsa
->dmp1
|| !rsa
->dmq1
|| !rsa
->iqmp
) {
1009 HWCRHKerr(HWCRHK_F_HWCRHK_RSA_MOD_EXP
,
1010 HWCRHK_R_MISSING_KEY_COMPONENTS
);
1014 /* Prepare the params */
1015 bn_expand2(r
, rsa
->n
->top
); /* Check for error !! */
1017 BN2MPI(m_p
, rsa
->p
);
1018 BN2MPI(m_q
, rsa
->q
);
1019 BN2MPI(m_dmp1
, rsa
->dmp1
);
1020 BN2MPI(m_dmq1
, rsa
->dmq1
);
1021 BN2MPI(m_iqmp
, rsa
->iqmp
);
1024 /* Perform the operation */
1025 ret
= p_hwcrhk_ModExpCRT(hwcrhk_context
, m_a
, m_p
, m_q
,
1026 m_dmp1
, m_dmq1
, m_iqmp
, &m_r
, &rmsg
);
1028 /* Convert the response */
1029 r
->top
= m_r
.size
/ sizeof(BN_ULONG
);
1034 * FIXME: When this error is returned, HWCryptoHook is telling us
1035 * that falling back to software computation might be a good
1038 if (ret
== HWCRYPTOHOOK_ERROR_FALLBACK
) {
1039 HWCRHKerr(HWCRHK_F_HWCRHK_RSA_MOD_EXP
,
1040 HWCRHK_R_REQUEST_FALLBACK
);
1042 HWCRHKerr(HWCRHK_F_HWCRHK_RSA_MOD_EXP
,
1043 HWCRHK_R_REQUEST_FAILED
);
1045 ERR_add_error_data(1, rmsg
.buf
);
1050 * If we're here, we must be here with some semblance of success :-)
1058 # ifndef OPENSSL_NO_RSA
1059 /* This function is aliased to mod_exp (with the mont stuff dropped). */
1060 static int hwcrhk_mod_exp_mont(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
1061 const BIGNUM
*m
, BN_CTX
*ctx
,
1064 return hwcrhk_mod_exp(r
, a
, p
, m
, ctx
);
1067 static int hwcrhk_rsa_finish(RSA
*rsa
)
1069 HWCryptoHook_RSAKeyHandle
*hptr
;
1071 hptr
= RSA_get_ex_data(rsa
, hndidx_rsa
);
1073 p_hwcrhk_RSAUnloadKey(*hptr
, NULL
);
1075 RSA_set_ex_data(rsa
, hndidx_rsa
, NULL
);
1082 # ifndef OPENSSL_NO_DH
1083 /* This function is aliased to mod_exp (with the dh and mont dropped). */
1084 static int hwcrhk_mod_exp_dh(const DH
*dh
, BIGNUM
*r
,
1085 const BIGNUM
*a
, const BIGNUM
*p
,
1086 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
)
1088 return hwcrhk_mod_exp(r
, a
, p
, m
, ctx
);
1092 /* Random bytes are good */
1093 static int hwcrhk_rand_bytes(unsigned char *buf
, int num
)
1096 HWCryptoHook_ErrMsgBuf rmsg
;
1097 int to_return
= 0; /* assume failure */
1101 rmsg
.size
= sizeof(tempbuf
);
1103 if (!hwcrhk_context
) {
1104 HWCRHKerr(HWCRHK_F_HWCRHK_RAND_BYTES
, HWCRHK_R_NOT_INITIALISED
);
1108 ret
= p_hwcrhk_RandomBytes(hwcrhk_context
, buf
, num
, &rmsg
);
1111 * FIXME: When this error is returned, HWCryptoHook is telling us
1112 * that falling back to software computation might be a good thing.
1114 if (ret
== HWCRYPTOHOOK_ERROR_FALLBACK
) {
1115 HWCRHKerr(HWCRHK_F_HWCRHK_RAND_BYTES
, HWCRHK_R_REQUEST_FALLBACK
);
1117 HWCRHKerr(HWCRHK_F_HWCRHK_RAND_BYTES
, HWCRHK_R_REQUEST_FAILED
);
1119 ERR_add_error_data(1, rmsg
.buf
);
1127 static int hwcrhk_rand_status(void)
1133 * Mutex calls: since the HWCryptoHook model closely follows the POSIX model
1134 * these just wrap the POSIX functions and add some logging.
1137 static int hwcrhk_mutex_init(HWCryptoHook_Mutex
* mt
,
1138 HWCryptoHook_CallerContext
* cactx
)
1140 mt
->lockid
= CRYPTO_get_new_dynlockid();
1141 if (mt
->lockid
== 0)
1142 return 1; /* failure */
1143 return 0; /* success */
1146 static int hwcrhk_mutex_lock(HWCryptoHook_Mutex
* mt
)
1148 CRYPTO_w_lock(mt
->lockid
);
1152 static void hwcrhk_mutex_unlock(HWCryptoHook_Mutex
* mt
)
1154 CRYPTO_w_unlock(mt
->lockid
);
1157 static void hwcrhk_mutex_destroy(HWCryptoHook_Mutex
* mt
)
1159 CRYPTO_destroy_dynlockid(mt
->lockid
);
1162 static int hwcrhk_get_pass(const char *prompt_info
,
1163 int *len_io
, char *buf
,
1164 HWCryptoHook_PassphraseContext
* ppctx
,
1165 HWCryptoHook_CallerContext
* cactx
)
1167 pem_password_cb
*callback
= NULL
;
1168 void *callback_data
= NULL
;
1169 UI_METHOD
*ui_method
= NULL
;
1171 * Despite what the documentation says prompt_info can be an empty
1174 if (prompt_info
&& !*prompt_info
)
1178 if (cactx
->ui_method
)
1179 ui_method
= cactx
->ui_method
;
1180 if (cactx
->password_callback
)
1181 callback
= cactx
->password_callback
;
1182 if (cactx
->callback_data
)
1183 callback_data
= cactx
->callback_data
;
1186 if (ppctx
->ui_method
) {
1187 ui_method
= ppctx
->ui_method
;
1190 if (ppctx
->callback_data
)
1191 callback_data
= ppctx
->callback_data
;
1193 if (callback
== NULL
&& ui_method
== NULL
) {
1194 HWCRHKerr(HWCRHK_F_HWCRHK_GET_PASS
, HWCRHK_R_NO_CALLBACK
);
1199 UI
*ui
= UI_new_method(ui_method
);
1202 char *prompt
= UI_construct_prompt(ui
,
1203 "pass phrase", prompt_info
);
1205 ok
= UI_add_input_string(ui
, prompt
,
1206 UI_INPUT_FLAG_DEFAULT_PWD
,
1207 buf
, 0, (*len_io
) - 1);
1208 UI_add_user_data(ui
, callback_data
);
1209 UI_ctrl(ui
, UI_CTRL_PRINT_ERRORS
, 1, 0, 0);
1213 ok
= UI_process(ui
);
1215 while (ok
< 0 && UI_ctrl(ui
, UI_CTRL_IS_REDOABLE
, 0, 0, 0));
1218 *len_io
= strlen(buf
);
1221 OPENSSL_free(prompt
);
1224 *len_io
= callback(buf
, *len_io
, 0, callback_data
);
1231 static int hwcrhk_insert_card(const char *prompt_info
,
1232 const char *wrong_info
,
1233 HWCryptoHook_PassphraseContext
* ppctx
,
1234 HWCryptoHook_CallerContext
* cactx
)
1238 void *callback_data
= NULL
;
1239 UI_METHOD
*ui_method
= NULL
;
1242 if (cactx
->ui_method
)
1243 ui_method
= cactx
->ui_method
;
1244 if (cactx
->callback_data
)
1245 callback_data
= cactx
->callback_data
;
1248 if (ppctx
->ui_method
)
1249 ui_method
= ppctx
->ui_method
;
1250 if (ppctx
->callback_data
)
1251 callback_data
= ppctx
->callback_data
;
1253 if (ui_method
== NULL
) {
1254 HWCRHKerr(HWCRHK_F_HWCRHK_INSERT_CARD
, HWCRHK_R_NO_CALLBACK
);
1258 ui
= UI_new_method(ui_method
);
1264 * Despite what the documentation says wrong_info can be an empty
1267 if (wrong_info
&& *wrong_info
)
1268 BIO_snprintf(buf
, sizeof(buf
) - 1,
1269 "Current card: \"%s\"\n", wrong_info
);
1272 ok
= UI_dup_info_string(ui
, buf
);
1273 if (ok
>= 0 && prompt_info
) {
1274 BIO_snprintf(buf
, sizeof(buf
) - 1,
1275 "Insert card \"%s\"", prompt_info
);
1276 ok
= UI_dup_input_boolean(ui
, buf
,
1277 "\n then hit <enter> or C<enter> to cancel\n",
1278 "\r\n", "Cc", UI_INPUT_FLAG_ECHO
,
1281 UI_add_user_data(ui
, callback_data
);
1284 ok
= UI_process(ui
);
1287 if (ok
== -2 || (ok
>= 0 && answer
== 'C'))
1297 static void hwcrhk_log_message(void *logstr
, const char *message
)
1299 BIO
*lstream
= NULL
;
1301 CRYPTO_w_lock(CRYPTO_LOCK_BIO
);
1303 lstream
= *(BIO
**)logstr
;
1305 BIO_printf(lstream
, "%s\n", message
);
1307 CRYPTO_w_unlock(CRYPTO_LOCK_BIO
);
1311 * This stuff is needed if this ENGINE is being compiled into a
1312 * self-contained shared-library.
1314 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
1315 static int bind_fn(ENGINE
*e
, const char *id
)
1317 if (id
&& (strcmp(id
, engine_hwcrhk_id
) != 0) &&
1318 (strcmp(id
, engine_hwcrhk_id_alt
) != 0))
1320 if (!bind_helper(e
))
1325 IMPLEMENT_DYNAMIC_CHECK_FN()
1326 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn
)
1327 # endif /* OPENSSL_NO_DYNAMIC_ENGINE */
1328 # endif /* !OPENSSL_NO_HW_CHIL */
1329 #endif /* !OPENSSL_NO_HW */