Coverage Report

Created: 2025-12-31 06:58

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
95.0k
#define MAX_CONF_VALUE_LENGTH 65536
44
45
static int is_keytype(const CONF *conf, char c, unsigned short type);
46
static char *eat_ws(CONF *conf, char *p);
47
static void trim_ws(CONF *conf, char *start);
48
static char *eat_alpha_numeric(CONF *conf, char *p);
49
static void clear_comments(CONF *conf, char *p);
50
static int str_copy(CONF *conf, char *section, char **to, char *from);
51
static char *scan_quote(CONF *conf, char *p);
52
static char *scan_dquote(CONF *conf, char *p);
53
1.37M
#define scan_esc(conf, p) (((IS_EOF((conf), (p)[1])) ? ((p) + 1) : ((p) + 2)))
54
#ifndef OPENSSL_NO_POSIX_IO
55
static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
56
    char **dirpath);
57
static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx);
58
#endif
59
60
static CONF *def_create(CONF_METHOD *meth);
61
static int def_init_default(CONF *conf);
62
#ifndef OPENSSL_NO_DEPRECATED_3_0
63
static int def_init_WIN32(CONF *conf);
64
#endif
65
static int def_destroy(CONF *conf);
66
static int def_destroy_data(CONF *conf);
67
static int def_load(CONF *conf, const char *name, long *eline);
68
static int def_load_bio(CONF *conf, BIO *bp, long *eline);
69
static int def_dump(const CONF *conf, BIO *bp);
70
static int def_is_number(const CONF *conf, char c);
71
static int def_to_int(const CONF *conf, char c);
72
73
static CONF_METHOD default_method = {
74
    "OpenSSL default",
75
    def_create,
76
    def_init_default,
77
    def_destroy,
78
    def_destroy_data,
79
    def_load_bio,
80
    def_dump,
81
    def_is_number,
82
    def_to_int,
83
    def_load
84
};
85
86
CONF_METHOD *NCONF_default(void)
87
16.9k
{
88
16.9k
    return &default_method;
89
16.9k
}
90
91
#ifndef OPENSSL_NO_DEPRECATED_3_0
92
static CONF_METHOD WIN32_method = {
93
    "WIN32",
94
    def_create,
95
    def_init_WIN32,
96
    def_destroy,
97
    def_destroy_data,
98
    def_load_bio,
99
    def_dump,
100
    def_is_number,
101
    def_to_int,
102
    def_load
103
};
104
105
CONF_METHOD *NCONF_WIN32(void)
106
0
{
107
0
    return &WIN32_method;
108
0
}
109
#endif
110
111
static CONF *def_create(CONF_METHOD *meth)
112
16.9k
{
113
16.9k
    CONF *ret;
114
115
16.9k
    ret = OPENSSL_malloc(sizeof(*ret));
116
16.9k
    if (ret != NULL)
117
16.9k
        if (meth->init(ret) == 0) {
118
0
            OPENSSL_free(ret);
119
0
            ret = NULL;
120
0
        }
121
16.9k
    return ret;
122
16.9k
}
123
124
static int def_init_default(CONF *conf)
125
23.6k
{
126
23.6k
    if (conf == NULL)
127
0
        return 0;
128
129
23.6k
    memset(conf, 0, sizeof(*conf));
130
23.6k
    conf->meth = &default_method;
131
23.6k
    conf->meth_data = (void *)CONF_type_default;
132
133
23.6k
    return 1;
134
23.6k
}
135
136
#ifndef OPENSSL_NO_DEPRECATED_3_0
137
static int def_init_WIN32(CONF *conf)
138
0
{
139
0
    if (conf == NULL)
140
0
        return 0;
141
142
0
    memset(conf, 0, sizeof(*conf));
143
0
    conf->meth = &WIN32_method;
144
0
    conf->meth_data = (void *)CONF_type_win32;
145
146
0
    return 1;
147
0
}
148
#endif
149
150
static int def_destroy(CONF *conf)
151
16.9k
{
152
16.9k
    if (def_destroy_data(conf)) {
153
16.9k
        OPENSSL_free(conf);
154
16.9k
        return 1;
155
16.9k
    }
156
0
    return 0;
157
16.9k
}
158
159
static int def_destroy_data(CONF *conf)
160
23.6k
{
161
23.6k
    if (conf == NULL)
162
0
        return 0;
163
23.6k
    _CONF_free_data(conf);
164
23.6k
    return 1;
165
23.6k
}
166
167
static int def_load(CONF *conf, const char *name, long *line)
168
185
{
169
185
    int ret;
170
185
    BIO *in = NULL;
171
172
#ifdef OPENSSL_SYS_VMS
173
    in = BIO_new_file(name, "r");
174
#else
175
185
    in = BIO_new_file(name, "rb");
176
185
#endif
177
185
    if (in == NULL) {
178
185
        if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
179
185
            ERR_raise(ERR_LIB_CONF, CONF_R_NO_SUCH_FILE);
180
0
        else
181
185
            ERR_raise(ERR_LIB_CONF, ERR_R_SYS_LIB);
182
185
        return 0;
183
185
    }
184
185
0
    ret = def_load_bio(conf, in, line);
186
0
    BIO_free(in);
187
188
0
    return ret;
189
185
}
190
191
/* Parse a boolean value and fill in *flag. Return 0 on error. */
192
static int parsebool(const char *pval, int *flag)
193
8.51k
{
194
8.51k
    if (OPENSSL_strcasecmp(pval, "on") == 0
195
5.64k
        || OPENSSL_strcasecmp(pval, "true") == 0) {
196
5.64k
        *flag = 1;
197
5.64k
    } else if (OPENSSL_strcasecmp(pval, "off") == 0
198
2.61k
        || OPENSSL_strcasecmp(pval, "false") == 0) {
199
2.61k
        *flag = 0;
200
2.61k
    } else {
201
263
        ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
202
263
        return 0;
203
263
    }
204
8.25k
    return 1;
205
8.51k
}
206
207
static int def_load_bio(CONF *conf, BIO *in, long *line)
208
1.05k
{
209
/* The macro BUFSIZE conflicts with a system macro in VxWorks */
210
25.4M
#define CONFBUFSIZE 512
211
1.05k
    int bufnum = 0, i, ii;
212
1.05k
    BUF_MEM *buff = NULL;
213
1.05k
    char *s, *p, *end;
214
1.05k
    int again;
215
1.05k
    int first_call = 1;
216
1.05k
    long eline = 0;
217
1.05k
    char btmp[DECIMAL_SIZE(eline) + 1];
218
1.05k
    CONF_VALUE *v = NULL, *tv;
219
1.05k
    CONF_VALUE *sv = NULL;
220
1.05k
    char *section = NULL, *buf;
221
1.05k
    char *start, *psection, *pname;
222
1.05k
    void *h = (void *)(conf->data);
223
1.05k
    STACK_OF(BIO) *biosk = NULL;
224
1.05k
#ifndef OPENSSL_NO_POSIX_IO
225
1.05k
    char *dirpath = NULL;
226
1.05k
    OPENSSL_DIR_CTX *dirctx = NULL;
227
1.05k
#endif
228
1.05k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
229
1.05k
    int numincludes = 0;
230
1.05k
#endif
231
232
1.05k
    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.05k
    section = OPENSSL_strdup("default");
238
1.05k
    if (section == NULL) {
239
0
        ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
240
0
        goto err;
241
0
    }
242
243
1.05k
    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.05k
    sv = _CONF_new_section(conf, section);
249
1.05k
    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.05k
    bufnum = 0;
255
1.05k
    again = 0;
256
8.49M
    for (;;) {
257
8.49M
        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.49M
        p = &(buff->data[bufnum]);
262
8.49M
        *p = '\0';
263
8.49M
    read_retry:
264
8.49M
        if (in != NULL && BIO_gets(in, p, CONFBUFSIZE - 1) < 0)
265
0
            goto err;
266
8.49M
        p[CONFBUFSIZE - 1] = '\0';
267
8.49M
        ii = i = strlen(p);
268
8.49M
        if (first_call) {
269
            /* Other BOMs imply unsupported multibyte encoding,
270
             * so don't strip them and let the error raise */
271
1.05k
            const unsigned char utf8_bom[3] = { 0xEF, 0xBB, 0xBF };
272
273
1.05k
            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.05k
            first_call = 0;
280
1.05k
        }
281
8.49M
        if (i == 0 && !again) {
282
            /* the currently processed BIO is NULL or at EOF */
283
1.38k
            BIO *parent;
284
285
1.38k
#ifndef OPENSSL_NO_POSIX_IO
286
            /* continue processing with the next file from directory */
287
1.38k
            if (dirctx != NULL) {
288
759
                BIO *next;
289
290
759
                if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
291
93
                    BIO_vfree(in);
292
93
                    in = next;
293
93
                    goto read_retry;
294
666
                } else {
295
666
                    OPENSSL_free(dirpath);
296
666
                    dirpath = NULL;
297
666
                }
298
759
            }
299
1.28k
#endif
300
            /* no more files in directory, continue with processing parent */
301
1.28k
            if ((parent = sk_BIO_pop(biosk)) == NULL) {
302
                /* everything processed get out of the loop */
303
621
                break;
304
666
            } else {
305
666
                BIO_vfree(in);
306
666
                in = parent;
307
666
                goto read_retry;
308
666
            }
309
1.28k
        }
310
8.49M
        again = 0;
311
16.8M
        while (i > 0) {
312
8.69M
            if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
313
304k
                break;
314
8.39M
            else
315
8.39M
                i--;
316
8.69M
        }
317
        /*
318
         * we removed some trailing stuff so there is a new line on the end.
319
         */
320
8.49M
        if (ii && i == ii)
321
104k
            again = 1; /* long line */
322
8.39M
        else {
323
8.39M
            p[i] = '\0';
324
8.39M
            eline++; /* another input line */
325
8.39M
        }
326
327
        /* we now have a line with trailing \r\n removed */
328
329
        /* i is the number of bytes */
330
8.49M
        bufnum += i;
331
332
8.49M
        v = NULL;
333
        /* check for line continuation */
334
8.49M
        if (!again && bufnum >= 1) {
335
            /*
336
             * If we have bytes and the last char '\\' and second last char
337
             * is not '\\'
338
             */
339
201k
            p = &(buff->data[bufnum - 1]);
340
201k
            if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
341
1.00k
                bufnum--;
342
1.00k
                again = 1;
343
1.00k
            }
344
201k
        }
