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