Coverage Report

Created: 2026-02-14 07:20

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
103k
#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.38M
#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
18.6k
{
88
18.6k
    return &default_method;
89
18.6k
}
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
18.6k
{
113
18.6k
    CONF *ret;
114
115
18.6k
    ret = OPENSSL_malloc(sizeof(*ret));
116
18.6k
    if (ret != NULL)
117
18.6k
        if (meth->init(ret) == 0) {
118
0
            OPENSSL_free(ret);
119
0
            ret = NULL;
120
0
        }
121
18.6k
    return ret;
122
18.6k
}
123
124
static int def_init_default(CONF *conf)
125
26.0k
{
126
26.0k
    if (conf == NULL)
127
0
        return 0;
128
129
26.0k
    memset(conf, 0, sizeof(*conf));
130
26.0k
    conf->meth = &default_method;
131
26.0k
    conf->meth_data = (void *)CONF_type_default;
132
133
26.0k
    return 1;
134
26.0k
}
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
18.6k
{
152
18.6k
    if (def_destroy_data(conf)) {
153
18.6k
        OPENSSL_free(conf);
154
18.6k
        return 1;
155
18.6k
    }
156
0
    return 0;
157
18.6k
}
158
159
static int def_destroy_data(CONF *conf)
160
26.0k
{
161
26.0k
    if (conf == NULL)
162
0
        return 0;
163
26.0k
    _CONF_free_data(conf);
164
26.0k
    return 1;
165
26.0k
}
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.71k
{
194
8.71k
    if (OPENSSL_strcasecmp(pval, "on") == 0
195
5.60k
        || OPENSSL_strcasecmp(pval, "true") == 0) {
196
5.60k
        *flag = 1;
197
5.60k
    } else if (OPENSSL_strcasecmp(pval, "off") == 0
198
2.81k
        || OPENSSL_strcasecmp(pval, "false") == 0) {
199
2.81k
        *flag = 0;
200
2.81k
    } else {
201
295
        ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
202
295
        return 0;
203
295
    }
204
8.41k
    return 1;
205
8.71k
}
206
207
static int def_load_bio(CONF *conf, BIO *in, long *line)
208
2.99k
{
209
/* The macro BUFSIZE conflicts with a system macro in VxWorks */
210
24.4M
#define CONFBUFSIZE 512
211
2.99k
    int bufnum = 0, i, ii;
212
2.99k
    BUF_MEM *buff = NULL;
213
2.99k
    char *s, *p, *end;
214
2.99k
    int again;
215
2.99k
    int first_call = 1;
216
2.99k
    long eline = 0;
217
2.99k
    char btmp[DECIMAL_SIZE(eline) + 1];
218
2.99k
    CONF_VALUE *v = NULL, *tv;
219
2.99k
    CONF_VALUE *sv = NULL;
220
2.99k
    char *section = NULL, *buf;
221
2.99k
    char *start, *psection, *pname;
222
2.99k
    void *h = (void *)(conf->data);
223
2.99k
    STACK_OF(BIO) *biosk = NULL;
224
2.99k
#ifndef OPENSSL_NO_POSIX_IO
225
2.99k
    char *dirpath = NULL;
226
2.99k
    OPENSSL_DIR_CTX *dirctx = NULL;
227
2.99k
#endif
228
2.99k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
229
2.99k
    int numincludes = 0;
230
2.99k
#endif
231
232
2.99k
    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
2.99k
    section = OPENSSL_strdup("default");
238
2.99k
    if (section == NULL) {
239
0
        ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
240
0
        goto err;
241
0
    }
242
243
2.99k
    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
2.99k
    sv = _CONF_new_section(conf, section);
249
2.99k
    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
2.99k
    bufnum = 0;
255
2.99k
    again = 0;
256
8.13M
    for (;;) {
257
8.13M
        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
8.13M
        p = &(buff->data[bufnum]);
262
8.13M
        *p = '\0';
263
8.13M
    read_retry:
264
8.13M
        if (in != NULL && BIO_gets(in, p, CONFBUFSIZE - 1) < 0)
265
0
            goto err;
266
8.13M
        p[CONFBUFSIZE - 1] = '\0';
267
8.13M
        ii = i = strlen(p);
268
8.13M
        if (first_call) {
269
            /* Other BOMs imply unsupported multibyte encoding,
270
             * so don't strip them and let the error raise */
271
2.99k
            const unsigned char utf8_bom[3] = { 0xEF, 0xBB, 0xBF };
272
273
2.99k
            if (i >= 3 && memcmp(p, utf8_bom, 3) == 0) {
274
6
                memmove(p, p + 3, i - 3);
275
6
                p[i - 3] = 0;
276
6
                i -= 3;
277
6
                ii -= 3;
278
6
            }
279
2.99k
            first_call = 0;
280
2.99k
        }
281
8.13M
        if (i == 0 && !again) {
282
            /* the currently processed BIO is NULL or at EOF */
283
2.64k
            BIO *parent;
284
285
2.64k
#ifndef OPENSSL_NO_POSIX_IO
286
            /* continue processing with the next file from directory */
287
2.64k
            if (dirctx != NULL) {
288
828
                BIO *next;
289
290
828
                if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
291
117
                    BIO_vfree(in);
292
117
                    in = next;
293
117
                    goto read_retry;
294
711
                } else {
295
711
                    OPENSSL_free(dirpath);
296
711
                    dirpath = NULL;
297
711
                }
298
828
            }
299
2.53k
#endif
300
            /* no more files in directory, continue with processing parent */
301
2.53k
            if ((parent = sk_BIO_pop(biosk)) == NULL) {
302
                /* everything processed get out of the loop */
303
1.82k
                break;
304
1.82k
            } else {
305
711
                BIO_vfree(in);
306
711
                in = parent;
307
711
                goto read_retry;
308
711
            }
309
2.53k
        }
310
8.13M
        again = 0;
311
16.1M
        while (i > 0) {
312
8.34M
            if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
313
330k
                break;
314
8.01M
            else
315
8.01M
                i--;
316
8.34M
        }
317
        /*
318
         * we removed some trailing stuff so there is a new line on the end.
319
         */
320
8.13M
        if (ii && i == ii)
321
117k
            again = 1; /* long line */
322
8.01M
        else {
323
8.01M
            p[i] = '\0';
324
8.01M
            eline++; /* another input line */
325
8.01M
        }
326
327
        /* we now have a line with trailing \r\n removed */
328
329
        /* i is the number of bytes */
330
8.13M
        bufnum += i;
331
332
8.13M
        v = NULL;
333
        /* check for line continuation */
334
8.13M
        if (!again && bufnum >= 1) {
335
            /*
336
             * If we have bytes and the last char '\\' and second last char
337
             * is not '\\'
338
             */
339
216k
            p = &(buff->data[bufnum - 1]);
340
216k
            if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
341
1.15k
                bufnum--;
342
1.15k
                again = 1;
343
1.15k
            }
344
216k
        }
