/src/openssl/crypto/conf/conf_mod.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2002-2021 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 | 1 | { |
89 | 1 | CRYPTO_THREAD_lock_free(module_list_lock); |
90 | 1 | module_list_lock = NULL; |
91 | | |
92 | 1 | sk_CONF_MODULE_free(supported_modules); |
93 | 1 | supported_modules = NULL; |
94 | | |
95 | 1 | sk_CONF_IMODULE_free(initialized_modules); |
96 | 1 | initialized_modules = NULL; |
97 | 1 | } |
98 | | |
99 | | DEFINE_RUN_ONCE_STATIC(do_init_module_list_lock) |
100 | 1 | { |
101 | 1 | module_list_lock = CRYPTO_THREAD_lock_new(); |
102 | 1 | if (module_list_lock == NULL) { |
103 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB); |
104 | 0 | return 0; |
105 | 0 | } |
106 | | |
107 | 1 | return 1; |
108 | 1 | } |
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 | 1 | { |
183 | 1 | char *file = NULL; |
184 | 1 | CONF *conf = NULL; |
185 | 1 | int ret = 0, diagnostics = 0; |
186 | | |
187 | 1 | if (filename == NULL) { |
188 | 1 | file = CONF_get1_default_config_file(); |
189 | 1 | if (file == NULL) |
190 | 0 | goto err; |
191 | 1 | } else { |
192 | 0 | file = (char *)filename; |
193 | 0 | } |
194 | | |
195 | 1 | ERR_set_mark(); |
196 | 1 | conf = NCONF_new_ex(libctx, NULL); |
197 | 1 | if (conf == NULL) |
198 | 0 | goto err; |
199 | | |
200 | 1 | if (NCONF_load(conf, file, NULL) <= 0) { |
201 | 1 | if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) && |
202 | 1 | (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE)) { |
203 | 1 | ret = 1; |
204 | 1 | } |
205 | 1 | goto err; |
206 | 1 | } |
207 | | |
208 | 0 | ret = CONF_modules_load(conf, appname, flags); |
209 | 0 | diagnostics = conf_diagnostics(conf); |
210 | |
|
211 | 1 | err: |
212 | 1 | if (filename == NULL) |
213 | 1 | OPENSSL_free(file); |
214 | 1 | NCONF_free(conf); |
215 | | |
216 | 1 | if ((flags & CONF_MFLAGS_IGNORE_RETURN_CODES) != 0 && !diagnostics) |
217 | 1 | ret = 1; |
218 | | |
219 | 1 | if (ret > 0) |
220 | 1 | ERR_pop_to_mark(); |
221 | 0 | else |
222 | 0 | ERR_clear_last_mark(); |
223 | | |
224 | 1 | return ret; |
225 | 0 | } |
226 | | |
227 | | int CONF_modules_load_file(const char *filename, |
228 | | const char *appname, unsigned long flags) |
229 | 1 | { |
230 | 1 | return CONF_modules_load_file_ex(NULL, filename, appname, flags); |
231 | 1 | } |
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 | goto err; |
337 | | |
338 | 0 | tmod->dso = dso; |
339 | 0 | tmod->name = OPENSSL_strdup(name); |
340 | 0 | tmod->init = ifunc; |
341 | 0 | tmod->finish = ffunc; |
342 | 0 | if (tmod->name == NULL) |
343 | 0 | goto err; |
344 | | |
345 | 0 | if (!sk_CONF_MODULE_push(supported_modules, tmod)) |
346 | 0 | goto err; |
347 | | |
348 | 0 | CRYPTO_THREAD_unlock(module_list_lock); |
349 | 0 | return tmod; |
350 | | |
351 | 0 | err: |
352 | 0 | CRYPTO_THREAD_unlock(module_list_lock); |
353 | 0 | if (tmod != NULL) { |
354 | 0 | OPENSSL_free(tmod->name); |
355 | 0 | OPENSSL_free(tmod); |
356 | 0 | } |
357 | 0 | return NULL; |
358 | 0 | } |
359 | | |
360 | | /* |
361 | | * Find a module from the list. We allow module names of the form |
362 | | * modname.XXXX to just search for modname to allow the same module to be |
363 | | * initialized more than once. |
364 | | */ |
365 | | |
366 | | static CONF_MODULE *module_find(const char *name) |
367 | 0 | { |
368 | 0 | CONF_MODULE *tmod; |
369 | 0 | int i, nchar; |
370 | 0 | char *p; |
371 | 0 | p = strrchr(name, '.'); |
372 | |
|
373 | 0 | if (p) |
374 | 0 | nchar = p - name; |
375 | 0 | else |
376 | 0 | nchar = strlen(name); |
377 | |
|
378 | 0 | if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock)) |
379 | 0 | return NULL; |
380 | | |
381 | 0 | if (!CRYPTO_THREAD_read_lock(module_list_lock)) |
382 | 0 | return NULL; |
383 | | |
384 | 0 | for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++) { |
385 | 0 | tmod = sk_CONF_MODULE_value(supported_modules, i); |
386 | 0 | if (strncmp(tmod->name, name, nchar) == 0) { |
387 | 0 | CRYPTO_THREAD_unlock(module_list_lock); |
388 | 0 | return tmod; |
389 | 0 | } |
390 | 0 | } |
391 | | |
392 | 0 | CRYPTO_THREAD_unlock(module_list_lock); |
393 | 0 | return NULL; |
394 | 0 | } |
395 | | |
396 | | /* initialize a module */ |
397 | | static int module_init(CONF_MODULE *pmod, const char *name, const char *value, |
398 | | const CONF *cnf) |
399 | 0 | { |
400 | 0 | int ret = 1; |
401 | 0 | int init_called = 0; |
402 | 0 | CONF_IMODULE *imod = NULL; |
403 | | |
404 | | /* Otherwise add initialized module to list */ |
405 | 0 | imod = OPENSSL_malloc(sizeof(*imod)); |
406 | 0 | if (imod == NULL) |
407 | 0 | goto err; |
408 | | |
409 | 0 | imod->pmod = pmod; |
410 | 0 | imod->name = OPENSSL_strdup(name); |
411 | 0 | imod->value = OPENSSL_strdup(value); |
412 | 0 | imod->usr_data = NULL; |
413 | |
|
414 | 0 | if (!imod->name || !imod->value) |
415 | 0 | goto memerr; |
416 | | |
417 | | /* Try to initialize module */ |
418 | 0 | if (pmod->init) { |
419 | 0 | ret = pmod->init(imod, cnf); |
420 | 0 | init_called = 1; |
421 | | /* Error occurred, exit */ |
422 | 0 | if (ret <= 0) |
423 | 0 | goto err; |
424 | 0 | } |
425 | | |
426 | 0 | if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock)) |
427 | 0 | goto err; |
428 | | |
429 | 0 | if (!CRYPTO_THREAD_write_lock(module_list_lock)) |
430 | 0 | goto err; |
431 | | |
432 | 0 | if (initialized_modules == NULL) { |
433 | 0 | initialized_modules = sk_CONF_IMODULE_new_null(); |
434 | 0 | if (initialized_modules == NULL) { |
435 | 0 | CRYPTO_THREAD_unlock(module_list_lock); |
436 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB); |
437 | 0 | goto err; |
438 | 0 | } |
439 | 0 | } |
440 | | |
441 | 0 | if (!sk_CONF_IMODULE_push(initialized_modules, imod)) { |
442 | 0 | CRYPTO_THREAD_unlock(module_list_lock); |
443 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB); |
444 | 0 | goto err; |
445 | 0 | } |
446 | | |
447 | 0 | pmod->links++; |
448 | |
|
449 | 0 | CRYPTO_THREAD_unlock(module_list_lock); |
450 | 0 | return ret; |
451 | | |
452 | 0 | err: |
453 | | |
454 | | /* We've started the module so we'd better finish it */ |
455 | 0 | if (pmod->finish && init_called) |
456 | 0 | pmod->finish(imod); |
457 | |
|
458 | 0 | memerr: |
459 | 0 | if (imod) { |
460 | 0 | OPENSSL_free(imod->name); |
461 | 0 | OPENSSL_free(imod->value); |
462 | 0 | OPENSSL_free(imod); |
463 | 0 | } |
464 | |
|
465 | 0 | return -1; |
466 | |
|
467 | 0 | } |
468 | | |
469 | | /* |
470 | | * Unload any dynamic modules that have a link count of zero: i.e. have no |
471 | | * active initialized modules. If 'all' is set then all modules are unloaded |
472 | | * including static ones. |
473 | | */ |
474 | | |
475 | | void CONF_modules_unload(int all) |
476 | 1 | { |
477 | 1 | int i; |
478 | 1 | CONF_MODULE *md; |
479 | | |
480 | 1 | if (!conf_modules_finish_int()) /* also inits module list lock */ |
481 | 0 | return; |
482 | | |
483 | 1 | if (!CRYPTO_THREAD_write_lock(module_list_lock)) |
484 | 0 | return; |
485 | | |
486 | | /* unload modules in reverse order */ |
487 | 1 | for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--) { |
488 | 0 | md = sk_CONF_MODULE_value(supported_modules, i); |
489 | | /* If static or in use and 'all' not set ignore it */ |
490 | 0 | if (((md->links > 0) || !md->dso) && !all) |
491 | 0 | continue; |
492 | | /* Since we're working in reverse this is OK */ |
493 | 0 | (void)sk_CONF_MODULE_delete(supported_modules, i); |
494 | 0 | module_free(md); |
495 | 0 | } |
496 | | |
497 | 1 | if (sk_CONF_MODULE_num(supported_modules) == 0) { |
498 | 0 | sk_CONF_MODULE_free(supported_modules); |
499 | 0 | supported_modules = NULL; |
500 | 0 | } |
501 | | |
502 | 1 | CRYPTO_THREAD_unlock(module_list_lock); |
503 | 1 | } |
504 | | |
505 | | /* unload a single module */ |
506 | | static void module_free(CONF_MODULE *md) |
507 | 0 | { |
508 | 0 | DSO_free(md->dso); |
509 | 0 | OPENSSL_free(md->name); |
510 | 0 | OPENSSL_free(md); |
511 | 0 | } |
512 | | |
513 | | /* finish and free up all modules instances */ |
514 | | |
515 | | static int conf_modules_finish_int(void) |
516 | 1 | { |
517 | 1 | CONF_IMODULE *imod; |
518 | | |
519 | 1 | if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock)) |
520 | 0 | return 0; |
521 | | |
522 | | /* If module_list_lock is NULL here it means we were already unloaded */ |
523 | 1 | if (module_list_lock == NULL |
524 | 1 | || !CRYPTO_THREAD_write_lock(module_list_lock)) |
525 | 0 | return 0; |
526 | | |
527 | 1 | while (sk_CONF_IMODULE_num(initialized_modules) > 0) { |
528 | 0 | imod = sk_CONF_IMODULE_pop(initialized_modules); |
529 | 0 | module_finish(imod); |
530 | 0 | } |
531 | 1 | sk_CONF_IMODULE_free(initialized_modules); |
532 | 1 | initialized_modules = NULL; |
533 | | |
534 | 1 | CRYPTO_THREAD_unlock(module_list_lock); |
535 | | |
536 | 1 | return 1; |
537 | 1 | } |
538 | | |
539 | | void CONF_modules_finish(void) |
540 | 0 | { |
541 | 0 | conf_modules_finish_int(); |
542 | 0 | } |
543 | | |
544 | | /* finish a module instance */ |
545 | | |
546 | | static void module_finish(CONF_IMODULE *imod) |
547 | 0 | { |
548 | 0 | if (!imod) |
549 | 0 | return; |
550 | 0 | if (imod->pmod->finish) |
551 | 0 | imod->pmod->finish(imod); |
552 | 0 | imod->pmod->links--; |
553 | 0 | OPENSSL_free(imod->name); |
554 | 0 | OPENSSL_free(imod->value); |
555 | 0 | OPENSSL_free(imod); |
556 | 0 | } |
557 | | |
558 | | /* Add a static module to OpenSSL */ |
559 | | |
560 | | int CONF_module_add(const char *name, conf_init_func *ifunc, |
561 | | conf_finish_func *ffunc) |
562 | 0 | { |
563 | 0 | if (module_add(NULL, name, ifunc, ffunc)) |
564 | 0 | return 1; |
565 | 0 | else |
566 | 0 | return 0; |
567 | 0 | } |
568 | | |
569 | | void ossl_config_modules_free(void) |
570 | 1 | { |
571 | 1 | CONF_modules_unload(1); /* calls CONF_modules_finish */ |
572 | 1 | module_lists_free(); |
573 | 1 | } |
574 | | |
575 | | /* Utility functions */ |
576 | | |
577 | | const char *CONF_imodule_get_name(const CONF_IMODULE *md) |
578 | 0 | { |
579 | 0 | return md->name; |
580 | 0 | } |
581 | | |
582 | | const char *CONF_imodule_get_value(const CONF_IMODULE *md) |
583 | 0 | { |
584 | 0 | return md->value; |
585 | 0 | } |
586 | | |
587 | | void *CONF_imodule_get_usr_data(const CONF_IMODULE *md) |
588 | 0 | { |
589 | 0 | return md->usr_data; |
590 | 0 | } |
591 | | |
592 | | void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data) |
593 | 0 | { |
594 | 0 | md->usr_data = usr_data; |
595 | 0 | } |
596 | | |
597 | | CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md) |
598 | 0 | { |
599 | 0 | return md->pmod; |
600 | 0 | } |
601 | | |
602 | | unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md) |
603 | 0 | { |
604 | 0 | return md->flags; |
605 | 0 | } |
606 | | |
607 | | void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags) |
608 | 0 | { |
609 | 0 | md->flags = flags; |
610 | 0 | } |
611 | | |
612 | | void *CONF_module_get_usr_data(CONF_MODULE *pmod) |
613 | 0 | { |
614 | 0 | return pmod->usr_data; |
615 | 0 | } |
616 | | |
617 | | void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data) |
618 | 0 | { |
619 | 0 | pmod->usr_data = usr_data; |
620 | 0 | } |
621 | | |
622 | | /* Return default config file name */ |
623 | | char *CONF_get1_default_config_file(void) |
624 | 1 | { |
625 | 1 | const char *t; |
626 | 1 | char *file, *sep = ""; |
627 | 1 | size_t size; |
628 | | |
629 | 1 | if ((file = ossl_safe_getenv("OPENSSL_CONF")) != NULL) |
630 | 0 | return OPENSSL_strdup(file); |
631 | | |
632 | 1 | t = X509_get_default_cert_area(); |
633 | 1 | #ifndef OPENSSL_SYS_VMS |
634 | 1 | sep = "/"; |
635 | 1 | #endif |
636 | 1 | size = strlen(t) + strlen(sep) + strlen(OPENSSL_CONF) + 1; |
637 | 1 | file = OPENSSL_malloc(size); |
638 | | |
639 | 1 | if (file == NULL) |
640 | 0 | return NULL; |
641 | 1 | BIO_snprintf(file, size, "%s%s%s", t, sep, OPENSSL_CONF); |
642 | | |
643 | 1 | return file; |
644 | 1 | } |
645 | | |
646 | | /* |
647 | | * This function takes a list separated by 'sep' and calls the callback |
648 | | * function giving the start and length of each member optionally stripping |
649 | | * leading and trailing whitespace. This can be used to parse comma separated |
650 | | * lists for example. |
651 | | */ |
652 | | |
653 | | int CONF_parse_list(const char *list_, int sep, int nospc, |
654 | | int (*list_cb) (const char *elem, int len, void *usr), |
655 | | void *arg) |
656 | 0 | { |
657 | 0 | int ret; |
658 | 0 | const char *lstart, *tmpend, *p; |
659 | |
|
660 | 0 | if (list_ == NULL) { |
661 | 0 | ERR_raise(ERR_LIB_CONF, CONF_R_LIST_CANNOT_BE_NULL); |
662 | 0 | return 0; |
663 | 0 | } |
664 | | |
665 | 0 | lstart = list_; |
666 | 0 | for (;;) { |
667 | 0 | if (nospc) { |
668 | 0 | while (*lstart && isspace((unsigned char)*lstart)) |
669 | 0 | lstart++; |
670 | 0 | } |
671 | 0 | p = strchr(lstart, sep); |
672 | 0 | if (p == lstart || *lstart == '\0') |
673 | 0 | ret = list_cb(NULL, 0, arg); |
674 | 0 | else { |
675 | 0 | if (p) |
676 | 0 | tmpend = p - 1; |
677 | 0 | else |
678 | 0 | tmpend = lstart + strlen(lstart) - 1; |
679 | 0 | if (nospc) { |
680 | 0 | while (isspace((unsigned char)*tmpend)) |
681 | 0 | tmpend--; |
682 | 0 | } |
683 | 0 | ret = list_cb(lstart, tmpend - lstart + 1, arg); |
684 | 0 | } |
685 | 0 | if (ret <= 0) |
686 | 0 | return ret; |
687 | 0 | if (p == NULL) |
688 | 0 | return 1; |
689 | 0 | lstart = p + 1; |
690 | 0 | } |
691 | 0 | } |