Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/crypto/conf/conf_def.c
Line
Count
Source
1
/*
2
 * Copyright 1995-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
/* Part of the code in here was originally in conf.c, which is now removed */
11
12
#include <stdio.h>
13
#include <string.h>
14
#include "internal/e_os.h" /* struct stat */
15
#ifdef __TANDEM
16
#include <sys/types.h> /* needed for stat.h */
17
#include <sys/stat.h> /* struct stat */
18
#endif
19
#include "internal/cryptlib.h"
20
#include "internal/o_dir.h"
21
#include <openssl/lhash.h>
22
#include <openssl/conf.h>
23
#include <openssl/conf_api.h>
24
#include "conf_local.h"
25
#include "conf_def.h"
26
#include <openssl/buffer.h>
27
#include <openssl/err.h>
28
#ifndef OPENSSL_NO_POSIX_IO
29
#include <sys/stat.h>
30
#ifdef _WIN32
31
#define stat _stat
32
#endif
33
#endif
34
35
#ifndef S_ISDIR
36
#define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
37
#endif
38
39
/*
40
 * The maximum length we can grow a value to after variable expansion. 64k
41
 * should be more than enough for all reasonable uses.
42
 */
43
98.3k
#define MAX_CONF_VALUE_LENGTH 65536
44
45
static int is_keytype(const CONF *conf, char c, unsigned short type);
46
static char *eat_ws(CONF *conf, char *p);
47
static void trim_ws(CONF *conf, char *start);
48
static char *eat_alpha_numeric(CONF *conf, char *p);
49
static void clear_comments(CONF *conf, char *p);
50
static int str_copy(CONF *conf, char *section, char **to, char *from);
51
static char *scan_quote(CONF *conf, char *p);
52
static char *scan_dquote(CONF *conf, char *p);
53
1.95M
#define scan_esc(conf, p) (((IS_EOF((conf), (p)[1])) ? ((p) + 1) : ((p) + 2)))
54
#ifndef OPENSSL_NO_POSIX_IO
55
static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
56
    char **dirpath);
