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