Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/conf/conf_mod.c
Line
Count
Source
1
/*
2
 * Copyright 2002-2024 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
220
{
90
220
    ossl_rcu_lock_free(module_list_lock);
91
220
    module_list_lock = NULL;
92
93
220
    sk_CONF_MODULE_free(supported_modules);
94
220
    supported_modules = NULL;
95
96
220
    sk_CONF_IMODULE_free(initialized_modules);
97
220
    initialized_modules = NULL;
98
220
}
99
100
DEFINE_RUN_ONCE_STATIC(do_init_module_list_lock)
101
220
{
102
220
    module_list_lock = ossl_rcu_lock_new(1, NULL);
103
220
    if (module_list_lock == NULL) {
104
0
        ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB);
105
0
        return 0;
106
0
    }
107
108
220
    return 1;
109
220
}
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
0
}
179
180
int CONF_modules_load_file_ex(OSSL_LIB_CTX *libctx, const char *filename,
181
    const char *appname, unsigned long flags)
182
185
{
183
185
    char *file = NULL;
184
185
    CONF *conf = NULL;
185
185
    int ret = 0, diagnostics = 0;
186
187
185
    ERR_set_mark();
188
189
185
    if (filename == NULL) {
190
185
        file = CONF_get1_default_config_file();
191
185
        if (file == NULL)
192
0
            goto err;
193
185
        if (*file == '\0') {
194
            /* Do not try to load an empty file name but do not error out */
195
0
            ret = 1;
196
0
            goto err;
197
0
        }
198
185
    } else {
199
0
        file = (char *)filename;
200
0
    }
201
202
185
    conf = NCONF_new_ex(libctx, NULL);
203
185
    if (conf == NULL)
204
0
        goto err;
205
206
185
    if (NCONF_load(conf, file, NULL) <= 0) {
207
185
        if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) && (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE)) {
208
185
            ret = 1;
209
185
        }
210
185
        goto err;
211
185
    }
212
213
0
    ret = CONF_modules_load(conf, appname, flags);
214
0
    diagnostics = conf_diagnostics(conf);
215
216
185
err:
217
185
    if (filename == NULL)
218
185
        OPENSSL_free(file);
219
185
    NCONF_free(conf);
220
221
185
    if ((flags & CONF_MFLAGS_IGNORE_RETURN_CODES) != 0 && !diagnostics)
222
185
        ret = 1;
223
224
185
    if (ret > 0)
225
185
        ERR_pop_to_mark();
226
0
    else
227
0
        ERR_clear_last_mark();
228
229
185
    return ret;
230
0
}
231
232
int CONF_modules_load_file(const char *filename,
233
    const char *appname, unsigned long flags)
234
0
{
235
0
    return CONF_modules_load_file_ex(NULL, filename, appname, flags);
236
0
}
237
238
DEFINE_RUN_ONCE_STATIC(do_load_builtin_modules)
239
0
{
240
0
    OPENSSL_load_builtin_modules();
241
0
#ifndef OPENSSL_NO_ENGINE
242
    /* Need to load ENGINEs */
243
0
    ENGINE_load_builtin_engines();
244
0
#endif
245
0
    return 1;
246
0
}
247
248
static int module_run(const CONF *cnf, const char *name, const char *value,
249
    unsigned long flags)
250
0
{
251
0
    CONF_MODULE *md;
252
0
    int ret;
253
254
0
    if (!RUN_ONCE(&load_builtin_modules, do_load_builtin_modules))
255
0
        return -1;
256
257
0
    md = module_find(name);
258
259
    /* Module not found: try to load DSO */
260
0
    if (!md && !(flags & CONF_MFLAGS_NO_DSO))
261
0
        md = module_load_dso(cnf, name, value);
262
263
0
    if (!md) {
264
0
        if (!(flags & CONF_MFLAGS_SILENT)) {
265
0
            ERR_raise_data(ERR_LIB_CONF, CONF_R_UNKNOWN_MODULE_NAME,
266
0
                "module=%s", name);
267
0
        }
268
0
        return -1;
269
0
    }
270
271
0
    ret = module_init(md, name, value, cnf);
272
273
0
    if (ret <= 0) {
274
0
        if (!(flags & CONF_MFLAGS_SILENT))
275
0
            ERR_raise_data(ERR_LIB_CONF, CONF_R_MODULE_INITIALIZATION_ERROR,
276
0
                "module=%s, value=%s retcode=%-8d",
277
0
                name, value, ret);
278
0
    }
279
280
0
    return ret;
281
0
}
282
283
/* Load a module from a DSO */
284
static CONF_MODULE *module_load_dso(const CONF *cnf,
285
    const char *name, const char *value)
