ENGINE_ADD(3ossl)

🌸 NAME 🌸

ENGINE_ADD(3ossl) is the OpenSSL manual page bestie, and it’s all about ENGINE cryptographic module support. πŸ’…

The whole function squad named here, in order, no skips:

  • ENGINE_get_DH, ENGINE_get_DSA, ENGINE_by_id, ENGINE_get_cipher_engine, ENGINE_get_default_DH, ENGINE_get_default_DSA, ENGINE_get_default_RAND, ENGINE_get_default_RSA, ENGINE_get_digest_engine, ENGINE_get_first, ENGINE_get_last, ENGINE_get_next, ENGINE_get_prev, ENGINE_new, ENGINE_get_ciphers, ENGINE_get_ctrl_function, ENGINE_get_digests, ENGINE_get_destroy_function, ENGINE_get_finish_function, ENGINE_get_init_function, ENGINE_get_load_privkey_function, ENGINE_get_load_pubkey_function, ENGINE_load_private_key, ENGINE_load_public_key, ENGINE_get_RAND, ENGINE_get_RSA, ENGINE_get_id, ENGINE_get_name, ENGINE_get_cmd_defns, ENGINE_get_cipher
  • ENGINE_get_digest, ENGINE_add, ENGINE_cmd_is_executable, ENGINE_ctrl, ENGINE_ctrl_cmd, ENGINE_ctrl_cmd_string, ENGINE_finish, ENGINE_free, ENGINE_get_flags, ENGINE_init, ENGINE_register_DH, ENGINE_register_DSA, ENGINE_register_RAND, ENGINE_register_RSA, ENGINE_register_all_complete, ENGINE_register_ciphers, ENGINE_register_complete, ENGINE_register_digests, ENGINE_remove, ENGINE_set_DH, ENGINE_set_DSA, ENGINE_set_RAND, ENGINE_set_RSA, ENGINE_set_ciphers, ENGINE_set_cmd_defns, ENGINE_set_ctrl_function, ENGINE_set_default, ENGINE_set_default_DH, ENGINE_set_default_DSA, ENGINE_set_default_RAND
  • ENGINE_set_default_RSA, ENGINE_set_default_ciphers, ENGINE_set_default_digests, ENGINE_set_default_string, ENGINE_set_destroy_function, ENGINE_set_digests, ENGINE_set_finish_function, ENGINE_set_flags, ENGINE_set_id, ENGINE_set_init_function, ENGINE_set_load_privkey_function, ENGINE_set_load_pubkey_function, ENGINE_set_name, ENGINE_up_ref, ENGINE_get_table_flags, ENGINE_cleanup, ENGINE_load_builtin_engines, ENGINE_register_all_DH, ENGINE_register_all_DSA, ENGINE_register_all_RAND, ENGINE_register_all_RSA, ENGINE_register_all_ciphers, ENGINE_register_all_digests, ENGINE_set_table_flags, ENGINE_unregister_DH, ENGINE_unregister_DSA, ENGINE_unregister_RAND, ENGINE_unregister_RSA, ENGINE_unregister_ciphers, ENGINE_unregister_digests

So yeah β€” ENGINE cryptographic module support, the whole iconic cast. πŸ‘‘


✨ SYNOPSIS ✨

Start with the include, bestie:

#include <openssl/engine.h>

The following functions have been deprecated since OpenSSL 3.0, and can be hidden entirely by defining OPENSSL_API_COMPAT with a suitable version value, see openssl_user_macros(7):

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_builtin_engines(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)(void));
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)(void), int cmd_optional);
int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
                           int cmd_optional);

ENGINE *ENGINE_new(void);
int ENGINE_free(ENGINE *e);
int ENGINE_up_ref(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);
const 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);

The following function has been deprecated since OpenSSL 1.1.0, and can be hidden entirely by defining OPENSSL_API_COMPAT with a suitable version value, see openssl_user_macros(7):

void ENGINE_cleanup(void);

πŸ’– DESCRIPTION πŸ’–

⚠️ Okay so real talk first: all of the functions described on this page are deprecated. Applications should instead use the provider APIs. That’s the big vibe check. 🫢

These 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, ECDH_METHOD, ECDSA_METHOD, β€” similarly for other OpenSSL APIs
  • 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 handles 🫢

