OpenSSL Security Advisory [30 July 2002]
[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 <openssl/stack.h>
64 #include <openssl/lhash.h>
65 #include <openssl/conf.h>
66 #include <openssl/conf_api.h>
67 #include "conf_def.h"
68 #include <openssl/buffer.h>
69 #include <openssl/err.h>
70 #include "cryptlib.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 CONF_METHOD *NCONF_WIN32()
124         {
125         return &WIN32_method;
126         }
127
128 static CONF *def_create(CONF_METHOD *meth)
129         {
130         CONF *ret;
131
132         ret = (CONF *)OPENSSL_malloc(sizeof(CONF) + sizeof(unsigned short *));
133         if (ret)
134                 if (meth->init(ret) == 0)
135                         {
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                 {
170                 OPENSSL_free(conf);
171                 return 1;
172                 }
173         return 0;
174         }
175
176 static int def_destroy_data(CONF *conf)
177         {
178         if (conf == NULL)
179                 return 0;
180         _CONF_free_data(conf);
181         return 1;
182         }
183
184 static int def_load(CONF *conf, const char *name, long *line)
185         {
186         int ret;
187         BIO *in=NULL;
188
189 #ifdef OPENSSL_SYS_VMS
190         in=BIO_new_file(name, "r");
191 #else
192         in=BIO_new_file(name, "rb");
193 #endif
194         if (in == NULL)
195                 {
196                 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
197                         CONFerr(CONF_F_CONF_LOAD,CONF_R_NO_SUCH_FILE);
198                 else
199                         CONFerr(CONF_F_CONF_LOAD,ERR_R_SYS_LIB);
200                 return 0;
201                 }
202
203         ret = def_load_bio(conf, in, line);
204         BIO_free(in);
205
206         return ret;
207         }
208
209 static int def_load_bio(CONF *conf, BIO *in, long *line)
210         {
211 #define BUFSIZE 512
212         int bufnum=0,i,ii;
213         BUF_MEM *buff=NULL;
214         char *s,*p,*end;
215         int again,n;
216         long eline=0;
217         char btmp[DECIMAL_SIZE(eline)+1];
218         CONF_VALUE *v=NULL,*tv;
219         CONF_VALUE *sv=NULL;
220         char *section=NULL,*buf;
221         STACK_OF(CONF_VALUE) *section_sk=NULL,*ts;
222         char *start,*psection,*pname;
223         void *h = (void *)(conf->data);
224
225         if ((buff=BUF_MEM_new()) == NULL)
226                 {
227                 CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_BUF_LIB);
228                 goto err;
229                 }
230
231         section=(char *)OPENSSL_malloc(10);
232         if (section == NULL)
233                 {
234                 CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
235                 goto err;
236                 }
237         strcpy(section,"default");
238
239         if (_CONF_new_data(conf) == 0)
240                 {
241                 CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
242                 goto err;
243                 }
244
245         sv=_CONF_new_section(conf,section);
246         if (sv == NULL)
247                 {
248                 CONFerr(CONF_F_CONF_LOAD_BIO,
249                                         CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
250                 goto err;
251                 }
252         section_sk=(STACK_OF(CONF_VALUE) *)sv->value;
253
254         bufnum=0;
255         for (;;)
256                 {
257                 again=0;
258                 if (!BUF_MEM_grow(buff,bufnum+BUFSIZE))
259                         {
260                         CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_BUF_LIB);
261                         goto err;
262                         }
263                 p= &(buff->data[bufnum]);
264                 *p='\0';
265                 BIO_gets(in, p, BUFSIZE-1);
266                 p[BUFSIZE-1]='\0';
267                 ii=i=strlen(p);
268                 if (i == 0) break;
269                 while (i > 0)
270                         {
271                         if ((p[i-1] != '\r') && (p[i-1] != '\n'))
272                                 break;
273                         else
274                                 i--;
275                         }
276                 /* we removed some trailing stuff so there is a new
277                  * line on the end. */
278                 if (i == ii)
279                         again=1; /* long line */
280                 else
281                         {
282                         p[i]='\0';
283                         eline++; /* another input line */
284                         }
285
286                 /* we now have a line with trailing \r\n removed */
287
288                 /* i is the number of bytes */
289                 bufnum+=i;
290
291                 v=NULL;
292                 /* check for line continuation */
293                 if (bufnum >= 1)
294                         {
295                         /* If we have bytes and the last char '\\' and
296                          * second last char is not '\\' */
297                         p= &(buff->data[bufnum-1]);
298                         if (IS_ESC(conf,p[0]) &&
299                                 ((bufnum <= 1) || !IS_ESC(conf,p[-1])))
300                                 {
301                                 bufnum--;
302                                 again=1;
303                                 }
304                         }
305                 if (again) continue;
306                 bufnum=0;
307                 buf=buff->data;
308
309                 clear_comments(conf, buf);
310                 n=strlen(buf);
311                 s=eat_ws(conf, buf);
312                 if (IS_EOF(conf,*s)) continue; /* blank line */
313                 if (*s == '[')
314                         {
315                         char *ss;
316
317                         s++;
318                         start=eat_ws(conf, s);
319                         ss=start;
320 again:
321                         end=eat_alpha_numeric(conf, ss);
322                         p=eat_ws(conf, end);
323                         if (*p != ']')
324                                 {
325                                 if (*p != '\0')
326                                         {
327                                         ss=p;
328                                         goto again;
329                                         }
330                                 CONFerr(CONF_F_CONF_LOAD_BIO,
331                                         CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
332                                 goto err;
333                                 }
334                         *end='\0';
335                         if (!str_copy(conf,NULL,&section,start)) goto err;
336                         if ((sv=_CONF_get_section(conf,section)) == NULL)
337                                 sv=_CONF_new_section(conf,section);
338                         if (sv == NULL)
339                                 {
340                                 CONFerr(CONF_F_CONF_LOAD_BIO,
341                                         CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
342                                 goto err;
343                                 }
344                         section_sk=(STACK_OF(CONF_VALUE) *)sv->value;
345                         continue;
346                         }
347                 else
348                         {
349                         pname=s;
350                         psection=NULL;
351                         end=eat_alpha_numeric(conf, s);
352                         if ((end[0] == ':') && (end[1] == ':'))
353                                 {
354                                 *end='\0';
355                                 end+=2;
356                                 psection=pname;
357                                 pname=end;
358                                 end=eat_alpha_numeric(conf, end);
359                                 }
360                         p=eat_ws(conf, end);
361                         if (*p != '=')
362                                 {
363                                 CONFerr(CONF_F_CONF_LOAD_BIO,
364                                                 CONF_R_MISSING_EQUAL_SIGN);
365                                 goto err;
366                                 }
367                         *end='\0';
368                         p++;
369                         start=eat_ws(conf, p);
370                         while (!IS_EOF(conf,*p))
371                                 p++;
372                         p--;
373                         while ((p != start) && (IS_WS(conf,*p)))
374                                 p--;
375                         p++;
376                         *p='\0';
377
378                         if (!(v=(CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE))))
379                                 {
380                                 CONFerr(CONF_F_CONF_LOAD_BIO,
381                                                         ERR_R_MALLOC_FAILURE);
382                                 goto err;
383                                 }
384                         if (psection == NULL) psection=section;
385                         v->name=(char *)OPENSSL_malloc(strlen(pname)+1);
386                         v->value=NULL;
387                         if (v->name == NULL)
388                                 {
389                                 CONFerr(CONF_F_CONF_LOAD_BIO,
390                                                         ERR_R_MALLOC_FAILURE);
391                                 goto err;
392                                 }
393                         strcpy(v->name,pname);
394                         if (!str_copy(conf,psection,&(v->value),start)) goto err;
395
396                         if (strcmp(psection,section) != 0)
397                                 {
398                                 if ((tv=_CONF_get_section(conf,psection))
399                                         == NULL)
400                                         tv=_CONF_new_section(conf,psection);
401                                 if (tv == NULL)
402                                         {
403                                         CONFerr(CONF_F_CONF_LOAD_BIO,
404                                            CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
405                                         goto err;
406                                         }
407                                 ts=(STACK_OF(CONF_VALUE) *)tv->value;
408                                 }
409                         else
410                                 {
411                                 tv=sv;
412                                 ts=section_sk;
413                                 }
414 #if 1
415                         if (_CONF_add_string(conf, tv, v) == 0)
416                                 {
417                                 CONFerr(CONF_F_CONF_LOAD_BIO,
418                                                         ERR_R_MALLOC_FAILURE);
419                                 goto err;
420                                 }
421 #else
422                         v->section=tv->section; 
423                         if (!sk_CONF_VALUE_push(ts,v))
424                                 {
425                                 CONFerr(CONF_F_CONF_LOAD_BIO,
426                                                         ERR_R_MALLOC_FAILURE);
427                                 goto err;
428                                 }
429                         vv=(CONF_VALUE *)lh_insert(conf->data,v);
430                         if (vv != NULL)
431                                 {
432                                 sk_CONF_VALUE_delete_ptr(ts,vv);
433                                 OPENSSL_free(vv->name);
434                                 OPENSSL_free(vv->value);
435                                 OPENSSL_free(vv);
436                                 }
437 #endif
438                         v=NULL;
439                         }
440                 }
441         if (buff != NULL) BUF_MEM_free(buff);
442         if (section != NULL) OPENSSL_free(section);
443         return(1);
444 err:
445         if (buff != NULL) BUF_MEM_free(buff);
446         if (section != NULL) OPENSSL_free(section);
447         if (line != NULL) *line=eline;
448         sprintf(btmp,"%ld",eline);
449         ERR_add_error_data(2,"line ",btmp);
450         if ((h != conf->data) && (conf->data != NULL))
451                 {
452                 CONF_free(conf->data);
453                 conf->data=NULL;
454                 }
455         if (v != NULL)
456                 {
457                 if (v->name != NULL) OPENSSL_free(v->name);
458                 if (v->value != NULL) OPENSSL_free(v->value);
459                 if (v != NULL) OPENSSL_free(v);
460                 }
461         return(0);
462         }
463
464 static void clear_comments(CONF *conf, char *p)
465         {
466         char *to;
467
468         to=p;
469         for (;;)
470                 {
471                 if (IS_FCOMMENT(conf,*p))
472                         {
473                         *p='\0';
474                         return;
475                         }
476                 if (!IS_WS(conf,*p))
477                         {
478                         break;
479                         }
480                 p++;
481                 }
482
483         for (;;)
484                 {
485                 if (IS_COMMENT(conf,*p))
486                         {
487                         *p='\0';
488                         return;
489                         }
490                 if (IS_DQUOTE(conf,*p))
491                         {
492                         p=scan_dquote(conf, p);
493                         continue;
494                         }
495                 if (IS_QUOTE(conf,*p))
496                         {
497                         p=scan_quote(conf, p);
498                         continue;
499                         }
500                 if (IS_ESC(conf,*p))
501                         {
502                         p=scan_esc(conf,p);
503                         continue;
504                         }
505                 if (IS_EOF(conf,*p))
506                         return;
507                 else
508                         p++;
509                 }
510         }
511
512 static int str_copy(CONF *conf, char *section, char **pto, char *from)
513         {
514         int q,r,rr=0,to=0,len=0;
515         char *s,*e,*rp,*p,*rrp,*np,*cp,v;
516         BUF_MEM *buf;
517
518         if ((buf=BUF_MEM_new()) == NULL) return(0);
519
520         len=strlen(from)+1;
521         if (!BUF_MEM_grow(buf,len)) goto err;
522
523         for (;;)
524                 {
525                 if (IS_QUOTE(conf,*from))
526                         {
527                         q= *from;
528                         from++;
529                         while (!IS_EOF(conf,*from) && (*from != q))
530                                 {
531                                 if (IS_ESC(conf,*from))
532                                         {
533                                         from++;
534                                         if (IS_EOF(conf,*from)) break;
535                                         }
536                                 buf->data[to++]= *(from++);
537                                 }
538                         if (*from == q) from++;
539                         }
540                 else if (IS_DQUOTE(conf,*from))
541                         {
542                         q= *from;
543                         from++;
544                         while (!IS_EOF(conf,*from))
545                                 {
546                                 if (*from == q)
547                                         {
548                                         if (*(from+1) == q)
549                                                 {
550                                                 from++;
551                                                 }
552                                         else
553                                                 {
554                                                 break;
555                                                 }
556                                         }
557                                 buf->data[to++]= *(from++);
558                                 }
559                         if (*from == q) from++;
560                         }
561                 else if (IS_ESC(conf,*from))
562                         {
563                         from++;
564                         v= *(from++);
565                         if (IS_EOF(conf,v)) break;
566                         else if (v == 'r') v='\r';
567                         else if (v == 'n') v='\n';
568                         else if (v == 'b') v='\b';
569                         else if (v == 't') v='\t';
570                         buf->data[to++]= v;
571                         }
572                 else if (IS_EOF(conf,*from))
573                         break;
574                 else if (*from == '$')
575                         {
576                         /* try to expand it */
577                         rrp=NULL;
578                         s= &(from[1]);
579                         if (*s == '{')
580                                 q='}';
581                         else if (*s == '(')
582                                 q=')';
583                         else q=0;
584
585                         if (q) s++;
586                         cp=section;
587                         e=np=s;
588                         while (IS_ALPHA_NUMERIC(conf,*e))
589                                 e++;
590                         if ((e[0] == ':') && (e[1] == ':'))
591                                 {
592                                 cp=np;
593                                 rrp=e;
594                                 rr= *e;
595                                 *rrp='\0';
596                                 e+=2;
597                                 np=e;
598                                 while (IS_ALPHA_NUMERIC(conf,*e))
599                                         e++;
600                                 }
601                         r= *e;
602                         *e='\0';
603                         rp=e;
604                         if (q)
605                                 {
606                                 if (r != q)
607                                         {
608                                         CONFerr(CONF_F_STR_COPY,CONF_R_NO_CLOSE_BRACE);
609                                         goto err;
610                                         }
611                                 e++;
612                                 }
613                         /* So at this point we have
614                          * ns which is the start of the name string which is
615                          *   '\0' terminated. 
616                          * cs which is the start of the section string which is
617                          *   '\0' terminated.
618                          * e is the 'next point after'.
619                          * r and s are the chars replaced by the '\0'
620                          * rp and sp is where 'r' and 's' came from.
621                          */
622                         p=_CONF_get_string(conf,cp,np);
623                         if (rrp != NULL) *rrp=rr;
624                         *rp=r;
625                         if (p == NULL)
626                                 {
627                                 CONFerr(CONF_F_STR_COPY,CONF_R_VARIABLE_HAS_NO_VALUE);
628                                 goto err;
629                                 }
630                         BUF_MEM_grow(buf,(strlen(p)+len-(e-from)));
631                         while (*p)
632                                 buf->data[to++]= *(p++);
633                         from=e;
634                         }
635                 else
636                         buf->data[to++]= *(from++);
637                 }
638         buf->data[to]='\0';
639         if (*pto != NULL) OPENSSL_free(*pto);
640         *pto=buf->data;
641         OPENSSL_free(buf);
642         return(1);
643 err:
644         if (buf != NULL) BUF_MEM_free(buf);
645         return(0);
646         }
647
648 static char *eat_ws(CONF *conf, char *p)
649         {
650         while (IS_WS(conf,*p) && (!IS_EOF(conf,*p)))
651                 p++;
652         return(p);
653         }
654
655 static char *eat_alpha_numeric(CONF *conf, char *p)
656         {
657         for (;;)
658                 {
659                 if (IS_ESC(conf,*p))
660                         {
661                         p=scan_esc(conf,p);
662                         continue;
663                         }
664                 if (!IS_ALPHA_NUMERIC_PUNCT(conf,*p))
665                         return(p);
666                 p++;
667                 }
668         }
669
670 static char *scan_quote(CONF *conf, char *p)
671         {
672         int q= *p;
673
674         p++;
675         while (!(IS_EOF(conf,*p)) && (*p != q))
676                 {
677                 if (IS_ESC(conf,*p))
678                         {
679                         p++;
680                         if (IS_EOF(conf,*p)) return(p);
681                         }
682                 p++;
683                 }
684         if (*p == q) p++;
685         return(p);
686         }
687
688
689 static char *scan_dquote(CONF *conf, char *p)
690         {
691         int q= *p;
692
693         p++;
694         while (!(IS_EOF(conf,*p)))
695                 {
696                 if (*p == q)
697                         {
698                         if (*(p+1) == q)
699                                 {
700                                 p++;
701                                 }
702                         else
703                                 {
704                                 break;
705                                 }
706                         }
707                 p++;
708                 }
709         if (*p == q) p++;
710         return(p);
711         }
712
713 static void dump_value(CONF_VALUE *a, BIO *out)
714         {
715         if (a->name)
716                 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
717         else
718                 BIO_printf(out, "[[%s]]\n", a->section);
719         }
720
721 static IMPLEMENT_LHASH_DOALL_ARG_FN(dump_value, CONF_VALUE *, BIO *)
722
723 static int def_dump(const CONF *conf, BIO *out)
724         {
725         lh_doall_arg(conf->data, LHASH_DOALL_ARG_FN(dump_value), out);
726         return 1;
727         }
728
729 static int def_is_number(const CONF *conf, char c)
730         {
731         return IS_NUMBER(conf,c);
732         }
733
734 static int def_to_int(const CONF *conf, char c)
735         {
736         return c - '0';
737         }
738