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