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 int is_keytype(const CONF *conf, char c, unsigned short type);
37 static char *eat_ws(CONF *conf, char *p);
38 static void trim_ws(CONF *conf, char *start);
39 static char *eat_alpha_numeric(CONF *conf, char *p);
40 static void clear_comments(CONF *conf, char *p);
41 static int str_copy(CONF *conf, char *section, char **to, char *from);
42 static char *scan_quote(CONF *conf, char *p);
43 static char *scan_dquote(CONF *conf, char *p);
44 #define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
45 #ifndef OPENSSL_NO_POSIX_IO
46 static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
48 static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx);
51 static CONF *def_create(CONF_METHOD *meth);
52 static int def_init_default(CONF *conf);
53 static int def_init_WIN32(CONF *conf);
54 static int def_destroy(CONF *conf);
55 static int def_destroy_data(CONF *conf);
56 static int def_load(CONF *conf, const char *name, long *eline);
57 static int def_load_bio(CONF *conf, BIO *bp, long *eline);
58 static int def_dump(const CONF *conf, BIO *bp);
59 static int def_is_number(const CONF *conf, char c);
60 static int def_to_int(const CONF *conf, char c);
62 static CONF_METHOD default_method = {
75 static CONF_METHOD WIN32_method = {
88 CONF_METHOD *NCONF_default()
90 return &default_method;
93 CONF_METHOD *NCONF_WIN32()
98 static CONF *def_create(CONF_METHOD *meth)
102 ret = OPENSSL_malloc(sizeof(*ret));
104 if (meth->init(ret) == 0) {
111 static int def_init_default(CONF *conf)
116 conf->meth = &default_method;
117 conf->meth_data = (void *)CONF_type_default;
123 static int def_init_WIN32(CONF *conf)
128 conf->meth = &WIN32_method;
129 conf->meth_data = (void *)CONF_type_win32;
135 static int def_destroy(CONF *conf)
137 if (def_destroy_data(conf)) {
144 static int def_destroy_data(CONF *conf)
148 _CONF_free_data(conf);
152 static int def_load(CONF *conf, const char *name, long *line)
157 #ifdef OPENSSL_SYS_VMS
158 in = BIO_new_file(name, "r");
160 in = BIO_new_file(name, "rb");
163 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
164 CONFerr(CONF_F_DEF_LOAD, CONF_R_NO_SUCH_FILE);
166 CONFerr(CONF_F_DEF_LOAD, ERR_R_SYS_LIB);
170 ret = def_load_bio(conf, in, line);
176 static int def_load_bio(CONF *conf, BIO *in, long *line)
178 /* The macro BUFSIZE conflicts with a system macro in VxWorks */
179 #define CONFBUFSIZE 512
180 int bufnum = 0, i, ii;
181 BUF_MEM *buff = NULL;
185 char btmp[DECIMAL_SIZE(eline) + 1];
186 CONF_VALUE *v = NULL, *tv;
187 CONF_VALUE *sv = NULL;
188 char *section = NULL, *buf;
189 char *start, *psection, *pname;
190 void *h = (void *)(conf->data);
191 STACK_OF(BIO) *biosk = NULL;
192 #ifndef OPENSSL_NO_POSIX_IO
193 char *dirpath = NULL;
194 OPENSSL_DIR_CTX *dirctx = NULL;
197 if ((buff = BUF_MEM_new()) == NULL) {
198 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
202 section = OPENSSL_strdup("default");
203 if (section == NULL) {
204 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
208 if (_CONF_new_data(conf) == 0) {
209 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
213 sv = _CONF_new_section(conf, section);
215 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
222 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
223 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
226 p = &(buff->data[bufnum]);
229 BIO_gets(in, p, CONFBUFSIZE - 1);
230 p[CONFBUFSIZE - 1] = '\0';
232 if (i == 0 && !again) {
233 /* the currently processed BIO is at EOF */
236 #ifndef OPENSSL_NO_POSIX_IO
237 /* continue processing with the next file from directory */
238 if (dirctx != NULL) {
241 if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
246 OPENSSL_free(dirpath);
251 /* no more files in directory, continue with processing parent */
252 if ((parent = sk_BIO_pop(biosk)) == NULL) {
253 /* everything processed get out of the loop */
263 if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
269 * we removed some trailing stuff so there is a new line on the end.
272 again = 1; /* long line */
275 eline++; /* another input line */
278 /* we now have a line with trailing \r\n removed */
280 /* i is the number of bytes */
284 /* check for line continuation */
287 * If we have bytes and the last char '\\' and second last char
290 p = &(buff->data[bufnum - 1]);
291 if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
301 clear_comments(conf, buf);
302 s = eat_ws(conf, buf);
303 if (IS_EOF(conf, *s))
304 continue; /* blank line */
309 start = eat_ws(conf, s);
312 end = eat_alpha_numeric(conf, ss);
313 p = eat_ws(conf, end);
315 if (*p != '\0' && ss != p) {
319 CONFerr(CONF_F_DEF_LOAD_BIO,
320 CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
324 if (!str_copy(conf, NULL, §ion, start))
326 if ((sv = _CONF_get_section(conf, section)) == NULL)
327 sv = _CONF_new_section(conf, section);
329 CONFerr(CONF_F_DEF_LOAD_BIO,
330 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
336 end = eat_alpha_numeric(conf, s);
337 if ((end[0] == ':') && (end[1] == ':')) {
342 end = eat_alpha_numeric(conf, end);
346 p = eat_ws(conf, end);
347 if (strncmp(pname, ".include", 8) == 0 && p != pname + 8) {
348 char *include = NULL;
352 if (!str_copy(conf, psection, &include, p))
354 /* get the BIO of the included file */
355 #ifndef OPENSSL_NO_POSIX_IO
356 next = process_include(include, &dirctx, &dirpath);
357 if (include != dirpath) {
358 /* dirpath will contain include in case of a directory */
359 OPENSSL_free(include);
362 next = BIO_new_file(include, "r");
363 OPENSSL_free(include);
366 /* push the currently processing BIO onto stack */
368 if ((biosk = sk_BIO_new_null()) == NULL) {
369 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
373 if (!sk_BIO_push(biosk, in)) {
374 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
377 /* continue with reading from the included BIO */
381 } else if (*p != '=') {
382 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);
387 start = eat_ws(conf, p);
388 trim_ws(conf, start);
390 if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
391 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
394 v->name = OPENSSL_strdup(pname);
396 if (v->name == NULL) {
397 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
400 if (!str_copy(conf, psection, &(v->value), start))
403 if (strcmp(psection, section) != 0) {
404 if ((tv = _CONF_get_section(conf, psection))
406 tv = _CONF_new_section(conf, psection);
408 CONFerr(CONF_F_DEF_LOAD_BIO,
409 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
414 if (_CONF_add_string(conf, tv, v) == 0) {
415 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
422 OPENSSL_free(section);
423 sk_BIO_pop_free(biosk, BIO_vfree);
427 OPENSSL_free(section);
428 sk_BIO_pop_free(biosk, BIO_vfree);
429 #ifndef OPENSSL_NO_POSIX_IO
430 OPENSSL_free(dirpath);
432 OPENSSL_DIR_end(&dirctx);
436 BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
437 ERR_add_error_data(2, "line ", btmp);
438 if (h != conf->data) {
439 CONF_free(conf->data);
443 OPENSSL_free(v->name);
444 OPENSSL_free(v->value);
450 static void clear_comments(CONF *conf, char *p)
453 if (IS_FCOMMENT(conf, *p)) {
457 if (!IS_WS(conf, *p)) {
464 if (IS_COMMENT(conf, *p)) {
468 if (IS_DQUOTE(conf, *p)) {
469 p = scan_dquote(conf, p);
472 if (IS_QUOTE(conf, *p)) {
473 p = scan_quote(conf, p);
476 if (IS_ESC(conf, *p)) {
477 p = scan_esc(conf, p);
480 if (IS_EOF(conf, *p))
487 static int str_copy(CONF *conf, char *section, char **pto, char *from)
489 int q, r, rr = 0, to = 0, len = 0;
490 char *s, *e, *rp, *p, *rrp, *np, *cp, v;
493 if ((buf = BUF_MEM_new()) == NULL)
496 len = strlen(from) + 1;
497 if (!BUF_MEM_grow(buf, len))
501 if (IS_QUOTE(conf, *from)) {
504 while (!IS_EOF(conf, *from) && (*from != q)) {
505 if (IS_ESC(conf, *from)) {
507 if (IS_EOF(conf, *from))
510 buf->data[to++] = *(from++);
514 } else if (IS_DQUOTE(conf, *from)) {
517 while (!IS_EOF(conf, *from)) {
519 if (*(from + 1) == q) {
525 buf->data[to++] = *(from++);
529 } else if (IS_ESC(conf, *from)) {
543 } else if (IS_EOF(conf, *from))
545 else if (*from == '$') {
548 /* try to expand it */
562 while (IS_ALNUM(conf, *e))
564 if ((e[0] == ':') && (e[1] == ':')) {
571 while (IS_ALNUM(conf, *e))
579 CONFerr(CONF_F_STR_COPY, CONF_R_NO_CLOSE_BRACE);
585 * So at this point we have
586 * np which is the start of the name string which is
588 * cp which is the start of the section string which is
590 * e is the 'next point after'.
591 * r and rr are the chars replaced by the '\0'
592 * rp and rrp is where 'r' and 'rr' came from.
594 p = _CONF_get_string(conf, cp, np);
599 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_HAS_NO_VALUE);
602 newsize = strlen(p) + buf->length - (e - from);
603 if (newsize > MAX_CONF_VALUE_LENGTH) {
604 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
607 if (!BUF_MEM_grow_clean(buf, newsize)) {
608 CONFerr(CONF_F_STR_COPY, ERR_R_MALLOC_FAILURE);
612 buf->data[to++] = *(p++);
615 * Since we change the pointer 'from', we also have to change the
616 * perceived length of the string it points at. /RL
622 * In case there were no braces or parenthesis around the
623 * variable reference, we have to put back the character that was
624 * replaced with a '\0'. /RL
628 buf->data[to++] = *(from++);
630 buf->data[to] = '\0';
640 #ifndef OPENSSL_NO_POSIX_IO
642 * Check whether included path is a directory.
643 * Returns next BIO to process and in case of a directory
644 * also an opened directory context and the include path.
646 static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
649 struct stat st = { 0 };
652 if (stat(include, &st) < 0) {
653 SYSerr(SYS_F_STAT, errno);
654 ERR_add_error_data(1, include);
655 /* missing include file is not fatal error */
659 if ((st.st_mode & S_IFDIR) == S_IFDIR) {
660 if (*dirctx != NULL) {
661 CONFerr(CONF_F_PROCESS_INCLUDE,
662 CONF_R_RECURSIVE_DIRECTORY_INCLUDE);
663 ERR_add_error_data(1, include);
666 /* a directory, load its contents */
667 if ((next = get_next_file(include, dirctx)) != NULL)
672 next = BIO_new_file(include, "r");
677 * Get next file from the directory path.
678 * Returns BIO of the next file to read and updates dirctx.
680 static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx)
682 const char *filename;
684 while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
687 namelen = strlen(filename);
690 if ((namelen > 5 && strcasecmp(filename + namelen - 5, ".conf") == 0)
691 || (namelen > 4 && strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
696 newlen = strlen(path) + namelen + 2;
697 newpath = OPENSSL_zalloc(newlen);
698 if (newpath == NULL) {
699 CONFerr(CONF_F_GET_NEXT_FILE, ERR_R_MALLOC_FAILURE);
702 #ifdef OPENSSL_SYS_VMS
704 * If the given path isn't clear VMS syntax,
705 * we treat it as on Unix.
708 size_t pathlen = strlen(path);
710 if (path[pathlen - 1] == ']' || path[pathlen - 1] == '>'
711 || path[pathlen - 1] == ':') {
712 /* Clear VMS directory syntax, just copy as is */
713 OPENSSL_strlcpy(newpath, path, newlen);
717 if (newpath[0] == '\0') {
718 OPENSSL_strlcpy(newpath, path, newlen);
719 OPENSSL_strlcat(newpath, "/", newlen);
721 OPENSSL_strlcat(newpath, filename, newlen);
723 bio = BIO_new_file(newpath, "r");
724 OPENSSL_free(newpath);
725 /* Errors when opening files are non-fatal. */
730 OPENSSL_DIR_end(dirctx);
736 static int is_keytype(const CONF *conf, char c, unsigned short type)
738 const unsigned short * keytypes = (const unsigned short *) conf->meth_data;
739 unsigned char key = (unsigned char)c;
741 #ifdef CHARSET_EBCDIC
744 /* key is out of range for os_toascii table */
748 /* convert key from ebcdic to ascii */
749 key = os_toascii[key];
753 /* key is not a seven bit ascii character */
757 return (keytypes[key] & type) ? 1 : 0;
760 static char *eat_ws(CONF *conf, char *p)
762 while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
767 static void trim_ws(CONF *conf, char *start)
771 while (!IS_EOF(conf, *p))
774 while ((p >= start) && IS_WS(conf, *p))
780 static char *eat_alpha_numeric(CONF *conf, char *p)
783 if (IS_ESC(conf, *p)) {
784 p = scan_esc(conf, p);
787 if (!IS_ALNUM_PUNCT(conf, *p))
793 static char *scan_quote(CONF *conf, char *p)
798 while (!(IS_EOF(conf, *p)) && (*p != q)) {
799 if (IS_ESC(conf, *p)) {
801 if (IS_EOF(conf, *p))
811 static char *scan_dquote(CONF *conf, char *p)
816 while (!(IS_EOF(conf, *p))) {
831 static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
834 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
836 BIO_printf(out, "[[%s]]\n", a->section);
839 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
841 static int def_dump(const CONF *conf, BIO *out)
843 lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
847 static int def_is_number(const CONF *conf, char c)
849 return IS_NUMBER(conf, c);
852 static int def_to_int(const CONF *conf, char c)