345
8.49M
        if (again)
346
105k
            continue;
347
8.39M
        bufnum = 0;
348
8.39M
        buf = buff->data;
349
350
8.39M
        clear_comments(conf, buf);
351
8.39M
        s = eat_ws(conf, buf);
352
8.39M
        if (IS_EOF(conf, *s))
353
8.28M
            continue; /* blank line */
354
102k
        if (*s == '[') {
355
11.8k
            char *ss;
356
357
11.8k
            s++;
358
11.8k
            start = eat_ws(conf, s);
359
11.8k
            ss = start;
360
12.0k
        again:
361
12.0k
            end = eat_alpha_numeric(conf, ss);
362
12.0k
            p = eat_ws(conf, end);
363
12.0k
            if (*p != ']') {
364
280
                if (*p != '\0' && ss != p) {
365
245
                    ss = p;
366
245
                    goto again;
367
245
                }
368
280
                ERR_raise(ERR_LIB_CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
369
35
                goto err;
370
280
            }
371
11.7k
            *end = '\0';
372
11.7k
            if (!str_copy(conf, NULL, &section, start))
373
0
                goto err;
374
11.7k
            if ((sv = _CONF_get_section(conf, section)) == NULL)
375
5.36k
                sv = _CONF_new_section(conf, section);
376
11.7k
            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.7k
            continue;
381
90.4k
        } else {
382
90.4k
            pname = s;
383
90.4k
            end = eat_alpha_numeric(conf, s);
384
90.4k
            if ((end[0] == ':') && (end[1] == ':')) {
385
9.43k
                *end = '\0';
386
9.43k
                end += 2;
387
9.43k
                psection = pname;
388
9.43k
                pname = end;
389
9.43k
                end = eat_alpha_numeric(conf, end);
390
81.0k
            } else {
391
81.0k
                psection = section;
392
81.0k
            }
393
90.4k
            p = eat_ws(conf, end);
394
90.4k
            if (strncmp(pname, ".pragma", 7) == 0
395
2.83k
                && (p != pname + 7 || *p == '=')) {
396
2.83k
                char *pval;
397
398
2.83k
                if (*p == '=') {
399
1.71k
                    p++;
400
1.71k
                    p = eat_ws(conf, p);
401
1.71k
                }
402
2.83k
                trim_ws(conf, p);
403
404
                /* Pragma values take the form keyword:value */
405
2.83k
                pval = strchr(p, ':');
406
2.83k
                if (pval == NULL || pval == p || pval[1] == '\0') {
407
16
                    ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
408
16
                    goto err;
409
16
                }
410
411
2.81k
                *pval++ = '\0';
412
2.81k
                trim_ws(conf, p);
413
2.81k
                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.81k
                if (strcmp(p, "dollarid") == 0) {
423
263
                    if (!parsebool(pval, &conf->flag_dollarid))
424
2
                        goto err;
425
2.55k
                } else if (strcmp(p, "abspath") == 0) {
426
962
                    if (!parsebool(pval, &conf->flag_abspath))
427
6
                        goto err;
428
1.59k
                } else if (strcmp(p, "includedir") == 0) {
429
299
                    OPENSSL_free(conf->includedir);
430
299
                    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
299
                }
435
436
                /*
437
                 * We *ignore* any unknown pragma.
438
                 */
439
2.81k
                continue;
440
87.6k
            } else if (strncmp(pname, ".include", 8) == 0
441
1.21k
                && (p != pname + 8 || *p == '=')) {
442
1.21k
                char *include = NULL;
443
1.21k
                BIO *next;
444
1.21k
                const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE");
445
1.21k
                char *include_path = NULL;
446
447
1.21k
#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.21k
                if (numincludes++ > 10)
458
7
                    goto err;
459
1.20k
#endif
460
461
1.20k
                if (include_dir == NULL)
462
1.20k
                    include_dir = conf->includedir;
463
464
1.20k
                if (*p == '=') {
465
143
                    p++;
466
143
                    p = eat_ws(conf, p);
467
143
                }
468
1.20k
                trim_ws(conf, p);
469
1.20k
                if (!str_copy(conf, psection, &include, p))
470
4
                    goto err;
471
472
1.20k
                if (include_dir != NULL && !ossl_is_absolute_path(include)) {
473
394
                    size_t newlen = strlen(include_dir) + strlen(include) + 2;
474
475
394
                    include_path = OPENSSL_malloc(newlen);
476
394
                    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
394
                    OPENSSL_strlcpy(include_path, include_dir, newlen);
483
394
                    if (!ossl_ends_with_dirsep(include_path))
484
352
                        OPENSSL_strlcat(include_path, "/", newlen);
485
394
                    OPENSSL_strlcat(include_path, include, newlen);
486
394
                    OPENSSL_free(include);
487
810
                } else {
488
810
                    include_path = include;
489
810
                }
490
491
1.20k
                if (conf->flag_abspath
492
15
                    && !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.20k
#ifndef OPENSSL_NO_POSIX_IO
500
1.20k
                next = process_include(include_path, &dirctx, &dirpath);
501
1.20k
                if (include_path != dirpath) {
502
                    /* dirpath will contain include in case of a directory */
503
525
                    OPENSSL_free(include_path);
504
525
                }
505
#else
506
                next = BIO_new_file(include_path, "r");
507
                OPENSSL_free(include_path);
508
#endif
509
510
1.20k
                if (next != NULL) {
511
                    /* push the currently processing BIO onto stack */
512
681
                    if (biosk == NULL) {
513
195
                        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
195
                    }
519
681
                    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
681
                    in = next;
526
681
                }
527
1.20k
                continue;
528
86.4k
            } else if (*p != '=') {
529
206
                ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN,
530
206
                    "HERE-->%s", p);
531
206
                goto err;
532
206
            }
533
86.2k
            *end = '\0';
534
86.2k
            p++;
535
86.2k
            start = eat_ws(conf, p);
536
86.2k
            trim_ws(conf, start);
537
538
86.2k
            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
86.2k
            v->name = OPENSSL_strdup(pname);
543
86.2k
            v->value = NULL;
544
86.2k
            if (v->name == NULL) {
545
0
                ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
546
0
                goto err;
547
0
            }
548
86.2k
            if (!str_copy(conf, psection, &(v->value), start))
549
156
                goto err;
550
551
86.0k
            if (strcmp(psection, section) != 0) {
552
9.42k
                if ((tv = _CONF_get_section(conf, psection))
553
9.42k
                    == NULL)
554
8.17k
                    tv = _CONF_new_section(conf, psection);
555
9.42k
                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.42k
            } else
561
76.6k
                tv = sv;
562
86.0k
            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
86.0k
            v = NULL;
567
86.0k
        }
568
102k
    }
