Coverage Report

Created: 2023-04-12 06:22

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