2 * Copyright 1995-2016 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/stack.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>
24 * The maximum length we can grow a value to after variable expansion. 64k
25 * should be more than enough for all reasonable uses.
27 #define MAX_CONF_VALUE_LENGTH 65536
29 static char *eat_ws(CONF *conf, char *p);
30 static char *eat_alpha_numeric(CONF *conf, char *p);
31 static void clear_comments(CONF *conf, char *p);
32 static int str_copy(CONF *conf, char *section, char **to, char *from);
33 static char *scan_quote(CONF *conf, char *p);
34 static char *scan_dquote(CONF *conf, char *p);
35 #define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
37 static CONF *def_create(CONF_METHOD *meth);
38 static int def_init_default(CONF *conf);
39 static int def_init_WIN32(CONF *conf);
40 static int def_destroy(CONF *conf);
41 static int def_destroy_data(CONF *conf);
42 static int def_load(CONF *conf, const char *name, long *eline);
43 static int def_load_bio(CONF *conf, BIO *bp, long *eline);
44 static int def_dump(const CONF *conf, BIO *bp);
45 static int def_is_number(const CONF *conf, char c);
46 static int def_to_int(const CONF *conf, char c);
48 static CONF_METHOD default_method = {
61 static CONF_METHOD WIN32_method = {
74 CONF_METHOD *NCONF_default()
76 return &default_method;
79 CONF_METHOD *NCONF_WIN32()
84 static CONF *def_create(CONF_METHOD *meth)
88 ret = OPENSSL_malloc(sizeof(*ret));
90 if (meth->init(ret) == 0) {
97 static int def_init_default(CONF *conf)
102 conf->meth = &default_method;
103 conf->meth_data = (void *)CONF_type_default;
109 static int def_init_WIN32(CONF *conf)
114 conf->meth = &WIN32_method;
115 conf->meth_data = (void *)CONF_type_win32;
121 static int def_destroy(CONF *conf)
123 if (def_destroy_data(conf)) {
130 static int def_destroy_data(CONF *conf)
134 _CONF_free_data(conf);
138 static int def_load(CONF *conf, const char *name, long *line)
143 #ifdef OPENSSL_SYS_VMS
144 in = BIO_new_file(name, "r");
146 in = BIO_new_file(name, "rb");
149 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
150 CONFerr(CONF_F_DEF_LOAD, CONF_R_NO_SUCH_FILE);
152 CONFerr(CONF_F_DEF_LOAD, ERR_R_SYS_LIB);
156 ret = def_load_bio(conf, in, line);
162 static int def_load_bio(CONF *conf, BIO *in, long *line)
164 /* The macro BUFSIZE conflicts with a system macro in VxWorks */
165 #define CONFBUFSIZE 512
166 int bufnum = 0, i, ii;
167 BUF_MEM *buff = NULL;
171 char btmp[DECIMAL_SIZE(eline) + 1];
172 CONF_VALUE *v = NULL, *tv;
173 CONF_VALUE *sv = NULL;
174 char *section = NULL, *buf;
175 char *start, *psection, *pname;
176 void *h = (void *)(conf->data);
178 if ((buff = BUF_MEM_new()) == NULL) {
179 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
183 section = OPENSSL_strdup("default");
184 if (section == NULL) {
185 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
189 if (_CONF_new_data(conf) == 0) {
190 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
194 sv = _CONF_new_section(conf, section);
196 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
203 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
204 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
207 p = &(buff->data[bufnum]);
209 BIO_gets(in, p, CONFBUFSIZE - 1);
210 p[CONFBUFSIZE - 1] = '\0';
212 if (i == 0 && !again)
216 if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
222 * we removed some trailing stuff so there is a new line on the end.
225 again = 1; /* long line */
228 eline++; /* another input line */
231 /* we now have a line with trailing \r\n removed */
233 /* i is the number of bytes */
237 /* check for line continuation */
240 * If we have bytes and the last char '\\' and second last char
243 p = &(buff->data[bufnum - 1]);
244 if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
254 clear_comments(conf, buf);
255 s = eat_ws(conf, buf);
256 if (IS_EOF(conf, *s))
257 continue; /* blank line */
262 start = eat_ws(conf, s);
265 end = eat_alpha_numeric(conf, ss);
266 p = eat_ws(conf, end);
268 if (*p != '\0' && ss != p) {
272 CONFerr(CONF_F_DEF_LOAD_BIO,
273 CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
277 if (!str_copy(conf, NULL, §ion, start))
279 if ((sv = _CONF_get_section(conf, section)) == NULL)
280 sv = _CONF_new_section(conf, section);
282 CONFerr(CONF_F_DEF_LOAD_BIO,
283 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
290 end = eat_alpha_numeric(conf, s);
291 if ((end[0] == ':') && (end[1] == ':')) {
296 end = eat_alpha_numeric(conf, end);
298 p = eat_ws(conf, end);
300 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);
305 start = eat_ws(conf, p);
306 while (!IS_EOF(conf, *p))
309 while ((p != start) && (IS_WS(conf, *p)))
314 if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
315 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
318 if (psection == NULL)
320 v->name = OPENSSL_malloc(strlen(pname) + 1);
322 if (v->name == NULL) {
323 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
326 OPENSSL_strlcpy(v->name, pname, strlen(pname) + 1);
327 if (!str_copy(conf, psection, &(v->value), start))
330 if (strcmp(psection, section) != 0) {
331 if ((tv = _CONF_get_section(conf, psection))
333 tv = _CONF_new_section(conf, psection);
335 CONFerr(CONF_F_DEF_LOAD_BIO,
336 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
341 if (_CONF_add_string(conf, tv, v) == 0) {
342 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
349 OPENSSL_free(section);
353 OPENSSL_free(section);
356 BIO_snprintf(btmp, sizeof btmp, "%ld", eline);
357 ERR_add_error_data(2, "line ", btmp);
358 if (h != conf->data) {
359 CONF_free(conf->data);
363 OPENSSL_free(v->name);
364 OPENSSL_free(v->value);
370 static void clear_comments(CONF *conf, char *p)
373 if (IS_FCOMMENT(conf, *p)) {
377 if (!IS_WS(conf, *p)) {
384 if (IS_COMMENT(conf, *p)) {
388 if (IS_DQUOTE(conf, *p)) {
389 p = scan_dquote(conf, p);
392 if (IS_QUOTE(conf, *p)) {
393 p = scan_quote(conf, p);
396 if (IS_ESC(conf, *p)) {
397 p = scan_esc(conf, p);
400 if (IS_EOF(conf, *p))
407 static int str_copy(CONF *conf, char *section, char **pto, char *from)
409 int q, r, rr = 0, to = 0, len = 0;
410 char *s, *e, *rp, *p, *rrp, *np, *cp, v;
413 if ((buf = BUF_MEM_new()) == NULL)
416 len = strlen(from) + 1;
417 if (!BUF_MEM_grow(buf, len))
421 if (IS_QUOTE(conf, *from)) {
424 while (!IS_EOF(conf, *from) && (*from != q)) {
425 if (IS_ESC(conf, *from)) {
427 if (IS_EOF(conf, *from))
430 buf->data[to++] = *(from++);
434 } else if (IS_DQUOTE(conf, *from)) {
437 while (!IS_EOF(conf, *from)) {
439 if (*(from + 1) == q) {
445 buf->data[to++] = *(from++);
449 } else if (IS_ESC(conf, *from)) {
463 } else if (IS_EOF(conf, *from))
465 else if (*from == '$') {
468 /* try to expand it */
482 while (IS_ALPHA_NUMERIC(conf, *e))
484 if ((e[0] == ':') && (e[1] == ':')) {
491 while (IS_ALPHA_NUMERIC(conf, *e))
499 CONFerr(CONF_F_STR_COPY, CONF_R_NO_CLOSE_BRACE);
505 * So at this point we have
506 * np which is the start of the name string which is
508 * cp which is the start of the section string which is
510 * e is the 'next point after'.
511 * r and rr are the chars replaced by the '\0'
512 * rp and rrp is where 'r' and 'rr' came from.
514 p = _CONF_get_string(conf, cp, np);
519 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_HAS_NO_VALUE);
522 newsize = strlen(p) + buf->length - (e - from);
523 if (newsize > MAX_CONF_VALUE_LENGTH) {
524 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
527 if (!BUF_MEM_grow_clean(buf, newsize)) {
528 CONFerr(CONF_F_STR_COPY, ERR_R_MALLOC_FAILURE);
532 buf->data[to++] = *(p++);
535 * Since we change the pointer 'from', we also have to change the
536 * perceived length of the string it points at. /RL
542 * In case there were no braces or parenthesis around the
543 * variable reference, we have to put back the character that was
544 * replaced with a '\0'. /RL
548 buf->data[to++] = *(from++);
550 buf->data[to] = '\0';
560 static char *eat_ws(CONF *conf, char *p)
562 while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
567 static char *eat_alpha_numeric(CONF *conf, char *p)
570 if (IS_ESC(conf, *p)) {
571 p = scan_esc(conf, p);
574 if (!IS_ALPHA_NUMERIC_PUNCT(conf, *p))
580 static char *scan_quote(CONF *conf, char *p)
585 while (!(IS_EOF(conf, *p)) && (*p != q)) {
586 if (IS_ESC(conf, *p)) {
588 if (IS_EOF(conf, *p))
598 static char *scan_dquote(CONF *conf, char *p)
603 while (!(IS_EOF(conf, *p))) {
618 static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
621 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
623 BIO_printf(out, "[[%s]]\n", a->section);
626 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
628 static int def_dump(const CONF *conf, BIO *out)
630 lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
634 static int def_is_number(const CONF *conf, char c)
636 return IS_NUMBER(conf, c);
639 static int def_to_int(const CONF *conf, char c)