Debian Codegraph @

external/boringssl/crypto/conf/conf.c

raw
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young ([email protected]).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson ([email protected]).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young ([email protected])"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson ([email protected])"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <openssl/conf.h>
58
59 #include <string.h>
60 #include <ctype.h>
61
62 #include <openssl/bio.h>
63 #include <openssl/buf.h>
64 #include <openssl/err.h>
65 #include <openssl/mem.h>
66
67 #include "conf_def.h"
68 #include "internal.h"
69
70
71 static uint32_t conf_value_hash(const CONF_VALUE *v) {
72 return (lh_strhash(v->section) << 2) ^ lh_strhash(v->name);
73 }
74
75 static int conf_value_cmp(const CONF_VALUE *a, const CONF_VALUE *b) {
76 int i;
77
78 if (a->section != b->section) {
79 i = strcmp(a->section, b->section);
80 if (i) {
81 return i;
82 }
83 }
84
85 if (a->name != NULL && b->name != NULL) {
86 return strcmp(a->name, b->name);
87 } else if (a->name == b->name) {
88 return 0;
89 } else {
90 return (a->name == NULL) ? -1 : 1;
91 }
92 }
93
94 CONF *NCONF_new(void *method) {
95 CONF *conf;
96
97 if (method != NULL) {
98 return NULL;
99 }
100
101 conf = OPENSSL_malloc(sizeof(CONF));
102 if (conf == NULL) {
103 return NULL;
104 }
105
106 conf->data = lh_CONF_VALUE_new(conf_value_hash, conf_value_cmp);
107 if (conf->data == NULL) {
108 OPENSSL_free(conf);
109 return NULL;
110 }
111
112 return conf;
113 }
114
115 CONF_VALUE *CONF_VALUE_new(void) {
116 CONF_VALUE *v = OPENSSL_malloc(sizeof(CONF_VALUE));
117 if (!v) {
118 OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
119 return NULL;
120 }
121 memset(v, 0, sizeof(CONF_VALUE));
122 return v;
123 }
124
125 static void value_free_contents(CONF_VALUE *value) {
126 if (value->section) {
127 OPENSSL_free(value->section);
128 }
129 if (value->name) {
130 OPENSSL_free(value->name);
131 if (value->value) {
132 OPENSSL_free(value->value);
133 }
134 } else {
135 if (value->value) {
136 sk_CONF_VALUE_free((STACK_OF(CONF_VALUE)*)value->value);
137 }
138 }
139 }
140
141 static void value_free(CONF_VALUE *value) {
142 value_free_contents(value);
143 OPENSSL_free(value);
144 }
145
146 void NCONF_free(CONF *conf) {
147 if (conf == NULL || conf->data == NULL) {
148 return;
149 }
150
151 lh_CONF_VALUE_doall(conf->data, value_free);
152 lh_CONF_VALUE_free(conf->data);
153 OPENSSL_free(conf);
154 }
155
156 static CONF_VALUE *NCONF_new_section(const CONF *conf, const char *section) {
157 STACK_OF(CONF_VALUE) *sk = NULL;
158 int ok = 0;
159 CONF_VALUE *v = NULL, *old_value;
160
161 sk = sk_CONF_VALUE_new_null();
162 v = CONF_VALUE_new();
163 if (sk == NULL || v == NULL) {
164 goto err;
165 }
166 v->section = OPENSSL_strdup(section);
167 if (v->section == NULL) {
168 goto err;
169 }
170
171 v->name = NULL;
172 v->value = (char *)sk;
173
174 if (!lh_CONF_VALUE_insert(conf->data, &old_value, v)) {
175 goto err;
176 }
177 if (old_value) {
178 value_free(old_value);
179 }
180 ok = 1;
181
182 err:
183 if (!ok) {
184 if (sk != NULL) {
185 sk_CONF_VALUE_free(sk);
186 }
187 if (v != NULL) {
188 OPENSSL_free(v);
189 }
190 v = NULL;
191 }
192 return v;
193 }
194
195 static int str_copy(CONF *conf, char *section, char **pto, char *from) {
196 int q, r, rr = 0, to = 0, len = 0;
197 char *s, *e, *rp, *rrp, *np, *cp, v;
198 const char *p;
199 BUF_MEM *buf;
200
201 buf = BUF_MEM_new();
202 if (buf == NULL) {
203 return 0;
204 }
205
206 len = strlen(from) + 1;
207 if (!BUF_MEM_grow(buf, len)) {
208 goto err;
209 }
210
211 for (;;) {
212 if (IS_QUOTE(conf, *from)) {
213 q = *from;
214 from++;
215 while (!IS_EOF(conf, *from) && (*from != q)) {
216 if (IS_ESC(conf, *from)) {
217 from++;
218 if (IS_EOF(conf, *from)) {
219 break;
220 }
221 }
222 buf->data[to++] = *(from++);
223 }
224 if (*from == q) {
225 from++;
226 }
227 } else if (IS_DQUOTE(conf, *from)) {
228 q = *from;
229 from++;
230 while (!IS_EOF(conf, *from)) {
231 if (*from == q) {
232 if (*(from + 1) == q) {
233 from++;
234 } else {
235 break;
236 }
237 }
238 buf->data[to++] = *(from++);
239 }
240 if (*from == q) {
241 from++;
242 }
243 } else if (IS_ESC(conf, *from)) {
244 from++;
245 v = *(from++);
246 if (IS_EOF(conf, v)) {
247 break;
248 } else if (v == 'r') {
249 v = '\r';
250 } else if (v == 'n') {
251 v = '\n';
252 } else if (v == 'b') {
253 v = '\b';
254 } else if (v == 't') {
255 v = '\t';
256 }
257 buf->data[to++] = v;
258 } else if (IS_EOF(conf, *from)) {
259 break;
260 } else if (*from == '$') {
261 /* try to expand it */
262 rrp = NULL;
263 s = &(from[1]);
264 if (*s == '{') {
265 q = '}';
266 } else if (*s == '(') {
267 q = ')';
268 } else {
269 q = 0;
270 }
271
272 if (q) {
273 s++;
274 }
275 cp = section;
276 e = np = s;
277 while (IS_ALPHA_NUMERIC(conf, *e)) {
278 e++;
279 }
280 if (e[0] == ':' && e[1] == ':') {
281 cp = np;
282 rrp = e;
283 rr = *e;
284 *rrp = '\0';
285 e += 2;
286 np = e;
287 while (IS_ALPHA_NUMERIC(conf, *e)) {
288 e++;
289 }
290 }
291 r = *e;
292 *e = '\0';
293 rp = e;
294 if (q) {
295 if (r != q) {
296 OPENSSL_PUT_ERROR(CONF, CONF_R_NO_CLOSE_BRACE);
297 goto err;
298 }
299 e++;
300 }
301 /* So at this point we have
302 * np which is the start of the name string which is
303 * '\0' terminated.
304 * cp which is the start of the section string which is
305 * '\0' terminated.
306 * e is the 'next point after'.
307 * r and rr are the chars replaced by the '\0'
308 * rp and rrp is where 'r' and 'rr' came from. */
309 p = NCONF_get_string(conf, cp, np);
310 if (rrp != NULL) {
311 *rrp = rr;
312 }
313 *rp = r;
314 if (p == NULL) {
315 OPENSSL_PUT_ERROR(CONF, CONF_R_VARIABLE_HAS_NO_VALUE);
316 goto err;
317 }
318 BUF_MEM_grow_clean(buf, (strlen(p) + buf->length - (e - from)));
319 while (*p) {
320 buf->data[to++] = *(p++);
321 }
322
323 /* Since we change the pointer 'from', we also have
324 to change the perceived length of the string it
325 points at. /RL */
326 len -= e - from;
327 from = e;
328
329 /* In case there were no braces or parenthesis around
330 the variable reference, we have to put back the
331 character that was replaced with a '\0'. /RL */
332 *rp = r;
333 } else {
334 buf->data[to++] = *(from++);
335 }
336 }
337
338 buf->data[to] = '\0';
339 if (*pto != NULL) {
340 OPENSSL_free(*pto);
341 }
342 *pto = buf->data;
343 OPENSSL_free(buf);
344 return 1;
345
346 err:
347 if (buf != NULL) {
348 BUF_MEM_free(buf);
349 }
350 return 0;
351 }
352
353 static CONF_VALUE *get_section(const CONF *conf, const char *section) {
354 CONF_VALUE template;
355
356 memset(&template, 0, sizeof(template));
357 template.section = (char *) section;
358 return lh_CONF_VALUE_retrieve(conf->data, &template);
359 }
360
361 STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section) {
362 CONF_VALUE *section_value = get_section(conf, section);
363 if (section_value == NULL) {
364 return NULL;
365 }
366 return (STACK_OF(CONF_VALUE)*) section_value->value;
367 }
368
369 const char *NCONF_get_string(const CONF *conf, const char *section,
370 const char *name) {
371 CONF_VALUE template, *value;
372
373 memset(&template, 0, sizeof(template));
374 template.section = (char *) section;
375 template.name = (char *) name;
376 value = lh_CONF_VALUE_retrieve(conf->data, &template);
377 if (value == NULL) {
378 return NULL;
379 }
380 return value->value;
381 }
382
383 static int add_string(const CONF *conf, CONF_VALUE *section,
384 CONF_VALUE *value) {
385 STACK_OF(CONF_VALUE) *section_stack = (STACK_OF(CONF_VALUE)*) section->value;
386 CONF_VALUE *old_value;
387
388 value->section = OPENSSL_strdup(section->section);
389 if (!sk_CONF_VALUE_push(section_stack, value)) {
390 return 0;
391 }
392
393 if (!lh_CONF_VALUE_insert(conf->data, &old_value, value)) {
394 return 0;
395 }
396 if (old_value != NULL) {
397 (void)sk_CONF_VALUE_delete_ptr(section_stack, old_value);
398 value_free(old_value);
399 }
400
401 return 1;
402 }
403
404 static char *eat_ws(CONF *conf, char *p) {
405 while (IS_WS(conf, *p) && !IS_EOF(conf, *p)) {
406 p++;
407 }
408 return p;
409 }
410
411 #define scan_esc(conf, p) (((IS_EOF((conf), (p)[1])) ? ((p) + 1) : ((p) + 2)))
412
413 static char *eat_alpha_numeric(CONF *conf, char *p) {
414 for (;;) {
415 if (IS_ESC(conf, *p)) {
416 p = scan_esc(conf, p);
417 continue;
418 }
419 if (!IS_ALPHA_NUMERIC_PUNCT(conf, *p)) {
420 return p;
421 }
422 p++;
423 }
424 }
425
426 static char *scan_quote(CONF *conf, char *p) {
427 int q = *p;
428
429 p++;
430 while (!IS_EOF(conf, *p) && *p != q) {
431 if (IS_ESC(conf, *p)) {
432 p++;
433 if (IS_EOF(conf, *p)) {
434 return p;
435 }
436 }
437 p++;
438 }
439 if (*p == q) {
440 p++;
441 }
442 return p;
443 }
444
445
446 static char *scan_dquote(CONF *conf, char *p) {
447 int q = *p;
448
449 p++;
450 while (!(IS_EOF(conf, *p))) {
451 if (*p == q) {
452 if (*(p + 1) == q) {
453 p++;
454 } else {
455 break;
456 }
457 }
458 p++;
459 }
460 if (*p == q) {
461 p++;
462 }
463 return p;
464 }
465
466 static void clear_comments(CONF *conf, char *p) {
467 for (;;) {
468 if (IS_FCOMMENT(conf, *p)) {
469 *p = '\0';
470 return;
471 }
472 if (!IS_WS(conf, *p)) {
473 break;
474 }
475 p++;
476 }
477
478 for (;;) {
479 if (IS_COMMENT(conf, *p)) {
480 *p = '\0';
481 return;
482 }
483 if (IS_DQUOTE(conf, *p)) {
484 p = scan_dquote(conf, p);
485 continue;
486 }
487 if (IS_QUOTE(conf, *p)) {
488 p = scan_quote(conf, p);
489 continue;
490 }
491 if (IS_ESC(conf, *p)) {
492 p = scan_esc(conf, p);
493 continue;
494 }
495 if (IS_EOF(conf, *p)) {
496 return;
497 } else {
498 p++;
499 }
500 }
501 }
502
503 static int def_load_bio(CONF *conf, BIO *in, long *out_error_line) {
504 static const size_t CONFBUFSIZE = 512;
505 int bufnum = 0, i, ii;
506 BUF_MEM *buff = NULL;
507 char *s, *p, *end;
508 int again;
509 long eline = 0;
510 char btmp[DECIMAL_SIZE(eline) + 1];
511 CONF_VALUE *v = NULL, *tv;
512 CONF_VALUE *sv = NULL;
513 char *section = NULL, *buf;
514 char *start, *psection, *pname;
515
516 if ((buff = BUF_MEM_new()) == NULL) {
517 OPENSSL_PUT_ERROR(CONF, ERR_R_BUF_LIB);
518 goto err;
519 }
520
521 section = OPENSSL_strdup("default");
522 if (section == NULL) {
523 OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
524 goto err;
525 }
526
527 sv = NCONF_new_section(conf, section);
528 if (sv == NULL) {
529 OPENSSL_PUT_ERROR(CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
530 goto err;
531 }
532
533 bufnum = 0;
534 again = 0;
535 for (;;) {
536 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
537 OPENSSL_PUT_ERROR(CONF, ERR_R_BUF_LIB);
538 goto err;
539 }
540 p = &(buff->data[bufnum]);
541 *p = '\0';
542 BIO_gets(in, p, CONFBUFSIZE - 1);
543 p[CONFBUFSIZE - 1] = '\0';
544 ii = i = strlen(p);
545 if (i == 0 && !again) {
546 break;
547 }
548 again = 0;
549 while (i > 0) {
550 if ((p[i - 1] != '\r') && (p[i - 1] != '\n')) {
551 break;
552 } else {
553 i--;
554 }
555 }
556 /* we removed some trailing stuff so there is a new
557 * line on the end. */
558 if (ii && i == ii) {
559 again = 1; /* long line */
560 } else {
561 p[i] = '\0';
562 eline++; /* another input line */
563 }
564
565 /* we now have a line with trailing \r\n removed */
566
567 /* i is the number of bytes */
568 bufnum += i;
569
570 v = NULL;
571 /* check for line continuation */
572 if (bufnum >= 1) {
573 /* If we have bytes and the last char '\\' and
574 * second last char is not '\\' */
575 p = &(buff->data[bufnum - 1]);
576 if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
577 bufnum--;
578 again = 1;
579 }
580 }
581 if (again) {
582 continue;
583 }
584 bufnum = 0;
585 buf = buff->data;
586
587 clear_comments(conf, buf);
588 s = eat_ws(conf, buf);
589 if (IS_EOF(conf, *s)) {
590 continue; /* blank line */
591 }
592 if (*s == '[') {
593 char *ss;
594
595 s++;
596 start = eat_ws(conf, s);
597 ss = start;
598 again:
599 end = eat_alpha_numeric(conf, ss);
600 p = eat_ws(conf, end);
601 if (*p != ']') {
602 if (*p != '\0' && ss != p) {
603 ss = p;
604 goto again;
605 }
606 OPENSSL_PUT_ERROR(CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
607 goto err;
608 }
609 *end = '\0';
610 if (!str_copy(conf, NULL, &section, start)) {
611 goto err;
612 }
613 if ((sv = get_section(conf, section)) == NULL) {
614 sv = NCONF_new_section(conf, section);
615 }
616 if (sv == NULL) {
617 OPENSSL_PUT_ERROR(CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
618 goto err;
619 }
620 continue;
621 } else {
622 pname = s;
623 psection = NULL;
624 end = eat_alpha_numeric(conf, s);
625 if ((end[0] == ':') && (end[1] == ':')) {
626 *end = '\0';
627 end += 2;
628 psection = pname;
629 pname = end;
630 end = eat_alpha_numeric(conf, end);
631 }
632 p = eat_ws(conf, end);
633 if (*p != '=') {
634 OPENSSL_PUT_ERROR(CONF, CONF_R_MISSING_EQUAL_SIGN);
635 goto err;
636 }
637 *end = '\0';
638 p++;
639 start = eat_ws(conf, p);
640 while (!IS_EOF(conf, *p)) {
641 p++;
642 }
643 p--;
644 while ((p != start) && (IS_WS(conf, *p))) {
645 p--;
646 }
647 p++;
648 *p = '\0';
649
650 if (!(v = CONF_VALUE_new())) {
651 goto err;
652 }
653 if (psection == NULL) {
654 psection = section;
655 }
656 v->name = OPENSSL_strdup(pname);
657 if (v->name == NULL) {
658 OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
659 goto err;
660 }
661 if (!str_copy(conf, psection, &(v->value), start)) {
662 goto err;
663 }
664
665 if (strcmp(psection, section) != 0) {
666 if ((tv = get_section(conf, psection)) == NULL) {
667 tv = NCONF_new_section(conf, psection);
668 }
669 if (tv == NULL) {
670 OPENSSL_PUT_ERROR(CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
671 goto err;
672 }
673 } else {
674 tv = sv;
675 }
676 if (add_string(conf, tv, v) == 0) {
677 OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
678 goto err;
679 }
680 v = NULL;
681 }
682 }
683 if (buff != NULL) {
684 BUF_MEM_free(buff);
685 }
686 if (section != NULL) {
687 OPENSSL_free(section);
688 }
689 return 1;
690
691 err:
692 if (buff != NULL) {
693 BUF_MEM_free(buff);
694 }
695 if (section != NULL) {
696 OPENSSL_free(section);
697 }
698 if (out_error_line != NULL) {
699 *out_error_line = eline;
700 }
701 BIO_snprintf(btmp, sizeof btmp, "%ld", eline);
702 ERR_add_error_data(2, "line ", btmp);
703
704 if (v != NULL) {
705 if (v->name != NULL) {
706 OPENSSL_free(v->name);
707 }
708 if (v->value != NULL) {
709 OPENSSL_free(v->value);
710 }
711 if (v != NULL) {
712 OPENSSL_free(v);
713 }
714 }
715 return 0;
716 }
717
718 int NCONF_load(CONF *conf, const char *filename, long *out_error_line) {
719 BIO *in = BIO_new_file(filename, "rb");
720 int ret;
721
722 if (in == NULL) {
723 OPENSSL_PUT_ERROR(CONF, ERR_R_SYS_LIB);
724 return 0;
725 }
726
727 ret = def_load_bio(conf, in, out_error_line);
728 BIO_free(in);
729
730 return ret;
731 }
732
733 int NCONF_load_bio(CONF *conf, BIO *bio, long *out_error_line) {
734 return def_load_bio(conf, bio, out_error_line);
735 }
736
737 int CONF_parse_list(const char *list, char sep, int remove_whitespace,
738 int (*list_cb)(const char *elem, int len, void *usr),
739 void *arg) {
740 int ret;
741 const char *lstart, *tmpend, *p;
742
743 if (list == NULL) {
744 OPENSSL_PUT_ERROR(CONF, CONF_R_LIST_CANNOT_BE_NULL);
745 return 0;
746 }
747
748 lstart = list;
749 for (;;) {
750 if (remove_whitespace) {
751 while (*lstart && isspace((unsigned char)*lstart)) {
752 lstart++;
753 }
754 }
755 p = strchr(lstart, sep);
756 if (p == lstart || !*lstart) {
757 ret = list_cb(NULL, 0, arg);
758 } else {
759 if (p) {
760 tmpend = p - 1;
761 } else {
762 tmpend = lstart + strlen(lstart) - 1;
763 }
764 if (remove_whitespace) {
765 while (isspace((unsigned char)*tmpend)) {
766 tmpend--;
767 }
768 }
769 ret = list_cb(lstart, tmpend - lstart + 1, arg);
770 }
771 if (ret <= 0) {
772 return ret;
773 }
774 if (p == NULL) {
775 return 1;
776 }
777 lstart = p + 1;
778 }
779 }
780
781 int CONF_modules_load_file(CONF_MUST_BE_NULL *filename, const char *appname,
782 unsigned long flags) {
783 return 1;
784 }
785
786 void CONF_modules_free(void) {}
787
788 void OPENSSL_config(CONF_MUST_BE_NULL *config_name) {}