Coverage Report

Created: 2025-11-16 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/conf/conf_def.c
Line
Count
Source
1
/*
2
 * Copyright 1995-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
/* 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.8k
#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
576k
#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
16.7k
{
88
16.7k
    return &default_method;
89
16.7k
}
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
16.7k
{
113
16.7k
    CONF *ret;
114
115
16.7k
    ret = OPENSSL_malloc(sizeof(*ret));
116
16.7k
    if (ret != NULL)
117
16.7k
        if (meth->init(ret) == 0) {
118
0
            OPENSSL_free(ret);
119
0
            ret = NULL;
120
0
        }
121
16.7k
    return ret;
122
16.7k
}
123
124
static int def_init_default(CONF *conf)
125
23.2k
{
126
23.2k
    if (conf == NULL)
127
0
        return 0;
128
129
23.2k
    memset(conf, 0, sizeof(*conf));
130
23.2k
    conf->meth = &default_method;
131
23.2k
    conf->meth_data = (void *)CONF_type_default;
132
133
23.2k
    return 1;
134
23.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
16.7k
{
152
16.7k
    if (def_destroy_data(conf)) {
153
16.7k
        OPENSSL_free(conf);
154
16.7k
        return 1;
155
16.7k
    }
156
0
    return 0;
157
16.7k
}
158
159
static int def_destroy_data(CONF *conf)
160
23.2k
{
161
23.2k
    if (conf == NULL)
162
0
        return 0;
163
23.2k
    _CONF_free_data(conf);
164
23.2k
    return 1;
165
23.2k
}
166
167
static int def_load(CONF *conf, const char *name, long *line)
168
167
{
169
167
    int ret;
170
167
    BIO *in = NULL;
171
172
#ifdef OPENSSL_SYS_VMS
173
    in = BIO_new_file(name, "r");
174
#else
175
167
    in = BIO_new_file(name, "rb");
176
167
#endif
177
167
    if (in == NULL) {
178
167
        if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
179
167
            ERR_raise(ERR_LIB_CONF, CONF_R_NO_SUCH_FILE);
180
0
        else
181
167
            ERR_raise(ERR_LIB_CONF, ERR_R_SYS_LIB);
182
167
        return 0;
183
167
    }
184
185
0
    ret = def_load_bio(conf, in, line);
186
0
    BIO_free(in);
187
188
0
    return ret;
189
167
}
190
191
192
/* Parse a boolean value and fill in *flag. Return 0 on error. */
193
static int parsebool(const char *pval, int *flag)
194
7.64k
{
195
7.64k
    if (OPENSSL_strcasecmp(pval, "on") == 0
196
5.54k
        || OPENSSL_strcasecmp(pval, "true") == 0) {
197
5.54k
        *flag = 1;
198
5.54k
    } else if (OPENSSL_strcasecmp(pval, "off") == 0
199
1.85k
               || OPENSSL_strcasecmp(pval, "false") == 0) {
200
1.85k
        *flag = 0;
201
1.85k
    } else {
202
250
        ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
203
250
        return 0;
204
250
    }
205
7.39k
    return 1;
206
7.64k
}
207
208
static int def_load_bio(CONF *conf, BIO *in, long *line)
209
15.4k
{
210
/* The macro BUFSIZE conflicts with a system macro in VxWorks */
211
98.2M
#define CONFBUFSIZE     512
212
15.4k
    int bufnum = 0, i, ii;
213
15.4k
    BUF_MEM *buff = NULL;
214
15.4k
    char *s, *p, *end;
215
15.4k
    int again;
216
15.4k
    int first_call = 1;
217
15.4k
    long eline = 0;
218
15.4k
    char btmp[DECIMAL_SIZE(eline) + 1];
219
15.4k
    CONF_VALUE *v = NULL, *tv;
220
15.4k
    CONF_VALUE *sv = NULL;
221
15.4k
    char *section = NULL, *buf;
222
15.4k
    char *start, *psection, *pname;
223
15.4k
    void *h = (void *)(conf->data);
224
15.4k
    STACK_OF(BIO) *biosk = NULL;
225
15.4k
#ifndef OPENSSL_NO_POSIX_IO
226
15.4k
    char *dirpath = NULL;
227
15.4k
    OPENSSL_DIR_CTX *dirctx = NULL;
228
15.4k
#endif
229
15.4k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
230
15.4k
    int numincludes = 0;
231
15.4k
#endif
232
233
15.4k
    if ((buff = BUF_MEM_new()) == NULL) {
234
0
        ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
235
0
        goto err;
236
0
    }
237
238
15.4k
    section = OPENSSL_strdup("default");
239
15.4k
    if (section == NULL)
240
0
        goto err;
241
242
15.4k
    if (_CONF_new_data(conf) == 0) {
243
0
        ERR_raise(ERR_LIB_CONF, ERR_R_CONF_LIB);
244
0
        goto err;
245
0
    }
246
247
15.4k
    sv = _CONF_new_section(conf, section);
248
15.4k
    if (sv == NULL) {
249
0
        ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
250
0
        goto err;
251
0
    }
252
253
15.4k
    bufnum = 0;
254
15.4k
    again = 0;
255
32.7M
    for (;;) {
256
32.7M
        if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
257
0
            ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
258
0
            goto err;
259
0
        }
260
32.7M
        p = &(buff->data[bufnum]);
261
32.7M
        *p = '\0';
262
32.7M
 read_retry:
263
32.7M
        if (in != NULL && BIO_gets(in, p, CONFBUFSIZE - 1) < 0)
264
0
            goto err;
265
32.7M
        p[CONFBUFSIZE - 1] = '\0';
266
32.7M
        ii = i = strlen(p);
267
32.7M
        if (first_call) {
268
            /* Other BOMs imply unsupported multibyte encoding,
269
             * so don't strip them and let the error raise */
270
15.4k
            const unsigned char utf8_bom[3] = {0xEF, 0xBB, 0xBF};
271
272
15.4k
            if (i >= 3 && memcmp(p, utf8_bom, 3) == 0) {
273
38
                memmove(p, p + 3, i - 3);
274
38
                p[i - 3] = 0;
275
38
                i -= 3;
276
38
                ii -= 3;
277
38
            }
278
15.4k
            first_call = 0;
279
15.4k
        }
280
32.7M
        if (i == 0 && !again) {
281
            /* the currently processed BIO is NULL or at EOF */
282
11.5k
            BIO *parent;
283
284
11.5k
#ifndef OPENSSL_NO_POSIX_IO
285
            /* continue processing with the next file from directory */
286
11.5k
            if (dirctx != NULL) {
287
2.26k
                BIO *next;
288
289
2.26k
                if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
290
589
                    BIO_vfree(in);
291
589
                    in = next;
292
589
                    goto read_retry;
293
1.68k
                } else {
294
1.68k
                    OPENSSL_free(dirpath);
295
1.68k
                    dirpath = NULL;
296
1.68k
                }
297
2.26k
            }
298
10.9k
#endif
299
            /* no more files in directory, continue with processing parent */
300
10.9k
            if ((parent = sk_BIO_pop(biosk)) == NULL) {
301
                /* everything processed get out of the loop */
302
9.31k
                break;
303
9.31k
            } else {
304
1.68k
                BIO_vfree(in);
305
1.68k
                in = parent;
306
1.68k
                goto read_retry;
307
1.68k
            }
308
10.9k
        }
