Blame doc/man3/ENGINE_add.pod

Packit c4476c
=pod
Packit c4476c
Packit c4476c
=head1 NAME
Packit c4476c
Packit c4476c
ENGINE_get_DH, ENGINE_get_DSA,
Packit c4476c
ENGINE_by_id, ENGINE_get_cipher_engine, ENGINE_get_default_DH,
Packit c4476c
ENGINE_get_default_DSA,
Packit c4476c
ENGINE_get_default_RAND,
Packit c4476c
ENGINE_get_default_RSA, ENGINE_get_digest_engine, ENGINE_get_first,
Packit c4476c
ENGINE_get_last, ENGINE_get_next, ENGINE_get_prev, ENGINE_new,
Packit c4476c
ENGINE_get_ciphers, ENGINE_get_ctrl_function, ENGINE_get_digests,
Packit c4476c
ENGINE_get_destroy_function, ENGINE_get_finish_function,
Packit c4476c
ENGINE_get_init_function, ENGINE_get_load_privkey_function,
Packit c4476c
ENGINE_get_load_pubkey_function, ENGINE_load_private_key,
Packit c4476c
ENGINE_load_public_key, ENGINE_get_RAND, ENGINE_get_RSA, ENGINE_get_id,
Packit c4476c
ENGINE_get_name, ENGINE_get_cmd_defns, ENGINE_get_cipher,
Packit c4476c
ENGINE_get_digest, ENGINE_add, ENGINE_cmd_is_executable,
Packit c4476c
ENGINE_ctrl, ENGINE_ctrl_cmd, ENGINE_ctrl_cmd_string,
Packit c4476c
ENGINE_finish, ENGINE_free, ENGINE_get_flags, ENGINE_init,
Packit c4476c
ENGINE_register_DH, ENGINE_register_DSA,
Packit c4476c
ENGINE_register_RAND, ENGINE_register_RSA,
Packit c4476c
ENGINE_register_all_complete, ENGINE_register_ciphers,
Packit c4476c
ENGINE_register_complete, ENGINE_register_digests, ENGINE_remove,
Packit c4476c
ENGINE_set_DH, ENGINE_set_DSA,
Packit c4476c
ENGINE_set_RAND, ENGINE_set_RSA, ENGINE_set_ciphers,
Packit c4476c
ENGINE_set_cmd_defns, ENGINE_set_ctrl_function, ENGINE_set_default,
Packit c4476c
ENGINE_set_default_DH, ENGINE_set_default_DSA,
Packit c4476c
ENGINE_set_default_RAND, ENGINE_set_default_RSA,
Packit c4476c
ENGINE_set_default_ciphers, ENGINE_set_default_digests,
Packit c4476c
ENGINE_set_default_string, ENGINE_set_destroy_function,
Packit c4476c
ENGINE_set_digests, ENGINE_set_finish_function, ENGINE_set_flags,
Packit c4476c
ENGINE_set_id, ENGINE_set_init_function, ENGINE_set_load_privkey_function,
Packit c4476c
ENGINE_set_load_pubkey_function, ENGINE_set_name, ENGINE_up_ref,
Packit c4476c
ENGINE_get_table_flags, ENGINE_cleanup,
Packit c4476c
ENGINE_load_builtin_engines, ENGINE_register_all_DH,
Packit c4476c
ENGINE_register_all_DSA,
Packit c4476c
ENGINE_register_all_RAND,
Packit c4476c
ENGINE_register_all_RSA, ENGINE_register_all_ciphers,
Packit c4476c
ENGINE_register_all_digests, ENGINE_set_table_flags, ENGINE_unregister_DH,
Packit c4476c
ENGINE_unregister_DSA,
Packit c4476c
ENGINE_unregister_RAND, ENGINE_unregister_RSA, ENGINE_unregister_ciphers,
Packit c4476c
ENGINE_unregister_digests
Packit c4476c
- ENGINE cryptographic module support
Packit c4476c
Packit c4476c
=head1 SYNOPSIS
Packit c4476c
Packit c4476c
 #include <openssl/engine.h>
Packit c4476c
Packit c4476c
 ENGINE *ENGINE_get_first(void);
Packit c4476c
 ENGINE *ENGINE_get_last(void);
Packit c4476c
 ENGINE *ENGINE_get_next(ENGINE *e);
Packit c4476c
 ENGINE *ENGINE_get_prev(ENGINE *e);
Packit c4476c
Packit c4476c
 int ENGINE_add(ENGINE *e);
Packit c4476c
 int ENGINE_remove(ENGINE *e);
Packit c4476c
Packit c4476c
 ENGINE *ENGINE_by_id(const char *id);
Packit c4476c
Packit c4476c
 int ENGINE_init(ENGINE *e);
Packit c4476c
 int ENGINE_finish(ENGINE *e);
Packit c4476c
Packit c4476c
 void ENGINE_load_builtin_engines(void);
Packit c4476c
Packit c4476c
 ENGINE *ENGINE_get_default_RSA(void);
Packit c4476c
 ENGINE *ENGINE_get_default_DSA(void);
Packit c4476c
 ENGINE *ENGINE_get_default_DH(void);
Packit c4476c
 ENGINE *ENGINE_get_default_RAND(void);
Packit c4476c
 ENGINE *ENGINE_get_cipher_engine(int nid);
Packit c4476c
 ENGINE *ENGINE_get_digest_engine(int nid);
Packit c4476c
Packit c4476c
 int ENGINE_set_default_RSA(ENGINE *e);
