/src/openssl32/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.08k | { |
210 | | /* The macro BUFSIZE conflicts with a system macro in VxWorks */ |
211 | 39.8M | #define CONFBUFSIZE 512 |
212 | 6.08k | int bufnum = 0, i, ii; |
213 | 6.08k | BUF_MEM *buff = NULL; |
214 | 6.08k | char *s, *p, *end; |
215 | 6.08k | int again; |
216 | 6.08k | int first_call = 1; |
217 | 6.08k | long eline = 0; |
218 | 6.08k | char btmp[DECIMAL_SIZE(eline) + 1]; |
219 | 6.08k | CONF_VALUE *v = NULL, *tv; |
220 | 6.08k | CONF_VALUE *sv = NULL; |
221 | 6.08k | char *section = NULL, *buf; |
222 | 6.08k | char *start, *psection, *pname; |
223 | 6.08k | void *h = (void *)(conf->data); |
224 | 6.08k | STACK_OF(BIO) *biosk = NULL; |
225 | 6.08k | #ifndef OPENSSL_NO_POSIX_IO |
226 | 6.08k | char *dirpath = NULL; |
227 | 6.08k | OPENSSL_DIR_CTX *dirctx = NULL; |
228 | 6.08k | #endif |
229 | 6.08k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
230 | 6.08k | int numincludes = 0; |
231 | 6.08k | #endif |
232 | | |
233 | 6.08k | 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.08k | section = OPENSSL_strdup("default"); |
239 | 6.08k | if (section == NULL) |
240 | 0 | goto err; |
241 | | |
242 | 6.08k | if (_CONF_new_data(conf) == 0) { |
243 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_CONF_LIB); |
244 | 0 | goto err; |
245 | 0 | } |
246 | | |
247 | 6.08k | sv = _CONF_new_section(conf, section); |
248 | 6.08k | if (sv == NULL) { |
249 | 0 | ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION); |
250 | 0 | goto err; |
251 | 0 | } |
252 | | |
253 | 6.08k | bufnum = 0; |
254 | 6.08k | again = 0; |
255 | 13.2M | for (;;) { |
256 | 13.2M | if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) { |
257 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB); |
258 | 0 | goto err; |
259 | 0 | } |
260 | 13.2M | p = &(buff->data[bufnum]); |
261 | 13.2M | *p = '\0'; |
262 | 13.2M | read_retry: |
263 | 13.2M | if (in != NULL && BIO_gets(in, p, CONFBUFSIZE - 1) < 0) |
264 | 0 | goto err; |
265 | 13.2M | p[CONFBUFSIZE - 1] = '\0'; |
266 | 13.2M | ii = i = strlen(p); |
267 | 13.2M | if (first_call) { |
268 | | /* Other BOMs imply unsupported multibyte encoding, |
269 | | * so don't strip them and let the error raise */ |
270 | 6.08k | const unsigned char utf8_bom[3] = {0xEF, 0xBB, 0xBF}; |
271 | | |
272 | 6.08k | if (i >= 3 && memcmp(p, utf8_bom, 3) == 0) { |
273 | 18 | memmove(p, p + 3, i - 3); |
274 | 18 | p[i - 3] = 0; |
275 | 18 | i -= 3; |
276 | 18 | ii -= 3; |
277 | 18 | } |
278 | 6.08k | first_call = 0; |
279 | 6.08k | } |
280 | 13.2M | if (i == 0 && !again) { |
281 | | /* the currently processed BIO is NULL or at EOF */ |
282 | 5.45k | BIO *parent; |
283 | | |
284 | 5.45k | #ifndef OPENSSL_NO_POSIX_IO |
285 | | /* continue processing with the next file from directory */ |
286 | 5.45k | if (dirctx != NULL) { |
287 | 1.64k | BIO *next; |
288 | | |
289 | 1.64k | if ((next = get_next_file(dirpath, &dirctx)) != NULL) { |
290 | 389 | BIO_vfree(in); |
291 | 389 | in = next; |
292 | 389 | goto read_retry; |
293 | 1.26k | } else { |
294 | 1.26k | OPENSSL_free(dirpath); |
295 | 1.26k | dirpath = NULL; |
296 | 1.26k | } |
297 | 1.64k | } |
298 | 5.06k | #endif |
299 | | /* no more files in directory, continue with processing parent */ |
300 | 5.06k | if ((parent = sk_BIO_pop(biosk)) == NULL) { |
301 | | /* everything processed get out of the loop */ |
302 | 3.80k | break; |
303 | 3.80k | } else { |
304 | 1.26k | BIO_vfree(in); |
305 | 1.26k | in = parent; |
306 | 1.26k | goto read_retry; |
307 | 1.26k | } |
308 | 5.06k | } |
309 | 13.2M | again = 0; |
310 | 26.3M | while (i > 0) { |
311 | 13.6M | if ((p[i - 1] != '\r') && (p[i - 1] != '\n')) |
312 | 558k | break; |
313 | 13.0M | else |
314 | 13.0M | i--; |
315 | 13.6M | } |
316 | | /* |
317 | | * we removed some trailing stuff so there is a new line on the end. |
318 | | */ |
319 | 13.2M | if (ii && i == ii) |
320 | 190k | again = 1; /* long line */ |
321 | 13.0M | else { |
322 | 13.0M | p[i] = '\0'; |
323 | 13.0M | eline++; /* another input line */ |
324 | 13.0M | } |
325 | | |
326 | | /* we now have a line with trailing \r\n removed */ |
327 | | |
328 | | /* i is the number of bytes */ |
329 | 13.2M | bufnum += i; |
330 | | |
331 | 13.2M | v = NULL; |
332 | | /* check for line continuation */ |
333 | 13.2M | if (!again && bufnum >= 1) { |
334 | | /* |
335 | | * If we have bytes and the last char '\\' and second last char |
336 | | * is not '\\' |
337 | | */ |
338 | 373k | p = &(buff->data[bufnum - 1]); |
339 | 373k | if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) { |
340 | 2.27k | bufnum--; |
341 | 2.27k | again = 1; |
342 | 2.27k | } |
343 | 373k | } |
344 | 13.2M | if (again) |
345 | 192k | continue; |
346 | 13.0M | bufnum = 0; |
347 | 13.0M | buf = buff->data; |
348 | | |
349 | 13.0M | clear_comments(conf, buf); |
350 | 13.0M | s = eat_ws(conf, buf); |
351 | 13.0M | if (IS_EOF(conf, *s)) |
352 | 12.8M | continue; /* blank line */ |
353 | 193k | if (*s == '[') { |
354 | 20.6k | char *ss; |
355 | | |
356 | 20.6k | s++; |
357 | 20.6k | start = eat_ws(conf, s); |
358 | 20.6k | ss = start; |
359 | 21.5k | again: |
360 | 21.5k | end = eat_alpha_numeric(conf, ss); |
361 | 21.5k | p = eat_ws(conf, end); |
362 | 21.5k | if (*p != ']') { |
363 | 1.13k | if (*p != '\0' && ss != p) { |
364 | 912 | ss = p; |
365 | 912 | goto again; |
366 | 912 | } |
367 | 1.13k | ERR_raise(ERR_LIB_CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET); |
368 | 227 | goto err; |
369 | 1.13k | } |
370 | 20.4k | *end = '\0'; |
371 | 20.4k | if (!str_copy(conf, NULL, §ion, start)) |
372 | 0 | goto err; |
373 | 20.4k | if ((sv = _CONF_get_section(conf, section)) == NULL) |
374 | 10.2k | sv = _CONF_new_section(conf, section); |
375 | 20.4k | if (sv == NULL) { |
376 | 0 | ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION); |
377 | 0 | goto err; |
378 | 0 | } |
379 | 20.4k | continue; |
380 | 173k | } else { |
381 | 173k | pname = s; |
382 | 173k | end = eat_alpha_numeric(conf, s); |
383 | 173k | if ((end[0] == ':') && (end[1] == ':')) { |
384 | 19.1k | *end = '\0'; |
385 | 19.1k | end += 2; |
386 | 19.1k | psection = pname; |
387 | 19.1k | pname = end; |
388 | 19.1k | end = eat_alpha_numeric(conf, end); |
389 | 153k | } else { |
390 | 153k | psection = section; |
391 | 153k | } |
392 | 173k | p = eat_ws(conf, end); |
393 | 173k | if (CHECK_AND_SKIP_PREFIX(pname, ".pragma") |
394 | 173k | && (p != pname || *p == '=')) { |
395 | 5.69k | char *pval; |
396 | | |
397 | 5.69k | if (*p == '=') { |
398 | 3.61k | p++; |
399 | 3.61k | p = eat_ws(conf, p); |
400 | 3.61k | } |
401 | 5.69k | trim_ws(conf, p); |
402 | | |
403 | | /* Pragma values take the form keyword:value */ |
404 | 5.69k | pval = strchr(p, ':'); |
405 | 5.69k | if (pval == NULL || pval == p || pval[1] == '\0') { |
406 | 50 | ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA); |
407 | 50 | goto err; |
408 | 50 | } |
409 | | |
410 | 5.64k | *pval++ = '\0'; |
411 | 5.64k | trim_ws(conf, p); |
412 | 5.64k | pval = eat_ws(conf, pval); |
413 | | |
414 | | /* |
415 | | * Known pragmas: |
416 | | * |
417 | | * dollarid takes "on", "true or "off", "false" |
418 | | * abspath takes "on", "true or "off", "false" |
419 | | * includedir directory prefix |
420 | | */ |
421 | 5.64k | if (strcmp(p, "dollarid") == 0) { |
422 | 676 | if (!parsebool(pval, &conf->flag_dollarid)) |
423 | 3 | goto err; |
424 | 4.97k | } else if (strcmp(p, "abspath") == 0) { |
425 | 2.06k | if (!parsebool(pval, &conf->flag_abspath)) |
426 | 95 | goto err; |
427 | 2.90k | } else if (strcmp(p, "includedir") == 0) { |
428 | 683 | OPENSSL_free(conf->includedir); |
429 | 683 | if ((conf->includedir = OPENSSL_strdup(pval)) == NULL) |
430 | 0 | goto err; |
431 | 683 | } |
432 | | |
433 | | /* |
434 | | * We *ignore* any unknown pragma. |
435 | | */ |
436 | 5.54k | continue; |
437 | 167k | } else if (CHECK_AND_SKIP_PREFIX(pname, ".include") |
438 | 167k | && (p != pname || *p == '=')) { |
439 | 2.72k | char *include = NULL; |
440 | 2.72k | BIO *next; |
441 | 2.72k | const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE"); |
442 | 2.72k | char *include_path = NULL; |
443 | | |
444 | 2.72k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
445 | | /* |
446 | | * The include processing below can cause the "conf" fuzzer to |
447 | | * timeout due to the fuzzer inserting large and complicated |
448 | | * includes - with a large amount of time spent in |
449 | | * OPENSSL_strlcat/OPENSSL_strcpy. This is not a security |
450 | | * concern because config files should never come from untrusted |
451 | | * sources. We just set an arbitrary limit on the allowed |
452 | | * number of includes when fuzzing to prevent this timeout. |
453 | | */ |
454 | 2.72k | if (numincludes++ > 10) |
455 | 16 | goto err; |
456 | 2.70k | #endif |
457 | | |
458 | 2.70k | if (include_dir == NULL) |
459 | 2.70k | include_dir = conf->includedir; |
460 | | |
461 | 2.70k | if (*p == '=') { |
462 | 366 | p++; |
463 | 366 | p = eat_ws(conf, p); |
464 | 366 | } |
465 | 2.70k | trim_ws(conf, p); |
466 | 2.70k | if (!str_copy(conf, psection, &include, p)) |
467 | 4 | goto err; |
468 | | |
469 | 2.70k | if (include_dir != NULL && !ossl_is_absolute_path(include)) { |
470 | 593 | size_t newlen = strlen(include_dir) + strlen(include) + 2; |
471 | | |
472 | 593 | include_path = OPENSSL_malloc(newlen); |
473 | 593 | if (include_path == NULL) { |
474 | 0 | OPENSSL_free(include); |
475 | 0 | goto err; |
476 | 0 | } |
477 | | |
478 | 593 | OPENSSL_strlcpy(include_path, include_dir, newlen); |
479 | 593 | if (!ossl_ends_with_dirsep(include_path)) |
480 | 548 | OPENSSL_strlcat(include_path, "/", newlen); |
481 | 593 | OPENSSL_strlcat(include_path, include, newlen); |
482 | 593 | OPENSSL_free(include); |
483 | 2.10k | } else { |
484 | 2.10k | include_path = include; |
485 | 2.10k | } |
486 | | |
487 | 2.70k | if (conf->flag_abspath |
488 | 2.70k | && !ossl_is_absolute_path(include_path)) { |
489 | 19 | ERR_raise(ERR_LIB_CONF, CONF_R_RELATIVE_PATH); |
490 | 19 | OPENSSL_free(include_path); |
491 | 19 | goto err; |
492 | 19 | } |
493 | | |
494 | | /* get the BIO of the included file */ |
495 | 2.68k | #ifndef OPENSSL_NO_POSIX_IO |
496 | 2.68k | next = process_include(include_path, &dirctx, &dirpath); |
497 | 2.68k | if (include_path != dirpath) { |
498 | | /* dirpath will contain include in case of a directory */ |
499 | 1.33k | OPENSSL_free(include_path); |
500 | 1.33k | } |
501 | | #else |
502 | | next = BIO_new_file(include_path, "r"); |
503 | | OPENSSL_free(include_path); |
504 | | #endif |
505 | | |
506 | 2.68k | if (next != NULL) { |
507 | | /* push the currently processing BIO onto stack */ |
508 | 1.36k | if (biosk == NULL) { |
509 | 471 | if ((biosk = sk_BIO_new_null()) == NULL) { |
510 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB); |
511 | 0 | BIO_free(next); |
512 | 0 | goto err; |
513 | 0 | } |
514 | 471 | } |
515 | 1.36k | if (!sk_BIO_push(biosk, in)) { |
516 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB); |
517 | 0 | BIO_free(next); |
518 | 0 | goto err; |
519 | 0 | } |
520 | | /* continue with reading from the included BIO */ |
521 | 1.36k | in = next; |
522 | 1.36k | } |
523 | 2.68k | continue; |
524 | 164k | } else if (*p != '=') { |
525 | 1.22k | ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN, |
526 | 1.22k | "HERE-->%s", p); |
527 | 1.22k | goto err; |
528 | 1.22k | } |
529 | 163k | *end = '\0'; |
530 | 163k | p++; |
531 | 163k | start = eat_ws(conf, p); |
532 | 163k | trim_ws(conf, start); |
533 | | |
534 | 163k | if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) |
535 | 0 | goto err; |
536 | 163k | v->name = OPENSSL_strdup(pname); |
537 | 163k | v->value = NULL; |
538 | 163k | if (v->name == NULL) |
539 | 0 | goto err; |
540 | 163k | if (!str_copy(conf, psection, &(v->value), start)) |
541 | 641 | goto err; |
542 | | |
543 | 162k | if (strcmp(psection, section) != 0) { |
544 | 18.9k | if ((tv = _CONF_get_section(conf, psection)) |
545 | 18.9k | == NULL) |
546 | 15.9k | tv = _CONF_new_section(conf, psection); |
547 | 18.9k | if (tv == NULL) { |
548 | 0 | ERR_raise(ERR_LIB_CONF, |
549 | 0 | CONF_R_UNABLE_TO_CREATE_NEW_SECTION); |
550 | 0 | goto err; |
551 | 0 | } |
552 | 18.9k | } else |
553 | 143k | tv = sv; |
554 | 162k | if (_CONF_add_string(conf, tv, v) == 0) { |
555 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_CONF_LIB); |
556 | 0 | goto err; |
557 | 0 | } |
558 | 162k | v = NULL; |
559 | 162k | } |
560 | 193k | } |
561 | 3.80k | BUF_MEM_free(buff); |
562 | 3.80k | OPENSSL_free(section); |
563 | | /* |
564 | | * No need to pop, since we only get here if the stack is empty. |
565 | | * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE! |
566 | | */ |
567 | 3.80k | sk_BIO_free(biosk); |
568 | 3.80k | return 1; |
569 | | |
570 | 2.28k | err: |
571 | 2.28k | BUF_MEM_free(buff); |
572 | 2.28k | OPENSSL_free(section); |
573 | | /* |
574 | | * Since |in| is the first element of the stack and should NOT be freed |
575 | | * here, we cannot use sk_BIO_pop_free(). Instead, we pop and free one |
576 | | * BIO at a time, making sure that the last one popped isn't. |
577 | | */ |
578 | 2.39k | while (sk_BIO_num(biosk) > 0) { |
579 | 109 | BIO *popped = sk_BIO_pop(biosk); |
580 | 109 | BIO_vfree(in); |
581 | 109 | in = popped; |
582 | 109 | } |
583 | 2.28k | sk_BIO_free(biosk); |
584 | 2.28k | #ifndef OPENSSL_NO_POSIX_IO |
585 | 2.28k | OPENSSL_free(dirpath); |
586 | 2.28k | if (dirctx != NULL) |
587 | 84 | OPENSSL_DIR_end(&dirctx); |
588 | 2.28k | #endif |
589 | 2.28k | if (line != NULL) |
590 | 2.28k | *line = eline; |
591 | 2.28k | BIO_snprintf(btmp, sizeof(btmp), "%ld", eline); |
592 | 2.28k | ERR_add_error_data(2, "line ", btmp); |
593 | 2.28k | if (h != conf->data) { |
594 | 2.28k | CONF_free(conf->data); |
595 | 2.28k | conf->data = NULL; |
596 | 2.28k | } |
597 | 2.28k | if (v != NULL) { |
598 | 641 | OPENSSL_free(v->name); |
599 | 641 | OPENSSL_free(v->value); |
600 | 641 | OPENSSL_free(v); |
601 | 641 | } |
602 | 2.28k | return 0; |
603 | 6.08k | } |
604 | | |
605 | | static void clear_comments(CONF *conf, char *p) |
606 | 28.2M | { |
607 | 28.3M | for (;;) { |
608 | 28.3M | if (IS_FCOMMENT(conf, *p)) { |
609 | 0 | *p = '\0'; |
610 | 0 | return; |
611 | 0 | } |
612 | 28.3M | if (!IS_WS(conf, *p)) { |
613 | 28.2M | break; |
614 | 28.2M | } |
615 | 90.1k | p++; |
616 | 90.1k | } |
617 | | |
618 | 165M | for (;;) { |
619 | 165M | if (IS_COMMENT(conf, *p)) { |
620 | 458k | *p = '\0'; |
621 | 458k | return; |
622 | 458k | } |
623 | 164M | if (IS_DQUOTE(conf, *p)) { |
624 | 0 | p = scan_dquote(conf, p); |
625 | 0 | continue; |
626 | 0 | } |
627 | 164M | if (IS_QUOTE(conf, *p)) { |
628 | 26.2k | p = scan_quote(conf, p); |
629 | 26.2k | continue; |
630 | 26.2k | } |
631 | 164M | if (IS_ESC(conf, *p)) { |
632 | 279k | p = scan_esc(conf, p); |
633 | 279k | continue; |
634 | 279k | } |
635 | 164M | if (IS_EOF(conf, *p)) |
636 | 27.7M | return; |
637 | 136M | else |
638 | 136M | p++; |
639 | 164M | } |
640 | 28.2M | } |
641 | | |
642 | | static int str_copy(CONF *conf, char *section, char **pto, char *from) |
643 | 390k | { |
644 | 390k | int q, r, rr = 0, to = 0, len = 0; |
645 | 390k | char *s, *e, *rp, *p, *rrp, *np, *cp, v; |
646 | 390k | BUF_MEM *buf; |
647 | | |
648 | 390k | if ((buf = BUF_MEM_new()) == NULL) |
649 | 0 | return 0; |
650 | | |
651 | 390k | len = strlen(from) + 1; |
652 | 390k | if (!BUF_MEM_grow(buf, len)) |
653 | 0 | goto err; |
654 | | |
655 | 57.5M | for (;;) { |
656 | 57.5M | if (IS_QUOTE(conf, *from)) { |
657 | 23.6k | q = *from; |
658 | 23.6k | from++; |
659 | 19.6M | while (!IS_EOF(conf, *from) && (*from != q)) { |
660 | 19.5M | if (IS_ESC(conf, *from)) { |
661 | 71.9k | from++; |
662 | 71.9k | if (IS_EOF(conf, *from)) |
663 | 2.07k | break; |
664 | 71.9k | } |
665 | 19.5M | buf->data[to++] = *(from++); |
666 | 19.5M | } |
667 | 23.6k | if (*from == q) |
668 | 19.1k | from++; |
669 | 57.5M | } else if (IS_DQUOTE(conf, *from)) { |
670 | 0 | q = *from; |
671 | 0 | from++; |
672 | 0 | while (!IS_EOF(conf, *from)) { |
673 | 0 | if (*from == q) { |
674 | 0 | if (*(from + 1) == q) { |
675 | 0 | from++; |
676 | 0 | } else { |
677 | 0 | break; |
678 | 0 | } |
679 | 0 | } |
680 | 0 | buf->data[to++] = *(from++); |
681 | 0 | } |
682 | 0 | if (*from == q) |
683 | 0 | from++; |
684 | 57.5M | } else if (IS_ESC(conf, *from)) { |
685 | 91.6k | from++; |
686 | 91.6k | v = *(from++); |
687 | 91.6k | if (IS_EOF(conf, v)) |
688 | 1.90k | break; |
689 | 89.7k | else if (v == 'r') |
690 | 10.8k | v = '\r'; |
691 | 78.9k | else if (v == 'n') |
692 | 6.12k | v = '\n'; |
693 | 72.8k | else if (v == 'b') |
694 | 3.27k | v = '\b'; |
695 | 69.5k | else if (v == 't') |
696 | 9.09k | v = '\t'; |
697 | 89.7k | buf->data[to++] = v; |
698 | 57.4M | } else if (IS_EOF(conf, *from)) |
699 | 386k | break; |
700 | 57.0M | else if (*from == '$' |
701 | 57.0M | && (!conf->flag_dollarid |
702 | 70.9k | || from[1] == '{' |
703 | 70.9k | || from[1] == '(')) { |
704 | 65.8k | size_t newsize; |
705 | | |
706 | | /* try to expand it */ |
707 | 65.8k | rrp = NULL; |
708 | 65.8k | s = &(from[1]); |
709 | 65.8k | if (*s == '{') |
710 | 2.37k | q = '}'; |
711 | 63.4k | else if (*s == '(') |
712 | 2.12k | q = ')'; |
713 | 61.3k | else |
714 | 61.3k | q = 0; |
715 | | |
716 | 65.8k | if (q) |
717 | 4.49k | s++; |
718 | 65.8k | cp = section; |
719 | 65.8k | e = np = s; |
720 | 14.5M | while (IS_ALNUM(conf, *e) |
721 | 14.5M | || (conf->flag_dollarid && IS_DOLLAR(conf, *e))) |
722 | 14.4M | e++; |
723 | 65.8k | if ((e[0] == ':') && (e[1] == ':')) { |
724 | 2.93k | cp = np; |
725 | 2.93k | rrp = e; |
726 | 2.93k | rr = *e; |
727 | 2.93k | *rrp = '\0'; |
728 | 2.93k | e += 2; |
729 | 2.93k | np = e; |
730 | 13.6k | while (IS_ALNUM(conf, *e) |
731 | 13.6k | || (conf->flag_dollarid && IS_DOLLAR(conf, *e))) |
732 | 10.6k | e++; |
733 | 2.93k | } |
734 | 65.8k | r = *e; |
735 | 65.8k | *e = '\0'; |
736 | 65.8k | rp = e; |
737 | 65.8k | if (q) { |
738 | 4.49k | if (r != q) { |
739 | 260 | ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE); |
740 | 260 | goto err; |
741 | 260 | } |
742 | 4.23k | e++; |
743 | 4.23k | } |
744 | | /*- |
745 | | * So at this point we have |
746 | | * np which is the start of the name string which is |
747 | | * '\0' terminated. |
748 | | * cp which is the start of the section string which is |
749 | | * '\0' terminated. |
750 | | * e is the 'next point after'. |
751 | | * r and rr are the chars replaced by the '\0' |
752 | | * rp and rrp is where 'r' and 'rr' came from. |
753 | | */ |
754 | 65.5k | p = _CONF_get_string(conf, cp, np); |
755 | 65.5k | if (rrp != NULL) |
756 | 2.89k | *rrp = rr; |
757 | 65.5k | *rp = r; |
758 | 65.5k | if (p == NULL) { |
759 | 1.03k | ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_HAS_NO_VALUE); |
760 | 1.03k | goto err; |
761 | 1.03k | } |
762 | 64.5k | newsize = strlen(p) + buf->length - (e - from); |
763 | 64.5k | if (newsize > MAX_CONF_VALUE_LENGTH) { |
764 | 24 | ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG); |
765 | 24 | goto err; |
766 | 24 | } |
767 | 64.5k | if (!BUF_MEM_grow_clean(buf, newsize)) { |
768 | 0 | ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB); |
769 | 0 | goto err; |
770 | 0 | } |
771 | 21.9M | while (*p) |
772 | 21.8M | buf->data[to++] = *(p++); |
773 | | |
774 | | /* |
775 | | * Since we change the pointer 'from', we also have to change the |
776 | | * perceived length of the string it points at. /RL |
777 | | */ |
778 | 64.5k | len -= e - from; |
779 | 64.5k | from = e; |
780 | | |
781 | | /* |
782 | | * In case there were no braces or parenthesis around the |
783 | | * variable reference, we have to put back the character that was |
784 | | * replaced with a '\0'. /RL |
785 | | */ |
786 | 64.5k | *rp = r; |
787 | 64.5k | } else |
788 | 56.9M | buf->data[to++] = *(from++); |
789 | 57.5M | } |
790 | 388k | buf->data[to] = '\0'; |
791 | 388k | OPENSSL_free(*pto); |
792 | 388k | *pto = buf->data; |
793 | 388k | OPENSSL_free(buf); |
794 | 388k | return 1; |
795 | 1.31k | err: |
796 | 1.31k | BUF_MEM_free(buf); |
797 | 1.31k | return 0; |
798 | 390k | } |
799 | | |
800 | | #ifndef OPENSSL_NO_POSIX_IO |
801 | | /* |
802 | | * Check whether included path is a directory. |
803 | | * Returns next BIO to process and in case of a directory |
804 | | * also an opened directory context and the include path. |
805 | | */ |
806 | | static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx, |
807 | | char **dirpath) |
808 | 5.36k | { |
809 | 5.36k | struct stat st; |
810 | 5.36k | BIO *next; |
811 | | |
812 | 5.36k | if (stat(include, &st) < 0) { |
813 | 1.68k | ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include); |
814 | | /* missing include file is not fatal error */ |
815 | 1.68k | return NULL; |
816 | 1.68k | } |
817 | | |
818 | 3.67k | if (S_ISDIR(st.st_mode)) { |
819 | 3.53k | if (*dirctx != NULL) { |
820 | 0 | ERR_raise_data(ERR_LIB_CONF, CONF_R_RECURSIVE_DIRECTORY_INCLUDE, |
821 | 0 | "%s", include); |
822 | 0 | return NULL; |
823 | 0 | } |
824 | | /* a directory, load its contents */ |
825 | 3.53k | if ((next = get_next_file(include, dirctx)) != NULL) |
826 | 2.76k | *dirpath = include; |
827 | 3.53k | return next; |
828 | 3.53k | } |
829 | | |
830 | 144 | next = BIO_new_file(include, "r"); |
831 | 144 | return next; |
832 | 3.67k | } |
833 | | |
834 | | /* |
835 | | * Get next file from the directory path. |
836 | | * Returns BIO of the next file to read and updates dirctx. |
837 | | */ |
838 | | static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx) |
839 | 6.74k | { |
840 | 6.74k | const char *filename; |
841 | 6.74k | size_t pathlen; |
842 | | |
843 | 6.74k | pathlen = strlen(path); |
844 | 128k | while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) { |
845 | 125k | size_t namelen; |
846 | | |
847 | 125k | namelen = strlen(filename); |
848 | | |
849 | | |
850 | 125k | if ((namelen > 5 |
851 | 125k | && OPENSSL_strcasecmp(filename + namelen - 5, ".conf") == 0) |
852 | 125k | || (namelen > 4 |
853 | 115k | && OPENSSL_strcasecmp(filename + namelen - 4, ".cnf") == 0)) { |
854 | 12.2k | size_t newlen; |
855 | 12.2k | char *newpath; |
856 | 12.2k | BIO *bio; |
857 | | |
858 | 12.2k | newlen = pathlen + namelen + 2; |
859 | 12.2k | newpath = OPENSSL_zalloc(newlen); |
860 | 12.2k | if (newpath == NULL) |
861 | 0 | break; |
862 | | #ifdef OPENSSL_SYS_VMS |
863 | | /* |
864 | | * If the given path isn't clear VMS syntax, |
865 | | * we treat it as on Unix. |
866 | | */ |
867 | | if (path[pathlen - 1] == ']' |
868 | | || path[pathlen - 1] == '>' |
869 | | || path[pathlen - 1] == ':') { |
870 | | /* Clear VMS directory syntax, just copy as is */ |
871 | | OPENSSL_strlcpy(newpath, path, newlen); |
872 | | } |
873 | | #endif |
874 | 12.2k | if (newpath[0] == '\0') { |
875 | 12.2k | OPENSSL_strlcpy(newpath, path, newlen); |
876 | 12.2k | OPENSSL_strlcat(newpath, "/", newlen); |
877 | 12.2k | } |
878 | 12.2k | OPENSSL_strlcat(newpath, filename, newlen); |
879 | | |
880 | 12.2k | bio = BIO_new_file(newpath, "r"); |
881 | 12.2k | OPENSSL_free(newpath); |
882 | | /* Errors when opening files are non-fatal. */ |
883 | 12.2k | if (bio != NULL) |
884 | 3.37k | return bio; |
885 | 12.2k | } |
886 | 125k | } |
887 | 3.36k | OPENSSL_DIR_end(dirctx); |
888 | 3.36k | *dirctx = NULL; |
889 | 3.36k | return NULL; |
890 | 6.74k | } |
891 | | #endif |
892 | | |
893 | | static int is_keytype(const CONF *conf, char c, unsigned short type) |
894 | 1.52G | { |
895 | 1.52G | const unsigned short *keytypes = (const unsigned short *) conf->meth_data; |
896 | 1.52G | unsigned char key = (unsigned char)c; |
897 | | |
898 | | #ifdef CHARSET_EBCDIC |
899 | | # if CHAR_BIT > 8 |
900 | | if (key > 255) { |
901 | | /* key is out of range for os_toascii table */ |
902 | | return 0; |
903 | | } |
904 | | # endif |
905 | | /* convert key from ebcdic to ascii */ |
906 | | key = os_toascii[key]; |
907 | | #endif |
908 | | |
909 | 1.52G | if (key > 127) { |
910 | | /* key is not a seven bit ascii character */ |
911 | 55.0M | return 0; |
912 | 55.0M | } |
913 | | |
914 | 1.47G | return (keytypes[key] & type) ? 1 : 0; |
915 | 1.52G | } |
916 | | |
917 | | static char *eat_ws(CONF *conf, char *p) |
918 | 29.0M | { |
919 | 29.6M | while (IS_WS(conf, *p) && (!IS_EOF(conf, *p))) |
920 | 656k | p++; |
921 | 29.0M | return p; |
922 | 29.0M | } |
923 | | |
924 | | static void trim_ws(CONF *conf, char *start) |
925 | 367k | { |
926 | 367k | char *p = start; |
927 | | |
928 | 106M | while (!IS_EOF(conf, *p)) |
929 | 106M | p++; |
930 | 367k | p--; |
931 | 504k | while ((p >= start) && IS_WS(conf, *p)) |
932 | 137k | p--; |
933 | 367k | p++; |
934 | 367k | *p = '\0'; |
935 | 367k | } |
936 | | |
937 | | static char *eat_alpha_numeric(CONF *conf, char *p) |
938 | 443k | { |
939 | 54.6M | for (;;) { |
940 | 54.6M | if (IS_ESC(conf, *p)) { |
941 | 69.1k | p = scan_esc(conf, p); |
942 | 69.1k | continue; |
943 | 69.1k | } |
944 | 54.5M | if (!(IS_ALNUM_PUNCT(conf, *p) |
945 | 54.5M | || (conf->flag_dollarid && IS_DOLLAR(conf, *p)))) |
946 | 443k | return p; |
947 | 54.0M | p++; |
948 | 54.0M | } |
949 | 443k | } |
950 | | |
951 | | static char *scan_quote(CONF *conf, char *p) |
952 | 26.2k | { |
953 | 26.2k | int q = *p; |
954 | | |
955 | 26.2k | p++; |
956 | 43.1M | while (!(IS_EOF(conf, *p)) && (*p != q)) { |
957 | 43.1M | if (IS_ESC(conf, *p)) { |
958 | 148k | p++; |
959 | 148k | if (IS_EOF(conf, *p)) |
960 | 919 | return p; |
961 | 148k | } |
962 | 43.1M | p++; |
963 | 43.1M | } |
964 | 25.3k | if (*p == q) |
965 | 21.2k | p++; |
966 | 25.3k | return p; |
967 | 26.2k | } |
968 | | |
969 | | static char *scan_dquote(CONF *conf, char *p) |
970 | 0 | { |
971 | 0 | int q = *p; |
972 | |
|
973 | 0 | p++; |
974 | 0 | while (!(IS_EOF(conf, *p))) { |
975 | 0 | if (*p == q) { |
976 | 0 | if (*(p + 1) == q) { |
977 | 0 | p++; |
978 | 0 | } else { |
979 | 0 | break; |
980 | 0 | } |
981 | 0 | } |
982 | 0 | p++; |
983 | 0 | } |
984 | 0 | if (*p == q) |
985 | 0 | p++; |
986 | 0 | return p; |
987 | 0 | } |
988 | | |
989 | | static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out) |
990 | 0 | { |
991 | 0 | if (a->name) |
992 | 0 | BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value); |
993 | 0 | else |
994 | 0 | BIO_printf(out, "[[%s]]\n", a->section); |
995 | 0 | } |
996 | | |
997 | | IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO); |
998 | | |
999 | | static int def_dump(const CONF *conf, BIO *out) |
1000 | 0 | { |
1001 | 0 | lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out); |
1002 | 0 | return 1; |
1003 | 0 | } |
1004 | | |
1005 | | static int def_is_number(const CONF *conf, char c) |
1006 | 0 | { |
1007 | 0 | return IS_NUMBER(conf, c); |
1008 | 0 | } |
1009 | | |
1010 | | static int def_to_int(const CONF *conf, char c) |
1011 | 0 | { |
1012 | 0 | return c - '0'; |
1013 | 0 | } |