Coverage Report

Created: 2026-05-24 07:14

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