Packit c4476c
 int ENGINE_set_default_DSA(ENGINE *e);
Packit c4476c
 int ENGINE_set_default_DH(ENGINE *e);
Packit c4476c
 int ENGINE_set_default_RAND(ENGINE *e);
Packit c4476c
 int ENGINE_set_default_ciphers(ENGINE *e);
Packit c4476c
 int ENGINE_set_default_digests(ENGINE *e);
Packit c4476c
 int ENGINE_set_default_string(ENGINE *e, const char *list);
Packit c4476c
Packit c4476c
 int ENGINE_set_default(ENGINE *e, unsigned int flags);
Packit c4476c
Packit c4476c
 unsigned int ENGINE_get_table_flags(void);
Packit c4476c
 void ENGINE_set_table_flags(unsigned int flags);
Packit c4476c
Packit c4476c
 int ENGINE_register_RSA(ENGINE *e);
Packit c4476c
 void ENGINE_unregister_RSA(ENGINE *e);
Packit c4476c
 void ENGINE_register_all_RSA(void);
Packit c4476c
 int ENGINE_register_DSA(ENGINE *e);
Packit c4476c
 void ENGINE_unregister_DSA(ENGINE *e);
Packit c4476c
 void ENGINE_register_all_DSA(void);
Packit c4476c
 int ENGINE_register_DH(ENGINE *e);
Packit c4476c
 void ENGINE_unregister_DH(ENGINE *e);
Packit c4476c
 void ENGINE_register_all_DH(void);
Packit c4476c
 int ENGINE_register_RAND(ENGINE *e);
Packit c4476c
 void ENGINE_unregister_RAND(ENGINE *e);
Packit c4476c
 void ENGINE_register_all_RAND(void);
Packit c4476c
 int ENGINE_register_ciphers(ENGINE *e);
Packit c4476c
 void ENGINE_unregister_ciphers(ENGINE *e);
Packit c4476c
 void ENGINE_register_all_ciphers(void);
Packit c4476c
 int ENGINE_register_digests(ENGINE *e);
Packit c4476c
 void ENGINE_unregister_digests(ENGINE *e);
Packit c4476c
 void ENGINE_register_all_digests(void);
Packit c4476c
 int ENGINE_register_complete(ENGINE *e);
Packit c4476c
 int ENGINE_register_all_complete(void);
Packit c4476c
Packit c4476c
 int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
Packit c4476c
 int ENGINE_cmd_is_executable(ENGINE *e, int cmd);
Packit c4476c
 int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
Packit c4476c
                     long i, void *p, void (*f)(void), int cmd_optional);
Packit c4476c
 int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
Packit c4476c
                            int cmd_optional);
Packit c4476c
Packit c4476c
 ENGINE *ENGINE_new(void);
Packit c4476c
 int ENGINE_free(ENGINE *e);
Packit c4476c
 int ENGINE_up_ref(ENGINE *e);
Packit c4476c
Packit c4476c
 int ENGINE_set_id(ENGINE *e, const char *id);
Packit c4476c
 int ENGINE_set_name(ENGINE *e, const char *name);
Packit c4476c
 int ENGINE_set_RSA(ENGINE *e, const RSA_METHOD *rsa_meth);
Packit c4476c
 int ENGINE_set_DSA(ENGINE *e, const DSA_METHOD *dsa_meth);
Packit c4476c
 int ENGINE_set_DH(ENGINE *e, const DH_METHOD *dh_meth);
Packit c4476c
 int ENGINE_set_RAND(ENGINE *e, const RAND_METHOD *rand_meth);
Packit c4476c
 int ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f);
Packit c4476c
 int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f);
Packit c4476c
 int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f);
Packit c4476c
 int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f);
Packit c4476c
 int ENGINE_set_load_privkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpriv_f);
Packit c4476c
 int ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f);
Packit c4476c
 int ENGINE_set_ciphers(ENGINE *e, ENGINE_CIPHERS_PTR f);
Packit c4476c
 int ENGINE_set_digests(ENGINE *e, ENGINE_DIGESTS_PTR f);
Packit c4476c
 int ENGINE_set_flags(ENGINE *e, int flags);
Packit c4476c
 int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns);
Packit c4476c
Packit c4476c
 const char *ENGINE_get_id(const ENGINE *e);
Packit c4476c
 const char *ENGINE_get_name(const ENGINE *e);
Packit c4476c
 const RSA_METHOD *ENGINE_get_RSA(const ENGINE *e);
Packit c4476c
 const DSA_METHOD *ENGINE_get_DSA(const ENGINE *e);
Packit c4476c
 const DH_METHOD *ENGINE_get_DH(const ENGINE *e);
Packit c4476c
 const RAND_METHOD *ENGINE_get_RAND(const ENGINE *e);
Packit c4476c
 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e);
Packit c4476c
 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e);
Packit c4476c
 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e);
Packit c4476c
 ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e);
Packit c4476c
 ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e);
Packit c4476c
 ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e);
Packit c4476c
 ENGINE_CIPHERS_PTR ENGINE_get_ciphers(const ENGINE *e);
Packit c4476c
 ENGINE_DIGESTS_PTR ENGINE_get_digests(const ENGINE *e);
Packit c4476c
 const EVP_CIPHER *ENGINE_get_cipher(ENGINE *e, int nid);
Packit c4476c
 const EVP_MD *ENGINE_get_digest(ENGINE *e, int nid);
Packit c4476c
 int ENGINE_get_flags(const ENGINE *e);
Packit c4476c
 const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e);
Packit c4476c
Packit c4476c
 EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