57
static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx);
58
#endif
59
60
static CONF *def_create(CONF_METHOD *meth);
61
static int def_init_default(CONF *conf);
62
#ifndef OPENSSL_NO_DEPRECATED_3_0
63
static int def_init_WIN32(CONF *conf);
64
#endif
65
static int def_destroy(CONF *conf);
66
static int def_destroy_data(CONF *conf);
67
static int def_load(CONF *conf, const char *name, long *eline);
68
static int def_load_bio(CONF *conf, BIO *bp, long *eline);
69
static int def_dump(const CONF *conf, BIO *bp);
70
static int def_is_number(const CONF *conf, char c);
71
static int def_to_int(const CONF *conf, char c);
72
73
static CONF_METHOD default_method = {
74
    "OpenSSL default",
75
    def_create,
76
    def_init_default,
77
    def_destroy,
78
    def_destroy_data,
79
    def_load_bio,
80
    def_dump,
81
    def_is_number,
82
    def_to_int,
83
    def_load
84
};
85
86
CONF_METHOD *NCONF_default(void)
87
41.2k
{
88
41.2k
    return &default_method;
89
41.2k
}
90
91
#ifndef OPENSSL_NO_DEPRECATED_3_0
92
static CONF_METHOD WIN32_method = {
93
    "WIN32",
94
    def_create,
95
    def_init_WIN32,
96
    def_destroy,
97
    def_destroy_data,
98
    def_load_bio,
99
    def_dump,
100
    def_is_number,
101
    def_to_int,
102
    def_load
103
};
104
105
CONF_METHOD *NCONF_WIN32(void)
106
0
{
107
0
    return &WIN32_method;
108
0
}
109
#endif
110
111
static CONF *def_create(CONF_METHOD *meth)
112
41.2k
{
113
41.2k
    CONF *ret;
114
115
41.2k
    ret = OPENSSL_malloc(sizeof(*ret));
116
41.2k
    if (ret != NULL)
117
41.2k
        if (meth->init(ret) == 0) {
118
0
            OPENSSL_free(ret);
119
0
            ret = NULL;
120
0
        }
121
41.2k
    return ret;
122
41.2k
}
123
124
static int def_init_default(CONF *conf)
125
50.2k
{
126
50.2k
    if (conf == NULL)
127
0
        return 0;
128
129
50.2k
    memset(conf, 0, sizeof(*conf));
130
50.2k
    conf->meth = &default_method;
131
50.2k
    conf->meth_data = (void *)CONF_type_default;
132
133
50.2k
    return 1;
134
50.2k
}
135
136
#ifndef OPENSSL_NO_DEPRECATED_3_0
137
static int def_init_WIN32(CONF *conf)
138
0
{
139
0
    if (conf == NULL)
140
0
        return 0;
141
142
0
    memset(conf, 0, sizeof(*conf));
143
0
    conf->meth = &WIN32_method;
144
0
    conf->meth_data = (void *)CONF_type_win32;
145
146
0
    return 1;
147
0
}
148
#endif
149
150
static int def_destroy(CONF *conf)
151
41.2k
{
152
41.2k
    if (def_destroy_data(conf)) {
153
41.2k
        OPENSSL_free(conf);
154
41.2k
        return 1;
155
41.2k
    }
156
0
    return 0;
157
41.2k
}
158
159
static int def_destroy_data(CONF *conf)
160
50.2k
{
161
50.2k
    if (conf == NULL)
162
0
        return 0;
163
50.2k
    _CONF_free_data(conf);
164
50.2k
    return 1;
165
50.2k
}
166
167
static int def_load(CONF *conf, const char *name, long *line)
168
225
{
169
225
    int ret;
170
225
    BIO *in = NULL;
171
172
#ifdef OPENSSL_SYS_VMS
173
    in = BIO_new_file(name, "r");
174
#else
175
225
    in = BIO_new_file(name, "rb");
176
225
#endif
177
225
    if (in == NULL) {
178
225
        if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
179
225
            ERR_raise(ERR_LIB_CONF, CONF_R_NO_SUCH_FILE);
180
0
        else
181
225
            ERR_raise(ERR_LIB_CONF, ERR_R_SYS_LIB);
182
225
        return 0;
183
225
    }
184
185
0
    ret = def_load_bio(conf, in, line);
186
0
    BIO_free(in);
187
188
0
    return ret;
189
225
}
190
191
/* Parse a boolean value and fill in *flag. Return 0 on error. */
192
static int parsebool(const char *pval, int *flag)
193
10.7k
{
194
10.7k
    if (OPENSSL_strcasecmp(pval, "on") == 0
195
6.73k
        || OPENSSL_strcasecmp(pval, "true") == 0) {
196
6.73k
        *flag = 1;
197
6.73k
    } else if (OPENSSL_strcasecmp(pval, "off") == 0
198
3.64k
        || OPENSSL_strcasecmp(pval, "false") == 0) {
199
3.64k
        *flag = 0;
200
3.64k
    } else {
201
369
        ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
202
369
        return 0;
203
369
    }
204
10.3k
    return 1;
205
10.7k
}
206
207
static int def_load_bio(CONF *conf, BIO *in, long *line)
208
26.2k
{
209
/* The macro BUFSIZE conflicts with a system macro in VxWorks */
210
60.8M
#define CONFBUFSIZE 512
211
26.2k
    int bufnum = 0, i, ii;
212
26.2k
    BUF_MEM *buff = NULL;
213
26.2k
    char *s, *p, *end;
214
26.2k
    int again;
215
26.2k
    int first_call = 1;
216
26.2k
    long eline = 0;
217
26.2k
    char btmp[DECIMAL_SIZE(eline) + 1];
218
26.2k
    CONF_VALUE *v = NULL, *tv;
219
26.2k
    CONF_VALUE *sv = NULL;
220
26.2k
    char *section = NULL, *buf;
221
26.2k
    char *start, *psection, *pname;
222
26.2k
    void *h = (void *)(conf->data);
223
26.2k
    STACK_OF(BIO) *biosk = NULL;
224
26.2k
#ifndef OPENSSL_NO_POSIX_IO
225
26.2k
    char *dirpath = NULL;
226
26.2k
    OPENSSL_DIR_CTX *dirctx = NULL;
227
26.2k
#endif
228
26.2k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
229
26.2k
    int numincludes = 0;
230
26.2k
#endif
231
232
26.2k
    if ((buff = BUF_MEM_new()) == NULL) {
233
0
        ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
234
0
        goto err;
235
0
    }
236
237
26.2k
    section = OPENSSL_strdup("default");
238
26.2k
    if (section == NULL)
239
0
        goto err;
240
241
26.2k
    if (_CONF_new_data(conf) == 0) {
242
0
        ERR_raise(ERR_LIB_CONF, ERR_R_CONF_LIB);
243
0
        goto err;
244
0
    }
245
246
26.2k
    sv = _CONF_new_section(conf, section);
247
26.2k
    if (sv == NULL) {
248
0
        ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
249
0
        goto err;
250
0
    }
251
252
26.2k
    bufnum = 0;
253
26.2k
    again = 0;
254
20.2M
    for (;;) {
255
20.2M
        if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
256
0
            ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
257
0
            goto err;
258
0
        }
259
20.2M
        p = &(buff->data[bufnum]);
260
20.2M
        *p = '\0';
261
20.2M
    read_retry:
262
20.2M
        if (in != NULL && BIO_gets(in, p, CONFBUFSIZE - 1) < 0)
263
0
            goto err;
264
20.2M
        p[CONFBUFSIZE - 1] = '\0';
265
20.2M
        ii = i = (int)strlen(p);
266
20.2M
        if (first_call) {
267
            /* Other BOMs imply unsupported multibyte encoding,
268
             * so don't strip them and let the error raise */
269
26.2k
            const unsigned char utf8_bom[3] = { 0xEF, 0xBB, 0xBF };
270
271
26.2k
            if (i >= 3 && memcmp(p, utf8_bom, 3) == 0) {
272
39
                memmove(p, p + 3, i - 3);
273
39
                p[i - 3] = 0;
274
39
                i -= 3;
275
39
                ii -= 3;
276
39
            }
277
26.2k
            first_call = 0;
278
26.2k
        }
279
20.2M
        if (i == 0 && !again) {
280
            /* the currently processed BIO is NULL or at EOF */
281
20.8k
            BIO *parent;
282
283
20.8k
#ifndef OPENSSL_NO_POSIX_IO
284
            /* continue processing with the next file from directory */
285
20.8k
            if (dirctx != NULL) {
286
1.49k
                BIO *next;
287
288
1.49k
                if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
289
449
                    BIO_vfree(in);
290
449
                    in = next;
291
449
                    goto read_retry;
292
1.04k
                } else {
293
1.04k
                    OPENSSL_free(dirpath);
294
1.04k
                    dirpath = NULL;
295
1.04k
                }
296
1.49k
            }
297
20.3k
#endif
298
            /* no more files in directory, continue with processing parent */
299
20.3k
            if ((parent = sk_BIO_pop(biosk)) == NULL) {
300
                /* everything processed get out of the loop */
301
19.3k
                break;
302
19.3k
            } else {
303
1.04k
                BIO_vfree(in);
304
1.04k
                in = parent;
305
1.04k
                goto read_retry;
306
1.04k
            }
307
20.3k
        }
