/src/openssl/crypto/conf/conf_def.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2026 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 | 57.6k | #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 | 1.95M | #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 | 41.2k | { |
88 | 41.2k | return &default_method; |
89 | 41.2k | } |
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 | 41.2k | { |
113 | 41.2k | CONF *ret; |
114 | | |
115 | 41.2k | ret = OPENSSL_malloc(sizeof(*ret)); |
116 | 41.2k | if (ret != NULL) |
117 | 41.2k | if (meth->init(ret) == 0) { |
118 | 0 | OPENSSL_free(ret); |
119 | 0 | ret = NULL; |
120 | 0 | } |
121 | 41.2k | return ret; |
122 | 41.2k | } |
123 | | |
124 | | static int def_init_default(CONF *conf) |
125 | 50.2k | { |
126 | 50.2k | if (conf == NULL) |
127 | 0 | return 0; |
128 | | |
129 | 50.2k | memset(conf, 0, sizeof(*conf)); |
130 | 50.2k | conf->meth = &default_method; |
131 | 50.2k | conf->meth_data = (void *)CONF_type_default; |
132 | | |
133 | 50.2k | return 1; |
134 | 50.2k | } |
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 | 41.2k | { |
152 | 41.2k | if (def_destroy_data(conf)) { |
153 | 41.2k | OPENSSL_free(conf); |
154 | 41.2k | return 1; |
155 | 41.2k | } |
156 | 0 | return 0; |
157 | 41.2k | } |
158 | | |
159 | | static int def_destroy_data(CONF *conf) |
160 | 50.2k | { |
161 | 50.2k | if (conf == NULL) |
162 | 0 | return 0; |
163 | 50.2k | _CONF_free_data(conf); |
164 | 50.2k | return 1; |
165 | 50.2k | } |
166 | | |
167 | | static int def_load(CONF *conf, const char *name, long *line) |
168 | 225 | { |
169 | 225 | int ret; |
170 | 225 | BIO *in = NULL; |
171 | | |
172 | | #ifdef OPENSSL_SYS_VMS |
173 | | in = BIO_new_file(name, "r"); |
174 | | #else |
175 | 225 | in = BIO_new_file(name, "rb"); |
176 | 225 | #endif |
177 | 225 | if (in == NULL) { |
178 | 225 | if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE) |
179 | 225 | ERR_raise(ERR_LIB_CONF, CONF_R_NO_SUCH_FILE); |
180 | 0 | else |
181 | 225 | ERR_raise(ERR_LIB_CONF, ERR_R_SYS_LIB); |
182 | 225 | return 0; |
183 | 225 | } |
184 | | |
185 | 0 | ret = def_load_bio(conf, in, line); |
186 | 0 | BIO_free(in); |
187 | |
|
188 | 0 | return ret; |
189 | 225 | } |
190 | | |
191 | | /* Parse a boolean value and fill in *flag. Return 0 on error. */ |
192 | | static int parsebool(const char *pval, int *flag) |
193 | 10.7k | { |
194 | 10.7k | if (OPENSSL_strcasecmp(pval, "on") == 0 |
195 | 6.73k | || OPENSSL_strcasecmp(pval, "true") == 0) { |
196 | 6.73k | *flag = 1; |
197 | 6.73k | } else if (OPENSSL_strcasecmp(pval, "off") == 0 |
198 | 3.64k | || OPENSSL_strcasecmp(pval, "false") == 0) { |
199 | 3.64k | *flag = 0; |
200 | 3.64k | } else { |
201 | 369 | ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA); |
202 | 369 | return 0; |
203 | 369 | } |
204 | 10.3k | return 1; |
205 | 10.7k | } |
206 | | |
207 | | static int def_load_bio(CONF *conf, BIO *in, long *line) |
208 | 14.8k | { |
209 | | /* The macro BUFSIZE conflicts with a system macro in VxWorks */ |
210 | 2.05M | #define CONFBUFSIZE 512 |
211 | 14.8k | int bufnum = 0, i, ii; |
212 | 14.8k | BUF_MEM *buff = NULL; |
213 | 14.8k | char *s, *p, *end; |
214 | 14.8k | int again; |
215 | 14.8k | int first_call = 1; |
216 | 14.8k | long eline = 0; |
217 | 14.8k | char btmp[DECIMAL_SIZE(eline) + 1]; |
218 | 14.8k | CONF_VALUE *v = NULL, *tv; |
219 | 14.8k | CONF_VALUE *sv = NULL; |
220 | 14.8k | char *section = NULL, *buf; |
221 | 14.8k | char *start, *psection, *pname; |
222 | 14.8k | void *h = (void *)(conf->data); |
223 | 14.8k | STACK_OF(BIO) *biosk = NULL; |
224 | 14.8k | #ifndef OPENSSL_NO_POSIX_IO |
225 | 14.8k | char *dirpath = NULL; |
226 | 14.8k | OPENSSL_DIR_CTX *dirctx = NULL; |
227 | 14.8k | #endif |
228 | 14.8k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
229 | 14.8k | int numincludes = 0; |
230 | 14.8k | #endif |
231 | | |
232 | 14.8k | if ((buff = BUF_MEM_new()) == NULL) { |
233 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB); |
234 | 0 | goto err; |
235 | 0 | } |
236 | | |
237 | 14.8k | section = OPENSSL_strdup("default"); |
238 | 14.8k | if (section == NULL) |
239 | 0 | goto err; |
240 | | |
241 | 14.8k | if (_CONF_new_data(conf) == 0) { |
242 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_CONF_LIB); |
243 | 0 | goto err; |
244 | 0 | } |
245 | | |
246 | 14.8k | sv = _CONF_new_section(conf, section); |
247 | 14.8k | if (sv == NULL) { |
248 | 0 | ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION); |
249 | 0 | goto err; |
250 | 0 | } |
251 | | |
252 | 14.8k | bufnum = 0; |
253 | 14.8k | again = 0; |
254 | 685k | for (;;) { |
255 | 685k | if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) { |
256 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB); |
257 | 0 | goto err; |
258 | 0 | } |
259 | 685k | p = &(buff->data[bufnum]); |
260 | 685k | *p = '\0'; |
261 | 686k | read_retry: |
262 | 686k | if (in != NULL && BIO_gets(in, p, CONFBUFSIZE - 1) < 0) |
263 | 0 | goto err; |
264 | 686k | p[CONFBUFSIZE - 1] = '\0'; |
265 | 686k | ii = i = (int)strlen(p); |
266 | 686k | if (first_call) { |
267 | | /* Other BOMs imply unsupported multibyte encoding, |
268 | | * so don't strip them and let the error raise */ |
269 | 14.8k | const unsigned char utf8_bom[3] = { 0xEF, 0xBB, 0xBF }; |
270 | | |
271 | 14.8k | if (i >= 3 && memcmp(p, utf8_bom, 3) == 0) { |
272 | 36 | memmove(p, p + 3, i - 3); |
273 | 36 | p[i - 3] = 0; |
274 | 36 | i -= 3; |
275 | 36 | ii -= 3; |
276 | 36 | } |
277 | 14.8k | first_call = 0; |
278 | 14.8k | } |
279 | 686k | if (i == 0 && !again) { |
280 | | /* the currently processed BIO is NULL or at EOF */ |
281 | 13.5k | BIO *parent; |
282 | | |
283 | 13.5k | #ifndef OPENSSL_NO_POSIX_IO |
284 | | /* continue processing with the next file from directory */ |
285 | 13.5k | if (dirctx != NULL) { |
286 | 807 | BIO *next; |
287 | | |
288 | 807 | if ((next = get_next_file(dirpath, &dirctx)) != NULL) { |
289 | 211 | BIO_vfree(in); |
290 | 211 | in = next; |
291 | 211 | goto read_retry; |
292 | 596 | } else { |
293 | 596 | OPENSSL_free(dirpath); |
294 | 596 | dirpath = NULL; |
295 | 596 | } |
296 | 807 | } |
297 | 13.3k | #endif |
298 | | /* no more files in directory, continue with processing parent */ |
299 | 13.3k | if ((parent = sk_BIO_pop(biosk)) == NULL) { |
300 | | /* everything processed get out of the loop */ |
301 | 12.7k | break; |
302 | 12.7k | } else { |
303 | 596 | BIO_vfree(in); |
304 | 596 | in = parent; |
305 | 596 | goto read_retry; |
306 | 596 | } |
307 | 13.3k | } |
308 | 672k | again = 0; |
309 | 1.17M | while (i > 0) { |
310 | 893k | if ((p[i - 1] != '\r') && (p[i - 1] != '\n')) |
311 | 393k | break; |
312 | 500k | else |
313 | 500k | i--; |
314 | 893k | } |
315 | | /* |
316 | | * we removed some trailing stuff so there is a new line on the end. |
317 | | */ |
318 | 672k | if (ii && i == ii) |
319 | 157k | again = 1; /* long line */ |
320 | 514k | else { |
321 | 514k | p[i] = '\0'; |
322 | 514k | eline++; /* another input line */ |
323 | 514k | } |
324 | | |
325 | | /* we now have a line with trailing \r\n removed */ |
326 | | |
327 | | /* i is the number of bytes */ |
328 | 672k | bufnum += i; |
329 | | |
330 | 672k | v = NULL; |
331 | | /* check for line continuation */ |
332 | 672k | if (!again && bufnum >= 1) { |
333 | | /* |
334 | | * If we have bytes and the last char '\\' and second last char |
335 | | * is not '\\' |
336 | | */ |
337 | 250k | p = &(buff->data[bufnum - 1]); |
338 | 250k | if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) { |
339 | 3.32k | bufnum--; |
340 | 3.32k | again = 1; |
341 | 3.32k | } |
342 | 250k | } |
343 | 672k | if (again) |
344 | 160k | continue; |
345 | 511k | bufnum = 0; |
346 | 511k | buf = buff->data; |
347 | | |
348 | 511k | clear_comments(conf, buf); |
349 | 511k | s = eat_ws(conf, buf); |
350 | 511k | if (IS_EOF(conf, *s)) |
351 | 348k | continue; /* blank line */ |
352 | 162k | if (*s == '[') { |
353 | 16.5k | char *ss; |
354 | | |
355 | 16.5k | s++; |
356 | 16.5k | start = eat_ws(conf, s); |
357 | 16.5k | ss = start; |
358 | 17.3k | again: |
359 | 17.3k | end = eat_alpha_numeric(conf, ss); |
360 | 17.3k | p = eat_ws(conf, end); |
361 | 17.3k | if (*p != ']') { |
362 | 1.07k | if (*p != '\0' && ss != p) { |
363 | 866 | ss = p; |
364 | 866 | goto again; |
365 | 866 | } |
366 | 1.07k | ERR_raise(ERR_LIB_CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET); |
367 | 210 | goto err; |
368 | 1.07k | } |
369 | 16.3k | *end = '\0'; |
370 | 16.3k | if (!str_copy(conf, NULL, §ion, start)) |
371 | 0 | goto err; |
372 | 16.3k | if ((sv = _CONF_get_section(conf, section)) == NULL) |
373 | 9.77k | sv = _CONF_new_section(conf, section); |
374 | 16.3k | if (sv == NULL) { |
375 | 0 | ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION); |
376 | 0 | goto err; |
377 | 0 | } |
378 | 16.3k | continue; |
379 | 146k | } else { |
380 | 146k | pname = s; |
381 | 146k | end = eat_alpha_numeric(conf, s); |
382 | 146k | if ((end[0] == ':') && (end[1] == ':')) { |
383 | 16.1k | *end = '\0'; |
384 | 16.1k | end += 2; |
385 | 16.1k | psection = pname; |
386 | 16.1k | pname = end; |
387 | 16.1k | end = eat_alpha_numeric(conf, end); |
388 | 130k | } else { |
389 | 130k | psection = section; |
390 | 130k | } |
391 | 146k | p = eat_ws(conf, end); |
392 | 146k | if (CHECK_AND_SKIP_PREFIX(pname, ".pragma") |
393 | 5.62k | && (p != pname || *p == '=')) { |
394 | 5.61k | char *pval; |
395 | | |
396 | 5.61k | if (*p == '=') { |
397 | 4.12k | p++; |
398 | 4.12k | p = eat_ws(conf, p); |
399 | 4.12k | } |
400 | 5.61k | trim_ws(conf, p); |
401 | | |
402 | | /* Pragma values take the form keyword:value */ |
403 | 5.61k | pval = strchr(p, ':'); |
404 | 5.61k | if (pval == NULL || pval == p || pval[1] == '\0') { |
405 | 85 | ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA); |
406 | 85 | goto err; |
407 | 85 | } |
408 | | |
409 | 5.52k | *pval++ = '\0'; |
410 | 5.52k | trim_ws(conf, p); |
411 | 5.52k | pval = eat_ws(conf, pval); |
412 | | |
413 | | /* |
414 | | * Known pragmas: |
415 | | * |
416 | | * dollarid takes "on", "true or "off", "false" |
417 | | * abspath takes "on", "true or "off", "false" |
418 | | * includedir directory prefix |
419 | | */ |
420 | 5.52k | if (strcmp(p, "dollarid") == 0) { |
421 | 1.21k | if (!parsebool(pval, &conf->flag_dollarid)) |
422 | 21 | goto err; |
423 | 4.31k | } else if (strcmp(p, "abspath") == 0) { |
424 | 1.34k | if (!parsebool(pval, &conf->flag_abspath)) |
425 | 63 | goto err; |
426 | 2.97k | } else if (strcmp(p, "includedir") == 0) { |
427 | 597 | OPENSSL_free(conf->includedir); |
428 | 597 | if ((conf->includedir = OPENSSL_strdup(pval)) == NULL) |
429 | 0 | goto err; |
430 | 597 | } |
431 | | |
432 | | /* |
433 | | * We *ignore* any unknown pragma. |
434 | | */ |
435 | 5.44k | continue; |
436 | 140k | } else if (CHECK_AND_SKIP_PREFIX(pname, ".include") |
437 | 2.21k | && (p != pname || *p == '=')) { |
438 | 2.20k | char *include = NULL; |
439 | 2.20k | BIO *next; |
440 | 2.20k | const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE"); |
441 | 2.20k | char *include_path = NULL; |
442 | | |
443 | 2.20k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
444 | | /* |
445 | | * The include processing below can cause the "conf" fuzzer to |
446 | | * timeout due to the fuzzer inserting large and complicated |
447 | | * includes - with a large amount of time spent in |
448 | | * OPENSSL_strlcat/OPENSSL_strcpy. This is not a security |
449 | | * concern because config files should never come from untrusted |
450 | | * sources. We just set an arbitrary limit on the allowed |
451 | | * number of includes when fuzzing to prevent this timeout. |
452 | | */ |
453 | 2.20k | if (numincludes++ > 10) |
454 | 13 | goto err; |
455 | 2.18k | #endif |
456 | | |
457 | 2.18k | if (include_dir == NULL) |
458 | 2.18k | include_dir = conf->includedir; |
459 | | |
460 | 2.18k | if (*p == '=') { |
461 | 627 | p++; |
462 | 627 | p = eat_ws(conf, p); |
463 | 627 | } |
464 | 2.18k | trim_ws(conf, p); |
465 | 2.18k | if (!str_copy(conf, psection, &include, p)) |
466 | 3 | goto err; |
467 | | |
468 | 2.18k | if (include_dir != NULL && !ossl_is_absolute_path(include)) { |
469 | 371 | const char *fmt = ossl_ends_with_dirsep(include_dir) |
470 | 371 | ? "%s%s" |
471 | 371 | : "%s/%s"; |
472 | | |
473 | 371 | if (ossl_asprintf(&include_path, fmt, |
474 | 371 | include_dir, include) |
475 | 371 | < 0) { |
476 | 0 | OPENSSL_free(include); |
477 | 0 | goto err; |
478 | 0 | } |
479 | 371 | OPENSSL_free(include); |
480 | 1.81k | } else { |
481 | 1.81k | include_path = include; |
482 | 1.81k | } |
483 | | |
484 | 2.18k | if (conf->flag_abspath |
485 | 47 | && !ossl_is_absolute_path(include_path)) { |
486 | 11 | ERR_raise(ERR_LIB_CONF, CONF_R_RELATIVE_PATH); |
487 | 11 | OPENSSL_free(include_path); |
488 | 11 | goto err; |
489 | 11 | } |
490 | | |
491 | | /* get the BIO of the included file */ |
492 | 2.17k | #ifndef OPENSSL_NO_POSIX_IO |
493 | 2.17k | next = process_include(include_path, &dirctx, &dirpath); |
494 | 2.17k | if (include_path != dirpath) { |
495 | | /* dirpath will contain include in case of a directory */ |
496 | 1.52k | OPENSSL_free(include_path); |
497 | 1.52k | } |
498 | | #else |
499 | | next = BIO_new_file(include_path, "r"); |
500 | | OPENSSL_free(include_path); |
501 | | #endif |
502 | | |
503 | 2.17k | if (next != NULL) { |
504 | | /* push the currently processing BIO onto stack */ |
505 | 663 | if (biosk == NULL) { |
506 | 294 | if ((biosk = sk_BIO_new_null()) == NULL) { |
507 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB); |
508 | 0 | BIO_free(next); |
509 | 0 | goto err; |
510 | 0 | } |
511 | 294 | } |
512 | 663 | if (!sk_BIO_push(biosk, in)) { |
513 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB); |
514 | 0 | BIO_free(next); |
515 | 0 | goto err; |
516 | 0 | } |
517 | | /* continue with reading from the included BIO */ |
518 | 663 | in = next; |
519 | 663 | } |
520 | 2.17k | continue; |
521 | 138k | } else if (*p != '=') { |
522 | 1.08k | ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN, |
523 | 1.08k | "HERE-->%s", p); |
524 | 1.08k | goto err; |
525 | 1.08k | } |
526 | 137k | *end = '\0'; |
527 | 137k | p++; |
528 | 137k | start = eat_ws(conf, p); |
529 | 137k | trim_ws(conf, start); |
530 | | |
531 | 137k | if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) |
532 | 0 | goto err; |
533 | 137k | v->name = OPENSSL_strdup(pname); |
534 | 137k | v->value = NULL; |
535 | 137k | if (v->name == NULL) |
536 | 0 | goto err; |
537 | 137k | if (!str_copy(conf, psection, &(v->value), start)) |
538 | 585 | goto err; |
539 | | |
540 | 136k | if (strcmp(psection, section) != 0) { |
541 | 16.0k | if ((tv = _CONF_get_section(conf, psection)) |
542 | 16.0k | == NULL) |
543 | 12.6k | tv = _CONF_new_section(conf, psection); |
544 | 16.0k | if (tv == NULL) { |
545 | 0 | ERR_raise(ERR_LIB_CONF, |
546 | 0 | CONF_R_UNABLE_TO_CREATE_NEW_SECTION); |
547 | 0 | goto err; |
548 | 0 | } |
549 | 16.0k | } else |
550 | 120k | tv = sv; |
551 | 136k | if (_CONF_add_string(conf, tv, v) == 0) { |
552 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_CONF_LIB); |
553 | 0 | goto err; |
554 | 0 | } |
555 | 136k | v = NULL; |
556 | 136k | } |
557 | 162k | } |
558 | 12.7k | BUF_MEM_free(buff); |
559 | 12.7k | OPENSSL_free(section); |
560 | | /* |
561 | | * No need to pop, since we only get here if the stack is empty. |
562 | | * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE! |
563 | | */ |
564 | 12.7k | sk_BIO_free(biosk); |
565 | 12.7k | return 1; |
566 | | |
567 | 2.07k | err: |
568 | 2.07k | BUF_MEM_free(buff); |
569 | 2.07k | OPENSSL_free(section); |
570 | | /* |
571 | | * Since |in| is the first element of the stack and should NOT be freed |
572 | | * here, we cannot use sk_BIO_pop_free(). Instead, we pop and free one |
573 | | * BIO at a time, making sure that the last one popped isn't. |
574 | | */ |
575 | 2.13k | while (sk_BIO_num(biosk) > 0) { |
576 | 67 | BIO *popped = sk_BIO_pop(biosk); |
577 | 67 | BIO_vfree(in); |
578 | 67 | in = popped; |
579 | 67 | } |
580 | 2.07k | sk_BIO_free(biosk); |
581 | 2.07k | #ifndef OPENSSL_NO_POSIX_IO |
582 | 2.07k | OPENSSL_free(dirpath); |
583 | 2.07k | if (dirctx != NULL) |
584 | 54 | OPENSSL_DIR_end(&dirctx); |
585 | 2.07k | #endif |
586 | 2.07k | if (line != NULL) |
587 | 1.17k | *line = eline; |
588 | 2.07k | snprintf(btmp, sizeof(btmp), "%ld", eline); |
589 | 2.07k | ERR_add_error_data(2, "line ", btmp); |
590 | 2.07k | if (h != conf->data) { |
591 | 2.07k | CONF_free(conf->data); |
592 | 2.07k | conf->data = NULL; |
593 | 2.07k | } |
594 | 2.07k | if (v != NULL) { |
595 | 585 | OPENSSL_free(v->name); |
596 | 585 | OPENSSL_free(v->value); |
597 | 585 | OPENSSL_free(v); |
598 | 585 | } |
599 | 2.07k | return 0; |
600 | 14.8k | } |
601 | | |
602 | | static void clear_comments(CONF *conf, char *p) |
603 | 20.1M | { |
604 | 20.1M | for (;;) { |
605 | 20.1M | if (IS_FCOMMENT(conf, *p)) { |
606 | 0 | *p = '\0'; |
607 | 0 | return; |
608 | 0 | } |
609 | 20.1M | if (!IS_WS(conf, *p)) { |
610 | 20.1M | break; |
611 | 20.1M | } |
612 | 54.1k | p++; |
613 | 54.1k | } |
614 | | |
615 | 331M | for (;;) { |
616 | 331M | if (IS_COMMENT(conf, *p)) { |
617 | 273k | *p = '\0'; |
618 | 273k | return; |
619 | 273k | } |
620 | 330M | if (IS_DQUOTE(conf, *p)) { |
621 | 0 | p = scan_dquote(conf, p); |
622 | 0 | continue; |
623 | 0 | } |
624 | 330M | if (IS_QUOTE(conf, *p)) { |
625 | 1.31M | p = scan_quote(conf, p); |
626 | 1.31M | continue; |
627 | 1.31M | } |
628 | 329M | if (IS_ESC(conf, *p)) { |
629 | 1.24M | p = scan_esc(conf, p); |
630 | 1.24M | continue; |
631 | 1.24M | } |
632 | 328M | if (IS_EOF(conf, *p)) |
633 | 19.8M | return; |
634 | 308M | else |
635 | 308M | p++; |
636 | 328M | } |
637 | 20.1M | } |
638 | | |
639 | | static int str_copy(CONF *conf, char *section, char **pto, char *from) |
640 | 155k | { |
641 | 155k | int q, r, rr = 0, to = 0; |
642 | 155k | char *s, *e, *rp, *p, *rrp, *np, *cp, v; |
643 | 155k | BUF_MEM *buf; |
644 | 155k | int ret = 0; |
645 | | |
646 | 155k | if ((buf = BUF_MEM_new()) == NULL) |
647 | 0 | return 0; |
648 | | |
649 | 155k | if (!BUF_MEM_grow(buf, strlen(from) + 1)) |
650 | 0 | goto err; |
651 | | |
652 | 17.4M | for (;;) { |
653 | 17.4M | if (IS_QUOTE(conf, *from)) { |
654 | 7.50k | q = *from; |
655 | 7.50k | from++; |
656 | 5.46M | while (!IS_EOF(conf, *from) && (*from != q)) { |
657 | 5.45M | if (IS_ESC(conf, *from)) { |
658 | 15.4k | from++; |
659 | 15.4k | if (IS_EOF(conf, *from)) |
660 | 1.07k | break; |
661 | 15.4k | } |
662 | 5.45M | buf->data[to++] = *(from++); |
663 | 5.45M | } |
664 | 7.50k | if (*from == q) |
665 | 3.86k | from++; |
666 | 17.4M | } else if (IS_DQUOTE(conf, *from)) { |
667 | 0 | q = *from; |
668 | 0 | from++; |
669 | 0 | while (!IS_EOF(conf, *from)) { |
670 | 0 | if (*from == q) { |
671 | 0 | if (*(from + 1) == q) { |
672 | 0 | from++; |
673 | 0 | } else { |
674 | 0 | break; |
675 | 0 | } |
676 | 0 | } |
677 | 0 | buf->data[to++] = *(from++); |
678 | 0 | } |
679 | 0 | if (*from == q) |
680 | 0 | from++; |
681 | 17.4M | } else if (IS_ESC(conf, *from)) { |
682 | 29.8k | from++; |
683 | 29.8k | v = *(from++); |
684 | 29.8k | if (IS_EOF(conf, v)) |
685 | 994 | break; |
686 | 28.8k | else if (v == 'r') |
687 | 689 | v = '\r'; |
688 | 28.1k | else if (v == 'n') |
689 | 694 | v = '\n'; |
690 | 27.4k | else if (v == 'b') |
691 | 762 | v = '\b'; |
692 | 26.7k | else if (v == 't') |
693 | 2.52k | v = '\t'; |
694 | 28.8k | buf->data[to++] = v; |
695 | 17.4M | } else if (IS_EOF(conf, *from)) |
696 | 154k | break; |
697 | 17.2M | else if (*from == '$' |
698 | 59.5k | && (!conf->flag_dollarid |
699 | 2.14k | || from[1] == '{' |
700 | 58.2k | || from[1] == '(')) { |
701 | 58.2k | size_t newsize; |
702 | | |
703 | | /* try to expand it */ |
704 | 58.2k | rrp = NULL; |
705 | 58.2k | s = &(from[1]); |
706 | 58.2k | if (*s == '{') |
707 | 1.03k | q = '}'; |
708 | 57.2k | else if (*s == '(') |
709 | 1.02k | q = ')'; |
710 | 56.2k | else |
711 | 56.2k | q = 0; |
712 | | |
713 | 58.2k | if (q) |
714 | 2.05k | s++; |
715 | 58.2k | cp = section; |
716 | 58.2k | e = np = s; |
717 | 3.97M | while (IS_ALNUM(conf, *e) |
718 | 58.6k | || (conf->flag_dollarid && IS_DOLLAR(conf, *e))) |
719 | 3.91M | e++; |
720 | 58.2k | if ((e[0] == ':') && (e[1] == ':')) { |
721 | 1.78k | cp = np; |
722 | 1.78k | rrp = e; |
723 | 1.78k | rr = *e; |
724 | 1.78k | *rrp = '\0'; |
725 | 1.78k | e += 2; |
726 | 1.78k | np = e; |
727 | 2.99k | while (IS_ALNUM(conf, *e) |
728 | 2.15k | || (conf->flag_dollarid && IS_DOLLAR(conf, *e))) |
729 | 1.20k | e++; |
730 | 1.78k | } |
731 | 58.2k | r = *e; |
732 | 58.2k | *e = '\0'; |
733 | 58.2k | rp = e; |
734 | 58.2k | if (q) { |
735 | 2.05k | if (r != q) { |
736 | 120 | ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE); |
737 | 120 | goto err; |
738 | 120 | } |
739 | 1.93k | e++; |
740 | 1.93k | } |
741 | | /*- |
742 | | * So at this point we have |
743 | | * np which is the start of the name string which is |
744 | | * '\0' terminated. |
745 | | * cp which is the start of the section string which is |
746 | | * '\0' terminated. |
747 | | * e is the 'next point after'. |
748 | | * r and rr are the chars replaced by the '\0' |
749 | | * rp and rrp is where 'r' and 'rr' came from. |
750 | | */ |
751 | 58.1k | p = _CONF_get_string(conf, cp, np); |
752 | 58.1k | if (rrp != NULL) |
753 | 1.76k | *rrp = rr; |
754 | 58.1k | *rp = r; |
755 | 58.1k | if (p == NULL) { |
756 | 454 | ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_HAS_NO_VALUE); |
757 | 454 | goto err; |
758 | 454 | } |
759 | 57.6k | newsize = strlen(p) + buf->length - (e - from); |
760 | 57.6k | if (newsize > MAX_CONF_VALUE_LENGTH) { |
761 | 14 | ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG); |
762 | 14 | goto err; |
763 | 14 | } |
764 | 57.6k | if (!BUF_MEM_grow_clean(buf, newsize)) { |
765 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB); |
766 | 0 | goto err; |
767 | 0 | } |
768 | 21.8M | while (*p) |
769 | 21.8M | buf->data[to++] = *(p++); |
770 | | |
771 | 57.6k | from = e; |
772 | | |
773 | | /* |
774 | | * In case there were no braces or parenthesis around the |
775 | | * variable reference, we have to put back the character that was |
776 | | * replaced with a '\0'. /RL |
777 | | */ |
778 | 57.6k | *rp = r; |
779 | 57.6k | } else |
780 | 17.2M | buf->data[to++] = *(from++); |
781 | 17.4M | } |
782 | 155k | buf->data[to] = '\0'; |
783 | 155k | OPENSSL_free(*pto); |
784 | 155k | *pto = buf->data; |
785 | | /* Take ownership of the buf mem data */ |
786 | 155k | buf->data = NULL; |
787 | 155k | ret = 1; |
788 | 155k | err: |
789 | 155k | BUF_MEM_free(buf); |
790 | 155k | return ret; |
791 | 155k | } |
792 | | |
793 | | #ifndef OPENSSL_NO_POSIX_IO |
794 | | /* |
795 | | * Check whether included path is a directory. |
796 | | * Returns next BIO to process and in case of a directory |
797 | | * also an opened directory context and the include path. |
798 | | */ |
799 | | static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx, |
800 | | char **dirpath) |
801 | 7.94k | { |
802 | 7.94k | struct stat st; |
803 | 7.94k | BIO *next; |
804 | | |
805 | 7.94k | if (stat(include, &st) < 0) { |
806 | 4.42k | ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include); |
807 | | /* missing include file is not fatal error */ |
808 | 4.42k | return NULL; |
809 | 4.42k | } |
810 | | |
811 | 3.51k | if (S_ISDIR(st.st_mode)) { |
812 | 3.48k | if (*dirctx != NULL) { |
813 | 0 | ERR_raise_data(ERR_LIB_CONF, CONF_R_RECURSIVE_DIRECTORY_INCLUDE, |
814 | 0 | "%s", include); |
815 | 0 | return NULL; |
816 | 0 | } |
817 | | /* a directory, load its contents */ |
818 | 3.48k | if ((next = get_next_file(include, dirctx)) != NULL) |
819 | 1.83k | *dirpath = include; |
820 | 3.48k | return next; |
821 | 3.48k | } |
822 | | |
823 | 33 | next = BIO_new_file(include, "r"); |
824 | 33 | return next; |
825 | 3.51k | } |
826 | | |
827 | | /* |
828 | | * Get next file from the directory path. |
829 | | * Returns BIO of the next file to read and updates dirctx. |
830 | | */ |
831 | | static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx) |
832 | 1.81k | { |
833 | 1.81k | const char *filename; |
834 | | |
835 | 80.4k | while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) { |
836 | 79.4k | size_t namelen; |
837 | | |
838 | 79.4k | namelen = strlen(filename); |
839 | | |
840 | 79.4k | if ((namelen > 5 |
841 | 60.0k | && OPENSSL_strcasecmp(filename + namelen - 5, ".conf") == 0) |
842 | 76.1k | || (namelen > 4 |
843 | 64.5k | && OPENSSL_strcasecmp(filename + namelen - 4, ".cnf") == 0)) { |
844 | 3.73k | const char *fmt = "%s/%s"; |
845 | 3.73k | char *newpath; |
846 | 3.73k | BIO *bio; |
847 | | |
848 | | #ifdef OPENSSL_SYS_VMS |
849 | | size_t pathlen = strlen(path); |
850 | | |
851 | | /* |
852 | | * If the given path isn't clear VMS syntax, |
853 | | * we treat it as on Unix. |
854 | | */ |
855 | | if (path[pathlen - 1] == ']' |
856 | | || path[pathlen - 1] == '>' |
857 | | || path[pathlen - 1] == ':') |
858 | | /* Clear VMS directory syntax, no separator is needed */ |
859 | | fmt = "%s%s"; |
860 | | #endif |
861 | 3.73k | if (ossl_asprintf(&newpath, fmt, path, filename) < 0) |
862 | 0 | break; |
863 | | |
864 | 3.73k | bio = BIO_new_file(newpath, "r"); |
865 | 3.73k | OPENSSL_free(newpath); |
866 | | /* Errors when opening files are non-fatal. */ |
867 | 3.73k | if (bio != NULL) |
868 | 861 | return bio; |
869 | 3.73k | } |
870 | 79.4k | } |
871 | 955 | OPENSSL_DIR_end(dirctx); |
872 | 955 | *dirctx = NULL; |
873 | 955 | return NULL; |
874 | 1.81k | } |
875 | | #endif |
876 | | |
877 | | static int is_keytype(const CONF *conf, char c, unsigned short type) |
878 | 2.79G | { |
879 | 2.79G | const unsigned short *keytypes = (const unsigned short *)conf->meth_data; |
880 | 2.79G | unsigned char key = (unsigned char)c; |
881 | | |
882 | | #ifdef CHARSET_EBCDIC |
883 | | #if CHAR_BIT > 8 |
884 | | if (key > 255) { |
885 | | /* key is out of range for os_toascii table */ |
886 | | return 0; |
887 | | } |
888 | | #endif |
889 | | /* convert key from ebcdic to ascii */ |
890 | | key = os_toascii[key]; |
891 | | #endif |
892 | | |
893 | 2.79G | if (key > 127) { |
894 | | /* key is not a seven bit ascii character */ |
895 | 185M | return 0; |
896 | 185M | } |
897 | | |
898 | 2.60G | return (keytypes[key] & type) ? 1 : 0; |
899 | 2.79G | } |
900 | | |
901 | | static char *eat_ws(CONF *conf, char *p) |
902 | 21.1M | { |
903 | 21.5M | while (IS_WS(conf, *p) && (!IS_EOF(conf, *p))) |
904 | 418k | p++; |
905 | 21.1M | return p; |
906 | 21.1M | } |
907 | | |
908 | | static void trim_ws(CONF *conf, char *start) |
909 | 498k | { |
910 | 498k | char *p = start; |
911 | | |
912 | 183M | while (!IS_EOF(conf, *p)) |
913 | 182M | p++; |
914 | 498k | p--; |
915 | 578k | while ((p >= start) && IS_WS(conf, *p)) |
916 | 80.5k | p--; |
917 | 498k | p++; |
918 | 498k | *p = '\0'; |
919 | 498k | } |
920 | | |
921 | | static char *eat_alpha_numeric(CONF *conf, char *p) |
922 | 608k | { |
923 | 135M | for (;;) { |
924 | 135M | if (IS_ESC(conf, *p)) { |
925 | 707k | p = scan_esc(conf, p); |
926 | 707k | continue; |
927 | 707k | } |
928 | 134M | if (!(IS_ALNUM_PUNCT(conf, *p) |
929 | 612k | || (conf->flag_dollarid && IS_DOLLAR(conf, *p)))) |
930 | 608k | return p; |
931 | 134M | p++; |
932 | 134M | } |
933 | 608k | } |
934 | | |
935 | | static char *scan_quote(CONF *conf, char *p) |
936 | 1.31M | { |
937 | 1.31M | int q = *p; |
938 | | |
939 | 1.31M | p++; |
940 | 59.6M | while (!(IS_EOF(conf, *p)) && (*p != q)) { |
941 | 58.3M | if (IS_ESC(conf, *p)) { |
942 | 232k | p++; |
943 | 232k | if (IS_EOF(conf, *p)) |
944 | 2.81k | return p; |
945 | 232k | } |
946 | 58.3M | p++; |
947 | 58.3M | } |
948 | 1.30M | if (*p == q) |
949 | 1.29M | p++; |
950 | 1.30M | return p; |
951 | 1.31M | } |
952 | | |
953 | | static char *scan_dquote(CONF *conf, char *p) |
954 | 0 | { |
955 | 0 | int q = *p; |
956 | |
|
957 | 0 | p++; |
958 | 0 | while (!(IS_EOF(conf, *p))) { |
959 | 0 | if (*p == q) { |
960 | 0 | if (*(p + 1) == q) { |
961 | 0 | p++; |
962 | 0 | } else { |
963 | 0 | break; |
964 | 0 | } |
965 | 0 | } |
966 | 0 | p++; |
967 | 0 | } |
968 | 0 | if (*p == q) |
969 | 0 | p++; |
970 | 0 | return p; |
971 | 0 | } |
972 | | |
973 | | static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out) |
974 | 0 | { |
975 | 0 | if (a->name) |
976 | 0 | BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value); |
977 | 0 | else |
978 | 0 | BIO_printf(out, "[[%s]]\n", a->section); |
979 | 0 | } |
980 | | |
981 | | IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO); |
982 | | |
983 | | static int def_dump(const CONF *conf, BIO *out) |
984 | 0 | { |
985 | 0 | lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out); |
986 | 0 | return 1; |
987 | 0 | } |
988 | | |
989 | | static int def_is_number(const CONF *conf, char c) |
990 | 0 | { |
991 | 0 | return IS_NUMBER(conf, c); |
992 | 0 | } |
993 | | |
994 | | static int def_to_int(const CONF *conf, char c) |
995 | 0 | { |
996 | 0 | return c - '0'; |
997 | 0 | } |