Packit c4476c
                                   UI_METHOD *ui_method, void *callback_data);
Packit c4476c
 EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
Packit c4476c
                                  UI_METHOD *ui_method, void *callback_data);
Packit c4476c
Packit c4476c
Deprecated:
Packit c4476c
Packit c4476c
 #if OPENSSL_API_COMPAT < 0x10100000L
Packit c4476c
 void ENGINE_cleanup(void)
Packit c4476c
 #endif
Packit c4476c
Packit c4476c
=head1 DESCRIPTION
Packit c4476c
Packit c4476c
These functions create, manipulate, and use cryptographic modules in the
Packit c4476c
form of B<ENGINE> objects. These objects act as containers for
Packit c4476c
implementations of cryptographic algorithms, and support a
Packit c4476c
reference-counted mechanism to allow them to be dynamically loaded in and
Packit c4476c
out of the running application.
Packit c4476c
Packit c4476c
The cryptographic functionality that can be provided by an B<ENGINE>
Packit c4476c
implementation includes the following abstractions;
Packit c4476c
Packit c4476c
 RSA_METHOD - for providing alternative RSA implementations
Packit c4476c
 DSA_METHOD, DH_METHOD, RAND_METHOD, ECDH_METHOD, ECDSA_METHOD,
Packit c4476c
       - similarly for other OpenSSL APIs
Packit c4476c
 EVP_CIPHER - potentially multiple cipher algorithms (indexed by 'nid')
Packit c4476c
 EVP_DIGEST - potentially multiple hash algorithms (indexed by 'nid')
Packit c4476c
 key-loading - loading public and/or private EVP_PKEY keys