345
8.13M
        if (again)
346
118k
            continue;
347
8.01M
        bufnum = 0;
348
8.01M
        buf = buff->data;
349
350
8.01M
        clear_comments(conf, buf);
351
8.01M
        s = eat_ws(conf, buf);
352
8.01M
        if (IS_EOF(conf, *s))
353
7.90M
            continue; /* blank line */
354
111k
        if (*s == '[') {
355
12.9k
            char *ss;
356
357
12.9k
            s++;
358
12.9k
            start = eat_ws(conf, s);
359
12.9k
            ss = start;
360
13.3k
        again:
361
13.3k
            end = eat_alpha_numeric(conf, ss);
362
13.3k
            p = eat_ws(conf, end);
363
13.3k
            if (*p != ']') {
364
488
                if (*p != '\0' && ss != p) {
365
365
                    ss = p;
366
365
                    goto again;
367
365
                }
368
488
                ERR_raise(ERR_LIB_CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
369
123
                goto err;
370
488
            }
371
12.8k
            *end = '\0';
372
12.8k
            if (!str_copy(conf, NULL, &section, start))
373
0
                goto err;
374
12.8k
            if ((sv = _CONF_get_section(conf, section)) == NULL)
375
6.38k
                sv = _CONF_new_section(conf, section);
376
12.8k
            if (sv == NULL) {
377
0
                ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
378
0
                goto err;
379
0
            }
380
12.8k
            continue;
381
98.0k
        } else {
382
98.0k
            pname = s;
383
98.0k
            end = eat_alpha_numeric(conf, s);
384
98.0k
            if ((end[0] == ':') && (end[1] == ':')) {
385
10.0k
                *end = '\0';
386
10.0k
                end += 2;
387
10.0k
                psection = pname;
388
10.0k
                pname = end;
389
10.0k
                end = eat_alpha_numeric(conf, end);
390
87.9k
            } else {
391
87.9k
                psection = section;
392
87.9k
            }
393
98.0k
            p = eat_ws(conf, end);
394
98.0k
            if (strncmp(pname, ".pragma", 7) == 0
395
3.24k
                && (p != pname + 7 || *p == '=')) {
396
3.23k
                char *pval;
397
398
3.23k
                if (*p == '=') {
399
1.97k
                    p++;
400
1.97k
                    p = eat_ws(conf, p);
401
1.97k
                }
402
3.23k
                trim_ws(conf, p);
403
404
                /* Pragma values take the form keyword:value */
405
3.23k
                pval = strchr(p, ':');
406
3.23k
                if (pval == NULL || pval == p || pval[1] == '\0') {
407
49
                    ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
408
49
                    goto err;
409
49
                }
410
411
3.19k
                *pval++ = '\0';
412
3.19k
                trim_ws(conf, p);
413
3.19k
                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
3.19k
                if (strcmp(p, "dollarid") == 0) {
423
337
                    if (!parsebool(pval, &conf->flag_dollarid))
424
2
                        goto err;
425
2.85k
                } else if (strcmp(p, "abspath") == 0) {
426
1.01k
                    if (!parsebool(pval, &conf->flag_abspath))
427
38
                        goto err;
428
1.83k
                } else if (strcmp(p, "includedir") == 0) {
429
329
                    OPENSSL_free(conf->includedir);
430
329
                    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
329
                }
435
436
                /*
437
                 * We *ignore* any unknown pragma.
438
                 */
439
3.15k
                continue;
440
94.8k
            } else if (strncmp(pname, ".include", 8) == 0
441
1.43k
                && (p != pname + 8 || *p == '=')) {
442
1.42k
                char *include = NULL;
443
1.42k
                BIO *next;
444
1.42k
                const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE");
445
1.42k
                char *include_path = NULL;
446
447
1.42k
#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
1.42k
                if (numincludes++ > 10)
458
4
                    goto err;
459
1.41k
#endif
460
461
1.41k
                if (include_dir == NULL)
462
1.41k
                    include_dir = conf->includedir;
463
464
1.41k
                if (*p == '=') {
465
204
                    p++;
466
204
                    p = eat_ws(conf, p);
467
204
                }
468
1.41k
                trim_ws(conf, p);
469
1.41k
                if (!str_copy(conf, psection, &include, p))
470
7
                    goto err;
471
472
1.41k
                if (include_dir != NULL && !ossl_is_absolute_path(include)) {
473
427
                    size_t newlen = strlen(include_dir) + strlen(include) + 2;
474
475
427
                    include_path = OPENSSL_malloc(newlen);
476
427
                    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
427
                    OPENSSL_strlcpy(include_path, include_dir, newlen);
483
427
                    if (!ossl_ends_with_dirsep(include_path))
484
370
                        OPENSSL_strlcat(include_path, "/", newlen);
485
427
                    OPENSSL_strlcat(include_path, include, newlen);
486
427
                    OPENSSL_free(include);
487
984
                } else {
488
984
                    include_path = include;
489
984
                }
490
491
1.41k
                if (conf->flag_abspath
492
20
                    && !ossl_is_absolute_path(include_path)) {
493
2
                    ERR_raise(ERR_LIB_CONF, CONF_R_RELATIVE_PATH);
494
2
                    OPENSSL_free(include_path);
495
2
                    goto err;
496
2
                }
497
498
                /* get the BIO of the included file */
499
1.40k
#ifndef OPENSSL_NO_POSIX_IO
500
1.40k
                next = process_include(include_path, &dirctx, &dirpath);
501
1.40k
                if (include_path != dirpath) {
502
                    /* dirpath will contain include in case of a directory */
503
676
                    OPENSSL_free(include_path);
504
676
                }
505
#else
506
                next = BIO_new_file(include_path, "r");
507
                OPENSSL_free(include_path);
508
#endif
509
510
1.40k
                if (next != NULL) {
511
                    /* push the currently processing BIO onto stack */
512
743
                    if (biosk == NULL) {
513
240
                        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
240
                    }
519
743
                    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
743
                    in = next;
526
743
                }
527
1.40k
                continue;
528
93.4k
            } else if (*p != '=') {
529
615
                ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN,
530
615
                    "HERE-->%s", p);
531
615
                goto err;
532
615
            }
533
92.7k
            *end = '\0';
534
92.7k
            p++;
535
92.7k
            start = eat_ws(conf, p);
536
92.7k
            trim_ws(conf, start);
537
538
92.7k
            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
92.7k
            v->name = OPENSSL_strdup(pname);
543
92.7k
            v->value = NULL;
544
92.7k
            if (v->name == NULL) {
545
0
                ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
546
0
                goto err;
547
0
            }
548
92.7k
            if (!str_copy(conf, psection, &(v->value), start))
549
332
                goto err;
550
551
92.4k
            if (strcmp(psection, section) != 0) {
552
10.0k
                if ((tv = _CONF_get_section(conf, psection))
553
10.0k
                    == NULL)
554
8.81k
                    tv = _CONF_new_section(conf, psection);
555
10.0k
                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
10.0k
            } else
561
82.4k
                tv = sv;
562
92.4k
            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
92.4k
            v = NULL;
567
92.4k
        }
