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