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