2 * Testing tool for ASN.1 routines
3 * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
14 extern int wpa_debug_level;
17 static const char * asn1_class_str(int class)
20 case ASN1_CLASS_UNIVERSAL:
22 case ASN1_CLASS_APPLICATION:
24 case ASN1_CLASS_CONTEXT_SPECIFIC:
25 return "Context-specific";
26 case ASN1_CLASS_PRIVATE:
34 int asn1_parse(const u8 *buf, size_t len, int level)
36 const u8 *pos, *prev, *end;
37 char prefix[10], str[100];
44 if ((size_t) _level > sizeof(prefix) - 1)
45 _level = sizeof(prefix) - 1;
46 memset(prefix, ' ', _level);
47 prefix[_level] = '\0';
53 if (asn1_get_next(pos, end - pos, &hdr) < 0)
59 wpa_printf(MSG_MSGDUMP, "ASN.1:%s Class %d(%s) P/C %d(%s) "
61 prefix, hdr.class, asn1_class_str(hdr.class),
63 hdr.constructed ? "Constructed" : "Primitive",
66 if (hdr.class == ASN1_CLASS_CONTEXT_SPECIFIC &&
68 if (asn1_parse(pos, hdr.length, level + 1) < 0)
73 if (hdr.class != ASN1_CLASS_UNIVERSAL)
79 wpa_printf(MSG_DEBUG, "ASN.1: Non-zero "
80 "end-of-contents length (%u)",
84 wpa_printf(MSG_MSGDUMP, "ASN.1:%s EOC", prefix);
86 case ASN1_TAG_BOOLEAN:
87 if (hdr.length != 1) {
88 wpa_printf(MSG_DEBUG, "ASN.1: Unexpected "
89 "Boolean length (%u)", hdr.length);
93 wpa_printf(MSG_MSGDUMP, "ASN.1:%s Boolean %s",
94 prefix, tmp ? "TRUE" : "FALSE");
96 case ASN1_TAG_INTEGER:
97 wpa_hexdump(MSG_MSGDUMP, "ASN.1: INTEGER",
101 case ASN1_TAG_BITSTRING:
102 wpa_hexdump(MSG_MSGDUMP, "ASN.1: BitString",
106 case ASN1_TAG_OCTETSTRING:
107 wpa_hexdump(MSG_MSGDUMP, "ASN.1: OctetString",
113 wpa_printf(MSG_DEBUG, "ASN.1: Non-zero Null "
114 "length (%u)", hdr.length);
117 wpa_printf(MSG_MSGDUMP, "ASN.1:%s Null", prefix);
120 if (asn1_get_oid(prev, end - prev, &oid, &prev) < 0) {
121 wpa_printf(MSG_DEBUG, "ASN.1: Invalid OID");
124 asn1_oid_to_str(&oid, str, sizeof(str));
125 wpa_printf(MSG_DEBUG, "ASN.1:%s OID %s", prefix, str);
128 case ANS1_TAG_RELATIVE_OID:
129 wpa_hexdump(MSG_MSGDUMP, "ASN.1: Relative OID",
133 case ASN1_TAG_SEQUENCE:
134 wpa_printf(MSG_MSGDUMP, "ASN.1:%s SEQUENCE", prefix);
135 if (asn1_parse(pos, hdr.length, level + 1) < 0)
140 wpa_printf(MSG_MSGDUMP, "ASN.1:%s SET", prefix);
141 if (asn1_parse(pos, hdr.length, level + 1) < 0)
145 case ASN1_TAG_PRINTABLESTRING:
146 wpa_hexdump_ascii(MSG_MSGDUMP,
147 "ASN.1: PrintableString",
151 case ASN1_TAG_IA5STRING:
152 wpa_hexdump_ascii(MSG_MSGDUMP, "ASN.1: IA5String",
156 case ASN1_TAG_UTCTIME:
157 wpa_hexdump_ascii(MSG_MSGDUMP, "ASN.1: UTCTIME",
161 case ASN1_TAG_VISIBLESTRING:
162 wpa_hexdump_ascii(MSG_MSGDUMP, "ASN.1: VisibleString",
167 wpa_printf(MSG_DEBUG, "ASN.1: Unknown tag %d",
177 int main(int argc, char *argv[])
185 f = fopen(argv[1], "rb");
188 len = fread(buf, 1, sizeof(buf), f);
191 if (asn1_parse(buf, len, 0) < 0)
192 printf("Failed to parse DER ASN.1\n");