568
111k
    }
569
1.82k
    BUF_MEM_free(buff);
570
1.82k
    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
1.82k
    sk_BIO_free(biosk);
576
1.82k
    return 1;
577
578
1.17k
err:
579
1.17k
    BUF_MEM_free(buff);
580
1.17k
    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
1.20k
    while (sk_BIO_num(biosk) > 0) {
587
32
        BIO *popped = sk_BIO_pop(biosk);
588
32
        BIO_vfree(in);
589
32
        in = popped;
590
32
    }
591
1.17k
    sk_BIO_free(biosk);
592
1.17k
#ifndef OPENSSL_NO_POSIX_IO
593
1.17k
    OPENSSL_free(dirpath);
594
1.17k
    if (dirctx != NULL)
595
22
        OPENSSL_DIR_end(&dirctx);
596
1.17k
#endif
597
1.17k
    if (line != NULL)
598
1.17k
        *line = eline;
599
1.17k
    BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
600
1.17k
    ERR_add_error_data(2, "line ", btmp);
601
1.17k
    if (h != conf->data) {
602
1.17k
        CONF_free(conf->data);
603
1.17k
        conf->data = NULL;
604
1.17k
    }
605
1.17k
    if (v != NULL) {
606
332
        OPENSSL_free(v->name);
607
332
        OPENSSL_free(v->value);
608
332
        OPENSSL_free(v);
609
332
    }
