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