308
20.2M
        again = 0;
309
39.8M
        while (i > 0) {
310
20.7M
            if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
311
1.14M
                break;
312
19.5M
            else
313
19.5M
                i--;
314
20.7M
        }
315
        /*
316
         * we removed some trailing stuff so there is a new line on the end.
317
         */
318
20.2M
        if (ii && i == ii)
319
637k
            again = 1; /* long line */
320
19.6M
        else {
321
19.6M
            p[i] = '\0';
322
19.6M
            eline++; /* another input line */
323
19.6M
        }
324
325
        /* we now have a line with trailing \r\n removed */
326
327
        /* i is the number of bytes */
328
20.2M
        bufnum += i;
329
330
20.2M
        v = NULL;
331
        /* check for line continuation */
332
20.2M
        if (!again && bufnum >= 1) {
333
            /*
334
             * If we have bytes and the last char '\\' and second last char
335
             * is not '\\'
336
             */
337
529k
            p = &(buff->data[bufnum - 1]);
338
529k
            if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
339
18.9k
                bufnum--;
340
18.9k
                again = 1;
341
18.9k
            }
342
529k
        }
343
20.2M
        if (again)
344
656k
            continue;
345
19.5M
        bufnum = 0;
346
19.5M
        buf = buff->data;
347
348
19.5M
        clear_comments(conf, buf);
349
19.5M
        s = eat_ws(conf, buf);
350
19.5M
        if (IS_EOF(conf, *s))
351
19.2M
            continue; /* blank line */
