/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 | 0 | #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 | 0 | #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 | { |
94 | 2 | return &default_method; |
95 | 2 | } |
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 | { |
104 | 2 | CONF *ret; |
105 | | |
106 | 2 | ret = OPENSSL_malloc(sizeof(*ret)); |
107 | 2 | if (ret != NULL) |
108 | 2 | if (meth->init(ret) == 0) { |
109 | 0 | OPENSSL_free(ret); |
110 | 0 | ret = NULL; |
111 | 0 | } |
112 | 2 | return ret; |
113 | 2 | } |
114 | | |
115 | | static int def_init_default(CONF *conf) |
116 | 2 | { |
117 | 2 | if (conf == NULL) |
118 | 0 | return 0; |
119 | | |
120 | 2 | conf->meth = &default_method; |
121 | 2 | conf->meth_data = (void *)CONF_type_default; |
122 | 2 | conf->data = NULL; |
123 | | |
124 | 2 | return 1; |
125 | 2 | } |
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 | { |
141 | 2 | if (def_destroy_data(conf)) { |
142 | 2 | OPENSSL_free(conf); |
143 | 2 | return 1; |
144 | 2 | } |
145 | 0 | return 0; |
146 | 2 | } |
147 | | |
148 | | static int def_destroy_data(CONF *conf) |
149 | 2 | { |
150 | 2 | if (conf == NULL) |
151 | 0 | return 0; |
152 | 2 | _CONF_free_data(conf); |
153 | 2 | return 1; |
154 | 2 | } |
155 | | |
156 | | static int def_load(CONF *conf, const char *name, long *line) |
157 | 2 | { |
158 | 2 | int ret; |
159 | 2 | BIO *in = NULL; |
160 | | |
161 | | #ifdef OPENSSL_SYS_VMS |
162 | | in = BIO_new_file(name, "r"); |
163 | | #else |
164 | 2 | in = BIO_new_file(name, "rb"); |
165 | 2 | #endif |
166 | 2 | if (in == NULL) { |
167 | 2 | if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE) |
168 | 2 | CONFerr(CONF_F_DEF_LOAD, CONF_R_NO_SUCH_FILE); |
169 | 0 | else |
170 | 2 | CONFerr(CONF_F_DEF_LOAD, ERR_R_SYS_LIB); |
171 | 2 | return 0; |
172 | 2 | } |
173 | | |
174 | 0 | ret = def_load_bio(conf, in, line); |
175 | 0 | BIO_free(in); |
176 | |
|
177 | 0 | return ret; |
178 | 2 | } |
179 | | |
180 | | static int def_load_bio(CONF *conf, BIO *in, long *line) |
181 | 0 | { |
182 | | /* The macro BUFSIZE conflicts with a system macro in VxWorks */ |
183 | 0 | #define CONFBUFSIZE 512 |
184 | 0 | int bufnum = 0, i, ii; |
185 | 0 | BUF_MEM *buff = NULL; |
186 | 0 | char *s, *p, *end; |
187 | 0 | int again; |
188 | 0 | int first_call = 1; |
189 | 0 | long eline = 0; |
190 | 0 | char btmp[DECIMAL_SIZE(eline) + 1]; |
191 | 0 | CONF_VALUE *v = NULL, *tv; |
192 | 0 | CONF_VALUE *sv = NULL; |
193 | 0 | char *section = NULL, *buf; |
194 | 0 | char *start, *psection, *pname; |
195 | 0 | void *h = (void *)(conf->data); |
196 | 0 | STACK_OF(BIO) *biosk = NULL; |
197 | 0 | #ifndef OPENSSL_NO_POSIX_IO |
198 | 0 | char *dirpath = NULL; |
199 | 0 | OPENSSL_DIR_CTX *dirctx = NULL; |
200 | 0 | #endif |
201 | |
|
202 | 0 | 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 | 0 | section = OPENSSL_strdup("default"); |
208 | 0 | if (section == NULL) { |
209 | 0 | CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE); |
210 | 0 | goto err; |
211 | 0 | } |
212 | | |
213 | 0 | 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 | 0 | sv = _CONF_new_section(conf, section); |
219 | 0 | 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 | 0 | bufnum = 0; |
225 | 0 | again = 0; |
226 | 0 | for (;;) { |
227 | 0 | 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 | 0 | p = &(buff->data[bufnum]); |
232 | 0 | *p = '\0'; |
233 | 0 | read_retry: |
234 | 0 | BIO_gets(in, p, CONFBUFSIZE - 1); |
235 | 0 | p[CONFBUFSIZE - 1] = '\0'; |
236 | 0 | ii = i = strlen(p); |
237 | 0 | if (first_call) { |
238 | | /* Other BOMs imply unsupported multibyte encoding, |
239 | | * so don't strip them and let the error raise */ |
240 | 0 | const unsigned char utf8_bom[3] = {0xEF, 0xBB, 0xBF}; |
241 | |
|
242 | 0 | if (i >= 3 && memcmp(p, utf8_bom, 3) == 0) { |
243 | 0 | memmove(p, p + 3, i - 3); |
244 | 0 | p[i - 3] = 0; |
245 | 0 | i -= 3; |
246 | 0 | ii -= 3; |
247 | 0 | } |
248 | 0 | first_call = 0; |
249 | 0 | } |
250 | 0 | if (i == 0 && !again) { |
251 | | /* the currently processed BIO is at EOF */ |
252 | 0 | BIO *parent; |
253 | |
|
254 | 0 | #ifndef OPENSSL_NO_POSIX_IO |
255 | | /* continue processing with the next file from directory */ |
256 | 0 | if (dirctx != NULL) { |
257 | 0 | BIO *next; |
258 | |
|
259 | 0 | if ((next = get_next_file(dirpath, &dirctx)) != NULL) { |
260 | 0 | BIO_vfree(in); |
261 | 0 | in = next; |
262 | 0 | goto read_retry; |
263 | 0 | } else { |
264 | 0 | OPENSSL_free(dirpath); |
265 | 0 | dirpath = NULL; |
266 | 0 | } |
267 | 0 | } |
268 | 0 | #endif |
269 | | /* no more files in directory, continue with processing parent */ |
270 | 0 | if ((parent = sk_BIO_pop(biosk)) == NULL) { |
271 | | /* everything processed get out of the loop */ |
272 | 0 | break; |
273 | 0 | } else { |
274 | 0 | BIO_vfree(in); |
275 | 0 | in = parent; |
276 | 0 | goto read_retry; |
277 | 0 | } |
278 | 0 | } |
279 | 0 | again = 0; |
280 | 0 | while (i > 0) { |
281 | 0 | if ((p[i - 1] != '\r') && (p[i - 1] != '\n')) |
282 | 0 | break; |
283 | 0 | else |
284 | 0 | i--; |
285 | 0 | } |
286 | | /* |
287 | | * we removed some trailing stuff so there is a new line on the end. |
288 | | */ |
289 | 0 | if (ii && i == ii) |
290 | 0 | again = 1; /* long line */ |
291 | 0 | else { |
292 | 0 | p[i] = '\0'; |
293 | 0 | eline++; /* another input line */ |
294 | 0 | } |
295 | | |
296 | | /* we now have a line with trailing \r\n removed */ |
297 | | |
298 | | /* i is the number of bytes */ |
299 | 0 | bufnum += i; |
300 | |
|
301 | 0 | v = NULL; |
302 | | /* check for line continuation */ |
303 | 0 | if (bufnum >= 1) { |
304 | | /* |
305 | | * If we have bytes and the last char '\\' and second last char |
306 | | * is not '\\' |
307 | | */ |
308 | 0 | p = &(buff->data[bufnum - 1]); |
309 | 0 | if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) { |
310 | 0 | bufnum--; |
311 | 0 | again = 1; |
312 | 0 | } |
313 | 0 | } |
314 | 0 | if (again) |
315 | 0 | continue; |
316 | 0 | bufnum = 0; |
317 | 0 | buf = buff->data; |
318 | |
|
319 | 0 | clear_comments(conf, buf); |
320 | 0 | s = eat_ws(conf, buf); |
321 | 0 | if (IS_EOF(conf, *s)) |
322 | 0 | continue; /* blank line */ |
323 | 0 | if (*s == '[') { |
324 | 0 | char *ss; |
325 | |
|
326 | 0 | s++; |
327 | 0 | start = eat_ws(conf, s); |
328 | 0 | ss = start; |
329 | 0 | again: |
330 | 0 | end = eat_alpha_numeric(conf, ss); |
331 | 0 | p = eat_ws(conf, end); |
332 | 0 | if (*p != ']') { |
333 | 0 | if (*p != '\0' && ss != p) { |
334 | 0 | ss = p; |
335 | 0 | goto again; |
336 | 0 | } |
337 | 0 | CONFerr(CONF_F_DEF_LOAD_BIO, |
338 | 0 | CONF_R_MISSING_CLOSE_SQUARE_BRACKET); |
339 | 0 | goto err; |
340 | 0 | } |
341 | 0 | *end = '\0'; |
342 | 0 | if (!str_copy(conf, NULL, §ion, start)) |
343 | 0 | goto err; |
344 | 0 | if ((sv = _CONF_get_section(conf, section)) == NULL) |
345 | 0 | sv = _CONF_new_section(conf, section); |
346 | 0 | 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 | 0 | continue; |
352 | 0 | } else { |
353 | 0 | pname = s; |
354 | 0 | end = eat_alpha_numeric(conf, s); |
355 | 0 | if ((end[0] == ':') && (end[1] == ':')) { |
356 | 0 | *end = '\0'; |
357 | 0 | end += 2; |
358 | 0 | psection = pname; |
359 | 0 | pname = end; |
360 | 0 | end = eat_alpha_numeric(conf, end); |
361 | 0 | } else { |
362 | 0 | psection = section; |
363 | 0 | } |
364 | 0 | p = eat_ws(conf, end); |
365 | 0 | if (strncmp(pname, ".include", 8) == 0 |
366 | 0 | && (p != pname + 8 || *p == '=')) { |
367 | 0 | char *include = NULL; |
368 | 0 | BIO *next; |
369 | |
|
370 | 0 | if (*p == '=') { |
371 | 0 | p++; |
372 | 0 | p = eat_ws(conf, p); |
373 | 0 | } |
374 | 0 | trim_ws(conf, p); |
375 | 0 | if (!str_copy(conf, psection, &include, p)) |
376 | 0 | goto err; |
377 | | /* get the BIO of the included file */ |
378 | 0 | #ifndef OPENSSL_NO_POSIX_IO |
379 | 0 | next = process_include(include, &dirctx, &dirpath); |
380 | 0 | if (include != dirpath) { |
381 | | /* dirpath will contain include in case of a directory */ |
382 | 0 | OPENSSL_free(include); |
383 | 0 | } |
384 | | #else |
385 | | next = BIO_new_file(include, "r"); |
386 | | OPENSSL_free(include); |
387 | | #endif |
388 | 0 | if (next != NULL) { |
389 | | /* push the currently processing BIO onto stack */ |
390 | 0 | if (biosk == NULL) { |
391 | 0 | 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 | 0 | } |
397 | 0 | 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 | 0 | in = next; |
404 | 0 | } |
405 | 0 | continue; |
406 | 0 | } else if (*p != '=') { |
407 | 0 | CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN); |
408 | 0 | goto err; |
409 | 0 | } |
410 | 0 | *end = '\0'; |
411 | 0 | p++; |
412 | 0 | start = eat_ws(conf, p); |
413 | 0 | trim_ws(conf, start); |
414 | |
|
415 | 0 | 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 | 0 | v->name = OPENSSL_strdup(pname); |
420 | 0 | v->value = NULL; |
421 | 0 | if (v->name == NULL) { |
422 | 0 | CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE); |
423 | 0 | goto err; |
424 | 0 | } |
425 | 0 | if (!str_copy(conf, psection, &(v->value), start)) |
426 | 0 | goto err; |
427 | | |
428 | 0 | if (strcmp(psection, section) != 0) { |
429 | 0 | if ((tv = _CONF_get_section(conf, psection)) |
430 | 0 | == NULL) |
431 | 0 | tv = _CONF_new_section(conf, psection); |
432 | 0 | 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 | 0 | } else |
438 | 0 | tv = sv; |
439 | 0 | 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 | 0 | v = NULL; |
444 | 0 | } |
445 | 0 | } |
446 | 0 | BUF_MEM_free(buff); |
447 | 0 | 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 | 0 | sk_BIO_free(biosk); |
453 | 0 | return 1; |
454 | 0 | err: |
455 | 0 | BUF_MEM_free(buff); |
456 | 0 | 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 | 0 | while (sk_BIO_num(biosk) > 0) { |
463 | 0 | BIO *popped = sk_BIO_pop(biosk); |
464 | 0 | BIO_vfree(in); |
465 | 0 | in = popped; |
466 | 0 | } |
467 | 0 | sk_BIO_free(biosk); |
468 | 0 | #ifndef OPENSSL_NO_POSIX_IO |
469 | 0 | OPENSSL_free(dirpath); |
470 | 0 | if (dirctx != NULL) |
471 | 0 | OPENSSL_DIR_end(&dirctx); |
472 | 0 | #endif |
473 | 0 | if (line != NULL) |
474 | 0 | *line = eline; |
475 | 0 | BIO_snprintf(btmp, sizeof(btmp), "%ld", eline); |
476 | 0 | ERR_add_error_data(2, "line ", btmp); |
477 | 0 | if (h != conf->data) { |
478 | 0 | CONF_free(conf->data); |
479 | 0 | conf->data = NULL; |
480 | 0 | } |
481 | 0 | if (v != NULL) { |
482 | 0 | OPENSSL_free(v->name); |
483 | 0 | OPENSSL_free(v->value); |
484 | 0 | OPENSSL_free(v); |
485 | 0 | } |
486 | 0 | return 0; |
487 | 0 | } |
488 | | |
489 | | static void clear_comments(CONF *conf, char *p) |
490 | 0 | { |
491 | 0 | for (;;) { |
492 | 0 | if (IS_FCOMMENT(conf, *p)) { |
493 | 0 | *p = '\0'; |
494 | 0 | return; |
495 | 0 | } |
496 | 0 | if (!IS_WS(conf, *p)) { |
497 | 0 | break; |
498 | 0 | } |
499 | 0 | p++; |
500 | 0 | } |
501 | | |
502 | 0 | for (;;) { |
503 | 0 | if (IS_COMMENT(conf, *p)) { |
504 | 0 | *p = '\0'; |
505 | 0 | return; |
506 | 0 | } |
507 | 0 | if (IS_DQUOTE(conf, *p)) { |
508 | 0 | p = scan_dquote(conf, p); |
509 | 0 | continue; |
510 | 0 | } |
511 | 0 | if (IS_QUOTE(conf, *p)) { |
512 | 0 | p = scan_quote(conf, p); |
513 | 0 | continue; |
514 | 0 | } |
515 | 0 | if (IS_ESC(conf, *p)) { |
516 | 0 | p = scan_esc(conf, p); |
517 | 0 | continue; |
518 | 0 | } |
519 | 0 | if (IS_EOF(conf, *p)) |
520 | 0 | return; |
521 | 0 | else |
522 | 0 | p++; |
523 | 0 | } |
524 | 0 | } |
525 | | |
526 | | static int str_copy(CONF *conf, char *section, char **pto, char *from) |
527 | 0 | { |
528 | 0 | int q, r, rr = 0, to = 0, len = 0; |
529 | 0 | char *s, *e, *rp, *p, *rrp, *np, *cp, v; |
530 | 0 | BUF_MEM *buf; |
531 | |
|
532 | 0 | if ((buf = BUF_MEM_new()) == NULL) |
533 | 0 | return 0; |
534 | | |
535 | 0 | len = strlen(from) + 1; |
536 | 0 | if (!BUF_MEM_grow(buf, len)) |
537 | 0 | goto err; |
538 | | |
539 | 0 | for (;;) { |
540 | 0 | if (IS_QUOTE(conf, *from)) { |
541 | 0 | q = *from; |
542 | 0 | from++; |
543 | 0 | while (!IS_EOF(conf, *from) && (*from != q)) { |
544 | 0 | if (IS_ESC(conf, *from)) { |
545 | 0 | from++; |
546 | 0 | if (IS_EOF(conf, *from)) |
547 | 0 | break; |
548 | 0 | } |
549 | 0 | buf->data[to++] = *(from++); |
550 | 0 | } |
551 | 0 | if (*from == q) |
552 | 0 | from++; |
553 | 0 | } 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 | 0 | } else if (IS_ESC(conf, *from)) { |
569 | 0 | from++; |
570 | 0 | v = *(from++); |
571 | 0 | if (IS_EOF(conf, v)) |
572 | 0 | break; |
573 | 0 | else if (v == 'r') |
574 | 0 | v = '\r'; |
575 | 0 | else if (v == 'n') |
576 | 0 | v = '\n'; |
577 | 0 | else if (v == 'b') |
578 | 0 | v = '\b'; |
579 | 0 | else if (v == 't') |
580 | 0 | v = '\t'; |
581 | 0 | buf->data[to++] = v; |
582 | 0 | } else if (IS_EOF(conf, *from)) |
583 | 0 | break; |
584 | 0 | else if (*from == '$') { |
585 | 0 | size_t newsize; |
586 | | |
587 | | /* try to expand it */ |
588 | 0 | rrp = NULL; |
589 | 0 | s = &(from[1]); |
590 | 0 | if (*s == '{') |
591 | 0 | q = '}'; |
592 | 0 | else if (*s == '(') |
593 | 0 | q = ')'; |
594 | 0 | else |
595 | 0 | q = 0; |
596 | |
|
597 | 0 | if (q) |
598 | 0 | s++; |
599 | 0 | cp = section; |
600 | 0 | e = np = s; |
601 | 0 | while (IS_ALNUM(conf, *e)) |
602 | 0 | e++; |
603 | 0 | if ((e[0] == ':') && (e[1] == ':')) { |
604 | 0 | cp = np; |
605 | 0 | rrp = e; |
606 | 0 | rr = *e; |
607 | 0 | *rrp = '\0'; |
608 | 0 | e += 2; |
609 | 0 | np = e; |
610 | 0 | while (IS_ALNUM(conf, *e)) |
611 | 0 | e++; |
612 | 0 | } |
613 | 0 | r = *e; |
614 | 0 | *e = '\0'; |
615 | 0 | rp = e; |
616 | 0 | if (q) { |
617 | 0 | if (r != q) { |
618 | 0 | CONFerr(CONF_F_STR_COPY, CONF_R_NO_CLOSE_BRACE); |
619 | 0 | goto err; |
620 | 0 | } |
621 | 0 | e++; |
622 | 0 | } |
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 | 0 | p = _CONF_get_string(conf, cp, np); |
634 | 0 | if (rrp != NULL) |
635 | 0 | *rrp = rr; |
636 | 0 | *rp = r; |
637 | 0 | if (p == NULL) { |
638 | 0 | CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_HAS_NO_VALUE); |
639 | 0 | goto err; |
640 | 0 | } |
641 | 0 | newsize = strlen(p) + buf->length - (e - from); |
642 | 0 | if (newsize > MAX_CONF_VALUE_LENGTH) { |
643 | 0 | CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_EXPANSION_TOO_LONG); |
644 | 0 | goto err; |
645 | 0 | } |
646 | 0 | 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 | 0 | while (*p) |
651 | 0 | 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 | 0 | len -= e - from; |
658 | 0 | 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 | 0 | *rp = r; |
666 | 0 | } else |
667 | 0 | buf->data[to++] = *(from++); |
668 | 0 | } |
669 | 0 | buf->data[to] = '\0'; |
670 | 0 | OPENSSL_free(*pto); |
671 | 0 | *pto = buf->data; |
672 | 0 | OPENSSL_free(buf); |
673 | 0 | return 1; |
674 | 0 | err: |
675 | 0 | BUF_MEM_free(buf); |
676 | 0 | return 0; |
677 | 0 | } |
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 | 0 | { |
688 | 0 | struct stat st = { 0 }; |
689 | 0 | BIO *next; |
690 | |
|
691 | 0 | if (stat(include, &st) < 0) { |
692 | 0 | SYSerr(SYS_F_STAT, errno); |
693 | 0 | ERR_add_error_data(1, include); |
694 | | /* missing include file is not fatal error */ |
695 | 0 | return NULL; |
696 | 0 | } |
697 | | |
698 | 0 | if (S_ISDIR(st.st_mode)) { |
699 | 0 | 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 | 0 | if ((next = get_next_file(include, dirctx)) != NULL) |
707 | 0 | *dirpath = include; |
708 | 0 | return next; |
709 | 0 | } |
710 | | |
711 | 0 | next = BIO_new_file(include, "r"); |
712 | 0 | return next; |
713 | 0 | } |
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 | 0 | { |
721 | 0 | const char *filename; |
722 | 0 | size_t pathlen; |
723 | |
|
724 | 0 | pathlen = strlen(path); |
725 | 0 | while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) { |
726 | 0 | size_t namelen; |
727 | |
|
728 | 0 | namelen = strlen(filename); |
729 | | |
730 | |
|
731 | 0 | if ((namelen > 5 && strcasecmp(filename + namelen - 5, ".conf") == 0) |
732 | 0 | || (namelen > 4 && strcasecmp(filename + namelen - 4, ".cnf") == 0)) { |
733 | 0 | size_t newlen; |
734 | 0 | char *newpath; |
735 | 0 | BIO *bio; |
736 | |
|
737 | 0 | newlen = pathlen + namelen + 2; |
738 | 0 | newpath = OPENSSL_zalloc(newlen); |
739 | 0 | 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 | 0 | if (newpath[0] == '\0') { |
756 | 0 | OPENSSL_strlcpy(newpath, path, newlen); |
757 | 0 | OPENSSL_strlcat(newpath, "/", newlen); |
758 | 0 | } |
759 | 0 | OPENSSL_strlcat(newpath, filename, newlen); |
760 | |
|
761 | 0 | bio = BIO_new_file(newpath, "r"); |
762 | 0 | OPENSSL_free(newpath); |
763 | | /* Errors when opening files are non-fatal. */ |
764 | 0 | if (bio != NULL) |
765 | 0 | return bio; |
766 | 0 | } |
767 | 0 | } |
768 | 0 | OPENSSL_DIR_end(dirctx); |
769 | 0 | *dirctx = NULL; |
770 | 0 | return NULL; |
771 | 0 | } |
772 | | #endif |
773 | | |
774 | | static int is_keytype(const CONF *conf, char c, unsigned short type) |
775 | 0 | { |
776 | 0 | const unsigned short * keytypes = (const unsigned short *) conf->meth_data; |
777 | 0 | 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 | 0 | if (key > 127) { |
791 | | /* key is not a seven bit ascii character */ |
792 | 0 | return 0; |
793 | 0 | } |
794 | | |
795 | 0 | return (keytypes[key] & type) ? 1 : 0; |
796 | 0 | } |
797 | | |
798 | | static char *eat_ws(CONF *conf, char *p) |
799 | 0 | { |
800 | 0 | while (IS_WS(conf, *p) && (!IS_EOF(conf, *p))) |
801 | 0 | p++; |
802 | 0 | return p; |
803 | 0 | } |
804 | | |
805 | | static void trim_ws(CONF *conf, char *start) |
806 | 0 | { |
807 | 0 | char *p = start; |
808 | |
|
809 | 0 | while (!IS_EOF(conf, *p)) |
810 | 0 | p++; |
811 | 0 | p--; |
812 | 0 | while ((p >= start) && IS_WS(conf, *p)) |
813 | 0 | p--; |
814 | 0 | p++; |
815 | 0 | *p = '\0'; |
816 | 0 | } |
817 | | |
818 | | static char *eat_alpha_numeric(CONF *conf, char *p) |
819 | 0 | { |
820 | 0 | for (;;) { |
821 | 0 | if (IS_ESC(conf, *p)) { |
822 | 0 | p = scan_esc(conf, p); |
823 | 0 | continue; |
824 | 0 | } |
825 | 0 | if (!IS_ALNUM_PUNCT(conf, *p)) |
826 | 0 | return p; |
827 | 0 | p++; |
828 | 0 | } |
829 | 0 | } |
830 | | |
831 | | static char *scan_quote(CONF *conf, char *p) |
832 | 0 | { |
833 | 0 | int q = *p; |
834 | |
|
835 | 0 | p++; |
836 | 0 | while (!(IS_EOF(conf, *p)) && (*p != q)) { |
837 | 0 | if (IS_ESC(conf, *p)) { |
838 | 0 | p++; |
839 | 0 | if (IS_EOF(conf, *p)) |
840 | 0 | return p; |
841 | 0 | } |
842 | 0 | p++; |
843 | 0 | } |
844 | 0 | if (*p == q) |
845 | 0 | p++; |
846 | 0 | return p; |
847 | 0 | } |
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 | } |