CONF: Convert one last CONFerr() to ERR_raise()
[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         BIO_gets(in, p, CONFBUFSIZE - 1);
243         p[CONFBUFSIZE - 1] = '\0';
244         ii = i = strlen(p);
245         if (i == 0 && !again) {
246             /* the currently processed BIO is at EOF */
247             BIO *parent;
248
249 #ifndef OPENSSL_NO_POSIX_IO
250             /* continue processing with the next file from directory */
251             if (dirctx != NULL) {
252                 BIO *next;
253
254                 if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
255                     BIO_vfree(in);
256                     in = next;
257                     goto read_retry;
258                 } else {
259                     OPENSSL_free(dirpath);
260                     dirpath = NULL;
261                 }
262             }
263 #endif
264             /* no more files in directory, continue with processing parent */
265             if ((parent = sk_BIO_pop(biosk)) == NULL) {
266                 /* everything processed get out of the loop */
267                 break;
268             } else {
269                 BIO_vfree(in);
270                 in = parent;
271                 goto read_retry;
272             }
273         }
274         again = 0;
275         while (i > 0) {
276             if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
277                 break;
278             else
279                 i--;
280         }
281         /*
282          * we removed some trailing stuff so there is a new line on the end.
283          */
284         if (ii && i == ii)
285             again = 1;          /* long line */
286         else {
287             p[i] = '\0';
288             eline++;            /* another input line */
289         }
290
291         /* we now have a line with trailing \r\n removed */
292
293         /* i is the number of bytes */
294         bufnum += i;
295
296         v = NULL;
297         /* check for line continuation */
298         if (bufnum >= 1) {
299             /*
300              * If we have bytes and the last char '\\' and second last char
301              * is not '\\'
302              */
303             p = &(buff->data[bufnum - 1]);
304             if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
305                 bufnum--;
306                 again = 1;
307             }
308         }
309         if (again)
310             continue;
311         bufnum = 0;
312         buf = buff->data;
313
314         clear_comments(conf, buf);
315         s = eat_ws(conf, buf);
316         if (IS_EOF(conf, *s))
317             continue;           /* blank line */
318         if (*s == '[') {
319             char *ss;
320
321             s++;
322             start = eat_ws(conf, s);
323             ss = start;
324  again:
325             end = eat_alpha_numeric(conf, ss);
326             p = eat_ws(conf, end);
327             if (*p != ']') {
328                 if (*p != '\0' && ss != p) {
329                     ss = p;
330                     goto again;
331                 }
332                 ERR_raise(ERR_LIB_CONF, 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                 ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
342                 goto err;
343             }
344             continue;
345         } else {
346             pname = s;
347             end = eat_alpha_numeric(conf, s);
348             if ((end[0] == ':') && (end[1] == ':')) {
349                 *end = '\0';
350                 end += 2;
351                 psection = pname;
352                 pname = end;
353                 end = eat_alpha_numeric(conf, end);
354             } else {
355                 psection = section;
356             }
357             p = eat_ws(conf, end);
358             if (strncmp(pname, ".pragma", 7) == 0
359                 && (p != pname + 7 || *p == '=')) {
360                 char *pval;
361
362                 if (*p == '=') {
363                     p++;
364                     p = eat_ws(conf, p);
365                 }
366                 trim_ws(conf, p);
367
368                 /* Pragma values take the form keyword:value */
369                 pval = strchr(p, ':');
370                 if (pval == NULL || pval == p || pval[1] == '\0') {
371                     ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
372                     goto err;
373                 }
374
375                 *pval++ = '\0';
376                 trim_ws(conf, p);
377                 pval = eat_ws(conf, pval);
378
379                 /*
380                  * Known pragmas:
381                  *
382                  * dollarid     takes "on", "true or "off", "false"
383                  */
384                 if (strcmp(p, "dollarid") == 0) {
385                     if (strcmp(pval, "on") == 0
386                         || strcmp(pval, "true") == 0) {
387                         conf->flag_dollarid = 1;
388                     } else if (strcmp(pval, "off") == 0
389                                || strcmp(pval, "false") == 0) {
390                         conf->flag_dollarid = 0;
391                     } else {
392                         ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
393                         goto err;
394                     }
395                 }
396                 /*
397                  * We *ignore* any unknown pragma.
398                  */
399                 continue;
400             } else if (strncmp(pname, ".include", 8) == 0
401                 && (p != pname + 8 || *p == '=')) {
402                 char *include = NULL;
403                 BIO *next;
404                 const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE");
405                 char *include_path = NULL;
406
407                 if (*p == '=') {
408                     p++;
409                     p = eat_ws(conf, p);
410                 }
411                 trim_ws(conf, p);
412                 if (!str_copy(conf, psection, &include, p))
413                     goto err;
414
415                 if (include_dir != NULL && !ossl_is_absolute_path(include)) {
416                     size_t newlen = strlen(include_dir) + strlen(include) + 2;
417
418                     include_path = OPENSSL_malloc(newlen);
419                     if (include_path == NULL) {
420                         ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
421                         OPENSSL_free(include);
422                         goto err;
423                     }
424
425                     OPENSSL_strlcpy(include_path, include_dir, newlen);
426                     if (!ossl_ends_with_dirsep(include_path))
427                         OPENSSL_strlcat(include_path, "/", newlen);
428                     OPENSSL_strlcat(include_path, include, newlen);
429                     OPENSSL_free(include);
430                 } else {
431                     include_path = include;
432                 }
433
434                 /* get the BIO of the included file */
435 #ifndef OPENSSL_NO_POSIX_IO
436                 next = process_include(include_path, &dirctx, &dirpath);
437                 if (include_path != dirpath) {
438                     /* dirpath will contain include in case of a directory */
439                     OPENSSL_free(include_path);
440                 }
441 #else
442                 next = BIO_new_file(include_path, "r");
443                 OPENSSL_free(include_path);
444 #endif
445
446                 if (next != NULL) {
447                     /* push the currently processing BIO onto stack */
448                     if (biosk == NULL) {
449                         if ((biosk = sk_BIO_new_null()) == NULL) {
450                             ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
451                             BIO_free(next);
452                             goto err;
453                         }
454                     }
455                     if (!sk_BIO_push(biosk, in)) {
456                         ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
457                         BIO_free(next);
458                         goto err;
459                     }
460                     /* continue with reading from the included BIO */
461                     in = next;
462                 }
463                 continue;
464             } else if (*p != '=') {
465                 ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN,
466                                "HERE-->%s", p);
467                 goto err;
468             }
469             *end = '\0';
470             p++;
471             start = eat_ws(conf, p);
472             trim_ws(conf, start);
473
474             if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
475                 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
476                 goto err;
477             }
478             v->name = OPENSSL_strdup(pname);
479             v->value = NULL;
480             if (v->name == NULL) {
481                 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
482                 goto err;
483             }
484             if (!str_copy(conf, psection, &(v->value), start))
485                 goto err;
486
487             if (strcmp(psection, section) != 0) {
488                 if ((tv = _CONF_get_section(conf, psection))
489                     == NULL)
490                     tv = _CONF_new_section(conf, psection);
491                 if (tv == NULL) {
492                     ERR_raise(ERR_LIB_CONF,
493                               CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
494                     goto err;
495                 }
496             } else
497                 tv = sv;
498             if (_CONF_add_string(conf, tv, v) == 0) {
499                 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
500                 goto err;
501             }
502             v = NULL;
503         }
504     }
505     BUF_MEM_free(buff);
506     OPENSSL_free(section);
507     /*
508      * No need to pop, since we only get here if the stack is empty.
509      * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE!
510      */
511     sk_BIO_free(biosk);
512     return 1;
513  err:
514     BUF_MEM_free(buff);
515     OPENSSL_free(section);
516     /*
517      * Since |in| is the first element of the stack and should NOT be freed
518      * here, we cannot use sk_BIO_pop_free().  Instead, we pop and free one
519      * BIO at a time, making sure that the last one popped isn't.
520      */
521     while (sk_BIO_num(biosk) > 0) {
522         BIO *popped = sk_BIO_pop(biosk);
523         BIO_vfree(in);
524         in = popped;
525     }
526     sk_BIO_free(biosk);
527 #ifndef OPENSSL_NO_POSIX_IO
528     OPENSSL_free(dirpath);
529     if (dirctx != NULL)
530         OPENSSL_DIR_end(&dirctx);
531 #endif
532     if (line != NULL)
533         *line = eline;
534     BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
535     ERR_add_error_data(2, "line ", btmp);
536     if (h != conf->data) {
537         CONF_free(conf->data);
538         conf->data = NULL;
539     }
540     if (v != NULL) {
541         OPENSSL_free(v->name);
542         OPENSSL_free(v->value);
543         OPENSSL_free(v);
544     }
545     return 0;
546 }
547
548 static void clear_comments(CONF *conf, char *p)
549 {
550     for (;;) {
551         if (IS_FCOMMENT(conf, *p)) {
552             *p = '\0';
553             return;
554         }
555         if (!IS_WS(conf, *p)) {
556             break;
557         }
558         p++;
559     }
560
561     for (;;) {
562         if (IS_COMMENT(conf, *p)) {
563             *p = '\0';
564             return;
565         }
566         if (IS_DQUOTE(conf, *p)) {
567             p = scan_dquote(conf, p);
568             continue;
569         }
570         if (IS_QUOTE(conf, *p)) {
571             p = scan_quote(conf, p);
572             continue;
573         }
574         if (IS_ESC(conf, *p)) {
575             p = scan_esc(conf, p);
576             continue;
577         }
578         if (IS_EOF(conf, *p))
579             return;
580         else
581             p++;
582     }
583 }
584
585 static int str_copy(CONF *conf, char *section, char **pto, char *from)
586 {
587     int q, r, rr = 0, to = 0, len = 0;
588     char *s, *e, *rp, *p, *rrp, *np, *cp, v;
589     BUF_MEM *buf;
590
591     if ((buf = BUF_MEM_new()) == NULL)
592         return 0;
593
594     len = strlen(from) + 1;
595     if (!BUF_MEM_grow(buf, len))
596         goto err;
597
598     for (;;) {
599         if (IS_QUOTE(conf, *from)) {
600             q = *from;
601             from++;
602             while (!IS_EOF(conf, *from) && (*from != q)) {
603                 if (IS_ESC(conf, *from)) {
604                     from++;
605                     if (IS_EOF(conf, *from))
606                         break;
607                 }
608                 buf->data[to++] = *(from++);
609             }
610             if (*from == q)
611                 from++;
612         } else if (IS_DQUOTE(conf, *from)) {
613             q = *from;
614             from++;
615             while (!IS_EOF(conf, *from)) {
616                 if (*from == q) {
617                     if (*(from + 1) == q) {
618                         from++;
619                     } else {
620                         break;
621                     }
622                 }
623                 buf->data[to++] = *(from++);
624             }
625             if (*from == q)
626                 from++;
627         } else if (IS_ESC(conf, *from)) {
628             from++;
629             v = *(from++);
630             if (IS_EOF(conf, v))
631                 break;
632             else if (v == 'r')
633                 v = '\r';
634             else if (v == 'n')
635                 v = '\n';
636             else if (v == 'b')
637                 v = '\b';
638             else if (v == 't')
639                 v = '\t';
640             buf->data[to++] = v;
641         } else if (IS_EOF(conf, *from))
642             break;
643         else if (*from == '$'
644                  && (!conf->flag_dollarid
645                      || from[1] == '{'
646                      || from[1] == '(')) {
647             size_t newsize;
648
649             /* try to expand it */
650             rrp = NULL;
651             s = &(from[1]);
652             if (*s == '{')
653                 q = '}';
654             else if (*s == '(')
655                 q = ')';
656             else
657                 q = 0;
658
659             if (q)
660                 s++;
661             cp = section;
662             e = np = s;
663             while (IS_ALNUM(conf, *e)
664                    || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
665                 e++;
666             if ((e[0] == ':') && (e[1] == ':')) {
667                 cp = np;
668                 rrp = e;
669                 rr = *e;
670                 *rrp = '\0';
671                 e += 2;
672                 np = e;
673                 while (IS_ALNUM(conf, *e)
674                        || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
675                     e++;
676             }
677             r = *e;
678             *e = '\0';
679             rp = e;
680             if (q) {
681                 if (r != q) {
682                     ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE);
683                     goto err;
684                 }
685                 e++;
686             }
687             /*-
688              * So at this point we have
689              * np which is the start of the name string which is
690              *   '\0' terminated.
691              * cp which is the start of the section string which is
692              *   '\0' terminated.
693              * e is the 'next point after'.
694              * r and rr are the chars replaced by the '\0'
695              * rp and rrp is where 'r' and 'rr' came from.
696              */
697             p = _CONF_get_string(conf, cp, np);
698             if (rrp != NULL)
699                 *rrp = rr;
700             *rp = r;
701             if (p == NULL) {
702                 ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_HAS_NO_VALUE);
703                 goto err;
704             }
705             newsize = strlen(p) + buf->length - (e - from);
706             if (newsize > MAX_CONF_VALUE_LENGTH) {
707                 ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
708                 goto err;
709             }
710             if (!BUF_MEM_grow_clean(buf, newsize)) {
711                 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
712                 goto err;
713             }
714             while (*p)
715                 buf->data[to++] = *(p++);
716
717             /*
718              * Since we change the pointer 'from', we also have to change the
719              * perceived length of the string it points at.  /RL
720              */
721             len -= e - from;
722             from = e;
723
724             /*
725              * In case there were no braces or parenthesis around the
726              * variable reference, we have to put back the character that was
727              * replaced with a '\0'.  /RL
728              */
729             *rp = r;
730         } else
731             buf->data[to++] = *(from++);
732     }
733     buf->data[to] = '\0';
734     OPENSSL_free(*pto);
735     *pto = buf->data;
736     OPENSSL_free(buf);
737     return 1;
738  err:
739     BUF_MEM_free(buf);
740     return 0;
741 }
742
743 #ifndef OPENSSL_NO_POSIX_IO
744 /*
745  * Check whether included path is a directory.
746  * Returns next BIO to process and in case of a directory
747  * also an opened directory context and the include path.
748  */
749 static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
750                             char **dirpath)
751 {
752     struct stat st;
753     BIO *next;
754
755     if (stat(include, &st) < 0) {
756         ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include);
757         /* missing include file is not fatal error */
758         return NULL;
759     }
760
761     if (S_ISDIR(st.st_mode)) {
762         if (*dirctx != NULL) {
763             ERR_raise_data(ERR_LIB_CONF, CONF_R_RECURSIVE_DIRECTORY_INCLUDE,
764                            "%s", 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                 ERR_raise(ERR_LIB_CONF, 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 }