309
32.7M
        again = 0;
310
65.0M
        while (i > 0) {
311
33.4M
            if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
312
1.11M
                break;
313
32.2M
            else
314
32.2M
                i--;
315
33.4M
        }
316
        /*
317
         * we removed some trailing stuff so there is a new line on the end.
318
         */
319
32.7M
        if (ii && i == ii)
320
434k
            again = 1;          /* long line */
321
32.3M
        else {
322
32.3M
            p[i] = '\0';
323
32.3M
            eline++;            /* another input line */
324
32.3M
        }
325
326
        /* we now have a line with trailing \r\n removed */
327
328
        /* i is the number of bytes */
329
32.7M
        bufnum += i;
330
331
32.7M
        v = NULL;
332
        /* check for line continuation */
333
32.7M
        if (!again && bufnum >= 1) {
334
            /*
335
             * If we have bytes and the last char '\\' and second last char
336
             * is not '\\'
337
             */
338
696k
            p = &(buff->data[bufnum - 1]);
339
696k
            if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
340
16.1k
                bufnum--;
341
16.1k
                again = 1;
342
16.1k
            }
343
696k
        }
344
32.7M
        if (again)
345
450k
            continue;
346
32.2M
        bufnum = 0;
347
32.2M
        buf = buff->data;
348
349
32.2M
        clear_comments(conf, buf);