Due to the modular nature of the ENGINE API, pointers to ENGINEs need to be treated as handles β€” i.e. not only as pointers, but also as references to the underlying ENGINE object. Ie. one should obtain a new reference when making copies of an ENGINE pointer if the copies will be used (and released) independently. πŸ’…

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 β€” 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 ENGINE 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 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 independently. If you have a functional reference to an ENGINE, you have a guarantee that the ENGINE has been initialised and is ready to perform cryptographic operations, and will remain initialised until after you have released your reference.

Structural 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 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. There are other ENGINE API functions that return structural references 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. If the argument to ENGINE_free() is NULL, nothing is done. πŸ¦‹

okay so before we go any further we have to talk about references, because this is genuinely the part where everyone gets confused and I refuse to let that be you 🫢

so. lots of ENGINE API function calls accept a structural reference, right? and ngl, a bunch of them will internally go and grab another reference β€” this typically happens whenever the supplied ENGINE is gonna be needed by OpenSSL after the function has already returned. Example incoming: the function that adds a new ENGINE to OpenSSL's internal list is ENGINE_add() β€” if it returns success, then OpenSSL has now stored a new structural reference internally, meaning the caller is still responsible for freeing their own reference with ENGINE_free() once they're finished with it. you don't get out of it, bestie. ✨

and in a similar way, some functions will just automatically release the structural reference you passed them, because releasing it is literally part of the function's job. Example: ENGINE_get_next() and ENGINE_get_prev() are the ones you use for iterating across the internal ENGINE list β€” they return a new structural reference to the next (or previous) ENGINE in the list, or NULL if you're at the end (or the beginning) of the list. But in either case, the structural reference passed to the function is released on behalf of the caller. so do not free it again, that is a bad time πŸ’€

to figure out how a particular function handles references, always go read that function's own documentation "man" page β€” or if that fails you, the <openssl/engine.h> header file includes some hints. iykyk πŸŽ€


🌈 Functional references 🌈

like we said, functional references exist when the actual cryptographic functionality of an ENGINE is required to be available. and you can get one in exactly two ways:

  1. from an existing structural reference to the ENGINE you want, or
  2. by asking OpenSSL for the default operational ENGINE for a given cryptographic purpose.

Route one β€” grabbing a functional reference from an existing structural reference β€” is just calling ENGINE_init(). Here's the vibe check on what it returns:

  • it returns zero if the ENGINE was not already operational and couldn't be successfully initialised (e.g. lack of system drivers, no special hardware attached, etc 😭)
  • otherwise it returns nonzero, which means the ENGINE is now operational and a new functional reference to it has been allocated πŸ’–

all functional references are released by calling ENGINE_finish() β€” and heads up, that also removes the implicit structural reference as well. package deal, no splitting. 🫢

Route two β€” the default-implementation route β€” is asking OpenSSL for a default implementation for a given task, e.g. by ENGINE_get_default_RSA(), ENGINE_get_default_cipher_engine(), etc. those get discussed in the next section, though application programmers don't usually need them directly, because they're used automatically when you create and use the relevant algorithm-specific types in OpenSSL β€” things like RSA, DSA, EVP_CIPHER_CTX, etc. so OpenSSL is doing it for you behind the scenes, like a good friend who orders for you when you're shy πŸ“


πŸ‘‘ Default implementations πŸ‘‘

for each supported abstraction, the ENGINE code keeps an internal table of state controlling which implementations are available for that abstraction and which one should be used by default. these implementations are registered in the tables and indexed by a 'nid' value β€” and there's a reason for that: abstractions like EVP_CIPHER and EVP_DIGEST support many distinct algorithms and modes, and ENGINEs can support arbitrarily many of them, so you need the index. for other abstractions like RSA, DSA, etc, there's only one "algorithm", so all implementations implicitly register using the same 'nid' index. one index, one algorithm, no drama. ✨

when a default ENGINE is requested for a given abstraction/algorithm/mode β€” e.g. when calling RSA_new_method(NULL) β€” a "get_default" call gets made to the ENGINE subsystem. it processes the corresponding state table and returns a functional reference to an initialised ENGINE whose implementation should be used. and if no ENGINE should (or can) be used? it returns NULL, and the caller just operates with a NULL ENGINE handle β€” which usually equates to using the conventional software implementation. in that latter case, OpenSSL will from then on behave the way it used to before the ENGINE API even existed. retro era, no cap πŸ’…

