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