Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/conf/conf.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/conf.h>
5
6 #include <ctype.h>
7 #include <string.h>
8
9 #include <openssl/bio.h>
10 #include <openssl/buf.h>
11 #include <openssl/err.h>
12 #include <openssl/lhash.h>
13 #include <openssl/mem.h>
14
15 #include "../internal.h"
16 #include "conf_def.h"
17 #include "internal.h"
18
19
20 static const char kDefaultSectionName[] = "default";
21
22 static uint32_t conf_value_hash(const CONF_VALUE *v) {
23 const uint32_t section_hash = v->section ? OPENSSL_strhash(v->section) : 0;
24 const uint32_t name_hash = v->name ? OPENSSL_strhash(v->name) : 0;
25 return (section_hash << 2) ^ name_hash;
26 }
27
28 static int conf_value_cmp(const CONF_VALUE *a, const CONF_VALUE *b) {
29 int i;
30
31 if (a->section != b->section) {
32 i = strcmp(a->section, b->section);
33 if (i) {
34 return i;
35 }
36 }
37
38 if (a->name != NULL && b->name != NULL) {
39 return strcmp(a->name, b->name);
40 } else if (a->name == b->name) {
41 return 0;
42 } else {
43 return (a->name == NULL) ? -1 : 1;
44 }
45 }
46
47 CONF *NCONF_new(void *method) {
48 CONF *conf;
49
50 if (method != NULL) {
51 return NULL;
52 }
53
54 conf = OPENSSL_malloc(sizeof(CONF));
55 if (conf == NULL) {
56 return NULL;
57 }
58
59 conf->data = lh_CONF_VALUE_new(conf_value_hash, conf_value_cmp);
60 if (conf->data == NULL) {
61 OPENSSL_free(conf);
62 return NULL;
63 }
64
65 return conf;
66 }
67
68 CONF_VALUE *CONF_VALUE_new(void) { return OPENSSL_zalloc(sizeof(CONF_VALUE)); }
69
70 static void value_free_contents(CONF_VALUE *value) {
71 OPENSSL_free(value->section);
72 if (value->name) {
73 OPENSSL_free(value->name);
74 OPENSSL_free(value->value);
75 } else {
76 // TODO(davidben): When |value->name| is NULL, |CONF_VALUE| is actually an
77 // entirely different structure. This is fragile and confusing. Make a
78 // proper |CONF_SECTION| type that doesn't require this.
79 sk_CONF_VALUE_free((STACK_OF(CONF_VALUE) *)value->value);
80 }
81 }
82
83 static void value_free(CONF_VALUE *value) {
84 if (value != NULL) {
85 value_free_contents(value);
86 OPENSSL_free(value);
87 }
88 }
89
90 static void value_free_arg(CONF_VALUE *value, void *arg) { value_free(value); }
91
92 void NCONF_free(CONF *conf) {
93 if (conf == NULL || conf->data == NULL) {
94 return;
95 }
96
97 lh_CONF_VALUE_doall_arg(conf->data, value_free_arg, NULL);
98 lh_CONF_VALUE_free(conf->data);
99 OPENSSL_free(conf);
100 }
101
102 static CONF_VALUE *NCONF_new_section(const CONF *conf, const char *section) {
103 STACK_OF(CONF_VALUE) *sk = NULL;
104 int ok = 0;
105 CONF_VALUE *v = NULL, *old_value;
106
107 sk = sk_CONF_VALUE_new_null();
108 v = CONF_VALUE_new();
109 if (sk == NULL || v == NULL) {
110 goto err;
111 }
112 v->section = OPENSSL_strdup(section);
113 if (v->section == NULL) {
114 goto err;
115 }
116
117 v->name = NULL;
118 v->value = (char *)sk;
119
120 if (!lh_CONF_VALUE_insert(conf->data, &old_value, v)) {
121 goto err;
122 }
123 value_free(old_value);
124 ok = 1;
125
126 err:
127 if (!ok) {
128 sk_CONF_VALUE_free(sk);
129 OPENSSL_free(v);
130 v = NULL;
131 }
132 return v;
133 }
134
135 static int str_copy(CONF *conf, char *section, char **pto, char *from) {
136 int q = 0, to = 0, len = 0, r = 0;
137 char *rrp = NULL, *s = NULL, *cp = NULL, *e = NULL, *rp = NULL, *np = NULL;
138 const char *p = NULL;
139 char rr = 0;
140 char v;
141 BUF_MEM *buf;
142
143 buf = BUF_MEM_new();
144 if (buf == NULL) {
145 return 0;
146 }
147
148 len = strlen(from) + 1;
149 if (!BUF_MEM_grow(buf, len)) {
150 goto err;
151 }
152
153 for (;;) {
154 if (IS_QUOTE(conf, *from)) {
155 q = *from;
156 from++;
157 while (!IS_EOF(conf, *from) && (*from != q)) {
158 if (IS_ESC(conf, *from)) {
159 from++;
160 if (IS_EOF(conf, *from)) {
161 break;
162 }
163 }
164 buf->data[to++] = *(from++);
165 }
166 if (*from == q) {
167 from++;
168 } else {
169 OPENSSL_PUT_ERROR(CONF, CONF_R_NO_CLOSE_QUOTE);
170 goto err;
171 }
172 } else if (IS_ESC(conf, *from)) {
173 from++;
174 v = *(from++);
175 if (IS_EOF(conf, v)) {
176 break;
177 } else if (v == 'r') {
178 v = '\r';
179 } else if (v == 'n') {
180 v = '\n';
181 } else if (v == 'b') {
182 v = '\b';
183 } else if (v == 't') {
184 v = '\t';
185 }
186 buf->data[to++] = v;
187 } else if (IS_EOF(conf, *from)) {
188 break;
189 } else if (*from == '$') {
190 size_t newsize = 0;
191
192 // try to expand it
193 rrp = NULL;
194 s = &(from[1]);
195 if (*s == '{') {
196 q = '}';
197 } else if (*s == '(') {
198 q = ')';
199 } else {
200 q = 0;
201 }
202
203 if (q) {
204 s++;
205 }
206 cp = section;
207 e = np = s;
208 while (IS_ALPHA_NUMERIC(conf, *e)) {
209 e++;
210 }
211 if ((e[0] == ':') && (e[1] == ':')) {
212 cp = np;
213 rrp = e;
214 rr = *e;
215 *rrp = '\0';
216 e += 2;
217 np = e;
218 while (IS_ALPHA_NUMERIC(conf, *e)) {
219 e++;
220 }
221 }
222 r = *e;
223 *e = '\0';
224 rp = e;
225 if (q) {
226 if (r != q) {
227 OPENSSL_PUT_ERROR(CONF, CONF_R_NO_CLOSE_BRACE);
228 goto err;
229 }
230 e++;
231 }
232 // So at this point we have
233 // np which is the start of the name string which is
234 // '\0' terminated.
235 // cp which is the start of the section string which is
236 // '\0' terminated.
237 // e is the 'next point after'.
238 // r and rr are the chars replaced by the '\0'
239 // rp and rrp is where 'r' and 'rr' came from.
240 p = NCONF_get_string(conf, cp, np);
241 if (rrp != NULL) {
242 *rrp = rr;
243 }
244 *rp = r;
245 if (p == NULL) {
246 OPENSSL_PUT_ERROR(CONF, CONF_R_VARIABLE_HAS_NO_VALUE);
247 goto err;
248 }
249 newsize = strlen(p) + buf->length - (e - from);
250 if (newsize > UINT16_MAX) {
251 OPENSSL_PUT_ERROR(CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
252 goto err;
253 }
254 if (!BUF_MEM_grow_clean(buf, newsize)) {
255 OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
256 goto err;
257 }
258 while (*p) {
259 buf->data[to++] = *(p++);
260 }
261
262 from = e;
263 } else {
264 buf->data[to++] = *(from++);
265 }
266 }
267
268 buf->data[to] = '\0';
269 OPENSSL_free(*pto);
270 *pto = buf->data;
271 OPENSSL_free(buf);
272 return 1;
273
274 err:
275 BUF_MEM_free(buf);
276 return 0;
277 }
278
279 static CONF_VALUE *get_section(const CONF *conf, const char *section) {
280 CONF_VALUE template;
281
282 OPENSSL_memset(&template, 0, sizeof(template));
283 template.section = (char *)section;
284 return lh_CONF_VALUE_retrieve(conf->data, &template);
285 }
286
287 const STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf,
288 const char *section) {
289 const CONF_VALUE *section_value = get_section(conf, section);
290 if (section_value == NULL) {
291 return NULL;
292 }
293 return (STACK_OF(CONF_VALUE) *)section_value->value;
294 }
295
296 const char *NCONF_get_string(const CONF *conf, const char *section,
297 const char *name) {
298 CONF_VALUE template, *value;
299
300 OPENSSL_memset(&template, 0, sizeof(template));
301
302 if (section != NULL) {
303 template.section = (char *)section;
304 template.name = (char *)name;
305 value = lh_CONF_VALUE_retrieve(conf->data, &template);
306 if (value != NULL) {
307 return value->value;
308 }
309 }
310
311 char defaultSectionName[] = "default";
312
313 template.section = defaultSectionName;
314 template.name = (char *)name;
315 value = lh_CONF_VALUE_retrieve(conf->data, &template);
316 if (value != NULL) {
317 return value->value;
318 }
319
320 return NULL;
321 }
322
323 static int add_string(const CONF *conf, CONF_VALUE *section,
324 CONF_VALUE *value) {
325 STACK_OF(CONF_VALUE) *section_stack = (STACK_OF(CONF_VALUE) *)section->value;
326 CONF_VALUE *old_value;
327
328 value->section = OPENSSL_strdup(section->section);
329 if (value->section == NULL) {
330 return 0;
331 }
332
333 if (!sk_CONF_VALUE_push(section_stack, value)) {
334 OPENSSL_free(value->section);
335 value->section = NULL;
336 return 0;
337 }
338
339 if (!lh_CONF_VALUE_insert(conf->data, &old_value, value)) {
340 (void)sk_CONF_VALUE_delete_ptr(section_stack, value);
341 OPENSSL_free(value->section);
342 value->section = NULL;
343 return 0;
344 }
345 if (old_value != NULL) {
346 (void)sk_CONF_VALUE_delete_ptr(section_stack, old_value);
347 value_free(old_value);
348 }
349
350 return 1;
351 }
352
353 static char *eat_ws(CONF *conf, char *p) {
354 while (IS_WS(conf, *p) && !IS_EOF(conf, *p)) {
355 p++;
356 }
357 return p;
358 }
359
360 #define scan_esc(conf, p) (((IS_EOF((conf), (p)[1])) ? ((p) + 1) : ((p) + 2)))
361
362 static char *eat_alpha_numeric(CONF *conf, char *p) {
363 for (;;) {
364 if (IS_ESC(conf, *p)) {
365 p = scan_esc(conf, p);
366 continue;
367 }
368 if (!IS_ALPHA_NUMERIC_PUNCT(conf, *p)) {
369 return p;
370 }
371 p++;
372 }
373 }
374
375 static char *scan_quote(CONF *conf, char *p) {
376 int q = *p;
377
378 p++;
379 while (!IS_EOF(conf, *p) && *p != q) {
380 if (IS_ESC(conf, *p)) {
381 p++;
382 if (IS_EOF(conf, *p)) {
383 return p;
384 }
385 }
386 p++;
387 }
388 if (*p == q) {
389 p++;
390 }
391 return p;
392 }
393
394 static void clear_comments(CONF *conf, char *p) {
395 for (;;) {
396 if (!IS_WS(conf, *p)) {
397 break;
398 }
399 p++;
400 }
401
402 for (;;) {
403 if (IS_COMMENT(conf, *p)) {
404 *p = '\0';
405 return;
406 }
407 if (IS_QUOTE(conf, *p)) {
408 p = scan_quote(conf, p);
409 continue;
410 }
411 if (IS_ESC(conf, *p)) {
412 p = scan_esc(conf, p);
413 continue;
414 }
415 if (IS_EOF(conf, *p)) {
416 return;
417 } else {
418 p++;
419 }
420 }
421 }
422
423 int NCONF_load_bio(CONF *conf, BIO *in, long *out_error_line) {
424 static const size_t CONFBUFSIZE = 512;
425 int bufnum = 0, i, ii;
426 BUF_MEM *buff = NULL;
427 char *s, *p, *end;
428 int again;
429 long eline = 0;
430 char btmp[DECIMAL_SIZE(eline) + 1];
431 CONF_VALUE *v = NULL, *tv;
432 CONF_VALUE *sv = NULL;
433 char *section = NULL, *buf;
434 char *start, *psection, *pname;
435
436 if ((buff = BUF_MEM_new()) == NULL) {
437 OPENSSL_PUT_ERROR(CONF, ERR_R_BUF_LIB);
438 goto err;
439 }
440
441 section = OPENSSL_strdup(kDefaultSectionName);
442 if (section == NULL) {
443 goto err;
444 }
445
446 sv = NCONF_new_section(conf, section);
447 if (sv == NULL) {
448 OPENSSL_PUT_ERROR(CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
449 goto err;
450 }
451
452 bufnum = 0;
453 again = 0;
454 for (;;) {
455 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
456 OPENSSL_PUT_ERROR(CONF, ERR_R_BUF_LIB);
457 goto err;
458 }
459 p = &(buff->data[bufnum]);
460 *p = '\0';
461 BIO_gets(in, p, CONFBUFSIZE - 1);
462 p[CONFBUFSIZE - 1] = '\0';
463 ii = i = strlen(p);
464 if (i == 0 && !again) {
465 break;
466 }
467 again = 0;
468 while (i > 0) {
469 if ((p[i - 1] != '\r') && (p[i - 1] != '\n')) {
470 break;
471 } else {
472 i--;
473 }
474 }
475 // we removed some trailing stuff so there is a new
476 // line on the end.
477 if (ii && i == ii) {
478 again = 1; // long line
479 } else {
480 p[i] = '\0';
481 eline++; // another input line
482 }
483
484 // we now have a line with trailing \r\n removed
485
486 // i is the number of bytes
487 bufnum += i;
488
489 v = NULL;
490 // check for line continuation
491 if (bufnum >= 1) {
492 // If we have bytes and the last char '\\' and
493 // second last char is not '\\'
494 p = &(buff->data[bufnum - 1]);
495 if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
496 bufnum--;
497 again = 1;
498 }
499 }
500 if (again) {
501 continue;
502 }
503 bufnum = 0;
504 buf = buff->data;
505
506 clear_comments(conf, buf);
507 s = eat_ws(conf, buf);
508 if (IS_EOF(conf, *s)) {
509 continue; // blank line
510 }
511 if (*s == '[') {
512 char *ss;
513
514 s++;
515 start = eat_ws(conf, s);
516 ss = start;
517 again:
518 end = eat_alpha_numeric(conf, ss);
519 p = eat_ws(conf, end);
520 if (*p != ']') {
521 if (*p != '\0' && ss != p) {
522 ss = p;
523 goto again;
524 }
525 OPENSSL_PUT_ERROR(CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
526 goto err;
527 }
528 *end = '\0';
529 if (!str_copy(conf, NULL, &section, start)) {
530 goto err;
531 }
532 if ((sv = get_section(conf, section)) == NULL) {
533 sv = NCONF_new_section(conf, section);
534 }
535 if (sv == NULL) {
536 OPENSSL_PUT_ERROR(CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
537 goto err;
538 }
539 continue;
540 } else {
541 pname = s;
542 psection = NULL;
543 end = eat_alpha_numeric(conf, s);
544 if ((end[0] == ':') && (end[1] == ':')) {
545 *end = '\0';
546 end += 2;
547 psection = pname;
548 pname = end;
549 end = eat_alpha_numeric(conf, end);
550 }
551 p = eat_ws(conf, end);
552 if (*p != '=') {
553 OPENSSL_PUT_ERROR(CONF, CONF_R_MISSING_EQUAL_SIGN);
554 goto err;
555 }
556 *end = '\0';
557 p++;
558 start = eat_ws(conf, p);
559 while (!IS_EOF(conf, *p)) {
560 p++;
561 }
562 p--;
563 while ((p != start) && (IS_WS(conf, *p))) {
564 p--;
565 }
566 p++;
567 *p = '\0';
568
569 if (!(v = CONF_VALUE_new())) {
570 goto err;
571 }
572 if (psection == NULL) {
573 psection = section;
574 }
575 v->name = OPENSSL_strdup(pname);
576 if (v->name == NULL) {
577 goto err;
578 }
579 if (!str_copy(conf, psection, &(v->value), start)) {
580 goto err;
581 }
582
583 if (strcmp(psection, section) != 0) {
584 if ((tv = get_section(conf, psection)) == NULL) {
585 tv = NCONF_new_section(conf, psection);
586 }
587 if (tv == NULL) {
588 OPENSSL_PUT_ERROR(CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
589 goto err;
590 }
591 } else {
592 tv = sv;
593 }
594 if (add_string(conf, tv, v) == 0) {
595 goto err;
596 }
597 v = NULL;
598 }
599 }
600 BUF_MEM_free(buff);
601 OPENSSL_free(section);
602 return 1;
603
604 err:
605 BUF_MEM_free(buff);
606 OPENSSL_free(section);
607 if (out_error_line != NULL) {
608 *out_error_line = eline;
609 }
610 snprintf(btmp, sizeof btmp, "%ld", eline);
611 ERR_add_error_data(2, "line ", btmp);
612
613 if (v != NULL) {
614 OPENSSL_free(v->name);
615 OPENSSL_free(v->value);
616 OPENSSL_free(v);
617 }
618 return 0;
619 }
620
621 int NCONF_load(CONF *conf, const char *filename, long *out_error_line) {
622 BIO *in = BIO_new_file(filename, "rb");
623 int ret;
624
625 if (in == NULL) {
626 OPENSSL_PUT_ERROR(CONF, ERR_R_SYS_LIB);
627 return 0;
628 }
629
630 ret = NCONF_load_bio(conf, in, out_error_line);
631 BIO_free(in);
632
633 return ret;
634 }
635
636 int CONF_parse_list(const char *list, char sep, int remove_whitespace,
637 int (*list_cb)(const char *elem, size_t len, void *usr),
638 void *arg) {
639 int ret;
640 const char *lstart, *tmpend, *p;
641
642 if (list == NULL) {
643 OPENSSL_PUT_ERROR(CONF, CONF_R_LIST_CANNOT_BE_NULL);
644 return 0;
645 }
646
647 lstart = list;
648 for (;;) {
649 if (remove_whitespace) {
650 while (*lstart && OPENSSL_isspace((unsigned char)*lstart)) {
651 lstart++;
652 }
653 }
654 p = strchr(lstart, sep);
655 if (p == lstart || !*lstart) {
656 ret = list_cb(NULL, 0, arg);
657 } else {
658 if (p) {
659 tmpend = p - 1;
660 } else {
661 tmpend = lstart + strlen(lstart) - 1;
662 }
663 if (remove_whitespace) {
664 while (OPENSSL_isspace((unsigned char)*tmpend)) {
665 tmpend--;
666 }
667 }
668 ret = list_cb(lstart, tmpend - lstart + 1, arg);
669 }
670 if (ret <= 0) {
671 return ret;
672 }
673 if (p == NULL) {
674 return 1;
675 }
676 lstart = p + 1;
677 }
678 }
679
680 int CONF_modules_load_file(const char *filename, const char *appname,
681 unsigned long flags) {
682 return 1;
683 }
684
685 char *CONF_get1_default_config_file(void) {
686 return OPENSSL_strdup("No support for Config files in AWS-LC.");
687 }
688
689 void CONF_modules_free(void) {}
690
691 void CONF_modules_unload(int all) {}
692
693 void CONF_modules_finish(void) {}
694
695 void OPENSSL_config(const char *config_name) {}
696
697 void OPENSSL_no_config(void) {}