Avoid leak in error path of asn1_parse2
[openssl.git] / crypto / asn1 / asn1_par.c
1 /*
2  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/buffer.h>
13 #include <openssl/objects.h>
14 #include <openssl/asn1.h>
15
16 #ifndef ASN1_PARSE_MAXDEPTH
17 #define ASN1_PARSE_MAXDEPTH 128
18 #endif
19
20 static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
21                        int offset, int depth, int indent, int dump);
22 static int asn1_print_info(BIO *bp, long offset, int depth, int hl, long len,
23                            int tag, int xclass, int constructed, int indent)
24 {
25     char str[128];
26     const char *p;
27     int pop_f_prefix = 0;
28     long saved_indent = -1;
29     int i = 0;
30
31     if (constructed & V_ASN1_CONSTRUCTED)
32         p = "cons: ";
33     else
34         p = "prim: ";
35     if (constructed != (V_ASN1_CONSTRUCTED | 1)) {
36         if (BIO_snprintf(str, sizeof(str), "%5ld:d=%-2d hl=%ld l=%4ld %s",
37                          offset, depth, (long)hl, len, p) <= 0)
38             goto err;
39     } else {
40         if (BIO_snprintf(str, sizeof(str), "%5ld:d=%-2d hl=%ld l=inf  %s",
41                          offset, depth, (long)hl, p) <= 0)
42             goto err;
43     }
44     if (BIO_set_prefix(bp, str) <= 0) {
45         if ((bp = BIO_push(BIO_new(BIO_f_prefix()), bp)) == NULL)
46             goto err;
47         pop_f_prefix = 1;
48     }
49     saved_indent = BIO_get_indent(bp);
50     if (BIO_set_prefix(bp, str) <= 0
51         || BIO_set_indent(bp, indent) < 0)
52         goto err;
53
54     /*
55      * BIO_set_prefix made a copy of |str|, so we can safely use it for
56      * something else, ASN.1 tag printout.
57      */
58     p = str;
59     if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
60         BIO_snprintf(str, sizeof(str), "priv [ %d ] ", tag);
61     else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
62         BIO_snprintf(str, sizeof(str), "cont [ %d ]", tag);
63     else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
64         BIO_snprintf(str, sizeof(str), "appl [ %d ]", tag);
65     else if (tag > 30)
66         BIO_snprintf(str, sizeof(str), "<ASN1 %d>", tag);
67     else
68         p = ASN1_tag2str(tag);
69
70     i = (BIO_printf(bp, "%-18s", p) > 0);
71  err:
72     if (saved_indent >= 0)
73         BIO_set_indent(bp, saved_indent);
74     if (pop_f_prefix) {
75         BIO *next = BIO_pop(bp);
76
77         BIO_free(bp);
78         bp = next;
79     }
80     return i;
81 }
82
83 int ASN1_parse(BIO *bp, const unsigned char *pp, long len, int indent)
84 {
85     return asn1_parse2(bp, &pp, len, 0, 0, indent, 0);
86 }
87
88 int ASN1_parse_dump(BIO *bp, const unsigned char *pp, long len, int indent,
89                     int dump)
90 {
91     return asn1_parse2(bp, &pp, len, 0, 0, indent, dump);
92 }
93
94 static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
95                        int offset, int depth, int indent, int dump)
96 {
97     const unsigned char *p, *ep, *tot, *op, *opp;
98     long len;
99     int tag, xclass, ret = 0;
100     int nl, hl, j, r;
101     ASN1_OBJECT *o = NULL;
102     ASN1_OCTET_STRING *os = NULL;
103     ASN1_INTEGER *ai = NULL;
104     ASN1_ENUMERATED *ae = NULL;
105     /* ASN1_BMPSTRING *bmp=NULL; */
106     int dump_indent, dump_cont = 0;
107
108     if (depth > ASN1_PARSE_MAXDEPTH) {
109         BIO_puts(bp, "BAD RECURSION DEPTH\n");
110         return 0;
111     }
112
113     dump_indent = 6;            /* Because we know BIO_dump_indent() */
114     p = *pp;
115     tot = p + length;
116     while (length > 0) {
117         op = p;
118         j = ASN1_get_object(&p, &len, &tag, &xclass, length);
119         if (j & 0x80) {
120             if (BIO_write(bp, "Error in encoding\n", 18) <= 0)
121                 goto end;
122             ret = 0;
123             goto end;
124         }
125         hl = (p - op);
126         length -= hl;
127         /*
128          * if j == 0x21 it is a constructed indefinite length object
129          */
130         if (!asn1_print_info(bp, (long)offset + (long)(op - *pp), depth,
131                              hl, len, tag, xclass, j, (indent) ? depth : 0))
132             goto end;
133         if (j & V_ASN1_CONSTRUCTED) {
134             const unsigned char *sp = p;
135
136             ep = p + len;
137             if (BIO_write(bp, "\n", 1) <= 0)
138                 goto end;
139             if (len > length) {
140                 BIO_printf(bp, "length is greater than %ld\n", length);
141                 ret = 0;
142                 goto end;
143             }
144             if ((j == 0x21) && (len == 0)) {
145                 for (;;) {
146                     r = asn1_parse2(bp, &p, (long)(tot - p),
147                                     offset + (p - *pp), depth + 1,
148                                     indent, dump);
149                     if (r == 0) {
150                         ret = 0;
151                         goto end;
152                     }
153                     if ((r == 2) || (p >= tot)) {
154                         len = p - sp;
155                         break;
156                     }
157                 }
158             } else {
159                 long tmp = len;
160
161                 while (p < ep) {
162                     sp = p;
163                     r = asn1_parse2(bp, &p, tmp,
164                                     offset + (p - *pp), depth + 1,
165                                     indent, dump);
166                     if (r == 0) {
167                         ret = 0;
168                         goto end;
169                     }
170                     tmp -= p - sp;
171                 }
172             }
173         } else if (xclass != 0) {
174             p += len;
175             if (BIO_write(bp, "\n", 1) <= 0)
176                 goto end;
177         } else {
178             nl = 0;
179             if ((tag == V_ASN1_PRINTABLESTRING) ||
180                 (tag == V_ASN1_T61STRING) ||
181                 (tag == V_ASN1_IA5STRING) ||
182                 (tag == V_ASN1_VISIBLESTRING) ||
183                 (tag == V_ASN1_NUMERICSTRING) ||
184                 (tag == V_ASN1_UTF8STRING) ||
185                 (tag == V_ASN1_UTCTIME) || (tag == V_ASN1_GENERALIZEDTIME)) {
186                 if (BIO_write(bp, ":", 1) <= 0)
187                     goto end;
188                 if ((len > 0) && BIO_write(bp, (const char *)p, (int)len)
189                     != (int)len)
190                     goto end;
191             } else if (tag == V_ASN1_OBJECT) {
192                 opp = op;
193                 if (d2i_ASN1_OBJECT(&o, &opp, len + hl) != NULL) {
194                     if (BIO_write(bp, ":", 1) <= 0)
195                         goto end;
196                     i2a_ASN1_OBJECT(bp, o);
197                 } else {
198                     if (BIO_puts(bp, ":BAD OBJECT") <= 0)
199                         goto end;
200                     dump_cont = 1;
201                 }
202             } else if (tag == V_ASN1_BOOLEAN) {
203                 if (len != 1) {
204                     if (BIO_puts(bp, ":BAD BOOLEAN") <= 0)
205                         goto end;
206                     dump_cont = 1;
207                 }
208                 if (len > 0)
209                     BIO_printf(bp, ":%u", p[0]);
210             } else if (tag == V_ASN1_BMPSTRING) {
211                 /* do the BMP thang */
212             } else if (tag == V_ASN1_OCTET_STRING) {
213                 int i, printable = 1;
214
215                 opp = op;
216                 os = d2i_ASN1_OCTET_STRING(NULL, &opp, len + hl);
217                 if (os != NULL && os->length > 0) {
218                     opp = os->data;
219                     /*
220                      * testing whether the octet string is printable
221                      */
222                     for (i = 0; i < os->length; i++) {
223                         if (((opp[i] < ' ') &&
224                              (opp[i] != '\n') &&
225                              (opp[i] != '\r') &&
226                              (opp[i] != '\t')) || (opp[i] > '~')) {
227                             printable = 0;
228                             break;
229                         }
230                     }
231                     if (printable)
232                         /* printable string */
233                     {
234                         if (BIO_write(bp, ":", 1) <= 0)
235                             goto end;
236                         if (BIO_write(bp, (const char *)opp, os->length) <= 0)
237                             goto end;
238                     } else if (!dump)
239                         /*
240                          * not printable => print octet string as hex dump
241                          */
242                     {
243                         if (BIO_write(bp, "[HEX DUMP]:", 11) <= 0)
244                             goto end;
245                         for (i = 0; i < os->length; i++) {
246                             if (BIO_printf(bp, "%02X", opp[i]) <= 0)
247                                 goto end;
248                         }
249                     } else
250                         /* print the normal dump */
251                     {
252                         if (!nl) {
253                             if (BIO_write(bp, "\n", 1) <= 0)
254                                 goto end;
255                         }
256                         if (BIO_dump_indent(bp,
257                                             (const char *)opp,
258                                             ((dump == -1 || dump >
259                                               os->
260                                               length) ? os->length : dump),
261                                             dump_indent) <= 0)
262                             goto end;
263                         nl = 1;
264                     }
265                 }
266                 ASN1_OCTET_STRING_free(os);
267                 os = NULL;
268             } else if (tag == V_ASN1_INTEGER) {
269                 int i;
270
271                 opp = op;
272                 ai = d2i_ASN1_INTEGER(NULL, &opp, len + hl);
273                 if (ai != NULL) {
274                     if (BIO_write(bp, ":", 1) <= 0)
275                         goto end;
276                     if (ai->type == V_ASN1_NEG_INTEGER)
277                         if (BIO_write(bp, "-", 1) <= 0)
278                             goto end;
279                     for (i = 0; i < ai->length; i++) {
280                         if (BIO_printf(bp, "%02X", ai->data[i]) <= 0)
281                             goto end;
282                     }
283                     if (ai->length == 0) {
284                         if (BIO_write(bp, "00", 2) <= 0)
285                             goto end;
286                     }
287                 } else {
288                     if (BIO_puts(bp, ":BAD INTEGER") <= 0)
289                         goto end;
290                     dump_cont = 1;
291                 }
292                 ASN1_INTEGER_free(ai);
293                 ai = NULL;
294             } else if (tag == V_ASN1_ENUMERATED) {
295                 int i;
296
297                 opp = op;
298                 ae = d2i_ASN1_ENUMERATED(NULL, &opp, len + hl);
299                 if (ae != NULL) {
300                     if (BIO_write(bp, ":", 1) <= 0)
301                         goto end;
302                     if (ae->type == V_ASN1_NEG_ENUMERATED)
303                         if (BIO_write(bp, "-", 1) <= 0)
304                             goto end;
305                     for (i = 0; i < ae->length; i++) {
306                         if (BIO_printf(bp, "%02X", ae->data[i]) <= 0)
307                             goto end;
308                     }
309                     if (ae->length == 0) {
310                         if (BIO_write(bp, "00", 2) <= 0)
311                             goto end;
312                     }
313                 } else {
314                     if (BIO_puts(bp, ":BAD ENUMERATED") <= 0)
315                         goto end;
316                     dump_cont = 1;
317                 }
318                 ASN1_ENUMERATED_free(ae);
319                 ae = NULL;
320             } else if (len > 0 && dump) {
321                 if (!nl) {
322                     if (BIO_write(bp, "\n", 1) <= 0)
323                         goto end;
324                 }
325                 if (BIO_dump_indent(bp, (const char *)p,
326                                     ((dump == -1 || dump > len) ? len : dump),
327                                     dump_indent) <= 0)
328                     goto end;
329                 nl = 1;
330             }
331             if (dump_cont) {
332                 int i;
333                 const unsigned char *tmp = op + hl;
334                 if (BIO_puts(bp, ":[") <= 0)
335                     goto end;
336                 for (i = 0; i < len; i++) {
337                     if (BIO_printf(bp, "%02X", tmp[i]) <= 0)
338                         goto end;
339                 }
340                 if (BIO_puts(bp, "]") <= 0)
341                     goto end;
342             }
343
344             if (!nl) {
345                 if (BIO_write(bp, "\n", 1) <= 0)
346                     goto end;
347             }
348             p += len;
349             if ((tag == V_ASN1_EOC) && (xclass == 0)) {
350                 ret = 2;        /* End of sequence */
351                 goto end;
352             }
353         }
354         length -= len;
355     }
356     ret = 1;
357  end:
358     ASN1_OBJECT_free(o);
359     ASN1_OCTET_STRING_free(os);
360     ASN1_INTEGER_free(ai);
361     ASN1_ENUMERATED_free(ae);
362     *pp = p;
363     return ret;
364 }
365
366 const char *ASN1_tag2str(int tag)
367 {
368     static const char *const tag2str[] = {
369         /* 0-4 */
370         "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
371         /* 5-9 */
372         "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL",
373         /* 10-13 */
374         "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>",
375         /* 15-17 */
376         "<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET",
377         /* 18-20 */
378         "NUMERICSTRING", "PRINTABLESTRING", "T61STRING",
379         /* 21-24 */
380         "VIDEOTEXSTRING", "IA5STRING", "UTCTIME", "GENERALIZEDTIME",
381         /* 25-27 */
382         "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING",
383         /* 28-30 */
384         "UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING"
385     };
386
387     if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
388         tag &= ~0x100;
389
390     if (tag < 0 || tag > 30)
391         return "(unknown)";
392     return tag2str[tag];
393 }