2 * Copyright 1995-2017 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 <openssl/lhash.h>
16 #include <openssl/conf.h>
17 #include <openssl/conf_api.h>
19 #include <openssl/buffer.h>
20 #include <openssl/err.h>
23 * The maximum length we can grow a value to after variable expansion. 64k
24 * should be more than enough for all reasonable uses.
26 #define MAX_CONF_VALUE_LENGTH 65536
28 static char *eat_ws(CONF *conf, char *p);
29 static char *eat_alpha_numeric(CONF *conf, char *p);
30 static void clear_comments(CONF *conf, char *p);
31 static int str_copy(CONF *conf, char *section, char **to, char *from);
32 static char *scan_quote(CONF *conf, char *p);
33 static char *scan_dquote(CONF *conf, char *p);
34 #define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
36 static CONF *def_create(CONF_METHOD *meth);
37 static int def_init_default(CONF *conf);
38 static int def_init_WIN32(CONF *conf);
39 static int def_destroy(CONF *conf);
40 static int def_destroy_data(CONF *conf);
41 static int def_load(CONF *conf, const char *name, long *eline);
42 static int def_load_bio(CONF *conf, BIO *bp, long *eline);
43 static int def_dump(const CONF *conf, BIO *bp);
44 static int def_is_number(const CONF *conf, char c);
45 static int def_to_int(const CONF *conf, char c);
47 static CONF_METHOD default_method = {
60 static CONF_METHOD WIN32_method = {
73 CONF_METHOD *NCONF_default()
75 return &default_method;
78 CONF_METHOD *NCONF_WIN32()
83 static CONF *def_create(CONF_METHOD *meth)
87 ret = OPENSSL_malloc(sizeof(*ret));
89 if (meth->init(ret) == 0) {
96 static int def_init_default(CONF *conf)
101 conf->meth = &default_method;
102 conf->meth_data = (void *)CONF_type_default;
108 static int def_init_WIN32(CONF *conf)
113 conf->meth = &WIN32_method;
114 conf->meth_data = (void *)CONF_type_win32;
120 static int def_destroy(CONF *conf)
122 if (def_destroy_data(conf)) {
129 static int def_destroy_data(CONF *conf)
133 _CONF_free_data(conf);
137 static int def_load(CONF *conf, const char *name, long *line)
142 #ifdef OPENSSL_SYS_VMS
143 in = BIO_new_file(name, "r");
145 in = BIO_new_file(name, "rb");
148 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
149 CONFerr(CONF_F_DEF_LOAD, CONF_R_NO_SUCH_FILE);
151 CONFerr(CONF_F_DEF_LOAD, ERR_R_SYS_LIB);
155 ret = def_load_bio(conf, in, line);
161 static int def_load_bio(CONF *conf, BIO *in, long *line)
163 /* The macro BUFSIZE conflicts with a system macro in VxWorks */
164 #define CONFBUFSIZE 512
165 int bufnum = 0, i, ii;
166 BUF_MEM *buff = NULL;
170 char btmp[DECIMAL_SIZE(eline) + 1];
171 CONF_VALUE *v = NULL, *tv;
172 CONF_VALUE *sv = NULL;
173 char *section = NULL, *buf;
174 char *start, *psection, *pname;
175 void *h = (void *)(conf->data);
177 if ((buff = BUF_MEM_new()) == NULL) {
178 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
182 section = OPENSSL_strdup("default");
183 if (section == NULL) {
184 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
188 if (_CONF_new_data(conf) == 0) {
189 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
193 sv = _CONF_new_section(conf, section);
195 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
202 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
203 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
206 p = &(buff->data[bufnum]);
208 BIO_gets(in, p, CONFBUFSIZE - 1);
209 p[CONFBUFSIZE - 1] = '\0';
211 if (i == 0 && !again)
215 if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
221 * we removed some trailing stuff so there is a new line on the end.
224 again = 1; /* long line */
227 eline++; /* another input line */
230 /* we now have a line with trailing \r\n removed */
232 /* i is the number of bytes */
236 /* check for line continuation */
239 * If we have bytes and the last char '\\' and second last char
242 p = &(buff->data[bufnum - 1]);
243 if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
253 clear_comments(conf, buf);
254 s = eat_ws(conf, buf);
255 if (IS_EOF(conf, *s))
256 continue; /* blank line */
261 start = eat_ws(conf, s);
264 end = eat_alpha_numeric(conf, ss);
265 p = eat_ws(conf, end);
267 if (*p != '\0' && ss != p) {
271 CONFerr(CONF_F_DEF_LOAD_BIO,
272 CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
276 if (!str_copy(conf, NULL, §ion, start))
278 if ((sv = _CONF_get_section(conf, section)) == NULL)
279 sv = _CONF_new_section(conf, section);
281 CONFerr(CONF_F_DEF_LOAD_BIO,
282 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
289 end = eat_alpha_numeric(conf, s);
290 if ((end[0] == ':') && (end[1] == ':')) {
295 end = eat_alpha_numeric(conf, end);
297 p = eat_ws(conf, end);
299 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);
304 start = eat_ws(conf, p);
305 while (!IS_EOF(conf, *p))
308 while ((p != start) && (IS_WS(conf, *p)))
313 if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
314 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
317 if (psection == NULL)
319 v->name = OPENSSL_strdup(pname);
321 if (v->name == NULL) {
322 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
325 if (!str_copy(conf, psection, &(v->value), start))
328 if (strcmp(psection, section) != 0) {
329 if ((tv = _CONF_get_section(conf, psection))
331 tv = _CONF_new_section(conf, psection);
333 CONFerr(CONF_F_DEF_LOAD_BIO,
334 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
339 if (_CONF_add_string(conf, tv, v) == 0) {
340 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
347 OPENSSL_free(section);
351 OPENSSL_free(section);
354 BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
355 ERR_add_error_data(2, "line ", btmp);
356 if (h != conf->data) {
357 CONF_free(conf->data);
361 OPENSSL_free(v->name);
362 OPENSSL_free(v->value);
368 static void clear_comments(CONF *conf, char *p)
371 if (IS_FCOMMENT(conf, *p)) {
375 if (!IS_WS(conf, *p)) {
382 if (IS_COMMENT(conf, *p)) {
386 if (IS_DQUOTE(conf, *p)) {
387 p = scan_dquote(conf, p);
390 if (IS_QUOTE(conf, *p)) {
391 p = scan_quote(conf, p);
394 if (IS_ESC(conf, *p)) {
395 p = scan_esc(conf, p);
398 if (IS_EOF(conf, *p))
405 static int str_copy(CONF *conf, char *section, char **pto, char *from)
407 int q, r, rr = 0, to = 0, len = 0;
408 char *s, *e, *rp, *p, *rrp, *np, *cp, v;
411 if ((buf = BUF_MEM_new()) == NULL)
414 len = strlen(from) + 1;
415 if (!BUF_MEM_grow(buf, len))
419 if (IS_QUOTE(conf, *from)) {
422 while (!IS_EOF(conf, *from) && (*from != q)) {
423 if (IS_ESC(conf, *from)) {
425 if (IS_EOF(conf, *from))
428 buf->data[to++] = *(from++);
432 } else if (IS_DQUOTE(conf, *from)) {
435 while (!IS_EOF(conf, *from)) {
437 if (*(from + 1) == q) {
443 buf->data[to++] = *(from++);
447 } else if (IS_ESC(conf, *from)) {
461 } else if (IS_EOF(conf, *from))
463 else if (*from == '$') {
466 /* try to expand it */
480 while (IS_ALPHA_NUMERIC(conf, *e))
482 if ((e[0] == ':') && (e[1] == ':')) {
489 while (IS_ALPHA_NUMERIC(conf, *e))
497 CONFerr(CONF_F_STR_COPY, CONF_R_NO_CLOSE_BRACE);
503 * So at this point we have
504 * np which is the start of the name string which is
506 * cp which is the start of the section string which is
508 * e is the 'next point after'.
509 * r and rr are the chars replaced by the '\0'
510 * rp and rrp is where 'r' and 'rr' came from.
512 p = _CONF_get_string(conf, cp, np);
517 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_HAS_NO_VALUE);
520 newsize = strlen(p) + buf->length - (e - from);
521 if (newsize > MAX_CONF_VALUE_LENGTH) {
522 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
525 if (!BUF_MEM_grow_clean(buf, newsize)) {
526 CONFerr(CONF_F_STR_COPY, ERR_R_MALLOC_FAILURE);
530 buf->data[to++] = *(p++);
533 * Since we change the pointer 'from', we also have to change the
534 * perceived length of the string it points at. /RL
540 * In case there were no braces or parenthesis around the
541 * variable reference, we have to put back the character that was
542 * replaced with a '\0'. /RL
546 buf->data[to++] = *(from++);
548 buf->data[to] = '\0';
558 static char *eat_ws(CONF *conf, char *p)
560 while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
565 static char *eat_alpha_numeric(CONF *conf, char *p)
568 if (IS_ESC(conf, *p)) {
569 p = scan_esc(conf, p);
572 if (!IS_ALPHA_NUMERIC_PUNCT(conf, *p))
578 static char *scan_quote(CONF *conf, char *p)
583 while (!(IS_EOF(conf, *p)) && (*p != q)) {
584 if (IS_ESC(conf, *p)) {
586 if (IS_EOF(conf, *p))
596 static char *scan_dquote(CONF *conf, char *p)
601 while (!(IS_EOF(conf, *p))) {
616 static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
619 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
621 BIO_printf(out, "[[%s]]\n", a->section);
624 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
626 static int def_dump(const CONF *conf, BIO *out)
628 lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
632 static int def_is_number(const CONF *conf, char c)
634 return IS_NUMBER(conf, c);
637 static int def_to_int(const CONF *conf, char c)