xref: /minix3/crypto/external/bsd/openssl/dist/engines/e_chil.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* crypto/engine/e_chil.c -*- mode: C; c-file-style: "eay" -*- */
2*0a6a1f1dSLionel Sambuc /*
3*0a6a1f1dSLionel Sambuc  * Written by Richard Levitte (richard@levitte.org), Geoff Thorpe
4*0a6a1f1dSLionel Sambuc  * (geoff@geoffthorpe.net) and Dr Stephen N Henson (steve@openssl.org) for
5*0a6a1f1dSLionel Sambuc  * the OpenSSL project 2000.
6ebfedea0SLionel Sambuc  */
7ebfedea0SLionel Sambuc /* ====================================================================
8ebfedea0SLionel Sambuc  * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
9ebfedea0SLionel Sambuc  *
10ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
12ebfedea0SLionel Sambuc  * are met:
13ebfedea0SLionel Sambuc  *
14ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
15ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
16ebfedea0SLionel Sambuc  *
17ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
18ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
19ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
20ebfedea0SLionel Sambuc  *    distribution.
21ebfedea0SLionel Sambuc  *
22ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
23ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
24ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
25ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26ebfedea0SLionel Sambuc  *
27ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
29ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
30ebfedea0SLionel Sambuc  *    licensing@OpenSSL.org.
31ebfedea0SLionel Sambuc  *
32ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
33ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
34ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
35ebfedea0SLionel Sambuc  *
36ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
37ebfedea0SLionel Sambuc  *    acknowledgment:
38ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
39ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
45ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
53ebfedea0SLionel Sambuc  * ====================================================================
54ebfedea0SLionel Sambuc  *
55ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
56ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
57ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
58ebfedea0SLionel Sambuc  *
59ebfedea0SLionel Sambuc  */
60ebfedea0SLionel Sambuc 
61ebfedea0SLionel Sambuc #include <stdio.h>
62ebfedea0SLionel Sambuc #include <string.h>
63ebfedea0SLionel Sambuc #include <openssl/crypto.h>
64ebfedea0SLionel Sambuc #include <openssl/pem.h>
65ebfedea0SLionel Sambuc #include <openssl/dso.h>
66ebfedea0SLionel Sambuc #include <openssl/engine.h>
67ebfedea0SLionel Sambuc #include <openssl/ui.h>
68ebfedea0SLionel Sambuc #include <openssl/rand.h>
69ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
70ebfedea0SLionel Sambuc # include <openssl/rsa.h>
71ebfedea0SLionel Sambuc #endif
72ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
73ebfedea0SLionel Sambuc # include <openssl/dh.h>
74ebfedea0SLionel Sambuc #endif
75ebfedea0SLionel Sambuc #include <openssl/bn.h>
76ebfedea0SLionel Sambuc 
77ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_HW
78ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_HW_CHIL
79ebfedea0SLionel Sambuc 
80*0a6a1f1dSLionel Sambuc /*-
81*0a6a1f1dSLionel Sambuc  * Attribution notice: nCipher have said several times that it's OK for
82ebfedea0SLionel Sambuc  * us to implement a general interface to their boxes, and recently declared
83ebfedea0SLionel Sambuc  * their HWCryptoHook to be public, and therefore available for us to use.
84ebfedea0SLionel Sambuc  * Thanks, nCipher.
85ebfedea0SLionel Sambuc  *
86ebfedea0SLionel Sambuc  * The hwcryptohook.h included here is from May 2000.
87ebfedea0SLionel Sambuc  * [Richard Levitte]
88ebfedea0SLionel Sambuc  */
89ebfedea0SLionel Sambuc #  ifdef FLAT_INC
90ebfedea0SLionel Sambuc #   include "hwcryptohook.h"
91ebfedea0SLionel Sambuc #  else
92ebfedea0SLionel Sambuc #   include "vendor_defns/hwcryptohook.h"
93ebfedea0SLionel Sambuc #  endif
94ebfedea0SLionel Sambuc 
95ebfedea0SLionel Sambuc #  define HWCRHK_LIB_NAME "CHIL engine"
96ebfedea0SLionel Sambuc #  include "e_chil_err.c"
97ebfedea0SLionel Sambuc 
98ebfedea0SLionel Sambuc static int hwcrhk_destroy(ENGINE *e);
99ebfedea0SLionel Sambuc static int hwcrhk_init(ENGINE *e);
100ebfedea0SLionel Sambuc static int hwcrhk_finish(ENGINE *e);
101ebfedea0SLionel Sambuc static int hwcrhk_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void));
102ebfedea0SLionel Sambuc 
103ebfedea0SLionel Sambuc /* Functions to handle mutexes */
104*0a6a1f1dSLionel Sambuc static int hwcrhk_mutex_init(HWCryptoHook_Mutex *,
105*0a6a1f1dSLionel Sambuc                              HWCryptoHook_CallerContext *);
106ebfedea0SLionel Sambuc static int hwcrhk_mutex_lock(HWCryptoHook_Mutex *);
107ebfedea0SLionel Sambuc static void hwcrhk_mutex_unlock(HWCryptoHook_Mutex *);
108ebfedea0SLionel Sambuc static void hwcrhk_mutex_destroy(HWCryptoHook_Mutex *);
109ebfedea0SLionel Sambuc 
110ebfedea0SLionel Sambuc /* BIGNUM stuff */
111ebfedea0SLionel Sambuc static int hwcrhk_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
112ebfedea0SLionel Sambuc                           const BIGNUM *m, BN_CTX *ctx);
113ebfedea0SLionel Sambuc 
114ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
115ebfedea0SLionel Sambuc /* RSA stuff */
116*0a6a1f1dSLionel Sambuc static int hwcrhk_rsa_mod_exp(BIGNUM *r, const BIGNUM *I, RSA *rsa,
117*0a6a1f1dSLionel Sambuc                               BN_CTX *ctx);
118ebfedea0SLionel Sambuc /* This function is aliased to mod_exp (with the mont stuff dropped). */
119ebfedea0SLionel Sambuc static int hwcrhk_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
120*0a6a1f1dSLionel Sambuc                                const BIGNUM *m, BN_CTX *ctx,
121*0a6a1f1dSLionel Sambuc                                BN_MONT_CTX *m_ctx);
122ebfedea0SLionel Sambuc static int hwcrhk_rsa_finish(RSA *rsa);
123ebfedea0SLionel Sambuc #  endif
124ebfedea0SLionel Sambuc 
125ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_DH
126ebfedea0SLionel Sambuc /* DH stuff */
127ebfedea0SLionel Sambuc /* This function is alised to mod_exp (with the DH and mont dropped). */
128ebfedea0SLionel Sambuc static int hwcrhk_mod_exp_dh(const DH *dh, BIGNUM *r,
129ebfedea0SLionel Sambuc                              const BIGNUM *a, const BIGNUM *p,
130*0a6a1f1dSLionel Sambuc                              const BIGNUM *m, BN_CTX *ctx,
131*0a6a1f1dSLionel Sambuc                              BN_MONT_CTX *m_ctx);
132ebfedea0SLionel Sambuc #  endif
133ebfedea0SLionel Sambuc 
134ebfedea0SLionel Sambuc /* RAND stuff */
135ebfedea0SLionel Sambuc static int hwcrhk_rand_bytes(unsigned char *buf, int num);
136ebfedea0SLionel Sambuc static int hwcrhk_rand_status(void);
137ebfedea0SLionel Sambuc 
138ebfedea0SLionel Sambuc /* KM stuff */
139ebfedea0SLionel Sambuc static EVP_PKEY *hwcrhk_load_privkey(ENGINE *eng, const char *key_id,
140*0a6a1f1dSLionel Sambuc                                      UI_METHOD *ui_method,
141*0a6a1f1dSLionel Sambuc                                      void *callback_data);
142ebfedea0SLionel Sambuc static EVP_PKEY *hwcrhk_load_pubkey(ENGINE *eng, const char *key_id,
143*0a6a1f1dSLionel Sambuc                                     UI_METHOD *ui_method,
144*0a6a1f1dSLionel Sambuc                                     void *callback_data);
145ebfedea0SLionel Sambuc 
146ebfedea0SLionel Sambuc /* Interaction stuff */
147ebfedea0SLionel Sambuc static int hwcrhk_insert_card(const char *prompt_info,
148ebfedea0SLionel Sambuc                               const char *wrong_info,
149ebfedea0SLionel Sambuc                               HWCryptoHook_PassphraseContext * ppctx,
150ebfedea0SLionel Sambuc                               HWCryptoHook_CallerContext * cactx);
151ebfedea0SLionel Sambuc static int hwcrhk_get_pass(const char *prompt_info,
152ebfedea0SLionel Sambuc                            int *len_io, char *buf,
153ebfedea0SLionel Sambuc                            HWCryptoHook_PassphraseContext * ppctx,
154ebfedea0SLionel Sambuc                            HWCryptoHook_CallerContext * cactx);
155ebfedea0SLionel Sambuc static void hwcrhk_log_message(void *logstr, const char *message);
156ebfedea0SLionel Sambuc 
157ebfedea0SLionel Sambuc /* The definitions for control commands specific to this engine */
158ebfedea0SLionel Sambuc #  define HWCRHK_CMD_SO_PATH              ENGINE_CMD_BASE
159ebfedea0SLionel Sambuc #  define HWCRHK_CMD_FORK_CHECK           (ENGINE_CMD_BASE + 1)
160ebfedea0SLionel Sambuc #  define HWCRHK_CMD_THREAD_LOCKING       (ENGINE_CMD_BASE + 2)
161ebfedea0SLionel Sambuc #  define HWCRHK_CMD_SET_USER_INTERFACE   (ENGINE_CMD_BASE + 3)
162ebfedea0SLionel Sambuc #  define HWCRHK_CMD_SET_CALLBACK_DATA    (ENGINE_CMD_BASE + 4)
163ebfedea0SLionel Sambuc static const ENGINE_CMD_DEFN hwcrhk_cmd_defns[] = {
164ebfedea0SLionel Sambuc     {HWCRHK_CMD_SO_PATH,
165ebfedea0SLionel Sambuc      "SO_PATH",
166ebfedea0SLionel Sambuc      "Specifies the path to the 'hwcrhk' shared library",
167ebfedea0SLionel Sambuc      ENGINE_CMD_FLAG_STRING},
168ebfedea0SLionel Sambuc     {HWCRHK_CMD_FORK_CHECK,
169ebfedea0SLionel Sambuc      "FORK_CHECK",
170ebfedea0SLionel Sambuc      "Turns fork() checking on (non-zero) or off (zero)",
171ebfedea0SLionel Sambuc      ENGINE_CMD_FLAG_NUMERIC},
172ebfedea0SLionel Sambuc     {HWCRHK_CMD_THREAD_LOCKING,
173ebfedea0SLionel Sambuc      "THREAD_LOCKING",
174ebfedea0SLionel Sambuc      "Turns thread-safe locking on (zero) or off (non-zero)",
175ebfedea0SLionel Sambuc      ENGINE_CMD_FLAG_NUMERIC},
176ebfedea0SLionel Sambuc     {HWCRHK_CMD_SET_USER_INTERFACE,
177ebfedea0SLionel Sambuc      "SET_USER_INTERFACE",
178ebfedea0SLionel Sambuc      "Set the global user interface (internal)",
179ebfedea0SLionel Sambuc      ENGINE_CMD_FLAG_INTERNAL},
180ebfedea0SLionel Sambuc     {HWCRHK_CMD_SET_CALLBACK_DATA,
181ebfedea0SLionel Sambuc      "SET_CALLBACK_DATA",
182ebfedea0SLionel Sambuc      "Set the global user interface extra data (internal)",
183ebfedea0SLionel Sambuc      ENGINE_CMD_FLAG_INTERNAL},
184ebfedea0SLionel Sambuc     {0, NULL, NULL, 0}
185ebfedea0SLionel Sambuc };
186ebfedea0SLionel Sambuc 
187ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
188ebfedea0SLionel Sambuc /* Our internal RSA_METHOD that we provide pointers to */
189*0a6a1f1dSLionel Sambuc static RSA_METHOD hwcrhk_rsa = {
190ebfedea0SLionel Sambuc     "CHIL RSA method",
191ebfedea0SLionel Sambuc     NULL,
192ebfedea0SLionel Sambuc     NULL,
193ebfedea0SLionel Sambuc     NULL,
194ebfedea0SLionel Sambuc     NULL,
195ebfedea0SLionel Sambuc     hwcrhk_rsa_mod_exp,
196ebfedea0SLionel Sambuc     hwcrhk_mod_exp_mont,
197ebfedea0SLionel Sambuc     NULL,
198ebfedea0SLionel Sambuc     hwcrhk_rsa_finish,
199ebfedea0SLionel Sambuc     0,
200ebfedea0SLionel Sambuc     NULL,
201ebfedea0SLionel Sambuc     NULL,
202ebfedea0SLionel Sambuc     NULL,
203ebfedea0SLionel Sambuc     NULL
204ebfedea0SLionel Sambuc };
205ebfedea0SLionel Sambuc #  endif
206ebfedea0SLionel Sambuc 
207ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_DH
208ebfedea0SLionel Sambuc /* Our internal DH_METHOD that we provide pointers to */
209*0a6a1f1dSLionel Sambuc static DH_METHOD hwcrhk_dh = {
210ebfedea0SLionel Sambuc     "CHIL DH method",
211ebfedea0SLionel Sambuc     NULL,
212ebfedea0SLionel Sambuc     NULL,
213ebfedea0SLionel Sambuc     hwcrhk_mod_exp_dh,
214ebfedea0SLionel Sambuc     NULL,
215ebfedea0SLionel Sambuc     NULL,
216ebfedea0SLionel Sambuc     0,
217ebfedea0SLionel Sambuc     NULL,
218ebfedea0SLionel Sambuc     NULL
219ebfedea0SLionel Sambuc };
220ebfedea0SLionel Sambuc #  endif
221ebfedea0SLionel Sambuc 
222*0a6a1f1dSLionel Sambuc static RAND_METHOD hwcrhk_rand = {
223ebfedea0SLionel Sambuc     /* "CHIL RAND method", */
224ebfedea0SLionel Sambuc     NULL,
225ebfedea0SLionel Sambuc     hwcrhk_rand_bytes,
226ebfedea0SLionel Sambuc     NULL,
227ebfedea0SLionel Sambuc     NULL,
228ebfedea0SLionel Sambuc     hwcrhk_rand_bytes,
229ebfedea0SLionel Sambuc     hwcrhk_rand_status,
230ebfedea0SLionel Sambuc };
231ebfedea0SLionel Sambuc 
232ebfedea0SLionel Sambuc /* Constants used when creating the ENGINE */
233ebfedea0SLionel Sambuc static const char *engine_hwcrhk_id = "chil";
234ebfedea0SLionel Sambuc static const char *engine_hwcrhk_name = "CHIL hardware engine support";
235ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_DYNAMIC_ENGINE
236ebfedea0SLionel Sambuc /* Compatibility hack, the dynamic library uses this form in the path */
237ebfedea0SLionel Sambuc static const char *engine_hwcrhk_id_alt = "ncipher";
238ebfedea0SLionel Sambuc #  endif
239ebfedea0SLionel Sambuc 
240ebfedea0SLionel Sambuc /* Internal stuff for HWCryptoHook */
241ebfedea0SLionel Sambuc 
242ebfedea0SLionel Sambuc /* Some structures needed for proper use of thread locks */
243*0a6a1f1dSLionel Sambuc /*
244*0a6a1f1dSLionel Sambuc  * hwcryptohook.h has some typedefs that turn struct HWCryptoHook_MutexValue
245*0a6a1f1dSLionel Sambuc  * into HWCryptoHook_Mutex
246*0a6a1f1dSLionel Sambuc  */
247*0a6a1f1dSLionel Sambuc struct HWCryptoHook_MutexValue {
248ebfedea0SLionel Sambuc     int lockid;
249ebfedea0SLionel Sambuc };
250ebfedea0SLionel Sambuc 
251*0a6a1f1dSLionel Sambuc /*
252*0a6a1f1dSLionel Sambuc  * hwcryptohook.h has some typedefs that turn struct
253*0a6a1f1dSLionel Sambuc  * HWCryptoHook_PassphraseContextValue into HWCryptoHook_PassphraseContext
254*0a6a1f1dSLionel Sambuc  */
255*0a6a1f1dSLionel Sambuc struct HWCryptoHook_PassphraseContextValue {
256ebfedea0SLionel Sambuc     UI_METHOD *ui_method;
257ebfedea0SLionel Sambuc     void *callback_data;
258ebfedea0SLionel Sambuc };
259ebfedea0SLionel Sambuc 
260*0a6a1f1dSLionel Sambuc /*
261*0a6a1f1dSLionel Sambuc  * hwcryptohook.h has some typedefs that turn struct
262*0a6a1f1dSLionel Sambuc  * HWCryptoHook_CallerContextValue into HWCryptoHook_CallerContext
263*0a6a1f1dSLionel Sambuc  */
264*0a6a1f1dSLionel Sambuc struct HWCryptoHook_CallerContextValue {
265ebfedea0SLionel Sambuc     pem_password_cb *password_callback; /* Deprecated! Only present for
266*0a6a1f1dSLionel Sambuc                                          * backward compatibility! */
267ebfedea0SLionel Sambuc     UI_METHOD *ui_method;
268ebfedea0SLionel Sambuc     void *callback_data;
269ebfedea0SLionel Sambuc };
270ebfedea0SLionel Sambuc 
271*0a6a1f1dSLionel Sambuc /*
272*0a6a1f1dSLionel Sambuc  * The MPI structure in HWCryptoHook is pretty compatible with OpenSSL
273*0a6a1f1dSLionel Sambuc  * BIGNUM's, so lets define a couple of conversion macros
274*0a6a1f1dSLionel Sambuc  */
275ebfedea0SLionel Sambuc #  define BN2MPI(mp, bn) \
276ebfedea0SLionel Sambuc     {mp.size = bn->top * sizeof(BN_ULONG); mp.buf = (unsigned char *)bn->d;}
277ebfedea0SLionel Sambuc #  define MPI2BN(bn, mp) \
278ebfedea0SLionel Sambuc     {mp.size = bn->dmax * sizeof(BN_ULONG); mp.buf = (unsigned char *)bn->d;}
279ebfedea0SLionel Sambuc 
280ebfedea0SLionel Sambuc static BIO *logstream = NULL;
281ebfedea0SLionel Sambuc static int disable_mutex_callbacks = 0;
282ebfedea0SLionel Sambuc 
283*0a6a1f1dSLionel Sambuc /*
284*0a6a1f1dSLionel Sambuc  * One might wonder why these are needed, since one can pass down at least a
285*0a6a1f1dSLionel Sambuc  * UI_METHOD and a pointer to callback data to the key-loading functions. The
286*0a6a1f1dSLionel Sambuc  * thing is that the ModExp and RSAImmed functions can load keys as well, if
287*0a6a1f1dSLionel Sambuc  * the data they get is in a special, nCipher-defined format (hint: if you
288*0a6a1f1dSLionel Sambuc  * look at the private exponent of the RSA data as a string, you'll see this
289*0a6a1f1dSLionel Sambuc  * string: "nCipher KM tool key id", followed by some bytes, followed a key
290*0a6a1f1dSLionel Sambuc  * identity string, followed by more bytes.  This happens when you use
291*0a6a1f1dSLionel Sambuc  * "embed" keys instead of "hwcrhk" keys).  Unfortunately, those functions do
292*0a6a1f1dSLionel Sambuc  * not take any passphrase or caller context, and our functions can't really
293*0a6a1f1dSLionel Sambuc  * take any callback data either.  Still, the "insert_card" and
294*0a6a1f1dSLionel Sambuc  * "get_passphrase" callbacks may be called down the line, and will need to
295*0a6a1f1dSLionel Sambuc  * know what user interface callbacks to call, and having callback data from
296*0a6a1f1dSLionel Sambuc  * the application may be a nice thing as well, so we need to keep track of
297*0a6a1f1dSLionel Sambuc  * that globally.
298*0a6a1f1dSLionel Sambuc  */
299ebfedea0SLionel Sambuc static HWCryptoHook_CallerContext password_context = { NULL, NULL, NULL };
300ebfedea0SLionel Sambuc 
301ebfedea0SLionel Sambuc /* Stuff to pass to the HWCryptoHook library */
302ebfedea0SLionel Sambuc static HWCryptoHook_InitInfo hwcrhk_globals = {
303ebfedea0SLionel Sambuc     HWCryptoHook_InitFlags_SimpleForkCheck, /* Flags */
304ebfedea0SLionel Sambuc     &logstream,                 /* logstream */
305ebfedea0SLionel Sambuc     sizeof(BN_ULONG),           /* limbsize */
306ebfedea0SLionel Sambuc     0,                          /* mslimb first: false for BNs */
307ebfedea0SLionel Sambuc     -1,                         /* msbyte first: use native */
308ebfedea0SLionel Sambuc     0,                          /* Max mutexes, 0 = no small limit */
309ebfedea0SLionel Sambuc     0,                          /* Max simultaneous, 0 = default */
310ebfedea0SLionel Sambuc 
311*0a6a1f1dSLionel Sambuc     /*
312*0a6a1f1dSLionel Sambuc      * The next few are mutex stuff: we write wrapper functions around the OS
313*0a6a1f1dSLionel Sambuc      * mutex functions.  We initialise them to 0 here, and change that to
314*0a6a1f1dSLionel Sambuc      * actual function pointers in hwcrhk_init() if dynamic locks are
315*0a6a1f1dSLionel Sambuc      * supported (that is, if the application programmer has made sure of
316*0a6a1f1dSLionel Sambuc      * setting up callbacks bafore starting this engine) *and* if
317*0a6a1f1dSLionel Sambuc      * disable_mutex_callbacks hasn't been set by a call to
318*0a6a1f1dSLionel Sambuc      * ENGINE_ctrl(ENGINE_CTRL_CHIL_NO_LOCKING).
319*0a6a1f1dSLionel Sambuc      */
320ebfedea0SLionel Sambuc     sizeof(HWCryptoHook_Mutex),
321ebfedea0SLionel Sambuc     0,
322ebfedea0SLionel Sambuc     0,
323ebfedea0SLionel Sambuc     0,
324ebfedea0SLionel Sambuc     0,
325ebfedea0SLionel Sambuc 
326*0a6a1f1dSLionel Sambuc     /*
327*0a6a1f1dSLionel Sambuc      * The next few are condvar stuff: we write wrapper functions round the
328*0a6a1f1dSLionel Sambuc      * OS functions.  Currently not implemented and not and absolute
329*0a6a1f1dSLionel Sambuc      * necessity even in threaded programs, therefore 0'ed.  Will hopefully
330*0a6a1f1dSLionel Sambuc      * be implemented some day, since it enhances the efficiency of
331*0a6a1f1dSLionel Sambuc      * HWCryptoHook.
332*0a6a1f1dSLionel Sambuc      */
333ebfedea0SLionel Sambuc     0,                          /* sizeof(HWCryptoHook_CondVar), */
334ebfedea0SLionel Sambuc     0,                          /* hwcrhk_cv_init, */
335ebfedea0SLionel Sambuc     0,                          /* hwcrhk_cv_wait, */
336ebfedea0SLionel Sambuc     0,                          /* hwcrhk_cv_signal, */
337ebfedea0SLionel Sambuc     0,                          /* hwcrhk_cv_broadcast, */
338ebfedea0SLionel Sambuc     0,                          /* hwcrhk_cv_destroy, */
339ebfedea0SLionel Sambuc 
340ebfedea0SLionel Sambuc     hwcrhk_get_pass,            /* pass phrase */
341ebfedea0SLionel Sambuc     hwcrhk_insert_card,         /* insert a card */
342ebfedea0SLionel Sambuc     hwcrhk_log_message          /* Log message */
343ebfedea0SLionel Sambuc };
344ebfedea0SLionel Sambuc 
345ebfedea0SLionel Sambuc /* Now, to our own code */
346ebfedea0SLionel Sambuc 
347*0a6a1f1dSLionel Sambuc /*
348*0a6a1f1dSLionel Sambuc  * This internal function is used by ENGINE_chil() and possibly by the
349*0a6a1f1dSLionel Sambuc  * "dynamic" ENGINE support too
350*0a6a1f1dSLionel Sambuc  */
bind_helper(ENGINE * e)351ebfedea0SLionel Sambuc static int bind_helper(ENGINE *e)
352ebfedea0SLionel Sambuc {
353ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
354ebfedea0SLionel Sambuc     const RSA_METHOD *meth1;
355ebfedea0SLionel Sambuc #  endif
356ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_DH
357ebfedea0SLionel Sambuc     const DH_METHOD *meth2;
358ebfedea0SLionel Sambuc #  endif
359ebfedea0SLionel Sambuc     if (!ENGINE_set_id(e, engine_hwcrhk_id) ||
360ebfedea0SLionel Sambuc         !ENGINE_set_name(e, engine_hwcrhk_name) ||
361ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
362ebfedea0SLionel Sambuc         !ENGINE_set_RSA(e, &hwcrhk_rsa) ||
363ebfedea0SLionel Sambuc #  endif
364ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_DH
365ebfedea0SLionel Sambuc         !ENGINE_set_DH(e, &hwcrhk_dh) ||
366ebfedea0SLionel Sambuc #  endif
367ebfedea0SLionel Sambuc         !ENGINE_set_RAND(e, &hwcrhk_rand) ||
368ebfedea0SLionel Sambuc         !ENGINE_set_destroy_function(e, hwcrhk_destroy) ||
369ebfedea0SLionel Sambuc         !ENGINE_set_init_function(e, hwcrhk_init) ||
370ebfedea0SLionel Sambuc         !ENGINE_set_finish_function(e, hwcrhk_finish) ||
371ebfedea0SLionel Sambuc         !ENGINE_set_ctrl_function(e, hwcrhk_ctrl) ||
372ebfedea0SLionel Sambuc         !ENGINE_set_load_privkey_function(e, hwcrhk_load_privkey) ||
373ebfedea0SLionel Sambuc         !ENGINE_set_load_pubkey_function(e, hwcrhk_load_pubkey) ||
374ebfedea0SLionel Sambuc         !ENGINE_set_cmd_defns(e, hwcrhk_cmd_defns))
375ebfedea0SLionel Sambuc         return 0;
376ebfedea0SLionel Sambuc 
377ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
378*0a6a1f1dSLionel Sambuc     /*
379*0a6a1f1dSLionel Sambuc      * We know that the "PKCS1_SSLeay()" functions hook properly to the
380*0a6a1f1dSLionel Sambuc      * cswift-specific mod_exp and mod_exp_crt so we use those functions. NB:
381*0a6a1f1dSLionel Sambuc      * We don't use ENGINE_openssl() or anything "more generic" because
382*0a6a1f1dSLionel Sambuc      * something like the RSAref code may not hook properly, and if you own
383*0a6a1f1dSLionel Sambuc      * one of these cards then you have the right to do RSA operations on it
384*0a6a1f1dSLionel Sambuc      * anyway!
385*0a6a1f1dSLionel Sambuc      */
386ebfedea0SLionel Sambuc     meth1 = RSA_PKCS1_SSLeay();
387ebfedea0SLionel Sambuc     hwcrhk_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
388ebfedea0SLionel Sambuc     hwcrhk_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
389ebfedea0SLionel Sambuc     hwcrhk_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
390ebfedea0SLionel Sambuc     hwcrhk_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
391ebfedea0SLionel Sambuc #  endif
392ebfedea0SLionel Sambuc 
393ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_DH
394ebfedea0SLionel Sambuc     /* Much the same for Diffie-Hellman */
395ebfedea0SLionel Sambuc     meth2 = DH_OpenSSL();
396ebfedea0SLionel Sambuc     hwcrhk_dh.generate_key = meth2->generate_key;
397ebfedea0SLionel Sambuc     hwcrhk_dh.compute_key = meth2->compute_key;
398ebfedea0SLionel Sambuc #  endif
399ebfedea0SLionel Sambuc 
400ebfedea0SLionel Sambuc     /* Ensure the hwcrhk error handling is set up */
401ebfedea0SLionel Sambuc     ERR_load_HWCRHK_strings();
402ebfedea0SLionel Sambuc     return 1;
403ebfedea0SLionel Sambuc }
404ebfedea0SLionel Sambuc 
405ebfedea0SLionel Sambuc #  ifdef OPENSSL_NO_DYNAMIC_ENGINE
engine_chil(void)406ebfedea0SLionel Sambuc static ENGINE *engine_chil(void)
407ebfedea0SLionel Sambuc {
408ebfedea0SLionel Sambuc     ENGINE *ret = ENGINE_new();
409ebfedea0SLionel Sambuc     if (!ret)
410ebfedea0SLionel Sambuc         return NULL;
411*0a6a1f1dSLionel Sambuc     if (!bind_helper(ret)) {
412ebfedea0SLionel Sambuc         ENGINE_free(ret);
413ebfedea0SLionel Sambuc         return NULL;
414ebfedea0SLionel Sambuc     }
415ebfedea0SLionel Sambuc     return ret;
416ebfedea0SLionel Sambuc }
417ebfedea0SLionel Sambuc 
ENGINE_load_chil(void)418ebfedea0SLionel Sambuc void ENGINE_load_chil(void)
419ebfedea0SLionel Sambuc {
420ebfedea0SLionel Sambuc     /* Copied from eng_[openssl|dyn].c */
421ebfedea0SLionel Sambuc     ENGINE *toadd = engine_chil();
422*0a6a1f1dSLionel Sambuc     if (!toadd)
423*0a6a1f1dSLionel Sambuc         return;
424ebfedea0SLionel Sambuc     ENGINE_add(toadd);
425ebfedea0SLionel Sambuc     ENGINE_free(toadd);
426ebfedea0SLionel Sambuc     ERR_clear_error();
427ebfedea0SLionel Sambuc }
428ebfedea0SLionel Sambuc #  endif
429ebfedea0SLionel Sambuc 
430*0a6a1f1dSLionel Sambuc /*
431*0a6a1f1dSLionel Sambuc  * This is a process-global DSO handle used for loading and unloading the
432*0a6a1f1dSLionel Sambuc  * HWCryptoHook library. NB: This is only set (or unset) during an init() or
433*0a6a1f1dSLionel Sambuc  * finish() call (reference counts permitting) and they're operating with
434*0a6a1f1dSLionel Sambuc  * global locks, so this should be thread-safe implicitly.
435*0a6a1f1dSLionel Sambuc  */
436ebfedea0SLionel Sambuc static DSO *hwcrhk_dso = NULL;
437ebfedea0SLionel Sambuc static HWCryptoHook_ContextHandle hwcrhk_context = 0;
438ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
439*0a6a1f1dSLionel Sambuc /* Index for KM handle.  Not really used yet. */
440*0a6a1f1dSLionel Sambuc static int hndidx_rsa = -1;
441ebfedea0SLionel Sambuc #  endif
442ebfedea0SLionel Sambuc 
443*0a6a1f1dSLionel Sambuc /*
444*0a6a1f1dSLionel Sambuc  * These are the function pointers that are (un)set when the library has
445*0a6a1f1dSLionel Sambuc  * successfully (un)loaded.
446*0a6a1f1dSLionel Sambuc  */
447ebfedea0SLionel Sambuc static HWCryptoHook_Init_t *p_hwcrhk_Init = NULL;
448ebfedea0SLionel Sambuc static HWCryptoHook_Finish_t *p_hwcrhk_Finish = NULL;
449ebfedea0SLionel Sambuc static HWCryptoHook_ModExp_t *p_hwcrhk_ModExp = NULL;
450ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
451ebfedea0SLionel Sambuc static HWCryptoHook_RSA_t *p_hwcrhk_RSA = NULL;
452ebfedea0SLionel Sambuc #  endif
453ebfedea0SLionel Sambuc static HWCryptoHook_RandomBytes_t *p_hwcrhk_RandomBytes = NULL;
454ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
455ebfedea0SLionel Sambuc static HWCryptoHook_RSALoadKey_t *p_hwcrhk_RSALoadKey = NULL;
456ebfedea0SLionel Sambuc static HWCryptoHook_RSAGetPublicKey_t *p_hwcrhk_RSAGetPublicKey = NULL;
457ebfedea0SLionel Sambuc static HWCryptoHook_RSAUnloadKey_t *p_hwcrhk_RSAUnloadKey = NULL;
458ebfedea0SLionel Sambuc #  endif
459ebfedea0SLionel Sambuc static HWCryptoHook_ModExpCRT_t *p_hwcrhk_ModExpCRT = NULL;
460ebfedea0SLionel Sambuc 
461ebfedea0SLionel Sambuc /* Used in the DSO operations. */
462ebfedea0SLionel Sambuc static const char *HWCRHK_LIBNAME = NULL;
free_HWCRHK_LIBNAME(void)463ebfedea0SLionel Sambuc static void free_HWCRHK_LIBNAME(void)
464ebfedea0SLionel Sambuc {
465ebfedea0SLionel Sambuc     if (HWCRHK_LIBNAME)
466ebfedea0SLionel Sambuc         OPENSSL_free((void *)HWCRHK_LIBNAME);
467ebfedea0SLionel Sambuc     HWCRHK_LIBNAME = NULL;
468ebfedea0SLionel Sambuc }
469*0a6a1f1dSLionel Sambuc 
get_HWCRHK_LIBNAME(void)470ebfedea0SLionel Sambuc static const char *get_HWCRHK_LIBNAME(void)
471ebfedea0SLionel Sambuc {
472ebfedea0SLionel Sambuc     if (HWCRHK_LIBNAME)
473ebfedea0SLionel Sambuc         return HWCRHK_LIBNAME;
474ebfedea0SLionel Sambuc     return "nfhwcrhk";
475ebfedea0SLionel Sambuc }
476*0a6a1f1dSLionel Sambuc 
set_HWCRHK_LIBNAME(const char * name)477ebfedea0SLionel Sambuc static long set_HWCRHK_LIBNAME(const char *name)
478ebfedea0SLionel Sambuc {
479ebfedea0SLionel Sambuc     free_HWCRHK_LIBNAME();
480ebfedea0SLionel Sambuc     return (((HWCRHK_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
481ebfedea0SLionel Sambuc }
482*0a6a1f1dSLionel Sambuc 
483ebfedea0SLionel Sambuc static const char *n_hwcrhk_Init = "HWCryptoHook_Init";
484ebfedea0SLionel Sambuc static const char *n_hwcrhk_Finish = "HWCryptoHook_Finish";
485ebfedea0SLionel Sambuc static const char *n_hwcrhk_ModExp = "HWCryptoHook_ModExp";
486ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
487ebfedea0SLionel Sambuc static const char *n_hwcrhk_RSA = "HWCryptoHook_RSA";
488ebfedea0SLionel Sambuc #  endif
489ebfedea0SLionel Sambuc static const char *n_hwcrhk_RandomBytes = "HWCryptoHook_RandomBytes";
490ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
491ebfedea0SLionel Sambuc static const char *n_hwcrhk_RSALoadKey = "HWCryptoHook_RSALoadKey";
492ebfedea0SLionel Sambuc static const char *n_hwcrhk_RSAGetPublicKey = "HWCryptoHook_RSAGetPublicKey";
493ebfedea0SLionel Sambuc static const char *n_hwcrhk_RSAUnloadKey = "HWCryptoHook_RSAUnloadKey";
494ebfedea0SLionel Sambuc #  endif
495ebfedea0SLionel Sambuc static const char *n_hwcrhk_ModExpCRT = "HWCryptoHook_ModExpCRT";
496ebfedea0SLionel Sambuc 
497*0a6a1f1dSLionel Sambuc /*
498*0a6a1f1dSLionel Sambuc  * HWCryptoHook library functions and mechanics - these are used by the
499*0a6a1f1dSLionel Sambuc  * higher-level functions further down. NB: As and where there's no error
500*0a6a1f1dSLionel Sambuc  * checking, take a look lower down where these functions are called, the
501*0a6a1f1dSLionel Sambuc  * checking and error handling is probably down there.
502*0a6a1f1dSLionel Sambuc  */
503ebfedea0SLionel Sambuc 
504ebfedea0SLionel Sambuc /* utility function to obtain a context */
get_context(HWCryptoHook_ContextHandle * hac,HWCryptoHook_CallerContext * cac)505ebfedea0SLionel Sambuc static int get_context(HWCryptoHook_ContextHandle * hac,
506ebfedea0SLionel Sambuc                        HWCryptoHook_CallerContext * cac)
507ebfedea0SLionel Sambuc {
508ebfedea0SLionel Sambuc     char tempbuf[1024];
509ebfedea0SLionel Sambuc     HWCryptoHook_ErrMsgBuf rmsg;
510ebfedea0SLionel Sambuc 
511ebfedea0SLionel Sambuc     rmsg.buf = tempbuf;
512ebfedea0SLionel Sambuc     rmsg.size = sizeof(tempbuf);
513ebfedea0SLionel Sambuc 
514*0a6a1f1dSLionel Sambuc     *hac = p_hwcrhk_Init(&hwcrhk_globals, sizeof(hwcrhk_globals), &rmsg, cac);
515ebfedea0SLionel Sambuc     if (!*hac)
516ebfedea0SLionel Sambuc         return 0;
517ebfedea0SLionel Sambuc     return 1;
518ebfedea0SLionel Sambuc }
519ebfedea0SLionel Sambuc 
520ebfedea0SLionel Sambuc /* similarly to release one. */
release_context(HWCryptoHook_ContextHandle hac)521ebfedea0SLionel Sambuc static void release_context(HWCryptoHook_ContextHandle hac)
522ebfedea0SLionel Sambuc {
523ebfedea0SLionel Sambuc     p_hwcrhk_Finish(hac);
524ebfedea0SLionel Sambuc }
525ebfedea0SLionel Sambuc 
526ebfedea0SLionel Sambuc /* Destructor (complements the "ENGINE_chil()" constructor) */
hwcrhk_destroy(ENGINE * e)527ebfedea0SLionel Sambuc static int hwcrhk_destroy(ENGINE *e)
528ebfedea0SLionel Sambuc {
529ebfedea0SLionel Sambuc     free_HWCRHK_LIBNAME();
530ebfedea0SLionel Sambuc     ERR_unload_HWCRHK_strings();
531ebfedea0SLionel Sambuc     return 1;
532ebfedea0SLionel Sambuc }
533ebfedea0SLionel Sambuc 
534ebfedea0SLionel Sambuc /* (de)initialisation functions. */
hwcrhk_init(ENGINE * e)535ebfedea0SLionel Sambuc static int hwcrhk_init(ENGINE *e)
536ebfedea0SLionel Sambuc {
537ebfedea0SLionel Sambuc     HWCryptoHook_Init_t *p1;
538ebfedea0SLionel Sambuc     HWCryptoHook_Finish_t *p2;
539ebfedea0SLionel Sambuc     HWCryptoHook_ModExp_t *p3;
540ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
541ebfedea0SLionel Sambuc     HWCryptoHook_RSA_t *p4;
542ebfedea0SLionel Sambuc     HWCryptoHook_RSALoadKey_t *p5;
543ebfedea0SLionel Sambuc     HWCryptoHook_RSAGetPublicKey_t *p6;
544ebfedea0SLionel Sambuc     HWCryptoHook_RSAUnloadKey_t *p7;
545ebfedea0SLionel Sambuc #  endif
546ebfedea0SLionel Sambuc     HWCryptoHook_RandomBytes_t *p8;
547ebfedea0SLionel Sambuc     HWCryptoHook_ModExpCRT_t *p9;
548ebfedea0SLionel Sambuc 
549*0a6a1f1dSLionel Sambuc     if (hwcrhk_dso != NULL) {
550ebfedea0SLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_INIT, HWCRHK_R_ALREADY_LOADED);
551ebfedea0SLionel Sambuc         goto err;
552ebfedea0SLionel Sambuc     }
553ebfedea0SLionel Sambuc     /* Attempt to load libnfhwcrhk.so/nfhwcrhk.dll/whatever. */
554ebfedea0SLionel Sambuc     hwcrhk_dso = DSO_load(NULL, get_HWCRHK_LIBNAME(), NULL, 0);
555*0a6a1f1dSLionel Sambuc     if (hwcrhk_dso == NULL) {
556ebfedea0SLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_INIT, HWCRHK_R_DSO_FAILURE);
557ebfedea0SLionel Sambuc         goto err;
558ebfedea0SLionel Sambuc     }
559ebfedea0SLionel Sambuc     if (!(p1 = (HWCryptoHook_Init_t *)
560ebfedea0SLionel Sambuc           DSO_bind_func(hwcrhk_dso, n_hwcrhk_Init)) ||
561ebfedea0SLionel Sambuc         !(p2 = (HWCryptoHook_Finish_t *)
562ebfedea0SLionel Sambuc           DSO_bind_func(hwcrhk_dso, n_hwcrhk_Finish)) ||
563ebfedea0SLionel Sambuc         !(p3 = (HWCryptoHook_ModExp_t *)
564ebfedea0SLionel Sambuc           DSO_bind_func(hwcrhk_dso, n_hwcrhk_ModExp)) ||
565ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
566ebfedea0SLionel Sambuc         !(p4 = (HWCryptoHook_RSA_t *)
567ebfedea0SLionel Sambuc           DSO_bind_func(hwcrhk_dso, n_hwcrhk_RSA)) ||
568ebfedea0SLionel Sambuc         !(p5 = (HWCryptoHook_RSALoadKey_t *)
569ebfedea0SLionel Sambuc           DSO_bind_func(hwcrhk_dso, n_hwcrhk_RSALoadKey)) ||
570ebfedea0SLionel Sambuc         !(p6 = (HWCryptoHook_RSAGetPublicKey_t *)
571ebfedea0SLionel Sambuc           DSO_bind_func(hwcrhk_dso, n_hwcrhk_RSAGetPublicKey)) ||
572ebfedea0SLionel Sambuc         !(p7 = (HWCryptoHook_RSAUnloadKey_t *)
573ebfedea0SLionel Sambuc           DSO_bind_func(hwcrhk_dso, n_hwcrhk_RSAUnloadKey)) ||
574ebfedea0SLionel Sambuc #  endif
575ebfedea0SLionel Sambuc         !(p8 = (HWCryptoHook_RandomBytes_t *)
576ebfedea0SLionel Sambuc           DSO_bind_func(hwcrhk_dso, n_hwcrhk_RandomBytes)) ||
577ebfedea0SLionel Sambuc         !(p9 = (HWCryptoHook_ModExpCRT_t *)
578*0a6a1f1dSLionel Sambuc           DSO_bind_func(hwcrhk_dso, n_hwcrhk_ModExpCRT))) {
579ebfedea0SLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_INIT, HWCRHK_R_DSO_FAILURE);
580ebfedea0SLionel Sambuc         goto err;
581ebfedea0SLionel Sambuc     }
582ebfedea0SLionel Sambuc     /* Copy the pointers */
583ebfedea0SLionel Sambuc     p_hwcrhk_Init = p1;
584ebfedea0SLionel Sambuc     p_hwcrhk_Finish = p2;
585ebfedea0SLionel Sambuc     p_hwcrhk_ModExp = p3;
586ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
587ebfedea0SLionel Sambuc     p_hwcrhk_RSA = p4;
588ebfedea0SLionel Sambuc     p_hwcrhk_RSALoadKey = p5;
589ebfedea0SLionel Sambuc     p_hwcrhk_RSAGetPublicKey = p6;
590ebfedea0SLionel Sambuc     p_hwcrhk_RSAUnloadKey = p7;
591ebfedea0SLionel Sambuc #  endif
592ebfedea0SLionel Sambuc     p_hwcrhk_RandomBytes = p8;
593ebfedea0SLionel Sambuc     p_hwcrhk_ModExpCRT = p9;
594ebfedea0SLionel Sambuc 
595*0a6a1f1dSLionel Sambuc     /*
596*0a6a1f1dSLionel Sambuc      * Check if the application decided to support dynamic locks, and if it
597*0a6a1f1dSLionel Sambuc      * does, use them.
598*0a6a1f1dSLionel Sambuc      */
599*0a6a1f1dSLionel Sambuc     if (disable_mutex_callbacks == 0) {
600ebfedea0SLionel Sambuc         if (CRYPTO_get_dynlock_create_callback() != NULL &&
601ebfedea0SLionel Sambuc             CRYPTO_get_dynlock_lock_callback() != NULL &&
602*0a6a1f1dSLionel Sambuc             CRYPTO_get_dynlock_destroy_callback() != NULL) {
603ebfedea0SLionel Sambuc             hwcrhk_globals.mutex_init = hwcrhk_mutex_init;
604ebfedea0SLionel Sambuc             hwcrhk_globals.mutex_acquire = hwcrhk_mutex_lock;
605ebfedea0SLionel Sambuc             hwcrhk_globals.mutex_release = hwcrhk_mutex_unlock;
606ebfedea0SLionel Sambuc             hwcrhk_globals.mutex_destroy = hwcrhk_mutex_destroy;
607ebfedea0SLionel Sambuc         }
608ebfedea0SLionel Sambuc     }
609ebfedea0SLionel Sambuc 
610*0a6a1f1dSLionel Sambuc     /*
611*0a6a1f1dSLionel Sambuc      * Try and get a context - if not, we may have a DSO but no accelerator!
612*0a6a1f1dSLionel Sambuc      */
613*0a6a1f1dSLionel Sambuc     if (!get_context(&hwcrhk_context, &password_context)) {
614ebfedea0SLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_INIT, HWCRHK_R_UNIT_FAILURE);
615ebfedea0SLionel Sambuc         goto err;
616ebfedea0SLionel Sambuc     }
617ebfedea0SLionel Sambuc     /* Everything's fine. */
618ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
619ebfedea0SLionel Sambuc     if (hndidx_rsa == -1)
620ebfedea0SLionel Sambuc         hndidx_rsa = RSA_get_ex_new_index(0,
621ebfedea0SLionel Sambuc                                           "nFast HWCryptoHook RSA key handle",
622ebfedea0SLionel Sambuc                                           NULL, NULL, NULL);
623ebfedea0SLionel Sambuc #  endif
624ebfedea0SLionel Sambuc     return 1;
625ebfedea0SLionel Sambuc  err:
626ebfedea0SLionel Sambuc     if (hwcrhk_dso)
627ebfedea0SLionel Sambuc         DSO_free(hwcrhk_dso);
628ebfedea0SLionel Sambuc     hwcrhk_dso = NULL;
629ebfedea0SLionel Sambuc     p_hwcrhk_Init = NULL;
630ebfedea0SLionel Sambuc     p_hwcrhk_Finish = NULL;
631ebfedea0SLionel Sambuc     p_hwcrhk_ModExp = NULL;
632ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
633ebfedea0SLionel Sambuc     p_hwcrhk_RSA = NULL;
634ebfedea0SLionel Sambuc     p_hwcrhk_RSALoadKey = NULL;
635ebfedea0SLionel Sambuc     p_hwcrhk_RSAGetPublicKey = NULL;
636ebfedea0SLionel Sambuc     p_hwcrhk_RSAUnloadKey = NULL;
637ebfedea0SLionel Sambuc #  endif
638ebfedea0SLionel Sambuc     p_hwcrhk_ModExpCRT = NULL;
639ebfedea0SLionel Sambuc     p_hwcrhk_RandomBytes = NULL;
640ebfedea0SLionel Sambuc     return 0;
641ebfedea0SLionel Sambuc }
642ebfedea0SLionel Sambuc 
hwcrhk_finish(ENGINE * e)643ebfedea0SLionel Sambuc static int hwcrhk_finish(ENGINE *e)
644ebfedea0SLionel Sambuc {
645ebfedea0SLionel Sambuc     int to_return = 1;
646ebfedea0SLionel Sambuc     free_HWCRHK_LIBNAME();
647*0a6a1f1dSLionel Sambuc     if (hwcrhk_dso == NULL) {
648ebfedea0SLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_FINISH, HWCRHK_R_NOT_LOADED);
649ebfedea0SLionel Sambuc         to_return = 0;
650ebfedea0SLionel Sambuc         goto err;
651ebfedea0SLionel Sambuc     }
652ebfedea0SLionel Sambuc     release_context(hwcrhk_context);
653*0a6a1f1dSLionel Sambuc     if (!DSO_free(hwcrhk_dso)) {
654ebfedea0SLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_FINISH, HWCRHK_R_DSO_FAILURE);
655ebfedea0SLionel Sambuc         to_return = 0;
656ebfedea0SLionel Sambuc         goto err;
657ebfedea0SLionel Sambuc     }
658ebfedea0SLionel Sambuc  err:
659ebfedea0SLionel Sambuc     if (logstream)
660ebfedea0SLionel Sambuc         BIO_free(logstream);
661ebfedea0SLionel Sambuc     hwcrhk_dso = NULL;
662ebfedea0SLionel Sambuc     p_hwcrhk_Init = NULL;
663ebfedea0SLionel Sambuc     p_hwcrhk_Finish = NULL;
664ebfedea0SLionel Sambuc     p_hwcrhk_ModExp = NULL;
665ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
666ebfedea0SLionel Sambuc     p_hwcrhk_RSA = NULL;
667ebfedea0SLionel Sambuc     p_hwcrhk_RSALoadKey = NULL;
668ebfedea0SLionel Sambuc     p_hwcrhk_RSAGetPublicKey = NULL;
669ebfedea0SLionel Sambuc     p_hwcrhk_RSAUnloadKey = NULL;
670ebfedea0SLionel Sambuc #  endif
671ebfedea0SLionel Sambuc     p_hwcrhk_ModExpCRT = NULL;
672ebfedea0SLionel Sambuc     p_hwcrhk_RandomBytes = NULL;
673ebfedea0SLionel Sambuc     return to_return;
674ebfedea0SLionel Sambuc }
675ebfedea0SLionel Sambuc 
hwcrhk_ctrl(ENGINE * e,int cmd,long i,void * p,void (* f)(void))676ebfedea0SLionel Sambuc static int hwcrhk_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
677ebfedea0SLionel Sambuc {
678ebfedea0SLionel Sambuc     int to_return = 1;
679ebfedea0SLionel Sambuc 
680*0a6a1f1dSLionel Sambuc     switch (cmd) {
681ebfedea0SLionel Sambuc     case HWCRHK_CMD_SO_PATH:
682*0a6a1f1dSLionel Sambuc         if (hwcrhk_dso) {
683ebfedea0SLionel Sambuc             HWCRHKerr(HWCRHK_F_HWCRHK_CTRL, HWCRHK_R_ALREADY_LOADED);
684ebfedea0SLionel Sambuc             return 0;
685ebfedea0SLionel Sambuc         }
686*0a6a1f1dSLionel Sambuc         if (p == NULL) {
687ebfedea0SLionel Sambuc             HWCRHKerr(HWCRHK_F_HWCRHK_CTRL, ERR_R_PASSED_NULL_PARAMETER);
688ebfedea0SLionel Sambuc             return 0;
689ebfedea0SLionel Sambuc         }
690ebfedea0SLionel Sambuc         return set_HWCRHK_LIBNAME((const char *)p);
691ebfedea0SLionel Sambuc     case ENGINE_CTRL_SET_LOGSTREAM:
692ebfedea0SLionel Sambuc         {
693ebfedea0SLionel Sambuc             BIO *bio = (BIO *)p;
694ebfedea0SLionel Sambuc 
695ebfedea0SLionel Sambuc             CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
696*0a6a1f1dSLionel Sambuc             if (logstream) {
697ebfedea0SLionel Sambuc                 BIO_free(logstream);
698ebfedea0SLionel Sambuc                 logstream = NULL;
699ebfedea0SLionel Sambuc             }
700ebfedea0SLionel Sambuc             if (CRYPTO_add(&bio->references, 1, CRYPTO_LOCK_BIO) > 1)
701ebfedea0SLionel Sambuc                 logstream = bio;
702ebfedea0SLionel Sambuc             else
703ebfedea0SLionel Sambuc                 HWCRHKerr(HWCRHK_F_HWCRHK_CTRL, HWCRHK_R_BIO_WAS_FREED);
704ebfedea0SLionel Sambuc         }
705ebfedea0SLionel Sambuc         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
706ebfedea0SLionel Sambuc         break;
707ebfedea0SLionel Sambuc     case ENGINE_CTRL_SET_PASSWORD_CALLBACK:
708ebfedea0SLionel Sambuc         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
709ebfedea0SLionel Sambuc         password_context.password_callback = (pem_password_cb *)f;
710ebfedea0SLionel Sambuc         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
711ebfedea0SLionel Sambuc         break;
712ebfedea0SLionel Sambuc     case ENGINE_CTRL_SET_USER_INTERFACE:
713ebfedea0SLionel Sambuc     case HWCRHK_CMD_SET_USER_INTERFACE:
714ebfedea0SLionel Sambuc         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
715ebfedea0SLionel Sambuc         password_context.ui_method = (UI_METHOD *)p;
716ebfedea0SLionel Sambuc         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
717ebfedea0SLionel Sambuc         break;
718ebfedea0SLionel Sambuc     case ENGINE_CTRL_SET_CALLBACK_DATA:
719ebfedea0SLionel Sambuc     case HWCRHK_CMD_SET_CALLBACK_DATA:
720ebfedea0SLionel Sambuc         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
721ebfedea0SLionel Sambuc         password_context.callback_data = p;
722ebfedea0SLionel Sambuc         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
723ebfedea0SLionel Sambuc         break;
724*0a6a1f1dSLionel Sambuc         /*
725*0a6a1f1dSLionel Sambuc          * this enables or disables the "SimpleForkCheck" flag used in the
726*0a6a1f1dSLionel Sambuc          * initialisation structure.
727*0a6a1f1dSLionel Sambuc          */
728ebfedea0SLionel Sambuc     case ENGINE_CTRL_CHIL_SET_FORKCHECK:
729ebfedea0SLionel Sambuc     case HWCRHK_CMD_FORK_CHECK:
730ebfedea0SLionel Sambuc         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
731ebfedea0SLionel Sambuc         if (i)
732*0a6a1f1dSLionel Sambuc             hwcrhk_globals.flags |= HWCryptoHook_InitFlags_SimpleForkCheck;
733ebfedea0SLionel Sambuc         else
734*0a6a1f1dSLionel Sambuc             hwcrhk_globals.flags &= ~HWCryptoHook_InitFlags_SimpleForkCheck;
735ebfedea0SLionel Sambuc         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
736ebfedea0SLionel Sambuc         break;
737*0a6a1f1dSLionel Sambuc         /*
738*0a6a1f1dSLionel Sambuc          * This will prevent the initialisation function from "installing"
739ebfedea0SLionel Sambuc          * the mutex-handling callbacks, even if they are available from
740ebfedea0SLionel Sambuc          * within the library (or were provided to the library from the
741ebfedea0SLionel Sambuc          * calling application). This is to remove any baggage for
742*0a6a1f1dSLionel Sambuc          * applications not using multithreading.
743*0a6a1f1dSLionel Sambuc          */
744ebfedea0SLionel Sambuc     case ENGINE_CTRL_CHIL_NO_LOCKING:
745ebfedea0SLionel Sambuc         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
746ebfedea0SLionel Sambuc         disable_mutex_callbacks = 1;
747ebfedea0SLionel Sambuc         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
748ebfedea0SLionel Sambuc         break;
749ebfedea0SLionel Sambuc     case HWCRHK_CMD_THREAD_LOCKING:
750ebfedea0SLionel Sambuc         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
751ebfedea0SLionel Sambuc         disable_mutex_callbacks = ((i == 0) ? 0 : 1);
752ebfedea0SLionel Sambuc         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
753ebfedea0SLionel Sambuc         break;
754ebfedea0SLionel Sambuc 
755ebfedea0SLionel Sambuc         /* The command isn't understood by this engine */
756ebfedea0SLionel Sambuc     default:
757ebfedea0SLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_CTRL,
758ebfedea0SLionel Sambuc                   HWCRHK_R_CTRL_COMMAND_NOT_IMPLEMENTED);
759ebfedea0SLionel Sambuc         to_return = 0;
760ebfedea0SLionel Sambuc         break;
761ebfedea0SLionel Sambuc     }
762ebfedea0SLionel Sambuc 
763ebfedea0SLionel Sambuc     return to_return;
764ebfedea0SLionel Sambuc }
765ebfedea0SLionel Sambuc 
hwcrhk_load_privkey(ENGINE * eng,const char * key_id,UI_METHOD * ui_method,void * callback_data)766ebfedea0SLionel Sambuc static EVP_PKEY *hwcrhk_load_privkey(ENGINE *eng, const char *key_id,
767*0a6a1f1dSLionel Sambuc                                      UI_METHOD *ui_method,
768*0a6a1f1dSLionel Sambuc                                      void *callback_data)
769ebfedea0SLionel Sambuc {
770ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
771ebfedea0SLionel Sambuc     RSA *rtmp = NULL;
772ebfedea0SLionel Sambuc #  endif
773ebfedea0SLionel Sambuc     EVP_PKEY *res = NULL;
774ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
775ebfedea0SLionel Sambuc     HWCryptoHook_MPI e, n;
776ebfedea0SLionel Sambuc     HWCryptoHook_RSAKeyHandle *hptr;
777ebfedea0SLionel Sambuc #  endif
778ebfedea0SLionel Sambuc #  if !defined(OPENSSL_NO_RSA)
779ebfedea0SLionel Sambuc     char tempbuf[1024];
780ebfedea0SLionel Sambuc     HWCryptoHook_ErrMsgBuf rmsg;
781ebfedea0SLionel Sambuc     HWCryptoHook_PassphraseContext ppctx;
782ebfedea0SLionel Sambuc #  endif
783ebfedea0SLionel Sambuc 
784ebfedea0SLionel Sambuc #  if !defined(OPENSSL_NO_RSA)
785ebfedea0SLionel Sambuc     rmsg.buf = tempbuf;
786ebfedea0SLionel Sambuc     rmsg.size = sizeof(tempbuf);
787ebfedea0SLionel Sambuc #  endif
788ebfedea0SLionel Sambuc 
789*0a6a1f1dSLionel Sambuc     if (!hwcrhk_context) {
790*0a6a1f1dSLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY, HWCRHK_R_NOT_INITIALISED);
791ebfedea0SLionel Sambuc         goto err;
792ebfedea0SLionel Sambuc     }
793ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
794ebfedea0SLionel Sambuc     hptr = OPENSSL_malloc(sizeof(HWCryptoHook_RSAKeyHandle));
795*0a6a1f1dSLionel Sambuc     if (!hptr) {
796*0a6a1f1dSLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY, ERR_R_MALLOC_FAILURE);
797ebfedea0SLionel Sambuc         goto err;
798ebfedea0SLionel Sambuc     }
799ebfedea0SLionel Sambuc     ppctx.ui_method = ui_method;
800ebfedea0SLionel Sambuc     ppctx.callback_data = callback_data;
801*0a6a1f1dSLionel Sambuc     if (p_hwcrhk_RSALoadKey(hwcrhk_context, key_id, hptr, &rmsg, &ppctx)) {
802*0a6a1f1dSLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY, HWCRHK_R_CHIL_ERROR);
803ebfedea0SLionel Sambuc         ERR_add_error_data(1, rmsg.buf);
804ebfedea0SLionel Sambuc         goto err;
805ebfedea0SLionel Sambuc     }
806*0a6a1f1dSLionel Sambuc     if (!*hptr) {
807*0a6a1f1dSLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY, HWCRHK_R_NO_KEY);
808ebfedea0SLionel Sambuc         goto err;
809ebfedea0SLionel Sambuc     }
810ebfedea0SLionel Sambuc #  endif
811ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
812ebfedea0SLionel Sambuc     rtmp = RSA_new_method(eng);
813ebfedea0SLionel Sambuc     RSA_set_ex_data(rtmp, hndidx_rsa, (char *)hptr);
814ebfedea0SLionel Sambuc     rtmp->e = BN_new();
815ebfedea0SLionel Sambuc     rtmp->n = BN_new();
816ebfedea0SLionel Sambuc     rtmp->flags |= RSA_FLAG_EXT_PKEY;
817ebfedea0SLionel Sambuc     MPI2BN(rtmp->e, e);
818ebfedea0SLionel Sambuc     MPI2BN(rtmp->n, n);
819ebfedea0SLionel Sambuc     if (p_hwcrhk_RSAGetPublicKey(*hptr, &n, &e, &rmsg)
820*0a6a1f1dSLionel Sambuc         != HWCRYPTOHOOK_ERROR_MPISIZE) {
821ebfedea0SLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY, HWCRHK_R_CHIL_ERROR);
822ebfedea0SLionel Sambuc         ERR_add_error_data(1, rmsg.buf);
823ebfedea0SLionel Sambuc         goto err;
824ebfedea0SLionel Sambuc     }
825ebfedea0SLionel Sambuc 
826ebfedea0SLionel Sambuc     bn_expand2(rtmp->e, e.size / sizeof(BN_ULONG));
827ebfedea0SLionel Sambuc     bn_expand2(rtmp->n, n.size / sizeof(BN_ULONG));
828ebfedea0SLionel Sambuc     MPI2BN(rtmp->e, e);
829ebfedea0SLionel Sambuc     MPI2BN(rtmp->n, n);
830ebfedea0SLionel Sambuc 
831*0a6a1f1dSLionel Sambuc     if (p_hwcrhk_RSAGetPublicKey(*hptr, &n, &e, &rmsg)) {
832*0a6a1f1dSLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY, HWCRHK_R_CHIL_ERROR);
833ebfedea0SLionel Sambuc         ERR_add_error_data(1, rmsg.buf);
834ebfedea0SLionel Sambuc         goto err;
835ebfedea0SLionel Sambuc     }
836ebfedea0SLionel Sambuc     rtmp->e->top = e.size / sizeof(BN_ULONG);
837ebfedea0SLionel Sambuc     bn_fix_top(rtmp->e);
838ebfedea0SLionel Sambuc     rtmp->n->top = n.size / sizeof(BN_ULONG);
839ebfedea0SLionel Sambuc     bn_fix_top(rtmp->n);
840ebfedea0SLionel Sambuc 
841ebfedea0SLionel Sambuc     res = EVP_PKEY_new();
842ebfedea0SLionel Sambuc     EVP_PKEY_assign_RSA(res, rtmp);
843ebfedea0SLionel Sambuc #  endif
844ebfedea0SLionel Sambuc 
845ebfedea0SLionel Sambuc     if (!res)
846ebfedea0SLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY,
847ebfedea0SLionel Sambuc                   HWCRHK_R_PRIVATE_KEY_ALGORITHMS_DISABLED);
848ebfedea0SLionel Sambuc 
849ebfedea0SLionel Sambuc     return res;
850ebfedea0SLionel Sambuc  err:
851ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
852ebfedea0SLionel Sambuc     if (rtmp)
853ebfedea0SLionel Sambuc         RSA_free(rtmp);
854ebfedea0SLionel Sambuc #  endif
855ebfedea0SLionel Sambuc     return NULL;
856ebfedea0SLionel Sambuc }
857ebfedea0SLionel Sambuc 
hwcrhk_load_pubkey(ENGINE * eng,const char * key_id,UI_METHOD * ui_method,void * callback_data)858ebfedea0SLionel Sambuc static EVP_PKEY *hwcrhk_load_pubkey(ENGINE *eng, const char *key_id,
859ebfedea0SLionel Sambuc                                     UI_METHOD *ui_method, void *callback_data)
860ebfedea0SLionel Sambuc {
861ebfedea0SLionel Sambuc     EVP_PKEY *res = NULL;
862ebfedea0SLionel Sambuc 
863ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
864*0a6a1f1dSLionel Sambuc     res = hwcrhk_load_privkey(eng, key_id, ui_method, callback_data);
865ebfedea0SLionel Sambuc #  endif
866ebfedea0SLionel Sambuc 
867ebfedea0SLionel Sambuc     if (res)
868*0a6a1f1dSLionel Sambuc         switch (res->type) {
869ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
870ebfedea0SLionel Sambuc         case EVP_PKEY_RSA:
871ebfedea0SLionel Sambuc             {
872ebfedea0SLionel Sambuc                 RSA *rsa = NULL;
873ebfedea0SLionel Sambuc 
874ebfedea0SLionel Sambuc                 CRYPTO_w_lock(CRYPTO_LOCK_EVP_PKEY);
875ebfedea0SLionel Sambuc                 rsa = res->pkey.rsa;
876ebfedea0SLionel Sambuc                 res->pkey.rsa = RSA_new();
877ebfedea0SLionel Sambuc                 res->pkey.rsa->n = rsa->n;
878ebfedea0SLionel Sambuc                 res->pkey.rsa->e = rsa->e;
879ebfedea0SLionel Sambuc                 rsa->n = NULL;
880ebfedea0SLionel Sambuc                 rsa->e = NULL;
881ebfedea0SLionel Sambuc                 CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
882ebfedea0SLionel Sambuc                 RSA_free(rsa);
883ebfedea0SLionel Sambuc             }
884ebfedea0SLionel Sambuc             break;
885ebfedea0SLionel Sambuc #  endif
886ebfedea0SLionel Sambuc         default:
887ebfedea0SLionel Sambuc             HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PUBKEY,
888ebfedea0SLionel Sambuc                       HWCRHK_R_CTRL_COMMAND_NOT_IMPLEMENTED);
889ebfedea0SLionel Sambuc             goto err;
890ebfedea0SLionel Sambuc         }
891ebfedea0SLionel Sambuc 
892ebfedea0SLionel Sambuc     return res;
893ebfedea0SLionel Sambuc  err:
894ebfedea0SLionel Sambuc     if (res)
895ebfedea0SLionel Sambuc         EVP_PKEY_free(res);
896ebfedea0SLionel Sambuc     return NULL;
897ebfedea0SLionel Sambuc }
898ebfedea0SLionel Sambuc 
899ebfedea0SLionel Sambuc /* A little mod_exp */
hwcrhk_mod_exp(BIGNUM * r,const BIGNUM * a,const BIGNUM * p,const BIGNUM * m,BN_CTX * ctx)900ebfedea0SLionel Sambuc static int hwcrhk_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
901ebfedea0SLionel Sambuc                           const BIGNUM *m, BN_CTX *ctx)
902ebfedea0SLionel Sambuc {
903ebfedea0SLionel Sambuc     char tempbuf[1024];
904ebfedea0SLionel Sambuc     HWCryptoHook_ErrMsgBuf rmsg;
905*0a6a1f1dSLionel Sambuc     /*
906*0a6a1f1dSLionel Sambuc      * Since HWCryptoHook_MPI is pretty compatible with BIGNUM's, we use them
907*0a6a1f1dSLionel Sambuc      * directly, plus a little macro magic.  We only thing we need to make
908*0a6a1f1dSLionel Sambuc      * sure of is that enough space is allocated.
909*0a6a1f1dSLionel Sambuc      */
910ebfedea0SLionel Sambuc     HWCryptoHook_MPI m_a, m_p, m_n, m_r;
911ebfedea0SLionel Sambuc     int to_return, ret;
912ebfedea0SLionel Sambuc 
913ebfedea0SLionel Sambuc     to_return = 0;              /* expect failure */
914ebfedea0SLionel Sambuc     rmsg.buf = tempbuf;
915ebfedea0SLionel Sambuc     rmsg.size = sizeof(tempbuf);
916ebfedea0SLionel Sambuc 
917*0a6a1f1dSLionel Sambuc     if (!hwcrhk_context) {
918ebfedea0SLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_MOD_EXP, HWCRHK_R_NOT_INITIALISED);
919ebfedea0SLionel Sambuc         goto err;
920ebfedea0SLionel Sambuc     }
921ebfedea0SLionel Sambuc     /* Prepare the params */
922ebfedea0SLionel Sambuc     bn_expand2(r, m->top);      /* Check for error !! */
923ebfedea0SLionel Sambuc     BN2MPI(m_a, a);
924ebfedea0SLionel Sambuc     BN2MPI(m_p, p);
925ebfedea0SLionel Sambuc     BN2MPI(m_n, m);
926ebfedea0SLionel Sambuc     MPI2BN(r, m_r);
927ebfedea0SLionel Sambuc 
928ebfedea0SLionel Sambuc     /* Perform the operation */
929ebfedea0SLionel Sambuc     ret = p_hwcrhk_ModExp(hwcrhk_context, m_a, m_p, m_n, &m_r, &rmsg);
930ebfedea0SLionel Sambuc 
931ebfedea0SLionel Sambuc     /* Convert the response */
932ebfedea0SLionel Sambuc     r->top = m_r.size / sizeof(BN_ULONG);
933ebfedea0SLionel Sambuc     bn_fix_top(r);
934ebfedea0SLionel Sambuc 
935*0a6a1f1dSLionel Sambuc     if (ret < 0) {
936*0a6a1f1dSLionel Sambuc         /*
937*0a6a1f1dSLionel Sambuc          * FIXME: When this error is returned, HWCryptoHook is telling us
938*0a6a1f1dSLionel Sambuc          * that falling back to software computation might be a good thing.
939*0a6a1f1dSLionel Sambuc          */
940*0a6a1f1dSLionel Sambuc         if (ret == HWCRYPTOHOOK_ERROR_FALLBACK) {
941ebfedea0SLionel Sambuc             HWCRHKerr(HWCRHK_F_HWCRHK_MOD_EXP, HWCRHK_R_REQUEST_FALLBACK);
942*0a6a1f1dSLionel Sambuc         } else {
943ebfedea0SLionel Sambuc             HWCRHKerr(HWCRHK_F_HWCRHK_MOD_EXP, HWCRHK_R_REQUEST_FAILED);
944ebfedea0SLionel Sambuc         }
945ebfedea0SLionel Sambuc         ERR_add_error_data(1, rmsg.buf);
946ebfedea0SLionel Sambuc         goto err;
947ebfedea0SLionel Sambuc     }
948ebfedea0SLionel Sambuc 
949ebfedea0SLionel Sambuc     to_return = 1;
950ebfedea0SLionel Sambuc  err:
951ebfedea0SLionel Sambuc     return to_return;
952ebfedea0SLionel Sambuc }
953ebfedea0SLionel Sambuc 
954ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
hwcrhk_rsa_mod_exp(BIGNUM * r,const BIGNUM * I,RSA * rsa,BN_CTX * ctx)955*0a6a1f1dSLionel Sambuc static int hwcrhk_rsa_mod_exp(BIGNUM *r, const BIGNUM *I, RSA *rsa,
956*0a6a1f1dSLionel Sambuc                               BN_CTX *ctx)
957ebfedea0SLionel Sambuc {
958ebfedea0SLionel Sambuc     char tempbuf[1024];
959ebfedea0SLionel Sambuc     HWCryptoHook_ErrMsgBuf rmsg;
960ebfedea0SLionel Sambuc     HWCryptoHook_RSAKeyHandle *hptr;
961ebfedea0SLionel Sambuc     int to_return = 0, ret;
962ebfedea0SLionel Sambuc 
963ebfedea0SLionel Sambuc     rmsg.buf = tempbuf;
964ebfedea0SLionel Sambuc     rmsg.size = sizeof(tempbuf);
965ebfedea0SLionel Sambuc 
966*0a6a1f1dSLionel Sambuc     if (!hwcrhk_context) {
967ebfedea0SLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_RSA_MOD_EXP, HWCRHK_R_NOT_INITIALISED);
968ebfedea0SLionel Sambuc         goto err;
969ebfedea0SLionel Sambuc     }
970ebfedea0SLionel Sambuc 
971*0a6a1f1dSLionel Sambuc     /*
972*0a6a1f1dSLionel Sambuc      * This provides support for nForce keys.  Since that's opaque data all
973*0a6a1f1dSLionel Sambuc      * we do is provide a handle to the proper key and let HWCryptoHook take
974*0a6a1f1dSLionel Sambuc      * care of the rest.
975*0a6a1f1dSLionel Sambuc      */
976*0a6a1f1dSLionel Sambuc     if ((hptr =
977*0a6a1f1dSLionel Sambuc          (HWCryptoHook_RSAKeyHandle *) RSA_get_ex_data(rsa, hndidx_rsa))
978*0a6a1f1dSLionel Sambuc         != NULL) {
979ebfedea0SLionel Sambuc         HWCryptoHook_MPI m_a, m_r;
980ebfedea0SLionel Sambuc 
981*0a6a1f1dSLionel Sambuc         if (!rsa->n) {
982ebfedea0SLionel Sambuc             HWCRHKerr(HWCRHK_F_HWCRHK_RSA_MOD_EXP,
983ebfedea0SLionel Sambuc                       HWCRHK_R_MISSING_KEY_COMPONENTS);
984ebfedea0SLionel Sambuc             goto err;
985ebfedea0SLionel Sambuc         }
986ebfedea0SLionel Sambuc 
987ebfedea0SLionel Sambuc         /* Prepare the params */
988ebfedea0SLionel Sambuc         bn_expand2(r, rsa->n->top); /* Check for error !! */
989ebfedea0SLionel Sambuc         BN2MPI(m_a, I);
990ebfedea0SLionel Sambuc         MPI2BN(r, m_r);
991ebfedea0SLionel Sambuc 
992ebfedea0SLionel Sambuc         /* Perform the operation */
993ebfedea0SLionel Sambuc         ret = p_hwcrhk_RSA(m_a, *hptr, &m_r, &rmsg);
994ebfedea0SLionel Sambuc 
995ebfedea0SLionel Sambuc         /* Convert the response */
996ebfedea0SLionel Sambuc         r->top = m_r.size / sizeof(BN_ULONG);
997ebfedea0SLionel Sambuc         bn_fix_top(r);
998ebfedea0SLionel Sambuc 
999*0a6a1f1dSLionel Sambuc         if (ret < 0) {
1000*0a6a1f1dSLionel Sambuc             /*
1001*0a6a1f1dSLionel Sambuc              * FIXME: When this error is returned, HWCryptoHook is telling us
1002*0a6a1f1dSLionel Sambuc              * that falling back to software computation might be a good
1003*0a6a1f1dSLionel Sambuc              * thing.
1004*0a6a1f1dSLionel Sambuc              */
1005*0a6a1f1dSLionel Sambuc             if (ret == HWCRYPTOHOOK_ERROR_FALLBACK) {
1006ebfedea0SLionel Sambuc                 HWCRHKerr(HWCRHK_F_HWCRHK_RSA_MOD_EXP,
1007ebfedea0SLionel Sambuc                           HWCRHK_R_REQUEST_FALLBACK);
1008*0a6a1f1dSLionel Sambuc             } else {
1009ebfedea0SLionel Sambuc                 HWCRHKerr(HWCRHK_F_HWCRHK_RSA_MOD_EXP,
1010ebfedea0SLionel Sambuc                           HWCRHK_R_REQUEST_FAILED);
1011ebfedea0SLionel Sambuc             }
1012ebfedea0SLionel Sambuc             ERR_add_error_data(1, rmsg.buf);
1013ebfedea0SLionel Sambuc             goto err;
1014ebfedea0SLionel Sambuc         }
1015*0a6a1f1dSLionel Sambuc     } else {
1016ebfedea0SLionel Sambuc         HWCryptoHook_MPI m_a, m_p, m_q, m_dmp1, m_dmq1, m_iqmp, m_r;
1017ebfedea0SLionel Sambuc 
1018*0a6a1f1dSLionel Sambuc         if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
1019ebfedea0SLionel Sambuc             HWCRHKerr(HWCRHK_F_HWCRHK_RSA_MOD_EXP,
1020ebfedea0SLionel Sambuc                       HWCRHK_R_MISSING_KEY_COMPONENTS);
1021ebfedea0SLionel Sambuc             goto err;
1022ebfedea0SLionel Sambuc         }
1023ebfedea0SLionel Sambuc 
1024ebfedea0SLionel Sambuc         /* Prepare the params */
1025ebfedea0SLionel Sambuc         bn_expand2(r, rsa->n->top); /* Check for error !! */
1026ebfedea0SLionel Sambuc         BN2MPI(m_a, I);
1027ebfedea0SLionel Sambuc         BN2MPI(m_p, rsa->p);
1028ebfedea0SLionel Sambuc         BN2MPI(m_q, rsa->q);
1029ebfedea0SLionel Sambuc         BN2MPI(m_dmp1, rsa->dmp1);
1030ebfedea0SLionel Sambuc         BN2MPI(m_dmq1, rsa->dmq1);
1031ebfedea0SLionel Sambuc         BN2MPI(m_iqmp, rsa->iqmp);
1032ebfedea0SLionel Sambuc         MPI2BN(r, m_r);
1033ebfedea0SLionel Sambuc 
1034ebfedea0SLionel Sambuc         /* Perform the operation */
1035ebfedea0SLionel Sambuc         ret = p_hwcrhk_ModExpCRT(hwcrhk_context, m_a, m_p, m_q,
1036ebfedea0SLionel Sambuc                                  m_dmp1, m_dmq1, m_iqmp, &m_r, &rmsg);
1037ebfedea0SLionel Sambuc 
1038ebfedea0SLionel Sambuc         /* Convert the response */
1039ebfedea0SLionel Sambuc         r->top = m_r.size / sizeof(BN_ULONG);
1040ebfedea0SLionel Sambuc         bn_fix_top(r);
1041ebfedea0SLionel Sambuc 
1042*0a6a1f1dSLionel Sambuc         if (ret < 0) {
1043*0a6a1f1dSLionel Sambuc             /*
1044*0a6a1f1dSLionel Sambuc              * FIXME: When this error is returned, HWCryptoHook is telling us
1045*0a6a1f1dSLionel Sambuc              * that falling back to software computation might be a good
1046*0a6a1f1dSLionel Sambuc              * thing.
1047*0a6a1f1dSLionel Sambuc              */
1048*0a6a1f1dSLionel Sambuc             if (ret == HWCRYPTOHOOK_ERROR_FALLBACK) {
1049ebfedea0SLionel Sambuc                 HWCRHKerr(HWCRHK_F_HWCRHK_RSA_MOD_EXP,
1050ebfedea0SLionel Sambuc                           HWCRHK_R_REQUEST_FALLBACK);
1051*0a6a1f1dSLionel Sambuc             } else {
1052ebfedea0SLionel Sambuc                 HWCRHKerr(HWCRHK_F_HWCRHK_RSA_MOD_EXP,
1053ebfedea0SLionel Sambuc                           HWCRHK_R_REQUEST_FAILED);
1054ebfedea0SLionel Sambuc             }
1055ebfedea0SLionel Sambuc             ERR_add_error_data(1, rmsg.buf);
1056ebfedea0SLionel Sambuc             goto err;
1057ebfedea0SLionel Sambuc         }
1058ebfedea0SLionel Sambuc     }
1059*0a6a1f1dSLionel Sambuc     /*
1060*0a6a1f1dSLionel Sambuc      * If we're here, we must be here with some semblance of success :-)
1061*0a6a1f1dSLionel Sambuc      */
1062ebfedea0SLionel Sambuc     to_return = 1;
1063ebfedea0SLionel Sambuc  err:
1064ebfedea0SLionel Sambuc     return to_return;
1065ebfedea0SLionel Sambuc }
1066ebfedea0SLionel Sambuc #  endif
1067ebfedea0SLionel Sambuc 
1068ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_RSA
1069ebfedea0SLionel Sambuc /* This function is aliased to mod_exp (with the mont stuff dropped). */
hwcrhk_mod_exp_mont(BIGNUM * r,const BIGNUM * a,const BIGNUM * p,const BIGNUM * m,BN_CTX * ctx,BN_MONT_CTX * m_ctx)1070ebfedea0SLionel Sambuc static int hwcrhk_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1071*0a6a1f1dSLionel Sambuc                                const BIGNUM *m, BN_CTX *ctx,
1072*0a6a1f1dSLionel Sambuc                                BN_MONT_CTX *m_ctx)
1073ebfedea0SLionel Sambuc {
1074ebfedea0SLionel Sambuc     return hwcrhk_mod_exp(r, a, p, m, ctx);
1075ebfedea0SLionel Sambuc }
1076ebfedea0SLionel Sambuc 
hwcrhk_rsa_finish(RSA * rsa)1077ebfedea0SLionel Sambuc static int hwcrhk_rsa_finish(RSA *rsa)
1078ebfedea0SLionel Sambuc {
1079ebfedea0SLionel Sambuc     HWCryptoHook_RSAKeyHandle *hptr;
1080ebfedea0SLionel Sambuc 
1081ebfedea0SLionel Sambuc     hptr = RSA_get_ex_data(rsa, hndidx_rsa);
1082*0a6a1f1dSLionel Sambuc     if (hptr) {
1083ebfedea0SLionel Sambuc         p_hwcrhk_RSAUnloadKey(*hptr, NULL);
1084ebfedea0SLionel Sambuc         OPENSSL_free(hptr);
1085ebfedea0SLionel Sambuc         RSA_set_ex_data(rsa, hndidx_rsa, NULL);
1086ebfedea0SLionel Sambuc     }
1087ebfedea0SLionel Sambuc     return 1;
1088ebfedea0SLionel Sambuc }
1089ebfedea0SLionel Sambuc 
1090ebfedea0SLionel Sambuc #  endif
1091ebfedea0SLionel Sambuc 
1092ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_DH
1093ebfedea0SLionel Sambuc /* This function is aliased to mod_exp (with the dh and mont dropped). */
hwcrhk_mod_exp_dh(const DH * dh,BIGNUM * r,const BIGNUM * a,const BIGNUM * p,const BIGNUM * m,BN_CTX * ctx,BN_MONT_CTX * m_ctx)1094ebfedea0SLionel Sambuc static int hwcrhk_mod_exp_dh(const DH *dh, BIGNUM *r,
1095ebfedea0SLionel Sambuc                              const BIGNUM *a, const BIGNUM *p,
1096ebfedea0SLionel Sambuc                              const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1097ebfedea0SLionel Sambuc {
1098ebfedea0SLionel Sambuc     return hwcrhk_mod_exp(r, a, p, m, ctx);
1099ebfedea0SLionel Sambuc }
1100ebfedea0SLionel Sambuc #  endif
1101ebfedea0SLionel Sambuc 
1102ebfedea0SLionel Sambuc /* Random bytes are good */
hwcrhk_rand_bytes(unsigned char * buf,int num)1103ebfedea0SLionel Sambuc static int hwcrhk_rand_bytes(unsigned char *buf, int num)
1104ebfedea0SLionel Sambuc {
1105ebfedea0SLionel Sambuc     char tempbuf[1024];
1106ebfedea0SLionel Sambuc     HWCryptoHook_ErrMsgBuf rmsg;
1107ebfedea0SLionel Sambuc     int to_return = 0;          /* assume failure */
1108ebfedea0SLionel Sambuc     int ret;
1109ebfedea0SLionel Sambuc 
1110ebfedea0SLionel Sambuc     rmsg.buf = tempbuf;
1111ebfedea0SLionel Sambuc     rmsg.size = sizeof(tempbuf);
1112ebfedea0SLionel Sambuc 
1113*0a6a1f1dSLionel Sambuc     if (!hwcrhk_context) {
1114ebfedea0SLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_RAND_BYTES, HWCRHK_R_NOT_INITIALISED);
1115ebfedea0SLionel Sambuc         goto err;
1116ebfedea0SLionel Sambuc     }
1117ebfedea0SLionel Sambuc 
1118ebfedea0SLionel Sambuc     ret = p_hwcrhk_RandomBytes(hwcrhk_context, buf, num, &rmsg);
1119*0a6a1f1dSLionel Sambuc     if (ret < 0) {
1120*0a6a1f1dSLionel Sambuc         /*
1121*0a6a1f1dSLionel Sambuc          * FIXME: When this error is returned, HWCryptoHook is telling us
1122*0a6a1f1dSLionel Sambuc          * that falling back to software computation might be a good thing.
1123*0a6a1f1dSLionel Sambuc          */
1124*0a6a1f1dSLionel Sambuc         if (ret == HWCRYPTOHOOK_ERROR_FALLBACK) {
1125*0a6a1f1dSLionel Sambuc             HWCRHKerr(HWCRHK_F_HWCRHK_RAND_BYTES, HWCRHK_R_REQUEST_FALLBACK);
1126*0a6a1f1dSLionel Sambuc         } else {
1127*0a6a1f1dSLionel Sambuc             HWCRHKerr(HWCRHK_F_HWCRHK_RAND_BYTES, HWCRHK_R_REQUEST_FAILED);
1128ebfedea0SLionel Sambuc         }
1129ebfedea0SLionel Sambuc         ERR_add_error_data(1, rmsg.buf);
1130ebfedea0SLionel Sambuc         goto err;
1131ebfedea0SLionel Sambuc     }
1132ebfedea0SLionel Sambuc     to_return = 1;
1133ebfedea0SLionel Sambuc  err:
1134ebfedea0SLionel Sambuc     return to_return;
1135ebfedea0SLionel Sambuc }
1136ebfedea0SLionel Sambuc 
hwcrhk_rand_status(void)1137ebfedea0SLionel Sambuc static int hwcrhk_rand_status(void)
1138ebfedea0SLionel Sambuc {
1139ebfedea0SLionel Sambuc     return 1;
1140ebfedea0SLionel Sambuc }
1141ebfedea0SLionel Sambuc 
1142*0a6a1f1dSLionel Sambuc /*
1143*0a6a1f1dSLionel Sambuc  * Mutex calls: since the HWCryptoHook model closely follows the POSIX model
1144ebfedea0SLionel Sambuc  * these just wrap the POSIX functions and add some logging.
1145ebfedea0SLionel Sambuc  */
1146ebfedea0SLionel Sambuc 
hwcrhk_mutex_init(HWCryptoHook_Mutex * mt,HWCryptoHook_CallerContext * cactx)1147ebfedea0SLionel Sambuc static int hwcrhk_mutex_init(HWCryptoHook_Mutex * mt,
1148ebfedea0SLionel Sambuc                              HWCryptoHook_CallerContext * cactx)
1149ebfedea0SLionel Sambuc {
1150ebfedea0SLionel Sambuc     mt->lockid = CRYPTO_get_new_dynlockid();
1151ebfedea0SLionel Sambuc     if (mt->lockid == 0)
1152ebfedea0SLionel Sambuc         return 1;               /* failure */
1153ebfedea0SLionel Sambuc     return 0;                   /* success */
1154ebfedea0SLionel Sambuc }
1155ebfedea0SLionel Sambuc 
hwcrhk_mutex_lock(HWCryptoHook_Mutex * mt)1156ebfedea0SLionel Sambuc static int hwcrhk_mutex_lock(HWCryptoHook_Mutex * mt)
1157ebfedea0SLionel Sambuc {
1158ebfedea0SLionel Sambuc     CRYPTO_w_lock(mt->lockid);
1159ebfedea0SLionel Sambuc     return 0;
1160ebfedea0SLionel Sambuc }
1161ebfedea0SLionel Sambuc 
hwcrhk_mutex_unlock(HWCryptoHook_Mutex * mt)1162ebfedea0SLionel Sambuc static void hwcrhk_mutex_unlock(HWCryptoHook_Mutex * mt)
1163ebfedea0SLionel Sambuc {
1164ebfedea0SLionel Sambuc     CRYPTO_w_unlock(mt->lockid);
1165ebfedea0SLionel Sambuc }
1166ebfedea0SLionel Sambuc 
hwcrhk_mutex_destroy(HWCryptoHook_Mutex * mt)1167ebfedea0SLionel Sambuc static void hwcrhk_mutex_destroy(HWCryptoHook_Mutex * mt)
1168ebfedea0SLionel Sambuc {
1169ebfedea0SLionel Sambuc     CRYPTO_destroy_dynlockid(mt->lockid);
1170ebfedea0SLionel Sambuc }
1171ebfedea0SLionel Sambuc 
hwcrhk_get_pass(const char * prompt_info,int * len_io,char * buf,HWCryptoHook_PassphraseContext * ppctx,HWCryptoHook_CallerContext * cactx)1172ebfedea0SLionel Sambuc static int hwcrhk_get_pass(const char *prompt_info,
1173ebfedea0SLionel Sambuc                            int *len_io, char *buf,
1174ebfedea0SLionel Sambuc                            HWCryptoHook_PassphraseContext * ppctx,
1175ebfedea0SLionel Sambuc                            HWCryptoHook_CallerContext * cactx)
1176ebfedea0SLionel Sambuc {
1177ebfedea0SLionel Sambuc     pem_password_cb *callback = NULL;
1178ebfedea0SLionel Sambuc     void *callback_data = NULL;
1179ebfedea0SLionel Sambuc     UI_METHOD *ui_method = NULL;
1180*0a6a1f1dSLionel Sambuc     /*
1181*0a6a1f1dSLionel Sambuc      * Despite what the documentation says prompt_info can be an empty
1182*0a6a1f1dSLionel Sambuc      * string.
1183ebfedea0SLionel Sambuc      */
1184ebfedea0SLionel Sambuc     if (prompt_info && !*prompt_info)
1185ebfedea0SLionel Sambuc         prompt_info = NULL;
1186ebfedea0SLionel Sambuc 
1187*0a6a1f1dSLionel Sambuc     if (cactx) {
1188ebfedea0SLionel Sambuc         if (cactx->ui_method)
1189ebfedea0SLionel Sambuc             ui_method = cactx->ui_method;
1190ebfedea0SLionel Sambuc         if (cactx->password_callback)
1191ebfedea0SLionel Sambuc             callback = cactx->password_callback;
1192ebfedea0SLionel Sambuc         if (cactx->callback_data)
1193ebfedea0SLionel Sambuc             callback_data = cactx->callback_data;
1194ebfedea0SLionel Sambuc     }
1195*0a6a1f1dSLionel Sambuc     if (ppctx) {
1196*0a6a1f1dSLionel Sambuc         if (ppctx->ui_method) {
1197ebfedea0SLionel Sambuc             ui_method = ppctx->ui_method;
1198ebfedea0SLionel Sambuc             callback = NULL;
1199ebfedea0SLionel Sambuc         }
1200ebfedea0SLionel Sambuc         if (ppctx->callback_data)
1201ebfedea0SLionel Sambuc             callback_data = ppctx->callback_data;
1202ebfedea0SLionel Sambuc     }
1203*0a6a1f1dSLionel Sambuc     if (callback == NULL && ui_method == NULL) {
1204ebfedea0SLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_GET_PASS, HWCRHK_R_NO_CALLBACK);
1205ebfedea0SLionel Sambuc         return -1;
1206ebfedea0SLionel Sambuc     }
1207ebfedea0SLionel Sambuc 
1208*0a6a1f1dSLionel Sambuc     if (ui_method) {
1209ebfedea0SLionel Sambuc         UI *ui = UI_new_method(ui_method);
1210*0a6a1f1dSLionel Sambuc         if (ui) {
1211ebfedea0SLionel Sambuc             int ok;
1212ebfedea0SLionel Sambuc             char *prompt = UI_construct_prompt(ui,
1213ebfedea0SLionel Sambuc                                                "pass phrase", prompt_info);
1214ebfedea0SLionel Sambuc 
1215ebfedea0SLionel Sambuc             ok = UI_add_input_string(ui, prompt,
1216ebfedea0SLionel Sambuc                                      UI_INPUT_FLAG_DEFAULT_PWD,
1217ebfedea0SLionel Sambuc                                      buf, 0, (*len_io) - 1);
1218ebfedea0SLionel Sambuc             UI_add_user_data(ui, callback_data);
1219ebfedea0SLionel Sambuc             UI_ctrl(ui, UI_CTRL_PRINT_ERRORS, 1, 0, 0);
1220ebfedea0SLionel Sambuc 
1221ebfedea0SLionel Sambuc             if (ok >= 0)
1222*0a6a1f1dSLionel Sambuc                 do {
1223ebfedea0SLionel Sambuc                     ok = UI_process(ui);
1224ebfedea0SLionel Sambuc                 }
1225ebfedea0SLionel Sambuc                 while (ok < 0 && UI_ctrl(ui, UI_CTRL_IS_REDOABLE, 0, 0, 0));
1226ebfedea0SLionel Sambuc 
1227ebfedea0SLionel Sambuc             if (ok >= 0)
1228ebfedea0SLionel Sambuc                 *len_io = strlen(buf);
1229ebfedea0SLionel Sambuc 
1230ebfedea0SLionel Sambuc             UI_free(ui);
1231ebfedea0SLionel Sambuc             OPENSSL_free(prompt);
1232ebfedea0SLionel Sambuc         }
1233*0a6a1f1dSLionel Sambuc     } else {
1234ebfedea0SLionel Sambuc         *len_io = callback(buf, *len_io, 0, callback_data);
1235ebfedea0SLionel Sambuc     }
1236ebfedea0SLionel Sambuc     if (!*len_io)
1237ebfedea0SLionel Sambuc         return -1;
1238ebfedea0SLionel Sambuc     return 0;
1239ebfedea0SLionel Sambuc }
1240ebfedea0SLionel Sambuc 
hwcrhk_insert_card(const char * prompt_info,const char * wrong_info,HWCryptoHook_PassphraseContext * ppctx,HWCryptoHook_CallerContext * cactx)1241ebfedea0SLionel Sambuc static int hwcrhk_insert_card(const char *prompt_info,
1242ebfedea0SLionel Sambuc                               const char *wrong_info,
1243ebfedea0SLionel Sambuc                               HWCryptoHook_PassphraseContext * ppctx,
1244ebfedea0SLionel Sambuc                               HWCryptoHook_CallerContext * cactx)
1245ebfedea0SLionel Sambuc {
1246ebfedea0SLionel Sambuc     int ok = -1;
1247ebfedea0SLionel Sambuc     UI *ui;
1248ebfedea0SLionel Sambuc     void *callback_data = NULL;
1249ebfedea0SLionel Sambuc     UI_METHOD *ui_method = NULL;
1250ebfedea0SLionel Sambuc 
1251*0a6a1f1dSLionel Sambuc     if (cactx) {
1252ebfedea0SLionel Sambuc         if (cactx->ui_method)
1253ebfedea0SLionel Sambuc             ui_method = cactx->ui_method;
1254ebfedea0SLionel Sambuc         if (cactx->callback_data)
1255ebfedea0SLionel Sambuc             callback_data = cactx->callback_data;
1256ebfedea0SLionel Sambuc     }
1257*0a6a1f1dSLionel Sambuc     if (ppctx) {
1258ebfedea0SLionel Sambuc         if (ppctx->ui_method)
1259ebfedea0SLionel Sambuc             ui_method = ppctx->ui_method;
1260ebfedea0SLionel Sambuc         if (ppctx->callback_data)
1261ebfedea0SLionel Sambuc             callback_data = ppctx->callback_data;
1262ebfedea0SLionel Sambuc     }
1263*0a6a1f1dSLionel Sambuc     if (ui_method == NULL) {
1264*0a6a1f1dSLionel Sambuc         HWCRHKerr(HWCRHK_F_HWCRHK_INSERT_CARD, HWCRHK_R_NO_CALLBACK);
1265ebfedea0SLionel Sambuc         return -1;
1266ebfedea0SLionel Sambuc     }
1267ebfedea0SLionel Sambuc 
1268ebfedea0SLionel Sambuc     ui = UI_new_method(ui_method);
1269ebfedea0SLionel Sambuc 
1270*0a6a1f1dSLionel Sambuc     if (ui) {
1271ebfedea0SLionel Sambuc         char answer;
1272ebfedea0SLionel Sambuc         char buf[BUFSIZ];
1273*0a6a1f1dSLionel Sambuc         /*
1274*0a6a1f1dSLionel Sambuc          * Despite what the documentation says wrong_info can be an empty
1275*0a6a1f1dSLionel Sambuc          * string.
1276ebfedea0SLionel Sambuc          */
1277ebfedea0SLionel Sambuc         if (wrong_info && *wrong_info)
1278ebfedea0SLionel Sambuc             BIO_snprintf(buf, sizeof(buf) - 1,
1279ebfedea0SLionel Sambuc                          "Current card: \"%s\"\n", wrong_info);
1280ebfedea0SLionel Sambuc         else
1281ebfedea0SLionel Sambuc             buf[0] = 0;
1282ebfedea0SLionel Sambuc         ok = UI_dup_info_string(ui, buf);
1283*0a6a1f1dSLionel Sambuc         if (ok >= 0 && prompt_info) {
1284ebfedea0SLionel Sambuc             BIO_snprintf(buf, sizeof(buf) - 1,
1285ebfedea0SLionel Sambuc                          "Insert card \"%s\"", prompt_info);
1286ebfedea0SLionel Sambuc             ok = UI_dup_input_boolean(ui, buf,
1287ebfedea0SLionel Sambuc                                       "\n then hit <enter> or C<enter> to cancel\n",
1288*0a6a1f1dSLionel Sambuc                                       "\r\n", "Cc", UI_INPUT_FLAG_ECHO,
1289*0a6a1f1dSLionel Sambuc                                       &answer);
1290ebfedea0SLionel Sambuc         }
1291ebfedea0SLionel Sambuc         UI_add_user_data(ui, callback_data);
1292ebfedea0SLionel Sambuc 
1293ebfedea0SLionel Sambuc         if (ok >= 0)
1294ebfedea0SLionel Sambuc             ok = UI_process(ui);
1295ebfedea0SLionel Sambuc         UI_free(ui);
1296ebfedea0SLionel Sambuc 
1297ebfedea0SLionel Sambuc         if (ok == -2 || (ok >= 0 && answer == 'C'))
1298ebfedea0SLionel Sambuc             ok = 1;
1299ebfedea0SLionel Sambuc         else if (ok < 0)
1300ebfedea0SLionel Sambuc             ok = -1;
1301ebfedea0SLionel Sambuc         else
1302ebfedea0SLionel Sambuc             ok = 0;
1303ebfedea0SLionel Sambuc     }
1304ebfedea0SLionel Sambuc     return ok;
1305ebfedea0SLionel Sambuc }
1306ebfedea0SLionel Sambuc 
hwcrhk_log_message(void * logstr,const char * message)1307ebfedea0SLionel Sambuc static void hwcrhk_log_message(void *logstr, const char *message)
1308ebfedea0SLionel Sambuc {
1309ebfedea0SLionel Sambuc     BIO *lstream = NULL;
1310ebfedea0SLionel Sambuc 
1311ebfedea0SLionel Sambuc     CRYPTO_w_lock(CRYPTO_LOCK_BIO);
1312ebfedea0SLionel Sambuc     if (logstr)
1313ebfedea0SLionel Sambuc         lstream = *(BIO **)logstr;
1314*0a6a1f1dSLionel Sambuc     if (lstream) {
1315ebfedea0SLionel Sambuc         BIO_printf(lstream, "%s\n", message);
1316ebfedea0SLionel Sambuc     }
1317ebfedea0SLionel Sambuc     CRYPTO_w_unlock(CRYPTO_LOCK_BIO);
1318ebfedea0SLionel Sambuc }
1319ebfedea0SLionel Sambuc 
1320*0a6a1f1dSLionel Sambuc /*
1321*0a6a1f1dSLionel Sambuc  * This stuff is needed if this ENGINE is being compiled into a
1322*0a6a1f1dSLionel Sambuc  * self-contained shared-library.
1323*0a6a1f1dSLionel Sambuc  */
1324ebfedea0SLionel Sambuc #  ifndef OPENSSL_NO_DYNAMIC_ENGINE
bind_fn(ENGINE * e,const char * id)1325ebfedea0SLionel Sambuc static int bind_fn(ENGINE *e, const char *id)
1326ebfedea0SLionel Sambuc {
1327ebfedea0SLionel Sambuc     if (id && (strcmp(id, engine_hwcrhk_id) != 0) &&
1328ebfedea0SLionel Sambuc         (strcmp(id, engine_hwcrhk_id_alt) != 0))
1329ebfedea0SLionel Sambuc         return 0;
1330ebfedea0SLionel Sambuc     if (!bind_helper(e))
1331ebfedea0SLionel Sambuc         return 0;
1332ebfedea0SLionel Sambuc     return 1;
1333ebfedea0SLionel Sambuc }
1334*0a6a1f1dSLionel Sambuc 
1335ebfedea0SLionel Sambuc IMPLEMENT_DYNAMIC_CHECK_FN()
1336ebfedea0SLionel Sambuc     IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
1337ebfedea0SLionel Sambuc #  endif                        /* OPENSSL_NO_DYNAMIC_ENGINE */
1338ebfedea0SLionel Sambuc # endif                         /* !OPENSSL_NO_HW_CHIL */
1339ebfedea0SLionel Sambuc #endif                          /* !OPENSSL_NO_HW */
1340