1296465Sdelphij/*-
2296465Sdelphij*  Written by Corinne Dive-Reclus(cdive@baltimore.com)
3160814Ssimon*
4296465Sdelphij*
5160814Ssimon* Redistribution and use in source and binary forms, with or without
6160814Ssimon* modification, are permitted provided that the following conditions
7160814Ssimon* are met:
8160814Ssimon*
9160814Ssimon* 1. Redistributions of source code must retain the above copyright
10296465Sdelphij*    notice, this list of conditions and the following disclaimer.
11160814Ssimon*
12160814Ssimon* 2. Redistributions in binary form must reproduce the above copyright
13160814Ssimon*    notice, this list of conditions and the following disclaimer in
14160814Ssimon*    the documentation and/or other materials provided with the
15160814Ssimon*    distribution.
16160814Ssimon*
17160814Ssimon* 3. All advertising materials mentioning features or use of this
18160814Ssimon*    software must display the following acknowledgment:
19160814Ssimon*    "This product includes software developed by the OpenSSL Project
20160814Ssimon*    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21160814Ssimon*
22160814Ssimon* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23160814Ssimon*    endorse or promote products derived from this software without
24160814Ssimon*    prior written permission. For written permission, please contact
25160814Ssimon*    licensing@OpenSSL.org.
26160814Ssimon*
27160814Ssimon* 5. Products derived from this software may not be called "OpenSSL"
28160814Ssimon*    nor may "OpenSSL" appear in their names without prior written
29160814Ssimon*    permission of the OpenSSL Project.
30160814Ssimon*
31160814Ssimon* 6. Redistributions of any form whatsoever must retain the following
32160814Ssimon*    acknowledgment:
33160814Ssimon*    "This product includes software developed by the OpenSSL Project
34160814Ssimon*    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35160814Ssimon*
36160814Ssimon* Written by Corinne Dive-Reclus(cdive@baltimore.com)
37160814Ssimon*
38160814Ssimon* Copyright@2001 Baltimore Technologies Ltd.
39160814Ssimon* All right Reserved.
40296465Sdelphij*                                                                                      *
41296465Sdelphij*        THIS FILE IS PROVIDED BY BALTIMORE TECHNOLOGIES ``AS IS'' AND                 *
42296465Sdelphij*        ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE         *
43296465Sdelphij*        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE    *
44296465Sdelphij*        ARE DISCLAIMED.  IN NO EVENT SHALL BALTIMORE TECHNOLOGIES BE LIABLE           *
45296465Sdelphij*        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL    *
46296465Sdelphij*        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS       *
47296465Sdelphij*        OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)         *
48296465Sdelphij*        HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT    *
49296465Sdelphij*        LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY     *
50296465Sdelphij*        OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF        *
51296465Sdelphij*        SUCH DAMAGE.                                                                  *
52160814Ssimon====================================================================*/
53160814Ssimon
54160814Ssimon#include <stdio.h>
55160814Ssimon#include <string.h>
56160814Ssimon#include <openssl/crypto.h>
57160814Ssimon#include <openssl/pem.h>
58160814Ssimon#include <openssl/dso.h>
59160814Ssimon#include <openssl/engine.h>
60160814Ssimon#include <openssl/rand.h>
61160814Ssimon#ifndef OPENSSL_NO_RSA
62296465Sdelphij# include <openssl/rsa.h>
63160814Ssimon#endif
64160814Ssimon#ifndef OPENSSL_NO_DSA
65296465Sdelphij# include <openssl/dsa.h>
66160814Ssimon#endif
67160814Ssimon#ifndef OPENSSL_NO_DH
68296465Sdelphij# include <openssl/dh.h>
69160814Ssimon#endif
70160814Ssimon#include <openssl/bn.h>
71160814Ssimon
72160814Ssimon#ifndef OPENSSL_NO_HW
73296465Sdelphij# ifndef OPENSSL_NO_HW_SUREWARE
74160814Ssimon
75296465Sdelphij#  ifdef FLAT_INC
76296465Sdelphij#   include "sureware.h"
77296465Sdelphij#  else
78296465Sdelphij#   include "vendor_defns/sureware.h"
79296465Sdelphij#  endif
80160814Ssimon
81296465Sdelphij#  define SUREWARE_LIB_NAME "sureware engine"
82296465Sdelphij#  include "e_sureware_err.c"
83160814Ssimon
84296465Sdelphijstatic int surewarehk_ctrl(ENGINE *e, int cmd, long i, void *p,
85296465Sdelphij                           void (*f) (void));
86160814Ssimonstatic int surewarehk_destroy(ENGINE *e);
87160814Ssimonstatic int surewarehk_init(ENGINE *e);
88160814Ssimonstatic int surewarehk_finish(ENGINE *e);
89160814Ssimonstatic int surewarehk_modexp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
90296465Sdelphij                             const BIGNUM *m, BN_CTX *ctx);
91160814Ssimon
92160814Ssimon/* RSA stuff */
93296465Sdelphij#  ifndef OPENSSL_NO_RSA
94296465Sdelphijstatic int surewarehk_rsa_priv_dec(int flen, const unsigned char *from,
95296465Sdelphij                                   unsigned char *to, RSA *rsa, int padding);
96296465Sdelphijstatic int surewarehk_rsa_sign(int flen, const unsigned char *from,
97296465Sdelphij                               unsigned char *to, RSA *rsa, int padding);
98296465Sdelphij#  endif
99160814Ssimon
100160814Ssimon/* RAND stuff */
101160814Ssimonstatic int surewarehk_rand_bytes(unsigned char *buf, int num);
102160814Ssimonstatic void surewarehk_rand_seed(const void *buf, int num);
103160814Ssimonstatic void surewarehk_rand_add(const void *buf, int num, double entropy);
104160814Ssimon
105160814Ssimon/* KM stuff */
106160814Ssimonstatic EVP_PKEY *surewarehk_load_privkey(ENGINE *e, const char *key_id,
107296465Sdelphij                                         UI_METHOD *ui_method,
108296465Sdelphij                                         void *callback_data);
109160814Ssimonstatic EVP_PKEY *surewarehk_load_pubkey(ENGINE *e, const char *key_id,
110296465Sdelphij                                        UI_METHOD *ui_method,
111296465Sdelphij                                        void *callback_data);
112160814Ssimonstatic void surewarehk_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
113296465Sdelphij                               int idx, long argl, void *argp);
114296465Sdelphij#  if 0
115160814Ssimonstatic void surewarehk_dh_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
116296465Sdelphij                                  int idx, long argl, void *argp);
117296465Sdelphij#  endif
118160814Ssimon
119296465Sdelphij#  ifndef OPENSSL_NO_RSA
120160814Ssimon/* This function is aliased to mod_exp (with the mont stuff dropped). */
121296465Sdelphijstatic int surewarehk_mod_exp_mont(BIGNUM *r, const BIGNUM *a,
122296465Sdelphij                                   const BIGNUM *p, const BIGNUM *m,
123296465Sdelphij                                   BN_CTX *ctx, BN_MONT_CTX *m_ctx)
124160814Ssimon{
125296465Sdelphij    return surewarehk_modexp(r, a, p, m, ctx);
126160814Ssimon}
127160814Ssimon
128160814Ssimon/* Our internal RSA_METHOD that we provide pointers to */
129296465Sdelphijstatic RSA_METHOD surewarehk_rsa = {
130296465Sdelphij    "SureWare RSA method",
131296465Sdelphij    NULL,                       /* pub_enc */
132296465Sdelphij    NULL,                       /* pub_dec */
133296465Sdelphij    surewarehk_rsa_sign,        /* our rsa_sign is OpenSSL priv_enc */
134296465Sdelphij    surewarehk_rsa_priv_dec,    /* priv_dec */
135296465Sdelphij    NULL,                       /* mod_exp */
136296465Sdelphij    surewarehk_mod_exp_mont,    /* mod_exp_mongomery */
137296465Sdelphij    NULL,                       /* init */
138296465Sdelphij    NULL,                       /* finish */
139296465Sdelphij    0,                          /* RSA flag */
140296465Sdelphij    NULL,
141296465Sdelphij    NULL,                       /* OpenSSL sign */
142296465Sdelphij    NULL,                       /* OpenSSL verify */
143296465Sdelphij    NULL                        /* keygen */
144296465Sdelphij};
145296465Sdelphij#  endif
146160814Ssimon
147296465Sdelphij#  ifndef OPENSSL_NO_DH
148160814Ssimon/* Our internal DH_METHOD that we provide pointers to */
149160814Ssimon/* This function is aliased to mod_exp (with the dh and mont dropped). */
150160814Ssimonstatic int surewarehk_modexp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
151296465Sdelphij                                const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
152296465Sdelphij                                BN_MONT_CTX *m_ctx)
153160814Ssimon{
154296465Sdelphij    return surewarehk_modexp(r, a, p, m, ctx);
155160814Ssimon}
156160814Ssimon
157296465Sdelphijstatic DH_METHOD surewarehk_dh = {
158296465Sdelphij    "SureWare DH method",
159296465Sdelphij    NULL,                       /* gen_key */
160296465Sdelphij    NULL,                       /* agree, */
161296465Sdelphij    surewarehk_modexp_dh,       /* dh mod exp */
162296465Sdelphij    NULL,                       /* init */
163296465Sdelphij    NULL,                       /* finish */
164296465Sdelphij    0,                          /* flags */
165296465Sdelphij    NULL,
166296465Sdelphij    NULL
167296465Sdelphij};
168296465Sdelphij#  endif
169160814Ssimon
170296465Sdelphijstatic RAND_METHOD surewarehk_rand = {
171296465Sdelphij    /* "SureWare RAND method", */
172296465Sdelphij    surewarehk_rand_seed,
173296465Sdelphij    surewarehk_rand_bytes,
174296465Sdelphij    NULL,                       /* cleanup */
175296465Sdelphij    surewarehk_rand_add,
176296465Sdelphij    surewarehk_rand_bytes,
177296465Sdelphij    NULL,                       /* rand_status */
178296465Sdelphij};
179160814Ssimon
180296465Sdelphij#  ifndef OPENSSL_NO_DSA
181160814Ssimon/* DSA stuff */
182296465Sdelphijstatic DSA_SIG *surewarehk_dsa_do_sign(const unsigned char *dgst, int dlen,
183296465Sdelphij                                       DSA *dsa);
184160814Ssimonstatic int surewarehk_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
185296465Sdelphij                                  BIGNUM *p1, BIGNUM *a2, BIGNUM *p2,
186296465Sdelphij                                  BIGNUM *m, BN_CTX *ctx,
187296465Sdelphij                                  BN_MONT_CTX *in_mont)
188160814Ssimon{
189296465Sdelphij    BIGNUM t;
190296465Sdelphij    int to_return = 0;
191296465Sdelphij    BN_init(&t);
192296465Sdelphij    /* let rr = a1 ^ p1 mod m */
193296465Sdelphij    if (!surewarehk_modexp(rr, a1, p1, m, ctx))
194296465Sdelphij        goto end;
195296465Sdelphij    /* let t = a2 ^ p2 mod m */
196296465Sdelphij    if (!surewarehk_modexp(&t, a2, p2, m, ctx))
197296465Sdelphij        goto end;
198296465Sdelphij    /* let rr = rr * t mod m */
199296465Sdelphij    if (!BN_mod_mul(rr, rr, &t, m, ctx))
200296465Sdelphij        goto end;
201296465Sdelphij    to_return = 1;
202296465Sdelphij end:
203296465Sdelphij    BN_free(&t);
204296465Sdelphij    return to_return;
205160814Ssimon}
206160814Ssimon
207296465Sdelphijstatic DSA_METHOD surewarehk_dsa = {
208296465Sdelphij    "SureWare DSA method",
209296465Sdelphij    surewarehk_dsa_do_sign,
210296465Sdelphij    NULL,                       /* sign setup */
211296465Sdelphij    NULL,                       /* verify, */
212296465Sdelphij    surewarehk_dsa_mod_exp,     /* mod exp */
213296465Sdelphij    NULL,                       /* bn mod exp */
214296465Sdelphij    NULL,                       /* init */
215296465Sdelphij    NULL,                       /* finish */
216296465Sdelphij    0,
217296465Sdelphij    NULL,
218296465Sdelphij    NULL,
219296465Sdelphij    NULL
220296465Sdelphij};
221296465Sdelphij#  endif
222160814Ssimon
223160814Ssimonstatic const char *engine_sureware_id = "sureware";
224160814Ssimonstatic const char *engine_sureware_name = "SureWare hardware engine support";
225160814Ssimon
226160814Ssimon/* Now, to our own code */
227160814Ssimon
228296465Sdelphij/*
229296465Sdelphij * As this is only ever called once, there's no need for locking (indeed -
230296465Sdelphij * the lock will already be held by our caller!!!)
231296465Sdelphij */
232160814Ssimonstatic int bind_sureware(ENGINE *e)
233160814Ssimon{
234296465Sdelphij#  ifndef OPENSSL_NO_RSA
235296465Sdelphij    const RSA_METHOD *meth1;
236296465Sdelphij#  endif
237296465Sdelphij#  ifndef OPENSSL_NO_DSA
238296465Sdelphij    const DSA_METHOD *meth2;
239296465Sdelphij#  endif
240296465Sdelphij#  ifndef OPENSSL_NO_DH
241296465Sdelphij    const DH_METHOD *meth3;
242296465Sdelphij#  endif
243160814Ssimon
244296465Sdelphij    if (!ENGINE_set_id(e, engine_sureware_id) ||
245296465Sdelphij        !ENGINE_set_name(e, engine_sureware_name) ||
246296465Sdelphij#  ifndef OPENSSL_NO_RSA
247296465Sdelphij        !ENGINE_set_RSA(e, &surewarehk_rsa) ||
248296465Sdelphij#  endif
249296465Sdelphij#  ifndef OPENSSL_NO_DSA
250296465Sdelphij        !ENGINE_set_DSA(e, &surewarehk_dsa) ||
251296465Sdelphij#  endif
252296465Sdelphij#  ifndef OPENSSL_NO_DH
253296465Sdelphij        !ENGINE_set_DH(e, &surewarehk_dh) ||
254296465Sdelphij#  endif
255296465Sdelphij        !ENGINE_set_RAND(e, &surewarehk_rand) ||
256296465Sdelphij        !ENGINE_set_destroy_function(e, surewarehk_destroy) ||
257296465Sdelphij        !ENGINE_set_init_function(e, surewarehk_init) ||
258296465Sdelphij        !ENGINE_set_finish_function(e, surewarehk_finish) ||
259296465Sdelphij        !ENGINE_set_ctrl_function(e, surewarehk_ctrl) ||
260296465Sdelphij        !ENGINE_set_load_privkey_function(e, surewarehk_load_privkey) ||
261296465Sdelphij        !ENGINE_set_load_pubkey_function(e, surewarehk_load_pubkey))
262296465Sdelphij        return 0;
263160814Ssimon
264296465Sdelphij#  ifndef OPENSSL_NO_RSA
265296465Sdelphij    /*
266296465Sdelphij     * We know that the "PKCS1_SSLeay()" functions hook properly to the
267296465Sdelphij     * cswift-specific mod_exp and mod_exp_crt so we use those functions. NB:
268296465Sdelphij     * We don't use ENGINE_openssl() or anything "more generic" because
269296465Sdelphij     * something like the RSAref code may not hook properly, and if you own
270296465Sdelphij     * one of these cards then you have the right to do RSA operations on it
271296465Sdelphij     * anyway!
272296465Sdelphij     */
273296465Sdelphij    meth1 = RSA_PKCS1_SSLeay();
274296465Sdelphij    if (meth1) {
275296465Sdelphij        surewarehk_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
276296465Sdelphij        surewarehk_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
277296465Sdelphij    }
278296465Sdelphij#  endif
279160814Ssimon
280296465Sdelphij#  ifndef OPENSSL_NO_DSA
281296465Sdelphij    /*
282296465Sdelphij     * Use the DSA_OpenSSL() method and just hook the mod_exp-ish bits.
283296465Sdelphij     */
284296465Sdelphij    meth2 = DSA_OpenSSL();
285296465Sdelphij    if (meth2) {
286296465Sdelphij        surewarehk_dsa.dsa_do_verify = meth2->dsa_do_verify;
287296465Sdelphij    }
288296465Sdelphij#  endif
289160814Ssimon
290296465Sdelphij#  ifndef OPENSSL_NO_DH
291296465Sdelphij    /* Much the same for Diffie-Hellman */
292296465Sdelphij    meth3 = DH_OpenSSL();
293296465Sdelphij    if (meth3) {
294296465Sdelphij        surewarehk_dh.generate_key = meth3->generate_key;
295296465Sdelphij        surewarehk_dh.compute_key = meth3->compute_key;
296296465Sdelphij    }
297296465Sdelphij#  endif
298160814Ssimon
299296465Sdelphij    /* Ensure the sureware error handling is set up */
300296465Sdelphij    ERR_load_SUREWARE_strings();
301296465Sdelphij    return 1;
302160814Ssimon}
303160814Ssimon
304296465Sdelphij#  ifndef OPENSSL_NO_DYNAMIC_ENGINE
305160814Ssimonstatic int bind_helper(ENGINE *e, const char *id)
306296465Sdelphij{
307296465Sdelphij    if (id && (strcmp(id, engine_sureware_id) != 0))
308296465Sdelphij        return 0;
309296465Sdelphij    if (!bind_sureware(e))
310296465Sdelphij        return 0;
311296465Sdelphij    return 1;
312296465Sdelphij}
313296465Sdelphij
314160814SsimonIMPLEMENT_DYNAMIC_CHECK_FN()
315296465Sdelphij    IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
316296465Sdelphij#  else
317160814Ssimonstatic ENGINE *engine_sureware(void)
318296465Sdelphij{
319296465Sdelphij    ENGINE *ret = ENGINE_new();
320296465Sdelphij    if (!ret)
321296465Sdelphij        return NULL;
322296465Sdelphij    if (!bind_sureware(ret)) {
323296465Sdelphij        ENGINE_free(ret);
324296465Sdelphij        return NULL;
325296465Sdelphij    }
326296465Sdelphij    return ret;
327296465Sdelphij}
328160814Ssimon
329160814Ssimonvoid ENGINE_load_sureware(void)
330296465Sdelphij{
331296465Sdelphij    /* Copied from eng_[openssl|dyn].c */
332296465Sdelphij    ENGINE *toadd = engine_sureware();
333296465Sdelphij    if (!toadd)
334296465Sdelphij        return;
335296465Sdelphij    ENGINE_add(toadd);
336296465Sdelphij    ENGINE_free(toadd);
337296465Sdelphij    ERR_clear_error();
338296465Sdelphij}
339296465Sdelphij#  endif
340160814Ssimon
341296465Sdelphij/*
342296465Sdelphij * This is a process-global DSO handle used for loading and unloading the
343296465Sdelphij * SureWareHook library. NB: This is only set (or unset) during an init() or
344296465Sdelphij * finish() call (reference counts permitting) and they're operating with
345296465Sdelphij * global locks, so this should be thread-safe implicitly.
346296465Sdelphij */
347160814Ssimonstatic DSO *surewarehk_dso = NULL;
348296465Sdelphij#  ifndef OPENSSL_NO_RSA
349296465Sdelphij/* Index for KM handle.  Not really used yet. */
350296465Sdelphijstatic int rsaHndidx = -1;
351296465Sdelphij#  endif
352296465Sdelphij#  ifndef OPENSSL_NO_DSA
353296465Sdelphij/* Index for KM handle.  Not really used yet. */
354296465Sdelphijstatic int dsaHndidx = -1;
355296465Sdelphij#  endif
356160814Ssimon
357296465Sdelphij/*
358296465Sdelphij * These are the function pointers that are (un)set when the library has
359296465Sdelphij * successfully (un)loaded.
360296465Sdelphij */
361160814Ssimonstatic SureWareHook_Init_t *p_surewarehk_Init = NULL;
362160814Ssimonstatic SureWareHook_Finish_t *p_surewarehk_Finish = NULL;
363160814Ssimonstatic SureWareHook_Rand_Bytes_t *p_surewarehk_Rand_Bytes = NULL;
364160814Ssimonstatic SureWareHook_Rand_Seed_t *p_surewarehk_Rand_Seed = NULL;
365160814Ssimonstatic SureWareHook_Load_Privkey_t *p_surewarehk_Load_Privkey = NULL;
366160814Ssimonstatic SureWareHook_Info_Pubkey_t *p_surewarehk_Info_Pubkey = NULL;
367160814Ssimonstatic SureWareHook_Load_Rsa_Pubkey_t *p_surewarehk_Load_Rsa_Pubkey = NULL;
368160814Ssimonstatic SureWareHook_Load_Dsa_Pubkey_t *p_surewarehk_Load_Dsa_Pubkey = NULL;
369296465Sdelphijstatic SureWareHook_Free_t *p_surewarehk_Free = NULL;
370296465Sdelphijstatic SureWareHook_Rsa_Priv_Dec_t *p_surewarehk_Rsa_Priv_Dec = NULL;
371296465Sdelphijstatic SureWareHook_Rsa_Sign_t *p_surewarehk_Rsa_Sign = NULL;
372296465Sdelphijstatic SureWareHook_Dsa_Sign_t *p_surewarehk_Dsa_Sign = NULL;
373296465Sdelphijstatic SureWareHook_Mod_Exp_t *p_surewarehk_Mod_Exp = NULL;
374160814Ssimon
375160814Ssimon/* Used in the DSO operations. */
376160814Ssimonstatic const char *surewarehk_LIBNAME = "SureWareHook";
377160814Ssimonstatic const char *n_surewarehk_Init = "SureWareHook_Init";
378160814Ssimonstatic const char *n_surewarehk_Finish = "SureWareHook_Finish";
379296465Sdelphijstatic const char *n_surewarehk_Rand_Bytes = "SureWareHook_Rand_Bytes";
380296465Sdelphijstatic const char *n_surewarehk_Rand_Seed = "SureWareHook_Rand_Seed";
381296465Sdelphijstatic const char *n_surewarehk_Load_Privkey = "SureWareHook_Load_Privkey";
382296465Sdelphijstatic const char *n_surewarehk_Info_Pubkey = "SureWareHook_Info_Pubkey";
383296465Sdelphijstatic const char *n_surewarehk_Load_Rsa_Pubkey =
384296465Sdelphij    "SureWareHook_Load_Rsa_Pubkey";
385296465Sdelphijstatic const char *n_surewarehk_Load_Dsa_Pubkey =
386296465Sdelphij    "SureWareHook_Load_Dsa_Pubkey";
387296465Sdelphijstatic const char *n_surewarehk_Free = "SureWareHook_Free";
388296465Sdelphijstatic const char *n_surewarehk_Rsa_Priv_Dec = "SureWareHook_Rsa_Priv_Dec";
389296465Sdelphijstatic const char *n_surewarehk_Rsa_Sign = "SureWareHook_Rsa_Sign";
390296465Sdelphijstatic const char *n_surewarehk_Dsa_Sign = "SureWareHook_Dsa_Sign";
391296465Sdelphijstatic const char *n_surewarehk_Mod_Exp = "SureWareHook_Mod_Exp";
392160814Ssimonstatic BIO *logstream = NULL;
393160814Ssimon
394296465Sdelphij/*
395296465Sdelphij * SureWareHook library functions and mechanics - these are used by the
396296465Sdelphij * higher-level functions further down. NB: As and where there's no error
397296465Sdelphij * checking, take a look lower down where these functions are called, the
398296465Sdelphij * checking and error handling is probably down there.
399296465Sdelphij */
400296465Sdelphijstatic int threadsafe = 1;
401296465Sdelphijstatic int surewarehk_ctrl(ENGINE *e, int cmd, long i, void *p,
402296465Sdelphij                           void (*f) (void))
403160814Ssimon{
404296465Sdelphij    int to_return = 1;
405160814Ssimon
406296465Sdelphij    switch (cmd) {
407296465Sdelphij    case ENGINE_CTRL_SET_LOGSTREAM:
408296465Sdelphij        {
409296465Sdelphij            BIO *bio = (BIO *)p;
410296465Sdelphij            CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
411296465Sdelphij            if (logstream) {
412296465Sdelphij                BIO_free(logstream);
413296465Sdelphij                logstream = NULL;
414296465Sdelphij            }
415296465Sdelphij            if (CRYPTO_add(&bio->references, 1, CRYPTO_LOCK_BIO) > 1)
416296465Sdelphij                logstream = bio;
417296465Sdelphij            else
418296465Sdelphij                SUREWAREerr(SUREWARE_F_SUREWAREHK_CTRL,
419296465Sdelphij                            SUREWARE_R_BIO_WAS_FREED);
420296465Sdelphij        }
421296465Sdelphij        CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
422296465Sdelphij        break;
423296465Sdelphij        /*
424296465Sdelphij         * This will prevent the initialisation function from "installing"
425296465Sdelphij         * the mutex-handling callbacks, even if they are available from
426296465Sdelphij         * within the library (or were provided to the library from the
427296465Sdelphij         * calling application). This is to remove any baggage for
428296465Sdelphij         * applications not using multithreading.
429296465Sdelphij         */
430296465Sdelphij    case ENGINE_CTRL_CHIL_NO_LOCKING:
431296465Sdelphij        CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
432296465Sdelphij        threadsafe = 0;
433296465Sdelphij        CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
434296465Sdelphij        break;
435160814Ssimon
436296465Sdelphij        /* The command isn't understood by this engine */
437296465Sdelphij    default:
438296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_CTRL,
439296465Sdelphij                    ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
440296465Sdelphij        to_return = 0;
441296465Sdelphij        break;
442296465Sdelphij    }
443160814Ssimon
444296465Sdelphij    return to_return;
445160814Ssimon}
446160814Ssimon
447160814Ssimon/* Destructor (complements the "ENGINE_surewarehk()" constructor) */
448160814Ssimonstatic int surewarehk_destroy(ENGINE *e)
449160814Ssimon{
450296465Sdelphij    ERR_unload_SUREWARE_strings();
451296465Sdelphij    return 1;
452160814Ssimon}
453160814Ssimon
454160814Ssimon/* (de)initialisation functions. */
455160814Ssimonstatic int surewarehk_init(ENGINE *e)
456160814Ssimon{
457296465Sdelphij    char msg[64] = "ENGINE_init";
458296465Sdelphij    SureWareHook_Init_t *p1 = NULL;
459296465Sdelphij    SureWareHook_Finish_t *p2 = NULL;
460296465Sdelphij    SureWareHook_Rand_Bytes_t *p3 = NULL;
461296465Sdelphij    SureWareHook_Rand_Seed_t *p4 = NULL;
462296465Sdelphij    SureWareHook_Load_Privkey_t *p5 = NULL;
463296465Sdelphij    SureWareHook_Load_Rsa_Pubkey_t *p6 = NULL;
464296465Sdelphij    SureWareHook_Free_t *p7 = NULL;
465296465Sdelphij    SureWareHook_Rsa_Priv_Dec_t *p8 = NULL;
466296465Sdelphij    SureWareHook_Rsa_Sign_t *p9 = NULL;
467296465Sdelphij    SureWareHook_Dsa_Sign_t *p12 = NULL;
468296465Sdelphij    SureWareHook_Info_Pubkey_t *p13 = NULL;
469296465Sdelphij    SureWareHook_Load_Dsa_Pubkey_t *p14 = NULL;
470296465Sdelphij    SureWareHook_Mod_Exp_t *p15 = NULL;
471160814Ssimon
472296465Sdelphij    if (surewarehk_dso != NULL) {
473296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT, ENGINE_R_ALREADY_LOADED);
474296465Sdelphij        goto err;
475296465Sdelphij    }
476296465Sdelphij    /* Attempt to load libsurewarehk.so/surewarehk.dll/whatever. */
477296465Sdelphij    surewarehk_dso = DSO_load(NULL, surewarehk_LIBNAME, NULL, 0);
478296465Sdelphij    if (surewarehk_dso == NULL) {
479296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT, ENGINE_R_DSO_FAILURE);
480296465Sdelphij        goto err;
481296465Sdelphij    }
482296465Sdelphij    if (!
483296465Sdelphij        (p1 =
484296465Sdelphij         (SureWareHook_Init_t *) DSO_bind_func(surewarehk_dso,
485296465Sdelphij                                               n_surewarehk_Init))
486296465Sdelphij|| !(p2 =
487296465Sdelphij     (SureWareHook_Finish_t *) DSO_bind_func(surewarehk_dso,
488296465Sdelphij                                             n_surewarehk_Finish))
489296465Sdelphij|| !(p3 =
490296465Sdelphij     (SureWareHook_Rand_Bytes_t *) DSO_bind_func(surewarehk_dso,
491296465Sdelphij                                                 n_surewarehk_Rand_Bytes))
492296465Sdelphij|| !(p4 =
493296465Sdelphij     (SureWareHook_Rand_Seed_t *) DSO_bind_func(surewarehk_dso,
494296465Sdelphij                                                n_surewarehk_Rand_Seed))
495296465Sdelphij|| !(p5 =
496296465Sdelphij     (SureWareHook_Load_Privkey_t *) DSO_bind_func(surewarehk_dso,
497296465Sdelphij                                                   n_surewarehk_Load_Privkey))
498296465Sdelphij|| !(p6 =
499296465Sdelphij     (SureWareHook_Load_Rsa_Pubkey_t *) DSO_bind_func(surewarehk_dso,
500296465Sdelphij                                                      n_surewarehk_Load_Rsa_Pubkey))
501296465Sdelphij|| !(p7 =
502296465Sdelphij     (SureWareHook_Free_t *) DSO_bind_func(surewarehk_dso, n_surewarehk_Free))
503296465Sdelphij|| !(p8 =
504296465Sdelphij     (SureWareHook_Rsa_Priv_Dec_t *) DSO_bind_func(surewarehk_dso,
505296465Sdelphij                                                   n_surewarehk_Rsa_Priv_Dec))
506296465Sdelphij|| !(p9 =
507296465Sdelphij     (SureWareHook_Rsa_Sign_t *) DSO_bind_func(surewarehk_dso,
508296465Sdelphij                                               n_surewarehk_Rsa_Sign))
509296465Sdelphij|| !(p12 =
510296465Sdelphij     (SureWareHook_Dsa_Sign_t *) DSO_bind_func(surewarehk_dso,
511296465Sdelphij                                               n_surewarehk_Dsa_Sign))
512296465Sdelphij|| !(p13 =
513296465Sdelphij     (SureWareHook_Info_Pubkey_t *) DSO_bind_func(surewarehk_dso,
514296465Sdelphij                                                  n_surewarehk_Info_Pubkey))
515296465Sdelphij|| !(p14 =
516296465Sdelphij     (SureWareHook_Load_Dsa_Pubkey_t *) DSO_bind_func(surewarehk_dso,
517296465Sdelphij                                                      n_surewarehk_Load_Dsa_Pubkey))
518296465Sdelphij|| !(p15 =
519296465Sdelphij     (SureWareHook_Mod_Exp_t *) DSO_bind_func(surewarehk_dso,
520296465Sdelphij                                              n_surewarehk_Mod_Exp))) {
521296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT, ENGINE_R_DSO_FAILURE);
522296465Sdelphij        goto err;
523296465Sdelphij    }
524296465Sdelphij    /* Copy the pointers */
525296465Sdelphij    p_surewarehk_Init = p1;
526296465Sdelphij    p_surewarehk_Finish = p2;
527296465Sdelphij    p_surewarehk_Rand_Bytes = p3;
528296465Sdelphij    p_surewarehk_Rand_Seed = p4;
529296465Sdelphij    p_surewarehk_Load_Privkey = p5;
530296465Sdelphij    p_surewarehk_Load_Rsa_Pubkey = p6;
531296465Sdelphij    p_surewarehk_Free = p7;
532296465Sdelphij    p_surewarehk_Rsa_Priv_Dec = p8;
533296465Sdelphij    p_surewarehk_Rsa_Sign = p9;
534296465Sdelphij    p_surewarehk_Dsa_Sign = p12;
535296465Sdelphij    p_surewarehk_Info_Pubkey = p13;
536296465Sdelphij    p_surewarehk_Load_Dsa_Pubkey = p14;
537296465Sdelphij    p_surewarehk_Mod_Exp = p15;
538296465Sdelphij    /* Contact the hardware and initialises it. */
539296465Sdelphij    if (p_surewarehk_Init(msg, threadsafe) == SUREWAREHOOK_ERROR_UNIT_FAILURE) {
540296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT, SUREWARE_R_UNIT_FAILURE);
541296465Sdelphij        goto err;
542296465Sdelphij    }
543296465Sdelphij    if (p_surewarehk_Init(msg, threadsafe) == SUREWAREHOOK_ERROR_UNIT_FAILURE) {
544296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT, SUREWARE_R_UNIT_FAILURE);
545296465Sdelphij        goto err;
546296465Sdelphij    }
547296465Sdelphij    /*
548296465Sdelphij     * try to load the default private key, if failed does not return a
549296465Sdelphij     * failure but wait for an explicit ENGINE_load_privakey
550296465Sdelphij     */
551296465Sdelphij    surewarehk_load_privkey(e, NULL, NULL, NULL);
552160814Ssimon
553296465Sdelphij    /* Everything's fine. */
554296465Sdelphij#  ifndef OPENSSL_NO_RSA
555296465Sdelphij    if (rsaHndidx == -1)
556296465Sdelphij        rsaHndidx = RSA_get_ex_new_index(0,
557296465Sdelphij                                         "SureWareHook RSA key handle",
558296465Sdelphij                                         NULL, NULL, surewarehk_ex_free);
559296465Sdelphij#  endif
560296465Sdelphij#  ifndef OPENSSL_NO_DSA
561296465Sdelphij    if (dsaHndidx == -1)
562296465Sdelphij        dsaHndidx = DSA_get_ex_new_index(0,
563296465Sdelphij                                         "SureWareHook DSA key handle",
564296465Sdelphij                                         NULL, NULL, surewarehk_ex_free);
565296465Sdelphij#  endif
566160814Ssimon
567296465Sdelphij    return 1;
568296465Sdelphij err:
569296465Sdelphij    if (surewarehk_dso)
570296465Sdelphij        DSO_free(surewarehk_dso);
571296465Sdelphij    surewarehk_dso = NULL;
572296465Sdelphij    p_surewarehk_Init = NULL;
573296465Sdelphij    p_surewarehk_Finish = NULL;
574296465Sdelphij    p_surewarehk_Rand_Bytes = NULL;
575296465Sdelphij    p_surewarehk_Rand_Seed = NULL;
576296465Sdelphij    p_surewarehk_Load_Privkey = NULL;
577296465Sdelphij    p_surewarehk_Load_Rsa_Pubkey = NULL;
578296465Sdelphij    p_surewarehk_Free = NULL;
579296465Sdelphij    p_surewarehk_Rsa_Priv_Dec = NULL;
580296465Sdelphij    p_surewarehk_Rsa_Sign = NULL;
581296465Sdelphij    p_surewarehk_Dsa_Sign = NULL;
582296465Sdelphij    p_surewarehk_Info_Pubkey = NULL;
583296465Sdelphij    p_surewarehk_Load_Dsa_Pubkey = NULL;
584296465Sdelphij    p_surewarehk_Mod_Exp = NULL;
585296465Sdelphij    return 0;
586160814Ssimon}
587160814Ssimon
588160814Ssimonstatic int surewarehk_finish(ENGINE *e)
589160814Ssimon{
590296465Sdelphij    int to_return = 1;
591296465Sdelphij    if (surewarehk_dso == NULL) {
592296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_FINISH, ENGINE_R_NOT_LOADED);
593296465Sdelphij        to_return = 0;
594296465Sdelphij        goto err;
595296465Sdelphij    }
596296465Sdelphij    p_surewarehk_Finish();
597296465Sdelphij    if (!DSO_free(surewarehk_dso)) {
598296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_FINISH, ENGINE_R_DSO_FAILURE);
599296465Sdelphij        to_return = 0;
600296465Sdelphij        goto err;
601296465Sdelphij    }
602160814Ssimon err:
603296465Sdelphij    if (logstream)
604296465Sdelphij        BIO_free(logstream);
605296465Sdelphij    surewarehk_dso = NULL;
606296465Sdelphij    p_surewarehk_Init = NULL;
607296465Sdelphij    p_surewarehk_Finish = NULL;
608296465Sdelphij    p_surewarehk_Rand_Bytes = NULL;
609296465Sdelphij    p_surewarehk_Rand_Seed = NULL;
610296465Sdelphij    p_surewarehk_Load_Privkey = NULL;
611296465Sdelphij    p_surewarehk_Load_Rsa_Pubkey = NULL;
612296465Sdelphij    p_surewarehk_Free = NULL;
613296465Sdelphij    p_surewarehk_Rsa_Priv_Dec = NULL;
614296465Sdelphij    p_surewarehk_Rsa_Sign = NULL;
615296465Sdelphij    p_surewarehk_Dsa_Sign = NULL;
616296465Sdelphij    p_surewarehk_Info_Pubkey = NULL;
617296465Sdelphij    p_surewarehk_Load_Dsa_Pubkey = NULL;
618296465Sdelphij    p_surewarehk_Mod_Exp = NULL;
619296465Sdelphij    return to_return;
620160814Ssimon}
621160814Ssimon
622296465Sdelphijstatic void surewarehk_error_handling(char *const msg, int func, int ret)
623160814Ssimon{
624296465Sdelphij    switch (ret) {
625296465Sdelphij    case SUREWAREHOOK_ERROR_UNIT_FAILURE:
626296465Sdelphij        ENGINEerr(func, SUREWARE_R_UNIT_FAILURE);
627296465Sdelphij        break;
628296465Sdelphij    case SUREWAREHOOK_ERROR_FALLBACK:
629296465Sdelphij        ENGINEerr(func, SUREWARE_R_REQUEST_FALLBACK);
630296465Sdelphij        break;
631296465Sdelphij    case SUREWAREHOOK_ERROR_DATA_SIZE:
632296465Sdelphij        ENGINEerr(func, SUREWARE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
633296465Sdelphij        break;
634296465Sdelphij    case SUREWAREHOOK_ERROR_INVALID_PAD:
635296465Sdelphij        ENGINEerr(func, SUREWARE_R_PADDING_CHECK_FAILED);
636296465Sdelphij        break;
637296465Sdelphij    default:
638296465Sdelphij        ENGINEerr(func, SUREWARE_R_REQUEST_FAILED);
639296465Sdelphij        break;
640296465Sdelphij    case 1:                    /* nothing */
641296465Sdelphij        msg[0] = '\0';
642296465Sdelphij    }
643296465Sdelphij    if (*msg) {
644296465Sdelphij        ERR_add_error_data(1, msg);
645296465Sdelphij        if (logstream) {
646296465Sdelphij            CRYPTO_w_lock(CRYPTO_LOCK_BIO);
647296465Sdelphij            BIO_write(logstream, msg, strlen(msg));
648296465Sdelphij            CRYPTO_w_unlock(CRYPTO_LOCK_BIO);
649296465Sdelphij        }
650296465Sdelphij    }
651160814Ssimon}
652160814Ssimon
653160814Ssimonstatic int surewarehk_rand_bytes(unsigned char *buf, int num)
654160814Ssimon{
655296465Sdelphij    int ret = 0;
656296465Sdelphij    char msg[64] = "ENGINE_rand_bytes";
657296465Sdelphij    if (!p_surewarehk_Rand_Bytes) {
658296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_RAND_BYTES,
659296465Sdelphij                    ENGINE_R_NOT_INITIALISED);
660296465Sdelphij    } else {
661296465Sdelphij        ret = p_surewarehk_Rand_Bytes(msg, buf, num);
662296465Sdelphij        surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_RAND_BYTES, ret);
663296465Sdelphij    }
664296465Sdelphij    return ret == 1 ? 1 : 0;
665160814Ssimon}
666160814Ssimon
667160814Ssimonstatic void surewarehk_rand_seed(const void *buf, int num)
668160814Ssimon{
669296465Sdelphij    int ret = 0;
670296465Sdelphij    char msg[64] = "ENGINE_rand_seed";
671296465Sdelphij    if (!p_surewarehk_Rand_Seed) {
672296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_RAND_SEED,
673296465Sdelphij                    ENGINE_R_NOT_INITIALISED);
674296465Sdelphij    } else {
675296465Sdelphij        ret = p_surewarehk_Rand_Seed(msg, buf, num);
676296465Sdelphij        surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_RAND_SEED, ret);
677296465Sdelphij    }
678160814Ssimon}
679160814Ssimon
680160814Ssimonstatic void surewarehk_rand_add(const void *buf, int num, double entropy)
681160814Ssimon{
682296465Sdelphij    surewarehk_rand_seed(buf, num);
683160814Ssimon}
684160814Ssimon
685296465Sdelphijstatic EVP_PKEY *sureware_load_public(ENGINE *e, const char *key_id,
686296465Sdelphij                                      char *hptr, unsigned long el,
687296465Sdelphij                                      char keytype)
688160814Ssimon{
689296465Sdelphij    EVP_PKEY *res = NULL;
690296465Sdelphij#  ifndef OPENSSL_NO_RSA
691296465Sdelphij    RSA *rsatmp = NULL;
692296465Sdelphij#  endif
693296465Sdelphij#  ifndef OPENSSL_NO_DSA
694296465Sdelphij    DSA *dsatmp = NULL;
695296465Sdelphij#  endif
696296465Sdelphij    char msg[64] = "sureware_load_public";
697296465Sdelphij    int ret = 0;
698296465Sdelphij    if (!p_surewarehk_Load_Rsa_Pubkey || !p_surewarehk_Load_Dsa_Pubkey) {
699296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,
700296465Sdelphij                    ENGINE_R_NOT_INITIALISED);
701296465Sdelphij        goto err;
702296465Sdelphij    }
703296465Sdelphij    switch (keytype) {
704296465Sdelphij#  ifndef OPENSSL_NO_RSA
705296465Sdelphij    case 1:
706296465Sdelphij         /*RSA*/
707296465Sdelphij            /* set private external reference */
708296465Sdelphij            rsatmp = RSA_new_method(e);
709296465Sdelphij        RSA_set_ex_data(rsatmp, rsaHndidx, hptr);
710296465Sdelphij        rsatmp->flags |= RSA_FLAG_EXT_PKEY;
711160814Ssimon
712296465Sdelphij        /* set public big nums */
713296465Sdelphij        rsatmp->e = BN_new();
714296465Sdelphij        rsatmp->n = BN_new();
715296465Sdelphij        bn_expand2(rsatmp->e, el / sizeof(BN_ULONG));
716296465Sdelphij        bn_expand2(rsatmp->n, el / sizeof(BN_ULONG));
717296465Sdelphij        if (!rsatmp->e || rsatmp->e->dmax != (int)(el / sizeof(BN_ULONG)) ||
718296465Sdelphij            !rsatmp->n || rsatmp->n->dmax != (int)(el / sizeof(BN_ULONG)))
719296465Sdelphij            goto err;
720296465Sdelphij        ret = p_surewarehk_Load_Rsa_Pubkey(msg, key_id, el,
721296465Sdelphij                                           (unsigned long *)rsatmp->n->d,
722296465Sdelphij                                           (unsigned long *)rsatmp->e->d);
723296465Sdelphij        surewarehk_error_handling(msg, SUREWARE_F_SUREWARE_LOAD_PUBLIC, ret);
724296465Sdelphij        if (ret != 1) {
725296465Sdelphij            SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,
726296465Sdelphij                        ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
727296465Sdelphij            goto err;
728296465Sdelphij        }
729296465Sdelphij        /* normalise pub e and pub n */
730296465Sdelphij        rsatmp->e->top = el / sizeof(BN_ULONG);
731296465Sdelphij        bn_fix_top(rsatmp->e);
732296465Sdelphij        rsatmp->n->top = el / sizeof(BN_ULONG);
733296465Sdelphij        bn_fix_top(rsatmp->n);
734296465Sdelphij        /* create an EVP object: engine + rsa key */
735296465Sdelphij        res = EVP_PKEY_new();
736296465Sdelphij        EVP_PKEY_assign_RSA(res, rsatmp);
737296465Sdelphij        break;
738296465Sdelphij#  endif
739160814Ssimon
740296465Sdelphij#  ifndef OPENSSL_NO_DSA
741296465Sdelphij    case 2:
742296465Sdelphij         /*DSA*/
743296465Sdelphij            /* set private/public external reference */
744296465Sdelphij            dsatmp = DSA_new_method(e);
745296465Sdelphij        DSA_set_ex_data(dsatmp, dsaHndidx, hptr);
746296465Sdelphij        /*
747296465Sdelphij         * dsatmp->flags |= DSA_FLAG_EXT_PKEY;
748296465Sdelphij         */
749160814Ssimon
750296465Sdelphij        /* set public key */
751296465Sdelphij        dsatmp->pub_key = BN_new();
752296465Sdelphij        dsatmp->p = BN_new();
753296465Sdelphij        dsatmp->q = BN_new();
754296465Sdelphij        dsatmp->g = BN_new();
755296465Sdelphij        bn_expand2(dsatmp->pub_key, el / sizeof(BN_ULONG));
756296465Sdelphij        bn_expand2(dsatmp->p, el / sizeof(BN_ULONG));
757296465Sdelphij        bn_expand2(dsatmp->q, 20 / sizeof(BN_ULONG));
758296465Sdelphij        bn_expand2(dsatmp->g, el / sizeof(BN_ULONG));
759296465Sdelphij        if (!dsatmp->pub_key
760296465Sdelphij            || dsatmp->pub_key->dmax != (int)(el / sizeof(BN_ULONG))
761296465Sdelphij            || !dsatmp->p || dsatmp->p->dmax != (int)(el / sizeof(BN_ULONG))
762296465Sdelphij            || !dsatmp->q || dsatmp->q->dmax != 20 / sizeof(BN_ULONG)
763296465Sdelphij            || !dsatmp->g || dsatmp->g->dmax != (int)(el / sizeof(BN_ULONG)))
764296465Sdelphij            goto err;
765160814Ssimon
766296465Sdelphij        ret = p_surewarehk_Load_Dsa_Pubkey(msg, key_id, el,
767296465Sdelphij                                           (unsigned long *)dsatmp->
768296465Sdelphij                                           pub_key->d,
769296465Sdelphij                                           (unsigned long *)dsatmp->p->d,
770296465Sdelphij                                           (unsigned long *)dsatmp->q->d,
771296465Sdelphij                                           (unsigned long *)dsatmp->g->d);
772296465Sdelphij        surewarehk_error_handling(msg, SUREWARE_F_SUREWARE_LOAD_PUBLIC, ret);
773296465Sdelphij        if (ret != 1) {
774296465Sdelphij            SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,
775296465Sdelphij                        ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
776296465Sdelphij            goto err;
777296465Sdelphij        }
778296465Sdelphij        /* set parameters */
779296465Sdelphij        /* normalise pubkey and parameters in case of */
780296465Sdelphij        dsatmp->pub_key->top = el / sizeof(BN_ULONG);
781296465Sdelphij        bn_fix_top(dsatmp->pub_key);
782296465Sdelphij        dsatmp->p->top = el / sizeof(BN_ULONG);
783296465Sdelphij        bn_fix_top(dsatmp->p);
784296465Sdelphij        dsatmp->q->top = 20 / sizeof(BN_ULONG);
785296465Sdelphij        bn_fix_top(dsatmp->q);
786296465Sdelphij        dsatmp->g->top = el / sizeof(BN_ULONG);
787296465Sdelphij        bn_fix_top(dsatmp->g);
788160814Ssimon
789296465Sdelphij        /* create an EVP object: engine + rsa key */
790296465Sdelphij        res = EVP_PKEY_new();
791296465Sdelphij        EVP_PKEY_assign_DSA(res, dsatmp);
792296465Sdelphij        break;
793296465Sdelphij#  endif
794160814Ssimon
795296465Sdelphij    default:
796296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,
797296465Sdelphij                    ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
798296465Sdelphij        goto err;
799296465Sdelphij    }
800296465Sdelphij    return res;
801160814Ssimon err:
802296465Sdelphij    if (res)
803296465Sdelphij        EVP_PKEY_free(res);
804296465Sdelphij#  ifndef OPENSSL_NO_RSA
805296465Sdelphij    if (rsatmp)
806296465Sdelphij        RSA_free(rsatmp);
807296465Sdelphij#  endif
808296465Sdelphij#  ifndef OPENSSL_NO_DSA
809296465Sdelphij    if (dsatmp)
810296465Sdelphij        DSA_free(dsatmp);
811296465Sdelphij#  endif
812296465Sdelphij    return NULL;
813160814Ssimon}
814160814Ssimon
815160814Ssimonstatic EVP_PKEY *surewarehk_load_privkey(ENGINE *e, const char *key_id,
816296465Sdelphij                                         UI_METHOD *ui_method,
817296465Sdelphij                                         void *callback_data)
818160814Ssimon{
819296465Sdelphij    EVP_PKEY *res = NULL;
820296465Sdelphij    int ret = 0;
821296465Sdelphij    unsigned long el = 0;
822296465Sdelphij    char *hptr = NULL;
823296465Sdelphij    char keytype = 0;
824296465Sdelphij    char msg[64] = "ENGINE_load_privkey";
825160814Ssimon
826296465Sdelphij    if (!p_surewarehk_Load_Privkey) {
827296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PRIVKEY,
828296465Sdelphij                    ENGINE_R_NOT_INITIALISED);
829296465Sdelphij    } else {
830296465Sdelphij        ret = p_surewarehk_Load_Privkey(msg, key_id, &hptr, &el, &keytype);
831296465Sdelphij        if (ret != 1) {
832296465Sdelphij            SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PRIVKEY,
833296465Sdelphij                        ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
834296465Sdelphij            ERR_add_error_data(1, msg);
835296465Sdelphij        } else
836296465Sdelphij            res = sureware_load_public(e, key_id, hptr, el, keytype);
837296465Sdelphij    }
838296465Sdelphij    return res;
839160814Ssimon}
840160814Ssimon
841160814Ssimonstatic EVP_PKEY *surewarehk_load_pubkey(ENGINE *e, const char *key_id,
842296465Sdelphij                                        UI_METHOD *ui_method,
843296465Sdelphij                                        void *callback_data)
844160814Ssimon{
845296465Sdelphij    EVP_PKEY *res = NULL;
846296465Sdelphij    int ret = 0;
847296465Sdelphij    unsigned long el = 0;
848296465Sdelphij    char *hptr = NULL;
849296465Sdelphij    char keytype = 0;
850296465Sdelphij    char msg[64] = "ENGINE_load_pubkey";
851160814Ssimon
852296465Sdelphij    if (!p_surewarehk_Info_Pubkey) {
853296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PUBKEY,
854296465Sdelphij                    ENGINE_R_NOT_INITIALISED);
855296465Sdelphij    } else {
856296465Sdelphij        /* call once to identify if DSA or RSA */
857296465Sdelphij        ret = p_surewarehk_Info_Pubkey(msg, key_id, &el, &keytype);
858296465Sdelphij        if (ret != 1) {
859296465Sdelphij            SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PUBKEY,
860296465Sdelphij                        ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
861296465Sdelphij            ERR_add_error_data(1, msg);
862296465Sdelphij        } else
863296465Sdelphij            res = sureware_load_public(e, key_id, hptr, el, keytype);
864296465Sdelphij    }
865296465Sdelphij    return res;
866160814Ssimon}
867160814Ssimon
868296465Sdelphij/*
869296465Sdelphij * This cleans up an RSA/DSA KM key(do not destroy the key into the hardware)
870296465Sdelphij * , called when ex_data is freed
871296465Sdelphij */
872160814Ssimonstatic void surewarehk_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
873296465Sdelphij                               int idx, long argl, void *argp)
874160814Ssimon{
875296465Sdelphij    if (!p_surewarehk_Free) {
876296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_EX_FREE, ENGINE_R_NOT_INITIALISED);
877296465Sdelphij    } else
878296465Sdelphij        p_surewarehk_Free((char *)item, 0);
879160814Ssimon}
880160814Ssimon
881296465Sdelphij#  if 0
882160814Ssimon/* not currently used (bug?) */
883296465Sdelphij/*
884296465Sdelphij * This cleans up an DH KM key (destroys the key into hardware), called when
885296465Sdelphij * ex_data is freed
886296465Sdelphij */
887160814Ssimonstatic void surewarehk_dh_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
888296465Sdelphij                                  int idx, long argl, void *argp)
889160814Ssimon{
890296465Sdelphij    if (!p_surewarehk_Free) {
891296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_DH_EX_FREE,
892296465Sdelphij                    ENGINE_R_NOT_INITIALISED);
893296465Sdelphij    } else
894296465Sdelphij        p_surewarehk_Free((char *)item, 1);
895160814Ssimon}
896296465Sdelphij#  endif
897160814Ssimon
898160814Ssimon/*
899296465Sdelphij * return number of decrypted bytes
900296465Sdelphij */
901296465Sdelphij#  ifndef OPENSSL_NO_RSA
902296465Sdelphijstatic int surewarehk_rsa_priv_dec(int flen, const unsigned char *from,
903296465Sdelphij                                   unsigned char *to, RSA *rsa, int padding)
904160814Ssimon{
905296465Sdelphij    int ret = 0, tlen;
906296465Sdelphij    char *buf = NULL, *hptr = NULL;
907296465Sdelphij    char msg[64] = "ENGINE_rsa_priv_dec";
908296465Sdelphij    if (!p_surewarehk_Rsa_Priv_Dec) {
909296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
910296465Sdelphij                    ENGINE_R_NOT_INITIALISED);
911296465Sdelphij    }
912296465Sdelphij    /* extract ref to private key */
913296465Sdelphij    else if (!(hptr = RSA_get_ex_data(rsa, rsaHndidx))) {
914296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
915296465Sdelphij                    SUREWARE_R_MISSING_KEY_COMPONENTS);
916296465Sdelphij        goto err;
917296465Sdelphij    }
918296465Sdelphij    /* analyse what padding we can do into the hardware */
919296465Sdelphij    if (padding == RSA_PKCS1_PADDING) {
920296465Sdelphij        /* do it one shot */
921296465Sdelphij        ret =
922296465Sdelphij            p_surewarehk_Rsa_Priv_Dec(msg, flen, (unsigned char *)from, &tlen,
923296465Sdelphij                                      to, hptr, SUREWARE_PKCS1_PAD);
924296465Sdelphij        surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
925296465Sdelphij                                  ret);
926296465Sdelphij        if (ret != 1)
927296465Sdelphij            goto err;
928296465Sdelphij        ret = tlen;
929296465Sdelphij    } else {                    /* do with no padding into hardware */
930296465Sdelphij
931296465Sdelphij        ret =
932296465Sdelphij            p_surewarehk_Rsa_Priv_Dec(msg, flen, (unsigned char *)from, &tlen,
933296465Sdelphij                                      to, hptr, SUREWARE_NO_PAD);
934296465Sdelphij        surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
935296465Sdelphij                                  ret);
936296465Sdelphij        if (ret != 1)
937296465Sdelphij            goto err;
938296465Sdelphij        /* intermediate buffer for padding */
939296465Sdelphij        if ((buf = OPENSSL_malloc(tlen)) == NULL) {
940296465Sdelphij            SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
941296465Sdelphij                        ERR_R_MALLOC_FAILURE);
942296465Sdelphij            goto err;
943296465Sdelphij        }
944296465Sdelphij        memcpy(buf, to, tlen);  /* transfert to into buf */
945296465Sdelphij        switch (padding) {      /* check padding in software */
946296465Sdelphij#   ifndef OPENSSL_NO_SHA
947296465Sdelphij        case RSA_PKCS1_OAEP_PADDING:
948296465Sdelphij            ret =
949296465Sdelphij                RSA_padding_check_PKCS1_OAEP(to, tlen, (unsigned char *)buf,
950296465Sdelphij                                             tlen, tlen, NULL, 0);
951296465Sdelphij            break;
952296465Sdelphij#   endif
953296465Sdelphij        case RSA_SSLV23_PADDING:
954296465Sdelphij            ret =
955296465Sdelphij                RSA_padding_check_SSLv23(to, tlen, (unsigned char *)buf, flen,
956296465Sdelphij                                         tlen);
957296465Sdelphij            break;
958296465Sdelphij        case RSA_NO_PADDING:
959296465Sdelphij            ret =
960296465Sdelphij                RSA_padding_check_none(to, tlen, (unsigned char *)buf, flen,
961296465Sdelphij                                       tlen);
962296465Sdelphij            break;
963296465Sdelphij        default:
964296465Sdelphij            SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
965296465Sdelphij                        SUREWARE_R_UNKNOWN_PADDING_TYPE);
966296465Sdelphij            goto err;
967296465Sdelphij        }
968296465Sdelphij        if (ret < 0)
969296465Sdelphij            SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,
970296465Sdelphij                        SUREWARE_R_PADDING_CHECK_FAILED);
971296465Sdelphij    }
972296465Sdelphij err:
973296465Sdelphij    if (buf) {
974296465Sdelphij        OPENSSL_cleanse(buf, tlen);
975296465Sdelphij        OPENSSL_free(buf);
976296465Sdelphij    }
977296465Sdelphij    return ret;
978160814Ssimon}
979160814Ssimon
980160814Ssimon/*
981296465Sdelphij * Does what OpenSSL rsa_priv_enc does.
982296465Sdelphij */
983296465Sdelphijstatic int surewarehk_rsa_sign(int flen, const unsigned char *from,
984296465Sdelphij                               unsigned char *to, RSA *rsa, int padding)
985160814Ssimon{
986296465Sdelphij    int ret = 0, tlen;
987296465Sdelphij    char *hptr = NULL;
988296465Sdelphij    char msg[64] = "ENGINE_rsa_sign";
989296465Sdelphij    if (!p_surewarehk_Rsa_Sign) {
990296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN, ENGINE_R_NOT_INITIALISED);
991296465Sdelphij    }
992296465Sdelphij    /* extract ref to private key */
993296465Sdelphij    else if (!(hptr = RSA_get_ex_data(rsa, rsaHndidx))) {
994296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN,
995296465Sdelphij                    SUREWARE_R_MISSING_KEY_COMPONENTS);
996296465Sdelphij    } else {
997296465Sdelphij        switch (padding) {
998296465Sdelphij        case RSA_PKCS1_PADDING: /* do it in one shot */
999296465Sdelphij            ret =
1000296465Sdelphij                p_surewarehk_Rsa_Sign(msg, flen, (unsigned char *)from, &tlen,
1001296465Sdelphij                                      to, hptr, SUREWARE_PKCS1_PAD);
1002296465Sdelphij            surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_RSA_SIGN,
1003296465Sdelphij                                      ret);
1004296465Sdelphij            break;
1005296465Sdelphij        case RSA_NO_PADDING:
1006296465Sdelphij        default:
1007296465Sdelphij            SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN,
1008296465Sdelphij                        SUREWARE_R_UNKNOWN_PADDING_TYPE);
1009296465Sdelphij        }
1010296465Sdelphij    }
1011296465Sdelphij    return ret == 1 ? tlen : ret;
1012160814Ssimon}
1013160814Ssimon
1014296465Sdelphij#  endif
1015160814Ssimon
1016296465Sdelphij#  ifndef OPENSSL_NO_DSA
1017160814Ssimon/* DSA sign and verify */
1018296465Sdelphijstatic DSA_SIG *surewarehk_dsa_do_sign(const unsigned char *from, int flen,
1019296465Sdelphij                                       DSA *dsa)
1020160814Ssimon{
1021296465Sdelphij    int ret = 0;
1022296465Sdelphij    char *hptr = NULL;
1023296465Sdelphij    DSA_SIG *psign = NULL;
1024296465Sdelphij    char msg[64] = "ENGINE_dsa_do_sign";
1025296465Sdelphij    if (!p_surewarehk_Dsa_Sign) {
1026296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,
1027296465Sdelphij                    ENGINE_R_NOT_INITIALISED);
1028296465Sdelphij        goto err;
1029296465Sdelphij    }
1030296465Sdelphij    /* extract ref to private key */
1031296465Sdelphij    else if (!(hptr = DSA_get_ex_data(dsa, dsaHndidx))) {
1032296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,
1033296465Sdelphij                    SUREWARE_R_MISSING_KEY_COMPONENTS);
1034296465Sdelphij        goto err;
1035296465Sdelphij    } else {
1036296465Sdelphij        if ((psign = DSA_SIG_new()) == NULL) {
1037296465Sdelphij            SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,
1038296465Sdelphij                        ERR_R_MALLOC_FAILURE);
1039296465Sdelphij            goto err;
1040296465Sdelphij        }
1041296465Sdelphij        psign->r = BN_new();
1042296465Sdelphij        psign->s = BN_new();
1043296465Sdelphij        bn_expand2(psign->r, 20 / sizeof(BN_ULONG));
1044296465Sdelphij        bn_expand2(psign->s, 20 / sizeof(BN_ULONG));
1045296465Sdelphij        if (!psign->r || psign->r->dmax != 20 / sizeof(BN_ULONG) ||
1046296465Sdelphij            !psign->s || psign->s->dmax != 20 / sizeof(BN_ULONG))
1047296465Sdelphij            goto err;
1048296465Sdelphij        ret = p_surewarehk_Dsa_Sign(msg, flen, from,
1049296465Sdelphij                                    (unsigned long *)psign->r->d,
1050296465Sdelphij                                    (unsigned long *)psign->s->d, hptr);
1051296465Sdelphij        surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,
1052296465Sdelphij                                  ret);
1053296465Sdelphij    }
1054296465Sdelphij    psign->r->top = 20 / sizeof(BN_ULONG);
1055296465Sdelphij    bn_fix_top(psign->r);
1056296465Sdelphij    psign->s->top = 20 / sizeof(BN_ULONG);
1057296465Sdelphij    bn_fix_top(psign->s);
1058160814Ssimon
1059296465Sdelphij err:
1060296465Sdelphij    if (psign) {
1061296465Sdelphij        DSA_SIG_free(psign);
1062296465Sdelphij        psign = NULL;
1063296465Sdelphij    }
1064296465Sdelphij    return psign;
1065160814Ssimon}
1066296465Sdelphij#  endif
1067160814Ssimon
1068160814Ssimonstatic int surewarehk_modexp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1069296465Sdelphij                             const BIGNUM *m, BN_CTX *ctx)
1070160814Ssimon{
1071296465Sdelphij    int ret = 0;
1072296465Sdelphij    char msg[64] = "ENGINE_modexp";
1073296465Sdelphij    if (!p_surewarehk_Mod_Exp) {
1074296465Sdelphij        SUREWAREerr(SUREWARE_F_SUREWAREHK_MODEXP, ENGINE_R_NOT_INITIALISED);
1075296465Sdelphij    } else {
1076296465Sdelphij        bn_expand2(r, m->top);
1077296465Sdelphij        if (r && r->dmax == m->top) {
1078296465Sdelphij            /* do it */
1079296465Sdelphij            ret = p_surewarehk_Mod_Exp(msg,
1080296465Sdelphij                                       m->top * sizeof(BN_ULONG),
1081296465Sdelphij                                       (unsigned long *)m->d,
1082296465Sdelphij                                       p->top * sizeof(BN_ULONG),
1083296465Sdelphij                                       (unsigned long *)p->d,
1084296465Sdelphij                                       a->top * sizeof(BN_ULONG),
1085296465Sdelphij                                       (unsigned long *)a->d,
1086296465Sdelphij                                       (unsigned long *)r->d);
1087296465Sdelphij            surewarehk_error_handling(msg, SUREWARE_F_SUREWAREHK_MODEXP, ret);
1088296465Sdelphij            if (ret == 1) {
1089296465Sdelphij                /* normalise result */
1090296465Sdelphij                r->top = m->top;
1091296465Sdelphij                bn_fix_top(r);
1092296465Sdelphij            }
1093296465Sdelphij        }
1094296465Sdelphij    }
1095296465Sdelphij    return ret;
1096160814Ssimon}
1097296465Sdelphij# endif                         /* !OPENSSL_NO_HW_SureWare */
1098296465Sdelphij#endif                          /* !OPENSSL_NO_HW */
1099