1832cf535f88cd4b6d2cc08179b8794d1cb3c4ca
[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                     if ((conf->includedir = OPENSSL_strdup(pval)) == NULL) {
428                         ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
429                         goto err;
430                     }
431                 }
432
433                 /*
434                  * We *ignore* any unknown pragma.
435                  */
436                 continue;
437             } else if (strncmp(pname, ".include", 8) == 0
438                 && (p != pname + 8 || *p == '=')) {
439                 char *include = NULL;
440                 BIO *next;
441                 const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE");
442                 char *include_path = NULL;
443
444                 if (include_dir == NULL)
445                     include_dir = conf->includedir;
446
447                 if (*p == '=') {
448                     p++;
449                     p = eat_ws(conf, p);
450                 }
451                 trim_ws(conf, p);
452                 if (!str_copy(conf, psection, &include, p))
453                     goto err;
454
455                 if (include_dir != NULL && !ossl_is_absolute_path(include)) {
456                     size_t newlen = strlen(include_dir) + strlen(include) + 2;
457
458                     include_path = OPENSSL_malloc(newlen);
459                     if (include_path == NULL) {
460                         ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
461                         OPENSSL_free(include);
462                         goto err;
463                     }
464
465                     OPENSSL_strlcpy(include_path, include_dir, newlen);
466                     if (!ossl_ends_with_dirsep(include_path))
467                         OPENSSL_strlcat(include_path, "/", newlen);
468                     OPENSSL_strlcat(include_path, include, newlen);
469                     OPENSSL_free(include);
470                 } else {
471                     include_path = include;
472                 }
473
474                 if (conf->flag_abspath
475                         && !ossl_is_absolute_path(include_path)) {
476                     ERR_raise(ERR_LIB_CONF, CONF_R_RELATIVE_PATH);
477                     OPENSSL_free(include_path);
478                     goto err;
479                 }
480
481                 /* get the BIO of the included file */
482 #ifndef OPENSSL_NO_POSIX_IO
483                 next = process_include(include_path, &dirctx, &dirpath);
484                 if (include_path != dirpath) {
485                     /* dirpath will contain include in case of a directory */
486                     OPENSSL_free(include_path);
487                 }
488 #else
489                 next = BIO_new_file(include_path, "r");
490                 OPENSSL_free(include_path);
491 #endif
492
493                 if (next != NULL) {
494                     /* push the currently processing BIO onto stack */
495                     if (biosk == NULL) {
496                         if ((biosk = sk_BIO_new_null()) == NULL) {
497                             ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
498                             BIO_free(next);
499                             goto err;
500                         }
501                     }
502                     if (!sk_BIO_push(biosk, in)) {
503                         ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
504                         BIO_free(next);
505                         goto err;
506                     }
507                     /* continue with reading from the included BIO */
508                     in = next;
509                 }
510                 continue;
511             } else if (*p != '=') {
512                 ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN,
513                                "HERE-->%s", p);
514                 goto err;
515             }
516             *end = '\0';
517             p++;
518             start = eat_ws(conf, p);
519             trim_ws(conf, start);
520
521             if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
522                 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
523                 goto err;
524             }
525             v->name = OPENSSL_strdup(pname);
526             v->value = NULL;
527             if (v->name == NULL) {
528                 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
529                 goto err;
530             }
531             if (!str_copy(conf, psection, &(v->value), start))
532                 goto err;
533
534             if (strcmp(psection, section) != 0) {
535                 if ((tv = _CONF_get_section(conf, psection))
536                     == NULL)
537                     tv = _CONF_new_section(conf, psection);
538                 if (tv == NULL) {
539                     ERR_raise(ERR_LIB_CONF,
540                               CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
541                     goto err;
542                 }
543             } else
544                 tv = sv;
545             if (_CONF_add_string(conf, tv, v) == 0) {
546                 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
547                 goto err;
548             }
549             v = NULL;
550         }
551     }
552     BUF_MEM_free(buff);
553     OPENSSL_free(section);
554     /*
555      * No need to pop, since we only get here if the stack is empty.
556      * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE!
557      */
558     sk_BIO_free(biosk);
559     return 1;
560
561  err:
562     BUF_MEM_free(buff);
563     OPENSSL_free(section);
564     /*
565      * Since |in| is the first element of the stack and should NOT be freed
566      * here, we cannot use sk_BIO_pop_free().  Instead, we pop and free one
567      * BIO at a time, making sure that the last one popped isn't.
568      */
569     while (sk_BIO_num(biosk) > 0) {
570         BIO *popped = sk_BIO_pop(biosk);
571         BIO_vfree(in);
572         in = popped;
573     }
574     sk_BIO_free(biosk);
575 #ifndef OPENSSL_NO_POSIX_IO
576     OPENSSL_free(dirpath);
577     if (dirctx != NULL)
578         OPENSSL_DIR_end(&dirctx);
579 #endif
580     if (line != NULL)
581         *line = eline;
582     BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
583     ERR_add_error_data(2, "line ", btmp);
584     if (h != conf->data) {
585         CONF_free(conf->data);
586         conf->data = NULL;
587     }
588     if (v != NULL) {
589         OPENSSL_free(v->name);
590         OPENSSL_free(v->value);
591         OPENSSL_free(v);
592     }
593     return 0;
594 }
595
596 static void clear_comments(CONF *conf, char *p)
597 {
598     for (;;) {
599         if (IS_FCOMMENT(conf, *p)) {
600             *p = '\0';
601             return;
602         }
603         if (!IS_WS(conf, *p)) {
604             break;
605         }
606         p++;
607     }
608
609     for (;;) {
610         if (IS_COMMENT(conf, *p)) {
611             *p = '\0';
612             return;
613         }
614         if (IS_DQUOTE(conf, *p)) {
615             p = scan_dquote(conf, p);
616             continue;
617         }
618         if (IS_QUOTE(conf, *p)) {
619             p = scan_quote(conf, p);
620             continue;
621         }
622         if (IS_ESC(conf, *p)) {
623             p = scan_esc(conf, p);
624             continue;
625         }
626         if (IS_EOF(conf, *p))
627             return;
628         else
629             p++;
630     }
631 }
632
633 static int str_copy(CONF *conf, char *section, char **pto, char *from)
634 {
635     int q, r, rr = 0, to = 0, len = 0;
636     char *s, *e, *rp, *p, *rrp, *np, *cp, v;
637     BUF_MEM *buf;
638
639     if ((buf = BUF_MEM_new()) == NULL)
640         return 0;
641
642     len = strlen(from) + 1;
643     if (!BUF_MEM_grow(buf, len))
644         goto err;
645
646     for (;;) {
647         if (IS_QUOTE(conf, *from)) {
648             q = *from;
649             from++;
650             while (!IS_EOF(conf, *from) && (*from != q)) {
651                 if (IS_ESC(conf, *from)) {
652                     from++;
653                     if (IS_EOF(conf, *from))
654                         break;
655                 }
656                 buf->data[to++] = *(from++);
657             }
658             if (*from == q)
659                 from++;
660         } else if (IS_DQUOTE(conf, *from)) {
661             q = *from;
662             from++;
663             while (!IS_EOF(conf, *from)) {
664                 if (*from == q) {
665                     if (*(from + 1) == q) {
666                         from++;
667                     } else {
668                         break;
669                     }
670                 }
671                 buf->data[to++] = *(from++);
672             }
673             if (*from == q)
674                 from++;
675         } else if (IS_ESC(conf, *from)) {
676             from++;
677             v = *(from++);
678             if (IS_EOF(conf, v))
679                 break;
680             else if (v == 'r')
681                 v = '\r';
682             else if (v == 'n')
683                 v = '\n';
684             else if (v == 'b')
685                 v = '\b';
686             else if (v == 't')
687                 v = '\t';
688             buf->data[to++] = v;
689         } else if (IS_EOF(conf, *from))
690             break;
691         else if (*from == '$'
692                  && (!conf->flag_dollarid
693                      || from[1] == '{'
694                      || from[1] == '(')) {
695             size_t newsize;
696
697             /* try to expand it */
698             rrp = NULL;
699             s = &(from[1]);
700             if (*s == '{')
701                 q = '}';
702             else if (*s == '(')
703                 q = ')';
704             else
705                 q = 0;
706
707             if (q)
708                 s++;
709             cp = section;
710             e = np = s;
711             while (IS_ALNUM(conf, *e)
712                    || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
713                 e++;
714             if ((e[0] == ':') && (e[1] == ':')) {
715                 cp = np;
716                 rrp = e;
717                 rr = *e;
718                 *rrp = '\0';
719                 e += 2;
720                 np = e;
721                 while (IS_ALNUM(conf, *e)
722                        || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
723                     e++;
724             }
725             r = *e;
726             *e = '\0';
727             rp = e;
728             if (q) {
729                 if (r != q) {
730                     ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE);
731                     goto err;
732                 }
733                 e++;
734             }
735             /*-
736              * So at this point we have
737              * np which is the start of the name string which is
738              *   '\0' terminated.
739              * cp which is the start of the section string which is
740              *   '\0' terminated.
741              * e is the 'next point after'.
742              * r and rr are the chars replaced by the '\0'
743              * rp and rrp is where 'r' and 'rr' came from.
744              */
745             p = _CONF_get_string(conf, cp, np);
746             if (rrp != NULL)
747                 *rrp = rr;
748             *rp = r;
749             if (p == NULL) {
750                 ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_HAS_NO_VALUE);
751                 goto err;
752             }
753             newsize = strlen(p) + buf->length - (e - from);
754             if (newsize > MAX_CONF_VALUE_LENGTH) {
755                 ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
756                 goto err;
757             }
758             if (!BUF_MEM_grow_clean(buf, newsize)) {
759                 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
760                 goto err;
761             }
762             while (*p)
763                 buf->data[to++] = *(p++);
764
765             /*
766              * Since we change the pointer 'from', we also have to change the
767              * perceived length of the string it points at.  /RL
768              */
769             len -= e - from;
770             from = e;
771
772             /*
773              * In case there were no braces or parenthesis around the
774              * variable reference, we have to put back the character that was
775              * replaced with a '\0'.  /RL
776              */
777             *rp = r;
778         } else
779             buf->data[to++] = *(from++);
780     }
781     buf->data[to] = '\0';
782     OPENSSL_free(*pto);
783     *pto = buf->data;
784     OPENSSL_free(buf);
785     return 1;
786  err:
787     BUF_MEM_free(buf);
788     return 0;
789 }
790
791 #ifndef OPENSSL_NO_POSIX_IO
792 /*
793  * Check whether included path is a directory.
794  * Returns next BIO to process and in case of a directory
795  * also an opened directory context and the include path.
796  */
797 static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
798                             char **dirpath)
799 {
800     struct stat st;
801     BIO *next;
802
803     if (stat(include, &st) < 0) {
804         ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include);
805         /* missing include file is not fatal error */
806         return NULL;
807     }
808
809     if (S_ISDIR(st.st_mode)) {
810         if (*dirctx != NULL) {
811             ERR_raise_data(ERR_LIB_CONF, CONF_R_RECURSIVE_DIRECTORY_INCLUDE,
812                            "%s", include);
813             return NULL;
814         }
815         /* a directory, load its contents */
816         if ((next = get_next_file(include, dirctx)) != NULL)
817             *dirpath = include;
818         return next;
819     }
820
821     next = BIO_new_file(include, "r");
822     return next;
823 }
824
825 /*
826  * Get next file from the directory path.
827  * Returns BIO of the next file to read and updates dirctx.
828  */
829 static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx)
830 {
831     const char *filename;
832     size_t pathlen;
833
834     pathlen = strlen(path);
835     while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
836         size_t namelen;
837
838         namelen = strlen(filename);
839
840
841         if ((namelen > 5 && strcasecmp(filename + namelen - 5, ".conf") == 0)
842             || (namelen > 4 && strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
843             size_t newlen;
844             char *newpath;
845             BIO *bio;
846
847             newlen = pathlen + namelen + 2;
848             newpath = OPENSSL_zalloc(newlen);
849             if (newpath == NULL) {
850                 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
851                 break;
852             }
853 #ifdef OPENSSL_SYS_VMS
854             /*
855              * If the given path isn't clear VMS syntax,
856              * we treat it as on Unix.
857              */
858             if (path[pathlen - 1] == ']'
859                 || path[pathlen - 1] == '>'
860                 || path[pathlen - 1] == ':') {
861                 /* Clear VMS directory syntax, just copy as is */
862                 OPENSSL_strlcpy(newpath, path, newlen);
863             }
864 #endif
865             if (newpath[0] == '\0') {
866                 OPENSSL_strlcpy(newpath, path, newlen);
867                 OPENSSL_strlcat(newpath, "/", newlen);
868             }
869             OPENSSL_strlcat(newpath, filename, newlen);
870
871             bio = BIO_new_file(newpath, "r");
872             OPENSSL_free(newpath);
873             /* Errors when opening files are non-fatal. */
874             if (bio != NULL)
875                 return bio;
876         }
877     }
878     OPENSSL_DIR_end(dirctx);
879     *dirctx = NULL;
880     return NULL;
881 }
882 #endif
883
884 static int is_keytype(const CONF *conf, char c, unsigned short type)
885 {
886     const unsigned short * keytypes = (const unsigned short *) conf->meth_data;
887     unsigned char key = (unsigned char)c;
888
889 #ifdef CHARSET_EBCDIC
890 # if CHAR_BIT > 8
891     if (key > 255) {
892         /* key is out of range for os_toascii table */
893         return 0;
894     }
895 # endif
896     /* convert key from ebcdic to ascii */
897     key = os_toascii[key];
898 #endif
899
900     if (key > 127) {
901         /* key is not a seven bit ascii character */
902         return 0;
903     }
904
905     return (keytypes[key] & type) ? 1 : 0;
906 }
907
908 static char *eat_ws(CONF *conf, char *p)
909 {
910     while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
911         p++;
912     return p;
913 }
914
915 static void trim_ws(CONF *conf, char *start)
916 {
917     char *p = start;
918
919     while (!IS_EOF(conf, *p))
920         p++;
921     p--;
922     while ((p >= start) && IS_WS(conf, *p))
923         p--;
924     p++;
925     *p = '\0';
926 }
927
928 static char *eat_alpha_numeric(CONF *conf, char *p)
929 {
930     for (;;) {
931         if (IS_ESC(conf, *p)) {
932             p = scan_esc(conf, p);
933             continue;
934         }
935         if (!(IS_ALNUM_PUNCT(conf, *p)
936               || (conf->flag_dollarid && IS_DOLLAR(conf, *p))))
937             return p;
938         p++;
939     }
940 }
941
942 static char *scan_quote(CONF *conf, char *p)
943 {
944     int q = *p;
945
946     p++;
947     while (!(IS_EOF(conf, *p)) && (*p != q)) {
948         if (IS_ESC(conf, *p)) {
949             p++;
950             if (IS_EOF(conf, *p))
951                 return p;
952         }
953         p++;
954     }
955     if (*p == q)
956         p++;
957     return p;
958 }
959
960 static char *scan_dquote(CONF *conf, char *p)
961 {
962     int q = *p;
963
964     p++;
965     while (!(IS_EOF(conf, *p))) {
966         if (*p == q) {
967             if (*(p + 1) == q) {
968                 p++;
969             } else {
970                 break;
971             }
972         }
973         p++;
974     }
975     if (*p == q)
976         p++;
977     return p;
978 }
979
980 static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
981 {
982     if (a->name)
983         BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
984     else
985         BIO_printf(out, "[[%s]]\n", a->section);
986 }
987
988 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
989
990 static int def_dump(const CONF *conf, BIO *out)
991 {
992     lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
993     return 1;
994 }
995
996 static int def_is_number(const CONF *conf, char c)
997 {
998     return IS_NUMBER(conf, c);
999 }
1000
1001 static int def_to_int(const CONF *conf, char c)
1002 {
1003     return c - '0';
1004 }