1 /* 2 * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved. 3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved 4 * 5 * Licensed under the OpenSSL license (the "License"). You may not use 6 * this file except in compliance with the License. You can obtain a copy 7 * in the file LICENSE in the source distribution or at 8 * https://www.openssl.org/source/license.html 9 */ 10 11 #ifndef OSSL_CRYPTO_ENGINE_ENG_LOCAL_H 12 # define OSSL_CRYPTO_ENGINE_ENG_LOCAL_H 13 14 # include "internal/cryptlib.h" 15 # include "crypto/engine.h" 16 # include "internal/thread_once.h" 17 # include "internal/refcount.h" 18 19 extern CRYPTO_RWLOCK *global_engine_lock; 20 21 /* 22 * If we compile with this symbol defined, then both reference counts in the 23 * ENGINE structure will be monitored with a line of output on stderr for 24 * each change. This prints the engine's pointer address (truncated to 25 * unsigned int), "struct" or "funct" to indicate the reference type, the 26 * before and after reference count, and the file:line-number pair. The 27 * "engine_ref_debug" statements must come *after* the change. 28 */ 29 # ifdef ENGINE_REF_COUNT_DEBUG 30 31 # define engine_ref_debug(e, isfunct, diff) \ 32 fprintf(stderr, "engine: %08x %s from %d to %d (%s:%d)\n", \ 33 (unsigned int)(e), (isfunct ? "funct" : "struct"), \ 34 ((isfunct) ? ((e)->funct_ref - (diff)) : ((e)->struct_ref - (diff))), \ 35 ((isfunct) ? (e)->funct_ref : (e)->struct_ref), \ 36 (OPENSSL_FILE), (OPENSSL_LINE)) 37 38 # else 39 40 # define engine_ref_debug(e, isfunct, diff) 41 42 # endif 43 44 /* 45 * Any code that will need cleanup operations should use these functions to 46 * register callbacks. engine_cleanup_int() will call all registered 47 * callbacks in order. NB: both the "add" functions assume the engine lock to 48 * already be held (in "write" mode). 49 */ 50 typedef void (ENGINE_CLEANUP_CB) (void); 51 typedef struct st_engine_cleanup_item { 52 ENGINE_CLEANUP_CB *cb; 53 } ENGINE_CLEANUP_ITEM; 54 DEFINE_STACK_OF(ENGINE_CLEANUP_ITEM) 55 void engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb); 56 void engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb); 57 58 /* We need stacks of ENGINEs for use in eng_table.c */ 59 DEFINE_STACK_OF(ENGINE) 60 61 /* 62 * If this symbol is defined then engine_table_select(), the function that is 63 * used by RSA, DSA (etc) code to select registered ENGINEs, cache defaults 64 * and functional references (etc), will display debugging summaries to 65 * stderr. 66 */ 67 /* #define ENGINE_TABLE_DEBUG */ 68 69 /* 70 * This represents an implementation table. Dependent code should instantiate 71 * it as a (ENGINE_TABLE *) pointer value set initially to NULL. 72 */ 73 typedef struct st_engine_table ENGINE_TABLE; 74 int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup, 75 ENGINE *e, const int *nids, int num_nids, 76 int setdefault); 77 void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e); 78 void engine_table_cleanup(ENGINE_TABLE **table); 79 # ifndef ENGINE_TABLE_DEBUG 80 ENGINE *engine_table_select(ENGINE_TABLE **table, int nid); 81 # else 82 ENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f, 83 int l); 84 # define engine_table_select(t,n) engine_table_select_tmp(t,n,OPENSSL_FILE,OPENSSL_LINE) 85 # endif 86 typedef void (engine_table_doall_cb) (int nid, STACK_OF(ENGINE) *sk, 87 ENGINE *def, void *arg); 88 void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb, 89 void *arg); 90 91 /* 92 * Internal versions of API functions that have control over locking. These 93 * are used between C files when functionality needs to be shared but the 94 * caller may already be controlling of the engine lock. 95 */ 96 int engine_unlocked_init(ENGINE *e); 97 int engine_unlocked_finish(ENGINE *e, int unlock_for_handlers); 98 int engine_free_util(ENGINE *e, int not_locked); 99 100 /* 101 * This function will reset all "set"able values in an ENGINE to NULL. This 102 * won't touch reference counts or ex_data, but is equivalent to calling all 103 * the ENGINE_set_***() functions with a NULL value. 104 */ 105 void engine_set_all_null(ENGINE *e); 106 107 /* 108 * NB: Bitwise OR-able values for the "flags" variable in ENGINE are now 109 * exposed in engine.h. 110 */ 111 112 /* Free up dynamically allocated public key methods associated with ENGINE */ 113 114 void engine_pkey_meths_free(ENGINE *e); 115 void engine_pkey_asn1_meths_free(ENGINE *e); 116 117 /* Once initialisation function */ 118 extern CRYPTO_ONCE engine_lock_init; 119 DECLARE_RUN_ONCE(do_engine_lock_init) 120 121 typedef void (*ENGINE_DYNAMIC_ID)(void); 122 int engine_add_dynamic_id(ENGINE *e, ENGINE_DYNAMIC_ID dynamic_id, 123 int not_locked); 124 void engine_remove_dynamic_id(ENGINE *e, int not_locked); 125 126 /* 127 * This is a structure for storing implementations of various crypto 128 * algorithms and functions. 129 */ 130 struct engine_st { 131 const char *id; 132 const char *name; 133 const RSA_METHOD *rsa_meth; 134 const DSA_METHOD *dsa_meth; 135 const DH_METHOD *dh_meth; 136 const EC_KEY_METHOD *ec_meth; 137 const RAND_METHOD *rand_meth; 138 /* Cipher handling is via this callback */ 139 ENGINE_CIPHERS_PTR ciphers; 140 /* Digest handling is via this callback */ 141 ENGINE_DIGESTS_PTR digests; 142 /* Public key handling via this callback */ 143 ENGINE_PKEY_METHS_PTR pkey_meths; 144 /* ASN1 public key handling via this callback */ 145 ENGINE_PKEY_ASN1_METHS_PTR pkey_asn1_meths; 146 ENGINE_GEN_INT_FUNC_PTR destroy; 147 ENGINE_GEN_INT_FUNC_PTR init; 148 ENGINE_GEN_INT_FUNC_PTR finish; 149 ENGINE_CTRL_FUNC_PTR ctrl; 150 ENGINE_LOAD_KEY_PTR load_privkey; 151 ENGINE_LOAD_KEY_PTR load_pubkey; 152 ENGINE_SSL_CLIENT_CERT_PTR load_ssl_client_cert; 153 const ENGINE_CMD_DEFN *cmd_defns; 154 int flags; 155 /* reference count on the structure itself */ 156 CRYPTO_REF_COUNT struct_ref; 157 /* 158 * reference count on usability of the engine type. NB: This controls the 159 * loading and initialisation of any functionality required by this 160 * engine, whereas the previous count is simply to cope with 161 * (de)allocation of this structure. Hence, running_ref <= struct_ref at 162 * all times. 163 */ 164 int funct_ref; 165 /* A place to store per-ENGINE data */ 166 CRYPTO_EX_DATA ex_data; 167 /* Used to maintain the linked-list of engines. */ 168 struct engine_st *prev; 169 struct engine_st *next; 170 /* Used to maintain the linked-list of dynamic engines. */ 171 struct engine_st *prev_dyn; 172 struct engine_st *next_dyn; 173 ENGINE_DYNAMIC_ID dynamic_id; 174 }; 175 176 typedef struct st_engine_pile ENGINE_PILE; 177 178 DEFINE_LHASH_OF(ENGINE_PILE); 179 180 #endif /* OSSL_CRYPTO_ENGINE_ENG_LOCAL_H */ 181