VirtualBox

source: vbox/trunk/src/libs/openssl-3.3.2/crypto/conf/conf_mod.c@ 108358

最後變更 在這個檔案從108358是 108206,由 vboxsync 提交於 6 週 前

openssl-3.3.2: Exported all files to OSE and removed .scm-settings ​bugref:10757

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 18.8 KB
 
1/*
2 * Copyright 2002-2024 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
8 */
9
10/* We need to use some engine deprecated APIs */
11#define OPENSSL_SUPPRESS_DEPRECATED
12
13#include "internal/cryptlib.h"
14#include "internal/rcu.h"
15#include <stdio.h>
16#include <ctype.h>
17#include <openssl/crypto.h>
18#include "internal/conf.h"
19#include <openssl/conf_api.h>
20#include "internal/dso.h"
21#include "internal/thread_once.h"
22#include <openssl/x509.h>
23#include <openssl/trace.h>
24#include <openssl/engine.h>
25#include "conf_local.h"
26
27DEFINE_STACK_OF(CONF_MODULE)
28DEFINE_STACK_OF(CONF_IMODULE)
29
30#define DSO_mod_init_name "OPENSSL_init"
31#define DSO_mod_finish_name "OPENSSL_finish"
32
33/*
34 * This structure contains a data about supported modules. entries in this
35 * table correspond to either dynamic or static modules.
36 */
37
38struct conf_module_st {
39 /* DSO of this module or NULL if static */
40 DSO *dso;
41 /* Name of the module */
42 char *name;
43 /* Init function */
44 conf_init_func *init;
45 /* Finish function */
46 conf_finish_func *finish;
47 /* Number of successfully initialized modules */
48 int links;
49 void *usr_data;
50};
51
52/*
53 * This structure contains information about modules that have been
54 * successfully initialized. There may be more than one entry for a given
55 * module.
56 */
57
58struct conf_imodule_st {
59 CONF_MODULE *pmod;
60 char *name;
61 char *value;
62 unsigned long flags;
63 void *usr_data;
64};
65
66static CRYPTO_ONCE init_module_list_lock = CRYPTO_ONCE_STATIC_INIT;
67static CRYPTO_RCU_LOCK *module_list_lock = NULL;
68static STACK_OF(CONF_MODULE) *supported_modules = NULL; /* protected by lock */
69static STACK_OF(CONF_IMODULE) *initialized_modules = NULL; /* protected by lock */
70
71static CRYPTO_ONCE load_builtin_modules = CRYPTO_ONCE_STATIC_INIT;
72
73static void module_free(CONF_MODULE *md);
74static void module_finish(CONF_IMODULE *imod);
75static int module_run(const CONF *cnf, const char *name, const char *value,
76 unsigned long flags);
77static CONF_MODULE *module_add(DSO *dso, const char *name,
78 conf_init_func *ifunc,
79 conf_finish_func *ffunc);
80static CONF_MODULE *module_find(const char *name);
81static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
82 const CONF *cnf);
83static CONF_MODULE *module_load_dso(const CONF *cnf, const char *name,
84 const char *value);
85
86static int conf_modules_finish_int(void);
87
88static void module_lists_free(void)
89{
90 ossl_rcu_lock_free(module_list_lock);
91 module_list_lock = NULL;
92
93 sk_CONF_MODULE_free(supported_modules);
94 supported_modules = NULL;
95
96 sk_CONF_IMODULE_free(initialized_modules);
97 initialized_modules = NULL;
98}
99
100DEFINE_RUN_ONCE_STATIC(do_init_module_list_lock)
101{
102 module_list_lock = ossl_rcu_lock_new(1, NULL);
103 if (module_list_lock == NULL) {
104 ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB);
105 return 0;
106 }
107
108 return 1;
109}
110
111static int conf_diagnostics(const CONF *cnf)
112{
113 return _CONF_get_number(cnf, NULL, "config_diagnostics") != 0;
114}
115
116/* Main function: load modules from a CONF structure */
117
118int CONF_modules_load(const CONF *cnf, const char *appname,
119 unsigned long flags)
120{
121 STACK_OF(CONF_VALUE) *values;
122 CONF_VALUE *vl;
123 char *vsection = NULL;
124 int ret, i;
125
126 if (!cnf)
127 return 1;
128
129 if (conf_diagnostics(cnf))
130 flags &= ~(CONF_MFLAGS_IGNORE_ERRORS
131 | CONF_MFLAGS_IGNORE_RETURN_CODES
132 | CONF_MFLAGS_SILENT
133 | CONF_MFLAGS_IGNORE_MISSING_FILE);
134
135 ERR_set_mark();
136 if (appname)
137 vsection = NCONF_get_string(cnf, NULL, appname);
138
139 if (!appname || (!vsection && (flags & CONF_MFLAGS_DEFAULT_SECTION)))
140 vsection = NCONF_get_string(cnf, NULL, "openssl_conf");
141
142 if (!vsection) {
143 ERR_pop_to_mark();
144 return 1;
145 }
146
147 OSSL_TRACE1(CONF, "Configuration in section %s\n", vsection);
148 values = NCONF_get_section(cnf, vsection);
149
150 if (values == NULL) {
151 if (!(flags & CONF_MFLAGS_SILENT)) {
152 ERR_clear_last_mark();
153 ERR_raise_data(ERR_LIB_CONF,
154 CONF_R_OPENSSL_CONF_REFERENCES_MISSING_SECTION,
155 "openssl_conf=%s", vsection);
156 } else {
157 ERR_pop_to_mark();
158 }
159 return 0;
160 }
161 ERR_pop_to_mark();
162
163 for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
164 vl = sk_CONF_VALUE_value(values, i);
165 ERR_set_mark();
166 ret = module_run(cnf, vl->name, vl->value, flags);
167 OSSL_TRACE3(CONF, "Running module %s (%s) returned %d\n",
168 vl->name, vl->value, ret);
169 if (ret <= 0)
170 if (!(flags & CONF_MFLAGS_IGNORE_ERRORS)) {
171 ERR_clear_last_mark();
172 return ret;
173 }
174 ERR_pop_to_mark();
175 }
176
177 return 1;
178
179}
180
181int CONF_modules_load_file_ex(OSSL_LIB_CTX *libctx, const char *filename,
182 const char *appname, unsigned long flags)
183{
184 char *file = NULL;
185 CONF *conf = NULL;
186 int ret = 0, diagnostics = 0;
187
188 ERR_set_mark();
189
190 if (filename == NULL) {
191 file = CONF_get1_default_config_file();
192 if (file == NULL)
193 goto err;
194 if (*file == '\0') {
195 /* Do not try to load an empty file name but do not error out */
196 ret = 1;
197 goto err;
198 }
199 } else {
200 file = (char *)filename;
201 }
202
203 conf = NCONF_new_ex(libctx, NULL);
204 if (conf == NULL)
205 goto err;
206
207 if (NCONF_load(conf, file, NULL) <= 0) {
208 if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
209 (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE)) {
210 ret = 1;
211 }
212 goto err;
213 }
214
215 ret = CONF_modules_load(conf, appname, flags);
216 diagnostics = conf_diagnostics(conf);
217
218 err:
219 if (filename == NULL)
220 OPENSSL_free(file);
221 NCONF_free(conf);
222
223 if ((flags & CONF_MFLAGS_IGNORE_RETURN_CODES) != 0 && !diagnostics)
224 ret = 1;
225
226 if (ret > 0)
227 ERR_pop_to_mark();
228 else
229 ERR_clear_last_mark();
230
231 return ret;
232}
233
234int CONF_modules_load_file(const char *filename,
235 const char *appname, unsigned long flags)
236{
237 return CONF_modules_load_file_ex(NULL, filename, appname, flags);
238}
239
240DEFINE_RUN_ONCE_STATIC(do_load_builtin_modules)
241{
242 OPENSSL_load_builtin_modules();
243#ifndef OPENSSL_NO_ENGINE
244 /* Need to load ENGINEs */
245 ENGINE_load_builtin_engines();
246#endif
247 return 1;
248}
249
250static int module_run(const CONF *cnf, const char *name, const char *value,
251 unsigned long flags)
252{
253 CONF_MODULE *md;
254 int ret;
255
256 if (!RUN_ONCE(&load_builtin_modules, do_load_builtin_modules))
257 return -1;
258
259 md = module_find(name);
260
261 /* Module not found: try to load DSO */
262 if (!md && !(flags & CONF_MFLAGS_NO_DSO))
263 md = module_load_dso(cnf, name, value);
264
265 if (!md) {
266 if (!(flags & CONF_MFLAGS_SILENT)) {
267 ERR_raise_data(ERR_LIB_CONF, CONF_R_UNKNOWN_MODULE_NAME,
268 "module=%s", name);
269 }
270 return -1;
271 }
272
273 ret = module_init(md, name, value, cnf);
274
275 if (ret <= 0) {
276 if (!(flags & CONF_MFLAGS_SILENT))
277 ERR_raise_data(ERR_LIB_CONF, CONF_R_MODULE_INITIALIZATION_ERROR,
278 "module=%s, value=%s retcode=%-8d",
279 name, value, ret);
280 }
281
282 return ret;
283}
284
285/* Load a module from a DSO */
286static CONF_MODULE *module_load_dso(const CONF *cnf,
287 const char *name, const char *value)
288{
289 DSO *dso = NULL;
290 conf_init_func *ifunc;
291 conf_finish_func *ffunc;
292 const char *path = NULL;
293 int errcode = 0;
294 CONF_MODULE *md;
295
296 /* Look for alternative path in module section */
297 path = _CONF_get_string(cnf, value, "path");
298 if (path == NULL) {
299 path = name;
300 }
301 dso = DSO_load(NULL, path, NULL, 0);
302 if (dso == NULL) {
303 errcode = CONF_R_ERROR_LOADING_DSO;
304 goto err;
305 }
306 ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
307 if (ifunc == NULL) {
308 errcode = CONF_R_MISSING_INIT_FUNCTION;
309 goto err;
310 }
311 ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
312 /* All OK, add module */
313 md = module_add(dso, name, ifunc, ffunc);
314
315 if (md == NULL)
316 goto err;
317
318 return md;
319
320 err:
321 DSO_free(dso);
322 ERR_raise_data(ERR_LIB_CONF, errcode, "module=%s, path=%s", name, path);
323 return NULL;
324}
325
326/* add module to list */
327static CONF_MODULE *module_add(DSO *dso, const char *name,
328 conf_init_func *ifunc, conf_finish_func *ffunc)
329{
330 CONF_MODULE *tmod = NULL;
331 STACK_OF(CONF_MODULE) *old_modules;
332 STACK_OF(CONF_MODULE) *new_modules;
333
334 if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock))
335 return NULL;
336
337 ossl_rcu_write_lock(module_list_lock);
338
339 old_modules = ossl_rcu_deref(&supported_modules);
340
341 if (old_modules == NULL)
342 new_modules = sk_CONF_MODULE_new_null();
343 else
344 new_modules = sk_CONF_MODULE_dup(old_modules);
345
346 if (new_modules == NULL)
347 goto err;
348
349 if ((tmod = OPENSSL_zalloc(sizeof(*tmod))) == NULL)
350 goto err;
351
352 tmod->dso = dso;
353 tmod->name = OPENSSL_strdup(name);
354 tmod->init = ifunc;
355 tmod->finish = ffunc;
356 if (tmod->name == NULL)
357 goto err;
358
359 if (!sk_CONF_MODULE_push(new_modules, tmod))
360 goto err;
361
362 ossl_rcu_assign_ptr(&supported_modules, &new_modules);
363 ossl_rcu_write_unlock(module_list_lock);
364 ossl_synchronize_rcu(module_list_lock);
365
366 sk_CONF_MODULE_free(old_modules);
367 return tmod;
368
369 err:
370 ossl_rcu_write_unlock(module_list_lock);
371 sk_CONF_MODULE_free(new_modules);
372 if (tmod != NULL) {
373 OPENSSL_free(tmod->name);
374 OPENSSL_free(tmod);
375 }
376 sk_CONF_MODULE_free(new_modules);
377 return NULL;
378}
379
380/*
381 * Find a module from the list. We allow module names of the form
382 * modname.XXXX to just search for modname to allow the same module to be
383 * initialized more than once.
384 */
385
386static CONF_MODULE *module_find(const char *name)
387{
388 CONF_MODULE *tmod;
389 int i, nchar;
390 char *p;
391 STACK_OF(CONF_MODULE) *mods;
392
393 p = strrchr(name, '.');
394
395 if (p)
396 nchar = p - name;
397 else
398 nchar = strlen(name);
399
400 if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock))
401 return NULL;
402
403 ossl_rcu_read_lock(module_list_lock);
404 mods = ossl_rcu_deref(&supported_modules);
405
406 for (i = 0; i < sk_CONF_MODULE_num(mods); i++) {
407 tmod = sk_CONF_MODULE_value(mods, i);
408 if (strncmp(tmod->name, name, nchar) == 0) {
409 ossl_rcu_read_unlock(module_list_lock);
410 return tmod;
411 }
412 }
413
414 ossl_rcu_read_unlock(module_list_lock);
415 return NULL;
416}
417
418/* initialize a module */
419static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
420 const CONF *cnf)
421{
422 int ret = 1;
423 int init_called = 0;
424 CONF_IMODULE *imod = NULL;
425 STACK_OF(CONF_IMODULE) *old_modules;
426 STACK_OF(CONF_IMODULE) *new_modules;
427
428 /* Otherwise add initialized module to list */
429 imod = OPENSSL_malloc(sizeof(*imod));
430 if (imod == NULL)
431 goto err;
432
433 imod->pmod = pmod;
434 imod->name = OPENSSL_strdup(name);
435 imod->value = OPENSSL_strdup(value);
436 imod->usr_data = NULL;
437
438 if (!imod->name || !imod->value)
439 goto memerr;
440
441 /* Try to initialize module */
442 if (pmod->init) {
443 ret = pmod->init(imod, cnf);
444 init_called = 1;
445 /* Error occurred, exit */
446 if (ret <= 0)
447 goto err;
448 }
449
450 if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock))
451 goto err;
452
453 ossl_rcu_write_lock(module_list_lock);
454
455 old_modules = ossl_rcu_deref(&initialized_modules);
456
457 if (old_modules == NULL)
458 new_modules = sk_CONF_IMODULE_new_null();
459 else
460 new_modules = sk_CONF_IMODULE_dup(old_modules);
461
462 if (new_modules == NULL) {
463 ossl_rcu_write_unlock(module_list_lock);
464 ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB);
465 goto err;
466 }
467
468 if (!sk_CONF_IMODULE_push(new_modules, imod)) {
469 ossl_rcu_write_unlock(module_list_lock);
470 sk_CONF_IMODULE_free(new_modules);
471 ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB);
472 goto err;
473 }
474
475 pmod->links++;
476
477 ossl_rcu_assign_ptr(&initialized_modules, &new_modules);
478 ossl_rcu_write_unlock(module_list_lock);
479 ossl_synchronize_rcu(module_list_lock);
480 sk_CONF_IMODULE_free(old_modules);
481 return ret;
482
483 err:
484
485 /* We've started the module so we'd better finish it */
486 if (pmod->finish && init_called)
487 pmod->finish(imod);
488
489 memerr:
490 if (imod) {
491 OPENSSL_free(imod->name);
492 OPENSSL_free(imod->value);
493 OPENSSL_free(imod);
494 }
495
496 return -1;
497
498}
499
500/*
501 * Unload any dynamic modules that have a link count of zero: i.e. have no
502 * active initialized modules. If 'all' is set then all modules are unloaded
503 * including static ones.
504 */
505
506void CONF_modules_unload(int all)
507{
508 int i;
509 CONF_MODULE *md;
510 STACK_OF(CONF_MODULE) *old_modules;
511 STACK_OF(CONF_MODULE) *new_modules;
512 STACK_OF(CONF_MODULE) *to_delete;
513
514 if (!conf_modules_finish_int()) /* also inits module list lock */
515 return;
516
517 ossl_rcu_write_lock(module_list_lock);
518
519 old_modules = ossl_rcu_deref(&supported_modules);
520 new_modules = sk_CONF_MODULE_dup(old_modules);
521
522 if (new_modules == NULL) {
523 ossl_rcu_write_unlock(module_list_lock);
524 return;
525 }
526
527 to_delete = sk_CONF_MODULE_new_null();
528
529 /* unload modules in reverse order */
530 for (i = sk_CONF_MODULE_num(new_modules) - 1; i >= 0; i--) {
531 md = sk_CONF_MODULE_value(new_modules, i);
532 /* If static or in use and 'all' not set ignore it */
533 if (((md->links > 0) || !md->dso) && !all)
534 continue;
535 /* Since we're working in reverse this is OK */
536 (void)sk_CONF_MODULE_delete(new_modules, i);
537 sk_CONF_MODULE_push(to_delete, md);
538 }
539
540 if (sk_CONF_MODULE_num(new_modules) == 0) {
541 sk_CONF_MODULE_free(new_modules);
542 new_modules = NULL;
543 }
544
545 ossl_rcu_assign_ptr(&supported_modules, &new_modules);
546 ossl_rcu_write_unlock(module_list_lock);
547 ossl_synchronize_rcu(module_list_lock);
548 sk_CONF_MODULE_free(old_modules);
549 sk_CONF_MODULE_pop_free(to_delete, module_free);
550
551}
552
553/* unload a single module */
554static void module_free(CONF_MODULE *md)
555{
556 DSO_free(md->dso);
557 OPENSSL_free(md->name);
558 OPENSSL_free(md);
559}
560
561/* finish and free up all modules instances */
562
563static int conf_modules_finish_int(void)
564{
565 CONF_IMODULE *imod;
566 STACK_OF(CONF_IMODULE) *old_modules;
567 STACK_OF(CONF_IMODULE) *new_modules = NULL;
568
569 if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock))
570 return 0;
571
572 /* If module_list_lock is NULL here it means we were already unloaded */
573 if (module_list_lock == NULL)
574 return 0;
575
576 ossl_rcu_write_lock(module_list_lock);
577 old_modules = ossl_rcu_deref(&initialized_modules);
578 ossl_rcu_assign_ptr(&initialized_modules, &new_modules);
579 ossl_rcu_write_unlock(module_list_lock);
580 ossl_synchronize_rcu(module_list_lock);
581
582 while (sk_CONF_IMODULE_num(old_modules) > 0) {
583 imod = sk_CONF_IMODULE_pop(old_modules);
584 module_finish(imod);
585 }
586 sk_CONF_IMODULE_free(old_modules);
587
588 return 1;
589}
590
591void CONF_modules_finish(void)
592{
593 conf_modules_finish_int();
594}
595
596/* finish a module instance */
597
598static void module_finish(CONF_IMODULE *imod)
599{
600 if (!imod)
601 return;
602 if (imod->pmod->finish)
603 imod->pmod->finish(imod);
604 imod->pmod->links--;
605 OPENSSL_free(imod->name);
606 OPENSSL_free(imod->value);
607 OPENSSL_free(imod);
608}
609
610/* Add a static module to OpenSSL */
611
612int CONF_module_add(const char *name, conf_init_func *ifunc,
613 conf_finish_func *ffunc)
614{
615 if (module_add(NULL, name, ifunc, ffunc))
616 return 1;
617 else
618 return 0;
619}
620
621void ossl_config_modules_free(void)
622{
623 CONF_modules_unload(1); /* calls CONF_modules_finish */
624 module_lists_free();
625}
626
627/* Utility functions */
628
629const char *CONF_imodule_get_name(const CONF_IMODULE *md)
630{
631 return md->name;
632}
633
634const char *CONF_imodule_get_value(const CONF_IMODULE *md)
635{
636 return md->value;
637}
638
639void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
640{
641 return md->usr_data;
642}
643
644void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
645{
646 md->usr_data = usr_data;
647}
648
649CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
650{
651 return md->pmod;
652}
653
654unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
655{
656 return md->flags;
657}
658
659void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
660{
661 md->flags = flags;
662}
663
664void *CONF_module_get_usr_data(CONF_MODULE *pmod)
665{
666 return pmod->usr_data;
667}
668
669void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
670{
671 pmod->usr_data = usr_data;
672}
673
674/* Return default config file name */
675char *CONF_get1_default_config_file(void)
676{
677 const char *t;
678 char *file, *sep = "";
679 size_t size;
680
681 if ((file = ossl_safe_getenv("OPENSSL_CONF")) != NULL)
682 return OPENSSL_strdup(file);
683
684 t = X509_get_default_cert_area();
685#ifndef OPENSSL_SYS_VMS
686 sep = "/";
687#endif
688 size = strlen(t) + strlen(sep) + strlen(OPENSSL_CONF) + 1;
689 file = OPENSSL_malloc(size);
690
691 if (file == NULL)
692 return NULL;
693 BIO_snprintf(file, size, "%s%s%s", t, sep, OPENSSL_CONF);
694
695 return file;
696}
697
698/*
699 * This function takes a list separated by 'sep' and calls the callback
700 * function giving the start and length of each member optionally stripping
701 * leading and trailing whitespace. This can be used to parse comma separated
702 * lists for example.
703 */
704
705int CONF_parse_list(const char *list_, int sep, int nospc,
706 int (*list_cb) (const char *elem, int len, void *usr),
707 void *arg)
708{
709 int ret;
710 const char *lstart, *tmpend, *p;
711
712 if (list_ == NULL) {
713 ERR_raise(ERR_LIB_CONF, CONF_R_LIST_CANNOT_BE_NULL);
714 return 0;
715 }
716
717 lstart = list_;
718 for (;;) {
719 if (nospc) {
720 while (*lstart && isspace((unsigned char)*lstart))
721 lstart++;
722 }
723 p = strchr(lstart, sep);
724 if (p == lstart || *lstart == '\0')
725 ret = list_cb(NULL, 0, arg);
726 else {
727 if (p)
728 tmpend = p - 1;
729 else
730 tmpend = lstart + strlen(lstart) - 1;
731 if (nospc) {
732 while (isspace((unsigned char)*tmpend))
733 tmpend--;
734 }
735 ret = list_cb(lstart, tmpend - lstart + 1, arg);
736 }
737 if (ret <= 0)
738 return ret;
739 if (p == NULL)
740 return 1;
741 lstart = p + 1;
742 }
743}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette