Coverage Report

Created: 2025-12-04 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/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
108k
#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.43M
#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
19.1k
{
88
19.1k
    return &default_method;
89
19.1k
}
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
19.1k
{
113
19.1k
    CONF *ret;
114
115
19.1k
    ret = OPENSSL_malloc(sizeof(*ret));
116
19.1k
    if (ret != NULL)
117
19.1k
        if (meth->init(ret) == 0) {
118
0
            OPENSSL_free(ret);
119
0
            ret = NULL;
120
0
        }
121
19.1k
    return ret;
122
19.1k
}
123
124
static int def_init_default(CONF *conf)
125
26.8k
{
126
26.8k
    if (conf == NULL)
127
0
        return 0;
128
129
26.8k
    memset(conf, 0, sizeof(*conf));
130
26.8k
    conf->meth = &default_method;
131
26.8k
    conf->meth_data = (void *)CONF_type_default;
132
133
26.8k
    return 1;
134
26.8k
}
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
19.1k
{
152
19.1k
    if (def_destroy_data(conf)) {
153
19.1k
        OPENSSL_free(conf);
154
19.1k
        return 1;
155
19.1k
    }
156
0
    return 0;
157
19.1k
}
158
159
static int def_destroy_data(CONF *conf)
160
26.8k
{
161
26.8k
    if (conf == NULL)
162
0
        return 0;
163
26.8k
    _CONF_free_data(conf);
164
26.8k
    return 1;
165
26.8k
}
166
167
static int def_load(CONF *conf, const char *name, long *line)
168
204
{
169
204
    int ret;
170
204
    BIO *in = NULL;
171
172
#ifdef OPENSSL_SYS_VMS
173
    in = BIO_new_file(name, "r");
174
#else
175
204
    in = BIO_new_file(name, "rb");
176
204
#endif
177
204
    if (in == NULL) {
178
204
        if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
179
204
            ERR_raise(ERR_LIB_CONF, CONF_R_NO_SUCH_FILE);
180
0
        else
181
204
            ERR_raise(ERR_LIB_CONF, ERR_R_SYS_LIB);
182
204
        return 0;
183
204
    }
184
185
0
    ret = def_load_bio(conf, in, line);
186
0
    BIO_free(in);
187
188
0
    return ret;
189
204
}
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
8.71k
{
195
8.71k
    if (OPENSSL_strcasecmp(pval, "on") == 0
196
6.26k
        || OPENSSL_strcasecmp(pval, "true") == 0) {
197
6.26k
        *flag = 1;
198
6.26k
    } else if (OPENSSL_strcasecmp(pval, "off") == 0
199
2.16k
               || OPENSSL_strcasecmp(pval, "false") == 0) {
200
2.16k
        *flag = 0;
201
2.16k
    } else {
202
285
        ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
203
285
        return 0;
204
285
    }
205
8.43k
    return 1;
206
8.71k
}
207
208
static int def_load_bio(CONF *conf, BIO *in, long *line)
209
17.8k
{
210
/* The macro BUFSIZE conflicts with a system macro in VxWorks */
211
103M
#define CONFBUFSIZE     512
212
17.8k
    int bufnum = 0, i, ii;
213
17.8k
    BUF_MEM *buff = NULL;
214
17.8k
    char *s, *p, *end;
215
17.8k
    int again;
216
17.8k
    int first_call = 1;
217
17.8k
    long eline = 0;
218
17.8k
    char btmp[DECIMAL_SIZE(eline) + 1];
219
17.8k
    CONF_VALUE *v = NULL, *tv;
220
17.8k
    CONF_VALUE *sv = NULL;
221
17.8k
    char *section = NULL, *buf;
222
17.8k
    char *start, *psection, *pname;
223
17.8k
    void *h = (void *)(conf->data);
224
17.8k
    STACK_OF(BIO) *biosk = NULL;
225
17.8k
#ifndef OPENSSL_NO_POSIX_IO
226
17.8k
    char *dirpath = NULL;
227
17.8k
    OPENSSL_DIR_CTX *dirctx = NULL;
228
17.8k
#endif
229
17.8k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
230
17.8k
    int numincludes = 0;
231
17.8k
#endif
232
233
17.8k
    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
17.8k
    section = OPENSSL_strdup("default");
239
17.8k
    if (section == NULL)
240
0
        goto err;
241
242
17.8k
    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
17.8k
    sv = _CONF_new_section(conf, section);
248
17.8k
    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
17.8k
    bufnum = 0;
254
17.8k
    again = 0;
255
34.5M
    for (;;) {
256
34.5M
        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
34.5M
        p = &(buff->data[bufnum]);
261
34.5M
        *p = '\0';
262
34.5M
 read_retry:
263
34.5M
        if (in != NULL && BIO_gets(in, p, CONFBUFSIZE - 1) < 0)
264
0
            goto err;
265
34.5M
        p[CONFBUFSIZE - 1] = '\0';
266
34.5M
        ii = i = (int)strlen(p);
267
34.5M
        if (first_call) {
268
            /* Other BOMs imply unsupported multibyte encoding,
269
             * so don't strip them and let the error raise */
270
17.8k
            const unsigned char utf8_bom[3] = {0xEF, 0xBB, 0xBF};
271
272
17.8k
            if (i >= 3 && memcmp(p, utf8_bom, 3) == 0) {
273
37
                memmove(p, p + 3, i - 3);
274
37
                p[i - 3] = 0;
275
37
                i -= 3;
276
37
                ii -= 3;
277
37
            }
278
17.8k
            first_call = 0;
279
17.8k
        }
280
34.5M
        if (i == 0 && !again) {
281
            /* the currently processed BIO is NULL or at EOF */
282
13.1k
            BIO *parent;
283
284
13.1k
#ifndef OPENSSL_NO_POSIX_IO
285
            /* continue processing with the next file from directory */
286
13.1k
            if (dirctx != NULL) {
287
2.52k
                BIO *next;
288
289
2.52k
                if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
290
707
                    BIO_vfree(in);
291
707
                    in = next;
292
707
                    goto read_retry;
293
1.81k
                } else {
294
1.81k
                    OPENSSL_free(dirpath);
295
1.81k
                    dirpath = NULL;
296
1.81k
                }
297
2.52k
            }
298
12.4k
#endif
299
            /* no more files in directory, continue with processing parent */
300
12.4k
            if ((parent = sk_BIO_pop(biosk)) == NULL) {
301
                /* everything processed get out of the loop */
302
10.5k
                break;
303
10.5k
            } else {
304
1.81k
                BIO_vfree(in);
305
1.81k
                in = parent;
306
1.81k
                goto read_retry;
307
1.81k
            }
308
12.4k
        }
309
34.5M
        again = 0;
310
68.5M
        while (i > 0) {
311
35.2M
            if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
312
1.22M
                break;
313
34.0M
            else
314
34.0M
                i--;
315
35.2M
        }
316
        /*
317
         * we removed some trailing stuff so there is a new line on the end.
318
         */
319
34.5M
        if (ii && i == ii)
320
513k
            again = 1;          /* long line */
321
34.0M
        else {
322
34.0M
            p[i] = '\0';
323
34.0M
            eline++;            /* another input line */
324
34.0M
        }
325
326
        /* we now have a line with trailing \r\n removed */
327
328
        /* i is the number of bytes */
329
34.5M
        bufnum += i;
330
331
34.5M
        v = NULL;
332
        /* check for line continuation */
333
34.5M
        if (!again && bufnum >= 1) {
334
            /*
335
             * If we have bytes and the last char '\\' and second last char
336
             * is not '\\'
337
             */
338
725k
            p = &(buff->data[bufnum - 1]);
339
725k
            if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
340
17.4k
                bufnum--;
341
17.4k
                again = 1;
342
17.4k
            }
343
725k
        }
344
34.5M
        if (again)
345
530k
            continue;
346
34.0M
        bufnum = 0;
347
34.0M
        buf = buff->data;
348
349
34.0M
        clear_comments(conf, buf);
350
34.0M
        s = eat_ws(conf, buf);
351
34.0M
        if (IS_EOF(conf, *s))
352
33.5M
            continue;           /* blank line */
353
439k
        if (*s == '[') {
354
41.8k
            char *ss;
355
356
41.8k
            s++;
357
41.8k
            start = eat_ws(conf, s);
358
41.8k
            ss = start;
359
48.1k
 again:
360
48.1k
            end = eat_alpha_numeric(conf, ss);
361
48.1k
            p = eat_ws(conf, end);
362
48.1k
            if (*p != ']') {
363
6.92k
                if (*p != '\0' && ss != p) {
364
6.22k
                    ss = p;
365
6.22k
                    goto again;
366
6.22k
                }
367
6.92k
                ERR_raise(ERR_LIB_CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
368
698
                goto err;
369
6.92k
            }
370
41.1k
            *end = '\0';
371
41.1k
            if (!str_copy(conf, NULL, &section, start))
372
0
                goto err;
373
41.1k
            if ((sv = _CONF_get_section(conf, section)) == NULL)
374
23.9k
                sv = _CONF_new_section(conf, section);
375
41.1k
            if (sv == NULL) {
376
0
                ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
377
0
                goto err;
378
0
            }
379
41.1k
            continue;
380
397k
        } else {
381
397k
            pname = s;
382
397k
            end = eat_alpha_numeric(conf, s);
383
397k
            if ((end[0] == ':') && (end[1] == ':')) {
384
67.6k
                *end = '\0';
385
67.6k
                end += 2;
386
67.6k
                psection = pname;
387
67.6k
                pname = end;
388
67.6k
                end = eat_alpha_numeric(conf, end);
389
329k
            } else {
390
329k
                psection = section;
391
329k
            }
392
397k
            p = eat_ws(conf, end);
393
397k
            if (CHECK_AND_SKIP_PREFIX(pname, ".pragma")
394
18.6k
                && (p != pname || *p == '=')) {
395
18.5k
                char *pval;
396
397
18.5k
                if (*p == '=') {
398
11.3k
                    p++;
399
11.3k
                    p = eat_ws(conf, p);
400
11.3k
                }
401
18.5k
                trim_ws(conf, p);
402
403
                /* Pragma values take the form keyword:value */
404
18.5k
                pval = strchr(p, ':');
405
18.5k
                if (pval == NULL || pval == p || pval[1] == '\0') {
406
286
                    ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
407
286
                    goto err;
408
286
                }
409
410
18.3k
                *pval++ = '\0';
411
18.3k
                trim_ws(conf, p);
412
18.3k
                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
18.3k
                if (strcmp(p, "dollarid") == 0) {
422
4.28k
                    if (!parsebool(pval, &conf->flag_dollarid))
423
43
                        goto err;
424
14.0k
                } else if (strcmp(p, "abspath") == 0) {
425
3.21k
                    if (!parsebool(pval, &conf->flag_abspath))
426
233
                        goto err;
427
10.8k
                } else if (strcmp(p, "includedir") == 0) {
428
1.95k
                    OPENSSL_free(conf->includedir);
429
1.95k
                    if ((conf->includedir = OPENSSL_strdup(pval)) == NULL)
430
0
                        goto err;
431
1.95k
                }
432
433
                /*
434
                 * We *ignore* any unknown pragma.
435
                 */
436
18.0k
                continue;
437
378k
            } else if (CHECK_AND_SKIP_PREFIX(pname, ".include")
438
6.67k
                && (p != pname || *p == '=')) {
439
6.63k
                char *include = NULL;
440
6.63k
                BIO *next;
441
6.63k
                const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE");
442
6.63k
                char *include_path = NULL;
443
444
6.63k
#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
6.63k
                if (numincludes++ > 10)
455
34
                    goto err;
456
6.59k
#endif
457
458
6.59k
                if (include_dir == NULL)
459
6.59k
                    include_dir = conf->includedir;
460
461
6.59k
                if (*p == '=') {
462
1.22k
                    p++;
463
1.22k
                    p = eat_ws(conf, p);
464
1.22k
                }
465
6.59k
                trim_ws(conf, p);
466
6.59k
                if (!str_copy(conf, psection, &include, p))
467
17
                    goto err;
468
469
6.58k
                if (include_dir != NULL && !ossl_is_absolute_path(include)) {
470
1.05k
                    size_t newlen = strlen(include_dir) + strlen(include) + 2;
471
472
1.05k
                    include_path = OPENSSL_malloc(newlen);
473
1.05k
                    if (include_path == NULL) {
474
0
                        OPENSSL_free(include);
475
0
                        goto err;
476
0
                    }
477
478
1.05k
                    OPENSSL_strlcpy(include_path, include_dir, newlen);
479
1.05k
                    if (!ossl_ends_with_dirsep(include_path))
480
888
                        OPENSSL_strlcat(include_path, "/", newlen);
481
1.05k
                    OPENSSL_strlcat(include_path, include, newlen);
482
1.05k
                    OPENSSL_free(include);
483
5.52k
                } else {
484
5.52k
                    include_path = include;
485
5.52k
                }
486
487
6.58k
                if (conf->flag_abspath
488
200
                        && !ossl_is_absolute_path(include_path)) {
489
53
                    ERR_raise(ERR_LIB_CONF, CONF_R_RELATIVE_PATH);
490
53
                    OPENSSL_free(include_path);
491
53
                    goto err;
492
53
                }
493
494
                /* get the BIO of the included file */
495
6.52k
#ifndef OPENSSL_NO_POSIX_IO
496
6.52k
                next = process_include(include_path, &dirctx, &dirpath);
497
6.52k
                if (include_path != dirpath) {
498
                    /* dirpath will contain include in case of a directory */
499
4.41k
                    OPENSSL_free(include_path);
500
4.41k
                }
501
#else
502
                next = BIO_new_file(include_path, "r");
503
                OPENSSL_free(include_path);
504
#endif
505
506
6.52k
                if (next != NULL) {
507
                    /* push the currently processing BIO onto stack */
508
2.13k
                    if (biosk == NULL) {
509
897
                        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
897
                    }
515
2.13k
                    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
2.13k
                    in = next;
522
2.13k
                }
523
6.52k
                continue;
524
371k
            } else if (*p != '=') {
525
4.02k
                ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN,
526
4.02k
                               "HERE-->%s", p);
527
4.02k
                goto err;
528
4.02k
            }
529
367k
            *end = '\0';
530
367k
            p++;
531
367k
            start = eat_ws(conf, p);
532
367k
            trim_ws(conf, start);
533
534
367k
            if ((v = OPENSSL_malloc(sizeof(*v))) == NULL)
535
0
                goto err;
536
367k
            v->name = OPENSSL_strdup(pname);
537
367k
            v->value = NULL;
538
367k
            if (v->name == NULL)
539
0
                goto err;
540
367k
            if (!str_copy(conf, psection, &(v->value), start))
541
1.82k
                goto err;
542
543
366k
            if (strcmp(psection, section) != 0) {
544
66.9k
                if ((tv = _CONF_get_section(conf, psection))
545
66.9k
                    == NULL)
546
53.5k
                    tv = _CONF_new_section(conf, psection);
547
66.9k
                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
66.9k
            } else
553
299k
                tv = sv;
554
366k
            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
366k
            v = NULL;
559
366k
        }
