-
Notifications
You must be signed in to change notification settings - Fork 2
/
asn1_test.c
210 lines (186 loc) · 4.76 KB
/
asn1_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Testing tool for ASN.1/X.509v3 routines
* Copyright (c) 2006, Jouni Malinen <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
* See README and COPYING for more details.
*/
#include "includes.h"
#include "common.h"
#include "asn1.h"
#include "x509v3.h"
extern int wpa_debug_level;
static const char * asn1_class_str(int class)
{
switch (class) {
case ASN1_CLASS_UNIVERSAL:
return "Universal";
case ASN1_CLASS_APPLICATION:
return "Application";
case ASN1_CLASS_CONTEXT_SPECIFIC:
return "Context-specific";
case ASN1_CLASS_PRIVATE:
return "Private";
default:
return "?";
}
}
int asn1_parse(const u8 *buf, size_t len, int level)
{
const u8 *pos, *prev, *end;
char prefix[10], str[100];
int _level;
struct asn1_hdr hdr;
struct asn1_oid oid;
u8 tmp;
_level = level;
if ((size_t) _level > sizeof(prefix) - 1)
_level = sizeof(prefix) - 1;
memset(prefix, ' ', _level);
prefix[_level] = '\0';
pos = buf;
end = buf + len;
while (pos < end) {
if (asn1_get_next(pos, end - pos, &hdr) < 0)
return -1;
prev = pos;
pos = hdr.payload;
wpa_printf(MSG_MSGDUMP, "ASN.1:%s Class %d(%s) P/C %d(%s) "
"Tag %u Length %u",
prefix, hdr.class, asn1_class_str(hdr.class),
hdr.constructed,
hdr.constructed ? "Constructed" : "Primitive",
hdr.tag, hdr.length);
if (hdr.class == ASN1_CLASS_CONTEXT_SPECIFIC &&
hdr.constructed) {
if (asn1_parse(pos, hdr.length, level + 1) < 0)
return -1;
pos += hdr.length;
}
if (hdr.class != ASN1_CLASS_UNIVERSAL)
continue;
switch (hdr.tag) {
case ASN1_TAG_EOC:
if (hdr.length) {
wpa_printf(MSG_DEBUG, "ASN.1: Non-zero "
"end-of-contents length (%u)",
hdr.length);
return -1;
}
wpa_printf(MSG_MSGDUMP, "ASN.1:%s EOC", prefix);
break;
case ASN1_TAG_BOOLEAN:
if (hdr.length != 1) {
wpa_printf(MSG_DEBUG, "ASN.1: Unexpected "
"Boolean length (%u)", hdr.length);
return -1;
}
tmp = *pos++;
wpa_printf(MSG_MSGDUMP, "ASN.1:%s Boolean %s",
prefix, tmp ? "TRUE" : "FALSE");
break;
case ASN1_TAG_INTEGER:
wpa_hexdump(MSG_MSGDUMP, "ASN.1: INTEGER",
pos, hdr.length);
pos += hdr.length;
break;
case ASN1_TAG_BITSTRING:
wpa_hexdump(MSG_MSGDUMP, "ASN.1: BitString",
pos, hdr.length);
pos += hdr.length;
break;
case ASN1_TAG_OCTETSTRING:
wpa_hexdump(MSG_MSGDUMP, "ASN.1: OctetString",
pos, hdr.length);
pos += hdr.length;
break;
case ASN1_TAG_NULL:
if (hdr.length) {
wpa_printf(MSG_DEBUG, "ASN.1: Non-zero Null "
"length (%u)", hdr.length);
return -1;
}
wpa_printf(MSG_MSGDUMP, "ASN.1:%s Null", prefix);
break;
case ASN1_TAG_OID:
if (asn1_get_oid(prev, end - prev, &oid, &prev) < 0) {
wpa_printf(MSG_DEBUG, "ASN.1: Invalid OID");
return -1;
}
asn1_oid_to_str(&oid, str, sizeof(str));
wpa_printf(MSG_DEBUG, "ASN.1:%s OID %s", prefix, str);
pos += hdr.length;
break;
case ANS1_TAG_RELATIVE_OID:
wpa_hexdump(MSG_MSGDUMP, "ASN.1: Relative OID",
pos, hdr.length);
pos += hdr.length;
break;
case ASN1_TAG_SEQUENCE:
wpa_printf(MSG_MSGDUMP, "ASN.1:%s SEQUENCE", prefix);
if (asn1_parse(pos, hdr.length, level + 1) < 0)
return -1;
pos += hdr.length;
break;
case ASN1_TAG_SET:
wpa_printf(MSG_MSGDUMP, "ASN.1:%s SET", prefix);
if (asn1_parse(pos, hdr.length, level + 1) < 0)
return -1;
pos += hdr.length;
break;
case ASN1_TAG_PRINTABLESTRING:
wpa_hexdump_ascii(MSG_MSGDUMP,
"ASN.1: PrintableString",
pos, hdr.length);
pos += hdr.length;
break;
case ASN1_TAG_IA5STRING:
wpa_hexdump_ascii(MSG_MSGDUMP, "ASN.1: IA5String",
pos, hdr.length);
pos += hdr.length;
break;
case ASN1_TAG_UTCTIME:
wpa_hexdump_ascii(MSG_MSGDUMP, "ASN.1: UTCTIME",
pos, hdr.length);
pos += hdr.length;
break;
case ASN1_TAG_VISIBLESTRING:
wpa_hexdump_ascii(MSG_MSGDUMP, "ASN.1: VisibleString",
pos, hdr.length);
pos += hdr.length;
break;
default:
wpa_printf(MSG_DEBUG, "ASN.1: Unknown tag %d",
hdr.tag);
return -1;
}
}
return 0;
}
int main(int argc, char *argv[])
{
FILE *f;
u8 buf[3000];
size_t len;
struct x509_certificate *cert;
wpa_debug_level = 0;
f = fopen(argv[1], "rb");
if (f == NULL)
return -1;
len = fread(buf, 1, sizeof(buf), f);
fclose(f);
if (asn1_parse(buf, len, 0) < 0)
printf("Failed to parse DER ASN.1\n");
printf("\n\n");
cert = x509_certificate_parse(buf, len);
if (cert == NULL)
printf("Failed to parse X.509 certificate\n");
x509_certificate_free(cert);
return 0;
}