2
* Testing tool for ASN.1/X.509v3 routines
3
* Copyright (c) 2006, Jouni Malinen <jkmaline@cc.hut.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
static void asn1_dump_oid(struct asn1_oid *oid)
50
for (i = 0; i < oid->len; i++) {
51
ret = snprintf(spos, str + sizeof(str) - spos,
53
if (ret < 0 || ret >= str + sizeof(str) - spos)
57
str[sizeof(str) - 1] = '\0';
58
wpa_printf(MSG_MSGDUMP, "ASN.1: OID %s", str);
62
int asn1_parse(const u8 *buf, size_t len, int level)
64
const u8 *pos, *prev, *end;
72
if ((size_t) _level > sizeof(prefix) - 1)
73
_level = sizeof(prefix) - 1;
74
memset(prefix, ' ', _level);
75
prefix[_level] = '\0';
81
if (asn1_get_next(pos, end - pos, &hdr) < 0)
87
if (hdr.class == ASN1_CLASS_CONTEXT_SPECIFIC)
90
wpa_printf(MSG_MSGDUMP, "ASN.1:%s Class %d(%s) P/C %d(%s) "
92
prefix, hdr.class, asn1_class_str(hdr.class),
94
hdr.constructed ? "Constructed" : "Primitive",
100
wpa_printf(MSG_DEBUG, "ASN.1: Non-zero "
101
"end-of-contents length (%u)",
105
wpa_printf(MSG_MSGDUMP, "ASN.1:%s EOC", prefix);
107
case ASN1_TAG_BOOLEAN:
108
if (hdr.length != 1) {
109
wpa_printf(MSG_DEBUG, "ASN.1: Unexpected "
110
"Boolean length (%u)", hdr.length);
114
wpa_printf(MSG_MSGDUMP, "ASN.1:%s Boolean %s",
115
prefix, tmp ? "TRUE" : "FALSE");
117
case ASN1_TAG_INTEGER:
118
wpa_hexdump(MSG_MSGDUMP, "ASN.1: INTEGER",
122
case ASN1_TAG_BITSTRING:
123
wpa_hexdump(MSG_MSGDUMP, "ASN.1: BitString",
127
case ASN1_TAG_OCTETSTRING:
128
wpa_hexdump(MSG_MSGDUMP, "ASN.1: OctetString",
134
wpa_printf(MSG_DEBUG, "ASN.1: Non-zero Null "
135
"length (%u)", hdr.length);
138
wpa_printf(MSG_MSGDUMP, "ASN.1:%s Null", prefix);
141
if (asn1_get_oid(prev, end - prev, &oid, &prev) < 0) {
142
wpa_printf(MSG_DEBUG, "ASN.1: Invalid OID");
148
case ANS1_TAG_RELATIVE_OID:
149
wpa_hexdump(MSG_MSGDUMP, "ASN.1: Relative OID",
153
case ASN1_TAG_SEQUENCE:
154
wpa_printf(MSG_MSGDUMP, "ASN.1:%s SEQUENCE", prefix);
155
if (asn1_parse(pos, hdr.length, level + 1) < 0)
160
wpa_printf(MSG_MSGDUMP, "ASN.1:%s SET", prefix);
161
if (asn1_parse(pos, hdr.length, level + 1) < 0)
165
case ASN1_TAG_PRINTABLESTRING:
166
wpa_hexdump_ascii(MSG_MSGDUMP,
167
"ASN.1: PrintableString",
171
case ASN1_TAG_IA5STRING:
172
wpa_hexdump_ascii(MSG_MSGDUMP, "ASN.1: IA5String",
176
case ASN1_TAG_UTCTIME:
177
wpa_hexdump_ascii(MSG_MSGDUMP, "ASN.1: UTCTIME",
181
case ASN1_TAG_VISIBLESTRING:
182
wpa_hexdump_ascii(MSG_MSGDUMP, "ASN.1: VisibleString",
187
wpa_printf(MSG_DEBUG, "ASN.1: Unknown tag %d",
197
int main(int argc, char *argv[])
202
struct x509_certificate cert;
206
f = fopen(argv[1], "rb");
209
len = fread(buf, 1, sizeof(buf), f);
212
if (asn1_parse(buf, len, 0) < 0)
213
printf("Failed to parse DER ASN.1\n");
217
if (x509_certificate_parse(buf, len, &cert) < 0)
218
printf("Failed to parse X.509 certificate\n");
219
x509_certificate_free(&cert);