610
1.17k
    return 0;
611
2.99k
}
612
613
static void clear_comments(CONF *conf, char *p)
614
35.3M
{
615
35.4M
    for (;;) {
616
35.4M
        if (IS_FCOMMENT(conf, *p)) {
617
0
            *p = '\0';
618
0
            return;
619
0
        }
620
35.4M
        if (!IS_WS(conf, *p)) {
621
35.3M
            break;
622
35.3M
        }
623
60.3k
        p++;
624
60.3k
    }
625
626
319M
    for (;;) {
627
319M
        if (IS_COMMENT(conf, *p)) {
628
309k
            *p = '\0';
629
309k
            return;
630
309k
        }
631
319M
        if (IS_DQUOTE(conf, *p)) {
632
0
            p = scan_dquote(conf, p);
633
0
            continue;
634
0
        }
635
319M
        if (IS_QUOTE(conf, *p)) {
636
272k
            p = scan_quote(conf, p);
637
272k
            continue;
638
272k
        }
639
319M
        if (IS_ESC(conf, *p)) {
640
865k
            p = scan_esc(conf, p);
641
865k
            continue;
642
865k
        }
643
318M
        if (IS_EOF(conf, *p))
644
35.0M
            return;
645
283M
        else
646
283M
            p++;
647
318M
    }
648
35.3M
}
649
650
static int str_copy(CONF *conf, char *section, char **pto, char *from)
651
402k
{
652
402k
    int q, r, rr = 0, to = 0, len = 0;
653
402k
    char *s, *e, *rp, *p, *rrp, *np, *cp, v;
654
402k
    BUF_MEM *buf;
655
656
402k
    if ((buf = BUF_MEM_new()) == NULL)
657
0
        return 0;
658
659
402k
    len = strlen(from) + 1;
660
402k
    if (!BUF_MEM_grow(buf, len))
661
0
        goto err;
662
663
86.6M
    for (;;) {
664
86.6M
        if (IS_QUOTE(conf, *from)) {
665
33.2k
            q = *from;
666
33.2k
            from++;
667
27.7M
            while (!IS_EOF(conf, *from) && (*from != q)) {
668
27.7M
                if (IS_ESC(conf, *from)) {
669
78.6k
                    from++;
670
78.6k
                    if (IS_EOF(conf, *from))
671
3.24k
                        break;
672
78.6k
                }
673
27.7M
                buf->data[to++] = *(from++);
674
27.7M
            }
675
33.2k
            if (*from == q)
676
25.7k
                from++;
677
86.6M
        } 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
86.6M
        } else if (IS_ESC(conf, *from)) {
693
255k
            from++;
694
255k
            v = *(from++);
695
255k
            if (IS_EOF(conf, v))
696
10.5k
                break;
697
244k
            else if (v == 'r')
698
63.1k
                v = '\r';
699
181k
            else if (v == 'n')
700
11.0k
                v = '\n';
701
170k
            else if (v == 'b')
702
4.51k
                v = '\b';
703
165k
            else if (v == 't')
704
5.83k
                v = '\t';
705
244k
            buf->data[to++] = v;
706
86.3M
        } else if (IS_EOF(conf, *from))
707
390k
            break;
708
85.9M
        else if (*from == '$'
709
148k
            && (!conf->flag_dollarid
710
47.3k
                || from[1] == '{'
711
105k
                || from[1] == '(')) {
712
105k
            size_t newsize;
713
714
            /* try to expand it */
715
105k
            rrp = NULL;
716
105k
            s = &(from[1]);
717
105k
            if (*s == '{')
718
4.46k
                q = '}';
719
100k
            else if (*s == '(')
720
3.20k
                q = ')';
721
97.7k
            else
722
97.7k
                q = 0;
723
724
105k
            if (q)
725
7.66k
                s++;
726
105k
            cp = section;
727
105k
            e = np = s;
728
19.9M
            while (IS_ALNUM(conf, *e)
729
106k
                || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
730
19.8M
                e++;
731
105k
            if ((e[0] == ':') && (e[1] == ':')) {
732
6.31k
                cp = np;
733
6.31k
                rrp = e;
734
6.31k
                rr = *e;
735
6.31k
                *rrp = '\0';
736
6.31k
                e += 2;
737
6.31k
                np = e;
738
10.4k
                while (IS_ALNUM(conf, *e)
739
7.56k
                    || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
740
4.11k
                    e++;
741
6.31k
            }
742
105k
            r = *e;
743
105k
            *e = '\0';
744
105k
            rp = e;
745
105k
            if (q) {
746
7.66k
                if (r != q) {
747
395
                    ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE);
748
395
                    goto err;
749
395
                }
750
7.26k
                e++;
751
7.26k
            }
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
104k
            p = _CONF_get_string(conf, cp, np);
763
104k
            if (rrp != NULL)
764
6.25k
                *rrp = rr;
765
104k
            *rp = r;
766
104k
            if (p == NULL) {
767
1.58k
                ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_HAS_NO_VALUE);
768
1.58k
                goto err;
769
1.58k
            }
770
103k
            newsize = strlen(p) + buf->length - (e - from);
771
103k
            if (newsize > MAX_CONF_VALUE_LENGTH) {
772
102
                ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
773
102
                goto err;
774
102
            }
775
103k
            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.1M
            while (*p)
780
36.0M
                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
103k
            len -= e - from;
787
103k
            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
103k
            *rp = r;
795
103k
        } else
796
85.8M
            buf->data[to++] = *(from++);
797
86.6M
    }