Packit c4476c
Packit c4476c
=head2 Reference counting and handles
Packit c4476c
Packit c4476c
Due to the modular nature of the ENGINE API, pointers to ENGINEs need to be
Packit c4476c
treated as handles - ie. not only as pointers, but also as references to
Packit c4476c
the underlying ENGINE object. Ie. one should obtain a new reference when
Packit c4476c
making copies of an ENGINE pointer if the copies will be used (and
Packit c4476c
released) independently.
Packit c4476c
Packit c4476c
ENGINE objects have two levels of reference-counting to match the way in
Packit c4476c
which the objects are used. At the most basic level, each ENGINE pointer is
Packit c4476c
inherently a B<structural> reference - a structural reference is required
Packit c4476c
to use the pointer value at all, as this kind of reference is a guarantee
Packit c4476c
that the structure can not be deallocated until the reference is released.
Packit c4476c
Packit c4476c
However, a structural reference provides no guarantee that the ENGINE is
Packit c4476c
initialised and able to use any of its cryptographic
Packit c4476c
implementations. Indeed it's quite possible that most ENGINEs will not
Packit c4476c
initialise at all in typical environments, as ENGINEs are typically used to
Packit c4476c
support specialised hardware. To use an ENGINE's functionality, you need a
Packit c4476c
B<functional> reference. This kind of reference can be considered a
Packit c4476c
specialised form of structural reference, because each functional reference
Packit c4476c
implicitly contains a structural reference as well - however to avoid
Packit c4476c
difficult-to-find programming bugs, it is recommended to treat the two
Packit c4476c
kinds of reference independently. If you have a functional reference to an
Packit c4476c
ENGINE, you have a guarantee that the ENGINE has been initialised and
Packit c4476c
is ready to perform cryptographic operations, and will remain initialised
Packit c4476c
until after you have released your reference.
Packit c4476c
Packit c4476c
I<Structural references>
Packit c4476c
Packit c4476c
This basic type of reference is used for instantiating new ENGINEs,
Packit c4476c
iterating across OpenSSL's internal linked-list of loaded
Packit c4476c
ENGINEs, reading information about an ENGINE, etc. Essentially a structural
Packit c4476c
reference is sufficient if you only need to query or manipulate the data of
Packit c4476c
an ENGINE implementation rather than use its functionality.
Packit c4476c
Packit c4476c
The ENGINE_new() function returns a structural reference to a new (empty)
Packit c4476c
ENGINE object. There are other ENGINE API functions that return structural
Packit c4476c
references such as; ENGINE_by_id(), ENGINE_get_first(), ENGINE_get_last(),
Packit c4476c
ENGINE_get_next(), ENGINE_get_prev(). All structural references should be
Packit c4476c
released by a corresponding to call to the ENGINE_free() function - the
Packit c4476c
ENGINE object itself will only actually be cleaned up and deallocated when
Packit c4476c
the last structural reference is released.
Packit c4476c
Packit c4476c
It should also be noted that many ENGINE API function calls that accept a
Packit c4476c
structural reference will internally obtain another reference - typically
Packit c4476c
this happens whenever the supplied ENGINE will be needed by OpenSSL after
Packit c4476c
the function has returned. Eg. the function to add a new ENGINE to
Packit c4476c
OpenSSL's internal list is ENGINE_add() - if this function returns success,
Packit c4476c
then OpenSSL will have stored a new structural reference internally so the
Packit c4476c
caller is still responsible for freeing their own reference with
Packit c4476c
ENGINE_free() when they are finished with it. In a similar way, some
Packit c4476c
functions will automatically release the structural reference passed to it
Packit c4476c
if part of the function's job is to do so. Eg. the ENGINE_get_next() and
Packit c4476c
ENGINE_get_prev() functions are used for iterating across the internal
Packit c4476c
ENGINE list - they will return a new structural reference to the next (or
Packit c4476c
previous) ENGINE in the list or NULL if at the end (or beginning) of the
Packit c4476c
list, but in either case the structural reference passed to the function is
Packit c4476c
released on behalf of the caller.
Packit c4476c
Packit c4476c
To clarify a particular function's handling of references, one should
Packit c4476c
always consult that function's documentation "man" page, or failing that
Packit c4476c
the openssl/engine.h header file includes some hints.
Packit c4476c
Packit c4476c
I<Functional references>
Packit c4476c
Packit c4476c
As mentioned, functional references exist when the cryptographic
Packit c4476c
functionality of an ENGINE is required to be available. A functional
Packit c4476c
reference can be obtained in one of two ways; from an existing structural
Packit c4476c
reference to the required ENGINE, or by asking OpenSSL for the default
Packit c4476c
operational ENGINE for a given cryptographic purpose.
Packit c4476c
Packit c4476c
To obtain a functional reference from an existing structural reference,
Packit c4476c
call the ENGINE_init() function. This returns zero if the ENGINE was not
Packit c4476c
already operational and couldn't be successfully initialised (eg. lack of
Packit c4476c
system drivers, no special hardware attached, etc), otherwise it will
Packit c4476c
return non-zero to indicate that the ENGINE is now operational and will
Packit c4476c
have allocated a new B<functional> reference to the ENGINE. All functional
Packit c4476c
references are released by calling ENGINE_finish() (which removes the
Packit c4476c
implicit structural reference as well).
Packit c4476c
Packit c4476c
The second way to get a functional reference is by asking OpenSSL for a
Packit c4476c
default implementation for a given task, eg. by ENGINE_get_default_RSA(),
Packit c4476c
ENGINE_get_default_cipher_engine(), etc. These are discussed in the next
Packit c4476c
section, though they are not usually required by application programmers as
Packit c4476c
they are used automatically when creating and using the relevant
Packit c4476c
algorithm-specific types in OpenSSL, such as RSA, DSA, EVP_CIPHER_CTX, etc.
Packit c4476c
Packit c4476c
=head2 Default implementations
Packit c4476c
Packit c4476c
For each supported abstraction, the ENGINE code maintains an internal table
Packit c4476c
of state to control which implementations are available for a given
Packit c4476c
abstraction and which should be used by default. These implementations are
Packit c4476c
registered in the tables and indexed by an 'nid' value, because
Packit c4476c
abstractions like EVP_CIPHER and EVP_DIGEST support many distinct
Packit c4476c
algorithms and modes, and ENGINEs can support arbitrarily many of them.
Packit c4476c
In the case of other abstractions like RSA, DSA, etc, there is only one
Packit c4476c
"algorithm" so all implementations implicitly register using the same 'nid'
Packit c4476c
index.
Packit c4476c
Packit c4476c
When a default ENGINE is requested for a given abstraction/algorithm/mode, (eg.
Packit c4476c
when calling RSA_new_method(NULL)), a "get_default" call will be made to the
Packit c4476c
ENGINE subsystem to process the corresponding state table and return a
Packit c4476c
functional reference to an initialised ENGINE whose implementation should be
Packit c4476c
used. If no ENGINE should (or can) be used, it will return NULL and the caller
Packit c4476c
will operate with a NULL ENGINE handle - this usually equates to using the
Packit c4476c
conventional software implementation. In the latter case, OpenSSL will from
Packit c4476c
then on behave the way it used to before the ENGINE API existed.
Packit c4476c
Packit c4476c
Each state table has a flag to note whether it has processed this
Packit c4476c
"get_default" query since the table was last modified, because to process
Packit c4476c
this question it must iterate across all the registered ENGINEs in the
Packit c4476c
table trying to initialise each of them in turn, in case one of them is
Packit c4476c
operational. If it returns a functional reference to an ENGINE, it will
Packit c4476c
also cache another reference to speed up processing future queries (without
Packit c4476c
needing to iterate across the table). Likewise, it will cache a NULL
Packit c4476c
response if no ENGINE was available so that future queries won't repeat the
Packit c4476c
same iteration unless the state table changes. This behaviour can also be
Packit c4476c
changed; if the ENGINE_TABLE_FLAG_NOINIT flag is set (using
Packit c4476c
ENGINE_set_table_flags()), no attempted initialisations will take place,
Packit c4476c
instead the only way for the state table to return a non-NULL ENGINE to the
Packit c4476c
"get_default" query will be if one is expressly set in the table. Eg.
Packit c4476c
ENGINE_set_default_RSA() does the same job as ENGINE_register_RSA() except
Packit c4476c
that it also sets the state table's cached response for the "get_default"
Packit c4476c
query. In the case of abstractions like EVP_CIPHER, where implementations are
Packit c4476c
indexed by 'nid', these flags and cached-responses are distinct for each 'nid'
Packit c4476c
value.
Packit c4476c
Packit c4476c
=head2 Application requirements
Packit c4476c
Packit c4476c
This section will explain the basic things an application programmer should
Packit c4476c
support to make the most useful elements of the ENGINE functionality
Packit c4476c
available to the user. The first thing to consider is whether the
Packit c4476c
programmer wishes to make alternative ENGINE modules available to the
Packit c4476c
application and user. OpenSSL maintains an internal linked list of
Packit c4476c
"visible" ENGINEs from which it has to operate - at start-up, this list is
Packit c4476c
empty and in fact if an application does not call any ENGINE API calls and
Packit c4476c
it uses static linking against openssl, then the resulting application
Packit c4476c
binary will not contain any alternative ENGINE code at all. So the first
Packit c4476c
consideration is whether any/all available ENGINE implementations should be
Packit c4476c
made visible to OpenSSL - this is controlled by calling the various "load"
Packit c4476c
functions.
Packit c4476c
Packit c4476c
The fact that ENGINEs are made visible to OpenSSL (and thus are linked into
Packit c4476c
the program and loaded into memory at run-time) does not mean they are
Packit c4476c
"registered" or called into use by OpenSSL automatically - that behaviour
Packit c4476c
is something for the application to control. Some applications
Packit c4476c
will want to allow the user to specify exactly which ENGINE they want used
Packit c4476c
if any is to be used at all. Others may prefer to load all support and have
Packit c4476c
OpenSSL automatically use at run-time any ENGINE that is able to
Packit c4476c
successfully initialise - ie. to assume that this corresponds to
Packit c4476c
acceleration hardware attached to the machine or some such thing. There are
Packit c4476c
probably numerous other ways in which applications may prefer to handle
Packit c4476c
things, so we will simply illustrate the consequences as they apply to a
Packit c4476c
couple of simple cases and leave developers to consider these and the
Packit c4476c
source code to openssl's builtin utilities as guides.
Packit c4476c
Packit c4476c
If no ENGINE API functions are called within an application, then OpenSSL
Packit c4476c
will not allocate any internal resources.  Prior to OpenSSL 1.1.0, however,
Packit c4476c
if any ENGINEs are loaded, even if not registered or used, it was necessary to
Packit c4476c
call ENGINE_cleanup() before the program exits.
Packit c4476c
Packit c4476c
I<Using a specific ENGINE implementation>
Packit c4476c
Packit c4476c
Here we'll assume an application has been configured by its user or admin
Packit c4476c
to want to use the "ACME" ENGINE if it is available in the version of
Packit c4476c
OpenSSL the application was compiled with. If it is available, it should be
Packit c4476c
used by default for all RSA, DSA, and symmetric cipher operations, otherwise
Packit c4476c
OpenSSL should use its builtin software as per usual. The following code
Packit c4476c
illustrates how to approach this;
Packit c4476c
Packit c4476c
 ENGINE *e;
