xref: /openbsd-src/lib/libcrypto/conf/conf_def.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /* $OpenBSD: conf_def.c,v 1.28 2014/07/11 15:38:03 miod Exp $ */
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 
64 #include <openssl/buffer.h>
65 #include <openssl/conf.h>
66 #include <openssl/conf_api.h>
67 #include <openssl/err.h>
68 #include <openssl/lhash.h>
69 #include <openssl/stack.h>
70 
71 #include "conf_def.h"
72 
73 static char *eat_ws(CONF *conf, char *p);
74 static char *eat_alpha_numeric(CONF *conf, char *p);
75 static void clear_comments(CONF *conf, char *p);
76 static int str_copy(CONF *conf, char *section, char **to, char *from);
77 static char *scan_quote(CONF *conf, char *p);
78 static char *scan_dquote(CONF *conf, char *p);
79 #define scan_esc(conf,p)	(((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
80 
81 static CONF *def_create(CONF_METHOD *meth);
82 static int def_init_default(CONF *conf);
83 static int def_init_WIN32(CONF *conf);
84 static int def_destroy(CONF *conf);
85 static int def_destroy_data(CONF *conf);
86 static int def_load(CONF *conf, const char *name, long *eline);
87 static int def_load_bio(CONF *conf, BIO *bp, long *eline);
88 static int def_dump(const CONF *conf, BIO *bp);
89 static int def_is_number(const CONF *conf, char c);
90 static int def_to_int(const CONF *conf, char c);
91 
92 static CONF_METHOD default_method = {
93 	.name = "OpenSSL default",
94 	.create = def_create,
95 	.init = def_init_default,
96 	.destroy = def_destroy,
97 	.destroy_data = def_destroy_data,
98 	.load_bio = def_load_bio,
99 	.dump = def_dump,
100 	.is_number = def_is_number,
101 	.to_int = def_to_int,
102 	.load = def_load
103 };
104 
105 static CONF_METHOD WIN32_method = {
106 	"WIN32",
107 	def_create,
108 	def_init_WIN32,
109 	def_destroy,
110 	def_destroy_data,
111 	def_load_bio,
112 	def_dump,
113 	def_is_number,
114 	def_to_int,
115 	def_load
116 };
117 
118 CONF_METHOD *
119 NCONF_default(void)
120 {
121 	return &default_method;
122 }
123 
124 CONF_METHOD *
125 NCONF_WIN32(void)
126 {
127 	return &WIN32_method;
128 }
129 
130 static CONF *
131 def_create(CONF_METHOD *meth)
132 {
133 	CONF *ret;
134 
135 	ret = malloc(sizeof(CONF) + sizeof(unsigned short *));
136 	if (ret)
137 		if (meth->init(ret) == 0) {
138 			free(ret);
139 			ret = NULL;
140 		}
141 	return ret;
142 }
143 
144 static int
145 def_init_default(CONF *conf)
146 {
147 	if (conf == NULL)
148 		return 0;
149 
150 	conf->meth = &default_method;
151 	conf->meth_data = CONF_type_default;
152 	conf->data = NULL;
153 
154 	return 1;
155 }
156 
157 static int
158 def_init_WIN32(CONF *conf)
159 {
160 	if (conf == NULL)
161 		return 0;
162 
163 	conf->meth = &WIN32_method;
164 	conf->meth_data = (void *)CONF_type_win32;
165 	conf->data = NULL;
166 
167 	return 1;
168 }
169 
170 static int
171 def_destroy(CONF *conf)
172 {
173 	if (def_destroy_data(conf)) {
174 		free(conf);
175 		return 1;
176 	}
177 	return 0;
178 }
179 
180 static int
181 def_destroy_data(CONF *conf)
182 {
183 	if (conf == NULL)
184 		return 0;
185 	_CONF_free_data(conf);
186 	return 1;
187 }
188 
189 static int
190 def_load(CONF *conf, const char *name, long *line)
191 {
192 	int ret;
193 	BIO *in = NULL;
194 
195 	in = BIO_new_file(name, "rb");
196 	if (in == NULL) {
197 		if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
198 			CONFerr(CONF_F_DEF_LOAD, CONF_R_NO_SUCH_FILE);
199 		else
200 			CONFerr(CONF_F_DEF_LOAD, ERR_R_SYS_LIB);
201 		return 0;
202 	}
203 
204 	ret = def_load_bio(conf, in, line);
205 	BIO_free(in);
206 
207 	return ret;
208 }
209 
210 static int
211 def_load_bio(CONF *conf, BIO *in, long *line)
212 {
213 /* The macro BUFSIZE conflicts with a system macro in VxWorks */
214 #define CONFBUFSIZE	512
215 	int bufnum = 0, i, ii;
216 	BUF_MEM *buff = NULL;
217 	char *s, *p, *end;
218 	int again;
219 	long eline = 0;
220 	CONF_VALUE *v = NULL, *tv;
221 	CONF_VALUE *sv = NULL;
222 	char *section = NULL, *buf;
223 	char *start, *psection, *pname;
224 	void *h = (void *)(conf->data);
225 
226 	if ((buff = BUF_MEM_new()) == NULL) {
227 		CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
228 		goto err;
229 	}
230 
231 	section = malloc(10);
232 	if (section == NULL) {
233 		CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
234 		goto err;
235 	}
236 	strlcpy(section, "default",10);
237 
238 	if (_CONF_new_data(conf) == 0) {
239 		CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
240 		goto err;
241 	}
242 
243 	sv = _CONF_new_section(conf, section);
244 	if (sv == NULL) {
245 		CONFerr(CONF_F_DEF_LOAD_BIO,
246 		    CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
247 		goto err;
248 	}
249 
250 	bufnum = 0;
251 	again = 0;
252 	for (;;) {
253 		if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
254 			CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
255 			goto err;
256 		}
257 		p = &(buff->data[bufnum]);
258 		*p = '\0';
259 		BIO_gets(in, p, CONFBUFSIZE - 1);
260 		p[CONFBUFSIZE - 1] = '\0';
261 		ii = i = strlen(p);
262 		if (i == 0 && !again)
263 			break;
264 		again = 0;
265 		while (i > 0) {
266 			if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
267 				break;
268 			else
269 				i--;
270 		}
271 		/* we removed some trailing stuff so there is a new
272 		 * line on the end. */
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 			/* If we have bytes and the last char '\\' and
289 			 * second last char is not '\\' */
290 			p = &(buff->data[bufnum - 1]);
291 			if (IS_ESC(conf, p[0]) &&
292 			    ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
293 				bufnum--;
294 				again = 1;
295 			}
296 		}
297 		if (again)
298 			continue;
299 		bufnum = 0;
300 		buf = buff->data;
301 
302 		clear_comments(conf, buf);
303 		s = eat_ws(conf, buf);
304 		if (IS_EOF(conf, *s))
305 			continue; /* blank line */
306 		if (*s == '[') {
307 			char *ss;
308 
309 			s++;
310 			start = eat_ws(conf, s);
311 			ss = start;
312 again:
313 			end = eat_alpha_numeric(conf, ss);
314 			p = eat_ws(conf, end);
315 			if (*p != ']') {
316 				if (*p != '\0' && ss != p) {
317 					ss = p;
318 					goto again;
319 				}
320 				CONFerr(CONF_F_DEF_LOAD_BIO,
321 				    CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
322 				goto err;
323 			}
324 			*end = '\0';
325 			if (!str_copy(conf, NULL, &section, start))
326 				goto err;
327 			if ((sv = _CONF_get_section(conf, section)) == NULL)
328 				sv = _CONF_new_section(conf, section);
329 			if (sv == NULL) {
330 				CONFerr(CONF_F_DEF_LOAD_BIO,
331 				    CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
332 				goto err;
333 			}
334 			continue;
335 		} else {
336 			pname = s;
337 			psection = NULL;
338 			end = eat_alpha_numeric(conf, s);
339 			if ((end[0] == ':') && (end[1] == ':')) {
340 				*end = '\0';
341 				end += 2;
342 				psection = pname;
343 				pname = end;
344 				end = eat_alpha_numeric(conf, end);
345 			}
346 			p = eat_ws(conf, end);
347 			if (*p != '=') {
348 				CONFerr(CONF_F_DEF_LOAD_BIO,
349 				    CONF_R_MISSING_EQUAL_SIGN);
350 				goto err;
351 			}
352 			*end = '\0';
353 			p++;
354 			start = eat_ws(conf, p);
355 			while (!IS_EOF(conf, *p))
356 				p++;
357 			p--;
358 			while ((p != start) && (IS_WS(conf, *p)))
359 				p--;
360 			p++;
361 			*p = '\0';
362 
363 			if (!(v = malloc(sizeof(CONF_VALUE)))) {
364 				CONFerr(CONF_F_DEF_LOAD_BIO,
365 				    ERR_R_MALLOC_FAILURE);
366 				goto err;
367 			}
368 			if (psection == NULL)
369 				psection = section;
370 			v->name = strdup(pname);
371 			v->value = NULL;
372 			if (v->name == NULL) {
373 				CONFerr(CONF_F_DEF_LOAD_BIO,
374 				    ERR_R_MALLOC_FAILURE);
375 				goto err;
376 			}
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 			} else
390 				tv = sv;
391 #if 1
392 			if (_CONF_add_string(conf, tv, v) == 0) {
393 				CONFerr(CONF_F_DEF_LOAD_BIO,
394 				    ERR_R_MALLOC_FAILURE);
395 				goto err;
396 			}
397 #else
398 			v->section = tv->section;
399 			if (!sk_CONF_VALUE_push(ts, v)) {
400 				CONFerr(CONF_F_DEF_LOAD_BIO,
401 				    ERR_R_MALLOC_FAILURE);
402 				goto err;
403 			}
404 			vv = (CONF_VALUE *)lh_insert(conf->data, v);
405 			if (vv != NULL) {
406 				sk_CONF_VALUE_delete_ptr(ts, vv);
407 				free(vv->name);
408 				free(vv->value);
409 				free(vv);
410 			}
411 #endif
412 			v = NULL;
413 		}
414 	}
415 	if (buff != NULL)
416 		BUF_MEM_free(buff);
417 	free(section);
418 	return (1);
419 
420 err:
421 	if (buff != NULL)
422 		BUF_MEM_free(buff);
423 	free(section);
424 	if (line != NULL)
425 		*line = eline;
426 	ERR_asprintf_error_data("line %ld", eline);
427 	if ((h != conf->data) && (conf->data != NULL)) {
428 		CONF_free(conf->data);
429 		conf->data = NULL;
430 	}
431 	if (v != NULL) {
432 		free(v->name);
433 		free(v->value);
434 		free(v);
435 	}
436 	return (0);
437 }
438 
439 static void
440 clear_comments(CONF *conf, char *p)
441 {
442 	for (;;) {
443 		if (IS_FCOMMENT(conf, *p)) {
444 			*p = '\0';
445 			return;
446 		}
447 		if (!IS_WS(conf, *p)) {
448 			break;
449 		}
450 		p++;
451 	}
452 
453 	for (;;) {
454 		if (IS_COMMENT(conf, *p)) {
455 			*p = '\0';
456 			return;
457 		}
458 		if (IS_DQUOTE(conf, *p)) {
459 			p = scan_dquote(conf, p);
460 			continue;
461 		}
462 		if (IS_QUOTE(conf, *p)) {
463 			p = scan_quote(conf, p);
464 			continue;
465 		}
466 		if (IS_ESC(conf, *p)) {
467 			p = scan_esc(conf, p);
468 			continue;
469 		}
470 		if (IS_EOF(conf, *p))
471 			return;
472 		else
473 			p++;
474 	}
475 }
476 
477 static int
478 str_copy(CONF *conf, char *section, char **pto, char *from)
479 {
480 	int q, r,rr = 0, to = 0, len = 0;
481 	char *s, *e, *rp, *p, *rrp, *np, *cp, v;
482 	BUF_MEM *buf;
483 
484 	if ((buf = BUF_MEM_new()) == NULL)
485 		return (0);
486 
487 	len = strlen(from) + 1;
488 	if (!BUF_MEM_grow(buf, len))
489 		goto err;
490 
491 	for (;;) {
492 		if (IS_QUOTE(conf, *from)) {
493 			q = *from;
494 			from++;
495 			while (!IS_EOF(conf, *from) && (*from != q)) {
496 				if (IS_ESC(conf, *from)) {
497 					from++;
498 					if (IS_EOF(conf, *from))
499 						break;
500 				}
501 				buf->data[to++] = *(from++);
502 			}
503 			if (*from == q)
504 				from++;
505 		} else if (IS_DQUOTE(conf, *from)) {
506 			q = *from;
507 			from++;
508 			while (!IS_EOF(conf, *from)) {
509 				if (*from == q) {
510 					if (*(from + 1) == q) {
511 						from++;
512 					} else {
513 						break;
514 					}
515 				}
516 				buf->data[to++] = *(from++);
517 			}
518 			if (*from == q)
519 				from++;
520 		} else if (IS_ESC(conf, *from)) {
521 			from++;
522 			v = *(from++);
523 			if (IS_EOF(conf, v))
524 				break;
525 			else if (v == 'r')
526 				v = '\r';
527 			else if (v == 'n')
528 				v = '\n';
529 			else if (v == 'b')
530 				v = '\b';
531 			else if (v == 't')
532 				v = '\t';
533 			buf->data[to++] = v;
534 		} else if (IS_EOF(conf, *from))
535 			break;
536 		else if (*from == '$') {
537 			/* try to expand it */
538 			rrp = NULL;
539 			s = &(from[1]);
540 			if (*s == '{')
541 				q = '}';
542 			else if (*s == '(')
543 				q = ')';
544 			else
545 				q = 0;
546 
547 			if (q)
548 				s++;
549 			cp = section;
550 			e = np = s;
551 			while (IS_ALPHA_NUMERIC(conf, *e))
552 				e++;
553 			if ((e[0] == ':') && (e[1] == ':')) {
554 				cp = np;
555 				rrp = e;
556 				rr = *e;
557 				*rrp = '\0';
558 				e += 2;
559 				np = e;
560 				while (IS_ALPHA_NUMERIC(conf, *e))
561 					e++;
562 			}
563 			r = *e;
564 			*e = '\0';
565 			rp = e;
566 			if (q) {
567 				if (r != q) {
568 					CONFerr(CONF_F_STR_COPY,
569 					    CONF_R_NO_CLOSE_BRACE);
570 					goto err;
571 				}
572 				e++;
573 			}
574 			/* So at this point we have
575 			 * np which is the start of the name string which is
576 			 *   '\0' terminated.
577 			 * cp which is the start of the section string which is
578 			 *   '\0' terminated.
579 			 * e is the 'next point after'.
580 			 * r and rr are the chars replaced by the '\0'
581 			 * rp and rrp is where 'r' and 'rr' came from.
582 			 */
583 			p = _CONF_get_string(conf, cp, np);
584 			if (rrp != NULL)
585 				*rrp = rr;
586 			*rp = r;
587 			if (p == NULL) {
588 				CONFerr(CONF_F_STR_COPY,
589 				    CONF_R_VARIABLE_HAS_NO_VALUE);
590 				goto err;
591 			}
592 			BUF_MEM_grow_clean(buf,
593 			    (strlen(p) + buf->length - (e - from)));
594 			while (*p)
595 				buf->data[to++] = *(p++);
596 
597 			/* Since we change the pointer 'from', we also have
598 			   to change the perceived length of the string it
599 			   points at.  /RL */
600 			len -= e - from;
601 			from = e;
602 
603 			/* In case there were no braces or parenthesis around
604 			   the variable reference, we have to put back the
605 			   character that was replaced with a '\0'.  /RL */
606 			*rp = r;
607 		} else
608 			buf->data[to++] = *(from++);
609 	}
610 	buf->data[to]='\0';
611 	free(*pto);
612 	*pto = buf->data;
613 	free(buf);
614 	return (1);
615 
616 err:
617 	if (buf != NULL)
618 		BUF_MEM_free(buf);
619 	return (0);
620 }
621 
622 static char *
623 eat_ws(CONF *conf, char *p)
624 {
625 	while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
626 		p++;
627 	return (p);
628 }
629 
630 static char *
631 eat_alpha_numeric(CONF *conf, char *p)
632 {
633 	for (;;) {
634 		if (IS_ESC(conf, *p)) {
635 			p = scan_esc(conf, p);
636 			continue;
637 		}
638 		if (!IS_ALPHA_NUMERIC_PUNCT(conf, *p))
639 			return (p);
640 		p++;
641 	}
642 }
643 
644 static char *
645 scan_quote(CONF *conf, char *p)
646 {
647 	int q = *p;
648 
649 	p++;
650 	while (!(IS_EOF(conf, *p)) && (*p != q)) {
651 		if (IS_ESC(conf, *p)) {
652 			p++;
653 			if (IS_EOF(conf, *p))
654 				return (p);
655 		}
656 		p++;
657 	}
658 	if (*p == q)
659 		p++;
660 	return (p);
661 }
662 
663 
664 static char *
665 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
686 dump_value_doall_arg(CONF_VALUE *a, BIO *out)
687 {
688 	if (a->name)
689 		BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
690 	else
691 		BIO_printf(out, "[[%s]]\n", a->section);
692 }
693 
694 static IMPLEMENT_LHASH_DOALL_ARG_FN(dump_value, CONF_VALUE, BIO)
695 
696 static int
697 def_dump(const CONF *conf, BIO *out)
698 {
699 	lh_CONF_VALUE_doall_arg(conf->data, LHASH_DOALL_ARG_FN(dump_value),
700 	    BIO, out);
701 	return 1;
702 }
703 
704 static int
705 def_is_number(const CONF *conf, char c)
706 {
707 	return IS_NUMBER(conf, c);
708 }
709 
710 static int
711 def_to_int(const CONF *conf, char c)
712 {
713 	return c - '0';
714 }
715