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