Coverage Report

Created: 2025-06-13 06:56

/src/openssl/crypto/conf/conf_lib.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2000-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/e_os.h"
11
#include <stdio.h>
12
#include <string.h>
13
#include "internal/conf.h"
14
#include "crypto/ctype.h"
15
#include <openssl/crypto.h>
16
#include <openssl/err.h>
17
#include <openssl/conf.h>
18
#include <openssl/conf_api.h>
19
#include "conf_local.h"
20
#include <openssl/lhash.h>
21
22
static CONF_METHOD *default_CONF_method = NULL;
23
24
/* Init a 'CONF' structure from an old LHASH */
25
26
void CONF_set_nconf(CONF *conf, LHASH_OF(CONF_VALUE) *hash)
27
0
{
28
0
    if (default_CONF_method == NULL)
29
0
        default_CONF_method = NCONF_default();
30
31
0
    default_CONF_method->init(conf);
32
0
    conf->data = hash;
33
0
}
34
35
/*
36
 * The following section contains the "CONF classic" functions, rewritten in
37
 * terms of the new CONF interface.
38
 */
39
40
int CONF_set_default_method(CONF_METHOD *meth)
41
0
{
42
0
    default_CONF_method = meth;
43
0
    return 1;
44
0
}
45
46
LHASH_OF(CONF_VALUE) *CONF_load(LHASH_OF(CONF_VALUE) *conf, const char *file,
47
                                long *eline)
48
0
{
49
0
    LHASH_OF(CONF_VALUE) *ltmp;
50
0
    BIO *in = NULL;
51
52
#ifdef OPENSSL_SYS_VMS
53
    in = BIO_new_file(file, "r");
54
#else
55
0
    in = BIO_new_file(file, "rb");
56
0
#endif
57
0
    if (in == NULL) {
58
0
        ERR_raise(ERR_LIB_CONF, ERR_R_SYS_LIB);
59
0
        return NULL;
60
0
    }
61
62
0
    ltmp = CONF_load_bio(conf, in, eline);
63
0
    BIO_free(in);
64
65
0
    return ltmp;
66
0
}
67
68
#ifndef OPENSSL_NO_STDIO
69
LHASH_OF(CONF_VALUE) *CONF_load_fp(LHASH_OF(CONF_VALUE) *conf, FILE *fp,
70
                                   long *eline)
71
0
{
72
0
    BIO *btmp;
73
0
    LHASH_OF(CONF_VALUE) *ltmp;
74
0
    if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
75
0
        ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
76
0
        return NULL;
77
0
    }
78
0
    ltmp = CONF_load_bio(conf, btmp, eline);
79
0
    BIO_free(btmp);
80
0
    return ltmp;
81
0
}
82
#endif
83
84
LHASH_OF(CONF_VALUE) *CONF_load_bio(LHASH_OF(CONF_VALUE) *conf, BIO *bp,
85
                                    long *eline)
86
0
{
87
0
    CONF ctmp;
88
0
    int ret;
89
90
0
    CONF_set_nconf(&ctmp, conf);
91
92
0
    ret = NCONF_load_bio(&ctmp, bp, eline);
93
0
    if (ret)
94
0
        return ctmp.data;
95
0
    return NULL;
96
0
}
97
98
STACK_OF(CONF_VALUE) *CONF_get_section(LHASH_OF(CONF_VALUE) *conf,
99
                                       const char *section)
100
0
{
101
0
    if (conf == NULL) {
102
0
        return NULL;
103
0
    } else {
104
0
        CONF ctmp;
105
106
0
        CONF_set_nconf(&ctmp, conf);
107
0
        return NCONF_get_section(&ctmp, section);
108
0
    }
109
0
}
110
111
char *CONF_get_string(LHASH_OF(CONF_VALUE) *conf, const char *group,
112
                      const char *name)
113
0
{
114
0
    if (conf == NULL) {
115
0
        return NCONF_get_string(NULL, group, name);
116
0
    } else {
117
0
        CONF ctmp;
118
119
0
        CONF_set_nconf(&ctmp, conf);
120
0
        return NCONF_get_string(&ctmp, group, name);
121
0
    }
122
0
}
123
124
long CONF_get_number(LHASH_OF(CONF_VALUE) *conf, const char *group,
125
                     const char *name)
