|
HP Open Source Security for OpenVMS Volume 2: HP SSL for OpenVMS > CRYPTO Application
Programming Interface (API) Reference
engine
NAMEengine — ENGINE cryptographic module support Synopsis#include <openssl/engine.h> ENGINE
*ENGINE_get_first(void); ENGINE *ENGINE_get_last(void); ENGINE
*ENGINE_get_next(ENGINE *e); ENGINE *ENGINE_get_prev(ENGINE
*e); int ENGINE_add(ENGINE *e); int ENGINE_remove(ENGINE
*e); ENGINE *ENGINE_by_id(const char *id); int
ENGINE_init(ENGINE *e); int ENGINE_finish(ENGINE *e); void
ENGINE_load_openssl(void); void ENGINE_load_dynamic(void); void
ENGINE_load_cswift(void); void ENGINE_load_chil(void); void
ENGINE_load_atalla(void); void ENGINE_load_nuron(void); void
ENGINE_load_ubsec(void); void ENGINE_load_aep(void); void
ENGINE_load_sureware(void); void ENGINE_load_4758cca(void); void
ENGINE_load_openbsd_dev_crypto(void); void ENGINE_load_builtin_engines(void); void
ENGINE_cleanup(void); ENGINE *ENGINE_get_default_RSA(void); ENGINE
*ENGINE_get_default_DSA(void); ENGINE *ENGINE_get_default_DH(void); ENGINE
*ENGINE_get_default_RAND(void); ENGINE *ENGINE_get_cipher_engine(int
nid); ENGINE *ENGINE_get_digest_engine(int nid); int
ENGINE_set_default_RSA(ENGINE *e); int ENGINE_set_default_DSA(ENGINE
*e); int ENGINE_set_default_DH(ENGINE *e); int
ENGINE_set_default_RAND(ENGINE *e); int ENGINE_set_default_ciphers(ENGINE
*e); int ENGINE_set_default_digests(ENGINE *e); int
ENGINE_set_default_string(ENGINE *e, const char *list); int
ENGINE_set_default(ENGINE *e, unsigned int flags); unsigned
int ENGINE_get_table_flags(void); void ENGINE_set_table_flags(unsigned
int flags); int ENGINE_register_RSA(ENGINE *e); void
ENGINE_unregister_RSA(ENGINE *e); void ENGINE_register_all_RSA(void); int
ENGINE_register_DSA(ENGINE *e); void ENGINE_unregister_DSA(ENGINE
*e); void ENGINE_register_all_DSA(void); int
ENGINE_register_DH(ENGINE *e); void ENGINE_unregister_DH(ENGINE
*e); void ENGINE_register_all_DH(void); int
ENGINE_register_RAND(ENGINE *e); void ENGINE_unregister_RAND(ENGINE
*e); void ENGINE_register_all_RAND(void); int
ENGINE_register_ciphers(ENGINE *e); void ENGINE_unregister_ciphers(ENGINE
*e); void ENGINE_register_all_ciphers(void); int
ENGINE_register_digests(ENGINE *e); void ENGINE_unregister_digests(ENGINE
*e); void ENGINE_register_all_digests(void); int
ENGINE_register_complete(ENGINE *e); int ENGINE_register_all_complete(void); int
ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)()); int
ENGINE_cmd_is_executable(ENGINE *e, int cmd); int ENGINE_ctrl_cmd(ENGINE
*e, const char *cmd_name, long i, void *p, void (*f)(), int cmd_optional); int
ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char
*arg, int cmd_optional); int ENGINE_set_ex_data(ENGINE
*e, int idx, void *arg); void *ENGINE_get_ex_data(const
ENGINE *e, int idx); int ENGINE_get_ex_new_index(long argl,
void *argp, CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, CRYPTO_EX_free
*free_func); ENGINE *ENGINE_new(void); int ENGINE_free(ENGINE
*e); int ENGINE_set_id(ENGINE *e, const char *id); int
ENGINE_set_name(ENGINE *e, const char *name); int ENGINE_set_RSA(ENGINE
*e, const RSA_METHOD *rsa_meth); int ENGINE_set_DSA(ENGINE
*e, const DSA_METHOD *dsa_meth); int ENGINE_set_DH(ENGINE
*e, const DH_METHOD *dh_meth); int ENGINE_set_RAND(ENGINE
*e, const RAND_METHOD *rand_meth); int ENGINE_set_destroy_function(ENGINE
*e, ENGINE_GEN_INT_FUNC_PTR destroy_f); int ENGINE_set_init_function(ENGINE
*e, ENGINE_GEN_INT_FUNC_PTR init_f); int ENGINE_set_finish_function(ENGINE
*e, ENGINE_GEN_INT_FUNC_PTR finish_f); int ENGINE_set_ctrl_function(ENGINE
*e, ENGINE_CTRL_FUNC_PTR ctrl_f); int ENGINE_set_load_privkey_function(ENGINE
*e, ENGINE_LOAD_KEY_PTR loadpriv_f); int ENGINE_set_load_pubkey_function(ENGINE
*e, ENGINE_LOAD_KEY_PTR loadpub_f); int ENGINE_set_ciphers(ENGINE
*e, ENGINE_CIPHERS_PTR f); int ENGINE_set_digests(ENGINE
*e, ENGINE_DIGESTS_PTR f); int ENGINE_set_flags(ENGINE
*e, int flags); int ENGINE_set_cmd_defns(ENGINE *e, const
ENGINE_CMD_DEFN *defns); const char *ENGINE_get_id(const
ENGINE *e); c onst char *ENGINE_get_name(const ENGINE *e); const
RSA_METHOD *ENGINE_get_RSA(const ENGINE *e); const DSA_METHOD
*ENGINE_get_DSA(const ENGINE *e); const DH_METHOD *ENGINE_get_DH(const
ENGINE *e); const RAND_METHOD *ENGINE_get_RAND(const ENGINE
*e); ENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const
ENGINE *e); ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const
ENGINE *e); ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const
ENGINE *e); ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const
ENGINE *e); ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const
ENGINE *e); ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const
ENGINE *e); ENGINE_CIPHERS_PTR ENGINE_get_ciphers(const
ENGINE *e); ENGINE_DIGESTS_PTR ENGINE_get_digests(const
ENGINE *e); const EVP_CIPHER *ENGINE_get_cipher(ENGINE
*e, int nid); const EVP_MD *ENGINE_get_digest(ENGINE *e,
int nid); int ENGINE_get_flags(const ENGINE *e); const
ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e); EVP_PKEY
*ENGINE_load_private_key(ENGINE *e, const char *key_id, UI_METHOD
*ui_method, void *callback_data); EVP_PKEY *ENGINE_load_public_key(ENGINE
*e, const char *key_id, UI_METHOD *ui_method, void *callback_data);
void ENGINE_add_conf_module(void); DESCRIPTIONThese functions create, manipulate, and use cryptographic
modules in the form of ENGINE objects. These objects
act as containers for implementations of cryptographic algorithms,
and support a reference-counted mechanism to allow them to be dynamically
loaded in and out of the running application. The cryptographic functionality that can be provided by an ENGINE implementation
includes the following abstractions; RSA_METHOD - for providing alternative RSA implementations DSA_METHOD, DH_METHOD, RAND_METHOD - alternative DSA, DH, and RAND EVP_CIPHER - potentially multiple cipher algorithms (indexed by 'nid') EVP_DIGEST - potentially multiple hash algorithms (indexed by 'nid') key-loading - loading public and/or private EVP_PKEY keys
|
|
Reference
counting and handlesDue to the modular nature of the ENGINE API, pointers to ENGINEs
need to be treated as handles - ie. not only as pointers, but also
as references to the underlying ENGINE object. Ie. you should obtain
a new reference when making copies of an ENGINE pointer if the copies
will be used (and released) independantly. ENGINE objects have two levels of reference-counting to match
the way in which the objects are used. At the most basic level,
each ENGINE pointer is inherently a structural reference
- you need a structural reference simply to refer to the pointer
value at all, as this kind of reference is your guarantee that the
structure can not be deallocated until you release your reference. However, a structural reference provides no guarantee that
the ENGINE has been initiliased to be usable to perform any of its
cryptographic implementations - and indeed it's quite possible that
most ENGINEs will not initialised at all on standard setups, as
ENGINEs are typically used to support specialised hardware. To use an
ENGINE's functionality, you need a functional 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
independantly. If you have a functional reference to an ENGINE,
you have a guarantee that the ENGINE has been initialised ready
to perform cryptographic operations and will not be uninitialised or
cleaned up until after you have released your reference. We will discuss the two kinds of reference separately, including
how to tell which one you are dealing with at any given point in
time (after all they are both simply (ENGINE *) pointers, the difference
is in the way they are used). Structural references This basic type of reference is typically used for creating
new ENGINEs dynamically, iterating across OpenSSL's internal linked-list
of loaded ENGINEs, reading information about an ENGINE, etc. Essentially
a structural reference is sufficient if you only need to query or
manipulate the data of an ENGINE implementation rather than use
its functionality. The ENGINE_new() function returns a structural reference to
a new (empty) ENGINE object. Other than that, structural references
come from return values to various ENGINE API functions such as; ENGINE_by_id(),
ENGINE_get_first(), ENGINE_get_last(), ENGINE_get_next(), ENGINE_get_prev().
All structural references should be released by a corresponding
to call to the ENGINE_free() function - the ENGINE object itself
will only actually be cleaned up and deallocated when the last structural
reference is released. It should also be noted that many ENGINE API function calls
that accept a structural reference will internally obtain another
reference - typically this happens whenever the supplied ENGINE
will be needed by OpenSSL after the function has returned. Eg. the
function to add a new ENGINE 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 ENGINE_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 ENGINE_get_prev() functions
are used for iterating across the internal ENGINE list - they will
return a new structural reference to the next (or previous) ENGINE
in the list or NULL 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 "man" page,
or failing that the openssl/engine.h header file includes some hints. Functional references As mentioned, functional references exist when the cryptographic
functionality of an ENGINE is required to be available. A functional
reference can be obtained in one of two ways; from an existing structural
reference to the required ENGINE, or by asking OpenSSL for the default
operational ENGINE 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 ENGINE was not already operational and couldn't be successfully
initialised (eg. lack of system drivers, no special hardware attached,
etc), otherwise it will return non-zero to indicate that the ENGINE
is now operational and will have allocated a new functional reference
to the ENGINE. In this case, the supplied ENGINE pointer is, from
the point of the view of the caller, both a structural reference
and a functional reference - so if the caller intends to use it
as a functional reference it should free the structural reference
with ENGINE_free() first. If the caller wishes to use it only as
a structural reference (eg. if the ENGINE_init() call was simply
to test if the ENGINE seems available/online), then it should free
the functional reference; all functional references are released
by the ENGINE_finish() function. The second way to get a functional reference is by asking
OpenSSL for a default implementation for a given task, eg. by ENGINE_get_default_RSA(),
ENGINE_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 RSA, DSA, EVP_CIPHER_CTX,
etc. Default
implementationsFor each supported abstraction, the ENGINE code maintains
an internal table of state to control which implementations are
available for a given abstraction and which should be used by default.
These implementations are registered in the tables separated-out
by an 'nid' index, because abstractions like EVP_CIPHER and EVP_DIGEST
support many distinct algorithms and modes - ENGINEs will support different
numbers and combinations of these. In the case of other abstractions
like RSA, DSA, etc, there is only one "algorithm" so all implementations
implicitly register using the same 'nid' index. ENGINEs can be registered into
these tables to make themselves available for use automatically
by the various abstractions, eg. RSA. For illustrative purposes,
we continue with the RSA example, though all comments apply similarly to
the other abstractions (they each get their own table and linkage
to the corresponding section of openssl code). When a new RSA key is being created, ie. in RSA_new_method(),
a "get_default" call will be made to the ENGINE subsystem to process
the RSA state table and return a functional reference to an initialised ENGINE
whose RSA_METHOD should be used. If no ENGINE should (or can) be
used, it will return NULL and the RSA key will operate with a NULL
ENGINE handle by using the conventional RSA implementation in OpenSSL
(and will from then on behave the way it used to before the ENGINE
API existed - for details see RSA_new_method(3)). Each state table has a flag to note whether it has processed
this "get_default" 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 ENGINE, it will also cache another reference to speed up processing
future queries (without needing to iterate across the table). Likewise,
it will cache a NULL response if no ENGINE 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 ENGINE_TABLE_FLAG_NOINIT
flag is set (using ENGINE_set_table_flags()), no attempted initialisations
will take place, instead the only way for the state table to return
a non-NULL ENGINE to the "get_default" query will be if one is expressly
set in the table. Eg. ENGINE_set_default_RSA() does the same job
as ENGINE_register_RSA() except that it also sets the state table's
cached response for the "get_default" query. In the case of abstractions like EVP_CIPHER, where implementations
are indexed by 'nid', these flags and cached-responses are distinct
for each 'nid' value. É It is worth illustrating the difference between "registration"
of ENGINEs into these per-algorithm state tables and using the alternative
"set_default" functions. The latter handles both "registration"
and also setting the cached "default" ENGINE in each relevant state
table - so registered ENGINEs will only have a chance to be initialised
for use as a default if a default ENGINE wasn't already set for
the same state table. Eg. if ENGINE X supports cipher nids {A,B}
and RSA, ENGINE Y supports ciphers {A} and DSA, and the following
code is executed; ENGINE_register_complete(X); ENGINE_set_default(Y, ENGINE_METHOD_ALL); e1 = ENGINE_get_default_RSA(); e2 = ENGINE_get_cipher_engine(A); e3 = ENGINE_get_cipher_engine(B); e4 = ENGINE_get_default_DSA(); e5 = ENGINE_get_cipher_engine(C);
|
The results would be as follows; assert(e1 == X); assert(e2 == Y); assert(e3 == X); assert(e4 == Y); assert(e5 == NULL);
|
Application
requirementsThis section will explain the basic things an application
programmer should support to make the most useful elements of the
ENGINE functionality available to the user. The first thing to consider
is whether the programmer wishes to make alternative ENGINE modules
available to the application and user. OpenSSL maintains an internal
linked list of "visible" ENGINEs from which it has to operate -
at start-up, this list is empty and in fact if an application does
not call any ENGINE API calls and it uses static linking against openssl,
then the resulting application binary will not contain any alternative
ENGINE code at all. So the first consideration is whether any/all
available ENGINE implementations should be made visible to OpenSSL -
this is controlled by calling the various "load" functions, eg. |
/* Make the "dynamic" ENGINE available */ void ENGINE_load_dynamic(void); /* Make the CryptoSwift hardware acceleration support available */ void ENGINE_load_cswift(void); /* Make support for nCipher's "CHIL" hardware available */ void ENGINE_load_chil(void); ... /* Make ALL ENGINE implementations bundled with OpenSSL available */ void ENGINE_load_builtin_engines(void);
|
Having called any of these functions, ENGINE objects would
have been dynamically allocated and populated with these implementations
and linked into OpenSSL's internal linked list. At this point it
is important to mention an important API function; void ENGINE_cleanup(void);
|
If no ENGINE API functions are called at all in an application,
then there are no inherent memory leaks to worry about from the
ENGINE functionality, however if any ENGINEs are "load"ed, even
if they are never registered or used, it is necessary to use the
ENGINE_cleanup() function to correspondingly cleanup before program
exit, if the caller wishes to avoid memory leaks. This mechanism
uses an internal callback registration table so that any ENGINE
API functionality that knows it requires cleanup can register its cleanup
details to be called during ENGINE_cleanup(). This approach allows
ENGINE_cleanup() to clean up after any ENGINE functionality at all
that your program uses, yet doesn't automatically create linker dependencies
to all possible ENGINE functionality - only the cleanup callbacks
required by the functionality you do use will be required by the
linker. 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 "registered" or called into use by OpenSSL
automatically - that behaviour is something for the application
to have control over. Some applications will want to allow the user to
specify exactly which ENGINE 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 ENGINE that is able to successfully initialise -
ie. 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. Using a specific ENGINE implementation Here we'll assume an application has been configured by its
user or admin to want to use the "ACME" ENGINE 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 RSA, DSA,
and symmetric cipher operation, otherwise OpenSSL should use its
builtin software as per usual. The following code illustrates how
to approach this; |
ENGINE *e; const char *engine_id = "ACME"; ENGINE_load_builtin_engines(); e = ENGINE_by_id(engine_id); if(!e) /* the engine isn't available */ return; if(!ENGINE_init(e)) { /* the engine couldn't initialise, release 'e' */ ENGINE_free(e); return; } if(!ENGINE_set_default_RSA(e)) /* This should only happen when 'e' can't 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);
|
|
|