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