📄 e_sureware.c
字号:
/* Written by Corinne Dive-Reclus(cdive@baltimore.com)* ** Redistribution and use in source and binary forms, with or without* modification, are permitted provided that the following conditions* are met:** 1. Redistributions of source code must retain the above copyright* notice, this list of conditions and the following disclaimer. ** 2. Redistributions in binary form must reproduce the above copyright* notice, this list of conditions and the following disclaimer in* the documentation and/or other materials provided with the* distribution.** 3. All advertising materials mentioning features or use of this* software must display the following acknowledgment:* "This product includes software developed by the OpenSSL Project* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"** 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to* endorse or promote products derived from this software without* prior written permission. For written permission, please contact* licensing@OpenSSL.org.** 5. Products derived from this software may not be called "OpenSSL"* nor may "OpenSSL" appear in their names without prior written* permission of the OpenSSL Project.** 6. Redistributions of any form whatsoever must retain the following* acknowledgment:* "This product includes software developed by the OpenSSL Project* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"** Written by Corinne Dive-Reclus(cdive@baltimore.com)** Copyright@2001 Baltimore Technologies Ltd.* All right Reserved.* * * THIS FILE IS PROVIDED BY BALTIMORE TECHNOLOGIES ``AS IS'' AND ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ** ARE DISCLAIMED. IN NO EVENT SHALL BALTIMORE TECHNOLOGIES BE LIABLE ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ** SUCH DAMAGE. *====================================================================*/#include <stdio.h>#include <string.h>#include <openssl/crypto.h>#include <openssl/pem.h>#include <openssl/dso.h>#include <openssl/engine.h>#include <openssl/rand.h>#ifndef OPENSSL_NO_RSA#include <openssl/rsa.h>#endif#ifndef OPENSSL_NO_DSA#include <openssl/dsa.h>#endif#ifndef OPENSSL_NO_DH#include <openssl/dh.h>#endif#include <openssl/bn.h>#ifndef OPENSSL_NO_HW#ifndef OPENSSL_NO_HW_SUREWARE#ifdef FLAT_INC#include "sureware.h"#else#include "vendor_defns/sureware.h"#endif#define SUREWARE_LIB_NAME "sureware engine"#include "e_sureware_err.c"static int surewarehk_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));static int surewarehk_destroy(ENGINE *e);static int surewarehk_init(ENGINE *e);static int surewarehk_finish(ENGINE *e);static int surewarehk_modexp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx);/* RSA stuff */#ifndef OPENSSL_NO_RSAstatic int surewarehk_rsa_priv_dec(int flen,const unsigned char *from,unsigned char *to, RSA *rsa,int padding);static int surewarehk_rsa_sign(int flen,const unsigned char *from,unsigned char *to, RSA *rsa,int padding);#endif/* RAND stuff */static int surewarehk_rand_bytes(unsigned char *buf, int num);static void surewarehk_rand_seed(const void *buf, int num);static void surewarehk_rand_add(const void *buf, int num, double entropy);/* KM stuff */static EVP_PKEY *surewarehk_load_privkey(ENGINE *e, const char *key_id, UI_METHOD *ui_method, void *callback_data);static EVP_PKEY *surewarehk_load_pubkey(ENGINE *e, const char *key_id, UI_METHOD *ui_method, void *callback_data);static void surewarehk_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad, int idx,long argl, void *argp);#if 0static void surewarehk_dh_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad, int idx,long argl, void *argp);#endif#ifndef OPENSSL_NO_RSA/* This function is aliased to mod_exp (with the mont stuff dropped). */static int surewarehk_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx){ return surewarehk_modexp(r, a, p, m, ctx);}/* Our internal RSA_METHOD that we provide pointers to */static RSA_METHOD surewarehk_rsa = { "SureWare RSA method", NULL, /* pub_enc*/ NULL, /* pub_dec*/ surewarehk_rsa_sign, /* our rsa_sign is OpenSSL priv_enc*/ surewarehk_rsa_priv_dec, /* priv_dec*/ NULL, /*mod_exp*/ surewarehk_mod_exp_mont, /*mod_exp_mongomery*/ NULL, /* init*/ NULL, /* finish*/ 0, /* RSA flag*/ NULL, NULL, /* OpenSSL sign*/ NULL, /* OpenSSL verify*/ NULL /* keygen */ };#endif#ifndef OPENSSL_NO_DH/* Our internal DH_METHOD that we provide pointers to *//* This function is aliased to mod_exp (with the dh and mont dropped). */static int surewarehk_modexp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx){ return surewarehk_modexp(r, a, p, m, ctx);}static DH_METHOD surewarehk_dh = { "SureWare DH method", NULL,/*gen_key*/ NULL,/*agree,*/ surewarehk_modexp_dh, /*dh mod exp*/ NULL, /* init*/ NULL, /* finish*/ 0, /* flags*/ NULL, NULL };#endifstatic RAND_METHOD surewarehk_rand = { /* "SureWare RAND method", */ surewarehk_rand_seed, surewarehk_rand_bytes, NULL,/*cleanup*/ surewarehk_rand_add, surewarehk_rand_bytes, NULL,/*rand_status*/ };#ifndef OPENSSL_NO_DSA/* DSA stuff */static DSA_SIG * surewarehk_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);static int surewarehk_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1, BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont){ BIGNUM t; int to_return = 0; BN_init(&t); /* let rr = a1 ^ p1 mod m */ if (!surewarehk_modexp(rr,a1,p1,m,ctx)) goto end; /* let t = a2 ^ p2 mod m */ if (!surewarehk_modexp(&t,a2,p2,m,ctx)) goto end; /* let rr = rr * t mod m */ if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end; to_return = 1;end: BN_free(&t); return to_return;}static DSA_METHOD surewarehk_dsa = { "SureWare DSA method", surewarehk_dsa_do_sign, NULL,/*sign setup*/ NULL,/*verify,*/ surewarehk_dsa_mod_exp,/*mod exp*/ NULL,/*bn mod exp*/ NULL, /*init*/ NULL,/*finish*/ 0, NULL, NULL, NULL };#endifstatic const char *engine_sureware_id = "sureware";static const char *engine_sureware_name = "SureWare hardware engine support";/* Now, to our own code *//* As this is only ever called once, there's no need for locking * (indeed - the lock will already be held by our caller!!!) */static int bind_sureware(ENGINE *e){#ifndef OPENSSL_NO_RSA const RSA_METHOD *meth1;#endif#ifndef OPENSSL_NO_DSA const DSA_METHOD *meth2;#endif#ifndef OPENSSL_NO_DH const DH_METHOD *meth3;#endif if(!ENGINE_set_id(e, engine_sureware_id) || !ENGINE_set_name(e, engine_sureware_name) ||#ifndef OPENSSL_NO_RSA !ENGINE_set_RSA(e, &surewarehk_rsa) ||#endif#ifndef OPENSSL_NO_DSA !ENGINE_set_DSA(e, &surewarehk_dsa) ||#endif#ifndef OPENSSL_NO_DH !ENGINE_set_DH(e, &surewarehk_dh) ||#endif !ENGINE_set_RAND(e, &surewarehk_rand) || !ENGINE_set_destroy_function(e, surewarehk_destroy) || !ENGINE_set_init_function(e, surewarehk_init) || !ENGINE_set_finish_function(e, surewarehk_finish) || !ENGINE_set_ctrl_function(e, surewarehk_ctrl) || !ENGINE_set_load_privkey_function(e, surewarehk_load_privkey) || !ENGINE_set_load_pubkey_function(e, surewarehk_load_pubkey)) return 0;#ifndef OPENSSL_NO_RSA /* We know that the "PKCS1_SSLeay()" functions hook properly * to the cswift-specific mod_exp and mod_exp_crt so we use * those functions. NB: We don't use ENGINE_openssl() or * anything "more generic" because something like the RSAref * code may not hook properly, and if you own one of these * cards then you have the right to do RSA operations on it * anyway! */ meth1 = RSA_PKCS1_SSLeay(); if (meth1) { surewarehk_rsa.rsa_pub_enc = meth1->rsa_pub_enc; surewarehk_rsa.rsa_pub_dec = meth1->rsa_pub_dec; }#endif#ifndef OPENSSL_NO_DSA /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish * bits. */ meth2 = DSA_OpenSSL(); if (meth2) { surewarehk_dsa.dsa_do_verify = meth2->dsa_do_verify; }#endif#ifndef OPENSSL_NO_DH /* Much the same for Diffie-Hellman */ meth3 = DH_OpenSSL(); if (meth3) { surewarehk_dh.generate_key = meth3->generate_key; surewarehk_dh.compute_key = meth3->compute_key; }#endif /* Ensure the sureware error handling is set up */ ERR_load_SUREWARE_strings(); return 1;}#ifndef OPENSSL_NO_DYNAMIC_ENGINEstatic int bind_helper(ENGINE *e, const char *id) { if(id && (strcmp(id, engine_sureware_id) != 0)) return 0; if(!bind_sureware(e)) return 0; return 1; } IMPLEMENT_DYNAMIC_CHECK_FN()IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)#elsestatic ENGINE *engine_sureware(void) { ENGINE *ret = ENGINE_new(); if(!ret) return NULL; if(!bind_sureware(ret)) { ENGINE_free(ret); return NULL; } return ret; }void ENGINE_load_sureware(void) { /* Copied from eng_[openssl|dyn].c */ ENGINE *toadd = engine_sureware(); if(!toadd) return; ENGINE_add(toadd); ENGINE_free(toadd); ERR_clear_error(); }#endif/* This is a process-global DSO handle used for loading and unloading * the SureWareHook library. NB: This is only set (or unset) during an * init() or finish() call (reference counts permitting) and they're * operating with global locks, so this should be thread-safe * implicitly. */static DSO *surewarehk_dso = NULL;#ifndef OPENSSL_NO_RSAstatic int rsaHndidx = -1; /* Index for KM handle. Not really used yet. */#endif#ifndef OPENSSL_NO_DSAstatic int dsaHndidx = -1; /* Index for KM handle. Not really used yet. */#endif/* These are the function pointers that are (un)set when the library has * successfully (un)loaded. */static SureWareHook_Init_t *p_surewarehk_Init = NULL;static SureWareHook_Finish_t *p_surewarehk_Finish = NULL;static SureWareHook_Rand_Bytes_t *p_surewarehk_Rand_Bytes = NULL;static SureWareHook_Rand_Seed_t *p_surewarehk_Rand_Seed = NULL;static SureWareHook_Load_Privkey_t *p_surewarehk_Load_Privkey = NULL;static SureWareHook_Info_Pubkey_t *p_surewarehk_Info_Pubkey = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -