summaryrefslogtreecommitdiff
path: root/lib/libcrypto/engine
diff options
context:
space:
mode:
authorBob Beck <beck@cvs.openbsd.org>2000-12-15 02:58:48 +0000
committerBob Beck <beck@cvs.openbsd.org>2000-12-15 02:58:48 +0000
commit01c37e03996dffeff1618614755c6cc676899acb (patch)
tree988002fb9b1d859c10890511aa5273cfb00662dc /lib/libcrypto/engine
parent9865f3ff77de9cfef0c5c8b0470daf6faa2f14af (diff)
openssl-engine-0.9.6 merge
Diffstat (limited to 'lib/libcrypto/engine')
-rw-r--r--lib/libcrypto/engine/engine_err.c183
-rw-r--r--lib/libcrypto/engine/engine_int.h160
-rw-r--r--lib/libcrypto/engine/engine_lib.c488
-rw-r--r--lib/libcrypto/engine/engine_list.c675
-rw-r--r--lib/libcrypto/engine/engine_openssl.c174
5 files changed, 1680 insertions, 0 deletions
diff --git a/lib/libcrypto/engine/engine_err.c b/lib/libcrypto/engine/engine_err.c
new file mode 100644
index 00000000000..0d7a31f6d51
--- /dev/null
+++ b/lib/libcrypto/engine/engine_err.c
@@ -0,0 +1,183 @@
+/* crypto/engine/engine_err.c */
+/* ====================================================================
+ * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
+ *
+ * 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
+ * openssl-core@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/)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
+ * EXPRESSED 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 THE OpenSSL PROJECT OR
+ * ITS CONTRIBUTORS 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.
+ * ====================================================================
+ *
+ * This product includes cryptographic software written by Eric Young
+ * (eay@cryptsoft.com). This product includes software written by Tim
+ * Hudson (tjh@cryptsoft.com).
+ *
+ */
+
+/* NOTE: this file was auto generated by the mkerr.pl script: any changes
+ * made to it will be overwritten when the script next updates this file,
+ * only reason strings will be preserved.
+ */
+
+#include <stdio.h>
+#include <openssl/err.h>
+#include <openssl/engine.h>
+
+/* BEGIN ERROR CODES */
+#ifndef NO_ERR
+static ERR_STRING_DATA ENGINE_str_functs[]=
+ {
+{ERR_PACK(0,ENGINE_F_ATALLA_FINISH,0), "ATALLA_FINISH"},
+{ERR_PACK(0,ENGINE_F_ATALLA_INIT,0), "ATALLA_INIT"},
+{ERR_PACK(0,ENGINE_F_ATALLA_MOD_EXP,0), "ATALLA_MOD_EXP"},
+{ERR_PACK(0,ENGINE_F_ATALLA_RSA_MOD_EXP,0), "ATALLA_RSA_MOD_EXP"},
+{ERR_PACK(0,ENGINE_F_CSWIFT_DSA_SIGN,0), "CSWIFT_DSA_SIGN"},
+{ERR_PACK(0,ENGINE_F_CSWIFT_DSA_VERIFY,0), "CSWIFT_DSA_VERIFY"},
+{ERR_PACK(0,ENGINE_F_CSWIFT_FINISH,0), "CSWIFT_FINISH"},
+{ERR_PACK(0,ENGINE_F_CSWIFT_INIT,0), "CSWIFT_INIT"},
+{ERR_PACK(0,ENGINE_F_CSWIFT_MOD_EXP,0), "CSWIFT_MOD_EXP"},
+{ERR_PACK(0,ENGINE_F_CSWIFT_MOD_EXP_CRT,0), "CSWIFT_MOD_EXP_CRT"},
+{ERR_PACK(0,ENGINE_F_CSWIFT_RSA_MOD_EXP,0), "CSWIFT_RSA_MOD_EXP"},
+{ERR_PACK(0,ENGINE_F_ENGINE_ADD,0), "ENGINE_add"},
+{ERR_PACK(0,ENGINE_F_ENGINE_BY_ID,0), "ENGINE_by_id"},
+{ERR_PACK(0,ENGINE_F_ENGINE_CTRL,0), "ENGINE_ctrl"},
+{ERR_PACK(0,ENGINE_F_ENGINE_FINISH,0), "ENGINE_finish"},
+{ERR_PACK(0,ENGINE_F_ENGINE_FREE,0), "ENGINE_free"},
+{ERR_PACK(0,ENGINE_F_ENGINE_GET_BN_MOD_EXP,0), "ENGINE_get_BN_mod_exp"},
+{ERR_PACK(0,ENGINE_F_ENGINE_GET_BN_MOD_EXP_CRT,0), "ENGINE_get_BN_mod_exp_crt"},
+{ERR_PACK(0,ENGINE_F_ENGINE_GET_CTRL_FUNCTION,0), "ENGINE_get_ctrl_function"},
+{ERR_PACK(0,ENGINE_F_ENGINE_GET_DH,0), "ENGINE_get_DH"},
+{ERR_PACK(0,ENGINE_F_ENGINE_GET_DSA,0), "ENGINE_get_DSA"},
+{ERR_PACK(0,ENGINE_F_ENGINE_GET_FINISH_FUNCTION,0), "ENGINE_get_finish_function"},
+{ERR_PACK(0,ENGINE_F_ENGINE_GET_ID,0), "ENGINE_get_id"},
+{ERR_PACK(0,ENGINE_F_ENGINE_GET_INIT_FUNCTION,0), "ENGINE_get_init_function"},
+{ERR_PACK(0,ENGINE_F_ENGINE_GET_NAME,0), "ENGINE_get_name"},
+{ERR_PACK(0,ENGINE_F_ENGINE_GET_NEXT,0), "ENGINE_get_next"},
+{ERR_PACK(0,ENGINE_F_ENGINE_GET_PREV,0), "ENGINE_get_prev"},
+{ERR_PACK(0,ENGINE_F_ENGINE_GET_RAND,0), "ENGINE_get_RAND"},
+{ERR_PACK(0,ENGINE_F_ENGINE_GET_RSA,0), "ENGINE_get_RSA"},
+{ERR_PACK(0,ENGINE_F_ENGINE_INIT,0), "ENGINE_init"},
+{ERR_PACK(0,ENGINE_F_ENGINE_LIST_ADD,0), "ENGINE_LIST_ADD"},
+{ERR_PACK(0,ENGINE_F_ENGINE_LIST_REMOVE,0), "ENGINE_LIST_REMOVE"},
+{ERR_PACK(0,ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,0), "ENGINE_load_private_key"},
+{ERR_PACK(0,ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,0), "ENGINE_load_public_key"},
+{ERR_PACK(0,ENGINE_F_ENGINE_NEW,0), "ENGINE_new"},
+{ERR_PACK(0,ENGINE_F_ENGINE_REMOVE,0), "ENGINE_remove"},
+{ERR_PACK(0,ENGINE_F_ENGINE_SET_BN_MOD_EXP,0), "ENGINE_set_BN_mod_exp"},
+{ERR_PACK(0,ENGINE_F_ENGINE_SET_BN_MOD_EXP_CRT,0), "ENGINE_set_BN_mod_exp_crt"},
+{ERR_PACK(0,ENGINE_F_ENGINE_SET_CTRL_FUNCTION,0), "ENGINE_set_ctrl_function"},
+{ERR_PACK(0,ENGINE_F_ENGINE_SET_DEFAULT_TYPE,0), "ENGINE_SET_DEFAULT_TYPE"},
+{ERR_PACK(0,ENGINE_F_ENGINE_SET_DH,0), "ENGINE_set_DH"},
+{ERR_PACK(0,ENGINE_F_ENGINE_SET_DSA,0), "ENGINE_set_DSA"},
+{ERR_PACK(0,ENGINE_F_ENGINE_SET_FINISH_FUNCTION,0), "ENGINE_set_finish_function"},
+{ERR_PACK(0,ENGINE_F_ENGINE_SET_ID,0), "ENGINE_set_id"},
+{ERR_PACK(0,ENGINE_F_ENGINE_SET_INIT_FUNCTION,0), "ENGINE_set_init_function"},
+{ERR_PACK(0,ENGINE_F_ENGINE_SET_NAME,0), "ENGINE_set_name"},
+{ERR_PACK(0,ENGINE_F_ENGINE_SET_RAND,0), "ENGINE_set_RAND"},
+{ERR_PACK(0,ENGINE_F_ENGINE_SET_RSA,0), "ENGINE_set_RSA"},
+{ERR_PACK(0,ENGINE_F_ENGINE_UNLOAD_KEY,0), "ENGINE_UNLOAD_KEY"},
+{ERR_PACK(0,ENGINE_F_HWCRHK_CTRL,0), "HWCRHK_CTRL"},
+{ERR_PACK(0,ENGINE_F_HWCRHK_FINISH,0), "HWCRHK_FINISH"},
+{ERR_PACK(0,ENGINE_F_HWCRHK_GET_PASS,0), "HWCRHK_GET_PASS"},
+{ERR_PACK(0,ENGINE_F_HWCRHK_INIT,0), "HWCRHK_INIT"},
+{ERR_PACK(0,ENGINE_F_HWCRHK_LOAD_PRIVKEY,0), "HWCRHK_LOAD_PRIVKEY"},
+{ERR_PACK(0,ENGINE_F_HWCRHK_LOAD_PUBKEY,0), "HWCRHK_LOAD_PUBKEY"},
+{ERR_PACK(0,ENGINE_F_HWCRHK_MOD_EXP,0), "HWCRHK_MOD_EXP"},
+{ERR_PACK(0,ENGINE_F_HWCRHK_MOD_EXP_CRT,0), "HWCRHK_MOD_EXP_CRT"},
+{ERR_PACK(0,ENGINE_F_HWCRHK_RAND_BYTES,0), "HWCRHK_RAND_BYTES"},
+{ERR_PACK(0,ENGINE_F_HWCRHK_RSA_MOD_EXP,0), "HWCRHK_RSA_MOD_EXP"},
+{ERR_PACK(0,ENGINE_F_LOG_MESSAGE,0), "LOG_MESSAGE"},
+{0,NULL}
+ };
+
+static ERR_STRING_DATA ENGINE_str_reasons[]=
+ {
+{ENGINE_R_ALREADY_LOADED ,"already loaded"},
+{ENGINE_R_BIO_WAS_FREED ,"bio was freed"},
+{ENGINE_R_BN_CTX_FULL ,"BN_CTX full"},
+{ENGINE_R_BN_EXPAND_FAIL ,"bn_expand fail"},
+{ENGINE_R_CHIL_ERROR ,"chil error"},
+{ENGINE_R_CONFLICTING_ENGINE_ID ,"conflicting engine id"},
+{ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED ,"ctrl command not implemented"},
+{ENGINE_R_DSO_FAILURE ,"DSO failure"},
+{ENGINE_R_ENGINE_IS_NOT_IN_LIST ,"engine is not in the list"},
+{ENGINE_R_FAILED_LOADING_PRIVATE_KEY ,"failed loading private key"},
+{ENGINE_R_FAILED_LOADING_PUBLIC_KEY ,"failed loading public key"},
+{ENGINE_R_FINISH_FAILED ,"finish failed"},
+{ENGINE_R_GET_HANDLE_FAILED ,"could not obtain hardware handle"},
+{ENGINE_R_ID_OR_NAME_MISSING ,"'id' or 'name' missing"},
+{ENGINE_R_INIT_FAILED ,"init failed"},
+{ENGINE_R_INTERNAL_LIST_ERROR ,"internal list error"},
+{ENGINE_R_MISSING_KEY_COMPONENTS ,"missing key components"},
+{ENGINE_R_NOT_INITIALISED ,"not initialised"},
+{ENGINE_R_NOT_LOADED ,"not loaded"},
+{ENGINE_R_NO_CALLBACK ,"no callback"},
+{ENGINE_R_NO_CONTROL_FUNCTION ,"no control function"},
+{ENGINE_R_NO_KEY ,"no key"},
+{ENGINE_R_NO_LOAD_FUNCTION ,"no load function"},
+{ENGINE_R_NO_REFERENCE ,"no reference"},
+{ENGINE_R_NO_SUCH_ENGINE ,"no such engine"},
+{ENGINE_R_NO_UNLOAD_FUNCTION ,"no unload function"},
+{ENGINE_R_PROVIDE_PARAMETERS ,"provide parameters"},
+{ENGINE_R_REQUEST_FAILED ,"request failed"},
+{ENGINE_R_REQUEST_FALLBACK ,"request fallback"},
+{ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL ,"size too large or too small"},
+{ENGINE_R_UNIT_FAILURE ,"unit failure"},
+{0,NULL}
+ };
+
+#endif
+
+void ERR_load_ENGINE_strings(void)
+ {
+ static int init=1;
+
+ if (init)
+ {
+ init=0;
+#ifndef NO_ERR
+ ERR_load_strings(ERR_LIB_ENGINE,ENGINE_str_functs);
+ ERR_load_strings(ERR_LIB_ENGINE,ENGINE_str_reasons);
+#endif
+
+ }
+ }
diff --git a/lib/libcrypto/engine/engine_int.h b/lib/libcrypto/engine/engine_int.h
new file mode 100644
index 00000000000..447fa2a3208
--- /dev/null
+++ b/lib/libcrypto/engine/engine_int.h
@@ -0,0 +1,160 @@
+/* crypto/engine/engine_int.h */
+/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
+ * project 2000.
+ */
+/* ====================================================================
+ * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
+ *
+ * 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/)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
+ * EXPRESSED 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 THE OpenSSL PROJECT OR
+ * ITS CONTRIBUTORS 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.
+ * ====================================================================
+ *
+ * This product includes cryptographic software written by Eric Young
+ * (eay@cryptsoft.com). This product includes software written by Tim
+ * Hudson (tjh@cryptsoft.com).
+ *
+ */
+
+#ifndef HEADER_ENGINE_INT_H
+#define HEADER_ENGINE_INT_H
+
+#include <openssl/rsa.h>
+#include <openssl/dsa.h>
+#include <openssl/dh.h>
+#include <openssl/rand.h>
+#include <openssl/bn.h>
+#include <openssl/evp.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Bitwise OR-able values for the "flags" variable in ENGINE. */
+#define ENGINE_FLAGS_MALLOCED 0x0001
+
+#ifndef HEADER_ENGINE_H
+/* Regrettably, we need to reproduce the "BN" function types here
+ * because there is no such "BIGNUM_METHOD" as there is with RSA,
+ * DSA, etc. We do this so that we don't have a case where engine.h
+ * and engine_int.h conflict with each other. */
+typedef int (*BN_MOD_EXP)(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
+ const BIGNUM *m, BN_CTX *ctx);
+
+/* private key operation for RSA, provided seperately in case other
+ * RSA implementations wish to use it. */
+typedef int (*BN_MOD_EXP_CRT)(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
+ const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1,
+ const BIGNUM *iqmp, BN_CTX *ctx);
+
+/* Generic function pointer */
+typedef int (*ENGINE_GEN_FUNC_PTR)();
+/* Generic function pointer taking no arguments */
+typedef int (*ENGINE_GEN_INT_FUNC_PTR)(void);
+/* Specific control function pointer */
+typedef int (*ENGINE_CTRL_FUNC_PTR)(int cmd, long i, void *p, void (*f)());
+
+#endif
+
+/* This is a structure for storing implementations of various crypto
+ * algorithms and functions. */
+typedef struct engine_st
+ {
+ const char *id;
+ const char *name;
+ RSA_METHOD *rsa_meth;
+ DSA_METHOD *dsa_meth;
+ DH_METHOD *dh_meth;
+ RAND_METHOD *rand_meth;
+ BN_MOD_EXP bn_mod_exp;
+ BN_MOD_EXP_CRT bn_mod_exp_crt;
+ int (*init)(void);
+ int (*finish)(void);
+ int (*ctrl)(int cmd, long i, void *p, void (*f)());
+ EVP_PKEY *(*load_privkey)(const char *key_id, const char *passphrase);
+ EVP_PKEY *(*load_pubkey)(const char *key_id, const char *passphrase);
+ int flags;
+ /* reference count on the structure itself */
+ int struct_ref;
+ /* reference count on usability of the engine type. NB: This
+ * controls the loading and initialisation of any functionlity
+ * required by this engine, whereas the previous count is
+ * simply to cope with (de)allocation of this structure. Hence,
+ * running_ref <= struct_ref at all times. */
+ int funct_ref;
+ /* Used to maintain the linked-list of engines. */
+ struct engine_st *prev;
+ struct engine_st *next;
+ } ENGINE;
+
+/* BUILT-IN ENGINES. (these functions are only ever called once and
+ * do not return references - they are purely for bootstrapping). */
+
+/* Returns a structure of software only methods (the default). */
+ENGINE *ENGINE_openssl();
+
+#ifndef NO_HW
+
+#ifndef NO_HW_CSWIFT
+/* Returns a structure of cswift methods ... NB: This can exist and be
+ * "used" even on non-cswift systems because the "init" will fail if the
+ * card/library are not found. */
+ENGINE *ENGINE_cswift();
+#endif /* !NO_HW_CSWIFT */
+
+#ifndef NO_HW_NCIPHER
+ENGINE *ENGINE_ncipher();
+#endif /* !NO_HW_NCIPHER */
+
+#ifndef NO_HW_ATALLA
+/* Returns a structure of atalla methods. */
+ENGINE *ENGINE_atalla();
+#endif /* !NO_HW_ATALLA */
+
+#endif /* !NO_HW */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* HEADER_ENGINE_INT_H */
diff --git a/lib/libcrypto/engine/engine_lib.c b/lib/libcrypto/engine/engine_lib.c
new file mode 100644
index 00000000000..1df07af03a6
--- /dev/null
+++ b/lib/libcrypto/engine/engine_lib.c
@@ -0,0 +1,488 @@
+/* crypto/engine/engine_lib.c */
+/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
+ * project 2000.
+ */
+/* ====================================================================
+ * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
+ *
+ * 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/)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
+ * EXPRESSED 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 THE OpenSSL PROJECT OR
+ * ITS CONTRIBUTORS 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.
+ * ====================================================================
+ *
+ * This product includes cryptographic software written by Eric Young
+ * (eay@cryptsoft.com). This product includes software written by Tim
+ * Hudson (tjh@cryptsoft.com).
+ *
+ */
+
+#include <openssl/crypto.h>
+#include "cryptlib.h"
+#include "engine_int.h"
+#include <openssl/engine.h>
+
+/* These pointers each have their own "functional reference" when they
+ * are non-NULL. Similarly, when they are retrieved by a call to
+ * ENGINE_get_default_[RSA|DSA|...] the returned pointer is also a
+ * reference and the caller is responsible for freeing that when they
+ * are finished with it (with a call to ENGINE_finish() *NOT* just
+ * ENGINE_free()!!!!!!). */
+static ENGINE *engine_def_rsa = NULL;
+static ENGINE *engine_def_dsa = NULL;
+static ENGINE *engine_def_dh = NULL;
+static ENGINE *engine_def_rand = NULL;
+static ENGINE *engine_def_bn_mod_exp = NULL;
+static ENGINE *engine_def_bn_mod_exp_crt = NULL;
+/* A static "once-only" flag used to control if/when the above were
+ * initialised to suitable start-up defaults. */
+static int engine_def_flag = 0;
+
+/* This is used in certain static utility functions to save code
+ * repetition for per-algorithm functions. */
+typedef enum {
+ ENGINE_TYPE_RSA,
+ ENGINE_TYPE_DSA,
+ ENGINE_TYPE_DH,
+ ENGINE_TYPE_RAND,
+ ENGINE_TYPE_BN_MOD_EXP,
+ ENGINE_TYPE_BN_MOD_EXP_CRT
+ } ENGINE_TYPE;
+
+static void engine_def_check_util(ENGINE **def, ENGINE *val)
+ {
+ *def = val;
+ val->struct_ref++;
+ val->funct_ref++;
+ }
+
+/* In a slight break with convention - this static function must be
+ * called *outside* any locking of CRYPTO_LOCK_ENGINE. */
+static void engine_def_check(void)
+ {
+ ENGINE *e;
+ if(engine_def_flag)
+ return;
+ e = ENGINE_get_first();
+ if(e == NULL)
+ /* The list is empty ... not much we can do! */
+ return;
+ /* We have a structural reference, see if getting a functional
+ * reference is possible. This is done to cope with init errors
+ * in the engine - the following locked code does a bunch of
+ * manual "ENGINE_init"s which do *not* allow such an init
+ * error so this is worth doing. */
+ if(ENGINE_init(e))
+ {
+ CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+ /* Doing another check here prevents an obvious race
+ * condition because the whole function itself cannot
+ * be locked. */
+ if(engine_def_flag)
+ goto skip_set_defaults;
+ /* OK, we got a functional reference, so we get one each
+ * for the defaults too. */
+ engine_def_check_util(&engine_def_rsa, e);
+ engine_def_check_util(&engine_def_dsa, e);
+ engine_def_check_util(&engine_def_dh, e);
+ engine_def_check_util(&engine_def_rand, e);
+ engine_def_check_util(&engine_def_bn_mod_exp, e);
+ engine_def_check_util(&engine_def_bn_mod_exp_crt, e);
+ engine_def_flag = 1;
+skip_set_defaults:
+ CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+ /* The "if" needs to be balanced out. */
+ ENGINE_finish(e);
+ }
+ /* We need to balance out the fact we obtained a structural
+ * reference to begin with from ENGINE_get_first(). */
+ ENGINE_free(e);
+ }
+
+/* Initialise a engine type for use (or up its functional reference count
+ * if it's already in use). */
+int ENGINE_init(ENGINE *e)
+ {
+ int to_return = 1;
+
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_INIT,ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+ if((e->funct_ref == 0) && e->init)
+ /* This is the first functional reference and the engine
+ * requires initialisation so we do it now. */
+ to_return = e->init();
+ if(to_return)
+ {
+ /* OK, we return a functional reference which is also a
+ * structural reference. */
+ e->struct_ref++;
+ e->funct_ref++;
+ }
+ CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+ return to_return;
+ }
+
+/* Free a functional reference to a engine type */
+int ENGINE_finish(ENGINE *e)
+ {
+ int to_return = 1;
+
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_FINISH,ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+ if((e->funct_ref == 1) && e->finish)
+#if 0
+ /* This is the last functional reference and the engine
+ * requires cleanup so we do it now. */
+ to_return = e->finish();
+ if(to_return)
+ {
+ /* Cleanup the functional reference which is also a
+ * structural reference. */
+ e->struct_ref--;
+ e->funct_ref--;
+ }
+ CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+#else
+ /* I'm going to deliberately do a convoluted version of this
+ * piece of code because we don't want "finish" functions
+ * being called inside a locked block of code, if at all
+ * possible. I'd rather have this call take an extra couple
+ * of ticks than have throughput serialised on a externally-
+ * provided callback function that may conceivably never come
+ * back. :-( */
+ {
+ CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+ /* CODE ALERT: This *IS* supposed to be "=" and NOT "==" :-) */
+ if((to_return = e->finish()))
+ {
+ CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+ /* Cleanup the functional reference which is also a
+ * structural reference. */
+ e->struct_ref--;
+ e->funct_ref--;
+ CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+ }
+ }
+ else
+ CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+#endif
+ return to_return;
+ }
+
+EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
+ const char *passphrase)
+ {
+ EVP_PKEY *pkey;
+
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+ if(e->funct_ref == 0)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
+ ENGINE_R_NOT_INITIALISED);
+ return 0;
+ }
+ if (!e->load_privkey)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
+ ENGINE_R_NO_LOAD_FUNCTION);
+ return 0;
+ }
+ CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+ pkey = e->load_privkey(key_id, passphrase);
+ if (!pkey)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
+ ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
+ return 0;
+ }
+ return pkey;
+ }
+
+EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
+ const char *passphrase)
+ {
+ EVP_PKEY *pkey;
+
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+ if(e->funct_ref == 0)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
+ ENGINE_R_NOT_INITIALISED);
+ return 0;
+ }
+ if (!e->load_pubkey)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
+ ENGINE_R_NO_LOAD_FUNCTION);
+ return 0;
+ }
+ CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+ pkey = e->load_pubkey(key_id, passphrase);
+ if (!pkey)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
+ ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
+ return 0;
+ }
+ return pkey;
+ }
+
+/* Initialise a engine type for use (or up its functional reference count
+ * if it's already in use). */
+int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
+ {
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_CTRL,ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+ if(e->struct_ref == 0)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_REFERENCE);
+ return 0;
+ }
+ if (!e->ctrl)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_CONTROL_FUNCTION);
+ return 0;
+ }
+ CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+ return e->ctrl(cmd, i, p, f);
+ }
+
+static ENGINE *engine_get_default_type(ENGINE_TYPE t)
+ {
+ ENGINE *ret = NULL;
+
+ /* engine_def_check is lean and mean and won't replace any
+ * prior default engines ... so we must ensure that it is always
+ * the first function to get to touch the default values. */
+ engine_def_check();
+ CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+ switch(t)
+ {
+ case ENGINE_TYPE_RSA:
+ ret = engine_def_rsa; break;
+ case ENGINE_TYPE_DSA:
+ ret = engine_def_dsa; break;
+ case ENGINE_TYPE_DH:
+ ret = engine_def_dh; break;
+ case ENGINE_TYPE_RAND:
+ ret = engine_def_rand; break;
+ case ENGINE_TYPE_BN_MOD_EXP:
+ ret = engine_def_bn_mod_exp; break;
+ case ENGINE_TYPE_BN_MOD_EXP_CRT:
+ ret = engine_def_bn_mod_exp_crt; break;
+ }
+ /* Unforunately we can't do this work outside the lock with a
+ * call to ENGINE_init() because that would leave a race
+ * condition open. */
+ if(ret)
+ {
+ ret->struct_ref++;
+ ret->funct_ref++;
+ }
+ CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+ return ret;
+ }
+
+ENGINE *ENGINE_get_default_RSA(void)
+ {
+ return engine_get_default_type(ENGINE_TYPE_RSA);
+ }
+
+ENGINE *ENGINE_get_default_DSA(void)
+ {
+ return engine_get_default_type(ENGINE_TYPE_DSA);
+ }
+
+ENGINE *ENGINE_get_default_DH(void)
+ {
+ return engine_get_default_type(ENGINE_TYPE_DH);
+ }
+
+ENGINE *ENGINE_get_default_RAND(void)
+ {
+ return engine_get_default_type(ENGINE_TYPE_RAND);
+ }
+
+ENGINE *ENGINE_get_default_BN_mod_exp(void)
+ {
+ return engine_get_default_type(ENGINE_TYPE_BN_MOD_EXP);
+ }
+
+ENGINE *ENGINE_get_default_BN_mod_exp_crt(void)
+ {
+ return engine_get_default_type(ENGINE_TYPE_BN_MOD_EXP_CRT);
+ }
+
+static int engine_set_default_type(ENGINE_TYPE t, ENGINE *e)
+ {
+ ENGINE *old = NULL;
+
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_SET_DEFAULT_TYPE,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ /* engine_def_check is lean and mean and won't replace any
+ * prior default engines ... so we must ensure that it is always
+ * the first function to get to touch the default values. */
+ engine_def_check();
+ /* Attempt to get a functional reference (we need one anyway, but
+ * also, 'e' may be just a structural reference being passed in so
+ * this call may actually be the first). */
+ if(!ENGINE_init(e))
+ {
+ ENGINEerr(ENGINE_F_ENGINE_SET_DEFAULT_TYPE,
+ ENGINE_R_INIT_FAILED);
+ return 0;
+ }
+ CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+ switch(t)
+ {
+ case ENGINE_TYPE_RSA:
+ old = engine_def_rsa;
+ engine_def_rsa = e; break;
+ case ENGINE_TYPE_DSA:
+ old = engine_def_dsa;
+ engine_def_dsa = e; break;
+ case ENGINE_TYPE_DH:
+ old = engine_def_dh;
+ engine_def_dh = e; break;
+ case ENGINE_TYPE_RAND:
+ old = engine_def_rand;
+ engine_def_rand = e; break;
+ case ENGINE_TYPE_BN_MOD_EXP:
+ old = engine_def_bn_mod_exp;
+ engine_def_bn_mod_exp = e; break;
+ case ENGINE_TYPE_BN_MOD_EXP_CRT:
+ old = engine_def_bn_mod_exp_crt;
+ engine_def_bn_mod_exp_crt = e; break;
+ }
+ CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+ /* If we've replaced a previous value, then we need to remove the
+ * functional reference we had. */
+ if(old && !ENGINE_finish(old))
+ {
+ ENGINEerr(ENGINE_F_ENGINE_SET_DEFAULT_TYPE,
+ ENGINE_R_FINISH_FAILED);
+ return 0;
+ }
+ return 1;
+ }
+
+int ENGINE_set_default_RSA(ENGINE *e)
+ {
+ return engine_set_default_type(ENGINE_TYPE_RSA, e);
+ }
+
+int ENGINE_set_default_DSA(ENGINE *e)
+ {
+ return engine_set_default_type(ENGINE_TYPE_DSA, e);
+ }
+
+int ENGINE_set_default_DH(ENGINE *e)
+ {
+ return engine_set_default_type(ENGINE_TYPE_DH, e);
+ }
+
+int ENGINE_set_default_RAND(ENGINE *e)
+ {
+ return engine_set_default_type(ENGINE_TYPE_RAND, e);
+ }
+
+int ENGINE_set_default_BN_mod_exp(ENGINE *e)
+ {
+ return engine_set_default_type(ENGINE_TYPE_BN_MOD_EXP, e);
+ }
+
+int ENGINE_set_default_BN_mod_exp_crt(ENGINE *e)
+ {
+ return engine_set_default_type(ENGINE_TYPE_BN_MOD_EXP_CRT, e);
+ }
+
+int ENGINE_set_default(ENGINE *e, unsigned int flags)
+ {
+ if((flags & ENGINE_METHOD_RSA) && e->rsa_meth &&
+ !ENGINE_set_default_RSA(e))
+ return 0;
+ if((flags & ENGINE_METHOD_DSA) && e->dsa_meth &&
+ !ENGINE_set_default_DSA(e))
+ return 0;
+ if((flags & ENGINE_METHOD_DH) && e->dh_meth &&
+ !ENGINE_set_default_DH(e))
+ return 0;
+ if((flags & ENGINE_METHOD_RAND) && e->rand_meth &&
+ !ENGINE_set_default_RAND(e))
+ return 0;
+ if((flags & ENGINE_METHOD_BN_MOD_EXP) && e->bn_mod_exp &&
+ !ENGINE_set_default_BN_mod_exp(e))
+ return 0;
+ if((flags & ENGINE_METHOD_BN_MOD_EXP_CRT) && e->bn_mod_exp_crt &&
+ !ENGINE_set_default_BN_mod_exp_crt(e))
+ return 0;
+ return 1;
+ }
+
diff --git a/lib/libcrypto/engine/engine_list.c b/lib/libcrypto/engine/engine_list.c
new file mode 100644
index 00000000000..d764c606611
--- /dev/null
+++ b/lib/libcrypto/engine/engine_list.c
@@ -0,0 +1,675 @@
+/* crypto/engine/engine_list.c */
+/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
+ * project 2000.
+ */
+/* ====================================================================
+ * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
+ *
+ * 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/)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
+ * EXPRESSED 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 THE OpenSSL PROJECT OR
+ * ITS CONTRIBUTORS 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.
+ * ====================================================================
+ *
+ * This product includes cryptographic software written by Eric Young
+ * (eay@cryptsoft.com). This product includes software written by Tim
+ * Hudson (tjh@cryptsoft.com).
+ *
+ */
+
+#include <openssl/crypto.h>
+#include "cryptlib.h"
+#include "engine_int.h"
+#include <openssl/engine.h>
+
+/* The linked-list of pointers to engine types. engine_list_head
+ * incorporates an implicit structural reference but engine_list_tail
+ * does not - the latter is a computational niceity and only points
+ * to something that is already pointed to by its predecessor in the
+ * list (or engine_list_head itself). In the same way, the use of the
+ * "prev" pointer in each ENGINE is to save excessive list iteration,
+ * it doesn't correspond to an extra structural reference. Hence,
+ * engine_list_head, and each non-null "next" pointer account for
+ * the list itself assuming exactly 1 structural reference on each
+ * list member. */
+static ENGINE *engine_list_head = NULL;
+static ENGINE *engine_list_tail = NULL;
+/* A boolean switch, used to ensure we only initialise once. This
+ * is needed because the engine list may genuinely become empty during
+ * use (so we can't use engine_list_head as an indicator for example. */
+static int engine_list_flag = 0;
+
+/* These static functions starting with a lower case "engine_" always
+ * take place when CRYPTO_LOCK_ENGINE has been locked up. */
+static int engine_list_add(ENGINE *e)
+ {
+ int conflict = 0;
+ ENGINE *iterator = NULL;
+
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ iterator = engine_list_head;
+ while(iterator && !conflict)
+ {
+ conflict = (strcmp(iterator->id, e->id) == 0);
+ iterator = iterator->next;
+ }
+ if(conflict)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
+ ENGINE_R_CONFLICTING_ENGINE_ID);
+ return 0;
+ }
+ if(engine_list_head == NULL)
+ {
+ /* We are adding to an empty list. */
+ if(engine_list_tail)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
+ ENGINE_R_INTERNAL_LIST_ERROR);
+ return 0;
+ }
+ engine_list_head = e;
+ e->prev = NULL;
+ }
+ else
+ {
+ /* We are adding to the tail of an existing list. */
+ if((engine_list_tail == NULL) ||
+ (engine_list_tail->next != NULL))
+ {
+ ENGINEerr(ENGINE_F_ENGINE_LIST_ADD,
+ ENGINE_R_INTERNAL_LIST_ERROR);
+ return 0;
+ }
+ engine_list_tail->next = e;
+ e->prev = engine_list_tail;
+ }
+ /* Having the engine in the list assumes a structural
+ * reference. */
+ e->struct_ref++;
+ /* However it came to be, e is the last item in the list. */
+ engine_list_tail = e;
+ e->next = NULL;
+ return 1;
+ }
+
+static int engine_list_remove(ENGINE *e)
+ {
+ ENGINE *iterator;
+
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ /* We need to check that e is in our linked list! */
+ iterator = engine_list_head;
+ while(iterator && (iterator != e))
+ iterator = iterator->next;
+ if(iterator == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_LIST_REMOVE,
+ ENGINE_R_ENGINE_IS_NOT_IN_LIST);
+ return 0;
+ }
+ /* un-link e from the chain. */
+ if(e->next)
+ e->next->prev = e->prev;
+ if(e->prev)
+ e->prev->next = e->next;
+ /* Correct our head/tail if necessary. */
+ if(engine_list_head == e)
+ engine_list_head = e->next;
+ if(engine_list_tail == e)
+ engine_list_tail = e->prev;
+ /* remove our structural reference. */
+ e->struct_ref--;
+ return 1;
+ }
+
+/* This check always takes place with CRYPTO_LOCK_ENGINE locked up
+ * so we're synchronised, but we can't call anything that tries to
+ * lock it again! :-) NB: For convenience (and code-clarity) we
+ * don't output errors for failures of the engine_list_add function
+ * as it will generate errors itself. */
+static int engine_internal_check(void)
+ {
+ if(engine_list_flag)
+ return 1;
+ /* This is our first time up, we need to populate the list
+ * with our statically compiled-in engines. */
+ if(!engine_list_add(ENGINE_openssl()))
+ return 0;
+#ifndef NO_HW
+#ifndef NO_HW_CSWIFT
+ if(!engine_list_add(ENGINE_cswift()))
+ return 0;
+#endif /* !NO_HW_CSWIFT */
+#ifndef NO_HW_NCIPHER
+ if(!engine_list_add(ENGINE_ncipher()))
+ return 0;
+#endif /* !NO_HW_NCIPHER */
+#ifndef NO_HW_ATALLA
+ if(!engine_list_add(ENGINE_atalla()))
+ return 0;
+#endif /* !NO_HW_ATALLA */
+#endif /* !NO_HW */
+ engine_list_flag = 1;
+ return 1;
+ }
+
+/* Get the first/last "ENGINE" type available. */
+ENGINE *ENGINE_get_first(void)
+ {
+ ENGINE *ret = NULL;
+
+ CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
+ if(engine_internal_check())
+ {
+ ret = engine_list_head;
+ if(ret)
+ ret->struct_ref++;
+ }
+ CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
+ return ret;
+ }
+ENGINE *ENGINE_get_last(void)
+ {
+ ENGINE *ret = NULL;
+
+ CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
+ if(engine_internal_check())
+ {
+ ret = engine_list_tail;
+ if(ret)
+ ret->struct_ref++;
+ }
+ CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
+ return ret;
+ }
+
+/* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */
+ENGINE *ENGINE_get_next(ENGINE *e)
+ {
+ ENGINE *ret = NULL;
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_GET_NEXT,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
+ ret = e->next;
+ e->struct_ref--;
+ if(ret)
+ ret->struct_ref++;
+ CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
+ return ret;
+ }
+ENGINE *ENGINE_get_prev(ENGINE *e)
+ {
+ ENGINE *ret = NULL;
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_GET_PREV,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
+ ret = e->prev;
+ e->struct_ref--;
+ if(ret)
+ ret->struct_ref++;
+ CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
+ return ret;
+ }
+
+/* Add another "ENGINE" type into the list. */
+int ENGINE_add(ENGINE *e)
+ {
+ int to_return = 1;
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_ADD,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ if((e->id == NULL) || (e->name == NULL))
+ {
+ ENGINEerr(ENGINE_F_ENGINE_ADD,
+ ENGINE_R_ID_OR_NAME_MISSING);
+ }
+ CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+ if(!engine_internal_check() || !engine_list_add(e))
+ {
+ ENGINEerr(ENGINE_F_ENGINE_ADD,
+ ENGINE_R_INTERNAL_LIST_ERROR);
+ to_return = 0;
+ }
+ CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+ return to_return;
+ }
+
+/* Remove an existing "ENGINE" type from the array. */
+int ENGINE_remove(ENGINE *e)
+ {
+ int to_return = 1;
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_REMOVE,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
+ if(!engine_internal_check() || !engine_list_remove(e))
+ {
+ ENGINEerr(ENGINE_F_ENGINE_REMOVE,
+ ENGINE_R_INTERNAL_LIST_ERROR);
+ to_return = 0;
+ }
+ CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
+ return to_return;
+ }
+
+ENGINE *ENGINE_by_id(const char *id)
+ {
+ ENGINE *iterator = NULL;
+ if(id == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_BY_ID,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return NULL;
+ }
+ CRYPTO_r_lock(CRYPTO_LOCK_ENGINE);
+ if(!engine_internal_check())
+ ENGINEerr(ENGINE_F_ENGINE_BY_ID,
+ ENGINE_R_INTERNAL_LIST_ERROR);
+ else
+ {
+ iterator = engine_list_head;
+ while(iterator && (strcmp(id, iterator->id) != 0))
+ iterator = iterator->next;
+ if(iterator)
+ /* We need to return a structural reference */
+ iterator->struct_ref++;
+ }
+ CRYPTO_r_unlock(CRYPTO_LOCK_ENGINE);
+ if(iterator == NULL)
+ ENGINEerr(ENGINE_F_ENGINE_BY_ID,
+ ENGINE_R_NO_SUCH_ENGINE);
+ return iterator;
+ }
+
+/* As per the comments in engine.h, it is generally better all round
+ * if the ENGINE structure is allocated within this framework. */
+#if 0
+int ENGINE_get_struct_size(void)
+ {
+ return sizeof(ENGINE);
+ }
+
+ENGINE *ENGINE_new(ENGINE *e)
+ {
+ ENGINE *ret;
+
+ if(e == NULL)
+ {
+ ret = (ENGINE *)(OPENSSL_malloc(sizeof(ENGINE));
+ if(ret == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_NEW,
+ ERR_R_MALLOC_FAILURE);
+ return NULL;
+ }
+ }
+ else
+ ret = e;
+ memset(ret, 0, sizeof(ENGINE));
+ if(e)
+ ret->flags = ENGINE_FLAGS_MALLOCED;
+ ret->struct_ref = 1;
+ return ret;
+ }
+#else
+ENGINE *ENGINE_new(void)
+ {
+ ENGINE *ret;
+
+ ret = (ENGINE *)OPENSSL_malloc(sizeof(ENGINE));
+ if(ret == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE);
+ return NULL;
+ }
+ memset(ret, 0, sizeof(ENGINE));
+ ret->flags = ENGINE_FLAGS_MALLOCED;
+ ret->struct_ref = 1;
+ return ret;
+ }
+#endif
+
+int ENGINE_free(ENGINE *e)
+ {
+ int i;
+
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_FREE,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ i = CRYPTO_add(&e->struct_ref,-1,CRYPTO_LOCK_ENGINE);
+#ifdef REF_PRINT
+ REF_PRINT("ENGINE",e);
+#endif
+ if (i > 0) return 1;
+#ifdef REF_CHECK
+ if (i < 0)
+ {
+ fprintf(stderr,"ENGINE_free, bad reference count\n");
+ abort();
+ }
+#endif
+ if(e->flags & ENGINE_FLAGS_MALLOCED)
+ OPENSSL_free(e);
+ return 1;
+ }
+
+int ENGINE_set_id(ENGINE *e, const char *id)
+ {
+ if((e == NULL) || (id == NULL))
+ {
+ ENGINEerr(ENGINE_F_ENGINE_SET_ID,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ e->id = id;
+ return 1;
+ }
+
+int ENGINE_set_name(ENGINE *e, const char *name)
+ {
+ if((e == NULL) || (name == NULL))
+ {
+ ENGINEerr(ENGINE_F_ENGINE_SET_NAME,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ e->name = name;
+ return 1;
+ }
+
+int ENGINE_set_RSA(ENGINE *e, RSA_METHOD *rsa_meth)
+ {
+ if((e == NULL) || (rsa_meth == NULL))
+ {
+ ENGINEerr(ENGINE_F_ENGINE_SET_RSA,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ e->rsa_meth = rsa_meth;
+ return 1;
+ }
+
+int ENGINE_set_DSA(ENGINE *e, DSA_METHOD *dsa_meth)
+ {
+ if((e == NULL) || (dsa_meth == NULL))
+ {
+ ENGINEerr(ENGINE_F_ENGINE_SET_DSA,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ e->dsa_meth = dsa_meth;
+ return 1;
+ }
+
+int ENGINE_set_DH(ENGINE *e, DH_METHOD *dh_meth)
+ {
+ if((e == NULL) || (dh_meth == NULL))
+ {
+ ENGINEerr(ENGINE_F_ENGINE_SET_DH,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ e->dh_meth = dh_meth;
+ return 1;
+ }
+
+int ENGINE_set_RAND(ENGINE *e, RAND_METHOD *rand_meth)
+ {
+ if((e == NULL) || (rand_meth == NULL))
+ {
+ ENGINEerr(ENGINE_F_ENGINE_SET_RAND,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ e->rand_meth = rand_meth;
+ return 1;
+ }
+
+int ENGINE_set_BN_mod_exp(ENGINE *e, BN_MOD_EXP bn_mod_exp)
+ {
+ if((e == NULL) || (bn_mod_exp == NULL))
+ {
+ ENGINEerr(ENGINE_F_ENGINE_SET_BN_MOD_EXP,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ e->bn_mod_exp = bn_mod_exp;
+ return 1;
+ }
+
+int ENGINE_set_BN_mod_exp_crt(ENGINE *e, BN_MOD_EXP_CRT bn_mod_exp_crt)
+ {
+ if((e == NULL) || (bn_mod_exp_crt == NULL))
+ {
+ ENGINEerr(ENGINE_F_ENGINE_SET_BN_MOD_EXP_CRT,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ e->bn_mod_exp_crt = bn_mod_exp_crt;
+ return 1;
+ }
+
+int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f)
+ {
+ if((e == NULL) || (init_f == NULL))
+ {
+ ENGINEerr(ENGINE_F_ENGINE_SET_INIT_FUNCTION,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ e->init = init_f;
+ return 1;
+ }
+
+int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f)
+ {
+ if((e == NULL) || (finish_f == NULL))
+ {
+ ENGINEerr(ENGINE_F_ENGINE_SET_FINISH_FUNCTION,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ e->finish = finish_f;
+ return 1;
+ }
+
+int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f)
+ {
+ if((e == NULL) || (ctrl_f == NULL))
+ {
+ ENGINEerr(ENGINE_F_ENGINE_SET_CTRL_FUNCTION,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ e->ctrl = ctrl_f;
+ return 1;
+ }
+
+const char *ENGINE_get_id(ENGINE *e)
+ {
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_GET_ID,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ return e->id;
+ }
+
+const char *ENGINE_get_name(ENGINE *e)
+ {
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_GET_NAME,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+ return e->name;
+ }
+
+RSA_METHOD *ENGINE_get_RSA(ENGINE *e)
+ {
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_GET_RSA,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return NULL;
+ }
+ return e->rsa_meth;
+ }
+
+DSA_METHOD *ENGINE_get_DSA(ENGINE *e)
+ {
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_GET_DSA,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return NULL;
+ }
+ return e->dsa_meth;
+ }
+
+DH_METHOD *ENGINE_get_DH(ENGINE *e)
+ {
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_GET_DH,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return NULL;
+ }
+ return e->dh_meth;
+ }
+
+RAND_METHOD *ENGINE_get_RAND(ENGINE *e)
+ {
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_GET_RAND,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return NULL;
+ }
+ return e->rand_meth;
+ }
+
+BN_MOD_EXP ENGINE_get_BN_mod_exp(ENGINE *e)
+ {
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_GET_BN_MOD_EXP,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return NULL;
+ }
+ return e->bn_mod_exp;
+ }
+
+BN_MOD_EXP_CRT ENGINE_get_BN_mod_exp_crt(ENGINE *e)
+ {
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_GET_BN_MOD_EXP_CRT,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return NULL;
+ }
+ return e->bn_mod_exp_crt;
+ }
+
+ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(ENGINE *e)
+ {
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_GET_INIT_FUNCTION,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return NULL;
+ }
+ return e->init;
+ }
+
+ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(ENGINE *e)
+ {
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_GET_FINISH_FUNCTION,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return NULL;
+ }
+ return e->finish;
+ }
+
+ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(ENGINE *e)
+ {
+ if(e == NULL)
+ {
+ ENGINEerr(ENGINE_F_ENGINE_GET_CTRL_FUNCTION,
+ ERR_R_PASSED_NULL_PARAMETER);
+ return NULL;
+ }
+ return e->ctrl;
+ }
+
diff --git a/lib/libcrypto/engine/engine_openssl.c b/lib/libcrypto/engine/engine_openssl.c
new file mode 100644
index 00000000000..9636f51168c
--- /dev/null
+++ b/lib/libcrypto/engine/engine_openssl.c
@@ -0,0 +1,174 @@
+/* crypto/engine/engine_openssl.c */
+/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
+ * project 2000.
+ */
+/* ====================================================================
+ * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
+ *
+ * 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/)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
+ * EXPRESSED 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 THE OpenSSL PROJECT OR
+ * ITS CONTRIBUTORS 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.
+ * ====================================================================
+ *
+ * This product includes cryptographic software written by Eric Young
+ * (eay@cryptsoft.com). This product includes software written by Tim
+ * Hudson (tjh@cryptsoft.com).
+ *
+ */
+
+
+#include <stdio.h>
+#include <openssl/crypto.h>
+#include "cryptlib.h"
+#include "engine_int.h"
+#include <openssl/engine.h>
+#include <openssl/dso.h>
+#include <openssl/rsa.h>
+#include <openssl/dsa.h>
+#include <openssl/dh.h>
+#include <openssl/rand.h>
+#include <openssl/bn.h>
+
+/* This is the only function we need to implement as OpenSSL
+ * doesn't have a native CRT mod_exp. Perhaps this should be
+ * BN_mod_exp_crt and moved into crypto/bn/ ?? ... dunno. */
+static int openssl_mod_exp_crt(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
+ const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1,
+ const BIGNUM *iqmp, BN_CTX *ctx);
+
+/* The ENGINE structure that can be pointed to. */
+static ENGINE engine_openssl =
+ {
+ "openssl",
+ "Software default engine support",
+ NULL,
+ NULL,
+ NULL, /* these methods are "stolen" in ENGINE_openssl() */
+ NULL,
+ NULL,
+ openssl_mod_exp_crt,
+ NULL, /* no init() */
+ NULL, /* no finish() */
+ NULL, /* no ctrl() */
+ NULL, /* no load_privkey() */
+ NULL, /* no load_pubkey() */
+ 0, /* no flags */
+ 0, 0, /* no references. */
+ NULL, NULL /* unlinked */
+ };
+
+/* As this is only ever called once, there's no need for locking
+ * (indeed - the lock will already be held by our caller!!!) */
+ENGINE *ENGINE_openssl()
+ {
+ /* We need to populate our structure with the software pointers
+ * that we want to steal. */
+ engine_openssl.rsa_meth = RSA_get_default_openssl_method();
+ engine_openssl.dsa_meth = DSA_get_default_openssl_method();
+ engine_openssl.dh_meth = DH_get_default_openssl_method();
+ engine_openssl.rand_meth = RAND_SSLeay();
+ engine_openssl.bn_mod_exp = BN_mod_exp;
+ return &engine_openssl;
+ }
+
+/* Chinese Remainder Theorem, taken and adapted from rsa_eay.c */
+static int openssl_mod_exp_crt(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
+ const BIGNUM *q, const BIGNUM *dmp1,
+ const BIGNUM *dmq1, const BIGNUM *iqmp, BN_CTX *ctx)
+ {
+ BIGNUM r1,m1;
+ int ret=0;
+ BN_CTX *bn_ctx;
+ BIGNUM *temp_bn = NULL;
+
+ if (ctx)
+ bn_ctx = ctx;
+ else
+ if ((bn_ctx=BN_CTX_new()) == NULL) goto err;
+ BN_init(&m1);
+ BN_init(&r1);
+ /* BN_mul() cannot accept const BIGNUMs so I use the BN_CTX
+ * to duplicate what I need. <sigh> */
+ if ((temp_bn = BN_CTX_get(bn_ctx)) == NULL) goto err;
+ if (!BN_copy(temp_bn, iqmp)) goto err;
+
+ if (!BN_mod(&r1, a, q, bn_ctx)) goto err;
+ if (!engine_openssl.bn_mod_exp(&m1, &r1, dmq1, q, bn_ctx))
+ goto err;
+
+ if (!BN_mod(&r1, a, p, bn_ctx)) goto err;
+ if (!engine_openssl.bn_mod_exp(r, &r1, dmp1, p, bn_ctx))
+ goto err;
+
+ if (!BN_sub(r, r, &m1)) goto err;
+ /* This will help stop the size of r0 increasing, which does
+ * affect the multiply if it optimised for a power of 2 size */
+ if (r->neg)
+ if (!BN_add(r, r, p)) goto err;
+
+ if (!BN_mul(&r1, r, temp_bn, bn_ctx)) goto err;
+ if (!BN_mod(r, &r1, p, bn_ctx)) goto err;
+ /* If p < q it is occasionally possible for the correction of
+ * adding 'p' if r is negative above to leave the result still
+ * negative. This can break the private key operations: the following
+ * second correction should *always* correct this rare occurrence.
+ * This will *never* happen with OpenSSL generated keys because
+ * they ensure p > q [steve]
+ */
+ if (r->neg)
+ if (!BN_add(r, r, p)) goto err;
+ /* Again, BN_mul() will need non-const values. */
+ if (!BN_copy(temp_bn, q)) goto err;
+ if (!BN_mul(&r1, r, temp_bn, bn_ctx)) goto err;
+ if (!BN_add(r, &r1, &m1)) goto err;
+
+ ret=1;
+err:
+ BN_clear_free(&m1);
+ BN_clear_free(&r1);
+ if (temp_bn)
+ bn_ctx->tos--;
+ if (!ctx)
+ BN_CTX_free(bn_ctx);
+ return(ret);
+ }