Coverage Report

Created: 2026-04-01 06:39

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
96.1k
#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.86M
#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.2k
{
88
17.2k
    return &default_method;
89
17.2k
}
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.2k
{
113
17.2k
    CONF *ret;
114
115
17.2k
    ret = OPENSSL_malloc(sizeof(*ret));
116
17.2k
    if (ret != NULL)
117
17.2k
        if (meth->init(ret) == 0) {
118
0
            OPENSSL_free(ret);
119
0
            ret = NULL;
120
0
        }
121
17.2k
    return ret;
122
17.2k
}
123
124
static int def_init_default(CONF *conf)
125
24.0k
{
126
24.0k
    if (conf == NULL)
127
0
        return 0;
128
129
24.0k
    memset(conf, 0, sizeof(*conf));
130
24.0k
    conf->meth = &default_method;
131
24.0k
    conf->meth_data = (void *)CONF_type_default;
132
133
24.0k
    return 1;
134
24.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
17.2k
{
152
17.2k
    if (def_destroy_data(conf)) {
153
17.2k
        OPENSSL_free(conf);
154
17.2k
        return 1;
155
17.2k
    }
156
0
    return 0;
157
17.2k
}
158
159
static int def_destroy_data(CONF *conf)
160
24.0k
{
161
24.0k
    if (conf == NULL)
162
0
        return 0;
163
24.0k
    _CONF_free_data(conf);
164
24.0k
    return 1;
165
24.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.46k
{
194
8.46k
    if (OPENSSL_strcasecmp(pval, "on") == 0
195
5.67k
        || OPENSSL_strcasecmp(pval, "true") == 0) {
196
5.67k
        *flag = 1;
197
5.67k
    } else if (OPENSSL_strcasecmp(pval, "off") == 0
198
2.51k
        || OPENSSL_strcasecmp(pval, "false") == 0) {
199
2.51k
        *flag = 0;
200
2.51k
    } else {
201
271
        ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
202
271
        return 0;
203
271
    }
204
8.19k
    return 1;
205
8.46k
}
206
207
static int def_load_bio(CONF *conf, BIO *in, long *line)
208
1.04k
{
209
/* The macro BUFSIZE conflicts with a system macro in VxWorks */
210
24.1M
#define CONFBUFSIZE 512
211
1.04k
    int bufnum = 0, i, ii;
212
1.04k
    BUF_MEM *buff = NULL;
213
1.04k
    char *s, *p, *end;
214
1.04k
    int again;
215
1.04k
    int first_call = 1;
216
1.04k
    long eline = 0;
217
1.04k
    char btmp[DECIMAL_SIZE(eline) + 1];
218
1.04k
    CONF_VALUE *v = NULL, *tv;
219
1.04k
    CONF_VALUE *sv = NULL;
220
1.04k
    char *section = NULL, *buf;
221
1.04k
    char *start, *psection, *pname;
222
1.04k
    void *h = (void *)(conf->data);
223
1.04k
    STACK_OF(BIO) *biosk = NULL;
224
1.04k
#ifndef OPENSSL_NO_POSIX_IO
225
1.04k
    char *dirpath = NULL;
226
1.04k
    OPENSSL_DIR_CTX *dirctx = NULL;
227
1.04k
#endif
228
1.04k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
229
1.04k
    int numincludes = 0;
230
1.04k
#endif
231
232
1.04k
    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.04k
    section = OPENSSL_strdup("default");
238
1.04k
    if (section == NULL) {
239
0
        ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
240
0
        goto err;
241
0
    }
242
243
1.04k
    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.04k
    sv = _CONF_new_section(conf, section);
249
1.04k
    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.04k
    bufnum = 0;
255
1.04k
    again = 0;
256
8.04M
    for (;;) {
257
8.04M
        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.04M
        p = &(buff->data[bufnum]);
262
8.04M
        *p = '\0';
263
8.04M
    read_retry:
264
8.04M
        if (in != NULL && BIO_gets(in, p, CONFBUFSIZE - 1) < 0)
265
0
            goto err;
266
8.04M
        p[CONFBUFSIZE - 1] = '\0';
267
8.04M
        ii = i = strlen(p);
268
8.04M
        if (first_call) {
269
            /* Other BOMs imply unsupported multibyte encoding,
270
             * so don't strip them and let the error raise */
271
1.04k
            const unsigned char utf8_bom[3] = { 0xEF, 0xBB, 0xBF };
272
273
1.04k
            if (i >= 3 && memcmp(p, utf8_bom, 3) == 0) {
274
4
                memmove(p, p + 3, i - 3);
275
4
                p[i - 3] = 0;
276
4
                i -= 3;
277
4
                ii -= 3;
278
4
            }
279
1.04k
            first_call = 0;
280
1.04k
        }
281
8.04M
        if (i == 0 && !again) {
282
            /* the currently processed BIO is NULL or at EOF */
283
1.32k
            BIO *parent;
284
285
1.32k
#ifndef OPENSSL_NO_POSIX_IO
286
            /* continue processing with the next file from directory */
287
1.32k
            if (dirctx != NULL) {
288
710
                BIO *next;
289
290
710
                if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
291
82
                    BIO_vfree(in);
292
82
                    in = next;
293
82
                    goto read_retry;
294
628
                } else {
295
628
                    OPENSSL_free(dirpath);
296
628
                    dirpath = NULL;
297
628
                }
298
710
            }
299
1.24k
#endif
300
            /* no more files in directory, continue with processing parent */
301
1.24k
            if ((parent = sk_BIO_pop(biosk)) == NULL) {
302
                /* everything processed get out of the loop */
303
617
                break;
304
628
            } else {
305
628
                BIO_vfree(in);
306
628
                in = parent;
307
628
                goto read_retry;
308
628
            }
309
1.24k
        }
310
8.04M
        again = 0;
311
15.9M
        while (i > 0) {
312
8.22M
            if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
313
303k
                break;
314
7.92M
            else
315
7.92M
                i--;
316
8.22M
        }
317
        /*
318
         * we removed some trailing stuff so there is a new line on the end.
319
         */
320
8.04M
        if (ii && i == ii)
321
114k
            again = 1; /* long line */
322
7.92M
        else {
323
7.92M
            p[i] = '\0';
324
7.92M
            eline++; /* another input line */
325
7.92M
        }
326
327
        /* we now have a line with trailing \r\n removed */
328
329
        /* i is the number of bytes */
330
8.04M
        bufnum += i;
331
332
8.04M
        v = NULL;
333
        /* check for line continuation */
334
8.04M
        if (!again && bufnum >= 1) {
335
            /*
336
             * If we have bytes and the last char '\\' and second last char
337
             * is not '\\'
338
             */
339
190k
            p = &(buff->data[bufnum - 1]);
340
190k
            if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
341
1.02k
                bufnum--;
342
1.02k
                again = 1;
343
1.02k
            }
344
190k
        }
345
8.04M
        if (again)
346
115k
            continue;
347
7.92M
        bufnum = 0;
348
7.92M
        buf = buff->data;
349
350
7.92M
        clear_comments(conf, buf);
351
7.92M
        s = eat_ws(conf, buf);
352
7.92M
        if (IS_EOF(conf, *s))
353
7.82M
            continue; /* blank line */
354
98.0k
        if (*s == '[') {
355
11.1k
            char *ss;
356
357
11.1k
            s++;
358
11.1k
            start = eat_ws(conf, s);
359
11.1k
            ss = start;
360
11.5k
        again:
361
11.5k
            end = eat_alpha_numeric(conf, ss);
362
11.5k
            p = eat_ws(conf, end);
363
11.5k
            if (*p != ']') {
364
474
                if (*p != '\0' && ss != p) {
365
441
                    ss = p;
366
441
                    goto again;
367
441
                }
368
474
                ERR_raise(ERR_LIB_CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
369
33
                goto err;
370
474
            }
371
11.0k
            *end = '\0';
372
11.0k
            if (!str_copy(conf, NULL, &section, start))
373
0
                goto err;
374
11.0k
            if ((sv = _CONF_get_section(conf, section)) == NULL)
375
4.95k
                sv = _CONF_new_section(conf, section);
376
11.0k
            if (sv == NULL) {
377
0
                ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
378
0
                goto err;
379
0
            }
380
11.0k
            continue;
381
86.8k
        } else {
382
86.8k
            pname = s;
383
86.8k
            end = eat_alpha_numeric(conf, s);
384
86.8k
            if ((end[0] == ':') && (end[1] == ':')) {
385
9.21k
                *end = '\0';
386
9.21k
                end += 2;
387
9.21k
                psection = pname;
388
9.21k
                pname = end;
389
9.21k
                end = eat_alpha_numeric(conf, end);
390
77.6k
            } else {
391
77.6k
                psection = section;
392
77.6k
            }
393
86.8k
            p = eat_ws(conf, end);
394
86.8k
            if (strncmp(pname, ".pragma", 7) == 0
395
2.78k
                && (p != pname + 7 || *p == '=')) {
396
2.78k
                char *pval;
397
398
2.78k
                if (*p == '=') {
399
1.67k
                    p++;
400
1.67k
                    p = eat_ws(conf, p);
401
1.67k
                }
402
2.78k
                trim_ws(conf, p);
403
404
                /* Pragma values take the form keyword:value */
405
2.78k
                pval = strchr(p, ':');
406
2.78k
                if (pval == NULL || pval == p || pval[1] == '\0') {
407
15
                    ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
408
15
                    goto err;
409
15
                }
410
411
2.76k
                *pval++ = '\0';
412
2.76k
                trim_ws(conf, p);
413
2.76k
                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.76k
                if (strcmp(p, "dollarid") == 0) {
423
248
                    if (!parsebool(pval, &conf->flag_dollarid))
424
3
                        goto err;
425
2.51k
                } else if (strcmp(p, "abspath") == 0) {
426
965
                    if (!parsebool(pval, &conf->flag_abspath))
427
6
                        goto err;
428
1.55k
                } else if (strcmp(p, "includedir") == 0) {
429
270
                    OPENSSL_free(conf->includedir);
430
270
                    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
270
                }
435
436
                /*
437
                 * We *ignore* any unknown pragma.
438
                 */
439
2.75k
                continue;
440
84.1k
            } else if (strncmp(pname, ".include", 8) == 0
441
1.15k
                && (p != pname + 8 || *p == '=')) {
442
1.14k
                char *include = NULL;
443
1.14k
                BIO *next;
444
1.14k
                const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE");
445
1.14k
                char *include_path = NULL;
446
447
1.14k
#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.14k
                if (numincludes++ > 10)
458
8
                    goto err;
459
1.14k
#endif
460
461
1.14k
                if (include_dir == NULL)
462
1.14k
                    include_dir = conf->includedir;
463
464
1.14k
                if (*p == '=') {
465
108
                    p++;
466
108
                    p = eat_ws(conf, p);
467
108
                }
468
1.14k
                trim_ws(conf, p);
469
1.14k
                if (!str_copy(conf, psection, &include, p))
470
4
                    goto err;
471
472
1.13k
                if (include_dir != NULL && !ossl_is_absolute_path(include)) {
473
316
                    size_t newlen = strlen(include_dir) + strlen(include) + 2;
474
475
316
                    include_path = OPENSSL_malloc(newlen);
476
316
                    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
316
                    OPENSSL_strlcpy(include_path, include_dir, newlen);
483
316
                    if (!ossl_ends_with_dirsep(include_path))
484
275
                        OPENSSL_strlcat(include_path, "/", newlen);
485
316
                    OPENSSL_strlcat(include_path, include, newlen);
486
316
                    OPENSSL_free(include);
487
821
                } else {
488
821
                    include_path = include;
489
821
                }
490
491
1.13k
                if (conf->flag_abspath
492
19
                    && !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
1.13k
#ifndef OPENSSL_NO_POSIX_IO
500
1.13k
                next = process_include(include_path, &dirctx, &dirpath);
501
1.13k
                if (include_path != dirpath) {
502
                    /* dirpath will contain include in case of a directory */
503
506
                    OPENSSL_free(include_path);
504
506
                }
505
#else
506
                next = BIO_new_file(include_path, "r");
507
                OPENSSL_free(include_path);
508
#endif
509
510
1.13k
                if (next != NULL) {
511
                    /* push the currently processing BIO onto stack */
512
633
                    if (biosk == NULL) {
513
169
                        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
169
                    }
519
633
                    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
633
                    in = next;
526
633
                }
527
1.13k
                continue;
528
82.9k
            } else if (*p != '=') {
529
195
                ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN,
530
195
                    "HERE-->%s", p);
531
195
                goto err;
532
195
            }
533
82.7k
            *end = '\0';
534
82.7k
            p++;
535
82.7k
            start = eat_ws(conf, p);
536
82.7k
            trim_ws(conf, start);
537
538
82.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
82.7k
            v->name = OPENSSL_strdup(pname);
543
82.7k
            v->value = NULL;
544
82.7k
            if (v->name == NULL) {
545
0
                ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
546
0
                goto err;
547
0
            }
548
82.7k
            if (!str_copy(conf, psection, &(v->value), start))
549
166
                goto err;
550
551
82.6k
            if (strcmp(psection, section) != 0) {
552
9.20k
                if ((tv = _CONF_get_section(conf, psection))
553
9.20k
                    == NULL)
554
7.89k
                    tv = _CONF_new_section(conf, psection);
555
9.20k
                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
9.20k
            } else
561
73.3k
                tv = sv;
562
82.6k
            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
82.6k
            v = NULL;
567
82.6k
        }
568
98.0k
    }
569
617
    BUF_MEM_free(buff);
570
617
    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
617
    sk_BIO_free(biosk);
576
617
    return 1;
577
578
430
err:
579
430
    BUF_MEM_free(buff);
580
430
    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
435
    while (sk_BIO_num(biosk) > 0) {
587
5
        BIO *popped = sk_BIO_pop(biosk);
588
5
        BIO_vfree(in);
589
5
        in = popped;
590
5
    }
591
430
    sk_BIO_free(biosk);
592
430
#ifndef OPENSSL_NO_POSIX_IO
593
430
    OPENSSL_free(dirpath);
594
430
    if (dirctx != NULL)
595
3
        OPENSSL_DIR_end(&dirctx);
596
430
#endif
597
430
    if (line != NULL)
598
430
        *line = eline;
599
430
    BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
600
430
    ERR_add_error_data(2, "line ", btmp);
601
430
    if (h != conf->data) {
602
430
        CONF_free(conf->data);
603
430
        conf->data = NULL;
604
430
    }
605
430
    if (v != NULL) {
606
166
        OPENSSL_free(v->name);
607
166
        OPENSSL_free(v->value);
608
166
        OPENSSL_free(v);
609
166
    }
610
430
    return 0;
611
1.04k
}
612
613
static void clear_comments(CONF *conf, char *p)
614
35.6M
{
615
35.7M
    for (;;) {
616
35.7M
        if (IS_FCOMMENT(conf, *p)) {
617
0
            *p = '\0';
618
0
            return;
619
0
        }
620
35.7M
        if (!IS_WS(conf, *p)) {
621
35.6M
            break;
622
35.6M
        }
623
58.2k
        p++;
624
58.2k
    }
625
626
325M
    for (;;) {
627
325M
        if (IS_COMMENT(conf, *p)) {
628
296k
            *p = '\0';
629
296k
            return;
630
296k
        }
631
324M
        if (IS_DQUOTE(conf, *p)) {
632
0
            p = scan_dquote(conf, p);
633
0
            continue;
634
0
        }
635
324M
        if (IS_QUOTE(conf, *p)) {
636
497k
            p = scan_quote(conf, p);
637
497k
            continue;
638
497k
        }
639
324M
        if (IS_ESC(conf, *p)) {
640
1.14M
            p = scan_esc(conf, p);
641
1.14M
            continue;
642
1.14M
        }
643
323M
        if (IS_EOF(conf, *p))
644
35.3M
            return;
645
287M
        else
646
287M
            p++;
647
323M
    }
648
35.6M
}
649
650
static int str_copy(CONF *conf, char *section, char **pto, char *from)
651
407k
{
652
407k
    int q, r, rr = 0, to = 0, len = 0;
653
407k
    char *s, *e, *rp, *p, *rrp, *np, *cp, v;
654
407k
    BUF_MEM *buf;
655
656
407k
    if ((buf = BUF_MEM_new()) == NULL)
657
0
        return 0;
658
659
407k
    len = strlen(from) + 1;
660
407k
    if (!BUF_MEM_grow(buf, len))
661
0
        goto err;
662
663
89.5M
    for (;;) {
664
89.5M
        if (IS_QUOTE(conf, *from)) {
665
47.1k
            q = *from;
666
47.1k
            from++;
667
29.1M
            while (!IS_EOF(conf, *from) && (*from != q)) {
668
29.1M
                if (IS_ESC(conf, *from)) {
669
81.2k
                    from++;
670
81.2k
                    if (IS_EOF(conf, *from))
671
3.44k
                        break;
672
81.2k
                }
673
29.1M
                buf->data[to++] = *(from++);
674
29.1M
            }
675
47.1k
            if (*from == q)
676
39.4k
                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
266k
            from++;
694
266k
            v = *(from++);
695
266k
            if (IS_EOF(conf, v))
696
10.7k
                break;
697
256k
            else if (v == 'r')
698
66.8k
                v = '\r';
699
189k
            else if (v == 'n')
700
7.12k
                v = '\n';
701
182k
            else if (v == 'b')
702
4.42k
                v = '\b';
703
177k
            else if (v == 't')
704
4.23k
                v = '\t';
705
256k
            buf->data[to++] = v;
706
89.2M
        } else if (IS_EOF(conf, *from))
707
394k
            break;
708
88.8M
        else if (*from == '$'
709
111k
            && (!conf->flag_dollarid
710
20.2k
                || from[1] == '{'
711
97.9k
                || from[1] == '(')) {
712
97.9k
            size_t newsize;
713
714
            /* try to expand it */
715
97.9k
            rrp = NULL;
716
97.9k
            s = &(from[1]);
717
97.9k
            if (*s == '{')
718
5.22k
                q = '}';
719
92.7k
            else if (*s == '(')
720
4.86k
                q = ')';
721
87.9k
            else
722
87.9k
                q = 0;
723
724
97.9k
            if (q)
725
10.0k
                s++;
726
97.9k
            cp = section;
727
97.9k
            e = np = s;
728
20.0M
            while (IS_ALNUM(conf, *e)
729
99.1k
                || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
730
19.9M
                e++;
731
97.9k
            if ((e[0] == ':') && (e[1] == ':')) {
732
6.08k
                cp = np;
733
6.08k
                rrp = e;
734
6.08k
                rr = *e;
735
6.08k
                *rrp = '\0';
736
6.08k
                e += 2;
737
6.08k
                np = e;
738
10.4k
                while (IS_ALNUM(conf, *e)
739
7.17k
                    || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
740
4.39k
                    e++;
741
6.08k
            }
742
97.9k
            r = *e;
743
97.9k
            *e = '\0';
744
97.9k
            rp = e;
745
97.9k
            if (q) {
746
10.0k
                if (r != q) {
747
352
                    ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE);
748
352
                    goto err;
749
352
                }
750
9.73k
                e++;
751
9.73k
            }
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
97.6k
            p = _CONF_get_string(conf, cp, np);
763
97.6k
            if (rrp != NULL)
764
6.04k
                *rrp = rr;
765
97.6k
            *rp = r;
766
97.6k
            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
96.1k
            newsize = strlen(p) + buf->length - (e - from);
771
96.1k
            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
96.0k
            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
35.0M
            while (*p)
780
34.9M
                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
96.0k
            len -= e - from;
787
96.0k
            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
96.0k
            *rp = r;
795
96.0k
        } else
796
88.7M
            buf->data[to++] = *(from++);
797
89.5M
    }
798
405k
    buf->data[to] = '\0';
799
405k
    OPENSSL_free(*pto);
800
405k
    *pto = buf->data;
801
405k
    OPENSSL_free(buf);
802
405k
    return 1;
803
1.93k
err:
804
1.93k
    BUF_MEM_free(buf);
805
1.93k
    return 0;
806
407k
}
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.31k
{
817
6.31k
    struct stat st;
818
6.31k
    BIO *next;
819
820
6.31k
    if (stat(include, &st) < 0) {
821
2.99k
        ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include);
822
        /* missing include file is not fatal error */
823
2.99k
        return NULL;
824
2.99k
    }
825
826
3.32k
    if (S_ISDIR(st.st_mode)) {
827
3.26k
        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.26k
        if ((next = get_next_file(include, dirctx)) != NULL)
834
1.88k
            *dirpath = include;
835
3.26k
        return next;
836
3.26k
    }
837
838
64
    next = BIO_new_file(include, "r");
839
64
    return next;
840
3.32k
}
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.57k
{
848
5.57k
    const char *filename;
849
5.57k
    size_t pathlen;
850
851
5.57k
    pathlen = strlen(path);
852
248k
    while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
853
245k
        size_t namelen;
854
855
245k
        namelen = strlen(filename);
856
857
245k
        if ((namelen > 5
858
190k
                && OPENSSL_strcasecmp(filename + namelen - 5, ".conf") == 0)
859
235k
            || (namelen > 4
860
201k
                && OPENSSL_strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
861
11.2k
            size_t newlen;
862
11.2k
            char *newpath;
863
11.2k
            BIO *bio;
864
865
11.2k
            newlen = pathlen + namelen + 2;
866
11.2k
            newpath = OPENSSL_zalloc(newlen);
867
11.2k
            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.2k
            if (newpath[0] == '\0') {
884
11.2k
                OPENSSL_strlcpy(newpath, path, newlen);
885
11.2k
                OPENSSL_strlcat(newpath, "/", newlen);
886
11.2k
            }
887
11.2k
            OPENSSL_strlcat(newpath, filename, newlen);
888
889
11.2k
            bio = BIO_new_file(newpath, "r");
890
11.2k
            OPENSSL_free(newpath);
891
            /* Errors when opening files are non-fatal. */
892
11.2k
            if (bio != NULL)
893
2.43k
                return bio;
894
11.2k
        }
895
245k
    }
896
3.14k
    OPENSSL_DIR_end(dirctx);
897
3.14k
    *dirctx = NULL;
898
3.14k
    return NULL;
899
5.57k
}
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
159M
        return 0;
921
159M
    }