350
32.2M
        s = eat_ws(conf, buf);
351
32.2M
        if (IS_EOF(conf, *s))
352
31.8M
            continue;           /* blank line */
353
425k
        if (*s == '[') {
354
39.4k
            char *ss;
355
356
39.4k
            s++;
357
39.4k
            start = eat_ws(conf, s);
358
39.4k
            ss = start;
359
44.9k
 again:
360
44.9k
            end = eat_alpha_numeric(conf, ss);
361
44.9k
            p = eat_ws(conf, end);
362
44.9k
            if (*p != ']') {
363
6.09k
                if (*p != '\0' && ss != p) {
364
5.50k
                    ss = p;
365
5.50k
                    goto again;
366
5.50k
                }
367
6.09k
                ERR_raise(ERR_LIB_CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
368
595
                goto err;
369
6.09k
            }
370
38.8k
            *end = '\0';
371
38.8k
            if (!str_copy(conf, NULL, &section, start))
372
0
                goto err;
373
38.8k
            if ((sv = _CONF_get_section(conf, section)) == NULL)
374
21.0k
                sv = _CONF_new_section(conf, section);
375
38.8k
            if (sv == NULL) {
376
0
                ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
377
0
                goto err;
378
0
            }
379
38.8k
            continue;
380
386k
        } else {
381
386k
            pname = s;
382
386k
            end = eat_alpha_numeric(conf, s);
383
386k
            if ((end[0] == ':') && (end[1] == ':')) {
384
70.9k
                *end = '\0';
385
70.9k
                end += 2;
386
70.9k
                psection = pname;
387
70.9k
                pname = end;
388
70.9k
                end = eat_alpha_numeric(conf, end);
389
315k
            } else {
390
315k
                psection = section;
391
315k
            }
392
386k
            p = eat_ws(conf, end);
393
386k
            if (CHECK_AND_SKIP_PREFIX(pname, ".pragma")
394
15.3k
                && (p != pname || *p == '=')) {
395
15.2k
                char *pval;
396
397
15.2k
                if (*p == '=') {
398
9.60k
                    p++;
399
9.60k
                    p = eat_ws(conf, p);
400
9.60k
                }
401
15.2k
                trim_ws(conf, p);
402
403
                /* Pragma values take the form keyword:value */
404
15.2k
                pval = strchr(p, ':');
405
15.2k
                if (pval == NULL || pval == p || pval[1] == '\0') {
406
244
                    ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
407
244
                    goto err;
408
244
                }
409
410
15.0k
                *pval++ = '\0';
411
15.0k
                trim_ws(conf, p);
412
15.0k
                pval = eat_ws(conf, pval);
413
414
                /*
415
                 * Known pragmas:
416
                 *
417
                 * dollarid     takes "on", "true or "off", "false"
418
                 * abspath      takes "on", "true or "off", "false"
419
                 * includedir   directory prefix
420
                 */
421
15.0k
                if (strcmp(p, "dollarid") == 0) {
422
3.30k
                    if (!parsebool(pval, &conf->flag_dollarid))
423
23
                        goto err;
424
11.7k
                } else if (strcmp(p, "abspath") == 0) {
425
3.10k
                    if (!parsebool(pval, &conf->flag_abspath))
426
219
                        goto err;
427
8.64k
                } else if (strcmp(p, "includedir") == 0) {
428
1.69k
                    OPENSSL_free(conf->includedir);
429
1.69k
                    if ((conf->includedir = OPENSSL_strdup(pval)) == NULL)
430
0
                        goto err;
431
1.69k
                }
432
433
                /*
434
                 * We *ignore* any unknown pragma.
435
                 */
436
14.8k
                continue;
437
371k
            } else if (CHECK_AND_SKIP_PREFIX(pname, ".include")
438
5.71k
                && (p != pname || *p == '=')) {
439
5.66k
                char *include = NULL;
440
5.66k
                BIO *next;
441
5.66k
                const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE");
442
5.66k
                char *include_path = NULL;
443
444
5.66k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
445
                /*
446
                 * The include processing below can cause the "conf" fuzzer to
447
                 * timeout due to the fuzzer inserting large and complicated
448
                 * includes - with a large amount of time spent in
449
                 * OPENSSL_strlcat/OPENSSL_strcpy. This is not a security
450
                 * concern because config files should never come from untrusted
451
                 * sources. We just set an arbitrary limit on the allowed
452
                 * number of includes when fuzzing to prevent this timeout.
453
                 */
454
5.66k
                if (numincludes++ > 10)
455
27
                    goto err;
456
5.64k
#endif
457
458
5.64k
                if (include_dir == NULL)
459
5.64k
                    include_dir = conf->includedir;
460
461
5.64k
                if (*p == '=') {
462
1.08k
                    p++;
463
1.08k
                    p = eat_ws(conf, p);
464
1.08k
                }
465
5.64k
                trim_ws(conf, p);
466
5.64k
                if (!str_copy(conf, psection, &include, p))
467
13
                    goto err;
468
469
5.62k
                if (include_dir != NULL && !ossl_is_absolute_path(include)) {
470
987
                    size_t newlen = strlen(include_dir) + strlen(include) + 2;
471
472
987
                    include_path = OPENSSL_malloc(newlen);
473
987
                    if (include_path == NULL) {
474
0
                        OPENSSL_free(include);
475
0
                        goto err;
476
0
                    }
477
478
987
                    OPENSSL_strlcpy(include_path, include_dir, newlen);
479
987
                    if (!ossl_ends_with_dirsep(include_path))
480
849
                        OPENSSL_strlcat(include_path, "/", newlen);
481
987
                    OPENSSL_strlcat(include_path, include, newlen);
482
987
                    OPENSSL_free(include);
483
4.64k
                } else {
484
4.64k
                    include_path = include;
485
4.64k
                }
486
487
5.62k
                if (conf->flag_abspath
488
182
                        && !ossl_is_absolute_path(include_path)) {
489
48
                    ERR_raise(ERR_LIB_CONF, CONF_R_RELATIVE_PATH);
490
48
                    OPENSSL_free(include_path);
491
48
                    goto err;
492
48
                }
493
494
                /* get the BIO of the included file */
495
5.58k
#ifndef OPENSSL_NO_POSIX_IO
496
5.58k
                next = process_include(include_path, &dirctx, &dirpath);
497
5.58k
                if (include_path != dirpath) {
498
                    /* dirpath will contain include in case of a directory */
499
3.62k
                    OPENSSL_free(include_path);
500
3.62k
                }
501
#else
502
                next = BIO_new_file(include_path, "r");
503
                OPENSSL_free(include_path);
504
#endif
505
506
5.58k
                if (next != NULL) {
507
                    /* push the currently processing BIO onto stack */
508
1.98k
                    if (biosk == NULL) {
509
852
                        if ((biosk = sk_BIO_new_null()) == NULL) {
510
0
                            ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB);
511
0
                            BIO_free(next);
512
0
                            goto err;
513
0
                        }
514
852
                    }
515
1.98k
                    if (!sk_BIO_push(biosk, in)) {
516
0
                        ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB);
517
0
                        BIO_free(next);
518
0
                        goto err;
519
0
                    }
520
                    /* continue with reading from the included BIO */
521
1.98k
                    in = next;
522
1.98k
                }
523
5.58k
                continue;
524
365k
            } else if (*p != '=') {
525
3.25k
                ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN,
526
3.25k
                               "HERE-->%s", p);