286
0
{
287
0
    DSO *dso = NULL;
288
0
    conf_init_func *ifunc;
289
0
    conf_finish_func *ffunc;
290
0
    const char *path = NULL;
291
0
    int errcode = 0;
292
0
    CONF_MODULE *md;
293
294
    /* Look for alternative path in module section */
295
0
    path = _CONF_get_string(cnf, value, "path");
296
0
    if (path == NULL) {
297
0
        path = name;
298
0
    }
299
0
    dso = DSO_load(NULL, path, NULL, 0);
300
0
    if (dso == NULL) {
301
0
        errcode = CONF_R_ERROR_LOADING_DSO;
302
0
        goto err;
303
0
    }
304
0
    ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
305
0
    if (ifunc == NULL) {
306
0
        errcode = CONF_R_MISSING_INIT_FUNCTION;
307
0
        goto err;
308
0
    }
309
0
    ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
310
    /* All OK, add module */
311
0
    md = module_add(dso, name, ifunc, ffunc);
312
313
0
    if (md == NULL)
314
0
        goto err;
315
316
0
    return md;
317
318
0
err:
319
0
    DSO_free(dso);
320
0
    ERR_raise_data(ERR_LIB_CONF, errcode, "module=%s, path=%s", name, path);
321
0
    return NULL;
322
0
}
323
324
/* add module to list */
325
static CONF_MODULE *module_add(DSO *dso, const char *name,
326
    conf_init_func *ifunc, conf_finish_func *ffunc)
327
0
{
328
0
    CONF_MODULE *tmod = NULL;
329
0
    STACK_OF(CONF_MODULE) *old_modules;
330
0
    STACK_OF(CONF_MODULE) *new_modules;
331
332
0
    if (!RUN_ONCE(&init_module_list_lock, do_init_module_list_lock))
333
0
        return NULL;
334
335
0
    ossl_rcu_write_lock(module_list_lock);
336
337
0
    old_modules = ossl_rcu_deref(&supported_modules);
338
339
0
    if (old_modules == NULL)
340
0
        new_modules = sk_CONF_MODULE_new_null();
341
0
    else
342
0
        new_modules = sk_CONF_MODULE_dup(old_modules);
343
344
0
    if (new_modules == NULL)
345
0
        goto err;
346
347
0
    if ((tmod = OPENSSL_zalloc(sizeof(*tmod))) == NULL)
348
0
        goto err;
349
350
0
    tmod->dso = dso;
351
0
    tmod->name = OPENSSL_strdup(name);
352
0
    tmod->init = ifunc;
353
0
    tmod->finish = ffunc;
354
0
    if (tmod->name == NULL)
355
0
        goto err;
356
357
0
    if (!sk_CONF_MODULE_push(new_modules, tmod))
358
0
        goto err;
359
360
0
    ossl_rcu_assign_ptr(&supported_modules, &new_modules);
361
0
    ossl_rcu_write_unlock(module_list_lock);
362
0
    ossl_synchronize_rcu(module_list_lock);
363
364
0
    sk_CONF_MODULE_free(old_modules);
365
0
    return tmod;
366
367
0
err:
368
0
    ossl_rcu_write_unlock(module_list_lock);
369
0
    sk_CONF_MODULE_free(new_modules);
370
0
    if (tmod != NULL) {
371
0
        OPENSSL_free(tmod->name);
372
0
        OPENSSL_free(tmod);
373
0
    }
374
0
    sk_CONF_MODULE_free(new_modules);
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
        sk_CONF_IMODULE_free(new_modules);
469
0
        ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB);
470
0
        goto err;
471
0
    }
472
473
0
    pmod->links++;
474
475
0
    ossl_rcu_assign_ptr(&initialized_modules, &new_modules);
476
0
    ossl_rcu_write_unlock(module_list_lock);
477
0
    ossl_synchronize_rcu(module_list_lock);
478
0
    sk_CONF_IMODULE_free(old_modules);
479
0
    return ret;
480
481
0
err:
482
483
    /* We've started the module so we'd better finish it */
484
0
    if (pmod->finish && init_called)
485
0
        pmod->finish(imod);
486
487
0
memerr:
488
0
    if (imod) {
489
0
        OPENSSL_free(imod->name);
490
0
        OPENSSL_free(imod->value);
491
0
        OPENSSL_free(imod);
492
0
    }
