xref: /netbsd-src/crypto/external/bsd/openssl.old/dist/doc/man3/ENGINE_add.pod (revision 4724848cf0da353df257f730694b7882798e5daf)
1*4724848cSchristos=pod
2*4724848cSchristos
3*4724848cSchristos=head1 NAME
4*4724848cSchristos
5*4724848cSchristosENGINE_get_DH, ENGINE_get_DSA,
6*4724848cSchristosENGINE_by_id, ENGINE_get_cipher_engine, ENGINE_get_default_DH,
7*4724848cSchristosENGINE_get_default_DSA,
8*4724848cSchristosENGINE_get_default_RAND,
9*4724848cSchristosENGINE_get_default_RSA, ENGINE_get_digest_engine, ENGINE_get_first,
10*4724848cSchristosENGINE_get_last, ENGINE_get_next, ENGINE_get_prev, ENGINE_new,
11*4724848cSchristosENGINE_get_ciphers, ENGINE_get_ctrl_function, ENGINE_get_digests,
12*4724848cSchristosENGINE_get_destroy_function, ENGINE_get_finish_function,
13*4724848cSchristosENGINE_get_init_function, ENGINE_get_load_privkey_function,
14*4724848cSchristosENGINE_get_load_pubkey_function, ENGINE_load_private_key,
15*4724848cSchristosENGINE_load_public_key, ENGINE_get_RAND, ENGINE_get_RSA, ENGINE_get_id,
16*4724848cSchristosENGINE_get_name, ENGINE_get_cmd_defns, ENGINE_get_cipher,
17*4724848cSchristosENGINE_get_digest, ENGINE_add, ENGINE_cmd_is_executable,
18*4724848cSchristosENGINE_ctrl, ENGINE_ctrl_cmd, ENGINE_ctrl_cmd_string,
19*4724848cSchristosENGINE_finish, ENGINE_free, ENGINE_get_flags, ENGINE_init,
20*4724848cSchristosENGINE_register_DH, ENGINE_register_DSA,
21*4724848cSchristosENGINE_register_RAND, ENGINE_register_RSA,
22*4724848cSchristosENGINE_register_all_complete, ENGINE_register_ciphers,
23*4724848cSchristosENGINE_register_complete, ENGINE_register_digests, ENGINE_remove,
24*4724848cSchristosENGINE_set_DH, ENGINE_set_DSA,
25*4724848cSchristosENGINE_set_RAND, ENGINE_set_RSA, ENGINE_set_ciphers,
26*4724848cSchristosENGINE_set_cmd_defns, ENGINE_set_ctrl_function, ENGINE_set_default,
27*4724848cSchristosENGINE_set_default_DH, ENGINE_set_default_DSA,
28*4724848cSchristosENGINE_set_default_RAND, ENGINE_set_default_RSA,
29*4724848cSchristosENGINE_set_default_ciphers, ENGINE_set_default_digests,
30*4724848cSchristosENGINE_set_default_string, ENGINE_set_destroy_function,
31*4724848cSchristosENGINE_set_digests, ENGINE_set_finish_function, ENGINE_set_flags,
32*4724848cSchristosENGINE_set_id, ENGINE_set_init_function, ENGINE_set_load_privkey_function,
33*4724848cSchristosENGINE_set_load_pubkey_function, ENGINE_set_name, ENGINE_up_ref,
34*4724848cSchristosENGINE_get_table_flags, ENGINE_cleanup,
35*4724848cSchristosENGINE_load_builtin_engines, ENGINE_register_all_DH,
36*4724848cSchristosENGINE_register_all_DSA,
37*4724848cSchristosENGINE_register_all_RAND,
38*4724848cSchristosENGINE_register_all_RSA, ENGINE_register_all_ciphers,
39*4724848cSchristosENGINE_register_all_digests, ENGINE_set_table_flags, ENGINE_unregister_DH,
40*4724848cSchristosENGINE_unregister_DSA,
41*4724848cSchristosENGINE_unregister_RAND, ENGINE_unregister_RSA, ENGINE_unregister_ciphers,
42*4724848cSchristosENGINE_unregister_digests
43*4724848cSchristos- ENGINE cryptographic module support
44*4724848cSchristos
45*4724848cSchristos=head1 SYNOPSIS
46*4724848cSchristos
47*4724848cSchristos #include <openssl/engine.h>
48*4724848cSchristos
49*4724848cSchristos ENGINE *ENGINE_get_first(void);
50*4724848cSchristos ENGINE *ENGINE_get_last(void);
51*4724848cSchristos ENGINE *ENGINE_get_next(ENGINE *e);
52*4724848cSchristos ENGINE *ENGINE_get_prev(ENGINE *e);
53*4724848cSchristos
54*4724848cSchristos int ENGINE_add(ENGINE *e);
55*4724848cSchristos int ENGINE_remove(ENGINE *e);
56*4724848cSchristos
57*4724848cSchristos ENGINE *ENGINE_by_id(const char *id);
58*4724848cSchristos
59*4724848cSchristos int ENGINE_init(ENGINE *e);
60*4724848cSchristos int ENGINE_finish(ENGINE *e);
61*4724848cSchristos
62*4724848cSchristos void ENGINE_load_builtin_engines(void);
63*4724848cSchristos
64*4724848cSchristos ENGINE *ENGINE_get_default_RSA(void);
65*4724848cSchristos ENGINE *ENGINE_get_default_DSA(void);
66*4724848cSchristos ENGINE *ENGINE_get_default_DH(void);
67*4724848cSchristos ENGINE *ENGINE_get_default_RAND(void);
68*4724848cSchristos ENGINE *ENGINE_get_cipher_engine(int nid);
69*4724848cSchristos ENGINE *ENGINE_get_digest_engine(int nid);
70*4724848cSchristos
71*4724848cSchristos int ENGINE_set_default_RSA(ENGINE *e);
72*4724848cSchristos int ENGINE_set_default_DSA(ENGINE *e);
73*4724848cSchristos int ENGINE_set_default_DH(ENGINE *e);
74*4724848cSchristos int ENGINE_set_default_RAND(ENGINE *e);
75*4724848cSchristos int ENGINE_set_default_ciphers(ENGINE *e);
76*4724848cSchristos int ENGINE_set_default_digests(ENGINE *e);
77*4724848cSchristos int ENGINE_set_default_string(ENGINE *e, const char *list);
78*4724848cSchristos
79*4724848cSchristos int ENGINE_set_default(ENGINE *e, unsigned int flags);
80*4724848cSchristos
81*4724848cSchristos unsigned int ENGINE_get_table_flags(void);
82*4724848cSchristos void ENGINE_set_table_flags(unsigned int flags);
83*4724848cSchristos
84*4724848cSchristos int ENGINE_register_RSA(ENGINE *e);
85*4724848cSchristos void ENGINE_unregister_RSA(ENGINE *e);
86*4724848cSchristos void ENGINE_register_all_RSA(void);
87*4724848cSchristos int ENGINE_register_DSA(ENGINE *e);
88*4724848cSchristos void ENGINE_unregister_DSA(ENGINE *e);
89*4724848cSchristos void ENGINE_register_all_DSA(void);
90*4724848cSchristos int ENGINE_register_DH(ENGINE *e);
91*4724848cSchristos void ENGINE_unregister_DH(ENGINE *e);
92*4724848cSchristos void ENGINE_register_all_DH(void);
93*4724848cSchristos int ENGINE_register_RAND(ENGINE *e);
94*4724848cSchristos void ENGINE_unregister_RAND(ENGINE *e);
95*4724848cSchristos void ENGINE_register_all_RAND(void);
96*4724848cSchristos int ENGINE_register_ciphers(ENGINE *e);
97*4724848cSchristos void ENGINE_unregister_ciphers(ENGINE *e);
98*4724848cSchristos void ENGINE_register_all_ciphers(void);
99*4724848cSchristos int ENGINE_register_digests(ENGINE *e);
100*4724848cSchristos void ENGINE_unregister_digests(ENGINE *e);
101*4724848cSchristos void ENGINE_register_all_digests(void);
102*4724848cSchristos int ENGINE_register_complete(ENGINE *e);
103*4724848cSchristos int ENGINE_register_all_complete(void);
104*4724848cSchristos
105*4724848cSchristos int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
106*4724848cSchristos int ENGINE_cmd_is_executable(ENGINE *e, int cmd);
107*4724848cSchristos int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
108*4724848cSchristos                     long i, void *p, void (*f)(void), int cmd_optional);
109*4724848cSchristos int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
110*4724848cSchristos                            int cmd_optional);
111*4724848cSchristos
112*4724848cSchristos ENGINE *ENGINE_new(void);
113*4724848cSchristos int ENGINE_free(ENGINE *e);
114*4724848cSchristos int ENGINE_up_ref(ENGINE *e);
115*4724848cSchristos
116*4724848cSchristos int ENGINE_set_id(ENGINE *e, const char *id);
117*4724848cSchristos int ENGINE_set_name(ENGINE *e, const char *name);
118*4724848cSchristos int ENGINE_set_RSA(ENGINE *e, const RSA_METHOD *rsa_meth);
119*4724848cSchristos int ENGINE_set_DSA(ENGINE *e, const DSA_METHOD *dsa_meth);
120*4724848cSchristos int ENGINE_set_DH(ENGINE *e, const DH_METHOD *dh_meth);
121*4724848cSchristos int ENGINE_set_RAND(ENGINE *e, const RAND_METHOD *rand_meth);
122*4724848cSchristos int ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f);
123*4724848cSchristos int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f);
124*4724848cSchristos int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f);
125*4724848cSchristos int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f);
126*4724848cSchristos int ENGINE_set_load_privkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpriv_f);
127*4724848cSchristos int ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f);
128*4724848cSchristos int ENGINE_set_ciphers(ENGINE *e, ENGINE_CIPHERS_PTR f);
129*4724848cSchristos int ENGINE_set_digests(ENGINE *e, ENGINE_DIGESTS_PTR f);
130*4724848cSchristos int ENGINE_set_flags(ENGINE *e, int flags);
131*4724848cSchristos int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns);
132*4724848cSchristos
133*4724848cSchristos const char *ENGINE_get_id(const ENGINE *e);
134*4724848cSchristos const char *ENGINE_get_name(const ENGINE *e);
135*4724848cSchristos const RSA_METHOD *ENGINE_get_RSA(const ENGINE *e);
136*4724848cSchristos const DSA_METHOD *ENGINE_get_DSA(const ENGINE *e);
137*4724848cSchristos const DH_METHOD *ENGINE_get_DH(const ENGINE *e);
138*4724848cSchristos const RAND_METHOD *ENGINE_get_RAND(const ENGINE *e);
139*4724848cSchristos ENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e);
140*4724848cSchristos ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e);
141*4724848cSchristos ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e);
142*4724848cSchristos ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e);
143*4724848cSchristos ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e);
144*4724848cSchristos ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e);
145*4724848cSchristos ENGINE_CIPHERS_PTR ENGINE_get_ciphers(const ENGINE *e);
146*4724848cSchristos ENGINE_DIGESTS_PTR ENGINE_get_digests(const ENGINE *e);
147*4724848cSchristos const EVP_CIPHER *ENGINE_get_cipher(ENGINE *e, int nid);
148*4724848cSchristos const EVP_MD *ENGINE_get_digest(ENGINE *e, int nid);
149*4724848cSchristos int ENGINE_get_flags(const ENGINE *e);
150*4724848cSchristos const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e);
151*4724848cSchristos
152*4724848cSchristos EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
153*4724848cSchristos                                   UI_METHOD *ui_method, void *callback_data);
154*4724848cSchristos EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
155*4724848cSchristos                                  UI_METHOD *ui_method, void *callback_data);
156*4724848cSchristos
157*4724848cSchristosDeprecated:
158*4724848cSchristos
159*4724848cSchristos #if OPENSSL_API_COMPAT < 0x10100000L
160*4724848cSchristos void ENGINE_cleanup(void)
161*4724848cSchristos #endif
162*4724848cSchristos
163*4724848cSchristos=head1 DESCRIPTION
164*4724848cSchristos
165*4724848cSchristosThese functions create, manipulate, and use cryptographic modules in the
166*4724848cSchristosform of B<ENGINE> objects. These objects act as containers for
167*4724848cSchristosimplementations of cryptographic algorithms, and support a
168*4724848cSchristosreference-counted mechanism to allow them to be dynamically loaded in and
169*4724848cSchristosout of the running application.
170*4724848cSchristos
171*4724848cSchristosThe cryptographic functionality that can be provided by an B<ENGINE>
172*4724848cSchristosimplementation includes the following abstractions;
173*4724848cSchristos
174*4724848cSchristos RSA_METHOD - for providing alternative RSA implementations
175*4724848cSchristos DSA_METHOD, DH_METHOD, RAND_METHOD, ECDH_METHOD, ECDSA_METHOD,
176*4724848cSchristos       - similarly for other OpenSSL APIs
177*4724848cSchristos EVP_CIPHER - potentially multiple cipher algorithms (indexed by 'nid')
178*4724848cSchristos EVP_DIGEST - potentially multiple hash algorithms (indexed by 'nid')
179*4724848cSchristos key-loading - loading public and/or private EVP_PKEY keys
180*4724848cSchristos
181*4724848cSchristos=head2 Reference counting and handles
182*4724848cSchristos
183*4724848cSchristosDue to the modular nature of the ENGINE API, pointers to ENGINEs need to be
184*4724848cSchristostreated as handles - i.e. not only as pointers, but also as references to
185*4724848cSchristosthe underlying ENGINE object. Ie. one should obtain a new reference when
186*4724848cSchristosmaking copies of an ENGINE pointer if the copies will be used (and
187*4724848cSchristosreleased) independently.
188*4724848cSchristos
189*4724848cSchristosENGINE objects have two levels of reference-counting to match the way in
190*4724848cSchristoswhich the objects are used. At the most basic level, each ENGINE pointer is
191*4724848cSchristosinherently a B<structural> reference - a structural reference is required
192*4724848cSchristosto use the pointer value at all, as this kind of reference is a guarantee
193*4724848cSchristosthat the structure can not be deallocated until the reference is released.
194*4724848cSchristos
195*4724848cSchristosHowever, a structural reference provides no guarantee that the ENGINE is
196*4724848cSchristosinitialised and able to use any of its cryptographic
197*4724848cSchristosimplementations. Indeed it's quite possible that most ENGINEs will not
198*4724848cSchristosinitialise at all in typical environments, as ENGINEs are typically used to
199*4724848cSchristossupport specialised hardware. To use an ENGINE's functionality, you need a
200*4724848cSchristosB<functional> reference. This kind of reference can be considered a
201*4724848cSchristosspecialised form of structural reference, because each functional reference
202*4724848cSchristosimplicitly contains a structural reference as well - however to avoid
203*4724848cSchristosdifficult-to-find programming bugs, it is recommended to treat the two
204*4724848cSchristoskinds of reference independently. If you have a functional reference to an
205*4724848cSchristosENGINE, you have a guarantee that the ENGINE has been initialised and
206*4724848cSchristosis ready to perform cryptographic operations, and will remain initialised
207*4724848cSchristosuntil after you have released your reference.
208*4724848cSchristos
209*4724848cSchristosI<Structural references>
210*4724848cSchristos
211*4724848cSchristosThis basic type of reference is used for instantiating new ENGINEs,
212*4724848cSchristositerating across OpenSSL's internal linked-list of loaded
213*4724848cSchristosENGINEs, reading information about an ENGINE, etc. Essentially a structural
214*4724848cSchristosreference is sufficient if you only need to query or manipulate the data of
215*4724848cSchristosan ENGINE implementation rather than use its functionality.
216*4724848cSchristos
217*4724848cSchristosThe ENGINE_new() function returns a structural reference to a new (empty)
218*4724848cSchristosENGINE object. There are other ENGINE API functions that return structural
219*4724848cSchristosreferences such as; ENGINE_by_id(), ENGINE_get_first(), ENGINE_get_last(),
220*4724848cSchristosENGINE_get_next(), ENGINE_get_prev(). All structural references should be
221*4724848cSchristosreleased by a corresponding to call to the ENGINE_free() function - the
222*4724848cSchristosENGINE object itself will only actually be cleaned up and deallocated when
223*4724848cSchristosthe last structural reference is released.
224*4724848cSchristos
225*4724848cSchristosIt should also be noted that many ENGINE API function calls that accept a
226*4724848cSchristosstructural reference will internally obtain another reference - typically
227*4724848cSchristosthis happens whenever the supplied ENGINE will be needed by OpenSSL after
228*4724848cSchristosthe function has returned. Eg. the function to add a new ENGINE to
229*4724848cSchristosOpenSSL's internal list is ENGINE_add() - if this function returns success,
230*4724848cSchristosthen OpenSSL will have stored a new structural reference internally so the
231*4724848cSchristoscaller is still responsible for freeing their own reference with
232*4724848cSchristosENGINE_free() when they are finished with it. In a similar way, some
233*4724848cSchristosfunctions will automatically release the structural reference passed to it
234*4724848cSchristosif part of the function's job is to do so. Eg. the ENGINE_get_next() and
235*4724848cSchristosENGINE_get_prev() functions are used for iterating across the internal
236*4724848cSchristosENGINE list - they will return a new structural reference to the next (or
237*4724848cSchristosprevious) ENGINE in the list or NULL if at the end (or beginning) of the
238*4724848cSchristoslist, but in either case the structural reference passed to the function is
239*4724848cSchristosreleased on behalf of the caller.
240*4724848cSchristos
241*4724848cSchristosTo clarify a particular function's handling of references, one should
242*4724848cSchristosalways consult that function's documentation "man" page, or failing that
243*4724848cSchristosthe openssl/engine.h header file includes some hints.
244*4724848cSchristos
245*4724848cSchristosI<Functional references>
246*4724848cSchristos
247*4724848cSchristosAs mentioned, functional references exist when the cryptographic
248*4724848cSchristosfunctionality of an ENGINE is required to be available. A functional
249*4724848cSchristosreference can be obtained in one of two ways; from an existing structural
250*4724848cSchristosreference to the required ENGINE, or by asking OpenSSL for the default
251*4724848cSchristosoperational ENGINE for a given cryptographic purpose.
252*4724848cSchristos
253*4724848cSchristosTo obtain a functional reference from an existing structural reference,
254*4724848cSchristoscall the ENGINE_init() function. This returns zero if the ENGINE was not
255*4724848cSchristosalready operational and couldn't be successfully initialised (e.g. lack of
256*4724848cSchristossystem drivers, no special hardware attached, etc), otherwise it will
257*4724848cSchristosreturn nonzero to indicate that the ENGINE is now operational and will
258*4724848cSchristoshave allocated a new B<functional> reference to the ENGINE. All functional
259*4724848cSchristosreferences are released by calling ENGINE_finish() (which removes the
260*4724848cSchristosimplicit structural reference as well).
261*4724848cSchristos
262*4724848cSchristosThe second way to get a functional reference is by asking OpenSSL for a
263*4724848cSchristosdefault implementation for a given task, e.g. by ENGINE_get_default_RSA(),
264*4724848cSchristosENGINE_get_default_cipher_engine(), etc. These are discussed in the next
265*4724848cSchristossection, though they are not usually required by application programmers as
266*4724848cSchristosthey are used automatically when creating and using the relevant
267*4724848cSchristosalgorithm-specific types in OpenSSL, such as RSA, DSA, EVP_CIPHER_CTX, etc.
268*4724848cSchristos
269*4724848cSchristos=head2 Default implementations
270*4724848cSchristos
271*4724848cSchristosFor each supported abstraction, the ENGINE code maintains an internal table
272*4724848cSchristosof state to control which implementations are available for a given
273*4724848cSchristosabstraction and which should be used by default. These implementations are
274*4724848cSchristosregistered in the tables and indexed by an 'nid' value, because
275*4724848cSchristosabstractions like EVP_CIPHER and EVP_DIGEST support many distinct
276*4724848cSchristosalgorithms and modes, and ENGINEs can support arbitrarily many of them.
277*4724848cSchristosIn the case of other abstractions like RSA, DSA, etc, there is only one
278*4724848cSchristos"algorithm" so all implementations implicitly register using the same 'nid'
279*4724848cSchristosindex.
280*4724848cSchristos
281*4724848cSchristosWhen a default ENGINE is requested for a given abstraction/algorithm/mode, (e.g.
282*4724848cSchristoswhen calling RSA_new_method(NULL)), a "get_default" call will be made to the
283*4724848cSchristosENGINE subsystem to process the corresponding state table and return a
284*4724848cSchristosfunctional reference to an initialised ENGINE whose implementation should be
285*4724848cSchristosused. If no ENGINE should (or can) be used, it will return NULL and the caller
286*4724848cSchristoswill operate with a NULL ENGINE handle - this usually equates to using the
287*4724848cSchristosconventional software implementation. In the latter case, OpenSSL will from
288*4724848cSchristosthen on behave the way it used to before the ENGINE API existed.
289*4724848cSchristos
290*4724848cSchristosEach state table has a flag to note whether it has processed this
291*4724848cSchristos"get_default" query since the table was last modified, because to process
292*4724848cSchristosthis question it must iterate across all the registered ENGINEs in the
293*4724848cSchristostable trying to initialise each of them in turn, in case one of them is
294*4724848cSchristosoperational. If it returns a functional reference to an ENGINE, it will
295*4724848cSchristosalso cache another reference to speed up processing future queries (without
296*4724848cSchristosneeding to iterate across the table). Likewise, it will cache a NULL
297*4724848cSchristosresponse if no ENGINE was available so that future queries won't repeat the
298*4724848cSchristossame iteration unless the state table changes. This behaviour can also be
299*4724848cSchristoschanged; if the ENGINE_TABLE_FLAG_NOINIT flag is set (using
300*4724848cSchristosENGINE_set_table_flags()), no attempted initialisations will take place,
301*4724848cSchristosinstead the only way for the state table to return a non-NULL ENGINE to the
302*4724848cSchristos"get_default" query will be if one is expressly set in the table. Eg.
303*4724848cSchristosENGINE_set_default_RSA() does the same job as ENGINE_register_RSA() except
304*4724848cSchristosthat it also sets the state table's cached response for the "get_default"
305*4724848cSchristosquery. In the case of abstractions like EVP_CIPHER, where implementations are
306*4724848cSchristosindexed by 'nid', these flags and cached-responses are distinct for each 'nid'
307*4724848cSchristosvalue.
308*4724848cSchristos
309*4724848cSchristos=head2 Application requirements
310*4724848cSchristos
311*4724848cSchristosThis section will explain the basic things an application programmer should
312*4724848cSchristossupport to make the most useful elements of the ENGINE functionality
313*4724848cSchristosavailable to the user. The first thing to consider is whether the
314*4724848cSchristosprogrammer wishes to make alternative ENGINE modules available to the
315*4724848cSchristosapplication and user. OpenSSL maintains an internal linked list of
316*4724848cSchristos"visible" ENGINEs from which it has to operate - at start-up, this list is
317*4724848cSchristosempty and in fact if an application does not call any ENGINE API calls and
318*4724848cSchristosit uses static linking against openssl, then the resulting application
319*4724848cSchristosbinary will not contain any alternative ENGINE code at all. So the first
320*4724848cSchristosconsideration is whether any/all available ENGINE implementations should be
321*4724848cSchristosmade visible to OpenSSL - this is controlled by calling the various "load"
322*4724848cSchristosfunctions.
323*4724848cSchristos
324*4724848cSchristosThe fact that ENGINEs are made visible to OpenSSL (and thus are linked into
325*4724848cSchristosthe program and loaded into memory at run-time) does not mean they are
326*4724848cSchristos"registered" or called into use by OpenSSL automatically - that behaviour
327*4724848cSchristosis something for the application to control. Some applications
328*4724848cSchristoswill want to allow the user to specify exactly which ENGINE they want used
329*4724848cSchristosif any is to be used at all. Others may prefer to load all support and have
330*4724848cSchristosOpenSSL automatically use at run-time any ENGINE that is able to
331*4724848cSchristossuccessfully initialise - i.e. to assume that this corresponds to
332*4724848cSchristosacceleration hardware attached to the machine or some such thing. There are
333*4724848cSchristosprobably numerous other ways in which applications may prefer to handle
334*4724848cSchristosthings, so we will simply illustrate the consequences as they apply to a
335*4724848cSchristoscouple of simple cases and leave developers to consider these and the
336*4724848cSchristossource code to openssl's builtin utilities as guides.
337*4724848cSchristos
338*4724848cSchristosIf no ENGINE API functions are called within an application, then OpenSSL
339*4724848cSchristoswill not allocate any internal resources.  Prior to OpenSSL 1.1.0, however,
340*4724848cSchristosif any ENGINEs are loaded, even if not registered or used, it was necessary to
341*4724848cSchristoscall ENGINE_cleanup() before the program exits.
342*4724848cSchristos
343*4724848cSchristosI<Using a specific ENGINE implementation>
344*4724848cSchristos
345*4724848cSchristosHere we'll assume an application has been configured by its user or admin
346*4724848cSchristosto want to use the "ACME" ENGINE if it is available in the version of
347*4724848cSchristosOpenSSL the application was compiled with. If it is available, it should be
348*4724848cSchristosused by default for all RSA, DSA, and symmetric cipher operations, otherwise
349*4724848cSchristosOpenSSL should use its builtin software as per usual. The following code
350*4724848cSchristosillustrates how to approach this;
351*4724848cSchristos
352*4724848cSchristos ENGINE *e;
353*4724848cSchristos const char *engine_id = "ACME";
354*4724848cSchristos ENGINE_load_builtin_engines();
355*4724848cSchristos e = ENGINE_by_id(engine_id);
356*4724848cSchristos if (!e)
357*4724848cSchristos     /* the engine isn't available */
358*4724848cSchristos     return;
359*4724848cSchristos if (!ENGINE_init(e)) {
360*4724848cSchristos     /* the engine couldn't initialise, release 'e' */
361*4724848cSchristos     ENGINE_free(e);
362*4724848cSchristos     return;
363*4724848cSchristos }
364*4724848cSchristos if (!ENGINE_set_default_RSA(e))
365*4724848cSchristos     /*
366*4724848cSchristos      * This should only happen when 'e' can't initialise, but the previous
367*4724848cSchristos      * statement suggests it did.
368*4724848cSchristos      */
369*4724848cSchristos     abort();
370*4724848cSchristos ENGINE_set_default_DSA(e);
371*4724848cSchristos ENGINE_set_default_ciphers(e);
372*4724848cSchristos /* Release the functional reference from ENGINE_init() */
373*4724848cSchristos ENGINE_finish(e);
374*4724848cSchristos /* Release the structural reference from ENGINE_by_id() */
375*4724848cSchristos ENGINE_free(e);
376*4724848cSchristos
377*4724848cSchristosI<Automatically using builtin ENGINE implementations>
378*4724848cSchristos
379*4724848cSchristosHere we'll assume we want to load and register all ENGINE implementations
380*4724848cSchristosbundled with OpenSSL, such that for any cryptographic algorithm required by
381*4724848cSchristosOpenSSL - if there is an ENGINE that implements it and can be initialised,
382*4724848cSchristosit should be used. The following code illustrates how this can work;
383*4724848cSchristos
384*4724848cSchristos /* Load all bundled ENGINEs into memory and make them visible */
385*4724848cSchristos ENGINE_load_builtin_engines();
386*4724848cSchristos /* Register all of them for every algorithm they collectively implement */
387*4724848cSchristos ENGINE_register_all_complete();
388*4724848cSchristos
389*4724848cSchristosThat's all that's required. Eg. the next time OpenSSL tries to set up an
390*4724848cSchristosRSA key, any bundled ENGINEs that implement RSA_METHOD will be passed to
391*4724848cSchristosENGINE_init() and if any of those succeed, that ENGINE will be set as the
392*4724848cSchristosdefault for RSA use from then on.
393*4724848cSchristos
394*4724848cSchristos=head2 Advanced configuration support
395*4724848cSchristos
396*4724848cSchristosThere is a mechanism supported by the ENGINE framework that allows each
397*4724848cSchristosENGINE implementation to define an arbitrary set of configuration
398*4724848cSchristos"commands" and expose them to OpenSSL and any applications based on
399*4724848cSchristosOpenSSL. This mechanism is entirely based on the use of name-value pairs
400*4724848cSchristosand assumes ASCII input (no unicode or UTF for now!), so it is ideal if
401*4724848cSchristosapplications want to provide a transparent way for users to provide
402*4724848cSchristosarbitrary configuration "directives" directly to such ENGINEs. It is also
403*4724848cSchristospossible for the application to dynamically interrogate the loaded ENGINE
404*4724848cSchristosimplementations for the names, descriptions, and input flags of their
405*4724848cSchristosavailable "control commands", providing a more flexible configuration
406*4724848cSchristosscheme. However, if the user is expected to know which ENGINE device he/she
407*4724848cSchristosis using (in the case of specialised hardware, this goes without saying)
408*4724848cSchristosthen applications may not need to concern themselves with discovering the
409*4724848cSchristossupported control commands and simply prefer to pass settings into ENGINEs
410*4724848cSchristosexactly as they are provided by the user.
411*4724848cSchristos
412*4724848cSchristosBefore illustrating how control commands work, it is worth mentioning what
413*4724848cSchristosthey are typically used for. Broadly speaking there are two uses for
414*4724848cSchristoscontrol commands; the first is to provide the necessary details to the
415*4724848cSchristosimplementation (which may know nothing at all specific to the host system)
416*4724848cSchristosso that it can be initialised for use. This could include the path to any
417*4724848cSchristosdriver or config files it needs to load, required network addresses,
418*4724848cSchristossmart-card identifiers, passwords to initialise protected devices,
419*4724848cSchristoslogging information, etc etc. This class of commands typically needs to be
420*4724848cSchristospassed to an ENGINE B<before> attempting to initialise it, i.e. before
421*4724848cSchristoscalling ENGINE_init(). The other class of commands consist of settings or
422*4724848cSchristosoperations that tweak certain behaviour or cause certain operations to take
423*4724848cSchristosplace, and these commands may work either before or after ENGINE_init(), or
424*4724848cSchristosin some cases both. ENGINE implementations should provide indications of
425*4724848cSchristosthis in the descriptions attached to builtin control commands and/or in
426*4724848cSchristosexternal product documentation.
427*4724848cSchristos
428*4724848cSchristosI<Issuing control commands to an ENGINE>
429*4724848cSchristos
430*4724848cSchristosLet's illustrate by example; a function for which the caller supplies the
431*4724848cSchristosname of the ENGINE it wishes to use, a table of string-pairs for use before
432*4724848cSchristosinitialisation, and another table for use after initialisation. Note that
433*4724848cSchristosthe string-pairs used for control commands consist of a command "name"
434*4724848cSchristosfollowed by the command "parameter" - the parameter could be NULL in some
435*4724848cSchristoscases but the name can not. This function should initialise the ENGINE
436*4724848cSchristos(issuing the "pre" commands beforehand and the "post" commands afterwards)
437*4724848cSchristosand set it as the default for everything except RAND and then return a
438*4724848cSchristosboolean success or failure.
439*4724848cSchristos
440*4724848cSchristos int generic_load_engine_fn(const char *engine_id,
441*4724848cSchristos                            const char **pre_cmds, int pre_num,
442*4724848cSchristos                            const char **post_cmds, int post_num)
443*4724848cSchristos {
444*4724848cSchristos     ENGINE *e = ENGINE_by_id(engine_id);
445*4724848cSchristos     if (!e) return 0;
446*4724848cSchristos     while (pre_num--) {
447*4724848cSchristos         if (!ENGINE_ctrl_cmd_string(e, pre_cmds[0], pre_cmds[1], 0)) {
448*4724848cSchristos             fprintf(stderr, "Failed command (%s - %s:%s)\n", engine_id,
449*4724848cSchristos                     pre_cmds[0], pre_cmds[1] ? pre_cmds[1] : "(NULL)");
450*4724848cSchristos             ENGINE_free(e);
451*4724848cSchristos             return 0;
452*4724848cSchristos         }
453*4724848cSchristos         pre_cmds += 2;
454*4724848cSchristos     }
455*4724848cSchristos     if (!ENGINE_init(e)) {
456*4724848cSchristos         fprintf(stderr, "Failed initialisation\n");
457*4724848cSchristos         ENGINE_free(e);
458*4724848cSchristos         return 0;
459*4724848cSchristos     }
460*4724848cSchristos     /*
461*4724848cSchristos      * ENGINE_init() returned a functional reference, so free the structural
462*4724848cSchristos      * reference from ENGINE_by_id().
463*4724848cSchristos      */
464*4724848cSchristos     ENGINE_free(e);
465*4724848cSchristos     while (post_num--) {
466*4724848cSchristos         if (!ENGINE_ctrl_cmd_string(e, post_cmds[0], post_cmds[1], 0)) {
467*4724848cSchristos             fprintf(stderr, "Failed command (%s - %s:%s)\n", engine_id,
468*4724848cSchristos                     post_cmds[0], post_cmds[1] ? post_cmds[1] : "(NULL)");
469*4724848cSchristos             ENGINE_finish(e);
470*4724848cSchristos             return 0;
471*4724848cSchristos         }
472*4724848cSchristos         post_cmds += 2;
473*4724848cSchristos     }
474*4724848cSchristos     ENGINE_set_default(e, ENGINE_METHOD_ALL & ~ENGINE_METHOD_RAND);
475*4724848cSchristos     /* Success */
476*4724848cSchristos     return 1;
477*4724848cSchristos }
478*4724848cSchristos
479*4724848cSchristosNote that ENGINE_ctrl_cmd_string() accepts a boolean argument that can
480*4724848cSchristosrelax the semantics of the function - if set nonzero it will only return
481*4724848cSchristosfailure if the ENGINE supported the given command name but failed while
482*4724848cSchristosexecuting it, if the ENGINE doesn't support the command name it will simply
483*4724848cSchristosreturn success without doing anything. In this case we assume the user is
484*4724848cSchristosonly supplying commands specific to the given ENGINE so we set this to
485*4724848cSchristosFALSE.
486*4724848cSchristos
487*4724848cSchristosI<Discovering supported control commands>
488*4724848cSchristos
489*4724848cSchristosIt is possible to discover at run-time the names, numerical-ids, descriptions
490*4724848cSchristosand input parameters of the control commands supported by an ENGINE using a
491*4724848cSchristosstructural reference. Note that some control commands are defined by OpenSSL
492*4724848cSchristositself and it will intercept and handle these control commands on behalf of the
493*4724848cSchristosENGINE, i.e. the ENGINE's ctrl() handler is not used for the control command.
494*4724848cSchristosopenssl/engine.h defines an index, ENGINE_CMD_BASE, that all control commands
495*4724848cSchristosimplemented by ENGINEs should be numbered from. Any command value lower than
496*4724848cSchristosthis symbol is considered a "generic" command is handled directly by the
497*4724848cSchristosOpenSSL core routines.
498*4724848cSchristos
499*4724848cSchristosIt is using these "core" control commands that one can discover the control
500*4724848cSchristoscommands implemented by a given ENGINE, specifically the commands:
501*4724848cSchristos
502*4724848cSchristos ENGINE_HAS_CTRL_FUNCTION
503*4724848cSchristos ENGINE_CTRL_GET_FIRST_CMD_TYPE
504*4724848cSchristos ENGINE_CTRL_GET_NEXT_CMD_TYPE
505*4724848cSchristos ENGINE_CTRL_GET_CMD_FROM_NAME
506*4724848cSchristos ENGINE_CTRL_GET_NAME_LEN_FROM_CMD
507*4724848cSchristos ENGINE_CTRL_GET_NAME_FROM_CMD
508*4724848cSchristos ENGINE_CTRL_GET_DESC_LEN_FROM_CMD
509*4724848cSchristos ENGINE_CTRL_GET_DESC_FROM_CMD
510*4724848cSchristos ENGINE_CTRL_GET_CMD_FLAGS
511*4724848cSchristos
512*4724848cSchristosWhilst these commands are automatically processed by the OpenSSL framework code,
513*4724848cSchristosthey use various properties exposed by each ENGINE to process these
514*4724848cSchristosqueries. An ENGINE has 3 properties it exposes that can affect how this behaves;
515*4724848cSchristosit can supply a ctrl() handler, it can specify ENGINE_FLAGS_MANUAL_CMD_CTRL in
516*4724848cSchristosthe ENGINE's flags, and it can expose an array of control command descriptions.
517*4724848cSchristosIf an ENGINE specifies the ENGINE_FLAGS_MANUAL_CMD_CTRL flag, then it will
518*4724848cSchristossimply pass all these "core" control commands directly to the ENGINE's ctrl()
519*4724848cSchristoshandler (and thus, it must have supplied one), so it is up to the ENGINE to
520*4724848cSchristosreply to these "discovery" commands itself. If that flag is not set, then the
521*4724848cSchristosOpenSSL framework code will work with the following rules:
522*4724848cSchristos
523*4724848cSchristos if no ctrl() handler supplied;
524*4724848cSchristos     ENGINE_HAS_CTRL_FUNCTION returns FALSE (zero),
525*4724848cSchristos     all other commands fail.
526*4724848cSchristos if a ctrl() handler was supplied but no array of control commands;
527*4724848cSchristos     ENGINE_HAS_CTRL_FUNCTION returns TRUE,
528*4724848cSchristos     all other commands fail.
529*4724848cSchristos if a ctrl() handler and array of control commands was supplied;
530*4724848cSchristos     ENGINE_HAS_CTRL_FUNCTION returns TRUE,
531*4724848cSchristos     all other commands proceed processing ...
532*4724848cSchristos
533*4724848cSchristosIf the ENGINE's array of control commands is empty then all other commands will
534*4724848cSchristosfail, otherwise; ENGINE_CTRL_GET_FIRST_CMD_TYPE returns the identifier of
535*4724848cSchristosthe first command supported by the ENGINE, ENGINE_GET_NEXT_CMD_TYPE takes the
536*4724848cSchristosidentifier of a command supported by the ENGINE and returns the next command
537*4724848cSchristosidentifier or fails if there are no more, ENGINE_CMD_FROM_NAME takes a string
538*4724848cSchristosname for a command and returns the corresponding identifier or fails if no such
539*4724848cSchristoscommand name exists, and the remaining commands take a command identifier and
540*4724848cSchristosreturn properties of the corresponding commands. All except
541*4724848cSchristosENGINE_CTRL_GET_FLAGS return the string length of a command name or description,
542*4724848cSchristosor populate a supplied character buffer with a copy of the command name or
543*4724848cSchristosdescription. ENGINE_CTRL_GET_FLAGS returns a bitwise-OR'd mask of the following
544*4724848cSchristospossible values:
545*4724848cSchristos
546*4724848cSchristos ENGINE_CMD_FLAG_NUMERIC
547*4724848cSchristos ENGINE_CMD_FLAG_STRING
548*4724848cSchristos ENGINE_CMD_FLAG_NO_INPUT
549*4724848cSchristos ENGINE_CMD_FLAG_INTERNAL
550*4724848cSchristos
551*4724848cSchristosIf the ENGINE_CMD_FLAG_INTERNAL flag is set, then any other flags are purely
552*4724848cSchristosinformational to the caller - this flag will prevent the command being usable
553*4724848cSchristosfor any higher-level ENGINE functions such as ENGINE_ctrl_cmd_string().
554*4724848cSchristos"INTERNAL" commands are not intended to be exposed to text-based configuration
555*4724848cSchristosby applications, administrations, users, etc. These can support arbitrary
556*4724848cSchristosoperations via ENGINE_ctrl(), including passing to and/or from the control
557*4724848cSchristoscommands data of any arbitrary type. These commands are supported in the
558*4724848cSchristosdiscovery mechanisms simply to allow applications to determine if an ENGINE
559*4724848cSchristossupports certain specific commands it might want to use (e.g. application "foo"
560*4724848cSchristosmight query various ENGINEs to see if they implement "FOO_GET_VENDOR_LOGO_GIF" -
561*4724848cSchristosand ENGINE could therefore decide whether or not to support this "foo"-specific
562*4724848cSchristosextension).
563*4724848cSchristos
564*4724848cSchristos=head1 ENVIRONMENT
565*4724848cSchristos
566*4724848cSchristos=over 4
567*4724848cSchristos
568*4724848cSchristos=item B<OPENSSL_ENGINES>
569*4724848cSchristos
570*4724848cSchristosThe path to the engines directory.
571*4724848cSchristosIgnored in set-user-ID and set-group-ID programs.
572*4724848cSchristos
573*4724848cSchristos=back
574*4724848cSchristos
575*4724848cSchristos=head1 RETURN VALUES
576*4724848cSchristos
577*4724848cSchristosENGINE_get_first(), ENGINE_get_last(), ENGINE_get_next() and ENGINE_get_prev()
578*4724848cSchristosreturn a valid B<ENGINE> structure or NULL if an error occurred.
579*4724848cSchristos
580*4724848cSchristosENGINE_add() and ENGINE_remove() return 1 on success or 0 on error.
581*4724848cSchristos
582*4724848cSchristosENGINE_by_id() returns a valid B<ENGINE> structure or NULL if an error occurred.
583*4724848cSchristos
584*4724848cSchristosENGINE_init() and ENGINE_finish() return 1 on success or 0 on error.
585*4724848cSchristos
586*4724848cSchristosAll ENGINE_get_default_TYPE() functions, ENGINE_get_cipher_engine() and
587*4724848cSchristosENGINE_get_digest_engine() return a valid B<ENGINE> structure on success or NULL
588*4724848cSchristosif an error occurred.
589*4724848cSchristos
590*4724848cSchristosAll ENGINE_set_default_TYPE() functions return 1 on success or 0 on error.
591*4724848cSchristos
592*4724848cSchristosENGINE_set_default() returns 1 on success or 0 on error.
593*4724848cSchristos
594*4724848cSchristosENGINE_get_table_flags() returns an unsigned integer value representing the
595*4724848cSchristosglobal table flags which are used to control the registration behaviour of
596*4724848cSchristosB<ENGINE> implementations.
597*4724848cSchristos
598*4724848cSchristosAll ENGINE_register_TYPE() functions return 1 on success or 0 on error.
599*4724848cSchristos
600*4724848cSchristosENGINE_register_complete() and ENGINE_register_all_complete() always return 1.
601*4724848cSchristos
602*4724848cSchristosENGINE_ctrl() returns a positive value on success or others on error.
603*4724848cSchristos
604*4724848cSchristosENGINE_cmd_is_executable() returns 1 if B<cmd> is executable or 0 otherwise.
605*4724848cSchristos
606*4724848cSchristosENGINE_ctrl_cmd() and ENGINE_ctrl_cmd_string() return 1 on success or 0 on error.
607*4724848cSchristos
608*4724848cSchristosENGINE_new() returns a valid B<ENGINE> structure on success or NULL if an error
609*4724848cSchristosoccurred.
610*4724848cSchristos
611*4724848cSchristosENGINE_free() always returns 1.
612*4724848cSchristos
613*4724848cSchristosENGINE_up_ref() returns 1 on success or 0 on error.
614*4724848cSchristos
615*4724848cSchristosENGINE_set_id() and ENGINE_set_name() return 1 on success or 0 on error.
616*4724848cSchristos
617*4724848cSchristosAll other B<ENGINE_set_*> functions return 1 on success or 0 on error.
618*4724848cSchristos
619*4724848cSchristosENGINE_get_id() and ENGINE_get_name() return a string representing the identifier
620*4724848cSchristosand the name of the ENGINE B<e> respectively.
621*4724848cSchristos
622*4724848cSchristosENGINE_get_RSA(), ENGINE_get_DSA(), ENGINE_get_DH() and ENGINE_get_RAND()
623*4724848cSchristosreturn corresponding method structures for each algorithms.
624*4724848cSchristos
625*4724848cSchristosENGINE_get_destroy_function(), ENGINE_get_init_function(),
626*4724848cSchristosENGINE_get_finish_function(), ENGINE_get_ctrl_function(),
627*4724848cSchristosENGINE_get_load_privkey_function(), ENGINE_get_load_pubkey_function(),
628*4724848cSchristosENGINE_get_ciphers() and ENGINE_get_digests() return corresponding function
629*4724848cSchristospointers of the callbacks.
630*4724848cSchristos
631*4724848cSchristosENGINE_get_cipher() returns a valid B<EVP_CIPHER> structure on success or NULL
632*4724848cSchristosif an error occurred.
633*4724848cSchristos
634*4724848cSchristosENGINE_get_digest() returns a valid B<EVP_MD> structure on success or NULL if an
635*4724848cSchristoserror occurred.
636*4724848cSchristos
637*4724848cSchristosENGINE_get_flags() returns an integer representing the ENGINE flags which are
638*4724848cSchristosused to control various behaviours of an ENGINE.
639*4724848cSchristos
640*4724848cSchristosENGINE_get_cmd_defns() returns an B<ENGINE_CMD_DEFN> structure or NULL if it's
641*4724848cSchristosnot set.
642*4724848cSchristos
643*4724848cSchristosENGINE_load_private_key() and ENGINE_load_public_key() return a valid B<EVP_PKEY>
644*4724848cSchristosstructure on success or NULL if an error occurred.
645*4724848cSchristos
646*4724848cSchristos=head1 SEE ALSO
647*4724848cSchristos
648*4724848cSchristosL<OPENSSL_init_crypto(3)>, L<RSA_new_method(3)>, L<DSA_new(3)>, L<DH_new(3)>,
649*4724848cSchristosL<RAND_bytes(3)>, L<config(5)>
650*4724848cSchristos
651*4724848cSchristos=head1 HISTORY
652*4724848cSchristos
653*4724848cSchristosENGINE_cleanup() was deprecated in OpenSSL 1.1.0 by the automatic cleanup
654*4724848cSchristosdone by OPENSSL_cleanup()
655*4724848cSchristosand should not be used.
656*4724848cSchristos
657*4724848cSchristos=head1 COPYRIGHT
658*4724848cSchristos
659*4724848cSchristosCopyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
660*4724848cSchristos
661*4724848cSchristosLicensed under the OpenSSL license (the "License").  You may not use
662*4724848cSchristosthis file except in compliance with the License.  You can obtain a copy
663*4724848cSchristosin the file LICENSE in the source distribution or at
664*4724848cSchristosL<https://www.openssl.org/source/license.html>.
665*4724848cSchristos
666*4724848cSchristos=cut
667