527
3.25k
                goto err;
528
3.25k
            }
529
362k
            *end = '\0';
530
362k
            p++;
531
362k
            start = eat_ws(conf, p);
532
362k
            trim_ws(conf, start);
533
534
362k
            if ((v = OPENSSL_malloc(sizeof(*v))) == NULL)
535
0
                goto err;
536
362k
            v->name = OPENSSL_strdup(pname);
537
362k
            v->value = NULL;
538
362k
            if (v->name == NULL)
539
0
                goto err;
540
362k
            if (!str_copy(conf, psection, &(v->value), start))
541
1.66k
                goto err;
542
543
360k
            if (strcmp(psection, section) != 0) {
544
70.4k
                if ((tv = _CONF_get_section(conf, psection))
545
70.4k
                    == NULL)
546
43.2k
                    tv = _CONF_new_section(conf, psection);
547
70.4k
                if (tv == NULL) {
548
0
                    ERR_raise(ERR_LIB_CONF,
549
0
                              CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
550
0
                    goto err;
551
0
                }
552
70.4k
            } else
553
290k
                tv = sv;
554
360k
            if (_CONF_add_string(conf, tv, v) == 0) {
555
0
                ERR_raise(ERR_LIB_CONF, ERR_R_CONF_LIB);
556
0
                goto err;
557
0
            }
558
360k
            v = NULL;
559
360k
        }
