]>
Commit | Line | Data |
---|---|---|
62867571 | 1 | /* |
454afd98 | 2 | * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
2044d382 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
62867571 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
d86b6915 RL |
10 | /* Part of the code in here was originally in conf.c, which is now removed */ |
11 | ||
d02b48c6 | 12 | #include <stdio.h> |
9887c71c | 13 | #include <string.h> |
08073700 RB |
14 | #ifdef __TANDEM |
15 | # include <strings.h> /* strcasecmp */ | |
16 | #endif | |
b39fc560 | 17 | #include "internal/cryptlib.h" |
b524b808 | 18 | #include "internal/o_dir.h" |
ec577822 BM |
19 | #include <openssl/lhash.h> |
20 | #include <openssl/conf.h> | |
d86b6915 RL |
21 | #include <openssl/conf_api.h> |
22 | #include "conf_def.h" | |
ec577822 BM |
23 | #include <openssl/buffer.h> |
24 | #include <openssl/err.h> | |
b524b808 TM |
25 | #ifndef OPENSSL_NO_POSIX_IO |
26 | # include <sys/stat.h> | |
27 | # ifdef _WIN32 | |
28 | # define stat _stat | |
29 | # define strcasecmp _stricmp | |
30 | # endif | |
31 | #endif | |
d02b48c6 | 32 | |
f20aa69e AP |
33 | #ifndef S_ISDIR |
34 | # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR) | |
35 | #endif | |
36 | ||
8a585601 MC |
37 | /* |
38 | * The maximum length we can grow a value to after variable expansion. 64k | |
39 | * should be more than enough for all reasonable uses. | |
40 | */ | |
41 | #define MAX_CONF_VALUE_LENGTH 65536 | |
42 | ||
a9b7a06e | 43 | static int is_keytype(const CONF *conf, char c, unsigned short type); |
d86b6915 | 44 | static char *eat_ws(CONF *conf, char *p); |
b524b808 | 45 | static void trim_ws(CONF *conf, char *start); |
d86b6915 RL |
46 | static char *eat_alpha_numeric(CONF *conf, char *p); |
47 | static void clear_comments(CONF *conf, char *p); | |
0f113f3e | 48 | static int str_copy(CONF *conf, char *section, char **to, char *from); |
d86b6915 RL |
49 | static char *scan_quote(CONF *conf, char *p); |
50 | static char *scan_dquote(CONF *conf, char *p); | |
0f113f3e | 51 | #define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2))) |
b524b808 TM |
52 | #ifndef OPENSSL_NO_POSIX_IO |
53 | static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx, | |
54 | char **dirpath); | |
55 | static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx); | |
56 | #endif | |
d86b6915 RL |
57 | |
58 | static CONF *def_create(CONF_METHOD *meth); | |
59 | static int def_init_default(CONF *conf); | |
936c2b9e | 60 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
d86b6915 | 61 | static int def_init_WIN32(CONF *conf); |
7cfc0a55 | 62 | #endif |
d86b6915 RL |
63 | static int def_destroy(CONF *conf); |
64 | static int def_destroy_data(CONF *conf); | |
befb3e7a RL |
65 | static int def_load(CONF *conf, const char *name, long *eline); |
66 | static int def_load_bio(CONF *conf, BIO *bp, long *eline); | |
9dd5ae65 BL |
67 | static int def_dump(const CONF *conf, BIO *bp); |
68 | static int def_is_number(const CONF *conf, char c); | |
69 | static int def_to_int(const CONF *conf, char c); | |
d86b6915 | 70 | |
d86b6915 | 71 | static CONF_METHOD default_method = { |
0f113f3e MC |
72 | "OpenSSL default", |
73 | def_create, | |
74 | def_init_default, | |
75 | def_destroy, | |
76 | def_destroy_data, | |
77 | def_load_bio, | |
78 | def_dump, | |
79 | def_is_number, | |
80 | def_to_int, | |
81 | def_load | |
82 | }; | |
d86b6915 | 83 | |
7cfc0a55 RS |
84 | CONF_METHOD *NCONF_default(void) |
85 | { | |
86 | return &default_method; | |
87 | } | |
88 | ||
936c2b9e | 89 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
d86b6915 | 90 | static CONF_METHOD WIN32_method = { |
0f113f3e MC |
91 | "WIN32", |
92 | def_create, | |
93 | def_init_WIN32, | |
94 | def_destroy, | |
95 | def_destroy_data, | |
96 | def_load_bio, | |
97 | def_dump, | |
98 | def_is_number, | |
99 | def_to_int, | |
100 | def_load | |
101 | }; | |
d86b6915 | 102 | |
3cb7c5cf | 103 | CONF_METHOD *NCONF_WIN32(void) |
0f113f3e MC |
104 | { |
105 | return &WIN32_method; | |
106 | } | |
7cfc0a55 | 107 | #endif |
d02b48c6 | 108 | |
d86b6915 | 109 | static CONF *def_create(CONF_METHOD *meth) |
0f113f3e MC |
110 | { |
111 | CONF *ret; | |
112 | ||
b4faea50 | 113 | ret = OPENSSL_malloc(sizeof(*ret)); |
90945fa3 | 114 | if (ret != NULL) |
0f113f3e MC |
115 | if (meth->init(ret) == 0) { |
116 | OPENSSL_free(ret); | |
117 | ret = NULL; | |
118 | } | |
119 | return ret; | |
120 | } | |
121 | ||
d86b6915 | 122 | static int def_init_default(CONF *conf) |
0f113f3e MC |
123 | { |
124 | if (conf == NULL) | |
125 | return 0; | |
d86b6915 | 126 | |
c15faa8d | 127 | memset(conf, 0, sizeof(*conf)); |
0f113f3e MC |
128 | conf->meth = &default_method; |
129 | conf->meth_data = (void *)CONF_type_default; | |
d02b48c6 | 130 | |
0f113f3e MC |
131 | return 1; |
132 | } | |
8623f693 | 133 | |
936c2b9e | 134 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
d86b6915 | 135 | static int def_init_WIN32(CONF *conf) |
0f113f3e MC |
136 | { |
137 | if (conf == NULL) | |
138 | return 0; | |
8623f693 | 139 | |
c15faa8d | 140 | memset(conf, 0, sizeof(*conf)); |
0f113f3e MC |
141 | conf->meth = &WIN32_method; |
142 | conf->meth_data = (void *)CONF_type_win32; | |
d86b6915 | 143 | |
0f113f3e MC |
144 | return 1; |
145 | } | |
7cfc0a55 | 146 | #endif |
d86b6915 RL |
147 | |
148 | static int def_destroy(CONF *conf) | |
0f113f3e MC |
149 | { |
150 | if (def_destroy_data(conf)) { | |
151 | OPENSSL_free(conf); | |
152 | return 1; | |
153 | } | |
154 | return 0; | |
155 | } | |
8623f693 | 156 | |
d86b6915 | 157 | static int def_destroy_data(CONF *conf) |
0f113f3e MC |
158 | { |
159 | if (conf == NULL) | |
160 | return 0; | |
161 | _CONF_free_data(conf); | |
162 | return 1; | |
163 | } | |
8623f693 | 164 | |
befb3e7a | 165 | static int def_load(CONF *conf, const char *name, long *line) |
0f113f3e MC |
166 | { |
167 | int ret; | |
168 | BIO *in = NULL; | |
befb3e7a | 169 | |
bc36ee62 | 170 | #ifdef OPENSSL_SYS_VMS |
0f113f3e | 171 | in = BIO_new_file(name, "r"); |
befb3e7a | 172 | #else |
0f113f3e | 173 | in = BIO_new_file(name, "rb"); |
befb3e7a | 174 | #endif |
0f113f3e MC |
175 | if (in == NULL) { |
176 | if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE) | |
9311d0c4 | 177 | ERR_raise(ERR_LIB_CONF, CONF_R_NO_SUCH_FILE); |
0f113f3e | 178 | else |
9311d0c4 | 179 | ERR_raise(ERR_LIB_CONF, ERR_R_SYS_LIB); |
0f113f3e MC |
180 | return 0; |
181 | } | |
befb3e7a | 182 | |
0f113f3e MC |
183 | ret = def_load_bio(conf, in, line); |
184 | BIO_free(in); | |
befb3e7a | 185 | |
0f113f3e MC |
186 | return ret; |
187 | } | |
befb3e7a RL |
188 | |
189 | static int def_load_bio(CONF *conf, BIO *in, long *line) | |
0f113f3e | 190 | { |
6a89a25c | 191 | /* The macro BUFSIZE conflicts with a system macro in VxWorks */ |
0f113f3e MC |
192 | #define CONFBUFSIZE 512 |
193 | int bufnum = 0, i, ii; | |
194 | BUF_MEM *buff = NULL; | |
195 | char *s, *p, *end; | |
196 | int again; | |
197 | long eline = 0; | |
198 | char btmp[DECIMAL_SIZE(eline) + 1]; | |
199 | CONF_VALUE *v = NULL, *tv; | |
200 | CONF_VALUE *sv = NULL; | |
201 | char *section = NULL, *buf; | |
202 | char *start, *psection, *pname; | |
203 | void *h = (void *)(conf->data); | |
b524b808 TM |
204 | STACK_OF(BIO) *biosk = NULL; |
205 | #ifndef OPENSSL_NO_POSIX_IO | |
206 | char *dirpath = NULL; | |
207 | OPENSSL_DIR_CTX *dirctx = NULL; | |
208 | #endif | |
0f113f3e MC |
209 | |
210 | if ((buff = BUF_MEM_new()) == NULL) { | |
9311d0c4 | 211 | ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB); |
0f113f3e MC |
212 | goto err; |
213 | } | |
214 | ||
7644a9ae | 215 | section = OPENSSL_strdup("default"); |
0f113f3e | 216 | if (section == NULL) { |
9311d0c4 | 217 | ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
218 | goto err; |
219 | } | |
0f113f3e MC |
220 | |
221 | if (_CONF_new_data(conf) == 0) { | |
9311d0c4 | 222 | ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
223 | goto err; |
224 | } | |
225 | ||
226 | sv = _CONF_new_section(conf, section); | |
227 | if (sv == NULL) { | |
9311d0c4 | 228 | ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION); |
0f113f3e MC |
229 | goto err; |
230 | } | |
231 | ||
232 | bufnum = 0; | |
233 | again = 0; | |
234 | for (;;) { | |
235 | if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) { | |
9311d0c4 | 236 | ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB); |
0f113f3e MC |
237 | goto err; |
238 | } | |
239 | p = &(buff->data[bufnum]); | |
240 | *p = '\0'; | |
b524b808 | 241 | read_retry: |
15795943 DDO |
242 | if (in != NULL && BIO_gets(in, p, CONFBUFSIZE - 1) < 0) |
243 | goto err; | |
0f113f3e MC |
244 | p[CONFBUFSIZE - 1] = '\0'; |
245 | ii = i = strlen(p); | |
b524b808 | 246 | if (i == 0 && !again) { |
15795943 | 247 | /* the currently processed BIO is NULL or at EOF */ |
b524b808 TM |
248 | BIO *parent; |
249 | ||
250 | #ifndef OPENSSL_NO_POSIX_IO | |
251 | /* continue processing with the next file from directory */ | |
252 | if (dirctx != NULL) { | |
253 | BIO *next; | |
254 | ||
255 | if ((next = get_next_file(dirpath, &dirctx)) != NULL) { | |
256 | BIO_vfree(in); | |
257 | in = next; | |
258 | goto read_retry; | |
259 | } else { | |
260 | OPENSSL_free(dirpath); | |
261 | dirpath = NULL; | |
262 | } | |
263 | } | |
264 | #endif | |
265 | /* no more files in directory, continue with processing parent */ | |
266 | if ((parent = sk_BIO_pop(biosk)) == NULL) { | |
267 | /* everything processed get out of the loop */ | |
268 | break; | |
269 | } else { | |
270 | BIO_vfree(in); | |
271 | in = parent; | |
272 | goto read_retry; | |
273 | } | |
274 | } | |
0f113f3e MC |
275 | again = 0; |
276 | while (i > 0) { | |
277 | if ((p[i - 1] != '\r') && (p[i - 1] != '\n')) | |
278 | break; | |
279 | else | |
280 | i--; | |
281 | } | |
282 | /* | |
283 | * we removed some trailing stuff so there is a new line on the end. | |
284 | */ | |
285 | if (ii && i == ii) | |
286 | again = 1; /* long line */ | |
287 | else { | |
288 | p[i] = '\0'; | |
289 | eline++; /* another input line */ | |
290 | } | |
291 | ||
292 | /* we now have a line with trailing \r\n removed */ | |
293 | ||
294 | /* i is the number of bytes */ | |
295 | bufnum += i; | |
296 | ||
297 | v = NULL; | |
298 | /* check for line continuation */ | |
299 | if (bufnum >= 1) { | |
300 | /* | |
301 | * If we have bytes and the last char '\\' and second last char | |
302 | * is not '\\' | |
303 | */ | |
304 | p = &(buff->data[bufnum - 1]); | |
305 | if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) { | |
306 | bufnum--; | |
307 | again = 1; | |
308 | } | |
309 | } | |
310 | if (again) | |
311 | continue; | |
312 | bufnum = 0; | |
313 | buf = buff->data; | |
314 | ||
315 | clear_comments(conf, buf); | |
316 | s = eat_ws(conf, buf); | |
317 | if (IS_EOF(conf, *s)) | |
318 | continue; /* blank line */ | |
319 | if (*s == '[') { | |
320 | char *ss; | |
321 | ||
322 | s++; | |
323 | start = eat_ws(conf, s); | |
324 | ss = start; | |
325 | again: | |
326 | end = eat_alpha_numeric(conf, ss); | |
327 | p = eat_ws(conf, end); | |
328 | if (*p != ']') { | |
329 | if (*p != '\0' && ss != p) { | |
330 | ss = p; | |
331 | goto again; | |
332 | } | |
9311d0c4 | 333 | ERR_raise(ERR_LIB_CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET); |
0f113f3e MC |
334 | goto err; |
335 | } | |
336 | *end = '\0'; | |
337 | if (!str_copy(conf, NULL, §ion, start)) | |
338 | goto err; | |
339 | if ((sv = _CONF_get_section(conf, section)) == NULL) | |
340 | sv = _CONF_new_section(conf, section); | |
341 | if (sv == NULL) { | |
9311d0c4 | 342 | ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION); |
0f113f3e MC |
343 | goto err; |
344 | } | |
345 | continue; | |
346 | } else { | |
347 | pname = s; | |
0f113f3e MC |
348 | end = eat_alpha_numeric(conf, s); |
349 | if ((end[0] == ':') && (end[1] == ':')) { | |
350 | *end = '\0'; | |
351 | end += 2; | |
352 | psection = pname; | |
353 | pname = end; | |
354 | end = eat_alpha_numeric(conf, end); | |
b524b808 TM |
355 | } else { |
356 | psection = section; | |
0f113f3e MC |
357 | } |
358 | p = eat_ws(conf, end); | |
0255c174 RL |
359 | if (strncmp(pname, ".pragma", 7) == 0 |
360 | && (p != pname + 7 || *p == '=')) { | |
361 | char *pval; | |
362 | ||
363 | if (*p == '=') { | |
364 | p++; | |
365 | p = eat_ws(conf, p); | |
366 | } | |
367 | trim_ws(conf, p); | |
368 | ||
369 | /* Pragma values take the form keyword:value */ | |
370 | pval = strchr(p, ':'); | |
371 | if (pval == NULL || pval == p || pval[1] == '\0') { | |
9311d0c4 | 372 | ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA); |
0255c174 RL |
373 | goto err; |
374 | } | |
375 | ||
376 | *pval++ = '\0'; | |
377 | trim_ws(conf, p); | |
378 | pval = eat_ws(conf, pval); | |
379 | ||
380 | /* | |
381 | * Known pragmas: | |
382 | * | |
383 | * dollarid takes "on", "true or "off", "false" | |
384 | */ | |
385 | if (strcmp(p, "dollarid") == 0) { | |
386 | if (strcmp(pval, "on") == 0 | |
387 | || strcmp(pval, "true") == 0) { | |
388 | conf->flag_dollarid = 1; | |
389 | } else if (strcmp(pval, "off") == 0 | |
390 | || strcmp(pval, "false") == 0) { | |
391 | conf->flag_dollarid = 0; | |
392 | } else { | |
9311d0c4 | 393 | ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA); |
0255c174 RL |
394 | goto err; |
395 | } | |
396 | } | |
397 | /* | |
398 | * We *ignore* any unknown pragma. | |
399 | */ | |
400 | continue; | |
401 | } else if (strncmp(pname, ".include", 8) == 0 | |
9d556033 | 402 | && (p != pname + 8 || *p == '=')) { |
b524b808 TM |
403 | char *include = NULL; |
404 | BIO *next; | |
7bb82f92 SL |
405 | const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE"); |
406 | char *include_path = NULL; | |
b524b808 | 407 | |
9d556033 TM |
408 | if (*p == '=') { |
409 | p++; | |
410 | p = eat_ws(conf, p); | |
411 | } | |
b524b808 TM |
412 | trim_ws(conf, p); |
413 | if (!str_copy(conf, psection, &include, p)) | |
414 | goto err; | |
7bb82f92 | 415 | |
d8701e25 | 416 | if (include_dir != NULL && !ossl_is_absolute_path(include)) { |
7bb82f92 SL |
417 | size_t newlen = strlen(include_dir) + strlen(include) + 2; |
418 | ||
419 | include_path = OPENSSL_malloc(newlen); | |
d8701e25 | 420 | if (include_path == NULL) { |
e19c5a10 | 421 | ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE); |
d8701e25 TM |
422 | OPENSSL_free(include); |
423 | goto err; | |
424 | } | |
425 | ||
7bb82f92 | 426 | OPENSSL_strlcpy(include_path, include_dir, newlen); |
d8701e25 TM |
427 | if (!ossl_ends_with_dirsep(include_path)) |
428 | OPENSSL_strlcat(include_path, "/", newlen); | |
7bb82f92 | 429 | OPENSSL_strlcat(include_path, include, newlen); |
15dd075f | 430 | OPENSSL_free(include); |
7bb82f92 SL |
431 | } else { |
432 | include_path = include; | |
433 | } | |
434 | ||
b524b808 TM |
435 | /* get the BIO of the included file */ |
436 | #ifndef OPENSSL_NO_POSIX_IO | |
7bb82f92 SL |
437 | next = process_include(include_path, &dirctx, &dirpath); |
438 | if (include_path != dirpath) { | |
b524b808 | 439 | /* dirpath will contain include in case of a directory */ |
15dd075f | 440 | OPENSSL_free(include_path); |
b524b808 TM |
441 | } |
442 | #else | |
7bb82f92 | 443 | next = BIO_new_file(include_path, "r"); |
15dd075f | 444 | OPENSSL_free(include_path); |
b524b808 | 445 | #endif |
7bb82f92 | 446 | |
b524b808 TM |
447 | if (next != NULL) { |
448 | /* push the currently processing BIO onto stack */ | |
449 | if (biosk == NULL) { | |
450 | if ((biosk = sk_BIO_new_null()) == NULL) { | |
9311d0c4 | 451 | ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE); |
4348995b | 452 | BIO_free(next); |
b524b808 TM |
453 | goto err; |
454 | } | |
455 | } | |
456 | if (!sk_BIO_push(biosk, in)) { | |
9311d0c4 | 457 | ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE); |
4348995b | 458 | BIO_free(next); |
b524b808 TM |
459 | goto err; |
460 | } | |
461 | /* continue with reading from the included BIO */ | |
462 | in = next; | |
463 | } | |
464 | continue; | |
465 | } else if (*p != '=') { | |
a150f8e1 RL |
466 | ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN, |
467 | "HERE-->%s", p); | |
0f113f3e MC |
468 | goto err; |
469 | } | |
470 | *end = '\0'; | |
471 | p++; | |
472 | start = eat_ws(conf, p); | |
b524b808 | 473 | trim_ws(conf, start); |
0f113f3e | 474 | |
75ebbd9a | 475 | if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) { |
9311d0c4 | 476 | ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
477 | goto err; |
478 | } | |
a2371fa9 | 479 | v->name = OPENSSL_strdup(pname); |
0f113f3e MC |
480 | v->value = NULL; |
481 | if (v->name == NULL) { | |
9311d0c4 | 482 | ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
483 | goto err; |
484 | } | |
0f113f3e MC |
485 | if (!str_copy(conf, psection, &(v->value), start)) |
486 | goto err; | |
487 | ||
488 | if (strcmp(psection, section) != 0) { | |
489 | if ((tv = _CONF_get_section(conf, psection)) | |
490 | == NULL) | |
491 | tv = _CONF_new_section(conf, psection); | |
492 | if (tv == NULL) { | |
9311d0c4 RL |
493 | ERR_raise(ERR_LIB_CONF, |
494 | CONF_R_UNABLE_TO_CREATE_NEW_SECTION); | |
0f113f3e MC |
495 | goto err; |
496 | } | |
497 | } else | |
498 | tv = sv; | |
0f113f3e | 499 | if (_CONF_add_string(conf, tv, v) == 0) { |
9311d0c4 | 500 | ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
501 | goto err; |
502 | } | |
0f113f3e MC |
503 | v = NULL; |
504 | } | |
505 | } | |
25aaa98a | 506 | BUF_MEM_free(buff); |
b548a1f1 | 507 | OPENSSL_free(section); |
85aebfcc RL |
508 | /* |
509 | * No need to pop, since we only get here if the stack is empty. | |
510 | * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE! | |
511 | */ | |
512 | sk_BIO_free(biosk); | |
a2371fa9 | 513 | return 1; |
0f113f3e | 514 | err: |
25aaa98a | 515 | BUF_MEM_free(buff); |
b548a1f1 | 516 | OPENSSL_free(section); |
85aebfcc RL |
517 | /* |
518 | * Since |in| is the first element of the stack and should NOT be freed | |
519 | * here, we cannot use sk_BIO_pop_free(). Instead, we pop and free one | |
520 | * BIO at a time, making sure that the last one popped isn't. | |
521 | */ | |
522 | while (sk_BIO_num(biosk) > 0) { | |
523 | BIO *popped = sk_BIO_pop(biosk); | |
524 | BIO_vfree(in); | |
525 | in = popped; | |
526 | } | |
527 | sk_BIO_free(biosk); | |
b524b808 TM |
528 | #ifndef OPENSSL_NO_POSIX_IO |
529 | OPENSSL_free(dirpath); | |
530 | if (dirctx != NULL) | |
531 | OPENSSL_DIR_end(&dirctx); | |
532 | #endif | |
0f113f3e MC |
533 | if (line != NULL) |
534 | *line = eline; | |
a2371fa9 | 535 | BIO_snprintf(btmp, sizeof(btmp), "%ld", eline); |
0f113f3e | 536 | ERR_add_error_data(2, "line ", btmp); |
25aaa98a | 537 | if (h != conf->data) { |
0f113f3e MC |
538 | CONF_free(conf->data); |
539 | conf->data = NULL; | |
540 | } | |
541 | if (v != NULL) { | |
b548a1f1 RS |
542 | OPENSSL_free(v->name); |
543 | OPENSSL_free(v->value); | |
544 | OPENSSL_free(v); | |
0f113f3e | 545 | } |
a2371fa9 | 546 | return 0; |
0f113f3e | 547 | } |
8623f693 | 548 | |
d86b6915 | 549 | static void clear_comments(CONF *conf, char *p) |
0f113f3e MC |
550 | { |
551 | for (;;) { | |
552 | if (IS_FCOMMENT(conf, *p)) { | |
553 | *p = '\0'; | |
554 | return; | |
555 | } | |
556 | if (!IS_WS(conf, *p)) { | |
557 | break; | |
558 | } | |
559 | p++; | |
560 | } | |
561 | ||
562 | for (;;) { | |
563 | if (IS_COMMENT(conf, *p)) { | |
564 | *p = '\0'; | |
565 | return; | |
566 | } | |
567 | if (IS_DQUOTE(conf, *p)) { | |
568 | p = scan_dquote(conf, p); | |
569 | continue; | |
570 | } | |
571 | if (IS_QUOTE(conf, *p)) { | |
572 | p = scan_quote(conf, p); | |
573 | continue; | |
574 | } | |
575 | if (IS_ESC(conf, *p)) { | |
576 | p = scan_esc(conf, p); | |
577 | continue; | |
578 | } | |
579 | if (IS_EOF(conf, *p)) | |
580 | return; | |
581 | else | |
582 | p++; | |
583 | } | |
584 | } | |
d02b48c6 | 585 | |
d86b6915 | 586 | static int str_copy(CONF *conf, char *section, char **pto, char *from) |
0f113f3e MC |
587 | { |
588 | int q, r, rr = 0, to = 0, len = 0; | |
589 | char *s, *e, *rp, *p, *rrp, *np, *cp, v; | |
590 | BUF_MEM *buf; | |
591 | ||
592 | if ((buf = BUF_MEM_new()) == NULL) | |
a2371fa9 | 593 | return 0; |
0f113f3e MC |
594 | |
595 | len = strlen(from) + 1; | |
596 | if (!BUF_MEM_grow(buf, len)) | |
597 | goto err; | |
598 | ||
599 | for (;;) { | |
600 | if (IS_QUOTE(conf, *from)) { | |
601 | q = *from; | |
602 | from++; | |
603 | while (!IS_EOF(conf, *from) && (*from != q)) { | |
604 | if (IS_ESC(conf, *from)) { | |
605 | from++; | |
606 | if (IS_EOF(conf, *from)) | |
607 | break; | |
608 | } | |
609 | buf->data[to++] = *(from++); | |
610 | } | |
611 | if (*from == q) | |
612 | from++; | |
613 | } else if (IS_DQUOTE(conf, *from)) { | |
614 | q = *from; | |
615 | from++; | |
616 | while (!IS_EOF(conf, *from)) { | |
617 | if (*from == q) { | |
618 | if (*(from + 1) == q) { | |
619 | from++; | |
620 | } else { | |
621 | break; | |
622 | } | |
623 | } | |
624 | buf->data[to++] = *(from++); | |
625 | } | |
626 | if (*from == q) | |
627 | from++; | |
628 | } else if (IS_ESC(conf, *from)) { | |
629 | from++; | |
630 | v = *(from++); | |
631 | if (IS_EOF(conf, v)) | |
632 | break; | |
633 | else if (v == 'r') | |
634 | v = '\r'; | |
635 | else if (v == 'n') | |
636 | v = '\n'; | |
637 | else if (v == 'b') | |
638 | v = '\b'; | |
639 | else if (v == 't') | |
640 | v = '\t'; | |
641 | buf->data[to++] = v; | |
642 | } else if (IS_EOF(conf, *from)) | |
643 | break; | |
0255c174 RL |
644 | else if (*from == '$' |
645 | && (!conf->flag_dollarid | |
646 | || from[1] == '{' | |
647 | || from[1] == '(')) { | |
8a585601 MC |
648 | size_t newsize; |
649 | ||
0f113f3e MC |
650 | /* try to expand it */ |
651 | rrp = NULL; | |
652 | s = &(from[1]); | |
653 | if (*s == '{') | |
654 | q = '}'; | |
655 | else if (*s == '(') | |
656 | q = ')'; | |
657 | else | |
658 | q = 0; | |
659 | ||
660 | if (q) | |
661 | s++; | |
662 | cp = section; | |
663 | e = np = s; | |
0255c174 RL |
664 | while (IS_ALNUM(conf, *e) |
665 | || (conf->flag_dollarid && IS_DOLLAR(conf, *e))) | |
0f113f3e MC |
666 | e++; |
667 | if ((e[0] == ':') && (e[1] == ':')) { | |
668 | cp = np; | |
669 | rrp = e; | |
670 | rr = *e; | |
671 | *rrp = '\0'; | |
672 | e += 2; | |
673 | np = e; | |
0255c174 RL |
674 | while (IS_ALNUM(conf, *e) |
675 | || (conf->flag_dollarid && IS_DOLLAR(conf, *e))) | |
0f113f3e MC |
676 | e++; |
677 | } | |
678 | r = *e; | |
679 | *e = '\0'; | |
680 | rp = e; | |
681 | if (q) { | |
682 | if (r != q) { | |
9311d0c4 | 683 | ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE); |
0f113f3e MC |
684 | goto err; |
685 | } | |
686 | e++; | |
687 | } | |
50e735f9 MC |
688 | /*- |
689 | * So at this point we have | |
690 | * np which is the start of the name string which is | |
691 | * '\0' terminated. | |
692 | * cp which is the start of the section string which is | |
693 | * '\0' terminated. | |
694 | * e is the 'next point after'. | |
695 | * r and rr are the chars replaced by the '\0' | |
696 | * rp and rrp is where 'r' and 'rr' came from. | |
697 | */ | |
0f113f3e MC |
698 | p = _CONF_get_string(conf, cp, np); |
699 | if (rrp != NULL) | |
700 | *rrp = rr; | |
701 | *rp = r; | |
702 | if (p == NULL) { | |
9311d0c4 | 703 | ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_HAS_NO_VALUE); |
0f113f3e MC |
704 | goto err; |
705 | } | |
8a585601 MC |
706 | newsize = strlen(p) + buf->length - (e - from); |
707 | if (newsize > MAX_CONF_VALUE_LENGTH) { | |
9311d0c4 | 708 | ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG); |
8a585601 MC |
709 | goto err; |
710 | } | |
711 | if (!BUF_MEM_grow_clean(buf, newsize)) { | |
9311d0c4 | 712 | ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE); |
b0333e69 GP |
713 | goto err; |
714 | } | |
0f113f3e MC |
715 | while (*p) |
716 | buf->data[to++] = *(p++); | |
717 | ||
718 | /* | |
719 | * Since we change the pointer 'from', we also have to change the | |
720 | * perceived length of the string it points at. /RL | |
721 | */ | |
722 | len -= e - from; | |
723 | from = e; | |
724 | ||
725 | /* | |
726 | * In case there were no braces or parenthesis around the | |
727 | * variable reference, we have to put back the character that was | |
728 | * replaced with a '\0'. /RL | |
729 | */ | |
730 | *rp = r; | |
731 | } else | |
732 | buf->data[to++] = *(from++); | |
733 | } | |
734 | buf->data[to] = '\0'; | |
b548a1f1 | 735 | OPENSSL_free(*pto); |
0f113f3e MC |
736 | *pto = buf->data; |
737 | OPENSSL_free(buf); | |
a2371fa9 | 738 | return 1; |
0f113f3e | 739 | err: |
25aaa98a | 740 | BUF_MEM_free(buf); |
a2371fa9 | 741 | return 0; |
0f113f3e | 742 | } |
d02b48c6 | 743 | |
b524b808 TM |
744 | #ifndef OPENSSL_NO_POSIX_IO |
745 | /* | |
746 | * Check whether included path is a directory. | |
747 | * Returns next BIO to process and in case of a directory | |
748 | * also an opened directory context and the include path. | |
749 | */ | |
750 | static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx, | |
751 | char **dirpath) | |
752 | { | |
2661d716 | 753 | struct stat st; |
b524b808 TM |
754 | BIO *next; |
755 | ||
756 | if (stat(include, &st) < 0) { | |
9311d0c4 | 757 | ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include); |
b524b808 TM |
758 | /* missing include file is not fatal error */ |
759 | return NULL; | |
760 | } | |
761 | ||
f20aa69e | 762 | if (S_ISDIR(st.st_mode)) { |
b524b808 | 763 | if (*dirctx != NULL) { |
a150f8e1 RL |
764 | ERR_raise_data(ERR_LIB_CONF, CONF_R_RECURSIVE_DIRECTORY_INCLUDE, |
765 | "%s", include); | |
b524b808 TM |
766 | return NULL; |
767 | } | |
768 | /* a directory, load its contents */ | |
769 | if ((next = get_next_file(include, dirctx)) != NULL) | |
770 | *dirpath = include; | |
771 | return next; | |
772 | } | |
773 | ||
774 | next = BIO_new_file(include, "r"); | |
775 | return next; | |
776 | } | |
777 | ||
778 | /* | |
779 | * Get next file from the directory path. | |
780 | * Returns BIO of the next file to read and updates dirctx. | |
781 | */ | |
782 | static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx) | |
783 | { | |
784 | const char *filename; | |
d1c1fb2d | 785 | size_t pathlen; |
b524b808 | 786 | |
d1c1fb2d | 787 | pathlen = strlen(path); |
b524b808 TM |
788 | while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) { |
789 | size_t namelen; | |
790 | ||
791 | namelen = strlen(filename); | |
792 | ||
4f7c840a | 793 | |
b524b808 TM |
794 | if ((namelen > 5 && strcasecmp(filename + namelen - 5, ".conf") == 0) |
795 | || (namelen > 4 && strcasecmp(filename + namelen - 4, ".cnf") == 0)) { | |
796 | size_t newlen; | |
797 | char *newpath; | |
798 | BIO *bio; | |
799 | ||
d1c1fb2d | 800 | newlen = pathlen + namelen + 2; |
b524b808 TM |
801 | newpath = OPENSSL_zalloc(newlen); |
802 | if (newpath == NULL) { | |
9311d0c4 | 803 | ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE); |
b524b808 TM |
804 | break; |
805 | } | |
4f7c840a RL |
806 | #ifdef OPENSSL_SYS_VMS |
807 | /* | |
808 | * If the given path isn't clear VMS syntax, | |
809 | * we treat it as on Unix. | |
810 | */ | |
d1c1fb2d | 811 | if (path[pathlen - 1] == ']' |
812 | || path[pathlen - 1] == '>' | |
813 | || path[pathlen - 1] == ':') { | |
814 | /* Clear VMS directory syntax, just copy as is */ | |
815 | OPENSSL_strlcpy(newpath, path, newlen); | |
4f7c840a | 816 | } |
b524b808 | 817 | #endif |
4f7c840a RL |
818 | if (newpath[0] == '\0') { |
819 | OPENSSL_strlcpy(newpath, path, newlen); | |
820 | OPENSSL_strlcat(newpath, "/", newlen); | |
821 | } | |
b524b808 TM |
822 | OPENSSL_strlcat(newpath, filename, newlen); |
823 | ||
824 | bio = BIO_new_file(newpath, "r"); | |
825 | OPENSSL_free(newpath); | |
826 | /* Errors when opening files are non-fatal. */ | |
827 | if (bio != NULL) | |
828 | return bio; | |
829 | } | |
830 | } | |
831 | OPENSSL_DIR_end(dirctx); | |
832 | *dirctx = NULL; | |
833 | return NULL; | |
834 | } | |
835 | #endif | |
836 | ||
a9b7a06e DMSP |
837 | static int is_keytype(const CONF *conf, char c, unsigned short type) |
838 | { | |
839 | const unsigned short * keytypes = (const unsigned short *) conf->meth_data; | |
840 | unsigned char key = (unsigned char)c; | |
841 | ||
842 | #ifdef CHARSET_EBCDIC | |
843 | # if CHAR_BIT > 8 | |
844 | if (key > 255) { | |
845 | /* key is out of range for os_toascii table */ | |
846 | return 0; | |
847 | } | |
848 | # endif | |
849 | /* convert key from ebcdic to ascii */ | |
850 | key = os_toascii[key]; | |
851 | #endif | |
852 | ||
853 | if (key > 127) { | |
854 | /* key is not a seven bit ascii character */ | |
855 | return 0; | |
856 | } | |
857 | ||
858 | return (keytypes[key] & type) ? 1 : 0; | |
859 | } | |
860 | ||
d86b6915 | 861 | static char *eat_ws(CONF *conf, char *p) |
0f113f3e MC |
862 | { |
863 | while (IS_WS(conf, *p) && (!IS_EOF(conf, *p))) | |
864 | p++; | |
a2371fa9 | 865 | return p; |
0f113f3e | 866 | } |
d02b48c6 | 867 | |
b524b808 TM |
868 | static void trim_ws(CONF *conf, char *start) |
869 | { | |
870 | char *p = start; | |
871 | ||
872 | while (!IS_EOF(conf, *p)) | |
873 | p++; | |
874 | p--; | |
875 | while ((p >= start) && IS_WS(conf, *p)) | |
876 | p--; | |
877 | p++; | |
878 | *p = '\0'; | |
879 | } | |
880 | ||
d86b6915 | 881 | static char *eat_alpha_numeric(CONF *conf, char *p) |
0f113f3e MC |
882 | { |
883 | for (;;) { | |
884 | if (IS_ESC(conf, *p)) { | |
885 | p = scan_esc(conf, p); | |
886 | continue; | |
887 | } | |
0255c174 RL |
888 | if (!(IS_ALNUM_PUNCT(conf, *p) |
889 | || (conf->flag_dollarid && IS_DOLLAR(conf, *p)))) | |
a2371fa9 | 890 | return p; |
0f113f3e MC |
891 | p++; |
892 | } | |
893 | } | |
d02b48c6 | 894 | |
d86b6915 | 895 | static char *scan_quote(CONF *conf, char *p) |
0f113f3e MC |
896 | { |
897 | int q = *p; | |
898 | ||
899 | p++; | |
900 | while (!(IS_EOF(conf, *p)) && (*p != q)) { | |
901 | if (IS_ESC(conf, *p)) { | |
902 | p++; | |
903 | if (IS_EOF(conf, *p)) | |
a2371fa9 | 904 | return p; |
0f113f3e MC |
905 | } |
906 | p++; | |
907 | } | |
908 | if (*p == q) | |
909 | p++; | |
a2371fa9 | 910 | return p; |
0f113f3e | 911 | } |
d86b6915 RL |
912 | |
913 | static char *scan_dquote(CONF *conf, char *p) | |
0f113f3e MC |
914 | { |
915 | int q = *p; | |
916 | ||
917 | p++; | |
918 | while (!(IS_EOF(conf, *p))) { | |
919 | if (*p == q) { | |
920 | if (*(p + 1) == q) { | |
921 | p++; | |
922 | } else { | |
923 | break; | |
924 | } | |
925 | } | |
926 | p++; | |
927 | } | |
928 | if (*p == q) | |
929 | p++; | |
a2371fa9 | 930 | return p; |
0f113f3e | 931 | } |
d02b48c6 | 932 | |
2a056de8 | 933 | static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out) |
0f113f3e MC |
934 | { |
935 | if (a->name) | |
936 | BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value); | |
937 | else | |
938 | BIO_printf(out, "[[%s]]\n", a->section); | |
939 | } | |
d86b6915 | 940 | |
2a056de8 | 941 | IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO); |
3c914840 | 942 | |
9dd5ae65 | 943 | static int def_dump(const CONF *conf, BIO *out) |
0f113f3e | 944 | { |
2a056de8 | 945 | lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out); |
0f113f3e MC |
946 | return 1; |
947 | } | |
d02b48c6 | 948 | |
9dd5ae65 | 949 | static int def_is_number(const CONF *conf, char c) |
0f113f3e MC |
950 | { |
951 | return IS_NUMBER(conf, c); | |
952 | } | |
d02b48c6 | 953 | |
9dd5ae65 | 954 | static int def_to_int(const CONF *conf, char c) |
0f113f3e MC |
955 | { |
956 | return c - '0'; | |
957 | } |