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