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;
198 char btmp[DECIMAL_SIZE(eline) + 1];
199 CONF_VALUE *v = NULL, *tv;
200 CONF_VALUE *sv = NULL;
201 char *section = NULL, *buf;
202 char *start, *psection, *pname;
203 void *h = (void *)(conf->data);
204 STACK_OF(BIO) *biosk = NULL;
205 #ifndef OPENSSL_NO_POSIX_IO
206 char *dirpath = NULL;
207 OPENSSL_DIR_CTX *dirctx = NULL;
210 if ((buff = BUF_MEM_new()) == NULL) {
211 ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
215 section = OPENSSL_strdup("default");
216 if (section == NULL) {
217 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
221 if (_CONF_new_data(conf) == 0) {
222 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
226 sv = _CONF_new_section(conf, section);
228 ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
235 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
236 ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
239 p = &(buff->data[bufnum]);
242 if (in != NULL && BIO_gets(in, p, CONFBUFSIZE - 1) < 0)
244 p[CONFBUFSIZE - 1] = '\0';
246 if (i == 0 && !again) {
247 /* the currently processed BIO is NULL or at EOF */
250 #ifndef OPENSSL_NO_POSIX_IO
251 /* continue processing with the next file from directory */
252 if (dirctx != NULL) {
255 if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
260 OPENSSL_free(dirpath);
265 /* no more files in directory, continue with processing parent */
266 if ((parent = sk_BIO_pop(biosk)) == NULL) {
267 /* everything processed get out of the loop */
277 if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
283 * we removed some trailing stuff so there is a new line on the end.
286 again = 1; /* long line */
289 eline++; /* another input line */
292 /* we now have a line with trailing \r\n removed */
294 /* i is the number of bytes */
298 /* check for line continuation */
301 * If we have bytes and the last char '\\' and second last char
304 p = &(buff->data[bufnum - 1]);
305 if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
315 clear_comments(conf, buf);
316 s = eat_ws(conf, buf);
317 if (IS_EOF(conf, *s))
318 continue; /* blank line */
323 start = eat_ws(conf, s);
326 end = eat_alpha_numeric(conf, ss);
327 p = eat_ws(conf, end);
329 if (*p != '\0' && ss != p) {
333 ERR_raise(ERR_LIB_CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
337 if (!str_copy(conf, NULL, §ion, start))
339 if ((sv = _CONF_get_section(conf, section)) == NULL)
340 sv = _CONF_new_section(conf, section);
342 ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
348 end = eat_alpha_numeric(conf, s);
349 if ((end[0] == ':') && (end[1] == ':')) {
354 end = eat_alpha_numeric(conf, end);
358 p = eat_ws(conf, end);
359 if (strncmp(pname, ".pragma", 7) == 0
360 && (p != pname + 7 || *p == '=')) {
369 /* Pragma values take the form keyword:value */
370 pval = strchr(p, ':');
371 if (pval == NULL || pval == p || pval[1] == '\0') {
372 ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
378 pval = eat_ws(conf, pval);
383 * dollarid takes "on", "true or "off", "false"
385 if (strcmp(p, "dollarid") == 0) {
386 if (strcmp(pval, "on") == 0
387 || strcmp(pval, "true") == 0) {
388 conf->flag_dollarid = 1;
389 } else if (strcmp(pval, "off") == 0
390 || strcmp(pval, "false") == 0) {
391 conf->flag_dollarid = 0;
393 ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA);
398 * We *ignore* any unknown pragma.
401 } else if (strncmp(pname, ".include", 8) == 0
402 && (p != pname + 8 || *p == '=')) {
403 char *include = NULL;
405 const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE");
406 char *include_path = NULL;
413 if (!str_copy(conf, psection, &include, p))
416 if (include_dir != NULL && !ossl_is_absolute_path(include)) {
417 size_t newlen = strlen(include_dir) + strlen(include) + 2;
419 include_path = OPENSSL_malloc(newlen);
420 if (include_path == NULL) {
421 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
422 OPENSSL_free(include);
426 OPENSSL_strlcpy(include_path, include_dir, newlen);
427 if (!ossl_ends_with_dirsep(include_path))
428 OPENSSL_strlcat(include_path, "/", newlen);
429 OPENSSL_strlcat(include_path, include, newlen);
430 OPENSSL_free(include);
432 include_path = include;
435 /* get the BIO of the included file */
436 #ifndef OPENSSL_NO_POSIX_IO
437 next = process_include(include_path, &dirctx, &dirpath);
438 if (include_path != dirpath) {
439 /* dirpath will contain include in case of a directory */
440 OPENSSL_free(include_path);
443 next = BIO_new_file(include_path, "r");
444 OPENSSL_free(include_path);
448 /* push the currently processing BIO onto stack */
450 if ((biosk = sk_BIO_new_null()) == NULL) {
451 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
456 if (!sk_BIO_push(biosk, in)) {
457 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
461 /* continue with reading from the included BIO */
465 } else if (*p != '=') {
466 ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN,
472 start = eat_ws(conf, p);
473 trim_ws(conf, start);
475 if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
476 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
479 v->name = OPENSSL_strdup(pname);
481 if (v->name == NULL) {
482 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
485 if (!str_copy(conf, psection, &(v->value), start))
488 if (strcmp(psection, section) != 0) {
489 if ((tv = _CONF_get_section(conf, psection))
491 tv = _CONF_new_section(conf, psection);
493 ERR_raise(ERR_LIB_CONF,
494 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
499 if (_CONF_add_string(conf, tv, v) == 0) {
500 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
507 OPENSSL_free(section);
509 * No need to pop, since we only get here if the stack is empty.
510 * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE!
516 OPENSSL_free(section);
518 * Since |in| is the first element of the stack and should NOT be freed
519 * here, we cannot use sk_BIO_pop_free(). Instead, we pop and free one
520 * BIO at a time, making sure that the last one popped isn't.
522 while (sk_BIO_num(biosk) > 0) {
523 BIO *popped = sk_BIO_pop(biosk);
528 #ifndef OPENSSL_NO_POSIX_IO
529 OPENSSL_free(dirpath);
531 OPENSSL_DIR_end(&dirctx);
535 BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
536 ERR_add_error_data(2, "line ", btmp);
537 if (h != conf->data) {
538 CONF_free(conf->data);
542 OPENSSL_free(v->name);
543 OPENSSL_free(v->value);
549 static void clear_comments(CONF *conf, char *p)
552 if (IS_FCOMMENT(conf, *p)) {
556 if (!IS_WS(conf, *p)) {
563 if (IS_COMMENT(conf, *p)) {
567 if (IS_DQUOTE(conf, *p)) {
568 p = scan_dquote(conf, p);
571 if (IS_QUOTE(conf, *p)) {
572 p = scan_quote(conf, p);
575 if (IS_ESC(conf, *p)) {
576 p = scan_esc(conf, p);
579 if (IS_EOF(conf, *p))
586 static int str_copy(CONF *conf, char *section, char **pto, char *from)
588 int q, r, rr = 0, to = 0, len = 0;
589 char *s, *e, *rp, *p, *rrp, *np, *cp, v;
592 if ((buf = BUF_MEM_new()) == NULL)
595 len = strlen(from) + 1;
596 if (!BUF_MEM_grow(buf, len))
600 if (IS_QUOTE(conf, *from)) {
603 while (!IS_EOF(conf, *from) && (*from != q)) {
604 if (IS_ESC(conf, *from)) {
606 if (IS_EOF(conf, *from))
609 buf->data[to++] = *(from++);
613 } else if (IS_DQUOTE(conf, *from)) {
616 while (!IS_EOF(conf, *from)) {
618 if (*(from + 1) == q) {
624 buf->data[to++] = *(from++);
628 } else if (IS_ESC(conf, *from)) {
642 } else if (IS_EOF(conf, *from))
644 else if (*from == '$'
645 && (!conf->flag_dollarid
647 || from[1] == '(')) {
650 /* try to expand it */
664 while (IS_ALNUM(conf, *e)
665 || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
667 if ((e[0] == ':') && (e[1] == ':')) {
674 while (IS_ALNUM(conf, *e)
675 || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
683 ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE);
689 * So at this point we have
690 * np which is the start of the name string which is
692 * cp which is the start of the section string which is
694 * e is the 'next point after'.
695 * r and rr are the chars replaced by the '\0'
696 * rp and rrp is where 'r' and 'rr' came from.
698 p = _CONF_get_string(conf, cp, np);
703 ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_HAS_NO_VALUE);
706 newsize = strlen(p) + buf->length - (e - from);
707 if (newsize > MAX_CONF_VALUE_LENGTH) {
708 ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
711 if (!BUF_MEM_grow_clean(buf, newsize)) {
712 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
716 buf->data[to++] = *(p++);
719 * Since we change the pointer 'from', we also have to change the
720 * perceived length of the string it points at. /RL
726 * In case there were no braces or parenthesis around the
727 * variable reference, we have to put back the character that was
728 * replaced with a '\0'. /RL
732 buf->data[to++] = *(from++);
734 buf->data[to] = '\0';
744 #ifndef OPENSSL_NO_POSIX_IO
746 * Check whether included path is a directory.
747 * Returns next BIO to process and in case of a directory
748 * also an opened directory context and the include path.
750 static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
756 if (stat(include, &st) < 0) {
757 ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include);
758 /* missing include file is not fatal error */
762 if (S_ISDIR(st.st_mode)) {
763 if (*dirctx != NULL) {
764 ERR_raise_data(ERR_LIB_CONF, CONF_R_RECURSIVE_DIRECTORY_INCLUDE,
768 /* a directory, load its contents */
769 if ((next = get_next_file(include, dirctx)) != NULL)
774 next = BIO_new_file(include, "r");
779 * Get next file from the directory path.
780 * Returns BIO of the next file to read and updates dirctx.
782 static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx)
784 const char *filename;
787 pathlen = strlen(path);
788 while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
791 namelen = strlen(filename);
794 if ((namelen > 5 && strcasecmp(filename + namelen - 5, ".conf") == 0)
795 || (namelen > 4 && strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
800 newlen = pathlen + namelen + 2;
801 newpath = OPENSSL_zalloc(newlen);
802 if (newpath == NULL) {
803 ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
806 #ifdef OPENSSL_SYS_VMS
808 * If the given path isn't clear VMS syntax,
809 * we treat it as on Unix.
811 if (path[pathlen - 1] == ']'
812 || path[pathlen - 1] == '>'
813 || path[pathlen - 1] == ':') {
814 /* Clear VMS directory syntax, just copy as is */
815 OPENSSL_strlcpy(newpath, path, newlen);
818 if (newpath[0] == '\0') {
819 OPENSSL_strlcpy(newpath, path, newlen);
820 OPENSSL_strlcat(newpath, "/", newlen);
822 OPENSSL_strlcat(newpath, filename, newlen);
824 bio = BIO_new_file(newpath, "r");
825 OPENSSL_free(newpath);
826 /* Errors when opening files are non-fatal. */
831 OPENSSL_DIR_end(dirctx);
837 static int is_keytype(const CONF *conf, char c, unsigned short type)
839 const unsigned short * keytypes = (const unsigned short *) conf->meth_data;
840 unsigned char key = (unsigned char)c;
842 #ifdef CHARSET_EBCDIC
845 /* key is out of range for os_toascii table */
849 /* convert key from ebcdic to ascii */
850 key = os_toascii[key];
854 /* key is not a seven bit ascii character */
858 return (keytypes[key] & type) ? 1 : 0;
861 static char *eat_ws(CONF *conf, char *p)
863 while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
868 static void trim_ws(CONF *conf, char *start)
872 while (!IS_EOF(conf, *p))
875 while ((p >= start) && IS_WS(conf, *p))
881 static char *eat_alpha_numeric(CONF *conf, char *p)
884 if (IS_ESC(conf, *p)) {
885 p = scan_esc(conf, p);
888 if (!(IS_ALNUM_PUNCT(conf, *p)
889 || (conf->flag_dollarid && IS_DOLLAR(conf, *p))))
895 static char *scan_quote(CONF *conf, char *p)
900 while (!(IS_EOF(conf, *p)) && (*p != q)) {
901 if (IS_ESC(conf, *p)) {
903 if (IS_EOF(conf, *p))
913 static char *scan_dquote(CONF *conf, char *p)
918 while (!(IS_EOF(conf, *p))) {
933 static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
936 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
938 BIO_printf(out, "[[%s]]\n", a->section);
941 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
943 static int def_dump(const CONF *conf, BIO *out)
945 lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
949 static int def_is_number(const CONF *conf, char c)
951 return IS_NUMBER(conf, c);
954 static int def_to_int(const CONF *conf, char c)