2 * Copyright 1995-2021 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 */
14 #include "e_os.h" /* strcasecmp and struct stat */
16 # include <sys/types.h> /* needed for stat.h */
17 # include <sys/stat.h> /* struct stat */
19 #include "internal/cryptlib.h"
20 #include "internal/o_dir.h"
21 #include <openssl/lhash.h>
22 #include <openssl/conf.h>
23 #include <openssl/conf_api.h>
24 #include "conf_local.h"
26 #include <openssl/buffer.h>
27 #include <openssl/err.h>
28 #ifndef OPENSSL_NO_POSIX_IO
29 # include <sys/stat.h>
36 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
40 * The maximum length we can grow a value to after variable expansion. 64k
41 * should be more than enough for all reasonable uses.
43 #define MAX_CONF_VALUE_LENGTH 65536
45 static int is_keytype(const CONF *conf, char c, unsigned short type);
46 static char *eat_ws(CONF *conf, char *p);
47 static void trim_ws(CONF *conf, char *start);
48 static char *eat_alpha_numeric(CONF *conf, char *p);
49 static void clear_comments(CONF *conf, char *p);
50 static int str_copy(CONF *conf, char *section, char **to, char *from);
51 static char *scan_quote(CONF *conf, char *p);
52 static char *scan_dquote(CONF *conf, char *p);
53 #define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
54 #ifndef OPENSSL_NO_POSIX_IO
55 static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
57 static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx);
60 static CONF *def_create(CONF_METHOD *meth);
61 static int def_init_default(CONF *conf);
62 #ifndef OPENSSL_NO_DEPRECATED_3_0
63 static int def_init_WIN32(CONF *conf);
65 static int def_destroy(CONF *conf);
66 static int def_destroy_data(CONF *conf);
67 static int def_load(CONF *conf, const char *name, long *eline);
68 static int def_load_bio(CONF *conf, BIO *bp, long *eline);
69 static int def_dump(const CONF *conf, BIO *bp);
70 static int def_is_number(const CONF *conf, char c);
71 static int def_to_int(const CONF *conf, char c);
73 static CONF_METHOD default_method = {
86 CONF_METHOD *NCONF_default(void)
88 return &default_method;
91 #ifndef OPENSSL_NO_DEPRECATED_3_0
92 static CONF_METHOD WIN32_method = {
105 CONF_METHOD *NCONF_WIN32(void)
107 return &WIN32_method;
111 static CONF *def_create(CONF_METHOD *meth)
115 ret = OPENSSL_malloc(sizeof(*ret));
117 if (meth->init(ret) == 0) {
124 static int def_init_default(CONF *conf)
129 memset(conf, 0, sizeof(*conf));
130 conf->meth = &default_method;
131 conf->meth_data = (void *)CONF_type_default;
136 #ifndef OPENSSL_NO_DEPRECATED_3_0
137 static int def_init_WIN32(CONF *conf)
142 memset(conf, 0, sizeof(*conf));
143 conf->meth = &WIN32_method;
144 conf->meth_data = (void *)CONF_type_win32;
150 static int def_destroy(CONF *conf)
152 if (def_destroy_data(conf)) {
159 static int def_destroy_data(CONF *conf)
163 _CONF_free_data(conf);
167 static int def_load(CONF *conf, const char *name, long *line)
172 #ifdef OPENSSL_SYS_VMS
173 in = BIO_new_file(name, "r");
175 in = BIO_new_file(name, "rb");
178 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
179 ERR_raise(ERR_LIB_CONF, CONF_R_NO_SUCH_FILE);
181 ERR_raise(ERR_LIB_CONF, ERR_R_SYS_LIB);
185 ret = def_load_bio(conf, in, line);
192 /* Parse a boolean value and fill in *flag. Return 0 on error. */
193 static int parsebool(const char *pval, int *flag)
195 if (strcasecmp(pval, "on") == 0
196 || strcasecmp(pval, "true") == 0) {
198 } else if (strcasecmp(pval, "off") == 0
199 || strcasecmp(pval, "false") == 0) {
202 ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
208 static int def_load_bio(CONF *conf, BIO *in, long *line)
210 /* The macro BUFSIZE conflicts with a system macro in VxWorks */
211 #define CONFBUFSIZE 512
212 int bufnum = 0, i, ii;
213 BUF_MEM *buff = NULL;
218 char btmp[DECIMAL_SIZE(eline) + 1];
219 CONF_VALUE *v = NULL, *tv;
220 CONF_VALUE *sv = NULL;
221 char *section = NULL, *buf;
222 char *start, *psection, *pname;
223 void *h = (void *)(conf->data);
224 STACK_OF(BIO) *biosk = NULL;
225 #ifndef OPENSSL_NO_POSIX_IO
226 char *dirpath = NULL;
227 OPENSSL_DIR_CTX *dirctx = NULL;
230 if ((buff = BUF_MEM_new()) == NULL) {
231 ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
235 section = OPENSSL_strdup("default");
236 if (section == NULL) {
237 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
241 if (_CONF_new_data(conf) == 0) {
242 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
246 sv = _CONF_new_section(conf, section);
248 ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
255 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
256 ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
259 p = &(buff->data[bufnum]);
262 if (in != NULL && BIO_gets(in, p, CONFBUFSIZE - 1) < 0)
264 p[CONFBUFSIZE - 1] = '\0';
267 /* Other BOMs imply unsupported multibyte encoding,
268 * so don't strip them and let the error raise */
269 const unsigned char utf8_bom[3] = {0xEF, 0xBB, 0xBF};
271 if (i >= 3 && memcmp(p, utf8_bom, 3) == 0) {
272 memmove(p, p + 3, i - 3);
279 if (i == 0 && !again) {
280 /* the currently processed BIO is NULL or at EOF */
283 #ifndef OPENSSL_NO_POSIX_IO
284 /* continue processing with the next file from directory */
285 if (dirctx != NULL) {
288 if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
293 OPENSSL_free(dirpath);
298 /* no more files in directory, continue with processing parent */
299 if ((parent = sk_BIO_pop(biosk)) == NULL) {
300 /* everything processed get out of the loop */
310 if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
316 * we removed some trailing stuff so there is a new line on the end.
319 again = 1; /* long line */
322 eline++; /* another input line */
325 /* we now have a line with trailing \r\n removed */
327 /* i is the number of bytes */
331 /* check for line continuation */
334 * If we have bytes and the last char '\\' and second last char
337 p = &(buff->data[bufnum - 1]);
338 if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
348 clear_comments(conf, buf);
349 s = eat_ws(conf, buf);
350 if (IS_EOF(conf, *s))
351 continue; /* blank line */
356 start = eat_ws(conf, s);
359 end = eat_alpha_numeric(conf, ss);
360 p = eat_ws(conf, end);
362 if (*p != '\0' && ss != p) {
366 ERR_raise(ERR_LIB_CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
370 if (!str_copy(conf, NULL, §ion, start))
372 if ((sv = _CONF_get_section(conf, section)) == NULL)
373 sv = _CONF_new_section(conf, section);
375 ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
381 end = eat_alpha_numeric(conf, s);
382 if ((end[0] == ':') && (end[1] == ':')) {
387 end = eat_alpha_numeric(conf, end);
391 p = eat_ws(conf, end);
392 if (strncmp(pname, ".pragma", 7) == 0
393 && (p != pname + 7 || *p == '=')) {
402 /* Pragma values take the form keyword:value */
403 pval = strchr(p, ':');
404 if (pval == NULL || pval == p || pval[1] == '\0') {
405 ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
411 pval = eat_ws(conf, pval);
416 * dollarid takes "on", "true or "off", "false"
417 * abspath takes "on", "true or "off", "false"
418 * includedir directory prefix
420 if (strcmp(p, "dollarid") == 0) {
421 if (!parsebool(pval, &conf->flag_dollarid))
423 } else if (strcmp(p, "abspath") == 0) {
424 if (!parsebool(pval, &conf->flag_abspath))
426 } else if (strcmp(p, "includedir") == 0) {
427 OPENSSL_free(conf->includedir);
428 if ((conf->includedir = OPENSSL_strdup(pval)) == NULL) {
429 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
435 * We *ignore* any unknown pragma.
438 } else if (strncmp(pname, ".include", 8) == 0
439 && (p != pname + 8 || *p == '=')) {
440 char *include = NULL;
442 const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE");
443 char *include_path = NULL;
445 if (include_dir == NULL)
446 include_dir = conf->includedir;
453 if (!str_copy(conf, psection, &include, p))
456 if (include_dir != NULL && !ossl_is_absolute_path(include)) {
457 size_t newlen = strlen(include_dir) + strlen(include) + 2;
459 include_path = OPENSSL_malloc(newlen);
460 if (include_path == NULL) {
461 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
462 OPENSSL_free(include);
466 OPENSSL_strlcpy(include_path, include_dir, newlen);
467 if (!ossl_ends_with_dirsep(include_path))
468 OPENSSL_strlcat(include_path, "/", newlen);
469 OPENSSL_strlcat(include_path, include, newlen);
470 OPENSSL_free(include);
472 include_path = include;
475 if (conf->flag_abspath
476 && !ossl_is_absolute_path(include_path)) {
477 ERR_raise(ERR_LIB_CONF, CONF_R_RELATIVE_PATH);
478 OPENSSL_free(include_path);
482 /* get the BIO of the included file */
483 #ifndef OPENSSL_NO_POSIX_IO
484 next = process_include(include_path, &dirctx, &dirpath);
485 if (include_path != dirpath) {
486 /* dirpath will contain include in case of a directory */
487 OPENSSL_free(include_path);
490 next = BIO_new_file(include_path, "r");
491 OPENSSL_free(include_path);
495 /* push the currently processing BIO onto stack */
497 if ((biosk = sk_BIO_new_null()) == NULL) {
498 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
503 if (!sk_BIO_push(biosk, in)) {
504 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
508 /* continue with reading from the included BIO */
512 } else if (*p != '=') {
513 ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN,
519 start = eat_ws(conf, p);
520 trim_ws(conf, start);
522 if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
523 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
526 v->name = OPENSSL_strdup(pname);
528 if (v->name == NULL) {
529 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
532 if (!str_copy(conf, psection, &(v->value), start))
535 if (strcmp(psection, section) != 0) {
536 if ((tv = _CONF_get_section(conf, psection))
538 tv = _CONF_new_section(conf, psection);
540 ERR_raise(ERR_LIB_CONF,
541 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
546 if (_CONF_add_string(conf, tv, v) == 0) {
547 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
554 OPENSSL_free(section);
556 * No need to pop, since we only get here if the stack is empty.
557 * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE!
564 OPENSSL_free(section);
566 * Since |in| is the first element of the stack and should NOT be freed
567 * here, we cannot use sk_BIO_pop_free(). Instead, we pop and free one
568 * BIO at a time, making sure that the last one popped isn't.
570 while (sk_BIO_num(biosk) > 0) {
571 BIO *popped = sk_BIO_pop(biosk);
576 #ifndef OPENSSL_NO_POSIX_IO
577 OPENSSL_free(dirpath);
579 OPENSSL_DIR_end(&dirctx);
583 BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
584 ERR_add_error_data(2, "line ", btmp);
585 if (h != conf->data) {
586 CONF_free(conf->data);
590 OPENSSL_free(v->name);
591 OPENSSL_free(v->value);
597 static void clear_comments(CONF *conf, char *p)
600 if (IS_FCOMMENT(conf, *p)) {
604 if (!IS_WS(conf, *p)) {
611 if (IS_COMMENT(conf, *p)) {
615 if (IS_DQUOTE(conf, *p)) {
616 p = scan_dquote(conf, p);
619 if (IS_QUOTE(conf, *p)) {
620 p = scan_quote(conf, p);
623 if (IS_ESC(conf, *p)) {
624 p = scan_esc(conf, p);
627 if (IS_EOF(conf, *p))
634 static int str_copy(CONF *conf, char *section, char **pto, char *from)
636 int q, r, rr = 0, to = 0, len = 0;
637 char *s, *e, *rp, *p, *rrp, *np, *cp, v;
640 if ((buf = BUF_MEM_new()) == NULL)
643 len = strlen(from) + 1;
644 if (!BUF_MEM_grow(buf, len))
648 if (IS_QUOTE(conf, *from)) {
651 while (!IS_EOF(conf, *from) && (*from != q)) {
652 if (IS_ESC(conf, *from)) {
654 if (IS_EOF(conf, *from))
657 buf->data[to++] = *(from++);
661 } else if (IS_DQUOTE(conf, *from)) {
664 while (!IS_EOF(conf, *from)) {
666 if (*(from + 1) == q) {
672 buf->data[to++] = *(from++);
676 } else if (IS_ESC(conf, *from)) {
690 } else if (IS_EOF(conf, *from))
692 else if (*from == '$'
693 && (!conf->flag_dollarid
695 || from[1] == '(')) {
698 /* try to expand it */
712 while (IS_ALNUM(conf, *e)
713 || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
715 if ((e[0] == ':') && (e[1] == ':')) {
722 while (IS_ALNUM(conf, *e)
723 || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
731 ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE);
737 * So at this point we have
738 * np which is the start of the name string which is
740 * cp which is the start of the section string which is
742 * e is the 'next point after'.
743 * r and rr are the chars replaced by the '\0'
744 * rp and rrp is where 'r' and 'rr' came from.
746 p = _CONF_get_string(conf, cp, np);
751 ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_HAS_NO_VALUE);
754 newsize = strlen(p) + buf->length - (e - from);
755 if (newsize > MAX_CONF_VALUE_LENGTH) {
756 ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
759 if (!BUF_MEM_grow_clean(buf, newsize)) {
760 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
764 buf->data[to++] = *(p++);
767 * Since we change the pointer 'from', we also have to change the
768 * perceived length of the string it points at. /RL
774 * In case there were no braces or parenthesis around the
775 * variable reference, we have to put back the character that was
776 * replaced with a '\0'. /RL
780 buf->data[to++] = *(from++);
782 buf->data[to] = '\0';
792 #ifndef OPENSSL_NO_POSIX_IO
794 * Check whether included path is a directory.
795 * Returns next BIO to process and in case of a directory
796 * also an opened directory context and the include path.
798 static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
804 if (stat(include, &st) < 0) {
805 ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include);
806 /* missing include file is not fatal error */
810 if (S_ISDIR(st.st_mode)) {
811 if (*dirctx != NULL) {
812 ERR_raise_data(ERR_LIB_CONF, CONF_R_RECURSIVE_DIRECTORY_INCLUDE,
816 /* a directory, load its contents */
817 if ((next = get_next_file(include, dirctx)) != NULL)
822 next = BIO_new_file(include, "r");
827 * Get next file from the directory path.
828 * Returns BIO of the next file to read and updates dirctx.
830 static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx)
832 const char *filename;
835 pathlen = strlen(path);
836 while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
839 namelen = strlen(filename);
842 if ((namelen > 5 && strcasecmp(filename + namelen - 5, ".conf") == 0)
843 || (namelen > 4 && strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
848 newlen = pathlen + namelen + 2;
849 newpath = OPENSSL_zalloc(newlen);
850 if (newpath == NULL) {
851 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
854 #ifdef OPENSSL_SYS_VMS
856 * If the given path isn't clear VMS syntax,
857 * we treat it as on Unix.
859 if (path[pathlen - 1] == ']'
860 || path[pathlen - 1] == '>'
861 || path[pathlen - 1] == ':') {
862 /* Clear VMS directory syntax, just copy as is */
863 OPENSSL_strlcpy(newpath, path, newlen);
866 if (newpath[0] == '\0') {
867 OPENSSL_strlcpy(newpath, path, newlen);
868 OPENSSL_strlcat(newpath, "/", newlen);
870 OPENSSL_strlcat(newpath, filename, newlen);
872 bio = BIO_new_file(newpath, "r");
873 OPENSSL_free(newpath);
874 /* Errors when opening files are non-fatal. */
879 OPENSSL_DIR_end(dirctx);
885 static int is_keytype(const CONF *conf, char c, unsigned short type)
887 const unsigned short * keytypes = (const unsigned short *) conf->meth_data;
888 unsigned char key = (unsigned char)c;
890 #ifdef CHARSET_EBCDIC
893 /* key is out of range for os_toascii table */
897 /* convert key from ebcdic to ascii */
898 key = os_toascii[key];
902 /* key is not a seven bit ascii character */
906 return (keytypes[key] & type) ? 1 : 0;
909 static char *eat_ws(CONF *conf, char *p)
911 while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
916 static void trim_ws(CONF *conf, char *start)
920 while (!IS_EOF(conf, *p))
923 while ((p >= start) && IS_WS(conf, *p))
929 static char *eat_alpha_numeric(CONF *conf, char *p)
932 if (IS_ESC(conf, *p)) {
933 p = scan_esc(conf, p);
936 if (!(IS_ALNUM_PUNCT(conf, *p)
937 || (conf->flag_dollarid && IS_DOLLAR(conf, *p))))
943 static char *scan_quote(CONF *conf, char *p)
948 while (!(IS_EOF(conf, *p)) && (*p != q)) {
949 if (IS_ESC(conf, *p)) {
951 if (IS_EOF(conf, *p))
961 static char *scan_dquote(CONF *conf, char *p)
966 while (!(IS_EOF(conf, *p))) {
981 static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
984 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
986 BIO_printf(out, "[[%s]]\n", a->section);
989 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
991 static int def_dump(const CONF *conf, BIO *out)
993 lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
997 static int def_is_number(const CONF *conf, char c)
999 return IS_NUMBER(conf, c);
1002 static int def_to_int(const CONF *conf, char c)