Coverage Report

Created: 2026-07-12 07:21

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
85.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.96M
#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.8k
{
88
16.8k
    return &default_method;
89
16.8k
}
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.8k
{
113
16.8k
    CONF *ret;
114
115
16.8k
    ret = OPENSSL_malloc(sizeof(*ret));
116
16.8k
    if (ret != NULL)
117
16.8k
        if (meth->init(ret) == 0) {
118
0
            OPENSSL_free(ret);
119
0
            ret = NULL;
120
0
        }
121
16.8k
    return ret;
122
16.8k
}
123
124
static int def_init_default(CONF *conf)
125
23.3k
{
126
23.3k
    if (conf == NULL)
127
0
        return 0;
128
129
23.3k
    memset(conf, 0, sizeof(*conf));
130
23.3k
    conf->meth = &default_method;
131
23.3k
    conf->meth_data = (void *)CONF_type_default;
132
133
23.3k
    return 1;
134
23.3k
}
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.8k
{
152
16.8k
    if (def_destroy_data(conf)) {
153
16.8k
        OPENSSL_free(conf);
154
16.8k
        return 1;
155
16.8k
    }
156
0
    return 0;
157
16.8k
}
158
159
static int def_destroy_data(CONF *conf)
160
23.3k
{
161
23.3k
    if (conf == NULL)
162
0
        return 0;
163
23.3k
    _CONF_free_data(conf);
164
23.3k
    return 1;
165
23.3k
}
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.32k
{
194
8.32k
    if (OPENSSL_strcasecmp(pval, "on") == 0
195
5.47k
        || OPENSSL_strcasecmp(pval, "true") == 0) {
196
5.47k
        *flag = 1;
197
5.47k
    } 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
247
        ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
202
247
        return 0;
203
247
    }
204
8.08k
    return 1;
205
8.32k
}
206
207
static int def_load_bio(CONF *conf, BIO *in, long *line)
208
990
{
209
/* The macro BUFSIZE conflicts with a system macro in VxWorks */
210
13.6M
#define CONFBUFSIZE 512
211
990
    int bufnum = 0, i, ii;
212
990
    BUF_MEM *buff = NULL;
213
990
    char *s, *p, *end;
214
990
    int again;
215
990
    int first_call = 1;
216
990
    long eline = 0;
217
990
    char btmp[DECIMAL_SIZE(eline) + 1];
218
990
    CONF_VALUE *v = NULL, *tv;
219
990
    CONF_VALUE *sv = NULL;
220
990
    char *section = NULL, *buf;
221
990
    char *start, *psection, *pname;
222
990
    void *h = (void *)(conf->data);
223
990
    STACK_OF(BIO) *biosk = NULL;
224
990
#ifndef OPENSSL_NO_POSIX_IO
225
990
    char *dirpath = NULL;
226
990
    OPENSSL_DIR_CTX *dirctx = NULL;
227
990
#endif
228
990
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
229
990
    int numincludes = 0;
230
990
#endif
231
232
990
    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
990
    section = OPENSSL_strdup("default");
238
990
    if (section == NULL) {
239
0
        ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
240
0
        goto err;
241
0
    }
242
243
990
    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
990
    sv = _CONF_new_section(conf, section);
249
990
    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
990
    bufnum = 0;
255
990
    again = 0;
256
4.54M
    for (;;) {
257
4.54M
        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
4.54M
        p = &(buff->data[bufnum]);
262
4.54M
        *p = '\0';
263
4.54M
    read_retry:
264
4.54M
        if (in != NULL && BIO_gets(in, p, CONFBUFSIZE - 1) < 0)
265
0
            goto err;
266
4.54M
        p[CONFBUFSIZE - 1] = '\0';
267
4.54M
        ii = i = strlen(p);
268
4.54M
        if (first_call) {
269
            /* Other BOMs imply unsupported multibyte encoding,
270
             * so don't strip them and let the error raise */
271
990
            const unsigned char utf8_bom[3] = { 0xEF, 0xBB, 0xBF };
272
273
990
            if (i >= 3 && memcmp(p, utf8_bom, 3) == 0) {
274
3
                memmove(p, p + 3, i - 3);
275
3
                p[i - 3] = 0;
276
3
                i -= 3;
277
3
                ii -= 3;
278
3
            }
279
990
            first_call = 0;
280
990
        }
281
4.54M
        if (i == 0 && !again) {
282
            /* the currently processed BIO is NULL or at EOF */
283
1.17k
            BIO *parent;
284
285
1.17k
#ifndef OPENSSL_NO_POSIX_IO
286
            /* continue processing with the next file from directory */
287
1.17k
            if (dirctx != NULL) {
288
561
                BIO *next;
289
290
561
                if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
291
88
                    BIO_vfree(in);
292
88
                    in = next;
293
88
                    goto read_retry;
294
473
                } else {
295
473
                    OPENSSL_free(dirpath);
296
473
                    dirpath = NULL;
297
473
                }
298
561
            }
299
1.08k
#endif
300
            /* no more files in directory, continue with processing parent */
301
1.08k
            if ((parent = sk_BIO_pop(biosk)) == NULL) {
302
                /* everything processed get out of the loop */
303
597
                break;
304
597
            } else {
305
488
                BIO_vfree(in);
306
488
                in = parent;
307
488
                goto read_retry;
308
488
            }
309
1.08k
        }
310
4.54M
        again = 0;
311
8.98M
        while (i > 0) {
312
4.68M
            if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
313
253k
                break;
314
4.43M
            else
315
4.43M
                i--;
316
4.68M
        }
317
        /*
318
         * we removed some trailing stuff so there is a new line on the end.
319
         */
320
4.54M
        if (ii && i == ii)
321
107k
            again = 1; /* long line */
322
4.43M
        else {
323
4.43M
            p[i] = '\0';
324
4.43M
            eline++; /* another input line */
325
4.43M
        }
326
327
        /* we now have a line with trailing \r\n removed */
328
329
        /* i is the number of bytes */
330
4.54M
        bufnum += i;
331
332
4.54M
        v = NULL;
333
        /* check for line continuation */
334
4.54M
        if (!again && bufnum >= 1) {
335
            /*
336
             * If we have bytes and the last char '\\' and second last char
337
             * is not '\\'
338
             */
339
146k
            p = &(buff->data[bufnum - 1]);
340
146k
            if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
341
1.04k
                bufnum--;
342
1.04k
                again = 1;
343
1.04k
            }
344
146k
        }
