Update contrib.
1 /* crypto/conf/conf.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
59 © Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
62 /* Part of the code in here was originally in conf.c, which is now removed */
67 #include <openssl/stack.h>
68 #include <openssl/lhash.h>
69 #include <openssl/conf.h>
70 #include <openssl/conf_api.h>
72 #include <openssl/buffer.h>
73 #include <openssl/err.h>
74 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
75 #include "libcrypto_wsd_macros.h"
76 #include "libcrypto_wsd.h"
79 static char *eat_ws(CONF *conf, char *p);
80 static char *eat_alpha_numeric(CONF *conf, char *p);
81 static void clear_comments(CONF *conf, char *p);
82 static int str_copy(CONF *conf,char *section,char **to, char *from);
83 static char *scan_quote(CONF *conf, char *p);
84 static char *scan_dquote(CONF *conf, char *p);
85 #define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
87 static CONF *def_create(CONF_METHOD *meth);
88 static int def_init_default(CONF *conf);
89 static int def_init_WIN32(CONF *conf);
90 static int def_destroy(CONF *conf);
91 static int def_destroy_data(CONF *conf);
92 static int def_load(CONF *conf, const char *name, long *eline);
93 static int def_load_bio(CONF *conf, BIO *bp, long *eline);
94 static int def_dump(const CONF *conf, BIO *bp);
95 static int def_is_number(const CONF *conf, char c);
96 static int def_to_int(const CONF *conf, char c);
98 const char CONF_def_version[]="CONF_def" OPENSSL_VERSION_PTEXT;
101 static CONF_METHOD default_method = {
114 static CONF_METHOD WIN32_method = {
127 GET_STATIC_VAR_FROM_TLS(default_method,conf_def,CONF_METHOD)
128 #define default_method (*GET_WSD_VAR_NAME(default_method,conf_def, s)())
129 const CONF_METHOD temp_s_default_method = {
142 GET_STATIC_VAR_FROM_TLS(WIN32_method,conf_def,CONF_METHOD)
143 #define WIN32_method (*GET_WSD_VAR_NAME(WIN32_method,conf_def, s)())
144 const CONF_METHOD temp_s_WIN32_method = {
160 EXPORT_C CONF_METHOD *NCONF_default()
162 return &default_method;
164 CONF_METHOD *NCONF_WIN32()
166 return &WIN32_method;
169 static CONF *def_create(CONF_METHOD *meth)
173 ret = (CONF *)OPENSSL_malloc(sizeof(CONF) + sizeof(unsigned short *));
175 if (meth->init(ret) == 0)
183 static int def_init_default(CONF *conf)
188 conf->meth = &default_method;
189 conf->meth_data = (void *)CONF_type_default;
195 static int def_init_WIN32(CONF *conf)
200 conf->meth = &WIN32_method;
201 conf->meth_data = (void *)CONF_type_win32;
207 static int def_destroy(CONF *conf)
209 if (def_destroy_data(conf))
217 static int def_destroy_data(CONF *conf)
221 _CONF_free_data(conf);
225 static int def_load(CONF *conf, const char *name, long *line)
230 #ifdef OPENSSL_SYS_VMS
231 in=BIO_new_file(name, "r");
233 in=BIO_new_file(name, "rb");
237 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
238 CONFerr(CONF_F_DEF_LOAD,CONF_R_NO_SUCH_FILE);
240 CONFerr(CONF_F_DEF_LOAD,ERR_R_SYS_LIB);
244 ret = def_load_bio(conf, in, line);
250 static int def_load_bio(CONF *conf, BIO *in, long *line)
252 /* The macro BUFSIZE conflicts with a system macro in VxWorks */
253 #define CONFBUFSIZE 512
259 char btmp[DECIMAL_SIZE(eline)+1];
260 CONF_VALUE *v=NULL,*tv;
262 char *section=NULL,*buf;
263 STACK_OF(CONF_VALUE) *section_sk=NULL,*ts;
264 char *start,*psection,*pname;
265 void *h = (void *)(conf->data);
267 if ((buff=BUF_MEM_new()) == NULL)
269 CONFerr(CONF_F_DEF_LOAD_BIO,ERR_R_BUF_LIB);
273 section=(char *)OPENSSL_malloc(10);
276 CONFerr(CONF_F_DEF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
279 BUF_strlcpy(section,"default",10);
281 if (_CONF_new_data(conf) == 0)
283 CONFerr(CONF_F_DEF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
287 sv=_CONF_new_section(conf,section);
290 CONFerr(CONF_F_DEF_LOAD_BIO,
291 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
294 section_sk=(STACK_OF(CONF_VALUE) *)sv->value;
300 if (!BUF_MEM_grow(buff,bufnum+CONFBUFSIZE))
302 CONFerr(CONF_F_DEF_LOAD_BIO,ERR_R_BUF_LIB);
305 p= &(buff->data[bufnum]);
307 BIO_gets(in, p, CONFBUFSIZE-1);
308 p[CONFBUFSIZE-1]='\0';
310 if (i == 0 && !again) break;
314 if ((p[i-1] != '\r') && (p[i-1] != '\n'))
319 /* we removed some trailing stuff so there is a new
320 * line on the end. */
322 again=1; /* long line */
326 eline++; /* another input line */
329 /* we now have a line with trailing \r\n removed */
331 /* i is the number of bytes */
335 /* check for line continuation */
338 /* If we have bytes and the last char '\\' and
339 * second last char is not '\\' */
340 p= &(buff->data[bufnum-1]);
341 if (IS_ESC(conf,p[0]) &&
342 ((bufnum <= 1) || !IS_ESC(conf,p[-1])))
352 clear_comments(conf, buf);
355 if (IS_EOF(conf,*s)) continue; /* blank line */
361 start=eat_ws(conf, s);
364 end=eat_alpha_numeric(conf, ss);
373 CONFerr(CONF_F_DEF_LOAD_BIO,
374 CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
378 if (!str_copy(conf,NULL,§ion,start)) goto err;
379 if ((sv=_CONF_get_section(conf,section)) == NULL)
380 sv=_CONF_new_section(conf,section);
383 CONFerr(CONF_F_DEF_LOAD_BIO,
384 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
387 section_sk=(STACK_OF(CONF_VALUE) *)sv->value;
394 end=eat_alpha_numeric(conf, s);
395 if ((end[0] == ':') && (end[1] == ':'))
401 end=eat_alpha_numeric(conf, end);
406 CONFerr(CONF_F_DEF_LOAD_BIO,
407 CONF_R_MISSING_EQUAL_SIGN);
412 start=eat_ws(conf, p);
413 while (!IS_EOF(conf,*p))
416 while ((p != start) && (IS_WS(conf,*p)))
421 if (!(v=(CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE))))
423 CONFerr(CONF_F_DEF_LOAD_BIO,
424 ERR_R_MALLOC_FAILURE);
427 if (psection == NULL) psection=section;
428 v->name=(char *)OPENSSL_malloc(strlen(pname)+1);
432 CONFerr(CONF_F_DEF_LOAD_BIO,
433 ERR_R_MALLOC_FAILURE);
436 BUF_strlcpy(v->name,pname,strlen(pname)+1);
437 if (!str_copy(conf,psection,&(v->value),start)) goto err;
439 if (strcmp(psection,section) != 0)
441 if ((tv=_CONF_get_section(conf,psection))
443 tv=_CONF_new_section(conf,psection);
446 CONFerr(CONF_F_DEF_LOAD_BIO,
447 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
450 ts=(STACK_OF(CONF_VALUE) *)tv->value;
458 if (_CONF_add_string(conf, tv, v) == 0)
460 CONFerr(CONF_F_DEF_LOAD_BIO,
461 ERR_R_MALLOC_FAILURE);
465 v->section=tv->section;
466 if (!sk_CONF_VALUE_push(ts,v))
468 CONFerr(CONF_F_DEF_LOAD_BIO,
469 ERR_R_MALLOC_FAILURE);
472 vv=(CONF_VALUE *)lh_insert(conf->data,v);
475 sk_CONF_VALUE_delete_ptr(ts,vv);
476 OPENSSL_free(vv->name);
477 OPENSSL_free(vv->value);
484 if (buff != NULL) BUF_MEM_free(buff);
485 if (section != NULL) OPENSSL_free(section);
488 if (buff != NULL) BUF_MEM_free(buff);
489 if (section != NULL) OPENSSL_free(section);
490 if (line != NULL) *line=eline;
491 BIO_snprintf(btmp,sizeof btmp,"%ld",eline);
492 ERR_add_error_data(2,"line ",btmp);
493 if ((h != conf->data) && (conf->data != NULL))
495 CONF_free(conf->data);
500 if (v->name != NULL) OPENSSL_free(v->name);
501 if (v->value != NULL) OPENSSL_free(v->value);
502 if (v != NULL) OPENSSL_free(v);
507 static void clear_comments(CONF *conf, char *p)
514 if (IS_FCOMMENT(conf,*p))
528 if (IS_COMMENT(conf,*p))
533 if (IS_DQUOTE(conf,*p))
535 p=scan_dquote(conf, p);
538 if (IS_QUOTE(conf,*p))
540 p=scan_quote(conf, p);
555 static int str_copy(CONF *conf, char *section, char **pto, char *from)
557 int q,r,rr=0,to=0,len=0;
558 char *s,*e,*rp,*p,*rrp,*np,*cp,v;
561 if ((buf=BUF_MEM_new()) == NULL) return(0);
564 if (!BUF_MEM_grow(buf,len)) goto err;
568 if (IS_QUOTE(conf,*from))
572 while (!IS_EOF(conf,*from) && (*from != q))
574 if (IS_ESC(conf,*from))
577 if (IS_EOF(conf,*from)) break;
579 buf->data[to++]= *(from++);
581 if (*from == q) from++;
583 else if (IS_DQUOTE(conf,*from))
587 while (!IS_EOF(conf,*from))
600 buf->data[to++]= *(from++);
602 if (*from == q) from++;
604 else if (IS_ESC(conf,*from))
608 if (IS_EOF(conf,v)) break;
609 else if (v == 'r') v='\r';
610 else if (v == 'n') v='\n';
611 else if (v == 'b') v='\b';
612 else if (v == 't') v='\t';
615 else if (IS_EOF(conf,*from))
617 else if (*from == '$')
619 /* try to expand it */
631 while (IS_ALPHA_NUMERIC(conf,*e))
633 if ((e[0] == ':') && (e[1] == ':'))
641 while (IS_ALPHA_NUMERIC(conf,*e))
651 CONFerr(CONF_F_STR_COPY,CONF_R_NO_CLOSE_BRACE);
656 /* So at this point we have
657 * np which is the start of the name string which is
659 * cp which is the start of the section string which is
661 * e is the 'next point after'.
662 * r and rr are the chars replaced by the '\0'
663 * rp and rrp is where 'r' and 'rr' came from.
665 p=_CONF_get_string(conf,cp,np);
666 if (rrp != NULL) *rrp=rr;
670 CONFerr(CONF_F_STR_COPY,CONF_R_VARIABLE_HAS_NO_VALUE);
673 BUF_MEM_grow_clean(buf,(strlen(p)+buf->length-(e-from)));
675 buf->data[to++]= *(p++);
677 /* Since we change the pointer 'from', we also have
678 to change the perceived length of the string it
683 /* In case there were no braces or parenthesis around
684 the variable reference, we have to put back the
685 character that was replaced with a '\0'. /RL */
689 buf->data[to++]= *(from++);
692 if (*pto != NULL) OPENSSL_free(*pto);
697 if (buf != NULL) BUF_MEM_free(buf);
701 static char *eat_ws(CONF *conf, char *p)
703 while (IS_WS(conf,*p) && (!IS_EOF(conf,*p)))
708 static char *eat_alpha_numeric(CONF *conf, char *p)
717 if (!IS_ALPHA_NUMERIC_PUNCT(conf,*p))
723 static char *scan_quote(CONF *conf, char *p)
728 while (!(IS_EOF(conf,*p)) && (*p != q))
733 if (IS_EOF(conf,*p)) return(p);
742 static char *scan_dquote(CONF *conf, char *p)
747 while (!(IS_EOF(conf,*p)))
766 static void dump_value(CONF_VALUE *a, BIO *out)
769 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
771 BIO_printf(out, "[[%s]]\n", a->section);
774 static IMPLEMENT_LHASH_DOALL_ARG_FN(dump_value, CONF_VALUE *, BIO *)
776 static int def_dump(const CONF *conf, BIO *out)
778 lh_doall_arg(conf->data, LHASH_DOALL_ARG_FN(dump_value), out);
782 static int def_is_number(const CONF *conf, char c)
784 return IS_NUMBER(conf,c);
787 static int def_to_int(const CONF *conf, char c)