2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* Part of the code in here was originally in conf.c, which is now removed */
15 # include <strings.h> /* strcasecmp */
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>
23 #include <openssl/buffer.h>
24 #include <openssl/err.h>
25 #ifndef OPENSSL_NO_POSIX_IO
26 # include <sys/stat.h>
29 # define strcasecmp _stricmp
34 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
38 * The maximum length we can grow a value to after variable expansion. 64k
39 * should be more than enough for all reasonable uses.
41 #define MAX_CONF_VALUE_LENGTH 65536
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,
55 static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx);
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);
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);
71 static CONF_METHOD default_method = {
84 CONF_METHOD *NCONF_default(void)
86 return &default_method;
89 #ifndef OPENSSL_NO_DEPRECATED_3_0
90 static CONF_METHOD WIN32_method = {
103 CONF_METHOD *NCONF_WIN32(void)
105 return &WIN32_method;
109 static CONF *def_create(CONF_METHOD *meth)
113 ret = OPENSSL_malloc(sizeof(*ret));
115 if (meth->init(ret) == 0) {
122 static int def_init_default(CONF *conf)
127 memset(conf, 0, sizeof(*conf));
128 conf->meth = &default_method;
129 conf->meth_data = (void *)CONF_type_default;
134 #ifndef OPENSSL_NO_DEPRECATED_3_0
135 static int def_init_WIN32(CONF *conf)
140 memset(conf, 0, sizeof(*conf));
141 conf->meth = &WIN32_method;
142 conf->meth_data = (void *)CONF_type_win32;
148 static int def_destroy(CONF *conf)
150 if (def_destroy_data(conf)) {
157 static int def_destroy_data(CONF *conf)
161 _CONF_free_data(conf);
165 static int def_load(CONF *conf, const char *name, long *line)
170 #ifdef OPENSSL_SYS_VMS
171 in = BIO_new_file(name, "r");
173 in = BIO_new_file(name, "rb");
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);
179 ERR_raise(ERR_LIB_CONF, ERR_R_SYS_LIB);
183 ret = def_load_bio(conf, in, line);
189 static int def_load_bio(CONF *conf, BIO *in, long *line)
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;
199 char btmp[DECIMAL_SIZE(eline) + 1];
200 CONF_VALUE *v = NULL, *tv;
201 CONF_VALUE *sv = NULL;
202 char *section = NULL, *buf;
203 char *start, *psection, *pname;
204 void *h = (void *)(conf->data);
205 STACK_OF(BIO) *biosk = NULL;
206 #ifndef OPENSSL_NO_POSIX_IO
207 char *dirpath = NULL;
208 OPENSSL_DIR_CTX *dirctx = NULL;
211 if ((buff = BUF_MEM_new()) == NULL) {
212 ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
216 section = OPENSSL_strdup("default");
217 if (section == NULL) {
218 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
222 if (_CONF_new_data(conf) == 0) {
223 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
227 sv = _CONF_new_section(conf, section);
229 ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
236 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
237 ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
240 p = &(buff->data[bufnum]);
243 if (in != NULL && BIO_gets(in, p, CONFBUFSIZE - 1) < 0)
245 p[CONFBUFSIZE - 1] = '\0';
248 /* Other BOMs imply unsupported multibyte encoding,
249 * so don't strip them and let the error raise */
250 const unsigned char utf8_bom[3] = {0xEF, 0xBB, 0xBF};
252 if (i >= 3 && memcmp(p, utf8_bom, 3) == 0) {
253 memmove(p, p + 3, i - 3);
260 if (i == 0 && !again) {
261 /* the currently processed BIO is NULL or at EOF */
264 #ifndef OPENSSL_NO_POSIX_IO
265 /* continue processing with the next file from directory */
266 if (dirctx != NULL) {
269 if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
274 OPENSSL_free(dirpath);
279 /* no more files in directory, continue with processing parent */
280 if ((parent = sk_BIO_pop(biosk)) == NULL) {
281 /* everything processed get out of the loop */
291 if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
297 * we removed some trailing stuff so there is a new line on the end.
300 again = 1; /* long line */
303 eline++; /* another input line */
306 /* we now have a line with trailing \r\n removed */
308 /* i is the number of bytes */
312 /* check for line continuation */
315 * If we have bytes and the last char '\\' and second last char
318 p = &(buff->data[bufnum - 1]);
319 if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
329 clear_comments(conf, buf);
330 s = eat_ws(conf, buf);
331 if (IS_EOF(conf, *s))
332 continue; /* blank line */
337 start = eat_ws(conf, s);
340 end = eat_alpha_numeric(conf, ss);
341 p = eat_ws(conf, end);
343 if (*p != '\0' && ss != p) {
347 ERR_raise(ERR_LIB_CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
351 if (!str_copy(conf, NULL, §ion, start))
353 if ((sv = _CONF_get_section(conf, section)) == NULL)
354 sv = _CONF_new_section(conf, section);
356 ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
362 end = eat_alpha_numeric(conf, s);
363 if ((end[0] == ':') && (end[1] == ':')) {
368 end = eat_alpha_numeric(conf, end);
372 p = eat_ws(conf, end);
373 if (strncmp(pname, ".pragma", 7) == 0
374 && (p != pname + 7 || *p == '=')) {
383 /* Pragma values take the form keyword:value */
384 pval = strchr(p, ':');
385 if (pval == NULL || pval == p || pval[1] == '\0') {
386 ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
392 pval = eat_ws(conf, pval);
397 * dollarid takes "on", "true or "off", "false"
399 if (strcmp(p, "dollarid") == 0) {
400 if (strcmp(pval, "on") == 0
401 || strcmp(pval, "true") == 0) {
402 conf->flag_dollarid = 1;
403 } else if (strcmp(pval, "off") == 0
404 || strcmp(pval, "false") == 0) {
405 conf->flag_dollarid = 0;
407 ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
412 * We *ignore* any unknown pragma.
415 } else if (strncmp(pname, ".include", 8) == 0
416 && (p != pname + 8 || *p == '=')) {
417 char *include = NULL;
419 const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE");
420 char *include_path = NULL;
427 if (!str_copy(conf, psection, &include, p))
430 if (include_dir != NULL && !ossl_is_absolute_path(include)) {
431 size_t newlen = strlen(include_dir) + strlen(include) + 2;
433 include_path = OPENSSL_malloc(newlen);
434 if (include_path == NULL) {
435 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
436 OPENSSL_free(include);
440 OPENSSL_strlcpy(include_path, include_dir, newlen);
441 if (!ossl_ends_with_dirsep(include_path))
442 OPENSSL_strlcat(include_path, "/", newlen);
443 OPENSSL_strlcat(include_path, include, newlen);
444 OPENSSL_free(include);
446 include_path = include;
449 /* get the BIO of the included file */
450 #ifndef OPENSSL_NO_POSIX_IO
451 next = process_include(include_path, &dirctx, &dirpath);
452 if (include_path != dirpath) {
453 /* dirpath will contain include in case of a directory */
454 OPENSSL_free(include_path);
457 next = BIO_new_file(include_path, "r");
458 OPENSSL_free(include_path);
462 /* push the currently processing BIO onto stack */
464 if ((biosk = sk_BIO_new_null()) == NULL) {
465 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
470 if (!sk_BIO_push(biosk, in)) {
471 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
475 /* continue with reading from the included BIO */
479 } else if (*p != '=') {
480 ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN,
486 start = eat_ws(conf, p);
487 trim_ws(conf, start);
489 if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
490 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
493 v->name = OPENSSL_strdup(pname);
495 if (v->name == NULL) {
496 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
499 if (!str_copy(conf, psection, &(v->value), start))
502 if (strcmp(psection, section) != 0) {
503 if ((tv = _CONF_get_section(conf, psection))
505 tv = _CONF_new_section(conf, psection);
507 ERR_raise(ERR_LIB_CONF,
508 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
513 if (_CONF_add_string(conf, tv, v) == 0) {
514 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
521 OPENSSL_free(section);
523 * No need to pop, since we only get here if the stack is empty.
524 * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE!
530 OPENSSL_free(section);
532 * Since |in| is the first element of the stack and should NOT be freed
533 * here, we cannot use sk_BIO_pop_free(). Instead, we pop and free one
534 * BIO at a time, making sure that the last one popped isn't.
536 while (sk_BIO_num(biosk) > 0) {
537 BIO *popped = sk_BIO_pop(biosk);
542 #ifndef OPENSSL_NO_POSIX_IO
543 OPENSSL_free(dirpath);
545 OPENSSL_DIR_end(&dirctx);
549 BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
550 ERR_add_error_data(2, "line ", btmp);
551 if (h != conf->data) {
552 CONF_free(conf->data);
556 OPENSSL_free(v->name);
557 OPENSSL_free(v->value);
563 static void clear_comments(CONF *conf, char *p)
566 if (IS_FCOMMENT(conf, *p)) {
570 if (!IS_WS(conf, *p)) {
577 if (IS_COMMENT(conf, *p)) {
581 if (IS_DQUOTE(conf, *p)) {
582 p = scan_dquote(conf, p);
585 if (IS_QUOTE(conf, *p)) {
586 p = scan_quote(conf, p);
589 if (IS_ESC(conf, *p)) {
590 p = scan_esc(conf, p);
593 if (IS_EOF(conf, *p))
600 static int str_copy(CONF *conf, char *section, char **pto, char *from)
602 int q, r, rr = 0, to = 0, len = 0;
603 char *s, *e, *rp, *p, *rrp, *np, *cp, v;
606 if ((buf = BUF_MEM_new()) == NULL)
609 len = strlen(from) + 1;
610 if (!BUF_MEM_grow(buf, len))
614 if (IS_QUOTE(conf, *from)) {
617 while (!IS_EOF(conf, *from) && (*from != q)) {
618 if (IS_ESC(conf, *from)) {
620 if (IS_EOF(conf, *from))
623 buf->data[to++] = *(from++);
627 } else if (IS_DQUOTE(conf, *from)) {
630 while (!IS_EOF(conf, *from)) {
632 if (*(from + 1) == q) {
638 buf->data[to++] = *(from++);
642 } else if (IS_ESC(conf, *from)) {
656 } else if (IS_EOF(conf, *from))
658 else if (*from == '$'
659 && (!conf->flag_dollarid
661 || from[1] == '(')) {
664 /* try to expand it */
678 while (IS_ALNUM(conf, *e)
679 || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
681 if ((e[0] == ':') && (e[1] == ':')) {
688 while (IS_ALNUM(conf, *e)
689 || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
697 ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE);
703 * So at this point we have
704 * np which is the start of the name string which is
706 * cp which is the start of the section string which is
708 * e is the 'next point after'.
709 * r and rr are the chars replaced by the '\0'
710 * rp and rrp is where 'r' and 'rr' came from.
712 p = _CONF_get_string(conf, cp, np);
717 ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_HAS_NO_VALUE);
720 newsize = strlen(p) + buf->length - (e - from);
721 if (newsize > MAX_CONF_VALUE_LENGTH) {
722 ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
725 if (!BUF_MEM_grow_clean(buf, newsize)) {
726 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
730 buf->data[to++] = *(p++);
733 * Since we change the pointer 'from', we also have to change the
734 * perceived length of the string it points at. /RL
740 * In case there were no braces or parenthesis around the
741 * variable reference, we have to put back the character that was
742 * replaced with a '\0'. /RL
746 buf->data[to++] = *(from++);
748 buf->data[to] = '\0';
758 #ifndef OPENSSL_NO_POSIX_IO
760 * Check whether included path is a directory.
761 * Returns next BIO to process and in case of a directory
762 * also an opened directory context and the include path.
764 static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
770 if (stat(include, &st) < 0) {
771 ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include);
772 /* missing include file is not fatal error */
776 if (S_ISDIR(st.st_mode)) {
777 if (*dirctx != NULL) {
778 ERR_raise_data(ERR_LIB_CONF, CONF_R_RECURSIVE_DIRECTORY_INCLUDE,
782 /* a directory, load its contents */
783 if ((next = get_next_file(include, dirctx)) != NULL)
788 next = BIO_new_file(include, "r");
793 * Get next file from the directory path.
794 * Returns BIO of the next file to read and updates dirctx.
796 static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx)
798 const char *filename;
801 pathlen = strlen(path);
802 while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
805 namelen = strlen(filename);
808 if ((namelen > 5 && strcasecmp(filename + namelen - 5, ".conf") == 0)
809 || (namelen > 4 && strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
814 newlen = pathlen + namelen + 2;
815 newpath = OPENSSL_zalloc(newlen);
816 if (newpath == NULL) {
817 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
820 #ifdef OPENSSL_SYS_VMS
822 * If the given path isn't clear VMS syntax,
823 * we treat it as on Unix.
825 if (path[pathlen - 1] == ']'
826 || path[pathlen - 1] == '>'
827 || path[pathlen - 1] == ':') {
828 /* Clear VMS directory syntax, just copy as is */
829 OPENSSL_strlcpy(newpath, path, newlen);
832 if (newpath[0] == '\0') {
833 OPENSSL_strlcpy(newpath, path, newlen);
834 OPENSSL_strlcat(newpath, "/", newlen);
836 OPENSSL_strlcat(newpath, filename, newlen);
838 bio = BIO_new_file(newpath, "r");
839 OPENSSL_free(newpath);
840 /* Errors when opening files are non-fatal. */
845 OPENSSL_DIR_end(dirctx);
851 static int is_keytype(const CONF *conf, char c, unsigned short type)
853 const unsigned short * keytypes = (const unsigned short *) conf->meth_data;
854 unsigned char key = (unsigned char)c;
856 #ifdef CHARSET_EBCDIC
859 /* key is out of range for os_toascii table */
863 /* convert key from ebcdic to ascii */
864 key = os_toascii[key];
868 /* key is not a seven bit ascii character */
872 return (keytypes[key] & type) ? 1 : 0;
875 static char *eat_ws(CONF *conf, char *p)
877 while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
882 static void trim_ws(CONF *conf, char *start)
886 while (!IS_EOF(conf, *p))
889 while ((p >= start) && IS_WS(conf, *p))
895 static char *eat_alpha_numeric(CONF *conf, char *p)
898 if (IS_ESC(conf, *p)) {
899 p = scan_esc(conf, p);
902 if (!(IS_ALNUM_PUNCT(conf, *p)
903 || (conf->flag_dollarid && IS_DOLLAR(conf, *p))))
909 static char *scan_quote(CONF *conf, char *p)
914 while (!(IS_EOF(conf, *p)) && (*p != q)) {
915 if (IS_ESC(conf, *p)) {
917 if (IS_EOF(conf, *p))
927 static char *scan_dquote(CONF *conf, char *p)
932 while (!(IS_EOF(conf, *p))) {
947 static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
950 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
952 BIO_printf(out, "[[%s]]\n", a->section);
955 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
957 static int def_dump(const CONF *conf, BIO *out)
959 lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
963 static int def_is_number(const CONF *conf, char c)
965 return IS_NUMBER(conf, c);
968 static int def_to_int(const CONF *conf, char c)