345
4.54M
        if (again)
346
108k
            continue;
347
4.43M
        bufnum = 0;
348
4.43M
        buf = buff->data;
349
350
4.43M
        clear_comments(conf, buf);
351
4.43M
        s = eat_ws(conf, buf);
352
4.43M
        if (IS_EOF(conf, *s))
353
4.35M
            continue; /* blank line */
354
79.1k
        if (*s == '[') {
355
8.58k
            char *ss;
356
357
8.58k
            s++;
358
8.58k
            start = eat_ws(conf, s);
359
8.58k
            ss = start;
360
8.89k
        again:
361
8.89k
            end = eat_alpha_numeric(conf, ss);
362
8.89k
            p = eat_ws(conf, end);
363
8.89k
            if (*p != ']') {
364
345
                if (*p != '\0' && ss != p) {
365
310
                    ss = p;
366
310
                    goto again;
367
310
                }
368
345
                ERR_raise(ERR_LIB_CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
369
35
                goto err;
370
345
            }
371
8.55k
            *end = '\0';
372
8.55k
            if (!str_copy(conf, NULL, &section, start))
373
0
                goto err;
374
8.55k
            if ((sv = _CONF_get_section(conf, section)) == NULL)
375
4.62k
                sv = _CONF_new_section(conf, section);
376
8.55k
            if (sv == NULL) {
377
0
                ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
378
0
                goto err;
379
0
            }
380
8.55k
            continue;
381
70.5k
        } else {
382
70.5k
            pname = s;
383
70.5k
            end = eat_alpha_numeric(conf, s);
384
70.5k
            if ((end[0] == ':') && (end[1] == ':')) {
385
8.77k
                *end = '\0';
386
8.77k
                end += 2;
387
8.77k
                psection = pname;
388
8.77k
                pname = end;
389
8.77k
                end = eat_alpha_numeric(conf, end);
390
61.7k
            } else {
391
61.7k
                psection = section;
392
61.7k
            }
393
70.5k
            p = eat_ws(conf, end);
394
70.5k
            if (strncmp(pname, ".pragma", 7) == 0
395
2.74k
                && (p != pname + 7 || *p == '=')) {
396
2.74k
                char *pval;
397
398
2.74k
                if (*p == '=') {
399
1.63k
                    p++;
400
1.63k
                    p = eat_ws(conf, p);
401
1.63k
                }
402
2.74k
                trim_ws(conf, p);
403
404
                /* Pragma values take the form keyword:value */
405
2.74k
                pval = strchr(p, ':');
406
2.74k
                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.72k
                *pval++ = '\0';
412
2.72k
                trim_ws(conf, p);
413
2.72k
                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.72k
                if (strcmp(p, "dollarid") == 0) {
423
238
                    if (!parsebool(pval, &conf->flag_dollarid))
424
1
                        goto err;
425
2.49k
                } else if (strcmp(p, "abspath") == 0) {
426
968
                    if (!parsebool(pval, &conf->flag_abspath))
427
5
                        goto err;
428
1.52k
                } else if (strcmp(p, "includedir") == 0) {
429
260
                    OPENSSL_free(conf->includedir);
430
260
                    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
260
                }
435
436
                /*
437
                 * We *ignore* any unknown pragma.
438
                 */
439
2.72k
                continue;
440
67.8k
            } else if (strncmp(pname, ".include", 8) == 0
441
986
                && (p != pname + 8 || *p == '=')) {
442
984
                char *include = NULL;
443
984
                BIO *next;
444
984
                const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE");
445
984
                char *include_path = NULL;
446
447
984
#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
984
                if (numincludes++ > 10)
458
7
                    goto err;
459
977
#endif
460
461
977
                if (include_dir == NULL)
462
977
                    include_dir = conf->includedir;
463
464
977
                if (*p == '=') {
465
102
                    p++;
466
102
                    p = eat_ws(conf, p);
467
102
                }
468
977
                trim_ws(conf, p);
469
977
                if (!str_copy(conf, psection, &include, p))
470
5
                    goto err;
471
472
972
                if (include_dir != NULL && !ossl_is_absolute_path(include)) {
473
216
                    size_t newlen = strlen(include_dir) + strlen(include) + 2;
474
475
216
                    include_path = OPENSSL_malloc(newlen);
476
216
                    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
216
                    OPENSSL_strlcpy(include_path, include_dir, newlen);
483
216
                    if (!ossl_ends_with_dirsep(include_path))
484
198
                        OPENSSL_strlcat(include_path, "/", newlen);
485
216
                    OPENSSL_strlcat(include_path, include, newlen);
486
216
                    OPENSSL_free(include);
487
756
                } else {
488
756
                    include_path = include;
489
756
                }
490
491
972
                if (conf->flag_abspath
492
12
                    && !ossl_is_absolute_path(include_path)) {
493
0
                    ERR_raise(ERR_LIB_CONF, CONF_R_RELATIVE_PATH);
494
0
                    OPENSSL_free(include_path);
495
0
                    goto err;
496
0
                }
497
498
                /* get the BIO of the included file */
499
972
#ifndef OPENSSL_NO_POSIX_IO
500
972
                next = process_include(include_path, &dirctx, &dirpath);
501
972
                if (include_path != dirpath) {
502
                    /* dirpath will contain include in case of a directory */
503
498
                    OPENSSL_free(include_path);
504
498
                }
505
#else
506
                next = BIO_new_file(include_path, "r");
507
                OPENSSL_free(include_path);
508
#endif
509
510
972
                if (next != NULL) {
511
                    /* push the currently processing BIO onto stack */
512
492
                    if (biosk == NULL) {
513
156
                        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
156
                    }
519
492
                    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
492
                    in = next;
526
492
                }
527
972
                continue;
528
66.8k
            } else if (*p != '=') {
529
165
                ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN,
530
165
                    "HERE-->%s", p);
531
165
                goto err;
532
165
            }
533
66.6k
            *end = '\0';
534
66.6k
            p++;
535
66.6k
            start = eat_ws(conf, p);
536
66.6k
            trim_ws(conf, start);
537
538
66.6k
            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
66.6k
            v->name = OPENSSL_strdup(pname);
543
66.6k
            v->value = NULL;
544
66.6k
            if (v->name == NULL) {
545
0
                ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
546
0
                goto err;
547
0
            }
548
66.6k
            if (!str_copy(conf, psection, &(v->value), start))
549
160
                goto err;
550
551
66.5k
            if (strcmp(psection, section) != 0) {
552
8.76k
                if ((tv = _CONF_get_section(conf, psection))
553
8.76k
                    == NULL)
554
7.88k
                    tv = _CONF_new_section(conf, psection);
555
8.76k
                if (tv == NULL) {
556
0
                    ERR_raise(ERR_LIB_CONF,
557
0
                        CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
558
0
                    goto err;
559
0
                }
560
8.76k
            } else
561
57.7k
                tv = sv;
562
66.5k
            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
66.5k
            v = NULL;
567
66.5k
        }
568
79.1k
    }
