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