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