xref: /openbsd-src/lib/libcrypto/asn1/asn1_par.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /* $OpenBSD: asn1_par.c,v 1.20 2014/07/12 16:03:36 miod Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <stdio.h>
60 
61 #include <openssl/asn1.h>
62 #include <openssl/buffer.h>
63 #include <openssl/objects.h>
64 
65 static int asn1_print_info(BIO *bp, int tag, int xclass, int constructed,
66     int indent);
67 static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
68     int offset, int depth, int indent, int dump);
69 
70 static int
71 asn1_print_info(BIO *bp, int tag, int xclass, int constructed,
72     int indent)
73 {
74 	static const char fmt[] = "%-18s";
75 	char str[128];
76 	const char *p;
77 
78 	if (constructed & V_ASN1_CONSTRUCTED)
79 		p="cons: ";
80 	else
81 		p="prim: ";
82 	if (BIO_write(bp, p, 6) < 6)
83 		goto err;
84 	BIO_indent(bp, indent, 128);
85 
86 	p = str;
87 	if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
88 		snprintf(str, sizeof str, "priv [ %d ] ", tag);
89 	else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
90 		snprintf(str, sizeof str, "cont [ %d ]", tag);
91 	else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
92 		snprintf(str, sizeof str, "appl [ %d ]", tag);
93 	else if (tag > 30)
94 		snprintf(str, sizeof str, "<ASN1 %d>", tag);
95 	else
96 		p = ASN1_tag2str(tag);
97 
98 	if (BIO_printf(bp, fmt, p) <= 0)
99 		goto err;
100 	return (1);
101 err:
102 	return (0);
103 }
104 
105 int
106 ASN1_parse(BIO *bp, const unsigned char *pp, long len, int indent)
107 {
108 	return (asn1_parse2(bp, &pp, len, 0, 0, indent, 0));
109 }
110 
111 int
112 ASN1_parse_dump(BIO *bp, const unsigned char *pp, long len, int indent, int dump)
113 {
114 	return (asn1_parse2(bp, &pp, len, 0, 0, indent, dump));
115 }
116 
117 static int
118 asn1_parse2(BIO *bp, const unsigned char **pp, long length, int offset,
119     int depth, int indent, int dump)
120 {
121 	const unsigned char *p, *ep, *tot, *op, *opp;
122 	long len;
123 	int tag, xclass, ret = 0;
124 	int nl, hl, j, r;
125 	ASN1_OBJECT *o = NULL;
126 	ASN1_OCTET_STRING *os = NULL;
127 	/* ASN1_BMPSTRING *bmp=NULL;*/
128 	int dump_indent;
129 
130 #if 0
131 	dump_indent = indent;
132 #else
133 	dump_indent = 6;	/* Because we know BIO_dump_indent() */
134 #endif
135 	p = *pp;
136 	tot = p + length;
137 	op = p - 1;
138 	while ((p < tot) && (op < p)) {
139 		op = p;
140 		j = ASN1_get_object(&p, &len, &tag, &xclass, length);
141 
142 		if (j & 0x80) {
143 			if (BIO_write(bp, "Error in encoding\n", 18) <= 0)
144 				goto end;
145 			ret = 0;
146 			goto end;
147 		}
148 		hl = (p - op);
149 		length -= hl;
150 		/* if j == 0x21 it is a constructed indefinite length object */
151 		if (BIO_printf(bp, "%5ld:", (long)offset +
152 		    (long)(op - *pp)) <= 0)
153 		    goto end;
154 
155 		if (j != (V_ASN1_CONSTRUCTED | 1)) {
156 			if (BIO_printf(bp, "d=%-2d hl=%ld l=%4ld ",
157 			    depth, (long)hl, len) <= 0)
158 				goto end;
159 		} else {
160 			if (BIO_printf(bp, "d=%-2d hl=%ld l=inf  ",
161 			    depth, (long)hl) <= 0)
162 				goto end;
163 		}
164 		if (!asn1_print_info(bp, tag, xclass, j, (indent) ? depth : 0))
165 			goto end;
166 		if (j & V_ASN1_CONSTRUCTED) {
167 			ep = p + len;
168 			if (BIO_write(bp, "\n", 1) <= 0)
169 				goto end;
170 			if (len > length) {
171 				BIO_printf(bp, "length is greater than %ld\n",
172 				    length);
173 				ret = 0;
174 				goto end;
175 			}
176 			if ((j == 0x21) && (len == 0)) {
177 				for (;;) {
178 					r = asn1_parse2(bp, &p, (long)(tot - p),
179 					    offset + (p - *pp), depth + 1,
180 					    indent, dump);
181 					if (r == 0) {
182 						ret = 0;
183 						goto end;
184 					}
185 					if ((r == 2) || (p >= tot))
186 						break;
187 				}
188 			} else
189 				while (p < ep) {
190 					r = asn1_parse2(bp, &p, (long)len,
191 					    offset + (p - *pp), depth + 1,
192 					    indent, dump);
193 					if (r == 0) {
194 						ret = 0;
195 						goto end;
196 					}
197 				}
198 		} else if (xclass != 0) {
199 			p += len;
200 			if (BIO_write(bp, "\n", 1) <= 0)
201 				goto end;
202 		} else {
203 			nl = 0;
204 			if ((tag == V_ASN1_PRINTABLESTRING) ||
205 			    (tag == V_ASN1_T61STRING) ||
206 			    (tag == V_ASN1_IA5STRING) ||
207 			    (tag == V_ASN1_VISIBLESTRING) ||
208 			    (tag == V_ASN1_NUMERICSTRING) ||
209 			    (tag == V_ASN1_UTF8STRING) ||
210 			    (tag == V_ASN1_UTCTIME) ||
211 			    (tag == V_ASN1_GENERALIZEDTIME)) {
212 				if (BIO_write(bp, ":", 1) <= 0)
213 					goto end;
214 				if ((len > 0) &&
215 				    BIO_write(bp, (const char *)p, (int)len) !=
216 				    (int)len)
217 					goto end;
218 			} else if (tag == V_ASN1_OBJECT) {
219 				opp = op;
220 				if (d2i_ASN1_OBJECT(&o, &opp, len + hl) !=
221 				    NULL) {
222 					if (BIO_write(bp, ":", 1) <= 0)
223 						goto end;
224 					i2a_ASN1_OBJECT(bp, o);
225 				} else {
226 					if (BIO_write(bp, ":BAD OBJECT",
227 					    11) <= 0)
228 						goto end;
229 				}
230 			} else if (tag == V_ASN1_BOOLEAN) {
231 				int ii;
232 
233 				opp = op;
234 				ii = d2i_ASN1_BOOLEAN(NULL, &opp, len + hl);
235 				if (ii < 0) {
236 					if (BIO_write(bp, "Bad boolean\n",
237 					    12) <= 0)
238 						goto end;
239 				}
240 				BIO_printf(bp, ":%d", ii);
241 			} else if (tag == V_ASN1_BMPSTRING) {
242 				/* do the BMP thang */
243 			} else if (tag == V_ASN1_OCTET_STRING) {
244 				int i, printable = 1;
245 
246 				opp = op;
247 				os = d2i_ASN1_OCTET_STRING(NULL, &opp, len + hl);
248 				if (os != NULL && os->length > 0) {
249 					opp = os->data;
250 					/* testing whether the octet string is
251 					 * printable */
252 					for (i = 0; i < os->length; i++) {
253 						if (((opp[i] < ' ') &&
254 						    (opp[i] != '\n') &&
255 						    (opp[i] != '\r') &&
256 						    (opp[i] != '\t')) ||
257 						    (opp[i] > '~')) {
258 							printable = 0;
259 							break;
260 						}
261 					}
262 					if (printable) {
263 						/* printable string */
264 						if (BIO_write(bp, ":", 1) <= 0)
265 							goto end;
266 						if (BIO_write(bp, (const char *)opp,
267 							    os->length) <= 0)
268 							goto end;
269 					} else if (!dump) {
270 						/* not printable => print octet string
271 						 * as hex dump */
272 						if (BIO_write(bp, "[HEX DUMP]:", 11) <= 0)
273 							goto end;
274 						for (i = 0; i < os->length; i++) {
275 							if (BIO_printf(bp,
276 							    "%02X", opp[i]) <= 0)
277 								goto end;
278 						}
279 					} else {
280 						/* print the normal dump */
281 						if (!nl) {
282 							if (BIO_write(bp, "\n", 1) <= 0)
283 								goto end;
284 						}
285 						if (BIO_dump_indent(bp,
286 						    (const char *)opp,
287 						    ((dump == -1 || dump >
288 						    os->length) ? os->length : dump),
289 						    dump_indent) <= 0)
290 							goto end;
291 						nl = 1;
292 					}
293 				}
294 				M_ASN1_OCTET_STRING_free(os);
295 				os = NULL;
296 			} else if (tag == V_ASN1_INTEGER) {
297 				ASN1_INTEGER *bs;
298 				int i;
299 
300 				opp = op;
301 				bs = d2i_ASN1_INTEGER(NULL, &opp, len + hl);
302 				if (bs != NULL) {
303 					if (BIO_write(bp, ":", 1) <= 0)
304 						goto end;
305 					if (bs->type == V_ASN1_NEG_INTEGER)
306 						if (BIO_write(bp, "-", 1) <= 0)
307 							goto end;
308 					for (i = 0; i < bs->length; i++) {
309 						if (BIO_printf(bp, "%02X",
310 						    bs->data[i]) <= 0)
311 							goto end;
312 					}
313 					if (bs->length == 0) {
314 						if (BIO_write(bp, "00", 2) <= 0)
315 							goto end;
316 					}
317 				} else {
318 					if (BIO_write(bp, "BAD INTEGER", 11) <= 0)
319 						goto end;
320 				}
321 				M_ASN1_INTEGER_free(bs);
322 			} else if (tag == V_ASN1_ENUMERATED) {
323 				ASN1_ENUMERATED *bs;
324 				int i;
325 
326 				opp = op;
327 				bs = d2i_ASN1_ENUMERATED(NULL, &opp, len + hl);
328 				if (bs != NULL) {
329 					if (BIO_write(bp, ":", 1) <= 0)
330 						goto end;
331 					if (bs->type == V_ASN1_NEG_ENUMERATED)
332 						if (BIO_write(bp, "-", 1) <= 0)
333 							goto end;
334 					for (i = 0; i < bs->length; i++) {
335 						if (BIO_printf(bp, "%02X",
336 						    bs->data[i]) <= 0)
337 							goto end;
338 					}
339 					if (bs->length == 0) {
340 						if (BIO_write(bp, "00", 2) <= 0)
341 							goto end;
342 					}
343 				} else {
344 					if (BIO_write(bp, "BAD ENUMERATED", 14) <= 0)
345 						goto end;
346 				}
347 				M_ASN1_ENUMERATED_free(bs);
348 			} else if (len > 0 && dump) {
349 				if (!nl) {
350 					if (BIO_write(bp, "\n", 1) <= 0)
351 						goto end;
352 				}
353 				if (BIO_dump_indent(bp, (const char *)p,
354 				    ((dump == -1 || dump > len) ? len : dump),
355 				    dump_indent) <= 0)
356 					goto end;
357 				nl = 1;
358 			}
359 
360 			if (!nl) {
361 				if (BIO_write(bp, "\n", 1) <= 0)
362 					goto end;
363 			}
364 			p += len;
365 			if ((tag == V_ASN1_EOC) && (xclass == 0)) {
366 				ret = 2; /* End of sequence */
367 				goto end;
368 			}
369 		}
370 		length -= len;
371 	}
372 	ret = 1;
373 
374 end:
375 	if (o != NULL)
376 		ASN1_OBJECT_free(o);
377 	M_ASN1_OCTET_STRING_free(os);
378 	*pp = p;
379 	return (ret);
380 }
381 
382 const char *
383 ASN1_tag2str(int tag)
384 {
385 	static const char * const tag2str[] = {
386 		"EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING", /* 0-4 */
387 		"NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL", /* 5-9 */
388 		"ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>", 	    /* 10-13 */
389 		"<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET", 		    /* 15-17 */
390 		"NUMERICSTRING", "PRINTABLESTRING", "T61STRING",	    /* 18-20 */
391 		"VIDEOTEXSTRING", "IA5STRING", "UTCTIME", "GENERALIZEDTIME", /* 21-24 */
392 		"GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING",	    /* 25-27 */
393 		"UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING"		    /* 28-30 */
394 	};
395 
396 	if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
397 		tag &= ~0x100;
398 
399 	if (tag < 0 || tag > 30)
400 		return "(unknown)";
401 	return tag2str[tag];
402 }
403