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