352
363k
        if (*s == '[') {
353
31.6k
            char *ss;
354
355
31.6k
            s++;
356
31.6k
            start = eat_ws(conf, s);
357
31.6k
            ss = start;
358
35.9k
        again:
359
35.9k
            end = eat_alpha_numeric(conf, ss);
360
35.9k
            p = eat_ws(conf, end);
361
35.9k
            if (*p != ']') {
362
5.08k
                if (*p != '\0' && ss != p) {
363
4.32k
                    ss = p;
364
4.32k
                    goto again;
365
4.32k
                }
366
5.08k
                ERR_raise(ERR_LIB_CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
367
757
                goto err;
368
5.08k
            }
369
30.9k
            *end = '\0';
370
30.9k
            if (!str_copy(conf, NULL, &section, start))
371
0
                goto err;
372
30.9k
            if ((sv = _CONF_get_section(conf, section)) == NULL)
373
19.0k
                sv = _CONF_new_section(conf, section);
374
30.9k
            if (sv == NULL) {
375
0
                ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
376
0
                goto err;
377
0
            }
378
30.9k
            continue;
379
332k
        } else {
380
332k
            pname = s;
381
332k
            end = eat_alpha_numeric(conf, s);
382
332k
            if ((end[0] == ':') && (end[1] == ':')) {
383
60.0k
                *end = '\0';
384
60.0k
                end += 2;
385
60.0k
                psection = pname;
386
60.0k
                pname = end;
387
60.0k
                end = eat_alpha_numeric(conf, end);
388
272k
            } else {
389
272k
                psection = section;
390
272k
            }
391
332k
            p = eat_ws(conf, end);
392
332k
            if (CHECK_AND_SKIP_PREFIX(pname, ".pragma")
393
19.1k
                && (p != pname || *p == '=')) {
394
19.1k
                char *pval;
395
396
19.1k
                if (*p == '=') {
397
11.2k
                    p++;
398
11.2k
                    p = eat_ws(conf, p);
399
11.2k
                }
400
19.1k
                trim_ws(conf, p);
401
402
                /* Pragma values take the form keyword:value */
403
19.1k
                pval = strchr(p, ':');
404
19.1k
                if (pval == NULL || pval == p || pval[1] == '\0') {
405
301
                    ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
406
301
                    goto err;
407
301
                }
408
409
18.8k
                *pval++ = '\0';
410
18.8k
                trim_ws(conf, p);
411
18.8k
                pval = eat_ws(conf, pval);
412
413
                /*
414
                 * Known pragmas:
415
                 *
416
                 * dollarid     takes "on", "true or "off", "false"
417
                 * abspath      takes "on", "true or "off", "false"
418
                 * includedir   directory prefix
419
                 */
420
18.8k
                if (strcmp(p, "dollarid") == 0) {
421
6.19k
                    if (!parsebool(pval, &conf->flag_dollarid))
422
65
                        goto err;
423
12.6k
                } else if (strcmp(p, "abspath") == 0) {
424
1.99k
                    if (!parsebool(pval, &conf->flag_abspath))
425
220
                        goto err;
426
10.6k
                } else if (strcmp(p, "includedir") == 0) {
427
1.97k
                    OPENSSL_free(conf->includedir);
428
1.97k
                    if ((conf->includedir = OPENSSL_strdup(pval)) == NULL)
429
0
                        goto err;
430
1.97k
                }
431
432
                /*
433
                 * We *ignore* any unknown pragma.
434
                 */
435
18.5k
                continue;
436
313k
            } else if (CHECK_AND_SKIP_PREFIX(pname, ".include")
437
5.92k
                && (p != pname || *p == '=')) {
438
5.88k
                char *include = NULL;
439
5.88k
                BIO *next;
440
5.88k
                const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE");
441
5.88k
                char *include_path = NULL;
442
443
5.88k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
444
                /*
445
                 * The include processing below can cause the "conf" fuzzer to
446
                 * timeout due to the fuzzer inserting large and complicated
447
                 * includes - with a large amount of time spent in
448
                 * OPENSSL_strlcat/OPENSSL_strcpy. This is not a security
449
                 * concern because config files should never come from untrusted
450
                 * sources. We just set an arbitrary limit on the allowed
451
                 * number of includes when fuzzing to prevent this timeout.
452
                 */
453
5.88k
                if (numincludes++ > 10)
454
36
                    goto err;
455
5.84k
#endif
456
457
5.84k
                if (include_dir == NULL)
458
5.84k
                    include_dir = conf->includedir;
459
460
5.84k
                if (*p == '=') {
461
1.33k
                    p++;
462
1.33k
                    p = eat_ws(conf, p);
463
1.33k
                }
464
5.84k
                trim_ws(conf, p);
465
5.84k
                if (!str_copy(conf, psection, &include, p))
466
26
                    goto err;
467
468
5.82k
                if (include_dir != NULL && !ossl_is_absolute_path(include)) {
469
1.29k
                    size_t newlen = strlen(include_dir) + strlen(include) + 2;
470
471
1.29k
                    include_path = OPENSSL_malloc(newlen);
472
1.29k
                    if (include_path == NULL) {
473
0
                        OPENSSL_free(include);
474
0
                        goto err;
475
0
                    }
476
477
1.29k
                    OPENSSL_strlcpy(include_path, include_dir, newlen);
478
1.29k
                    if (!ossl_ends_with_dirsep(include_path))
479
1.09k
                        OPENSSL_strlcat(include_path, "/", newlen);
480
1.29k
                    OPENSSL_strlcat(include_path, include, newlen);
481
1.29k
                    OPENSSL_free(include);
482
4.53k
                } else {
483
4.53k
                    include_path = include;
484
4.53k
                }
485
486
5.82k
                if (conf->flag_abspath
487
159
                    && !ossl_is_absolute_path(include_path)) {
488
51
                    ERR_raise(ERR_LIB_CONF, CONF_R_RELATIVE_PATH);
489
51
                    OPENSSL_free(include_path);
490
51
                    goto err;
491
51
                }
492
493
                /* get the BIO of the included file */
494
5.77k
#ifndef OPENSSL_NO_POSIX_IO
495
5.77k
                next = process_include(include_path, &dirctx, &dirpath);
496
5.77k
                if (include_path != dirpath) {
497
                    /* dirpath will contain include in case of a directory */
498
4.58k
                    OPENSSL_free(include_path);
499
4.58k
                }
500
#else
501
                next = BIO_new_file(include_path, "r");
502
                OPENSSL_free(include_path);
503
#endif
504
505
5.77k
                if (next != NULL) {
506
                    /* push the currently processing BIO onto stack */
507
1.20k
                    if (biosk == NULL) {
508
493
                        if ((biosk = sk_BIO_new_null()) == NULL) {
509
0
                            ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB);
510
0
                            BIO_free(next);
511
0
                            goto err;
512
0
                        }
513
493
                    }
514
1.20k
                    if (!sk_BIO_push(biosk, in)) {
515
0
                        ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB);
516
0
                        BIO_free(next);
517
0
                        goto err;
518
0
                    }
519
                    /* continue with reading from the included BIO */
520
1.20k
                    in = next;
521
1.20k
                }
522
5.77k
                continue;
523
307k
            } else if (*p != '=') {
524
3.66k
                ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN,
525
3.66k
                    "HERE-->%s", p);