every state table has a flag noting whether it has already processed this "get_default" query since the table was last modified β€” and it needs that flag, because to answer the question it has to iterate across all the registered ENGINEs in the table, trying to initialise each of them in turn, in case one of them turns out to be operational. so:

  • if it returns a functional reference to an ENGINE, it will also cache another reference, to speed up processing future queries (no more iterating across the table, slay ⚑)
  • likewise, it will cache a NULL response if no ENGINE was available, so future queries won't repeat that same iteration β€” unless the state table changes, obviously.

and this behaviour can also be changed! if the ENGINE_TABLE_FLAG_NOINIT flag is set (using ENGINE_set_table_flags()), then no attempted initialisations will take place at all. instead, the only way the state table can return a non-NULL ENGINE to the "get_default" query is if one is expressly set in the table. e.g. ENGINE_set_default_RSA() does the same job as ENGINE_register_RSA() β€” except it also sets the state table's cached response for the "get_default" query. and for abstractions like EVP_CIPHER, where implementations are indexed by 'nid', these flags and cached-responses are distinct for each 'nid' value. one vibe per nid, basically 🌸


πŸŽ€ Application requirements πŸŽ€

this section is the "what should you, the application programmer, actually support" section β€” the goal being to make the most useful elements of ENGINE functionality available to the user. 🫢

the first thing to consider is whether the programmer wants to make alternative ENGINE modules available to the application and the user. here's the thing: OpenSSL maintains an internal linked list of "visible" ENGINEs that it has to operate from β€” and at start-up, that list is empty. and in fact, if an application doesn't call any ENGINE API calls and it uses static linking against openssl, then the resulting application binary won't contain any alternative ENGINE code at all. zero. none. not a crumb. πŸ’€

so the first consideration is: should any/all available ENGINE implementations be made visible to OpenSSL? that's controlled by calling the various "load" functions. ⭐

now β€” making ENGINEs visible to OpenSSL (and therefore linking them into the program and loading them into memory at run-time) does not mean they're "registered" or called into use by OpenSSL automatically. that behaviour is something for the application to control. and different apps have different opinions:

  • some applications want to let the user 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's able to successfully initialise β€” i.e. assuming that corresponds to acceleration hardware attached to the machine or some such thing

there are probably a zillion other ways applications may prefer to handle things, so the page simply illustrates the consequences as they apply to a couple of simple cases, and leaves developers to consider these plus the source code to openssl's built-in utilities as guides. do your homework bestie, it's giving "read the room and read the source" πŸ“š

one more thing: if no ENGINE API functions are called within an application, then OpenSSL will not allocate any internal resources. however β€” and this is a big however β€” prior to OpenSSL 1.1.0, if any ENGINEs were loaded, even if they were not registered or used, it was necessary to call ENGINE_cleanup() before the program exits. so like, know your version πŸ™ˆ


🌈 Using a specific ENGINE implementation 🌈

here we're assuming an application has been configured by its user or admin to want to use the "ACME" ENGINE, if it's available in the version of OpenSSL the application was compiled with. if it's available, it should be used by default for all RSA, DSA, and symmetric cipher operations β€” otherwise, OpenSSL should just use its built-in 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);

like, look at those last two comments β€” finish the functional reference, free the structural reference. two different references, two different cleanups. that's the whole personality of this API, honestly 😭


πŸ¦‹ Automatically using built-in ENGINE implementations πŸ¦‹

here we're assuming we want to load and register all ENGINE implementations bundled with OpenSSL, such that for any cryptographic algorithm required by OpenSSL β€” if there's an ENGINE that implements it and can be initialised β€” it should be used. the following code illustrates how this can work:

