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