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
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 CONFerr(CONF_F_DEF_LOAD, CONF_R_NO_SUCH_FILE);
181 CONFerr(CONF_F_DEF_LOAD, ERR_R_SYS_LIB);
185 ret = def_load_bio(conf, in, line);
191 static int def_load_bio(CONF *conf, BIO *in, long *line)
193 /* The macro BUFSIZE conflicts with a system macro in VxWorks */
194 #define CONFBUFSIZE 512
195 int bufnum = 0, i, ii;
196 BUF_MEM *buff = NULL;
200 char btmp[DECIMAL_SIZE(eline) + 1];
201 CONF_VALUE *v = NULL, *tv;
202 CONF_VALUE *sv = NULL;
203 char *section = NULL, *buf;
204 char *start, *psection, *pname;
205 void *h = (void *)(conf->data);
206 STACK_OF(BIO) *biosk = NULL;
207 #ifndef OPENSSL_NO_POSIX_IO
208 char *dirpath = NULL;
209 OPENSSL_DIR_CTX *dirctx = NULL;
212 if ((buff = BUF_MEM_new()) == NULL) {
213 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
217 section = OPENSSL_strdup("default");
218 if (section == NULL) {
219 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
223 if (_CONF_new_data(conf) == 0) {
224 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
228 sv = _CONF_new_section(conf, section);
230 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
237 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
238 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
241 p = &(buff->data[bufnum]);
244 BIO_gets(in, p, CONFBUFSIZE - 1);
245 p[CONFBUFSIZE - 1] = '\0';
247 if (i == 0 && !again) {
248 /* the currently processed BIO is at EOF */
251 #ifndef OPENSSL_NO_POSIX_IO
252 /* continue processing with the next file from directory */
253 if (dirctx != NULL) {
256 if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
261 OPENSSL_free(dirpath);
266 /* no more files in directory, continue with processing parent */
267 if ((parent = sk_BIO_pop(biosk)) == NULL) {
268 /* everything processed get out of the loop */
278 if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
284 * we removed some trailing stuff so there is a new line on the end.
287 again = 1; /* long line */
290 eline++; /* another input line */
293 /* we now have a line with trailing \r\n removed */
295 /* i is the number of bytes */
299 /* check for line continuation */
302 * If we have bytes and the last char '\\' and second last char
305 p = &(buff->data[bufnum - 1]);
306 if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
316 clear_comments(conf, buf);
317 s = eat_ws(conf, buf);
318 if (IS_EOF(conf, *s))
319 continue; /* blank line */
324 start = eat_ws(conf, s);
327 end = eat_alpha_numeric(conf, ss);
328 p = eat_ws(conf, end);
330 if (*p != '\0' && ss != p) {
334 CONFerr(CONF_F_DEF_LOAD_BIO,
335 CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
339 if (!str_copy(conf, NULL, §ion, start))
341 if ((sv = _CONF_get_section(conf, section)) == NULL)
342 sv = _CONF_new_section(conf, section);
344 CONFerr(CONF_F_DEF_LOAD_BIO,
345 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
351 end = eat_alpha_numeric(conf, s);
352 if ((end[0] == ':') && (end[1] == ':')) {
357 end = eat_alpha_numeric(conf, end);
361 p = eat_ws(conf, end);
362 if (strncmp(pname, ".pragma", 7) == 0
363 && (p != pname + 7 || *p == '=')) {
372 /* Pragma values take the form keyword:value */
373 pval = strchr(p, ':');
374 if (pval == NULL || pval == p || pval[1] == '\0') {
375 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_INVALID_PRAGMA);
381 pval = eat_ws(conf, pval);
386 * dollarid takes "on", "true or "off", "false"
388 if (strcmp(p, "dollarid") == 0) {
389 if (strcmp(pval, "on") == 0
390 || strcmp(pval, "true") == 0) {
391 conf->flag_dollarid = 1;
392 } else if (strcmp(pval, "off") == 0
393 || strcmp(pval, "false") == 0) {
394 conf->flag_dollarid = 0;
396 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_INVALID_PRAGMA);
401 * We *ignore* any unknown pragma.
404 } else if (strncmp(pname, ".include", 8) == 0
405 && (p != pname + 8 || *p == '=')) {
406 char *include = NULL;
408 const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE");
409 char *include_path = NULL;
416 if (!str_copy(conf, psection, &include, p))
419 if (include_dir != NULL) {
420 size_t newlen = strlen(include_dir) + strlen(include) + 2;
422 include_path = OPENSSL_malloc(newlen);
423 OPENSSL_strlcpy(include_path, include_dir, newlen);
424 OPENSSL_strlcat(include_path, "/", newlen);
425 OPENSSL_strlcat(include_path, include, newlen);
426 OPENSSL_free(include);
428 include_path = include;
431 /* get the BIO of the included file */
432 #ifndef OPENSSL_NO_POSIX_IO
433 next = process_include(include_path, &dirctx, &dirpath);
434 if (include_path != dirpath) {
435 /* dirpath will contain include in case of a directory */
436 OPENSSL_free(include_path);
439 next = BIO_new_file(include_path, "r");
440 OPENSSL_free(include_path);
444 /* push the currently processing BIO onto stack */
446 if ((biosk = sk_BIO_new_null()) == NULL) {
447 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
452 if (!sk_BIO_push(biosk, in)) {
453 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
457 /* continue with reading from the included BIO */
461 } else if (*p != '=') {
462 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);
463 ERR_add_error_data(2, "HERE-->", p);
468 start = eat_ws(conf, p);
469 trim_ws(conf, start);
471 if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
472 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
475 v->name = OPENSSL_strdup(pname);
477 if (v->name == NULL) {
478 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
481 if (!str_copy(conf, psection, &(v->value), start))
484 if (strcmp(psection, section) != 0) {
485 if ((tv = _CONF_get_section(conf, psection))
487 tv = _CONF_new_section(conf, psection);
489 CONFerr(CONF_F_DEF_LOAD_BIO,
490 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
495 if (_CONF_add_string(conf, tv, v) == 0) {
496 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
503 OPENSSL_free(section);
505 * No need to pop, since we only get here if the stack is empty.
506 * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE!
512 OPENSSL_free(section);
514 * Since |in| is the first element of the stack and should NOT be freed
515 * here, we cannot use sk_BIO_pop_free(). Instead, we pop and free one
516 * BIO at a time, making sure that the last one popped isn't.
518 while (sk_BIO_num(biosk) > 0) {
519 BIO *popped = sk_BIO_pop(biosk);
524 #ifndef OPENSSL_NO_POSIX_IO
525 OPENSSL_free(dirpath);
527 OPENSSL_DIR_end(&dirctx);
531 BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
532 ERR_add_error_data(2, "line ", btmp);
533 if (h != conf->data) {
534 CONF_free(conf->data);
538 OPENSSL_free(v->name);
539 OPENSSL_free(v->value);
545 static void clear_comments(CONF *conf, char *p)
548 if (IS_FCOMMENT(conf, *p)) {
552 if (!IS_WS(conf, *p)) {
559 if (IS_COMMENT(conf, *p)) {
563 if (IS_DQUOTE(conf, *p)) {
564 p = scan_dquote(conf, p);
567 if (IS_QUOTE(conf, *p)) {
568 p = scan_quote(conf, p);
571 if (IS_ESC(conf, *p)) {
572 p = scan_esc(conf, p);
575 if (IS_EOF(conf, *p))
582 static int str_copy(CONF *conf, char *section, char **pto, char *from)
584 int q, r, rr = 0, to = 0, len = 0;
585 char *s, *e, *rp, *p, *rrp, *np, *cp, v;
588 if ((buf = BUF_MEM_new()) == NULL)
591 len = strlen(from) + 1;
592 if (!BUF_MEM_grow(buf, len))
596 if (IS_QUOTE(conf, *from)) {
599 while (!IS_EOF(conf, *from) && (*from != q)) {
600 if (IS_ESC(conf, *from)) {
602 if (IS_EOF(conf, *from))
605 buf->data[to++] = *(from++);
609 } else if (IS_DQUOTE(conf, *from)) {
612 while (!IS_EOF(conf, *from)) {
614 if (*(from + 1) == q) {
620 buf->data[to++] = *(from++);
624 } else if (IS_ESC(conf, *from)) {
638 } else if (IS_EOF(conf, *from))
640 else if (*from == '$'
641 && (!conf->flag_dollarid
643 || from[1] == '(')) {
646 /* try to expand it */
660 while (IS_ALNUM(conf, *e)
661 || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
663 if ((e[0] == ':') && (e[1] == ':')) {
670 while (IS_ALNUM(conf, *e)
671 || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
679 CONFerr(CONF_F_STR_COPY, CONF_R_NO_CLOSE_BRACE);
685 * So at this point we have
686 * np which is the start of the name string which is
688 * cp which is the start of the section string which is
690 * e is the 'next point after'.
691 * r and rr are the chars replaced by the '\0'
692 * rp and rrp is where 'r' and 'rr' came from.
694 p = _CONF_get_string(conf, cp, np);
699 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_HAS_NO_VALUE);
702 newsize = strlen(p) + buf->length - (e - from);
703 if (newsize > MAX_CONF_VALUE_LENGTH) {
704 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
707 if (!BUF_MEM_grow_clean(buf, newsize)) {
708 CONFerr(CONF_F_STR_COPY, ERR_R_MALLOC_FAILURE);
712 buf->data[to++] = *(p++);
715 * Since we change the pointer 'from', we also have to change the
716 * perceived length of the string it points at. /RL
722 * In case there were no braces or parenthesis around the
723 * variable reference, we have to put back the character that was
724 * replaced with a '\0'. /RL
728 buf->data[to++] = *(from++);
730 buf->data[to] = '\0';
740 #ifndef OPENSSL_NO_POSIX_IO
742 * Check whether included path is a directory.
743 * Returns next BIO to process and in case of a directory
744 * also an opened directory context and the include path.
746 static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
752 if (stat(include, &st) < 0) {
753 ERR_raise_data(ERR_LIB_SYS, errno,
756 /* missing include file is not fatal error */
760 if (S_ISDIR(st.st_mode)) {
761 if (*dirctx != NULL) {
762 CONFerr(CONF_F_PROCESS_INCLUDE,
763 CONF_R_RECURSIVE_DIRECTORY_INCLUDE);
764 ERR_add_error_data(1, include);
767 /* a directory, load its contents */
768 if ((next = get_next_file(include, dirctx)) != NULL)
773 next = BIO_new_file(include, "r");
778 * Get next file from the directory path.
779 * Returns BIO of the next file to read and updates dirctx.
781 static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx)
783 const char *filename;
786 pathlen = strlen(path);
787 while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
790 namelen = strlen(filename);
793 if ((namelen > 5 && strcasecmp(filename + namelen - 5, ".conf") == 0)
794 || (namelen > 4 && strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
799 newlen = pathlen + namelen + 2;
800 newpath = OPENSSL_zalloc(newlen);
801 if (newpath == NULL) {
802 CONFerr(CONF_F_GET_NEXT_FILE, ERR_R_MALLOC_FAILURE);
805 #ifdef OPENSSL_SYS_VMS
807 * If the given path isn't clear VMS syntax,
808 * we treat it as on Unix.
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);
817 if (newpath[0] == '\0') {
818 OPENSSL_strlcpy(newpath, path, newlen);
819 OPENSSL_strlcat(newpath, "/", newlen);
821 OPENSSL_strlcat(newpath, filename, newlen);
823 bio = BIO_new_file(newpath, "r");
824 OPENSSL_free(newpath);
825 /* Errors when opening files are non-fatal. */
830 OPENSSL_DIR_end(dirctx);
836 static int is_keytype(const CONF *conf, char c, unsigned short type)
838 const unsigned short * keytypes = (const unsigned short *) conf->meth_data;
839 unsigned char key = (unsigned char)c;
841 #ifdef CHARSET_EBCDIC
844 /* key is out of range for os_toascii table */
848 /* convert key from ebcdic to ascii */
849 key = os_toascii[key];
853 /* key is not a seven bit ascii character */
857 return (keytypes[key] & type) ? 1 : 0;
860 static char *eat_ws(CONF *conf, char *p)
862 while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
867 static void trim_ws(CONF *conf, char *start)
871 while (!IS_EOF(conf, *p))
874 while ((p >= start) && IS_WS(conf, *p))
880 static char *eat_alpha_numeric(CONF *conf, char *p)
883 if (IS_ESC(conf, *p)) {
884 p = scan_esc(conf, p);
887 if (!(IS_ALNUM_PUNCT(conf, *p)
888 || (conf->flag_dollarid && IS_DOLLAR(conf, *p))))
894 static char *scan_quote(CONF *conf, char *p)
899 while (!(IS_EOF(conf, *p)) && (*p != q)) {
900 if (IS_ESC(conf, *p)) {
902 if (IS_EOF(conf, *p))
912 static char *scan_dquote(CONF *conf, char *p)
917 while (!(IS_EOF(conf, *p))) {
932 static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
935 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
937 BIO_printf(out, "[[%s]]\n", a->section);
940 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
942 static int def_dump(const CONF *conf, BIO *out)
944 lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
948 static int def_is_number(const CONF *conf, char c)
950 return IS_NUMBER(conf, c);
953 static int def_to_int(const CONF *conf, char c)