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