]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/engine/eng_local.h
Convert the ENGINE struct_ref field to be an atomic
[thirdparty/openssl.git] / crypto / engine / eng_local.h
CommitLineData
0f113f3e 1/*
38fc02a7 2 * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
aa8f3d76 3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5270e702 4 *
3c120f91 5 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
5270e702 9 */
b1322259 10
ae4186b0
DMSP
11#ifndef OSSL_CRYPTO_ENGINE_ENG_LOCAL_H
12# define OSSL_CRYPTO_ENGINE_ENG_LOCAL_H
5270e702 13
f272be67 14# include <openssl/trace.h>
b39fc560 15# include "internal/cryptlib.h"
25f2138b 16# include "crypto/engine.h"
176db6dc 17# include "internal/thread_once.h"
2f545ae4 18# include "internal/refcount.h"
11c0f120 19
40e068d5
MC
20extern CRYPTO_RWLOCK *global_engine_lock;
21
0f113f3e 22/*
a935791d
RS
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
e568d64f
MC
26 * *after* the change. Since this is for tracing only we do not concern
27 * ourselves with using atomic primitives for reading the struct_ref
0f113f3e 28 */
a935791d 29# define ENGINE_REF_PRINT(e, isfunct, diff) \
f518e3e8 30 OSSL_TRACE6(ENGINE_REF_COUNT, \
a935791d 31 "engine: %p %s from %d to %d (%s:%d)\n", \
f518e3e8
RL
32 (void *)(e), (isfunct ? "funct" : "struct"), \
33 ((isfunct) \
34 ? ((e)->funct_ref - (diff)) \
35 : ((e)->struct_ref - (diff))), \
36 ((isfunct) ? (e)->funct_ref : (e)->struct_ref), \
37 (OPENSSL_FILE), (OPENSSL_LINE))
b41f836e 38
0f113f3e
MC
39/*
40 * Any code that will need cleanup operations should use these functions to
b3599dbb 41 * register callbacks. engine_cleanup_int() will call all registered
6d4fb1d5
MC
42 * callbacks in order. NB: both the "add" functions assume the engine lock to
43 * already be held (in "write" mode).
0f113f3e
MC
44 */
45typedef void (ENGINE_CLEANUP_CB) (void);
46typedef struct st_engine_cleanup_item {
47 ENGINE_CLEANUP_CB *cb;
48} ENGINE_CLEANUP_ITEM;
85885715 49DEFINE_STACK_OF(ENGINE_CLEANUP_ITEM)
b6d1e52d
GT
50void engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb);
51void engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb);
52
53/* We need stacks of ENGINEs for use in eng_table.c */
85885715 54DEFINE_STACK_OF(ENGINE)
b6d1e52d 55
0f113f3e
MC
56/*
57 * This represents an implementation table. Dependent code should instantiate
58 * it as a (ENGINE_TABLE *) pointer value set initially to NULL.
59 */
b6d1e52d
GT
60typedef struct st_engine_table ENGINE_TABLE;
61int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup,
0f113f3e
MC
62 ENGINE *e, const int *nids, int num_nids,
63 int setdefault);
b6d1e52d
GT
64void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e);
65void engine_table_cleanup(ENGINE_TABLE **table);
a861711b
RS
66ENGINE *ossl_engine_table_select(ENGINE_TABLE **table, int nid,
67 const char *f, int l);
0f113f3e
MC
68typedef void (engine_table_doall_cb) (int nid, STACK_OF(ENGINE) *sk,
69 ENGINE *def, void *arg);
70void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb,
71 void *arg);
72
73/*
74 * Internal versions of API functions that have control over locking. These
75 * are used between C files when functionality needs to be shared but the
40e068d5 76 * caller may already be controlling of the engine lock.
0f113f3e 77 */
b6d1e52d
GT
78int engine_unlocked_init(ENGINE *e);
79int engine_unlocked_finish(ENGINE *e, int unlock_for_handlers);
0a3dce82 80int engine_free_util(ENGINE *e, int not_locked);
354c3ace 81
0f113f3e
MC
82/*
83 * This function will reset all "set"able values in an ENGINE to NULL. This
84 * won't touch reference counts or ex_data, but is equivalent to calling all
85 * the ENGINE_set_***() functions with a NULL value.
86 */
e4a6cf42
GT
87void engine_set_all_null(ENGINE *e);
88
0f113f3e
MC
89/*
90 * NB: Bitwise OR-able values for the "flags" variable in ENGINE are now
91 * exposed in engine.h.
92 */
5270e702 93
7e5b0681
DSH
94/* Free up dynamically allocated public key methods associated with ENGINE */
95
96void engine_pkey_meths_free(ENGINE *e);
01b8b3c7 97void engine_pkey_asn1_meths_free(ENGINE *e);
7e5b0681 98
40e068d5
MC
99/* Once initialisation function */
100extern CRYPTO_ONCE engine_lock_init;
c2e4e5d2 101DECLARE_RUN_ONCE(do_engine_lock_init)
40e068d5 102
e2571e02
BE
103typedef void (*ENGINE_DYNAMIC_ID)(void);
104int engine_add_dynamic_id(ENGINE *e, ENGINE_DYNAMIC_ID dynamic_id,
105 int not_locked);
106void engine_remove_dynamic_id(ENGINE *e, int not_locked);
107
0f113f3e
MC
108/*
109 * This is a structure for storing implementations of various crypto
110 * algorithms and functions.
111 */
112struct engine_st {
113 const char *id;
114 const char *name;
115 const RSA_METHOD *rsa_meth;
116 const DSA_METHOD *dsa_meth;
117 const DH_METHOD *dh_meth;
7d711cbc 118 const EC_KEY_METHOD *ec_meth;
0f113f3e 119 const RAND_METHOD *rand_meth;
0f113f3e
MC
120 /* Cipher handling is via this callback */
121 ENGINE_CIPHERS_PTR ciphers;
122 /* Digest handling is via this callback */
123 ENGINE_DIGESTS_PTR digests;
124 /* Public key handling via this callback */
125 ENGINE_PKEY_METHS_PTR pkey_meths;
126 /* ASN1 public key handling via this callback */
127 ENGINE_PKEY_ASN1_METHS_PTR pkey_asn1_meths;
128 ENGINE_GEN_INT_FUNC_PTR destroy;
129 ENGINE_GEN_INT_FUNC_PTR init;
130 ENGINE_GEN_INT_FUNC_PTR finish;
131 ENGINE_CTRL_FUNC_PTR ctrl;
132 ENGINE_LOAD_KEY_PTR load_privkey;
133 ENGINE_LOAD_KEY_PTR load_pubkey;
134 ENGINE_SSL_CLIENT_CERT_PTR load_ssl_client_cert;
135 const ENGINE_CMD_DEFN *cmd_defns;
136 int flags;
137 /* reference count on the structure itself */
2f545ae4 138 CRYPTO_REF_COUNT struct_ref;
e568d64f 139 CRYPTO_RWLOCK *refcnt_lock;
0f113f3e
MC
140 /*
141 * reference count on usability of the engine type. NB: This controls the
8483a003 142 * loading and initialisation of any functionality required by this
0f113f3e
MC
143 * engine, whereas the previous count is simply to cope with
144 * (de)allocation of this structure. Hence, running_ref <= struct_ref at
145 * all times.
146 */
147 int funct_ref;
148 /* A place to store per-ENGINE data */
149 CRYPTO_EX_DATA ex_data;
150 /* Used to maintain the linked-list of engines. */
151 struct engine_st *prev;
152 struct engine_st *next;
e2571e02
BE
153 /* Used to maintain the linked-list of dynamic engines. */
154 struct engine_st *prev_dyn;
155 struct engine_st *next_dyn;
156 ENGINE_DYNAMIC_ID dynamic_id;
0f113f3e 157};
5270e702 158
e6b5c341
DSH
159typedef struct st_engine_pile ENGINE_PILE;
160
5317b6ee 161DEFINE_LHASH_OF_EX(ENGINE_PILE);
e6b5c341 162
ae4186b0 163#endif /* OSSL_CRYPTO_ENGINE_ENG_LOCAL_H */