ENGINE_add.pod 31 KB

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