560
439k
    }
561
10.5k
    BUF_MEM_free(buff);
562
10.5k
    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
10.5k
    sk_BIO_free(biosk);
568
10.5k
    return 1;
569
570
7.21k
 err:
571
7.21k
    BUF_MEM_free(buff);
572
7.21k
    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
7.53k
    while (sk_BIO_num(biosk) > 0) {
579
321
        BIO *popped = sk_BIO_pop(biosk);
580
321
        BIO_vfree(in);
581
321
        in = popped;
582
321
    }
583
7.21k
    sk_BIO_free(biosk);
584
7.21k
#ifndef OPENSSL_NO_POSIX_IO
585
7.21k
    OPENSSL_free(dirpath);
586
7.21k
    if (dirctx != NULL)
587
293
        OPENSSL_DIR_end(&dirctx);
588
7.21k
#endif
589
7.21k
    if (line != NULL)
590
7.21k
        *line = eline;
591
7.21k
    BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
592
7.21k
    ERR_add_error_data(2, "line ", btmp);
593
7.21k
    if (h != conf->data) {
594
7.21k
        CONF_free(conf->data);
595
7.21k
        conf->data = NULL;
596
7.21k
    }
597
7.21k
    if (v != NULL) {
598
1.82k
        OPENSSL_free(v->name);
599
1.82k
        OPENSSL_free(v->value);
600
1.82k
        OPENSSL_free(v);
601
1.82k
    }
