1 .\" $OpenBSD: engine.3,v 1.13 2018/03/22 21:08:22 schwarze Exp $
2 .\" full merge up to: OpenSSL crypto/engine e6390aca Jul 21 10:06:03 2015 -0400
3 .\" selective merge up to: man3/ENGINE_add 1f13ad31 Dec 25 17:50:39 2017 +0800
5 .\" This file was written by Geoff Thorpe <geoff@openssl.org>
6 .\" with contributions from Paul Yang <yang.yang@baishancloud.com>.
7 .\" Copyright (c) 2002, 2004, 2007, 2015, 2017 The OpenSSL Project.
8 .\" All rights reserved.
10 .\" Redistribution and use in source and binary forms, with or without
11 .\" modification, are permitted provided that the following conditions
14 .\" 1. Redistributions of source code must retain the above copyright
15 .\" notice, this list of conditions and the following disclaimer.
17 .\" 2. Redistributions in binary form must reproduce the above copyright
18 .\" notice, this list of conditions and the following disclaimer in
19 .\" the documentation and/or other materials provided with the
22 .\" 3. All advertising materials mentioning features or use of this
23 .\" software must display the following acknowledgment:
24 .\" "This product includes software developed by the OpenSSL Project
25 .\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
27 .\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28 .\" endorse or promote products derived from this software without
29 .\" prior written permission. For written permission, please contact
30 .\" openssl-core@openssl.org.
32 .\" 5. Products derived from this software may not be called "OpenSSL"
33 .\" nor may "OpenSSL" appear in their names without prior written
34 .\" permission of the OpenSSL Project.
36 .\" 6. Redistributions of any form whatsoever must retain the following
38 .\" "This product includes software developed by the OpenSSL Project
39 .\" for use in the OpenSSL Toolkit (http://www.openssl.org/)"
41 .\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42 .\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
45 .\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48 .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50 .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52 .\" OF THE POSSIBILITY OF SUCH DAMAGE.
54 .Dd $Mdocdate: March 22 2018 $
58 .Nm ENGINE_get_first ,
67 .Nm ENGINE_load_openssl ,
68 .Nm ENGINE_load_dynamic ,
69 .Nm ENGINE_load_cryptodev ,
70 .Nm ENGINE_load_builtin_engines ,
72 .Nm ENGINE_get_default_RSA ,
73 .Nm ENGINE_get_default_DSA ,
74 .Nm ENGINE_get_default_ECDH ,
75 .Nm ENGINE_get_default_ECDSA ,
76 .Nm ENGINE_get_default_DH ,
77 .Nm ENGINE_get_default_RAND ,
78 .Nm ENGINE_get_cipher_engine ,
79 .Nm ENGINE_get_digest_engine ,
80 .Nm ENGINE_set_default_RSA ,
81 .Nm ENGINE_set_default_DSA ,
82 .Nm ENGINE_set_default_ECDH ,
83 .Nm ENGINE_set_default_ECDSA ,
84 .Nm ENGINE_set_default_DH ,
85 .Nm ENGINE_set_default_RAND ,
86 .Nm ENGINE_set_default_ciphers ,
87 .Nm ENGINE_set_default_digests ,
88 .Nm ENGINE_set_default_string ,
89 .Nm ENGINE_set_default ,
90 .Nm ENGINE_get_table_flags ,
91 .Nm ENGINE_set_table_flags ,
92 .Nm ENGINE_register_RSA ,
93 .Nm ENGINE_unregister_RSA ,
94 .Nm ENGINE_register_all_RSA ,
95 .Nm ENGINE_register_DSA ,
96 .Nm ENGINE_unregister_DSA ,
97 .Nm ENGINE_register_all_DSA ,
98 .Nm ENGINE_register_ECDH ,
99 .Nm ENGINE_unregister_ECDH ,
100 .Nm ENGINE_register_all_ECDH ,
101 .Nm ENGINE_register_ECDSA ,
102 .Nm ENGINE_unregister_ECDSA ,
103 .Nm ENGINE_register_all_ECDSA ,
104 .Nm ENGINE_register_DH ,
105 .Nm ENGINE_unregister_DH ,
106 .Nm ENGINE_register_all_DH ,
107 .Nm ENGINE_register_RAND ,
108 .Nm ENGINE_unregister_RAND ,
109 .Nm ENGINE_register_all_RAND ,
110 .Nm ENGINE_register_STORE ,
111 .Nm ENGINE_unregister_STORE ,
112 .Nm ENGINE_register_all_STORE ,
113 .Nm ENGINE_register_ciphers ,
114 .Nm ENGINE_unregister_ciphers ,
115 .Nm ENGINE_register_all_ciphers ,
116 .Nm ENGINE_register_digests ,
117 .Nm ENGINE_unregister_digests ,
118 .Nm ENGINE_register_all_digests ,
119 .Nm ENGINE_register_complete ,
120 .Nm ENGINE_register_all_complete ,
122 .Nm ENGINE_cmd_is_executable ,
123 .Nm ENGINE_ctrl_cmd ,
124 .Nm ENGINE_ctrl_cmd_string ,
129 .Nm ENGINE_set_name ,
132 .Nm ENGINE_set_ECDH ,
133 .Nm ENGINE_set_ECDSA ,
135 .Nm ENGINE_set_RAND ,
136 .Nm ENGINE_set_STORE ,
137 .Nm ENGINE_set_destroy_function ,
138 .Nm ENGINE_set_init_function ,
139 .Nm ENGINE_set_finish_function ,
140 .Nm ENGINE_set_ctrl_function ,
141 .Nm ENGINE_set_load_privkey_function ,
142 .Nm ENGINE_set_load_pubkey_function ,
143 .Nm ENGINE_set_ciphers ,
144 .Nm ENGINE_set_digests ,
145 .Nm ENGINE_set_flags ,
146 .Nm ENGINE_set_cmd_defns ,
148 .Nm ENGINE_get_name ,
151 .Nm ENGINE_get_ECDH ,
152 .Nm ENGINE_get_ECDSA ,
154 .Nm ENGINE_get_RAND ,
155 .Nm ENGINE_get_STORE ,
156 .Nm ENGINE_get_destroy_function ,
157 .Nm ENGINE_get_init_function ,
158 .Nm ENGINE_get_finish_function ,
159 .Nm ENGINE_get_ctrl_function ,
160 .Nm ENGINE_get_load_privkey_function ,
161 .Nm ENGINE_get_load_pubkey_function ,
162 .Nm ENGINE_get_ciphers ,
163 .Nm ENGINE_get_digests ,
164 .Nm ENGINE_get_cipher ,
165 .Nm ENGINE_get_digest ,
166 .Nm ENGINE_get_flags ,
167 .Nm ENGINE_get_cmd_defns ,
168 .Nm ENGINE_load_private_key ,
169 .Nm ENGINE_load_public_key
170 .Nd ENGINE cryptographic module support
174 .Fn ENGINE_get_first void
176 .Fn ENGINE_get_last void
206 .Fn ENGINE_load_openssl void
208 .Fn ENGINE_load_dynamic void
210 .Fn ENGINE_load_cryptodev void
212 .Fn ENGINE_load_builtin_engines void
214 .Fn ENGINE_cleanup void
216 .Fn ENGINE_get_default_RSA void
218 .Fn ENGINE_get_default_DSA void
220 .Fn ENGINE_get_default_ECDH void
222 .Fn ENGINE_get_default_ECDSA void
224 .Fn ENGINE_get_default_DH void
226 .Fn ENGINE_get_default_RAND void
228 .Fo ENGINE_get_cipher_engine
232 .Fo ENGINE_get_digest_engine
236 .Fo ENGINE_set_default_RSA
240 .Fo ENGINE_set_default_DSA
244 .Fo ENGINE_set_default_ECDH
248 .Fo ENGINE_set_default_ECDSA
252 .Fo ENGINE_set_default_DH
256 .Fo ENGINE_set_default_RAND
260 .Fo ENGINE_set_default_ciphers
264 .Fo ENGINE_set_default_digests
268 .Fo ENGINE_set_default_string
270 .Fa "const char *list"
273 .Fo ENGINE_set_default
275 .Fa "unsigned int flags"
278 .Fn ENGINE_get_table_flags void
280 .Fo ENGINE_set_table_flags
281 .Fa "unsigned int flags"
284 .Fo ENGINE_register_RSA
288 .Fo ENGINE_unregister_RSA
292 .Fn ENGINE_register_all_RSA void
294 .Fo ENGINE_register_DSA
298 .Fo ENGINE_unregister_DSA
302 .Fn ENGINE_register_all_DSA void
304 .Fo ENGINE_register_ECDH
308 .Fo ENGINE_unregister_ECDH
312 .Fn ENGINE_register_all_ECDH void
314 .Fo ENGINE_register_ECDSA
318 .Fo ENGINE_unregister_ECDSA
322 .Fn ENGINE_register_all_ECDSA void
324 .Fo ENGINE_register_DH
328 .Fo ENGINE_unregister_DH
332 .Fn ENGINE_register_all_DH void
334 .Fo ENGINE_register_RAND
338 .Fo ENGINE_unregister_RAND
342 .Fn ENGINE_register_all_RAND void
344 .Fo ENGINE_register_STORE
348 .Fo ENGINE_unregister_STORE
352 .Fn ENGINE_register_all_STORE void
354 .Fo ENGINE_register_ciphers
358 .Fo ENGINE_unregister_ciphers
362 .Fn ENGINE_register_all_ciphers void
364 .Fo ENGINE_register_digests
368 .Fo ENGINE_unregister_digests
372 .Fn ENGINE_register_all_digests void
374 .Fo ENGINE_register_complete
378 .Fn ENGINE_register_all_complete void
385 .Fa "void (*f)(void)"
388 .Fo ENGINE_cmd_is_executable
395 .Fa "const char *cmd_name"
398 .Fa "void (*f)(void)"
399 .Fa "int cmd_optional"
402 .Fo ENGINE_ctrl_cmd_string
404 .Fa "const char *cmd_name"
405 .Fa "const char *arg"
406 .Fa "int cmd_optional"
426 .Fa "const char *name"
431 .Fa "const RSA_METHOD *rsa_meth"
436 .Fa "const DSA_METHOD *dsa_meth"
441 .Fa "const ECDH_METHOD *dh_meth"
446 .Fa "const ECDSA_METHOD *dh_meth"
451 .Fa "const DH_METHOD *dh_meth"
456 .Fa "const RAND_METHOD *rand_meth"
461 .Fa "const STORE_METHOD *rand_meth"
464 .Fo ENGINE_set_destroy_function
466 .Fa "ENGINE_GEN_INT_FUNC_PTR destroy_f"
469 .Fo ENGINE_set_init_function
471 .Fa "ENGINE_GEN_INT_FUNC_PTR init_f"
474 .Fo ENGINE_set_finish_function
476 .Fa "ENGINE_GEN_INT_FUNC_PTR finish_f"
479 .Fo ENGINE_set_ctrl_function
481 .Fa "ENGINE_CTRL_FUNC_PTR ctrl_f"
484 .Fo ENGINE_set_load_privkey_function
486 .Fa "ENGINE_LOAD_KEY_PTR loadpriv_f"
489 .Fo ENGINE_set_load_pubkey_function
491 .Fa "ENGINE_LOAD_KEY_PTR loadpub_f"
494 .Fo ENGINE_set_ciphers
496 .Fa "ENGINE_CIPHERS_PTR f"
499 .Fo ENGINE_set_digests
501 .Fa "ENGINE_DIGESTS_PTR f"
509 .Fo ENGINE_set_cmd_defns
511 .Fa "const ENGINE_CMD_DEFN *defns"
515 .Fa "const ENGINE *e"
519 .Fa "const ENGINE *e"
521 .Ft const RSA_METHOD *
523 .Fa "const ENGINE *e"
525 .Ft const DSA_METHOD *
527 .Fa "const ENGINE *e"
529 .Ft const ECDH_METHOD *
531 .Fa "const ENGINE *e"
533 .Ft const ECDSA_METHOD *
535 .Fa "const ENGINE *e"
537 .Ft const DH_METHOD *
539 .Fa "const ENGINE *e"
541 .Ft const RAND_METHOD *
543 .Fa "const ENGINE *e"
545 .Ft const STORE_METHOD *
547 .Fa "const ENGINE *e"
549 .Ft ENGINE_GEN_INT_FUNC_PTR
550 .Fo ENGINE_get_destroy_function
551 .Fa "const ENGINE *e"
553 .Ft ENGINE_GEN_INT_FUNC_PTR
554 .Fo ENGINE_get_init_function
555 .Fa "const ENGINE *e"
557 .Ft ENGINE_GEN_INT_FUNC_PTR
558 .Fo ENGINE_get_finish_function
559 .Fa "const ENGINE *e"
561 .Ft ENGINE_CTRL_FUNC_PTR
562 .Fo ENGINE_get_ctrl_function
563 .Fa "const ENGINE *e"
565 .Ft ENGINE_LOAD_KEY_PTR
566 .Fo ENGINE_get_load_privkey_function
567 .Fa "const ENGINE *e"
569 .Ft ENGINE_LOAD_KEY_PTR
570 .Fo ENGINE_get_load_pubkey_function
571 .Fa "const ENGINE *e"
573 .Ft ENGINE_CIPHERS_PTR
574 .Fo ENGINE_get_ciphers
575 .Fa "const ENGINE *e"
577 .Ft ENGINE_DIGESTS_PTR
578 .Fo ENGINE_get_digests
579 .Fa "const ENGINE *e"
581 .Ft const EVP_CIPHER *
582 .Fo ENGINE_get_cipher
587 .Fo ENGINE_get_digest
593 .Fa "const ENGINE *e"
595 .Ft const ENGINE_CMD_DEFN *
596 .Fo ENGINE_get_cmd_defns
597 .Fa "const ENGINE *e"
600 .Fo ENGINE_load_private_key
602 .Fa "const char *key_id"
603 .Fa "UI_METHOD *ui_method"
604 .Fa "void *callback_data"
607 .Fo ENGINE_load_public_key
609 .Fa "const char *key_id"
610 .Fa "UI_METHOD *ui_method"
611 .Fa "void *callback_data"
614 These functions create, manipulate, and use cryptographic modules
618 These objects act as containers for implementations of cryptographic
619 algorithms, and support a reference-counted mechanism to allow them to
620 be dynamically loaded in and out of the running application.
622 The cryptographic functionality that can be provided by an
624 implementation includes the following abstractions:
629 for providing alternative RSA implementations
631 .Vt DSA_METHOD , DH_METHOD , RAND_METHOD , ECDH_METHOD ,
632 .Vt ECDSA_METHOD , STORE_METHOD :
633 similarly for other OpenSSL APIs
636 potentially multiple cipher algorithms (indexed by 'nid')
639 potentially multiple hash algorithms (indexed by 'nid')
641 key-loading: loading public and/or private EVP_PKEY keys
643 .Ss Reference counting and handles
644 Due to the modular nature of the
648 need to be treated as handles - i.e. not only as pointers, but also
649 as references to the underlying
652 One should obtain a new reference when making copies of an
654 pointer if the copies will be used (and released) independently.
657 objects have two levels of reference-counting to match the way in
658 which the objects are used.
659 At the most basic level, each
661 pointer is inherently a
663 reference - a structural reference is required to use the pointer value
664 at all, as this kind of reference is a guarantee that the structure cannot
665 be deallocated until the reference is released.
667 However, a structural reference provides no guarantee that the
669 is initialised and able to use any of its cryptographic implementations.
670 Indeed it's quite possible that most
672 will not initialise at all in typical environments, as
674 are typically used to support specialised hardware.
677 functionality, you need a
680 This kind of reference can be considered a specialised form of
681 structural reference, because each functional reference implicitly
682 contains a structural reference as well - however to avoid
683 difficult-to-find programming bugs, it is recommended to treat the two
684 kinds of reference independently.
685 If you have a functional reference to an
687 you have a guarantee that the
689 has been initialised and is ready to perform cryptographic operations and
690 will remain uninitialised until after you have released your
693 .Em Structural references
695 This basic type of reference is used for instantiating new
697 iterating across OpenSSL's internal linked-list of loaded
699 reading information about an
702 Essentially a structural reference is sufficient if you only need to
703 query or manipulate the data of an
705 implementation rather than use its functionality.
709 function returns a structural reference to a new (empty)
714 API functions that return structural references such as
716 .Fn ENGINE_get_first ,
717 .Fn ENGINE_get_last ,
718 .Fn ENGINE_get_next ,
720 .Fn ENGINE_get_prev .
721 All structural references should be released by a corresponding call
727 object itself will only actually be cleaned up and deallocated when
728 the last structural reference is released.
730 It should also be noted that many
732 API function calls that accept a structural reference will internally
733 obtain another reference.
734 Typically this happens whenever the supplied
736 will be needed by OpenSSL after the function has returned.
737 For example, the function to add a new
739 to OpenSSL's internal list is
741 If this function returns success, OpenSSL will have stored a new
742 structural reference internally so the caller is still responsible for
743 freeing their own reference with
745 when they are finished with it.
746 In a similar way, some functions will automatically release the
747 structural reference passed to it if part of the function's job is
753 functions are used for iterating across the internal
757 will return a new structural reference to the next (or previous)
761 if at the end (or beginning) of the list, but in either case the
762 structural reference passed to the function is released on behalf
765 To clarify a particular function's handling of references, one should
766 always consult that function's manual page, or failing that the
768 header file includes some hints.
770 .Em Functional references
772 As mentioned, functional references exist when the cryptographic
775 is required to be available.
776 A functional reference can be obtained in one of two ways; from an
777 existing structural reference to the required
779 or by asking OpenSSL for the default operational
781 for a given cryptographic purpose.
783 To obtain a functional reference from an existing structural reference,
787 This returns zero if the
789 was not already operational and couldn't be successfully initialised
790 (e.g. lack of system drivers, no special hardware attached),
791 otherwise it will return non-zero to indicate that the
793 is now operational and will have allocated a new
797 All functional references are released by calling
799 which removes the implicit structural reference as well.
801 The second way to get a functional reference is by asking OpenSSL for a
802 default implementation for a given task, e.g.
804 .Fn ENGINE_get_default_RSA ,
805 .Fn ENGINE_get_default_cipher_engine ,
807 These are discussed in the next section, though they are not usually
808 required by application programmers as they are used automatically when
809 creating and using the relevant algorithm-specific types in OpenSSL,
810 such as RSA, DSA, EVP_CIPHER_CTX, etc.
811 .Ss Default implementations
812 For each supported abstraction, the
814 code maintains an internal table of state to control which
815 implementations are available for a given abstraction and which
816 should be used by default.
817 These implementations are registered in the tables and indexed by an
819 value, because abstractions like
823 support many distinct algorithms and modes, and
825 can support arbitrarily many of them.
826 In the case of other abstractions like RSA, DSA, etc., there is
827 only one "algorithm" so all implementations implicitly register
834 is requested for a given abstraction/algorithm/mode, (e.g. when
836 .Fn RSA_new_method NULL ) ,
837 a "get_default" call will be made to the
839 subsystem to process the corresponding state table and return
840 a functional reference to an initialised
842 whose implementation should be used.
845 should (or can) be used, it will return
847 and the caller will operate with a
851 This usually equates to using the conventional software implementation.
852 In the latter case, OpenSSL will from then on behave the way it used to
857 Each state table has a flag to note whether it has processed this
858 "get_default" query since the table was last modified, because to
859 process this question it must iterate across all the registered
861 in the table trying to initialise each of them in turn, in case one of
863 If it returns a functional reference to an
865 it will also cache another reference to speed up processing future
866 queries (without needing to iterate across the table).
867 Likewise, it will cache a
871 was available so that future queries won't repeat the same iteration
872 unless the state table changes.
873 This behaviour can also be changed; if the
874 .Dv ENGINE_TABLE_FLAG_NOINIT
876 .Fn ENGINE_set_table_flags ) ,
877 no attempted initialisations will take place, instead the only way for
878 the state table to return a
881 to the "get_default" query will be if one is expressly set in the table.
883 .Fn ENGINE_set_default_RSA
885 .Fn ENGINE_register_RSA
886 except that it also sets the state table's cached response for the
888 In the case of abstractions like
890 where implementations are indexed by
892 these flags and cached-responses are distinct for each
895 .Ss Application requirements
896 This section will explain the basic things an application programmer
897 should support to make the most useful elements of the
899 functionality available to the user.
900 The first thing to consider is whether the programmer wishes to make
903 modules available to the application and user.
904 OpenSSL maintains an internal linked list of "visible"
906 from which it has to operate.
907 At start-up, this list is empty, and in fact if an application does
910 API calls and it uses static
911 linking against openssl, then the resulting application binary will
912 not contain any alternative
915 So the first consideration is whether any/all available
917 implementations should be made visible to OpenSSL.
918 This is controlled by calling the various "load" functions, e.g.
919 .Fn ENGINE_load_builtin_engines
922 implementations bundled with OpenSSL available.
925 .Fn ENGINE_load_dynamic
926 is a placeholder and does not enable dynamic engine loading support.
928 Having called any of these functions,
930 objects would have been dynamically allocated and populated with
931 these implementations and linked into OpenSSL's internal linked
936 API functions are called at all in an application, then there are
937 no inherent memory leaks to worry about from the
939 functionality, however if any
941 are loaded, even if they are never registered or used, it is necessary
944 function to correspondingly cleanup before program exit, if the caller
945 wishes to avoid memory leaks.
946 This mechanism uses an internal callback registration table so that any
948 API functionality that knows it requires cleanup can register its
949 cleanup details to be called during
953 to clean up after any
955 functionality at all that your program uses, yet doesn't automatically
956 create linker dependencies to all possible
958 functionality - only the cleanup callbacks required by the functionality
959 you do use will be required by the linker.
963 are made visible to OpenSSL (and thus are linked into the program
964 and loaded into memory at run-time) does not mean they are "registered"
965 or called into use by OpenSSL automatically - that behaviour is
966 something for the application to control.
967 Some applications will want to allow the user to specify exactly which
969 they want used if any is to be used at all.
970 Others may prefer to load all support and have OpenSSL automatically use
973 that is able to successfully initialised - i.e. to assume that this
974 corresponds to acceleration hardware attached to the machine or
976 There are probably numerous other ways in which applications may prefer
977 to handle things, so we will simply illustrate the consequences as they
978 apply to a couple of simple cases and leave developers to consider these
979 and the source code to openssl's builtin utilities as guides.
981 .Em Using a specific ENGINE implementation
983 Here we'll assume an application has been configured by its user or
984 admin to want to use the "ACME"
986 if it is available in the version of OpenSSL the application was
988 If it is available, it should be used by default for all RSA, DSA, and
989 symmetric cipher operations, otherwise OpenSSL should use its builtin
991 The following code illustrates how to approach this:
994 const char *engine_id = "ACME";
995 ENGINE_load_builtin_engines();
996 e = ENGINE_by_id(engine_id);
998 /* the engine isn't available */
1000 if (!ENGINE_init(e)) {
1001 /* the engine couldn't initialise, release 'e' */
1005 if (!ENGINE_set_default_RSA(e))
1006 /* This should only happen when 'e' can't initialise, but the previous
1007 * statement suggests it did. */
1009 ENGINE_set_default_DSA(e);
1010 ENGINE_set_default_ciphers(e);
1011 /* Release the functional reference from ENGINE_init() */
1013 /* Release the structural reference from ENGINE_by_id() */
1017 .Em Automatically using builtin ENGINE implementations
1019 Here we'll assume we want to load and register all
1021 implementations bundled with OpenSSL, such that for any cryptographic
1022 algorithm required by OpenSSL - if there is an
1024 that implements it and can be initialised, it should be used.
1025 The following code illustrates how this can work;
1027 /* Load all bundled ENGINEs into memory and make them visible */
1028 ENGINE_load_builtin_engines();
1029 /* Register all of them for every algorithm they collectively implement */
1030 ENGINE_register_all_complete();
1033 That's all that's required.
1034 For example, the next time OpenSSL tries to set up an RSA key, any bundled
1040 and if any of those succeed, that
1042 will be set as the default for RSA use from then on.
1043 .Ss Advanced configuration support
1044 There is a mechanism supported by the
1046 framework that allows each
1048 implementation to define an arbitrary set of configuration
1049 "commands" and expose them to OpenSSL and any applications based on
1051 This mechanism is entirely based on the use of name-value pairs
1052 and assumes ASCII input (no unicode or UTF for now!), so it is ideal if
1053 applications want to provide a transparent way for users to provide
1054 arbitrary configuration "directives" directly to such
1056 It is also possible for the application to dynamically interrogate the
1059 implementations for the names, descriptions, and input flags of
1060 their available "control commands", providing a more flexible
1061 configuration scheme.
1062 However, if the user is expected to know which
1064 device he/she is using (in the case of specialised hardware, this
1065 goes without saying) then applications may not need to concern
1066 themselves with discovering the supported control commands and
1067 simply prefer to pass settings into
1069 exactly as they are provided by the user.
1071 Before illustrating how control commands work, it is worth mentioning
1072 what they are typically used for.
1073 Broadly speaking there are two uses for control commands; the first is
1074 to provide the necessary details to the implementation (which may know
1075 nothing at all specific to the host system) so that it can be
1076 initialised for use.
1077 This could include the path to any driver or config files it needs to
1078 load, required network addresses, smart-card identifiers, passwords to
1079 initialise protected devices, logging information, etc.
1080 This class of commands typically needs to be passed to an
1083 attempting to initialise it, i.e. before calling
1085 The other class of commands consist of settings or operations that tweak
1086 certain behaviour or cause certain operations to take place, and these
1087 commands may work either before or after
1089 or in some cases both.
1091 implementations should provide indications of this in the descriptions
1092 attached to builtin control commands and/or in external product
1095 .Em Issuing control commands to an ENGINE
1097 Let's illustrate by example; a function for which the caller supplies
1100 it wishes to use, a table of string-pairs for use before initialisation,
1101 and another table for use after initialisation.
1102 Note that the string-pairs used for control commands consist of a
1103 command "name" followed by the command "parameter" - the parameter
1106 in some cases but the name cannot.
1107 This function should initialise the
1109 (issuing the "pre" commands beforehand and the "post" commands
1110 afterwards) and set it as the default for everything except RAND
1111 and then return a boolean success or failure.
1114 generic_load_engine_fn(const char *engine_id,
1115 const char **pre_cmds, int pre_num,
1116 const char **post_cmds, int post_num)
1118 ENGINE *e = ENGINE_by_id(engine_id);
1123 if (!ENGINE_ctrl_cmd_string(e,
1124 pre_cmds[0], pre_cmds[1], 0)) {
1126 "Failed command (%s - %s:%s)\en",
1127 engine_id, pre_cmds[0],
1128 pre_cmds[1] ? pre_cmds[1] : "(NULL)");
1134 if (!ENGINE_init(e)) {
1135 fprintf(stderr, "Failed initialisation\en");
1140 * ENGINE_init() returned a functional reference,
1141 * so free the structural reference from
1145 while (post_num--) {
1146 if (!ENGINE_ctrl_cmd_string(e,
1147 post_cmds[0], post_cmds[1], 0)) {
1149 "Failed command (%s - %s:%s)\en",
1150 engine_id, post_cmds[0],
1151 post_cmds[1] ? post_cmds[1] : "(NULL)");
1157 ENGINE_set_default(e, ENGINE_METHOD_ALL & ~ENGINE_METHOD_RAND);
1164 .Fn ENGINE_ctrl_cmd_string
1165 accepts a boolean argument that can relax the semantics of the function.
1166 If set to non-zero it will only return failure if the
1168 supported the given command name but failed while executing it, if the
1170 doesn't support the command name it will simply return success without
1172 In this case we assume the user is only supplying commands specific to
1175 so we set this to FALSE.
1177 .Em Discovering supported control commands
1179 It is possible to discover at run-time the names, numerical-ids,
1180 descriptions and input parameters of the control commands supported by an
1182 using a structural reference.
1183 Note that some control commands are defined by OpenSSL itself and it
1184 will intercept and handle these control commands on behalf of the
1188 ctrl() handler is not used for the control command.
1189 .In openssl/engine.h
1191 .Dv ENGINE_CMD_BASE ,
1192 that all control commands implemented by
1194 should be numbered from.
1195 Any command value lower than this symbol is considered a "generic"
1196 command is handled directly by the OpenSSL core routines.
1198 It is using these "core" control commands that one can discover the
1199 control commands implemented by a given
1201 specifically the commands:
1203 #define ENGINE_HAS_CTRL_FUNCTION 10
1204 #define ENGINE_CTRL_GET_FIRST_CMD_TYPE 11
1205 #define ENGINE_CTRL_GET_NEXT_CMD_TYPE 12
1206 #define ENGINE_CTRL_GET_CMD_FROM_NAME 13
1207 #define ENGINE_CTRL_GET_NAME_LEN_FROM_CMD 14
1208 #define ENGINE_CTRL_GET_NAME_FROM_CMD 15
1209 #define ENGINE_CTRL_GET_DESC_LEN_FROM_CMD 16
1210 #define ENGINE_CTRL_GET_DESC_FROM_CMD 17
1211 #define ENGINE_CTRL_GET_CMD_FLAGS 18
1214 Whilst these commands are automatically processed by the OpenSSL
1215 framework code, they use various properties exposed by each
1217 to process these queries.
1220 has 3 properties it exposes that can affect how this behaves;
1221 it can supply a ctrl() handler, it can specify
1222 .Dv ENGINE_FLAGS_MANUAL_CMD_CTRL
1225 flags, and it can expose an array of control command descriptions.
1229 .Dv ENGINE_FLAGS_MANUAL_CMD_CTRL
1230 flag, then it will simply pass all these "core" control commands
1233 ctrl() handler (and thus, it must have supplied one), so it is up
1236 to reply to these "discovery" commands itself.
1237 If that flag is not set, then the OpenSSL framework code will work with
1238 the following rules;
1240 .It If no ctrl() handler is supplied:
1241 .Dv ENGINE_HAS_CTRL_FUNCTION
1242 returns FALSE (zero), all other commands fail.
1243 .It If a ctrl() handler was supplied but no array of control commands:
1244 .Dv ENGINE_HAS_CTRL_FUNCTION
1245 returns TRUE, all other commands fail.
1246 .It If a ctrl() handler and array of control commands was supplied:
1247 .Dv ENGINE_HAS_CTRL_FUNCTION
1248 returns TRUE, all other commands proceed processing...
1253 array of control commands is empty, then all other commands will fail.
1255 .Dv ENGINE_CTRL_GET_FIRST_CMD_TYPE
1256 returns the identifier of the first command supported by the
1258 .Dv ENGINE_GET_NEXT_CMD_TYPE
1259 takes the identifier of a command supported by the
1261 and returns the next command identifier or fails if there are no more,
1262 .Dv ENGINE_CMD_FROM_NAME
1263 takes a string name for a command and returns the corresponding
1264 identifier or fails if no such command name exists, and the remaining
1265 commands take a command identifier and return properties of the
1266 corresponding commands.
1268 .Dv ENGINE_CTRL_GET_FLAGS
1269 return the string length of a command name or description, or
1270 populate a supplied character buffer with a copy of the command
1271 name or description.
1272 .Dv ENGINE_CTRL_GET_FLAGS
1273 returns a bitwise-OR'd mask of the following possible values:
1275 #define ENGINE_CMD_FLAG_NUMERIC (unsigned int)0x0001
1276 #define ENGINE_CMD_FLAG_STRING (unsigned int)0x0002
1277 #define ENGINE_CMD_FLAG_NO_INPUT (unsigned int)0x0004
1278 #define ENGINE_CMD_FLAG_INTERNAL (unsigned int)0x0008
1282 .Dv ENGINE_CMD_FLAG_INTERNAL
1283 flag is set, then any other flags are purely informational to the caller.
1284 This flag will prevent the command being usable for any higher-level
1287 .Fn ENGINE_ctrl_cmd_string .
1288 "INTERNAL" commands are not intended to be exposed to text-based
1289 configuration by applications, administrations, users, etc.
1290 These can support arbitrary operations via
1292 including passing to and/or from the control commands data of any
1294 These commands are supported in the discovery mechanisms simply allow
1295 applications to determine if an
1297 supports certain specific commands it might want to use (e.g.
1298 application "foo" might query various
1300 to see if they implement "FOO_GET_VENDOR_LOGO_GIF" - and
1302 could therefore decide whether or not to support this "foo"-specific
1305 .Fn ENGINE_get_first ,
1306 .Fn ENGINE_get_last ,
1307 .Fn ENGINE_get_next ,
1308 .Fn ENGINE_get_prev ,
1310 .Fn ENGINE_get_cipher_engine ,
1311 .Fn ENGINE_get_digest_engine ,
1314 .Fn ENGINE_get_default_*
1315 functions return a valid
1319 if an error occurred.
1325 .Fn ENGINE_ctrl_cmd ,
1326 .Fn ENGINE_ctrl_cmd_string ,
1332 .Fn ENGINE_register_*
1333 functions return 1 on success or 0 on error.
1335 .Fn ENGINE_get_table_flags
1336 returns an unsigned integer value representing the global table
1337 flags which are used to control the registration behaviour of
1343 positive return values indicate success and negative return values
1345 The meaning of a zero return value depends on the particular
1347 and may indicate both success and failure, which is pathetic.
1349 .Fn ENGINE_cmd_is_executable
1352 is executable or 0 otherwise.
1357 return a pointer to an internal string representing the identifier
1362 .Fn ENGINE_get_RSA ,
1363 .Fn ENGINE_get_DSA ,
1365 .Fn ENGINE_get_RAND ,
1367 .Fn ENGINE_get_STORE
1368 return a method structure for the respective algorithm.
1370 .Fn ENGINE_get_destroy_function ,
1371 .Fn ENGINE_get_init_function ,
1372 .Fn ENGINE_get_finish_function ,
1373 .Fn ENGINE_get_ctrl_function ,
1374 .Fn ENGINE_get_load_privkey_function ,
1375 .Fn ENGINE_get_load_pubkey_function ,
1376 .Fn ENGINE_get_ciphers ,
1378 .Fn ENGINE_get_digests
1379 return a function pointer to the respective callback.
1381 .Fn ENGINE_get_cipher
1384 structure on success or
1386 if an error occurred.
1388 .Fn ENGINE_get_digest
1391 structure on success or
1393 if an error occurred.
1395 .Fn ENGINE_get_flags
1396 returns an integer representing the flags
1397 which are used to control various behaviours of an
1400 .Fn ENGINE_get_cmd_defns
1407 .Fn ENGINE_load_private_key
1409 .Fn ENGINE_load_public_key
1412 structure on success or
1414 if an error occurred.
1418 .Xr ENGINE_add_conf_module 3 ,
1419 .Xr ENGINE_set_ex_data 3 ,
1422 The engine API first appeared in OpenSSL 0.9.7
1423 and has been available since