/* 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();

and that is literally all that's required. iconic, minimal, gorgeous. ✨

so e.g. the next time OpenSSL tries to set up an RSA key, any bundled ENGINEs that implement RSA_METHOD will be passed to ENGINE_init(), and if any of those succeed, that ENGINE will be set as the default for RSA use from then on. automatic. no extra calls. we love a self-starting main character πŸ‘‘


πŸ’œ Advanced configuration support πŸ’œ

there's a mechanism supported by the ENGINE framework that lets each ENGINE implementation define an arbitrary set of configuration "commands" and expose them to OpenSSL (and any applications built on OpenSSL). this mechanism is entirely based on name-value pairs and assumes ASCII input β€” no unicode or UTF for now! so it's ideal if applications want to give users a transparent way to hand arbitrary configuration "directives" directly to such ENGINEs. πŸ’…

it's also possible for the application to dynamically interrogate the loaded ENGINE implementations for the names, descriptions, and input flags of their available "control commands", which gives you a more flexible configuration scheme. fancy. πŸŽ€

however β€” if the user is expected to know which ENGINE device they're using (and in the case of specialised hardware, this goes without saying), then applications may not need to bother discovering the supported control commands at all. they can simply prefer to pass settings into ENGINEs exactly as they are provided by the user. sometimes "just forward the user's string" is the whole strategy and honestly? valid 🌸


Before we get into how control commands actually work, bestie, let's talk about what they're even for πŸ’… 'cause there are lowkey two whole flavours:

  • The first kind is giving "here's everything you need to know so I can exist" β€” supplying the implementation with the details it needs so it can be initialised (and remember, the implementation might know nothing at all specific to the host system). That's stuff like 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 ENGINE before you attempt to initialise it β€” like, before calling ENGINE_init().
  • The second kind is more "vibe tweaks and actions" β€” settings or operations that tweak certain behaviour or cause certain operations to take place. These may work either before or after ENGINE_init(), or in some cases both. ENGINE implementations should provide indications of this in the descriptions attached to built-in control commands and/or in external product documentation. πŸ‘‘

πŸŽ€ Issuing control commands to an ENGINE

Okay, let's illustrate by example. Picture a function where the caller supplies:

  • the name of the ENGINE 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 "name" followed by the command "parameter" β€” the parameter could be NULL in some cases but the name can not. This function should initialise the ENGINE (issuing the "pre" commands beforehand and the "post" commands afterwards), set it as the default for everything except RAND, and then return a boolean success or failure:

 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)\n", 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\n");
         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)\n", 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;
 }

Slight glow-up moment: ENGINE_ctrl_cmd_string() accepts a boolean argument that can relax the semantics of the function ✨ If it's set nonzero, it will only return failure if the ENGINE supported the given command name but failed while executing it β€” and if the ENGINE doesn't support the command name it'll simply return success without doing anything. In this case we assume the user is only supplying commands specific to the given ENGINE, so we set this to FALSE. 🫢


πŸ¦‹ Discovering 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 ENGINE β€” 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 ENGINE, i.e. the ENGINE's ctrl() handler is not used for the control command. <openssl/engine.h> defines an index, ENGINE_CMD_BASE, that all control commands implemented by ENGINEs should be numbered from. Any command value lower than this symbol is considered a "generic" command and is handled directly by the OpenSSL core routines. πŸ’€ (iykyk β€” don't go numbering below the base, bestie.)

It's by using these "core" control commands that one can discover the control commands implemented by a given ENGINE β€” specifically these ones:

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

Whilst these commands are automatically processed by the OpenSSL framework code, they use various properties exposed by each ENGINE to process these queries. An ENGINE has 3 properties it exposes that can affect how this behaves:

  1. it can supply a ctrl() handler,
  2. it can specify ENGINE_FLAGS_MANUAL_CMD_CTRL in the ENGINE's flags,
  3. and it can expose an array of control command descriptions.

If an ENGINE specifies the ENGINE_FLAGS_MANUAL_CMD_CTRL flag, then it will simply pass all these "core" control commands directly to the ENGINE's ctrl() handler (and thus, it must have supplied one), so it is up to the ENGINE to reply to these "discovery" commands itself. If that flag is not set, then the OpenSSL framework code will work with the following rules:

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 ...

If the ENGINE's array of control commands is empty then all other commands will fail, otherwise:

  • ENGINE_CTRL_GET_FIRST_CMD_TYPE returns the identifier of the first command supported by the ENGINE,
  • ENGINE_GET_NEXT_CMD_TYPE takes the identifier of a command supported by the ENGINE and returns the next command identifier, or fails if there are no more,
  • ENGINE_CMD_FROM_NAME 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 ENGINE_CTRL_GET_FLAGS 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. ENGINE_CTRL_GET_FLAGS returns a bitwise-OR'd mask of the following possible values:

ENGINE_CMD_FLAG_NUMERIC
ENGINE_CMD_FLAG_STRING
ENGINE_CMD_FLAG_NO_INPUT
ENGINE_CMD_FLAG_INTERNAL
🚨 If the ENGINE_CMD_FLAG_INTERNAL 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 ENGINE functions such as ENGINE_ctrl_cmd_string(). "INTERNAL" 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 ENGINE supports certain specific commands it might want to use β€” e.g. application "foo" might query various ENGINEs to see if they implement "FOO_GET_VENDOR_LOGO_GIF", and the ENGINE could therefore decide whether or not to support this "foo"-specific extension. 🧚

🌈 ENVIRONMENT

  • OPENSSL_ENGINES β€” the path to the engines directory. Ignored in set-user-ID and set-group-ID programs. πŸ™ˆ

⭐ RETURN VALUES

Okay, buckle up, this is the whole roster πŸ’–

  • ENGINE_get_first(), ENGINE_get_last(), ENGINE_get_next() and ENGINE_get_prev() return a valid ENGINE structure or NULL if an error occurred.
  • ENGINE_add() and ENGINE_remove() return 1 on success or 0 on error.
  • ENGINE_by_id() returns a valid ENGINE structure or NULL if an error occurred.
  • ENGINE_init() and ENGINE_finish() return 1 on success or 0 on error.
  • All ENGINE_get_default_TYPE() functions, ENGINE_get_cipher_engine() and ENGINE_get_digest_engine() return a valid ENGINE structure on success or NULL if an error occurred.
  • All ENGINE_set_default_TYPE() functions return 1 on success or 0 on error.
  • ENGINE_set_default() returns 1 on success or 0 on error.
  • ENGINE_get_table_flags() returns an unsigned integer value representing the global table flags which are used to control the registration behaviour of ENGINE implementations.
  • All ENGINE_register_TYPE() functions return 1 on success or 0 on error.
  • ENGINE_register_complete() and ENGINE_register_all_complete() always return 1. (slay, no notes πŸ‘‘)
  • ENGINE_ctrl() returns a positive value on success or others on error.
  • ENGINE_cmd_is_executable() returns 1 if cmd is executable or 0 otherwise.
  • ENGINE_ctrl_cmd() and ENGINE_ctrl_cmd_string() return 1 on success or 0 on error.
  • ENGINE_new() returns a valid ENGINE structure on success or NULL if an error occurred.
  • ENGINE_free() always returns 1.
  • ENGINE_up_ref() returns 1 on success or 0 on error.
  • ENGINE_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.
  • ENGINE_get_id() and ENGINE_get_name() return a string representing the identifier and the name of the ENGINE e respectively.
  • ENGINE_get_RSA(), ENGINE_get_DSA(), ENGINE_get_DH() and ENGINE_get_RAND() return corresponding method structures for each algorithms.
  • ENGINE_get_destroy_function(), ENGINE_get_init_function(), ENGINE_get_finish_function(), ENGINE_get_ctrl_function(), ENGINE_get_load_privkey_function(), ENGINE_get_load_pubkey_function(), ENGINE_get_ciphers() and ENGINE_get_digests() return corresponding function pointers of the callbacks.
  • ENGINE_get_cipher() returns a valid EVP_CIPHER structure on success or NULL if an error occurred.
  • ENGINE_get_digest() returns a valid EVP_MD structure on success or NULL if an error occurred.
  • ENGINE_get_flags() returns an integer representing the ENGINE flags which are used to control various behaviours of an ENGINE.
  • ENGINE_get_cmd_defns() returns an ENGINE_CMD_DEFN structure or NULL if it's not set.
  • ENGINE_load_private_key() and ENGINE_load_public_key() return a valid EVP_PKEY structure on success or NULL if an error occurred. πŸ”₯

πŸ’œ SEE ALSO

OPENSSL_init_crypto(3), RSA_new_method(3), DSA_new(3), DH_new(3), RAND_bytes(3), config(5) πŸ¦‹

πŸ’– HISTORY πŸ’–

Okay so story time, bestie 😭 β€” literally all of these functions were deprecated in OpenSSL 3.0. The whole squad. No cap.

And ENGINE_cleanup()? That one got deprecated even earlier, in OpenSSL 1.1.0, because the automatic cleanup done by OPENSSL_cleanup() handles it now. So you should not use it. Like, please don't β€” it's giving retired icon πŸ’….


Copyright 2002-2024 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at https://www.openssl.org/source/license.html.


That's the whole page, bestie 🫢 β€” footer vibes: OpenSSL 3.5.9, dated 2026-09-29, manual page ENGINE_ADD(3ossl). Mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from