Packit c4476c
 const char *engine_id = "ACME";
Packit c4476c
 ENGINE_load_builtin_engines();
Packit c4476c
 e = ENGINE_by_id(engine_id);
Packit c4476c
 if (!e)
Packit c4476c
     /* the engine isn't available */
Packit c4476c
     return;
Packit c4476c
 if (!ENGINE_init(e)) {
Packit c4476c
     /* the engine couldn't initialise, release 'e' */
Packit c4476c
     ENGINE_free(e);
Packit c4476c
     return;
Packit c4476c
 }
Packit c4476c
 if (!ENGINE_set_default_RSA(e))
Packit c4476c
     /*
Packit c4476c
      * This should only happen when 'e' can't initialise, but the previous
Packit c4476c
      * statement suggests it did.
Packit c4476c
      */
Packit c4476c
     abort();
Packit c4476c
 ENGINE_set_default_DSA(e);
Packit c4476c
 ENGINE_set_default_ciphers(e);
Packit c4476c
 /* Release the functional reference from ENGINE_init() */
Packit c4476c
 ENGINE_finish(e);
Packit c4476c
 /* Release the structural reference from ENGINE_by_id() */
Packit c4476c
 ENGINE_free(e);
Packit c4476c
Packit c4476c
I<Automatically using builtin ENGINE implementations>
Packit c4476c
Packit c4476c
Here we'll assume we want to load and register all ENGINE implementations
Packit c4476c
bundled with OpenSSL, such that for any cryptographic algorithm required by
Packit c4476c
OpenSSL - if there is an ENGINE that implements it and can be initialised,
Packit c4476c
it should be used. The following code illustrates how this can work;
Packit c4476c
Packit c4476c
 /* Load all bundled ENGINEs into memory and make them visible */
Packit c4476c
 ENGINE_load_builtin_engines();
Packit c4476c
 /* Register all of them for every algorithm they collectively implement */
Packit c4476c
 ENGINE_register_all_complete();
