xref: /onnv-gate/usr/src/common/openssl/crypto/conf/conf_def.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/conf/conf.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /* Part of the code in here was originally in conf.c, which is now removed */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #include <stdio.h>
620Sstevel@tonic-gate #include <string.h>
63*2139Sjp161948 #include "cryptlib.h"
640Sstevel@tonic-gate #include <openssl/stack.h>
650Sstevel@tonic-gate #include <openssl/lhash.h>
660Sstevel@tonic-gate #include <openssl/conf.h>
670Sstevel@tonic-gate #include <openssl/conf_api.h>
680Sstevel@tonic-gate #include "conf_def.h"
690Sstevel@tonic-gate #include <openssl/buffer.h>
700Sstevel@tonic-gate #include <openssl/err.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate static char *eat_ws(CONF *conf, char *p);
730Sstevel@tonic-gate static char *eat_alpha_numeric(CONF *conf, char *p);
740Sstevel@tonic-gate static void clear_comments(CONF *conf, char *p);
750Sstevel@tonic-gate static int str_copy(CONF *conf,char *section,char **to, char *from);
760Sstevel@tonic-gate static char *scan_quote(CONF *conf, char *p);
770Sstevel@tonic-gate static char *scan_dquote(CONF *conf, char *p);
780Sstevel@tonic-gate #define scan_esc(conf,p)	(((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
790Sstevel@tonic-gate 
800Sstevel@tonic-gate static CONF *def_create(CONF_METHOD *meth);
810Sstevel@tonic-gate static int def_init_default(CONF *conf);
820Sstevel@tonic-gate static int def_init_WIN32(CONF *conf);
830Sstevel@tonic-gate static int def_destroy(CONF *conf);
840Sstevel@tonic-gate static int def_destroy_data(CONF *conf);
850Sstevel@tonic-gate static int def_load(CONF *conf, const char *name, long *eline);
860Sstevel@tonic-gate static int def_load_bio(CONF *conf, BIO *bp, long *eline);
870Sstevel@tonic-gate static int def_dump(const CONF *conf, BIO *bp);
880Sstevel@tonic-gate static int def_is_number(const CONF *conf, char c);
890Sstevel@tonic-gate static int def_to_int(const CONF *conf, char c);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate const char *CONF_def_version="CONF_def" OPENSSL_VERSION_PTEXT;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate static CONF_METHOD default_method = {
940Sstevel@tonic-gate 	"OpenSSL default",
950Sstevel@tonic-gate 	def_create,
960Sstevel@tonic-gate 	def_init_default,
970Sstevel@tonic-gate 	def_destroy,
980Sstevel@tonic-gate 	def_destroy_data,
990Sstevel@tonic-gate 	def_load_bio,
1000Sstevel@tonic-gate 	def_dump,
1010Sstevel@tonic-gate 	def_is_number,
1020Sstevel@tonic-gate 	def_to_int,
1030Sstevel@tonic-gate 	def_load
1040Sstevel@tonic-gate 	};
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate static CONF_METHOD WIN32_method = {
1070Sstevel@tonic-gate 	"WIN32",
1080Sstevel@tonic-gate 	def_create,
1090Sstevel@tonic-gate 	def_init_WIN32,
1100Sstevel@tonic-gate 	def_destroy,
1110Sstevel@tonic-gate 	def_destroy_data,
1120Sstevel@tonic-gate 	def_load_bio,
1130Sstevel@tonic-gate 	def_dump,
1140Sstevel@tonic-gate 	def_is_number,
1150Sstevel@tonic-gate 	def_to_int,
1160Sstevel@tonic-gate 	def_load
1170Sstevel@tonic-gate 	};
1180Sstevel@tonic-gate 
NCONF_default()1190Sstevel@tonic-gate CONF_METHOD *NCONF_default()
1200Sstevel@tonic-gate 	{
1210Sstevel@tonic-gate 	return &default_method;
1220Sstevel@tonic-gate 	}
NCONF_WIN32()1230Sstevel@tonic-gate CONF_METHOD *NCONF_WIN32()
1240Sstevel@tonic-gate 	{
1250Sstevel@tonic-gate 	return &WIN32_method;
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 
def_create(CONF_METHOD * meth)1280Sstevel@tonic-gate static CONF *def_create(CONF_METHOD *meth)
1290Sstevel@tonic-gate 	{
1300Sstevel@tonic-gate 	CONF *ret;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	ret = (CONF *)OPENSSL_malloc(sizeof(CONF) + sizeof(unsigned short *));
1330Sstevel@tonic-gate 	if (ret)
1340Sstevel@tonic-gate 		if (meth->init(ret) == 0)
1350Sstevel@tonic-gate 			{
1360Sstevel@tonic-gate 			OPENSSL_free(ret);
1370Sstevel@tonic-gate 			ret = NULL;
1380Sstevel@tonic-gate 			}
1390Sstevel@tonic-gate 	return ret;
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 
def_init_default(CONF * conf)1420Sstevel@tonic-gate static int def_init_default(CONF *conf)
1430Sstevel@tonic-gate 	{
1440Sstevel@tonic-gate 	if (conf == NULL)
1450Sstevel@tonic-gate 		return 0;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	conf->meth = &default_method;
1480Sstevel@tonic-gate 	conf->meth_data = (void *)CONF_type_default;
1490Sstevel@tonic-gate 	conf->data = NULL;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	return 1;
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 
def_init_WIN32(CONF * conf)1540Sstevel@tonic-gate static int def_init_WIN32(CONF *conf)
1550Sstevel@tonic-gate 	{
1560Sstevel@tonic-gate 	if (conf == NULL)
1570Sstevel@tonic-gate 		return 0;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	conf->meth = &WIN32_method;
1600Sstevel@tonic-gate 	conf->meth_data = (void *)CONF_type_win32;
1610Sstevel@tonic-gate 	conf->data = NULL;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	return 1;
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 
def_destroy(CONF * conf)1660Sstevel@tonic-gate static int def_destroy(CONF *conf)
1670Sstevel@tonic-gate 	{
1680Sstevel@tonic-gate 	if (def_destroy_data(conf))
1690Sstevel@tonic-gate 		{
1700Sstevel@tonic-gate 		OPENSSL_free(conf);
1710Sstevel@tonic-gate 		return 1;
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate 	return 0;
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 
def_destroy_data(CONF * conf)1760Sstevel@tonic-gate static int def_destroy_data(CONF *conf)
1770Sstevel@tonic-gate 	{
1780Sstevel@tonic-gate 	if (conf == NULL)
1790Sstevel@tonic-gate 		return 0;
1800Sstevel@tonic-gate 	_CONF_free_data(conf);
1810Sstevel@tonic-gate 	return 1;
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate 
def_load(CONF * conf,const char * name,long * line)1840Sstevel@tonic-gate static int def_load(CONF *conf, const char *name, long *line)
1850Sstevel@tonic-gate 	{
1860Sstevel@tonic-gate 	int ret;
1870Sstevel@tonic-gate 	BIO *in=NULL;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
1900Sstevel@tonic-gate 	in=BIO_new_file(name, "r");
1910Sstevel@tonic-gate #else
1920Sstevel@tonic-gate 	in=BIO_new_file(name, "rb");
1930Sstevel@tonic-gate #endif
1940Sstevel@tonic-gate 	if (in == NULL)
1950Sstevel@tonic-gate 		{
1960Sstevel@tonic-gate 		if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
197*2139Sjp161948 			CONFerr(CONF_F_DEF_LOAD,CONF_R_NO_SUCH_FILE);
1980Sstevel@tonic-gate 		else
199*2139Sjp161948 			CONFerr(CONF_F_DEF_LOAD,ERR_R_SYS_LIB);
2000Sstevel@tonic-gate 		return 0;
2010Sstevel@tonic-gate 		}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	ret = def_load_bio(conf, in, line);
2040Sstevel@tonic-gate 	BIO_free(in);
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	return ret;
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 
def_load_bio(CONF * conf,BIO * in,long * line)2090Sstevel@tonic-gate static int def_load_bio(CONF *conf, BIO *in, long *line)
2100Sstevel@tonic-gate 	{
2110Sstevel@tonic-gate /* The macro BUFSIZE conflicts with a system macro in VxWorks */
2120Sstevel@tonic-gate #define CONFBUFSIZE	512
2130Sstevel@tonic-gate 	int bufnum=0,i,ii;
2140Sstevel@tonic-gate 	BUF_MEM *buff=NULL;
2150Sstevel@tonic-gate 	char *s,*p,*end;
2160Sstevel@tonic-gate 	int again,n;
2170Sstevel@tonic-gate 	long eline=0;
2180Sstevel@tonic-gate 	char btmp[DECIMAL_SIZE(eline)+1];
2190Sstevel@tonic-gate 	CONF_VALUE *v=NULL,*tv;
2200Sstevel@tonic-gate 	CONF_VALUE *sv=NULL;
2210Sstevel@tonic-gate 	char *section=NULL,*buf;
2220Sstevel@tonic-gate 	STACK_OF(CONF_VALUE) *section_sk=NULL,*ts;
2230Sstevel@tonic-gate 	char *start,*psection,*pname;
2240Sstevel@tonic-gate 	void *h = (void *)(conf->data);
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	if ((buff=BUF_MEM_new()) == NULL)
2270Sstevel@tonic-gate 		{
228*2139Sjp161948 		CONFerr(CONF_F_DEF_LOAD_BIO,ERR_R_BUF_LIB);
2290Sstevel@tonic-gate 		goto err;
2300Sstevel@tonic-gate 		}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	section=(char *)OPENSSL_malloc(10);
2330Sstevel@tonic-gate 	if (section == NULL)
2340Sstevel@tonic-gate 		{
235*2139Sjp161948 		CONFerr(CONF_F_DEF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
2360Sstevel@tonic-gate 		goto err;
2370Sstevel@tonic-gate 		}
2380Sstevel@tonic-gate 	BUF_strlcpy(section,"default",10);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	if (_CONF_new_data(conf) == 0)
2410Sstevel@tonic-gate 		{
242*2139Sjp161948 		CONFerr(CONF_F_DEF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
2430Sstevel@tonic-gate 		goto err;
2440Sstevel@tonic-gate 		}
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	sv=_CONF_new_section(conf,section);
2470Sstevel@tonic-gate 	if (sv == NULL)
2480Sstevel@tonic-gate 		{
249*2139Sjp161948 		CONFerr(CONF_F_DEF_LOAD_BIO,
2500Sstevel@tonic-gate 					CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
2510Sstevel@tonic-gate 		goto err;
2520Sstevel@tonic-gate 		}
2530Sstevel@tonic-gate 	section_sk=(STACK_OF(CONF_VALUE) *)sv->value;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	bufnum=0;
2560Sstevel@tonic-gate 	again=0;
2570Sstevel@tonic-gate 	for (;;)
2580Sstevel@tonic-gate 		{
2590Sstevel@tonic-gate 		if (!BUF_MEM_grow(buff,bufnum+CONFBUFSIZE))
2600Sstevel@tonic-gate 			{
261*2139Sjp161948 			CONFerr(CONF_F_DEF_LOAD_BIO,ERR_R_BUF_LIB);
2620Sstevel@tonic-gate 			goto err;
2630Sstevel@tonic-gate 			}
2640Sstevel@tonic-gate 		p= &(buff->data[bufnum]);
2650Sstevel@tonic-gate 		*p='\0';
2660Sstevel@tonic-gate 		BIO_gets(in, p, CONFBUFSIZE-1);
2670Sstevel@tonic-gate 		p[CONFBUFSIZE-1]='\0';
2680Sstevel@tonic-gate 		ii=i=strlen(p);
2690Sstevel@tonic-gate 		if (i == 0 && !again) break;
2700Sstevel@tonic-gate 		again=0;
2710Sstevel@tonic-gate 		while (i > 0)
2720Sstevel@tonic-gate 			{
2730Sstevel@tonic-gate 			if ((p[i-1] != '\r') && (p[i-1] != '\n'))
2740Sstevel@tonic-gate 				break;
2750Sstevel@tonic-gate 			else
2760Sstevel@tonic-gate 				i--;
2770Sstevel@tonic-gate 			}
2780Sstevel@tonic-gate 		/* we removed some trailing stuff so there is a new
2790Sstevel@tonic-gate 		 * line on the end. */
2800Sstevel@tonic-gate 		if (ii && i == ii)
2810Sstevel@tonic-gate 			again=1; /* long line */
2820Sstevel@tonic-gate 		else
2830Sstevel@tonic-gate 			{
2840Sstevel@tonic-gate 			p[i]='\0';
2850Sstevel@tonic-gate 			eline++; /* another input line */
2860Sstevel@tonic-gate 			}
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 		/* we now have a line with trailing \r\n removed */
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 		/* i is the number of bytes */
2910Sstevel@tonic-gate 		bufnum+=i;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		v=NULL;
2940Sstevel@tonic-gate 		/* check for line continuation */
2950Sstevel@tonic-gate 		if (bufnum >= 1)
2960Sstevel@tonic-gate 			{
2970Sstevel@tonic-gate 			/* If we have bytes and the last char '\\' and
2980Sstevel@tonic-gate 			 * second last char is not '\\' */
2990Sstevel@tonic-gate 			p= &(buff->data[bufnum-1]);
3000Sstevel@tonic-gate 			if (IS_ESC(conf,p[0]) &&
3010Sstevel@tonic-gate 				((bufnum <= 1) || !IS_ESC(conf,p[-1])))
3020Sstevel@tonic-gate 				{
3030Sstevel@tonic-gate 				bufnum--;
3040Sstevel@tonic-gate 				again=1;
3050Sstevel@tonic-gate 				}
3060Sstevel@tonic-gate 			}
3070Sstevel@tonic-gate 		if (again) continue;
3080Sstevel@tonic-gate 		bufnum=0;
3090Sstevel@tonic-gate 		buf=buff->data;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 		clear_comments(conf, buf);
3120Sstevel@tonic-gate 		n=strlen(buf);
3130Sstevel@tonic-gate 		s=eat_ws(conf, buf);
3140Sstevel@tonic-gate 		if (IS_EOF(conf,*s)) continue; /* blank line */
3150Sstevel@tonic-gate 		if (*s == '[')
3160Sstevel@tonic-gate 			{
3170Sstevel@tonic-gate 			char *ss;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 			s++;
3200Sstevel@tonic-gate 			start=eat_ws(conf, s);
3210Sstevel@tonic-gate 			ss=start;
3220Sstevel@tonic-gate again:
3230Sstevel@tonic-gate 			end=eat_alpha_numeric(conf, ss);
3240Sstevel@tonic-gate 			p=eat_ws(conf, end);
3250Sstevel@tonic-gate 			if (*p != ']')
3260Sstevel@tonic-gate 				{
3270Sstevel@tonic-gate 				if (*p != '\0')
3280Sstevel@tonic-gate 					{
3290Sstevel@tonic-gate 					ss=p;
3300Sstevel@tonic-gate 					goto again;
3310Sstevel@tonic-gate 					}
332*2139Sjp161948 				CONFerr(CONF_F_DEF_LOAD_BIO,
3330Sstevel@tonic-gate 					CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
3340Sstevel@tonic-gate 				goto err;
3350Sstevel@tonic-gate 				}
3360Sstevel@tonic-gate 			*end='\0';
3370Sstevel@tonic-gate 			if (!str_copy(conf,NULL,&section,start)) goto err;
3380Sstevel@tonic-gate 			if ((sv=_CONF_get_section(conf,section)) == NULL)
3390Sstevel@tonic-gate 				sv=_CONF_new_section(conf,section);
3400Sstevel@tonic-gate 			if (sv == NULL)
3410Sstevel@tonic-gate 				{
342*2139Sjp161948 				CONFerr(CONF_F_DEF_LOAD_BIO,
3430Sstevel@tonic-gate 					CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
3440Sstevel@tonic-gate 				goto err;
3450Sstevel@tonic-gate 				}
3460Sstevel@tonic-gate 			section_sk=(STACK_OF(CONF_VALUE) *)sv->value;
3470Sstevel@tonic-gate 			continue;
3480Sstevel@tonic-gate 			}
3490Sstevel@tonic-gate 		else
3500Sstevel@tonic-gate 			{
3510Sstevel@tonic-gate 			pname=s;
3520Sstevel@tonic-gate 			psection=NULL;
3530Sstevel@tonic-gate 			end=eat_alpha_numeric(conf, s);
3540Sstevel@tonic-gate 			if ((end[0] == ':') && (end[1] == ':'))
3550Sstevel@tonic-gate 				{
3560Sstevel@tonic-gate 				*end='\0';
3570Sstevel@tonic-gate 				end+=2;
3580Sstevel@tonic-gate 				psection=pname;
3590Sstevel@tonic-gate 				pname=end;
3600Sstevel@tonic-gate 				end=eat_alpha_numeric(conf, end);
3610Sstevel@tonic-gate 				}
3620Sstevel@tonic-gate 			p=eat_ws(conf, end);
3630Sstevel@tonic-gate 			if (*p != '=')
3640Sstevel@tonic-gate 				{
365*2139Sjp161948 				CONFerr(CONF_F_DEF_LOAD_BIO,
3660Sstevel@tonic-gate 						CONF_R_MISSING_EQUAL_SIGN);
3670Sstevel@tonic-gate 				goto err;
3680Sstevel@tonic-gate 				}
3690Sstevel@tonic-gate 			*end='\0';
3700Sstevel@tonic-gate 			p++;
3710Sstevel@tonic-gate 			start=eat_ws(conf, p);
3720Sstevel@tonic-gate 			while (!IS_EOF(conf,*p))
3730Sstevel@tonic-gate 				p++;
3740Sstevel@tonic-gate 			p--;
3750Sstevel@tonic-gate 			while ((p != start) && (IS_WS(conf,*p)))
3760Sstevel@tonic-gate 				p--;
3770Sstevel@tonic-gate 			p++;
3780Sstevel@tonic-gate 			*p='\0';
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 			if (!(v=(CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE))))
3810Sstevel@tonic-gate 				{
382*2139Sjp161948 				CONFerr(CONF_F_DEF_LOAD_BIO,
3830Sstevel@tonic-gate 							ERR_R_MALLOC_FAILURE);
3840Sstevel@tonic-gate 				goto err;
3850Sstevel@tonic-gate 				}
3860Sstevel@tonic-gate 			if (psection == NULL) psection=section;
3870Sstevel@tonic-gate 			v->name=(char *)OPENSSL_malloc(strlen(pname)+1);
3880Sstevel@tonic-gate 			v->value=NULL;
3890Sstevel@tonic-gate 			if (v->name == NULL)
3900Sstevel@tonic-gate 				{
391*2139Sjp161948 				CONFerr(CONF_F_DEF_LOAD_BIO,
3920Sstevel@tonic-gate 							ERR_R_MALLOC_FAILURE);
3930Sstevel@tonic-gate 				goto err;
3940Sstevel@tonic-gate 				}
3950Sstevel@tonic-gate 			BUF_strlcpy(v->name,pname,strlen(pname)+1);
3960Sstevel@tonic-gate 			if (!str_copy(conf,psection,&(v->value),start)) goto err;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 			if (strcmp(psection,section) != 0)
3990Sstevel@tonic-gate 				{
4000Sstevel@tonic-gate 				if ((tv=_CONF_get_section(conf,psection))
4010Sstevel@tonic-gate 					== NULL)
4020Sstevel@tonic-gate 					tv=_CONF_new_section(conf,psection);
4030Sstevel@tonic-gate 				if (tv == NULL)
4040Sstevel@tonic-gate 					{
405*2139Sjp161948 					CONFerr(CONF_F_DEF_LOAD_BIO,
4060Sstevel@tonic-gate 					   CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
4070Sstevel@tonic-gate 					goto err;
4080Sstevel@tonic-gate 					}
4090Sstevel@tonic-gate 				ts=(STACK_OF(CONF_VALUE) *)tv->value;
4100Sstevel@tonic-gate 				}
4110Sstevel@tonic-gate 			else
4120Sstevel@tonic-gate 				{
4130Sstevel@tonic-gate 				tv=sv;
4140Sstevel@tonic-gate 				ts=section_sk;
4150Sstevel@tonic-gate 				}
4160Sstevel@tonic-gate #if 1
4170Sstevel@tonic-gate 			if (_CONF_add_string(conf, tv, v) == 0)
4180Sstevel@tonic-gate 				{
419*2139Sjp161948 				CONFerr(CONF_F_DEF_LOAD_BIO,
4200Sstevel@tonic-gate 							ERR_R_MALLOC_FAILURE);
4210Sstevel@tonic-gate 				goto err;
4220Sstevel@tonic-gate 				}
4230Sstevel@tonic-gate #else
4240Sstevel@tonic-gate 			v->section=tv->section;
4250Sstevel@tonic-gate 			if (!sk_CONF_VALUE_push(ts,v))
4260Sstevel@tonic-gate 				{
427*2139Sjp161948 				CONFerr(CONF_F_DEF_LOAD_BIO,
4280Sstevel@tonic-gate 							ERR_R_MALLOC_FAILURE);
4290Sstevel@tonic-gate 				goto err;
4300Sstevel@tonic-gate 				}
4310Sstevel@tonic-gate 			vv=(CONF_VALUE *)lh_insert(conf->data,v);
4320Sstevel@tonic-gate 			if (vv != NULL)
4330Sstevel@tonic-gate 				{
4340Sstevel@tonic-gate 				sk_CONF_VALUE_delete_ptr(ts,vv);
4350Sstevel@tonic-gate 				OPENSSL_free(vv->name);
4360Sstevel@tonic-gate 				OPENSSL_free(vv->value);
4370Sstevel@tonic-gate 				OPENSSL_free(vv);
4380Sstevel@tonic-gate 				}
4390Sstevel@tonic-gate #endif
4400Sstevel@tonic-gate 			v=NULL;
4410Sstevel@tonic-gate 			}
4420Sstevel@tonic-gate 		}
4430Sstevel@tonic-gate 	if (buff != NULL) BUF_MEM_free(buff);
4440Sstevel@tonic-gate 	if (section != NULL) OPENSSL_free(section);
4450Sstevel@tonic-gate 	return(1);
4460Sstevel@tonic-gate err:
4470Sstevel@tonic-gate 	if (buff != NULL) BUF_MEM_free(buff);
4480Sstevel@tonic-gate 	if (section != NULL) OPENSSL_free(section);
4490Sstevel@tonic-gate 	if (line != NULL) *line=eline;
4500Sstevel@tonic-gate 	BIO_snprintf(btmp,sizeof btmp,"%ld",eline);
4510Sstevel@tonic-gate 	ERR_add_error_data(2,"line ",btmp);
4520Sstevel@tonic-gate 	if ((h != conf->data) && (conf->data != NULL))
4530Sstevel@tonic-gate 		{
4540Sstevel@tonic-gate 		CONF_free(conf->data);
4550Sstevel@tonic-gate 		conf->data=NULL;
4560Sstevel@tonic-gate 		}
4570Sstevel@tonic-gate 	if (v != NULL)
4580Sstevel@tonic-gate 		{
4590Sstevel@tonic-gate 		if (v->name != NULL) OPENSSL_free(v->name);
4600Sstevel@tonic-gate 		if (v->value != NULL) OPENSSL_free(v->value);
4610Sstevel@tonic-gate 		if (v != NULL) OPENSSL_free(v);
4620Sstevel@tonic-gate 		}
4630Sstevel@tonic-gate 	return(0);
4640Sstevel@tonic-gate 	}
4650Sstevel@tonic-gate 
clear_comments(CONF * conf,char * p)4660Sstevel@tonic-gate static void clear_comments(CONF *conf, char *p)
4670Sstevel@tonic-gate 	{
4680Sstevel@tonic-gate 	char *to;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	to=p;
4710Sstevel@tonic-gate 	for (;;)
4720Sstevel@tonic-gate 		{
4730Sstevel@tonic-gate 		if (IS_FCOMMENT(conf,*p))
4740Sstevel@tonic-gate 			{
4750Sstevel@tonic-gate 			*p='\0';
4760Sstevel@tonic-gate 			return;
4770Sstevel@tonic-gate 			}
4780Sstevel@tonic-gate 		if (!IS_WS(conf,*p))
4790Sstevel@tonic-gate 			{
4800Sstevel@tonic-gate 			break;
4810Sstevel@tonic-gate 			}
4820Sstevel@tonic-gate 		p++;
4830Sstevel@tonic-gate 		}
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	for (;;)
4860Sstevel@tonic-gate 		{
4870Sstevel@tonic-gate 		if (IS_COMMENT(conf,*p))
4880Sstevel@tonic-gate 			{
4890Sstevel@tonic-gate 			*p='\0';
4900Sstevel@tonic-gate 			return;
4910Sstevel@tonic-gate 			}
4920Sstevel@tonic-gate 		if (IS_DQUOTE(conf,*p))
4930Sstevel@tonic-gate 			{
4940Sstevel@tonic-gate 			p=scan_dquote(conf, p);
4950Sstevel@tonic-gate 			continue;
4960Sstevel@tonic-gate 			}
4970Sstevel@tonic-gate 		if (IS_QUOTE(conf,*p))
4980Sstevel@tonic-gate 			{
4990Sstevel@tonic-gate 			p=scan_quote(conf, p);
5000Sstevel@tonic-gate 			continue;
5010Sstevel@tonic-gate 			}
5020Sstevel@tonic-gate 		if (IS_ESC(conf,*p))
5030Sstevel@tonic-gate 			{
5040Sstevel@tonic-gate 			p=scan_esc(conf,p);
5050Sstevel@tonic-gate 			continue;
5060Sstevel@tonic-gate 			}
5070Sstevel@tonic-gate 		if (IS_EOF(conf,*p))
5080Sstevel@tonic-gate 			return;
5090Sstevel@tonic-gate 		else
5100Sstevel@tonic-gate 			p++;
5110Sstevel@tonic-gate 		}
5120Sstevel@tonic-gate 	}
5130Sstevel@tonic-gate 
str_copy(CONF * conf,char * section,char ** pto,char * from)5140Sstevel@tonic-gate static int str_copy(CONF *conf, char *section, char **pto, char *from)
5150Sstevel@tonic-gate 	{
5160Sstevel@tonic-gate 	int q,r,rr=0,to=0,len=0;
5170Sstevel@tonic-gate 	char *s,*e,*rp,*p,*rrp,*np,*cp,v;
5180Sstevel@tonic-gate 	BUF_MEM *buf;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	if ((buf=BUF_MEM_new()) == NULL) return(0);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	len=strlen(from)+1;
5230Sstevel@tonic-gate 	if (!BUF_MEM_grow(buf,len)) goto err;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	for (;;)
5260Sstevel@tonic-gate 		{
5270Sstevel@tonic-gate 		if (IS_QUOTE(conf,*from))
5280Sstevel@tonic-gate 			{
5290Sstevel@tonic-gate 			q= *from;
5300Sstevel@tonic-gate 			from++;
5310Sstevel@tonic-gate 			while (!IS_EOF(conf,*from) && (*from != q))
5320Sstevel@tonic-gate 				{
5330Sstevel@tonic-gate 				if (IS_ESC(conf,*from))
5340Sstevel@tonic-gate 					{
5350Sstevel@tonic-gate 					from++;
5360Sstevel@tonic-gate 					if (IS_EOF(conf,*from)) break;
5370Sstevel@tonic-gate 					}
5380Sstevel@tonic-gate 				buf->data[to++]= *(from++);
5390Sstevel@tonic-gate 				}
5400Sstevel@tonic-gate 			if (*from == q) from++;
5410Sstevel@tonic-gate 			}
5420Sstevel@tonic-gate 		else if (IS_DQUOTE(conf,*from))
5430Sstevel@tonic-gate 			{
5440Sstevel@tonic-gate 			q= *from;
5450Sstevel@tonic-gate 			from++;
5460Sstevel@tonic-gate 			while (!IS_EOF(conf,*from))
5470Sstevel@tonic-gate 				{
5480Sstevel@tonic-gate 				if (*from == q)
5490Sstevel@tonic-gate 					{
5500Sstevel@tonic-gate 					if (*(from+1) == q)
5510Sstevel@tonic-gate 						{
5520Sstevel@tonic-gate 						from++;
5530Sstevel@tonic-gate 						}
5540Sstevel@tonic-gate 					else
5550Sstevel@tonic-gate 						{
5560Sstevel@tonic-gate 						break;
5570Sstevel@tonic-gate 						}
5580Sstevel@tonic-gate 					}
5590Sstevel@tonic-gate 				buf->data[to++]= *(from++);
5600Sstevel@tonic-gate 				}
5610Sstevel@tonic-gate 			if (*from == q) from++;
5620Sstevel@tonic-gate 			}
5630Sstevel@tonic-gate 		else if (IS_ESC(conf,*from))
5640Sstevel@tonic-gate 			{
5650Sstevel@tonic-gate 			from++;
5660Sstevel@tonic-gate 			v= *(from++);
5670Sstevel@tonic-gate 			if (IS_EOF(conf,v)) break;
5680Sstevel@tonic-gate 			else if (v == 'r') v='\r';
5690Sstevel@tonic-gate 			else if (v == 'n') v='\n';
5700Sstevel@tonic-gate 			else if (v == 'b') v='\b';
5710Sstevel@tonic-gate 			else if (v == 't') v='\t';
5720Sstevel@tonic-gate 			buf->data[to++]= v;
5730Sstevel@tonic-gate 			}
5740Sstevel@tonic-gate 		else if (IS_EOF(conf,*from))
5750Sstevel@tonic-gate 			break;
5760Sstevel@tonic-gate 		else if (*from == '$')
5770Sstevel@tonic-gate 			{
5780Sstevel@tonic-gate 			/* try to expand it */
5790Sstevel@tonic-gate 			rrp=NULL;
5800Sstevel@tonic-gate 			s= &(from[1]);
5810Sstevel@tonic-gate 			if (*s == '{')
5820Sstevel@tonic-gate 				q='}';
5830Sstevel@tonic-gate 			else if (*s == '(')
5840Sstevel@tonic-gate 				q=')';
5850Sstevel@tonic-gate 			else q=0;
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 			if (q) s++;
5880Sstevel@tonic-gate 			cp=section;
5890Sstevel@tonic-gate 			e=np=s;
5900Sstevel@tonic-gate 			while (IS_ALPHA_NUMERIC(conf,*e))
5910Sstevel@tonic-gate 				e++;
5920Sstevel@tonic-gate 			if ((e[0] == ':') && (e[1] == ':'))
5930Sstevel@tonic-gate 				{
5940Sstevel@tonic-gate 				cp=np;
5950Sstevel@tonic-gate 				rrp=e;
5960Sstevel@tonic-gate 				rr= *e;
5970Sstevel@tonic-gate 				*rrp='\0';
5980Sstevel@tonic-gate 				e+=2;
5990Sstevel@tonic-gate 				np=e;
6000Sstevel@tonic-gate 				while (IS_ALPHA_NUMERIC(conf,*e))
6010Sstevel@tonic-gate 					e++;
6020Sstevel@tonic-gate 				}
6030Sstevel@tonic-gate 			r= *e;
6040Sstevel@tonic-gate 			*e='\0';
6050Sstevel@tonic-gate 			rp=e;
6060Sstevel@tonic-gate 			if (q)
6070Sstevel@tonic-gate 				{
6080Sstevel@tonic-gate 				if (r != q)
6090Sstevel@tonic-gate 					{
6100Sstevel@tonic-gate 					CONFerr(CONF_F_STR_COPY,CONF_R_NO_CLOSE_BRACE);
6110Sstevel@tonic-gate 					goto err;
6120Sstevel@tonic-gate 					}
6130Sstevel@tonic-gate 				e++;
6140Sstevel@tonic-gate 				}
6150Sstevel@tonic-gate 			/* So at this point we have
616*2139Sjp161948 			 * np which is the start of the name string which is
6170Sstevel@tonic-gate 			 *   '\0' terminated.
618*2139Sjp161948 			 * cp which is the start of the section string which is
6190Sstevel@tonic-gate 			 *   '\0' terminated.
6200Sstevel@tonic-gate 			 * e is the 'next point after'.
621*2139Sjp161948 			 * r and rr are the chars replaced by the '\0'
622*2139Sjp161948 			 * rp and rrp is where 'r' and 'rr' came from.
6230Sstevel@tonic-gate 			 */
6240Sstevel@tonic-gate 			p=_CONF_get_string(conf,cp,np);
6250Sstevel@tonic-gate 			if (rrp != NULL) *rrp=rr;
6260Sstevel@tonic-gate 			*rp=r;
6270Sstevel@tonic-gate 			if (p == NULL)
6280Sstevel@tonic-gate 				{
6290Sstevel@tonic-gate 				CONFerr(CONF_F_STR_COPY,CONF_R_VARIABLE_HAS_NO_VALUE);
6300Sstevel@tonic-gate 				goto err;
6310Sstevel@tonic-gate 				}
632*2139Sjp161948 			BUF_MEM_grow_clean(buf,(strlen(p)+buf->length-(e-from)));
6330Sstevel@tonic-gate 			while (*p)
6340Sstevel@tonic-gate 				buf->data[to++]= *(p++);
635*2139Sjp161948 
636*2139Sjp161948 			/* Since we change the pointer 'from', we also have
637*2139Sjp161948 			   to change the perceived length of the string it
638*2139Sjp161948 			   points at.  /RL */
639*2139Sjp161948 			len -= e-from;
6400Sstevel@tonic-gate 			from=e;
641*2139Sjp161948 
642*2139Sjp161948 			/* In case there were no braces or parenthesis around
643*2139Sjp161948 			   the variable reference, we have to put back the
644*2139Sjp161948 			   character that was replaced with a '\0'.  /RL */
645*2139Sjp161948 			*rp = r;
6460Sstevel@tonic-gate 			}
6470Sstevel@tonic-gate 		else
6480Sstevel@tonic-gate 			buf->data[to++]= *(from++);
6490Sstevel@tonic-gate 		}
6500Sstevel@tonic-gate 	buf->data[to]='\0';
6510Sstevel@tonic-gate 	if (*pto != NULL) OPENSSL_free(*pto);
6520Sstevel@tonic-gate 	*pto=buf->data;
6530Sstevel@tonic-gate 	OPENSSL_free(buf);
6540Sstevel@tonic-gate 	return(1);
6550Sstevel@tonic-gate err:
6560Sstevel@tonic-gate 	if (buf != NULL) BUF_MEM_free(buf);
6570Sstevel@tonic-gate 	return(0);
6580Sstevel@tonic-gate 	}
6590Sstevel@tonic-gate 
eat_ws(CONF * conf,char * p)6600Sstevel@tonic-gate static char *eat_ws(CONF *conf, char *p)
6610Sstevel@tonic-gate 	{
6620Sstevel@tonic-gate 	while (IS_WS(conf,*p) && (!IS_EOF(conf,*p)))
6630Sstevel@tonic-gate 		p++;
6640Sstevel@tonic-gate 	return(p);
6650Sstevel@tonic-gate 	}
6660Sstevel@tonic-gate 
eat_alpha_numeric(CONF * conf,char * p)6670Sstevel@tonic-gate static char *eat_alpha_numeric(CONF *conf, char *p)
6680Sstevel@tonic-gate 	{
6690Sstevel@tonic-gate 	for (;;)
6700Sstevel@tonic-gate 		{
6710Sstevel@tonic-gate 		if (IS_ESC(conf,*p))
6720Sstevel@tonic-gate 			{
6730Sstevel@tonic-gate 			p=scan_esc(conf,p);
6740Sstevel@tonic-gate 			continue;
6750Sstevel@tonic-gate 			}
6760Sstevel@tonic-gate 		if (!IS_ALPHA_NUMERIC_PUNCT(conf,*p))
6770Sstevel@tonic-gate 			return(p);
6780Sstevel@tonic-gate 		p++;
6790Sstevel@tonic-gate 		}
6800Sstevel@tonic-gate 	}
6810Sstevel@tonic-gate 
scan_quote(CONF * conf,char * p)6820Sstevel@tonic-gate static char *scan_quote(CONF *conf, char *p)
6830Sstevel@tonic-gate 	{
6840Sstevel@tonic-gate 	int q= *p;
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	p++;
6870Sstevel@tonic-gate 	while (!(IS_EOF(conf,*p)) && (*p != q))
6880Sstevel@tonic-gate 		{
6890Sstevel@tonic-gate 		if (IS_ESC(conf,*p))
6900Sstevel@tonic-gate 			{
6910Sstevel@tonic-gate 			p++;
6920Sstevel@tonic-gate 			if (IS_EOF(conf,*p)) return(p);
6930Sstevel@tonic-gate 			}
6940Sstevel@tonic-gate 		p++;
6950Sstevel@tonic-gate 		}
6960Sstevel@tonic-gate 	if (*p == q) p++;
6970Sstevel@tonic-gate 	return(p);
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 
scan_dquote(CONF * conf,char * p)7010Sstevel@tonic-gate static char *scan_dquote(CONF *conf, char *p)
7020Sstevel@tonic-gate 	{
7030Sstevel@tonic-gate 	int q= *p;
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	p++;
7060Sstevel@tonic-gate 	while (!(IS_EOF(conf,*p)))
7070Sstevel@tonic-gate 		{
7080Sstevel@tonic-gate 		if (*p == q)
7090Sstevel@tonic-gate 			{
7100Sstevel@tonic-gate 			if (*(p+1) == q)
7110Sstevel@tonic-gate 				{
7120Sstevel@tonic-gate 				p++;
7130Sstevel@tonic-gate 				}
7140Sstevel@tonic-gate 			else
7150Sstevel@tonic-gate 				{
7160Sstevel@tonic-gate 				break;
7170Sstevel@tonic-gate 				}
7180Sstevel@tonic-gate 			}
7190Sstevel@tonic-gate 		p++;
7200Sstevel@tonic-gate 		}
7210Sstevel@tonic-gate 	if (*p == q) p++;
7220Sstevel@tonic-gate 	return(p);
7230Sstevel@tonic-gate 	}
7240Sstevel@tonic-gate 
dump_value(CONF_VALUE * a,BIO * out)7250Sstevel@tonic-gate static void dump_value(CONF_VALUE *a, BIO *out)
7260Sstevel@tonic-gate 	{
7270Sstevel@tonic-gate 	if (a->name)
7280Sstevel@tonic-gate 		BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
7290Sstevel@tonic-gate 	else
7300Sstevel@tonic-gate 		BIO_printf(out, "[[%s]]\n", a->section);
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate 
IMPLEMENT_LHASH_DOALL_ARG_FN(dump_value,CONF_VALUE *,BIO *)7330Sstevel@tonic-gate static IMPLEMENT_LHASH_DOALL_ARG_FN(dump_value, CONF_VALUE *, BIO *)
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate static int def_dump(const CONF *conf, BIO *out)
7360Sstevel@tonic-gate 	{
7370Sstevel@tonic-gate 	lh_doall_arg(conf->data, LHASH_DOALL_ARG_FN(dump_value), out);
7380Sstevel@tonic-gate 	return 1;
7390Sstevel@tonic-gate 	}
7400Sstevel@tonic-gate 
def_is_number(const CONF * conf,char c)7410Sstevel@tonic-gate static int def_is_number(const CONF *conf, char c)
7420Sstevel@tonic-gate 	{
7430Sstevel@tonic-gate 	return IS_NUMBER(conf,c);
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate 
def_to_int(const CONF * conf,char c)7460Sstevel@tonic-gate static int def_to_int(const CONF *conf, char c)
7470Sstevel@tonic-gate 	{
7480Sstevel@tonic-gate 	return c - '0';
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 
751