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