Coverage Report

Created: 2023-06-08 06:40

/src/openssl111/crypto/conf/conf_mod.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2002-2019 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
79
0
    int ret, i;
80
81
0
    if (!cnf)
82
0
        return 1;
83
84
0
    if (appname)
85
0
        vsection = NCONF_get_string(cnf, NULL, appname);
86
87
0
    if (!appname || (!vsection && (flags & CONF_MFLAGS_DEFAULT_SECTION)))
88
0
        vsection = NCONF_get_string(cnf, NULL, "openssl_conf");
89
90
0
    if (!vsection) {
91
0
        ERR_clear_error();
92
0
        return 1;
93
0
    }
94
95
0
    values = NCONF_get_section(cnf, vsection);
96
97
0
    if (!values)
98
0
        return 0;
99
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
108
0
    return 1;
109
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
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
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
138
0
    ret = CONF_modules_load(conf, appname, flags);
139
140
0
 err:
141
0
    if (filename == NULL)
142
0
        OPENSSL_free(file);
143
0
    NCONF_free(conf);
144
145
0
    if (flags & CONF_MFLAGS_IGNORE_RETURN_CODES)
146
0
        return 1;
147
148
0
    return ret;
149
0
}
150
151
static int module_run(const CONF *cnf, const char *name, const char *value,
152
                      unsigned long flags)
153
0
{
154
0
    CONF_MODULE *md;
155
0
    int ret;
156
157
0
    md = module_find(name);
158
159
    /* Module not found: try to load DSO */
160
0
    if (!md && !(flags & CONF_MFLAGS_NO_DSO))
161
0
        md = module_load_dso(cnf, name, value);
162
163
0
    if (!md) {
164
0
        if (!(flags & CONF_MFLAGS_SILENT)) {
165
0
            CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
166
0
            ERR_add_error_data(2, "module=", name);
167
0
        }
168
0
        return -1;
169
0
    }
170
171
0
    ret = module_init(md, name, value, cnf);
172
173
0
    if (ret <= 0) {
174
0
        if (!(flags & CONF_MFLAGS_SILENT)) {
175
0
            char rcode[DECIMAL_SIZE(ret) + 1];
176
177
0
            CONFerr(CONF_F_MODULE_RUN, CONF_R_MODULE_INITIALIZATION_ERROR);
178
0
            BIO_snprintf(rcode, sizeof(rcode), "%-8d", ret);
179
0
            ERR_add_error_data(6, "module=", name, ", value=", value,
180
0
                               ", retcode=", rcode);
181
0
        }
182
0
    }
183
184
0
    return ret;
185
0
}
186
187
/* Load a module from a DSO */
188
static CONF_MODULE *module_load_dso(const CONF *cnf,
189
                                    const char *name, const char *value)
190
0
{
191
0
    DSO *dso = NULL;
192
0
    conf_init_func *ifunc;
193
0
    conf_finish_func *ffunc;
194
0
    const char *path = NULL;
195
0
    int errcode = 0;
196
0
    CONF_MODULE *md;
197
    /* Look for alternative path in module section */
198
0
    path = NCONF_get_string(cnf, value, "path");
199
0
    if (!path) {
200
0
        ERR_clear_error();
201
0
        path = name;
202
0
    }
203
0
    dso = DSO_load(NULL, path, NULL, 0);
204
0
    if (!dso) {
205
0
        errcode = CONF_R_ERROR_LOADING_DSO;
206
0
        goto err;
207
0
    }
208
0
    ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
209
0
    if (!ifunc) {
210
0
        errcode = CONF_R_MISSING_INIT_FUNCTION;
211
0
        goto err;
212
0
    }
213
0
    ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
214
    /* All OK, add module */
215
0
    md = module_add(dso, name, ifunc, ffunc);
216
217
0
    if (!md)
218
0
        goto err;
219
220
0
    return md;
221
222
0
 err:
223
0
    DSO_free(dso);
224
0
    CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
225
0
    ERR_add_error_data(4, "module=", name, ", path=", path);
226
0
    return NULL;
227
0
}
228
229
/* add module to list */
230
static CONF_MODULE *module_add(DSO *dso, const char *name,
231
                               conf_init_func *ifunc, conf_finish_func *ffunc)
