2 * Copyright 1995-2018 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 * The maximum length we can grow a value to after variable expansion. 64k
32 * should be more than enough for all reasonable uses.
34 #define MAX_CONF_VALUE_LENGTH 65536
36 static char *eat_ws(CONF *conf, char *p);
37 static void trim_ws(CONF *conf, char *start);
38 static char *eat_alpha_numeric(CONF *conf, char *p);
39 static void clear_comments(CONF *conf, char *p);
40 static int str_copy(CONF *conf, char *section, char **to, char *from);
41 static char *scan_quote(CONF *conf, char *p);
42 static char *scan_dquote(CONF *conf, char *p);
43 #define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
44 #ifndef OPENSSL_NO_POSIX_IO
45 static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
47 static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx);
50 static CONF *def_create(CONF_METHOD *meth);
51 static int def_init_default(CONF *conf);
52 static int def_init_WIN32(CONF *conf);
53 static int def_destroy(CONF *conf);
54 static int def_destroy_data(CONF *conf);
55 static int def_load(CONF *conf, const char *name, long *eline);
56 static int def_load_bio(CONF *conf, BIO *bp, long *eline);
57 static int def_dump(const CONF *conf, BIO *bp);
58 static int def_is_number(const CONF *conf, char c);
59 static int def_to_int(const CONF *conf, char c);
61 static CONF_METHOD default_method = {
74 static CONF_METHOD WIN32_method = {
87 CONF_METHOD *NCONF_default()
89 return &default_method;
92 CONF_METHOD *NCONF_WIN32()
97 static CONF *def_create(CONF_METHOD *meth)
101 ret = OPENSSL_malloc(sizeof(*ret));
103 if (meth->init(ret) == 0) {
110 static int def_init_default(CONF *conf)
115 conf->meth = &default_method;
116 conf->meth_data = (void *)CONF_type_default;
122 static int def_init_WIN32(CONF *conf)
127 conf->meth = &WIN32_method;
128 conf->meth_data = (void *)CONF_type_win32;
134 static int def_destroy(CONF *conf)
136 if (def_destroy_data(conf)) {
143 static int def_destroy_data(CONF *conf)
147 _CONF_free_data(conf);
151 static int def_load(CONF *conf, const char *name, long *line)
156 #ifdef OPENSSL_SYS_VMS
157 in = BIO_new_file(name, "r");
159 in = BIO_new_file(name, "rb");
162 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
163 CONFerr(CONF_F_DEF_LOAD, CONF_R_NO_SUCH_FILE);
165 CONFerr(CONF_F_DEF_LOAD, ERR_R_SYS_LIB);
169 ret = def_load_bio(conf, in, line);
175 static int def_load_bio(CONF *conf, BIO *in, long *line)
177 /* The macro BUFSIZE conflicts with a system macro in VxWorks */
178 #define CONFBUFSIZE 512
179 int bufnum = 0, i, ii;
180 BUF_MEM *buff = NULL;
184 char btmp[DECIMAL_SIZE(eline) + 1];
185 CONF_VALUE *v = NULL, *tv;
186 CONF_VALUE *sv = NULL;
187 char *section = NULL, *buf;
188 char *start, *psection, *pname;
189 void *h = (void *)(conf->data);
190 STACK_OF(BIO) *biosk = NULL;
191 #ifndef OPENSSL_NO_POSIX_IO
192 char *dirpath = NULL;
193 OPENSSL_DIR_CTX *dirctx = NULL;
196 if ((buff = BUF_MEM_new()) == NULL) {
197 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
201 section = OPENSSL_strdup("default");
202 if (section == NULL) {
203 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
207 if (_CONF_new_data(conf) == 0) {
208 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
212 sv = _CONF_new_section(conf, section);
214 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
221 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
222 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
225 p = &(buff->data[bufnum]);
228 BIO_gets(in, p, CONFBUFSIZE - 1);
229 p[CONFBUFSIZE - 1] = '\0';
231 if (i == 0 && !again) {
232 /* the currently processed BIO is at EOF */
235 #ifndef OPENSSL_NO_POSIX_IO
236 /* continue processing with the next file from directory */
237 if (dirctx != NULL) {
240 if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
245 OPENSSL_free(dirpath);
250 /* no more files in directory, continue with processing parent */
251 if ((parent = sk_BIO_pop(biosk)) == NULL) {
252 /* everything processed get out of the loop */
262 if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
268 * we removed some trailing stuff so there is a new line on the end.
271 again = 1; /* long line */
274 eline++; /* another input line */
277 /* we now have a line with trailing \r\n removed */
279 /* i is the number of bytes */
283 /* check for line continuation */
286 * If we have bytes and the last char '\\' and second last char
289 p = &(buff->data[bufnum - 1]);
290 if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
300 clear_comments(conf, buf);
301 s = eat_ws(conf, buf);
302 if (IS_EOF(conf, *s))
303 continue; /* blank line */
308 start = eat_ws(conf, s);
311 end = eat_alpha_numeric(conf, ss);
312 p = eat_ws(conf, end);
314 if (*p != '\0' && ss != p) {
318 CONFerr(CONF_F_DEF_LOAD_BIO,
319 CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
323 if (!str_copy(conf, NULL, §ion, start))
325 if ((sv = _CONF_get_section(conf, section)) == NULL)
326 sv = _CONF_new_section(conf, section);
328 CONFerr(CONF_F_DEF_LOAD_BIO,
329 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
335 end = eat_alpha_numeric(conf, s);
336 if ((end[0] == ':') && (end[1] == ':')) {
341 end = eat_alpha_numeric(conf, end);
345 p = eat_ws(conf, end);
346 if (strncmp(pname, ".include", 8) == 0 && p != pname + 8) {
347 char *include = NULL;
351 if (!str_copy(conf, psection, &include, p))
353 /* get the BIO of the included file */
354 #ifndef OPENSSL_NO_POSIX_IO
355 next = process_include(include, &dirctx, &dirpath);
356 if (include != dirpath) {
357 /* dirpath will contain include in case of a directory */
358 OPENSSL_free(include);
361 next = BIO_new_file(include, "r");
362 OPENSSL_free(include);
365 /* push the currently processing BIO onto stack */
367 if ((biosk = sk_BIO_new_null()) == NULL) {
368 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
372 if (!sk_BIO_push(biosk, in)) {
373 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
376 /* continue with reading from the included BIO */
380 } else if (*p != '=') {
381 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);
386 start = eat_ws(conf, p);
387 trim_ws(conf, start);
389 if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
390 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
393 v->name = OPENSSL_strdup(pname);
395 if (v->name == NULL) {
396 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
399 if (!str_copy(conf, psection, &(v->value), start))
402 if (strcmp(psection, section) != 0) {
403 if ((tv = _CONF_get_section(conf, psection))
405 tv = _CONF_new_section(conf, psection);
407 CONFerr(CONF_F_DEF_LOAD_BIO,
408 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
413 if (_CONF_add_string(conf, tv, v) == 0) {
414 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
421 OPENSSL_free(section);
422 sk_BIO_pop_free(biosk, BIO_vfree);
426 OPENSSL_free(section);
427 sk_BIO_pop_free(biosk, BIO_vfree);
428 #ifndef OPENSSL_NO_POSIX_IO
429 OPENSSL_free(dirpath);
431 OPENSSL_DIR_end(&dirctx);
435 BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
436 ERR_add_error_data(2, "line ", btmp);
437 if (h != conf->data) {
438 CONF_free(conf->data);
442 OPENSSL_free(v->name);
443 OPENSSL_free(v->value);
449 static void clear_comments(CONF *conf, char *p)
452 if (IS_FCOMMENT(conf, *p)) {
456 if (!IS_WS(conf, *p)) {
463 if (IS_COMMENT(conf, *p)) {
467 if (IS_DQUOTE(conf, *p)) {
468 p = scan_dquote(conf, p);
471 if (IS_QUOTE(conf, *p)) {
472 p = scan_quote(conf, p);
475 if (IS_ESC(conf, *p)) {
476 p = scan_esc(conf, p);
479 if (IS_EOF(conf, *p))
486 static int str_copy(CONF *conf, char *section, char **pto, char *from)
488 int q, r, rr = 0, to = 0, len = 0;
489 char *s, *e, *rp, *p, *rrp, *np, *cp, v;
492 if ((buf = BUF_MEM_new()) == NULL)
495 len = strlen(from) + 1;
496 if (!BUF_MEM_grow(buf, len))
500 if (IS_QUOTE(conf, *from)) {
503 while (!IS_EOF(conf, *from) && (*from != q)) {
504 if (IS_ESC(conf, *from)) {
506 if (IS_EOF(conf, *from))
509 buf->data[to++] = *(from++);
513 } else if (IS_DQUOTE(conf, *from)) {
516 while (!IS_EOF(conf, *from)) {
518 if (*(from + 1) == q) {
524 buf->data[to++] = *(from++);
528 } else if (IS_ESC(conf, *from)) {
542 } else if (IS_EOF(conf, *from))
544 else if (*from == '$') {
547 /* try to expand it */
561 while (IS_ALNUM(conf, *e))
563 if ((e[0] == ':') && (e[1] == ':')) {
570 while (IS_ALNUM(conf, *e))
578 CONFerr(CONF_F_STR_COPY, CONF_R_NO_CLOSE_BRACE);
584 * So at this point we have
585 * np which is the start of the name string which is
587 * cp which is the start of the section string which is
589 * e is the 'next point after'.
590 * r and rr are the chars replaced by the '\0'
591 * rp and rrp is where 'r' and 'rr' came from.
593 p = _CONF_get_string(conf, cp, np);
598 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_HAS_NO_VALUE);
601 newsize = strlen(p) + buf->length - (e - from);
602 if (newsize > MAX_CONF_VALUE_LENGTH) {
603 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
606 if (!BUF_MEM_grow_clean(buf, newsize)) {
607 CONFerr(CONF_F_STR_COPY, ERR_R_MALLOC_FAILURE);
611 buf->data[to++] = *(p++);
614 * Since we change the pointer 'from', we also have to change the
615 * perceived length of the string it points at. /RL
621 * In case there were no braces or parenthesis around the
622 * variable reference, we have to put back the character that was
623 * replaced with a '\0'. /RL
627 buf->data[to++] = *(from++);
629 buf->data[to] = '\0';
639 #ifndef OPENSSL_NO_POSIX_IO
641 * Check whether included path is a directory.
642 * Returns next BIO to process and in case of a directory
643 * also an opened directory context and the include path.
645 static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
648 struct stat st = { 0 };
651 if (stat(include, &st) < 0) {
652 SYSerr(SYS_F_STAT, errno);
653 ERR_add_error_data(1, include);
654 /* missing include file is not fatal error */
658 if ((st.st_mode & S_IFDIR) == S_IFDIR) {
659 if (*dirctx != NULL) {
660 CONFerr(CONF_F_PROCESS_INCLUDE,
661 CONF_R_RECURSIVE_DIRECTORY_INCLUDE);
662 ERR_add_error_data(1, include);
665 /* a directory, load its contents */
666 if ((next = get_next_file(include, dirctx)) != NULL)
671 next = BIO_new_file(include, "r");
676 * Get next file from the directory path.
677 * Returns BIO of the next file to read and updates dirctx.
679 static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx)
681 const char *filename;
683 while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
686 namelen = strlen(filename);
689 if ((namelen > 5 && strcasecmp(filename + namelen - 5, ".conf") == 0)
690 || (namelen > 4 && strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
695 newlen = strlen(path) + namelen + 2;
696 newpath = OPENSSL_zalloc(newlen);
697 if (newpath == NULL) {
698 CONFerr(CONF_F_GET_NEXT_FILE, ERR_R_MALLOC_FAILURE);
701 #ifdef OPENSSL_SYS_VMS
703 * If the given path isn't clear VMS syntax,
704 * we treat it as on Unix.
707 size_t pathlen = strlen(path);
709 if (path[pathlen - 1] == ']' || path[pathlen - 1] == '>'
710 || path[pathlen - 1] == ':') {
711 /* Clear VMS directory syntax, just copy as is */
712 OPENSSL_strlcpy(newpath, path, newlen);
716 if (newpath[0] == '\0') {
717 OPENSSL_strlcpy(newpath, path, newlen);
718 OPENSSL_strlcat(newpath, "/", newlen);
720 OPENSSL_strlcat(newpath, filename, newlen);
722 bio = BIO_new_file(newpath, "r");
723 OPENSSL_free(newpath);
724 /* Errors when opening files are non-fatal. */
729 OPENSSL_DIR_end(dirctx);
735 static char *eat_ws(CONF *conf, char *p)
737 while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
742 static void trim_ws(CONF *conf, char *start)
746 while (!IS_EOF(conf, *p))
749 while ((p >= start) && IS_WS(conf, *p))
755 static char *eat_alpha_numeric(CONF *conf, char *p)
758 if (IS_ESC(conf, *p)) {
759 p = scan_esc(conf, p);
762 if (!IS_ALNUM_PUNCT(conf, *p))
768 static char *scan_quote(CONF *conf, char *p)
773 while (!(IS_EOF(conf, *p)) && (*p != q)) {
774 if (IS_ESC(conf, *p)) {
776 if (IS_EOF(conf, *p))
786 static char *scan_dquote(CONF *conf, char *p)
791 while (!(IS_EOF(conf, *p))) {
806 static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
809 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
811 BIO_printf(out, "[[%s]]\n", a->section);
814 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
816 static int def_dump(const CONF *conf, BIO *out)
818 lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
822 static int def_is_number(const CONF *conf, char c)
824 return IS_NUMBER(conf, c);
827 static int def_to_int(const CONF *conf, char c)