922
923
2.59G
    return (keytypes[key] & type) ? 1 : 0;
924
2.75G
}
925
926
static char *eat_ws(CONF *conf, char *p)
927
36.5M
{
928
36.9M
    while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
929
431k
        p++;
930
36.5M
    return p;
931
36.5M
}
932
933
static void trim_ws(CONF *conf, char *start)
934
407k
{
935
407k
    char *p = start;
936
937
172M
    while (!IS_EOF(conf, *p))
938
172M
        p++;
939
407k
    p--;
940
494k
    while ((p >= start) && IS_WS(conf, *p))
941
87.5k
        p--;
942
407k
    p++;
943
407k
    *p = '\0';
944
407k
}
945
946
static char *eat_alpha_numeric(CONF *conf, char *p)
947
503k
{
948
127M
    for (;;) {
949
127M
        if (IS_ESC(conf, *p)) {
950
711k
            p = scan_esc(conf, p);
951
711k
            continue;
952
711k
        }
953
126M
        if (!(IS_ALNUM_PUNCT(conf, *p)
954
506k
                || (conf->flag_dollarid && IS_DOLLAR(conf, *p))))
955
503k
            return p;
956
126M
        p++;
957
126M
    }
958
503k
}
959
960
static char *scan_quote(CONF *conf, char *p)
961
497k
{
962
497k
    int q = *p;
963
964
497k
    p++;
965
60.0M
    while (!(IS_EOF(conf, *p)) && (*p != q)) {
966
59.5M
        if (IS_ESC(conf, *p)) {
967
179k
            p++;
968
179k
            if (IS_EOF(conf, *p))
969
3.31k
                return p;
970
179k
        }
971
59.5M
        p++;
972
59.5M
    }
973
494k
    if (*p == q)
974
487k
        p++;
975
494k
    return p;
976
497k
}
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
}