569
621
    BUF_MEM_free(buff);
570
621
    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
621
    sk_BIO_free(biosk);
576
621
    return 1;
577
578
432
err:
579
432
    BUF_MEM_free(buff);
580
432
    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
447
    while (sk_BIO_num(biosk) > 0) {
587
15
        BIO *popped = sk_BIO_pop(biosk);
588
15
        BIO_vfree(in);
589
15
        in = popped;
590
15
    }
591
432
    sk_BIO_free(biosk);
592
432
#ifndef OPENSSL_NO_POSIX_IO
593
432
    OPENSSL_free(dirpath);
594
432
    if (dirctx != NULL)
595
13
        OPENSSL_DIR_end(&dirctx);
596
432
#endif
597
432
    if (line != NULL)
598
432
        *line = eline;
599
432
    BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
600
432
    ERR_add_error_data(2, "line ", btmp);
601
432
    if (h != conf->data) {
602
432
        CONF_free(conf->data);
603
432
        conf->data = NULL;
604
432
    }
605
432
    if (v != NULL) {
606
156
        OPENSSL_free(v->name);
607
156
        OPENSSL_free(v->value);
608
156
        OPENSSL_free(v);
609
156
    }
610
432
    return 0;
611
1.05k
}
612
613
static void clear_comments(CONF *conf, char *p)
614
37.6M
{
615
37.7M
    for (;;) {
616
37.7M
        if (IS_FCOMMENT(conf, *p)) {
617
0
            *p = '\0';
618
0
            return;
619
0
        }
620
37.7M
        if (!IS_WS(conf, *p)) {
621
37.6M
            break;
622
37.6M
        }
623
60.7k
        p++;
624
60.7k
    }
625
626
311M
    for (;;) {
627
311M
        if (IS_COMMENT(conf, *p)) {
628
309k
            *p = '\0';
629
309k
            return;
630
309k
        }
631
311M
        if (IS_DQUOTE(conf, *p)) {
632
0
            p = scan_dquote(conf, p);
633
0
            continue;
634
0
        }
635
311M
        if (IS_QUOTE(conf, *p)) {
636
42.0k
            p = scan_quote(conf, p);
637
42.0k
            continue;
638
42.0k
        }
639
311M
        if (IS_ESC(conf, *p)) {
640
871k
            p = scan_esc(conf, p);
641
871k
            continue;
642
871k
        }
643
310M
        if (IS_EOF(conf, *p))
644
37.3M
            return;
645
273M
        else
646
273M
            p++;
647
310M
    }
648
37.6M
}
649
650
static int str_copy(CONF *conf, char *section, char **pto, char *from)
651
400k
{
652
400k
    int q, r, rr = 0, to = 0, len = 0;
653
400k
    char *s, *e, *rp, *p, *rrp, *np, *cp, v;
654
400k
    BUF_MEM *buf;
655
656
400k
    if ((buf = BUF_MEM_new()) == NULL)
657
0
        return 0;
658
659
400k
    len = strlen(from) + 1;
660
400k
    if (!BUF_MEM_grow(buf, len))
661
0
        goto err;
662
663
87.2M
    for (;;) {
664
87.2M
        if (IS_QUOTE(conf, *from)) {
665
32.8k
            q = *from;
666
32.8k
            from++;
667
26.9M
            while (!IS_EOF(conf, *from) && (*from != q)) {
668
26.9M
                if (IS_ESC(conf, *from)) {
669
91.4k
                    from++;
670
91.4k
                    if (IS_EOF(conf, *from))
671
2.84k
                        break;
672
91.4k
                }
673
26.9M
                buf->data[to++] = *(from++);
674
26.9M
            }
675
32.8k
            if (*from == q)
676
25.6k
                from++;
677
87.2M
        } 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
87.2M
        } else if (IS_ESC(conf, *from)) {
693
289k
            from++;
694
289k
            v = *(from++);
695
289k
            if (IS_EOF(conf, v))
696
10.6k
                break;
697
278k
            else if (v == 'r')
698
60.4k
                v = '\r';
699
218k
            else if (v == 'n')
700
10.9k
                v = '\n';
701
207k
            else if (v == 'b')
702
5.79k
                v = '\b';
703
201k
            else if (v == 't')
704
4.65k
                v = '\t';
705
278k
            buf->data[to++] = v;
706
86.9M
        } else if (IS_EOF(conf, *from))
707
388k
            break;
708
86.5M
        else if (*from == '$'
709
143k
            && (!conf->flag_dollarid
710
51.8k
                || from[1] == '{'
711
96.8k
                || from[1] == '(')) {
712
96.8k
            size_t newsize;
713
714
            /* try to expand it */
715
96.8k
            rrp = NULL;
716
96.8k
            s = &(from[1]);
717
96.8k
            if (*s == '{')
718
4.65k
                q = '}';
719
92.2k
            else if (*s == '(')
720
3.60k
                q = ')';
721
88.6k
            else
722
88.6k
                q = 0;
723
724
96.8k
            if (q)
725
8.25k
                s++;
726
96.8k
            cp = section;
727
96.8k
            e = np = s;
728
19.6M
            while (IS_ALNUM(conf, *e)
729
97.9k
                || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
730
19.5M
                e++;
731
96.8k
            if ((e[0] == ':') && (e[1] == ':')) {
732
5.95k
                cp = np;
733
5.95k
                rrp = e;
734
5.95k
                rr = *e;
735
5.95k
                *rrp = '\0';
736
5.95k
                e += 2;
737
5.95k
                np = e;
738
11.9k
                while (IS_ALNUM(conf, *e)
739
7.03k
                    || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
740
6.01k
                    e++;
741
5.95k
            }
742
96.8k
            r = *e;
743
96.8k
            *e = '\0';
744
96.8k
            rp = e;
745
96.8k
            if (q) {
746
8.25k
                if (r != q) {
747
351
                    ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE);
748
351
                    goto err;
749
351
                }
750
7.90k
                e++;
751
7.90k
            }
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
96.5k
            p = _CONF_get_string(conf, cp, np);
763
96.5k
            if (rrp != NULL)
764
5.90k
                *rrp = rr;
765
96.5k
            *rp = r;
766
96.5k
            if (p == NULL) {
767
1.45k
                ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_HAS_NO_VALUE);
768
1.45k
                goto err;
769
1.45k
            }
770
95.0k
            newsize = strlen(p) + buf->length - (e - from);
771
95.0k
            if (newsize > MAX_CONF_VALUE_LENGTH) {
772
91
                ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
773
91
                goto err;
774
91
            }
775
94.9k
            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
33.2M
            while (*p)
780
33.1M
                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
94.9k
            len -= e - from;
787
94.9k
            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
94.9k
            *rp = r;
795
94.9k
        } else
796
86.4M
            buf->data[to++] = *(from++);
797
87.2M
    }