602
7.21k
    return 0;
603
17.8k
}
604
605
static void clear_comments(CONF *conf, char *p)
606
42.3M
{
607
42.4M
    for (;;) {
608
42.4M
        if (IS_FCOMMENT(conf, *p)) {
609
0
            *p = '\0';
610
0
            return;
611
0
        }
612
42.4M
        if (!IS_WS(conf, *p)) {
613
42.3M
            break;
614
42.3M
        }
615
86.9k
        p++;
616
86.9k
    }
617
618
282M
    for (;;) {
619
282M
        if (IS_COMMENT(conf, *p)) {
620
437k
            *p = '\0';
621
437k
            return;
622
437k
        }
623
282M
        if (IS_DQUOTE(conf, *p)) {
624
0
            p = scan_dquote(conf, p);
625
0
            continue;
626
0
        }
627
282M
        if (IS_QUOTE(conf, *p)) {
628
50.0k
            p = scan_quote(conf, p);
629
50.0k
            continue;
630
50.0k
        }
631
282M
        if (IS_ESC(conf, *p)) {
632
878k
            p = scan_esc(conf, p);
633
878k
            continue;
634
878k
        }
635
281M
        if (IS_EOF(conf, *p))
636
41.9M
            return;
637
239M
        else
638
239M
            p++;
639
281M
    }
640
42.3M
}
641
642
static int str_copy(CONF *conf, char *section, char **pto, char *from)
643
515k
{
644
515k
    int q, r, rr = 0, to = 0;
645
515k
    char *s, *e, *rp, *p, *rrp, *np, *cp, v;
646
515k
    BUF_MEM *buf;
647
648
515k
    if ((buf = BUF_MEM_new()) == NULL)
649
0
        return 0;
650
651
515k
    if (!BUF_MEM_grow(buf, strlen(from) + 1))
652
0
        goto err;
653
654
104M
    for (;;) {
655
104M
        if (IS_QUOTE(conf, *from)) {
656
38.0k
            q = *from;
657
38.0k
            from++;
658
24.0M
            while (!IS_EOF(conf, *from) && (*from != q)) {
659
23.9M
                if (IS_ESC(conf, *from)) {
660
104k
                    from++;
661
104k
                    if (IS_EOF(conf, *from))
662
3.83k
                        break;
663
104k
                }
664
23.9M
                buf->data[to++] = *(from++);
665
23.9M
            }
666
38.0k
            if (*from == q)
667
29.0k
                from++;
668
104M
        } 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
104M
        } else if (IS_ESC(conf, *from)) {
684
187k
            from++;
685
187k
            v = *(from++);
686
187k
            if (IS_EOF(conf, v))
687
11.2k
                break;
688
175k
            else if (v == 'r')
689
37.6k
                v = '\r';
690
138k
            else if (v == 'n')
691
7.10k
                v = '\n';
692
131k
            else if (v == 'b')
693
4.68k
                v = '\b';
694
126k
            else if (v == 't')
695
3.03k
                v = '\t';
696
175k
            buf->data[to++] = v;
697
103M
        } else if (IS_EOF(conf, *from))
698
501k
            break;
699
103M
        else if (*from == '$'
700
166k
                 && (!conf->flag_dollarid
701
62.3k
                     || from[1] == '{'
702
109k
                     || from[1] == '(')) {
703
109k
            size_t newsize;
704
705
            /* try to expand it */
706
109k
            rrp = NULL;
707
109k
            s = &(from[1]);
708
109k
            if (*s == '{')
709
5.85k
                q = '}';
710
104k
            else if (*s == '(')
711
3.76k
                q = ')';
712
100k
            else
713
100k
                q = 0;
714
715
109k
            if (q)
716
9.61k
                s++;
717
109k
            cp = section;
718
109k
            e = np = s;
719
16.9M
            while (IS_ALNUM(conf, *e)
720
111k
                   || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
721
16.8M
                e++;
722
109k
            if ((e[0] == ':') && (e[1] == ':')) {
723
5.70k
                cp = np;
724
5.70k
                rrp = e;
725
5.70k
                rr = *e;
726
5.70k
                *rrp = '\0';
727
5.70k
                e += 2;
728
5.70k
                np = e;
729
10.9k
                while (IS_ALNUM(conf, *e)
730
6.98k
                       || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
731
5.22k
                    e++;
732
5.70k
            }
733
109k
            r = *e;
734
109k
            *e = '\0';
735
109k
            rp = e;
736
109k
            if (q) {
737
9.61k
                if (r != q) {
738
406
                    ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE);
739
406
                    goto err;
740
406
                }
741
9.20k
                e++;
742
9.20k
            }
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
109k
            p = _CONF_get_string(conf, cp, np);
754
109k
            if (rrp != NULL)
755
5.65k
                *rrp = rr;
756
109k
            *rp = r;
757
109k
            if (p == NULL) {
758
1.52k
                ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_HAS_NO_VALUE);
759
1.52k
                goto err;
760
1.52k
            }
761
108k
            newsize = strlen(p) + buf->length - (e - from);
762
108k
            if (newsize > MAX_CONF_VALUE_LENGTH) {
763
82
                ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
764
82
                goto err;
765
82
            }
766
107k
            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
35.2M
            while (*p)
771
35.1M
                buf->data[to++] = *(p++);
772
773
107k
            from = e;
774
775
            /*
776
             * In case there were no braces or parenthesis around the
777
             * variable reference, we have to put back the character that was
778
             * replaced with a '\0'.  /RL
779
             */
780
107k
            *rp = r;
781
107k
        } else
782
103M
            buf->data[to++] = *(from++);
783
104M
    }
