Standard preamble:
========================================================================
..
.... Set up some character translations and predefined strings. \*(-- will
give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
double quote, and \*(R" will give a right double quote. \*(C+ will
give a nicer C++. Capital omega is used to do unbreakable dashes and
therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
nothing in troff, for use with C<>.
.tr \(*W- . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\}
Escape single quotes in literal strings from groff's Unicode transform.
If the F register is >0, we'll generate index entries on stderr for
titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
entries marked with X<> in POD. Of course, you'll have to process the
output yourself in some meaningful fashion.
Avoid warning from groff about undefined register 'F'.
.. .nr rF 0 . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF
Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] .\} . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents . \" corrections for vroff . \" for low resolution devices (crt and lpr) \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} ========================================================================
Title "ENGINE_ADD 3"
way too many mistakes in technical documents.
Deprecated:
.Vb 3 #if OPENSSL_API_COMPAT < 0x10100000L void ENGINE_cleanup(void) #endif .Ve
The cryptographic functionality that can be provided by an \s-1ENGINE\s0 implementation includes the following abstractions;
.Vb 6 RSA_METHOD - for providing alternative RSA implementations DSA_METHOD, DH_METHOD, RAND_METHOD, ECDH_METHOD, ECDSA_METHOD, - similarly for other OpenSSL APIs EVP_CIPHER - potentially multiple cipher algorithms (indexed by \*(Aqnid\*(Aq) EVP_DIGEST - potentially multiple hash algorithms (indexed by \*(Aqnid\*(Aq) key-loading - loading public and/or private EVP_PKEY keys .Ve
\s-1ENGINE\s0 objects have two levels of reference-counting to match the way in which the objects are used. At the most basic level, each \s-1ENGINE\s0 pointer is inherently a structural reference - a structural reference is required to use the pointer value at all, as this kind of reference is a guarantee that the structure can not be deallocated until the reference is released.
However, a structural reference provides no guarantee that the \s-1ENGINE\s0 is initialised and able to use any of its cryptographic implementations. Indeed it's quite possible that most ENGINEs will not initialise at all in typical environments, as ENGINEs are typically used to support specialised hardware. To use an \s-1ENGINE\s0's functionality, you need a \fBfunctional reference. This kind of reference can be considered a specialised form of structural reference, because each functional reference implicitly contains a structural reference as well - however to avoid difficult-to-find programming bugs, it is recommended to treat the two kinds of reference independently. If you have a functional reference to an \s-1ENGINE,\s0 you have a guarantee that the \s-1ENGINE\s0 has been initialised and is ready to perform cryptographic operations, and will remain initialised until after you have released your reference.
\fIStructural references
This basic type of reference is used for instantiating new ENGINEs, iterating across OpenSSL's internal linked-list of loaded ENGINEs, reading information about an \s-1ENGINE,\s0 etc. Essentially a structural reference is sufficient if you only need to query or manipulate the data of an \s-1ENGINE\s0 implementation rather than use its functionality.
The ENGINE_new() function returns a structural reference to a new (empty) \s-1ENGINE\s0 object. There are other \s-1ENGINE API\s0 functions that return structural references such as; ENGINE_by_id(), ENGINE_get_first(), ENGINE_get_last(), \fBENGINE_get_next(), ENGINE_get_prev(). All structural references should be released by a corresponding to call to the ENGINE_free() function - the \s-1ENGINE\s0 object itself will only actually be cleaned up and deallocated when the last structural reference is released.
It should also be noted that many \s-1ENGINE API\s0 function calls that accept a structural reference will internally obtain another reference - typically this happens whenever the supplied \s-1ENGINE\s0 will be needed by OpenSSL after the function has returned. Eg. the function to add a new \s-1ENGINE\s0 to OpenSSL's internal list is ENGINE_add() - if this function returns success, then OpenSSL will have stored a new structural reference internally so the caller is still responsible for freeing their own reference with \fBENGINE_free() when they are finished with it. In a similar way, some functions will automatically release the structural reference passed to it if part of the function's job is to do so. Eg. the ENGINE_get_next() and \fBENGINE_get_prev() functions are used for iterating across the internal \s-1ENGINE\s0 list - they will return a new structural reference to the next (or previous) \s-1ENGINE\s0 in the list or \s-1NULL\s0 if at the end (or beginning) of the list, but in either case the structural reference passed to the function is released on behalf of the caller.
To clarify a particular function's handling of references, one should always consult that function's documentation \*(L"man\*(R" page, or failing that the openssl/engine.h header file includes some hints.
\fIFunctional references
As mentioned, functional references exist when the cryptographic functionality of an \s-1ENGINE\s0 is required to be available. A functional reference can be obtained in one of two ways; from an existing structural reference to the required \s-1ENGINE,\s0 or by asking OpenSSL for the default operational \s-1ENGINE\s0 for a given cryptographic purpose.
To obtain a functional reference from an existing structural reference, call the ENGINE_init() function. This returns zero if the \s-1ENGINE\s0 was not already operational and couldn't be successfully initialised (e.g. lack of system drivers, no special hardware attached, etc), otherwise it will return nonzero to indicate that the \s-1ENGINE\s0 is now operational and will have allocated a new functional reference to the \s-1ENGINE.\s0 All functional references are released by calling ENGINE_finish() (which removes the implicit structural reference as well).
The second way to get a functional reference is by asking OpenSSL for a default implementation for a given task, e.g. by ENGINE_get_default_RSA(), \fBENGINE_get_default_cipher_engine(), etc. These are discussed in the next section, though they are not usually required by application programmers as they are used automatically when creating and using the relevant algorithm-specific types in OpenSSL, such as \s-1RSA, DSA, EVP_CIPHER_CTX,\s0 etc.
When a default \s-1ENGINE\s0 is requested for a given abstraction/algorithm/mode, (e.g. when calling RSA_new_method(\s-1NULL\s0)), a \*(L"get_default\*(R" call will be made to the \s-1ENGINE\s0 subsystem to process the corresponding state table and return a functional reference to an initialised \s-1ENGINE\s0 whose implementation should be used. If no \s-1ENGINE\s0 should (or can) be used, it will return \s-1NULL\s0 and the caller will operate with a \s-1NULL ENGINE\s0 handle - this usually equates to using the conventional software implementation. In the latter case, OpenSSL will from then on behave the way it used to before the \s-1ENGINE API\s0 existed.
Each state table has a flag to note whether it has processed this \*(L"get_default\*(R" query since the table was last modified, because to process this question it must iterate across all the registered ENGINEs in the table trying to initialise each of them in turn, in case one of them is operational. If it returns a functional reference to an \s-1ENGINE,\s0 it will also cache another reference to speed up processing future queries (without needing to iterate across the table). Likewise, it will cache a \s-1NULL\s0 response if no \s-1ENGINE\s0 was available so that future queries won't repeat the same iteration unless the state table changes. This behaviour can also be changed; if the \s-1ENGINE_TABLE_FLAG_NOINIT\s0 flag is set (using \fBENGINE_set_table_flags()), no attempted initialisations will take place, instead the only way for the state table to return a non-NULL \s-1ENGINE\s0 to the \*(L"get_default\*(R" query will be if one is expressly set in the table. Eg. \fBENGINE_set_default_RSA() does the same job as ENGINE_register_RSA() except that it also sets the state table's cached response for the \*(L"get_default\*(R" query. In the case of abstractions like \s-1EVP_CIPHER,\s0 where implementations are indexed by 'nid', these flags and cached-responses are distinct for each 'nid' value.
The fact that ENGINEs are made visible to OpenSSL (and thus are linked into the program and loaded into memory at run-time) does not mean they are \*(L"registered\*(R" or called into use by OpenSSL automatically - that behaviour is something for the application to control. Some applications will want to allow the user to specify exactly which \s-1ENGINE\s0 they want used if any is to be used at all. Others may prefer to load all support and have OpenSSL automatically use at run-time any \s-1ENGINE\s0 that is able to successfully initialise - i.e. to assume that this corresponds to acceleration hardware attached to the machine or some such thing. There are probably numerous other ways in which applications may prefer to handle things, so we will simply illustrate the consequences as they apply to a couple of simple cases and leave developers to consider these and the source code to openssl's builtin utilities as guides.
If no \s-1ENGINE API\s0 functions are called within an application, then OpenSSL will not allocate any internal resources. Prior to OpenSSL 1.1.0, however, if any ENGINEs are loaded, even if not registered or used, it was necessary to call ENGINE_cleanup() before the program exits.
\fIUsing a specific \s-1ENGINE\s0 implementation
Here we'll assume an application has been configured by its user or admin to want to use the \*(L"\s-1ACME\*(R" ENGINE\s0 if it is available in the version of OpenSSL the application was compiled with. If it is available, it should be used by default for all \s-1RSA, DSA,\s0 and symmetric cipher operations, otherwise OpenSSL should use its builtin software as per usual. The following code illustrates how to approach this;
.Vb 10 ENGINE *e; const char *engine_id = "ACME"; ENGINE_load_builtin_engines(); e = ENGINE_by_id(engine_id); if (!e) /* the engine isn\*(Aqt available */ return; if (!ENGINE_init(e)) { /* the engine couldn\*(Aqt initialise, release \*(Aqe\*(Aq */ ENGINE_free(e); return; } if (!ENGINE_set_default_RSA(e)) /* * This should only happen when \*(Aqe\*(Aq can\*(Aqt initialise, but the previous * statement suggests it did. */ abort(); ENGINE_set_default_DSA(e); ENGINE_set_default_ciphers(e); /* Release the functional reference from ENGINE_init() */ ENGINE_finish(e); /* Release the structural reference from ENGINE_by_id() */ ENGINE_free(e); .Ve
\fIAutomatically using builtin \s-1ENGINE\s0 implementations
Here we'll assume we want to load and register all \s-1ENGINE\s0 implementations bundled with OpenSSL, such that for any cryptographic algorithm required by OpenSSL - if there is an \s-1ENGINE\s0 that implements it and can be initialised, it should be used. The following code illustrates how this can work;
.Vb 4 /* Load all bundled ENGINEs into memory and make them visible */ ENGINE_load_builtin_engines(); /* Register all of them for every algorithm they collectively implement */ ENGINE_register_all_complete(); .Ve
That's all that's required. Eg. the next time OpenSSL tries to set up an \s-1RSA\s0 key, any bundled ENGINEs that implement \s-1RSA_METHOD\s0 will be passed to \fBENGINE_init() and if any of those succeed, that \s-1ENGINE\s0 will be set as the default for \s-1RSA\s0 use from then on.
Before illustrating how control commands work, it is worth mentioning what they are typically used for. Broadly speaking there are two uses for control commands; the first is to provide the necessary details to the implementation (which may know nothing at all specific to the host system) so that it can be initialised for use. This could include the path to any driver or config files it needs to load, required network addresses, smart-card identifiers, passwords to initialise protected devices, logging information, etc etc. This class of commands typically needs to be passed to an \s-1ENGINE\s0 before attempting to initialise it, i.e. before calling ENGINE_init(). The other class of commands consist of settings or operations that tweak certain behaviour or cause certain operations to take place, and these commands may work either before or after ENGINE_init(), or in some cases both. \s-1ENGINE\s0 implementations should provide indications of this in the descriptions attached to builtin control commands and/or in external product documentation.
\fIIssuing control commands to an \s-1ENGINE\s0
Let's illustrate by example; a function for which the caller supplies the name of the \s-1ENGINE\s0 it wishes to use, a table of string-pairs for use before initialisation, and another table for use after initialisation. Note that the string-pairs used for control commands consist of a command \*(L"name\*(R" followed by the command \*(L"parameter\*(R" - the parameter could be \s-1NULL\s0 in some cases but the name can not. This function should initialise the \s-1ENGINE\s0 (issuing the \*(L"pre\*(R" commands beforehand and the \*(L"post\*(R" commands afterwards) and set it as the default for everything except \s-1RAND\s0 and then return a boolean success or failure.
.Vb 10 int generic_load_engine_fn(const char *engine_id, const char **pre_cmds, int pre_num, const char **post_cmds, int post_num) { ENGINE *e = ENGINE_by_id(engine_id); if (!e) return 0; while (pre_num--) { if (!ENGINE_ctrl_cmd_string(e, pre_cmds[0], pre_cmds[1], 0)) { fprintf(stderr, "Failed command (%s - %s:%s)\en", engine_id, pre_cmds[0], pre_cmds[1] ? pre_cmds[1] : "(NULL)"); ENGINE_free(e); return 0; } pre_cmds += 2; } if (!ENGINE_init(e)) { fprintf(stderr, "Failed initialisation\en"); ENGINE_free(e); return 0; } /* * ENGINE_init() returned a functional reference, so free the structural * reference from ENGINE_by_id(). */ ENGINE_free(e); while (post_num--) { if (!ENGINE_ctrl_cmd_string(e, post_cmds[0], post_cmds[1], 0)) { fprintf(stderr, "Failed command (%s - %s:%s)\en", engine_id, post_cmds[0], post_cmds[1] ? post_cmds[1] : "(NULL)"); ENGINE_finish(e); return 0; } post_cmds += 2; } ENGINE_set_default(e, ENGINE_METHOD_ALL & ~ENGINE_METHOD_RAND); /* Success */ return 1; } .Ve
Note that ENGINE_ctrl_cmd_string() accepts a boolean argument that can relax the semantics of the function - if set nonzero it will only return failure if the \s-1ENGINE\s0 supported the given command name but failed while executing it, if the \s-1ENGINE\s0 doesn't support the command name it will simply return success without doing anything. In this case we assume the user is only supplying commands specific to the given \s-1ENGINE\s0 so we set this to \s-1FALSE.\s0
\fIDiscovering supported control commands
It is possible to discover at run-time the names, numerical-ids, descriptions and input parameters of the control commands supported by an \s-1ENGINE\s0 using a structural reference. Note that some control commands are defined by OpenSSL itself and it will intercept and handle these control commands on behalf of the \s-1ENGINE,\s0 i.e. the \s-1ENGINE\s0's ctrl() handler is not used for the control command. openssl/engine.h defines an index, \s-1ENGINE_CMD_BASE,\s0 that all control commands implemented by ENGINEs should be numbered from. Any command value lower than this symbol is considered a \*(L"generic\*(R" command is handled directly by the OpenSSL core routines.
It is using these \*(L"core\*(R" control commands that one can discover the control commands implemented by a given \s-1ENGINE,\s0 specifically the commands:
.Vb 9 ENGINE_HAS_CTRL_FUNCTION ENGINE_CTRL_GET_FIRST_CMD_TYPE ENGINE_CTRL_GET_NEXT_CMD_TYPE ENGINE_CTRL_GET_CMD_FROM_NAME ENGINE_CTRL_GET_NAME_LEN_FROM_CMD ENGINE_CTRL_GET_NAME_FROM_CMD ENGINE_CTRL_GET_DESC_LEN_FROM_CMD ENGINE_CTRL_GET_DESC_FROM_CMD ENGINE_CTRL_GET_CMD_FLAGS .Ve
Whilst these commands are automatically processed by the OpenSSL framework code, they use various properties exposed by each \s-1ENGINE\s0 to process these queries. An \s-1ENGINE\s0 has 3 properties it exposes that can affect how this behaves; it can supply a ctrl() handler, it can specify \s-1ENGINE_FLAGS_MANUAL_CMD_CTRL\s0 in the \s-1ENGINE\s0's flags, and it can expose an array of control command descriptions. If an \s-1ENGINE\s0 specifies the \s-1ENGINE_FLAGS_MANUAL_CMD_CTRL\s0 flag, then it will simply pass all these \*(L"core\*(R" control commands directly to the \s-1ENGINE\s0's ctrl() handler (and thus, it must have supplied one), so it is up to the \s-1ENGINE\s0 to reply to these \*(L"discovery\*(R" commands itself. If that flag is not set, then the OpenSSL framework code will work with the following rules:
.Vb 9 if no ctrl() handler supplied; ENGINE_HAS_CTRL_FUNCTION returns FALSE (zero), all other commands fail. if a ctrl() handler was supplied but no array of control commands; ENGINE_HAS_CTRL_FUNCTION returns TRUE, all other commands fail. if a ctrl() handler and array of control commands was supplied; ENGINE_HAS_CTRL_FUNCTION returns TRUE, all other commands proceed processing ... .Ve
If the \s-1ENGINE\s0's array of control commands is empty then all other commands will fail, otherwise; \s-1ENGINE_CTRL_GET_FIRST_CMD_TYPE\s0 returns the identifier of the first command supported by the \s-1ENGINE, ENGINE_GET_NEXT_CMD_TYPE\s0 takes the identifier of a command supported by the \s-1ENGINE\s0 and returns the next command identifier or fails if there are no more, \s-1ENGINE_CMD_FROM_NAME\s0 takes a string name for a command and returns the corresponding identifier or fails if no such command name exists, and the remaining commands take a command identifier and return properties of the corresponding commands. All except \s-1ENGINE_CTRL_GET_FLAGS\s0 return the string length of a command name or description, or populate a supplied character buffer with a copy of the command name or description. \s-1ENGINE_CTRL_GET_FLAGS\s0 returns a bitwise-OR'd mask of the following possible values:
.Vb 4 ENGINE_CMD_FLAG_NUMERIC ENGINE_CMD_FLAG_STRING ENGINE_CMD_FLAG_NO_INPUT ENGINE_CMD_FLAG_INTERNAL .Ve
If the \s-1ENGINE_CMD_FLAG_INTERNAL\s0 flag is set, then any other flags are purely informational to the caller - this flag will prevent the command being usable for any higher-level \s-1ENGINE\s0 functions such as ENGINE_ctrl_cmd_string(). \*(L"\s-1INTERNAL\*(R"\s0 commands are not intended to be exposed to text-based configuration by applications, administrations, users, etc. These can support arbitrary operations via ENGINE_ctrl(), including passing to and/or from the control commands data of any arbitrary type. These commands are supported in the discovery mechanisms simply to allow applications to determine if an \s-1ENGINE\s0 supports certain specific commands it might want to use (e.g. application \*(L"foo\*(R" might query various ENGINEs to see if they implement \*(L"\s-1FOO_GET_VENDOR_LOGO_GIF\*(R"\s0 - and \s-1ENGINE\s0 could therefore decide whether or not to support this \*(L"foo\*(R"-specific extension).
\fBENGINE_add() and ENGINE_remove() return 1 on success or 0 on error.
\fBENGINE_by_id() returns a valid \s-1ENGINE\s0 structure or \s-1NULL\s0 if an error occurred.
\fBENGINE_init() and ENGINE_finish() return 1 on success or 0 on error.
All ENGINE_get_default_TYPE() functions, ENGINE_get_cipher_engine() and \fBENGINE_get_digest_engine() return a valid \s-1ENGINE\s0 structure on success or \s-1NULL\s0 if an error occurred.
All ENGINE_set_default_TYPE() functions return 1 on success or 0 on error.
\fBENGINE_set_default() returns 1 on success or 0 on error.
\fBENGINE_get_table_flags() returns an unsigned integer value representing the global table flags which are used to control the registration behaviour of \fB\s-1ENGINE\s0 implementations.
All ENGINE_register_TYPE() functions return 1 on success or 0 on error.
\fBENGINE_register_complete() and ENGINE_register_all_complete() always return 1.
\fBENGINE_ctrl() returns a positive value on success or others on error.
\fBENGINE_cmd_is_executable() returns 1 if cmd is executable or 0 otherwise.
\fBENGINE_ctrl_cmd() and ENGINE_ctrl_cmd_string() return 1 on success or 0 on error.
\fBENGINE_new() returns a valid \s-1ENGINE\s0 structure on success or \s-1NULL\s0 if an error occurred.
\fBENGINE_free() always returns 1.
\fBENGINE_up_ref() returns 1 on success or 0 on error.
\fBENGINE_set_id() and ENGINE_set_name() return 1 on success or 0 on error.
All other ENGINE_set_* functions return 1 on success or 0 on error.
\fBENGINE_get_id() and ENGINE_get_name() return a string representing the identifier and the name of the \s-1ENGINE\s0 e respectively.
\fBENGINE_get_RSA(), ENGINE_get_DSA(), ENGINE_get_DH() and ENGINE_get_RAND() return corresponding method structures for each algorithms.
\fBENGINE_get_destroy_function(), ENGINE_get_init_function(), \fBENGINE_get_finish_function(), ENGINE_get_ctrl_function(), \fBENGINE_get_load_privkey_function(), ENGINE_get_load_pubkey_function(), \fBENGINE_get_ciphers() and ENGINE_get_digests() return corresponding function pointers of the callbacks.
\fBENGINE_get_cipher() returns a valid \s-1EVP_CIPHER\s0 structure on success or \s-1NULL\s0 if an error occurred.
\fBENGINE_get_digest() returns a valid \s-1EVP_MD\s0 structure on success or \s-1NULL\s0 if an error occurred.
\fBENGINE_get_flags() returns an integer representing the \s-1ENGINE\s0 flags which are used to control various behaviours of an \s-1ENGINE.\s0
\fBENGINE_get_cmd_defns() returns an \s-1ENGINE_CMD_DEFN\s0 structure or \s-1NULL\s0 if it's not set.
\fBENGINE_load_private_key() and ENGINE_load_public_key() return a valid \s-1EVP_PKEY\s0 structure on success or \s-1NULL\s0 if an error occurred.
Licensed under the OpenSSL license (the \*(L"License\*(R"). You may not use this file except in compliance with the License. You can obtain a copy in the file \s-1LICENSE\s0 in the source distribution or at <https://www.openssl.org/source/license.html>.