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