560
425k
    }
561
9.31k
    BUF_MEM_free(buff);
562
9.31k
    OPENSSL_free(section);
563
    /*
564
     * No need to pop, since we only get here if the stack is empty.
565
     * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE!
566
     */
567
9.31k
    sk_BIO_free(biosk);
568
9.31k
    return 1;
569
570
6.08k
 err:
571
6.08k
    BUF_MEM_free(buff);
572
6.08k
    OPENSSL_free(section);
573
    /*
574
     * Since |in| is the first element of the stack and should NOT be freed
575
     * here, we cannot use sk_BIO_pop_free().  Instead, we pop and free one
576
     * BIO at a time, making sure that the last one popped isn't.
577
     */
578
6.38k
    while (sk_BIO_num(biosk) > 0) {
579
300
        BIO *popped = sk_BIO_pop(biosk);
580
300
        BIO_vfree(in);
581
300
        in = popped;
582
300
    }
583
6.08k
    sk_BIO_free(biosk);
584
6.08k
#ifndef OPENSSL_NO_POSIX_IO
585
6.08k
    OPENSSL_free(dirpath);
586
6.08k
    if (dirctx != NULL)
587
273
        OPENSSL_DIR_end(&dirctx);
588
6.08k
#endif
589
6.08k
    if (line != NULL)
590
6.08k
        *line = eline;
591
6.08k
    BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
592
6.08k
    ERR_add_error_data(2, "line ", btmp);
593
6.08k
    if (h != conf->data) {
594
6.08k
        CONF_free(conf->data);
595
6.08k
        conf->data = NULL;
596
6.08k
    }
597
6.08k
    if (v != NULL) {
598
1.66k
        OPENSSL_free(v->name);
599
1.66k
        OPENSSL_free(v->value);
600
1.66k
        OPENSSL_free(v);
601
1.66k
    }
602
6.08k
    return 0;