126
0
{
127
0
    int status;
128
0
    long result = 0;
129
130
0
    ERR_set_mark();
131
0
    if (conf == NULL) {
132
0
        status = NCONF_get_number_e(NULL, group, name, &result);
133
0
    } else {
134
0
        CONF ctmp;
135
136
0
        CONF_set_nconf(&ctmp, conf);
137
0
        status = NCONF_get_number_e(&ctmp, group, name, &result);
138
0
    }
139
0
    ERR_pop_to_mark();
140
0
    return status == 0 ? 0L : result;
141
0
}
142
143
void CONF_free(LHASH_OF(CONF_VALUE) *conf)
144
0
{
145
0
    CONF ctmp;
146
0
    CONF_set_nconf(&ctmp, conf);
147
0
    NCONF_free_data(&ctmp);
148
0
}
149
150
#ifndef OPENSSL_NO_STDIO
151
int CONF_dump_fp(LHASH_OF(CONF_VALUE) *conf, FILE *out)
152
0
{
153
0
    BIO *btmp;
154
0
    int ret;
155
156
0
    if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
157
0
        ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
158
0
        return 0;
159
0
    }
160
0
    ret = CONF_dump_bio(conf, btmp);
161
0
    BIO_free(btmp);
162
0
    return ret;
163
0
}
164
#endif
165
166
int CONF_dump_bio(LHASH_OF(CONF_VALUE) *conf, BIO *out)
167
0
{
168
0
    CONF ctmp;
169
170
0
    CONF_set_nconf(&ctmp, conf);
171
0
    return NCONF_dump_bio(&ctmp, out);
172
0
}
173
174
/*
175
 * The following section contains the "New CONF" functions.  They are
176
 * completely centralised around a new CONF structure that may contain
177
 * basically anything, but at least a method pointer and a table of data.
178
 * These functions are also written in terms of the bridge functions used by
179
 * the "CONF classic" functions, for consistency.
180
 */
181
182
CONF *NCONF_new_ex(OSSL_LIB_CTX *libctx, CONF_METHOD *meth)
183
0
{
184
0
    CONF *ret;
185
186
0
    if (meth == NULL)
187
0
        meth = NCONF_default();
188
189
0
    ret = meth->create(meth);
190
0
    if (ret == NULL) {
191
0
        ERR_raise(ERR_LIB_CONF, ERR_R_CONF_LIB);
192
0
        return NULL;
193
0
    }
194
0
    ret->libctx = libctx;
195
196
0
    return ret;
197
0
}
198
199
CONF *NCONF_new(CONF_METHOD *meth)
200
0
{
201
0
    return NCONF_new_ex(NULL, meth);
202
0
}
203
204
void NCONF_free(CONF *conf)
205
0
{
206
0
    if (conf == NULL)
207
0
        return;
208
0
    conf->meth->destroy(conf);
209
0
}
210
211
void NCONF_free_data(CONF *conf)
212
0
{
213
0
    if (conf == NULL)
214
0
        return;
215
0
    conf->meth->destroy_data(conf);
216
0
}
217
218
OSSL_LIB_CTX *NCONF_get0_libctx(const CONF *conf)
219
0
{
220
0
    return conf->libctx;
221
0
}
222
223
typedef STACK_OF(OPENSSL_CSTRING) SECTION_NAMES;
224
225
IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, SECTION_NAMES);
226
227
static void collect_section_name(const CONF_VALUE *v, SECTION_NAMES *names)
228
0
{
229
    /* A section is a CONF_VALUE with name == NULL */
230
0
    if (v->name == NULL)
231
        /* A failure to push cannot be handled so we ignore the result. */
232
0
        (void)sk_OPENSSL_CSTRING_push(names, v->section);
233
0
}
234
235
static int section_name_cmp(OPENSSL_CSTRING const *a, OPENSSL_CSTRING const *b)
236
0
{
237
0
    return strcmp(*a, *b);
238
0
}
239
240
STACK_OF(OPENSSL_CSTRING) *NCONF_get_section_names(const CONF *cnf)
241
0
{
242
0
    SECTION_NAMES *names;
243
244
0
    if ((names = sk_OPENSSL_CSTRING_new(section_name_cmp)) == NULL)
245
0
        return NULL;
246
0
    lh_CONF_VALUE_doall_SECTION_NAMES(cnf->data, collect_section_name, names);
247
0
    sk_OPENSSL_CSTRING_sort(names);
248
0
    return names;
249
0
}
250
251
int NCONF_load(CONF *conf, const char *file, long *eline)
252
0
{
253
0
    if (conf == NULL) {
254
0
        ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF);
255
0
        return 0;
256
0
    }
