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