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