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