Some BIG 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 /* When querying a ENGINE-specific control command's 'description', this string
81  * is used if the ENGINE_CMD_DEFN has cmd_desc set to NULL. */
82 static const char *int_no_description = "";
83
84 /* This is used in certain static utility functions to save code
85  * repetition for per-algorithm functions. */
86 typedef enum {
87         ENGINE_TYPE_RSA,
88         ENGINE_TYPE_DSA,
89         ENGINE_TYPE_DH,
90         ENGINE_TYPE_RAND,
91         ENGINE_TYPE_BN_MOD_EXP,
92         ENGINE_TYPE_BN_MOD_EXP_CRT
93         } ENGINE_TYPE;
94
95 static void engine_def_check_util(ENGINE **def, ENGINE *val)
96         {
97         *def = val;
98         val->struct_ref++;
99         val->funct_ref++;
100         }
101
102 /* In a slight break with convention - this static function must be
103  * called *outside* any locking of CRYPTO_LOCK_ENGINE. */
104 static void engine_def_check(void)
105         {
106         ENGINE *e;
107         if(engine_def_flag)
108                 return;
109         e = ENGINE_get_first();
110         if(e == NULL)
111                 /* The list is empty ... not much we can do! */
112                 return;
113         /* We have a structural reference, see if getting a functional
114          * reference is possible. This is done to cope with init errors
115          * in the engine - the following locked code does a bunch of
116          * manual "ENGINE_init"s which do *not* allow such an init
117          * error so this is worth doing. */
118         if(ENGINE_init(e))
119                 {
120                 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
121                 /* Doing another check here prevents an obvious race
122                  * condition because the whole function itself cannot
123                  * be locked. */
124                 if(engine_def_flag)
125                         goto skip_set_defaults;
126                 /* OK, we got a functional reference, so we get one each
127                  * for the defaults too. */
128                 engine_def_check_util(&engine_def_rsa, e);
129                 engine_def_check_util(&engine_def_dsa, e);
130                 engine_def_check_util(&engine_def_dh, e);
131                 engine_def_check_util(&engine_def_rand, e);
132                 engine_def_check_util(&engine_def_bn_mod_exp, e);
133                 engine_def_check_util(&engine_def_bn_mod_exp_crt, e);
134                 engine_def_flag = 1;
135 skip_set_defaults:
136                 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
137                 /* The "if" needs to be balanced out. */
138                 ENGINE_finish(e);
139                 }
140         /* We need to balance out the fact we obtained a structural
141          * reference to begin with from ENGINE_get_first(). */
142         ENGINE_free(e);
143         }
144
145 /* Initialise a engine type for use (or up its functional reference count
146  * if it's already in use). */
147 int ENGINE_init(ENGINE *e)
148         {
149         int to_return = 1;
150
151         if(e == NULL)
152                 {
153                 ENGINEerr(ENGINE_F_ENGINE_INIT,ERR_R_PASSED_NULL_PARAMETER);
154                 return 0;
155                 }
156         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
157         if((e->funct_ref == 0) && e->init)
158                 /* This is the first functional reference and the engine
159                  * requires initialisation so we do it now. */
160                 to_return = e->init(e);
161         if(to_return)
162                 {
163                 /* OK, we return a functional reference which is also a
164                  * structural reference. */
165                 e->struct_ref++;
166                 e->funct_ref++;
167                 }
168         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
169         return to_return;
170         }
171
172 /* Free a functional reference to a engine type */
173 int ENGINE_finish(ENGINE *e)
174         {
175         int to_return = 1;
176
177         if(e == NULL)
178                 {
179                 ENGINEerr(ENGINE_F_ENGINE_FINISH,ERR_R_PASSED_NULL_PARAMETER);
180                 return 0;
181                 }
182         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
183         if((e->funct_ref == 1) && e->finish)
184 #if 0
185                 /* This is the last functional reference and the engine
186                  * requires cleanup so we do it now. */
187                 to_return = e->finish();
188         if(to_return)
189                 {
190                 /* Cleanup the functional reference which is also a
191                  * structural reference. */
192                 e->struct_ref--;
193                 e->funct_ref--;
194                 }
195         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
196 #else
197                 /* I'm going to deliberately do a convoluted version of this
198                  * piece of code because we don't want "finish" functions
199                  * being called inside a locked block of code, if at all
200                  * possible. I'd rather have this call take an extra couple
201                  * of ticks than have throughput serialised on a externally-
202                  * provided callback function that may conceivably never come
203                  * back. :-( */
204                 {
205                 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
206                 /* CODE ALERT: This *IS* supposed to be "=" and NOT "==" :-) */
207                 if((to_return = e->finish(e)))
208                         {
209                         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
210                         /* Cleanup the functional reference which is also a
211                          * structural reference. */
212                         e->struct_ref--;
213                         e->funct_ref--;
214                         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
215                         }
216                 }
217         else
218                 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
219 #endif
220         return to_return;
221         }
222
223 EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
224         const char *passphrase)
225         {
226         EVP_PKEY *pkey;
227
228         if(e == NULL)
229                 {
230                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
231                         ERR_R_PASSED_NULL_PARAMETER);
232                 return 0;
233                 }
234         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
235         if(e->funct_ref == 0)
236                 {
237                 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
238                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
239                         ENGINE_R_NOT_INITIALISED);
240                 return 0;
241                 }
242         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
243         if (!e->load_privkey)
244                 {
245                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
246                         ENGINE_R_NO_LOAD_FUNCTION);
247                 return 0;
248                 }
249         pkey = e->load_privkey(e, key_id, passphrase);
250         if (!pkey)
251                 {
252                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
253                         ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
254                 return 0;
255                 }
256         return pkey;
257         }
258
259 EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
260         const char *passphrase)
261         {
262         EVP_PKEY *pkey;
263
264         if(e == NULL)
265                 {
266                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
267                         ERR_R_PASSED_NULL_PARAMETER);
268                 return 0;
269                 }
270         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
271         if(e->funct_ref == 0)
272                 {
273                 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
274                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
275                         ENGINE_R_NOT_INITIALISED);
276                 return 0;
277                 }
278         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
279         if (!e->load_pubkey)
280                 {
281                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
282                         ENGINE_R_NO_LOAD_FUNCTION);
283                 return 0;
284                 }
285         pkey = e->load_pubkey(e, key_id, passphrase);
286         if (!pkey)
287                 {
288                 ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
289                         ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
290                 return 0;
291                 }
292         return pkey;
293         }
294
295 /* These internal functions handle 'CMD'-related control commands when the
296  * ENGINE in question has asked us to take care of it (ie. the ENGINE did not
297  * set the ENGINE_FLAGS_MANUAL_CMD_CTRL flag. */
298
299 static int int_ctrl_cmd_is_null(const ENGINE_CMD_DEFN *defn)
300         {
301         if((defn->cmd_num == 0) || (defn->cmd_name == NULL))
302                 return 1;
303         return 0;
304         }
305
306 static int int_ctrl_cmd_by_name(const ENGINE_CMD_DEFN *defn, const char *s)
307         {
308         int idx = 0;
309         while(!int_ctrl_cmd_is_null(defn) && (strcmp(defn->cmd_name, s) != 0))
310                 {
311                 idx++;
312                 defn++;
313                 }
314         if(int_ctrl_cmd_is_null(defn))
315                 /* The given name wasn't found */
316                 return -1;
317         return idx;
318         }
319
320 static int int_ctrl_cmd_by_num(const ENGINE_CMD_DEFN *defn, unsigned int num)
321         {
322         int idx = 0;
323         /* NB: It is stipulated that 'cmd_defn' lists are ordered by cmd_num. So
324          * our searches don't need to take any longer than necessary. */
325         while(!int_ctrl_cmd_is_null(defn) && (defn->cmd_num < num))
326                 {
327                 idx++;
328                 defn++;
329                 }
330         if(defn->cmd_num == num)
331                 return idx;
332         /* The given cmd_num wasn't found */
333         return -1;
334         }
335
336 static int int_ctrl_helper(ENGINE *e, int cmd, long i, void *p, void (*f)())
337         {
338         int idx;
339         char *s = (char *)p;
340         /* Take care of the easy one first (eg. it requires no searches) */
341         if(cmd == ENGINE_CTRL_GET_FIRST_CMD_TYPE)
342                 {
343                 if((e->cmd_defns == NULL) || int_ctrl_cmd_is_null(e->cmd_defns))
344                         return 0;
345                 return e->cmd_defns->cmd_num;
346                 }
347         /* One or two commands require that "p" be a valid string buffer */
348         if((cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) ||
349                         (cmd == ENGINE_CTRL_GET_NAME_FROM_CMD) ||
350                         (cmd == ENGINE_CTRL_GET_DESC_FROM_CMD))
351                 {
352                 if(s == NULL)
353                         {
354                         ENGINEerr(ENGINE_F_INT_CTRL_HELPER,
355                                 ERR_R_PASSED_NULL_PARAMETER);
356                         return -1;
357                         }
358                 }
359         /* Now handle cmd_name -> cmd_num conversion */
360         if(cmd == ENGINE_CTRL_GET_CMD_FROM_NAME)
361                 {
362                 if((e->cmd_defns == NULL) || ((idx = int_ctrl_cmd_by_name(
363                                                 e->cmd_defns, s)) < 0))
364                         {
365                         ENGINEerr(ENGINE_F_INT_CTRL_HELPER,
366                                 ENGINE_R_INVALID_CMD_NAME);
367                         return -1;
368                         }
369                 return e->cmd_defns[idx].cmd_num;
370                 }
371         /* For the rest of the commands, the 'long' argument must specify a
372          * valie command number - so we need to conduct a search. */
373         if((e->cmd_defns == NULL) || ((idx = int_ctrl_cmd_by_num(e->cmd_defns,
374                                         (unsigned int)i)) < 0))
375                 {
376                 ENGINEerr(ENGINE_F_INT_CTRL_HELPER,
377                         ENGINE_R_INVALID_CMD_NUMBER);
378                 return -1;
379                 }
380         /* Now the logic splits depending on command type */
381         switch(cmd)
382                 {
383         case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
384                 idx++;
385                 if(int_ctrl_cmd_is_null(e->cmd_defns + idx))
386                         /* end-of-list */
387                         return 0;
388                 else
389                         return e->cmd_defns[idx].cmd_num;
390         case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
391                 return strlen(e->cmd_defns[idx].cmd_name);
392         case ENGINE_CTRL_GET_NAME_FROM_CMD:
393                 return sprintf(s, "%s", e->cmd_defns[idx].cmd_name);
394         case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
395                 if(e->cmd_defns[idx].cmd_desc)
396                         return strlen(e->cmd_defns[idx].cmd_desc);
397                 return strlen(int_no_description);
398         case ENGINE_CTRL_GET_DESC_FROM_CMD:
399                 if(e->cmd_defns[idx].cmd_desc)
400                         return sprintf(s, "%s", e->cmd_defns[idx].cmd_desc);
401                 return sprintf(s, "%s", int_no_description);
402         case ENGINE_CTRL_GET_CMD_FLAGS:
403                 return e->cmd_defns[idx].cmd_flags;
404                 }
405         /* Shouldn't really be here ... */
406         ENGINEerr(ENGINE_F_INT_CTRL_HELPER,ENGINE_R_INTERNAL_LIST_ERROR);
407         return -1;
408         }
409
410 int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
411         {
412         int ctrl_exists, ref_exists;
413         if(e == NULL)
414                 {
415                 ENGINEerr(ENGINE_F_ENGINE_CTRL,ERR_R_PASSED_NULL_PARAMETER);
416                 return 0;
417                 }
418         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
419         ref_exists = ((e->struct_ref > 0) ? 1 : 0);
420         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
421         ctrl_exists = ((e->ctrl == NULL) ? 0 : 1);
422         if(!ref_exists)
423                 {
424                 ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_REFERENCE);
425                 return 0;
426                 }
427         /* Intercept any "root-level" commands before trying to hand them on to
428          * ctrl() handlers. */
429         switch(cmd)
430                 {
431         case ENGINE_CTRL_HAS_CTRL_FUNCTION:
432                 return ctrl_exists;
433         case ENGINE_CTRL_GET_FIRST_CMD_TYPE:
434         case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
435         case ENGINE_CTRL_GET_CMD_FROM_NAME:
436         case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
437         case ENGINE_CTRL_GET_NAME_FROM_CMD:
438         case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
439         case ENGINE_CTRL_GET_DESC_FROM_CMD:
440         case ENGINE_CTRL_GET_CMD_FLAGS:
441                 if(ctrl_exists && !(e->flags & ENGINE_FLAGS_MANUAL_CMD_CTRL))
442                         return int_ctrl_helper(e,cmd,i,p,f);
443                 if(!ctrl_exists)
444                         {
445                         ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_CONTROL_FUNCTION);
446                         /* For these cmd-related functions, failure is indicated
447                          * by a -1 return value (because 0 is used as a valid
448                          * return in some places). */
449                         return -1;
450                         }
451         default:
452                 break;
453                 }
454         /* Anything else requires a ctrl() handler to exist. */
455         if(!ctrl_exists)
456                 {
457                 ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_CONTROL_FUNCTION);
458                 return 0;
459                 }
460         return e->ctrl(e, cmd, i, p, f);
461         }
462
463 int ENGINE_cmd_is_executable(ENGINE *e, int cmd)
464         {
465         int flags;
466         if((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, cmd, NULL, NULL)) < 0)
467                 {
468                 ENGINEerr(ENGINE_F_ENGINE_CMD_IS_EXECUTABLE,
469                         ENGINE_R_INVALID_CMD_NUMBER);
470                 return 0;
471                 }
472         if(!(flags & ENGINE_CMD_FLAG_NO_INPUT) &&
473                         !(flags & ENGINE_CMD_FLAG_NUMERIC) &&
474                         !(flags & ENGINE_CMD_FLAG_STRING))
475                 return 0;
476         return 1;
477         }
478
479 int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
480                                 int cmd_optional)
481         {
482         int num, flags;
483         long l;
484         char *ptr;
485         if((e == NULL) || (cmd_name == NULL))
486                 {
487                 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
488                         ERR_R_PASSED_NULL_PARAMETER);
489                 return 0;
490                 }
491         if((e->ctrl == NULL) || ((num = ENGINE_ctrl(e,
492                                         ENGINE_CTRL_GET_CMD_FROM_NAME,
493                                         0, (void *)cmd_name, NULL)) <= 0))
494                 {
495                 /* If the command didn't *have* to be supported, we fake
496                  * success. This allows certain settings to be specified for
497                  * multiple ENGINEs and only require a change of ENGINE id
498                  * (without having to selectively apply settings). Eg. changing
499                  * from a hardware device back to the regular software ENGINE
500                  * without editing the config file, etc. */
501                 if(cmd_optional)
502                         {
503                         ERR_clear_error();
504                         return 1;
505                         }
506                 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
507                         ENGINE_R_INVALID_CMD_NAME);
508                 return 0;
509                 }
510         if(!ENGINE_cmd_is_executable(e, num))
511                 {
512                 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
513                         ENGINE_R_CMD_NOT_EXECUTABLE);
514                 return 0;
515                 }
516         if((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num, NULL, NULL)) < 0)
517                 {
518                 /* Shouldn't happen, given that ENGINE_cmd_is_executable()
519                  * returned success. */
520                 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
521                         ENGINE_R_INTERNAL_LIST_ERROR);
522                 return 0;
523                 }
524         /* If the command takes no input, there must be no input. And vice
525          * versa. */
526         if(flags & ENGINE_CMD_FLAG_NO_INPUT)
527                 {
528                 if(arg != NULL)
529                         {
530                         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
531                                 ENGINE_R_COMMAND_TAKES_NO_INPUT);
532                         return 0;
533                         }
534                 /* We deliberately force the result of ENGINE_ctrl() to 0 or 1
535                  * rather than returning it as "return data". This is to ensure
536                  * usage of these commands is consistent across applications and
537                  * that certain applications don't understand it one way, and
538                  * others another. */
539                 if(ENGINE_ctrl(e, num, 0, (void *)arg, NULL))
540                         return 1;
541                 return 0;
542                 }
543         /* So, we require input */
544         if(arg == NULL)
545                 {
546                 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
547                         ENGINE_R_COMMAND_TAKES_INPUT);
548                 return 0;
549                 }
550         /* If it takes string input, that's easy */
551         if(flags & ENGINE_CMD_FLAG_STRING)
552                 {
553                 /* Same explanation as above */
554                 if(ENGINE_ctrl(e, num, 0, (void *)arg, NULL))
555                         return 1;
556                 return 0;
557                 }
558         /* If it doesn't take numeric either, then it is unsupported for use in
559          * a config-setting situation, which is what this function is for. This
560          * should never happen though, because ENGINE_cmd_is_executable() was
561          * used. */
562         if(!(flags & ENGINE_CMD_FLAG_NUMERIC))
563                 {
564                 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
565                         ENGINE_R_INTERNAL_LIST_ERROR);
566                 return 0;
567                 }
568         l = strtol(arg, &ptr, 10);
569         if((arg == ptr) || (*ptr != '\0'))
570                 {
571                 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
572                         ENGINE_R_ARGUMENT_IS_NOT_A_NUMBER);
573                 return 0;
574                 }
575         /* Force the result of the control command to 0 or 1, for the reasons
576          * mentioned before. */
577         if(ENGINE_ctrl(e, num, l, NULL, NULL))
578                 return 1;
579         return 0;
580         }
581
582 static ENGINE *engine_get_default_type(ENGINE_TYPE t)
583         {
584         ENGINE *ret = NULL;
585
586         /* engine_def_check is lean and mean and won't replace any
587          * prior default engines ... so we must ensure that it is always
588          * the first function to get to touch the default values. */
589         engine_def_check();
590         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
591         switch(t)
592                 {
593         case ENGINE_TYPE_RSA:
594                 ret = engine_def_rsa; break;
595         case ENGINE_TYPE_DSA:
596                 ret = engine_def_dsa; break;
597         case ENGINE_TYPE_DH:
598                 ret = engine_def_dh; break;
599         case ENGINE_TYPE_RAND:
600                 ret = engine_def_rand; break;
601         case ENGINE_TYPE_BN_MOD_EXP:
602                 ret = engine_def_bn_mod_exp; break;
603         case ENGINE_TYPE_BN_MOD_EXP_CRT:
604                 ret = engine_def_bn_mod_exp_crt; break;
605                 }
606         /* Unforunately we can't do this work outside the lock with a
607          * call to ENGINE_init() because that would leave a race
608          * condition open. */
609         if(ret)
610                 {
611                 ret->struct_ref++;
612                 ret->funct_ref++;
613                 }
614         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
615         return ret;
616         }
617
618 ENGINE *ENGINE_get_default_RSA(void)
619         {
620         return engine_get_default_type(ENGINE_TYPE_RSA);
621         }
622
623 ENGINE *ENGINE_get_default_DSA(void)
624         {
625         return engine_get_default_type(ENGINE_TYPE_DSA);
626         }
627
628 ENGINE *ENGINE_get_default_DH(void)
629         {
630         return engine_get_default_type(ENGINE_TYPE_DH);
631         }
632
633 ENGINE *ENGINE_get_default_RAND(void)
634         {
635         return engine_get_default_type(ENGINE_TYPE_RAND);
636         }
637
638 ENGINE *ENGINE_get_default_BN_mod_exp(void)
639         {
640         return engine_get_default_type(ENGINE_TYPE_BN_MOD_EXP);
641         }
642
643 ENGINE *ENGINE_get_default_BN_mod_exp_crt(void)
644         {
645         return engine_get_default_type(ENGINE_TYPE_BN_MOD_EXP_CRT);
646         }
647
648 static int engine_set_default_type(ENGINE_TYPE t, ENGINE *e)
649         {
650         ENGINE *old = NULL;
651
652         if(e == NULL)
653                 {
654                 ENGINEerr(ENGINE_F_ENGINE_SET_DEFAULT_TYPE,
655                         ERR_R_PASSED_NULL_PARAMETER);
656                 return 0;
657                 }
658         /* engine_def_check is lean and mean and won't replace any
659          * prior default engines ... so we must ensure that it is always
660          * the first function to get to touch the default values. */
661         engine_def_check();
662         /* Attempt to get a functional reference (we need one anyway, but
663          * also, 'e' may be just a structural reference being passed in so
664          * this call may actually be the first). */
665         if(!ENGINE_init(e))
666                 {
667                 ENGINEerr(ENGINE_F_ENGINE_SET_DEFAULT_TYPE,
668                         ENGINE_R_INIT_FAILED);
669                 return 0;
670                 }
671         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
672         switch(t)
673                 {
674         case ENGINE_TYPE_RSA:
675                 old = engine_def_rsa;
676                 engine_def_rsa = e; break;
677         case ENGINE_TYPE_DSA:
678                 old = engine_def_dsa;
679                 engine_def_dsa = e; break;
680         case ENGINE_TYPE_DH:
681                 old = engine_def_dh;
682                 engine_def_dh = e; break;
683         case ENGINE_TYPE_RAND:
684                 old = engine_def_rand;
685                 engine_def_rand = e; break;
686         case ENGINE_TYPE_BN_MOD_EXP:
687                 old = engine_def_bn_mod_exp;
688                 engine_def_bn_mod_exp = e; break;
689         case ENGINE_TYPE_BN_MOD_EXP_CRT:
690                 old = engine_def_bn_mod_exp_crt;
691                 engine_def_bn_mod_exp_crt = e; break;
692                 }
693         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
694         /* If we've replaced a previous value, then we need to remove the
695          * functional reference we had. */
696         if(old && !ENGINE_finish(old))
697                 {
698                 ENGINEerr(ENGINE_F_ENGINE_SET_DEFAULT_TYPE,
699                         ENGINE_R_FINISH_FAILED);
700                 return 0;
701                 }
702         return 1;
703         }
704
705 int ENGINE_set_default_RSA(ENGINE *e)
706         {
707         return engine_set_default_type(ENGINE_TYPE_RSA, e);
708         }
709
710 int ENGINE_set_default_DSA(ENGINE *e)
711         {
712         return engine_set_default_type(ENGINE_TYPE_DSA, e);
713         }
714
715 int ENGINE_set_default_DH(ENGINE *e)
716         {
717         return engine_set_default_type(ENGINE_TYPE_DH, e);
718         }
719
720 int ENGINE_set_default_RAND(ENGINE *e)
721         {
722         return engine_set_default_type(ENGINE_TYPE_RAND, e);
723         }
724
725 int ENGINE_set_default_BN_mod_exp(ENGINE *e)
726         {
727         return engine_set_default_type(ENGINE_TYPE_BN_MOD_EXP, e);
728         }
729
730 int ENGINE_set_default_BN_mod_exp_crt(ENGINE *e)
731         {
732         return engine_set_default_type(ENGINE_TYPE_BN_MOD_EXP_CRT, e);
733         }
734
735 int ENGINE_set_default(ENGINE *e, unsigned int flags)
736         {
737         if((flags & ENGINE_METHOD_RSA) && e->rsa_meth &&
738                         !ENGINE_set_default_RSA(e))
739                 return 0;
740         if((flags & ENGINE_METHOD_DSA) && e->dsa_meth &&
741                         !ENGINE_set_default_DSA(e))
742                 return 0;
743         if((flags & ENGINE_METHOD_DH) && e->dh_meth &&
744                         !ENGINE_set_default_DH(e))
745                 return 0;
746         if((flags & ENGINE_METHOD_RAND) && e->rand_meth &&
747                         !ENGINE_set_default_RAND(e))
748                 return 0;
749         if((flags & ENGINE_METHOD_BN_MOD_EXP) && e->bn_mod_exp &&
750                         !ENGINE_set_default_BN_mod_exp(e))
751                 return 0;
752         if((flags & ENGINE_METHOD_BN_MOD_EXP_CRT) && e->bn_mod_exp_crt &&
753                         !ENGINE_set_default_BN_mod_exp_crt(e))
754                 return 0;
755         return 1;
756         }
757