]> git.ipfire.org Git - thirdparty/openssl.git/blame - doc/crypto/engine.pod
Rename INIT funtions, deprecate old ones.
[thirdparty/openssl.git] / doc / crypto / engine.pod
CommitLineData
3f90e450
GT
1=pod
2
3=head1 NAME
4
5engine - ENGINE cryptographic module support
6
7=head1 SYNOPSIS
8
9 #include <openssl/engine.h>
10
11 ENGINE *ENGINE_get_first(void);
12 ENGINE *ENGINE_get_last(void);
13 ENGINE *ENGINE_get_next(ENGINE *e);
14 ENGINE *ENGINE_get_prev(ENGINE *e);
15
16 int ENGINE_add(ENGINE *e);
17 int ENGINE_remove(ENGINE *e);
18
19 ENGINE *ENGINE_by_id(const char *id);
20
21 int ENGINE_init(ENGINE *e);
22 int ENGINE_finish(ENGINE *e);
23
3f90e450
GT
24 void ENGINE_load_builtin_engines(void);
25
26 void ENGINE_cleanup(void);
27
28 ENGINE *ENGINE_get_default_RSA(void);
29 ENGINE *ENGINE_get_default_DSA(void);
6a659296
GT
30 ENGINE *ENGINE_get_default_ECDH(void);
31 ENGINE *ENGINE_get_default_ECDSA(void);
3f90e450
GT
32 ENGINE *ENGINE_get_default_DH(void);
33 ENGINE *ENGINE_get_default_RAND(void);
34 ENGINE *ENGINE_get_cipher_engine(int nid);
35 ENGINE *ENGINE_get_digest_engine(int nid);
36
37 int ENGINE_set_default_RSA(ENGINE *e);
38 int ENGINE_set_default_DSA(ENGINE *e);
6a659296
GT
39 int ENGINE_set_default_ECDH(ENGINE *e);
40 int ENGINE_set_default_ECDSA(ENGINE *e);
3f90e450
GT
41 int ENGINE_set_default_DH(ENGINE *e);
42 int ENGINE_set_default_RAND(ENGINE *e);
43 int ENGINE_set_default_ciphers(ENGINE *e);
44 int ENGINE_set_default_digests(ENGINE *e);
45 int ENGINE_set_default_string(ENGINE *e, const char *list);
46
47 int ENGINE_set_default(ENGINE *e, unsigned int flags);
48
49 unsigned int ENGINE_get_table_flags(void);
50 void ENGINE_set_table_flags(unsigned int flags);
51
52 int ENGINE_register_RSA(ENGINE *e);
53 void ENGINE_unregister_RSA(ENGINE *e);
54 void ENGINE_register_all_RSA(void);
55 int ENGINE_register_DSA(ENGINE *e);
56 void ENGINE_unregister_DSA(ENGINE *e);
57 void ENGINE_register_all_DSA(void);
6a659296
GT
58 int ENGINE_register_ECDH(ENGINE *e);
59 void ENGINE_unregister_ECDH(ENGINE *e);
60 void ENGINE_register_all_ECDH(void);
61 int ENGINE_register_ECDSA(ENGINE *e);
62 void ENGINE_unregister_ECDSA(ENGINE *e);
63 void ENGINE_register_all_ECDSA(void);
3f90e450
GT
64 int ENGINE_register_DH(ENGINE *e);
65 void ENGINE_unregister_DH(ENGINE *e);
66 void ENGINE_register_all_DH(void);
67 int ENGINE_register_RAND(ENGINE *e);
68 void ENGINE_unregister_RAND(ENGINE *e);
69 void ENGINE_register_all_RAND(void);
70 int ENGINE_register_ciphers(ENGINE *e);
71 void ENGINE_unregister_ciphers(ENGINE *e);
72 void ENGINE_register_all_ciphers(void);
73 int ENGINE_register_digests(ENGINE *e);
74 void ENGINE_unregister_digests(ENGINE *e);
75 void ENGINE_register_all_digests(void);
76 int ENGINE_register_complete(ENGINE *e);
77 int ENGINE_register_all_complete(void);
78
6a659296 79 int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
3f90e450
GT
80 int ENGINE_cmd_is_executable(ENGINE *e, int cmd);
81 int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
6a659296 82 long i, void *p, void (*f)(void), int cmd_optional);
3f90e450 83 int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
6a659296 84 int cmd_optional);
3f90e450 85
3f90e450
GT
86 ENGINE *ENGINE_new(void);
87 int ENGINE_free(ENGINE *e);
6a659296 88 int ENGINE_up_ref(ENGINE *e);
3f90e450
GT
89
90 int ENGINE_set_id(ENGINE *e, const char *id);
91 int ENGINE_set_name(ENGINE *e, const char *name);
92 int ENGINE_set_RSA(ENGINE *e, const RSA_METHOD *rsa_meth);
93 int ENGINE_set_DSA(ENGINE *e, const DSA_METHOD *dsa_meth);
6a659296
GT
94 int ENGINE_set_ECDH(ENGINE *e, const ECDH_METHOD *dh_meth);
95 int ENGINE_set_ECDSA(ENGINE *e, const ECDSA_METHOD *dh_meth);
3f90e450
GT
96 int ENGINE_set_DH(ENGINE *e, const DH_METHOD *dh_meth);
97 int ENGINE_set_RAND(ENGINE *e, const RAND_METHOD *rand_meth);
98 int ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f);
99 int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f);
100 int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f);
101 int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f);
102 int ENGINE_set_load_privkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpriv_f);
103 int ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f);
104 int ENGINE_set_ciphers(ENGINE *e, ENGINE_CIPHERS_PTR f);
105 int ENGINE_set_digests(ENGINE *e, ENGINE_DIGESTS_PTR f);
106 int ENGINE_set_flags(ENGINE *e, int flags);
107 int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns);
108
109 const char *ENGINE_get_id(const ENGINE *e);
110 const char *ENGINE_get_name(const ENGINE *e);
111 const RSA_METHOD *ENGINE_get_RSA(const ENGINE *e);
112 const DSA_METHOD *ENGINE_get_DSA(const ENGINE *e);
6a659296
GT
113 const ECDH_METHOD *ENGINE_get_ECDH(const ENGINE *e);
114 const ECDSA_METHOD *ENGINE_get_ECDSA(const ENGINE *e);
3f90e450
GT
115 const DH_METHOD *ENGINE_get_DH(const ENGINE *e);
116 const RAND_METHOD *ENGINE_get_RAND(const ENGINE *e);
117 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e);
118 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e);
119 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e);
120 ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e);
121 ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e);
122 ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e);
123 ENGINE_CIPHERS_PTR ENGINE_get_ciphers(const ENGINE *e);
124 ENGINE_DIGESTS_PTR ENGINE_get_digests(const ENGINE *e);
125 const EVP_CIPHER *ENGINE_get_cipher(ENGINE *e, int nid);
126 const EVP_MD *ENGINE_get_digest(ENGINE *e, int nid);
127 int ENGINE_get_flags(const ENGINE *e);
128 const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e);
129
130 EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
131 UI_METHOD *ui_method, void *callback_data);
132 EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
133 UI_METHOD *ui_method, void *callback_data);
134
135 void ENGINE_add_conf_module(void);
136
137=head1 DESCRIPTION
138
139These functions create, manipulate, and use cryptographic modules in the
140form of B<ENGINE> objects. These objects act as containers for
141implementations of cryptographic algorithms, and support a
142reference-counted mechanism to allow them to be dynamically loaded in and
143out of the running application.
144
145The cryptographic functionality that can be provided by an B<ENGINE>
146implementation includes the following abstractions;
147
148 RSA_METHOD - for providing alternative RSA implementations
6a659296 149 DSA_METHOD, DH_METHOD, RAND_METHOD, ECDH_METHOD, ECDSA_METHOD,
7984f082 150 - similarly for other OpenSSL APIs
3f90e450
GT
151 EVP_CIPHER - potentially multiple cipher algorithms (indexed by 'nid')
152 EVP_DIGEST - potentially multiple hash algorithms (indexed by 'nid')
153 key-loading - loading public and/or private EVP_PKEY keys
154
155=head2 Reference counting and handles
156
157Due to the modular nature of the ENGINE API, pointers to ENGINEs need to be
158treated as handles - ie. not only as pointers, but also as references to
6a659296 159the underlying ENGINE object. Ie. one should obtain a new reference when
3f90e450 160making copies of an ENGINE pointer if the copies will be used (and
b6a338cb 161released) independently.
3f90e450
GT
162
163ENGINE objects have two levels of reference-counting to match the way in
164which the objects are used. At the most basic level, each ENGINE pointer is
6a659296
GT
165inherently a B<structural> reference - a structural reference is required
166to use the pointer value at all, as this kind of reference is a guarantee
167that the structure can not be deallocated until the reference is released.
168
169However, a structural reference provides no guarantee that the ENGINE is
740ceb5b 170initialised and able to use any of its cryptographic
6a659296
GT
171implementations. Indeed it's quite possible that most ENGINEs will not
172initialise at all in typical environments, as ENGINEs are typically used to
3f90e450
GT
173support specialised hardware. To use an ENGINE's functionality, you need a
174B<functional> reference. This kind of reference can be considered a
175specialised form of structural reference, because each functional reference
176implicitly contains a structural reference as well - however to avoid
177difficult-to-find programming bugs, it is recommended to treat the two
b6a338cb 178kinds of reference independently. If you have a functional reference to an
740ceb5b
RS
179ENGINE, you have a guarantee that the ENGINE has been initialised and
180is ready to perform cryptographic operations, and will remain initialised
6a659296 181until after you have released your reference.
3f90e450 182
4390d661 183I<Structural references>
3f90e450 184
6a659296
GT
185This basic type of reference is used for instantiating new ENGINEs,
186iterating across OpenSSL's internal linked-list of loaded
3f90e450
GT
187ENGINEs, reading information about an ENGINE, etc. Essentially a structural
188reference is sufficient if you only need to query or manipulate the data of
189an ENGINE implementation rather than use its functionality.
190
191The ENGINE_new() function returns a structural reference to a new (empty)
6a659296
GT
192ENGINE object. There are other ENGINE API functions that return structural
193references such as; ENGINE_by_id(), ENGINE_get_first(), ENGINE_get_last(),
194ENGINE_get_next(), ENGINE_get_prev(). All structural references should be
195released by a corresponding to call to the ENGINE_free() function - the
196ENGINE object itself will only actually be cleaned up and deallocated when
197the last structural reference is released.
3f90e450
GT
198
199It should also be noted that many ENGINE API function calls that accept a
200structural reference will internally obtain another reference - typically
201this happens whenever the supplied ENGINE will be needed by OpenSSL after
202the function has returned. Eg. the function to add a new ENGINE to
203OpenSSL's internal list is ENGINE_add() - if this function returns success,
204then OpenSSL will have stored a new structural reference internally so the
205caller is still responsible for freeing their own reference with
206ENGINE_free() when they are finished with it. In a similar way, some
207functions will automatically release the structural reference passed to it
208if part of the function's job is to do so. Eg. the ENGINE_get_next() and
209ENGINE_get_prev() functions are used for iterating across the internal
210ENGINE list - they will return a new structural reference to the next (or
211previous) ENGINE in the list or NULL if at the end (or beginning) of the
212list, but in either case the structural reference passed to the function is
213released on behalf of the caller.
214
215To clarify a particular function's handling of references, one should
216always consult that function's documentation "man" page, or failing that
217the openssl/engine.h header file includes some hints.
218
4390d661 219I<Functional references>
3f90e450
GT
220
221As mentioned, functional references exist when the cryptographic
222functionality of an ENGINE is required to be available. A functional
223reference can be obtained in one of two ways; from an existing structural
224reference to the required ENGINE, or by asking OpenSSL for the default
225operational ENGINE for a given cryptographic purpose.
226
227To obtain a functional reference from an existing structural reference,
228call the ENGINE_init() function. This returns zero if the ENGINE was not
229already operational and couldn't be successfully initialised (eg. lack of
230system drivers, no special hardware attached, etc), otherwise it will
231return non-zero to indicate that the ENGINE is now operational and will
6a659296
GT
232have allocated a new B<functional> reference to the ENGINE. All functional
233references are released by calling ENGINE_finish() (which removes the
234implicit structural reference as well).
3f90e450
GT
235
236The second way to get a functional reference is by asking OpenSSL for a
237default implementation for a given task, eg. by ENGINE_get_default_RSA(),
238ENGINE_get_default_cipher_engine(), etc. These are discussed in the next
239section, though they are not usually required by application programmers as
240they are used automatically when creating and using the relevant
241algorithm-specific types in OpenSSL, such as RSA, DSA, EVP_CIPHER_CTX, etc.
242
243=head2 Default implementations
244
245For each supported abstraction, the ENGINE code maintains an internal table
246of state to control which implementations are available for a given
247abstraction and which should be used by default. These implementations are
6a659296 248registered in the tables and indexed by an 'nid' value, because
3f90e450 249abstractions like EVP_CIPHER and EVP_DIGEST support many distinct
6a659296
GT
250algorithms and modes, and ENGINEs can support arbitrarily many of them.
251In the case of other abstractions like RSA, DSA, etc, there is only one
252"algorithm" so all implementations implicitly register using the same 'nid'
253index.
254
255When a default ENGINE is requested for a given abstraction/algorithm/mode, (eg.
256when calling RSA_new_method(NULL)), a "get_default" call will be made to the
257ENGINE subsystem to process the corresponding state table and return a
258functional reference to an initialised ENGINE whose implementation should be
259used. If no ENGINE should (or can) be used, it will return NULL and the caller
260will operate with a NULL ENGINE handle - this usually equates to using the
261conventional software implementation. In the latter case, OpenSSL will from
262then on behave the way it used to before the ENGINE API existed.
3f90e450
GT
263
264Each state table has a flag to note whether it has processed this
265"get_default" query since the table was last modified, because to process
266this question it must iterate across all the registered ENGINEs in the
267table trying to initialise each of them in turn, in case one of them is
268operational. If it returns a functional reference to an ENGINE, it will
269also cache another reference to speed up processing future queries (without
270needing to iterate across the table). Likewise, it will cache a NULL
271response if no ENGINE was available so that future queries won't repeat the
272same iteration unless the state table changes. This behaviour can also be
273changed; if the ENGINE_TABLE_FLAG_NOINIT flag is set (using
274ENGINE_set_table_flags()), no attempted initialisations will take place,
275instead the only way for the state table to return a non-NULL ENGINE to the
276"get_default" query will be if one is expressly set in the table. Eg.
277ENGINE_set_default_RSA() does the same job as ENGINE_register_RSA() except
278that it also sets the state table's cached response for the "get_default"
6a659296
GT
279query. In the case of abstractions like EVP_CIPHER, where implementations are
280indexed by 'nid', these flags and cached-responses are distinct for each 'nid'
281value.
3f90e450
GT
282
283=head2 Application requirements
284
285This section will explain the basic things an application programmer should
286support to make the most useful elements of the ENGINE functionality
287available to the user. The first thing to consider is whether the
288programmer wishes to make alternative ENGINE modules available to the
289application and user. OpenSSL maintains an internal linked list of
290"visible" ENGINEs from which it has to operate - at start-up, this list is
291empty and in fact if an application does not call any ENGINE API calls and
292it uses static linking against openssl, then the resulting application
293binary will not contain any alternative ENGINE code at all. So the first
294consideration is whether any/all available ENGINE implementations should be
295made visible to OpenSSL - this is controlled by calling the various "load"
f672aee4 296functions.
3f90e450
GT
297
298Having called any of these functions, ENGINE objects would have been
299dynamically allocated and populated with these implementations and linked
300into OpenSSL's internal linked list. At this point it is important to
301mention an important API function;
302
303 void ENGINE_cleanup(void);
304
305If no ENGINE API functions are called at all in an application, then there
306are no inherent memory leaks to worry about from the ENGINE functionality,
6a659296 307however if any ENGINEs are loaded, even if they are never registered or
3f90e450
GT
308used, it is necessary to use the ENGINE_cleanup() function to
309correspondingly cleanup before program exit, if the caller wishes to avoid
310memory leaks. This mechanism uses an internal callback registration table
311so that any ENGINE API functionality that knows it requires cleanup can
312register its cleanup details to be called during ENGINE_cleanup(). This
313approach allows ENGINE_cleanup() to clean up after any ENGINE functionality
314at all that your program uses, yet doesn't automatically create linker
315dependencies to all possible ENGINE functionality - only the cleanup
316callbacks required by the functionality you do use will be required by the
317linker.
318
319The fact that ENGINEs are made visible to OpenSSL (and thus are linked into
320the program and loaded into memory at run-time) does not mean they are
321"registered" or called into use by OpenSSL automatically - that behaviour
6a659296 322is something for the application to control. Some applications
3f90e450
GT
323will want to allow the user to specify exactly which ENGINE they want used
324if any is to be used at all. Others may prefer to load all support and have
325OpenSSL automatically use at run-time any ENGINE that is able to
326successfully initialise - ie. to assume that this corresponds to
327acceleration hardware attached to the machine or some such thing. There are
328probably numerous other ways in which applications may prefer to handle
329things, so we will simply illustrate the consequences as they apply to a
330couple of simple cases and leave developers to consider these and the
331source code to openssl's builtin utilities as guides.
332
4390d661 333I<Using a specific ENGINE implementation>
3f90e450
GT
334
335Here we'll assume an application has been configured by its user or admin
336to want to use the "ACME" ENGINE if it is available in the version of
337OpenSSL the application was compiled with. If it is available, it should be
740ceb5b 338used by default for all RSA, DSA, and symmetric cipher operations, otherwise
3f90e450
GT
339OpenSSL should use its builtin software as per usual. The following code
340illustrates how to approach this;
341
342 ENGINE *e;
343 const char *engine_id = "ACME";
344 ENGINE_load_builtin_engines();
345 e = ENGINE_by_id(engine_id);
346 if(!e)
347 /* the engine isn't available */
348 return;
349 if(!ENGINE_init(e)) {
350 /* the engine couldn't initialise, release 'e' */
351 ENGINE_free(e);
352 return;
353 }
354 if(!ENGINE_set_default_RSA(e))
355 /* This should only happen when 'e' can't initialise, but the previous
356 * statement suggests it did. */
357 abort();
358 ENGINE_set_default_DSA(e);
359 ENGINE_set_default_ciphers(e);
360 /* Release the functional reference from ENGINE_init() */
361 ENGINE_finish(e);
362 /* Release the structural reference from ENGINE_by_id() */
363 ENGINE_free(e);
364
4390d661 365I<Automatically using builtin ENGINE implementations>
3f90e450
GT
366
367Here we'll assume we want to load and register all ENGINE implementations
368bundled with OpenSSL, such that for any cryptographic algorithm required by
740ceb5b 369OpenSSL - if there is an ENGINE that implements it and can be initialised,
3f90e450
GT
370it should be used. The following code illustrates how this can work;
371
372 /* Load all bundled ENGINEs into memory and make them visible */
373 ENGINE_load_builtin_engines();
374 /* Register all of them for every algorithm they collectively implement */
375 ENGINE_register_all_complete();
376
377That's all that's required. Eg. the next time OpenSSL tries to set up an
378RSA key, any bundled ENGINEs that implement RSA_METHOD will be passed to
379ENGINE_init() and if any of those succeed, that ENGINE will be set as the
6a659296 380default for RSA use from then on.
3f90e450
GT
381
382=head2 Advanced configuration support
383
384There is a mechanism supported by the ENGINE framework that allows each
385ENGINE implementation to define an arbitrary set of configuration
386"commands" and expose them to OpenSSL and any applications based on
387OpenSSL. This mechanism is entirely based on the use of name-value pairs
6a659296 388and assumes ASCII input (no unicode or UTF for now!), so it is ideal if
3f90e450
GT
389applications want to provide a transparent way for users to provide
390arbitrary configuration "directives" directly to such ENGINEs. It is also
391possible for the application to dynamically interrogate the loaded ENGINE
392implementations for the names, descriptions, and input flags of their
393available "control commands", providing a more flexible configuration
394scheme. However, if the user is expected to know which ENGINE device he/she
395is using (in the case of specialised hardware, this goes without saying)
396then applications may not need to concern themselves with discovering the
6a659296
GT
397supported control commands and simply prefer to pass settings into ENGINEs
398exactly as they are provided by the user.
3f90e450
GT
399
400Before illustrating how control commands work, it is worth mentioning what
401they are typically used for. Broadly speaking there are two uses for
402control commands; the first is to provide the necessary details to the
403implementation (which may know nothing at all specific to the host system)
404so that it can be initialised for use. This could include the path to any
405driver or config files it needs to load, required network addresses,
6a659296 406smart-card identifiers, passwords to initialise protected devices,
3f90e450
GT
407logging information, etc etc. This class of commands typically needs to be
408passed to an ENGINE B<before> attempting to initialise it, ie. before
409calling ENGINE_init(). The other class of commands consist of settings or
410operations that tweak certain behaviour or cause certain operations to take
411place, and these commands may work either before or after ENGINE_init(), or
6a659296 412in some cases both. ENGINE implementations should provide indications of
3f90e450
GT
413this in the descriptions attached to builtin control commands and/or in
414external product documentation.
415
4390d661 416I<Issuing control commands to an ENGINE>
3f90e450
GT
417
418Let's illustrate by example; a function for which the caller supplies the
419name of the ENGINE it wishes to use, a table of string-pairs for use before
420initialisation, and another table for use after initialisation. Note that
421the string-pairs used for control commands consist of a command "name"
422followed by the command "parameter" - the parameter could be NULL in some
423cases but the name can not. This function should initialise the ENGINE
424(issuing the "pre" commands beforehand and the "post" commands afterwards)
425and set it as the default for everything except RAND and then return a
426boolean success or failure.
427
428 int generic_load_engine_fn(const char *engine_id,
429 const char **pre_cmds, int pre_num,
430 const char **post_cmds, int post_num)
431 {
432 ENGINE *e = ENGINE_by_id(engine_id);
433 if(!e) return 0;
434 while(pre_num--) {
435 if(!ENGINE_ctrl_cmd_string(e, pre_cmds[0], pre_cmds[1], 0)) {
436 fprintf(stderr, "Failed command (%s - %s:%s)\n", engine_id,
437 pre_cmds[0], pre_cmds[1] ? pre_cmds[1] : "(NULL)");
438 ENGINE_free(e);
439 return 0;
440 }
441 pre_cmds += 2;
442 }
443 if(!ENGINE_init(e)) {
444 fprintf(stderr, "Failed initialisation\n");
445 ENGINE_free(e);
446 return 0;
447 }
448 /* ENGINE_init() returned a functional reference, so free the structural
449 * reference from ENGINE_by_id(). */
450 ENGINE_free(e);
451 while(post_num--) {
452 if(!ENGINE_ctrl_cmd_string(e, post_cmds[0], post_cmds[1], 0)) {
453 fprintf(stderr, "Failed command (%s - %s:%s)\n", engine_id,
454 post_cmds[0], post_cmds[1] ? post_cmds[1] : "(NULL)");
455 ENGINE_finish(e);
456 return 0;
457 }
458 post_cmds += 2;
459 }
460 ENGINE_set_default(e, ENGINE_METHOD_ALL & ~ENGINE_METHOD_RAND);
461 /* Success */
462 return 1;
463 }
464
465Note that ENGINE_ctrl_cmd_string() accepts a boolean argument that can
466relax the semantics of the function - if set non-zero it will only return
467failure if the ENGINE supported the given command name but failed while
468executing it, if the ENGINE doesn't support the command name it will simply
469return success without doing anything. In this case we assume the user is
470only supplying commands specific to the given ENGINE so we set this to
471FALSE.
472
4390d661 473I<Discovering supported control commands>
3f90e450
GT
474
475It is possible to discover at run-time the names, numerical-ids, descriptions
6a659296
GT
476and input parameters of the control commands supported by an ENGINE using a
477structural reference. Note that some control commands are defined by OpenSSL
478itself and it will intercept and handle these control commands on behalf of the
479ENGINE, ie. the ENGINE's ctrl() handler is not used for the control command.
480openssl/engine.h defines an index, ENGINE_CMD_BASE, that all control commands
481implemented by ENGINEs should be numbered from. Any command value lower than
482this symbol is considered a "generic" command is handled directly by the
483OpenSSL core routines.
3f90e450
GT
484
485It is using these "core" control commands that one can discover the the control
486commands implemented by a given ENGINE, specifically the commands;
487
488 #define ENGINE_HAS_CTRL_FUNCTION 10
489 #define ENGINE_CTRL_GET_FIRST_CMD_TYPE 11
490 #define ENGINE_CTRL_GET_NEXT_CMD_TYPE 12
491 #define ENGINE_CTRL_GET_CMD_FROM_NAME 13
492 #define ENGINE_CTRL_GET_NAME_LEN_FROM_CMD 14
493 #define ENGINE_CTRL_GET_NAME_FROM_CMD 15
494 #define ENGINE_CTRL_GET_DESC_LEN_FROM_CMD 16
495 #define ENGINE_CTRL_GET_DESC_FROM_CMD 17
496 #define ENGINE_CTRL_GET_CMD_FLAGS 18
497
498Whilst these commands are automatically processed by the OpenSSL framework code,
6a659296
GT
499they use various properties exposed by each ENGINE to process these
500queries. An ENGINE has 3 properties it exposes that can affect how this behaves;
3f90e450
GT
501it can supply a ctrl() handler, it can specify ENGINE_FLAGS_MANUAL_CMD_CTRL in
502the ENGINE's flags, and it can expose an array of control command descriptions.
503If an ENGINE specifies the ENGINE_FLAGS_MANUAL_CMD_CTRL flag, then it will
504simply pass all these "core" control commands directly to the ENGINE's ctrl()
505handler (and thus, it must have supplied one), so it is up to the ENGINE to
506reply to these "discovery" commands itself. If that flag is not set, then the
507OpenSSL framework code will work with the following rules;
508
509 if no ctrl() handler supplied;
510 ENGINE_HAS_CTRL_FUNCTION returns FALSE (zero),
511 all other commands fail.
512 if a ctrl() handler was supplied but no array of control commands;
513 ENGINE_HAS_CTRL_FUNCTION returns TRUE,
514 all other commands fail.
515 if a ctrl() handler and array of control commands was supplied;
516 ENGINE_HAS_CTRL_FUNCTION returns TRUE,
517 all other commands proceed processing ...
518
519If the ENGINE's array of control commands is empty then all other commands will
520fail, otherwise; ENGINE_CTRL_GET_FIRST_CMD_TYPE returns the identifier of
521the first command supported by the ENGINE, ENGINE_GET_NEXT_CMD_TYPE takes the
522identifier of a command supported by the ENGINE and returns the next command
523identifier or fails if there are no more, ENGINE_CMD_FROM_NAME takes a string
524name for a command and returns the corresponding identifier or fails if no such
525command name exists, and the remaining commands take a command identifier and
526return properties of the corresponding commands. All except
527ENGINE_CTRL_GET_FLAGS return the string length of a command name or description,
528or populate a supplied character buffer with a copy of the command name or
529description. ENGINE_CTRL_GET_FLAGS returns a bitwise-OR'd mask of the following
530possible values;
531
532 #define ENGINE_CMD_FLAG_NUMERIC (unsigned int)0x0001
533 #define ENGINE_CMD_FLAG_STRING (unsigned int)0x0002
534 #define ENGINE_CMD_FLAG_NO_INPUT (unsigned int)0x0004
535 #define ENGINE_CMD_FLAG_INTERNAL (unsigned int)0x0008
536
537If the ENGINE_CMD_FLAG_INTERNAL flag is set, then any other flags are purely
538informational to the caller - this flag will prevent the command being usable
539for any higher-level ENGINE functions such as ENGINE_ctrl_cmd_string().
540"INTERNAL" commands are not intended to be exposed to text-based configuration
541by applications, administrations, users, etc. These can support arbitrary
542operations via ENGINE_ctrl(), including passing to and/or from the control
543commands data of any arbitrary type. These commands are supported in the
186bb907 544discovery mechanisms simply to allow applications to determine if an ENGINE
3f90e450
GT
545supports certain specific commands it might want to use (eg. application "foo"
546might query various ENGINEs to see if they implement "FOO_GET_VENDOR_LOGO_GIF" -
547and ENGINE could therefore decide whether or not to support this "foo"-specific
548extension).
549
3f90e450
GT
550=head1 SEE ALSO
551
f672aee4
RS
552L<OPENSSL_init_crypto(3)>, L<rsa(3)>, L<dsa(3)>, L<dh(3)>, L<rand(3)>
553
554=head1 HISTORY
555
556ENGINE_load_openssl(), ENGINE_load_dynamic(), and ENGINE_load_cryptodev()
557were deprecated in OpenSSL 1.1.0 by OPENSSL_init_crypto().
3f90e450
GT
558
559=cut