Packit c4476c
Packit c4476c
That's all that's required. Eg. the next time OpenSSL tries to set up an
Packit c4476c
RSA key, any bundled ENGINEs that implement RSA_METHOD will be passed to
Packit c4476c
ENGINE_init() and if any of those succeed, that ENGINE will be set as the
Packit c4476c
default for RSA use from then on.
Packit c4476c
Packit c4476c
=head2 Advanced configuration support
Packit c4476c
Packit c4476c
There is a mechanism supported by the ENGINE framework that allows each
Packit c4476c
ENGINE implementation to define an arbitrary set of configuration
Packit c4476c
"commands" and expose them to OpenSSL and any applications based on
Packit c4476c
OpenSSL. This mechanism is entirely based on the use of name-value pairs
Packit c4476c
and assumes ASCII input (no unicode or UTF for now!), so it is ideal if
Packit c4476c
applications want to provide a transparent way for users to provide
Packit c4476c
arbitrary configuration "directives" directly to such ENGINEs. It is also
Packit c4476c
possible for the application to dynamically interrogate the loaded ENGINE
Packit c4476c
implementations for the names, descriptions, and input flags of their
Packit c4476c
available "control commands", providing a more flexible configuration
Packit c4476c
scheme. However, if the user is expected to know which ENGINE device he/she
Packit c4476c
is using (in the case of specialised hardware, this goes without saying)
Packit c4476c
then applications may not need to concern themselves with discovering the
Packit c4476c
supported control commands and simply prefer to pass settings into ENGINEs
Packit c4476c
exactly as they are provided by the user.
Packit c4476c
Packit c4476c
Before illustrating how control commands work, it is worth mentioning what
Packit c4476c
they are typically used for. Broadly speaking there are two uses for
Packit c4476c
control commands; the first is to provide the necessary details to the
Packit c4476c
implementation (which may know nothing at all specific to the host system)
Packit c4476c
so that it can be initialised for use. This could include the path to any
Packit c4476c
driver or config files it needs to load, required network addresses,
Packit c4476c
smart-card identifiers, passwords to initialise protected devices,
Packit c4476c
logging information, etc etc. This class of commands typically needs to be
Packit c4476c
passed to an ENGINE B<before> attempting to initialise it, ie. before
Packit c4476c
calling ENGINE_init(). The other class of commands consist of settings or
Packit c4476c
operations that tweak certain behaviour or cause certain operations to take
Packit c4476c
place, and these commands may work either before or after ENGINE_init(), or
Packit c4476c
in some cases both. ENGINE implementations should provide indications of
Packit c4476c
this in the descriptions attached to builtin control commands and/or in
Packit c4476c
external product documentation.
Packit c4476c
Packit c4476c
I<Issuing control commands to an ENGINE>
Packit c4476c
Packit c4476c
Let's illustrate by example; a function for which the caller supplies the
Packit c4476c
name of the ENGINE it wishes to use, a table of string-pairs for use before
Packit c4476c
initialisation, and another table for use after initialisation. Note that
Packit c4476c
the string-pairs used for control commands consist of a command "name"
Packit c4476c
followed by the command "parameter" - the parameter could be NULL in some
Packit c4476c
cases but the name can not. This function should initialise the ENGINE
Packit c4476c
(issuing the "pre" commands beforehand and the "post" commands afterwards)
Packit c4476c
and set it as the default for everything except RAND and then return a
Packit c4476c
boolean success or failure.
Packit c4476c
Packit c4476c
 int generic_load_engine_fn(const char *engine_id,
Packit c4476c
                            const char **pre_cmds, int pre_num,
Packit c4476c
                            const char **post_cmds, int post_num)
Packit c4476c
 {
Packit c4476c
     ENGINE *e = ENGINE_by_id(engine_id);
Packit c4476c
     if (!e) return 0;
Packit c4476c
     while (pre_num--) {
Packit c4476c
         if (!ENGINE_ctrl_cmd_string(e, pre_cmds[0], pre_cmds[1], 0)) {
Packit c4476c
             fprintf(stderr, "Failed command (%s - %s:%s)\n", engine_id,
Packit c4476c
                     pre_cmds[0], pre_cmds[1] ? pre_cmds[1] : "(NULL)");
Packit c4476c
             ENGINE_free(e);
Packit c4476c
             return 0;
Packit c4476c
         }
Packit c4476c
         pre_cmds += 2;
Packit c4476c
     }
Packit c4476c
     if (!ENGINE_init(e)) {
Packit c4476c
         fprintf(stderr, "Failed initialisation\n");
Packit c4476c
         ENGINE_free(e);
Packit c4476c
         return 0;
Packit c4476c
     }
Packit c4476c
     /*
Packit c4476c
      * ENGINE_init() returned a functional reference, so free the structural
Packit c4476c
      * reference from ENGINE_by_id().
Packit c4476c
      */
Packit c4476c
     ENGINE_free(e);
Packit c4476c
     while (post_num--) {
Packit c4476c
         if (!ENGINE_ctrl_cmd_string(e, post_cmds[0], post_cmds[1], 0)) {
Packit c4476c
             fprintf(stderr, "Failed command (%s - %s:%s)\n", engine_id,
Packit c4476c
                     post_cmds[0], post_cmds[1] ? post_cmds[1] : "(NULL)");
Packit c4476c
             ENGINE_finish(e);
Packit c4476c
             return 0;
Packit c4476c
         }
Packit c4476c
         post_cmds += 2;
Packit c4476c
     }
Packit c4476c
     ENGINE_set_default(e, ENGINE_METHOD_ALL & ~ENGINE_METHOD_RAND);
Packit c4476c
     /* Success */
Packit c4476c
     return 1;
Packit c4476c
 }
Packit c4476c
Packit c4476c
Note that ENGINE_ctrl_cmd_string() accepts a boolean argument that can
Packit c4476c
relax the semantics of the function - if set non-zero it will only return
Packit c4476c
failure if the ENGINE supported the given command name but failed while
Packit c4476c
executing it, if the ENGINE doesn't support the command name it will simply
Packit c4476c
return success without doing anything. In this case we assume the user is
Packit c4476c
only supplying commands specific to the given ENGINE so we set this to
Packit c4476c
FALSE.
Packit c4476c
Packit c4476c
I<Discovering supported control commands>
Packit c4476c
Packit c4476c
It is possible to discover at run-time the names, numerical-ids, descriptions
Packit c4476c
and input parameters of the control commands supported by an ENGINE using a
Packit c4476c
structural reference. Note that some control commands are defined by OpenSSL
Packit c4476c
itself and it will intercept and handle these control commands on behalf of the
Packit c4476c
ENGINE, ie. the ENGINE's ctrl() handler is not used for the control command.
Packit c4476c
openssl/engine.h defines an index, ENGINE_CMD_BASE, that all control commands
Packit c4476c
implemented by ENGINEs should be numbered from. Any command value lower than
Packit c4476c
this symbol is considered a "generic" command is handled directly by the
Packit c4476c
OpenSSL core routines.
Packit c4476c
Packit c4476c
It is using these "core" control commands that one can discover the control
Packit c4476c
commands implemented by a given ENGINE, specifically the commands:
Packit c4476c
Packit c4476c
 ENGINE_HAS_CTRL_FUNCTION
