Coverage Report

Created: 2025-07-12 06:25

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