2 * Testing tool for ASN.1/X.509v3 routines
3 * Copyright (c) 2006, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
21 extern int wpa_debug_level
;
24 static const char * asn1_class_str(int class)
27 case ASN1_CLASS_UNIVERSAL
:
29 case ASN1_CLASS_APPLICATION
:
31 case ASN1_CLASS_CONTEXT_SPECIFIC
:
32 return "Context-specific";
33 case ASN1_CLASS_PRIVATE
:
41 int asn1_parse(const u8
*buf
, size_t len
, int level
)
43 const u8
*pos
, *prev
, *end
;
44 char prefix
[10], str
[100];
51 if ((size_t) _level
> sizeof(prefix
) - 1)
52 _level
= sizeof(prefix
) - 1;
53 memset(prefix
, ' ', _level
);
54 prefix
[_level
] = '\0';
60 if (asn1_get_next(pos
, end
- pos
, &hdr
) < 0)
66 wpa_printf(MSG_MSGDUMP
, "ASN.1:%s Class %d(%s) P/C %d(%s) "
68 prefix
, hdr
.class, asn1_class_str(hdr
.class),
70 hdr
.constructed
? "Constructed" : "Primitive",
73 if (hdr
.class == ASN1_CLASS_CONTEXT_SPECIFIC
&&
75 if (asn1_parse(pos
, hdr
.length
, level
+ 1) < 0)
80 if (hdr
.class != ASN1_CLASS_UNIVERSAL
)
86 wpa_printf(MSG_DEBUG
, "ASN.1: Non-zero "
87 "end-of-contents length (%u)",
91 wpa_printf(MSG_MSGDUMP
, "ASN.1:%s EOC", prefix
);
93 case ASN1_TAG_BOOLEAN
:
94 if (hdr
.length
!= 1) {
95 wpa_printf(MSG_DEBUG
, "ASN.1: Unexpected "
96 "Boolean length (%u)", hdr
.length
);
100 wpa_printf(MSG_MSGDUMP
, "ASN.1:%s Boolean %s",
101 prefix
, tmp
? "TRUE" : "FALSE");
103 case ASN1_TAG_INTEGER
:
104 wpa_hexdump(MSG_MSGDUMP
, "ASN.1: INTEGER",
108 case ASN1_TAG_BITSTRING
:
109 wpa_hexdump(MSG_MSGDUMP
, "ASN.1: BitString",
113 case ASN1_TAG_OCTETSTRING
:
114 wpa_hexdump(MSG_MSGDUMP
, "ASN.1: OctetString",
120 wpa_printf(MSG_DEBUG
, "ASN.1: Non-zero Null "
121 "length (%u)", hdr
.length
);
124 wpa_printf(MSG_MSGDUMP
, "ASN.1:%s Null", prefix
);
127 if (asn1_get_oid(prev
, end
- prev
, &oid
, &prev
) < 0) {
128 wpa_printf(MSG_DEBUG
, "ASN.1: Invalid OID");
131 asn1_oid_to_str(&oid
, str
, sizeof(str
));
132 wpa_printf(MSG_DEBUG
, "ASN.1:%s OID %s", prefix
, str
);
135 case ANS1_TAG_RELATIVE_OID
:
136 wpa_hexdump(MSG_MSGDUMP
, "ASN.1: Relative OID",
140 case ASN1_TAG_SEQUENCE
:
141 wpa_printf(MSG_MSGDUMP
, "ASN.1:%s SEQUENCE", prefix
);
142 if (asn1_parse(pos
, hdr
.length
, level
+ 1) < 0)
147 wpa_printf(MSG_MSGDUMP
, "ASN.1:%s SET", prefix
);
148 if (asn1_parse(pos
, hdr
.length
, level
+ 1) < 0)
152 case ASN1_TAG_PRINTABLESTRING
:
153 wpa_hexdump_ascii(MSG_MSGDUMP
,
154 "ASN.1: PrintableString",
158 case ASN1_TAG_IA5STRING
:
159 wpa_hexdump_ascii(MSG_MSGDUMP
, "ASN.1: IA5String",
163 case ASN1_TAG_UTCTIME
:
164 wpa_hexdump_ascii(MSG_MSGDUMP
, "ASN.1: UTCTIME",
168 case ASN1_TAG_VISIBLESTRING
:
169 wpa_hexdump_ascii(MSG_MSGDUMP
, "ASN.1: VisibleString",
174 wpa_printf(MSG_DEBUG
, "ASN.1: Unknown tag %d",
184 int main(int argc
, char *argv
[])
189 struct x509_certificate
*cert
;
193 f
= fopen(argv
[1], "rb");
196 len
= fread(buf
, 1, sizeof(buf
), f
);
199 if (asn1_parse(buf
, len
, 0) < 0)
200 printf("Failed to parse DER ASN.1\n");
204 cert
= x509_certificate_parse(buf
, len
);
206 printf("Failed to parse X.509 certificate\n");
207 x509_certificate_free(cert
);