784
513k
    buf->data[to] = '\0';
785
513k
    OPENSSL_free(*pto);
786
513k
    *pto = buf->data;
787
513k
    OPENSSL_free(buf);
788
513k
    return 1;
789
2.01k
 err:
790
2.01k
    BUF_MEM_free(buf);
791
2.01k
    return 0;
792
515k
}
793
794
#ifndef OPENSSL_NO_POSIX_IO
795
/*
796
 * Check whether included path is a directory.
797
 * Returns next BIO to process and in case of a directory
798
 * also an opened directory context and the include path.
799
 */
800
static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
801
                            char **dirpath)
802
7.65k
{
803
7.65k
    struct stat st;
804
7.65k
    BIO *next;
805
806
7.65k
    if (stat(include, &st) < 0) {
807
3.18k
        ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include);
808
        /* missing include file is not fatal error */
809
3.18k
        return NULL;
810
3.18k
    }
811
812
4.47k
    if (S_ISDIR(st.st_mode)) {
813
4.37k
        if (*dirctx != NULL) {
814
0
            ERR_raise_data(ERR_LIB_CONF, CONF_R_RECURSIVE_DIRECTORY_INCLUDE,
815
0
                           "%s", include);
816
0
            return NULL;
817
0
        }
818
        /* a directory, load its contents */
819
4.37k
        if ((next = get_next_file(include, dirctx)) != NULL)
820
2.77k
            *dirpath = include;
821
4.37k
        return next;
822
4.37k
    }