526
3.66k
                goto err;
527
3.66k
            }
528
303k
            *end = '\0';
529
303k
            p++;
530
303k
            start = eat_ws(conf, p);
531
303k
            trim_ws(conf, start);
532
533
303k
            if ((v = OPENSSL_malloc(sizeof(*v))) == NULL)
534
0
                goto err;
535
303k
            v->name = OPENSSL_strdup(pname);
536
303k
            v->value = NULL;
537
303k
            if (v->name == NULL)
538
0
                goto err;
539
303k
            if (!str_copy(conf, psection, &(v->value), start))
540
1.76k
                goto err;
541
542
301k
            if (strcmp(psection, section) != 0) {
543
59.7k
                if ((tv = _CONF_get_section(conf, psection))
544
59.7k
                    == NULL)
545
45.7k
                    tv = _CONF_new_section(conf, psection);
546
59.7k
                if (tv == NULL) {
547
0
                    ERR_raise(ERR_LIB_CONF,
548
0
                        CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
549
0
                    goto err;
550
0
                }
551
59.7k
            } else
552
242k
                tv = sv;
553
301k
            if (_CONF_add_string(conf, tv, v) == 0) {
554
0
                ERR_raise(ERR_LIB_CONF, ERR_R_CONF_LIB);
555
0
                goto err;
556
0
            }
557
301k
            v = NULL;
558
301k
        }
559
363k
    }
560
19.3k
    BUF_MEM_free(buff);
561
19.3k
    OPENSSL_free(section);
562
    /*
563
     * No need to pop, since we only get here if the stack is empty.
564
     * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE!
565
     */
566
19.3k
    sk_BIO_free(biosk);
567
19.3k
    return 1;
568
569
6.89k
err:
570
6.89k
    BUF_MEM_free(buff);
571
6.89k
    OPENSSL_free(section);
572
    /*
573
     * Since |in| is the first element of the stack and should NOT be freed
574
     * here, we cannot use sk_BIO_pop_free().  Instead, we pop and free one
575
     * BIO at a time, making sure that the last one popped isn't.
576
     */
577
7.04k
    while (sk_BIO_num(biosk) > 0) {
578
157
        BIO *popped = sk_BIO_pop(biosk);
579
157
        BIO_vfree(in);
580
157
        in = popped;
581
157
    }
582
6.89k
    sk_BIO_free(biosk);
583
6.89k
#ifndef OPENSSL_NO_POSIX_IO
584
6.89k
    OPENSSL_free(dirpath);
585
6.89k
    if (dirctx != NULL)
586
137
        OPENSSL_DIR_end(&dirctx);
587
6.89k
#endif
588
6.89k
    if (line != NULL)
589
6.03k
        *line = eline;
590
6.89k
    BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
591
6.89k
    ERR_add_error_data(2, "line ", btmp);
592
6.89k
    if (h != conf->data) {
593
6.89k
        CONF_free(conf->data);
594
6.89k
        conf->data = NULL;
595
6.89k
    }
596
6.89k
    if (v != NULL) {
597
1.76k
        OPENSSL_free(v->name);
598
1.76k
        OPENSSL_free(v->value);
599
1.76k
        OPENSSL_free(v);
600
1.76k
    }
601
6.89k
    return 0;