798
398k
    buf->data[to] = '\0';
799
398k
    OPENSSL_free(*pto);
800
398k
    *pto = buf->data;
801
398k
    OPENSSL_free(buf);
802
398k
    return 1;
803
1.89k
err:
804
1.89k
    BUF_MEM_free(buf);
805
1.89k
    return 0;
806
400k
}
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.27k
{
817
6.27k
    struct stat st;
818
6.27k
    BIO *next;
819
820
6.27k
    if (stat(include, &st) < 0) {
821
2.79k
        ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include);
822
        /* missing include file is not fatal error */
823
2.79k
        return NULL;
824
2.79k
    }
825
826
3.48k
    if (S_ISDIR(st.st_mode)) {
827
3.40k
        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.40k
        if ((next = get_next_file(include, dirctx)) != NULL)
834
2.01k
            *dirpath = include;
835
3.40k
        return next;
836
3.40k
    }
837
838
71
    next = BIO_new_file(include, "r");
839
71
    return next;
840
3.48k
}
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.84k
{
848
5.84k
    const char *filename;
849
5.84k
    size_t pathlen;
850
851
5.84k
    pathlen = strlen(path);
852
245k
    while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
853
242k
        size_t namelen;
854
855
242k
        namelen = strlen(filename);
856
857
242k
        if ((namelen > 5
858
187k
                && OPENSSL_strcasecmp(filename + namelen - 5, ".conf") == 0)
859
232k
            || (namelen > 4
860
198k
                && 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.63k
                return bio;
894
11.2k
        }
895
242k
    }
896
3.21k
    OPENSSL_DIR_end(dirctx);
897
3.21k
    *dirctx = NULL;
898
3.21k
    return NULL;
899
5.84k
}
900
#endif
901
902
static int is_keytype(const CONF *conf, char c, unsigned short type)
903
2.63G
{
904
2.63G
    const unsigned short *keytypes = (const unsigned short *)conf->meth_data;
905
2.63G
    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.63G
    if (key > 127) {
919
        /* key is not a seven bit ascii character */
920
162M
        return 0;
921
162M
    }
922
923
2.47G
    return (keytypes[key] & type) ? 1 : 0;
924
2.63G
}
925
926
static char *eat_ws(CONF *conf, char *p)
927
38.4M
{
928
38.9M
    while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
929
443k
        p++;
930
38.4M
    return p;
931
38.4M
}
932
933
static void trim_ws(CONF *conf, char *start)
934
398k
{
935
398k
    char *p = start;
936
937
158M
    while (!IS_EOF(conf, *p))
938
158M
        p++;
939
398k
    p--;
940
489k
    while ((p >= start) && IS_WS(conf, *p))
941
90.8k
        p--;
942
398k
    p++;
943
398k
    *p = '\0';
944
398k
}
945
946
static char *eat_alpha_numeric(CONF *conf, char *p)
947
493k
{
948
121M
    for (;;) {
949
121M
        if (IS_ESC(conf, *p)) {
950
498k
            p = scan_esc(conf, p);
951
498k
            continue;
952
498k
        }
953
120M
        if (!(IS_ALNUM_PUNCT(conf, *p)
954
496k
                || (conf->flag_dollarid && IS_DOLLAR(conf, *p))))
955
493k
            return p;
956
120M
        p++;
957
120M
    }
958
493k
}
959
960
static char *scan_quote(CONF *conf, char *p)
961
42.0k
{
962
42.0k
    int q = *p;
963
964
42.0k
    p++;
965
50.3M
    while (!(IS_EOF(conf, *p)) && (*p != q)) {
966
50.3M
        if (IS_ESC(conf, *p)) {
967
167k
            p++;
968
167k
            if (IS_EOF(conf, *p))
969
1.75k
                return p;
970
167k
        }
971
50.3M
        p++;
972
50.3M
    }
973
40.2k
    if (*p == q)
974
33.6k
        p++;
975
40.2k
    return p;
976
42.0k
}
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
}