257
258
0
    return conf->meth->load(conf, file, eline);
259
0
}
260
261
#ifndef OPENSSL_NO_STDIO
262
int NCONF_load_fp(CONF *conf, FILE *fp, long *eline)
263
0
{
264
0
    BIO *btmp;
265
0
    int ret;
266
0
    if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
267
0
        ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
268
0
        return 0;
269
0
    }
270
0
    ret = NCONF_load_bio(conf, btmp, eline);
271
0
    BIO_free(btmp);
272
0
    return ret;
273
0
}
274
#endif
275
276
int NCONF_load_bio(CONF *conf, BIO *bp, long *eline)
277
0
{
278
0
    if (conf == NULL) {
279
0
        ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF);
280
0
        return 0;
281
0
    }
282
283
0
    return conf->meth->load_bio(conf, bp, eline);
284
0
}
285
286
STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section)
287
0
{
288
0
    if (conf == NULL) {
289
0
        ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF);
290
0
        return NULL;
291
0
    }
292
293
0
    if (section == NULL) {
294
0
        ERR_raise(ERR_LIB_CONF, CONF_R_NO_SECTION);
295
0
        return NULL;
296
0
    }
297
298
0
    return _CONF_get_section_values(conf, section);
299
0
}
300
301
char *NCONF_get_string(const CONF *conf, const char *group, const char *name)
302
0
{
303
0
    char *s = _CONF_get_string(conf, group, name);
304
305
    /*
306
     * Since we may get a value from an environment variable even if conf is
307
     * NULL, let's check the value first
308
     */
309
0
    if (s)
310
0
        return s;
311
312
0
    if (conf == NULL) {
313
0
        ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE);
314
0
        return NULL;
315
0
    }
316
0
    ERR_raise_data(ERR_LIB_CONF, CONF_R_NO_VALUE,
317
0
                   "group=%s name=%s", group, name);
318
0
    return NULL;
319
0
}
320
321
static int default_is_number(const CONF *conf, char c)
322
0
{
323
0
    return ossl_isdigit(c);
324
0
}
325
326
static int default_to_int(const CONF *conf, char c)
327
0
{
328
0
    return (int)(c - '0');
329
0
}
330
331
int NCONF_get_number_e(const CONF *conf, const char *group, const char *name,
332
                       long *result)
333
0
{
334
0
    char *str;
335
0
    long res;
336
0
    int (*is_number)(const CONF *, char) = &default_is_number;
337
0
    int (*to_int)(const CONF *, char) = &default_to_int;
338
339
0
    if (result == NULL) {
340
0
        ERR_raise(ERR_LIB_CONF, ERR_R_PASSED_NULL_PARAMETER);
341
0
        return 0;
342
0
    }
343
344
0
    str = NCONF_get_string(conf, group, name);
345
346
0
    if (str == NULL)
347
0
        return 0;
348
349
0
    if (conf != NULL) {
350
0
        if (conf->meth->is_number != NULL)
351
0
            is_number = conf->meth->is_number;
352
0
        if (conf->meth->to_int != NULL)
353
0
            to_int = conf->meth->to_int;
354
0
    }
355
0
    for (res = 0; is_number(conf, *str); str++) {
356
0
        const int d = to_int(conf, *str);
357
358
0
        if (res > (LONG_MAX - d) / 10L) {
359
0
            ERR_raise(ERR_LIB_CONF, CONF_R_NUMBER_TOO_LARGE);
360
0
            return 0;
361
0
        }
362
0
        res = res * 10 + d;
363
0
    }
364
365
0
    *result = res;
366
0
    return 1;
367
0
}
368
369
long _CONF_get_number(const CONF *conf, const char *section,
370
                      const char *name)