602
26.2k
}
603
604
static void clear_comments(CONF *conf, char *p)
605
20.1M
{
606
20.1M
    for (;;) {
607
20.1M
        if (IS_FCOMMENT(conf, *p)) {
608
0
            *p = '\0';
609
0
            return;
610
0
        }
611
20.1M
        if (!IS_WS(conf, *p)) {
612
20.1M
            break;
613
20.1M
        }
614
54.1k
        p++;
615
54.1k
    }
616
617
331M
    for (;;) {
618
331M
        if (IS_COMMENT(conf, *p)) {
619
273k
            *p = '\0';
620
273k
            return;
621
273k
        }
622
330M
        if (IS_DQUOTE(conf, *p)) {
623
0
            p = scan_dquote(conf, p);
624
0
            continue;
625
0
        }
626
330M
        if (IS_QUOTE(conf, *p)) {
627
1.31M
            p = scan_quote(conf, p);
628
1.31M
            continue;
629
1.31M
        }
630
329M
        if (IS_ESC(conf, *p)) {
631
1.24M
            p = scan_esc(conf, p);
632
1.24M
            continue;
633
1.24M
        }
634
328M
        if (IS_EOF(conf, *p))
635
19.8M
            return;
636
308M
        else
637
308M
            p++;
638
328M
    }
639
20.1M
}
640
641
static int str_copy(CONF *conf, char *section, char **pto, char *from)
642
340k
{
643
340k
    int q, r, rr = 0, to = 0;
644
340k
    char *s, *e, *rp, *p, *rrp, *np, *cp, v;
645
340k
    BUF_MEM *buf;
646
647
340k
    if ((buf = BUF_MEM_new()) == NULL)
648
0
        return 0;
649
650
340k
    if (!BUF_MEM_grow(buf, strlen(from) + 1))
651
0
        goto err;
652
653
83.6M
    for (;;) {
654
83.6M
        if (IS_QUOTE(conf, *from)) {
655
66.6k
            q = *from;
656
66.6k
            from++;
657
25.8M
            while (!IS_EOF(conf, *from) && (*from != q)) {
658
25.7M
                if (IS_ESC(conf, *from)) {
659
98.2k
                    from++;
660
98.2k
                    if (IS_EOF(conf, *from))
661
3.31k
                        break;
662
98.2k
                }
663
25.7M
                buf->data[to++] = *(from++);
664
25.7M
            }
665
66.6k
            if (*from == q)
666
57.4k
                from++;
667
83.6M
        } else if (IS_DQUOTE(conf, *from)) {
668
0
            q = *from;
669
0
            from++;
670
0
            while (!IS_EOF(conf, *from)) {
671
0
                if (*from == q) {
672
0
                    if (*(from + 1) == q) {
673
0
                        from++;
674
0
                    } else {
675
0
                        break;
676
0
                    }
677
0
                }
678
0
                buf->data[to++] = *(from++);
679
0
            }
680
0
            if (*from == q)
681
0
                from++;
682
83.6M
        } else if (IS_ESC(conf, *from)) {
683
322k
            from++;
684
322k
            v = *(from++);
685
322k
            if (IS_EOF(conf, v))
686
10.3k
                break;
687
312k
            else if (v == 'r')
688
59.1k
                v = '\r';
689
252k
            else if (v == 'n')
690
6.19k
                v = '\n';
691
246k
            else if (v == 'b')
692
10.1k
                v = '\b';
693
236k
            else if (v == 't')
694
4.46k
                v = '\t';
695
312k
            buf->data[to++] = v;
696
83.3M
        } else if (IS_EOF(conf, *from))
697
328k
            break;
698
82.9M
        else if (*from == '$'
699
106k
            && (!conf->flag_dollarid
700
9.90k
                || from[1] == '{'
701
100k
                || from[1] == '(')) {
702
100k
            size_t newsize;
703
704
            /* try to expand it */
705
100k
            rrp = NULL;
706
100k
            s = &(from[1]);
707
100k
            if (*s == '{')
708
3.45k
                q = '}';
709
96.5k
            else if (*s == '(')
710
2.99k
                q = ')';
711
93.5k
            else
712
93.5k
                q = 0;
713
714
100k
            if (q)
715
6.44k
                s++;
716
100k
            cp = section;
717
100k
            e = np = s;
718
14.3M
            while (IS_ALNUM(conf, *e)
719
101k
                || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
720
14.2M
                e++;
721
100k
            if ((e[0] == ':') && (e[1] == ':')) {
722
7.54k
                cp = np;
723
7.54k
                rrp = e;
724
7.54k
                rr = *e;
725
7.54k
                *rrp = '\0';
726
7.54k
                e += 2;
727
7.54k
                np = e;
728
10.9k
                while (IS_ALNUM(conf, *e)
729
8.66k
                    || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
730
3.38k
                    e++;
731
7.54k
            }
732
100k
            r = *e;
733
100k
            *e = '\0';
734
100k
            rp = e;
735
100k
            if (q) {
736
6.44k
                if (r != q) {
737
385
                    ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE);
738
385
                    goto err;
739
385
                }
740
6.06k
                e++;
741
6.06k
            }
742
            /*-
743
             * So at this point we have
744
             * np which is the start of the name string which is
745
             *   '\0' terminated.
746
             * cp which is the start of the section string which is
747
             *   '\0' terminated.
748
             * e is the 'next point after'.
749
             * r and rr are the chars replaced by the '\0'
750
             * rp and rrp is where 'r' and 'rr' came from.
751
             */
752
99.6k
            p = _CONF_get_string(conf, cp, np);
753
99.6k
            if (rrp != NULL)
754
7.49k
                *rrp = rr;
755
99.6k
            *rp = r;
756
99.6k
            if (p == NULL) {
757
1.30k
                ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_HAS_NO_VALUE);
758
1.30k
                goto err;
759
1.30k
            }
760
98.3k
            newsize = strlen(p) + buf->length - (e - from);
761
98.3k
            if (newsize > MAX_CONF_VALUE_LENGTH) {
762
107
                ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
763
107
                goto err;
764
107
            }
765
98.2k
            if (!BUF_MEM_grow_clean(buf, newsize)) {
766
0
                ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
767
0
                goto err;
768
0
            }
769
53.8M
            while (*p)
770
53.7M
                buf->data[to++] = *(p++);
771
772
98.2k
            from = e;
773
774
            /*
775
             * In case there were no braces or parenthesis around the
776
             * variable reference, we have to put back the character that was
777
             * replaced with a '\0'.  /RL
778
             */
779
98.2k
            *rp = r;
780
98.2k
        } else
781
82.8M
            buf->data[to++] = *(from++);
782
83.6M
    }
783
338k
    buf->data[to] = '\0';
784
338k
    OPENSSL_free(*pto);
785
338k
    *pto = buf->data;
786
338k
    OPENSSL_free(buf);
787
338k
    return 1;
788
1.79k
err:
789
1.79k
    BUF_MEM_free(buf);
790
1.79k
    return 0;
791
340k
}
792
793
#ifndef OPENSSL_NO_POSIX_IO
794
/*
795
 * Check whether included path is a directory.
796
 * Returns next BIO to process and in case of a directory
797
 * also an opened directory context and the include path.
798
 */
