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