Coverage Report

Created: 2025-12-31 06:58

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
95.0k
#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.37M
#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.9k
{
88
16.9k
    return &default_method;
89
16.9k
}
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.9k
{
113
16.9k
    CONF *ret;
114
115
16.9k
    ret = OPENSSL_malloc(sizeof(*ret));
116
16.9k
    if (ret != NULL)
117
16.9k
        if (meth->init(ret) == 0) {
118
0
            OPENSSL_free(ret);
119
0
            ret = NULL;
120
0
        }
121
16.9k
    return ret;
122
16.9k
}
123
124
static int def_init_default(CONF *conf)
125
23.6k
{
126
23.6k
    if (conf == NULL)
127
0
        return 0;
128
129
23.6k
    memset(conf, 0, sizeof(*conf));
130
23.6k
    conf->meth = &default_method;
131
23.6k
    conf->meth_data = (void *)CONF_type_default;
132
133
23.6k
    return 1;
134
23.6k
}
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.9k
{
152
16.9k
    if (def_destroy_data(conf)) {
153
16.9k
        OPENSSL_free(conf);
154
16.9k
        return 1;
155
16.9k
    }
156
0
    return 0;
157
16.9k
}
158
159
static int def_destroy_data(CONF *conf)
160
23.6k
{
161
23.6k
    if (conf == NULL)
162
0
        return 0;
163
23.6k
    _CONF_free_data(conf);
164
23.6k
    return 1;
165
23.6k
}
166
167
static int def_load(CONF *conf, const char *name, long *line)
168
185
{
169
185
    int ret;
170
185
    BIO *in = NULL;
171
172
#ifdef OPENSSL_SYS_VMS
173
    in = BIO_new_file(name, "r");
174
#else
175
185
    in = BIO_new_file(name, "rb");
176
185
#endif
177
185
    if (in == NULL) {
178
185
        if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
179
185
            ERR_raise(ERR_LIB_CONF, CONF_R_NO_SUCH_FILE);
180
0
        else
181
185
            ERR_raise(ERR_LIB_CONF, ERR_R_SYS_LIB);
182
185
        return 0;
183
185
    }
184
185
0
    ret = def_load_bio(conf, in, line);
186
0
    BIO_free(in);
187
188
0
    return ret;
189
185
}
190
191
/* Parse a boolean value and fill in *flag. Return 0 on error. */
192
static int parsebool(const char *pval, int *flag)
193
8.51k
{
194
8.51k
    if (OPENSSL_strcasecmp(pval, "on") == 0
195
5.64k
        || OPENSSL_strcasecmp(pval, "true") == 0) {
196
5.64k
        *flag = 1;
197
5.64k
    } else if (OPENSSL_strcasecmp(pval, "off") == 0
198
2.61k
        || OPENSSL_strcasecmp(pval, "false") == 0) {
199
2.61k
        *flag = 0;
200
2.61k
    } else {
201
263
        ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
202
263
        return 0;
203
263
    }
204
8.25k
    return 1;
205
8.51k
}
206
207
static int def_load_bio(CONF *conf, BIO *in, long *line)
208
15.7k
{
209
/* The macro BUFSIZE conflicts with a system macro in VxWorks */
210
89.5M
#define CONFBUFSIZE 512
211
15.7k
    int bufnum = 0, i, ii;
212
15.7k
    BUF_MEM *buff = NULL;
213
15.7k
    char *s, *p, *end;
214
15.7k
    int again;
215
15.7k
    int first_call = 1;
216
15.7k
    long eline = 0;
217
15.7k
    char btmp[DECIMAL_SIZE(eline) + 1];
218
15.7k
    CONF_VALUE *v = NULL, *tv;
219
15.7k
    CONF_VALUE *sv = NULL;
220
15.7k
    char *section = NULL, *buf;
221
15.7k
    char *start, *psection, *pname;
222
15.7k
    void *h = (void *)(conf->data);
223
15.7k
    STACK_OF(BIO) *biosk = NULL;
224
15.7k
#ifndef OPENSSL_NO_POSIX_IO
225
15.7k
    char *dirpath = NULL;
226
15.7k
    OPENSSL_DIR_CTX *dirctx = NULL;
227
15.7k
#endif
228
15.7k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
229
15.7k
    int numincludes = 0;
230
15.7k
#endif
231
232
15.7k
    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
15.7k
    section = OPENSSL_strdup("default");
238
15.7k
    if (section == NULL)
239
0
        goto err;
240
241
15.7k
    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
15.7k
    sv = _CONF_new_section(conf, section);
247
15.7k
    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
15.7k
    bufnum = 0;
253
15.7k
    again = 0;
254
29.8M
    for (;;) {
255
29.8M
        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
29.8M
        p = &(buff->data[bufnum]);
260
29.8M
        *p = '\0';
261
29.8M
    read_retry:
262
29.8M
        if (in != NULL && BIO_gets(in, p, CONFBUFSIZE - 1) < 0)
263
0
            goto err;
264
29.8M
        p[CONFBUFSIZE - 1] = '\0';
265
29.8M
        ii = i = strlen(p);
266
29.8M
        if (first_call) {
267
            /* Other BOMs imply unsupported multibyte encoding,
268
             * so don't strip them and let the error raise */
269
15.7k
            const unsigned char utf8_bom[3] = { 0xEF, 0xBB, 0xBF };
270
271
15.7k
            if (i >= 3 && memcmp(p, utf8_bom, 3) == 0) {
272
133
                memmove(p, p + 3, i - 3);
273
133
                p[i - 3] = 0;
274
133
                i -= 3;
275
133
                ii -= 3;
276
133
            }
277
15.7k
            first_call = 0;
278
15.7k
        }
279
29.8M
        if (i == 0 && !again) {
280
            /* the currently processed BIO is NULL or at EOF */
281
11.1k
            BIO *parent;
282
283
11.1k
#ifndef OPENSSL_NO_POSIX_IO
284
            /* continue processing with the next file from directory */
285
11.1k
            if (dirctx != NULL) {
286
1.67k
                BIO *next;
287
288
1.67k
                if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
289
534
                    BIO_vfree(in);
290
534
                    in = next;
291
534
                    goto read_retry;
292
1.14k
                } else {
293
1.14k
                    OPENSSL_free(dirpath);
294
1.14k
                    dirpath = NULL;
295
1.14k
                }
296
1.67k
            }
297
10.6k
#endif
298
            /* no more files in directory, continue with processing parent */
299
10.6k
            if ((parent = sk_BIO_pop(biosk)) == NULL) {
300
                /* everything processed get out of the loop */
301
9.46k
                break;
302
9.46k
            } else {
303
1.14k
                BIO_vfree(in);
304
1.14k
                in = parent;
305
1.14k
                goto read_retry;
306
1.14k
            }
307
10.6k
        }
308
29.8M
        again = 0;
309
59.0M
        while (i > 0) {
310
30.2M
            if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
311
1.04M
                break;
312
29.2M
            else
313
29.2M
                i--;
314
30.2M
        }
315
        /*
316
         * we removed some trailing stuff so there is a new line on the end.
317
         */
318
29.8M
        if (ii && i == ii)
319
558k
            again = 1; /* long line */
320
29.2M
        else {
321
29.2M
            p[i] = '\0';
322
29.2M
            eline++; /* another input line */
323
29.2M
        }
324
325
        /* we now have a line with trailing \r\n removed */
326
327
        /* i is the number of bytes */
328
29.8M
        bufnum += i;
329
330
29.8M
        v = NULL;
331
        /* check for line continuation */
332
29.8M
        if (!again && bufnum >= 1) {
333
            /*
334
             * If we have bytes and the last char '\\' and second last char
335
             * is not '\\'
336
             */
337
502k
            p = &(buff->data[bufnum - 1]);
338
502k
            if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
339
18.2k
                bufnum--;
340
18.2k
                again = 1;
341
18.2k
            }
342
502k
        }
