ENGINE_add.pod 31 KB

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