493
494
0
    return -1;
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
196
{
505
196
    int i;
506
196
    CONF_MODULE *md;
507
196
    STACK_OF(CONF_MODULE) *old_modules;
508
196
    STACK_OF(CONF_MODULE) *new_modules;
509
196
    STACK_OF(CONF_MODULE) *to_delete;
510
511
196
    if (!conf_modules_finish_int()) /* also inits module list lock */
512
0
        return;
513
514
196
    ossl_rcu_write_lock(module_list_lock);
515
516
196
    old_modules = ossl_rcu_deref(&supported_modules);
517
196
    new_modules = sk_CONF_MODULE_dup(old_modules);
518
519
196
    if (new_modules == NULL) {
520
0
        ossl_rcu_write_unlock(module_list_lock);
521
0
        return;
522
0
    }
523
524
196
    to_delete = sk_CONF_MODULE_new_null();
525
526
    /* unload modules in reverse order */
527
196
    for (i = sk_CONF_MODULE_num(new_modules) - 1; i >= 0; i--) {
528
0
        md = sk_CONF_MODULE_value(new_modules, i);
529
        /* If static or in use and 'all' not set ignore it */
530
0
        if (((md->links > 0) || !md->dso) && !all)
531
0
            continue;
532
        /* Since we're working in reverse this is OK */
533
0
        (void)sk_CONF_MODULE_delete(new_modules, i);
534
0
        sk_CONF_MODULE_push(to_delete, md);
535
0
    }
536
537
196
    if (sk_CONF_MODULE_num(new_modules) == 0) {
538
196
        sk_CONF_MODULE_free(new_modules);
539
196
        new_modules = NULL;
540
196
    }
541
542
196
    ossl_rcu_assign_ptr(&supported_modules, &new_modules);
543
196
    ossl_rcu_write_unlock(module_list_lock);
544
196
    ossl_synchronize_rcu(module_list_lock);
545
196
    sk_CONF_MODULE_free(old_modules);
546
196
    sk_CONF_MODULE_pop_free(to_delete, module_free);
547
196
}
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
196
{
561
196
    CONF_IMODULE *imod;
562
196
    STACK_OF(CONF_IMODULE) *old_modules;
563
196
    STACK_OF(CONF_IMODULE) *new_modules = NULL;
564
565
196
    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
196
    if (module_list_lock == NULL)
570
0
        return 0;
571
572
196
    ossl_rcu_write_lock(module_list_lock);
573
196
    old_modules = ossl_rcu_deref(&initialized_modules);
574
196
    ossl_rcu_assign_ptr(&initialized_modules, &new_modules);
575
196
    ossl_rcu_write_unlock(module_list_lock);
576
196
    ossl_synchronize_rcu(module_list_lock);
577
578
196
    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
196
    sk_CONF_IMODULE_free(old_modules);
583
584
196
    return 1;
585
196
}
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
220
{
619
220
    CONF_modules_unload(1); /* calls CONF_modules_finish */
620
220
    module_lists_free();
621
220
}
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
42
{
673
42
    const char *t;
674
42
    char *file, *sep = "";
675
42
    size_t size;
676
677
42
    if ((file = ossl_safe_getenv("OPENSSL_CONF")) != NULL)
678
0
        return OPENSSL_strdup(file);
679
680
42
    t = X509_get_default_cert_area();
681
42
#ifndef OPENSSL_SYS_VMS
682
42
    sep = "/";
683
42
#endif
684
42
    size = strlen(t) + strlen(sep) + strlen(OPENSSL_CONF) + 1;
685
42
    file = OPENSSL_malloc(size);
686
687
42
    if (file == NULL)
688
0
        return NULL;
689
42
    BIO_snprintf(file, size, "%s%s%s", t, sep, OPENSSL_CONF);
690
691
42
    return file;
692
42
}
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
617k
{
705
617k
    int ret;
706
617k
    const char *lstart, *tmpend, *p;
707
708
617k
    if (list_ == NULL) {
709
0
        ERR_raise(ERR_LIB_CONF, CONF_R_LIST_CANNOT_BE_NULL);
710
0
        return 0;
711
0
    }
712
713
617k
    lstart = list_;
714
1.57M
    for (;;) {
715
1.57M
        if (nospc) {
716
1.85M
            while (*lstart && isspace((unsigned char)*lstart))
717
272k
                lstart++;
718
1.57M
        }
719
1.57M
        p = strchr(lstart, sep);
720
1.57M
        if (p == lstart || *lstart == '\0')
721
0
            ret = list_cb(NULL, 0, arg);
722
1.57M
        else {
723
1.57M
            if (p)
724
961k
                tmpend = p - 1;
725
617k
            else
726
617k
                tmpend = lstart + strlen(lstart) - 1;
727
1.57M
            if (nospc) {
728
1.57M
                while (isspace((unsigned char)*tmpend))
729
272k
                    tmpend--;
730
1.57M
            }
731
1.57M
            ret = list_cb(lstart, tmpend - lstart + 1, arg);
732
1.57M
        }
733
1.57M
        if (ret <= 0)
734
0
            return ret;
735
1.57M
        if (p == NULL)
736
617k
            return 1;
737
961k
        lstart = p + 1;
738
961k
    }
739
617k
}