232
0
{
233
0
    CONF_MODULE *tmod = NULL;
234
0
    if (supported_modules == NULL)
235
0
        supported_modules = sk_CONF_MODULE_new_null();
236
0
    if (supported_modules == NULL)
237
0
        return NULL;
238
0
    if ((tmod = OPENSSL_zalloc(sizeof(*tmod))) == NULL) {
239
0
        CONFerr(CONF_F_MODULE_ADD, ERR_R_MALLOC_FAILURE);
240
0
        return NULL;
241
0
    }
242
243
0
    tmod->dso = dso;
244
0
    tmod->name = OPENSSL_strdup(name);
245
0
    tmod->init = ifunc;
246
0
    tmod->finish = ffunc;
247
0
    if (tmod->name == NULL) {
248
0
        OPENSSL_free(tmod);
249
0
        return NULL;
250
0
    }
251
252
0
    if (!sk_CONF_MODULE_push(supported_modules, tmod)) {
253
0
        OPENSSL_free(tmod->name);
254
0
        OPENSSL_free(tmod);
255
0
        return NULL;
256
0
    }
257
258
0
    return tmod;
259
0
}
260
261
/*
262
 * Find a module from the list. We allow module names of the form
263
 * modname.XXXX to just search for modname to allow the same module to be
264
 * initialized more than once.
265
 */
266
267
static CONF_MODULE *module_find(const char *name)
268
0
{
269
0
    CONF_MODULE *tmod;
270
0
    int i, nchar;
271
0
    char *p;
272
0
    p = strrchr(name, '.');
273
274
0
    if (p)
275
0
        nchar = p - name;
276
0
    else
277
0
        nchar = strlen(name);
278
279
0
    for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++) {
280
0
        tmod = sk_CONF_MODULE_value(supported_modules, i);
281
0
        if (strncmp(tmod->name, name, nchar) == 0)
282
0
            return tmod;
283
0
    }
284
285
0
    return NULL;
286
287
0
}
288
289
/* initialize a module */
290
static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
291
                       const CONF *cnf)
292
0
{
293
0
    int ret = 1;
294
0
    int init_called = 0;
295
0
    CONF_IMODULE *imod = NULL;
296
297
    /* Otherwise add initialized module to list */
298
0
    imod = OPENSSL_malloc(sizeof(*imod));
299
0
    if (imod == NULL)
300
0
        goto err;
301
302
0
    imod->pmod = pmod;
303
0
    imod->name = OPENSSL_strdup(name);
304
0
    imod->value = OPENSSL_strdup(value);
305
0
    imod->usr_data = NULL;
306
307
0
    if (!imod->name || !imod->value)
308
0
        goto memerr;
309
310
    /* Try to initialize module */
311
0
    if (pmod->init) {
312
0
        ret = pmod->init(imod, cnf);
313
0
        init_called = 1;
314
        /* Error occurred, exit */
315
0
        if (ret <= 0)
316
0
            goto err;
317
0
    }
318
319
0
    if (initialized_modules == NULL) {
320
0
        initialized_modules = sk_CONF_IMODULE_new_null();
321
0
        if (!initialized_modules) {
322
0
            CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
323
0
            goto err;
324
0
        }
325
0
    }
326
327
0
    if (!sk_CONF_IMODULE_push(initialized_modules, imod)) {
328
0
        CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
329
0
        goto err;
330
0
    }
331
332
0
    pmod->links++;
333
334
0
    return ret;
335
336
0
 err:
337
338
    /* We've started the module so we'd better finish it */
339
0
    if (pmod->finish && init_called)
340
0
        pmod->finish(imod);
341
342
0
 memerr:
343
0
    if (imod) {
344
0
        OPENSSL_free(imod->name);
345
0
        OPENSSL_free(imod->value);
346
0
        OPENSSL_free(imod);
347
0
    }
348
349
0
    return -1;
350
351
0
}
352
353
/*
354
 * Unload any dynamic modules that have a link count of zero: i.e. have no
355
 * active initialized modules. If 'all' is set then all modules are unloaded
356
 * including static ones.
357
 */