823
824
94
    next = BIO_new_file(include, "r");
825
94
    return next;
826
4.47k
}
827
828
/*
829
 * Get next file from the directory path.
830
 * Returns BIO of the next file to read and updates dirctx.
831
 */
832
static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx)
833
7.63k
{
834
7.63k
    const char *filename;
835
7.63k
    size_t pathlen;
836
837
7.63k
    pathlen = strlen(path);
838
318k
    while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
839
314k
        size_t namelen;
840
841
314k
        namelen = strlen(filename);
842
843
844
314k
        if ((namelen > 5
845
249k
             && OPENSSL_strcasecmp(filename + namelen - 5, ".conf") == 0)
846
303k
            || (namelen > 4
847
264k
                && OPENSSL_strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
848
13.1k
            size_t newlen;
849
13.1k
            char *newpath;
850
13.1k
            BIO *bio;
851
852
13.1k
            newlen = pathlen + namelen + 2;
853
13.1k
            newpath = OPENSSL_zalloc(newlen);
854
13.1k
            if (newpath == NULL)
855
0
                break;
856
#ifdef OPENSSL_SYS_VMS
857
            /*
858
             * If the given path isn't clear VMS syntax,
859
             * we treat it as on Unix.
860
             */
861
            if (path[pathlen - 1] == ']'
862
                || path[pathlen - 1] == '>'
863
                || path[pathlen - 1] == ':') {
864
                /* Clear VMS directory syntax, just copy as is */
865
                OPENSSL_strlcpy(newpath, path, newlen);
866
            }
867
#endif
868
13.1k
            if (newpath[0] == '\0') {
869
13.1k
                OPENSSL_strlcpy(newpath, path, newlen);
870
13.1k
                OPENSSL_strlcat(newpath, "/", newlen);
871
13.1k
            }
872
13.1k
            OPENSSL_strlcat(newpath, filename, newlen);
873
874
13.1k
            bio = BIO_new_file(newpath, "r");
875
13.1k
            OPENSSL_free(newpath);
876
            /* Errors when opening files are non-fatal. */
877
13.1k
            if (bio != NULL)
878
3.56k
                return bio;
879
13.1k
        }
880
314k
    }
881
4.06k
    OPENSSL_DIR_end(dirctx);
882
4.06k
    *dirctx = NULL;
883
4.06k
    return NULL;
884
7.63k
}
885
#endif
886
887
static int is_keytype(const CONF *conf, char c, unsigned short type)
888
2.49G
{
889
2.49G
    const unsigned short *keytypes = (const unsigned short *) conf->meth_data;
890
2.49G
    unsigned char key = (unsigned char)c;
891
892
#ifdef CHARSET_EBCDIC
893
# if CHAR_BIT > 8
894
    if (key > 255) {
895
        /* key is out of range for os_toascii table */
896
        return 0;
897
    }
898
# endif
899
    /* convert key from ebcdic to ascii */
900
    key = os_toascii[key];
901
#endif
902
903
2.49G
    if (key > 127) {
904
        /* key is not a seven bit ascii character */
905
175M
        return 0;
906
175M
    }
907
908
2.31G
    return (keytypes[key] & type) ? 1 : 0;
909
2.49G
}
910
911
static char *eat_ws(CONF *conf, char *p)
912
43.4M
{
913
44.0M
    while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
914
631k
        p++;
915
43.4M
    return p;
916
43.4M
}
917
918
static void trim_ws(CONF *conf, char *start)
919
504k
{
920
504k
    char *p = start;
921
922
158M
    while (!IS_EOF(conf, *p))
923
158M
        p++;
924
504k
    p--;
925
636k
    while ((p >= start) && IS_WS(conf, *p))
926
131k
        p--;
927
504k
    p++;
928
504k
    *p = '\0';
929
504k
}
930
931
static char *eat_alpha_numeric(CONF *conf, char *p)
932
625k
{
933
82.8M
    for (;;) {
934
82.8M
        if (IS_ESC(conf, *p)) {
935
553k
            p = scan_esc(conf, p);
936
553k
            continue;
937
553k
        }
938
82.3M
        if (!(IS_ALNUM_PUNCT(conf, *p)
939
629k
              || (conf->flag_dollarid && IS_DOLLAR(conf, *p))))
940
625k
            return p;
941
81.7M
        p++;
942
81.7M
    }
943
625k
}
944
945
static char *scan_quote(CONF *conf, char *p)
946
50.0k
{
947
50.0k
    int q = *p;
948
949
50.0k
    p++;
950
51.0M
    while (!(IS_EOF(conf, *p)) && (*p != q)) {
951
50.9M
        if (IS_ESC(conf, *p)) {
952
190k
            p++;
953
190k
            if (IS_EOF(conf, *p))
954
2.54k
                return p;
955
190k
        }
956
50.9M
        p++;
957
50.9M
    }
958
47.5k
    if (*p == q)
959
39.5k
        p++;
960
47.5k
    return p;
961
50.0k
}
962
963
static char *scan_dquote(CONF *conf, char *p)
964
0
{
965
0
    int q = *p;
966
967
0
    p++;
968
0
    while (!(IS_EOF(conf, *p))) {
969
0
        if (*p == q) {
970
0
            if (*(p + 1) == q) {
971
0
                p++;
972
0
            } else {
973
0
                break;
974
0
            }
975
0
        }
976
0
        p++;
977
0
    }
978
0
    if (*p == q)
979
0
        p++;
980
0
    return p;
981
0
}
982
983
static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
984
0
{
985
0
    if (a->name)
986
0
        BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
987
0
    else
988
0
        BIO_printf(out, "[[%s]]\n", a->section);
989
0
}
990
991
IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
992
993
static int def_dump(const CONF *conf, BIO *out)
994
0
{
995
0
    lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
996
0
    return 1;
997
0
}
998
999
static int def_is_number(const CONF *conf, char c)
1000
0
{
1001
0
    return IS_NUMBER(conf, c);
1002
0
}
1003
1004
static int def_to_int(const CONF *conf, char c)
1005
0
{
1006
0
    return c - '0';
1007
0
}