Packit c4476c
 ENGINE_CTRL_GET_FIRST_CMD_TYPE
Packit c4476c
 ENGINE_CTRL_GET_NEXT_CMD_TYPE
Packit c4476c
 ENGINE_CTRL_GET_CMD_FROM_NAME
Packit c4476c
 ENGINE_CTRL_GET_NAME_LEN_FROM_CMD
Packit c4476c
 ENGINE_CTRL_GET_NAME_FROM_CMD
Packit c4476c
 ENGINE_CTRL_GET_DESC_LEN_FROM_CMD
Packit c4476c
 ENGINE_CTRL_GET_DESC_FROM_CMD
Packit c4476c
 ENGINE_CTRL_GET_CMD_FLAGS
Packit c4476c
Packit c4476c
Whilst these commands are automatically processed by the OpenSSL framework code,
Packit c4476c
they use various properties exposed by each ENGINE to process these
Packit c4476c
queries. An ENGINE has 3 properties it exposes that can affect how this behaves;
Packit c4476c
it can supply a ctrl() handler, it can specify ENGINE_FLAGS_MANUAL_CMD_CTRL in
Packit c4476c
the ENGINE's flags, and it can expose an array of control command descriptions.
Packit c4476c
If an ENGINE specifies the ENGINE_FLAGS_MANUAL_CMD_CTRL flag, then it will
Packit c4476c
simply pass all these "core" control commands directly to the ENGINE's ctrl()
Packit c4476c
handler (and thus, it must have supplied one), so it is up to the ENGINE to
Packit c4476c
reply to these "discovery" commands itself. If that flag is not set, then the
Packit c4476c
OpenSSL framework code will work with the following rules:
Packit c4476c
Packit c4476c
 if no ctrl() handler supplied;
Packit c4476c
     ENGINE_HAS_CTRL_FUNCTION returns FALSE (zero),
Packit c4476c
     all other commands fail.
Packit c4476c
 if a ctrl() handler was supplied but no array of control commands;
Packit c4476c
     ENGINE_HAS_CTRL_FUNCTION returns TRUE,
Packit c4476c
     all other commands fail.
Packit c4476c
 if a ctrl() handler and array of control commands was supplied;
Packit c4476c
     ENGINE_HAS_CTRL_FUNCTION returns TRUE,
Packit c4476c
     all other commands proceed processing ...
Packit c4476c
Packit c4476c
If the ENGINE's array of control commands is empty then all other commands will
Packit c4476c
fail, otherwise; ENGINE_CTRL_GET_FIRST_CMD_TYPE returns the identifier of
Packit c4476c
the first command supported by the ENGINE, ENGINE_GET_NEXT_CMD_TYPE takes the
Packit c4476c
identifier of a command supported by the ENGINE and returns the next command
Packit c4476c
identifier or fails if there are no more, ENGINE_CMD_FROM_NAME takes a string
Packit c4476c
name for a command and returns the corresponding identifier or fails if no such
Packit c4476c
command name exists, and the remaining commands take a command identifier and
Packit c4476c
return properties of the corresponding commands. All except
Packit c4476c
ENGINE_CTRL_GET_FLAGS return the string length of a command name or description,
Packit c4476c
or populate a supplied character buffer with a copy of the command name or
Packit c4476c
description. ENGINE_CTRL_GET_FLAGS returns a bitwise-OR'd mask of the following
Packit c4476c
possible values:
Packit c4476c
Packit c4476c
 ENGINE_CMD_FLAG_NUMERIC
Packit c4476c
 ENGINE_CMD_FLAG_STRING
Packit c4476c
 ENGINE_CMD_FLAG_NO_INPUT
Packit c4476c
 ENGINE_CMD_FLAG_INTERNAL