343
29.8M
        if (again)
344
577k
            continue;
345
29.2M
        bufnum = 0;
346
29.2M
        buf = buff->data;
347
348
29.2M
        clear_comments(conf, buf);
349
29.2M
        s = eat_ws(conf, buf);
350
29.2M
        if (IS_EOF(conf, *s))
351
28.9M
            continue; /* blank line */
352
321k
        if (*s == '[') {
353
29.1k
            char *ss;
354
355
29.1k
            s++;
356
29.1k
            start = eat_ws(conf, s);
357
29.1k
            ss = start;
358
33.2k
        again:
359
33.2k
            end = eat_alpha_numeric(conf, ss);
360
33.2k
            p = eat_ws(conf, end);
361
33.2k
            if (*p != ']') {
362
4.72k
                if (*p != '\0' && ss != p) {
363
4.11k
                    ss = p;
364
4.11k
                    goto again;
365
4.11k
                }
366
4.72k
                ERR_raise(ERR_LIB_CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
367
616
                goto err;
368
4.72k
            }
369
28.5k
            *end = '\0';
370
28.5k
            if (!str_copy(conf, NULL, &section, start))
371
0
                goto err;
372
28.5k
            if ((sv = _CONF_get_section(conf, section)) == NULL)
373
18.3k
                sv = _CONF_new_section(conf, section);
374
28.5k
            if (sv == NULL) {
375
0
                ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
376
0
                goto err;
377
0
            }
378
28.5k
            continue;
379
292k
        } else {
380
292k
            pname = s;
381
292k
            end = eat_alpha_numeric(conf, s);
382
292k
            if ((end[0] == ':') && (end[1] == ':')) {
383
55.0k
                *end = '\0';
384
55.0k
                end += 2;
385
55.0k
                psection = pname;
386
55.0k
                pname = end;
387
55.0k
                end = eat_alpha_numeric(conf, end);
388
237k
            } else {
389
237k
                psection = section;
390
237k
            }
391
292k
            p = eat_ws(conf, end);
392
292k
            if (CHECK_AND_SKIP_PREFIX(pname, ".pragma")
393
16.3k
                && (p != pname || *p == '=')) {
394
16.3k
                char *pval;
395
396
16.3k
                if (*p == '=') {
397
10.2k
                    p++;
398
10.2k
                    p = eat_ws(conf, p);
399
10.2k
                }
400
16.3k
                trim_ws(conf, p);
401
402
                /* Pragma values take the form keyword:value */
403
16.3k
                pval = strchr(p, ':');
404
16.3k
                if (pval == NULL || pval == p || pval[1] == '\0') {
405
248
                    ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
406
248
                    goto err;
407
248
                }
408
409
16.1k
                *pval++ = '\0';
410
16.1k
                trim_ws(conf, p);
411
16.1k
                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
16.1k
                if (strcmp(p, "dollarid") == 0) {
421
5.07k
                    if (!parsebool(pval, &conf->flag_dollarid))
422
47
                        goto err;
423
11.0k
                } else if (strcmp(p, "abspath") == 0) {
424
2.21k
                    if (!parsebool(pval, &conf->flag_abspath))
425
208
                        goto err;
426
8.80k
                } else if (strcmp(p, "includedir") == 0) {
427
1.65k
                    OPENSSL_free(conf->includedir);
428
1.65k
                    if ((conf->includedir = OPENSSL_strdup(pval)) == NULL)
429
0
                        goto err;
430
1.65k
                }
431
432
                /*
433
                 * We *ignore* any unknown pragma.
434
                 */
435
15.8k
                continue;
436
276k
            } else if (CHECK_AND_SKIP_PREFIX(pname, ".include")
437
5.19k
                && (p != pname || *p == '=')) {
438
5.15k
                char *include = NULL;
439
5.15k
                BIO *next;
440
5.15k
                const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE");
441
5.15k
                char *include_path = NULL;
442
443
5.15k
#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.15k
                if (numincludes++ > 10)
454
28
                    goto err;
455
5.12k
#endif
456
457
5.12k
                if (include_dir == NULL)
458
5.12k
                    include_dir = conf->includedir;
459
460
5.12k
                if (*p == '=') {
461
1.11k
                    p++;
462
1.11k
                    p = eat_ws(conf, p);
463
1.11k
                }
464
5.12k
                trim_ws(conf, p);
465
5.12k
                if (!str_copy(conf, psection, &include, p))
466
15
                    goto err;
467
468
5.11k
                if (include_dir != NULL && !ossl_is_absolute_path(include)) {
469
987
                    size_t newlen = strlen(include_dir) + strlen(include) + 2;
470
471
987
                    include_path = OPENSSL_malloc(newlen);
472
987
                    if (include_path == NULL) {
473
0
                        OPENSSL_free(include);
474
0
                        goto err;
475
0
                    }
476
477
987
                    OPENSSL_strlcpy(include_path, include_dir, newlen);
478
987
                    if (!ossl_ends_with_dirsep(include_path))
479
845
                        OPENSSL_strlcat(include_path, "/", newlen);
480
987
                    OPENSSL_strlcat(include_path, include, newlen);
481
987
                    OPENSSL_free(include);
482
4.12k
                } else {
483
4.12k
                    include_path = include;
484
4.12k
                }
485
486
5.11k
                if (conf->flag_abspath
487
154
                    && !ossl_is_absolute_path(include_path)) {
488
45
                    ERR_raise(ERR_LIB_CONF, CONF_R_RELATIVE_PATH);
489
45
                    OPENSSL_free(include_path);
490
45
                    goto err;
491
45
                }
492
493
                /* get the BIO of the included file */
494
5.06k
#ifndef OPENSSL_NO_POSIX_IO
495
5.06k
                next = process_include(include_path, &dirctx, &dirpath);
496
5.06k
                if (include_path != dirpath) {
497
                    /* dirpath will contain include in case of a directory */
498
3.73k
                    OPENSSL_free(include_path);
499
3.73k
                }
500
#else
501
                next = BIO_new_file(include_path, "r");
502
                OPENSSL_free(include_path);
503
#endif
504
505
5.06k
                if (next != NULL) {
506
                    /* push the currently processing BIO onto stack */
507
1.34k
                    if (biosk == NULL) {
508
584
                        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
584
                    }
514
1.34k
                    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.34k
                    in = next;
521
1.34k
                }
522
5.06k
                continue;
523
271k
            } else if (*p != '=') {
524
3.34k
                ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN,
525
3.34k
                    "HERE-->%s", p);
526
3.34k
                goto err;
527
3.34k
            }
528
267k
            *end = '\0';
529
267k
            p++;
530
267k
            start = eat_ws(conf, p);
531
267k
            trim_ws(conf, start);
532
533
267k
            if ((v = OPENSSL_malloc(sizeof(*v))) == NULL)
534
0
                goto err;
535
267k
            v->name = OPENSSL_strdup(pname);
536
267k
            v->value = NULL;
537
267k
            if (v->name == NULL)
538
0
                goto err;
539
267k
            if (!str_copy(conf, psection, &(v->value), start))
540
1.72k
                goto err;
541
542
266k
            if (strcmp(psection, section) != 0) {
543
54.7k
                if ((tv = _CONF_get_section(conf, psection))
544
54.7k
                    == NULL)
545
43.9k
                    tv = _CONF_new_section(conf, psection);
546
54.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
54.7k
            } else
552
211k
                tv = sv;
553
266k
            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
266k
            v = NULL;
558
266k
        }
