Coverage Report

Created: 2025-12-10 06:24

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