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