2 * WPA Supplicant - test code
3 * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
14 * IEEE 802.1X Supplicant test code (to be used in place of wpa_supplicant.c.
15 * Not used in production version.
23 #include "eapol_supp/eapol_supp_sm.h"
24 #include "eap_peer/eap.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"
35 extern int wpa_debug_level
;
36 extern int wpa_debug_show_keys
;
38 struct wpa_driver_ops
*wpa_supplicant_drivers
[] = { NULL
};
41 struct eapol_test_data
{
42 struct wpa_supplicant
*wpa_s
;
44 int eapol_test_num_reauths
;
46 int num_mppe_ok
, num_mppe_mismatch
;
49 struct radius_msg
*last_recv_radius
;
50 struct in_addr own_ip_addr
;
51 struct radius_client_data
*radius
;
52 struct hostapd_radius_servers
*radius_conf
;
54 u8
*last_eap_radius
; /* last received EAP Response from Authentication
56 size_t last_eap_radius_len
;
58 u8 authenticator_pmk
[PMK_LEN
];
59 size_t authenticator_pmk_len
;
60 int radius_access_accept_received
;
61 int radius_access_reject_received
;
65 size_t eap_identity_len
;
68 u8 own_addr
[ETH_ALEN
];
73 static struct eapol_test_data eapol_test
;
76 static void send_eap_request_identity(void *eloop_ctx
, void *timeout_ctx
);
79 static void hostapd_logger_cb(void *ctx
, const u8
*addr
, unsigned int module
,
80 int level
, const char *txt
, size_t len
)
83 wpa_printf(MSG_DEBUG
, "STA " MACSTR
": %s\n",
86 wpa_printf(MSG_DEBUG
, "%s", txt
);
90 static void ieee802_1x_encapsulate_radius(struct eapol_test_data
*e
,
91 const u8
*eap
, size_t len
)
93 struct radius_msg
*msg
;
95 const struct eap_hdr
*hdr
;
98 wpa_printf(MSG_DEBUG
, "Encapsulating EAP message into a RADIUS "
101 e
->radius_identifier
= radius_client_get_id(e
->radius
);
102 msg
= radius_msg_new(RADIUS_CODE_ACCESS_REQUEST
,
103 e
->radius_identifier
);
105 printf("Could not create net RADIUS packet\n");
109 radius_msg_make_authenticator(msg
, (u8
*) e
, sizeof(*e
));
111 hdr
= (const struct eap_hdr
*) eap
;
112 pos
= (const u8
*) (hdr
+ 1);
113 if (len
> sizeof(*hdr
) && hdr
->code
== EAP_CODE_RESPONSE
&&
114 pos
[0] == EAP_TYPE_IDENTITY
) {
116 os_free(e
->eap_identity
);
117 e
->eap_identity_len
= len
- sizeof(*hdr
) - 1;
118 e
->eap_identity
= os_malloc(e
->eap_identity_len
);
119 if (e
->eap_identity
) {
120 os_memcpy(e
->eap_identity
, pos
, e
->eap_identity_len
);
121 wpa_hexdump(MSG_DEBUG
, "Learned identity from "
122 "EAP-Response-Identity",
123 e
->eap_identity
, e
->eap_identity_len
);
127 if (e
->eap_identity
&&
128 !radius_msg_add_attr(msg
, RADIUS_ATTR_USER_NAME
,
129 e
->eap_identity
, e
->eap_identity_len
)) {
130 printf("Could not add User-Name\n");
134 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_NAS_IP_ADDRESS
,
135 (u8
*) &e
->own_ip_addr
, 4)) {
136 printf("Could not add NAS-IP-Address\n");
140 os_snprintf(buf
, sizeof(buf
), RADIUS_802_1X_ADDR_FORMAT
,
141 MAC2STR(e
->wpa_s
->own_addr
));
142 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_CALLING_STATION_ID
,
143 (u8
*) buf
, os_strlen(buf
))) {
144 printf("Could not add Calling-Station-Id\n");
148 /* TODO: should probably check MTU from driver config; 2304 is max for
149 * IEEE 802.11, but use 1400 to avoid problems with too large packets
151 if (!radius_msg_add_attr_int32(msg
, RADIUS_ATTR_FRAMED_MTU
, 1400)) {
152 printf("Could not add Framed-MTU\n");
156 if (!radius_msg_add_attr_int32(msg
, RADIUS_ATTR_NAS_PORT_TYPE
,
157 RADIUS_NAS_PORT_TYPE_IEEE_802_11
)) {
158 printf("Could not add NAS-Port-Type\n");
162 os_snprintf(buf
, sizeof(buf
), "%s", e
->connect_info
);
163 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_CONNECT_INFO
,
164 (u8
*) buf
, os_strlen(buf
))) {
165 printf("Could not add Connect-Info\n");
171 if (e
->cui_flag
== 1) {
174 } else if (e
->cui_flag
== 2) {
175 os_snprintf(buf
, sizeof(buf
), "%s", e
->cui_str
);
178 if (!radius_msg_add_attr(msg
,
179 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY
,
181 printf("Could not add Chargeable-User-Identity\n");
186 if (eap
&& !radius_msg_add_eap(msg
, eap
, len
)) {
187 printf("Could not add EAP-Message\n");
191 /* State attribute must be copied if and only if this packet is
192 * Access-Request reply to the previous Access-Challenge */
193 if (e
->last_recv_radius
&& e
->last_recv_radius
->hdr
->code
==
194 RADIUS_CODE_ACCESS_CHALLENGE
) {
195 int res
= radius_msg_copy_attr(msg
, e
->last_recv_radius
,
198 printf("Could not copy State attribute from previous "
199 "Access-Challenge\n");
203 wpa_printf(MSG_DEBUG
, " Copied RADIUS State "
208 radius_client_send(e
->radius
, msg
, RADIUS_AUTH
, e
->wpa_s
->own_addr
);
212 radius_msg_free(msg
);
217 static int eapol_test_eapol_send(void *ctx
, int type
, const u8
*buf
,
220 /* struct wpa_supplicant *wpa_s = ctx; */
221 printf("WPA: eapol_test_eapol_send(type=%d len=%lu)\n",
222 type
, (unsigned long) len
);
223 if (type
== IEEE802_1X_TYPE_EAP_PACKET
) {
224 wpa_hexdump(MSG_DEBUG
, "TX EAP -> RADIUS", buf
, len
);
225 ieee802_1x_encapsulate_radius(&eapol_test
, buf
, len
);
231 static void eapol_test_set_config_blob(void *ctx
,
232 struct wpa_config_blob
*blob
)
234 struct wpa_supplicant
*wpa_s
= ctx
;
235 wpa_config_set_blob(wpa_s
->conf
, blob
);
239 static const struct wpa_config_blob
*
240 eapol_test_get_config_blob(void *ctx
, const char *name
)
242 struct wpa_supplicant
*wpa_s
= ctx
;
243 return wpa_config_get_blob(wpa_s
->conf
, name
);
247 static void eapol_test_eapol_done_cb(void *ctx
)
249 printf("WPA: EAPOL processing complete\n");
253 static void eapol_sm_reauth(void *eloop_ctx
, void *timeout_ctx
)
255 struct eapol_test_data
*e
= eloop_ctx
;
256 printf("\n\n\n\n\neapol_test: Triggering EAP reauthentication\n\n");
257 e
->radius_access_accept_received
= 0;
258 send_eap_request_identity(e
->wpa_s
, NULL
);
262 static int eapol_test_compare_pmk(struct eapol_test_data
*e
)
267 if (eapol_sm_get_key(e
->wpa_s
->eapol
, pmk
, PMK_LEN
) == 0) {
268 wpa_hexdump(MSG_DEBUG
, "PMK from EAPOL", pmk
, PMK_LEN
);
269 if (os_memcmp(pmk
, e
->authenticator_pmk
, PMK_LEN
) != 0) {
270 printf("WARNING: PMK mismatch\n");
271 wpa_hexdump(MSG_DEBUG
, "PMK from AS",
272 e
->authenticator_pmk
, PMK_LEN
);
273 } else if (e
->radius_access_accept_received
)
275 } else if (e
->authenticator_pmk_len
== 16 &&
276 eapol_sm_get_key(e
->wpa_s
->eapol
, pmk
, 16) == 0) {
277 wpa_hexdump(MSG_DEBUG
, "LEAP PMK from EAPOL", pmk
, 16);
278 if (os_memcmp(pmk
, e
->authenticator_pmk
, 16) != 0) {
279 printf("WARNING: PMK mismatch\n");
280 wpa_hexdump(MSG_DEBUG
, "PMK from AS",
281 e
->authenticator_pmk
, 16);
282 } else if (e
->radius_access_accept_received
)
284 } else if (e
->radius_access_accept_received
&& e
->no_mppe_keys
) {
285 /* No keying material expected */
289 if (ret
&& !e
->no_mppe_keys
)
290 e
->num_mppe_mismatch
++;
291 else if (!e
->no_mppe_keys
)
298 static void eapol_sm_cb(struct eapol_sm
*eapol
, int success
, void *ctx
)
300 struct eapol_test_data
*e
= ctx
;
301 printf("eapol_sm_cb: success=%d\n", success
);
302 e
->eapol_test_num_reauths
--;
303 if (e
->eapol_test_num_reauths
< 0)
306 eapol_test_compare_pmk(e
);
307 eloop_register_timeout(0, 100000, eapol_sm_reauth
, e
, NULL
);
312 static int test_eapol(struct eapol_test_data
*e
, struct wpa_supplicant
*wpa_s
,
313 struct wpa_ssid
*ssid
)
315 struct eapol_config eapol_conf
;
316 struct eapol_ctx
*ctx
;
318 ctx
= os_zalloc(sizeof(*ctx
));
320 printf("Failed to allocate EAPOL context.\n");
324 ctx
->msg_ctx
= wpa_s
;
325 ctx
->scard_ctx
= wpa_s
->scard
;
326 ctx
->cb
= eapol_sm_cb
;
328 ctx
->eapol_send_ctx
= wpa_s
;
330 ctx
->eapol_done_cb
= eapol_test_eapol_done_cb
;
331 ctx
->eapol_send
= eapol_test_eapol_send
;
332 ctx
->set_config_blob
= eapol_test_set_config_blob
;
333 ctx
->get_config_blob
= eapol_test_get_config_blob
;
334 #ifdef EAP_TLS_OPENSSL
335 ctx
->opensc_engine_path
= wpa_s
->conf
->opensc_engine_path
;
336 ctx
->pkcs11_engine_path
= wpa_s
->conf
->pkcs11_engine_path
;
337 ctx
->pkcs11_module_path
= wpa_s
->conf
->pkcs11_module_path
;
338 #endif /* EAP_TLS_OPENSSL */
340 wpa_s
->eapol
= eapol_sm_init(ctx
);
341 if (wpa_s
->eapol
== NULL
) {
343 printf("Failed to initialize EAPOL state machines.\n");
347 wpa_s
->current_ssid
= ssid
;
348 os_memset(&eapol_conf
, 0, sizeof(eapol_conf
));
349 eapol_conf
.accept_802_1x_keys
= 1;
350 eapol_conf
.required_keys
= 0;
351 eapol_conf
.fast_reauth
= wpa_s
->conf
->fast_reauth
;
352 eapol_conf
.workaround
= ssid
->eap_workaround
;
353 eapol_sm_notify_config(wpa_s
->eapol
, &ssid
->eap
, &eapol_conf
);
354 eapol_sm_register_scard_ctx(wpa_s
->eapol
, wpa_s
->scard
);
357 eapol_sm_notify_portValid(wpa_s
->eapol
, FALSE
);
358 /* 802.1X::portControl = Auto */
359 eapol_sm_notify_portEnabled(wpa_s
->eapol
, TRUE
);
365 static void test_eapol_clean(struct eapol_test_data
*e
,
366 struct wpa_supplicant
*wpa_s
)
368 radius_client_deinit(e
->radius
);
369 os_free(e
->last_eap_radius
);
370 if (e
->last_recv_radius
) {
371 radius_msg_free(e
->last_recv_radius
);
372 os_free(e
->last_recv_radius
);
374 os_free(e
->eap_identity
);
375 e
->eap_identity
= NULL
;
376 eapol_sm_deinit(wpa_s
->eapol
);
378 if (e
->radius_conf
&& e
->radius_conf
->auth_server
) {
379 os_free(e
->radius_conf
->auth_server
->shared_secret
);
380 os_free(e
->radius_conf
->auth_server
);
382 os_free(e
->radius_conf
);
383 e
->radius_conf
= NULL
;
384 scard_deinit(wpa_s
->scard
);
385 if (wpa_s
->ctrl_iface
) {
386 wpa_supplicant_ctrl_iface_deinit(wpa_s
->ctrl_iface
);
387 wpa_s
->ctrl_iface
= NULL
;
389 wpa_config_free(wpa_s
->conf
);
393 static void send_eap_request_identity(void *eloop_ctx
, void *timeout_ctx
)
395 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
397 struct ieee802_1x_hdr
*hdr
;
400 hdr
= (struct ieee802_1x_hdr
*) buf
;
401 hdr
->version
= EAPOL_VERSION
;
402 hdr
->type
= IEEE802_1X_TYPE_EAP_PACKET
;
403 hdr
->length
= htons(5);
405 eap
= (struct eap_hdr
*) (hdr
+ 1);
406 eap
->code
= EAP_CODE_REQUEST
;
408 eap
->length
= htons(5);
409 pos
= (u8
*) (eap
+ 1);
410 *pos
= EAP_TYPE_IDENTITY
;
412 printf("Sending fake EAP-Request-Identity\n");
413 eapol_sm_rx_eapol(wpa_s
->eapol
, wpa_s
->bssid
, buf
,
418 static void eapol_test_timeout(void *eloop_ctx
, void *timeout_ctx
)
420 struct eapol_test_data
*e
= eloop_ctx
;
421 printf("EAPOL test timed out\n");
422 e
->auth_timed_out
= 1;
427 static char *eap_type_text(u8 type
)
430 case EAP_TYPE_IDENTITY
: return "Identity";
431 case EAP_TYPE_NOTIFICATION
: return "Notification";
432 case EAP_TYPE_NAK
: return "Nak";
433 case EAP_TYPE_TLS
: return "TLS";
434 case EAP_TYPE_TTLS
: return "TTLS";
435 case EAP_TYPE_PEAP
: return "PEAP";
436 case EAP_TYPE_SIM
: return "SIM";
437 case EAP_TYPE_GTC
: return "GTC";
438 case EAP_TYPE_MD5
: return "MD5";
439 case EAP_TYPE_OTP
: return "OTP";
440 case EAP_TYPE_FAST
: return "FAST";
441 case EAP_TYPE_SAKE
: return "SAKE";
442 case EAP_TYPE_PSK
: return "PSK";
443 default: return "Unknown";
448 static void ieee802_1x_decapsulate_radius(struct eapol_test_data
*e
)
455 struct radius_msg
*msg
;
457 if (e
->last_recv_radius
== NULL
)
460 msg
= e
->last_recv_radius
;
462 eap
= radius_msg_get_eap(msg
, &len
);
464 /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
465 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
467 wpa_printf(MSG_DEBUG
, "could not extract "
468 "EAP-Message from RADIUS message");
469 os_free(e
->last_eap_radius
);
470 e
->last_eap_radius
= NULL
;
471 e
->last_eap_radius_len
= 0;
475 if (len
< sizeof(*hdr
)) {
476 wpa_printf(MSG_DEBUG
, "too short EAP packet "
477 "received from authentication server");
482 if (len
> sizeof(*hdr
))
483 eap_type
= eap
[sizeof(*hdr
)];
485 hdr
= (struct eap_hdr
*) eap
;
487 case EAP_CODE_REQUEST
:
488 os_snprintf(buf
, sizeof(buf
), "EAP-Request-%s (%d)",
489 eap_type
>= 0 ? eap_type_text(eap_type
) : "??",
492 case EAP_CODE_RESPONSE
:
493 os_snprintf(buf
, sizeof(buf
), "EAP Response-%s (%d)",
494 eap_type
>= 0 ? eap_type_text(eap_type
) : "??",
497 case EAP_CODE_SUCCESS
:
498 os_strlcpy(buf
, "EAP Success", sizeof(buf
));
499 /* LEAP uses EAP Success within an authentication, so must not
500 * stop here with eloop_terminate(); */
502 case EAP_CODE_FAILURE
:
503 os_strlcpy(buf
, "EAP Failure", sizeof(buf
));
507 os_strlcpy(buf
, "unknown EAP code", sizeof(buf
));
508 wpa_hexdump(MSG_DEBUG
, "Decapsulated EAP packet", eap
, len
);
511 wpa_printf(MSG_DEBUG
, "decapsulated EAP packet (code=%d "
512 "id=%d len=%d) from RADIUS server: %s",
513 hdr
->code
, hdr
->identifier
, ntohs(hdr
->length
), buf
);
515 /* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
517 os_free(e
->last_eap_radius
);
518 e
->last_eap_radius
= eap
;
519 e
->last_eap_radius_len
= len
;
522 struct ieee802_1x_hdr
*dot1x
;
523 dot1x
= os_malloc(sizeof(*dot1x
) + len
);
524 assert(dot1x
!= NULL
);
525 dot1x
->version
= EAPOL_VERSION
;
526 dot1x
->type
= IEEE802_1X_TYPE_EAP_PACKET
;
527 dot1x
->length
= htons(len
);
528 os_memcpy((u8
*) (dot1x
+ 1), eap
, len
);
529 eapol_sm_rx_eapol(e
->wpa_s
->eapol
, e
->wpa_s
->bssid
,
530 (u8
*) dot1x
, sizeof(*dot1x
) + len
);
536 static void ieee802_1x_get_keys(struct eapol_test_data
*e
,
537 struct radius_msg
*msg
, struct radius_msg
*req
,
538 u8
*shared_secret
, size_t shared_secret_len
)
540 struct radius_ms_mppe_keys
*keys
;
542 keys
= radius_msg_get_ms_keys(msg
, req
, shared_secret
,
544 if (keys
&& keys
->send
== NULL
&& keys
->recv
== NULL
) {
546 keys
= radius_msg_get_cisco_keys(msg
, req
, shared_secret
,
552 wpa_hexdump(MSG_DEBUG
, "MS-MPPE-Send-Key (sign)",
553 keys
->send
, keys
->send_len
);
556 wpa_hexdump(MSG_DEBUG
, "MS-MPPE-Recv-Key (crypt)",
557 keys
->recv
, keys
->recv_len
);
558 e
->authenticator_pmk_len
=
559 keys
->recv_len
> PMK_LEN
? PMK_LEN
:
561 os_memcpy(e
->authenticator_pmk
, keys
->recv
,
562 e
->authenticator_pmk_len
);
572 /* Process the RADIUS frames from Authentication Server */
573 static RadiusRxResult
574 ieee802_1x_receive_auth(struct radius_msg
*msg
, struct radius_msg
*req
,
575 u8
*shared_secret
, size_t shared_secret_len
,
578 struct eapol_test_data
*e
= data
;
580 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
581 * present when packet contains an EAP-Message attribute */
582 if (msg
->hdr
->code
== RADIUS_CODE_ACCESS_REJECT
&&
583 radius_msg_get_attr(msg
, RADIUS_ATTR_MESSAGE_AUTHENTICATOR
, NULL
,
585 radius_msg_get_attr(msg
, RADIUS_ATTR_EAP_MESSAGE
, NULL
, 0) < 0) {
586 wpa_printf(MSG_DEBUG
, "Allowing RADIUS "
587 "Access-Reject without Message-Authenticator "
588 "since it does not include EAP-Message\n");
589 } else if (radius_msg_verify(msg
, shared_secret
, shared_secret_len
,
591 printf("Incoming RADIUS packet did not have correct "
592 "Message-Authenticator - dropped\n");
593 return RADIUS_RX_UNKNOWN
;
596 if (msg
->hdr
->code
!= RADIUS_CODE_ACCESS_ACCEPT
&&
597 msg
->hdr
->code
!= RADIUS_CODE_ACCESS_REJECT
&&
598 msg
->hdr
->code
!= RADIUS_CODE_ACCESS_CHALLENGE
) {
599 printf("Unknown RADIUS message code\n");
600 return RADIUS_RX_UNKNOWN
;
603 e
->radius_identifier
= -1;
604 wpa_printf(MSG_DEBUG
, "RADIUS packet matching with station");
606 if (e
->last_recv_radius
) {
607 radius_msg_free(e
->last_recv_radius
);
608 os_free(e
->last_recv_radius
);
611 e
->last_recv_radius
= msg
;
613 switch (msg
->hdr
->code
) {
614 case RADIUS_CODE_ACCESS_ACCEPT
:
615 e
->radius_access_accept_received
= 1;
616 ieee802_1x_get_keys(e
, msg
, req
, shared_secret
,
619 case RADIUS_CODE_ACCESS_REJECT
:
620 e
->radius_access_reject_received
= 1;
624 ieee802_1x_decapsulate_radius(e
);
626 if ((msg
->hdr
->code
== RADIUS_CODE_ACCESS_ACCEPT
&&
627 e
->eapol_test_num_reauths
< 0) ||
628 msg
->hdr
->code
== RADIUS_CODE_ACCESS_REJECT
) {
632 return RADIUS_RX_QUEUED
;
636 static void wpa_init_conf(struct eapol_test_data
*e
,
637 struct wpa_supplicant
*wpa_s
, const char *authsrv
,
638 int port
, const char *secret
,
639 const char *cli_addr
)
641 struct hostapd_radius_server
*as
;
645 os_memcpy(wpa_s
->own_addr
, e
->own_addr
, ETH_ALEN
);
646 e
->own_ip_addr
.s_addr
= htonl((127 << 24) | 1);
647 os_strlcpy(wpa_s
->ifname
, "test", sizeof(wpa_s
->ifname
));
649 e
->radius_conf
= os_zalloc(sizeof(struct hostapd_radius_servers
));
650 assert(e
->radius_conf
!= NULL
);
651 e
->radius_conf
->num_auth_servers
= 1;
652 as
= os_zalloc(sizeof(struct hostapd_radius_server
));
654 #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
658 sscanf(authsrv
, "%d.%d.%d.%d", &a
[0], &a
[1], &a
[2], &a
[3]);
659 pos
= (u8
*) &as
->addr
.u
.v4
;
665 #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
666 inet_aton(authsrv
, &as
->addr
.u
.v4
);
667 #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
668 as
->addr
.af
= AF_INET
;
670 as
->shared_secret
= (u8
*) os_strdup(secret
);
671 as
->shared_secret_len
= os_strlen(secret
);
672 e
->radius_conf
->auth_server
= as
;
673 e
->radius_conf
->auth_servers
= as
;
674 e
->radius_conf
->msg_dumps
= 1;
676 if (hostapd_parse_ip_addr(cli_addr
,
677 &e
->radius_conf
->client_addr
) == 0)
678 e
->radius_conf
->force_client_addr
= 1;
680 wpa_printf(MSG_ERROR
, "Invalid IP address '%s'",
686 e
->radius
= radius_client_init(wpa_s
, e
->radius_conf
);
687 assert(e
->radius
!= NULL
);
689 res
= radius_client_register(e
->radius
, RADIUS_AUTH
,
690 ieee802_1x_receive_auth
, e
);
695 static int scard_test(void)
697 struct scard_data
*scard
;
700 unsigned char _rand
[16];
702 unsigned char sres
[4];
704 #endif /* PCSC_FUNCS */
705 #define num_triplets 5
706 unsigned char rand_
[num_triplets
][16];
707 unsigned char sres_
[num_triplets
][4];
708 unsigned char kc_
[num_triplets
][8];
712 #define AKA_RAND_LEN 16
713 #define AKA_AUTN_LEN 16
714 #define AKA_AUTS_LEN 14
715 #define RES_MAX_LEN 16
718 unsigned char aka_rand
[AKA_RAND_LEN
];
719 unsigned char aka_autn
[AKA_AUTN_LEN
];
720 unsigned char aka_auts
[AKA_AUTS_LEN
];
721 unsigned char aka_res
[RES_MAX_LEN
];
723 unsigned char aka_ik
[IK_LEN
];
724 unsigned char aka_ck
[CK_LEN
];
726 scard
= scard_init(SCARD_TRY_BOTH
);
729 if (scard_set_pin(scard
, "1234")) {
730 wpa_printf(MSG_WARNING
, "PIN validation failed");
736 if (scard_get_imsi(scard
, imsi
, &len
))
738 wpa_hexdump_ascii(MSG_DEBUG
, "SCARD: IMSI", (u8
*) imsi
, len
);
739 /* NOTE: Permanent Username: 1 | IMSI */
741 os_memset(_rand
, 0, sizeof(_rand
));
742 if (scard_gsm_auth(scard
, _rand
, sres
, kc
))
745 os_memset(_rand
, 0xff, sizeof(_rand
));
746 if (scard_gsm_auth(scard
, _rand
, sres
, kc
))
749 for (i
= 0; i
< num_triplets
; i
++) {
750 os_memset(rand_
[i
], i
, sizeof(rand_
[i
]));
751 if (scard_gsm_auth(scard
, rand_
[i
], sres_
[i
], kc_
[i
]))
755 for (i
= 0; i
< num_triplets
; i
++) {
757 for (j
= 0; j
< len
; j
++)
758 printf("%c", imsi
[j
]);
760 for (j
= 0; j
< 16; j
++)
761 printf("%02X", rand_
[i
][j
]);
763 for (j
= 0; j
< 4; j
++)
764 printf("%02X", sres_
[i
][j
]);
766 for (j
= 0; j
< 8; j
++)
767 printf("%02X", kc_
[i
][j
]);
771 wpa_printf(MSG_DEBUG
, "Trying to use UMTS authentication");
774 os_memset(aka_rand
, 0xaa, 16);
775 os_memcpy(aka_autn
, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
776 "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
778 res
= scard_umts_auth(scard
, aka_rand
, aka_autn
, aka_res
, &aka_res_len
,
779 aka_ik
, aka_ck
, aka_auts
);
781 wpa_printf(MSG_DEBUG
, "UMTS auth completed successfully");
782 wpa_hexdump(MSG_DEBUG
, "RES", aka_res
, aka_res_len
);
783 wpa_hexdump(MSG_DEBUG
, "IK", aka_ik
, IK_LEN
);
784 wpa_hexdump(MSG_DEBUG
, "CK", aka_ck
, CK_LEN
);
785 } else if (res
== -2) {
786 wpa_printf(MSG_DEBUG
, "UMTS auth resulted in synchronization "
788 wpa_hexdump(MSG_DEBUG
, "AUTS", aka_auts
, AKA_AUTS_LEN
);
790 wpa_printf(MSG_DEBUG
, "UMTS auth failed");
801 static int scard_get_triplets(int argc
, char *argv
[])
803 struct scard_data
*scard
;
806 unsigned char _rand
[16];
807 unsigned char sres
[4];
813 if (argc
< 2 || ((num_triplets
= atoi(argv
[1])) <= 0)) {
814 printf("invalid parameters for sim command\n");
818 if (argc
<= 2 || os_strcmp(argv
[2], "debug") != 0) {
819 /* disable debug output */
820 wpa_debug_level
= 99;
823 scard
= scard_init(SCARD_GSM_SIM_ONLY
);
825 printf("Failed to open smartcard connection\n");
828 if (scard_set_pin(scard
, argv
[0])) {
829 wpa_printf(MSG_WARNING
, "PIN validation failed");
835 if (scard_get_imsi(scard
, imsi
, &len
)) {
840 for (i
= 0; i
< num_triplets
; i
++) {
841 os_memset(_rand
, i
, sizeof(_rand
));
842 if (scard_gsm_auth(scard
, _rand
, sres
, kc
))
845 /* IMSI:Kc:SRES:RAND */
846 for (j
= 0; j
< len
; j
++)
847 printf("%c", imsi
[j
]);
849 for (j
= 0; j
< 8; j
++)
850 printf("%02X", kc
[j
]);
852 for (j
= 0; j
< 4; j
++)
853 printf("%02X", sres
[j
]);
855 for (j
= 0; j
< 16; j
++)
856 printf("%02X", _rand
[j
]);
866 static void eapol_test_terminate(int sig
, void *eloop_ctx
,
869 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
870 wpa_msg(wpa_s
, MSG_INFO
, "Signal %d received - terminating", sig
);
875 static void usage(void)
878 "eapol_test [-nWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
879 "[-s<AS secret>]\\\n"
880 " [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
881 " [-M<client MAC address>] \\\n"
882 " [-I<CUI>] [-i] [-A<client IP>]\n"
884 "eapol_test sim <PIN> <num triplets> [debug]\n"
887 " -c<conf> = configuration file\n"
888 " -a<AS IP> = IP address of the authentication server, "
889 "default 127.0.0.1\n"
890 " -p<AS port> = UDP port of the authentication server, "
892 " -s<AS secret> = shared secret with the authentication "
893 "server, default 'radius'\n"
894 " -A<client IP> = IP address of the client, default: select "
896 " -r<count> = number of re-authentications\n"
897 " -W = wait for a control interface monitor before starting\n"
898 " -S = save configuration after authentiation\n"
899 " -n = no MPPE keys expected\n"
900 " -t<timeout> = sets timeout in seconds (default: 30 s)\n"
901 " -C<Connect-Info> = RADIUS Connect-Info (default: "
902 "CONNECT 11Mbps 802.11b)\n"
903 " -M<client MAC address> = Set own MAC address "
904 "(Calling-Station-Id,\n"
905 " default: 02:00:00:00:00:01)\n"
906 " -I<CUI> = send Chargeable-User-Identity containing the "
908 " -i = send NUL value in Chargeable-User-Identity\n");
912 int main(int argc
, char *argv
[])
914 struct wpa_supplicant wpa_s
;
915 int c
, ret
= 1, wait_for_monitor
= 0, save_config
= 0;
916 char *as_addr
= "127.0.0.1";
918 char *as_secret
= "radius";
919 char *cli_addr
= NULL
;
923 if (os_program_init())
926 hostapd_logger_register_cb(hostapd_logger_cb
);
928 os_memset(&eapol_test
, 0, sizeof(eapol_test
));
929 eapol_test
.connect_info
= "CONNECT 11Mbps 802.11b";
930 os_memcpy(eapol_test
.own_addr
, "\x02\x00\x00\x00\x00\x01", ETH_ALEN
);
933 wpa_debug_show_keys
= 1;
936 c
= getopt(argc
, argv
, "a:A:c:C:iI:M:np:r:s:St:W");
950 eapol_test
.connect_info
= optarg
;
953 eapol_test
.cui_flag
= 1;
956 eapol_test
.cui_flag
= 2;
957 eapol_test
.cui_str
= optarg
;
960 if (hwaddr_aton(optarg
, eapol_test
.own_addr
)) {
966 eapol_test
.no_mppe_keys
++;
969 as_port
= atoi(optarg
);
972 eapol_test
.eapol_test_num_reauths
= atoi(optarg
);
981 timeout
= atoi(optarg
);
992 if (argc
> optind
&& os_strcmp(argv
[optind
], "scard") == 0) {
996 if (argc
> optind
&& os_strcmp(argv
[optind
], "sim") == 0) {
997 return scard_get_triplets(argc
- optind
- 1,
1003 printf("Configuration file is required.\n");
1007 if (eap_peer_register_methods()) {
1008 wpa_printf(MSG_ERROR
, "Failed to register EAP methods");
1012 if (eloop_init(&wpa_s
)) {
1013 wpa_printf(MSG_ERROR
, "Failed to initialize event loop");
1017 os_memset(&wpa_s
, 0, sizeof(wpa_s
));
1018 eapol_test
.wpa_s
= &wpa_s
;
1019 wpa_s
.conf
= wpa_config_read(conf
);
1020 if (wpa_s
.conf
== NULL
) {
1021 printf("Failed to parse configuration file '%s'.\n", conf
);
1024 if (wpa_s
.conf
->ssid
== NULL
) {
1025 printf("No networks defined.\n");
1029 wpa_init_conf(&eapol_test
, &wpa_s
, as_addr
, as_port
, as_secret
,
1031 wpa_s
.ctrl_iface
= wpa_supplicant_ctrl_iface_init(&wpa_s
);
1032 if (wpa_s
.ctrl_iface
== NULL
) {
1033 printf("Failed to initialize control interface '%s'.\n"
1034 "You may have another eapol_test process already "
1035 "running or the file was\n"
1036 "left by an unclean termination of eapol_test in "
1037 "which case you will need\n"
1038 "to manually remove this file before starting "
1039 "eapol_test again.\n",
1040 wpa_s
.conf
->ctrl_interface
);
1043 if (wpa_supplicant_scard_init(&wpa_s
, wpa_s
.conf
->ssid
))
1046 if (test_eapol(&eapol_test
, &wpa_s
, wpa_s
.conf
->ssid
))
1049 if (wait_for_monitor
)
1050 wpa_supplicant_ctrl_iface_wait(wpa_s
.ctrl_iface
);
1052 eloop_register_timeout(timeout
, 0, eapol_test_timeout
, &eapol_test
,
1054 eloop_register_timeout(0, 0, send_eap_request_identity
, &wpa_s
, NULL
);
1055 eloop_register_signal_terminate(eapol_test_terminate
, NULL
);
1056 eloop_register_signal_reconfig(eapol_test_terminate
, NULL
);
1059 eloop_cancel_timeout(eapol_test_timeout
, &eapol_test
, NULL
);
1060 eloop_cancel_timeout(eapol_sm_reauth
, &eapol_test
, NULL
);
1062 if (eapol_test_compare_pmk(&eapol_test
) == 0 ||
1063 eapol_test
.no_mppe_keys
)
1065 if (eapol_test
.auth_timed_out
)
1067 if (eapol_test
.radius_access_reject_received
)
1071 wpa_config_write(conf
, wpa_s
.conf
);
1073 test_eapol_clean(&eapol_test
, &wpa_s
);
1075 eap_peer_unregister_methods();
1079 printf("MPPE keys OK: %d mismatch: %d\n",
1080 eapol_test
.num_mppe_ok
, eapol_test
.num_mppe_mismatch
);
1081 if (eapol_test
.num_mppe_mismatch
)
1084 printf("FAILURE\n");
1086 printf("SUCCESS\n");
1088 os_program_deinit();