559
321k
    }
560
9.46k
    BUF_MEM_free(buff);
561
9.46k
    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
9.46k
    sk_BIO_free(biosk);
567
9.46k
    return 1;
568
569
6.27k
err:
570
6.27k
    BUF_MEM_free(buff);
571
6.27k
    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
6.47k
    while (sk_BIO_num(biosk) > 0) {
578
202
        BIO *popped = sk_BIO_pop(biosk);
579
202
        BIO_vfree(in);
580
202
        in = popped;
581
202
    }
582
6.27k
    sk_BIO_free(biosk);
583
6.27k
#ifndef OPENSSL_NO_POSIX_IO
584
6.27k
    OPENSSL_free(dirpath);
585
6.27k
    if (dirctx != NULL)
586
186
        OPENSSL_DIR_end(&dirctx);
587
6.27k
#endif
588
6.27k
    if (line != NULL)
589
6.27k
        *line = eline;
590
6.27k
    BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
591
6.27k
    ERR_add_error_data(2, "line ", btmp);
592
6.27k
    if (h != conf->data) {
593
6.27k
        CONF_free(conf->data);
594
6.27k
        conf->data = NULL;
595
6.27k
    }
596
6.27k
    if (v != NULL) {
597
1.72k
        OPENSSL_free(v->name);
598
1.72k
        OPENSSL_free(v->value);
599
1.72k
        OPENSSL_free(v);
600
1.72k
    }
