import libcrypto (LibreSSL 2.5.2)
[unleashed.git] / lib / libcrypto / man / engine.3
bloba8ef6a89516b333ce3b338a528369f70857d4081
1 .\"     $OpenBSD: engine.3,v 1.10 2017/01/06 20:35:23 schwarze Exp $
2 .\"     OpenSSL a528d4f0 Oct 27 13:40:11 2015 -0400
3 .\"
4 .\" This file was written by Geoff Thorpe <geoff@openssl.org>.
5 .\" Copyright (c) 2002, 2004, 2007, 2015 The OpenSSL Project.
6 .\" All rights reserved.
7 .\"
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
10 .\" are met:
11 .\"
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\"    notice, this list of conditions and the following disclaimer.
14 .\"
15 .\" 2. Redistributions in binary form must reproduce the above copyright
16 .\"    notice, this list of conditions and the following disclaimer in
17 .\"    the documentation and/or other materials provided with the
18 .\"    distribution.
19 .\"
20 .\" 3. All advertising materials mentioning features or use of this
21 .\"    software must display the following acknowledgment:
22 .\"    "This product includes software developed by the OpenSSL Project
23 .\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 .\"
25 .\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 .\"    endorse or promote products derived from this software without
27 .\"    prior written permission. For written permission, please contact
28 .\"    openssl-core@openssl.org.
29 .\"
30 .\" 5. Products derived from this software may not be called "OpenSSL"
31 .\"    nor may "OpenSSL" appear in their names without prior written
32 .\"    permission of the OpenSSL Project.
33 .\"
34 .\" 6. Redistributions of any form whatsoever must retain the following
35 .\"    acknowledgment:
36 .\"    "This product includes software developed by the OpenSSL Project
37 .\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 .\"
39 .\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 .\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 .\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 .\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 .\" OF THE POSSIBILITY OF SUCH DAMAGE.
51 .\"
52 .Dd $Mdocdate: January 6 2017 $
53 .Dt ENGINE 3
54 .Os
55 .Sh NAME
56 .Nm ENGINE_add ,
57 .Nm ENGINE_by_id ,
58 .Nm ENGINE_finish ,
59 .Nm ENGINE_get_first ,
60 .Nm ENGINE_get_last ,
61 .Nm ENGINE_get_next ,
62 .Nm ENGINE_get_prev ,
63 .Nm ENGINE_init ,
64 .Nm ENGINE_load_builtin_engines ,
65 .Nm ENGINE_remove
66 .Nd ENGINE cryptographic module support
67 .Sh SYNOPSIS
68 .In openssl/engine.h
69 .Ft ENGINE *
70 .Fn ENGINE_get_first void
71 .Ft ENGINE *
72 .Fn ENGINE_get_last void
73 .Ft ENGINE *
74 .Fo ENGINE_get_next
75 .Fa "ENGINE *e"
76 .Fc
77 .Ft ENGINE *
78 .Fo ENGINE_get_prev
79 .Fa "ENGINE *e"
80 .Fc
81 .Ft int
82 .Fo ENGINE_add
83 .Fa "ENGINE *e"
84 .Fc
85 .Ft int
86 .Fo ENGINE_remove
87 .Fa "ENGINE *e"
88 .Fc
89 .Ft ENGINE *
90 .Fo ENGINE_by_id
91 .Fa "const char *id"
92 .Fc
93 .Ft int
94 .Fo ENGINE_init
95 .Fa "ENGINE *e"
96 .Fc
97 .Ft int
98 .Fo ENGINE_finish
99 .Fa "ENGINE *e"
101 .Ft void
102 .Fn ENGINE_load_openssl void
103 .Ft void
104 .Fn ENGINE_load_dynamic void
105 .Ft void
106 .Fn ENGINE_load_cryptodev void
107 .Ft void
108 .Fn ENGINE_load_builtin_engines void
109 .Ft void
110 .Fn ENGINE_cleanup void
111 .Ft ENGINE *
112 .Fn ENGINE_get_default_RSA void
113 .Ft ENGINE *
114 .Fn ENGINE_get_default_DSA void
115 .Ft ENGINE *
116 .Fn ENGINE_get_default_ECDH void
117 .Ft ENGINE *
118 .Fn ENGINE_get_default_ECDSA void
119 .Ft ENGINE *
120 .Fn ENGINE_get_default_DH void
121 .Ft ENGINE *
122 .Fn ENGINE_get_default_RAND void
123 .Ft ENGINE *
124 .Fo ENGINE_get_cipher_engine
125 .Fa "int nid"
127 .Ft ENGINE *
128 .Fo ENGINE_get_digest_engine
129 .Fa "int nid"
131 .Ft int
132 .Fo ENGINE_set_default_RSA
133 .Fa "ENGINE *e"
135 .Ft int
136 .Fo ENGINE_set_default_DSA
137 .Fa "ENGINE *e"
139 .Ft int
140 .Fo ENGINE_set_default_ECDH
141 .Fa "ENGINE *e"
143 .Ft int
144 .Fo ENGINE_set_default_ECDSA
145 .Fa "ENGINE *e"
147 .Ft int
148 .Fo ENGINE_set_default_DH
149 .Fa "ENGINE *e"
151 .Ft int
152 .Fo ENGINE_set_default_RAND
153 .Fa "ENGINE *e"
155 .Ft int
156 .Fo ENGINE_set_default_ciphers
157 .Fa "ENGINE *e"
159 .Ft int
160 .Fo ENGINE_set_default_digests
161 .Fa "ENGINE *e"
163 .Ft int
164 .Fo ENGINE_set_default_string
165 .Fa "ENGINE *e"
166 .Fa "const char *list"
168 .Ft int
169 .Fo ENGINE_set_default
170 .Fa "ENGINE *e"
171 .Fa "unsigned int flags"
173 .Ft unsigned int
174 .Fn ENGINE_get_table_flags void
175 .Ft void
176 .Fo ENGINE_set_table_flags
177 .Fa "unsigned int flags"
179 .Ft int
180 .Fo ENGINE_register_RSA
181 .Fa "ENGINE *e"
183 .Ft void
184 .Fo ENGINE_unregister_RSA
185 .Fa "ENGINE *e"
187 .Ft void
188 .Fn ENGINE_register_all_RSA void
189 .Ft int
190 .Fo ENGINE_register_DSA
191 .Fa "ENGINE *e"
193 .Ft void
194 .Fo ENGINE_unregister_DSA
195 .Fa "ENGINE *e"
197 .Ft void
198 .Fn ENGINE_register_all_DSA void
199 .Ft int
200 .Fo ENGINE_register_ECDH
201 .Fa "ENGINE *e"
203 .Ft void
204 .Fo ENGINE_unregister_ECDH
205 .Fa "ENGINE *e"
207 .Ft void
208 .Fn ENGINE_register_all_ECDH void
209 .Ft int
210 .Fo ENGINE_register_ECDSA
211 .Fa "ENGINE *e"
213 .Ft void
214 .Fo ENGINE_unregister_ECDSA
215 .Fa "ENGINE *e"
217 .Ft void
218 .Fn ENGINE_register_all_ECDSA void
219 .Ft int
220 .Fo ENGINE_register_DH
221 .Fa "ENGINE *e"
223 .Ft void
224 .Fo ENGINE_unregister_DH
225 .Fa "ENGINE *e"
227 .Ft void
228 .Fn ENGINE_register_all_DH void
229 .Ft int
230 .Fo ENGINE_register_RAND
231 .Fa "ENGINE *e"
233 .Ft void
234 .Fo ENGINE_unregister_RAND
235 .Fa "ENGINE *e"
237 .Ft void
238 .Fn ENGINE_register_all_RAND void
239 .Ft int
240 .Fo ENGINE_register_STORE
241 .Fa "ENGINE *e"
243 .Ft void
244 .Fo ENGINE_unregister_STORE
245 .Fa "ENGINE *e"
247 .Ft void
248 .Fn ENGINE_register_all_STORE void
249 .Ft int
250 .Fo ENGINE_register_ciphers
251 .Fa "ENGINE *e"
253 .Ft void
254 .Fo ENGINE_unregister_ciphers
255 .Fa "ENGINE *e"
257 .Ft void
258 .Fn ENGINE_register_all_ciphers void
259 .Ft int
260 .Fo ENGINE_register_digests
261 .Fa "ENGINE *e"
263 .Ft void
264 .Fo ENGINE_unregister_digests
265 .Fa "ENGINE *e"
267 .Ft void
268 .Fn ENGINE_register_all_digests void
269 .Ft int
270 .Fo ENGINE_register_complete
271 .Fa "ENGINE *e"
273 .Ft int
274 .Fn ENGINE_register_all_complete void
275 .Ft int
276 .Fo ENGINE_ctrl
277 .Fa "ENGINE *e"
278 .Fa "int cmd"
279 .Fa "long i"
280 .Fa "void *p"
281 .Fa "void (*f)(void)"
283 .Ft int
284 .Fo ENGINE_cmd_is_executable
285 .Fa "ENGINE *e"
286 .Fa "int cmd"
288 .Ft int
289 .Fo ENGINE_ctrl_cmd
290 .Fa "ENGINE *e"
291 .Fa "const char *cmd_name"
292 .Fa "long i"
293 .Fa "void *p"
294 .Fa "void (*f)(void)"
295 .Fa "int cmd_optional"
297 .Ft int
298 .Fo ENGINE_ctrl_cmd_string
299 .Fa "ENGINE *e"
300 .Fa "const char *cmd_name"
301 .Fa "const char *arg"
302 .Fa "int cmd_optional"
304 .Ft ENGINE *
305 .Fn ENGINE_new void
306 .Ft int
307 .Fo ENGINE_free
308 .Fa "ENGINE *e"
310 .Ft int
311 .Fo ENGINE_up_ref
312 .Fa "ENGINE *e"
314 .Ft int
315 .Fo ENGINE_set_id
316 .Fa "ENGINE *e"
317 .Fa "const char *id"
319 .Ft int
320 .Fo ENGINE_set_name
321 .Fa "ENGINE *e"
322 .Fa "const char *name"
324 .Ft int
325 .Fo ENGINE_set_RSA
326 .Fa "ENGINE *e"
327 .Fa "const RSA_METHOD *rsa_meth"
329 .Ft int
330 .Fo ENGINE_set_DSA
331 .Fa "ENGINE *e"
332 .Fa "const DSA_METHOD *dsa_meth"
334 .Ft int
335 .Fo ENGINE_set_ECDH
336 .Fa "ENGINE *e"
337 .Fa "const ECDH_METHOD *dh_meth"
339 .Ft int
340 .Fo ENGINE_set_ECDSA
341 .Fa "ENGINE *e"
342 .Fa "const ECDSA_METHOD *dh_meth"
344 .Ft int
345 .Fo ENGINE_set_DH
346 .Fa "ENGINE *e"
347 .Fa "const DH_METHOD *dh_meth"
349 .Ft int
350 .Fo ENGINE_set_RAND
351 .Fa "ENGINE *e"
352 .Fa "const RAND_METHOD *rand_meth"
354 .Ft int
355 .Fo ENGINE_set_STORE
356 .Fa "ENGINE *e"
357 .Fa "const STORE_METHOD *rand_meth"
359 .Ft int
360 .Fo ENGINE_set_destroy_function
361 .Fa "ENGINE *e"
362 .Fa "ENGINE_GEN_INT_FUNC_PTR destroy_f"
364 .Ft int
365 .Fo ENGINE_set_init_function
366 .Fa "ENGINE *e"
367 .Fa "ENGINE_GEN_INT_FUNC_PTR init_f"
369 .Ft int
370 .Fo ENGINE_set_finish_function
371 .Fa "ENGINE *e"
372 .Fa "ENGINE_GEN_INT_FUNC_PTR finish_f"
374 .Ft int
375 .Fo ENGINE_set_ctrl_function
376 .Fa "ENGINE *e"
377 .Fa "ENGINE_CTRL_FUNC_PTR ctrl_f"
379 .Ft int
380 .Fo ENGINE_set_load_privkey_function
381 .Fa "ENGINE *e"
382 .Fa "ENGINE_LOAD_KEY_PTR loadpriv_f"
384 .Ft int
385 .Fo ENGINE_set_load_pubkey_function
386 .Fa "ENGINE *e"
387 .Fa "ENGINE_LOAD_KEY_PTR loadpub_f"
389 .Ft int
390 .Fo ENGINE_set_ciphers
391 .Fa "ENGINE *e"
392 .Fa "ENGINE_CIPHERS_PTR f"
394 .Ft int
395 .Fo ENGINE_set_digests
396 .Fa "ENGINE *e"
397 .Fa "ENGINE_DIGESTS_PTR f"
399 .Ft int
400 .Fo ENGINE_set_flags
401 .Fa "ENGINE *e"
402 .Fa "int flags"
404 .Ft int
405 .Fo ENGINE_set_cmd_defns
406 .Fa "ENGINE *e"
407 .Fa "const ENGINE_CMD_DEFN *defns"
409 .Ft const char *
410 .Fo ENGINE_get_id
411 .Fa "const ENGINE *e"
413 .Ft const char *
414 .Fo ENGINE_get_name
415 .Fa "const ENGINE *e"
417 .Ft const RSA_METHOD *
418 .Fo ENGINE_get_RSA
419 .Fa "const ENGINE *e"
421 .Ft const DSA_METHOD *
422 .Fo ENGINE_get_DSA
423 .Fa "const ENGINE *e"
425 .Ft const ECDH_METHOD *
426 .Fo ENGINE_get_ECDH
427 .Fa "const ENGINE *e"
429 .Ft const ECDSA_METHOD *
430 .Fo ENGINE_get_ECDSA
431 .Fa "const ENGINE *e"
433 .Ft const DH_METHOD *
434 .Fo ENGINE_get_DH
435 .Fa "const ENGINE *e"
437 .Ft const RAND_METHOD *
438 .Fo ENGINE_get_RAND
439 .Fa "const ENGINE *e"
441 .Ft const STORE_METHOD *
442 .Fo ENGINE_get_STORE
443 .Fa "const ENGINE *e"
445 .Ft ENGINE_GEN_INT_FUNC_PTR
446 .Fo ENGINE_get_destroy_function
447 .Fa "const ENGINE *e"
449 .Ft ENGINE_GEN_INT_FUNC_PTR
450 .Fo ENGINE_get_init_function
451 .Fa "const ENGINE *e"
453 .Ft ENGINE_GEN_INT_FUNC_PTR
454 .Fo ENGINE_get_finish_function
455 .Fa "const ENGINE *e"
457 .Ft ENGINE_CTRL_FUNC_PTR
458 .Fo ENGINE_get_ctrl_function
459 .Fa "const ENGINE *e"
461 .Ft ENGINE_LOAD_KEY_PTR
462 .Fo ENGINE_get_load_privkey_function
463 .Fa "const ENGINE *e"
465 .Ft ENGINE_LOAD_KEY_PTR
466 .Fo ENGINE_get_load_pubkey_function
467 .Fa "const ENGINE *e"
469 .Ft ENGINE_CIPHERS_PTR
470 .Fo ENGINE_get_ciphers
471 .Fa "const ENGINE *e"
473 .Ft ENGINE_DIGESTS_PTR
474 .Fo ENGINE_get_digests
475 .Fa "const ENGINE *e"
477 .Ft const EVP_CIPHER *
478 .Fo ENGINE_get_cipher
479 .Fa "ENGINE *e"
480 .Fa "int nid"
482 .Ft const EVP_MD *
483 .Fo ENGINE_get_digest
484 .Fa "ENGINE *e"
485 .Fa "int nid"
487 .Ft int
488 .Fo ENGINE_get_flags
489 .Fa "const ENGINE *e"
491 .Ft const ENGINE_CMD_DEFN *
492 .Fo ENGINE_get_cmd_defns
493 .Fa "const ENGINE *e"
495 .Ft EVP_PKEY *
496 .Fo ENGINE_load_private_key
497 .Fa "ENGINE *e"
498 .Fa "const char *key_id"
499 .Fa "UI_METHOD *ui_method"
500 .Fa "void *callback_data"
502 .Ft EVP_PKEY *
503 .Fo ENGINE_load_public_key
504 .Fa "ENGINE *e"
505 .Fa "const char *key_id"
506 .Fa "UI_METHOD *ui_method"
507 .Fa "void *callback_data"
509 .Sh DESCRIPTION
510 These functions create, manipulate, and use cryptographic modules
511 in the form of
512 .Vt ENGINE
513 objects.
514 These objects act as containers for implementations of cryptographic
515 algorithms, and support a reference-counted mechanism to allow them to
516 be dynamically loaded in and out of the running application.
518 The cryptographic functionality that can be provided by an
519 .Vt ENGINE
520 implementation includes the following abstractions:
522 .Bl -bullet -compact
524 .Vt RSA_METHOD :
525 for providing alternative RSA implementations
527 .Vt DSA_METHOD , DH_METHOD , RAND_METHOD , ECDH_METHOD ,
528 .Vt ECDSA_METHOD , STORE_METHOD :
529 similarly for other OpenSSL APIs
531 .Vt EVP_CIPHER :
532 potentially multiple cipher algorithms (indexed by 'nid')
534 .Vt EVP_DIGEST :
535 potentially multiple hash algorithms (indexed by 'nid')
537 key-loading: loading public and/or private EVP_PKEY keys
539 .Ss Reference counting and handles
540 Due to the modular nature of the
541 .Nm engine
542 API, pointers to
543 .Vt ENGINE Ns s
544 need to be treated as handles - i.e. not only as pointers, but also
545 as references to the underlying
546 .Vt ENGINE
547 object.
548 One should obtain a new reference when making copies of an
549 .Vt ENGINE
550 pointer if the copies will be used (and released) independently.
552 .Vt ENGINE
553 objects have two levels of reference-counting to match the way in
554 which the objects are used.
555 At the most basic level, each
556 .Vt ENGINE
557 pointer is inherently a
558 .Sy structural
559 reference - a structural reference is required to use the pointer value
560 at all, as this kind of reference is a guarantee that the structure cannot
561 be deallocated until the reference is released.
563 However, a structural reference provides no guarantee that the
564 .Vt ENGINE
565 is initialised and able to use any of its cryptographic implementations.
566 Indeed it's quite possible that most
567 .Vt ENGINE Ns s
568 will not initialise at all in typical environments, as
569 .Vt ENGINE Ns s
570 are typically used to support specialised hardware.
571 To use an
572 .Vt ENGINE Ap s
573 functionality, you need a
574 .Sy functional
575 reference.
576 This kind of reference can be considered a specialised form of
577 structural reference, because each functional reference implicitly
578 contains a structural reference as well - however to avoid
579 difficult-to-find programming bugs, it is recommended to treat the two
580 kinds of reference independently.
581 If you have a functional reference to an
582 .Vt ENGINE ,
583 you have a guarantee that the
584 .Vt ENGINE
585 has been initialised and is ready to perform cryptographic operations and
586 will remain uninitialised until after you have released your
587 reference.
589 .Em Structural references
591 This basic type of reference is used for instantiating new
592 .Vt ENGINE Ns s ,
593 iterating across OpenSSL's internal linked-list of loaded
594 .Vt ENGINE Ns s ,
595 reading information about an
596 .Vt ENGINE ,
597 etc.
598 Essentially a structural reference is sufficient if you only need to
599 query or manipulate the data of an
600 .Vt ENGINE
601 implementation rather than use its functionality.
604 .Fn ENGINE_new
605 function returns a structural reference to a new (empty)
606 .Vt ENGINE
607 object.
608 There are other
609 .Nm engine
610 API functions that return structural references such as
611 .Fn ENGINE_by_id ,
612 .Fn ENGINE_get_first ,
613 .Fn ENGINE_get_last ,
614 .Fn ENGINE_get_next ,
616 .Fn ENGINE_get_prev .
617 All structural references should be released by a corresponding call
618 to the
619 .Fn ENGINE_free
620 function.
622 .Vt ENGINE
623 object itself will only actually be cleaned up and deallocated when
624 the last structural reference is released.
626 It should also be noted that many
627 .Nm engine
628 API function calls that accept a structural reference will internally
629 obtain another reference.
630 Typically this happens whenever the supplied
631 .Vt ENGINE
632 will be needed by OpenSSL after the function has returned.
633 For example, the function to add a new
634 .Vt ENGINE
635 to OpenSSL's internal list is
636 .Fn ENGINE_add .
637 If this function returns success, OpenSSL will have stored a new
638 structural reference internally so the caller is still responsible for
639 freeing their own reference with
640 .Fn ENGINE_free
641 when they are finished with it.
642 In a similar way, some functions will automatically release the
643 structural reference passed to it if part of the function's job is
644 to do so.
645 For example, the
646 .Fn ENGINE_get_next
648 .Fn ENGINE_get_prev
649 functions are used for iterating across the internal
650 .Vt ENGINE
651 list.
652 They
653 will return a new structural reference to the next (or previous)
654 .Vt ENGINE
655 in the list or
656 .Dv NULL
657 if at the end (or beginning) of the list, but in either case the
658 structural reference passed to the function is released on behalf
659 of the caller.
661 To clarify a particular function's handling of references, one should
662 always consult that function's manual page, or failing that the
663 .In openssl/engine.h
664 header file includes some hints.
666 .Em Functional references
668 As mentioned, functional references exist when the cryptographic
669 functionality of an
670 .Vt ENGINE
671 is required to be available.
672 A functional reference can be obtained in one of two ways; from an
673 existing structural reference to the required
674 .Vt ENGINE ,
675 or by asking OpenSSL for the default operational
676 .Vt ENGINE
677 for a given cryptographic purpose.
679 To obtain a functional reference from an existing structural reference,
680 call the
681 .Fn ENGINE_init
682 function.
683 This returns zero if the
684 .Vt ENGINE
685 was not already operational and couldn't be successfully initialised
686 (e.g. lack of system drivers, no special hardware attached),
687 otherwise it will return non-zero to indicate that the
688 .Vt ENGINE
689 is now operational and will have allocated a new
690 .Sy functional
691 reference to the
692 .Vt ENGINE .
693 All functional references are released by calling
694 .Fn ENGINE_finish ,
695 which removes the implicit structural reference as well.
697 The second way to get a functional reference is by asking OpenSSL for a
698 default implementation for a given task, e.g.
700 .Fn ENGINE_get_default_RSA ,
701 .Fn ENGINE_get_default_cipher_engine ,
702 etc.
703 These are discussed in the next section, though they are not usually
704 required by application programmers as they are used automatically when
705 creating and using the relevant algorithm-specific types in OpenSSL,
706 such as RSA, DSA, EVP_CIPHER_CTX, etc.
707 .Ss Default implementations
708 For each supported abstraction, the
709 .Nm engine
710 code maintains an internal table of state to control which
711 implementations are available for a given abstraction and which
712 should be used by default.
713 These implementations are registered in the tables and indexed by an
714 .Fa nid
715 value, because abstractions like
716 .Vt EVP_CIPHER
718 .Vt EVP_DIGEST
719 support many distinct algorithms and modes, and
720 .Vt ENGINE Ns s
721 can support arbitrarily many of them.
722 In the case of other abstractions like RSA, DSA, etc., there is
723 only one "algorithm" so all implementations implicitly register
724 using the same
725 .Fa nid
726 index.
728 When a default
729 .Vt ENGINE
730 is requested for a given abstraction/algorithm/mode, (e.g. when
731 calling
732 .Fn RSA_new_method NULL ) ,
733 a "get_default" call will be made to the
734 .Nm engine
735 subsystem to process the corresponding state table and return
736 a functional reference to an initialised
737 .Vt ENGINE
738 whose implementation should be used.
739 If no
740 .Vt ENGINE
741 should (or can) be used, it will return
742 .Dv NULL
743 and the caller will operate with a
744 .Dv NULL
745 .Vt ENGINE
746 handle.
747 This usually equates to using the conventional software implementation.
748 In the latter case, OpenSSL will from then on behave the way it used to
749 before the
750 .Nm engine
751 API existed.
753 Each state table has a flag to note whether it has processed this
754 "get_default" query since the table was last modified, because to
755 process this question it must iterate across all the registered
756 .Vt ENGINE Ns s
757 in the table trying to initialise each of them in turn, in case one of
758 them is operational.
759 If it returns a functional reference to an
760 .Vt ENGINE ,
761 it will also cache another reference to speed up processing future
762 queries (without needing to iterate across the table).
763 Likewise, it will cache a
764 .Dv NULL
765 response if no
766 .Vt ENGINE
767 was available so that future queries won't repeat the same iteration
768 unless the state table changes.
769 This behaviour can also be changed; if the
770 .Dv ENGINE_TABLE_FLAG_NOINIT
771 flag is set (using
772 .Fn ENGINE_set_table_flags ) ,
773 no attempted initialisations will take place, instead the only way for
774 the state table to return a
775 .Pf non- Dv NULL
776 .Vt ENGINE
777 to the "get_default" query will be if one is expressly set in the table.
778 For example,
779 .Fn ENGINE_set_default_RSA
780 does the same job as
781 .Fn ENGINE_register_RSA
782 except that it also sets the state table's cached response for the
783 "get_default" query.
784 In the case of abstractions like
785 .Vt EVP_CIPHER ,
786 where implementations are indexed by
787 .Fa nid ,
788 these flags and cached-responses are distinct for each
789 .Fa nid
790 value.
791 .Ss Application requirements
792 This section will explain the basic things an application programmer
793 should support to make the most useful elements of the
794 .Nm engine
795 functionality available to the user.
796 The first thing to consider is whether the programmer wishes to make
797 alternative
798 .Vt ENGINE
799 modules available to the application and user.
800 OpenSSL maintains an internal linked list of "visible"
801 .Vt ENGINE Ns s
802 from which it has to operate.
803 At start-up, this list is empty, and in fact if an application does
804 not call any
805 .Nm engine
806 API calls and it uses static
807 linking against openssl, then the resulting application binary will
808 not contain any alternative
809 .Nm engine
810 code at all.
811 So the first consideration is whether any/all available
812 .Vt ENGINE
813 implementations should be made visible to OpenSSL.
814 This is controlled by calling the various "load" functions, e.g.
815 .Fn ENGINE_load_builtin_engines
816 to make all
817 .Vt ENGINE
818 implementations bundled with OpenSSL available.
820 Note that
821 .Fn ENGINE_load_dynamic
822 is a placeholder and does not enable dynamic engine loading support.
824 Having called any of these functions,
825 .Vt ENGINE
826 objects would have been dynamically allocated and populated with
827 these implementations and linked into OpenSSL's internal linked
828 list.
830 If no
831 .Nm engine
832 API functions are called at all in an application, then there are
833 no inherent memory leaks to worry about from the
834 .Nm engine
835 functionality, however if any
836 .Vt ENGINE Ns s
837 are loaded, even if they are never registered or used, it is necessary
838 to use the
839 .Fn ENGINE_cleanup
840 function to correspondingly cleanup before program exit, if the caller
841 wishes to avoid memory leaks.
842 This mechanism uses an internal callback registration table so that any
843 .Nm engine
844 API functionality that knows it requires cleanup can register its
845 cleanup details to be called during
846 .Fn ENGINE_cleanup .
847 This approach allows
848 .Fn ENGINE_cleanup
849 to clean up after any
850 .Nm engine
851 functionality at all that your program uses, yet doesn't automatically
852 create linker dependencies to all possible
853 .Nm engine
854 functionality - only the cleanup callbacks required by the functionality
855 you do use will be required by the linker.
857 The fact that
858 .Vt ENGINE Ns s
859 are made visible to OpenSSL (and thus are linked into the program
860 and loaded into memory at run-time) does not mean they are "registered"
861 or called into use by OpenSSL automatically - that behaviour is
862 something for the application to control.
863 Some applications will want to allow the user to specify exactly which
864 .Vt ENGINE
865 they want used if any is to be used at all.
866 Others may prefer to load all support and have OpenSSL automatically use
867 at run-time any
868 .Vt ENGINE
869 that is able to successfully initialised - i.e. to assume that this
870 corresponds to acceleration hardware attached to the machine or
871 some such thing.
872 There are probably numerous other ways in which applications may prefer
873 to handle things, so we will simply illustrate the consequences as they
874 apply to a couple of simple cases and leave developers to consider these
875 and the source code to openssl's builtin utilities as guides.
877 .Em Using a specific ENGINE implementation
879 Here we'll assume an application has been configured by its user or
880 admin to want to use the "ACME"
881 .Vt ENGINE
882 if it is available in the version of OpenSSL the application was
883 compiled with.
884 If it is available, it should be used by default for all RSA, DSA, and
885 symmetric cipher operations, otherwise OpenSSL should use its builtin
886 software as usual.
887 The following code illustrates how to approach this:
888 .Bd -literal
889 ENGINE *e;
890 const char *engine_id = "ACME";
891 ENGINE_load_builtin_engines();
892 e = ENGINE_by_id(engine_id);
893 if (!e)
894         /* the engine isn't available */
895         return;
896 if (!ENGINE_init(e)) {
897         /* the engine couldn't initialise, release 'e' */
898         ENGINE_free(e);
899         return;
901 if (!ENGINE_set_default_RSA(e))
902         /* This should only happen when 'e' can't initialise, but the previous
903          * statement suggests it did. */
904         abort();
905 ENGINE_set_default_DSA(e);
906 ENGINE_set_default_ciphers(e);
907 /* Release the functional reference from ENGINE_init() */
908 ENGINE_finish(e);
909 /* Release the structural reference from ENGINE_by_id() */
910 ENGINE_free(e);
913 .Em Automatically using builtin ENGINE implementations
915 Here we'll assume we want to load and register all
916 .Vt ENGINE
917 implementations bundled with OpenSSL, such that for any cryptographic
918 algorithm required by OpenSSL - if there is an
919 .Vt ENGINE
920 that implements it and can be initialised, it should be used.
921 The following code illustrates how this can work;
922 .Bd -literal
923 /* Load all bundled ENGINEs into memory and make them visible */
924 ENGINE_load_builtin_engines();
925 /* Register all of them for every algorithm they collectively implement */
926 ENGINE_register_all_complete();
929 That's all that's required.
930 For example, the next time OpenSSL tries to set up an RSA key, any bundled
931 .Vt ENGINE Ns s
932 that implement
933 .Vt RSA_METHOD
934 will be passed to
935 .Fn ENGINE_init
936 and if any of those succeed, that
937 .Vt ENGINE
938 will be set as the default for RSA use from then on.
939 .Ss Advanced configuration support
940 There is a mechanism supported by the
941 .Nm engine
942 framework that allows each
943 .Vt ENGINE
944 implementation to define an arbitrary set of configuration
945 "commands" and expose them to OpenSSL and any applications based on
946 OpenSSL.
947 This mechanism is entirely based on the use of name-value pairs
948 and assumes ASCII input (no unicode or UTF for now!), so it is ideal if
949 applications want to provide a transparent way for users to provide
950 arbitrary configuration "directives" directly to such
951 .Vt ENGINE Ns s .
952 It is also possible for the application to dynamically interrogate the
953 loaded
954 .Vt ENGINE
955 implementations for the names, descriptions, and input flags of
956 their available "control commands", providing a more flexible
957 configuration scheme.
958 However, if the user is expected to know which
959 .Vt ENGINE
960 device he/she is using (in the case of specialised hardware, this
961 goes without saying) then applications may not need to concern
962 themselves with discovering the supported control commands and
963 simply prefer to pass settings into
964 .Vt ENGINE s
965 exactly as they are provided by the user.
967 Before illustrating how control commands work, it is worth mentioning
968 what they are typically used for.
969 Broadly speaking there are two uses for control commands; the first is
970 to provide the necessary details to the implementation (which may know
971 nothing at all specific to the host system) so that it can be
972 initialised for use.
973 This could include the path to any driver or config files it needs to
974 load, required network addresses, smart-card identifiers, passwords to
975 initialise protected devices, logging information, etc.
976 This class of commands typically needs to be passed to an
977 .Vt ENGINE
978 .Sy before
979 attempting to initialise it, i.e. before calling
980 .Fn ENGINE_init .
981 The other class of commands consist of settings or operations that tweak
982 certain behaviour or cause certain operations to take place, and these
983 commands may work either before or after
984 .Fn ENGINE_init ,
985 or in some cases both.
986 .Vt ENGINE
987 implementations should provide indications of this in the descriptions
988 attached to builtin control commands and/or in external product
989 documentation.
991 .Em Issuing control commands to an ENGINE
993 Let's illustrate by example; a function for which the caller supplies
994 the name of the
995 .Vt ENGINE
996 it wishes to use, a table of string-pairs for use before initialisation,
997 and another table for use after initialisation.
998 Note that the string-pairs used for control commands consist of a
999 command "name" followed by the command "parameter" - the parameter
1000 could be
1001 .Dv NULL
1002 in some cases but the name cannot.
1003 This function should initialise the
1004 .Vt ENGINE
1005 (issuing the "pre" commands beforehand and the "post" commands
1006 afterwards) and set it as the default for everything except RAND
1007 and then return a boolean success or failure.
1008 .Bd -literal
1010 generic_load_engine_fn(const char *engine_id,
1011     const char **pre_cmds, int pre_num,
1012     const char **post_cmds, int post_num)
1014         ENGINE *e = ENGINE_by_id(engine_id);
1016         if (!e)
1017                 return 0;
1018         while (pre_num--) {
1019                 if (!ENGINE_ctrl_cmd_string(e,
1020                     pre_cmds[0], pre_cmds[1], 0)) {
1021                         fprintf(stderr,
1022                             "Failed command (%s - %s:%s)\en",
1023                             engine_id, pre_cmds[0],
1024                             pre_cmds[1] ? pre_cmds[1] : "(NULL)");
1025                         ENGINE_free(e);
1026                         return 0;
1027                 }
1028                 pre_cmds += 2;
1029         }
1030         if (!ENGINE_init(e)) {
1031                 fprintf(stderr, "Failed initialisation\en");
1032                 ENGINE_free(e);
1033                 return 0;
1034         }
1035         /*
1036          * ENGINE_init() returned a functional reference,
1037          * so free the structural reference from
1038          * ENGINE_by_id().
1039          */
1040         ENGINE_free(e);
1041         while (post_num--) {
1042                 if (!ENGINE_ctrl_cmd_string(e,
1043                     post_cmds[0], post_cmds[1], 0)) {
1044                         fprintf(stderr,
1045                             "Failed command (%s - %s:%s)\en",
1046                             engine_id, post_cmds[0],
1047                             post_cmds[1] ? post_cmds[1] : "(NULL)");
1048                         ENGINE_finish(e);
1049                         return 0;
1050                 }
1051                 post_cmds += 2;
1052         }
1053         ENGINE_set_default(e, ENGINE_METHOD_ALL & ~ENGINE_METHOD_RAND);
1054         /* Success */
1055         return 1;
1059 Note that
1060 .Fn ENGINE_ctrl_cmd_string
1061 accepts a boolean argument that can relax the semantics of the function.
1062 If set to non-zero it will only return failure if the
1063 .Vt ENGINE
1064 supported the given command name but failed while executing it, if the
1065 .Vt ENGINE
1066 doesn't support the command name it will simply return success without
1067 doing anything.
1068 In this case we assume the user is only supplying commands specific to
1069 the given
1070 .Vt ENGINE
1071 so we set this to FALSE.
1073 .Em Discovering supported control commands
1075 It is possible to discover at run-time the names, numerical-ids,
1076 descriptions and input parameters of the control commands supported by an
1077 .Vt ENGINE
1078 using a structural reference.
1079 Note that some control commands are defined by OpenSSL itself and it
1080 will intercept and handle these control commands on behalf of the
1081 .Vt ENGINE ,
1082 i.e. the
1083 .Vt ENGINE Ap s
1084 ctrl() handler is not used for the control command.
1085 .In openssl/engine.h
1086 defines an index,
1087 .Dv ENGINE_CMD_BASE ,
1088 that all control commands implemented by
1089 .Vt ENGINE Ns s
1090 should be numbered from.
1091 Any command value lower than this symbol is considered a "generic"
1092 command is handled directly by the OpenSSL core routines.
1094 It is using these "core" control commands that one can discover the
1095 control commands implemented by a given
1096 .Vt ENGINE ,
1097 specifically the commands:
1098 .Bd -literal
1099 #define ENGINE_HAS_CTRL_FUNCTION                10
1100 #define ENGINE_CTRL_GET_FIRST_CMD_TYPE          11
1101 #define ENGINE_CTRL_GET_NEXT_CMD_TYPE           12
1102 #define ENGINE_CTRL_GET_CMD_FROM_NAME           13
1103 #define ENGINE_CTRL_GET_NAME_LEN_FROM_CMD       14
1104 #define ENGINE_CTRL_GET_NAME_FROM_CMD           15
1105 #define ENGINE_CTRL_GET_DESC_LEN_FROM_CMD       16
1106 #define ENGINE_CTRL_GET_DESC_FROM_CMD           17
1107 #define ENGINE_CTRL_GET_CMD_FLAGS               18
1110 Whilst these commands are automatically processed by the OpenSSL
1111 framework code, they use various properties exposed by each
1112 .Vt ENGINE
1113 to process these queries.
1115 .Vt ENGINE
1116 has 3 properties it exposes that can affect how this behaves;
1117 it can supply a ctrl() handler, it can specify
1118 .Dv ENGINE_FLAGS_MANUAL_CMD_CTRL
1119 in the
1120 .Vt ENGINE Ap s
1121 flags, and it can expose an array of control command descriptions.
1122 If an
1123 .Vt ENGINE
1124 specifies the
1125 .Dv ENGINE_FLAGS_MANUAL_CMD_CTRL
1126 flag, then it will simply pass all these "core" control commands
1127 directly to the
1128 .Vt ENGINE Ap s
1129 ctrl() handler (and thus, it must have supplied one), so it is up
1130 to the
1131 .Vt ENGINE
1132 to reply to these "discovery" commands itself.
1133 If that flag is not set, then the OpenSSL framework code will work with
1134 the following rules;
1135 .Bl -tag -width Ds
1136 .It If no ctrl() handler is supplied:
1137 .Dv ENGINE_HAS_CTRL_FUNCTION
1138 returns FALSE (zero), all other commands fail.
1139 .It If a ctrl() handler was supplied but no array of control commands:
1140 .Dv ENGINE_HAS_CTRL_FUNCTION
1141 returns TRUE, all other commands fail.
1142 .It If a ctrl() handler and array of control commands was supplied:
1143 .Dv ENGINE_HAS_CTRL_FUNCTION
1144 returns TRUE, all other commands proceed processing...
1147 If the
1148 .Vt ENGINE Ns s
1149 array of control commands is empty, then all other commands will fail.
1150 Otherwise
1151 .Dv ENGINE_CTRL_GET_FIRST_CMD_TYPE
1152 returns the identifier of the first command supported by the
1153 .Vt ENGINE ,
1154 .Dv ENGINE_GET_NEXT_CMD_TYPE
1155 takes the identifier of a command supported by the
1156 .Vt ENGINE
1157 and returns the next command identifier or fails if there are no more,
1158 .Dv ENGINE_CMD_FROM_NAME
1159 takes a string name for a command and returns the corresponding
1160 identifier or fails if no such command name exists, and the remaining
1161 commands take a command identifier and return properties of the
1162 corresponding commands.
1163 All except
1164 .Dv ENGINE_CTRL_GET_FLAGS
1165 return the string length of a command name or description, or
1166 populate a supplied character buffer with a copy of the command
1167 name or description.
1168 .Dv ENGINE_CTRL_GET_FLAGS
1169 returns a bitwise-OR'd mask of the following possible values:
1170 .Bd -literal
1171 #define ENGINE_CMD_FLAG_NUMERIC         (unsigned int)0x0001
1172 #define ENGINE_CMD_FLAG_STRING          (unsigned int)0x0002
1173 #define ENGINE_CMD_FLAG_NO_INPUT        (unsigned int)0x0004
1174 #define ENGINE_CMD_FLAG_INTERNAL        (unsigned int)0x0008
1177 If the
1178 .Dv ENGINE_CMD_FLAG_INTERNAL
1179 flag is set, then any other flags are purely informational to the caller.
1180 This flag will prevent the command being usable for any higher-level
1181 .Vt ENGINE
1182 functions such as
1183 .Fn ENGINE_ctrl_cmd_string .
1184 "INTERNAL" commands are not intended to be exposed to text-based
1185 configuration by applications, administrations, users, etc.
1186 These can support arbitrary operations via
1187 .Fn ENGINE_ctrl ,
1188 including passing to and/or from the control commands data of any
1189 arbitrary type.
1190 These commands are supported in the discovery mechanisms simply allow
1191 applications to determine if an
1192 .Vt ENGINE
1193 supports certain specific commands it might want to use (e.g.
1194 application "foo" might query various
1195 .Vt ENGINE Ns s
1196 to see if they implement "FOO_GET_VENDOR_LOGO_GIF" - and
1197 .Vt ENGINE
1198 could therefore decide whether or not to support this "foo"-specific
1199 extension).
1200 .Sh SEE ALSO
1201 .Xr DH_new 3 ,
1202 .Xr DSA_new 3 ,
1203 .Xr ENGINE_add_conf_module 3 ,
1204 .Xr ENGINE_set_ex_data 3 ,
1205 .Xr RSA_new 3