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