3a1c200ce47e66659b40357559d4a93ae441e53e
[openssl.git] / crypto / engine / eng_dyn.c
1 /* crypto/engine/eng_dyn.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3  * project 2001.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999-2001 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
60 #include <stdio.h>
61 #include <openssl/crypto.h>
62 #include "cryptlib.h"
63 #include "eng_int.h"
64 #include <openssl/engine.h>
65 #include <openssl/dso.h>
66
67 /* Shared libraries implementing ENGINEs for use by the "dynamic" ENGINE loader
68  * should implement the hook-up functions with the following prototypes. */
69
70 /* Our ENGINE handlers */
71 static int dynamic_init(ENGINE *e);
72 static int dynamic_finish(ENGINE *e);
73 static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
74 /* Predeclare our context type */
75 typedef struct st_dynamic_data_ctx dynamic_data_ctx;
76 /* The implementation for the important control command */
77 static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx);
78
79 #define DYNAMIC_CMD_SO_PATH             ENGINE_CMD_BASE
80 #define DYNAMIC_CMD_NO_VCHECK           (ENGINE_CMD_BASE + 1)
81 #define DYNAMIC_CMD_ID                  (ENGINE_CMD_BASE + 2)
82 #define DYNAMIC_CMD_LIST_ADD            (ENGINE_CMD_BASE + 3)
83 #define DYNAMIC_CMD_DIR_LOAD            (ENGINE_CMD_BASE + 4)
84 #define DYNAMIC_CMD_DIR_ADD             (ENGINE_CMD_BASE + 5)
85 #define DYNAMIC_CMD_LOAD                (ENGINE_CMD_BASE + 6)
86
87 /* The constants used when creating the ENGINE */
88 static const char *engine_dynamic_id = "dynamic";
89 static const char *engine_dynamic_name = "Dynamic engine loading support";
90 static const ENGINE_CMD_DEFN dynamic_cmd_defns[] = {
91         {DYNAMIC_CMD_SO_PATH,
92                 "SO_PATH",
93                 "Specifies the path to the new ENGINE shared library",
94                 ENGINE_CMD_FLAG_STRING},
95         {DYNAMIC_CMD_NO_VCHECK,
96                 "NO_VCHECK",
97                 "Specifies to continue even if version checking fails (boolean)",
98                 ENGINE_CMD_FLAG_NUMERIC},
99         {DYNAMIC_CMD_ID,
100                 "ID",
101                 "Specifies an ENGINE id name for loading",
102                 ENGINE_CMD_FLAG_STRING},
103         {DYNAMIC_CMD_LIST_ADD,
104                 "LIST_ADD",
105                 "Whether to add a loaded ENGINE to the internal list (0=no,1=yes,2=mandatory)",
106                 ENGINE_CMD_FLAG_NUMERIC},
107         {DYNAMIC_CMD_DIR_LOAD,
108                 "DIR_LOAD",
109                 "Specifies whether to load from 'DIR_ADD' directories (0=no,1=yes,2=mandatory)",
110                 ENGINE_CMD_FLAG_NUMERIC},
111         {DYNAMIC_CMD_DIR_ADD,
112                 "DIR_ADD",
113                 "Adds a directory from which ENGINEs can be loaded",
114                 ENGINE_CMD_FLAG_STRING},
115         {DYNAMIC_CMD_LOAD,
116                 "LOAD",
117                 "Load up the ENGINE specified by other settings",
118                 ENGINE_CMD_FLAG_NO_INPUT},
119         {0, NULL, NULL, 0}
120         };
121 static const ENGINE_CMD_DEFN dynamic_cmd_defns_empty[] = {
122         {0, NULL, NULL, 0}
123         };
124
125 /* Loading code stores state inside the ENGINE structure via the "ex_data"
126  * element. We load all our state into a single structure and use that as a
127  * single context in the "ex_data" stack. */
128 struct st_dynamic_data_ctx
129         {
130         /* The DSO object we load that supplies the ENGINE code */
131         DSO *dynamic_dso;
132         /* The function pointer to the version checking shared library function */
133         dynamic_v_check_fn v_check;
134         /* The function pointer to the engine-binding shared library function */
135         dynamic_bind_engine bind_engine;
136         /* The default name/path for loading the shared library */
137         const char *DYNAMIC_LIBNAME;
138         /* Whether to continue loading on a version check failure */
139         int no_vcheck;
140         /* If non-NULL, stipulates the 'id' of the ENGINE to be loaded */
141         const char *engine_id;
142         /* If non-zero, a successfully loaded ENGINE should be added to the internal
143          * ENGINE list. If 2, the add must succeed or the entire load should fail. */
144         int list_add_value;
145         /* The symbol name for the version checking function */
146         const char *DYNAMIC_F1;
147         /* The symbol name for the "initialise ENGINE structure" function */
148         const char *DYNAMIC_F2;
149         /* Whether to never use 'dirs', use 'dirs' as a fallback, or only use
150          * 'dirs' for loading. Default is to use 'dirs' as a fallback. */
151         int dir_load;
152         /* A stack of directories from which ENGINEs could be loaded */
153         STACK *dirs;
154         };
155
156 /* This is the "ex_data" index we obtain and reserve for use with our context
157  * structure. */
158 static int dynamic_ex_data_idx = -1;
159
160 static void int_free_str(void *s) { OPENSSL_free(s); }
161 /* Because our ex_data element may or may not get allocated depending on whether
162  * a "first-use" occurs before the ENGINE is freed, we have a memory leak
163  * problem to solve. We can't declare a "new" handler for the ex_data as we
164  * don't want a dynamic_data_ctx in *all* ENGINE structures of all types (this
165  * is a bug in the design of CRYPTO_EX_DATA). As such, we just declare a "free"
166  * handler and that will get called if an ENGINE is being destroyed and there
167  * was an ex_data element corresponding to our context type. */
168 static void dynamic_data_ctx_free_func(void *parent, void *ptr,
169                         CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
170         {
171         if(ptr)
172                 {
173                 dynamic_data_ctx *ctx = (dynamic_data_ctx *)ptr;
174                 if(ctx->dynamic_dso)
175                         DSO_free(ctx->dynamic_dso);
176                 if(ctx->DYNAMIC_LIBNAME)
177                         OPENSSL_free((void*)ctx->DYNAMIC_LIBNAME);
178                 if(ctx->engine_id)
179                         OPENSSL_free((void*)ctx->engine_id);
180                 if(ctx->dirs)
181                         sk_pop_free(ctx->dirs, int_free_str);
182                 OPENSSL_free(ctx);
183                 }
184         }
185
186 /* Construct the per-ENGINE context. We create it blindly and then use a lock to
187  * check for a race - if so, all but one of the threads "racing" will have
188  * wasted their time. The alternative involves creating everything inside the
189  * lock which is far worse. */
190 static int dynamic_set_data_ctx(ENGINE *e, dynamic_data_ctx **ctx)
191         {
192         dynamic_data_ctx *c;
193         c = OPENSSL_malloc(sizeof(dynamic_data_ctx));
194         if(!c)
195                 {
196                 ENGINEerr(ENGINE_F_SET_DATA_CTX,ERR_R_MALLOC_FAILURE);
197                 return 0;
198                 }
199         memset(c, 0, sizeof(dynamic_data_ctx));
200         c->dynamic_dso = NULL;
201         c->v_check = NULL;
202         c->bind_engine = NULL;
203         c->DYNAMIC_LIBNAME = NULL;
204         c->no_vcheck = 0;
205         c->engine_id = NULL;
206         c->list_add_value = 0;
207         c->DYNAMIC_F1 = "v_check";
208         c->DYNAMIC_F2 = "bind_engine";
209         c->dir_load = 1;
210         c->dirs = sk_new_null();
211         if(!c->dirs)
212                 {
213                 ENGINEerr(ENGINE_F_SET_DATA_CTX,ERR_R_MALLOC_FAILURE);
214                 OPENSSL_free(c);
215                 return 0;
216                 }
217         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
218         if((*ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e,
219                                 dynamic_ex_data_idx)) == NULL)
220                 {
221                 /* Good, we're the first */
222                 ENGINE_set_ex_data(e, dynamic_ex_data_idx, c);
223                 *ctx = c;
224                 c = NULL;
225                 }
226         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
227         /* If we lost the race to set the context, c is non-NULL and *ctx is the
228          * context of the thread that won. */
229         if(c)
230                 OPENSSL_free(c);
231         return 1;
232         }
233
234 /* This function retrieves the context structure from an ENGINE's "ex_data", or
235  * if it doesn't exist yet, sets it up. */
236 static dynamic_data_ctx *dynamic_get_data_ctx(ENGINE *e)
237         {
238         dynamic_data_ctx *ctx;
239         if(dynamic_ex_data_idx < 0)
240                 {
241                 /* Create and register the ENGINE ex_data, and associate our
242                  * "free" function with it to ensure any allocated contexts get
243                  * freed when an ENGINE goes underground. */
244                 int new_idx = ENGINE_get_ex_new_index(0, NULL, NULL, NULL,
245                                         dynamic_data_ctx_free_func);
246                 if(new_idx == -1)
247                         {
248                         ENGINEerr(ENGINE_F_DYNAMIC_GET_DATA_CTX,ENGINE_R_NO_INDEX);
249                         return NULL;
250                         }
251                 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
252                 /* Avoid a race by checking again inside this lock */
253                 if(dynamic_ex_data_idx < 0)
254                         {
255                         /* Good, someone didn't beat us to it */
256                         dynamic_ex_data_idx = new_idx;
257                         new_idx = -1;
258                         }
259                 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
260                 /* In theory we could "give back" the index here if
261                  * (new_idx>-1), but it's not possible and wouldn't gain us much
262                  * if it were. */
263                 }
264         ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e, dynamic_ex_data_idx);
265         /* Check if the context needs to be created */
266         if((ctx == NULL) && !dynamic_set_data_ctx(e, &ctx))
267                 /* "set_data" will set errors if necessary */
268                 return NULL;
269         return ctx;
270         }
271
272 static ENGINE *engine_dynamic(void)
273         {
274         ENGINE *ret = ENGINE_new();
275         if(!ret)
276                 return NULL;
277         if(!ENGINE_set_id(ret, engine_dynamic_id) ||
278                         !ENGINE_set_name(ret, engine_dynamic_name) ||
279                         !ENGINE_set_init_function(ret, dynamic_init) ||
280                         !ENGINE_set_finish_function(ret, dynamic_finish) ||
281                         !ENGINE_set_ctrl_function(ret, dynamic_ctrl) ||
282                         !ENGINE_set_flags(ret, ENGINE_FLAGS_BY_ID_COPY) ||
283                         !ENGINE_set_cmd_defns(ret, dynamic_cmd_defns))
284                 {
285                 ENGINE_free(ret);
286                 return NULL;
287                 }
288         return ret;
289         }
290
291 void ENGINE_load_dynamic(void)
292         {
293         ENGINE *toadd = engine_dynamic();
294         if(!toadd) return;
295         ENGINE_add(toadd);
296         /* If the "add" worked, it gets a structural reference. So either way,
297          * we release our just-created reference. */
298         ENGINE_free(toadd);
299         /* If the "add" didn't work, it was probably a conflict because it was
300          * already added (eg. someone calling ENGINE_load_blah then calling
301          * ENGINE_load_builtin_engines() perhaps). */
302         ERR_clear_error();
303         }
304
305 static int dynamic_init(ENGINE *e)
306         {
307         /* We always return failure - the "dyanamic" engine itself can't be used
308          * for anything. */
309         return 0;
310         }
311
312 static int dynamic_finish(ENGINE *e)
313         {
314         /* This should never be called on account of "dynamic_init" always
315          * failing. */
316         return 0;
317         }
318
319 static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
320         {
321         dynamic_data_ctx *ctx = dynamic_get_data_ctx(e);
322         int initialised;
323         
324         if(!ctx)
325                 {
326                 ENGINEerr(ENGINE_F_DYNAMIC_CTRL,ENGINE_R_NOT_LOADED);
327                 return 0;
328                 }
329         initialised = ((ctx->dynamic_dso == NULL) ? 0 : 1);
330         /* All our control commands require the ENGINE to be uninitialised */
331         if(initialised)
332                 {
333                 ENGINEerr(ENGINE_F_DYNAMIC_CTRL,
334                         ENGINE_R_ALREADY_LOADED);
335                 return 0;
336                 }
337         switch(cmd)
338                 {
339         case DYNAMIC_CMD_SO_PATH:
340                 /* a NULL 'p' or a string of zero-length is the same thing */
341                 if(p && (strlen((const char *)p) < 1))
342                         p = NULL;
343                 if(ctx->DYNAMIC_LIBNAME)
344                         OPENSSL_free((void*)ctx->DYNAMIC_LIBNAME);
345                 if(p)
346                         ctx->DYNAMIC_LIBNAME = BUF_strdup(p);
347                 else
348                         ctx->DYNAMIC_LIBNAME = NULL;
349                 return (ctx->DYNAMIC_LIBNAME ? 1 : 0);
350         case DYNAMIC_CMD_NO_VCHECK:
351                 ctx->no_vcheck = ((i == 0) ? 0 : 1);
352                 return 1;
353         case DYNAMIC_CMD_ID:
354                 /* a NULL 'p' or a string of zero-length is the same thing */
355                 if(p && (strlen((const char *)p) < 1))
356                         p = NULL;
357                 if(ctx->engine_id)
358                         OPENSSL_free((void*)ctx->engine_id);
359                 if(p)
360                         ctx->engine_id = BUF_strdup(p);
361                 else
362                         ctx->engine_id = NULL;
363                 return (ctx->engine_id ? 1 : 0);
364         case DYNAMIC_CMD_LIST_ADD:
365                 if((i < 0) || (i > 2))
366                         {
367                         ENGINEerr(ENGINE_F_DYNAMIC_CTRL,
368                                 ENGINE_R_INVALID_ARGUMENT);
369                         return 0;
370                         }
371                 ctx->list_add_value = (int)i;
372                 return 1;
373         case DYNAMIC_CMD_LOAD:
374                 return dynamic_load(e, ctx);
375         case DYNAMIC_CMD_DIR_LOAD:
376                 if((i < 0) || (i > 2))
377                         {
378                         ENGINEerr(ENGINE_F_DYNAMIC_CTRL,
379                                 ENGINE_R_INVALID_ARGUMENT);
380                         return 0;
381                         }
382                 ctx->dir_load = (int)i;
383                 return 1;
384         case DYNAMIC_CMD_DIR_ADD:
385                 /* a NULL 'p' or a string of zero-length is the same thing */
386                 if(!p || (strlen((const char *)p) < 1))
387                         {
388                         ENGINEerr(ENGINE_F_DYNAMIC_CTRL,
389                                 ENGINE_R_INVALID_ARGUMENT);
390                         return 0;
391                         }
392                 {
393                 char *tmp_str = BUF_strdup(p);
394                 if(!tmp_str)
395                         {
396                         ENGINEerr(ENGINE_F_DYNAMIC_CTRL,
397                                 ERR_R_MALLOC_FAILURE);
398                         return 0;
399                         }
400                 sk_insert(ctx->dirs, tmp_str, -1);
401                 }
402                 return 1;
403         default:
404                 break;
405                 }
406         ENGINEerr(ENGINE_F_DYNAMIC_CTRL,ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
407         return 0;
408         }
409
410 static int int_load(dynamic_data_ctx *ctx)
411         {
412         int num, loop;
413         /* Unless told not to, try a direct load */
414         if((ctx->dir_load != 2) && (DSO_load(ctx->dynamic_dso,
415                                 ctx->DYNAMIC_LIBNAME, NULL, 0)) != NULL)
416                 return 1;
417         /* If we're not allowed to use 'dirs' or we have none, fail */
418         if(!ctx->dir_load || ((num = sk_num(ctx->dirs)) < 1))
419                 return 0;
420         for(loop = 0; loop < num; loop++)
421                 {
422                 const char *s = sk_value(ctx->dirs, loop);
423                 char *merge = DSO_merge(ctx->dynamic_dso, ctx->DYNAMIC_LIBNAME, s);
424                 if(!merge)
425                         return 0;
426                 if(DSO_load(ctx->dynamic_dso, merge, NULL, 0))
427                         {
428                         /* Found what we're looking for */
429                         OPENSSL_free(merge);
430                         return 1;
431                         }
432                 OPENSSL_free(merge);
433                 }
434         return 0;
435         }
436
437 static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx)
438         {
439         ENGINE cpy;
440         dynamic_fns fns;
441
442         if(!ctx->dynamic_dso)
443                 ctx->dynamic_dso = DSO_new();
444         if(!ctx->DYNAMIC_LIBNAME)
445                 {
446                 if(!ctx->engine_id)
447                         return 0;
448                 ctx->DYNAMIC_LIBNAME =
449                         DSO_convert_filename(ctx->dynamic_dso, ctx->engine_id);
450                 }
451         if(!int_load(ctx))
452                 {
453                 ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
454                         ENGINE_R_DSO_NOT_FOUND);
455                 DSO_free(ctx->dynamic_dso);
456                 ctx->dynamic_dso = NULL;
457                 return 0;
458                 }
459         /* We have to find a bind function otherwise it'll always end badly */
460         if(!(ctx->bind_engine = (dynamic_bind_engine)DSO_bind_func(
461                                         ctx->dynamic_dso, ctx->DYNAMIC_F2)))
462                 {
463                 ctx->bind_engine = NULL;
464                 DSO_free(ctx->dynamic_dso);
465                 ctx->dynamic_dso = NULL;
466                 ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
467                         ENGINE_R_DSO_FAILURE);
468                 return 0;
469                 }
470         /* Do we perform version checking? */
471         if(!ctx->no_vcheck)
472                 {
473                 unsigned long vcheck_res = 0;
474                 /* Now we try to find a version checking function and decide how
475                  * to cope with failure if/when it fails. */
476                 ctx->v_check = (dynamic_v_check_fn)DSO_bind_func(
477                                 ctx->dynamic_dso, ctx->DYNAMIC_F1);
478                 if(ctx->v_check)
479                         vcheck_res = ctx->v_check(OSSL_DYNAMIC_VERSION);
480                 /* We fail if the version checker veto'd the load *or* if it is
481                  * deferring to us (by returning its version) and we think it is
482                  * too old. */
483                 if(vcheck_res < OSSL_DYNAMIC_OLDEST)
484                         {
485                         /* Fail */
486                         ctx->bind_engine = NULL;
487                         ctx->v_check = NULL;
488                         DSO_free(ctx->dynamic_dso);
489                         ctx->dynamic_dso = NULL;
490                         ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
491                                 ENGINE_R_VERSION_INCOMPATIBILITY);
492                         return 0;
493                         }
494                 }
495         /* First binary copy the ENGINE structure so that we can roll back if
496          * the hand-over fails */
497         memcpy(&cpy, e, sizeof(ENGINE));
498         /* Provide the ERR, "ex_data", memory, and locking callbacks so the
499          * loaded library uses our state rather than its own. FIXME: As noted in
500          * engine.h, much of this would be simplified if each area of code
501          * provided its own "summary" structure of all related callbacks. It
502          * would also increase opaqueness. */
503         fns.static_state = ENGINE_get_static_state();
504         fns.err_fns = ERR_get_implementation();
505         fns.ex_data_fns = CRYPTO_get_ex_data_implementation();
506         CRYPTO_get_mem_functions(&fns.mem_fns.malloc_cb,
507                                 &fns.mem_fns.realloc_cb,
508                                 &fns.mem_fns.free_cb);
509         fns.lock_fns.lock_locking_cb = CRYPTO_get_locking_callback();
510         fns.lock_fns.lock_add_lock_cb = CRYPTO_get_add_lock_callback();
511         fns.lock_fns.dynlock_create_cb = CRYPTO_get_dynlock_create_callback();
512         fns.lock_fns.dynlock_lock_cb = CRYPTO_get_dynlock_lock_callback();
513         fns.lock_fns.dynlock_destroy_cb = CRYPTO_get_dynlock_destroy_callback();
514         /* Now that we've loaded the dynamic engine, make sure no "dynamic"
515          * ENGINE elements will show through. */
516         engine_set_all_null(e);
517
518         /* Try to bind the ENGINE onto our own ENGINE structure */
519         if(!ctx->bind_engine(e, ctx->engine_id, &fns))
520                 {
521                 ctx->bind_engine = NULL;
522                 ctx->v_check = NULL;
523                 DSO_free(ctx->dynamic_dso);
524                 ctx->dynamic_dso = NULL;
525                 ENGINEerr(ENGINE_F_DYNAMIC_LOAD,ENGINE_R_INIT_FAILED);
526                 /* Copy the original ENGINE structure back */
527                 memcpy(e, &cpy, sizeof(ENGINE));
528                 return 0;
529                 }
530         /* Do we try to add this ENGINE to the internal list too? */
531         if(ctx->list_add_value > 0)
532                 {
533                 if(!ENGINE_add(e))
534                         {
535                         /* Do we tolerate this or fail? */
536                         if(ctx->list_add_value > 1)
537                                 {
538                                 /* Fail - NB: By this time, it's too late to
539                                  * rollback, and trying to do so allows the
540                                  * bind_engine() code to have created leaks. We
541                                  * just have to fail where we are, after the
542                                  * ENGINE has changed. */
543                                 ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
544                                         ENGINE_R_CONFLICTING_ENGINE_ID);
545                                 return 0;
546                                 }
547                         /* Tolerate */
548                         ERR_clear_error();
549                         }
550                 }
551         return 1;
552         }