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.]
58 /* Part of the code in here was originally in conf.c, which is now removed */
62 #include "internal/cryptlib.h"
63 #include <openssl/stack.h>
64 #include <openssl/lhash.h>
65 #include <openssl/conf.h>
66 #include <openssl/conf_api.h>
68 #include <openssl/buffer.h>
69 #include <openssl/err.h>
71 static char *eat_ws(CONF *conf, char *p);
72 static char *eat_alpha_numeric(CONF *conf, char *p);
73 static void clear_comments(CONF *conf, char *p);
74 static int str_copy(CONF *conf, char *section, char **to, char *from);
75 static char *scan_quote(CONF *conf, char *p);
76 static char *scan_dquote(CONF *conf, char *p);
77 #define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
79 static CONF *def_create(CONF_METHOD *meth);
80 static int def_init_default(CONF *conf);
81 static int def_init_WIN32(CONF *conf);
82 static int def_destroy(CONF *conf);
83 static int def_destroy_data(CONF *conf);
84 static int def_load(CONF *conf, const char *name, long *eline);
85 static int def_load_bio(CONF *conf, BIO *bp, long *eline);
86 static int def_dump(const CONF *conf, BIO *bp);
87 static int def_is_number(const CONF *conf, char c);
88 static int def_to_int(const CONF *conf, char c);
90 static CONF_METHOD default_method = {
103 static CONF_METHOD WIN32_method = {
116 CONF_METHOD *NCONF_default()
118 return &default_method;
121 CONF_METHOD *NCONF_WIN32()
123 return &WIN32_method;
126 static CONF *def_create(CONF_METHOD *meth)
130 ret = OPENSSL_malloc(sizeof(*ret));
132 if (meth->init(ret) == 0) {
139 static int def_init_default(CONF *conf)
144 conf->meth = &default_method;
145 conf->meth_data = (void *)CONF_type_default;
151 static int def_init_WIN32(CONF *conf)
156 conf->meth = &WIN32_method;
157 conf->meth_data = (void *)CONF_type_win32;
163 static int def_destroy(CONF *conf)
165 if (def_destroy_data(conf)) {
172 static int def_destroy_data(CONF *conf)
176 _CONF_free_data(conf);
180 static int def_load(CONF *conf, const char *name, long *line)
185 #ifdef OPENSSL_SYS_VMS
186 in = BIO_new_file(name, "r");
188 in = BIO_new_file(name, "rb");
191 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
192 CONFerr(CONF_F_DEF_LOAD, CONF_R_NO_SUCH_FILE);
194 CONFerr(CONF_F_DEF_LOAD, ERR_R_SYS_LIB);
198 ret = def_load_bio(conf, in, line);
204 static int def_load_bio(CONF *conf, BIO *in, long *line)
206 /* The macro BUFSIZE conflicts with a system macro in VxWorks */
207 #define CONFBUFSIZE 512
208 int bufnum = 0, i, ii;
209 BUF_MEM *buff = NULL;
213 char btmp[DECIMAL_SIZE(eline) + 1];
214 CONF_VALUE *v = NULL, *tv;
215 CONF_VALUE *sv = NULL;
216 char *section = NULL, *buf;
217 char *start, *psection, *pname;
218 void *h = (void *)(conf->data);
220 if ((buff = BUF_MEM_new()) == NULL) {
221 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
225 section = OPENSSL_strdup("default");
226 if (section == NULL) {
227 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
231 if (_CONF_new_data(conf) == 0) {
232 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
236 sv = _CONF_new_section(conf, section);
238 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
245 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
246 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
249 p = &(buff->data[bufnum]);
251 BIO_gets(in, p, CONFBUFSIZE - 1);
252 p[CONFBUFSIZE - 1] = '\0';
254 if (i == 0 && !again)
258 if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
264 * we removed some trailing stuff so there is a new line on the end.
267 again = 1; /* long line */
270 eline++; /* another input line */
273 /* we now have a line with trailing \r\n removed */
275 /* i is the number of bytes */
279 /* check for line continuation */
282 * If we have bytes and the last char '\\' and second last char
285 p = &(buff->data[bufnum - 1]);
286 if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
296 clear_comments(conf, buf);
297 s = eat_ws(conf, buf);
298 if (IS_EOF(conf, *s))
299 continue; /* blank line */
304 start = eat_ws(conf, s);
307 end = eat_alpha_numeric(conf, ss);
308 p = eat_ws(conf, end);
310 if (*p != '\0' && ss != p) {
314 CONFerr(CONF_F_DEF_LOAD_BIO,
315 CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
319 if (!str_copy(conf, NULL, §ion, start))
321 if ((sv = _CONF_get_section(conf, section)) == NULL)
322 sv = _CONF_new_section(conf, section);
324 CONFerr(CONF_F_DEF_LOAD_BIO,
325 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
332 end = eat_alpha_numeric(conf, s);
333 if ((end[0] == ':') && (end[1] == ':')) {
338 end = eat_alpha_numeric(conf, end);
340 p = eat_ws(conf, end);
342 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);
347 start = eat_ws(conf, p);
348 while (!IS_EOF(conf, *p))
351 while ((p != start) && (IS_WS(conf, *p)))
356 if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
357 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
360 if (psection == NULL)
362 v->name = OPENSSL_malloc(strlen(pname) + 1);
364 if (v->name == NULL) {
365 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
368 OPENSSL_strlcpy(v->name, pname, strlen(pname) + 1);
369 if (!str_copy(conf, psection, &(v->value), start))
372 if (strcmp(psection, section) != 0) {
373 if ((tv = _CONF_get_section(conf, psection))
375 tv = _CONF_new_section(conf, psection);
377 CONFerr(CONF_F_DEF_LOAD_BIO,
378 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
383 if (_CONF_add_string(conf, tv, v) == 0) {
384 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
391 OPENSSL_free(section);
395 OPENSSL_free(section);
398 BIO_snprintf(btmp, sizeof btmp, "%ld", eline);
399 ERR_add_error_data(2, "line ", btmp);
400 if (h != conf->data) {
401 CONF_free(conf->data);
405 OPENSSL_free(v->name);
406 OPENSSL_free(v->value);
412 static void clear_comments(CONF *conf, char *p)
415 if (IS_FCOMMENT(conf, *p)) {
419 if (!IS_WS(conf, *p)) {
426 if (IS_COMMENT(conf, *p)) {
430 if (IS_DQUOTE(conf, *p)) {
431 p = scan_dquote(conf, p);
434 if (IS_QUOTE(conf, *p)) {
435 p = scan_quote(conf, p);
438 if (IS_ESC(conf, *p)) {
439 p = scan_esc(conf, p);
442 if (IS_EOF(conf, *p))
449 static int str_copy(CONF *conf, char *section, char **pto, char *from)
451 int q, r, rr = 0, to = 0, len = 0;
452 char *s, *e, *rp, *p, *rrp, *np, *cp, v;
455 if ((buf = BUF_MEM_new()) == NULL)
458 len = strlen(from) + 1;
459 if (!BUF_MEM_grow(buf, len))
463 if (IS_QUOTE(conf, *from)) {
466 while (!IS_EOF(conf, *from) && (*from != q)) {
467 if (IS_ESC(conf, *from)) {
469 if (IS_EOF(conf, *from))
472 buf->data[to++] = *(from++);
476 } else if (IS_DQUOTE(conf, *from)) {
479 while (!IS_EOF(conf, *from)) {
481 if (*(from + 1) == q) {
487 buf->data[to++] = *(from++);
491 } else if (IS_ESC(conf, *from)) {
505 } else if (IS_EOF(conf, *from))
507 else if (*from == '$') {
508 /* try to expand it */
522 while (IS_ALPHA_NUMERIC(conf, *e))
524 if ((e[0] == ':') && (e[1] == ':')) {
531 while (IS_ALPHA_NUMERIC(conf, *e))
539 CONFerr(CONF_F_STR_COPY, CONF_R_NO_CLOSE_BRACE);
545 * So at this point we have
546 * np which is the start of the name string which is
548 * cp which is the start of the section string which is
550 * e is the 'next point after'.
551 * r and rr are the chars replaced by the '\0'
552 * rp and rrp is where 'r' and 'rr' came from.
554 p = _CONF_get_string(conf, cp, np);
559 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_HAS_NO_VALUE);
562 if (!BUF_MEM_grow_clean(buf,
563 (strlen(p) + buf->length - (e - from)))) {
564 CONFerr(CONF_F_STR_COPY, ERR_R_MALLOC_FAILURE);
568 buf->data[to++] = *(p++);
571 * Since we change the pointer 'from', we also have to change the
572 * perceived length of the string it points at. /RL
578 * In case there were no braces or parenthesis around the
579 * variable reference, we have to put back the character that was
580 * replaced with a '\0'. /RL
584 buf->data[to++] = *(from++);
586 buf->data[to] = '\0';
596 static char *eat_ws(CONF *conf, char *p)
598 while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
603 static char *eat_alpha_numeric(CONF *conf, char *p)
606 if (IS_ESC(conf, *p)) {
607 p = scan_esc(conf, p);
610 if (!IS_ALPHA_NUMERIC_PUNCT(conf, *p))
616 static char *scan_quote(CONF *conf, char *p)
621 while (!(IS_EOF(conf, *p)) && (*p != q)) {
622 if (IS_ESC(conf, *p)) {
624 if (IS_EOF(conf, *p))
634 static char *scan_dquote(CONF *conf, char *p)
639 while (!(IS_EOF(conf, *p))) {
654 static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
657 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
659 BIO_printf(out, "[[%s]]\n", a->section);
662 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
664 static int def_dump(const CONF *conf, BIO *out)
666 lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
670 static int def_is_number(const CONF *conf, char c)
672 return IS_NUMBER(conf, c);
675 static int def_to_int(const CONF *conf, char c)