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