/src/openssl36/crypto/conf/conf_def.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2025 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 | 95.0k | #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.37M | #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 | 16.9k | { |
88 | 16.9k | return &default_method; |
89 | 16.9k | } |
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 | 16.9k | { |
113 | 16.9k | CONF *ret; |
114 | | |
115 | 16.9k | ret = OPENSSL_malloc(sizeof(*ret)); |
116 | 16.9k | if (ret != NULL) |
117 | 16.9k | if (meth->init(ret) == 0) { |
118 | 0 | OPENSSL_free(ret); |
119 | 0 | ret = NULL; |
120 | 0 | } |
121 | 16.9k | return ret; |
122 | 16.9k | } |
123 | | |
124 | | static int def_init_default(CONF *conf) |
125 | 23.6k | { |
126 | 23.6k | if (conf == NULL) |
127 | 0 | return 0; |
128 | | |
129 | 23.6k | memset(conf, 0, sizeof(*conf)); |
130 | 23.6k | conf->meth = &default_method; |
131 | 23.6k | conf->meth_data = (void *)CONF_type_default; |
132 | | |
133 | 23.6k | return 1; |
134 | 23.6k | } |
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 | 16.9k | { |
152 | 16.9k | if (def_destroy_data(conf)) { |
153 | 16.9k | OPENSSL_free(conf); |
154 | 16.9k | return 1; |
155 | 16.9k | } |
156 | 0 | return 0; |
157 | 16.9k | } |
158 | | |
159 | | static int def_destroy_data(CONF *conf) |
160 | 23.6k | { |
161 | 23.6k | if (conf == NULL) |
162 | 0 | return 0; |
163 | 23.6k | _CONF_free_data(conf); |
164 | 23.6k | return 1; |
165 | 23.6k | } |
166 | | |
167 | | static int def_load(CONF *conf, const char *name, long *line) |
168 | 185 | { |
169 | 185 | int ret; |
170 | 185 | BIO *in = NULL; |
171 | | |
172 | | #ifdef OPENSSL_SYS_VMS |
173 | | in = BIO_new_file(name, "r"); |
174 | | #else |
175 | 185 | in = BIO_new_file(name, "rb"); |
176 | 185 | #endif |
177 | 185 | if (in == NULL) { |
178 | 185 | if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE) |
179 | 185 | ERR_raise(ERR_LIB_CONF, CONF_R_NO_SUCH_FILE); |
180 | 0 | else |
181 | 185 | ERR_raise(ERR_LIB_CONF, ERR_R_SYS_LIB); |
182 | 185 | return 0; |
183 | 185 | } |
184 | | |
185 | 0 | ret = def_load_bio(conf, in, line); |
186 | 0 | BIO_free(in); |
187 | |
|
188 | 0 | return ret; |
189 | 185 | } |
190 | | |
191 | | /* Parse a boolean value and fill in *flag. Return 0 on error. */ |
192 | | static int parsebool(const char *pval, int *flag) |
193 | 8.51k | { |
194 | 8.51k | if (OPENSSL_strcasecmp(pval, "on") == 0 |
195 | 5.64k | || OPENSSL_strcasecmp(pval, "true") == 0) { |
196 | 5.64k | *flag = 1; |
197 | 5.64k | } else if (OPENSSL_strcasecmp(pval, "off") == 0 |
198 | 2.61k | || OPENSSL_strcasecmp(pval, "false") == 0) { |
199 | 2.61k | *flag = 0; |
200 | 2.61k | } else { |
201 | 263 | ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA); |
202 | 263 | return 0; |
203 | 263 | } |
204 | 8.25k | return 1; |
205 | 8.51k | } |
206 | | |
207 | | static int def_load_bio(CONF *conf, BIO *in, long *line) |
208 | 15.7k | { |
209 | | /* The macro BUFSIZE conflicts with a system macro in VxWorks */ |
210 | 89.5M | #define CONFBUFSIZE 512 |
211 | 15.7k | int bufnum = 0, i, ii; |
212 | 15.7k | BUF_MEM *buff = NULL; |
213 | 15.7k | char *s, *p, *end; |
214 | 15.7k | int again; |
215 | 15.7k | int first_call = 1; |
216 | 15.7k | long eline = 0; |
217 | 15.7k | char btmp[DECIMAL_SIZE(eline) + 1]; |
218 | 15.7k | CONF_VALUE *v = NULL, *tv; |
219 | 15.7k | CONF_VALUE *sv = NULL; |
220 | 15.7k | char *section = NULL, *buf; |
221 | 15.7k | char *start, *psection, *pname; |
222 | 15.7k | void *h = (void *)(conf->data); |
223 | 15.7k | STACK_OF(BIO) *biosk = NULL; |
224 | 15.7k | #ifndef OPENSSL_NO_POSIX_IO |
225 | 15.7k | char *dirpath = NULL; |
226 | 15.7k | OPENSSL_DIR_CTX *dirctx = NULL; |
227 | 15.7k | #endif |
228 | 15.7k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
229 | 15.7k | int numincludes = 0; |
230 | 15.7k | #endif |
231 | | |
232 | 15.7k | 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 | 15.7k | section = OPENSSL_strdup("default"); |
238 | 15.7k | if (section == NULL) |
239 | 0 | goto err; |
240 | | |
241 | 15.7k | 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 | 15.7k | sv = _CONF_new_section(conf, section); |
247 | 15.7k | 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 | 15.7k | bufnum = 0; |
253 | 15.7k | again = 0; |
254 | 29.8M | for (;;) { |
255 | 29.8M | 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 | 29.8M | p = &(buff->data[bufnum]); |
260 | 29.8M | *p = '\0'; |
261 | 29.8M | read_retry: |
262 | 29.8M | if (in != NULL && BIO_gets(in, p, CONFBUFSIZE - 1) < 0) |
263 | 0 | goto err; |
264 | 29.8M | p[CONFBUFSIZE - 1] = '\0'; |
265 | 29.8M | ii = i = (int)strlen(p); |
266 | 29.8M | if (first_call) { |
267 | | /* Other BOMs imply unsupported multibyte encoding, |
268 | | * so don't strip them and let the error raise */ |
269 | 15.7k | const unsigned char utf8_bom[3] = { 0xEF, 0xBB, 0xBF }; |
270 | | |
271 | 15.7k | if (i >= 3 && memcmp(p, utf8_bom, 3) == 0) { |
272 | 133 | memmove(p, p + 3, i - 3); |
273 | 133 | p[i - 3] = 0; |
274 | 133 | i -= 3; |
275 | 133 | ii -= 3; |
276 | 133 | } |
277 | 15.7k | first_call = 0; |
278 | 15.7k | } |
279 | 29.8M | if (i == 0 && !again) { |
280 | | /* the currently processed BIO is NULL or at EOF */ |
281 | 11.1k | BIO *parent; |
282 | | |
283 | 11.1k | #ifndef OPENSSL_NO_POSIX_IO |
284 | | /* continue processing with the next file from directory */ |
285 | 11.1k | if (dirctx != NULL) { |
286 | 1.67k | BIO *next; |
287 | | |
288 | 1.67k | if ((next = get_next_file(dirpath, &dirctx)) != NULL) { |
289 | 534 | BIO_vfree(in); |
290 | 534 | in = next; |
291 | 534 | goto read_retry; |
292 | 1.14k | } else { |
293 | 1.14k | OPENSSL_free(dirpath); |
294 | 1.14k | dirpath = NULL; |
295 | 1.14k | } |
296 | 1.67k | } |
297 | 10.6k | #endif |
298 | | /* no more files in directory, continue with processing parent */ |
299 | 10.6k | if ((parent = sk_BIO_pop(biosk)) == NULL) { |
300 | | /* everything processed get out of the loop */ |
301 | 9.46k | break; |
302 | 9.46k | } else { |
303 | 1.14k | BIO_vfree(in); |
304 | 1.14k | in = parent; |
305 | 1.14k | goto read_retry; |
306 | 1.14k | } |
307 | 10.6k | } |
308 | 29.8M | again = 0; |
309 | 59.0M | while (i > 0) { |
310 | 30.2M | if ((p[i - 1] != '\r') && (p[i - 1] != '\n')) |
311 | 1.04M | break; |
312 | 29.2M | else |
313 | 29.2M | i--; |
314 | 30.2M | } |
315 | | /* |
316 | | * we removed some trailing stuff so there is a new line on the end. |
317 | | */ |
318 | 29.8M | if (ii && i == ii) |
319 | 558k | again = 1; /* long line */ |
320 | 29.2M | else { |
321 | 29.2M | p[i] = '\0'; |
322 | 29.2M | eline++; /* another input line */ |
323 | 29.2M | } |
324 | | |
325 | | /* we now have a line with trailing \r\n removed */ |
326 | | |
327 | | /* i is the number of bytes */ |
328 | 29.8M | bufnum += i; |
329 | | |
330 | 29.8M | v = NULL; |
331 | | /* check for line continuation */ |
332 | 29.8M | if (!again && bufnum >= 1) { |
333 | | /* |
334 | | * If we have bytes and the last char '\\' and second last char |
335 | | * is not '\\' |
336 | | */ |
337 | 502k | p = &(buff->data[bufnum - 1]); |
338 | 502k | if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) { |
339 | 18.2k | bufnum--; |
340 | 18.2k | again = 1; |
341 | 18.2k | } |
342 | 502k | } |
343 | 29.8M | if (again) |
344 | 577k | continue; |
345 | 29.2M | bufnum = 0; |
346 | 29.2M | buf = buff->data; |
347 | | |
348 | 29.2M | clear_comments(conf, buf); |
349 | 29.2M | s = eat_ws(conf, buf); |
350 | 29.2M | if (IS_EOF(conf, *s)) |
351 | 28.9M | continue; /* blank line */ |
352 | 321k | if (*s == '[') { |
353 | 29.1k | char *ss; |
354 | | |
355 | 29.1k | s++; |
356 | 29.1k | start = eat_ws(conf, s); |
357 | 29.1k | ss = start; |
358 | 33.2k | again: |
359 | 33.2k | end = eat_alpha_numeric(conf, ss); |
360 | 33.2k | p = eat_ws(conf, end); |
361 | 33.2k | if (*p != ']') { |
362 | 4.72k | if (*p != '\0' && ss != p) { |
363 | 4.11k | ss = p; |
364 | 4.11k | goto again; |
365 | 4.11k | } |
366 | 4.72k | ERR_raise(ERR_LIB_CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET); |
367 | 616 | goto err; |
368 | 4.72k | } |
369 | 28.5k | *end = '\0'; |
370 | 28.5k | if (!str_copy(conf, NULL, §ion, start)) |
371 | 0 | goto err; |
372 | 28.5k | if ((sv = _CONF_get_section(conf, section)) == NULL) |
373 | 18.3k | sv = _CONF_new_section(conf, section); |
374 | 28.5k | if (sv == NULL) { |
375 | 0 | ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION); |
376 | 0 | goto err; |
377 | 0 | } |
378 | 28.5k | continue; |
379 | 292k | } else { |
380 | 292k | pname = s; |
381 | 292k | end = eat_alpha_numeric(conf, s); |
382 | 292k | if ((end[0] == ':') && (end[1] == ':')) { |
383 | 55.0k | *end = '\0'; |
384 | 55.0k | end += 2; |
385 | 55.0k | psection = pname; |
386 | 55.0k | pname = end; |
387 | 55.0k | end = eat_alpha_numeric(conf, end); |
388 | 237k | } else { |
389 | 237k | psection = section; |
390 | 237k | } |
391 | 292k | p = eat_ws(conf, end); |
392 | 292k | if (CHECK_AND_SKIP_PREFIX(pname, ".pragma") |
393 | 16.3k | && (p != pname || *p == '=')) { |
394 | 16.3k | char *pval; |
395 | | |
396 | 16.3k | if (*p == '=') { |
397 | 10.2k | p++; |
398 | 10.2k | p = eat_ws(conf, p); |
399 | 10.2k | } |
400 | 16.3k | trim_ws(conf, p); |
401 | | |
402 | | /* Pragma values take the form keyword:value */ |
403 | 16.3k | pval = strchr(p, ':'); |
404 | 16.3k | if (pval == NULL || pval == p || pval[1] == '\0') { |
405 | 248 | ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA); |
406 | 248 | goto err; |
407 | 248 | } |
408 | | |
409 | 16.1k | *pval++ = '\0'; |
410 | 16.1k | trim_ws(conf, p); |
411 | 16.1k | 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 | 16.1k | if (strcmp(p, "dollarid") == 0) { |
421 | 5.07k | if (!parsebool(pval, &conf->flag_dollarid)) |
422 | 47 | goto err; |
423 | 11.0k | } else if (strcmp(p, "abspath") == 0) { |
424 | 2.21k | if (!parsebool(pval, &conf->flag_abspath)) |
425 | 208 | goto err; |
426 | 8.80k | } else if (strcmp(p, "includedir") == 0) { |
427 | 1.65k | OPENSSL_free(conf->includedir); |
428 | 1.65k | if ((conf->includedir = OPENSSL_strdup(pval)) == NULL) |
429 | 0 | goto err; |
430 | 1.65k | } |
431 | | |
432 | | /* |
433 | | * We *ignore* any unknown pragma. |
434 | | */ |
435 | 15.8k | continue; |
436 | 276k | } else if (CHECK_AND_SKIP_PREFIX(pname, ".include") |
437 | 5.19k | && (p != pname || *p == '=')) { |
438 | 5.15k | char *include = NULL; |
439 | 5.15k | BIO *next; |
440 | 5.15k | const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE"); |
441 | 5.15k | char *include_path = NULL; |
442 | | |
443 | 5.15k | #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 | 5.15k | if (numincludes++ > 10) |
454 | 28 | goto err; |
455 | 5.12k | #endif |
456 | | |
457 | 5.12k | if (include_dir == NULL) |
458 | 5.12k | include_dir = conf->includedir; |
459 | | |
460 | 5.12k | if (*p == '=') { |
461 | 1.11k | p++; |
462 | 1.11k | p = eat_ws(conf, p); |
463 | 1.11k | } |
464 | 5.12k | trim_ws(conf, p); |
465 | 5.12k | if (!str_copy(conf, psection, &include, p)) |
466 | 15 | goto err; |
467 | | |
468 | 5.11k | if (include_dir != NULL && !ossl_is_absolute_path(include)) { |
469 | 987 | size_t newlen = strlen(include_dir) + strlen(include) + 2; |
470 | | |
471 | 987 | include_path = OPENSSL_malloc(newlen); |
472 | 987 | if (include_path == NULL) { |
473 | 0 | OPENSSL_free(include); |
474 | 0 | goto err; |
475 | 0 | } |
476 | | |
477 | 987 | OPENSSL_strlcpy(include_path, include_dir, newlen); |
478 | 987 | if (!ossl_ends_with_dirsep(include_path)) |
479 | 845 | OPENSSL_strlcat(include_path, "/", newlen); |
480 | 987 | OPENSSL_strlcat(include_path, include, newlen); |
481 | 987 | OPENSSL_free(include); |
482 | 4.12k | } else { |
483 | 4.12k | include_path = include; |
484 | 4.12k | } |
485 | | |
486 | 5.11k | if (conf->flag_abspath |
487 | 154 | && !ossl_is_absolute_path(include_path)) { |
488 | 45 | ERR_raise(ERR_LIB_CONF, CONF_R_RELATIVE_PATH); |
489 | 45 | OPENSSL_free(include_path); |
490 | 45 | goto err; |
491 | 45 | } |
492 | | |
493 | | /* get the BIO of the included file */ |
494 | 5.06k | #ifndef OPENSSL_NO_POSIX_IO |
495 | 5.06k | next = process_include(include_path, &dirctx, &dirpath); |
496 | 5.06k | if (include_path != dirpath) { |
497 | | /* dirpath will contain include in case of a directory */ |
498 | 3.73k | OPENSSL_free(include_path); |
499 | 3.73k | } |
500 | | #else |
501 | | next = BIO_new_file(include_path, "r"); |
502 | | OPENSSL_free(include_path); |
503 | | #endif |
504 | | |
505 | 5.06k | if (next != NULL) { |
506 | | /* push the currently processing BIO onto stack */ |
507 | 1.34k | if (biosk == NULL) { |
508 | 584 | if ((biosk = sk_BIO_new_null()) == NULL) { |
509 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB); |
510 | 0 | BIO_free(next); |
511 | 0 | goto err; |
512 | 0 | } |
513 | 584 | } |
514 | 1.34k | if (!sk_BIO_push(biosk, in)) { |
515 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB); |
516 | 0 | BIO_free(next); |
517 | 0 | goto err; |
518 | 0 | } |
519 | | /* continue with reading from the included BIO */ |
520 | 1.34k | in = next; |
521 | 1.34k | } |
522 | 5.06k | continue; |
523 | 271k | } else if (*p != '=') { |
524 | 3.34k | ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN, |
525 | 3.34k | "HERE-->%s", p); |
526 | 3.34k | goto err; |
527 | 3.34k | } |
528 | 267k | *end = '\0'; |
529 | 267k | p++; |
530 | 267k | start = eat_ws(conf, p); |
531 | 267k | trim_ws(conf, start); |
532 | | |
533 | 267k | if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) |
534 | 0 | goto err; |
535 | 267k | v->name = OPENSSL_strdup(pname); |
536 | 267k | v->value = NULL; |
537 | 267k | if (v->name == NULL) |
538 | 0 | goto err; |
539 | 267k | if (!str_copy(conf, psection, &(v->value), start)) |
540 | 1.72k | goto err; |
541 | | |
542 | 266k | if (strcmp(psection, section) != 0) { |
543 | 54.7k | if ((tv = _CONF_get_section(conf, psection)) |
544 | 54.7k | == NULL) |
545 | 43.9k | tv = _CONF_new_section(conf, psection); |
546 | 54.7k | if (tv == NULL) { |
547 | 0 | ERR_raise(ERR_LIB_CONF, |
548 | 0 | CONF_R_UNABLE_TO_CREATE_NEW_SECTION); |
549 | 0 | goto err; |
550 | 0 | } |
551 | 54.7k | } else |
552 | 211k | tv = sv; |
553 | 266k | if (_CONF_add_string(conf, tv, v) == 0) { |
554 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_CONF_LIB); |
555 | 0 | goto err; |
556 | 0 | } |
557 | 266k | v = NULL; |
558 | 266k | } |
559 | 321k | } |
560 | 9.46k | BUF_MEM_free(buff); |
561 | 9.46k | OPENSSL_free(section); |
562 | | /* |
563 | | * No need to pop, since we only get here if the stack is empty. |
564 | | * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE! |
565 | | */ |
566 | 9.46k | sk_BIO_free(biosk); |
567 | 9.46k | return 1; |
568 | | |
569 | 6.27k | err: |
570 | 6.27k | BUF_MEM_free(buff); |
571 | 6.27k | OPENSSL_free(section); |
572 | | /* |
573 | | * Since |in| is the first element of the stack and should NOT be freed |
574 | | * here, we cannot use sk_BIO_pop_free(). Instead, we pop and free one |
575 | | * BIO at a time, making sure that the last one popped isn't. |
576 | | */ |
577 | 6.47k | while (sk_BIO_num(biosk) > 0) { |
578 | 202 | BIO *popped = sk_BIO_pop(biosk); |
579 | 202 | BIO_vfree(in); |
580 | 202 | in = popped; |
581 | 202 | } |
582 | 6.27k | sk_BIO_free(biosk); |
583 | 6.27k | #ifndef OPENSSL_NO_POSIX_IO |
584 | 6.27k | OPENSSL_free(dirpath); |
585 | 6.27k | if (dirctx != NULL) |
586 | 186 | OPENSSL_DIR_end(&dirctx); |
587 | 6.27k | #endif |
588 | 6.27k | if (line != NULL) |
589 | 6.27k | *line = eline; |
590 | 6.27k | BIO_snprintf(btmp, sizeof(btmp), "%ld", eline); |
591 | 6.27k | ERR_add_error_data(2, "line ", btmp); |
592 | 6.27k | if (h != conf->data) { |
593 | 6.27k | CONF_free(conf->data); |
594 | 6.27k | conf->data = NULL; |
595 | 6.27k | } |
596 | 6.27k | if (v != NULL) { |
597 | 1.72k | OPENSSL_free(v->name); |
598 | 1.72k | OPENSSL_free(v->value); |
599 | 1.72k | OPENSSL_free(v); |
600 | 1.72k | } |
601 | 6.27k | return 0; |
602 | 15.7k | } |
603 | | |
604 | | static void clear_comments(CONF *conf, char *p) |
605 | 37.6M | { |
606 | 37.7M | for (;;) { |
607 | 37.7M | if (IS_FCOMMENT(conf, *p)) { |
608 | 0 | *p = '\0'; |
609 | 0 | return; |
610 | 0 | } |
611 | 37.7M | if (!IS_WS(conf, *p)) { |
612 | 37.6M | break; |
613 | 37.6M | } |
614 | 60.7k | p++; |
615 | 60.7k | } |
616 | | |
617 | 311M | for (;;) { |
618 | 311M | if (IS_COMMENT(conf, *p)) { |
619 | 309k | *p = '\0'; |
620 | 309k | return; |
621 | 309k | } |
622 | 311M | if (IS_DQUOTE(conf, *p)) { |
623 | 0 | p = scan_dquote(conf, p); |
624 | 0 | continue; |
625 | 0 | } |
626 | 311M | if (IS_QUOTE(conf, *p)) { |
627 | 42.0k | p = scan_quote(conf, p); |
628 | 42.0k | continue; |
629 | 42.0k | } |
630 | 311M | if (IS_ESC(conf, *p)) { |
631 | 871k | p = scan_esc(conf, p); |
632 | 871k | continue; |
633 | 871k | } |
634 | 310M | if (IS_EOF(conf, *p)) |
635 | 37.3M | return; |
636 | 273M | else |
637 | 273M | p++; |
638 | 310M | } |
639 | 37.6M | } |
640 | | |
641 | | static int str_copy(CONF *conf, char *section, char **pto, char *from) |
642 | 400k | { |
643 | 400k | int q, r, rr = 0, to = 0; |
644 | 400k | char *s, *e, *rp, *p, *rrp, *np, *cp, v; |
645 | 400k | BUF_MEM *buf; |
646 | | |
647 | 400k | if ((buf = BUF_MEM_new()) == NULL) |
648 | 0 | return 0; |
649 | | |
650 | 400k | if (!BUF_MEM_grow(buf, strlen(from) + 1)) |
651 | 0 | goto err; |
652 | | |
653 | 87.2M | for (;;) { |
654 | 87.2M | if (IS_QUOTE(conf, *from)) { |
655 | 32.8k | q = *from; |
656 | 32.8k | from++; |
657 | 26.9M | while (!IS_EOF(conf, *from) && (*from != q)) { |
658 | 26.9M | if (IS_ESC(conf, *from)) { |
659 | 91.4k | from++; |
660 | 91.4k | if (IS_EOF(conf, *from)) |
661 | 2.84k | break; |
662 | 91.4k | } |
663 | 26.9M | buf->data[to++] = *(from++); |
664 | 26.9M | } |
665 | 32.8k | if (*from == q) |
666 | 25.6k | from++; |
667 | 87.2M | } else if (IS_DQUOTE(conf, *from)) { |
668 | 0 | q = *from; |
669 | 0 | from++; |
670 | 0 | while (!IS_EOF(conf, *from)) { |
671 | 0 | if (*from == q) { |
672 | 0 | if (*(from + 1) == q) { |
673 | 0 | from++; |
674 | 0 | } else { |
675 | 0 | break; |
676 | 0 | } |
677 | 0 | } |
678 | 0 | buf->data[to++] = *(from++); |
679 | 0 | } |
680 | 0 | if (*from == q) |
681 | 0 | from++; |
682 | 87.2M | } else if (IS_ESC(conf, *from)) { |
683 | 289k | from++; |
684 | 289k | v = *(from++); |
685 | 289k | if (IS_EOF(conf, v)) |
686 | 10.6k | break; |
687 | 278k | else if (v == 'r') |
688 | 60.4k | v = '\r'; |
689 | 218k | else if (v == 'n') |
690 | 10.9k | v = '\n'; |
691 | 207k | else if (v == 'b') |
692 | 5.79k | v = '\b'; |
693 | 201k | else if (v == 't') |
694 | 4.65k | v = '\t'; |
695 | 278k | buf->data[to++] = v; |
696 | 86.9M | } else if (IS_EOF(conf, *from)) |
697 | 388k | break; |
698 | 86.5M | else if (*from == '$' |
699 | 143k | && (!conf->flag_dollarid |
700 | 51.8k | || from[1] == '{' |
701 | 96.8k | || from[1] == '(')) { |
702 | 96.8k | size_t newsize; |
703 | | |
704 | | /* try to expand it */ |
705 | 96.8k | rrp = NULL; |
706 | 96.8k | s = &(from[1]); |
707 | 96.8k | if (*s == '{') |
708 | 4.65k | q = '}'; |
709 | 92.2k | else if (*s == '(') |
710 | 3.60k | q = ')'; |
711 | 88.6k | else |
712 | 88.6k | q = 0; |
713 | | |
714 | 96.8k | if (q) |
715 | 8.25k | s++; |
716 | 96.8k | cp = section; |
717 | 96.8k | e = np = s; |
718 | 19.6M | while (IS_ALNUM(conf, *e) |
719 | 97.9k | || (conf->flag_dollarid && IS_DOLLAR(conf, *e))) |
720 | 19.5M | e++; |
721 | 96.8k | if ((e[0] == ':') && (e[1] == ':')) { |
722 | 5.95k | cp = np; |
723 | 5.95k | rrp = e; |
724 | 5.95k | rr = *e; |
725 | 5.95k | *rrp = '\0'; |
726 | 5.95k | e += 2; |
727 | 5.95k | np = e; |
728 | 11.9k | while (IS_ALNUM(conf, *e) |
729 | 7.03k | || (conf->flag_dollarid && IS_DOLLAR(conf, *e))) |
730 | 6.01k | e++; |
731 | 5.95k | } |
732 | 96.8k | r = *e; |
733 | 96.8k | *e = '\0'; |
734 | 96.8k | rp = e; |
735 | 96.8k | if (q) { |
736 | 8.25k | if (r != q) { |
737 | 351 | ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE); |
738 | 351 | goto err; |
739 | 351 | } |
740 | 7.90k | e++; |
741 | 7.90k | } |
742 | | /*- |
743 | | * So at this point we have |
744 | | * np which is the start of the name string which is |
745 | | * '\0' terminated. |
746 | | * cp which is the start of the section string which is |
747 | | * '\0' terminated. |
748 | | * e is the 'next point after'. |
749 | | * r and rr are the chars replaced by the '\0' |
750 | | * rp and rrp is where 'r' and 'rr' came from. |
751 | | */ |
752 | 96.5k | p = _CONF_get_string(conf, cp, np); |
753 | 96.5k | if (rrp != NULL) |
754 | 5.90k | *rrp = rr; |
755 | 96.5k | *rp = r; |
756 | 96.5k | if (p == NULL) { |
757 | 1.45k | ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_HAS_NO_VALUE); |
758 | 1.45k | goto err; |
759 | 1.45k | } |
760 | 95.0k | newsize = strlen(p) + buf->length - (e - from); |
761 | 95.0k | if (newsize > MAX_CONF_VALUE_LENGTH) { |
762 | 91 | ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG); |
763 | 91 | goto err; |
764 | 91 | } |
765 | 94.9k | if (!BUF_MEM_grow_clean(buf, newsize)) { |
766 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB); |
767 | 0 | goto err; |
768 | 0 | } |
769 | 33.2M | while (*p) |
770 | 33.1M | buf->data[to++] = *(p++); |
771 | | |
772 | 94.9k | from = e; |
773 | | |
774 | | /* |
775 | | * In case there were no braces or parenthesis around the |
776 | | * variable reference, we have to put back the character that was |
777 | | * replaced with a '\0'. /RL |
778 | | */ |
779 | 94.9k | *rp = r; |
780 | 94.9k | } else |
781 | 86.4M | buf->data[to++] = *(from++); |
782 | 87.2M | } |
783 | 398k | buf->data[to] = '\0'; |
784 | 398k | OPENSSL_free(*pto); |
785 | 398k | *pto = buf->data; |
786 | 398k | OPENSSL_free(buf); |
787 | 398k | return 1; |
788 | 1.89k | err: |
789 | 1.89k | BUF_MEM_free(buf); |
790 | 1.89k | return 0; |
791 | 400k | } |
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 | 6.27k | { |
802 | 6.27k | struct stat st; |
803 | 6.27k | BIO *next; |
804 | | |
805 | 6.27k | if (stat(include, &st) < 0) { |
806 | 2.79k | ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include); |
807 | | /* missing include file is not fatal error */ |
808 | 2.79k | return NULL; |
809 | 2.79k | } |
810 | | |
811 | 3.48k | if (S_ISDIR(st.st_mode)) { |
812 | 3.40k | 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.40k | if ((next = get_next_file(include, dirctx)) != NULL) |
819 | 2.01k | *dirpath = include; |
820 | 3.40k | return next; |
821 | 3.40k | } |
822 | | |
823 | 71 | next = BIO_new_file(include, "r"); |
824 | 71 | return next; |
825 | 3.48k | } |
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 | 5.84k | { |
833 | 5.84k | const char *filename; |
834 | 5.84k | size_t pathlen; |
835 | | |
836 | 5.84k | pathlen = strlen(path); |
837 | 245k | while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) { |
838 | 242k | size_t namelen; |
839 | | |
840 | 242k | namelen = strlen(filename); |
841 | | |
842 | 242k | if ((namelen > 5 |
843 | 187k | && OPENSSL_strcasecmp(filename + namelen - 5, ".conf") == 0) |
844 | 232k | || (namelen > 4 |
845 | 198k | && OPENSSL_strcasecmp(filename + namelen - 4, ".cnf") == 0)) { |
846 | 11.2k | size_t newlen; |
847 | 11.2k | char *newpath; |
848 | 11.2k | BIO *bio; |
849 | | |
850 | 11.2k | newlen = pathlen + namelen + 2; |
851 | 11.2k | newpath = OPENSSL_zalloc(newlen); |
852 | 11.2k | if (newpath == NULL) |
853 | 0 | break; |
854 | | #ifdef OPENSSL_SYS_VMS |
855 | | /* |
856 | | * If the given path isn't clear VMS syntax, |
857 | | * we treat it as on Unix. |
858 | | */ |
859 | | if (path[pathlen - 1] == ']' |
860 | | || path[pathlen - 1] == '>' |
861 | | || path[pathlen - 1] == ':') { |
862 | | /* Clear VMS directory syntax, just copy as is */ |
863 | | OPENSSL_strlcpy(newpath, path, newlen); |
864 | | } |
865 | | #endif |
866 | 11.2k | if (newpath[0] == '\0') { |
867 | 11.2k | OPENSSL_strlcpy(newpath, path, newlen); |
868 | 11.2k | OPENSSL_strlcat(newpath, "/", newlen); |
869 | 11.2k | } |
870 | 11.2k | OPENSSL_strlcat(newpath, filename, newlen); |
871 | | |
872 | 11.2k | bio = BIO_new_file(newpath, "r"); |
873 | 11.2k | OPENSSL_free(newpath); |
874 | | /* Errors when opening files are non-fatal. */ |
875 | 11.2k | if (bio != NULL) |
876 | 2.63k | return bio; |
877 | 11.2k | } |
878 | 242k | } |
879 | 3.21k | OPENSSL_DIR_end(dirctx); |
880 | 3.21k | *dirctx = NULL; |
881 | 3.21k | return NULL; |
882 | 5.84k | } |
883 | | #endif |
884 | | |
885 | | static int is_keytype(const CONF *conf, char c, unsigned short type) |
886 | 2.63G | { |
887 | 2.63G | const unsigned short *keytypes = (const unsigned short *)conf->meth_data; |
888 | 2.63G | unsigned char key = (unsigned char)c; |
889 | | |
890 | | #ifdef CHARSET_EBCDIC |
891 | | #if CHAR_BIT > 8 |
892 | | if (key > 255) { |
893 | | /* key is out of range for os_toascii table */ |
894 | | return 0; |
895 | | } |
896 | | #endif |
897 | | /* convert key from ebcdic to ascii */ |
898 | | key = os_toascii[key]; |
899 | | #endif |
900 | | |
901 | 2.63G | if (key > 127) { |
902 | | /* key is not a seven bit ascii character */ |
903 | 162M | return 0; |
904 | 162M | } |
905 | | |
906 | 2.47G | return (keytypes[key] & type) ? 1 : 0; |
907 | 2.63G | } |
908 | | |
909 | | static char *eat_ws(CONF *conf, char *p) |
910 | 38.4M | { |
911 | 38.9M | while (IS_WS(conf, *p) && (!IS_EOF(conf, *p))) |
912 | 443k | p++; |
913 | 38.4M | return p; |
914 | 38.4M | } |
915 | | |
916 | | static void trim_ws(CONF *conf, char *start) |
917 | 398k | { |
918 | 398k | char *p = start; |
919 | | |
920 | 158M | while (!IS_EOF(conf, *p)) |
921 | 158M | p++; |
922 | 398k | p--; |
923 | 489k | while ((p >= start) && IS_WS(conf, *p)) |
924 | 90.8k | p--; |
925 | 398k | p++; |
926 | 398k | *p = '\0'; |
927 | 398k | } |
928 | | |
929 | | static char *eat_alpha_numeric(CONF *conf, char *p) |
930 | 493k | { |
931 | 121M | for (;;) { |
932 | 121M | if (IS_ESC(conf, *p)) { |
933 | 498k | p = scan_esc(conf, p); |
934 | 498k | continue; |
935 | 498k | } |
936 | 120M | if (!(IS_ALNUM_PUNCT(conf, *p) |
937 | 496k | || (conf->flag_dollarid && IS_DOLLAR(conf, *p)))) |
938 | 493k | return p; |
939 | 120M | p++; |
940 | 120M | } |
941 | 493k | } |
942 | | |
943 | | static char *scan_quote(CONF *conf, char *p) |
944 | 42.0k | { |
945 | 42.0k | int q = *p; |
946 | | |
947 | 42.0k | p++; |
948 | 50.3M | while (!(IS_EOF(conf, *p)) && (*p != q)) { |
949 | 50.3M | if (IS_ESC(conf, *p)) { |
950 | 167k | p++; |
951 | 167k | if (IS_EOF(conf, *p)) |
952 | 1.75k | return p; |
953 | 167k | } |
954 | 50.3M | p++; |
955 | 50.3M | } |
956 | 40.2k | if (*p == q) |
957 | 33.6k | p++; |
958 | 40.2k | return p; |
959 | 42.0k | } |
960 | | |
961 | | static char *scan_dquote(CONF *conf, char *p) |
962 | 0 | { |
963 | 0 | int q = *p; |
964 | |
|
965 | 0 | p++; |
966 | 0 | while (!(IS_EOF(conf, *p))) { |
967 | 0 | if (*p == q) { |
968 | 0 | if (*(p + 1) == q) { |
969 | 0 | p++; |
970 | 0 | } else { |
971 | 0 | break; |
972 | 0 | } |
973 | 0 | } |
974 | 0 | p++; |
975 | 0 | } |
976 | 0 | if (*p == q) |
977 | 0 | p++; |
978 | 0 | return p; |
979 | 0 | } |
980 | | |
981 | | static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out) |
982 | 0 | { |
983 | 0 | if (a->name) |
984 | 0 | BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value); |
985 | 0 | else |
986 | 0 | BIO_printf(out, "[[%s]]\n", a->section); |
987 | 0 | } |
988 | | |
989 | | IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO); |
990 | | |
991 | | static int def_dump(const CONF *conf, BIO *out) |
992 | 0 | { |
993 | 0 | lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out); |
994 | 0 | return 1; |
995 | 0 | } |
996 | | |
997 | | static int def_is_number(const CONF *conf, char c) |
998 | 0 | { |
999 | 0 | return IS_NUMBER(conf, c); |
1000 | 0 | } |
1001 | | |
1002 | | static int def_to_int(const CONF *conf, char c) |
1003 | 0 | { |
1004 | 0 | return c - '0'; |
1005 | 0 | } |