engine.pod 29 KB

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