601
6.27k
    return 0;
602
15.7k
}
603
604
static void clear_comments(CONF *conf, char *p)
605
37.6M
{
606
37.7M
    for (;;) {
607
37.7M
        if (IS_FCOMMENT(conf, *p)) {
608
0
            *p = '\0';
609
0
            return;
610
0
        }
611
37.7M
        if (!IS_WS(conf, *p)) {
612
37.6M
            break;
613
37.6M
        }
614
60.7k
        p++;
615
60.7k
    }
616
617
311M
    for (;;) {
618
311M
        if (IS_COMMENT(conf, *p)) {
619
309k
            *p = '\0';
620
309k
            return;
621
309k
        }
622
311M
        if (IS_DQUOTE(conf, *p)) {
623
0
            p = scan_dquote(conf, p);
624
0
            continue;
625
0
        }
626
311M
        if (IS_QUOTE(conf, *p)) {
627
42.0k
            p = scan_quote(conf, p);
628
42.0k
            continue;
629
42.0k
        }
630
311M
        if (IS_ESC(conf, *p)) {
631
871k
            p = scan_esc(conf, p);
632
871k
            continue;
633
871k
        }
634
310M
        if (IS_EOF(conf, *p))
635
37.3M
            return;
636
273M
        else
637
273M
            p++;
638
310M
    }
639
37.6M
}
640
641
static int str_copy(CONF *conf, char *section, char **pto, char *from)
642
400k
{
643
400k
    int q, r, rr = 0, to = 0, len = 0;
644
400k
    char *s, *e, *rp, *p, *rrp, *np, *cp, v;
645
400k
    BUF_MEM *buf;
646
647
400k
    if ((buf = BUF_MEM_new()) == NULL)
648
0
        return 0;
649
650
400k
    len = strlen(from) + 1;
651
400k
    if (!BUF_MEM_grow(buf, len))
652
0
        goto err;
653
654
87.2M
    for (;;) {
655
87.2M
        if (IS_QUOTE(conf, *from)) {
656
32.8k
            q = *from;
657
32.8k
            from++;
658
26.9M
            while (!IS_EOF(conf, *from) && (*from != q)) {
659
26.9M
                if (IS_ESC(conf, *from)) {
660
91.4k
                    from++;
661
91.4k
                    if (IS_EOF(conf, *from))
662
2.84k
                        break;
663
91.4k
                }
664
26.9M
                buf->data[to++] = *(from++);
665
26.9M
            }
666
32.8k
            if (*from == q)
667
25.6k
                from++;
668
87.2M
        } else if (IS_DQUOTE(conf, *from)) {
669
0
            q = *from;
670
0
            from++;
671
0
            while (!IS_EOF(conf, *from)) {
672
0
                if (*from == q) {
673
0
                    if (*(from + 1) == q) {
674
0
                        from++;
675
0
                    } else {
676
0
                        break;
677
0
                    }
678
0
                }
679
0
                buf->data[to++] = *(from++);
680
0
            }
681
0
            if (*from == q)
682
0
                from++;
683
87.2M
        } else if (IS_ESC(conf, *from)) {
684
289k
            from++;
685
289k
            v = *(from++);
686
289k
            if (IS_EOF(conf, v))
687
10.6k
                break;
688
278k
            else if (v == 'r')
689
60.4k
                v = '\r';
690
218k
            else if (v == 'n')
691
10.9k
                v = '\n';
692
207k
            else if (v == 'b')
693
5.79k
                v = '\b';
694
201k
            else if (v == 't')
695
4.65k
                v = '\t';
696
278k
            buf->data[to++] = v;
697
86.9M
        } else if (IS_EOF(conf, *from))
698
388k
            break;
699
86.5M
        else if (*from == '$'
700
143k
            && (!conf->flag_dollarid
701
51.8k
                || from[1] == '{'
702
96.8k
                || from[1] == '(')) {
703
96.8k
            size_t newsize;
704
705
            /* try to expand it */
706
96.8k
            rrp = NULL;
707
96.8k
            s = &(from[1]);
708
96.8k
            if (*s == '{')
709
4.65k
                q = '}';
710
92.2k
            else if (*s == '(')
711
3.60k
                q = ')';
712
88.6k
            else
713
88.6k
                q = 0;
714
715
96.8k
            if (q)
716
8.25k
                s++;
717
96.8k
            cp = section;
718
96.8k
            e = np = s;
719
19.6M
            while (IS_ALNUM(conf, *e)
720
97.9k
                || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
721
19.5M
                e++;
722
96.8k
            if ((e[0] == ':') && (e[1] == ':')) {
723
5.95k
                cp = np;
724
5.95k
                rrp = e;
725
5.95k
                rr = *e;
726
5.95k
                *rrp = '\0';
727
5.95k
                e += 2;
728
5.95k
                np = e;
729
11.9k
                while (IS_ALNUM(conf, *e)
730
7.03k
                    || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
731
6.01k
                    e++;
732
5.95k
            }
733
96.8k
            r = *e;
734
96.8k
            *e = '\0';
735
96.8k
            rp = e;
736
96.8k
            if (q) {
737
8.25k
                if (r != q) {
738
351
                    ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE);
739
351
                    goto err;
740
351
                }
741
7.90k
                e++;
742
7.90k
            }
743
            /*-
744
             * So at this point we have
745
             * np which is the start of the name string which is
746
             *   '\0' terminated.
747
             * cp which is the start of the section string which is
748
             *   '\0' terminated.
749
             * e is the 'next point after'.
750
             * r and rr are the chars replaced by the '\0'
751
             * rp and rrp is where 'r' and 'rr' came from.
752
             */
753
96.5k
            p = _CONF_get_string(conf, cp, np);
754
96.5k
            if (rrp != NULL)
755
5.90k
                *rrp = rr;
756
96.5k
            *rp = r;
757
96.5k
            if (p == NULL) {
758
1.45k
                ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_HAS_NO_VALUE);
759
1.45k
                goto err;
760
1.45k
            }
761
95.0k
            newsize = strlen(p) + buf->length - (e - from);
762
95.0k
            if (newsize > MAX_CONF_VALUE_LENGTH) {
763
91
                ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
764
91
                goto err;
765
91
            }
766
94.9k
            if (!BUF_MEM_grow_clean(buf, newsize)) {
767
0
                ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
768
0
                goto err;
769
0
            }
770
33.2M
            while (*p)
771
33.1M
                buf->data[to++] = *(p++);
772
773
            /*
774
             * Since we change the pointer 'from', we also have to change the
775
             * perceived length of the string it points at.  /RL
776
             */
777
94.9k
            len -= e - from;
778
94.9k
            from = e;
779
780
            /*
781
             * In case there were no braces or parenthesis around the
782
             * variable reference, we have to put back the character that was
783
             * replaced with a '\0'.  /RL
784
             */
785
94.9k
            *rp = r;
786
94.9k
        } else
787
86.4M
            buf->data[to++] = *(from++);
788
87.2M
    }
789
398k
    buf->data[to] = '\0';
790
398k
    OPENSSL_free(*pto);
791
398k
    *pto = buf->data;
792
398k
    OPENSSL_free(buf);
793
398k
    return 1;
794
1.89k
err:
795
1.89k
    BUF_MEM_free(buf);
796
1.89k
    return 0;
797
400k
}
798
799
#ifndef OPENSSL_NO_POSIX_IO
800
/*
801
 * Check whether included path is a directory.
802
 * Returns next BIO to process and in case of a directory
803
 * also an opened directory context and the include path.
804
 */