Packit c4476c
Packit c4476c
If the ENGINE_CMD_FLAG_INTERNAL flag is set, then any other flags are purely
Packit c4476c
informational to the caller - this flag will prevent the command being usable
Packit c4476c
for any higher-level ENGINE functions such as ENGINE_ctrl_cmd_string().
Packit c4476c
"INTERNAL" commands are not intended to be exposed to text-based configuration
Packit c4476c
by applications, administrations, users, etc. These can support arbitrary
Packit c4476c
operations via ENGINE_ctrl(), including passing to and/or from the control
Packit c4476c
commands data of any arbitrary type. These commands are supported in the
Packit c4476c
discovery mechanisms simply to allow applications to determine if an ENGINE
Packit c4476c
supports certain specific commands it might want to use (eg. application "foo"
Packit c4476c
might query various ENGINEs to see if they implement "FOO_GET_VENDOR_LOGO_GIF" -
Packit c4476c
and ENGINE could therefore decide whether or not to support this "foo"-specific
Packit c4476c
extension).
Packit c4476c
Packit c4476c
=head1 ENVIRONMENT
Packit c4476c
Packit c4476c
=over 4
Packit c4476c
Packit c4476c
=item B<OPENSSL_ENGINES>
Packit c4476c
Packit c4476c
The path to the engines directory.
Packit c4476c
Ignored in set-user-ID and set-group-ID programs.
Packit c4476c
Packit c4476c
=back
Packit c4476c
Packit c4476c
=head1 RETURN VALUES
Packit c4476c
Packit c4476c
ENGINE_get_first(), ENGINE_get_last(), ENGINE_get_next() and ENGINE_get_prev()
Packit c4476c
return a valid B<ENGINE> structure or NULL if an error occurred.
Packit c4476c
Packit c4476c
ENGINE_add() and ENGINE_remove() return 1 on success or 0 on error.
Packit c4476c
Packit c4476c
ENGINE_by_id() returns a valid B<ENGINE> structure or NULL if an error occurred.
Packit c4476c
Packit c4476c
ENGINE_init() and ENGINE_finish() return 1 on success or 0 on error.
Packit c4476c
Packit c4476c
All ENGINE_get_default_TYPE() functions, ENGINE_get_cipher_engine() and
Packit c4476c
ENGINE_get_digest_engine() return a valid B<ENGINE> structure on success or NULL
Packit c4476c
if an error occurred.
Packit c4476c
Packit c4476c
All ENGINE_set_default_TYPE() functions return 1 on success or 0 on error.
Packit c4476c
Packit c4476c
ENGINE_set_default() returns 1 on success or 0 on error.
Packit c4476c
Packit c4476c
ENGINE_get_table_flags() returns an unsigned integer value representing the
Packit c4476c
global table flags which are used to control the registration behaviour of
Packit c4476c
B<ENGINE> implementations.
Packit c4476c
Packit c4476c
All ENGINE_register_TYPE() functions return 1 on success or 0 on error.
Packit c4476c
Packit c4476c
ENGINE_register_complete() and ENGINE_register_all_complete() return 1 on success
Packit c4476c
or 0 on error.
Packit c4476c
Packit c4476c
ENGINE_ctrl() returns a positive value on success or others on error.
Packit c4476c
Packit c4476c
ENGINE_cmd_is_executable() returns 1 if B<cmd> is executable or 0 otherwise.
Packit c4476c
Packit c4476c
ENGINE_ctrl_cmd() and ENGINE_ctrl_cmd_string() return 1 on success or 0 on error.
Packit c4476c
Packit c4476c
ENGINE_new() returns a valid B<ENGINE> structure on success or NULL if an error
Packit c4476c
occurred.
Packit c4476c
Packit c4476c
ENGINE_free() returns 1 on success or 0 on error.
Packit c4476c
Packit c4476c
ENGINE_up_ref() returns 1 on success or 0 on error.
Packit c4476c
Packit c4476c
ENGINE_set_id() and ENGINE_set_name() return 1 on success or 0 on error.
Packit c4476c
Packit c4476c
All other B<ENGINE_set_*> functions return 1 on success or 0 on error.
Packit c4476c
Packit c4476c
ENGINE_get_id() and ENGINE_get_name() return a string representing the identifier
Packit c4476c
and the name of the ENGINE B<e> respectively.
Packit c4476c
Packit c4476c
ENGINE_get_RSA(), ENGINE_get_DSA(), ENGINE_get_DH() and ENGINE_get_RAND()
Packit c4476c
return corresponding method structures for each algorithms.
Packit c4476c
Packit c4476c
ENGINE_get_destroy_function(), ENGINE_get_init_function(),
Packit c4476c
ENGINE_get_finish_function(), ENGINE_get_ctrl_function(),
Packit c4476c
ENGINE_get_load_privkey_function(), ENGINE_get_load_pubkey_function(),
Packit c4476c
ENGINE_get_ciphers() and ENGINE_get_digests() return corresponding function
Packit c4476c
pointers of the callbacks.
Packit c4476c
Packit c4476c
ENGINE_get_cipher() returns a valid B<EVP_CIPHER> structure on success or NULL
Packit c4476c
if an error occurred.
Packit c4476c
Packit c4476c
ENGINE_get_digest() returns a valid B<EVP_MD> structure on success or NULL if an
Packit c4476c
error occurred.
Packit c4476c
Packit c4476c
ENGINE_get_flags() returns an integer representing the ENGINE flags which are
Packit c4476c
used to control various behaviours of an ENGINE.
Packit c4476c
Packit c4476c
ENGINE_get_cmd_defns() returns an B<ENGINE_CMD_DEFN> structure or NULL if it's
Packit c4476c
not set.
Packit c4476c
Packit c4476c
ENGINE_load_private_key() and ENGINE_load_public_key() return a valid B<EVP_PKEY>
Packit c4476c
structure on success or NULL if an error occurred.
Packit c4476c
Packit c4476c
=head1 SEE ALSO
Packit c4476c
Packit c4476c
L<OPENSSL_init_crypto(3)>, L<RSA_new_method(3)>, L<DSA_new(3)>, L<DH_new(3)>,
Packit c4476c
L<RAND_bytes(3)>, L<config(5)>
Packit c4476c
Packit c4476c
=head1 HISTORY
Packit c4476c
Packit c4476c
ENGINE_cleanup() was deprecated in OpenSSL 1.1.0 by the automatic cleanup
Packit c4476c
done by OPENSSL_cleanup()
Packit c4476c
and should not be used.
Packit c4476c
Packit c4476c
=head1 COPYRIGHT
Packit c4476c
Packit c4476c
Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
Packit c4476c
Packit c4476c
Licensed under the OpenSSL license (the "License").  You may not use
Packit c4476c
this file except in compliance with the License.  You can obtain a copy
Packit c4476c
in the file LICENSE in the source distribution or at
Packit c4476c
L<https://www.openssl.org/source/license.html>.
Packit c4476c
Packit c4476c
=cut