Work around Travis "virtual memory exhausted" error
[openssl.git] / crypto / conf / conf_def.c
1 /*
2  * Copyright 1995-2016 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_malloc(strlen(pname) + 1);
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             OPENSSL_strlcpy(v->name, pname, strlen(pname) + 1);
327             if (!str_copy(conf, psection, &(v->value), start))
328                 goto err;
329
330             if (strcmp(psection, section) != 0) {
331                 if ((tv = _CONF_get_section(conf, psection))
332                     == NULL)
333                     tv = _CONF_new_section(conf, psection);
334                 if (tv == NULL) {
335                     CONFerr(CONF_F_DEF_LOAD_BIO,
336                             CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
337                     goto err;
338                 }
339             } else
340                 tv = sv;
341             if (_CONF_add_string(conf, tv, v) == 0) {
342                 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
343                 goto err;
344             }
345             v = NULL;
346         }
347     }
348     BUF_MEM_free(buff);
349     OPENSSL_free(section);
350     return (1);
351  err:
352     BUF_MEM_free(buff);
353     OPENSSL_free(section);
354     if (line != NULL)
355         *line = eline;
356     BIO_snprintf(btmp, sizeof btmp, "%ld", eline);
357     ERR_add_error_data(2, "line ", btmp);
358     if (h != conf->data) {
359         CONF_free(conf->data);
360         conf->data = NULL;
361     }
362     if (v != NULL) {
363         OPENSSL_free(v->name);
364         OPENSSL_free(v->value);
365         OPENSSL_free(v);
366     }
367     return (0);
368 }
369
370 static void clear_comments(CONF *conf, char *p)
371 {
372     for (;;) {
373         if (IS_FCOMMENT(conf, *p)) {
374             *p = '\0';
375             return;
376         }
377         if (!IS_WS(conf, *p)) {
378             break;
379         }
380         p++;
381     }
382
383     for (;;) {
384         if (IS_COMMENT(conf, *p)) {
385             *p = '\0';
386             return;
387         }
388         if (IS_DQUOTE(conf, *p)) {
389             p = scan_dquote(conf, p);
390             continue;
391         }
392         if (IS_QUOTE(conf, *p)) {
393             p = scan_quote(conf, p);
394             continue;
395         }
396         if (IS_ESC(conf, *p)) {
397             p = scan_esc(conf, p);
398             continue;
399         }
400         if (IS_EOF(conf, *p))
401             return;
402         else
403             p++;
404     }
405 }
406
407 static int str_copy(CONF *conf, char *section, char **pto, char *from)
408 {
409     int q, r, rr = 0, to = 0, len = 0;
410     char *s, *e, *rp, *p, *rrp, *np, *cp, v;
411     BUF_MEM *buf;
412
413     if ((buf = BUF_MEM_new()) == NULL)
414         return (0);
415
416     len = strlen(from) + 1;
417     if (!BUF_MEM_grow(buf, len))
418         goto err;
419
420     for (;;) {
421         if (IS_QUOTE(conf, *from)) {
422             q = *from;
423             from++;
424             while (!IS_EOF(conf, *from) && (*from != q)) {
425                 if (IS_ESC(conf, *from)) {
426                     from++;
427                     if (IS_EOF(conf, *from))
428                         break;
429                 }
430                 buf->data[to++] = *(from++);
431             }
432             if (*from == q)
433                 from++;
434         } else if (IS_DQUOTE(conf, *from)) {
435             q = *from;
436             from++;
437             while (!IS_EOF(conf, *from)) {
438                 if (*from == q) {
439                     if (*(from + 1) == q) {
440                         from++;
441                     } else {
442                         break;
443                     }
444                 }
445                 buf->data[to++] = *(from++);
446             }
447             if (*from == q)
448                 from++;
449         } else if (IS_ESC(conf, *from)) {
450             from++;
451             v = *(from++);
452             if (IS_EOF(conf, v))
453                 break;
454             else if (v == 'r')
455                 v = '\r';
456             else if (v == 'n')
457                 v = '\n';
458             else if (v == 'b')
459                 v = '\b';
460             else if (v == 't')
461                 v = '\t';
462             buf->data[to++] = v;
463         } else if (IS_EOF(conf, *from))
464             break;
465         else if (*from == '$') {
466             size_t newsize;
467
468             /* try to expand it */
469             rrp = NULL;
470             s = &(from[1]);
471             if (*s == '{')
472                 q = '}';
473             else if (*s == '(')
474                 q = ')';
475             else
476                 q = 0;
477
478             if (q)
479                 s++;
480             cp = section;
481             e = np = s;
482             while (IS_ALPHA_NUMERIC(conf, *e))
483                 e++;
484             if ((e[0] == ':') && (e[1] == ':')) {
485                 cp = np;
486                 rrp = e;
487                 rr = *e;
488                 *rrp = '\0';
489                 e += 2;
490                 np = e;
491                 while (IS_ALPHA_NUMERIC(conf, *e))
492                     e++;
493             }
494             r = *e;
495             *e = '\0';
496             rp = e;
497             if (q) {
498                 if (r != q) {
499                     CONFerr(CONF_F_STR_COPY, CONF_R_NO_CLOSE_BRACE);
500                     goto err;
501                 }
502                 e++;
503             }
504             /*-
505              * So at this point we have
506              * np which is the start of the name string which is
507              *   '\0' terminated.
508              * cp which is the start of the section string which is
509              *   '\0' terminated.
510              * e is the 'next point after'.
511              * r and rr are the chars replaced by the '\0'
512              * rp and rrp is where 'r' and 'rr' came from.
513              */
514             p = _CONF_get_string(conf, cp, np);
515             if (rrp != NULL)
516                 *rrp = rr;
517             *rp = r;
518             if (p == NULL) {
519                 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_HAS_NO_VALUE);
520                 goto err;
521             }
522             newsize = strlen(p) + buf->length - (e - from);
523             if (newsize > MAX_CONF_VALUE_LENGTH) {
524                 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
525                 goto err;
526             }
527             if (!BUF_MEM_grow_clean(buf, newsize)) {
528                 CONFerr(CONF_F_STR_COPY, ERR_R_MALLOC_FAILURE);
529                 goto err;
530             }
531             while (*p)
532                 buf->data[to++] = *(p++);
533
534             /*
535              * Since we change the pointer 'from', we also have to change the
536              * perceived length of the string it points at.  /RL
537              */
538             len -= e - from;
539             from = e;
540
541             /*
542              * In case there were no braces or parenthesis around the
543              * variable reference, we have to put back the character that was
544              * replaced with a '\0'.  /RL
545              */
546             *rp = r;
547         } else
548             buf->data[to++] = *(from++);
549     }
550     buf->data[to] = '\0';
551     OPENSSL_free(*pto);
552     *pto = buf->data;
553     OPENSSL_free(buf);
554     return (1);
555  err:
556     BUF_MEM_free(buf);
557     return (0);
558 }
559
560 static char *eat_ws(CONF *conf, char *p)
561 {
562     while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
563         p++;
564     return (p);
565 }
566
567 static char *eat_alpha_numeric(CONF *conf, char *p)
568 {
569     for (;;) {
570         if (IS_ESC(conf, *p)) {
571             p = scan_esc(conf, p);
572             continue;
573         }
574         if (!IS_ALPHA_NUMERIC_PUNCT(conf, *p))
575             return (p);
576         p++;
577     }
578 }
579
580 static char *scan_quote(CONF *conf, char *p)
581 {
582     int q = *p;
583
584     p++;
585     while (!(IS_EOF(conf, *p)) && (*p != q)) {
586         if (IS_ESC(conf, *p)) {
587             p++;
588             if (IS_EOF(conf, *p))
589                 return (p);
590         }
591         p++;
592     }
593     if (*p == q)
594         p++;
595     return (p);
596 }
597
598 static char *scan_dquote(CONF *conf, char *p)
599 {
600     int q = *p;
601
602     p++;
603     while (!(IS_EOF(conf, *p))) {
604         if (*p == q) {
605             if (*(p + 1) == q) {
606                 p++;
607             } else {
608                 break;
609             }
610         }
611         p++;
612     }
613     if (*p == q)
614         p++;
615     return (p);
616 }
617
618 static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
619 {
620     if (a->name)
621         BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
622     else
623         BIO_printf(out, "[[%s]]\n", a->section);
624 }
625
626 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
627
628 static int def_dump(const CONF *conf, BIO *out)
629 {
630     lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
631     return 1;
632 }
633
634 static int def_is_number(const CONF *conf, char c)
635 {
636     return IS_NUMBER(conf, c);
637 }
638
639 static int def_to_int(const CONF *conf, char c)
640 {
641     return c - '0';
642 }