805
static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
806
    char **dirpath)
807
6.27k
{
808
6.27k
    struct stat st;
809
6.27k
    BIO *next;
810
811
6.27k
    if (stat(include, &st) < 0) {
812
2.79k
        ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include);
813
        /* missing include file is not fatal error */
814
2.79k
        return NULL;
815
2.79k
    }
816
817
3.48k
    if (S_ISDIR(st.st_mode)) {
818
3.40k
        if (*dirctx != NULL) {
819
0
            ERR_raise_data(ERR_LIB_CONF, CONF_R_RECURSIVE_DIRECTORY_INCLUDE,
820
0
                "%s", include);
821
0
            return NULL;
822
0
        }
823
        /* a directory, load its contents */
824
3.40k
        if ((next = get_next_file(include, dirctx)) != NULL)
825
2.01k
            *dirpath = include;
826
3.40k
        return next;
827
3.40k
    }
828
829
71
    next = BIO_new_file(include, "r");
830
71
    return next;
831
3.48k
}
832
833
/*
834
 * Get next file from the directory path.
835
 * Returns BIO of the next file to read and updates dirctx.
836
 */
837
static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx)
838
5.84k
{
839
5.84k
    const char *filename;
840
5.84k
    size_t pathlen;
841
842
5.84k
    pathlen = strlen(path);
843
245k
    while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
844
242k
        size_t namelen;
845
846
242k
        namelen = strlen(filename);
847
848
242k
        if ((namelen > 5
849
187k
                && OPENSSL_strcasecmp(filename + namelen - 5, ".conf") == 0)
850
232k
            || (namelen > 4
851
198k
                && OPENSSL_strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
852
11.2k
            size_t newlen;
853
11.2k
            char *newpath;
854
11.2k
            BIO *bio;
855
856
11.2k
            newlen = pathlen + namelen + 2;
857
11.2k
            newpath = OPENSSL_zalloc(newlen);
858
11.2k
            if (newpath == NULL)
859
0
                break;
860
#ifdef OPENSSL_SYS_VMS
861
            /*
862
             * If the given path isn't clear VMS syntax,
863
             * we treat it as on Unix.
864
             */
865
            if (path[pathlen - 1] == ']'
866
                || path[pathlen - 1] == '>'
867
                || path[pathlen - 1] == ':') {
868
                /* Clear VMS directory syntax, just copy as is */
869
                OPENSSL_strlcpy(newpath, path, newlen);
870
            }
871
#endif
872
11.2k
            if (newpath[0] == '\0') {
873
11.2k
                OPENSSL_strlcpy(newpath, path, newlen);
874
11.2k
                OPENSSL_strlcat(newpath, "/", newlen);
875
11.2k
            }
876
11.2k
            OPENSSL_strlcat(newpath, filename, newlen);
877
878
11.2k
            bio = BIO_new_file(newpath, "r");
879
11.2k
            OPENSSL_free(newpath);
880
            /* Errors when opening files are non-fatal. */
881
11.2k
            if (bio != NULL)
882
2.63k
                return bio;
883
11.2k
        }
884
242k
    }
885
3.21k
    OPENSSL_DIR_end(dirctx);
886
3.21k
    *dirctx = NULL;
887
3.21k
    return NULL;
888
5.84k
}
889
#endif
890
891
static int is_keytype(const CONF *conf, char c, unsigned short type)
892
2.63G
{
893
2.63G
    const unsigned short *keytypes = (const unsigned short *)conf->meth_data;
894
2.63G
    unsigned char key = (unsigned char)c;
895
896
#ifdef CHARSET_EBCDIC
897
#if CHAR_BIT > 8
898
    if (key > 255) {
899
        /* key is out of range for os_toascii table */
900
        return 0;
901
    }
902
#endif
903
    /* convert key from ebcdic to ascii */
904
    key = os_toascii[key];
905
#endif
906
907
2.63G
    if (key > 127) {
908
        /* key is not a seven bit ascii character */
909
162M
        return 0;
910
162M
    }
911
912
2.47G
    return (keytypes[key] & type) ? 1 : 0;
913
2.63G
}
914
915
static char *eat_ws(CONF *conf, char *p)
916
38.4M
{
917
38.9M
    while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
918
443k
        p++;
919
38.4M
    return p;
920
38.4M
}
921
922
static void trim_ws(CONF *conf, char *start)
923
398k
{
924
398k
    char *p = start;
925
926
158M
    while (!IS_EOF(conf, *p))
927
158M
        p++;
928
398k
    p--;
929
489k
    while ((p >= start) && IS_WS(conf, *p))
930
90.8k
        p--;
931
398k
    p++;
932
398k
    *p = '\0';
933
398k
}
934
935
static char *eat_alpha_numeric(CONF *conf, char *p)
936
493k
{
937
121M
    for (;;) {
938
121M
        if (IS_ESC(conf, *p)) {
939
498k
            p = scan_esc(conf, p);
940
498k
            continue;
941
498k
        }
942
120M
        if (!(IS_ALNUM_PUNCT(conf, *p)
943
496k
                || (conf->flag_dollarid && IS_DOLLAR(conf, *p))))
944
493k
            return p;
945
120M
        p++;
946
120M
    }
947
493k
}
948
949
static char *scan_quote(CONF *conf, char *p)
950
42.0k
{
951
42.0k
    int q = *p;
952
953
42.0k
    p++;
954
50.3M
    while (!(IS_EOF(conf, *p)) && (*p != q)) {
955
50.3M
        if (IS_ESC(conf, *p)) {
956
167k
            p++;
957
167k
            if (IS_EOF(conf, *p))
958
1.75k
                return p;
959
167k
        }
960
50.3M
        p++;
961
50.3M
    }
962
40.2k
    if (*p == q)
963
33.6k
        p++;
964
40.2k
    return p;
965
42.0k
}
966
967
static char *scan_dquote(CONF *conf, char *p)
968
0
{
969
0
    int q = *p;
970
971
0
    p++;
972
0
    while (!(IS_EOF(conf, *p))) {
973
0
        if (*p == q) {
974
0
            if (*(p + 1) == q) {
975
0
                p++;
976
0
            } else {
977
0
                break;
978
0
            }
979
0
        }
980
0
        p++;
981
0
    }
982
0
    if (*p == q)
983
0
        p++;
984
0
    return p;
985
0
}
986
987
static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
988
0
{
989
0
    if (a->name)
990
0
        BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
991
0
    else
992
0
        BIO_printf(out, "[[%s]]\n", a->section);
993
0
}
994
995
IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
996
997
static int def_dump(const CONF *conf, BIO *out)
998
0
{
999
0
    lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
1000
0
    return 1;
1001
0
}
1002
1003
static int def_is_number(const CONF *conf, char c)
1004
0
{
1005
0
    return IS_NUMBER(conf, c);
1006
0
}
1007
1008
static int def_to_int(const CONF *conf, char c)
1009
0
{
1010
0
    return c - '0';
1011
0
}