2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 "internal/cryptlib.h"
15 #include "internal/o_dir.h"
16 #include <openssl/lhash.h>
17 #include <openssl/conf.h>
18 #include <openssl/conf_api.h>
20 #include <openssl/buffer.h>
21 #include <openssl/err.h>
22 #ifndef OPENSSL_NO_POSIX_IO
23 # include <sys/stat.h>
26 # define strcasecmp _stricmp
31 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
35 * The maximum length we can grow a value to after variable expansion. 64k
36 * should be more than enough for all reasonable uses.
38 #define MAX_CONF_VALUE_LENGTH 65536
40 static int is_keytype(const CONF *conf, char c, unsigned short type);
41 static char *eat_ws(CONF *conf, char *p);
42 static void trim_ws(CONF *conf, char *start);
43 static char *eat_alpha_numeric(CONF *conf, char *p);
44 static void clear_comments(CONF *conf, char *p);
45 static int str_copy(CONF *conf, char *section, char **to, char *from);
46 static char *scan_quote(CONF *conf, char *p);
47 static char *scan_dquote(CONF *conf, char *p);
48 #define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
49 #ifndef OPENSSL_NO_POSIX_IO
50 static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
52 static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx);
55 static CONF *def_create(CONF_METHOD *meth);
56 static int def_init_default(CONF *conf);
57 static int def_init_WIN32(CONF *conf);
58 static int def_destroy(CONF *conf);
59 static int def_destroy_data(CONF *conf);
60 static int def_load(CONF *conf, const char *name, long *eline);
61 static int def_load_bio(CONF *conf, BIO *bp, long *eline);
62 static int def_dump(const CONF *conf, BIO *bp);
63 static int def_is_number(const CONF *conf, char c);
64 static int def_to_int(const CONF *conf, char c);
66 static CONF_METHOD default_method = {
79 static CONF_METHOD WIN32_method = {
92 CONF_METHOD *NCONF_default(void)
94 return &default_method;
97 CONF_METHOD *NCONF_WIN32(void)
102 static CONF *def_create(CONF_METHOD *meth)
106 ret = OPENSSL_malloc(sizeof(*ret));
108 if (meth->init(ret) == 0) {
115 static int def_init_default(CONF *conf)
120 conf->meth = &default_method;
121 conf->meth_data = (void *)CONF_type_default;
127 static int def_init_WIN32(CONF *conf)
132 conf->meth = &WIN32_method;
133 conf->meth_data = (void *)CONF_type_win32;
139 static int def_destroy(CONF *conf)
141 if (def_destroy_data(conf)) {
148 static int def_destroy_data(CONF *conf)
152 _CONF_free_data(conf);
156 static int def_load(CONF *conf, const char *name, long *line)
161 #ifdef OPENSSL_SYS_VMS
162 in = BIO_new_file(name, "r");
164 in = BIO_new_file(name, "rb");
167 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
168 CONFerr(CONF_F_DEF_LOAD, CONF_R_NO_SUCH_FILE);
170 CONFerr(CONF_F_DEF_LOAD, ERR_R_SYS_LIB);
174 ret = def_load_bio(conf, in, line);
180 static int def_load_bio(CONF *conf, BIO *in, long *line)
182 /* The macro BUFSIZE conflicts with a system macro in VxWorks */
183 #define CONFBUFSIZE 512
184 int bufnum = 0, i, ii;
185 BUF_MEM *buff = NULL;
190 char btmp[DECIMAL_SIZE(eline) + 1];
191 CONF_VALUE *v = NULL, *tv;
192 CONF_VALUE *sv = NULL;
193 char *section = NULL, *buf;
194 char *start, *psection, *pname;
195 void *h = (void *)(conf->data);
196 STACK_OF(BIO) *biosk = NULL;
197 #ifndef OPENSSL_NO_POSIX_IO
198 char *dirpath = NULL;
199 OPENSSL_DIR_CTX *dirctx = NULL;
202 if ((buff = BUF_MEM_new()) == NULL) {
203 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
207 section = OPENSSL_strdup("default");
208 if (section == NULL) {
209 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
213 if (_CONF_new_data(conf) == 0) {
214 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
218 sv = _CONF_new_section(conf, section);
220 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
227 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
228 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
231 p = &(buff->data[bufnum]);
234 BIO_gets(in, p, CONFBUFSIZE - 1);
235 p[CONFBUFSIZE - 1] = '\0';
238 /* Other BOMs imply unsupported multibyte encoding,
239 * so don't strip them and let the error raise */
240 const unsigned char utf8_bom[3] = {0xEF, 0xBB, 0xBF};
242 if (i >= 3 && memcmp(p, utf8_bom, 3) == 0) {
243 memmove(p, p + 3, i - 3);
250 if (i == 0 && !again) {
251 /* the currently processed BIO is at EOF */
254 #ifndef OPENSSL_NO_POSIX_IO
255 /* continue processing with the next file from directory */
256 if (dirctx != NULL) {
259 if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
264 OPENSSL_free(dirpath);
269 /* no more files in directory, continue with processing parent */
270 if ((parent = sk_BIO_pop(biosk)) == NULL) {
271 /* everything processed get out of the loop */
281 if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
287 * we removed some trailing stuff so there is a new line on the end.
290 again = 1; /* long line */
293 eline++; /* another input line */
296 /* we now have a line with trailing \r\n removed */
298 /* i is the number of bytes */
302 /* check for line continuation */
305 * If we have bytes and the last char '\\' and second last char
308 p = &(buff->data[bufnum - 1]);
309 if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
319 clear_comments(conf, buf);
320 s = eat_ws(conf, buf);
321 if (IS_EOF(conf, *s))
322 continue; /* blank line */
327 start = eat_ws(conf, s);
330 end = eat_alpha_numeric(conf, ss);
331 p = eat_ws(conf, end);
333 if (*p != '\0' && ss != p) {
337 CONFerr(CONF_F_DEF_LOAD_BIO,
338 CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
342 if (!str_copy(conf, NULL, §ion, start))
344 if ((sv = _CONF_get_section(conf, section)) == NULL)
345 sv = _CONF_new_section(conf, section);
347 CONFerr(CONF_F_DEF_LOAD_BIO,
348 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
354 end = eat_alpha_numeric(conf, s);
355 if ((end[0] == ':') && (end[1] == ':')) {
360 end = eat_alpha_numeric(conf, end);
364 p = eat_ws(conf, end);
365 if (strncmp(pname, ".include", 8) == 0
366 && (p != pname + 8 || *p == '=')) {
367 char *include = NULL;
375 if (!str_copy(conf, psection, &include, p))
377 /* get the BIO of the included file */
378 #ifndef OPENSSL_NO_POSIX_IO
379 next = process_include(include, &dirctx, &dirpath);
380 if (include != dirpath) {
381 /* dirpath will contain include in case of a directory */
382 OPENSSL_free(include);
385 next = BIO_new_file(include, "r");
386 OPENSSL_free(include);
389 /* push the currently processing BIO onto stack */
391 if ((biosk = sk_BIO_new_null()) == NULL) {
392 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
397 if (!sk_BIO_push(biosk, in)) {
398 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
402 /* continue with reading from the included BIO */
406 } else if (*p != '=') {
407 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);
412 start = eat_ws(conf, p);
413 trim_ws(conf, start);
415 if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
416 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
419 v->name = OPENSSL_strdup(pname);
421 if (v->name == NULL) {
422 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
425 if (!str_copy(conf, psection, &(v->value), start))
428 if (strcmp(psection, section) != 0) {
429 if ((tv = _CONF_get_section(conf, psection))
431 tv = _CONF_new_section(conf, psection);
433 CONFerr(CONF_F_DEF_LOAD_BIO,
434 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
439 if (_CONF_add_string(conf, tv, v) == 0) {
440 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
447 OPENSSL_free(section);
449 * No need to pop, since we only get here if the stack is empty.
450 * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE!
456 OPENSSL_free(section);
458 * Since |in| is the first element of the stack and should NOT be freed
459 * here, we cannot use sk_BIO_pop_free(). Instead, we pop and free one
460 * BIO at a time, making sure that the last one popped isn't.
462 while (sk_BIO_num(biosk) > 0) {
463 BIO *popped = sk_BIO_pop(biosk);
468 #ifndef OPENSSL_NO_POSIX_IO
469 OPENSSL_free(dirpath);
471 OPENSSL_DIR_end(&dirctx);
475 BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
476 ERR_add_error_data(2, "line ", btmp);
477 if (h != conf->data) {
478 CONF_free(conf->data);
482 OPENSSL_free(v->name);
483 OPENSSL_free(v->value);
489 static void clear_comments(CONF *conf, char *p)
492 if (IS_FCOMMENT(conf, *p)) {
496 if (!IS_WS(conf, *p)) {
503 if (IS_COMMENT(conf, *p)) {
507 if (IS_DQUOTE(conf, *p)) {
508 p = scan_dquote(conf, p);
511 if (IS_QUOTE(conf, *p)) {
512 p = scan_quote(conf, p);
515 if (IS_ESC(conf, *p)) {
516 p = scan_esc(conf, p);
519 if (IS_EOF(conf, *p))
526 static int str_copy(CONF *conf, char *section, char **pto, char *from)
528 int q, r, rr = 0, to = 0, len = 0;
529 char *s, *e, *rp, *p, *rrp, *np, *cp, v;
532 if ((buf = BUF_MEM_new()) == NULL)
535 len = strlen(from) + 1;
536 if (!BUF_MEM_grow(buf, len))
540 if (IS_QUOTE(conf, *from)) {
543 while (!IS_EOF(conf, *from) && (*from != q)) {
544 if (IS_ESC(conf, *from)) {
546 if (IS_EOF(conf, *from))
549 buf->data[to++] = *(from++);
553 } else if (IS_DQUOTE(conf, *from)) {
556 while (!IS_EOF(conf, *from)) {
558 if (*(from + 1) == q) {
564 buf->data[to++] = *(from++);
568 } else if (IS_ESC(conf, *from)) {
582 } else if (IS_EOF(conf, *from))
584 else if (*from == '$') {
587 /* try to expand it */
601 while (IS_ALNUM(conf, *e))
603 if ((e[0] == ':') && (e[1] == ':')) {
610 while (IS_ALNUM(conf, *e))
618 CONFerr(CONF_F_STR_COPY, CONF_R_NO_CLOSE_BRACE);
624 * So at this point we have
625 * np which is the start of the name string which is
627 * cp which is the start of the section string which is
629 * e is the 'next point after'.
630 * r and rr are the chars replaced by the '\0'
631 * rp and rrp is where 'r' and 'rr' came from.
633 p = _CONF_get_string(conf, cp, np);
638 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_HAS_NO_VALUE);
641 newsize = strlen(p) + buf->length - (e - from);
642 if (newsize > MAX_CONF_VALUE_LENGTH) {
643 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
646 if (!BUF_MEM_grow_clean(buf, newsize)) {
647 CONFerr(CONF_F_STR_COPY, ERR_R_MALLOC_FAILURE);
651 buf->data[to++] = *(p++);
654 * Since we change the pointer 'from', we also have to change the
655 * perceived length of the string it points at. /RL
661 * In case there were no braces or parenthesis around the
662 * variable reference, we have to put back the character that was
663 * replaced with a '\0'. /RL
667 buf->data[to++] = *(from++);
669 buf->data[to] = '\0';
679 #ifndef OPENSSL_NO_POSIX_IO
681 * Check whether included path is a directory.
682 * Returns next BIO to process and in case of a directory
683 * also an opened directory context and the include path.
685 static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
688 struct stat st = { 0 };
691 if (stat(include, &st) < 0) {
692 SYSerr(SYS_F_STAT, errno);
693 ERR_add_error_data(1, include);
694 /* missing include file is not fatal error */
698 if (S_ISDIR(st.st_mode)) {
699 if (*dirctx != NULL) {
700 CONFerr(CONF_F_PROCESS_INCLUDE,
701 CONF_R_RECURSIVE_DIRECTORY_INCLUDE);
702 ERR_add_error_data(1, include);
705 /* a directory, load its contents */
706 if ((next = get_next_file(include, dirctx)) != NULL)
711 next = BIO_new_file(include, "r");
716 * Get next file from the directory path.
717 * Returns BIO of the next file to read and updates dirctx.
719 static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx)
721 const char *filename;
724 pathlen = strlen(path);
725 while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
728 namelen = strlen(filename);
731 if ((namelen > 5 && strcasecmp(filename + namelen - 5, ".conf") == 0)
732 || (namelen > 4 && strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
737 newlen = pathlen + namelen + 2;
738 newpath = OPENSSL_zalloc(newlen);
739 if (newpath == NULL) {
740 CONFerr(CONF_F_GET_NEXT_FILE, ERR_R_MALLOC_FAILURE);
743 #ifdef OPENSSL_SYS_VMS
745 * If the given path isn't clear VMS syntax,
746 * we treat it as on Unix.
748 if (path[pathlen - 1] == ']'
749 || path[pathlen - 1] == '>'
750 || path[pathlen - 1] == ':') {
751 /* Clear VMS directory syntax, just copy as is */
752 OPENSSL_strlcpy(newpath, path, newlen);
755 if (newpath[0] == '\0') {
756 OPENSSL_strlcpy(newpath, path, newlen);
757 OPENSSL_strlcat(newpath, "/", newlen);
759 OPENSSL_strlcat(newpath, filename, newlen);
761 bio = BIO_new_file(newpath, "r");
762 OPENSSL_free(newpath);
763 /* Errors when opening files are non-fatal. */
768 OPENSSL_DIR_end(dirctx);
774 static int is_keytype(const CONF *conf, char c, unsigned short type)
776 const unsigned short * keytypes = (const unsigned short *) conf->meth_data;
777 unsigned char key = (unsigned char)c;
779 #ifdef CHARSET_EBCDIC
782 /* key is out of range for os_toascii table */
786 /* convert key from ebcdic to ascii */
787 key = os_toascii[key];
791 /* key is not a seven bit ascii character */
795 return (keytypes[key] & type) ? 1 : 0;
798 static char *eat_ws(CONF *conf, char *p)
800 while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
805 static void trim_ws(CONF *conf, char *start)
809 while (!IS_EOF(conf, *p))
812 while ((p >= start) && IS_WS(conf, *p))
818 static char *eat_alpha_numeric(CONF *conf, char *p)
821 if (IS_ESC(conf, *p)) {
822 p = scan_esc(conf, p);
825 if (!IS_ALNUM_PUNCT(conf, *p))
831 static char *scan_quote(CONF *conf, char *p)
836 while (!(IS_EOF(conf, *p)) && (*p != q)) {
837 if (IS_ESC(conf, *p)) {
839 if (IS_EOF(conf, *p))
849 static char *scan_dquote(CONF *conf, char *p)
854 while (!(IS_EOF(conf, *p))) {
869 static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
872 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
874 BIO_printf(out, "[[%s]]\n", a->section);
877 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
879 static int def_dump(const CONF *conf, BIO *out)
881 lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
885 static int def_is_number(const CONF *conf, char c)
887 return IS_NUMBER(conf, c);
890 static int def_to_int(const CONF *conf, char c)