Don't return an error until the global lock is released.
[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();
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()))
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                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
234                         ENGINE_R_NOT_INITIALISED);
235                 return 0;
236                 }
237         if (!e->load_privkey)
238                 {
239                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
240                         ENGINE_R_NO_LOAD_FUNCTION);
241                 return 0;
242                 }
243         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
244         pkey = e->load_privkey(key_id, passphrase);
245         if (!pkey)
246                 {
247                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
248                         ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
249                 return 0;
250                 }
251         return pkey;
252         }
253
254 EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
255         const char *passphrase)
256         {
257         EVP_PKEY *pkey;
258
259         if(e == NULL)
260                 {
261                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
262                         ERR_R_PASSED_NULL_PARAMETER);
263                 return 0;
264                 }
265         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
266         if(e->funct_ref == 0)
267                 {
268                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
269                         ENGINE_R_NOT_INITIALISED);
270                 return 0;
271                 }
272         if (!e->load_pubkey)
273                 {
274                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
275                         ENGINE_R_NO_LOAD_FUNCTION);
276                 return 0;
277                 }
278         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
279         pkey = e->load_pubkey(key_id, passphrase);
280         if (!pkey)
281                 {
282                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
283                         ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
284                 return 0;
285                 }
286         return pkey;
287         }
288
289 int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
290         {
291         int ctrl_exists;
292         if(e == NULL)
293                 {
294                 ENGINEerr(ENGINE_F_ENGINE_CTRL,ERR_R_PASSED_NULL_PARAMETER);
295                 return 0;
296                 }
297         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
298         if(e->struct_ref == 0)
299                 {
300                 ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_REFERENCE);
301                 return 0;
302                 }
303         ctrl_exists = (e->ctrl ? 1 : 0);
304         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
305         if (!ctrl_exists)
306                 {
307                 ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_CONTROL_FUNCTION);
308                 return 0;
309                 }
310         return e->ctrl(cmd, i, p, f);
311         }
312
313 static ENGINE *engine_get_default_type(ENGINE_TYPE t)
314         {
315         ENGINE *ret = NULL;
316
317         /* engine_def_check is lean and mean and won't replace any
318          * prior default engines ... so we must ensure that it is always
319          * the first function to get to touch the default values. */
320         engine_def_check();
321         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
322         switch(t)
323                 {
324         case ENGINE_TYPE_RSA:
325                 ret = engine_def_rsa; break;
326         case ENGINE_TYPE_DSA:
327                 ret = engine_def_dsa; break;
328         case ENGINE_TYPE_DH:
329                 ret = engine_def_dh; break;
330         case ENGINE_TYPE_RAND:
331                 ret = engine_def_rand; break;
332         case ENGINE_TYPE_BN_MOD_EXP:
333                 ret = engine_def_bn_mod_exp; break;
334         case ENGINE_TYPE_BN_MOD_EXP_CRT:
335                 ret = engine_def_bn_mod_exp_crt; break;
336                 }
337         /* Unforunately we can't do this work outside the lock with a
338          * call to ENGINE_init() because that would leave a race
339          * condition open. */
340         if(ret)
341                 {
342                 ret->struct_ref++;
343                 ret->funct_ref++;
344                 }
345         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
346         return ret;
347         }
348
349 ENGINE *ENGINE_get_default_RSA(void)
350         {
351         return engine_get_default_type(ENGINE_TYPE_RSA);
352         }
353
354 ENGINE *ENGINE_get_default_DSA(void)
355         {
356         return engine_get_default_type(ENGINE_TYPE_DSA);
357         }
358
359 ENGINE *ENGINE_get_default_DH(void)
360         {
361         return engine_get_default_type(ENGINE_TYPE_DH);
362         }
363
364 ENGINE *ENGINE_get_default_RAND(void)
365         {
366         return engine_get_default_type(ENGINE_TYPE_RAND);
367         }
368
369 ENGINE *ENGINE_get_default_BN_mod_exp(void)
370         {
371         return engine_get_default_type(ENGINE_TYPE_BN_MOD_EXP);
372         }
373
374 ENGINE *ENGINE_get_default_BN_mod_exp_crt(void)
375         {
376         return engine_get_default_type(ENGINE_TYPE_BN_MOD_EXP_CRT);
377         }
378
379 static int engine_set_default_type(ENGINE_TYPE t, ENGINE *e)
380         {
381         ENGINE *old = NULL;
382
383         if(e == NULL)
384                 {
385                 ENGINEerr(ENGINE_F_ENGINE_SET_DEFAULT_TYPE,
386                         ERR_R_PASSED_NULL_PARAMETER);
387                 return 0;
388                 }
389         /* engine_def_check is lean and mean and won't replace any
390          * prior default engines ... so we must ensure that it is always
391          * the first function to get to touch the default values. */
392         engine_def_check();
393         /* Attempt to get a functional reference (we need one anyway, but
394          * also, 'e' may be just a structural reference being passed in so
395          * this call may actually be the first). */
396         if(!ENGINE_init(e))
397                 {
398                 ENGINEerr(ENGINE_F_ENGINE_SET_DEFAULT_TYPE,
399                         ENGINE_R_INIT_FAILED);
400                 return 0;
401                 }
402         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
403         switch(t)
404                 {
405         case ENGINE_TYPE_RSA:
406                 old = engine_def_rsa;
407                 engine_def_rsa = e; break;
408         case ENGINE_TYPE_DSA:
409                 old = engine_def_dsa;
410                 engine_def_dsa = e; break;
411         case ENGINE_TYPE_DH:
412                 old = engine_def_dh;
413                 engine_def_dh = e; break;
414         case ENGINE_TYPE_RAND:
415                 old = engine_def_rand;
416                 engine_def_rand = e; break;
417         case ENGINE_TYPE_BN_MOD_EXP:
418                 old = engine_def_bn_mod_exp;
419                 engine_def_bn_mod_exp = e; break;
420         case ENGINE_TYPE_BN_MOD_EXP_CRT:
421                 old = engine_def_bn_mod_exp_crt;
422                 engine_def_bn_mod_exp_crt = e; break;
423                 }
424         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
425         /* If we've replaced a previous value, then we need to remove the
426          * functional reference we had. */
427         if(old && !ENGINE_finish(old))
428                 {
429                 ENGINEerr(ENGINE_F_ENGINE_SET_DEFAULT_TYPE,
430                         ENGINE_R_FINISH_FAILED);
431                 return 0;
432                 }
433         return 1;
434         }
435
436 int ENGINE_set_default_RSA(ENGINE *e)
437         {
438         return engine_set_default_type(ENGINE_TYPE_RSA, e);
439         }
440
441 int ENGINE_set_default_DSA(ENGINE *e)
442         {
443         return engine_set_default_type(ENGINE_TYPE_DSA, e);
444         }
445
446 int ENGINE_set_default_DH(ENGINE *e)
447         {
448         return engine_set_default_type(ENGINE_TYPE_DH, e);
449         }
450
451 int ENGINE_set_default_RAND(ENGINE *e)
452         {
453         return engine_set_default_type(ENGINE_TYPE_RAND, e);
454         }
455
456 int ENGINE_set_default_BN_mod_exp(ENGINE *e)
457         {
458         return engine_set_default_type(ENGINE_TYPE_BN_MOD_EXP, e);
459         }
460
461 int ENGINE_set_default_BN_mod_exp_crt(ENGINE *e)
462         {
463         return engine_set_default_type(ENGINE_TYPE_BN_MOD_EXP_CRT, e);
464         }
465
466 int ENGINE_set_default(ENGINE *e, unsigned int flags)
467         {
468         if((flags & ENGINE_METHOD_RSA) && e->rsa_meth &&
469                         !ENGINE_set_default_RSA(e))
470                 return 0;
471         if((flags & ENGINE_METHOD_DSA) && e->dsa_meth &&
472                         !ENGINE_set_default_DSA(e))
473                 return 0;
474         if((flags & ENGINE_METHOD_DH) && e->dh_meth &&
475                         !ENGINE_set_default_DH(e))
476                 return 0;
477         if((flags & ENGINE_METHOD_RAND) && e->rand_meth &&
478                         !ENGINE_set_default_RAND(e))
479                 return 0;
480         if((flags & ENGINE_METHOD_BN_MOD_EXP) && e->bn_mod_exp &&
481                         !ENGINE_set_default_BN_mod_exp(e))
482                 return 0;
483         if((flags & ENGINE_METHOD_BN_MOD_EXP_CRT) && e->bn_mod_exp_crt &&
484                         !ENGINE_set_default_BN_mod_exp_crt(e))
485                 return 0;
486         return 1;
487         }
488