]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/asn1/asn1_par.c
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/buffer.h>
13 #include <openssl/objects.h>
14 #include <openssl/asn1.h>
16 #ifndef ASN1_PARSE_MAXDEPTH
17 #define ASN1_PARSE_MAXDEPTH 128
20 static int asn1_print_info(BIO
*bp
, int tag
, int xclass
, int constructed
,
22 static int asn1_parse2(BIO
*bp
, const unsigned char **pp
, long length
,
23 int offset
, int depth
, int indent
, int dump
);
24 static int asn1_print_info(BIO
*bp
, int tag
, int xclass
, int constructed
,
27 static const char fmt
[] = "%-18s";
31 if (constructed
& V_ASN1_CONSTRUCTED
)
35 if (BIO_write(bp
, p
, 6) < 6)
37 BIO_indent(bp
, indent
, 128);
40 if ((xclass
& V_ASN1_PRIVATE
) == V_ASN1_PRIVATE
)
41 sprintf(str
, "priv [ %d ] ", tag
);
42 else if ((xclass
& V_ASN1_CONTEXT_SPECIFIC
) == V_ASN1_CONTEXT_SPECIFIC
)
43 sprintf(str
, "cont [ %d ]", tag
);
44 else if ((xclass
& V_ASN1_APPLICATION
) == V_ASN1_APPLICATION
)
45 sprintf(str
, "appl [ %d ]", tag
);
47 sprintf(str
, "<ASN1 %d>", tag
);
49 p
= ASN1_tag2str(tag
);
51 if (BIO_printf(bp
, fmt
, p
) <= 0)
58 int ASN1_parse(BIO
*bp
, const unsigned char *pp
, long len
, int indent
)
60 return (asn1_parse2(bp
, &pp
, len
, 0, 0, indent
, 0));
63 int ASN1_parse_dump(BIO
*bp
, const unsigned char *pp
, long len
, int indent
,
66 return (asn1_parse2(bp
, &pp
, len
, 0, 0, indent
, dump
));
69 static int asn1_parse2(BIO
*bp
, const unsigned char **pp
, long length
,
70 int offset
, int depth
, int indent
, int dump
)
72 const unsigned char *p
, *ep
, *tot
, *op
, *opp
;
74 int tag
, xclass
, ret
= 0;
76 ASN1_OBJECT
*o
= NULL
;
77 ASN1_OCTET_STRING
*os
= NULL
;
78 /* ASN1_BMPSTRING *bmp=NULL; */
79 int dump_indent
, dump_cont
= 0;
81 if (depth
> ASN1_PARSE_MAXDEPTH
) {
82 BIO_puts(bp
, "BAD RECURSION DEPTH\n");
86 dump_indent
= 6; /* Because we know BIO_dump_indent() */
91 j
= ASN1_get_object(&p
, &len
, &tag
, &xclass
, length
);
93 if (BIO_write(bp
, "Error in encoding\n", 18) <= 0)
101 * if j == 0x21 it is a constructed indefinite length object
103 if (BIO_printf(bp
, "%5ld:", (long)offset
+ (long)(op
- *pp
))
107 if (j
!= (V_ASN1_CONSTRUCTED
| 1)) {
108 if (BIO_printf(bp
, "d=%-2d hl=%ld l=%4ld ",
109 depth
, (long)hl
, len
) <= 0)
112 if (BIO_printf(bp
, "d=%-2d hl=%ld l=inf ", depth
, (long)hl
) <= 0)
115 if (!asn1_print_info(bp
, tag
, xclass
, j
, (indent
) ? depth
: 0))
117 if (j
& V_ASN1_CONSTRUCTED
) {
118 const unsigned char *sp
= p
;
121 if (BIO_write(bp
, "\n", 1) <= 0)
124 BIO_printf(bp
, "length is greater than %ld\n", length
);
128 if ((j
== 0x21) && (len
== 0)) {
130 r
= asn1_parse2(bp
, &p
, (long)(tot
- p
),
131 offset
+ (p
- *pp
), depth
+ 1,
137 if ((r
== 2) || (p
>= tot
)) {
147 r
= asn1_parse2(bp
, &p
, tmp
,
148 offset
+ (p
- *pp
), depth
+ 1,
157 } else if (xclass
!= 0) {
159 if (BIO_write(bp
, "\n", 1) <= 0)
163 if ((tag
== V_ASN1_PRINTABLESTRING
) ||
164 (tag
== V_ASN1_T61STRING
) ||
165 (tag
== V_ASN1_IA5STRING
) ||
166 (tag
== V_ASN1_VISIBLESTRING
) ||
167 (tag
== V_ASN1_NUMERICSTRING
) ||
168 (tag
== V_ASN1_UTF8STRING
) ||
169 (tag
== V_ASN1_UTCTIME
) || (tag
== V_ASN1_GENERALIZEDTIME
)) {
170 if (BIO_write(bp
, ":", 1) <= 0)
172 if ((len
> 0) && BIO_write(bp
, (const char *)p
, (int)len
)
175 } else if (tag
== V_ASN1_OBJECT
) {
177 if (d2i_ASN1_OBJECT(&o
, &opp
, len
+ hl
) != NULL
) {
178 if (BIO_write(bp
, ":", 1) <= 0)
180 i2a_ASN1_OBJECT(bp
, o
);
182 if (BIO_puts(bp
, ":BAD OBJECT") <= 0)
186 } else if (tag
== V_ASN1_BOOLEAN
) {
188 if (BIO_puts(bp
, ":BAD BOOLEAN") <= 0)
193 BIO_printf(bp
, ":%u", p
[0]);
194 } else if (tag
== V_ASN1_BMPSTRING
) {
195 /* do the BMP thang */
196 } else if (tag
== V_ASN1_OCTET_STRING
) {
197 int i
, printable
= 1;
200 os
= d2i_ASN1_OCTET_STRING(NULL
, &opp
, len
+ hl
);
201 if (os
!= NULL
&& os
->length
> 0) {
204 * testing whether the octet string is printable
206 for (i
= 0; i
< os
->length
; i
++) {
207 if (((opp
[i
] < ' ') &&
210 (opp
[i
] != '\t')) || (opp
[i
] > '~')) {
216 /* printable string */
218 if (BIO_write(bp
, ":", 1) <= 0)
220 if (BIO_write(bp
, (const char *)opp
, os
->length
) <= 0)
224 * not printable => print octet string as hex dump
227 if (BIO_write(bp
, "[HEX DUMP]:", 11) <= 0)
229 for (i
= 0; i
< os
->length
; i
++) {
230 if (BIO_printf(bp
, "%02X", opp
[i
]) <= 0)
234 /* print the normal dump */
237 if (BIO_write(bp
, "\n", 1) <= 0)
240 if (BIO_dump_indent(bp
,
242 ((dump
== -1 || dump
>
244 length
) ? os
->length
: dump
),
250 ASN1_OCTET_STRING_free(os
);
252 } else if (tag
== V_ASN1_INTEGER
) {
257 bs
= d2i_ASN1_INTEGER(NULL
, &opp
, len
+ hl
);
259 if (BIO_write(bp
, ":", 1) <= 0)
261 if (bs
->type
== V_ASN1_NEG_INTEGER
)
262 if (BIO_write(bp
, "-", 1) <= 0)
264 for (i
= 0; i
< bs
->length
; i
++) {
265 if (BIO_printf(bp
, "%02X", bs
->data
[i
]) <= 0)
268 if (bs
->length
== 0) {
269 if (BIO_write(bp
, "00", 2) <= 0)
273 if (BIO_puts(bp
, ":BAD INTEGER") <= 0)
277 ASN1_INTEGER_free(bs
);
278 } else if (tag
== V_ASN1_ENUMERATED
) {
283 bs
= d2i_ASN1_ENUMERATED(NULL
, &opp
, len
+ hl
);
285 if (BIO_write(bp
, ":", 1) <= 0)
287 if (bs
->type
== V_ASN1_NEG_ENUMERATED
)
288 if (BIO_write(bp
, "-", 1) <= 0)
290 for (i
= 0; i
< bs
->length
; i
++) {
291 if (BIO_printf(bp
, "%02X", bs
->data
[i
]) <= 0)
294 if (bs
->length
== 0) {
295 if (BIO_write(bp
, "00", 2) <= 0)
299 if (BIO_puts(bp
, ":BAD ENUMERATED") <= 0)
303 ASN1_ENUMERATED_free(bs
);
304 } else if (len
> 0 && dump
) {
306 if (BIO_write(bp
, "\n", 1) <= 0)
309 if (BIO_dump_indent(bp
, (const char *)p
,
310 ((dump
== -1 || dump
> len
) ? len
: dump
),
317 const unsigned char *tmp
= op
+ hl
;
318 if (BIO_puts(bp
, ":[") <= 0)
320 for (i
= 0; i
< len
; i
++) {
321 if (BIO_printf(bp
, "%02X", tmp
[i
]) <= 0)
324 if (BIO_puts(bp
, "]") <= 0)
329 if (BIO_write(bp
, "\n", 1) <= 0)
333 if ((tag
== V_ASN1_EOC
) && (xclass
== 0)) {
334 ret
= 2; /* End of sequence */
343 ASN1_OCTET_STRING_free(os
);
348 const char *ASN1_tag2str(int tag
)
350 static const char *const tag2str
[] = {
352 "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
354 "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL",
356 "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>",
358 "<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET",
360 "NUMERICSTRING", "PRINTABLESTRING", "T61STRING",
362 "VIDEOTEXSTRING", "IA5STRING", "UTCTIME", "GENERALIZEDTIME",
364 "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING",
366 "UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING"
369 if ((tag
== V_ASN1_NEG_INTEGER
) || (tag
== V_ASN1_NEG_ENUMERATED
))
372 if (tag
< 0 || tag
> 30)