798
400k
    buf->data[to] = '\0';
799
400k
    OPENSSL_free(*pto);
800
400k
    *pto = buf->data;
801
400k
    OPENSSL_free(buf);
802
400k
    return 1;
803
2.08k
err:
804
2.08k
    BUF_MEM_free(buf);
805
2.08k
    return 0;
806
402k
}
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.46k
{
817
6.46k
    struct stat st;
818
6.46k
    BIO *next;
819
820
6.46k
    if (stat(include, &st) < 0) {
821
2.98k
        ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include);
822
        /* missing include file is not fatal error */
823
2.98k
        return NULL;
824
2.98k
    }
825
826
3.47k
    if (S_ISDIR(st.st_mode)) {
827
3.39k
        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.39k
        if ((next = get_next_file(include, dirctx)) != NULL)
834
2.01k
            *dirpath = include;
835
3.39k
        return next;
836
3.39k
    }
837
838
86
    next = BIO_new_file(include, "r");
839
86
    return next;
840
3.47k
}
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
5.86k
{
848
5.86k
    const char *filename;
849
5.86k
    size_t pathlen;
850
851
5.86k
    pathlen = strlen(path);
852
243k
    while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
853
239k
        size_t namelen;
854
855
239k
        namelen = strlen(filename);
856
857
239k
        if ((namelen > 5
858
184k
                && OPENSSL_strcasecmp(filename + namelen - 5, ".conf") == 0)
859
229k
            || (namelen > 4
860
195k
                && OPENSSL_strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
861
11.8k
            size_t newlen;
862
11.8k
            char *newpath;
863
11.8k
            BIO *bio;
864
865
11.8k
            newlen = pathlen + namelen + 2;
866
11.8k
            newpath = OPENSSL_zalloc(newlen);
867
11.8k
            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
11.8k
            if (newpath[0] == '\0') {
884
11.8k
                OPENSSL_strlcpy(newpath, path, newlen);
885
11.8k
                OPENSSL_strlcat(newpath, "/", newlen);
886
11.8k
            }
887
11.8k
            OPENSSL_strlcat(newpath, filename, newlen);
888
889
11.8k
            bio = BIO_new_file(newpath, "r");
890
11.8k
            OPENSSL_free(newpath);
891
            /* Errors when opening files are non-fatal. */
892
11.8k
            if (bio != NULL)
893
2.65k
                return bio;
894
11.8k
        }
895
239k
    }
896
3.21k
    OPENSSL_DIR_end(dirctx);
897
3.21k
    *dirctx = NULL;
898
3.21k
    return NULL;
899
5.86k
}
900
#endif
901
902
static int is_keytype(const CONF *conf, char c, unsigned short type)
903
2.68G
{
904
2.68G
    const unsigned short *keytypes = (const unsigned short *)conf->meth_data;
905
2.68G
    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.68G
    if (key > 127) {
919
        /* key is not a seven bit ascii character */
920
153M
        return 0;
921
153M
    }
922
923
2.53G
    return (keytypes[key] & type) ? 1 : 0;
924
2.68G
}
925
926
static char *eat_ws(CONF *conf, char *p)
927
36.2M
{
928
36.6M
    while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
929
441k
        p++;
930
36.2M
    return p;
931
36.2M
}
932
933
static void trim_ws(CONF *conf, char *start)
934
400k
{
935
400k
    char *p = start;
936
937
164M
    while (!IS_EOF(conf, *p))
938
163M
        p++;
939
400k
    p--;
940
490k
    while ((p >= start) && IS_WS(conf, *p))
941
90.2k
        p--;
942
400k
    p++;
943
400k
    *p = '\0';
944
400k
}
945
946
static char *eat_alpha_numeric(CONF *conf, char *p)
947
494k
{
948
127M
    for (;;) {
949
127M
        if (IS_ESC(conf, *p)) {
950
515k
            p = scan_esc(conf, p);
951
515k
            continue;
952
515k
        }
953
127M
        if (!(IS_ALNUM_PUNCT(conf, *p)
954
497k
                || (conf->flag_dollarid && IS_DOLLAR(conf, *p))))
955
494k
            return p;
956
126M
        p++;
957
126M
    }
958
494k
}
959
960
static char *scan_quote(CONF *conf, char *p)
961
272k
{
962
272k
    int q = *p;
963
964
272k
    p++;
965
52.8M
    while (!(IS_EOF(conf, *p)) && (*p != q)) {
966
52.6M
        if (IS_ESC(conf, *p)) {
967
148k
            p++;
968
148k
            if (IS_EOF(conf, *p))
969
1.73k
                return p;
970
148k
        }
971
52.6M
        p++;
972
52.6M
    }
973
270k
    if (*p == q)
974
263k
        p++;
975
270k
    return p;
976
272k
}
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
}