358
359
void CONF_modules_unload(int all)
360
2
{
361
2
    int i;
362
2
    CONF_MODULE *md;
363
2
    CONF_modules_finish();
364
    /* unload modules in reverse order */
365
2
    for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--) {
366
0
        md = sk_CONF_MODULE_value(supported_modules, i);
367
        /* If static or in use and 'all' not set ignore it */
368
0
        if (((md->links > 0) || !md->dso) && !all)
369
0
            continue;
370
        /* Since we're working in reverse this is OK */
371
0
        (void)sk_CONF_MODULE_delete(supported_modules, i);
372
0
        module_free(md);
373
0
    }
374
2
    if (sk_CONF_MODULE_num(supported_modules) == 0) {
375
0
        sk_CONF_MODULE_free(supported_modules);
376
0
        supported_modules = NULL;
377
0
    }
378
2
}
379
380
/* unload a single module */
381
static void module_free(CONF_MODULE *md)
382
0
{
383
0
    DSO_free(md->dso);
384
0
    OPENSSL_free(md->name);
385
0
    OPENSSL_free(md);
386
0
}
387
388
/* finish and free up all modules instances */
389
390
void CONF_modules_finish(void)
391
4
{
392
4
    CONF_IMODULE *imod;
393
4
    while (sk_CONF_IMODULE_num(initialized_modules) > 0) {
394
0
        imod = sk_CONF_IMODULE_pop(initialized_modules);
395
0
        module_finish(imod);
396
0
    }
397
4
    sk_CONF_IMODULE_free(initialized_modules);
398
4
    initialized_modules = NULL;
399
4
}
400
401
/* finish a module instance */
402
403
static void module_finish(CONF_IMODULE *imod)
404
0
{
405
0
    if (!imod)
406
0
        return;
407
0
    if (imod->pmod->finish)
408
0
        imod->pmod->finish(imod);
409
0
    imod->pmod->links--;
410
0
    OPENSSL_free(imod->name);
411
0
    OPENSSL_free(imod->value);
412
0
    OPENSSL_free(imod);
413
0
}
414
415
/* Add a static module to OpenSSL */
416
417
int CONF_module_add(const char *name, conf_init_func *ifunc,
418
                    conf_finish_func *ffunc)
419
0
{
420
0
    if (module_add(NULL, name, ifunc, ffunc))
421
0
        return 1;
422
0
    else
423
0
        return 0;
424
0
}
425
426
void conf_modules_free_int(void)
427
2
{
428
2
    CONF_modules_finish();
429
2
    CONF_modules_unload(1);
430
2
}
431
432
/* Utility functions */
433
434
const char *CONF_imodule_get_name(const CONF_IMODULE *md)
435
0
{
436
0
    return md->name;
437
0
}
438
439
const char *CONF_imodule_get_value(const CONF_IMODULE *md)
440
0
{
441
0
    return md->value;
442
0
}
443
444
void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
445
0
{
446
0
    return md->usr_data;
447
0
}
448
449
void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
450
0
{
451
0
    md->usr_data = usr_data;
452
0
}
453
454
CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
455
0
{
456
0
    return md->pmod;
457
0
}
458
459
unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
460
0
{
461
0
    return md->flags;
462
0
}
463
464
void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
465
0
{
466
0
    md->flags = flags;
467
0
}
468
469
void *CONF_module_get_usr_data(CONF_MODULE *pmod)
470
0
{
471
0
    return pmod->usr_data;
472
0
}
473
474
void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
475
0
{
476
0
    pmod->usr_data = usr_data;
477
0
}
478
479
/* Return default config file name */
480
481
char *CONF_get1_default_config_file(void)
482
0
{
483
0
    char *file, *sep = "";
484
0
    int len;
485
486
0
    if ((file = ossl_safe_getenv("OPENSSL_CONF")) != NULL)
487
0
        return OPENSSL_strdup(file);
488
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
496
0
    file = OPENSSL_malloc(len + 1);
497
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
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
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
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
}