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