~ubuntu-branches/ubuntu/precise/wpasupplicant/precise-security

« back to all changes in this revision

Viewing changes to asn1_test.c

  • Committer: Bazaar Package Importer
  • Author(s): Kel Modderman
  • Date: 2006-10-05 08:04:01 UTC
  • mfrom: (1.2.1 upstream) (2.1.14 edgy)
  • Revision ID: james.westby@ubuntu.com-20061005080401-myfwjtq7di70dyeo
* Update madwifi headers to latest SVN. (Closes: #388316)
* Remove failed attempt at action locking. [debian/functions.sh,
  debian/wpa_action.sh]
* Add hysteresis checking functions, to avoid "event loops" while
  using wpa-roam. [debian/functions.sh, debian/wpa_action.sh]
* Change of co-maintainer email address.
* Add ishex() function to functions.sh to determine wpa-psk value type in
  plaintext or hex. This effectively eliminates the need for the bogus and
  somewhat confusing wpa-passphrase contruct specific to our scripts and
  allows wpa-psk to work with either a 8 to 63 character long plaintext
  string or 64 character long hex string.
* Adjust README.modes to not refer to the redundant wpa-passphrase stuff.
* Add big fat NOTE about acceptable wpa-psk's to top of example gallery.
* Strip surrounding quotes from wpa-ssid if present, instead of just whining
  about them.
* Update email address in copyright blurb of functions.sh, ifupdown.sh and
  wpa_action.sh.  

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Testing tool for ASN.1/X.509v3 routines
 
3
 * Copyright (c) 2006, Jouni Malinen <jkmaline@cc.hut.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 "asn1.h"
 
19
#include "x509v3.h"
 
20
 
 
21
extern int wpa_debug_level;
 
22
 
 
23
 
 
24
static const char * asn1_class_str(int class)
 
25
{
 
26
        switch (class) {
 
27
        case ASN1_CLASS_UNIVERSAL:
 
28
                return "Universal";
 
29
        case ASN1_CLASS_APPLICATION:
 
30
                return "Application";
 
31
        case ASN1_CLASS_CONTEXT_SPECIFIC:
 
32
                return "Context-specific";
 
33
        case ASN1_CLASS_PRIVATE:
 
34
                return "Private";
 
35
        default:
 
36
                return "?";
 
37
        }
 
38
}
 
39
 
 
40
 
 
41
static void asn1_dump_oid(struct asn1_oid *oid)
 
42
{
 
43
        char str[100], *spos;
 
44
        size_t i;
 
45
        int ret;
 
46
 
 
47
        str[0] = '\0';
 
48
        spos = str;
 
49
 
 
50
        for (i = 0; i < oid->len; i++) {
 
51
                ret = snprintf(spos, str + sizeof(str) - spos,
 
52
                               ".%lu", oid->oid[i]);
 
53
                if (ret < 0 || ret >= str + sizeof(str) - spos)
 
54
                        break;
 
55
                spos += ret;
 
56
        }
 
57
        str[sizeof(str) - 1] = '\0';
 
58
        wpa_printf(MSG_MSGDUMP, "ASN.1: OID %s", str);
 
59
}
 
60
 
 
61
 
 
62
int asn1_parse(const u8 *buf, size_t len, int level)
 
63
{
 
64
        const u8 *pos, *prev, *end;
 
65
        char prefix[10];
 
66
        int _level;
 
67
        struct asn1_hdr hdr;
 
68
        struct asn1_oid oid;
 
69
        u8 tmp;
 
70
 
 
71
        _level = level;
 
72
        if ((size_t) _level > sizeof(prefix) - 1)
 
73
                _level = sizeof(prefix) - 1;
 
74
        memset(prefix, ' ', _level);
 
75
        prefix[_level] = '\0';
 
76
 
 
77
        pos = buf;
 
78
        end = buf + len;
 
79
 
 
80
        while (pos < end) {
 
81
                if (asn1_get_next(pos, end - pos, &hdr) < 0)
 
82
                        return -1;
 
83
 
 
84
                prev = pos;
 
85
                pos = hdr.payload;
 
86
 
 
87
                if (hdr.class == ASN1_CLASS_CONTEXT_SPECIFIC)
 
88
                        continue;
 
89
 
 
90
                wpa_printf(MSG_MSGDUMP, "ASN.1:%s Class %d(%s) P/C %d(%s) "
 
91
                           "Tag %u Length %u",
 
92
                           prefix, hdr.class, asn1_class_str(hdr.class),
 
93
                           hdr.constructed,
 
94
                           hdr.constructed ? "Constructed" : "Primitive",
 
95
                           hdr.tag, hdr.length);
 
96
 
 
97
                switch (hdr.tag) {
 
98
                case ASN1_TAG_EOC:
 
99
                        if (hdr.length) {
 
100
                                wpa_printf(MSG_DEBUG, "ASN.1: Non-zero "
 
101
                                           "end-of-contents length (%u)",
 
102
                                           hdr.length);
 
103
                                return -1;
 
104
                        }
 
105
                        wpa_printf(MSG_MSGDUMP, "ASN.1:%s EOC", prefix);
 
106
                        break;
 
107
                case ASN1_TAG_BOOLEAN:
 
108
                        if (hdr.length != 1) {
 
109
                                wpa_printf(MSG_DEBUG, "ASN.1: Unexpected "
 
110
                                           "Boolean length (%u)", hdr.length);
 
111
                                return -1;
 
112
                        }
 
113
                        tmp = *pos++;
 
114
                        wpa_printf(MSG_MSGDUMP, "ASN.1:%s Boolean %s",
 
115
                                   prefix, tmp ? "TRUE" : "FALSE");
 
116
                        break;
 
117
                case ASN1_TAG_INTEGER:
 
118
                        wpa_hexdump(MSG_MSGDUMP, "ASN.1: INTEGER",
 
119
                                    pos, hdr.length);
 
120
                        pos += hdr.length;
 
121
                        break;
 
122
                case ASN1_TAG_BITSTRING:
 
123
                        wpa_hexdump(MSG_MSGDUMP, "ASN.1: BitString",
 
124
                                    pos, hdr.length);
 
125
                        pos += hdr.length;
 
126
                        break;
 
127
                case ASN1_TAG_OCTETSTRING:
 
128
                        wpa_hexdump(MSG_MSGDUMP, "ASN.1: OctetString",
 
129
                                    pos, hdr.length);
 
130
                        pos += hdr.length;
 
131
                        break;
 
132
                case ASN1_TAG_NULL:
 
133
                        if (hdr.length) {
 
134
                                wpa_printf(MSG_DEBUG, "ASN.1: Non-zero Null "
 
135
                                           "length (%u)", hdr.length);
 
136
                                return -1;
 
137
                        }
 
138
                        wpa_printf(MSG_MSGDUMP, "ASN.1:%s Null", prefix);
 
139
                        break;
 
140
                case ASN1_TAG_OID:
 
141
                        if (asn1_get_oid(prev, end - prev, &oid, &prev) < 0) {
 
142
                                wpa_printf(MSG_DEBUG, "ASN.1: Invalid OID");
 
143
                                return -1;
 
144
                        }
 
145
                        asn1_dump_oid(&oid);
 
146
                        pos += hdr.length;
 
147
                        break;
 
148
                case ANS1_TAG_RELATIVE_OID:
 
149
                        wpa_hexdump(MSG_MSGDUMP, "ASN.1: Relative OID",
 
150
                                    pos, hdr.length);
 
151
                        pos += hdr.length;
 
152
                        break;
 
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)
 
156
                                return -1;
 
157
                        pos += hdr.length;
 
158
                        break;
 
159
                case ASN1_TAG_SET:
 
160
                        wpa_printf(MSG_MSGDUMP, "ASN.1:%s SET", prefix);
 
161
                        if (asn1_parse(pos, hdr.length, level + 1) < 0)
 
162
                                return -1;
 
163
                        pos += hdr.length;
 
164
                        break;
 
165
                case ASN1_TAG_PRINTABLESTRING:
 
166
                        wpa_hexdump_ascii(MSG_MSGDUMP,
 
167
                                          "ASN.1: PrintableString",
 
168
                                          pos, hdr.length);
 
169
                        pos += hdr.length;
 
170
                        break;
 
171
                case ASN1_TAG_IA5STRING:
 
172
                        wpa_hexdump_ascii(MSG_MSGDUMP, "ASN.1: IA5String",
 
173
                                          pos, hdr.length);
 
174
                        pos += hdr.length;
 
175
                        break;
 
176
                case ASN1_TAG_UTCTIME:
 
177
                        wpa_hexdump_ascii(MSG_MSGDUMP, "ASN.1: UTCTIME",
 
178
                                          pos, hdr.length);
 
179
                        pos += hdr.length;
 
180
                        break;
 
181
                case ASN1_TAG_VISIBLESTRING:
 
182
                        wpa_hexdump_ascii(MSG_MSGDUMP, "ASN.1: VisibleString",
 
183
                                          pos, hdr.length);
 
184
                        pos += hdr.length;
 
185
                        break;
 
186
                default:
 
187
                        wpa_printf(MSG_DEBUG, "ASN.1: Unknown tag %d",
 
188
                                   hdr.tag);
 
189
                        return -1;
 
190
                }
 
191
        }
 
192
 
 
193
        return 0;
 
194
}
 
195
 
 
196
 
 
197
int main(int argc, char *argv[])
 
198
{
 
199
        FILE *f;
 
200
        u8 buf[3000];
 
201
        size_t len;
 
202
        struct x509_certificate cert;
 
203
 
 
204
        wpa_debug_level = 0;
 
205
 
 
206
        f = fopen(argv[1], "rb");
 
207
        if (f == NULL)
 
208
                return -1;
 
209
        len = fread(buf, 1, sizeof(buf), f);
 
210
        fclose(f);
 
211
 
 
212
        if (asn1_parse(buf, len, 0) < 0)
 
213
                printf("Failed to parse DER ASN.1\n");
 
214
 
 
215
        printf("\n\n");
 
216
 
 
217
        if (x509_certificate_parse(buf, len, &cert) < 0)
 
218
                printf("Failed to parse X.509 certificate\n");
 
219
        x509_certificate_free(&cert);
 
220
 
 
221
        return 0;
 
222
}