xref: /onnv-gate/usr/src/common/openssl/doc/crypto/engine.pod (revision 2175:b0b2f052a486)
1*2175Sjp161948=pod
2*2175Sjp161948
3*2175Sjp161948=head1 NAME
4*2175Sjp161948
5*2175Sjp161948engine - ENGINE cryptographic module support
6*2175Sjp161948
7*2175Sjp161948=head1 SYNOPSIS
8*2175Sjp161948
9*2175Sjp161948 #include <openssl/engine.h>
10*2175Sjp161948
11*2175Sjp161948 ENGINE *ENGINE_get_first(void);
12*2175Sjp161948 ENGINE *ENGINE_get_last(void);
13*2175Sjp161948 ENGINE *ENGINE_get_next(ENGINE *e);
14*2175Sjp161948 ENGINE *ENGINE_get_prev(ENGINE *e);
15*2175Sjp161948
16*2175Sjp161948 int ENGINE_add(ENGINE *e);
17*2175Sjp161948 int ENGINE_remove(ENGINE *e);
18*2175Sjp161948
19*2175Sjp161948 ENGINE *ENGINE_by_id(const char *id);
20*2175Sjp161948
21*2175Sjp161948 int ENGINE_init(ENGINE *e);
22*2175Sjp161948 int ENGINE_finish(ENGINE *e);
23*2175Sjp161948
24*2175Sjp161948 void ENGINE_load_openssl(void);
25*2175Sjp161948 void ENGINE_load_dynamic(void);
26*2175Sjp161948 #ifndef OPENSSL_NO_STATIC_ENGINE
27*2175Sjp161948 void ENGINE_load_4758cca(void);
28*2175Sjp161948 void ENGINE_load_aep(void);
29*2175Sjp161948 void ENGINE_load_atalla(void);
30*2175Sjp161948 void ENGINE_load_chil(void);
31*2175Sjp161948 void ENGINE_load_cswift(void);
32*2175Sjp161948 void ENGINE_load_gmp(void);
33*2175Sjp161948 void ENGINE_load_nuron(void);
34*2175Sjp161948 void ENGINE_load_sureware(void);
35*2175Sjp161948 void ENGINE_load_ubsec(void);
36*2175Sjp161948 #endif
37*2175Sjp161948 void ENGINE_load_cryptodev(void);
38*2175Sjp161948 void ENGINE_load_builtin_engines(void);
39*2175Sjp161948
40*2175Sjp161948 void ENGINE_cleanup(void);
41*2175Sjp161948
42*2175Sjp161948 ENGINE *ENGINE_get_default_RSA(void);
43*2175Sjp161948 ENGINE *ENGINE_get_default_DSA(void);
44*2175Sjp161948 ENGINE *ENGINE_get_default_ECDH(void);
45*2175Sjp161948 ENGINE *ENGINE_get_default_ECDSA(void);
46*2175Sjp161948 ENGINE *ENGINE_get_default_DH(void);
47*2175Sjp161948 ENGINE *ENGINE_get_default_RAND(void);
48*2175Sjp161948 ENGINE *ENGINE_get_cipher_engine(int nid);
49*2175Sjp161948 ENGINE *ENGINE_get_digest_engine(int nid);
50*2175Sjp161948
51*2175Sjp161948 int ENGINE_set_default_RSA(ENGINE *e);
52*2175Sjp161948 int ENGINE_set_default_DSA(ENGINE *e);
53*2175Sjp161948 int ENGINE_set_default_ECDH(ENGINE *e);
54*2175Sjp161948 int ENGINE_set_default_ECDSA(ENGINE *e);
55*2175Sjp161948 int ENGINE_set_default_DH(ENGINE *e);
56*2175Sjp161948 int ENGINE_set_default_RAND(ENGINE *e);
57*2175Sjp161948 int ENGINE_set_default_ciphers(ENGINE *e);
58*2175Sjp161948 int ENGINE_set_default_digests(ENGINE *e);
59*2175Sjp161948 int ENGINE_set_default_string(ENGINE *e, const char *list);
60*2175Sjp161948
61*2175Sjp161948 int ENGINE_set_default(ENGINE *e, unsigned int flags);
62*2175Sjp161948
63*2175Sjp161948 unsigned int ENGINE_get_table_flags(void);
64*2175Sjp161948 void ENGINE_set_table_flags(unsigned int flags);
65*2175Sjp161948
66*2175Sjp161948 int ENGINE_register_RSA(ENGINE *e);
67*2175Sjp161948 void ENGINE_unregister_RSA(ENGINE *e);
68*2175Sjp161948 void ENGINE_register_all_RSA(void);
69*2175Sjp161948 int ENGINE_register_DSA(ENGINE *e);
70*2175Sjp161948 void ENGINE_unregister_DSA(ENGINE *e);
71*2175Sjp161948 void ENGINE_register_all_DSA(void);
72*2175Sjp161948 int ENGINE_register_ECDH(ENGINE *e);
73*2175Sjp161948 void ENGINE_unregister_ECDH(ENGINE *e);
74*2175Sjp161948 void ENGINE_register_all_ECDH(void);
75*2175Sjp161948 int ENGINE_register_ECDSA(ENGINE *e);
76*2175Sjp161948 void ENGINE_unregister_ECDSA(ENGINE *e);
77*2175Sjp161948 void ENGINE_register_all_ECDSA(void);
78*2175Sjp161948 int ENGINE_register_DH(ENGINE *e);
79*2175Sjp161948 void ENGINE_unregister_DH(ENGINE *e);
80*2175Sjp161948 void ENGINE_register_all_DH(void);
81*2175Sjp161948 int ENGINE_register_RAND(ENGINE *e);
82*2175Sjp161948 void ENGINE_unregister_RAND(ENGINE *e);
83*2175Sjp161948 void ENGINE_register_all_RAND(void);
84*2175Sjp161948 int ENGINE_register_STORE(ENGINE *e);
85*2175Sjp161948 void ENGINE_unregister_STORE(ENGINE *e);
86*2175Sjp161948 void ENGINE_register_all_STORE(void);
87*2175Sjp161948 int ENGINE_register_ciphers(ENGINE *e);
88*2175Sjp161948 void ENGINE_unregister_ciphers(ENGINE *e);
89*2175Sjp161948 void ENGINE_register_all_ciphers(void);
90*2175Sjp161948 int ENGINE_register_digests(ENGINE *e);
91*2175Sjp161948 void ENGINE_unregister_digests(ENGINE *e);
92*2175Sjp161948 void ENGINE_register_all_digests(void);
93*2175Sjp161948 int ENGINE_register_complete(ENGINE *e);
94*2175Sjp161948 int ENGINE_register_all_complete(void);
95*2175Sjp161948
96*2175Sjp161948 int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
97*2175Sjp161948 int ENGINE_cmd_is_executable(ENGINE *e, int cmd);
98*2175Sjp161948 int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
99*2175Sjp161948         long i, void *p, void (*f)(void), int cmd_optional);
100*2175Sjp161948 int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
101*2175Sjp161948         int cmd_optional);
102*2175Sjp161948
103*2175Sjp161948 int ENGINE_set_ex_data(ENGINE *e, int idx, void *arg);
104*2175Sjp161948 void *ENGINE_get_ex_data(const ENGINE *e, int idx);
105*2175Sjp161948
106*2175Sjp161948 int ENGINE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
107*2175Sjp161948         CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
108*2175Sjp161948
109*2175Sjp161948 ENGINE *ENGINE_new(void);
110*2175Sjp161948 int ENGINE_free(ENGINE *e);
111*2175Sjp161948 int ENGINE_up_ref(ENGINE *e);
112*2175Sjp161948
113*2175Sjp161948 int ENGINE_set_id(ENGINE *e, const char *id);
114*2175Sjp161948 int ENGINE_set_name(ENGINE *e, const char *name);
115*2175Sjp161948 int ENGINE_set_RSA(ENGINE *e, const RSA_METHOD *rsa_meth);
116*2175Sjp161948 int ENGINE_set_DSA(ENGINE *e, const DSA_METHOD *dsa_meth);
117*2175Sjp161948 int ENGINE_set_ECDH(ENGINE *e, const ECDH_METHOD *dh_meth);
118*2175Sjp161948 int ENGINE_set_ECDSA(ENGINE *e, const ECDSA_METHOD *dh_meth);
119*2175Sjp161948 int ENGINE_set_DH(ENGINE *e, const DH_METHOD *dh_meth);
120*2175Sjp161948 int ENGINE_set_RAND(ENGINE *e, const RAND_METHOD *rand_meth);
121*2175Sjp161948 int ENGINE_set_STORE(ENGINE *e, const STORE_METHOD *rand_meth);
122*2175Sjp161948 int ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f);
123*2175Sjp161948 int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f);
124*2175Sjp161948 int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f);
125*2175Sjp161948 int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f);
126*2175Sjp161948 int ENGINE_set_load_privkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpriv_f);
127*2175Sjp161948 int ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f);
128*2175Sjp161948 int ENGINE_set_ciphers(ENGINE *e, ENGINE_CIPHERS_PTR f);
129*2175Sjp161948 int ENGINE_set_digests(ENGINE *e, ENGINE_DIGESTS_PTR f);
130*2175Sjp161948 int ENGINE_set_flags(ENGINE *e, int flags);
131*2175Sjp161948 int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns);
132*2175Sjp161948
133*2175Sjp161948 const char *ENGINE_get_id(const ENGINE *e);
134*2175Sjp161948 const char *ENGINE_get_name(const ENGINE *e);
135*2175Sjp161948 const RSA_METHOD *ENGINE_get_RSA(const ENGINE *e);
136*2175Sjp161948 const DSA_METHOD *ENGINE_get_DSA(const ENGINE *e);
137*2175Sjp161948 const ECDH_METHOD *ENGINE_get_ECDH(const ENGINE *e);
138*2175Sjp161948 const ECDSA_METHOD *ENGINE_get_ECDSA(const ENGINE *e);
139*2175Sjp161948 const DH_METHOD *ENGINE_get_DH(const ENGINE *e);
140*2175Sjp161948 const RAND_METHOD *ENGINE_get_RAND(const ENGINE *e);
141*2175Sjp161948 const STORE_METHOD *ENGINE_get_STORE(const ENGINE *e);
142*2175Sjp161948 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e);
143*2175Sjp161948 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e);
144*2175Sjp161948 ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e);
145*2175Sjp161948 ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e);
146*2175Sjp161948 ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e);
147*2175Sjp161948 ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e);
148*2175Sjp161948 ENGINE_CIPHERS_PTR ENGINE_get_ciphers(const ENGINE *e);
149*2175Sjp161948 ENGINE_DIGESTS_PTR ENGINE_get_digests(const ENGINE *e);
150*2175Sjp161948 const EVP_CIPHER *ENGINE_get_cipher(ENGINE *e, int nid);
151*2175Sjp161948 const EVP_MD *ENGINE_get_digest(ENGINE *e, int nid);
152*2175Sjp161948 int ENGINE_get_flags(const ENGINE *e);
153*2175Sjp161948 const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e);
154*2175Sjp161948
155*2175Sjp161948 EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
156*2175Sjp161948     UI_METHOD *ui_method, void *callback_data);
157*2175Sjp161948 EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
158*2175Sjp161948     UI_METHOD *ui_method, void *callback_data);
159*2175Sjp161948
160*2175Sjp161948 void ENGINE_add_conf_module(void);
161*2175Sjp161948
162*2175Sjp161948=head1 DESCRIPTION
163*2175Sjp161948
164*2175Sjp161948These functions create, manipulate, and use cryptographic modules in the
165*2175Sjp161948form of B<ENGINE> objects. These objects act as containers for
166*2175Sjp161948implementations of cryptographic algorithms, and support a
167*2175Sjp161948reference-counted mechanism to allow them to be dynamically loaded in and
168*2175Sjp161948out of the running application.
169*2175Sjp161948
170*2175Sjp161948The cryptographic functionality that can be provided by an B<ENGINE>
171*2175Sjp161948implementation includes the following abstractions;
172*2175Sjp161948
173*2175Sjp161948 RSA_METHOD - for providing alternative RSA implementations
174*2175Sjp161948 DSA_METHOD, DH_METHOD, RAND_METHOD, ECDH_METHOD, ECDSA_METHOD,
175*2175Sjp161948       STORE_METHOD - similarly for other OpenSSL APIs
176*2175Sjp161948 EVP_CIPHER - potentially multiple cipher algorithms (indexed by 'nid')
177*2175Sjp161948 EVP_DIGEST - potentially multiple hash algorithms (indexed by 'nid')
178*2175Sjp161948 key-loading - loading public and/or private EVP_PKEY keys
179*2175Sjp161948
180*2175Sjp161948=head2 Reference counting and handles
181*2175Sjp161948
182*2175Sjp161948Due to the modular nature of the ENGINE API, pointers to ENGINEs need to be
183*2175Sjp161948treated as handles - ie. not only as pointers, but also as references to
184*2175Sjp161948the underlying ENGINE object. Ie. one should obtain a new reference when
185*2175Sjp161948making copies of an ENGINE pointer if the copies will be used (and
186*2175Sjp161948released) independantly.
187*2175Sjp161948
188*2175Sjp161948ENGINE objects have two levels of reference-counting to match the way in
189*2175Sjp161948which the objects are used. At the most basic level, each ENGINE pointer is
190*2175Sjp161948inherently a B<structural> reference - a structural reference is required
191*2175Sjp161948to use the pointer value at all, as this kind of reference is a guarantee
192*2175Sjp161948that the structure can not be deallocated until the reference is released.
193*2175Sjp161948
194*2175Sjp161948However, a structural reference provides no guarantee that the ENGINE is
195*2175Sjp161948initiliased and able to use any of its cryptographic
196*2175Sjp161948implementations. Indeed it's quite possible that most ENGINEs will not
197*2175Sjp161948initialise at all in typical environments, as ENGINEs are typically used to
198*2175Sjp161948support specialised hardware. To use an ENGINE's functionality, you need a
199*2175Sjp161948B<functional> reference. This kind of reference can be considered a
200*2175Sjp161948specialised form of structural reference, because each functional reference
201*2175Sjp161948implicitly contains a structural reference as well - however to avoid
202*2175Sjp161948difficult-to-find programming bugs, it is recommended to treat the two
203*2175Sjp161948kinds of reference independantly. If you have a functional reference to an
204*2175Sjp161948ENGINE, you have a guarantee that the ENGINE has been initialised ready to
205*2175Sjp161948perform cryptographic operations and will remain uninitialised
206*2175Sjp161948until after you have released your reference.
207*2175Sjp161948
208*2175Sjp161948I<Structural references>
209*2175Sjp161948
210*2175Sjp161948This basic type of reference is used for instantiating new ENGINEs,
211*2175Sjp161948iterating across OpenSSL's internal linked-list of loaded
212*2175Sjp161948ENGINEs, reading information about an ENGINE, etc. Essentially a structural
213*2175Sjp161948reference is sufficient if you only need to query or manipulate the data of
214*2175Sjp161948an ENGINE implementation rather than use its functionality.
215*2175Sjp161948
216*2175Sjp161948The ENGINE_new() function returns a structural reference to a new (empty)
217*2175Sjp161948ENGINE object. There are other ENGINE API functions that return structural
218*2175Sjp161948references such as; ENGINE_by_id(), ENGINE_get_first(), ENGINE_get_last(),
219*2175Sjp161948ENGINE_get_next(), ENGINE_get_prev(). All structural references should be
220*2175Sjp161948released by a corresponding to call to the ENGINE_free() function - the
221*2175Sjp161948ENGINE object itself will only actually be cleaned up and deallocated when
222*2175Sjp161948the last structural reference is released.
223*2175Sjp161948
224*2175Sjp161948It should also be noted that many ENGINE API function calls that accept a
225*2175Sjp161948structural reference will internally obtain another reference - typically
226*2175Sjp161948this happens whenever the supplied ENGINE will be needed by OpenSSL after
227*2175Sjp161948the function has returned. Eg. the function to add a new ENGINE to
228*2175Sjp161948OpenSSL's internal list is ENGINE_add() - if this function returns success,
229*2175Sjp161948then OpenSSL will have stored a new structural reference internally so the
230*2175Sjp161948caller is still responsible for freeing their own reference with
231*2175Sjp161948ENGINE_free() when they are finished with it. In a similar way, some
232*2175Sjp161948functions will automatically release the structural reference passed to it
233*2175Sjp161948if part of the function's job is to do so. Eg. the ENGINE_get_next() and
234*2175Sjp161948ENGINE_get_prev() functions are used for iterating across the internal
235*2175Sjp161948ENGINE list - they will return a new structural reference to the next (or
236*2175Sjp161948previous) ENGINE in the list or NULL if at the end (or beginning) of the
237*2175Sjp161948list, but in either case the structural reference passed to the function is
238*2175Sjp161948released on behalf of the caller.
239*2175Sjp161948
240*2175Sjp161948To clarify a particular function's handling of references, one should
241*2175Sjp161948always consult that function's documentation "man" page, or failing that
242*2175Sjp161948the openssl/engine.h header file includes some hints.
243*2175Sjp161948
244*2175Sjp161948I<Functional references>
245*2175Sjp161948
246*2175Sjp161948As mentioned, functional references exist when the cryptographic
247*2175Sjp161948functionality of an ENGINE is required to be available. A functional
248*2175Sjp161948reference can be obtained in one of two ways; from an existing structural
249*2175Sjp161948reference to the required ENGINE, or by asking OpenSSL for the default
250*2175Sjp161948operational ENGINE for a given cryptographic purpose.
251*2175Sjp161948
252*2175Sjp161948To obtain a functional reference from an existing structural reference,
253*2175Sjp161948call the ENGINE_init() function. This returns zero if the ENGINE was not
254*2175Sjp161948already operational and couldn't be successfully initialised (eg. lack of
255*2175Sjp161948system drivers, no special hardware attached, etc), otherwise it will
256*2175Sjp161948return non-zero to indicate that the ENGINE is now operational and will
257*2175Sjp161948have allocated a new B<functional> reference to the ENGINE. All functional
258*2175Sjp161948references are released by calling ENGINE_finish() (which removes the
259*2175Sjp161948implicit structural reference as well).
260*2175Sjp161948
261*2175Sjp161948The second way to get a functional reference is by asking OpenSSL for a
262*2175Sjp161948default implementation for a given task, eg. by ENGINE_get_default_RSA(),
263*2175Sjp161948ENGINE_get_default_cipher_engine(), etc. These are discussed in the next
264*2175Sjp161948section, though they are not usually required by application programmers as
265*2175Sjp161948they are used automatically when creating and using the relevant
266*2175Sjp161948algorithm-specific types in OpenSSL, such as RSA, DSA, EVP_CIPHER_CTX, etc.
267*2175Sjp161948
268*2175Sjp161948=head2 Default implementations
269*2175Sjp161948
270*2175Sjp161948For each supported abstraction, the ENGINE code maintains an internal table
271*2175Sjp161948of state to control which implementations are available for a given
272*2175Sjp161948abstraction and which should be used by default. These implementations are
273*2175Sjp161948registered in the tables and indexed by an 'nid' value, because
274*2175Sjp161948abstractions like EVP_CIPHER and EVP_DIGEST support many distinct
275*2175Sjp161948algorithms and modes, and ENGINEs can support arbitrarily many of them.
276*2175Sjp161948In the case of other abstractions like RSA, DSA, etc, there is only one
277*2175Sjp161948"algorithm" so all implementations implicitly register using the same 'nid'
278*2175Sjp161948index.
279*2175Sjp161948
280*2175Sjp161948When a default ENGINE is requested for a given abstraction/algorithm/mode, (eg.
281*2175Sjp161948when calling RSA_new_method(NULL)), a "get_default" call will be made to the
282*2175Sjp161948ENGINE subsystem to process the corresponding state table and return a
283*2175Sjp161948functional reference to an initialised ENGINE whose implementation should be
284*2175Sjp161948used. If no ENGINE should (or can) be used, it will return NULL and the caller
285*2175Sjp161948will operate with a NULL ENGINE handle - this usually equates to using the
286*2175Sjp161948conventional software implementation. In the latter case, OpenSSL will from
287*2175Sjp161948then on behave the way it used to before the ENGINE API existed.
288*2175Sjp161948
289*2175Sjp161948Each state table has a flag to note whether it has processed this
290*2175Sjp161948"get_default" query since the table was last modified, because to process
291*2175Sjp161948this question it must iterate across all the registered ENGINEs in the
292*2175Sjp161948table trying to initialise each of them in turn, in case one of them is
293*2175Sjp161948operational. If it returns a functional reference to an ENGINE, it will
294*2175Sjp161948also cache another reference to speed up processing future queries (without
295*2175Sjp161948needing to iterate across the table). Likewise, it will cache a NULL
296*2175Sjp161948response if no ENGINE was available so that future queries won't repeat the
297*2175Sjp161948same iteration unless the state table changes. This behaviour can also be
298*2175Sjp161948changed; if the ENGINE_TABLE_FLAG_NOINIT flag is set (using
299*2175Sjp161948ENGINE_set_table_flags()), no attempted initialisations will take place,
300*2175Sjp161948instead the only way for the state table to return a non-NULL ENGINE to the
301*2175Sjp161948"get_default" query will be if one is expressly set in the table. Eg.
302*2175Sjp161948ENGINE_set_default_RSA() does the same job as ENGINE_register_RSA() except
303*2175Sjp161948that it also sets the state table's cached response for the "get_default"
304*2175Sjp161948query. In the case of abstractions like EVP_CIPHER, where implementations are
305*2175Sjp161948indexed by 'nid', these flags and cached-responses are distinct for each 'nid'
306*2175Sjp161948value.
307*2175Sjp161948
308*2175Sjp161948=head2 Application requirements
309*2175Sjp161948
310*2175Sjp161948This section will explain the basic things an application programmer should
311*2175Sjp161948support to make the most useful elements of the ENGINE functionality
312*2175Sjp161948available to the user. The first thing to consider is whether the
313*2175Sjp161948programmer wishes to make alternative ENGINE modules available to the
314*2175Sjp161948application and user. OpenSSL maintains an internal linked list of
315*2175Sjp161948"visible" ENGINEs from which it has to operate - at start-up, this list is
316*2175Sjp161948empty and in fact if an application does not call any ENGINE API calls and
317*2175Sjp161948it uses static linking against openssl, then the resulting application
318*2175Sjp161948binary will not contain any alternative ENGINE code at all. So the first
319*2175Sjp161948consideration is whether any/all available ENGINE implementations should be
320*2175Sjp161948made visible to OpenSSL - this is controlled by calling the various "load"
321*2175Sjp161948functions, eg.
322*2175Sjp161948
323*2175Sjp161948 /* Make the "dynamic" ENGINE available */
324*2175Sjp161948 void ENGINE_load_dynamic(void);
325*2175Sjp161948 /* Make the CryptoSwift hardware acceleration support available */
326*2175Sjp161948 void ENGINE_load_cswift(void);
327*2175Sjp161948 /* Make support for nCipher's "CHIL" hardware available */
328*2175Sjp161948 void ENGINE_load_chil(void);
329*2175Sjp161948 ...
330*2175Sjp161948 /* Make ALL ENGINE implementations bundled with OpenSSL available */
331*2175Sjp161948 void ENGINE_load_builtin_engines(void);
332*2175Sjp161948
333*2175Sjp161948Having called any of these functions, ENGINE objects would have been
334*2175Sjp161948dynamically allocated and populated with these implementations and linked
335*2175Sjp161948into OpenSSL's internal linked list. At this point it is important to
336*2175Sjp161948mention an important API function;
337*2175Sjp161948
338*2175Sjp161948 void ENGINE_cleanup(void);
339*2175Sjp161948
340*2175Sjp161948If no ENGINE API functions are called at all in an application, then there
341*2175Sjp161948are no inherent memory leaks to worry about from the ENGINE functionality,
342*2175Sjp161948however if any ENGINEs are loaded, even if they are never registered or
343*2175Sjp161948used, it is necessary to use the ENGINE_cleanup() function to
344*2175Sjp161948correspondingly cleanup before program exit, if the caller wishes to avoid
345*2175Sjp161948memory leaks. This mechanism uses an internal callback registration table
346*2175Sjp161948so that any ENGINE API functionality that knows it requires cleanup can
347*2175Sjp161948register its cleanup details to be called during ENGINE_cleanup(). This
348*2175Sjp161948approach allows ENGINE_cleanup() to clean up after any ENGINE functionality
349*2175Sjp161948at all that your program uses, yet doesn't automatically create linker
350*2175Sjp161948dependencies to all possible ENGINE functionality - only the cleanup
351*2175Sjp161948callbacks required by the functionality you do use will be required by the
352*2175Sjp161948linker.
353*2175Sjp161948
354*2175Sjp161948The fact that ENGINEs are made visible to OpenSSL (and thus are linked into
355*2175Sjp161948the program and loaded into memory at run-time) does not mean they are
356*2175Sjp161948"registered" or called into use by OpenSSL automatically - that behaviour
357*2175Sjp161948is something for the application to control. Some applications
358*2175Sjp161948will want to allow the user to specify exactly which ENGINE they want used
359*2175Sjp161948if any is to be used at all. Others may prefer to load all support and have
360*2175Sjp161948OpenSSL automatically use at run-time any ENGINE that is able to
361*2175Sjp161948successfully initialise - ie. to assume that this corresponds to
362*2175Sjp161948acceleration hardware attached to the machine or some such thing. There are
363*2175Sjp161948probably numerous other ways in which applications may prefer to handle
364*2175Sjp161948things, so we will simply illustrate the consequences as they apply to a
365*2175Sjp161948couple of simple cases and leave developers to consider these and the
366*2175Sjp161948source code to openssl's builtin utilities as guides.
367*2175Sjp161948
368*2175Sjp161948I<Using a specific ENGINE implementation>
369*2175Sjp161948
370*2175Sjp161948Here we'll assume an application has been configured by its user or admin
371*2175Sjp161948to want to use the "ACME" ENGINE if it is available in the version of
372*2175Sjp161948OpenSSL the application was compiled with. If it is available, it should be
373*2175Sjp161948used by default for all RSA, DSA, and symmetric cipher operation, otherwise
374*2175Sjp161948OpenSSL should use its builtin software as per usual. The following code
375*2175Sjp161948illustrates how to approach this;
376*2175Sjp161948
377*2175Sjp161948 ENGINE *e;
378*2175Sjp161948 const char *engine_id = "ACME";
379*2175Sjp161948 ENGINE_load_builtin_engines();
380*2175Sjp161948 e = ENGINE_by_id(engine_id);
381*2175Sjp161948 if(!e)
382*2175Sjp161948     /* the engine isn't available */
383*2175Sjp161948     return;
384*2175Sjp161948 if(!ENGINE_init(e)) {
385*2175Sjp161948     /* the engine couldn't initialise, release 'e' */
386*2175Sjp161948     ENGINE_free(e);
387*2175Sjp161948     return;
388*2175Sjp161948 }
389*2175Sjp161948 if(!ENGINE_set_default_RSA(e))
390*2175Sjp161948     /* This should only happen when 'e' can't initialise, but the previous
391*2175Sjp161948      * statement suggests it did. */
392*2175Sjp161948     abort();
393*2175Sjp161948 ENGINE_set_default_DSA(e);
394*2175Sjp161948 ENGINE_set_default_ciphers(e);
395*2175Sjp161948 /* Release the functional reference from ENGINE_init() */
396*2175Sjp161948 ENGINE_finish(e);
397*2175Sjp161948 /* Release the structural reference from ENGINE_by_id() */
398*2175Sjp161948 ENGINE_free(e);
399*2175Sjp161948
400*2175Sjp161948I<Automatically using builtin ENGINE implementations>
401*2175Sjp161948
402*2175Sjp161948Here we'll assume we want to load and register all ENGINE implementations
403*2175Sjp161948bundled with OpenSSL, such that for any cryptographic algorithm required by
404*2175Sjp161948OpenSSL - if there is an ENGINE that implements it and can be initialise,
405*2175Sjp161948it should be used. The following code illustrates how this can work;
406*2175Sjp161948
407*2175Sjp161948 /* Load all bundled ENGINEs into memory and make them visible */
408*2175Sjp161948 ENGINE_load_builtin_engines();
409*2175Sjp161948 /* Register all of them for every algorithm they collectively implement */
410*2175Sjp161948 ENGINE_register_all_complete();
411*2175Sjp161948
412*2175Sjp161948That's all that's required. Eg. the next time OpenSSL tries to set up an
413*2175Sjp161948RSA key, any bundled ENGINEs that implement RSA_METHOD will be passed to
414*2175Sjp161948ENGINE_init() and if any of those succeed, that ENGINE will be set as the
415*2175Sjp161948default for RSA use from then on.
416*2175Sjp161948
417*2175Sjp161948=head2 Advanced configuration support
418*2175Sjp161948
419*2175Sjp161948There is a mechanism supported by the ENGINE framework that allows each
420*2175Sjp161948ENGINE implementation to define an arbitrary set of configuration
421*2175Sjp161948"commands" and expose them to OpenSSL and any applications based on
422*2175Sjp161948OpenSSL. This mechanism is entirely based on the use of name-value pairs
423*2175Sjp161948and assumes ASCII input (no unicode or UTF for now!), so it is ideal if
424*2175Sjp161948applications want to provide a transparent way for users to provide
425*2175Sjp161948arbitrary configuration "directives" directly to such ENGINEs. It is also
426*2175Sjp161948possible for the application to dynamically interrogate the loaded ENGINE
427*2175Sjp161948implementations for the names, descriptions, and input flags of their
428*2175Sjp161948available "control commands", providing a more flexible configuration
429*2175Sjp161948scheme. However, if the user is expected to know which ENGINE device he/she
430*2175Sjp161948is using (in the case of specialised hardware, this goes without saying)
431*2175Sjp161948then applications may not need to concern themselves with discovering the
432*2175Sjp161948supported control commands and simply prefer to pass settings into ENGINEs
433*2175Sjp161948exactly as they are provided by the user.
434*2175Sjp161948
435*2175Sjp161948Before illustrating how control commands work, it is worth mentioning what
436*2175Sjp161948they are typically used for. Broadly speaking there are two uses for
437*2175Sjp161948control commands; the first is to provide the necessary details to the
438*2175Sjp161948implementation (which may know nothing at all specific to the host system)
439*2175Sjp161948so that it can be initialised for use. This could include the path to any
440*2175Sjp161948driver or config files it needs to load, required network addresses,
441*2175Sjp161948smart-card identifiers, passwords to initialise protected devices,
442*2175Sjp161948logging information, etc etc. This class of commands typically needs to be
443*2175Sjp161948passed to an ENGINE B<before> attempting to initialise it, ie. before
444*2175Sjp161948calling ENGINE_init(). The other class of commands consist of settings or
445*2175Sjp161948operations that tweak certain behaviour or cause certain operations to take
446*2175Sjp161948place, and these commands may work either before or after ENGINE_init(), or
447*2175Sjp161948in some cases both. ENGINE implementations should provide indications of
448*2175Sjp161948this in the descriptions attached to builtin control commands and/or in
449*2175Sjp161948external product documentation.
450*2175Sjp161948
451*2175Sjp161948I<Issuing control commands to an ENGINE>
452*2175Sjp161948
453*2175Sjp161948Let's illustrate by example; a function for which the caller supplies the
454*2175Sjp161948name of the ENGINE it wishes to use, a table of string-pairs for use before
455*2175Sjp161948initialisation, and another table for use after initialisation. Note that
456*2175Sjp161948the string-pairs used for control commands consist of a command "name"
457*2175Sjp161948followed by the command "parameter" - the parameter could be NULL in some
458*2175Sjp161948cases but the name can not. This function should initialise the ENGINE
459*2175Sjp161948(issuing the "pre" commands beforehand and the "post" commands afterwards)
460*2175Sjp161948and set it as the default for everything except RAND and then return a
461*2175Sjp161948boolean success or failure.
462*2175Sjp161948
463*2175Sjp161948 int generic_load_engine_fn(const char *engine_id,
464*2175Sjp161948                            const char **pre_cmds, int pre_num,
465*2175Sjp161948                            const char **post_cmds, int post_num)
466*2175Sjp161948 {
467*2175Sjp161948     ENGINE *e = ENGINE_by_id(engine_id);
468*2175Sjp161948     if(!e) return 0;
469*2175Sjp161948     while(pre_num--) {
470*2175Sjp161948         if(!ENGINE_ctrl_cmd_string(e, pre_cmds[0], pre_cmds[1], 0)) {
471*2175Sjp161948             fprintf(stderr, "Failed command (%s - %s:%s)\n", engine_id,
472*2175Sjp161948                 pre_cmds[0], pre_cmds[1] ? pre_cmds[1] : "(NULL)");
473*2175Sjp161948             ENGINE_free(e);
474*2175Sjp161948             return 0;
475*2175Sjp161948         }
476*2175Sjp161948	 pre_cmds += 2;
477*2175Sjp161948     }
478*2175Sjp161948     if(!ENGINE_init(e)) {
479*2175Sjp161948         fprintf(stderr, "Failed initialisation\n");
480*2175Sjp161948         ENGINE_free(e);
481*2175Sjp161948         return 0;
482*2175Sjp161948     }
483*2175Sjp161948     /* ENGINE_init() returned a functional reference, so free the structural
484*2175Sjp161948      * reference from ENGINE_by_id(). */
485*2175Sjp161948     ENGINE_free(e);
486*2175Sjp161948     while(post_num--) {
487*2175Sjp161948         if(!ENGINE_ctrl_cmd_string(e, post_cmds[0], post_cmds[1], 0)) {
488*2175Sjp161948             fprintf(stderr, "Failed command (%s - %s:%s)\n", engine_id,
489*2175Sjp161948                 post_cmds[0], post_cmds[1] ? post_cmds[1] : "(NULL)");
490*2175Sjp161948             ENGINE_finish(e);
491*2175Sjp161948             return 0;
492*2175Sjp161948         }
493*2175Sjp161948	 post_cmds += 2;
494*2175Sjp161948     }
495*2175Sjp161948     ENGINE_set_default(e, ENGINE_METHOD_ALL & ~ENGINE_METHOD_RAND);
496*2175Sjp161948     /* Success */
497*2175Sjp161948     return 1;
498*2175Sjp161948 }
499*2175Sjp161948
500*2175Sjp161948Note that ENGINE_ctrl_cmd_string() accepts a boolean argument that can
501*2175Sjp161948relax the semantics of the function - if set non-zero it will only return
502*2175Sjp161948failure if the ENGINE supported the given command name but failed while
503*2175Sjp161948executing it, if the ENGINE doesn't support the command name it will simply
504*2175Sjp161948return success without doing anything. In this case we assume the user is
505*2175Sjp161948only supplying commands specific to the given ENGINE so we set this to
506*2175Sjp161948FALSE.
507*2175Sjp161948
508*2175Sjp161948I<Discovering supported control commands>
509*2175Sjp161948
510*2175Sjp161948It is possible to discover at run-time the names, numerical-ids, descriptions
511*2175Sjp161948and input parameters of the control commands supported by an ENGINE using a
512*2175Sjp161948structural reference. Note that some control commands are defined by OpenSSL
513*2175Sjp161948itself and it will intercept and handle these control commands on behalf of the
514*2175Sjp161948ENGINE, ie. the ENGINE's ctrl() handler is not used for the control command.
515*2175Sjp161948openssl/engine.h defines an index, ENGINE_CMD_BASE, that all control commands
516*2175Sjp161948implemented by ENGINEs should be numbered from. Any command value lower than
517*2175Sjp161948this symbol is considered a "generic" command is handled directly by the
518*2175Sjp161948OpenSSL core routines.
519*2175Sjp161948
520*2175Sjp161948It is using these "core" control commands that one can discover the the control
521*2175Sjp161948commands implemented by a given ENGINE, specifically the commands;
522*2175Sjp161948
523*2175Sjp161948 #define ENGINE_HAS_CTRL_FUNCTION		10
524*2175Sjp161948 #define ENGINE_CTRL_GET_FIRST_CMD_TYPE		11
525*2175Sjp161948 #define ENGINE_CTRL_GET_NEXT_CMD_TYPE		12
526*2175Sjp161948 #define ENGINE_CTRL_GET_CMD_FROM_NAME		13
527*2175Sjp161948 #define ENGINE_CTRL_GET_NAME_LEN_FROM_CMD	14
528*2175Sjp161948 #define ENGINE_CTRL_GET_NAME_FROM_CMD		15
529*2175Sjp161948 #define ENGINE_CTRL_GET_DESC_LEN_FROM_CMD	16
530*2175Sjp161948 #define ENGINE_CTRL_GET_DESC_FROM_CMD		17
531*2175Sjp161948 #define ENGINE_CTRL_GET_CMD_FLAGS		18
532*2175Sjp161948
533*2175Sjp161948Whilst these commands are automatically processed by the OpenSSL framework code,
534*2175Sjp161948they use various properties exposed by each ENGINE to process these
535*2175Sjp161948queries. An ENGINE has 3 properties it exposes that can affect how this behaves;
536*2175Sjp161948it can supply a ctrl() handler, it can specify ENGINE_FLAGS_MANUAL_CMD_CTRL in
537*2175Sjp161948the ENGINE's flags, and it can expose an array of control command descriptions.
538*2175Sjp161948If an ENGINE specifies the ENGINE_FLAGS_MANUAL_CMD_CTRL flag, then it will
539*2175Sjp161948simply pass all these "core" control commands directly to the ENGINE's ctrl()
540*2175Sjp161948handler (and thus, it must have supplied one), so it is up to the ENGINE to
541*2175Sjp161948reply to these "discovery" commands itself. If that flag is not set, then the
542*2175Sjp161948OpenSSL framework code will work with the following rules;
543*2175Sjp161948
544*2175Sjp161948 if no ctrl() handler supplied;
545*2175Sjp161948     ENGINE_HAS_CTRL_FUNCTION returns FALSE (zero),
546*2175Sjp161948     all other commands fail.
547*2175Sjp161948 if a ctrl() handler was supplied but no array of control commands;
548*2175Sjp161948     ENGINE_HAS_CTRL_FUNCTION returns TRUE,
549*2175Sjp161948     all other commands fail.
550*2175Sjp161948 if a ctrl() handler and array of control commands was supplied;
551*2175Sjp161948     ENGINE_HAS_CTRL_FUNCTION returns TRUE,
552*2175Sjp161948     all other commands proceed processing ...
553*2175Sjp161948
554*2175Sjp161948If the ENGINE's array of control commands is empty then all other commands will
555*2175Sjp161948fail, otherwise; ENGINE_CTRL_GET_FIRST_CMD_TYPE returns the identifier of
556*2175Sjp161948the first command supported by the ENGINE, ENGINE_GET_NEXT_CMD_TYPE takes the
557*2175Sjp161948identifier of a command supported by the ENGINE and returns the next command
558*2175Sjp161948identifier or fails if there are no more, ENGINE_CMD_FROM_NAME takes a string
559*2175Sjp161948name for a command and returns the corresponding identifier or fails if no such
560*2175Sjp161948command name exists, and the remaining commands take a command identifier and
561*2175Sjp161948return properties of the corresponding commands. All except
562*2175Sjp161948ENGINE_CTRL_GET_FLAGS return the string length of a command name or description,
563*2175Sjp161948or populate a supplied character buffer with a copy of the command name or
564*2175Sjp161948description. ENGINE_CTRL_GET_FLAGS returns a bitwise-OR'd mask of the following
565*2175Sjp161948possible values;
566*2175Sjp161948
567*2175Sjp161948 #define ENGINE_CMD_FLAG_NUMERIC		(unsigned int)0x0001
568*2175Sjp161948 #define ENGINE_CMD_FLAG_STRING			(unsigned int)0x0002
569*2175Sjp161948 #define ENGINE_CMD_FLAG_NO_INPUT		(unsigned int)0x0004
570*2175Sjp161948 #define ENGINE_CMD_FLAG_INTERNAL		(unsigned int)0x0008
571*2175Sjp161948
572*2175Sjp161948If the ENGINE_CMD_FLAG_INTERNAL flag is set, then any other flags are purely
573*2175Sjp161948informational to the caller - this flag will prevent the command being usable
574*2175Sjp161948for any higher-level ENGINE functions such as ENGINE_ctrl_cmd_string().
575*2175Sjp161948"INTERNAL" commands are not intended to be exposed to text-based configuration
576*2175Sjp161948by applications, administrations, users, etc. These can support arbitrary
577*2175Sjp161948operations via ENGINE_ctrl(), including passing to and/or from the control
578*2175Sjp161948commands data of any arbitrary type. These commands are supported in the
579*2175Sjp161948discovery mechanisms simply to allow applications determinie if an ENGINE
580*2175Sjp161948supports certain specific commands it might want to use (eg. application "foo"
581*2175Sjp161948might query various ENGINEs to see if they implement "FOO_GET_VENDOR_LOGO_GIF" -
582*2175Sjp161948and ENGINE could therefore decide whether or not to support this "foo"-specific
583*2175Sjp161948extension).
584*2175Sjp161948
585*2175Sjp161948=head2 Future developments
586*2175Sjp161948
587*2175Sjp161948The ENGINE API and internal architecture is currently being reviewed. Slated for
588*2175Sjp161948possible release in 0.9.8 is support for transparent loading of "dynamic"
589*2175Sjp161948ENGINEs (built as self-contained shared-libraries). This would allow ENGINE
590*2175Sjp161948implementations to be provided independantly of OpenSSL libraries and/or
591*2175Sjp161948OpenSSL-based applications, and would also remove any requirement for
592*2175Sjp161948applications to explicitly use the "dynamic" ENGINE to bind to shared-library
593*2175Sjp161948implementations.
594*2175Sjp161948
595*2175Sjp161948=head1 SEE ALSO
596*2175Sjp161948
597*2175Sjp161948L<rsa(3)|rsa(3)>, L<dsa(3)|dsa(3)>, L<dh(3)|dh(3)>, L<rand(3)|rand(3)>
598*2175Sjp161948
599*2175Sjp161948=cut
600