603
15.4k
}
604
605
static void clear_comments(CONF *conf, char *p)
606
40.6M
{
607
40.7M
    for (;;) {
608
40.7M
        if (IS_FCOMMENT(conf, *p)) {
609
0
            *p = '\0';
610
0
            return;
611
0
        }
612
40.7M
        if (!IS_WS(conf, *p)) {
613
40.6M
            break;
614
40.6M
        }
615
85.3k
        p++;
616
85.3k
    }
617
618
246M
    for (;;) {
619
246M
        if (IS_COMMENT(conf, *p)) {
620
419k
            *p = '\0';
621
419k
            return;
622
419k
        }
623
245M
        if (IS_DQUOTE(conf, *p)) {
624
0
            p = scan_dquote(conf, p);
625
0
            continue;
626
0
        }
627
245M
        if (IS_QUOTE(conf, *p)) {
628
40.5k
            p = scan_quote(conf, p);
629
40.5k
            continue;
630
40.5k
        }
631
245M
        if (IS_ESC(conf, *p)) {
632
466k
            p = scan_esc(conf, p);
633
466k
            continue;
634
466k
        }
635
245M
        if (IS_EOF(conf, *p))
636
40.1M
            return;
637
205M
        else
638
205M
            p++;
639
245M
    }
640
40.6M
}
641
642
static int str_copy(CONF *conf, char *section, char **pto, char *from)
643
505k
{
644
505k
    int q, r, rr = 0, to = 0, len = 0;
645
505k
    char *s, *e, *rp, *p, *rrp, *np, *cp, v;
646
505k
    BUF_MEM *buf;
647
648
505k
    if ((buf = BUF_MEM_new()) == NULL)
649
0
        return 0;
650
651
505k
    len = strlen(from) + 1;
652
505k
    if (!BUF_MEM_grow(buf, len))
653
0
        goto err;
654
655
91.1M
    for (;;) {
656
91.1M
        if (IS_QUOTE(conf, *from)) {
657
32.6k
            q = *from;
658
32.6k
            from++;
659
23.3M
            while (!IS_EOF(conf, *from) && (*from != q)) {
660
23.3M
                if (IS_ESC(conf, *from)) {
661
108k
                    from++;
662
108k
                    if (IS_EOF(conf, *from))
663
3.09k
                        break;
664
108k
                }
665
23.2M
                buf->data[to++] = *(from++);
666
23.2M
            }
667
32.6k
            if (*from == q)
668
24.0k
                from++;
669
91.1M
        } else if (IS_DQUOTE(conf, *from)) {
670
0
            q = *from;
671
0
            from++;
672
0
            while (!IS_EOF(conf, *from)) {
673
0
                if (*from == q) {
674
0
                    if (*(from + 1) == q) {
675
0
                        from++;
676
0
                    } else {
677
0
                        break;
678
0
                    }
679
0
                }
680
0
                buf->data[to++] = *(from++);
681
0
            }
682
0
            if (*from == q)
683
0
                from++;
684
91.1M
        } else if (IS_ESC(conf, *from)) {
685
202k
            from++;
686
202k
            v = *(from++);
687
202k
            if (IS_EOF(conf, v))
688
10.9k
                break;
689
191k
            else if (v == 'r')
690
43.3k
                v = '\r';
691
148k
            else if (v == 'n')
692
7.02k
                v = '\n';
693
141k
            else if (v == 'b')
694
4.38k
                v = '\b';
695
136k
            else if (v == 't')
696
5.04k
                v = '\t';
697
191k
            buf->data[to++] = v;
698
90.9M
        } else if (IS_EOF(conf, *from))
699
492k
            break;
700
90.4M
        else if (*from == '$'
701
160k
                 && (!conf->flag_dollarid
702
66.2k
                     || from[1] == '{'
703
100k
                     || from[1] == '(')) {
704
100k
            size_t newsize;
705
706
            /* try to expand it */
707
100k
            rrp = NULL;
708
100k
            s = &(from[1]);
709
100k
            if (*s == '{')
710
5.64k
                q = '}';
711
94.9k
            else if (*s == '(')
712
3.67k
                q = ')';
713
91.3k
            else
714
91.3k
                q = 0;
715
716
100k
            if (q)
717
9.31k
                s++;
718
100k
            cp = section;
719
100k
            e = np = s;
720
16.4M
            while (IS_ALNUM(conf, *e)
721
101k
                   || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
722
16.3M
                e++;
723
100k
            if ((e[0] == ':') && (e[1] == ':')) {
724
4.68k
                cp = np;
725
4.68k
                rrp = e;
726
4.68k
                rr = *e;
727
4.68k
                *rrp = '\0';
728
4.68k
                e += 2;
729
4.68k
                np = e;
730
9.29k
                while (IS_ALNUM(conf, *e)
731
5.77k
                       || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
732
4.60k
                    e++;
733
4.68k
            }
734
100k
            r = *e;
735
100k
            *e = '\0';
736
100k
            rp = e;
737
100k
            if (q) {
738
9.31k
                if (r != q) {
739
351
                    ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE);
740
351
                    goto err;
741
351
                }
742
8.96k
                e++;
743
8.96k
            }
744
            /*-
745
             * So at this point we have
746
             * np which is the start of the name string which is
747
             *   '\0' terminated.
748
             * cp which is the start of the section string which is
749
             *   '\0' terminated.
750
             * e is the 'next point after'.
751
             * r and rr are the chars replaced by the '\0'
752
             * rp and rrp is where 'r' and 'rr' came from.
753
             */
754
100k
            p = _CONF_get_string(conf, cp, np);
755
100k
            if (rrp != NULL)
756
4.64k
                *rrp = rr;
757
100k
            *rp = r;
758
100k
            if (p == NULL) {
759
1.43k
                ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_HAS_NO_VALUE);
760
1.43k
                goto err;
761
1.43k
            }
762
98.8k
            newsize = strlen(p) + buf->length - (e - from);
763
98.8k
            if (newsize > MAX_CONF_VALUE_LENGTH) {
764
71
                ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
765
71
                goto err;
766
71
            }
767
98.7k
            if (!BUF_MEM_grow_clean(buf, newsize)) {
768
0
                ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
769
0
                goto err;
770
0
            }
771
29.2M
            while (*p)
772
29.1M
                buf->data[to++] = *(p++);
773
774
            /*
775
             * Since we change the pointer 'from', we also have to change the
776
             * perceived length of the string it points at.  /RL
777
             */
778
98.7k
            len -= e - from;
779
98.7k
            from = e;
780
781
            /*
782
             * In case there were no braces or parenthesis around the
783
             * variable reference, we have to put back the character that was
784
             * replaced with a '\0'.  /RL
785
             */
786
98.7k
            *rp = r;
787
98.7k
        } else
788
90.3M
            buf->data[to++] = *(from++);
789
91.1M
    }
790
503k
    buf->data[to] = '\0';
791
503k
    OPENSSL_free(*pto);
792
503k
    *pto = buf->data;
793
503k
    OPENSSL_free(buf);
794
503k
    return 1;
795
1.85k
 err:
796
1.85k
    BUF_MEM_free(buf);
797
1.85k
    return 0;
798
505k
}
799
800
#ifndef OPENSSL_NO_POSIX_IO
801
/*
802
 * Check whether included path is a directory.
803
 * Returns next BIO to process and in case of a directory
804
 * also an opened directory context and the include path.
805
 */
