Coverage Report

Created: 2025-12-31 06:58

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