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>
23 static char *eat_ws(CONF
*conf
, char *p
);
24 static char *eat_alpha_numeric(CONF
*conf
, char *p
);
25 static void clear_comments(CONF
*conf
, char *p
);
26 static int str_copy(CONF
*conf
, char *section
, char **to
, char *from
);
27 static char *scan_quote(CONF
*conf
, char *p
);
28 static char *scan_dquote(CONF
*conf
, char *p
);
29 #define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
31 static CONF
*def_create(CONF_METHOD
*meth
);
32 static int def_init_default(CONF
*conf
);
33 static int def_init_WIN32(CONF
*conf
);
34 static int def_destroy(CONF
*conf
);
35 static int def_destroy_data(CONF
*conf
);
36 static int def_load(CONF
*conf
, const char *name
, long *eline
);
37 static int def_load_bio(CONF
*conf
, BIO
*bp
, long *eline
);
38 static int def_dump(const CONF
*conf
, BIO
*bp
);
39 static int def_is_number(const CONF
*conf
, char c
);
40 static int def_to_int(const CONF
*conf
, char c
);
42 static CONF_METHOD default_method
= {
55 static CONF_METHOD WIN32_method
= {
68 CONF_METHOD
*NCONF_default()
70 return &default_method
;
73 CONF_METHOD
*NCONF_WIN32()
78 static CONF
*def_create(CONF_METHOD
*meth
)
82 ret
= OPENSSL_malloc(sizeof(*ret
));
84 if (meth
->init(ret
) == 0) {
91 static int def_init_default(CONF
*conf
)
96 conf
->meth
= &default_method
;
97 conf
->meth_data
= (void *)CONF_type_default
;
103 static int def_init_WIN32(CONF
*conf
)
108 conf
->meth
= &WIN32_method
;
109 conf
->meth_data
= (void *)CONF_type_win32
;
115 static int def_destroy(CONF
*conf
)
117 if (def_destroy_data(conf
)) {
124 static int def_destroy_data(CONF
*conf
)
128 _CONF_free_data(conf
);
132 static int def_load(CONF
*conf
, const char *name
, long *line
)
137 #ifdef OPENSSL_SYS_VMS
138 in
= BIO_new_file(name
, "r");
140 in
= BIO_new_file(name
, "rb");
143 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE
)
144 CONFerr(CONF_F_DEF_LOAD
, CONF_R_NO_SUCH_FILE
);
146 CONFerr(CONF_F_DEF_LOAD
, ERR_R_SYS_LIB
);
150 ret
= def_load_bio(conf
, in
, line
);
156 static int def_load_bio(CONF
*conf
, BIO
*in
, long *line
)
158 /* The macro BUFSIZE conflicts with a system macro in VxWorks */
159 #define CONFBUFSIZE 512
160 int bufnum
= 0, i
, ii
;
161 BUF_MEM
*buff
= NULL
;
165 char btmp
[DECIMAL_SIZE(eline
) + 1];
166 CONF_VALUE
*v
= NULL
, *tv
;
167 CONF_VALUE
*sv
= NULL
;
168 char *section
= NULL
, *buf
;
169 char *start
, *psection
, *pname
;
170 void *h
= (void *)(conf
->data
);
172 if ((buff
= BUF_MEM_new()) == NULL
) {
173 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_BUF_LIB
);
177 section
= OPENSSL_strdup("default");
178 if (section
== NULL
) {
179 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
183 if (_CONF_new_data(conf
) == 0) {
184 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
188 sv
= _CONF_new_section(conf
, section
);
190 CONFerr(CONF_F_DEF_LOAD_BIO
, CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
197 if (!BUF_MEM_grow(buff
, bufnum
+ CONFBUFSIZE
)) {
198 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_BUF_LIB
);
201 p
= &(buff
->data
[bufnum
]);
203 BIO_gets(in
, p
, CONFBUFSIZE
- 1);
204 p
[CONFBUFSIZE
- 1] = '\0';
206 if (i
== 0 && !again
)
210 if ((p
[i
- 1] != '\r') && (p
[i
- 1] != '\n'))
216 * we removed some trailing stuff so there is a new line on the end.
219 again
= 1; /* long line */
222 eline
++; /* another input line */
225 /* we now have a line with trailing \r\n removed */
227 /* i is the number of bytes */
231 /* check for line continuation */
234 * If we have bytes and the last char '\\' and second last char
237 p
= &(buff
->data
[bufnum
- 1]);
238 if (IS_ESC(conf
, p
[0]) && ((bufnum
<= 1) || !IS_ESC(conf
, p
[-1]))) {
248 clear_comments(conf
, buf
);
249 s
= eat_ws(conf
, buf
);
250 if (IS_EOF(conf
, *s
))
251 continue; /* blank line */
256 start
= eat_ws(conf
, s
);
259 end
= eat_alpha_numeric(conf
, ss
);
260 p
= eat_ws(conf
, end
);
262 if (*p
!= '\0' && ss
!= p
) {
266 CONFerr(CONF_F_DEF_LOAD_BIO
,
267 CONF_R_MISSING_CLOSE_SQUARE_BRACKET
);
271 if (!str_copy(conf
, NULL
, §ion
, start
))
273 if ((sv
= _CONF_get_section(conf
, section
)) == NULL
)
274 sv
= _CONF_new_section(conf
, section
);
276 CONFerr(CONF_F_DEF_LOAD_BIO
,
277 CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
284 end
= eat_alpha_numeric(conf
, s
);
285 if ((end
[0] == ':') && (end
[1] == ':')) {
290 end
= eat_alpha_numeric(conf
, end
);
292 p
= eat_ws(conf
, end
);
294 CONFerr(CONF_F_DEF_LOAD_BIO
, CONF_R_MISSING_EQUAL_SIGN
);
299 start
= eat_ws(conf
, p
);
300 while (!IS_EOF(conf
, *p
))
303 while ((p
!= start
) && (IS_WS(conf
, *p
)))
308 if ((v
= OPENSSL_malloc(sizeof(*v
))) == NULL
) {
309 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
312 if (psection
== NULL
)
314 v
->name
= OPENSSL_malloc(strlen(pname
) + 1);
316 if (v
->name
== NULL
) {
317 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
320 OPENSSL_strlcpy(v
->name
, pname
, strlen(pname
) + 1);
321 if (!str_copy(conf
, psection
, &(v
->value
), start
))
324 if (strcmp(psection
, section
) != 0) {
325 if ((tv
= _CONF_get_section(conf
, psection
))
327 tv
= _CONF_new_section(conf
, psection
);
329 CONFerr(CONF_F_DEF_LOAD_BIO
,
330 CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
335 if (_CONF_add_string(conf
, tv
, v
) == 0) {
336 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
343 OPENSSL_free(section
);
347 OPENSSL_free(section
);
350 BIO_snprintf(btmp
, sizeof btmp
, "%ld", eline
);
351 ERR_add_error_data(2, "line ", btmp
);
352 if (h
!= conf
->data
) {
353 CONF_free(conf
->data
);
357 OPENSSL_free(v
->name
);
358 OPENSSL_free(v
->value
);
364 static void clear_comments(CONF
*conf
, char *p
)
367 if (IS_FCOMMENT(conf
, *p
)) {
371 if (!IS_WS(conf
, *p
)) {
378 if (IS_COMMENT(conf
, *p
)) {
382 if (IS_DQUOTE(conf
, *p
)) {
383 p
= scan_dquote(conf
, p
);
386 if (IS_QUOTE(conf
, *p
)) {
387 p
= scan_quote(conf
, p
);
390 if (IS_ESC(conf
, *p
)) {
391 p
= scan_esc(conf
, p
);
394 if (IS_EOF(conf
, *p
))
401 static int str_copy(CONF
*conf
, char *section
, char **pto
, char *from
)
403 int q
, r
, rr
= 0, to
= 0, len
= 0;
404 char *s
, *e
, *rp
, *p
, *rrp
, *np
, *cp
, v
;
407 if ((buf
= BUF_MEM_new()) == NULL
)
410 len
= strlen(from
) + 1;
411 if (!BUF_MEM_grow(buf
, len
))
415 if (IS_QUOTE(conf
, *from
)) {
418 while (!IS_EOF(conf
, *from
) && (*from
!= q
)) {
419 if (IS_ESC(conf
, *from
)) {
421 if (IS_EOF(conf
, *from
))
424 buf
->data
[to
++] = *(from
++);
428 } else if (IS_DQUOTE(conf
, *from
)) {
431 while (!IS_EOF(conf
, *from
)) {
433 if (*(from
+ 1) == q
) {
439 buf
->data
[to
++] = *(from
++);
443 } else if (IS_ESC(conf
, *from
)) {
457 } else if (IS_EOF(conf
, *from
))
459 else if (*from
== '$') {
460 /* try to expand it */
474 while (IS_ALPHA_NUMERIC(conf
, *e
))
476 if ((e
[0] == ':') && (e
[1] == ':')) {
483 while (IS_ALPHA_NUMERIC(conf
, *e
))
491 CONFerr(CONF_F_STR_COPY
, CONF_R_NO_CLOSE_BRACE
);
497 * So at this point we have
498 * np which is the start of the name string which is
500 * cp which is the start of the section string which is
502 * e is the 'next point after'.
503 * r and rr are the chars replaced by the '\0'
504 * rp and rrp is where 'r' and 'rr' came from.
506 p
= _CONF_get_string(conf
, cp
, np
);
511 CONFerr(CONF_F_STR_COPY
, CONF_R_VARIABLE_HAS_NO_VALUE
);
514 if (!BUF_MEM_grow_clean(buf
,
515 (strlen(p
) + buf
->length
- (e
- from
)))) {
516 CONFerr(CONF_F_STR_COPY
, ERR_R_MALLOC_FAILURE
);
520 buf
->data
[to
++] = *(p
++);
523 * Since we change the pointer 'from', we also have to change the
524 * perceived length of the string it points at. /RL
530 * In case there were no braces or parenthesis around the
531 * variable reference, we have to put back the character that was
532 * replaced with a '\0'. /RL
536 buf
->data
[to
++] = *(from
++);
538 buf
->data
[to
] = '\0';
548 static char *eat_ws(CONF
*conf
, char *p
)
550 while (IS_WS(conf
, *p
) && (!IS_EOF(conf
, *p
)))
555 static char *eat_alpha_numeric(CONF
*conf
, char *p
)
558 if (IS_ESC(conf
, *p
)) {
559 p
= scan_esc(conf
, p
);
562 if (!IS_ALPHA_NUMERIC_PUNCT(conf
, *p
))
568 static char *scan_quote(CONF
*conf
, char *p
)
573 while (!(IS_EOF(conf
, *p
)) && (*p
!= q
)) {
574 if (IS_ESC(conf
, *p
)) {
576 if (IS_EOF(conf
, *p
))
586 static char *scan_dquote(CONF
*conf
, char *p
)
591 while (!(IS_EOF(conf
, *p
))) {
606 static void dump_value_doall_arg(const CONF_VALUE
*a
, BIO
*out
)
609 BIO_printf(out
, "[%s] %s=%s\n", a
->section
, a
->name
, a
->value
);
611 BIO_printf(out
, "[[%s]]\n", a
->section
);
614 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE
, BIO
);
616 static int def_dump(const CONF
*conf
, BIO
*out
)
618 lh_CONF_VALUE_doall_BIO(conf
->data
, dump_value_doall_arg
, out
);
622 static int def_is_number(const CONF
*conf
, char c
)
624 return IS_NUMBER(conf
, c
);
627 static int def_to_int(const CONF
*conf
, char c
)