799
static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
800
    char **dirpath)
801
7.94k
{
802
7.94k
    struct stat st;
803
7.94k
    BIO *next;
804
805
7.94k
    if (stat(include, &st) < 0) {
806
4.42k
        ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include);
807
        /* missing include file is not fatal error */
808
4.42k
        return NULL;
809
4.42k
    }
810
811
3.51k
    if (S_ISDIR(st.st_mode)) {
812
3.48k
        if (*dirctx != NULL) {
813
0
            ERR_raise_data(ERR_LIB_CONF, CONF_R_RECURSIVE_DIRECTORY_INCLUDE,
814
0
                "%s", include);
815
0
            return NULL;
816
0
        }
817
        /* a directory, load its contents */
818
3.48k
        if ((next = get_next_file(include, dirctx)) != NULL)
819
1.83k
            *dirpath = include;
820
3.48k
        return next;
821
3.48k
    }
822
823
33
    next = BIO_new_file(include, "r");
824
33
    return next;
825
3.51k
}
826
827
/*
828
 * Get next file from the directory path.
829
 * Returns BIO of the next file to read and updates dirctx.
830
 */
831
static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx)
832
3.96k
{
833
3.96k
    const char *filename;
834
3.96k
    size_t pathlen;
835
836
3.96k
    pathlen = strlen(path);
837
277k
    while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
838
275k
        size_t namelen;
839
840
275k
        namelen = strlen(filename);
841
842
275k
        if ((namelen > 5
843
219k
                && OPENSSL_strcasecmp(filename + namelen - 5, ".conf") == 0)
844
267k
            || (namelen > 4
845
232k
                && OPENSSL_strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
846
8.53k
            size_t newlen;
847
8.53k
            char *newpath;
848
8.53k
            BIO *bio;
849
850
8.53k
            newlen = pathlen + namelen + 2;
851
8.53k
            newpath = OPENSSL_zalloc(newlen);
852
8.53k
            if (newpath == NULL)
853
0
                break;
854
#ifdef OPENSSL_SYS_VMS
855
            /*
856
             * If the given path isn't clear VMS syntax,
857
             * we treat it as on Unix.
858
             */
859
            if (path[pathlen - 1] == ']'
860
                || path[pathlen - 1] == '>'
861
                || path[pathlen - 1] == ':') {
862
                /* Clear VMS directory syntax, just copy as is */
863
                OPENSSL_strlcpy(newpath, path, newlen);
864
            }
865
#endif
866
8.53k
            if (newpath[0] == '\0') {
867
8.53k
                OPENSSL_strlcpy(newpath, path, newlen);
868
8.53k
                OPENSSL_strlcat(newpath, "/", newlen);
869
8.53k
            }
870
8.53k
            OPENSSL_strlcat(newpath, filename, newlen);
871
872
8.53k
            bio = BIO_new_file(newpath, "r");
873
8.53k
            OPENSSL_free(newpath);
874
            /* Errors when opening files are non-fatal. */
875
8.53k
            if (bio != NULL)
876
1.63k
                return bio;
877
8.53k
        }
878
275k
    }
879
2.33k
    OPENSSL_DIR_end(dirctx);
880
2.33k
    *dirctx = NULL;
881
2.33k
    return NULL;
882
3.96k
}
883
#endif
884
885
static int is_keytype(const CONF *conf, char c, unsigned short type)
886
2.79G
{
887
2.79G
    const unsigned short *keytypes = (const unsigned short *)conf->meth_data;
888
2.79G
    unsigned char key = (unsigned char)c;
889
890
#ifdef CHARSET_EBCDIC
891
#if CHAR_BIT > 8
892
    if (key > 255) {
893
        /* key is out of range for os_toascii table */
894
        return 0;
895
    }
896
#endif
897
    /* convert key from ebcdic to ascii */
898
    key = os_toascii[key];
899
#endif
900
901
2.79G
    if (key > 127) {
902
        /* key is not a seven bit ascii character */
903
185M
        return 0;
904
185M
    }
905
906
2.60G
    return (keytypes[key] & type) ? 1 : 0;
907
2.79G
}
908
909
static char *eat_ws(CONF *conf, char *p)
910
21.1M
{
911
21.5M
    while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
912
418k
        p++;
913
21.1M
    return p;
914
21.1M
}
915
916
static void trim_ws(CONF *conf, char *start)
917
498k
{
918
498k
    char *p = start;
919
920
183M
    while (!IS_EOF(conf, *p))
921
182M
        p++;
922
498k
    p--;
923
578k
    while ((p >= start) && IS_WS(conf, *p))
924
80.5k
        p--;
925
498k
    p++;
926
498k
    *p = '\0';
927
498k
}
928
929
static char *eat_alpha_numeric(CONF *conf, char *p)
930
608k
{
931
135M
    for (;;) {
932
135M
        if (IS_ESC(conf, *p)) {
933
707k
            p = scan_esc(conf, p);
934
707k
            continue;
935
707k
        }
936
134M
        if (!(IS_ALNUM_PUNCT(conf, *p)
937
612k
                || (conf->flag_dollarid && IS_DOLLAR(conf, *p))))
938
608k
            return p;
939
134M
        p++;
940
134M
    }
941
608k
}
942
943
static char *scan_quote(CONF *conf, char *p)
944
1.31M
{
945
1.31M
    int q = *p;
946
947
1.31M
    p++;
948
59.6M
    while (!(IS_EOF(conf, *p)) && (*p != q)) {
949
58.3M
        if (IS_ESC(conf, *p)) {
950
232k
            p++;
951
232k
            if (IS_EOF(conf, *p))
952
2.81k
                return p;
953
232k
        }
954
58.3M
        p++;
955
58.3M
    }
956
1.30M
    if (*p == q)
957
1.29M
        p++;
958
1.30M
    return p;
959
1.31M
}
960
961
static char *scan_dquote(CONF *conf, char *p)
962
0
{
963
0
    int q = *p;
964
965
0
    p++;
966
0
    while (!(IS_EOF(conf, *p))) {
967
0
        if (*p == q) {
968
0
            if (*(p + 1) == q) {
969
0
                p++;
970
0
            } else {
971
0
                break;
972
0
            }
973
0
        }
974
0
        p++;
975
0
    }
976
0
    if (*p == q)
977
0
        p++;
978
0
    return p;
979
0
}
980
981
static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
982
0
{
983
0
    if (a->name)
984
0
        BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
985
0
    else
986
0
        BIO_printf(out, "[[%s]]\n", a->section);
987
0
}
988
989
IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
990
991
static int def_dump(const CONF *conf, BIO *out)
992
0
{
993
0
    lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
994
0
    return 1;
995
0
}
996
997
static int def_is_number(const CONF *conf, char c)
998
0
{
999
0
    return IS_NUMBER(conf, c);
1000
0
}
1001
1002
static int def_to_int(const CONF *conf, char c)
1003
0
{
1004
0
    return c - '0';
1005
0
}