569
597
    BUF_MEM_free(buff);
570
597
    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
597
    sk_BIO_free(biosk);
576
597
    return 1;
577
578
393
err:
579
393
    BUF_MEM_free(buff);
580
393
    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
397
    while (sk_BIO_num(biosk) > 0) {
587
4
        BIO *popped = sk_BIO_pop(biosk);
588
4
        BIO_vfree(in);
589
4
        in = popped;
590
4
    }
591
393
    sk_BIO_free(biosk);
592
393
#ifndef OPENSSL_NO_POSIX_IO
593
393
    OPENSSL_free(dirpath);
594
393
    if (dirctx != NULL)
595
1
        OPENSSL_DIR_end(&dirctx);
596
393
#endif
597
393
    if (line != NULL)
598
393
        *line = eline;
599
393
    BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
600
393
    ERR_add_error_data(2, "line ", btmp);
601
393
    if (h != conf->data) {
602
393
        CONF_free(conf->data);
603
393
        conf->data = NULL;
604
393
    }
605
393
    if (v != NULL) {
606
160
        OPENSSL_free(v->name);
607
160
        OPENSSL_free(v->value);
608
160
        OPENSSL_free(v);
609
160
    }
610
393
    return 0;
611
990
}
612
613
static void clear_comments(CONF *conf, char *p)
614
31.5M
{
615
31.5M
    for (;;) {
616
31.5M
        if (IS_FCOMMENT(conf, *p)) {
617
0
            *p = '\0';
618
0
            return;
619
0
        }
620
31.5M
        if (!IS_WS(conf, *p)) {
621
31.5M
            break;
622
31.5M
        }
623
71.1k
        p++;
624
71.1k
    }
625
626
330M
    for (;;) {
627
330M
        if (IS_COMMENT(conf, *p)) {
628
362k
            *p = '\0';
629
362k
            return;
630
362k
        }
631
330M
        if (IS_DQUOTE(conf, *p)) {
632
0
            p = scan_dquote(conf, p);
633
0
            continue;
634
0
        }
635
330M
        if (IS_QUOTE(conf, *p)) {
636
513k
            p = scan_quote(conf, p);
637
513k
            continue;
638
513k
        }
639
329M
        if (IS_ESC(conf, *p)) {
640
1.23M
            p = scan_esc(conf, p);
641
1.23M
            continue;
642
1.23M
        }
643
328M
        if (IS_EOF(conf, *p))
644
31.1M
            return;
645
297M
        else
646
297M
            p++;
647
328M
    }
648
31.5M
}
649
650
static int str_copy(CONF *conf, char *section, char **pto, char *from)
651
450k
{
652
450k
    int q, r, rr = 0, to = 0, len = 0;
653
450k
    char *s, *e, *rp, *p, *rrp, *np, *cp, v;
654
450k
    BUF_MEM *buf;
655
656
450k
    if ((buf = BUF_MEM_new()) == NULL)
657
0
        return 0;
658
659
450k
    len = strlen(from) + 1;
660
450k
    if (!BUF_MEM_grow(buf, len))
661
0
        goto err;
662
663
99.2M
    for (;;) {
664
99.2M
        if (IS_QUOTE(conf, *from)) {
665
30.6k
            q = *from;
666
30.6k
            from++;
667
30.4M
            while (!IS_EOF(conf, *from) && (*from != q)) {
668
30.3M
                if (IS_ESC(conf, *from)) {
669
77.6k
                    from++;
670
77.6k
                    if (IS_EOF(conf, *from))
671
2.88k
                        break;
672
77.6k
                }
673
30.3M
                buf->data[to++] = *(from++);
674
30.3M
            }
675
30.6k
            if (*from == q)
676
23.9k
                from++;
677
99.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
99.2M
        } else if (IS_ESC(conf, *from)) {
693
342k
            from++;
694
342k
            v = *(from++);
695
342k
            if (IS_EOF(conf, v))
696
10.2k
                break;
697
331k
            else if (v == 'r')
698
72.1k
                v = '\r';
699
259k
            else if (v == 'n')
700
7.32k
                v = '\n';
701
252k
            else if (v == 'b')
702
4.59k
                v = '\b';
703
247k
            else if (v == 't')
704
5.14k
                v = '\t';
705
331k
            buf->data[to++] = v;
706
98.9M
        } else if (IS_EOF(conf, *from))
707
437k
            break;
708
98.4M
        else if (*from == '$'
709
96.5k
            && (!conf->flag_dollarid
710
14.0k
                || from[1] == '{'
711
86.9k
                || from[1] == '(')) {
712
86.9k
            size_t newsize;
713
714
            /* try to expand it */
715
86.9k
            rrp = NULL;
716
86.9k
            s = &(from[1]);
717
86.9k
            if (*s == '{')
718
4.43k
                q = '}';
719
82.5k
            else if (*s == '(')
720
3.30k
                q = ')';
721
79.2k
            else
722
79.2k
                q = 0;
723
724
86.9k
            if (q)
725
7.74k
                s++;
726
86.9k
            cp = section;
727
86.9k
            e = np = s;
728
19.3M
            while (IS_ALNUM(conf, *e)
729
88.0k
                || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
730
19.2M
                e++;
731
86.9k
            if ((e[0] == ':') && (e[1] == ':')) {
732
7.31k
                cp = np;
733
7.31k
                rrp = e;
734
7.31k
                rr = *e;
735
7.31k
                *rrp = '\0';
736
7.31k
                e += 2;
737
7.31k
                np = e;
738
11.2k
                while (IS_ALNUM(conf, *e)
739
8.40k
                    || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
740
3.88k
                    e++;
741
7.31k
            }
742
86.9k
            r = *e;
743
86.9k
            *e = '\0';
744
86.9k
            rp = e;
745
86.9k
            if (q) {
746
7.74k
                if (r != q) {
747
345
                    ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE);
748
345
                    goto err;
749
345
                }
750
7.40k
                e++;
751
7.40k
            }
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
86.6k
            p = _CONF_get_string(conf, cp, np);
763
86.6k
            if (rrp != NULL)
764
7.27k
                *rrp = rr;
765
86.6k
            *rp = r;
766
86.6k
            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
85.1k
            newsize = strlen(p) + buf->length - (e - from);
771
85.1k
            if (newsize > MAX_CONF_VALUE_LENGTH) {
772
94
                ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
773
94
                goto err;
774
94
            }
775
85.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
37.4M
            while (*p)
780
37.3M
                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
85.0k
            len -= e - from;
787
85.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
85.0k
            *rp = r;
795
85.0k
        } else
796
98.3M
            buf->data[to++] = *(from++);
797
99.2M
    }
798
448k
    buf->data[to] = '\0';
799
448k
    OPENSSL_free(*pto);
800
448k
    *pto = buf->data;
801
448k
    OPENSSL_free(buf);
802
448k
    return 1;
803
1.89k
err:
804
1.89k
    BUF_MEM_free(buf);
805
1.89k
    return 0;
806
450k
}
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.84k
{
817
6.84k
    struct stat st;
818
6.84k
    BIO *next;
819
820
6.84k
    if (stat(include, &st) < 0) {
821
2.96k
        ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include);
822
        /* missing include file is not fatal error */
823
2.96k
        return NULL;
824
2.96k
    }
825
826
3.87k
    if (S_ISDIR(st.st_mode)) {
827
3.79k
        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.79k
        if ((next = get_next_file(include, dirctx)) != NULL)
834
2.30k
            *dirpath = include;
835
3.79k
        return next;
836
3.79k
    }
837
838
79
    next = BIO_new_file(include, "r");
839
79
    return next;
840
3.87k
}
841
842
/*
843
 * Get next file from the directory path.
844
 * Returns BIO of the next file to read and updates dirctx.
845
 */
