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 */
15 # include <strings.h> /* strcasecmp */
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>
25 #include <openssl/buffer.h>
26 #include <openssl/err.h>
27 #ifndef OPENSSL_NO_POSIX_IO
28 # include <sys/stat.h>
31 # 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 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);
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;
201 char btmp[DECIMAL_SIZE(eline) + 1];
202 CONF_VALUE *v = NULL, *tv;
203 CONF_VALUE *sv = NULL;
204 char *section = NULL, *buf;
205 char *start, *psection, *pname;
206 void *h = (void *)(conf->data);
207 STACK_OF(BIO) *biosk = NULL;
208 #ifndef OPENSSL_NO_POSIX_IO
209 char *dirpath = NULL;
210 OPENSSL_DIR_CTX *dirctx = NULL;
213 if ((buff = BUF_MEM_new()) == NULL) {
214 ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
218 section = OPENSSL_strdup("default");
219 if (section == NULL) {
220 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
224 if (_CONF_new_data(conf) == 0) {
225 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
229 sv = _CONF_new_section(conf, section);
231 ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
238 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
239 ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
242 p = &(buff->data[bufnum]);
245 if (in != NULL && BIO_gets(in, p, CONFBUFSIZE - 1) < 0)
247 p[CONFBUFSIZE - 1] = '\0';
250 /* Other BOMs imply unsupported multibyte encoding,
251 * so don't strip them and let the error raise */
252 const unsigned char utf8_bom[3] = {0xEF, 0xBB, 0xBF};
254 if (i >= 3 && memcmp(p, utf8_bom, 3) == 0) {
255 memmove(p, p + 3, i - 3);
262 if (i == 0 && !again) {
263 /* the currently processed BIO is NULL or at EOF */
266 #ifndef OPENSSL_NO_POSIX_IO
267 /* continue processing with the next file from directory */
268 if (dirctx != NULL) {
271 if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
276 OPENSSL_free(dirpath);
281 /* no more files in directory, continue with processing parent */
282 if ((parent = sk_BIO_pop(biosk)) == NULL) {
283 /* everything processed get out of the loop */
293 if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
299 * we removed some trailing stuff so there is a new line on the end.
302 again = 1; /* long line */
305 eline++; /* another input line */
308 /* we now have a line with trailing \r\n removed */
310 /* i is the number of bytes */
314 /* check for line continuation */
317 * If we have bytes and the last char '\\' and second last char
320 p = &(buff->data[bufnum - 1]);
321 if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
331 clear_comments(conf, buf);
332 s = eat_ws(conf, buf);
333 if (IS_EOF(conf, *s))
334 continue; /* blank line */
339 start = eat_ws(conf, s);
342 end = eat_alpha_numeric(conf, ss);
343 p = eat_ws(conf, end);
345 if (*p != '\0' && ss != p) {
349 ERR_raise(ERR_LIB_CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
353 if (!str_copy(conf, NULL, §ion, start))
355 if ((sv = _CONF_get_section(conf, section)) == NULL)
356 sv = _CONF_new_section(conf, section);
358 ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
364 end = eat_alpha_numeric(conf, s);
365 if ((end[0] == ':') && (end[1] == ':')) {
370 end = eat_alpha_numeric(conf, end);
374 p = eat_ws(conf, end);
375 if (strncmp(pname, ".pragma", 7) == 0
376 && (p != pname + 7 || *p == '=')) {
385 /* Pragma values take the form keyword:value */
386 pval = strchr(p, ':');
387 if (pval == NULL || pval == p || pval[1] == '\0') {
388 ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
394 pval = eat_ws(conf, pval);
399 * dollarid takes "on", "true or "off", "false"
401 if (strcmp(p, "dollarid") == 0) {
402 if (strcmp(pval, "on") == 0
403 || strcmp(pval, "true") == 0) {
404 conf->flag_dollarid = 1;
405 } else if (strcmp(pval, "off") == 0
406 || strcmp(pval, "false") == 0) {
407 conf->flag_dollarid = 0;
409 ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
414 * We *ignore* any unknown pragma.
417 } else if (strncmp(pname, ".include", 8) == 0
418 && (p != pname + 8 || *p == '=')) {
419 char *include = NULL;
421 const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE");
422 char *include_path = NULL;
429 if (!str_copy(conf, psection, &include, p))
432 if (include_dir != NULL && !ossl_is_absolute_path(include)) {
433 size_t newlen = strlen(include_dir) + strlen(include) + 2;
435 include_path = OPENSSL_malloc(newlen);
436 if (include_path == NULL) {
437 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
438 OPENSSL_free(include);
442 OPENSSL_strlcpy(include_path, include_dir, newlen);
443 if (!ossl_ends_with_dirsep(include_path))
444 OPENSSL_strlcat(include_path, "/", newlen);
445 OPENSSL_strlcat(include_path, include, newlen);
446 OPENSSL_free(include);
448 include_path = include;
451 /* get the BIO of the included file */
452 #ifndef OPENSSL_NO_POSIX_IO
453 next = process_include(include_path, &dirctx, &dirpath);
454 if (include_path != dirpath) {
455 /* dirpath will contain include in case of a directory */
456 OPENSSL_free(include_path);
459 next = BIO_new_file(include_path, "r");
460 OPENSSL_free(include_path);
464 /* push the currently processing BIO onto stack */
466 if ((biosk = sk_BIO_new_null()) == NULL) {
467 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
472 if (!sk_BIO_push(biosk, in)) {
473 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
477 /* continue with reading from the included BIO */
481 } else if (*p != '=') {
482 ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN,
488 start = eat_ws(conf, p);
489 trim_ws(conf, start);
491 if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
492 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
495 v->name = OPENSSL_strdup(pname);
497 if (v->name == NULL) {
498 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
501 if (!str_copy(conf, psection, &(v->value), start))
504 if (strcmp(psection, section) != 0) {
505 if ((tv = _CONF_get_section(conf, psection))
507 tv = _CONF_new_section(conf, psection);
509 ERR_raise(ERR_LIB_CONF,
510 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
515 if (_CONF_add_string(conf, tv, v) == 0) {
516 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
523 OPENSSL_free(section);
525 * No need to pop, since we only get here if the stack is empty.
526 * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE!
532 OPENSSL_free(section);
534 * Since |in| is the first element of the stack and should NOT be freed
535 * here, we cannot use sk_BIO_pop_free(). Instead, we pop and free one
536 * BIO at a time, making sure that the last one popped isn't.
538 while (sk_BIO_num(biosk) > 0) {
539 BIO *popped = sk_BIO_pop(biosk);
544 #ifndef OPENSSL_NO_POSIX_IO
545 OPENSSL_free(dirpath);
547 OPENSSL_DIR_end(&dirctx);
551 BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
552 ERR_add_error_data(2, "line ", btmp);
553 if (h != conf->data) {
554 CONF_free(conf->data);
558 OPENSSL_free(v->name);
559 OPENSSL_free(v->value);
565 static void clear_comments(CONF *conf, char *p)
568 if (IS_FCOMMENT(conf, *p)) {
572 if (!IS_WS(conf, *p)) {
579 if (IS_COMMENT(conf, *p)) {
583 if (IS_DQUOTE(conf, *p)) {
584 p = scan_dquote(conf, p);
587 if (IS_QUOTE(conf, *p)) {
588 p = scan_quote(conf, p);
591 if (IS_ESC(conf, *p)) {
592 p = scan_esc(conf, p);
595 if (IS_EOF(conf, *p))
602 static int str_copy(CONF *conf, char *section, char **pto, char *from)
604 int q, r, rr = 0, to = 0, len = 0;
605 char *s, *e, *rp, *p, *rrp, *np, *cp, v;
608 if ((buf = BUF_MEM_new()) == NULL)
611 len = strlen(from) + 1;
612 if (!BUF_MEM_grow(buf, len))
616 if (IS_QUOTE(conf, *from)) {
619 while (!IS_EOF(conf, *from) && (*from != q)) {
620 if (IS_ESC(conf, *from)) {
622 if (IS_EOF(conf, *from))
625 buf->data[to++] = *(from++);
629 } else if (IS_DQUOTE(conf, *from)) {
632 while (!IS_EOF(conf, *from)) {
634 if (*(from + 1) == q) {
640 buf->data[to++] = *(from++);
644 } else if (IS_ESC(conf, *from)) {
658 } else if (IS_EOF(conf, *from))
660 else if (*from == '$'
661 && (!conf->flag_dollarid
663 || from[1] == '(')) {
666 /* try to expand it */
680 while (IS_ALNUM(conf, *e)
681 || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
683 if ((e[0] == ':') && (e[1] == ':')) {
690 while (IS_ALNUM(conf, *e)
691 || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
699 ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE);
705 * So at this point we have
706 * np which is the start of the name string which is
708 * cp which is the start of the section string which is
710 * e is the 'next point after'.
711 * r and rr are the chars replaced by the '\0'
712 * rp and rrp is where 'r' and 'rr' came from.
714 p = _CONF_get_string(conf, cp, np);
719 ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_HAS_NO_VALUE);
722 newsize = strlen(p) + buf->length - (e - from);
723 if (newsize > MAX_CONF_VALUE_LENGTH) {
724 ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
727 if (!BUF_MEM_grow_clean(buf, newsize)) {
728 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
732 buf->data[to++] = *(p++);
735 * Since we change the pointer 'from', we also have to change the
736 * perceived length of the string it points at. /RL
742 * In case there were no braces or parenthesis around the
743 * variable reference, we have to put back the character that was
744 * replaced with a '\0'. /RL
748 buf->data[to++] = *(from++);
750 buf->data[to] = '\0';
760 #ifndef OPENSSL_NO_POSIX_IO
762 * Check whether included path is a directory.
763 * Returns next BIO to process and in case of a directory
764 * also an opened directory context and the include path.
766 static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
772 if (stat(include, &st) < 0) {
773 ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include);
774 /* missing include file is not fatal error */
778 if (S_ISDIR(st.st_mode)) {
779 if (*dirctx != NULL) {
780 ERR_raise_data(ERR_LIB_CONF, CONF_R_RECURSIVE_DIRECTORY_INCLUDE,
784 /* a directory, load its contents */
785 if ((next = get_next_file(include, dirctx)) != NULL)
790 next = BIO_new_file(include, "r");
795 * Get next file from the directory path.
796 * Returns BIO of the next file to read and updates dirctx.
798 static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx)
800 const char *filename;
803 pathlen = strlen(path);
804 while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
807 namelen = strlen(filename);
810 if ((namelen > 5 && strcasecmp(filename + namelen - 5, ".conf") == 0)
811 || (namelen > 4 && strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
816 newlen = pathlen + namelen + 2;
817 newpath = OPENSSL_zalloc(newlen);
818 if (newpath == NULL) {
819 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
822 #ifdef OPENSSL_SYS_VMS
824 * If the given path isn't clear VMS syntax,
825 * we treat it as on Unix.
827 if (path[pathlen - 1] == ']'
828 || path[pathlen - 1] == '>'
829 || path[pathlen - 1] == ':') {
830 /* Clear VMS directory syntax, just copy as is */
831 OPENSSL_strlcpy(newpath, path, newlen);
834 if (newpath[0] == '\0') {
835 OPENSSL_strlcpy(newpath, path, newlen);
836 OPENSSL_strlcat(newpath, "/", newlen);
838 OPENSSL_strlcat(newpath, filename, newlen);
840 bio = BIO_new_file(newpath, "r");
841 OPENSSL_free(newpath);
842 /* Errors when opening files are non-fatal. */
847 OPENSSL_DIR_end(dirctx);
853 static int is_keytype(const CONF *conf, char c, unsigned short type)
855 const unsigned short * keytypes = (const unsigned short *) conf->meth_data;
856 unsigned char key = (unsigned char)c;
858 #ifdef CHARSET_EBCDIC
861 /* key is out of range for os_toascii table */
865 /* convert key from ebcdic to ascii */
866 key = os_toascii[key];
870 /* key is not a seven bit ascii character */
874 return (keytypes[key] & type) ? 1 : 0;
877 static char *eat_ws(CONF *conf, char *p)
879 while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
884 static void trim_ws(CONF *conf, char *start)
888 while (!IS_EOF(conf, *p))
891 while ((p >= start) && IS_WS(conf, *p))
897 static char *eat_alpha_numeric(CONF *conf, char *p)
900 if (IS_ESC(conf, *p)) {
901 p = scan_esc(conf, p);
904 if (!(IS_ALNUM_PUNCT(conf, *p)
905 || (conf->flag_dollarid && IS_DOLLAR(conf, *p))))
911 static char *scan_quote(CONF *conf, char *p)
916 while (!(IS_EOF(conf, *p)) && (*p != q)) {
917 if (IS_ESC(conf, *p)) {
919 if (IS_EOF(conf, *p))
929 static char *scan_dquote(CONF *conf, char *p)
934 while (!(IS_EOF(conf, *p))) {
949 static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
952 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
954 BIO_printf(out, "[[%s]]\n", a->section);
957 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
959 static int def_dump(const CONF *conf, BIO *out)
961 lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
965 static int def_is_number(const CONF *conf, char c)
967 return IS_NUMBER(conf, c);
970 static int def_to_int(const CONF *conf, char c)