806
static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
807
                            char **dirpath)
808
6.70k
{
809
6.70k
    struct stat st;
810
6.70k
    BIO *next;
811
812
6.70k
    if (stat(include, &st) < 0) {
813
2.73k
        ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include);
814
        /* missing include file is not fatal error */
815
2.73k
        return NULL;
816
2.73k
    }
817
818
3.97k
    if (S_ISDIR(st.st_mode)) {
819
3.88k
        if (*dirctx != NULL) {
820
0
            ERR_raise_data(ERR_LIB_CONF, CONF_R_RECURSIVE_DIRECTORY_INCLUDE,
821
0
                           "%s", include);
822
0
            return NULL;
823
0
        }
824
        /* a directory, load its contents */
825
3.88k
        if ((next = get_next_file(include, dirctx)) != NULL)
826
2.60k
            *dirpath = include;
827
3.88k
        return next;
828
3.88k
    }
829
830
91
    next = BIO_new_file(include, "r");
831
91
    return next;
832
3.97k
}
833
834
/*
835
 * Get next file from the directory path.
836
 * Returns BIO of the next file to read and updates dirctx.
837
 */
838
static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx)
839
6.87k
{
840
6.87k
    const char *filename;
841
6.87k
    size_t pathlen;
842
843
6.87k
    pathlen = strlen(path);
844
223k
    while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
845
219k
        size_t namelen;
846
847
219k
        namelen = strlen(filename);
848
849
850
219k
        if ((namelen > 5
851
164k
             && OPENSSL_strcasecmp(filename + namelen - 5, ".conf") == 0)
852
209k
            || (namelen > 4
853
176k
                && OPENSSL_strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
854
11.4k
            size_t newlen;
855
11.4k
            char *newpath;
856
11.4k
            BIO *bio;
857
858
11.4k
            newlen = pathlen + namelen + 2;
859
11.4k
            newpath = OPENSSL_zalloc(newlen);
860
11.4k
            if (newpath == NULL)
861
0
                break;
862
#ifdef OPENSSL_SYS_VMS
863
            /*
864
             * If the given path isn't clear VMS syntax,
865
             * we treat it as on Unix.
866
             */
867
            if (path[pathlen - 1] == ']'
868
                || path[pathlen - 1] == '>'
869
                || path[pathlen - 1] == ':') {
870
                /* Clear VMS directory syntax, just copy as is */
871
                OPENSSL_strlcpy(newpath, path, newlen);
872
            }
873
#endif
874
11.4k
            if (newpath[0] == '\0') {
875
11.4k
                OPENSSL_strlcpy(newpath, path, newlen);
876
11.4k
                OPENSSL_strlcat(newpath, "/", newlen);
877
11.4k
            }
878
11.4k
            OPENSSL_strlcat(newpath, filename, newlen);
879
880
11.4k
            bio = BIO_new_file(newpath, "r");
881
11.4k
            OPENSSL_free(newpath);
882
            /* Errors when opening files are non-fatal. */
883
11.4k
            if (bio != NULL)
884
3.28k
                return bio;
885
11.4k
        }
886
219k
    }
887
3.59k
    OPENSSL_DIR_end(dirctx);
888
3.59k
    *dirctx = NULL;
889
3.59k
    return NULL;
890
6.87k
}
891
#endif
892
893
static int is_keytype(const CONF *conf, char c, unsigned short type)
894
2.21G
{
895
2.21G
    const unsigned short *keytypes = (const unsigned short *) conf->meth_data;
896
2.21G
    unsigned char key = (unsigned char)c;
897
898
#ifdef CHARSET_EBCDIC
899
# if CHAR_BIT > 8
900
    if (key > 255) {
901
        /* key is out of range for os_toascii table */
902
        return 0;
903
    }
904
# endif
905
    /* convert key from ebcdic to ascii */
906
    key = os_toascii[key];
907
#endif
908
909
2.21G
    if (key > 127) {
910
        /* key is not a seven bit ascii character */
911
110M
        return 0;
912
110M
    }
913
914
2.10G
    return (keytypes[key] & type) ? 1 : 0;
915
2.21G
}
916
917
static char *eat_ws(CONF *conf, char *p)
918
41.6M
{
919
42.2M
    while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
920
612k
        p++;
921
41.6M
    return p;
922
41.6M
}
923
924
static void trim_ws(CONF *conf, char *start)
925
490k
{
926
490k
    char *p = start;
927
928
152M
    while (!IS_EOF(conf, *p))
929
151M
        p++;
930
490k
    p--;
931
617k
    while ((p >= start) && IS_WS(conf, *p))
932
126k
        p--;
933
490k
    p++;
934
490k
    *p = '\0';
935
490k
}
936
937
static char *eat_alpha_numeric(CONF *conf, char *p)
938
614k
{
939
69.4M
    for (;;) {
940
69.4M
        if (IS_ESC(conf, *p)) {
941
109k
            p = scan_esc(conf, p);
942
109k
            continue;
943
109k
        }
944
69.3M
        if (!(IS_ALNUM_PUNCT(conf, *p)
945
617k
              || (conf->flag_dollarid && IS_DOLLAR(conf, *p))))
946
614k
            return p;
947
68.7M
        p++;
948
68.7M
    }
949
614k
}
950
951
static char *scan_quote(CONF *conf, char *p)
952
40.5k
{
953
40.5k
    int q = *p;
954
955
40.5k
    p++;
956
47.9M
    while (!(IS_EOF(conf, *p)) && (*p != q)) {
957
47.9M
        if (IS_ESC(conf, *p)) {
958
230k
            p++;
959
230k
            if (IS_EOF(conf, *p))
960
1.47k
                return p;
961
230k
        }
962
47.9M
        p++;
963
47.9M
    }
964
39.0k
    if (*p == q)
965
31.1k
        p++;
966
39.0k
    return p;
967
40.5k
}
968
969
static char *scan_dquote(CONF *conf, char *p)
970
0
{
971
0
    int q = *p;
972
973
0
    p++;
974
0
    while (!(IS_EOF(conf, *p))) {
975
0
        if (*p == q) {
976
0
            if (*(p + 1) == q) {
977
0
                p++;
978
0
            } else {
979
0
                break;
980
0
            }
981
0
        }
982
0
        p++;
983
0
    }
984
0
    if (*p == q)
985
0
        p++;
986
0
    return p;
987
0
}
988
989
static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
990
0
{
991
0
    if (a->name)
992
0
        BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
993
0
    else
994
0
        BIO_printf(out, "[[%s]]\n", a->section);
995
0
}
996
997
IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
998
999
static int def_dump(const CONF *conf, BIO *out)
1000
0
{
1001
0
    lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
1002
0
    return 1;
1003
0
}
1004
1005
static int def_is_number(const CONF *conf, char c)
1006
0
{
1007
0
    return IS_NUMBER(conf, c);
1008
0
}
1009
1010
static int def_to_int(const CONF *conf, char c)
1011
0
{
1012
0
    return c - '0';
1013
0
}