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