371
0
{
372
0
    int status;
373
0
    long result = 0;
374
375
0
    ERR_set_mark();
376
0
    status = NCONF_get_number_e(conf, section, name, &result);
377
0
    ERR_pop_to_mark();
378
0
    return status == 0 ? 0L : result;
379
0
}
380
381
#ifndef OPENSSL_NO_STDIO
382
int NCONF_dump_fp(const CONF *conf, FILE *out)
383
0
{
384
0
    BIO *btmp;
385
0
    int ret;
386
0
    if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
387
0
        ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
388
0
        return 0;
389
0
    }
390
0
    ret = NCONF_dump_bio(conf, btmp);
391
0
    BIO_free(btmp);
392
0
    return ret;
393
0
}
394
#endif
395
396
int NCONF_dump_bio(const CONF *conf, BIO *out)
397
0
{
398
0
    if (conf == NULL) {
399
0
        ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF);
400
0
        return 0;
401
0
    }
402
403
0
    return conf->meth->dump(conf, out);
404
0
}
405
406
/*
407
 * These routines call the C malloc/free, to avoid intermixing with
408
 * OpenSSL function pointers before the library is initialized.
409
 */
410
OPENSSL_INIT_SETTINGS *OPENSSL_INIT_new(void)
411
0
{
412
0
    OPENSSL_INIT_SETTINGS *ret = malloc(sizeof(*ret));
413
414
0
    if (ret == NULL)
415
0
        return NULL;
416
417
0
    memset(ret, 0, sizeof(*ret));
418
0
    ret->flags = DEFAULT_CONF_MFLAGS;
419
420
0
    return ret;
421
0
}
422
423
424
#ifndef OPENSSL_NO_STDIO
425
/*
426
 * If CRYPTO_set_mem_functions is called after this, then
427
 * memory allocation and deallocation in this function can
428
 * become disjointed. Avoid this by always using standard
429
 * strdup & free instead of OPENSSL_strdup & OPENSSL_free.
430
 */
431
int OPENSSL_INIT_set_config_filename(OPENSSL_INIT_SETTINGS *settings,
432
                                     const char *filename)
433
0
{
434
0
    char *newfilename = NULL;
435
436
0
    if (filename != NULL) {
437
0
        newfilename = strdup(filename);
438
0
        if (newfilename == NULL)
439
0
            return 0;
440
0
    }
441
442
0
    free(settings->filename);
443
0
    settings->filename = newfilename;
444
445
0
    return 1;
446
0
}
447
448
void OPENSSL_INIT_set_config_file_flags(OPENSSL_INIT_SETTINGS *settings,
449
                                        unsigned long flags)
450
0
{
451
0
    settings->flags = flags;
452
0
}
453
454
/*
455
 * If CRYPTO_set_mem_functions is called after this, then
456
 * memory allocation and deallocation in this function can
457
 * become disjointed. Avoid this by always using standard
458
 * strdup & free instead of OPENSSL_strdup & OPENSSL_free.
459
 */
460
int OPENSSL_INIT_set_config_appname(OPENSSL_INIT_SETTINGS *settings,
461
                                    const char *appname)
462
0
{
463
0
    char *newappname = NULL;
464
465
0
    if (appname != NULL) {
466
0
        newappname = strdup(appname);
467
0
        if (newappname == NULL)
468
0
            return 0;
469
0
    }
470
471
0
    free(settings->appname);
472
0
    settings->appname = newappname;
473
474
0
    return 1;
475
0
}
476
#endif
477
478
void OPENSSL_INIT_free(OPENSSL_INIT_SETTINGS *settings)
479
0
{
480
0
    if (settings == NULL)
481
0
        return;
482
483
0
    free(settings->filename);
484
0
    free(settings->appname);
485
0
    free(settings);
486
0
}