eapol_test.c revision 1.1.1.1.6.1 1 1.1 christos /*
2 1.1 christos * WPA Supplicant - test code
3 1.1.1.1.6.1 yamt * Copyright (c) 2003-2011, Jouni Malinen <j (at) w1.fi>
4 1.1 christos *
5 1.1 christos * This program is free software; you can redistribute it and/or modify
6 1.1 christos * it under the terms of the GNU General Public License version 2 as
7 1.1 christos * published by the Free Software Foundation.
8 1.1 christos *
9 1.1 christos * Alternatively, this software may be distributed under the terms of BSD
10 1.1 christos * license.
11 1.1 christos *
12 1.1 christos * See README and COPYING for more details.
13 1.1 christos *
14 1.1 christos * IEEE 802.1X Supplicant test code (to be used in place of wpa_supplicant.c.
15 1.1 christos * Not used in production version.
16 1.1 christos */
17 1.1 christos
18 1.1 christos #include "includes.h"
19 1.1 christos #include <assert.h>
20 1.1 christos
21 1.1 christos #include "common.h"
22 1.1 christos #include "config.h"
23 1.1 christos #include "eapol_supp/eapol_supp_sm.h"
24 1.1 christos #include "eap_peer/eap.h"
25 1.1.1.1.6.1 yamt #include "eap_server/eap_methods.h"
26 1.1 christos #include "eloop.h"
27 1.1.1.1.6.1 yamt #include "utils/base64.h"
28 1.1 christos #include "rsn_supp/wpa.h"
29 1.1 christos #include "eap_peer/eap_i.h"
30 1.1 christos #include "wpa_supplicant_i.h"
31 1.1 christos #include "radius/radius.h"
32 1.1 christos #include "radius/radius_client.h"
33 1.1.1.1.6.1 yamt #include "common/wpa_ctrl.h"
34 1.1 christos #include "ctrl_iface.h"
35 1.1 christos #include "pcsc_funcs.h"
36 1.1 christos
37 1.1 christos
38 1.1 christos extern int wpa_debug_level;
39 1.1 christos extern int wpa_debug_show_keys;
40 1.1 christos
41 1.1 christos struct wpa_driver_ops *wpa_drivers[] = { NULL };
42 1.1 christos
43 1.1 christos
44 1.1 christos struct extra_radius_attr {
45 1.1 christos u8 type;
46 1.1 christos char syntax;
47 1.1 christos char *data;
48 1.1 christos struct extra_radius_attr *next;
49 1.1 christos };
50 1.1 christos
51 1.1 christos struct eapol_test_data {
52 1.1 christos struct wpa_supplicant *wpa_s;
53 1.1 christos
54 1.1 christos int eapol_test_num_reauths;
55 1.1 christos int no_mppe_keys;
56 1.1 christos int num_mppe_ok, num_mppe_mismatch;
57 1.1 christos
58 1.1 christos u8 radius_identifier;
59 1.1 christos struct radius_msg *last_recv_radius;
60 1.1 christos struct in_addr own_ip_addr;
61 1.1 christos struct radius_client_data *radius;
62 1.1 christos struct hostapd_radius_servers *radius_conf;
63 1.1 christos
64 1.1 christos u8 *last_eap_radius; /* last received EAP Response from Authentication
65 1.1 christos * Server */
66 1.1 christos size_t last_eap_radius_len;
67 1.1 christos
68 1.1 christos u8 authenticator_pmk[PMK_LEN];
69 1.1 christos size_t authenticator_pmk_len;
70 1.1 christos int radius_access_accept_received;
71 1.1 christos int radius_access_reject_received;
72 1.1 christos int auth_timed_out;
73 1.1 christos
74 1.1 christos u8 *eap_identity;
75 1.1 christos size_t eap_identity_len;
76 1.1 christos
77 1.1 christos char *connect_info;
78 1.1 christos u8 own_addr[ETH_ALEN];
79 1.1 christos struct extra_radius_attr *extra_attrs;
80 1.1.1.1.6.1 yamt
81 1.1.1.1.6.1 yamt FILE *server_cert_file;
82 1.1 christos };
83 1.1 christos
84 1.1 christos static struct eapol_test_data eapol_test;
85 1.1 christos
86 1.1 christos
87 1.1 christos static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx);
88 1.1 christos
89 1.1 christos
90 1.1 christos static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
91 1.1 christos int level, const char *txt, size_t len)
92 1.1 christos {
93 1.1 christos if (addr)
94 1.1 christos wpa_printf(MSG_DEBUG, "STA " MACSTR ": %s\n",
95 1.1 christos MAC2STR(addr), txt);
96 1.1 christos else
97 1.1 christos wpa_printf(MSG_DEBUG, "%s", txt);
98 1.1 christos }
99 1.1 christos
100 1.1 christos
101 1.1 christos static int add_extra_attr(struct radius_msg *msg,
102 1.1 christos struct extra_radius_attr *attr)
103 1.1 christos {
104 1.1 christos size_t len;
105 1.1 christos char *pos;
106 1.1 christos u32 val;
107 1.1 christos char buf[128];
108 1.1 christos
109 1.1 christos switch (attr->syntax) {
110 1.1 christos case 's':
111 1.1 christos os_snprintf(buf, sizeof(buf), "%s", attr->data);
112 1.1 christos len = os_strlen(buf);
113 1.1 christos break;
114 1.1 christos case 'n':
115 1.1 christos buf[0] = '\0';
116 1.1 christos len = 1;
117 1.1 christos break;
118 1.1 christos case 'x':
119 1.1 christos pos = attr->data;
120 1.1 christos if (pos[0] == '0' && pos[1] == 'x')
121 1.1 christos pos += 2;
122 1.1 christos len = os_strlen(pos);
123 1.1 christos if ((len & 1) || (len / 2) > sizeof(buf)) {
124 1.1 christos printf("Invalid extra attribute hexstring\n");
125 1.1 christos return -1;
126 1.1 christos }
127 1.1 christos len /= 2;
128 1.1 christos if (hexstr2bin(pos, (u8 *) buf, len) < 0) {
129 1.1 christos printf("Invalid extra attribute hexstring\n");
130 1.1 christos return -1;
131 1.1 christos }
132 1.1 christos break;
133 1.1 christos case 'd':
134 1.1 christos val = htonl(atoi(attr->data));
135 1.1 christos os_memcpy(buf, &val, 4);
136 1.1 christos len = 4;
137 1.1 christos break;
138 1.1 christos default:
139 1.1 christos printf("Incorrect extra attribute syntax specification\n");
140 1.1 christos return -1;
141 1.1 christos }
142 1.1 christos
143 1.1 christos if (!radius_msg_add_attr(msg, attr->type, (u8 *) buf, len)) {
144 1.1 christos printf("Could not add attribute %d\n", attr->type);
145 1.1 christos return -1;
146 1.1 christos }
147 1.1 christos
148 1.1 christos return 0;
149 1.1 christos }
150 1.1 christos
151 1.1 christos
152 1.1 christos static int add_extra_attrs(struct radius_msg *msg,
153 1.1 christos struct extra_radius_attr *attrs)
154 1.1 christos {
155 1.1 christos struct extra_radius_attr *p;
156 1.1 christos for (p = attrs; p; p = p->next) {
157 1.1 christos if (add_extra_attr(msg, p) < 0)
158 1.1 christos return -1;
159 1.1 christos }
160 1.1 christos return 0;
161 1.1 christos }
162 1.1 christos
163 1.1 christos
164 1.1 christos static struct extra_radius_attr *
165 1.1 christos find_extra_attr(struct extra_radius_attr *attrs, u8 type)
166 1.1 christos {
167 1.1 christos struct extra_radius_attr *p;
168 1.1 christos for (p = attrs; p; p = p->next) {
169 1.1 christos if (p->type == type)
170 1.1 christos return p;
171 1.1 christos }
172 1.1 christos return NULL;
173 1.1 christos }
174 1.1 christos
175 1.1 christos
176 1.1 christos static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
177 1.1 christos const u8 *eap, size_t len)
178 1.1 christos {
179 1.1 christos struct radius_msg *msg;
180 1.1 christos char buf[128];
181 1.1 christos const struct eap_hdr *hdr;
182 1.1 christos const u8 *pos;
183 1.1 christos
184 1.1 christos wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
185 1.1 christos "packet");
186 1.1 christos
187 1.1 christos e->radius_identifier = radius_client_get_id(e->radius);
188 1.1 christos msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
189 1.1 christos e->radius_identifier);
190 1.1 christos if (msg == NULL) {
191 1.1 christos printf("Could not create net RADIUS packet\n");
192 1.1 christos return;
193 1.1 christos }
194 1.1 christos
195 1.1 christos radius_msg_make_authenticator(msg, (u8 *) e, sizeof(*e));
196 1.1 christos
197 1.1 christos hdr = (const struct eap_hdr *) eap;
198 1.1 christos pos = (const u8 *) (hdr + 1);
199 1.1 christos if (len > sizeof(*hdr) && hdr->code == EAP_CODE_RESPONSE &&
200 1.1 christos pos[0] == EAP_TYPE_IDENTITY) {
201 1.1 christos pos++;
202 1.1 christos os_free(e->eap_identity);
203 1.1 christos e->eap_identity_len = len - sizeof(*hdr) - 1;
204 1.1 christos e->eap_identity = os_malloc(e->eap_identity_len);
205 1.1 christos if (e->eap_identity) {
206 1.1 christos os_memcpy(e->eap_identity, pos, e->eap_identity_len);
207 1.1 christos wpa_hexdump(MSG_DEBUG, "Learned identity from "
208 1.1 christos "EAP-Response-Identity",
209 1.1 christos e->eap_identity, e->eap_identity_len);
210 1.1 christos }
211 1.1 christos }
212 1.1 christos
213 1.1 christos if (e->eap_identity &&
214 1.1 christos !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
215 1.1 christos e->eap_identity, e->eap_identity_len)) {
216 1.1 christos printf("Could not add User-Name\n");
217 1.1 christos goto fail;
218 1.1 christos }
219 1.1 christos
220 1.1 christos if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_NAS_IP_ADDRESS) &&
221 1.1 christos !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
222 1.1 christos (u8 *) &e->own_ip_addr, 4)) {
223 1.1 christos printf("Could not add NAS-IP-Address\n");
224 1.1 christos goto fail;
225 1.1 christos }
226 1.1 christos
227 1.1 christos os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
228 1.1 christos MAC2STR(e->wpa_s->own_addr));
229 1.1 christos if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_CALLING_STATION_ID)
230 1.1 christos &&
231 1.1 christos !radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
232 1.1 christos (u8 *) buf, os_strlen(buf))) {
233 1.1 christos printf("Could not add Calling-Station-Id\n");
234 1.1 christos goto fail;
235 1.1 christos }
236 1.1 christos
237 1.1 christos /* TODO: should probably check MTU from driver config; 2304 is max for
238 1.1 christos * IEEE 802.11, but use 1400 to avoid problems with too large packets
239 1.1 christos */
240 1.1 christos if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_FRAMED_MTU) &&
241 1.1 christos !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
242 1.1 christos printf("Could not add Framed-MTU\n");
243 1.1 christos goto fail;
244 1.1 christos }
245 1.1 christos
246 1.1 christos if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_NAS_PORT_TYPE) &&
247 1.1 christos !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
248 1.1 christos RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
249 1.1 christos printf("Could not add NAS-Port-Type\n");
250 1.1 christos goto fail;
251 1.1 christos }
252 1.1 christos
253 1.1 christos os_snprintf(buf, sizeof(buf), "%s", e->connect_info);
254 1.1 christos if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_CONNECT_INFO) &&
255 1.1 christos !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
256 1.1 christos (u8 *) buf, os_strlen(buf))) {
257 1.1 christos printf("Could not add Connect-Info\n");
258 1.1 christos goto fail;
259 1.1 christos }
260 1.1 christos
261 1.1 christos if (add_extra_attrs(msg, e->extra_attrs) < 0)
262 1.1 christos goto fail;
263 1.1 christos
264 1.1 christos if (eap && !radius_msg_add_eap(msg, eap, len)) {
265 1.1 christos printf("Could not add EAP-Message\n");
266 1.1 christos goto fail;
267 1.1 christos }
268 1.1 christos
269 1.1 christos /* State attribute must be copied if and only if this packet is
270 1.1 christos * Access-Request reply to the previous Access-Challenge */
271 1.1 christos if (e->last_recv_radius &&
272 1.1 christos radius_msg_get_hdr(e->last_recv_radius)->code ==
273 1.1 christos RADIUS_CODE_ACCESS_CHALLENGE) {
274 1.1 christos int res = radius_msg_copy_attr(msg, e->last_recv_radius,
275 1.1 christos RADIUS_ATTR_STATE);
276 1.1 christos if (res < 0) {
277 1.1 christos printf("Could not copy State attribute from previous "
278 1.1 christos "Access-Challenge\n");
279 1.1 christos goto fail;
280 1.1 christos }
281 1.1 christos if (res > 0) {
282 1.1 christos wpa_printf(MSG_DEBUG, " Copied RADIUS State "
283 1.1 christos "Attribute");
284 1.1 christos }
285 1.1 christos }
286 1.1 christos
287 1.1.1.1.6.1 yamt if (radius_client_send(e->radius, msg, RADIUS_AUTH, e->wpa_s->own_addr)
288 1.1.1.1.6.1 yamt < 0)
289 1.1.1.1.6.1 yamt goto fail;
290 1.1 christos return;
291 1.1 christos
292 1.1 christos fail:
293 1.1 christos radius_msg_free(msg);
294 1.1 christos }
295 1.1 christos
296 1.1 christos
297 1.1 christos static int eapol_test_eapol_send(void *ctx, int type, const u8 *buf,
298 1.1 christos size_t len)
299 1.1 christos {
300 1.1 christos printf("WPA: eapol_test_eapol_send(type=%d len=%lu)\n",
301 1.1 christos type, (unsigned long) len);
302 1.1 christos if (type == IEEE802_1X_TYPE_EAP_PACKET) {
303 1.1 christos wpa_hexdump(MSG_DEBUG, "TX EAP -> RADIUS", buf, len);
304 1.1 christos ieee802_1x_encapsulate_radius(&eapol_test, buf, len);
305 1.1 christos }
306 1.1 christos return 0;
307 1.1 christos }
308 1.1 christos
309 1.1 christos
310 1.1 christos static void eapol_test_set_config_blob(void *ctx,
311 1.1 christos struct wpa_config_blob *blob)
312 1.1 christos {
313 1.1.1.1.6.1 yamt struct eapol_test_data *e = ctx;
314 1.1.1.1.6.1 yamt wpa_config_set_blob(e->wpa_s->conf, blob);
315 1.1 christos }
316 1.1 christos
317 1.1 christos
318 1.1 christos static const struct wpa_config_blob *
319 1.1 christos eapol_test_get_config_blob(void *ctx, const char *name)
320 1.1 christos {
321 1.1.1.1.6.1 yamt struct eapol_test_data *e = ctx;
322 1.1.1.1.6.1 yamt return wpa_config_get_blob(e->wpa_s->conf, name);
323 1.1 christos }
324 1.1 christos
325 1.1 christos
326 1.1 christos static void eapol_test_eapol_done_cb(void *ctx)
327 1.1 christos {
328 1.1 christos printf("WPA: EAPOL processing complete\n");
329 1.1 christos }
330 1.1 christos
331 1.1 christos
332 1.1 christos static void eapol_sm_reauth(void *eloop_ctx, void *timeout_ctx)
333 1.1 christos {
334 1.1 christos struct eapol_test_data *e = eloop_ctx;
335 1.1 christos printf("\n\n\n\n\neapol_test: Triggering EAP reauthentication\n\n");
336 1.1 christos e->radius_access_accept_received = 0;
337 1.1 christos send_eap_request_identity(e->wpa_s, NULL);
338 1.1 christos }
339 1.1 christos
340 1.1 christos
341 1.1 christos static int eapol_test_compare_pmk(struct eapol_test_data *e)
342 1.1 christos {
343 1.1 christos u8 pmk[PMK_LEN];
344 1.1 christos int ret = 1;
345 1.1 christos
346 1.1 christos if (eapol_sm_get_key(e->wpa_s->eapol, pmk, PMK_LEN) == 0) {
347 1.1 christos wpa_hexdump(MSG_DEBUG, "PMK from EAPOL", pmk, PMK_LEN);
348 1.1 christos if (os_memcmp(pmk, e->authenticator_pmk, PMK_LEN) != 0) {
349 1.1 christos printf("WARNING: PMK mismatch\n");
350 1.1 christos wpa_hexdump(MSG_DEBUG, "PMK from AS",
351 1.1 christos e->authenticator_pmk, PMK_LEN);
352 1.1 christos } else if (e->radius_access_accept_received)
353 1.1 christos ret = 0;
354 1.1 christos } else if (e->authenticator_pmk_len == 16 &&
355 1.1 christos eapol_sm_get_key(e->wpa_s->eapol, pmk, 16) == 0) {
356 1.1 christos wpa_hexdump(MSG_DEBUG, "LEAP PMK from EAPOL", pmk, 16);
357 1.1 christos if (os_memcmp(pmk, e->authenticator_pmk, 16) != 0) {
358 1.1 christos printf("WARNING: PMK mismatch\n");
359 1.1 christos wpa_hexdump(MSG_DEBUG, "PMK from AS",
360 1.1 christos e->authenticator_pmk, 16);
361 1.1 christos } else if (e->radius_access_accept_received)
362 1.1 christos ret = 0;
363 1.1 christos } else if (e->radius_access_accept_received && e->no_mppe_keys) {
364 1.1 christos /* No keying material expected */
365 1.1 christos ret = 0;
366 1.1 christos }
367 1.1 christos
368 1.1 christos if (ret && !e->no_mppe_keys)
369 1.1 christos e->num_mppe_mismatch++;
370 1.1 christos else if (!e->no_mppe_keys)
371 1.1 christos e->num_mppe_ok++;
372 1.1 christos
373 1.1 christos return ret;
374 1.1 christos }
375 1.1 christos
376 1.1 christos
377 1.1 christos static void eapol_sm_cb(struct eapol_sm *eapol, int success, void *ctx)
378 1.1 christos {
379 1.1 christos struct eapol_test_data *e = ctx;
380 1.1 christos printf("eapol_sm_cb: success=%d\n", success);
381 1.1 christos e->eapol_test_num_reauths--;
382 1.1 christos if (e->eapol_test_num_reauths < 0)
383 1.1 christos eloop_terminate();
384 1.1 christos else {
385 1.1 christos eapol_test_compare_pmk(e);
386 1.1 christos eloop_register_timeout(0, 100000, eapol_sm_reauth, e, NULL);
387 1.1 christos }
388 1.1 christos }
389 1.1 christos
390 1.1 christos
391 1.1.1.1.6.1 yamt static void eapol_test_write_cert(FILE *f, const char *subject,
392 1.1.1.1.6.1 yamt const struct wpabuf *cert)
393 1.1.1.1.6.1 yamt {
394 1.1.1.1.6.1 yamt unsigned char *encoded;
395 1.1.1.1.6.1 yamt
396 1.1.1.1.6.1 yamt encoded = base64_encode(wpabuf_head(cert), wpabuf_len(cert), NULL);
397 1.1.1.1.6.1 yamt if (encoded == NULL)
398 1.1.1.1.6.1 yamt return;
399 1.1.1.1.6.1 yamt fprintf(f, "%s\n-----BEGIN CERTIFICATE-----\n%s"
400 1.1.1.1.6.1 yamt "-----END CERTIFICATE-----\n\n", subject, encoded);
401 1.1.1.1.6.1 yamt os_free(encoded);
402 1.1.1.1.6.1 yamt }
403 1.1.1.1.6.1 yamt
404 1.1.1.1.6.1 yamt
405 1.1.1.1.6.1 yamt static void eapol_test_cert_cb(void *ctx, int depth, const char *subject,
406 1.1.1.1.6.1 yamt const char *cert_hash,
407 1.1.1.1.6.1 yamt const struct wpabuf *cert)
408 1.1.1.1.6.1 yamt {
409 1.1.1.1.6.1 yamt struct eapol_test_data *e = ctx;
410 1.1.1.1.6.1 yamt
411 1.1.1.1.6.1 yamt wpa_msg(e->wpa_s, MSG_INFO, WPA_EVENT_EAP_PEER_CERT
412 1.1.1.1.6.1 yamt "depth=%d subject='%s'%s%s",
413 1.1.1.1.6.1 yamt depth, subject,
414 1.1.1.1.6.1 yamt cert_hash ? " hash=" : "",
415 1.1.1.1.6.1 yamt cert_hash ? cert_hash : "");
416 1.1.1.1.6.1 yamt
417 1.1.1.1.6.1 yamt if (cert) {
418 1.1.1.1.6.1 yamt char *cert_hex;
419 1.1.1.1.6.1 yamt size_t len = wpabuf_len(cert) * 2 + 1;
420 1.1.1.1.6.1 yamt cert_hex = os_malloc(len);
421 1.1.1.1.6.1 yamt if (cert_hex) {
422 1.1.1.1.6.1 yamt wpa_snprintf_hex(cert_hex, len, wpabuf_head(cert),
423 1.1.1.1.6.1 yamt wpabuf_len(cert));
424 1.1.1.1.6.1 yamt wpa_msg_ctrl(e->wpa_s, MSG_INFO,
425 1.1.1.1.6.1 yamt WPA_EVENT_EAP_PEER_CERT
426 1.1.1.1.6.1 yamt "depth=%d subject='%s' cert=%s",
427 1.1.1.1.6.1 yamt depth, subject, cert_hex);
428 1.1.1.1.6.1 yamt os_free(cert_hex);
429 1.1.1.1.6.1 yamt }
430 1.1.1.1.6.1 yamt
431 1.1.1.1.6.1 yamt if (e->server_cert_file)
432 1.1.1.1.6.1 yamt eapol_test_write_cert(e->server_cert_file,
433 1.1.1.1.6.1 yamt subject, cert);
434 1.1.1.1.6.1 yamt }
435 1.1.1.1.6.1 yamt }
436 1.1.1.1.6.1 yamt
437 1.1.1.1.6.1 yamt
438 1.1 christos static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
439 1.1 christos struct wpa_ssid *ssid)
440 1.1 christos {
441 1.1 christos struct eapol_config eapol_conf;
442 1.1 christos struct eapol_ctx *ctx;
443 1.1 christos
444 1.1 christos ctx = os_zalloc(sizeof(*ctx));
445 1.1 christos if (ctx == NULL) {
446 1.1 christos printf("Failed to allocate EAPOL context.\n");
447 1.1 christos return -1;
448 1.1 christos }
449 1.1.1.1.6.1 yamt ctx->ctx = e;
450 1.1 christos ctx->msg_ctx = wpa_s;
451 1.1 christos ctx->scard_ctx = wpa_s->scard;
452 1.1 christos ctx->cb = eapol_sm_cb;
453 1.1 christos ctx->cb_ctx = e;
454 1.1 christos ctx->eapol_send_ctx = wpa_s;
455 1.1 christos ctx->preauth = 0;
456 1.1 christos ctx->eapol_done_cb = eapol_test_eapol_done_cb;
457 1.1 christos ctx->eapol_send = eapol_test_eapol_send;
458 1.1 christos ctx->set_config_blob = eapol_test_set_config_blob;
459 1.1 christos ctx->get_config_blob = eapol_test_get_config_blob;
460 1.1 christos ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
461 1.1 christos ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
462 1.1 christos ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
463 1.1.1.1.6.1 yamt ctx->cert_cb = eapol_test_cert_cb;
464 1.1.1.1.6.1 yamt ctx->cert_in_cb = 1;
465 1.1 christos
466 1.1 christos wpa_s->eapol = eapol_sm_init(ctx);
467 1.1 christos if (wpa_s->eapol == NULL) {
468 1.1 christos os_free(ctx);
469 1.1 christos printf("Failed to initialize EAPOL state machines.\n");
470 1.1 christos return -1;
471 1.1 christos }
472 1.1 christos
473 1.1 christos wpa_s->current_ssid = ssid;
474 1.1 christos os_memset(&eapol_conf, 0, sizeof(eapol_conf));
475 1.1 christos eapol_conf.accept_802_1x_keys = 1;
476 1.1 christos eapol_conf.required_keys = 0;
477 1.1 christos eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
478 1.1 christos eapol_conf.workaround = ssid->eap_workaround;
479 1.1 christos eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
480 1.1 christos eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
481 1.1 christos
482 1.1 christos
483 1.1 christos eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
484 1.1 christos /* 802.1X::portControl = Auto */
485 1.1 christos eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
486 1.1 christos
487 1.1 christos return 0;
488 1.1 christos }
489 1.1 christos
490 1.1 christos
491 1.1 christos static void test_eapol_clean(struct eapol_test_data *e,
492 1.1 christos struct wpa_supplicant *wpa_s)
493 1.1 christos {
494 1.1 christos struct extra_radius_attr *p, *prev;
495 1.1 christos
496 1.1 christos radius_client_deinit(e->radius);
497 1.1 christos os_free(e->last_eap_radius);
498 1.1 christos radius_msg_free(e->last_recv_radius);
499 1.1 christos e->last_recv_radius = NULL;
500 1.1 christos os_free(e->eap_identity);
501 1.1 christos e->eap_identity = NULL;
502 1.1 christos eapol_sm_deinit(wpa_s->eapol);
503 1.1 christos wpa_s->eapol = NULL;
504 1.1 christos if (e->radius_conf && e->radius_conf->auth_server) {
505 1.1 christos os_free(e->radius_conf->auth_server->shared_secret);
506 1.1 christos os_free(e->radius_conf->auth_server);
507 1.1 christos }
508 1.1 christos os_free(e->radius_conf);
509 1.1 christos e->radius_conf = NULL;
510 1.1 christos scard_deinit(wpa_s->scard);
511 1.1 christos if (wpa_s->ctrl_iface) {
512 1.1 christos wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
513 1.1 christos wpa_s->ctrl_iface = NULL;
514 1.1 christos }
515 1.1 christos wpa_config_free(wpa_s->conf);
516 1.1 christos
517 1.1 christos p = e->extra_attrs;
518 1.1 christos while (p) {
519 1.1 christos prev = p;
520 1.1 christos p = p->next;
521 1.1 christos os_free(prev);
522 1.1 christos }
523 1.1 christos }
524 1.1 christos
525 1.1 christos
526 1.1 christos static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx)
527 1.1 christos {
528 1.1 christos struct wpa_supplicant *wpa_s = eloop_ctx;
529 1.1 christos u8 buf[100], *pos;
530 1.1 christos struct ieee802_1x_hdr *hdr;
531 1.1 christos struct eap_hdr *eap;
532 1.1 christos
533 1.1 christos hdr = (struct ieee802_1x_hdr *) buf;
534 1.1 christos hdr->version = EAPOL_VERSION;
535 1.1 christos hdr->type = IEEE802_1X_TYPE_EAP_PACKET;
536 1.1 christos hdr->length = htons(5);
537 1.1 christos
538 1.1 christos eap = (struct eap_hdr *) (hdr + 1);
539 1.1 christos eap->code = EAP_CODE_REQUEST;
540 1.1 christos eap->identifier = 0;
541 1.1 christos eap->length = htons(5);
542 1.1 christos pos = (u8 *) (eap + 1);
543 1.1 christos *pos = EAP_TYPE_IDENTITY;
544 1.1 christos
545 1.1 christos printf("Sending fake EAP-Request-Identity\n");
546 1.1 christos eapol_sm_rx_eapol(wpa_s->eapol, wpa_s->bssid, buf,
547 1.1 christos sizeof(*hdr) + 5);
548 1.1 christos }
549 1.1 christos
550 1.1 christos
551 1.1 christos static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
552 1.1 christos {
553 1.1 christos struct eapol_test_data *e = eloop_ctx;
554 1.1 christos printf("EAPOL test timed out\n");
555 1.1 christos e->auth_timed_out = 1;
556 1.1 christos eloop_terminate();
557 1.1 christos }
558 1.1 christos
559 1.1 christos
560 1.1 christos static char *eap_type_text(u8 type)
561 1.1 christos {
562 1.1 christos switch (type) {
563 1.1 christos case EAP_TYPE_IDENTITY: return "Identity";
564 1.1 christos case EAP_TYPE_NOTIFICATION: return "Notification";
565 1.1 christos case EAP_TYPE_NAK: return "Nak";
566 1.1 christos case EAP_TYPE_TLS: return "TLS";
567 1.1 christos case EAP_TYPE_TTLS: return "TTLS";
568 1.1 christos case EAP_TYPE_PEAP: return "PEAP";
569 1.1 christos case EAP_TYPE_SIM: return "SIM";
570 1.1 christos case EAP_TYPE_GTC: return "GTC";
571 1.1 christos case EAP_TYPE_MD5: return "MD5";
572 1.1 christos case EAP_TYPE_OTP: return "OTP";
573 1.1 christos case EAP_TYPE_FAST: return "FAST";
574 1.1 christos case EAP_TYPE_SAKE: return "SAKE";
575 1.1 christos case EAP_TYPE_PSK: return "PSK";
576 1.1 christos default: return "Unknown";
577 1.1 christos }
578 1.1 christos }
579 1.1 christos
580 1.1 christos
581 1.1 christos static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
582 1.1 christos {
583 1.1 christos u8 *eap;
584 1.1 christos size_t len;
585 1.1 christos struct eap_hdr *hdr;
586 1.1 christos int eap_type = -1;
587 1.1 christos char buf[64];
588 1.1 christos struct radius_msg *msg;
589 1.1 christos
590 1.1 christos if (e->last_recv_radius == NULL)
591 1.1 christos return;
592 1.1 christos
593 1.1 christos msg = e->last_recv_radius;
594 1.1 christos
595 1.1 christos eap = radius_msg_get_eap(msg, &len);
596 1.1 christos if (eap == NULL) {
597 1.1 christos /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
598 1.1 christos * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
599 1.1 christos * attribute */
600 1.1 christos wpa_printf(MSG_DEBUG, "could not extract "
601 1.1 christos "EAP-Message from RADIUS message");
602 1.1 christos os_free(e->last_eap_radius);
603 1.1 christos e->last_eap_radius = NULL;
604 1.1 christos e->last_eap_radius_len = 0;
605 1.1 christos return;
606 1.1 christos }
607 1.1 christos
608 1.1 christos if (len < sizeof(*hdr)) {
609 1.1 christos wpa_printf(MSG_DEBUG, "too short EAP packet "
610 1.1 christos "received from authentication server");
611 1.1 christos os_free(eap);
612 1.1 christos return;
613 1.1 christos }
614 1.1 christos
615 1.1 christos if (len > sizeof(*hdr))
616 1.1 christos eap_type = eap[sizeof(*hdr)];
617 1.1 christos
618 1.1 christos hdr = (struct eap_hdr *) eap;
619 1.1 christos switch (hdr->code) {
620 1.1 christos case EAP_CODE_REQUEST:
621 1.1 christos os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
622 1.1 christos eap_type >= 0 ? eap_type_text(eap_type) : "??",
623 1.1 christos eap_type);
624 1.1 christos break;
625 1.1 christos case EAP_CODE_RESPONSE:
626 1.1 christos os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
627 1.1 christos eap_type >= 0 ? eap_type_text(eap_type) : "??",
628 1.1 christos eap_type);
629 1.1 christos break;
630 1.1 christos case EAP_CODE_SUCCESS:
631 1.1 christos os_strlcpy(buf, "EAP Success", sizeof(buf));
632 1.1 christos /* LEAP uses EAP Success within an authentication, so must not
633 1.1 christos * stop here with eloop_terminate(); */
634 1.1 christos break;
635 1.1 christos case EAP_CODE_FAILURE:
636 1.1 christos os_strlcpy(buf, "EAP Failure", sizeof(buf));
637 1.1 christos eloop_terminate();
638 1.1 christos break;
639 1.1 christos default:
640 1.1 christos os_strlcpy(buf, "unknown EAP code", sizeof(buf));
641 1.1 christos wpa_hexdump(MSG_DEBUG, "Decapsulated EAP packet", eap, len);
642 1.1 christos break;
643 1.1 christos }
644 1.1 christos wpa_printf(MSG_DEBUG, "decapsulated EAP packet (code=%d "
645 1.1 christos "id=%d len=%d) from RADIUS server: %s",
646 1.1 christos hdr->code, hdr->identifier, ntohs(hdr->length), buf);
647 1.1 christos
648 1.1 christos /* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
649 1.1 christos
650 1.1 christos os_free(e->last_eap_radius);
651 1.1 christos e->last_eap_radius = eap;
652 1.1 christos e->last_eap_radius_len = len;
653 1.1 christos
654 1.1 christos {
655 1.1 christos struct ieee802_1x_hdr *dot1x;
656 1.1 christos dot1x = os_malloc(sizeof(*dot1x) + len);
657 1.1 christos assert(dot1x != NULL);
658 1.1 christos dot1x->version = EAPOL_VERSION;
659 1.1 christos dot1x->type = IEEE802_1X_TYPE_EAP_PACKET;
660 1.1 christos dot1x->length = htons(len);
661 1.1 christos os_memcpy((u8 *) (dot1x + 1), eap, len);
662 1.1 christos eapol_sm_rx_eapol(e->wpa_s->eapol, e->wpa_s->bssid,
663 1.1 christos (u8 *) dot1x, sizeof(*dot1x) + len);
664 1.1 christos os_free(dot1x);
665 1.1 christos }
666 1.1 christos }
667 1.1 christos
668 1.1 christos
669 1.1 christos static void ieee802_1x_get_keys(struct eapol_test_data *e,
670 1.1 christos struct radius_msg *msg, struct radius_msg *req,
671 1.1 christos const u8 *shared_secret,
672 1.1 christos size_t shared_secret_len)
673 1.1 christos {
674 1.1 christos struct radius_ms_mppe_keys *keys;
675 1.1 christos
676 1.1 christos keys = radius_msg_get_ms_keys(msg, req, shared_secret,
677 1.1 christos shared_secret_len);
678 1.1 christos if (keys && keys->send == NULL && keys->recv == NULL) {
679 1.1 christos os_free(keys);
680 1.1 christos keys = radius_msg_get_cisco_keys(msg, req, shared_secret,
681 1.1 christos shared_secret_len);
682 1.1 christos }
683 1.1 christos
684 1.1 christos if (keys) {
685 1.1 christos if (keys->send) {
686 1.1 christos wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)",
687 1.1 christos keys->send, keys->send_len);
688 1.1 christos }
689 1.1 christos if (keys->recv) {
690 1.1 christos wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)",
691 1.1 christos keys->recv, keys->recv_len);
692 1.1 christos e->authenticator_pmk_len =
693 1.1 christos keys->recv_len > PMK_LEN ? PMK_LEN :
694 1.1 christos keys->recv_len;
695 1.1 christos os_memcpy(e->authenticator_pmk, keys->recv,
696 1.1 christos e->authenticator_pmk_len);
697 1.1 christos if (e->authenticator_pmk_len == 16 && keys->send &&
698 1.1 christos keys->send_len == 16) {
699 1.1 christos /* MS-CHAP-v2 derives 16 octet keys */
700 1.1 christos wpa_printf(MSG_DEBUG, "Use MS-MPPE-Send-Key "
701 1.1 christos "to extend PMK to 32 octets");
702 1.1 christos os_memcpy(e->authenticator_pmk +
703 1.1 christos e->authenticator_pmk_len,
704 1.1 christos keys->send, keys->send_len);
705 1.1 christos e->authenticator_pmk_len += keys->send_len;
706 1.1 christos }
707 1.1 christos }
708 1.1 christos
709 1.1 christos os_free(keys->send);
710 1.1 christos os_free(keys->recv);
711 1.1 christos os_free(keys);
712 1.1 christos }
713 1.1 christos }
714 1.1 christos
715 1.1 christos
716 1.1 christos /* Process the RADIUS frames from Authentication Server */
717 1.1 christos static RadiusRxResult
718 1.1 christos ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
719 1.1 christos const u8 *shared_secret, size_t shared_secret_len,
720 1.1 christos void *data)
721 1.1 christos {
722 1.1 christos struct eapol_test_data *e = data;
723 1.1 christos struct radius_hdr *hdr = radius_msg_get_hdr(msg);
724 1.1 christos
725 1.1 christos /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
726 1.1 christos * present when packet contains an EAP-Message attribute */
727 1.1 christos if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
728 1.1 christos radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
729 1.1 christos 0) < 0 &&
730 1.1 christos radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
731 1.1 christos wpa_printf(MSG_DEBUG, "Allowing RADIUS "
732 1.1 christos "Access-Reject without Message-Authenticator "
733 1.1 christos "since it does not include EAP-Message\n");
734 1.1 christos } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
735 1.1 christos req, 1)) {
736 1.1 christos printf("Incoming RADIUS packet did not have correct "
737 1.1 christos "Message-Authenticator - dropped\n");
738 1.1 christos return RADIUS_RX_UNKNOWN;
739 1.1 christos }
740 1.1 christos
741 1.1 christos if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
742 1.1 christos hdr->code != RADIUS_CODE_ACCESS_REJECT &&
743 1.1 christos hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
744 1.1 christos printf("Unknown RADIUS message code\n");
745 1.1 christos return RADIUS_RX_UNKNOWN;
746 1.1 christos }
747 1.1 christos
748 1.1 christos e->radius_identifier = -1;
749 1.1 christos wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
750 1.1 christos
751 1.1 christos radius_msg_free(e->last_recv_radius);
752 1.1 christos e->last_recv_radius = msg;
753 1.1 christos
754 1.1 christos switch (hdr->code) {
755 1.1 christos case RADIUS_CODE_ACCESS_ACCEPT:
756 1.1 christos e->radius_access_accept_received = 1;
757 1.1 christos ieee802_1x_get_keys(e, msg, req, shared_secret,
758 1.1 christos shared_secret_len);
759 1.1 christos break;
760 1.1 christos case RADIUS_CODE_ACCESS_REJECT:
761 1.1 christos e->radius_access_reject_received = 1;
762 1.1 christos break;
763 1.1 christos }
764 1.1 christos
765 1.1 christos ieee802_1x_decapsulate_radius(e);
766 1.1 christos
767 1.1 christos if ((hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
768 1.1 christos e->eapol_test_num_reauths < 0) ||
769 1.1 christos hdr->code == RADIUS_CODE_ACCESS_REJECT) {
770 1.1 christos eloop_terminate();
771 1.1 christos }
772 1.1 christos
773 1.1 christos return RADIUS_RX_QUEUED;
774 1.1 christos }
775 1.1 christos
776 1.1 christos
777 1.1 christos static void wpa_init_conf(struct eapol_test_data *e,
778 1.1 christos struct wpa_supplicant *wpa_s, const char *authsrv,
779 1.1 christos int port, const char *secret,
780 1.1 christos const char *cli_addr)
781 1.1 christos {
782 1.1 christos struct hostapd_radius_server *as;
783 1.1 christos int res;
784 1.1 christos
785 1.1 christos wpa_s->bssid[5] = 1;
786 1.1 christos os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN);
787 1.1 christos e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
788 1.1 christos os_strlcpy(wpa_s->ifname, "test", sizeof(wpa_s->ifname));
789 1.1 christos
790 1.1 christos e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers));
791 1.1 christos assert(e->radius_conf != NULL);
792 1.1 christos e->radius_conf->num_auth_servers = 1;
793 1.1 christos as = os_zalloc(sizeof(struct hostapd_radius_server));
794 1.1 christos assert(as != NULL);
795 1.1 christos #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
796 1.1 christos {
797 1.1 christos int a[4];
798 1.1 christos u8 *pos;
799 1.1 christos sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
800 1.1 christos pos = (u8 *) &as->addr.u.v4;
801 1.1 christos *pos++ = a[0];
802 1.1 christos *pos++ = a[1];
803 1.1 christos *pos++ = a[2];
804 1.1 christos *pos++ = a[3];
805 1.1 christos }
806 1.1 christos #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
807 1.1 christos inet_aton(authsrv, &as->addr.u.v4);
808 1.1 christos #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
809 1.1 christos as->addr.af = AF_INET;
810 1.1 christos as->port = port;
811 1.1 christos as->shared_secret = (u8 *) os_strdup(secret);
812 1.1 christos as->shared_secret_len = os_strlen(secret);
813 1.1 christos e->radius_conf->auth_server = as;
814 1.1 christos e->radius_conf->auth_servers = as;
815 1.1 christos e->radius_conf->msg_dumps = 1;
816 1.1 christos if (cli_addr) {
817 1.1 christos if (hostapd_parse_ip_addr(cli_addr,
818 1.1 christos &e->radius_conf->client_addr) == 0)
819 1.1 christos e->radius_conf->force_client_addr = 1;
820 1.1 christos else {
821 1.1 christos wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
822 1.1 christos cli_addr);
823 1.1 christos assert(0);
824 1.1 christos }
825 1.1 christos }
826 1.1 christos
827 1.1 christos e->radius = radius_client_init(wpa_s, e->radius_conf);
828 1.1 christos assert(e->radius != NULL);
829 1.1 christos
830 1.1 christos res = radius_client_register(e->radius, RADIUS_AUTH,
831 1.1 christos ieee802_1x_receive_auth, e);
832 1.1 christos assert(res == 0);
833 1.1 christos }
834 1.1 christos
835 1.1 christos
836 1.1 christos static int scard_test(void)
837 1.1 christos {
838 1.1 christos struct scard_data *scard;
839 1.1 christos size_t len;
840 1.1 christos char imsi[20];
841 1.1 christos unsigned char _rand[16];
842 1.1 christos #ifdef PCSC_FUNCS
843 1.1 christos unsigned char sres[4];
844 1.1 christos unsigned char kc[8];
845 1.1 christos #endif /* PCSC_FUNCS */
846 1.1 christos #define num_triplets 5
847 1.1 christos unsigned char rand_[num_triplets][16];
848 1.1 christos unsigned char sres_[num_triplets][4];
849 1.1 christos unsigned char kc_[num_triplets][8];
850 1.1 christos int i, res;
851 1.1 christos size_t j;
852 1.1 christos
853 1.1 christos #define AKA_RAND_LEN 16
854 1.1 christos #define AKA_AUTN_LEN 16
855 1.1 christos #define AKA_AUTS_LEN 14
856 1.1 christos #define RES_MAX_LEN 16
857 1.1 christos #define IK_LEN 16
858 1.1 christos #define CK_LEN 16
859 1.1 christos unsigned char aka_rand[AKA_RAND_LEN];
860 1.1 christos unsigned char aka_autn[AKA_AUTN_LEN];
861 1.1 christos unsigned char aka_auts[AKA_AUTS_LEN];
862 1.1 christos unsigned char aka_res[RES_MAX_LEN];
863 1.1 christos size_t aka_res_len;
864 1.1 christos unsigned char aka_ik[IK_LEN];
865 1.1 christos unsigned char aka_ck[CK_LEN];
866 1.1 christos
867 1.1 christos scard = scard_init(SCARD_TRY_BOTH);
868 1.1 christos if (scard == NULL)
869 1.1 christos return -1;
870 1.1 christos if (scard_set_pin(scard, "1234")) {
871 1.1 christos wpa_printf(MSG_WARNING, "PIN validation failed");
872 1.1 christos scard_deinit(scard);
873 1.1 christos return -1;
874 1.1 christos }
875 1.1 christos
876 1.1 christos len = sizeof(imsi);
877 1.1 christos if (scard_get_imsi(scard, imsi, &len))
878 1.1 christos goto failed;
879 1.1 christos wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
880 1.1 christos /* NOTE: Permanent Username: 1 | IMSI */
881 1.1 christos
882 1.1 christos os_memset(_rand, 0, sizeof(_rand));
883 1.1 christos if (scard_gsm_auth(scard, _rand, sres, kc))
884 1.1 christos goto failed;
885 1.1 christos
886 1.1 christos os_memset(_rand, 0xff, sizeof(_rand));
887 1.1 christos if (scard_gsm_auth(scard, _rand, sres, kc))
888 1.1 christos goto failed;
889 1.1 christos
890 1.1 christos for (i = 0; i < num_triplets; i++) {
891 1.1 christos os_memset(rand_[i], i, sizeof(rand_[i]));
892 1.1 christos if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
893 1.1 christos goto failed;
894 1.1 christos }
895 1.1 christos
896 1.1 christos for (i = 0; i < num_triplets; i++) {
897 1.1 christos printf("1");
898 1.1 christos for (j = 0; j < len; j++)
899 1.1 christos printf("%c", imsi[j]);
900 1.1 christos printf(",");
901 1.1 christos for (j = 0; j < 16; j++)
902 1.1 christos printf("%02X", rand_[i][j]);
903 1.1 christos printf(",");
904 1.1 christos for (j = 0; j < 4; j++)
905 1.1 christos printf("%02X", sres_[i][j]);
906 1.1 christos printf(",");
907 1.1 christos for (j = 0; j < 8; j++)
908 1.1 christos printf("%02X", kc_[i][j]);
909 1.1 christos printf("\n");
910 1.1 christos }
911 1.1 christos
912 1.1 christos wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
913 1.1 christos
914 1.1 christos /* seq 39 (0x28) */
915 1.1 christos os_memset(aka_rand, 0xaa, 16);
916 1.1 christos os_memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
917 1.1 christos "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
918 1.1 christos
919 1.1 christos res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
920 1.1 christos aka_ik, aka_ck, aka_auts);
921 1.1 christos if (res == 0) {
922 1.1 christos wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
923 1.1 christos wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
924 1.1 christos wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
925 1.1 christos wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
926 1.1 christos } else if (res == -2) {
927 1.1 christos wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
928 1.1 christos "failure");
929 1.1 christos wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
930 1.1 christos } else {
931 1.1 christos wpa_printf(MSG_DEBUG, "UMTS auth failed");
932 1.1 christos }
933 1.1 christos
934 1.1 christos failed:
935 1.1 christos scard_deinit(scard);
936 1.1 christos
937 1.1 christos return 0;
938 1.1 christos #undef num_triplets
939 1.1 christos }
940 1.1 christos
941 1.1 christos
942 1.1 christos static int scard_get_triplets(int argc, char *argv[])
943 1.1 christos {
944 1.1 christos struct scard_data *scard;
945 1.1 christos size_t len;
946 1.1 christos char imsi[20];
947 1.1 christos unsigned char _rand[16];
948 1.1 christos unsigned char sres[4];
949 1.1 christos unsigned char kc[8];
950 1.1 christos int num_triplets;
951 1.1 christos int i;
952 1.1 christos size_t j;
953 1.1 christos
954 1.1 christos if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
955 1.1 christos printf("invalid parameters for sim command\n");
956 1.1 christos return -1;
957 1.1 christos }
958 1.1 christos
959 1.1 christos if (argc <= 2 || os_strcmp(argv[2], "debug") != 0) {
960 1.1 christos /* disable debug output */
961 1.1 christos wpa_debug_level = 99;
962 1.1 christos }
963 1.1 christos
964 1.1 christos scard = scard_init(SCARD_GSM_SIM_ONLY);
965 1.1 christos if (scard == NULL) {
966 1.1 christos printf("Failed to open smartcard connection\n");
967 1.1 christos return -1;
968 1.1 christos }
969 1.1 christos if (scard_set_pin(scard, argv[0])) {
970 1.1 christos wpa_printf(MSG_WARNING, "PIN validation failed");
971 1.1 christos scard_deinit(scard);
972 1.1 christos return -1;
973 1.1 christos }
974 1.1 christos
975 1.1 christos len = sizeof(imsi);
976 1.1 christos if (scard_get_imsi(scard, imsi, &len)) {
977 1.1 christos scard_deinit(scard);
978 1.1 christos return -1;
979 1.1 christos }
980 1.1 christos
981 1.1 christos for (i = 0; i < num_triplets; i++) {
982 1.1 christos os_memset(_rand, i, sizeof(_rand));
983 1.1 christos if (scard_gsm_auth(scard, _rand, sres, kc))
984 1.1 christos break;
985 1.1 christos
986 1.1 christos /* IMSI:Kc:SRES:RAND */
987 1.1 christos for (j = 0; j < len; j++)
988 1.1 christos printf("%c", imsi[j]);
989 1.1 christos printf(":");
990 1.1 christos for (j = 0; j < 8; j++)
991 1.1 christos printf("%02X", kc[j]);
992 1.1 christos printf(":");
993 1.1 christos for (j = 0; j < 4; j++)
994 1.1 christos printf("%02X", sres[j]);
995 1.1 christos printf(":");
996 1.1 christos for (j = 0; j < 16; j++)
997 1.1 christos printf("%02X", _rand[j]);
998 1.1 christos printf("\n");
999 1.1 christos }
1000 1.1 christos
1001 1.1 christos scard_deinit(scard);
1002 1.1 christos
1003 1.1 christos return 0;
1004 1.1 christos }
1005 1.1 christos
1006 1.1 christos
1007 1.1 christos static void eapol_test_terminate(int sig, void *signal_ctx)
1008 1.1 christos {
1009 1.1 christos struct wpa_supplicant *wpa_s = signal_ctx;
1010 1.1 christos wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
1011 1.1 christos eloop_terminate();
1012 1.1 christos }
1013 1.1 christos
1014 1.1 christos
1015 1.1 christos static void usage(void)
1016 1.1 christos {
1017 1.1 christos printf("usage:\n"
1018 1.1 christos "eapol_test [-nWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
1019 1.1 christos "[-s<AS secret>]\\\n"
1020 1.1 christos " [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
1021 1.1.1.1.6.1 yamt " [-M<client MAC address>] [-o<server cert file] \\\n"
1022 1.1 christos " [-N<attr spec>] \\\n"
1023 1.1 christos " [-A<client IP>]\n"
1024 1.1 christos "eapol_test scard\n"
1025 1.1 christos "eapol_test sim <PIN> <num triplets> [debug]\n"
1026 1.1 christos "\n");
1027 1.1 christos printf("options:\n"
1028 1.1 christos " -c<conf> = configuration file\n"
1029 1.1 christos " -a<AS IP> = IP address of the authentication server, "
1030 1.1 christos "default 127.0.0.1\n"
1031 1.1 christos " -p<AS port> = UDP port of the authentication server, "
1032 1.1 christos "default 1812\n"
1033 1.1 christos " -s<AS secret> = shared secret with the authentication "
1034 1.1 christos "server, default 'radius'\n"
1035 1.1 christos " -A<client IP> = IP address of the client, default: select "
1036 1.1 christos "automatically\n"
1037 1.1 christos " -r<count> = number of re-authentications\n"
1038 1.1 christos " -W = wait for a control interface monitor before starting\n"
1039 1.1 christos " -S = save configuration after authentication\n"
1040 1.1 christos " -n = no MPPE keys expected\n"
1041 1.1 christos " -t<timeout> = sets timeout in seconds (default: 30 s)\n"
1042 1.1 christos " -C<Connect-Info> = RADIUS Connect-Info (default: "
1043 1.1 christos "CONNECT 11Mbps 802.11b)\n"
1044 1.1 christos " -M<client MAC address> = Set own MAC address "
1045 1.1 christos "(Calling-Station-Id,\n"
1046 1.1 christos " default: 02:00:00:00:00:01)\n"
1047 1.1.1.1.6.1 yamt " -o<server cert file> = Write received server certificate\n"
1048 1.1.1.1.6.1 yamt " chain to the specified file\n"
1049 1.1 christos " -N<attr spec> = send arbitrary attribute specified by:\n"
1050 1.1 christos " attr_id:syntax:value or attr_id\n"
1051 1.1 christos " attr_id - number id of the attribute\n"
1052 1.1 christos " syntax - one of: s, d, x\n"
1053 1.1 christos " s = string\n"
1054 1.1 christos " d = integer\n"
1055 1.1 christos " x = octet string\n"
1056 1.1 christos " value - attribute value.\n"
1057 1.1 christos " When only attr_id is specified, NULL will be used as "
1058 1.1 christos "value.\n"
1059 1.1 christos " Multiple attributes can be specified by using the "
1060 1.1 christos "option several times.\n");
1061 1.1 christos }
1062 1.1 christos
1063 1.1 christos
1064 1.1 christos int main(int argc, char *argv[])
1065 1.1 christos {
1066 1.1 christos struct wpa_supplicant wpa_s;
1067 1.1 christos int c, ret = 1, wait_for_monitor = 0, save_config = 0;
1068 1.1 christos char *as_addr = "127.0.0.1";
1069 1.1 christos int as_port = 1812;
1070 1.1 christos char *as_secret = "radius";
1071 1.1 christos char *cli_addr = NULL;
1072 1.1 christos char *conf = NULL;
1073 1.1 christos int timeout = 30;
1074 1.1 christos char *pos;
1075 1.1 christos struct extra_radius_attr *p = NULL, *p1;
1076 1.1 christos
1077 1.1 christos if (os_program_init())
1078 1.1 christos return -1;
1079 1.1 christos
1080 1.1 christos hostapd_logger_register_cb(hostapd_logger_cb);
1081 1.1 christos
1082 1.1 christos os_memset(&eapol_test, 0, sizeof(eapol_test));
1083 1.1 christos eapol_test.connect_info = "CONNECT 11Mbps 802.11b";
1084 1.1 christos os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN);
1085 1.1 christos
1086 1.1 christos wpa_debug_level = 0;
1087 1.1 christos wpa_debug_show_keys = 1;
1088 1.1 christos
1089 1.1 christos for (;;) {
1090 1.1.1.1.6.1 yamt c = getopt(argc, argv, "a:A:c:C:M:nN:o:p:r:s:St:W");
1091 1.1 christos if (c < 0)
1092 1.1 christos break;
1093 1.1 christos switch (c) {
1094 1.1 christos case 'a':
1095 1.1 christos as_addr = optarg;
1096 1.1 christos break;
1097 1.1 christos case 'A':
1098 1.1 christos cli_addr = optarg;
1099 1.1 christos break;
1100 1.1 christos case 'c':
1101 1.1 christos conf = optarg;
1102 1.1 christos break;
1103 1.1 christos case 'C':
1104 1.1 christos eapol_test.connect_info = optarg;
1105 1.1 christos break;
1106 1.1 christos case 'M':
1107 1.1 christos if (hwaddr_aton(optarg, eapol_test.own_addr)) {
1108 1.1 christos usage();
1109 1.1 christos return -1;
1110 1.1 christos }
1111 1.1 christos break;
1112 1.1 christos case 'n':
1113 1.1 christos eapol_test.no_mppe_keys++;
1114 1.1 christos break;
1115 1.1.1.1.6.1 yamt case 'o':
1116 1.1.1.1.6.1 yamt if (eapol_test.server_cert_file)
1117 1.1.1.1.6.1 yamt fclose(eapol_test.server_cert_file);
1118 1.1.1.1.6.1 yamt eapol_test.server_cert_file = fopen(optarg, "w");
1119 1.1.1.1.6.1 yamt if (eapol_test.server_cert_file == NULL) {
1120 1.1.1.1.6.1 yamt printf("Could not open '%s' for writing\n",
1121 1.1.1.1.6.1 yamt optarg);
1122 1.1.1.1.6.1 yamt return -1;
1123 1.1.1.1.6.1 yamt }
1124 1.1.1.1.6.1 yamt break;
1125 1.1 christos case 'p':
1126 1.1 christos as_port = atoi(optarg);
1127 1.1 christos break;
1128 1.1 christos case 'r':
1129 1.1 christos eapol_test.eapol_test_num_reauths = atoi(optarg);
1130 1.1 christos break;
1131 1.1 christos case 's':
1132 1.1 christos as_secret = optarg;
1133 1.1 christos break;
1134 1.1 christos case 'S':
1135 1.1 christos save_config++;
1136 1.1 christos break;
1137 1.1 christos case 't':
1138 1.1 christos timeout = atoi(optarg);
1139 1.1 christos break;
1140 1.1 christos case 'W':
1141 1.1 christos wait_for_monitor++;
1142 1.1 christos break;
1143 1.1 christos case 'N':
1144 1.1 christos p1 = os_zalloc(sizeof(p1));
1145 1.1 christos if (p1 == NULL)
1146 1.1 christos break;
1147 1.1 christos if (!p)
1148 1.1 christos eapol_test.extra_attrs = p1;
1149 1.1 christos else
1150 1.1 christos p->next = p1;
1151 1.1 christos p = p1;
1152 1.1 christos
1153 1.1 christos p->type = atoi(optarg);
1154 1.1 christos pos = os_strchr(optarg, ':');
1155 1.1 christos if (pos == NULL) {
1156 1.1 christos p->syntax = 'n';
1157 1.1 christos p->data = NULL;
1158 1.1 christos break;
1159 1.1 christos }
1160 1.1 christos
1161 1.1 christos pos++;
1162 1.1 christos if (pos[0] == '\0' || pos[1] != ':') {
1163 1.1 christos printf("Incorrect format of attribute "
1164 1.1 christos "specification\n");
1165 1.1 christos break;
1166 1.1 christos }
1167 1.1 christos
1168 1.1 christos p->syntax = pos[0];
1169 1.1 christos p->data = pos + 2;
1170 1.1 christos break;
1171 1.1 christos default:
1172 1.1 christos usage();
1173 1.1 christos return -1;
1174 1.1 christos }
1175 1.1 christos }
1176 1.1 christos
1177 1.1 christos if (argc > optind && os_strcmp(argv[optind], "scard") == 0) {
1178 1.1 christos return scard_test();
1179 1.1 christos }
1180 1.1 christos
1181 1.1 christos if (argc > optind && os_strcmp(argv[optind], "sim") == 0) {
1182 1.1 christos return scard_get_triplets(argc - optind - 1,
1183 1.1 christos &argv[optind + 1]);
1184 1.1 christos }
1185 1.1 christos
1186 1.1 christos if (conf == NULL) {
1187 1.1 christos usage();
1188 1.1 christos printf("Configuration file is required.\n");
1189 1.1 christos return -1;
1190 1.1 christos }
1191 1.1 christos
1192 1.1 christos if (eap_register_methods()) {
1193 1.1 christos wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1194 1.1 christos return -1;
1195 1.1 christos }
1196 1.1 christos
1197 1.1 christos if (eloop_init()) {
1198 1.1 christos wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1199 1.1 christos return -1;
1200 1.1 christos }
1201 1.1 christos
1202 1.1 christos os_memset(&wpa_s, 0, sizeof(wpa_s));
1203 1.1 christos eapol_test.wpa_s = &wpa_s;
1204 1.1 christos wpa_s.conf = wpa_config_read(conf);
1205 1.1 christos if (wpa_s.conf == NULL) {
1206 1.1 christos printf("Failed to parse configuration file '%s'.\n", conf);
1207 1.1 christos return -1;
1208 1.1 christos }
1209 1.1 christos if (wpa_s.conf->ssid == NULL) {
1210 1.1 christos printf("No networks defined.\n");
1211 1.1 christos return -1;
1212 1.1 christos }
1213 1.1 christos
1214 1.1 christos wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret,
1215 1.1 christos cli_addr);
1216 1.1 christos wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
1217 1.1 christos if (wpa_s.ctrl_iface == NULL) {
1218 1.1 christos printf("Failed to initialize control interface '%s'.\n"
1219 1.1 christos "You may have another eapol_test process already "
1220 1.1 christos "running or the file was\n"
1221 1.1 christos "left by an unclean termination of eapol_test in "
1222 1.1 christos "which case you will need\n"
1223 1.1 christos "to manually remove this file before starting "
1224 1.1 christos "eapol_test again.\n",
1225 1.1 christos wpa_s.conf->ctrl_interface);
1226 1.1 christos return -1;
1227 1.1 christos }
1228 1.1 christos if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
1229 1.1 christos return -1;
1230 1.1 christos
1231 1.1 christos if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
1232 1.1 christos return -1;
1233 1.1 christos
1234 1.1 christos if (wait_for_monitor)
1235 1.1 christos wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface);
1236 1.1 christos
1237 1.1 christos eloop_register_timeout(timeout, 0, eapol_test_timeout, &eapol_test,
1238 1.1 christos NULL);
1239 1.1 christos eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s, NULL);
1240 1.1 christos eloop_register_signal_terminate(eapol_test_terminate, &wpa_s);
1241 1.1 christos eloop_register_signal_reconfig(eapol_test_terminate, &wpa_s);
1242 1.1 christos eloop_run();
1243 1.1 christos
1244 1.1 christos eloop_cancel_timeout(eapol_test_timeout, &eapol_test, NULL);
1245 1.1 christos eloop_cancel_timeout(eapol_sm_reauth, &eapol_test, NULL);
1246 1.1 christos
1247 1.1 christos if (eapol_test_compare_pmk(&eapol_test) == 0 ||
1248 1.1 christos eapol_test.no_mppe_keys)
1249 1.1 christos ret = 0;
1250 1.1 christos if (eapol_test.auth_timed_out)
1251 1.1 christos ret = -2;
1252 1.1 christos if (eapol_test.radius_access_reject_received)
1253 1.1 christos ret = -3;
1254 1.1 christos
1255 1.1 christos if (save_config)
1256 1.1 christos wpa_config_write(conf, wpa_s.conf);
1257 1.1 christos
1258 1.1 christos test_eapol_clean(&eapol_test, &wpa_s);
1259 1.1 christos
1260 1.1 christos eap_peer_unregister_methods();
1261 1.1.1.1.6.1 yamt #ifdef CONFIG_AP
1262 1.1.1.1.6.1 yamt eap_server_unregister_methods();
1263 1.1.1.1.6.1 yamt #endif /* CONFIG_AP */
1264 1.1 christos
1265 1.1 christos eloop_destroy();
1266 1.1 christos
1267 1.1.1.1.6.1 yamt if (eapol_test.server_cert_file)
1268 1.1.1.1.6.1 yamt fclose(eapol_test.server_cert_file);
1269 1.1.1.1.6.1 yamt
1270 1.1 christos printf("MPPE keys OK: %d mismatch: %d\n",
1271 1.1 christos eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
1272 1.1 christos if (eapol_test.num_mppe_mismatch)
1273 1.1 christos ret = -4;
1274 1.1 christos if (ret)
1275 1.1 christos printf("FAILURE\n");
1276 1.1 christos else
1277 1.1 christos printf("SUCCESS\n");
1278 1.1 christos
1279 1.1 christos os_program_deinit();
1280 1.1 christos
1281 1.1 christos return ret;
1282 1.1 christos }
1283