Coverage Report

Created: 2023-06-08 06:40

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