Coverage Report

Created: 2024-02-25 06:25

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