Initial import from FreeBSD RELENG_4:
[dragonfly.git] / secure / lib / libcrypto / man / engine.3
CommitLineData
984263bc
MD
1.\" Automatically generated by Pod::Man version 1.15
2.\" Wed Feb 19 16:43:04 2003
3.\"
4.\" Standard preamble:
5.\" ======================================================================
6.de Sh \" Subsection heading
7.br
8.if t .Sp
9.ne 5
10.PP
11\fB\\$1\fR
12.PP
13..
14.de Sp \" Vertical space (when we can't use .PP)
15.if t .sp .5v
16.if n .sp
17..
18.de Ip \" List item
19.br
20.ie \\n(.$>=3 .ne \\$3
21.el .ne 3
22.IP "\\$1" \\$2
23..
24.de Vb \" Begin verbatim text
25.ft CW
26.nf
27.ne \\$1
28..
29.de Ve \" End verbatim text
30.ft R
31
32.fi
33..
34.\" Set up some character translations and predefined strings. \*(-- will
35.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
36.\" double quote, and \*(R" will give a right double quote. | will give a
37.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used
38.\" to do unbreakable dashes and therefore won't be available. \*(C` and
39.\" \*(C' expand to `' in nroff, nothing in troff, for use with C<>
40.tr \(*W-|\(bv\*(Tr
41.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
42.ie n \{\
43. ds -- \(*W-
44. ds PI pi
45. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
46. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
47. ds L" ""
48. ds R" ""
49. ds C` ""
50. ds C' ""
51'br\}
52.el\{\
53. ds -- \|\(em\|
54. ds PI \(*p
55. ds L" ``
56. ds R" ''
57'br\}
58.\"
59.\" If the F register is turned on, we'll generate index entries on stderr
60.\" for titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and
61.\" index entries marked with X<> in POD. Of course, you'll have to process
62.\" the output yourself in some meaningful fashion.
63.if \nF \{\
64. de IX
65. tm Index:\\$1\t\\n%\t"\\$2"
66..
67. nr % 0
68. rr F
69.\}
70.\"
71.\" For nroff, turn off justification. Always turn off hyphenation; it
72.\" makes way too many mistakes in technical documents.
73.hy 0
74.if n .na
75.\"
76.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
77.\" Fear. Run. Save yourself. No user-serviceable parts.
78.bd B 3
79. \" fudge factors for nroff and troff
80.if n \{\
81. ds #H 0
82. ds #V .8m
83. ds #F .3m
84. ds #[ \f1
85. ds #] \fP
86.\}
87.if t \{\
88. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
89. ds #V .6m
90. ds #F 0
91. ds #[ \&
92. ds #] \&
93.\}
94. \" simple accents for nroff and troff
95.if n \{\
96. ds ' \&
97. ds ` \&
98. ds ^ \&
99. ds , \&
100. ds ~ ~
101. ds /
102.\}
103.if t \{\
104. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
105. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
106. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
107. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
108. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
109. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
110.\}
111. \" troff and (daisy-wheel) nroff accents
112.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
113.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
114.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
115.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
116.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
117.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
118.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
119.ds ae a\h'-(\w'a'u*4/10)'e
120.ds Ae A\h'-(\w'A'u*4/10)'E
121. \" corrections for vroff
122.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
123.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
124. \" for low resolution devices (crt and lpr)
125.if \n(.H>23 .if \n(.V>19 \
126\{\
127. ds : e
128. ds 8 ss
129. ds o a
130. ds d- d\h'-1'\(ga
131. ds D- D\h'-1'\(hy
132. ds th \o'bp'
133. ds Th \o'LP'
134. ds ae ae
135. ds Ae AE
136.\}
137.rm #[ #] #H #V #F C
138.\" ======================================================================
139.\"
140.IX Title "engine 3"
141.TH engine 3 "0.9.7a" "2003-02-19" "OpenSSL"
142.UC
143.SH "NAME"
144engine \- \s-1ENGINE\s0 cryptographic module support
145.SH "SYNOPSIS"
146.IX Header "SYNOPSIS"
147.Vb 1
148\& #include <openssl/engine.h>
149.Ve
150.Vb 4
151\& ENGINE *ENGINE_get_first(void);
152\& ENGINE *ENGINE_get_last(void);
153\& ENGINE *ENGINE_get_next(ENGINE *e);
154\& ENGINE *ENGINE_get_prev(ENGINE *e);
155.Ve
156.Vb 2
157\& int ENGINE_add(ENGINE *e);
158\& int ENGINE_remove(ENGINE *e);
159.Ve
160.Vb 1
161\& ENGINE *ENGINE_by_id(const char *id);
162.Ve
163.Vb 2
164\& int ENGINE_init(ENGINE *e);
165\& int ENGINE_finish(ENGINE *e);
166.Ve
167.Vb 12
168\& void ENGINE_load_openssl(void);
169\& void ENGINE_load_dynamic(void);
170\& void ENGINE_load_cswift(void);
171\& void ENGINE_load_chil(void);
172\& void ENGINE_load_atalla(void);
173\& void ENGINE_load_nuron(void);
174\& void ENGINE_load_ubsec(void);
175\& void ENGINE_load_aep(void);
176\& void ENGINE_load_sureware(void);
177\& void ENGINE_load_4758cca(void);
178\& void ENGINE_load_openbsd_dev_crypto(void);
179\& void ENGINE_load_builtin_engines(void);
180.Ve
181.Vb 1
182\& void ENGINE_cleanup(void);
183.Ve
184.Vb 6
185\& ENGINE *ENGINE_get_default_RSA(void);
186\& ENGINE *ENGINE_get_default_DSA(void);
187\& ENGINE *ENGINE_get_default_DH(void);
188\& ENGINE *ENGINE_get_default_RAND(void);
189\& ENGINE *ENGINE_get_cipher_engine(int nid);
190\& ENGINE *ENGINE_get_digest_engine(int nid);
191.Ve
192.Vb 7
193\& int ENGINE_set_default_RSA(ENGINE *e);
194\& int ENGINE_set_default_DSA(ENGINE *e);
195\& int ENGINE_set_default_DH(ENGINE *e);
196\& int ENGINE_set_default_RAND(ENGINE *e);
197\& int ENGINE_set_default_ciphers(ENGINE *e);
198\& int ENGINE_set_default_digests(ENGINE *e);
199\& int ENGINE_set_default_string(ENGINE *e, const char *list);
200.Ve
201.Vb 1
202\& int ENGINE_set_default(ENGINE *e, unsigned int flags);
203.Ve
204.Vb 2
205\& unsigned int ENGINE_get_table_flags(void);
206\& void ENGINE_set_table_flags(unsigned int flags);
207.Ve
208.Vb 20
209\& int ENGINE_register_RSA(ENGINE *e);
210\& void ENGINE_unregister_RSA(ENGINE *e);
211\& void ENGINE_register_all_RSA(void);
212\& int ENGINE_register_DSA(ENGINE *e);
213\& void ENGINE_unregister_DSA(ENGINE *e);
214\& void ENGINE_register_all_DSA(void);
215\& int ENGINE_register_DH(ENGINE *e);
216\& void ENGINE_unregister_DH(ENGINE *e);
217\& void ENGINE_register_all_DH(void);
218\& int ENGINE_register_RAND(ENGINE *e);
219\& void ENGINE_unregister_RAND(ENGINE *e);
220\& void ENGINE_register_all_RAND(void);
221\& int ENGINE_register_ciphers(ENGINE *e);
222\& void ENGINE_unregister_ciphers(ENGINE *e);
223\& void ENGINE_register_all_ciphers(void);
224\& int ENGINE_register_digests(ENGINE *e);
225\& void ENGINE_unregister_digests(ENGINE *e);
226\& void ENGINE_register_all_digests(void);
227\& int ENGINE_register_complete(ENGINE *e);
228\& int ENGINE_register_all_complete(void);
229.Ve
230.Vb 6
231\& int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
232\& int ENGINE_cmd_is_executable(ENGINE *e, int cmd);
233\& int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
234\& long i, void *p, void (*f)(), int cmd_optional);
235\& int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
236\& int cmd_optional);
237.Ve
238.Vb 2
239\& int ENGINE_set_ex_data(ENGINE *e, int idx, void *arg);
240\& void *ENGINE_get_ex_data(const ENGINE *e, int idx);
241.Ve
242.Vb 2
243\& int ENGINE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
244\& CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
245.Ve
246.Vb 2
247\& ENGINE *ENGINE_new(void);
248\& int ENGINE_free(ENGINE *e);
249.Ve
250.Vb 16
251\& int ENGINE_set_id(ENGINE *e, const char *id);
252\& int ENGINE_set_name(ENGINE *e, const char *name);
253\& int ENGINE_set_RSA(ENGINE *e, const RSA_METHOD *rsa_meth);
254\& int ENGINE_set_DSA(ENGINE *e, const DSA_METHOD *dsa_meth);
255\& int ENGINE_set_DH(ENGINE *e, const DH_METHOD *dh_meth);
256\& int ENGINE_set_RAND(ENGINE *e, const RAND_METHOD *rand_meth);
257\& int ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f);
258\& int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f);
259\& int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f);
260\& int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f);
261\& int ENGINE_set_load_privkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpriv_f);
262\& int ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f);
263\& int ENGINE_set_ciphers(ENGINE *e, ENGINE_CIPHERS_PTR f);
264\& int ENGINE_set_digests(ENGINE *e, ENGINE_DIGESTS_PTR f);
265\& int ENGINE_set_flags(ENGINE *e, int flags);
266\& int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns);
267.Ve
268.Vb 18
269\& const char *ENGINE_get_id(const ENGINE *e);
270\& const char *ENGINE_get_name(const ENGINE *e);
271\& const RSA_METHOD *ENGINE_get_RSA(const ENGINE *e);
272\& const DSA_METHOD *ENGINE_get_DSA(const ENGINE *e);
273\& const DH_METHOD *ENGINE_get_DH(const ENGINE *e);
274\& const RAND_METHOD *ENGINE_get_RAND(const ENGINE *e);
275\& ENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e);
276\& ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e);
277\& ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e);
278\& ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e);
279\& ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e);
280\& ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e);
281\& ENGINE_CIPHERS_PTR ENGINE_get_ciphers(const ENGINE *e);
282\& ENGINE_DIGESTS_PTR ENGINE_get_digests(const ENGINE *e);
283\& const EVP_CIPHER *ENGINE_get_cipher(ENGINE *e, int nid);
284\& const EVP_MD *ENGINE_get_digest(ENGINE *e, int nid);
285\& int ENGINE_get_flags(const ENGINE *e);
286\& const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e);
287.Ve
288.Vb 4
289\& EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
290\& UI_METHOD *ui_method, void *callback_data);
291\& EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
292\& UI_METHOD *ui_method, void *callback_data);
293.Ve
294.Vb 1
295\& void ENGINE_add_conf_module(void);
296.Ve
297.SH "DESCRIPTION"
298.IX Header "DESCRIPTION"
299These functions create, manipulate, and use cryptographic modules in the
300form of \fB\s-1ENGINE\s0\fR objects. These objects act as containers for
301implementations of cryptographic algorithms, and support a
302reference-counted mechanism to allow them to be dynamically loaded in and
303out of the running application.
304.PP
305The cryptographic functionality that can be provided by an \fB\s-1ENGINE\s0\fR
306implementation includes the following abstractions;
307.PP
308.Vb 5
309\& RSA_METHOD - for providing alternative RSA implementations
310\& DSA_METHOD, DH_METHOD, RAND_METHOD - alternative DSA, DH, and RAND
311\& EVP_CIPHER - potentially multiple cipher algorithms (indexed by 'nid')
312\& EVP_DIGEST - potentially multiple hash algorithms (indexed by 'nid')
313\& key-loading - loading public and/or private EVP_PKEY keys
314.Ve
315.Sh "Reference counting and handles"
316.IX Subsection "Reference counting and handles"
317Due to the modular nature of the \s-1ENGINE\s0 \s-1API\s0, pointers to ENGINEs need to be
318treated as handles \- ie. not only as pointers, but also as references to
319the underlying \s-1ENGINE\s0 object. Ie. you should obtain a new reference when
320making copies of an \s-1ENGINE\s0 pointer if the copies will be used (and
321released) independantly.
322.PP
323\&\s-1ENGINE\s0 objects have two levels of reference-counting to match the way in
324which the objects are used. At the most basic level, each \s-1ENGINE\s0 pointer is
325inherently a \fBstructural\fR reference \- you need a structural reference
326simply to refer to the pointer value at all, as this kind of reference is
327your guarantee that the structure can not be deallocated until you release
328your reference.
329.PP
330However, a structural reference provides no guarantee that the \s-1ENGINE\s0 has
331been initiliased to be usable to perform any of its cryptographic
332implementations \- and indeed it's quite possible that most ENGINEs will not
333initialised at all on standard setups, as ENGINEs are typically used to
334support specialised hardware. To use an \s-1ENGINE\s0's functionality, you need a
335\&\fBfunctional\fR reference. This kind of reference can be considered a
336specialised form of structural reference, because each functional reference
337implicitly contains a structural reference as well \- however to avoid
338difficult-to-find programming bugs, it is recommended to treat the two
339kinds of reference independantly. If you have a functional reference to an
340\&\s-1ENGINE\s0, you have a guarantee that the \s-1ENGINE\s0 has been initialised ready to
341perform cryptographic operations and will not be uninitialised or cleaned
342up until after you have released your reference.
343.PP
344We will discuss the two kinds of reference separately, including how to
345tell which one you are dealing with at any given point in time (after all
346they are both simply (\s-1ENGINE\s0 *) pointers, the difference is in the way they
347are used).
348.PP
349\&\fIStructural references\fR
350.PP
351This basic type of reference is typically used for creating new ENGINEs
352dynamically, iterating across OpenSSL's internal linked-list of loaded
353ENGINEs, reading information about an \s-1ENGINE\s0, etc. Essentially a structural
354reference is sufficient if you only need to query or manipulate the data of
355an \s-1ENGINE\s0 implementation rather than use its functionality.
356.PP
357The \fIENGINE_new()\fR function returns a structural reference to a new (empty)
358\&\s-1ENGINE\s0 object. Other than that, structural references come from return
359values to various \s-1ENGINE\s0 \s-1API\s0 functions such as; \fIENGINE_by_id()\fR,
360\&\fIENGINE_get_first()\fR, \fIENGINE_get_last()\fR, \fIENGINE_get_next()\fR,
361\&\fIENGINE_get_prev()\fR. All structural references should be released by a
362corresponding to call to the \fIENGINE_free()\fR function \- the \s-1ENGINE\s0 object
363itself will only actually be cleaned up and deallocated when the last
364structural reference is released.
365.PP
366It should also be noted that many \s-1ENGINE\s0 \s-1API\s0 function calls that accept a
367structural reference will internally obtain another reference \- typically
368this happens whenever the supplied \s-1ENGINE\s0 will be needed by OpenSSL after
369the function has returned. Eg. the function to add a new \s-1ENGINE\s0 to
370OpenSSL's internal list is \fIENGINE_add()\fR \- if this function returns success,
371then OpenSSL will have stored a new structural reference internally so the
372caller is still responsible for freeing their own reference with
373\&\fIENGINE_free()\fR when they are finished with it. In a similar way, some
374functions will automatically release the structural reference passed to it
375if part of the function's job is to do so. Eg. the \fIENGINE_get_next()\fR and
376\&\fIENGINE_get_prev()\fR functions are used for iterating across the internal
377\&\s-1ENGINE\s0 list \- they will return a new structural reference to the next (or
378previous) \s-1ENGINE\s0 in the list or \s-1NULL\s0 if at the end (or beginning) of the
379list, but in either case the structural reference passed to the function is
380released on behalf of the caller.
381.PP
382To clarify a particular function's handling of references, one should
383always consult that function's documentation \*(L"man\*(R" page, or failing that
384the openssl/engine.h header file includes some hints.
385.PP
386\&\fIFunctional references\fR
387.PP
388As mentioned, functional references exist when the cryptographic
389functionality of an \s-1ENGINE\s0 is required to be available. A functional
390reference can be obtained in one of two ways; from an existing structural
391reference to the required \s-1ENGINE\s0, or by asking OpenSSL for the default
392operational \s-1ENGINE\s0 for a given cryptographic purpose.
393.PP
394To obtain a functional reference from an existing structural reference,
395call the \fIENGINE_init()\fR function. This returns zero if the \s-1ENGINE\s0 was not
396already operational and couldn't be successfully initialised (eg. lack of
397system drivers, no special hardware attached, etc), otherwise it will
398return non-zero to indicate that the \s-1ENGINE\s0 is now operational and will
399have allocated a new \fBfunctional\fR reference to the \s-1ENGINE\s0. In this case,
400the supplied \s-1ENGINE\s0 pointer is, from the point of the view of the caller,
401both a structural reference and a functional reference \- so if the caller
402intends to use it as a functional reference it should free the structural
403reference with \fIENGINE_free()\fR first. If the caller wishes to use it only as
404a structural reference (eg. if the \fIENGINE_init()\fR call was simply to test if
405the \s-1ENGINE\s0 seems available/online), then it should free the functional
406reference; all functional references are released by the \fIENGINE_finish()\fR
407function.
408.PP
409The second way to get a functional reference is by asking OpenSSL for a
410default implementation for a given task, eg. by \fIENGINE_get_default_RSA()\fR,
411\&\fIENGINE_get_default_cipher_engine()\fR, etc. These are discussed in the next
412section, though they are not usually required by application programmers as
413they are used automatically when creating and using the relevant
414algorithm-specific types in OpenSSL, such as \s-1RSA\s0, \s-1DSA\s0, \s-1EVP_CIPHER_CTX\s0, etc.
415.Sh "Default implementations"
416.IX Subsection "Default implementations"
417For each supported abstraction, the \s-1ENGINE\s0 code maintains an internal table
418of state to control which implementations are available for a given
419abstraction and which should be used by default. These implementations are
420registered in the tables separated-out by an 'nid' index, because
421abstractions like \s-1EVP_CIPHER\s0 and \s-1EVP_DIGEST\s0 support many distinct
422algorithms and modes \- ENGINEs will support different numbers and
423combinations of these. In the case of other abstractions like \s-1RSA\s0, \s-1DSA\s0,
424etc, there is only one \*(L"algorithm\*(R" so all implementations implicitly
425register using the same 'nid' index. ENGINEs can be \fBregistered\fR into
426these tables to make themselves available for use automatically by the
427various abstractions, eg. \s-1RSA\s0. For illustrative purposes, we continue with
428the \s-1RSA\s0 example, though all comments apply similarly to the other
429abstractions (they each get their own table and linkage to the
430corresponding section of openssl code).
431.PP
432When a new \s-1RSA\s0 key is being created, ie. in \fIRSA_new_method()\fR, a
433\&\*(L"get_default\*(R" call will be made to the \s-1ENGINE\s0 subsystem to process the \s-1RSA\s0
434state table and return a functional reference to an initialised \s-1ENGINE\s0
435whose \s-1RSA_METHOD\s0 should be used. If no \s-1ENGINE\s0 should (or can) be used, it
436will return \s-1NULL\s0 and the \s-1RSA\s0 key will operate with a \s-1NULL\s0 \s-1ENGINE\s0 handle by
437using the conventional \s-1RSA\s0 implementation in OpenSSL (and will from then on
438behave the way it used to before the \s-1ENGINE\s0 \s-1API\s0 existed \- for details see
439RSA_new_method(3)).
440.PP
441Each state table has a flag to note whether it has processed this
442\&\*(L"get_default\*(R" query since the table was last modified, because to process
443this question it must iterate across all the registered ENGINEs in the
444table trying to initialise each of them in turn, in case one of them is
445operational. If it returns a functional reference to an \s-1ENGINE\s0, it will
446also cache another reference to speed up processing future queries (without
447needing to iterate across the table). Likewise, it will cache a \s-1NULL\s0
448response if no \s-1ENGINE\s0 was available so that future queries won't repeat the
449same iteration unless the state table changes. This behaviour can also be
450changed; if the \s-1ENGINE_TABLE_FLAG_NOINIT\s0 flag is set (using
451\&\fIENGINE_set_table_flags()\fR), no attempted initialisations will take place,
452instead the only way for the state table to return a non-NULL \s-1ENGINE\s0 to the
453\&\*(L"get_default\*(R" query will be if one is expressly set in the table. Eg.
454\&\fIENGINE_set_default_RSA()\fR does the same job as \fIENGINE_register_RSA()\fR except
455that it also sets the state table's cached response for the \*(L"get_default\*(R"
456query.
457.PP
458In the case of abstractions like \s-1EVP_CIPHER\s0, where implementations are
459indexed by 'nid', these flags and cached-responses are distinct for each
460\&'nid' value.
461.PP
462It is worth illustrating the difference between \*(L"registration\*(R" of ENGINEs
463into these per-algorithm state tables and using the alternative
464\&\*(L"set_default\*(R" functions. The latter handles both \*(L"registration\*(R" and also
465setting the cached \*(L"default\*(R" \s-1ENGINE\s0 in each relevant state table \- so
466registered ENGINEs will only have a chance to be initialised for use as a
467default if a default \s-1ENGINE\s0 wasn't already set for the same state table.
468Eg. if \s-1ENGINE\s0 X supports cipher nids {A,B} and \s-1RSA\s0, \s-1ENGINE\s0 Y supports
469ciphers {A} and \s-1DSA\s0, and the following code is executed;
470.PP
471.Vb 7
472\& ENGINE_register_complete(X);
473\& ENGINE_set_default(Y, ENGINE_METHOD_ALL);
474\& e1 = ENGINE_get_default_RSA();
475\& e2 = ENGINE_get_cipher_engine(A);
476\& e3 = ENGINE_get_cipher_engine(B);
477\& e4 = ENGINE_get_default_DSA();
478\& e5 = ENGINE_get_cipher_engine(C);
479.Ve
480The results would be as follows;
481.PP
482.Vb 5
483\& assert(e1 == X);
484\& assert(e2 == Y);
485\& assert(e3 == X);
486\& assert(e4 == Y);
487\& assert(e5 == NULL);
488.Ve
489.Sh "Application requirements"
490.IX Subsection "Application requirements"
491This section will explain the basic things an application programmer should
492support to make the most useful elements of the \s-1ENGINE\s0 functionality
493available to the user. The first thing to consider is whether the
494programmer wishes to make alternative \s-1ENGINE\s0 modules available to the
495application and user. OpenSSL maintains an internal linked list of
496\&\*(L"visible\*(R" ENGINEs from which it has to operate \- at start-up, this list is
497empty and in fact if an application does not call any \s-1ENGINE\s0 \s-1API\s0 calls and
498it uses static linking against openssl, then the resulting application
499binary will not contain any alternative \s-1ENGINE\s0 code at all. So the first
500consideration is whether any/all available \s-1ENGINE\s0 implementations should be
501made visible to OpenSSL \- this is controlled by calling the various \*(L"load\*(R"
502functions, eg.
503.PP
504.Vb 9
505\& /* Make the "dynamic" ENGINE available */
506\& void ENGINE_load_dynamic(void);
507\& /* Make the CryptoSwift hardware acceleration support available */
508\& void ENGINE_load_cswift(void);
509\& /* Make support for nCipher's "CHIL" hardware available */
510\& void ENGINE_load_chil(void);
511\& ...
512\& /* Make ALL ENGINE implementations bundled with OpenSSL available */
513\& void ENGINE_load_builtin_engines(void);
514.Ve
515Having called any of these functions, \s-1ENGINE\s0 objects would have been
516dynamically allocated and populated with these implementations and linked
517into OpenSSL's internal linked list. At this point it is important to
518mention an important \s-1API\s0 function;
519.PP
520.Vb 1
521\& void ENGINE_cleanup(void);
522.Ve
523If no \s-1ENGINE\s0 \s-1API\s0 functions are called at all in an application, then there
524are no inherent memory leaks to worry about from the \s-1ENGINE\s0 functionality,
525however if any ENGINEs are \*(L"load\*(R"ed, even if they are never registered or
526used, it is necessary to use the \fIENGINE_cleanup()\fR function to
527correspondingly cleanup before program exit, if the caller wishes to avoid
528memory leaks. This mechanism uses an internal callback registration table
529so that any \s-1ENGINE\s0 \s-1API\s0 functionality that knows it requires cleanup can
530register its cleanup details to be called during \fIENGINE_cleanup()\fR. This
531approach allows \fIENGINE_cleanup()\fR to clean up after any \s-1ENGINE\s0 functionality
532at all that your program uses, yet doesn't automatically create linker
533dependencies to all possible \s-1ENGINE\s0 functionality \- only the cleanup
534callbacks required by the functionality you do use will be required by the
535linker.
536.PP
537The fact that ENGINEs are made visible to OpenSSL (and thus are linked into
538the program and loaded into memory at run-time) does not mean they are
539\&\*(L"registered\*(R" or called into use by OpenSSL automatically \- that behaviour
540is something for the application to have control over. Some applications
541will want to allow the user to specify exactly which \s-1ENGINE\s0 they want used
542if any is to be used at all. Others may prefer to load all support and have
543OpenSSL automatically use at run-time any \s-1ENGINE\s0 that is able to
544successfully initialise \- ie. to assume that this corresponds to
545acceleration hardware attached to the machine or some such thing. There are
546probably numerous other ways in which applications may prefer to handle
547things, so we will simply illustrate the consequences as they apply to a
548couple of simple cases and leave developers to consider these and the
549source code to openssl's builtin utilities as guides.
550.PP
551\&\fIUsing a specific \s-1ENGINE\s0 implementation\fR
552.PP
553Here we'll assume an application has been configured by its user or admin
554to want to use the \*(L"\s-1ACME\s0\*(R" \s-1ENGINE\s0 if it is available in the version of
555OpenSSL the application was compiled with. If it is available, it should be
556used by default for all \s-1RSA\s0, \s-1DSA\s0, and symmetric cipher operation, otherwise
557OpenSSL should use its builtin software as per usual. The following code
558illustrates how to approach this;
559.PP
560.Vb 22
561\& ENGINE *e;
562\& const char *engine_id = "ACME";
563\& ENGINE_load_builtin_engines();
564\& e = ENGINE_by_id(engine_id);
565\& if(!e)
566\& /* the engine isn't available */
567\& return;
568\& if(!ENGINE_init(e)) {
569\& /* the engine couldn't initialise, release 'e' */
570\& ENGINE_free(e);
571\& return;
572\& }
573\& if(!ENGINE_set_default_RSA(e))
574\& /* This should only happen when 'e' can't initialise, but the previous
575\& * statement suggests it did. */
576\& abort();
577\& ENGINE_set_default_DSA(e);
578\& ENGINE_set_default_ciphers(e);
579\& /* Release the functional reference from ENGINE_init() */
580\& ENGINE_finish(e);
581\& /* Release the structural reference from ENGINE_by_id() */
582\& ENGINE_free(e);
583.Ve
584\&\fIAutomatically using builtin \s-1ENGINE\s0 implementations\fR
585.PP
586Here we'll assume we want to load and register all \s-1ENGINE\s0 implementations
587bundled with OpenSSL, such that for any cryptographic algorithm required by
588OpenSSL \- if there is an \s-1ENGINE\s0 that implements it and can be initialise,
589it should be used. The following code illustrates how this can work;
590.PP
591.Vb 4
592\& /* Load all bundled ENGINEs into memory and make them visible */
593\& ENGINE_load_builtin_engines();
594\& /* Register all of them for every algorithm they collectively implement */
595\& ENGINE_register_all_complete();
596.Ve
597That's all that's required. Eg. the next time OpenSSL tries to set up an
598\&\s-1RSA\s0 key, any bundled ENGINEs that implement \s-1RSA_METHOD\s0 will be passed to
599\&\fIENGINE_init()\fR and if any of those succeed, that \s-1ENGINE\s0 will be set as the
600default for use with \s-1RSA\s0 from then on.
601.Sh "Advanced configuration support"
602.IX Subsection "Advanced configuration support"
603There is a mechanism supported by the \s-1ENGINE\s0 framework that allows each
604\&\s-1ENGINE\s0 implementation to define an arbitrary set of configuration
605\&\*(L"commands\*(R" and expose them to OpenSSL and any applications based on
606OpenSSL. This mechanism is entirely based on the use of name-value pairs
607and and assumes \s-1ASCII\s0 input (no unicode or \s-1UTF\s0 for now!), so it is ideal if
608applications want to provide a transparent way for users to provide
609arbitrary configuration \*(L"directives\*(R" directly to such ENGINEs. It is also
610possible for the application to dynamically interrogate the loaded \s-1ENGINE\s0
611implementations for the names, descriptions, and input flags of their
612available \*(L"control commands\*(R", providing a more flexible configuration
613scheme. However, if the user is expected to know which \s-1ENGINE\s0 device he/she
614is using (in the case of specialised hardware, this goes without saying)
615then applications may not need to concern themselves with discovering the
616supported control commands and simply prefer to allow settings to passed
617into ENGINEs exactly as they are provided by the user.
618.PP
619Before illustrating how control commands work, it is worth mentioning what
620they are typically used for. Broadly speaking there are two uses for
621control commands; the first is to provide the necessary details to the
622implementation (which may know nothing at all specific to the host system)
623so that it can be initialised for use. This could include the path to any
624driver or config files it needs to load, required network addresses,
625smart-card identifiers, passwords to initialise password-protected devices,
626logging information, etc etc. This class of commands typically needs to be
627passed to an \s-1ENGINE\s0 \fBbefore\fR attempting to initialise it, ie. before
628calling \fIENGINE_init()\fR. The other class of commands consist of settings or
629operations that tweak certain behaviour or cause certain operations to take
630place, and these commands may work either before or after \fIENGINE_init()\fR, or
631in same cases both. \s-1ENGINE\s0 implementations should provide indications of
632this in the descriptions attached to builtin control commands and/or in
633external product documentation.
634.PP
635\&\fIIssuing control commands to an \s-1ENGINE\s0\fR
636.PP
637Let's illustrate by example; a function for which the caller supplies the
638name of the \s-1ENGINE\s0 it wishes to use, a table of string-pairs for use before
639initialisation, and another table for use after initialisation. Note that
640the string-pairs used for control commands consist of a command \*(L"name\*(R"
641followed by the command \*(L"parameter\*(R" \- the parameter could be \s-1NULL\s0 in some
642cases but the name can not. This function should initialise the \s-1ENGINE\s0
643(issuing the \*(L"pre\*(R" commands beforehand and the \*(L"post\*(R" commands afterwards)
644and set it as the default for everything except \s-1RAND\s0 and then return a
645boolean success or failure.
646.PP
647.Vb 36
648\& int generic_load_engine_fn(const char *engine_id,
649\& const char **pre_cmds, int pre_num,
650\& const char **post_cmds, int post_num)
651\& {
652\& ENGINE *e = ENGINE_by_id(engine_id);
653\& if(!e) return 0;
654\& while(pre_num--) {
655\& if(!ENGINE_ctrl_cmd_string(e, pre_cmds[0], pre_cmds[1], 0)) {
656\& fprintf(stderr, "Failed command (%s - %s:%s)\en", engine_id,
657\& pre_cmds[0], pre_cmds[1] ? pre_cmds[1] : "(NULL)");
658\& ENGINE_free(e);
659\& return 0;
660\& }
661\& pre_cmds += 2;
662\& }
663\& if(!ENGINE_init(e)) {
664\& fprintf(stderr, "Failed initialisation\en");
665\& ENGINE_free(e);
666\& return 0;
667\& }
668\& /* ENGINE_init() returned a functional reference, so free the structural
669\& * reference from ENGINE_by_id(). */
670\& ENGINE_free(e);
671\& while(post_num--) {
672\& if(!ENGINE_ctrl_cmd_string(e, post_cmds[0], post_cmds[1], 0)) {
673\& fprintf(stderr, "Failed command (%s - %s:%s)\en", engine_id,
674\& post_cmds[0], post_cmds[1] ? post_cmds[1] : "(NULL)");
675\& ENGINE_finish(e);
676\& return 0;
677\& }
678\& post_cmds += 2;
679\& }
680\& ENGINE_set_default(e, ENGINE_METHOD_ALL & ~ENGINE_METHOD_RAND);
681\& /* Success */
682\& return 1;
683\& }
684.Ve
685Note that \fIENGINE_ctrl_cmd_string()\fR accepts a boolean argument that can
686relax the semantics of the function \- if set non-zero it will only return
687failure if the \s-1ENGINE\s0 supported the given command name but failed while
688executing it, if the \s-1ENGINE\s0 doesn't support the command name it will simply
689return success without doing anything. In this case we assume the user is
690only supplying commands specific to the given \s-1ENGINE\s0 so we set this to
691\&\s-1FALSE\s0.
692.PP
693\&\fIDiscovering supported control commands\fR
694.PP
695It is possible to discover at run-time the names, numerical-ids, descriptions
696and input parameters of the control commands supported from a structural
697reference to any \s-1ENGINE\s0. It is first important to note that some control
698commands are defined by OpenSSL itself and it will intercept and handle these
699control commands on behalf of the \s-1ENGINE\s0, ie. the \s-1ENGINE\s0's \fIctrl()\fR handler is not
700used for the control command. openssl/engine.h defines a symbol,
701\&\s-1ENGINE_CMD_BASE\s0, that all control commands implemented by ENGINEs from. Any
702command value lower than this symbol is considered a \*(L"generic\*(R" command is
703handled directly by the OpenSSL core routines.
704.PP
705It is using these \*(L"core\*(R" control commands that one can discover the the control
706commands implemented by a given \s-1ENGINE\s0, specifically the commands;
707.PP
708.Vb 9
709\& #define ENGINE_HAS_CTRL_FUNCTION 10
710\& #define ENGINE_CTRL_GET_FIRST_CMD_TYPE 11
711\& #define ENGINE_CTRL_GET_NEXT_CMD_TYPE 12
712\& #define ENGINE_CTRL_GET_CMD_FROM_NAME 13
713\& #define ENGINE_CTRL_GET_NAME_LEN_FROM_CMD 14
714\& #define ENGINE_CTRL_GET_NAME_FROM_CMD 15
715\& #define ENGINE_CTRL_GET_DESC_LEN_FROM_CMD 16
716\& #define ENGINE_CTRL_GET_DESC_FROM_CMD 17
717\& #define ENGINE_CTRL_GET_CMD_FLAGS 18
718.Ve
719Whilst these commands are automatically processed by the OpenSSL framework code,
720they use various properties exposed by each \s-1ENGINE\s0 by which to process these
721queries. An \s-1ENGINE\s0 has 3 properties it exposes that can affect this behaviour;
722it can supply a \fIctrl()\fR handler, it can specify \s-1ENGINE_FLAGS_MANUAL_CMD_CTRL\s0 in
723the \s-1ENGINE\s0's flags, and it can expose an array of control command descriptions.
724If an \s-1ENGINE\s0 specifies the \s-1ENGINE_FLAGS_MANUAL_CMD_CTRL\s0 flag, then it will
725simply pass all these \*(L"core\*(R" control commands directly to the \s-1ENGINE\s0's \fIctrl()\fR
726handler (and thus, it must have supplied one), so it is up to the \s-1ENGINE\s0 to
727reply to these \*(L"discovery\*(R" commands itself. If that flag is not set, then the
728OpenSSL framework code will work with the following rules;
729.PP
730.Vb 9
731\& if no ctrl() handler supplied;
732\& ENGINE_HAS_CTRL_FUNCTION returns FALSE (zero),
733\& all other commands fail.
734\& if a ctrl() handler was supplied but no array of control commands;
735\& ENGINE_HAS_CTRL_FUNCTION returns TRUE,
736\& all other commands fail.
737\& if a ctrl() handler and array of control commands was supplied;
738\& ENGINE_HAS_CTRL_FUNCTION returns TRUE,
739\& all other commands proceed processing ...
740.Ve
741If the \s-1ENGINE\s0's array of control commands is empty then all other commands will
742fail, otherwise; \s-1ENGINE_CTRL_GET_FIRST_CMD_TYPE\s0 returns the identifier of
743the first command supported by the \s-1ENGINE\s0, \s-1ENGINE_GET_NEXT_CMD_TYPE\s0 takes the
744identifier of a command supported by the \s-1ENGINE\s0 and returns the next command
745identifier or fails if there are no more, \s-1ENGINE_CMD_FROM_NAME\s0 takes a string
746name for a command and returns the corresponding identifier or fails if no such
747command name exists, and the remaining commands take a command identifier and
748return properties of the corresponding commands. All except
749\&\s-1ENGINE_CTRL_GET_FLAGS\s0 return the string length of a command name or description,
750or populate a supplied character buffer with a copy of the command name or
751description. \s-1ENGINE_CTRL_GET_FLAGS\s0 returns a bitwise-OR'd mask of the following
752possible values;
753.PP
754.Vb 4
755\& #define ENGINE_CMD_FLAG_NUMERIC (unsigned int)0x0001
756\& #define ENGINE_CMD_FLAG_STRING (unsigned int)0x0002
757\& #define ENGINE_CMD_FLAG_NO_INPUT (unsigned int)0x0004
758\& #define ENGINE_CMD_FLAG_INTERNAL (unsigned int)0x0008
759.Ve
760If the \s-1ENGINE_CMD_FLAG_INTERNAL\s0 flag is set, then any other flags are purely
761informational to the caller \- this flag will prevent the command being usable
762for any higher-level \s-1ENGINE\s0 functions such as \fIENGINE_ctrl_cmd_string()\fR.
763\&\*(L"\s-1INTERNAL\s0\*(R" commands are not intended to be exposed to text-based configuration
764by applications, administrations, users, etc. These can support arbitrary
765operations via \fIENGINE_ctrl()\fR, including passing to and/or from the control
766commands data of any arbitrary type. These commands are supported in the
767discovery mechanisms simply to allow applications determinie if an \s-1ENGINE\s0
768supports certain specific commands it might want to use (eg. application \*(L"foo\*(R"
769might query various ENGINEs to see if they implement \*(L"\s-1FOO_GET_VENDOR_LOGO_GIF\s0\*(R" \-
770and \s-1ENGINE\s0 could therefore decide whether or not to support this \*(L"foo\*(R"\-specific
771extension).
772.Sh "Future developments"
773.IX Subsection "Future developments"
774The \s-1ENGINE\s0 \s-1API\s0 and internal architecture is currently being reviewed. Slated for
775possible release in 0.9.8 is support for transparent loading of \*(L"dynamic\*(R"
776ENGINEs (built as self-contained shared-libraries). This would allow \s-1ENGINE\s0
777implementations to be provided independantly of OpenSSL libraries and/or
778OpenSSL-based applications, and would also remove any requirement for
779applications to explicitly use the \*(L"dynamic\*(R" \s-1ENGINE\s0 to bind to shared-library
780implementations.
781.SH "SEE ALSO"
782.IX Header "SEE ALSO"
783rsa(3), dsa(3), dh(3), rand(3),
784RSA_new_method(3)