]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/asn1/asn1_par.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
59 #include "internal/cryptlib.h"
60 #include <openssl/buffer.h>
61 #include <openssl/objects.h>
62 #include <openssl/asn1.h>
64 #ifndef ASN1_PARSE_MAXDEPTH
65 #define ASN1_PARSE_MAXDEPTH 128
68 static int asn1_print_info(BIO
*bp
, int tag
, int xclass
, int constructed
,
70 static int asn1_parse2(BIO
*bp
, const unsigned char **pp
, long length
,
71 int offset
, int depth
, int indent
, int dump
);
72 static int asn1_print_info(BIO
*bp
, int tag
, int xclass
, int constructed
,
75 static const char fmt
[] = "%-18s";
79 if (constructed
& V_ASN1_CONSTRUCTED
)
83 if (BIO_write(bp
, p
, 6) < 6)
85 BIO_indent(bp
, indent
, 128);
88 if ((xclass
& V_ASN1_PRIVATE
) == V_ASN1_PRIVATE
)
89 BIO_snprintf(str
, sizeof str
, "priv [ %d ] ", tag
);
90 else if ((xclass
& V_ASN1_CONTEXT_SPECIFIC
) == V_ASN1_CONTEXT_SPECIFIC
)
91 BIO_snprintf(str
, sizeof str
, "cont [ %d ]", tag
);
92 else if ((xclass
& V_ASN1_APPLICATION
) == V_ASN1_APPLICATION
)
93 BIO_snprintf(str
, sizeof str
, "appl [ %d ]", tag
);
95 BIO_snprintf(str
, sizeof str
, "<ASN1 %d>", tag
);
97 p
= ASN1_tag2str(tag
);
99 if (BIO_printf(bp
, fmt
, p
) <= 0)
106 int ASN1_parse(BIO
*bp
, const unsigned char *pp
, long len
, int indent
)
108 return (asn1_parse2(bp
, &pp
, len
, 0, 0, indent
, 0));
111 int ASN1_parse_dump(BIO
*bp
, const unsigned char *pp
, long len
, int indent
,
114 return (asn1_parse2(bp
, &pp
, len
, 0, 0, indent
, dump
));
117 static int asn1_parse2(BIO
*bp
, const unsigned char **pp
, long length
,
118 int offset
, int depth
, int indent
, int dump
)
120 const unsigned char *p
, *ep
, *tot
, *op
, *opp
;
122 int tag
, xclass
, ret
= 0;
124 ASN1_OBJECT
*o
= NULL
;
125 ASN1_OCTET_STRING
*os
= NULL
;
126 /* ASN1_BMPSTRING *bmp=NULL; */
127 int dump_indent
, dump_cont
= 0;
129 if (depth
> ASN1_PARSE_MAXDEPTH
) {
130 BIO_puts(bp
, "BAD RECURSION DEPTH\n");
134 dump_indent
= 6; /* Because we know BIO_dump_indent() */
138 while ((p
< tot
) && (op
< p
)) {
140 j
= ASN1_get_object(&p
, &len
, &tag
, &xclass
, length
);
142 if (BIO_write(bp
, "Error in encoding\n", 18) <= 0)
150 * if j == 0x21 it is a constructed indefinite length object
152 if (BIO_printf(bp
, "%5ld:", (long)offset
+ (long)(op
- *pp
))
156 if (j
!= (V_ASN1_CONSTRUCTED
| 1)) {
157 if (BIO_printf(bp
, "d=%-2d hl=%ld l=%4ld ",
158 depth
, (long)hl
, len
) <= 0)
161 if (BIO_printf(bp
, "d=%-2d hl=%ld l=inf ", depth
, (long)hl
) <= 0)
164 if (!asn1_print_info(bp
, tag
, xclass
, j
, (indent
) ? depth
: 0))
166 if (j
& V_ASN1_CONSTRUCTED
) {
167 const unsigned char *sp
= p
;
170 if (BIO_write(bp
, "\n", 1) <= 0)
173 BIO_printf(bp
, "length is greater than %ld\n", length
);
177 if ((j
== 0x21) && (len
== 0)) {
179 r
= asn1_parse2(bp
, &p
, (long)(tot
- p
),
180 offset
+ (p
- *pp
), depth
+ 1,
186 if ((r
== 2) || (p
>= tot
)) {
196 r
= asn1_parse2(bp
, &p
, tmp
,
197 offset
+ (p
- *pp
), depth
+ 1,
206 } else if (xclass
!= 0) {
208 if (BIO_write(bp
, "\n", 1) <= 0)
212 if ((tag
== V_ASN1_PRINTABLESTRING
) ||
213 (tag
== V_ASN1_T61STRING
) ||
214 (tag
== V_ASN1_IA5STRING
) ||
215 (tag
== V_ASN1_VISIBLESTRING
) ||
216 (tag
== V_ASN1_NUMERICSTRING
) ||
217 (tag
== V_ASN1_UTF8STRING
) ||
218 (tag
== V_ASN1_UTCTIME
) || (tag
== V_ASN1_GENERALIZEDTIME
)) {
219 if (BIO_write(bp
, ":", 1) <= 0)
221 if ((len
> 0) && BIO_write(bp
, (const char *)p
, (int)len
)
224 } else if (tag
== V_ASN1_OBJECT
) {
226 if (d2i_ASN1_OBJECT(&o
, &opp
, len
+ hl
) != NULL
) {
227 if (BIO_write(bp
, ":", 1) <= 0)
229 i2a_ASN1_OBJECT(bp
, o
);
231 if (BIO_puts(bp
, ":BAD OBJECT") <= 0)
235 } else if (tag
== V_ASN1_BOOLEAN
) {
237 if (BIO_puts(bp
, ":BAD BOOLEAN") <= 0)
242 BIO_printf(bp
, ":%u", p
[0]);
243 } else if (tag
== V_ASN1_BMPSTRING
) {
244 /* do the BMP thang */
245 } else if (tag
== V_ASN1_OCTET_STRING
) {
246 int i
, printable
= 1;
249 os
= d2i_ASN1_OCTET_STRING(NULL
, &opp
, len
+ hl
);
250 if (os
!= NULL
&& os
->length
> 0) {
253 * testing whether the octet string is printable
255 for (i
= 0; i
< os
->length
; i
++) {
256 if (((opp
[i
] < ' ') &&
259 (opp
[i
] != '\t')) || (opp
[i
] > '~')) {
265 /* printable string */
267 if (BIO_write(bp
, ":", 1) <= 0)
269 if (BIO_write(bp
, (const char *)opp
, os
->length
) <= 0)
273 * not printable => print octet string as hex dump
276 if (BIO_write(bp
, "[HEX DUMP]:", 11) <= 0)
278 for (i
= 0; i
< os
->length
; i
++) {
279 if (BIO_printf(bp
, "%02X", opp
[i
]) <= 0)
283 /* print the normal dump */
286 if (BIO_write(bp
, "\n", 1) <= 0)
289 if (BIO_dump_indent(bp
,
291 ((dump
== -1 || dump
>
293 length
) ? os
->length
: dump
),
299 ASN1_OCTET_STRING_free(os
);
301 } else if (tag
== V_ASN1_INTEGER
) {
306 bs
= d2i_ASN1_INTEGER(NULL
, &opp
, len
+ hl
);
308 if (BIO_write(bp
, ":", 1) <= 0)
310 if (bs
->type
== V_ASN1_NEG_INTEGER
)
311 if (BIO_write(bp
, "-", 1) <= 0)
313 for (i
= 0; i
< bs
->length
; i
++) {
314 if (BIO_printf(bp
, "%02X", bs
->data
[i
]) <= 0)
317 if (bs
->length
== 0) {
318 if (BIO_write(bp
, "00", 2) <= 0)
322 if (BIO_puts(bp
, ":BAD INTEGER") <= 0)
326 ASN1_INTEGER_free(bs
);
327 } else if (tag
== V_ASN1_ENUMERATED
) {
332 bs
= d2i_ASN1_ENUMERATED(NULL
, &opp
, len
+ hl
);
334 if (BIO_write(bp
, ":", 1) <= 0)
336 if (bs
->type
== V_ASN1_NEG_ENUMERATED
)
337 if (BIO_write(bp
, "-", 1) <= 0)
339 for (i
= 0; i
< bs
->length
; i
++) {
340 if (BIO_printf(bp
, "%02X", bs
->data
[i
]) <= 0)
343 if (bs
->length
== 0) {
344 if (BIO_write(bp
, "00", 2) <= 0)
348 if (BIO_puts(bp
, ":BAD ENUMERATED") <= 0)
352 ASN1_ENUMERATED_free(bs
);
353 } else if (len
> 0 && dump
) {
355 if (BIO_write(bp
, "\n", 1) <= 0)
358 if (BIO_dump_indent(bp
, (const char *)p
,
359 ((dump
== -1 || dump
> len
) ? len
: dump
),
366 const unsigned char *tmp
= op
+ hl
;
367 if (BIO_puts(bp
, ":[") <= 0)
369 for (i
= 0; i
< len
; i
++) {
370 if (BIO_printf(bp
, "%02X", tmp
[i
]) <= 0)
373 if (BIO_puts(bp
, "]") <= 0)
378 if (BIO_write(bp
, "\n", 1) <= 0)
382 if ((tag
== V_ASN1_EOC
) && (xclass
== 0)) {
383 ret
= 2; /* End of sequence */
392 ASN1_OCTET_STRING_free(os
);
397 const char *ASN1_tag2str(int tag
)
399 static const char *const tag2str
[] = {
401 "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
403 "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL",
405 "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>",
407 "<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET",
409 "NUMERICSTRING", "PRINTABLESTRING", "T61STRING",
411 "VIDEOTEXSTRING", "IA5STRING", "UTCTIME", "GENERALIZEDTIME",
413 "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING",
415 "UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING"
418 if ((tag
== V_ASN1_NEG_INTEGER
) || (tag
== V_ASN1_NEG_ENUMERATED
))
421 if (tag
< 0 || tag
> 30)