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 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
;
189 char btmp
[DECIMAL_SIZE(eline
) + 1];
190 CONF_VALUE
*v
= NULL
, *tv
;
191 CONF_VALUE
*sv
= NULL
;
192 char *section
= NULL
, *buf
;
193 char *start
, *psection
, *pname
;
194 void *h
= (void *)(conf
->data
);
195 STACK_OF(BIO
) *biosk
= NULL
;
196 #ifndef OPENSSL_NO_POSIX_IO
197 char *dirpath
= NULL
;
198 OPENSSL_DIR_CTX
*dirctx
= NULL
;
201 if ((buff
= BUF_MEM_new()) == NULL
) {
202 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_BUF_LIB
);
206 section
= OPENSSL_strdup("default");
207 if (section
== NULL
) {
208 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
212 if (_CONF_new_data(conf
) == 0) {
213 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
217 sv
= _CONF_new_section(conf
, section
);
219 CONFerr(CONF_F_DEF_LOAD_BIO
, CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
226 if (!BUF_MEM_grow(buff
, bufnum
+ CONFBUFSIZE
)) {
227 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_BUF_LIB
);
230 p
= &(buff
->data
[bufnum
]);
233 BIO_gets(in
, p
, CONFBUFSIZE
- 1);
234 p
[CONFBUFSIZE
- 1] = '\0';
236 if (i
== 0 && !again
) {
237 /* the currently processed BIO is at EOF */
240 #ifndef OPENSSL_NO_POSIX_IO
241 /* continue processing with the next file from directory */
242 if (dirctx
!= NULL
) {
245 if ((next
= get_next_file(dirpath
, &dirctx
)) != NULL
) {
250 OPENSSL_free(dirpath
);
255 /* no more files in directory, continue with processing parent */
256 if ((parent
= sk_BIO_pop(biosk
)) == NULL
) {
257 /* everything processed get out of the loop */
267 if ((p
[i
- 1] != '\r') && (p
[i
- 1] != '\n'))
273 * we removed some trailing stuff so there is a new line on the end.
276 again
= 1; /* long line */
279 eline
++; /* another input line */
282 /* we now have a line with trailing \r\n removed */
284 /* i is the number of bytes */
288 /* check for line continuation */
291 * If we have bytes and the last char '\\' and second last char
294 p
= &(buff
->data
[bufnum
- 1]);
295 if (IS_ESC(conf
, p
[0]) && ((bufnum
<= 1) || !IS_ESC(conf
, p
[-1]))) {
305 clear_comments(conf
, buf
);
306 s
= eat_ws(conf
, buf
);
307 if (IS_EOF(conf
, *s
))
308 continue; /* blank line */
313 start
= eat_ws(conf
, s
);
316 end
= eat_alpha_numeric(conf
, ss
);
317 p
= eat_ws(conf
, end
);
319 if (*p
!= '\0' && ss
!= p
) {
323 CONFerr(CONF_F_DEF_LOAD_BIO
,
324 CONF_R_MISSING_CLOSE_SQUARE_BRACKET
);
328 if (!str_copy(conf
, NULL
, §ion
, start
))
330 if ((sv
= _CONF_get_section(conf
, section
)) == NULL
)
331 sv
= _CONF_new_section(conf
, section
);
333 CONFerr(CONF_F_DEF_LOAD_BIO
,
334 CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
340 end
= eat_alpha_numeric(conf
, s
);
341 if ((end
[0] == ':') && (end
[1] == ':')) {
346 end
= eat_alpha_numeric(conf
, end
);
350 p
= eat_ws(conf
, end
);
351 if (strncmp(pname
, ".include", 8) == 0
352 && (p
!= pname
+ 8 || *p
== '=')) {
353 char *include
= NULL
;
361 if (!str_copy(conf
, psection
, &include
, p
))
363 /* get the BIO of the included file */
364 #ifndef OPENSSL_NO_POSIX_IO
365 next
= process_include(include
, &dirctx
, &dirpath
);
366 if (include
!= dirpath
) {
367 /* dirpath will contain include in case of a directory */
368 OPENSSL_free(include
);
371 next
= BIO_new_file(include
, "r");
372 OPENSSL_free(include
);
375 /* push the currently processing BIO onto stack */
377 if ((biosk
= sk_BIO_new_null()) == NULL
) {
378 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
382 if (!sk_BIO_push(biosk
, in
)) {
383 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
386 /* continue with reading from the included BIO */
390 } else if (*p
!= '=') {
391 CONFerr(CONF_F_DEF_LOAD_BIO
, CONF_R_MISSING_EQUAL_SIGN
);
396 start
= eat_ws(conf
, p
);
397 trim_ws(conf
, start
);
399 if ((v
= OPENSSL_malloc(sizeof(*v
))) == NULL
) {
400 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
403 v
->name
= OPENSSL_strdup(pname
);
405 if (v
->name
== NULL
) {
406 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
409 if (!str_copy(conf
, psection
, &(v
->value
), start
))
412 if (strcmp(psection
, section
) != 0) {
413 if ((tv
= _CONF_get_section(conf
, psection
))
415 tv
= _CONF_new_section(conf
, psection
);
417 CONFerr(CONF_F_DEF_LOAD_BIO
,
418 CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
423 if (_CONF_add_string(conf
, tv
, v
) == 0) {
424 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
431 OPENSSL_free(section
);
433 * No need to pop, since we only get here if the stack is empty.
434 * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE!
440 OPENSSL_free(section
);
442 * Since |in| is the first element of the stack and should NOT be freed
443 * here, we cannot use sk_BIO_pop_free(). Instead, we pop and free one
444 * BIO at a time, making sure that the last one popped isn't.
446 while (sk_BIO_num(biosk
) > 0) {
447 BIO
*popped
= sk_BIO_pop(biosk
);
452 #ifndef OPENSSL_NO_POSIX_IO
453 OPENSSL_free(dirpath
);
455 OPENSSL_DIR_end(&dirctx
);
459 BIO_snprintf(btmp
, sizeof(btmp
), "%ld", eline
);
460 ERR_add_error_data(2, "line ", btmp
);
461 if (h
!= conf
->data
) {
462 CONF_free(conf
->data
);
466 OPENSSL_free(v
->name
);
467 OPENSSL_free(v
->value
);
473 static void clear_comments(CONF
*conf
, char *p
)
476 if (IS_FCOMMENT(conf
, *p
)) {
480 if (!IS_WS(conf
, *p
)) {
487 if (IS_COMMENT(conf
, *p
)) {
491 if (IS_DQUOTE(conf
, *p
)) {
492 p
= scan_dquote(conf
, p
);
495 if (IS_QUOTE(conf
, *p
)) {
496 p
= scan_quote(conf
, p
);
499 if (IS_ESC(conf
, *p
)) {
500 p
= scan_esc(conf
, p
);
503 if (IS_EOF(conf
, *p
))
510 static int str_copy(CONF
*conf
, char *section
, char **pto
, char *from
)
512 int q
, r
, rr
= 0, to
= 0, len
= 0;
513 char *s
, *e
, *rp
, *p
, *rrp
, *np
, *cp
, v
;
516 if ((buf
= BUF_MEM_new()) == NULL
)
519 len
= strlen(from
) + 1;
520 if (!BUF_MEM_grow(buf
, len
))
524 if (IS_QUOTE(conf
, *from
)) {
527 while (!IS_EOF(conf
, *from
) && (*from
!= q
)) {
528 if (IS_ESC(conf
, *from
)) {
530 if (IS_EOF(conf
, *from
))
533 buf
->data
[to
++] = *(from
++);
537 } else if (IS_DQUOTE(conf
, *from
)) {
540 while (!IS_EOF(conf
, *from
)) {
542 if (*(from
+ 1) == q
) {
548 buf
->data
[to
++] = *(from
++);
552 } else if (IS_ESC(conf
, *from
)) {
566 } else if (IS_EOF(conf
, *from
))
568 else if (*from
== '$') {
571 /* try to expand it */
585 while (IS_ALNUM(conf
, *e
))
587 if ((e
[0] == ':') && (e
[1] == ':')) {
594 while (IS_ALNUM(conf
, *e
))
602 CONFerr(CONF_F_STR_COPY
, CONF_R_NO_CLOSE_BRACE
);
608 * So at this point we have
609 * np which is the start of the name string which is
611 * cp which is the start of the section string which is
613 * e is the 'next point after'.
614 * r and rr are the chars replaced by the '\0'
615 * rp and rrp is where 'r' and 'rr' came from.
617 p
= _CONF_get_string(conf
, cp
, np
);
622 CONFerr(CONF_F_STR_COPY
, CONF_R_VARIABLE_HAS_NO_VALUE
);
625 newsize
= strlen(p
) + buf
->length
- (e
- from
);
626 if (newsize
> MAX_CONF_VALUE_LENGTH
) {
627 CONFerr(CONF_F_STR_COPY
, CONF_R_VARIABLE_EXPANSION_TOO_LONG
);
630 if (!BUF_MEM_grow_clean(buf
, newsize
)) {
631 CONFerr(CONF_F_STR_COPY
, ERR_R_MALLOC_FAILURE
);
635 buf
->data
[to
++] = *(p
++);
638 * Since we change the pointer 'from', we also have to change the
639 * perceived length of the string it points at. /RL
645 * In case there were no braces or parenthesis around the
646 * variable reference, we have to put back the character that was
647 * replaced with a '\0'. /RL
651 buf
->data
[to
++] = *(from
++);
653 buf
->data
[to
] = '\0';
663 #ifndef OPENSSL_NO_POSIX_IO
665 * Check whether included path is a directory.
666 * Returns next BIO to process and in case of a directory
667 * also an opened directory context and the include path.
669 static BIO
*process_include(char *include
, OPENSSL_DIR_CTX
**dirctx
,
675 if (stat(include
, &st
) < 0) {
676 FUNCerr("stat", errno
);
677 ERR_add_error_data(1, include
);
678 /* missing include file is not fatal error */
682 if (S_ISDIR(st
.st_mode
)) {
683 if (*dirctx
!= NULL
) {
684 CONFerr(CONF_F_PROCESS_INCLUDE
,
685 CONF_R_RECURSIVE_DIRECTORY_INCLUDE
);
686 ERR_add_error_data(1, include
);
689 /* a directory, load its contents */
690 if ((next
= get_next_file(include
, dirctx
)) != NULL
)
695 next
= BIO_new_file(include
, "r");
700 * Get next file from the directory path.
701 * Returns BIO of the next file to read and updates dirctx.
703 static BIO
*get_next_file(const char *path
, OPENSSL_DIR_CTX
**dirctx
)
705 const char *filename
;
707 while ((filename
= OPENSSL_DIR_read(dirctx
, path
)) != NULL
) {
710 namelen
= strlen(filename
);
713 if ((namelen
> 5 && strcasecmp(filename
+ namelen
- 5, ".conf") == 0)
714 || (namelen
> 4 && strcasecmp(filename
+ namelen
- 4, ".cnf") == 0)) {
719 newlen
= strlen(path
) + namelen
+ 2;
720 newpath
= OPENSSL_zalloc(newlen
);
721 if (newpath
== NULL
) {
722 CONFerr(CONF_F_GET_NEXT_FILE
, ERR_R_MALLOC_FAILURE
);
725 #ifdef OPENSSL_SYS_VMS
727 * If the given path isn't clear VMS syntax,
728 * we treat it as on Unix.
731 size_t pathlen
= strlen(path
);
733 if (path
[pathlen
- 1] == ']' || path
[pathlen
- 1] == '>'
734 || path
[pathlen
- 1] == ':') {
735 /* Clear VMS directory syntax, just copy as is */
736 OPENSSL_strlcpy(newpath
, path
, newlen
);
740 if (newpath
[0] == '\0') {
741 OPENSSL_strlcpy(newpath
, path
, newlen
);
742 OPENSSL_strlcat(newpath
, "/", newlen
);
744 OPENSSL_strlcat(newpath
, filename
, newlen
);
746 bio
= BIO_new_file(newpath
, "r");
747 OPENSSL_free(newpath
);
748 /* Errors when opening files are non-fatal. */
753 OPENSSL_DIR_end(dirctx
);
759 static int is_keytype(const CONF
*conf
, char c
, unsigned short type
)
761 const unsigned short * keytypes
= (const unsigned short *) conf
->meth_data
;
762 unsigned char key
= (unsigned char)c
;
764 #ifdef CHARSET_EBCDIC
767 /* key is out of range for os_toascii table */
771 /* convert key from ebcdic to ascii */
772 key
= os_toascii
[key
];
776 /* key is not a seven bit ascii character */
780 return (keytypes
[key
] & type
) ? 1 : 0;
783 static char *eat_ws(CONF
*conf
, char *p
)
785 while (IS_WS(conf
, *p
) && (!IS_EOF(conf
, *p
)))
790 static void trim_ws(CONF
*conf
, char *start
)
794 while (!IS_EOF(conf
, *p
))
797 while ((p
>= start
) && IS_WS(conf
, *p
))
803 static char *eat_alpha_numeric(CONF
*conf
, char *p
)
806 if (IS_ESC(conf
, *p
)) {
807 p
= scan_esc(conf
, p
);
810 if (!IS_ALNUM_PUNCT(conf
, *p
))
816 static char *scan_quote(CONF
*conf
, char *p
)
821 while (!(IS_EOF(conf
, *p
)) && (*p
!= q
)) {
822 if (IS_ESC(conf
, *p
)) {
824 if (IS_EOF(conf
, *p
))
834 static char *scan_dquote(CONF
*conf
, char *p
)
839 while (!(IS_EOF(conf
, *p
))) {
854 static void dump_value_doall_arg(const CONF_VALUE
*a
, BIO
*out
)
857 BIO_printf(out
, "[%s] %s=%s\n", a
->section
, a
->name
, a
->value
);
859 BIO_printf(out
, "[[%s]]\n", a
->section
);
862 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE
, BIO
);
864 static int def_dump(const CONF
*conf
, BIO
*out
)
866 lh_CONF_VALUE_doall_BIO(conf
->data
, dump_value_doall_arg
, out
);
870 static int def_is_number(const CONF
*conf
, char c
)
872 return IS_NUMBER(conf
, c
);
875 static int def_to_int(const CONF
*conf
, char c
)