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