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