2 * Copyright 2001-2023 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the Apache License 2.0 (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
11 #ifndef OSSL_CRYPTO_ENGINE_ENG_LOCAL_H
12 # define OSSL_CRYPTO_ENGINE_ENG_LOCAL_H
14 # include <openssl/trace.h>
15 # include "internal/cryptlib.h"
16 # include "crypto/engine.h"
17 # include "internal/thread_once.h"
18 # include "internal/refcount.h"
20 extern CRYPTO_RWLOCK *global_engine_lock;
23 * This prints the engine's pointer address, "struct" or "funct" to
24 * indicate the reference type, the before and after reference count, and
25 * the file:line-number pair. The "ENGINE_REF_PRINT" statements must come
28 # define ENGINE_REF_PRINT(e, isfunct, diff) \
29 OSSL_TRACE6(ENGINE_REF_COUNT, \
30 "engine: %p %s from %d to %d (%s:%d)\n", \
31 (void *)(e), (isfunct ? "funct" : "struct"), \
33 ? ((e)->funct_ref - (diff)) \
34 : (eng_struct_ref(e) - (diff))), \
35 ((isfunct) ? (e)->funct_ref : eng_struct_ref(e)), \
36 (OPENSSL_FILE), (OPENSSL_LINE))
39 * Any code that will need cleanup operations should use these functions to
40 * register callbacks. engine_cleanup_int() will call all registered
41 * callbacks in order. NB: both the "add" functions assume the engine lock to
42 * already be held (in "write" mode).
44 typedef void (ENGINE_CLEANUP_CB) (void);
45 typedef struct st_engine_cleanup_item {
46 ENGINE_CLEANUP_CB *cb;
47 } ENGINE_CLEANUP_ITEM;
48 DEFINE_STACK_OF(ENGINE_CLEANUP_ITEM)
49 int engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb);
50 int engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb);
52 /* We need stacks of ENGINEs for use in eng_table.c */
53 DEFINE_STACK_OF(ENGINE)
56 * This represents an implementation table. Dependent code should instantiate
57 * it as a (ENGINE_TABLE *) pointer value set initially to NULL.
59 typedef struct st_engine_table ENGINE_TABLE;
60 int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup,
61 ENGINE *e, const int *nids, int num_nids,
63 void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e);
64 void engine_table_cleanup(ENGINE_TABLE **table);
65 ENGINE *ossl_engine_table_select(ENGINE_TABLE **table, int nid,
66 const char *f, int l);
67 typedef void (engine_table_doall_cb) (int nid, STACK_OF(ENGINE) *sk,
68 ENGINE *def, void *arg);
69 void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb,
73 * Internal versions of API functions that have control over locking. These
74 * are used between C files when functionality needs to be shared but the
75 * caller may already be controlling of the engine lock.
77 int engine_unlocked_init(ENGINE *e);
78 int engine_unlocked_finish(ENGINE *e, int unlock_for_handlers);
79 int engine_free_util(ENGINE *e, int not_locked);
82 * This function will reset all "set"able values in an ENGINE to NULL. This
83 * won't touch reference counts or ex_data, but is equivalent to calling all
84 * the ENGINE_set_***() functions with a NULL value.
86 void engine_set_all_null(ENGINE *e);
89 * NB: Bitwise OR-able values for the "flags" variable in ENGINE are now
90 * exposed in engine.h.
93 /* Free up dynamically allocated public key methods associated with ENGINE */
95 void engine_pkey_meths_free(ENGINE *e);
96 void engine_pkey_asn1_meths_free(ENGINE *e);
98 /* Once initialisation function */
99 extern CRYPTO_ONCE engine_lock_init;
100 DECLARE_RUN_ONCE(do_engine_lock_init)
102 typedef void (*ENGINE_DYNAMIC_ID)(void);
103 int engine_add_dynamic_id(ENGINE *e, ENGINE_DYNAMIC_ID dynamic_id,
105 void engine_remove_dynamic_id(ENGINE *e, int not_locked);
108 * This is a structure for storing implementations of various crypto
109 * algorithms and functions.
114 const RSA_METHOD *rsa_meth;
115 const DSA_METHOD *dsa_meth;
116 const DH_METHOD *dh_meth;
117 const EC_KEY_METHOD *ec_meth;
118 const RAND_METHOD *rand_meth;
119 /* Cipher handling is via this callback */
120 ENGINE_CIPHERS_PTR ciphers;
121 /* Digest handling is via this callback */
122 ENGINE_DIGESTS_PTR digests;
123 /* Public key handling via this callback */
124 ENGINE_PKEY_METHS_PTR pkey_meths;
125 /* ASN1 public key handling via this callback */
126 ENGINE_PKEY_ASN1_METHS_PTR pkey_asn1_meths;
127 ENGINE_GEN_INT_FUNC_PTR destroy;
128 ENGINE_GEN_INT_FUNC_PTR init;
129 ENGINE_GEN_INT_FUNC_PTR finish;
130 ENGINE_CTRL_FUNC_PTR ctrl;
131 ENGINE_LOAD_KEY_PTR load_privkey;
132 ENGINE_LOAD_KEY_PTR load_pubkey;
133 ENGINE_SSL_CLIENT_CERT_PTR load_ssl_client_cert;
134 const ENGINE_CMD_DEFN *cmd_defns;
136 /* reference count on the structure itself */
137 CRYPTO_REF_COUNT struct_ref;
139 * reference count on usability of the engine type. NB: This controls the
140 * loading and initialisation of any functionality required by this
141 * engine, whereas the previous count is simply to cope with
142 * (de)allocation of this structure. Hence, running_ref <= struct_ref at
146 /* A place to store per-ENGINE data */
147 CRYPTO_EX_DATA ex_data;
148 /* Used to maintain the linked-list of engines. */
149 struct engine_st *prev;
150 struct engine_st *next;
151 /* Used to maintain the linked-list of dynamic engines. */
152 struct engine_st *prev_dyn;
153 struct engine_st *next_dyn;
154 ENGINE_DYNAMIC_ID dynamic_id;
157 typedef struct st_engine_pile ENGINE_PILE;
159 DEFINE_LHASH_OF_EX(ENGINE_PILE);
161 static ossl_unused ossl_inline int eng_struct_ref(ENGINE *e)
165 CRYPTO_GET_REF(&e->struct_ref, &res);
169 #endif /* OSSL_CRYPTO_ENGINE_ENG_LOCAL_H */