Some more tweaks to ENGINE code.
[openssl.git] / crypto / engine / engine_lib.c
1 /* crypto/engine/engine_lib.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3  * project 2000.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <openssl/crypto.h>
60 #include "cryptlib.h"
61 #include "engine_int.h"
62 #include <openssl/engine.h>
63
64 /* These pointers each have their own "functional reference" when they
65  * are non-NULL. Similarly, when they are retrieved by a call to
66  * ENGINE_get_default_[RSA|DSA|...] the returned pointer is also a
67  * reference and the caller is responsible for freeing that when they
68  * are finished with it (with a call to ENGINE_finish() *NOT* just
69  * ENGINE_free()!!!!!!). */
70 static ENGINE *engine_def_rsa = NULL;
71 static ENGINE *engine_def_dsa = NULL;
72 static ENGINE *engine_def_dh = NULL;
73 static ENGINE *engine_def_rand = NULL;
74 static ENGINE *engine_def_bn_mod_exp = NULL;
75 static ENGINE *engine_def_bn_mod_exp_crt = NULL;
76 /* A static "once-only" flag used to control if/when the above were
77  * initialised to suitable start-up defaults. */
78 static int engine_def_flag = 0;
79
80 /* This is used in certain static utility functions to save code
81  * repetition for per-algorithm functions. */
82 typedef enum {
83         ENGINE_TYPE_RSA,
84         ENGINE_TYPE_DSA,
85         ENGINE_TYPE_DH,
86         ENGINE_TYPE_RAND,
87         ENGINE_TYPE_BN_MOD_EXP,
88         ENGINE_TYPE_BN_MOD_EXP_CRT
89         } ENGINE_TYPE;
90
91 static void engine_def_check_util(ENGINE **def, ENGINE *val)
92         {
93         *def = val;
94         val->struct_ref++;
95         val->funct_ref++;
96         }
97
98 /* In a slight break with convention - this static function must be
99  * called *outside* any locking of CRYPTO_LOCK_ENGINE. */
100 static void engine_def_check(void)
101         {
102         ENGINE *e;
103         if(engine_def_flag)
104                 return;
105         e = ENGINE_get_first();
106         if(e == NULL)
107                 /* The list is empty ... not much we can do! */
108                 return;
109         /* We have a structural reference, see if getting a functional
110          * reference is possible. This is done to cope with init errors
111          * in the engine - the following locked code does a bunch of
112          * manual "ENGINE_init"s which do *not* allow such an init
113          * error so this is worth doing. */
114         if(ENGINE_init(e))
115                 {
116                 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
117                 /* Doing another check here prevents an obvious race
118                  * condition because the whole function itself cannot
119                  * be locked. */
120                 if(engine_def_flag)
121                         goto skip_set_defaults;
122                 /* OK, we got a functional reference, so we get one each
123                  * for the defaults too. */
124                 engine_def_check_util(&engine_def_rsa, e);
125                 engine_def_check_util(&engine_def_dsa, e);
126                 engine_def_check_util(&engine_def_dh, e);
127                 engine_def_check_util(&engine_def_rand, e);
128                 engine_def_check_util(&engine_def_bn_mod_exp, e);
129                 engine_def_check_util(&engine_def_bn_mod_exp_crt, e);
130                 engine_def_flag = 1;
131 skip_set_defaults:
132                 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
133                 /* The "if" needs to be balanced out. */
134                 ENGINE_finish(e);
135                 }
136         /* We need to balance out the fact we obtained a structural
137          * reference to begin with from ENGINE_get_first(). */
138         ENGINE_free(e);
139         }
140
141 /* Initialise a engine type for use (or up its functional reference count
142  * if it's already in use). */
143 int ENGINE_init(ENGINE *e)
144         {
145         int to_return = 1;
146
147         if(e == NULL)
148                 {
149                 ENGINEerr(ENGINE_F_ENGINE_INIT,ERR_R_PASSED_NULL_PARAMETER);
150                 return 0;
151                 }
152         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
153         if((e->funct_ref == 0) && e->init)
154                 /* This is the first functional reference and the engine
155                  * requires initialisation so we do it now. */
156                 to_return = e->init(e);
157         if(to_return)
158                 {
159                 /* OK, we return a functional reference which is also a
160                  * structural reference. */
161                 e->struct_ref++;
162                 e->funct_ref++;
163                 }
164         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
165         return to_return;
166         }
167
168 /* Free a functional reference to a engine type */
169 int ENGINE_finish(ENGINE *e)
170         {
171         int to_return = 1;
172
173         if(e == NULL)
174                 {
175                 ENGINEerr(ENGINE_F_ENGINE_FINISH,ERR_R_PASSED_NULL_PARAMETER);
176                 return 0;
177                 }
178         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
179         if((e->funct_ref == 1) && e->finish)
180 #if 0
181                 /* This is the last functional reference and the engine
182                  * requires cleanup so we do it now. */
183                 to_return = e->finish();
184         if(to_return)
185                 {
186                 /* Cleanup the functional reference which is also a
187                  * structural reference. */
188                 e->struct_ref--;
189                 e->funct_ref--;
190                 }
191         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
192 #else
193                 /* I'm going to deliberately do a convoluted version of this
194                  * piece of code because we don't want "finish" functions
195                  * being called inside a locked block of code, if at all
196                  * possible. I'd rather have this call take an extra couple
197                  * of ticks than have throughput serialised on a externally-
198                  * provided callback function that may conceivably never come
199                  * back. :-( */
200                 {
201                 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
202                 /* CODE ALERT: This *IS* supposed to be "=" and NOT "==" :-) */
203                 if((to_return = e->finish(e)))
204                         {
205                         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
206                         /* Cleanup the functional reference which is also a
207                          * structural reference. */
208                         e->struct_ref--;
209                         e->funct_ref--;
210                         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
211                         }
212                 }
213         else
214                 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
215 #endif
216         return to_return;
217         }
218
219 EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
220         const char *passphrase)
221         {
222         EVP_PKEY *pkey;
223
224         if(e == NULL)
225                 {
226                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
227                         ERR_R_PASSED_NULL_PARAMETER);
228                 return 0;
229                 }
230         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
231         if(e->funct_ref == 0)
232                 {
233                 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
234                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
235                         ENGINE_R_NOT_INITIALISED);
236                 return 0;
237                 }
238         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
239         if (!e->load_privkey)
240                 {
241                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
242                         ENGINE_R_NO_LOAD_FUNCTION);
243                 return 0;
244                 }
245         pkey = e->load_privkey(e, key_id, passphrase);
246         if (!pkey)
247                 {
248                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
249                         ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
250                 return 0;
251                 }
252         return pkey;
253         }
254
255 EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
256         const char *passphrase)
257         {
258         EVP_PKEY *pkey;
259
260         if(e == NULL)
261                 {
262                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
263                         ERR_R_PASSED_NULL_PARAMETER);
264                 return 0;
265                 }
266         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
267         if(e->funct_ref == 0)
268                 {
269                 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
270                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
271                         ENGINE_R_NOT_INITIALISED);
272                 return 0;
273                 }
274         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
275         if (!e->load_pubkey)
276                 {
277                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
278                         ENGINE_R_NO_LOAD_FUNCTION);
279                 return 0;
280                 }
281         pkey = e->load_pubkey(e, key_id, passphrase);
282         if (!pkey)
283                 {
284                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
285                         ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
286                 return 0;
287                 }
288         return pkey;
289         }
290
291 int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
292         {
293         int ctrl_exists, ref_exists;
294         if(e == NULL)
295                 {
296                 ENGINEerr(ENGINE_F_ENGINE_CTRL,ERR_R_PASSED_NULL_PARAMETER);
297                 return 0;
298                 }
299         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
300         ref_exists = ((e->struct_ref > 0) ? 1 : 0);
301         ctrl_exists = (e->ctrl ? 1 : 0);
302         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
303         if(!ref_exists)
304                 {
305                 ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_REFERENCE);
306                 return 0;
307                 }
308         if (!ctrl_exists)
309                 {
310                 ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_CONTROL_FUNCTION);
311                 return 0;
312                 }
313         return e->ctrl(e, cmd, i, p, f);
314         }
315
316 static ENGINE *engine_get_default_type(ENGINE_TYPE t)
317         {
318         ENGINE *ret = NULL;
319
320         /* engine_def_check is lean and mean and won't replace any
321          * prior default engines ... so we must ensure that it is always
322          * the first function to get to touch the default values. */
323         engine_def_check();
324         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
325         switch(t)
326                 {
327         case ENGINE_TYPE_RSA:
328                 ret = engine_def_rsa; break;
329         case ENGINE_TYPE_DSA:
330                 ret = engine_def_dsa; break;
331         case ENGINE_TYPE_DH:
332                 ret = engine_def_dh; break;
333         case ENGINE_TYPE_RAND:
334                 ret = engine_def_rand; break;
335         case ENGINE_TYPE_BN_MOD_EXP:
336                 ret = engine_def_bn_mod_exp; break;
337         case ENGINE_TYPE_BN_MOD_EXP_CRT:
338                 ret = engine_def_bn_mod_exp_crt; break;
339                 }
340         /* Unforunately we can't do this work outside the lock with a
341          * call to ENGINE_init() because that would leave a race
342          * condition open. */
343         if(ret)
344                 {
345                 ret->struct_ref++;
346                 ret->funct_ref++;
347                 }
348         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
349         return ret;
350         }
351
352 ENGINE *ENGINE_get_default_RSA(void)
353         {
354         return engine_get_default_type(ENGINE_TYPE_RSA);
355         }
356
357 ENGINE *ENGINE_get_default_DSA(void)
358         {
359         return engine_get_default_type(ENGINE_TYPE_DSA);
360         }
361
362 ENGINE *ENGINE_get_default_DH(void)
363         {
364         return engine_get_default_type(ENGINE_TYPE_DH);
365         }
366
367 ENGINE *ENGINE_get_default_RAND(void)
368         {
369         return engine_get_default_type(ENGINE_TYPE_RAND);
370         }
371
372 ENGINE *ENGINE_get_default_BN_mod_exp(void)
373         {
374         return engine_get_default_type(ENGINE_TYPE_BN_MOD_EXP);
375         }
376
377 ENGINE *ENGINE_get_default_BN_mod_exp_crt(void)
378         {
379         return engine_get_default_type(ENGINE_TYPE_BN_MOD_EXP_CRT);
380         }
381
382 static int engine_set_default_type(ENGINE_TYPE t, ENGINE *e)
383         {
384         ENGINE *old = NULL;
385
386         if(e == NULL)
387                 {
388                 ENGINEerr(ENGINE_F_ENGINE_SET_DEFAULT_TYPE,
389                         ERR_R_PASSED_NULL_PARAMETER);
390                 return 0;
391                 }
392         /* engine_def_check is lean and mean and won't replace any
393          * prior default engines ... so we must ensure that it is always
394          * the first function to get to touch the default values. */
395         engine_def_check();
396         /* Attempt to get a functional reference (we need one anyway, but
397          * also, 'e' may be just a structural reference being passed in so
398          * this call may actually be the first). */
399         if(!ENGINE_init(e))
400                 {
401                 ENGINEerr(ENGINE_F_ENGINE_SET_DEFAULT_TYPE,
402                         ENGINE_R_INIT_FAILED);
403                 return 0;
404                 }
405         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
406         switch(t)
407                 {
408         case ENGINE_TYPE_RSA:
409                 old = engine_def_rsa;
410                 engine_def_rsa = e; break;
411         case ENGINE_TYPE_DSA:
412                 old = engine_def_dsa;
413                 engine_def_dsa = e; break;
414         case ENGINE_TYPE_DH:
415                 old = engine_def_dh;
416                 engine_def_dh = e; break;
417         case ENGINE_TYPE_RAND:
418                 old = engine_def_rand;
419                 engine_def_rand = e; break;
420         case ENGINE_TYPE_BN_MOD_EXP:
421                 old = engine_def_bn_mod_exp;
422                 engine_def_bn_mod_exp = e; break;
423         case ENGINE_TYPE_BN_MOD_EXP_CRT:
424                 old = engine_def_bn_mod_exp_crt;
425                 engine_def_bn_mod_exp_crt = e; break;
426                 }
427         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
428         /* If we've replaced a previous value, then we need to remove the
429          * functional reference we had. */
430         if(old && !ENGINE_finish(old))
431                 {
432                 ENGINEerr(ENGINE_F_ENGINE_SET_DEFAULT_TYPE,
433                         ENGINE_R_FINISH_FAILED);
434                 return 0;
435                 }
436         return 1;
437         }
438
439 int ENGINE_set_default_RSA(ENGINE *e)
440         {
441         return engine_set_default_type(ENGINE_TYPE_RSA, e);
442         }
443
444 int ENGINE_set_default_DSA(ENGINE *e)
445         {
446         return engine_set_default_type(ENGINE_TYPE_DSA, e);
447         }
448
449 int ENGINE_set_default_DH(ENGINE *e)
450         {
451         return engine_set_default_type(ENGINE_TYPE_DH, e);
452         }
453
454 int ENGINE_set_default_RAND(ENGINE *e)
455         {
456         return engine_set_default_type(ENGINE_TYPE_RAND, e);
457         }
458
459 int ENGINE_set_default_BN_mod_exp(ENGINE *e)
460         {
461         return engine_set_default_type(ENGINE_TYPE_BN_MOD_EXP, e);
462         }
463
464 int ENGINE_set_default_BN_mod_exp_crt(ENGINE *e)
465         {
466         return engine_set_default_type(ENGINE_TYPE_BN_MOD_EXP_CRT, e);
467         }
468
469 int ENGINE_set_default(ENGINE *e, unsigned int flags)
470         {
471         if((flags & ENGINE_METHOD_RSA) && e->rsa_meth &&
472                         !ENGINE_set_default_RSA(e))
473                 return 0;
474         if((flags & ENGINE_METHOD_DSA) && e->dsa_meth &&
475                         !ENGINE_set_default_DSA(e))
476                 return 0;
477         if((flags & ENGINE_METHOD_DH) && e->dh_meth &&
478                         !ENGINE_set_default_DH(e))
479                 return 0;
480         if((flags & ENGINE_METHOD_RAND) && e->rand_meth &&
481                         !ENGINE_set_default_RAND(e))
482                 return 0;
483         if((flags & ENGINE_METHOD_BN_MOD_EXP) && e->bn_mod_exp &&
484                         !ENGINE_set_default_BN_mod_exp(e))
485                 return 0;
486         if((flags & ENGINE_METHOD_BN_MOD_EXP_CRT) && e->bn_mod_exp_crt &&
487                         !ENGINE_set_default_BN_mod_exp_crt(e))
488                 return 0;
489         return 1;
490         }
491