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