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 */
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
33 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
37 * The maximum length we can grow a value to after variable expansion. 64k
38 * should be more than enough for all reasonable uses.
40 #define MAX_CONF_VALUE_LENGTH 65536
42 static int is_keytype(const CONF
*conf
, char c
, unsigned short type
);
43 static char *eat_ws(CONF
*conf
, char *p
);
44 static void trim_ws(CONF
*conf
, char *start
);
45 static char *eat_alpha_numeric(CONF
*conf
, char *p
);
46 static void clear_comments(CONF
*conf
, char *p
);
47 static int str_copy(CONF
*conf
, char *section
, char **to
, char *from
);
48 static char *scan_quote(CONF
*conf
, char *p
);
49 static char *scan_dquote(CONF
*conf
, char *p
);
50 #define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
51 #ifndef OPENSSL_NO_POSIX_IO
52 static BIO
*process_include(char *include
, OPENSSL_DIR_CTX
**dirctx
,
54 static BIO
*get_next_file(const char *path
, OPENSSL_DIR_CTX
**dirctx
);
57 static CONF
*def_create(CONF_METHOD
*meth
);
58 static int def_init_default(CONF
*conf
);
59 #ifndef OPENSSL_NO_DEPRECATED_3_0
60 static int def_init_WIN32(CONF
*conf
);
62 static int def_destroy(CONF
*conf
);
63 static int def_destroy_data(CONF
*conf
);
64 static int def_load(CONF
*conf
, const char *name
, long *eline
);
65 static int def_load_bio(CONF
*conf
, BIO
*bp
, long *eline
);
66 static int def_dump(const CONF
*conf
, BIO
*bp
);
67 static int def_is_number(const CONF
*conf
, char c
);
68 static int def_to_int(const CONF
*conf
, char c
);
70 static CONF_METHOD default_method
= {
83 CONF_METHOD
*NCONF_default(void)
85 return &default_method
;
88 #ifndef OPENSSL_NO_DEPRECATED_3_0
89 static CONF_METHOD WIN32_method
= {
102 CONF_METHOD
*NCONF_WIN32(void)
104 return &WIN32_method
;
108 static CONF
*def_create(CONF_METHOD
*meth
)
112 ret
= OPENSSL_malloc(sizeof(*ret
));
114 if (meth
->init(ret
) == 0) {
121 static int def_init_default(CONF
*conf
)
126 memset(conf
, 0, sizeof(*conf
));
127 conf
->meth
= &default_method
;
128 conf
->meth_data
= (void *)CONF_type_default
;
133 #ifndef OPENSSL_NO_DEPRECATED_3_0
134 static int def_init_WIN32(CONF
*conf
)
139 memset(conf
, 0, sizeof(*conf
));
140 conf
->meth
= &WIN32_method
;
141 conf
->meth_data
= (void *)CONF_type_win32
;
147 static int def_destroy(CONF
*conf
)
149 if (def_destroy_data(conf
)) {
156 static int def_destroy_data(CONF
*conf
)
160 _CONF_free_data(conf
);
164 static int def_load(CONF
*conf
, const char *name
, long *line
)
169 #ifdef OPENSSL_SYS_VMS
170 in
= BIO_new_file(name
, "r");
172 in
= BIO_new_file(name
, "rb");
175 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE
)
176 CONFerr(CONF_F_DEF_LOAD
, CONF_R_NO_SUCH_FILE
);
178 CONFerr(CONF_F_DEF_LOAD
, ERR_R_SYS_LIB
);
182 ret
= def_load_bio(conf
, in
, line
);
188 static int def_load_bio(CONF
*conf
, BIO
*in
, long *line
)
190 /* The macro BUFSIZE conflicts with a system macro in VxWorks */
191 #define CONFBUFSIZE 512
192 int bufnum
= 0, i
, ii
;
193 BUF_MEM
*buff
= NULL
;
197 char btmp
[DECIMAL_SIZE(eline
) + 1];
198 CONF_VALUE
*v
= NULL
, *tv
;
199 CONF_VALUE
*sv
= NULL
;
200 char *section
= NULL
, *buf
;
201 char *start
, *psection
, *pname
;
202 void *h
= (void *)(conf
->data
);
203 STACK_OF(BIO
) *biosk
= NULL
;
204 #ifndef OPENSSL_NO_POSIX_IO
205 char *dirpath
= NULL
;
206 OPENSSL_DIR_CTX
*dirctx
= NULL
;
209 if ((buff
= BUF_MEM_new()) == NULL
) {
210 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_BUF_LIB
);
214 section
= OPENSSL_strdup("default");
215 if (section
== NULL
) {
216 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
220 if (_CONF_new_data(conf
) == 0) {
221 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
225 sv
= _CONF_new_section(conf
, section
);
227 CONFerr(CONF_F_DEF_LOAD_BIO
, CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
234 if (!BUF_MEM_grow(buff
, bufnum
+ CONFBUFSIZE
)) {
235 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_BUF_LIB
);
238 p
= &(buff
->data
[bufnum
]);
241 BIO_gets(in
, p
, CONFBUFSIZE
- 1);
242 p
[CONFBUFSIZE
- 1] = '\0';
244 if (i
== 0 && !again
) {
245 /* the currently processed BIO is at EOF */
248 #ifndef OPENSSL_NO_POSIX_IO
249 /* continue processing with the next file from directory */
250 if (dirctx
!= NULL
) {
253 if ((next
= get_next_file(dirpath
, &dirctx
)) != NULL
) {
258 OPENSSL_free(dirpath
);
263 /* no more files in directory, continue with processing parent */
264 if ((parent
= sk_BIO_pop(biosk
)) == NULL
) {
265 /* everything processed get out of the loop */
275 if ((p
[i
- 1] != '\r') && (p
[i
- 1] != '\n'))
281 * we removed some trailing stuff so there is a new line on the end.
284 again
= 1; /* long line */
287 eline
++; /* another input line */
290 /* we now have a line with trailing \r\n removed */
292 /* i is the number of bytes */
296 /* check for line continuation */
299 * If we have bytes and the last char '\\' and second last char
302 p
= &(buff
->data
[bufnum
- 1]);
303 if (IS_ESC(conf
, p
[0]) && ((bufnum
<= 1) || !IS_ESC(conf
, p
[-1]))) {
313 clear_comments(conf
, buf
);
314 s
= eat_ws(conf
, buf
);
315 if (IS_EOF(conf
, *s
))
316 continue; /* blank line */
321 start
= eat_ws(conf
, s
);
324 end
= eat_alpha_numeric(conf
, ss
);
325 p
= eat_ws(conf
, end
);
327 if (*p
!= '\0' && ss
!= p
) {
331 CONFerr(CONF_F_DEF_LOAD_BIO
,
332 CONF_R_MISSING_CLOSE_SQUARE_BRACKET
);
336 if (!str_copy(conf
, NULL
, §ion
, start
))
338 if ((sv
= _CONF_get_section(conf
, section
)) == NULL
)
339 sv
= _CONF_new_section(conf
, section
);
341 CONFerr(CONF_F_DEF_LOAD_BIO
,
342 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 CONFerr(CONF_F_DEF_LOAD_BIO
, 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 CONFerr(CONF_F_DEF_LOAD_BIO
, 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
) {
417 size_t newlen
= strlen(include_dir
) + strlen(include
) + 2;
419 include_path
= OPENSSL_malloc(newlen
);
420 OPENSSL_strlcpy(include_path
, include_dir
, newlen
);
421 OPENSSL_strlcat(include_path
, "/", newlen
);
422 OPENSSL_strlcat(include_path
, include
, newlen
);
423 OPENSSL_free(include
);
425 include_path
= include
;
428 /* get the BIO of the included file */
429 #ifndef OPENSSL_NO_POSIX_IO
430 next
= process_include(include_path
, &dirctx
, &dirpath
);
431 if (include_path
!= dirpath
) {
432 /* dirpath will contain include in case of a directory */
433 OPENSSL_free(include_path
);
436 next
= BIO_new_file(include_path
, "r");
437 OPENSSL_free(include_path
);
441 /* push the currently processing BIO onto stack */
443 if ((biosk
= sk_BIO_new_null()) == NULL
) {
444 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
448 if (!sk_BIO_push(biosk
, in
)) {
449 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
452 /* continue with reading from the included BIO */
456 } else if (*p
!= '=') {
457 CONFerr(CONF_F_DEF_LOAD_BIO
, CONF_R_MISSING_EQUAL_SIGN
);
458 ERR_add_error_data(2, "HERE-->", p
);
463 start
= eat_ws(conf
, p
);
464 trim_ws(conf
, start
);
466 if ((v
= OPENSSL_malloc(sizeof(*v
))) == NULL
) {
467 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
470 v
->name
= OPENSSL_strdup(pname
);
472 if (v
->name
== NULL
) {
473 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
476 if (!str_copy(conf
, psection
, &(v
->value
), start
))
479 if (strcmp(psection
, section
) != 0) {
480 if ((tv
= _CONF_get_section(conf
, psection
))
482 tv
= _CONF_new_section(conf
, psection
);
484 CONFerr(CONF_F_DEF_LOAD_BIO
,
485 CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
490 if (_CONF_add_string(conf
, tv
, v
) == 0) {
491 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
498 OPENSSL_free(section
);
500 * No need to pop, since we only get here if the stack is empty.
501 * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE!
507 OPENSSL_free(section
);
509 * Since |in| is the first element of the stack and should NOT be freed
510 * here, we cannot use sk_BIO_pop_free(). Instead, we pop and free one
511 * BIO at a time, making sure that the last one popped isn't.
513 while (sk_BIO_num(biosk
) > 0) {
514 BIO
*popped
= sk_BIO_pop(biosk
);
519 #ifndef OPENSSL_NO_POSIX_IO
520 OPENSSL_free(dirpath
);
522 OPENSSL_DIR_end(&dirctx
);
526 BIO_snprintf(btmp
, sizeof(btmp
), "%ld", eline
);
527 ERR_add_error_data(2, "line ", btmp
);
528 if (h
!= conf
->data
) {
529 CONF_free(conf
->data
);
533 OPENSSL_free(v
->name
);
534 OPENSSL_free(v
->value
);
540 static void clear_comments(CONF
*conf
, char *p
)
543 if (IS_FCOMMENT(conf
, *p
)) {
547 if (!IS_WS(conf
, *p
)) {
554 if (IS_COMMENT(conf
, *p
)) {
558 if (IS_DQUOTE(conf
, *p
)) {
559 p
= scan_dquote(conf
, p
);
562 if (IS_QUOTE(conf
, *p
)) {
563 p
= scan_quote(conf
, p
);
566 if (IS_ESC(conf
, *p
)) {
567 p
= scan_esc(conf
, p
);
570 if (IS_EOF(conf
, *p
))
577 static int str_copy(CONF
*conf
, char *section
, char **pto
, char *from
)
579 int q
, r
, rr
= 0, to
= 0, len
= 0;
580 char *s
, *e
, *rp
, *p
, *rrp
, *np
, *cp
, v
;
583 if ((buf
= BUF_MEM_new()) == NULL
)
586 len
= strlen(from
) + 1;
587 if (!BUF_MEM_grow(buf
, len
))
591 if (IS_QUOTE(conf
, *from
)) {
594 while (!IS_EOF(conf
, *from
) && (*from
!= q
)) {
595 if (IS_ESC(conf
, *from
)) {
597 if (IS_EOF(conf
, *from
))
600 buf
->data
[to
++] = *(from
++);
604 } else if (IS_DQUOTE(conf
, *from
)) {
607 while (!IS_EOF(conf
, *from
)) {
609 if (*(from
+ 1) == q
) {
615 buf
->data
[to
++] = *(from
++);
619 } else if (IS_ESC(conf
, *from
)) {
633 } else if (IS_EOF(conf
, *from
))
635 else if (*from
== '$'
636 && (!conf
->flag_dollarid
638 || from
[1] == '(')) {
641 /* try to expand it */
655 while (IS_ALNUM(conf
, *e
)
656 || (conf
->flag_dollarid
&& IS_DOLLAR(conf
, *e
)))
658 if ((e
[0] == ':') && (e
[1] == ':')) {
665 while (IS_ALNUM(conf
, *e
)
666 || (conf
->flag_dollarid
&& IS_DOLLAR(conf
, *e
)))
674 CONFerr(CONF_F_STR_COPY
, CONF_R_NO_CLOSE_BRACE
);
680 * So at this point we have
681 * np which is the start of the name string which is
683 * cp which is the start of the section string which is
685 * e is the 'next point after'.
686 * r and rr are the chars replaced by the '\0'
687 * rp and rrp is where 'r' and 'rr' came from.
689 p
= _CONF_get_string(conf
, cp
, np
);
694 CONFerr(CONF_F_STR_COPY
, CONF_R_VARIABLE_HAS_NO_VALUE
);
697 newsize
= strlen(p
) + buf
->length
- (e
- from
);
698 if (newsize
> MAX_CONF_VALUE_LENGTH
) {
699 CONFerr(CONF_F_STR_COPY
, CONF_R_VARIABLE_EXPANSION_TOO_LONG
);
702 if (!BUF_MEM_grow_clean(buf
, newsize
)) {
703 CONFerr(CONF_F_STR_COPY
, ERR_R_MALLOC_FAILURE
);
707 buf
->data
[to
++] = *(p
++);
710 * Since we change the pointer 'from', we also have to change the
711 * perceived length of the string it points at. /RL
717 * In case there were no braces or parenthesis around the
718 * variable reference, we have to put back the character that was
719 * replaced with a '\0'. /RL
723 buf
->data
[to
++] = *(from
++);
725 buf
->data
[to
] = '\0';
735 #ifndef OPENSSL_NO_POSIX_IO
737 * Check whether included path is a directory.
738 * Returns next BIO to process and in case of a directory
739 * also an opened directory context and the include path.
741 static BIO
*process_include(char *include
, OPENSSL_DIR_CTX
**dirctx
,
747 if (stat(include
, &st
) < 0) {
748 ERR_raise_data(ERR_LIB_SYS
, errno
,
751 /* missing include file is not fatal error */
755 if (S_ISDIR(st
.st_mode
)) {
756 if (*dirctx
!= NULL
) {
757 CONFerr(CONF_F_PROCESS_INCLUDE
,
758 CONF_R_RECURSIVE_DIRECTORY_INCLUDE
);
759 ERR_add_error_data(1, include
);
762 /* a directory, load its contents */
763 if ((next
= get_next_file(include
, dirctx
)) != NULL
)
768 next
= BIO_new_file(include
, "r");
773 * Get next file from the directory path.
774 * Returns BIO of the next file to read and updates dirctx.
776 static BIO
*get_next_file(const char *path
, OPENSSL_DIR_CTX
**dirctx
)
778 const char *filename
;
781 pathlen
= strlen(path
);
782 while ((filename
= OPENSSL_DIR_read(dirctx
, path
)) != NULL
) {
785 namelen
= strlen(filename
);
788 if ((namelen
> 5 && strcasecmp(filename
+ namelen
- 5, ".conf") == 0)
789 || (namelen
> 4 && strcasecmp(filename
+ namelen
- 4, ".cnf") == 0)) {
794 newlen
= pathlen
+ namelen
+ 2;
795 newpath
= OPENSSL_zalloc(newlen
);
796 if (newpath
== NULL
) {
797 CONFerr(CONF_F_GET_NEXT_FILE
, ERR_R_MALLOC_FAILURE
);
800 #ifdef OPENSSL_SYS_VMS
802 * If the given path isn't clear VMS syntax,
803 * we treat it as on Unix.
805 if (path
[pathlen
- 1] == ']'
806 || path
[pathlen
- 1] == '>'
807 || path
[pathlen
- 1] == ':') {
808 /* Clear VMS directory syntax, just copy as is */
809 OPENSSL_strlcpy(newpath
, path
, newlen
);
812 if (newpath
[0] == '\0') {
813 OPENSSL_strlcpy(newpath
, path
, newlen
);
814 OPENSSL_strlcat(newpath
, "/", newlen
);
816 OPENSSL_strlcat(newpath
, filename
, newlen
);
818 bio
= BIO_new_file(newpath
, "r");
819 OPENSSL_free(newpath
);
820 /* Errors when opening files are non-fatal. */
825 OPENSSL_DIR_end(dirctx
);
831 static int is_keytype(const CONF
*conf
, char c
, unsigned short type
)
833 const unsigned short * keytypes
= (const unsigned short *) conf
->meth_data
;
834 unsigned char key
= (unsigned char)c
;
836 #ifdef CHARSET_EBCDIC
839 /* key is out of range for os_toascii table */
843 /* convert key from ebcdic to ascii */
844 key
= os_toascii
[key
];
848 /* key is not a seven bit ascii character */
852 return (keytypes
[key
] & type
) ? 1 : 0;
855 static char *eat_ws(CONF
*conf
, char *p
)
857 while (IS_WS(conf
, *p
) && (!IS_EOF(conf
, *p
)))
862 static void trim_ws(CONF
*conf
, char *start
)
866 while (!IS_EOF(conf
, *p
))
869 while ((p
>= start
) && IS_WS(conf
, *p
))
875 static char *eat_alpha_numeric(CONF
*conf
, char *p
)
878 if (IS_ESC(conf
, *p
)) {
879 p
= scan_esc(conf
, p
);
882 if (!(IS_ALNUM_PUNCT(conf
, *p
)
883 || (conf
->flag_dollarid
&& IS_DOLLAR(conf
, *p
))))
889 static char *scan_quote(CONF
*conf
, char *p
)
894 while (!(IS_EOF(conf
, *p
)) && (*p
!= q
)) {
895 if (IS_ESC(conf
, *p
)) {
897 if (IS_EOF(conf
, *p
))
907 static char *scan_dquote(CONF
*conf
, char *p
)
912 while (!(IS_EOF(conf
, *p
))) {
927 static void dump_value_doall_arg(const CONF_VALUE
*a
, BIO
*out
)
930 BIO_printf(out
, "[%s] %s=%s\n", a
->section
, a
->name
, a
->value
);
932 BIO_printf(out
, "[[%s]]\n", a
->section
);
935 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE
, BIO
);
937 static int def_dump(const CONF
*conf
, BIO
*out
)
939 lh_CONF_VALUE_doall_BIO(conf
->data
, dump_value_doall_arg
, out
);
943 static int def_is_number(const CONF
*conf
, char c
)
945 return IS_NUMBER(conf
, c
);
948 static int def_to_int(const CONF
*conf
, char c
)