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