846
static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx)
847
6.60k
{
848
6.60k
    const char *filename;
849
6.60k
    size_t pathlen;
850
851
6.60k
    pathlen = strlen(path);
852
258k
    while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
853
254k
        size_t namelen;
854
855
254k
        namelen = strlen(filename);
856
857
254k
        if ((namelen > 5
858
189k
                && OPENSSL_strcasecmp(filename + namelen - 5, ".conf") == 0)
859
241k
            || (namelen > 4
860
202k
                && OPENSSL_strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
861
15.1k
            size_t newlen;
862
15.1k
            char *newpath;
863
15.1k
            BIO *bio;
864
865
15.1k
            newlen = pathlen + namelen + 2;
866
15.1k
            newpath = OPENSSL_zalloc(newlen);
867
15.1k
            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
15.1k
            if (newpath[0] == '\0') {
884
15.1k
                OPENSSL_strlcpy(newpath, path, newlen);
885
15.1k
                OPENSSL_strlcat(newpath, "/", newlen);
886
15.1k
            }
887
15.1k
            OPENSSL_strlcat(newpath, filename, newlen);
888
889
15.1k
            bio = BIO_new_file(newpath, "r");
890
15.1k
            OPENSSL_free(newpath);
891
            /* Errors when opening files are non-fatal. */
892
15.1k
            if (bio != NULL)
893
2.96k
                return bio;
894
15.1k
        }
895
254k
    }
896
3.64k
    OPENSSL_DIR_end(dirctx);
897
3.64k
    *dirctx = NULL;
898
3.64k
    return NULL;
899
6.60k
}
900
#endif
901
902
static int is_keytype(const CONF *conf, char c, unsigned short type)
903
2.81G
{
904
2.81G
    const unsigned short *keytypes = (const unsigned short *)conf->meth_data;
905
2.81G
    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.81G
    if (key > 127) {
919
        /* key is not a seven bit ascii character */
920
151M
        return 0;
921
151M
    }
922
923
2.66G
    return (keytypes[key] & type) ? 1 : 0;
924
2.81G
}
925
926
static char *eat_ws(CONF *conf, char *p)
927
32.4M
{
928
33.0M
    while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
929
523k
        p++;
930
32.4M
    return p;
931
32.4M
}
932
933
static void trim_ws(CONF *conf, char *start)
934
443k
{
935
443k
    char *p = start;
936
937
169M
    while (!IS_EOF(conf, *p))
938
168M
        p++;
939
443k
    p--;
940
549k
    while ((p >= start) && IS_WS(conf, *p))
941
105k
        p--;
942
443k
    p++;
943
443k
    *p = '\0';
944
443k
}
945
946
static char *eat_alpha_numeric(CONF *conf, char *p)
947
540k
{
948
139M
    for (;;) {
949
139M
        if (IS_ESC(conf, *p)) {
950
725k
            p = scan_esc(conf, p);
951
725k
            continue;
952
725k
        }
953
138M
        if (!(IS_ALNUM_PUNCT(conf, *p)
954
544k
                || (conf->flag_dollarid && IS_DOLLAR(conf, *p))))
955
540k
            return p;
956
138M
        p++;
957
138M
    }
958
540k
}
959
960
static char *scan_quote(CONF *conf, char *p)
961
513k
{
962
513k
    int q = *p;
963
964
513k
    p++;
965
53.9M
    while (!(IS_EOF(conf, *p)) && (*p != q)) {
966
53.4M
        if (IS_ESC(conf, *p)) {
967
203k
            p++;
968
203k
            if (IS_EOF(conf, *p))
969
2.28k
                return p;
970
203k
        }
971
53.3M
        p++;
972
53.3M
    }
973
511k
    if (*p == q)
974
505k
        p++;
975
511k
    return p;
976
513k
}
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
}