1 /* $OpenBSD: conf_mod.c,v 1.28 2023/07/20 15:05:30 tb Exp $ */ 2 /* Written by Stephen Henson (steve@openssl.org) for the OpenSSL 3 * project 2001. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 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 #include <ctype.h> 60 #include <stdio.h> 61 #include <string.h> 62 #include <unistd.h> 63 64 #include <openssl/conf.h> 65 #include <openssl/crypto.h> 66 #include <openssl/err.h> 67 #include <openssl/x509.h> 68 69 /* This structure contains data about supported modules. */ 70 struct conf_module_st { 71 /* Name of the module */ 72 char *name; 73 /* Init function */ 74 conf_init_func *init; 75 /* Finish function */ 76 conf_finish_func *finish; 77 /* Number of successfully initialized modules */ 78 int links; 79 void *usr_data; 80 }; 81 82 83 /* This structure contains information about modules that have been 84 * successfully initialized. There may be more than one entry for a 85 * given module. 86 */ 87 88 struct conf_imodule_st { 89 CONF_MODULE *pmod; 90 char *name; 91 char *value; 92 unsigned long flags; 93 void *usr_data; 94 }; 95 96 static STACK_OF(CONF_MODULE) *supported_modules = NULL; 97 static STACK_OF(CONF_IMODULE) *initialized_modules = NULL; 98 99 static void module_free(CONF_MODULE *md); 100 static void module_finish(CONF_IMODULE *imod); 101 static int module_run(const CONF *cnf, char *name, char *value, 102 unsigned long flags); 103 static CONF_MODULE *module_add(const char *name, conf_init_func *ifunc, 104 conf_finish_func *ffunc); 105 static CONF_MODULE *module_find(char *name); 106 static int module_init(CONF_MODULE *pmod, char *name, char *value, 107 const CONF *cnf); 108 109 /* Main function: load modules from a CONF structure */ 110 111 int 112 CONF_modules_load(const CONF *cnf, const char *appname, unsigned long flags) 113 { 114 STACK_OF(CONF_VALUE) *values; 115 CONF_VALUE *vl; 116 char *vsection = NULL; 117 118 int ret, i; 119 120 if (!cnf) 121 return 1; 122 123 if (appname) 124 vsection = NCONF_get_string(cnf, NULL, appname); 125 126 if (!appname || (!vsection && (flags & CONF_MFLAGS_DEFAULT_SECTION))) 127 vsection = NCONF_get_string(cnf, NULL, "openssl_conf"); 128 129 if (!vsection) { 130 ERR_clear_error(); 131 return 1; 132 } 133 134 values = NCONF_get_section(cnf, vsection); 135 136 if (!values) 137 return 0; 138 139 for (i = 0; i < sk_CONF_VALUE_num(values); i++) { 140 vl = sk_CONF_VALUE_value(values, i); 141 ret = module_run(cnf, vl->name, vl->value, flags); 142 if (ret <= 0) 143 if (!(flags & CONF_MFLAGS_IGNORE_ERRORS)) 144 return ret; 145 } 146 147 return 1; 148 } 149 150 int 151 CONF_modules_load_file(const char *filename, const char *appname, 152 unsigned long flags) 153 { 154 char *file = NULL; 155 CONF *conf = NULL; 156 int ret = 0; 157 conf = NCONF_new(NULL); 158 if (!conf) 159 goto err; 160 161 if (filename == NULL) { 162 file = CONF_get1_default_config_file(); 163 if (!file) 164 goto err; 165 } else 166 file = (char *)filename; 167 168 if (NCONF_load(conf, file, NULL) <= 0) { 169 if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) && 170 (ERR_GET_REASON(ERR_peek_last_error()) == 171 CONF_R_NO_SUCH_FILE)) { 172 ERR_clear_error(); 173 ret = 1; 174 } 175 goto err; 176 } 177 178 ret = CONF_modules_load(conf, appname, flags); 179 180 err: 181 if (filename == NULL) 182 free(file); 183 NCONF_free(conf); 184 185 return ret; 186 } 187 188 static int 189 module_run(const CONF *cnf, char *name, char *value, unsigned long flags) 190 { 191 CONF_MODULE *md; 192 int ret; 193 194 if ((md = module_find(name)) == NULL) { 195 if (!(flags & CONF_MFLAGS_SILENT)) { 196 CONFerror(CONF_R_UNKNOWN_MODULE_NAME); 197 ERR_asprintf_error_data("module=%s", name); 198 } 199 return -1; 200 } 201 202 ret = module_init(md, name, value, cnf); 203 204 if (ret <= 0) { 205 if (!(flags & CONF_MFLAGS_SILENT)) { 206 CONFerror(CONF_R_MODULE_INITIALIZATION_ERROR); 207 ERR_asprintf_error_data 208 ("module=%s, value=%s, retcode=%-8d", 209 name, value, ret); 210 } 211 } 212 213 return ret; 214 } 215 216 /* add module to list */ 217 static CONF_MODULE * 218 module_add(const char *name, conf_init_func *ifunc, conf_finish_func *ffunc) 219 { 220 CONF_MODULE *tmod = NULL; 221 222 if (name == NULL) 223 return NULL; 224 if (supported_modules == NULL) 225 supported_modules = sk_CONF_MODULE_new_null(); 226 if (supported_modules == NULL) 227 return NULL; 228 tmod = malloc(sizeof(CONF_MODULE)); 229 if (tmod == NULL) 230 return NULL; 231 232 tmod->name = strdup(name); 233 tmod->init = ifunc; 234 tmod->finish = ffunc; 235 tmod->links = 0; 236 237 if (!sk_CONF_MODULE_push(supported_modules, tmod)) { 238 free(tmod); 239 return NULL; 240 } 241 242 return tmod; 243 } 244 245 /* Find a module from the list. We allow module names of the 246 * form modname.XXXX to just search for modname to allow the 247 * same module to be initialized more than once. 248 */ 249 250 static CONF_MODULE * 251 module_find(char *name) 252 { 253 CONF_MODULE *tmod; 254 int i, nchar; 255 char *p; 256 257 p = strrchr(name, '.'); 258 259 if (p) 260 nchar = p - name; 261 else 262 nchar = strlen(name); 263 264 for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++) { 265 tmod = sk_CONF_MODULE_value(supported_modules, i); 266 if (!strncmp(tmod->name, name, nchar)) 267 return tmod; 268 } 269 270 return NULL; 271 } 272 273 /* initialize a module */ 274 static int 275 module_init(CONF_MODULE *pmod, char *name, char *value, const CONF *cnf) 276 { 277 int ret = 1; 278 int init_called = 0; 279 CONF_IMODULE *imod = NULL; 280 281 /* Otherwise add initialized module to list */ 282 imod = malloc(sizeof(CONF_IMODULE)); 283 if (!imod) 284 goto err; 285 286 imod->pmod = pmod; 287 imod->name = name ? strdup(name) : NULL; 288 imod->value = value ? strdup(value) : NULL; 289 imod->usr_data = NULL; 290 291 if (!imod->name || !imod->value) 292 goto memerr; 293 294 /* Try to initialize module */ 295 if (pmod->init) { 296 ret = pmod->init(imod, cnf); 297 init_called = 1; 298 /* Error occurred, exit */ 299 if (ret <= 0) 300 goto err; 301 } 302 303 if (initialized_modules == NULL) { 304 initialized_modules = sk_CONF_IMODULE_new_null(); 305 if (!initialized_modules) { 306 CONFerror(ERR_R_MALLOC_FAILURE); 307 goto err; 308 } 309 } 310 311 if (!sk_CONF_IMODULE_push(initialized_modules, imod)) { 312 CONFerror(ERR_R_MALLOC_FAILURE); 313 goto err; 314 } 315 316 pmod->links++; 317 318 return ret; 319 320 err: 321 /* We've started the module so we'd better finish it */ 322 if (pmod->finish && init_called) 323 pmod->finish(imod); 324 325 memerr: 326 if (imod) { 327 free(imod->name); 328 free(imod->value); 329 free(imod); 330 } 331 332 return -1; 333 } 334 335 /* Unload any dynamic modules that have a link count of zero: 336 * i.e. have no active initialized modules. If 'all' is set 337 * then all modules are unloaded including static ones. 338 */ 339 340 void 341 CONF_modules_unload(int all) 342 { 343 int i; 344 CONF_MODULE *md; 345 346 CONF_modules_finish(); 347 348 /* unload modules in reverse order */ 349 for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--) { 350 md = sk_CONF_MODULE_value(supported_modules, i); 351 if (!all) 352 continue; 353 /* Since we're working in reverse this is OK */ 354 (void)sk_CONF_MODULE_delete(supported_modules, i); 355 module_free(md); 356 } 357 if (sk_CONF_MODULE_num(supported_modules) == 0) { 358 sk_CONF_MODULE_free(supported_modules); 359 supported_modules = NULL; 360 } 361 } 362 363 /* unload a single module */ 364 static void 365 module_free(CONF_MODULE *md) 366 { 367 free(md->name); 368 free(md); 369 } 370 371 /* finish and free up all modules instances */ 372 373 void 374 CONF_modules_finish(void) 375 { 376 CONF_IMODULE *imod; 377 378 while (sk_CONF_IMODULE_num(initialized_modules) > 0) { 379 imod = sk_CONF_IMODULE_pop(initialized_modules); 380 module_finish(imod); 381 } 382 sk_CONF_IMODULE_free(initialized_modules); 383 initialized_modules = NULL; 384 } 385 386 /* finish a module instance */ 387 388 static void 389 module_finish(CONF_IMODULE *imod) 390 { 391 if (imod->pmod->finish) 392 imod->pmod->finish(imod); 393 imod->pmod->links--; 394 free(imod->name); 395 free(imod->value); 396 free(imod); 397 } 398 399 /* Add a static module to OpenSSL */ 400 401 int 402 CONF_module_add(const char *name, conf_init_func *ifunc, conf_finish_func *ffunc) 403 { 404 return module_add(name, ifunc, ffunc) != NULL; 405 } 406 407 void 408 CONF_modules_free(void) 409 { 410 CONF_modules_finish(); 411 CONF_modules_unload(1); 412 } 413 414 /* Utility functions */ 415 416 const char * 417 CONF_imodule_get_name(const CONF_IMODULE *md) 418 { 419 return md->name; 420 } 421 422 const char * 423 CONF_imodule_get_value(const CONF_IMODULE *md) 424 { 425 return md->value; 426 } 427 428 void * 429 CONF_imodule_get_usr_data(const CONF_IMODULE *md) 430 { 431 return md->usr_data; 432 } 433 434 void 435 CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data) 436 { 437 md->usr_data = usr_data; 438 } 439 440 CONF_MODULE * 441 CONF_imodule_get_module(const CONF_IMODULE *md) 442 { 443 return md->pmod; 444 } 445 446 unsigned long 447 CONF_imodule_get_flags(const CONF_IMODULE *md) 448 { 449 return md->flags; 450 } 451 452 void 453 CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags) 454 { 455 md->flags = flags; 456 } 457 458 void * 459 CONF_module_get_usr_data(CONF_MODULE *pmod) 460 { 461 return pmod->usr_data; 462 } 463 464 void 465 CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data) 466 { 467 pmod->usr_data = usr_data; 468 } 469 470 /* Return default config file name */ 471 472 char * 473 CONF_get1_default_config_file(void) 474 { 475 char *file = NULL; 476 477 if (asprintf(&file, "%s/openssl.cnf", 478 X509_get_default_cert_area()) == -1) 479 return (NULL); 480 return file; 481 } 482 483 /* This function takes a list separated by 'sep' and calls the 484 * callback function giving the start and length of each member 485 * optionally stripping leading and trailing whitespace. This can 486 * be used to parse comma separated lists for example. 487 */ 488 489 int 490 CONF_parse_list(const char *list_, int sep, int nospc, 491 int (*list_cb)(const char *elem, int len, void *usr), void *arg) 492 { 493 int ret; 494 const char *lstart, *tmpend, *p; 495 496 if (list_ == NULL) { 497 CONFerror(CONF_R_LIST_CANNOT_BE_NULL); 498 return 0; 499 } 500 501 lstart = list_; 502 for (;;) { 503 if (nospc) { 504 while (*lstart && isspace((unsigned char)*lstart)) 505 lstart++; 506 } 507 p = strchr(lstart, sep); 508 if (p == lstart || !*lstart) 509 ret = list_cb(NULL, 0, arg); 510 else { 511 if (p) 512 tmpend = p - 1; 513 else 514 tmpend = lstart + strlen(lstart) - 1; 515 if (nospc) { 516 while (isspace((unsigned char)*tmpend)) 517 tmpend--; 518 } 519 ret = list_cb(lstart, tmpend - lstart + 1, arg); 520 } 521 if (ret <= 0) 522 return ret; 523 if (p == NULL) 524 return 1; 525 lstart = p + 1; 526 } 527 } 528