2 * Copyright 1995-2018 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 "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 #ifndef OPENSSL_NO_DEPRECATED_3_0
58 static int def_init_WIN32(CONF *conf);
60 static int def_destroy(CONF *conf);
61 static int def_destroy_data(CONF *conf);
62 static int def_load(CONF *conf, const char *name, long *eline);
63 static int def_load_bio(CONF *conf, BIO *bp, long *eline);
64 static int def_dump(const CONF *conf, BIO *bp);
65 static int def_is_number(const CONF *conf, char c);
66 static int def_to_int(const CONF *conf, char c);
68 static CONF_METHOD default_method = {
81 CONF_METHOD *NCONF_default(void)
83 return &default_method;
86 #ifndef OPENSSL_NO_DEPRECATED_3_0
87 static CONF_METHOD WIN32_method = {
100 CONF_METHOD *NCONF_WIN32(void)
102 return &WIN32_method;
106 static CONF *def_create(CONF_METHOD *meth)
110 ret = OPENSSL_malloc(sizeof(*ret));
112 if (meth->init(ret) == 0) {
119 static int def_init_default(CONF *conf)
124 conf->meth = &default_method;
125 conf->meth_data = (void *)CONF_type_default;
131 #ifndef OPENSSL_NO_DEPRECATED_3_0
132 static int def_init_WIN32(CONF *conf)
137 conf->meth = &WIN32_method;
138 conf->meth_data = (void *)CONF_type_win32;
145 static int def_destroy(CONF *conf)
147 if (def_destroy_data(conf)) {
154 static int def_destroy_data(CONF *conf)
158 _CONF_free_data(conf);
162 static int def_load(CONF *conf, const char *name, long *line)
167 #ifdef OPENSSL_SYS_VMS
168 in = BIO_new_file(name, "r");
170 in = BIO_new_file(name, "rb");
173 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
174 CONFerr(CONF_F_DEF_LOAD, CONF_R_NO_SUCH_FILE);
176 CONFerr(CONF_F_DEF_LOAD, ERR_R_SYS_LIB);
180 ret = def_load_bio(conf, in, line);
186 static int def_load_bio(CONF *conf, BIO *in, long *line)
188 /* The macro BUFSIZE conflicts with a system macro in VxWorks */
189 #define CONFBUFSIZE 512
190 int bufnum = 0, i, ii;
191 BUF_MEM *buff = NULL;
195 char btmp[DECIMAL_SIZE(eline) + 1];
196 CONF_VALUE *v = NULL, *tv;
197 CONF_VALUE *sv = NULL;
198 char *section = NULL, *buf;
199 char *start, *psection, *pname;
200 void *h = (void *)(conf->data);
201 STACK_OF(BIO) *biosk = NULL;
202 #ifndef OPENSSL_NO_POSIX_IO
203 char *dirpath = NULL;
204 OPENSSL_DIR_CTX *dirctx = NULL;
207 if ((buff = BUF_MEM_new()) == NULL) {
208 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
212 section = OPENSSL_strdup("default");
213 if (section == NULL) {
214 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
218 if (_CONF_new_data(conf) == 0) {
219 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
223 sv = _CONF_new_section(conf, section);
225 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
232 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
233 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
236 p = &(buff->data[bufnum]);
239 BIO_gets(in, p, CONFBUFSIZE - 1);
240 p[CONFBUFSIZE - 1] = '\0';
242 if (i == 0 && !again) {
243 /* the currently processed BIO is at EOF */
246 #ifndef OPENSSL_NO_POSIX_IO
247 /* continue processing with the next file from directory */
248 if (dirctx != NULL) {
251 if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
256 OPENSSL_free(dirpath);
261 /* no more files in directory, continue with processing parent */
262 if ((parent = sk_BIO_pop(biosk)) == NULL) {
263 /* everything processed get out of the loop */
273 if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
279 * we removed some trailing stuff so there is a new line on the end.
282 again = 1; /* long line */
285 eline++; /* another input line */
288 /* we now have a line with trailing \r\n removed */
290 /* i is the number of bytes */
294 /* check for line continuation */
297 * If we have bytes and the last char '\\' and second last char
300 p = &(buff->data[bufnum - 1]);
301 if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
311 clear_comments(conf, buf);
312 s = eat_ws(conf, buf);
313 if (IS_EOF(conf, *s))
314 continue; /* blank line */
319 start = eat_ws(conf, s);
322 end = eat_alpha_numeric(conf, ss);
323 p = eat_ws(conf, end);
325 if (*p != '\0' && ss != p) {
329 CONFerr(CONF_F_DEF_LOAD_BIO,
330 CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
334 if (!str_copy(conf, NULL, §ion, start))
336 if ((sv = _CONF_get_section(conf, section)) == NULL)
337 sv = _CONF_new_section(conf, section);
339 CONFerr(CONF_F_DEF_LOAD_BIO,
340 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
346 end = eat_alpha_numeric(conf, s);
347 if ((end[0] == ':') && (end[1] == ':')) {
352 end = eat_alpha_numeric(conf, end);
356 p = eat_ws(conf, end);
357 if (strncmp(pname, ".pragma", 7) == 0
358 && (p != pname + 7 || *p == '=')) {
367 /* Pragma values take the form keyword:value */
368 pval = strchr(p, ':');
369 if (pval == NULL || pval == p || pval[1] == '\0') {
370 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_INVALID_PRAGMA);
376 pval = eat_ws(conf, pval);
381 * dollarid takes "on", "true or "off", "false"
383 if (strcmp(p, "dollarid") == 0) {
384 if (strcmp(pval, "on") == 0
385 || strcmp(pval, "true") == 0) {
386 conf->flag_dollarid = 1;
387 } else if (strcmp(pval, "off") == 0
388 || strcmp(pval, "false") == 0) {
389 conf->flag_dollarid = 0;
391 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_INVALID_PRAGMA);
396 * We *ignore* any unknown pragma.
399 } else if (strncmp(pname, ".include", 8) == 0
400 && (p != pname + 8 || *p == '=')) {
401 char *include = NULL;
403 const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE");
404 char *include_path = NULL;
411 if (!str_copy(conf, psection, &include, p))
414 if (include_dir != NULL) {
415 size_t newlen = strlen(include_dir) + strlen(include) + 2;
417 include_path = OPENSSL_malloc(newlen);
418 OPENSSL_strlcpy(include_path, include_dir, newlen);
419 OPENSSL_strlcat(include_path, "/", newlen);
420 OPENSSL_strlcat(include_path, include, newlen);
422 include_path = include;
425 /* get the BIO of the included file */
426 #ifndef OPENSSL_NO_POSIX_IO
427 next = process_include(include_path, &dirctx, &dirpath);
428 if (include_path != dirpath) {
429 /* dirpath will contain include in case of a directory */
430 OPENSSL_free(include);
431 if (include_path != include)
432 OPENSSL_free(include_path);
435 next = BIO_new_file(include_path, "r");
436 OPENSSL_free(include);
437 if (include_path != include)
438 OPENSSL_free(include_path);
442 /* push the currently processing BIO onto stack */
444 if ((biosk = sk_BIO_new_null()) == NULL) {
445 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
449 if (!sk_BIO_push(biosk, in)) {
450 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
453 /* continue with reading from the included BIO */
457 } else if (*p != '=') {
458 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);
459 ERR_add_error_data(2, "HERE-->", p);
464 start = eat_ws(conf, p);
465 trim_ws(conf, start);
467 if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
468 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
471 v->name = OPENSSL_strdup(pname);
473 if (v->name == NULL) {
474 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
477 if (!str_copy(conf, psection, &(v->value), start))
480 if (strcmp(psection, section) != 0) {
481 if ((tv = _CONF_get_section(conf, psection))
483 tv = _CONF_new_section(conf, psection);
485 CONFerr(CONF_F_DEF_LOAD_BIO,
486 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
491 if (_CONF_add_string(conf, tv, v) == 0) {
492 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
499 OPENSSL_free(section);
501 * No need to pop, since we only get here if the stack is empty.
502 * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE!
508 OPENSSL_free(section);
510 * Since |in| is the first element of the stack and should NOT be freed
511 * here, we cannot use sk_BIO_pop_free(). Instead, we pop and free one
512 * BIO at a time, making sure that the last one popped isn't.
514 while (sk_BIO_num(biosk) > 0) {
515 BIO *popped = sk_BIO_pop(biosk);
520 #ifndef OPENSSL_NO_POSIX_IO
521 OPENSSL_free(dirpath);
523 OPENSSL_DIR_end(&dirctx);
527 BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
528 ERR_add_error_data(2, "line ", btmp);
529 if (h != conf->data) {
530 CONF_free(conf->data);
534 OPENSSL_free(v->name);
535 OPENSSL_free(v->value);
541 static void clear_comments(CONF *conf, char *p)
544 if (IS_FCOMMENT(conf, *p)) {
548 if (!IS_WS(conf, *p)) {
555 if (IS_COMMENT(conf, *p)) {
559 if (IS_DQUOTE(conf, *p)) {
560 p = scan_dquote(conf, p);
563 if (IS_QUOTE(conf, *p)) {
564 p = scan_quote(conf, p);
567 if (IS_ESC(conf, *p)) {
568 p = scan_esc(conf, p);
571 if (IS_EOF(conf, *p))
578 static int str_copy(CONF *conf, char *section, char **pto, char *from)
580 int q, r, rr = 0, to = 0, len = 0;
581 char *s, *e, *rp, *p, *rrp, *np, *cp, v;
584 if ((buf = BUF_MEM_new()) == NULL)
587 len = strlen(from) + 1;
588 if (!BUF_MEM_grow(buf, len))
592 if (IS_QUOTE(conf, *from)) {
595 while (!IS_EOF(conf, *from) && (*from != q)) {
596 if (IS_ESC(conf, *from)) {
598 if (IS_EOF(conf, *from))
601 buf->data[to++] = *(from++);
605 } else if (IS_DQUOTE(conf, *from)) {
608 while (!IS_EOF(conf, *from)) {
610 if (*(from + 1) == q) {
616 buf->data[to++] = *(from++);
620 } else if (IS_ESC(conf, *from)) {
634 } else if (IS_EOF(conf, *from))
636 else if (*from == '$'
637 && (!conf->flag_dollarid
639 || from[1] == '(')) {
642 /* try to expand it */
656 while (IS_ALNUM(conf, *e)
657 || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
659 if ((e[0] == ':') && (e[1] == ':')) {
666 while (IS_ALNUM(conf, *e)
667 || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
675 CONFerr(CONF_F_STR_COPY, CONF_R_NO_CLOSE_BRACE);
681 * So at this point we have
682 * np which is the start of the name string which is
684 * cp which is the start of the section string which is
686 * e is the 'next point after'.
687 * r and rr are the chars replaced by the '\0'
688 * rp and rrp is where 'r' and 'rr' came from.
690 p = _CONF_get_string(conf, cp, np);
695 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_HAS_NO_VALUE);
698 newsize = strlen(p) + buf->length - (e - from);
699 if (newsize > MAX_CONF_VALUE_LENGTH) {
700 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
703 if (!BUF_MEM_grow_clean(buf, newsize)) {
704 CONFerr(CONF_F_STR_COPY, ERR_R_MALLOC_FAILURE);
708 buf->data[to++] = *(p++);
711 * Since we change the pointer 'from', we also have to change the
712 * perceived length of the string it points at. /RL
718 * In case there were no braces or parenthesis around the
719 * variable reference, we have to put back the character that was
720 * replaced with a '\0'. /RL
724 buf->data[to++] = *(from++);
726 buf->data[to] = '\0';
736 #ifndef OPENSSL_NO_POSIX_IO
738 * Check whether included path is a directory.
739 * Returns next BIO to process and in case of a directory
740 * also an opened directory context and the include path.
742 static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
748 if (stat(include, &st) < 0) {
749 ERR_raise_data(ERR_LIB_SYS, errno,
752 /* missing include file is not fatal error */
756 if (S_ISDIR(st.st_mode)) {
757 if (*dirctx != NULL) {
758 CONFerr(CONF_F_PROCESS_INCLUDE,
759 CONF_R_RECURSIVE_DIRECTORY_INCLUDE);
760 ERR_add_error_data(1, include);
763 /* a directory, load its contents */
764 if ((next = get_next_file(include, dirctx)) != NULL)
769 next = BIO_new_file(include, "r");
774 * Get next file from the directory path.
775 * Returns BIO of the next file to read and updates dirctx.
777 static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx)
779 const char *filename;
782 pathlen = strlen(path);
783 while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
786 namelen = strlen(filename);
789 if ((namelen > 5 && strcasecmp(filename + namelen - 5, ".conf") == 0)
790 || (namelen > 4 && strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
795 newlen = pathlen + namelen + 2;
796 newpath = OPENSSL_zalloc(newlen);
797 if (newpath == NULL) {
798 CONFerr(CONF_F_GET_NEXT_FILE, ERR_R_MALLOC_FAILURE);
801 #ifdef OPENSSL_SYS_VMS
803 * If the given path isn't clear VMS syntax,
804 * we treat it as on Unix.
806 if (path[pathlen - 1] == ']'
807 || path[pathlen - 1] == '>'
808 || path[pathlen - 1] == ':') {
809 /* Clear VMS directory syntax, just copy as is */
810 OPENSSL_strlcpy(newpath, path, newlen);
813 if (newpath[0] == '\0') {
814 OPENSSL_strlcpy(newpath, path, newlen);
815 OPENSSL_strlcat(newpath, "/", newlen);
817 OPENSSL_strlcat(newpath, filename, newlen);
819 bio = BIO_new_file(newpath, "r");
820 OPENSSL_free(newpath);
821 /* Errors when opening files are non-fatal. */
826 OPENSSL_DIR_end(dirctx);
832 static int is_keytype(const CONF *conf, char c, unsigned short type)
834 const unsigned short * keytypes = (const unsigned short *) conf->meth_data;
835 unsigned char key = (unsigned char)c;
837 #ifdef CHARSET_EBCDIC
840 /* key is out of range for os_toascii table */
844 /* convert key from ebcdic to ascii */
845 key = os_toascii[key];
849 /* key is not a seven bit ascii character */
853 return (keytypes[key] & type) ? 1 : 0;
856 static char *eat_ws(CONF *conf, char *p)
858 while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
863 static void trim_ws(CONF *conf, char *start)
867 while (!IS_EOF(conf, *p))
870 while ((p >= start) && IS_WS(conf, *p))
876 static char *eat_alpha_numeric(CONF *conf, char *p)
879 if (IS_ESC(conf, *p)) {
880 p = scan_esc(conf, p);
883 if (!(IS_ALNUM_PUNCT(conf, *p)
884 || (conf->flag_dollarid && IS_DOLLAR(conf, *p))))
890 static char *scan_quote(CONF *conf, char *p)
895 while (!(IS_EOF(conf, *p)) && (*p != q)) {
896 if (IS_ESC(conf, *p)) {
898 if (IS_EOF(conf, *p))
908 static char *scan_dquote(CONF *conf, char *p)
913 while (!(IS_EOF(conf, *p))) {
928 static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
931 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
933 BIO_printf(out, "[[%s]]\n", a->section);
936 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
938 static int def_dump(const CONF *conf, BIO *out)
940 lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
944 static int def_is_number(const CONF *conf, char c)
946 return IS_NUMBER(conf, c);
949 static int def_to_int(const CONF *conf, char c)