1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
57 #include <openssl/conf.h>
62 #include <openssl/bio.h>
63 #include <openssl/buf.h>
64 #include <openssl/err.h>
65 #include <openssl/mem.h>
69 #include "../internal.h"
72 // The maximum length we can grow a value to after variable expansion. 64k
73 // should be more than enough for all reasonable uses.
74 #define MAX_CONF_VALUE_LENGTH 65536
76 static uint32_t conf_value_hash(const CONF_VALUE *v) {
77 return (lh_strhash(v->section) << 2) ^ lh_strhash(v->name);
80 static int conf_value_cmp(const CONF_VALUE *a, const CONF_VALUE *b) {
83 if (a->section != b->section) {
84 i = strcmp(a->section, b->section);
90 if (a->name != NULL && b->name != NULL) {
91 return strcmp(a->name, b->name);
92 } else if (a->name == b->name) {
95 return (a->name == NULL) ? -1 : 1;
99 CONF *NCONF_new(void *method) {
102 if (method != NULL) {
106 conf = OPENSSL_malloc(sizeof(CONF));
111 conf->data = lh_CONF_VALUE_new(conf_value_hash, conf_value_cmp);
112 if (conf->data == NULL) {
120 CONF_VALUE *CONF_VALUE_new(void) {
121 CONF_VALUE *v = OPENSSL_malloc(sizeof(CONF_VALUE));
123 OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
126 OPENSSL_memset(v, 0, sizeof(CONF_VALUE));
130 static void value_free_contents(CONF_VALUE *value) {
131 if (value->section) {
132 OPENSSL_free(value->section);
135 OPENSSL_free(value->name);
137 OPENSSL_free(value->value);
141 sk_CONF_VALUE_free((STACK_OF(CONF_VALUE)*)value->value);
146 static void value_free(CONF_VALUE *value) {
147 value_free_contents(value);
151 void NCONF_free(CONF *conf) {
152 if (conf == NULL || conf->data == NULL) {
156 lh_CONF_VALUE_doall(conf->data, value_free);
157 lh_CONF_VALUE_free(conf->data);
161 static CONF_VALUE *NCONF_new_section(const CONF *conf, const char *section) {
162 STACK_OF(CONF_VALUE) *sk = NULL;
164 CONF_VALUE *v = NULL, *old_value;
166 sk = sk_CONF_VALUE_new_null();
167 v = CONF_VALUE_new();
168 if (sk == NULL || v == NULL) {
171 v->section = OPENSSL_strdup(section);
172 if (v->section == NULL) {
177 v->value = (char *)sk;
179 if (!lh_CONF_VALUE_insert(conf->data, &old_value, v)) {
183 value_free(old_value);
190 sk_CONF_VALUE_free(sk);
200 static int str_copy(CONF *conf, char *section, char **pto, char *from) {
201 int q, r, rr = 0, to = 0, len = 0;
202 char *s, *e, *rp, *rrp, *np, *cp, v;
211 len = strlen(from) + 1;
212 if (!BUF_MEM_grow(buf, len)) {
217 if (IS_QUOTE(conf, *from)) {
220 while (!IS_EOF(conf, *from) && (*from != q)) {
221 if (IS_ESC(conf, *from)) {
223 if (IS_EOF(conf, *from)) {
227 buf->data[to++] = *(from++);
232 } else if (IS_DQUOTE(conf, *from)) {
235 while (!IS_EOF(conf, *from)) {
237 if (*(from + 1) == q) {
243 buf->data[to++] = *(from++);
248 } else if (IS_ESC(conf, *from)) {
251 if (IS_EOF(conf, v)) {
253 } else if (v == 'r') {
255 } else if (v == 'n') {
257 } else if (v == 'b') {
259 } else if (v == 't') {
263 } else if (IS_EOF(conf, *from)) {
265 } else if (*from == '$') {
271 } else if (*s == '(') {
282 while (IS_ALPHA_NUMERIC(conf, *e)) {
285 if (e[0] == ':' && e[1] == ':') {
292 while (IS_ALPHA_NUMERIC(conf, *e)) {
301 OPENSSL_PUT_ERROR(CONF, CONF_R_NO_CLOSE_BRACE);
306 // So at this point we have
307 // np which is the start of the name string which is
309 // cp which is the start of the section string which is
311 // e is the 'next point after'.
312 // r and rr are the chars replaced by the '\0'
313 // rp and rrp is where 'r' and 'rr' came from.
314 p = NCONF_get_string(conf, cp, np);
320 OPENSSL_PUT_ERROR(CONF, CONF_R_VARIABLE_HAS_NO_VALUE);
323 size_t newsize = strlen(p) + buf->length - (e - from);
324 if (newsize > MAX_CONF_VALUE_LENGTH) {
325 OPENSSL_PUT_ERROR(CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
328 if (!BUF_MEM_grow_clean(buf, newsize)) {
329 OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
333 buf->data[to++] = *(p++);
336 /* Since we change the pointer 'from', we also have
337 to change the perceived length of the string it
342 /* In case there were no braces or parenthesis around
343 the variable reference, we have to put back the
344 character that was replaced with a '\0'. /RL */
347 buf->data[to++] = *(from++);
351 buf->data[to] = '\0';
366 static CONF_VALUE *get_section(const CONF *conf, const char *section) {
369 OPENSSL_memset(&template, 0, sizeof(template));
370 template.section = (char *) section;
371 return lh_CONF_VALUE_retrieve(conf->data, &template);
374 STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section) {
375 CONF_VALUE *section_value = get_section(conf, section);
376 if (section_value == NULL) {
379 return (STACK_OF(CONF_VALUE)*) section_value->value;
382 const char *NCONF_get_string(const CONF *conf, const char *section,
384 CONF_VALUE template, *value;
386 OPENSSL_memset(&template, 0, sizeof(template));
387 template.section = (char *) section;
388 template.name = (char *) name;
389 value = lh_CONF_VALUE_retrieve(conf->data, &template);
396 static int add_string(const CONF *conf, CONF_VALUE *section,
398 STACK_OF(CONF_VALUE) *section_stack = (STACK_OF(CONF_VALUE)*) section->value;
399 CONF_VALUE *old_value;
401 value->section = OPENSSL_strdup(section->section);
402 if (!sk_CONF_VALUE_push(section_stack, value)) {
406 if (!lh_CONF_VALUE_insert(conf->data, &old_value, value)) {
409 if (old_value != NULL) {
410 (void)sk_CONF_VALUE_delete_ptr(section_stack, old_value);
411 value_free(old_value);
417 static char *eat_ws(CONF *conf, char *p) {
418 while (IS_WS(conf, *p) && !IS_EOF(conf, *p)) {
424 #define scan_esc(conf, p) (((IS_EOF((conf), (p)[1])) ? ((p) + 1) : ((p) + 2)))
426 static char *eat_alpha_numeric(CONF *conf, char *p) {
428 if (IS_ESC(conf, *p)) {
429 p = scan_esc(conf, p);
432 if (!IS_ALPHA_NUMERIC_PUNCT(conf, *p)) {
439 static char *scan_quote(CONF *conf, char *p) {
443 while (!IS_EOF(conf, *p) && *p != q) {
444 if (IS_ESC(conf, *p)) {
446 if (IS_EOF(conf, *p)) {
459 static char *scan_dquote(CONF *conf, char *p) {
463 while (!(IS_EOF(conf, *p))) {
479 static void clear_comments(CONF *conf, char *p) {
481 if (IS_FCOMMENT(conf, *p)) {
485 if (!IS_WS(conf, *p)) {
492 if (IS_COMMENT(conf, *p)) {
496 if (IS_DQUOTE(conf, *p)) {
497 p = scan_dquote(conf, p);
500 if (IS_QUOTE(conf, *p)) {
501 p = scan_quote(conf, p);
504 if (IS_ESC(conf, *p)) {
505 p = scan_esc(conf, p);
508 if (IS_EOF(conf, *p)) {
516 static int def_load_bio(CONF *conf, BIO *in, long *out_error_line) {
517 static const size_t CONFBUFSIZE = 512;
518 int bufnum = 0, i, ii;
519 BUF_MEM *buff = NULL;
523 char btmp[DECIMAL_SIZE(eline) + 1];
524 CONF_VALUE *v = NULL, *tv;
525 CONF_VALUE *sv = NULL;
526 char *section = NULL, *buf;
527 char *start, *psection, *pname;
529 if ((buff = BUF_MEM_new()) == NULL) {
530 OPENSSL_PUT_ERROR(CONF, ERR_R_BUF_LIB);
534 section = OPENSSL_strdup("default");
535 if (section == NULL) {
536 OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
540 sv = NCONF_new_section(conf, section);
542 OPENSSL_PUT_ERROR(CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
549 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
550 OPENSSL_PUT_ERROR(CONF, ERR_R_BUF_LIB);
553 p = &(buff->data[bufnum]);
555 BIO_gets(in, p, CONFBUFSIZE - 1);
556 p[CONFBUFSIZE - 1] = '\0';
558 if (i == 0 && !again) {
563 if ((p[i - 1] != '\r') && (p[i - 1] != '\n')) {
569 // we removed some trailing stuff so there is a new
572 again = 1; // long line
575 eline++; // another input line
578 // we now have a line with trailing \r\n removed
580 // i is the number of bytes
584 // check for line continuation
586 // If we have bytes and the last char '\\' and
587 // second last char is not '\\'
588 p = &(buff->data[bufnum - 1]);
589 if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
600 clear_comments(conf, buf);
601 s = eat_ws(conf, buf);
602 if (IS_EOF(conf, *s)) {
603 continue; // blank line
609 start = eat_ws(conf, s);
612 end = eat_alpha_numeric(conf, ss);
613 p = eat_ws(conf, end);
615 if (*p != '\0' && ss != p) {
619 OPENSSL_PUT_ERROR(CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
623 if (!str_copy(conf, NULL, §ion, start)) {
626 if ((sv = get_section(conf, section)) == NULL) {
627 sv = NCONF_new_section(conf, section);
630 OPENSSL_PUT_ERROR(CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
637 end = eat_alpha_numeric(conf, s);
638 if ((end[0] == ':') && (end[1] == ':')) {
643 end = eat_alpha_numeric(conf, end);
645 p = eat_ws(conf, end);
647 OPENSSL_PUT_ERROR(CONF, CONF_R_MISSING_EQUAL_SIGN);
652 start = eat_ws(conf, p);
653 while (!IS_EOF(conf, *p)) {
657 while ((p != start) && (IS_WS(conf, *p))) {
663 if (!(v = CONF_VALUE_new())) {
666 if (psection == NULL) {
669 v->name = OPENSSL_strdup(pname);
670 if (v->name == NULL) {
671 OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
674 if (!str_copy(conf, psection, &(v->value), start)) {
678 if (strcmp(psection, section) != 0) {
679 if ((tv = get_section(conf, psection)) == NULL) {
680 tv = NCONF_new_section(conf, psection);
683 OPENSSL_PUT_ERROR(CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
689 if (add_string(conf, tv, v) == 0) {
690 OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
699 if (section != NULL) {
700 OPENSSL_free(section);
708 if (section != NULL) {
709 OPENSSL_free(section);
711 if (out_error_line != NULL) {
712 *out_error_line = eline;
714 BIO_snprintf(btmp, sizeof btmp, "%ld", eline);
715 ERR_add_error_data(2, "line ", btmp);
718 if (v->name != NULL) {
719 OPENSSL_free(v->name);
721 if (v->value != NULL) {
722 OPENSSL_free(v->value);
731 int NCONF_load(CONF *conf, const char *filename, long *out_error_line) {
732 BIO *in = BIO_new_file(filename, "rb");
736 OPENSSL_PUT_ERROR(CONF, ERR_R_SYS_LIB);
740 ret = def_load_bio(conf, in, out_error_line);
746 int NCONF_load_bio(CONF *conf, BIO *bio, long *out_error_line) {
747 return def_load_bio(conf, bio, out_error_line);
750 int CONF_parse_list(const char *list, char sep, int remove_whitespace,
751 int (*list_cb)(const char *elem, int len, void *usr),
754 const char *lstart, *tmpend, *p;
757 OPENSSL_PUT_ERROR(CONF, CONF_R_LIST_CANNOT_BE_NULL);
763 if (remove_whitespace) {
764 while (*lstart && isspace((unsigned char)*lstart)) {
768 p = strchr(lstart, sep);
769 if (p == lstart || !*lstart) {
770 ret = list_cb(NULL, 0, arg);
775 tmpend = lstart + strlen(lstart) - 1;
777 if (remove_whitespace) {
778 while (isspace((unsigned char)*tmpend)) {
782 ret = list_cb(lstart, tmpend - lstart + 1, arg);
794 int CONF_modules_load_file(const char *filename, const char *appname,
795 unsigned long flags) {
799 void CONF_modules_free(void) {}
801 void OPENSSL_config(const char *config_name) {}
803 void OPENSSL_no_config(void) {}