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"
26 #include "rsn_supp/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"
35 extern int wpa_debug_level
;
36 extern int wpa_debug_show_keys
;
38 struct wpa_driver_ops
*wpa_drivers
[] = { NULL
};
41 struct extra_radius_attr
{
45 struct extra_radius_attr
*next
;
48 struct eapol_test_data
{
49 struct wpa_supplicant
*wpa_s
;
51 int eapol_test_num_reauths
;
53 int num_mppe_ok
, num_mppe_mismatch
;
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
;
61 u8
*last_eap_radius
; /* last received EAP Response from Authentication
63 size_t last_eap_radius_len
;
65 u8 authenticator_pmk
[PMK_LEN
];
66 size_t authenticator_pmk_len
;
67 int radius_access_accept_received
;
68 int radius_access_reject_received
;
72 size_t eap_identity_len
;
75 u8 own_addr
[ETH_ALEN
];
76 struct extra_radius_attr
*extra_attrs
;
79 static struct eapol_test_data eapol_test
;
82 static void send_eap_request_identity(void *eloop_ctx
, void *timeout_ctx
);
85 static void hostapd_logger_cb(void *ctx
, const u8
*addr
, unsigned int module
,
86 int level
, const char *txt
, size_t len
)
89 wpa_printf(MSG_DEBUG
, "STA " MACSTR
": %s\n",
92 wpa_printf(MSG_DEBUG
, "%s", txt
);
96 static int add_extra_attr(struct radius_msg
*msg
,
97 struct extra_radius_attr
*attr
)
104 switch (attr
->syntax
) {
106 os_snprintf(buf
, sizeof(buf
), "%s", attr
->data
);
107 len
= os_strlen(buf
);
115 if (pos
[0] == '0' && pos
[1] == 'x')
117 len
= os_strlen(pos
);
118 if ((len
& 1) || (len
/ 2) > sizeof(buf
)) {
119 printf("Invalid extra attribute hexstring\n");
123 if (hexstr2bin(pos
, (u8
*) buf
, len
) < 0) {
124 printf("Invalid extra attribute hexstring\n");
129 val
= htonl(atoi(attr
->data
));
130 os_memcpy(buf
, &val
, 4);
134 printf("Incorrect extra attribute syntax specification\n");
138 if (!radius_msg_add_attr(msg
, attr
->type
, (u8
*) buf
, len
)) {
139 printf("Could not add attribute %d\n", attr
->type
);
147 static int add_extra_attrs(struct radius_msg
*msg
,
148 struct extra_radius_attr
*attrs
)
150 struct extra_radius_attr
*p
;
151 for (p
= attrs
; p
; p
= p
->next
) {
152 if (add_extra_attr(msg
, p
) < 0)
159 static struct extra_radius_attr
*
160 find_extra_attr(struct extra_radius_attr
*attrs
, u8 type
)
162 struct extra_radius_attr
*p
;
163 for (p
= attrs
; p
; p
= p
->next
) {
171 static void ieee802_1x_encapsulate_radius(struct eapol_test_data
*e
,
172 const u8
*eap
, size_t len
)
174 struct radius_msg
*msg
;
176 const struct eap_hdr
*hdr
;
179 wpa_printf(MSG_DEBUG
, "Encapsulating EAP message into a RADIUS "
182 e
->radius_identifier
= radius_client_get_id(e
->radius
);
183 msg
= radius_msg_new(RADIUS_CODE_ACCESS_REQUEST
,
184 e
->radius_identifier
);
186 printf("Could not create net RADIUS packet\n");
190 radius_msg_make_authenticator(msg
, (u8
*) e
, sizeof(*e
));
192 hdr
= (const struct eap_hdr
*) eap
;
193 pos
= (const u8
*) (hdr
+ 1);
194 if (len
> sizeof(*hdr
) && hdr
->code
== EAP_CODE_RESPONSE
&&
195 pos
[0] == EAP_TYPE_IDENTITY
) {
197 os_free(e
->eap_identity
);
198 e
->eap_identity_len
= len
- sizeof(*hdr
) - 1;
199 e
->eap_identity
= os_malloc(e
->eap_identity_len
);
200 if (e
->eap_identity
) {
201 os_memcpy(e
->eap_identity
, pos
, e
->eap_identity_len
);
202 wpa_hexdump(MSG_DEBUG
, "Learned identity from "
203 "EAP-Response-Identity",
204 e
->eap_identity
, e
->eap_identity_len
);
208 if (e
->eap_identity
&&
209 !radius_msg_add_attr(msg
, RADIUS_ATTR_USER_NAME
,
210 e
->eap_identity
, e
->eap_identity_len
)) {
211 printf("Could not add User-Name\n");
215 if (!find_extra_attr(e
->extra_attrs
, RADIUS_ATTR_NAS_IP_ADDRESS
) &&
216 !radius_msg_add_attr(msg
, RADIUS_ATTR_NAS_IP_ADDRESS
,
217 (u8
*) &e
->own_ip_addr
, 4)) {
218 printf("Could not add NAS-IP-Address\n");
222 os_snprintf(buf
, sizeof(buf
), RADIUS_802_1X_ADDR_FORMAT
,
223 MAC2STR(e
->wpa_s
->own_addr
));
224 if (!find_extra_attr(e
->extra_attrs
, RADIUS_ATTR_CALLING_STATION_ID
)
226 !radius_msg_add_attr(msg
, RADIUS_ATTR_CALLING_STATION_ID
,
227 (u8
*) buf
, os_strlen(buf
))) {
228 printf("Could not add Calling-Station-Id\n");
232 /* TODO: should probably check MTU from driver config; 2304 is max for
233 * IEEE 802.11, but use 1400 to avoid problems with too large packets
235 if (!find_extra_attr(e
->extra_attrs
, RADIUS_ATTR_FRAMED_MTU
) &&
236 !radius_msg_add_attr_int32(msg
, RADIUS_ATTR_FRAMED_MTU
, 1400)) {
237 printf("Could not add Framed-MTU\n");
241 if (!find_extra_attr(e
->extra_attrs
, RADIUS_ATTR_NAS_PORT_TYPE
) &&
242 !radius_msg_add_attr_int32(msg
, RADIUS_ATTR_NAS_PORT_TYPE
,
243 RADIUS_NAS_PORT_TYPE_IEEE_802_11
)) {
244 printf("Could not add NAS-Port-Type\n");
248 os_snprintf(buf
, sizeof(buf
), "%s", e
->connect_info
);
249 if (!find_extra_attr(e
->extra_attrs
, RADIUS_ATTR_CONNECT_INFO
) &&
250 !radius_msg_add_attr(msg
, RADIUS_ATTR_CONNECT_INFO
,
251 (u8
*) buf
, os_strlen(buf
))) {
252 printf("Could not add Connect-Info\n");
256 if (add_extra_attrs(msg
, e
->extra_attrs
) < 0)
259 if (eap
&& !radius_msg_add_eap(msg
, eap
, len
)) {
260 printf("Could not add EAP-Message\n");
264 /* State attribute must be copied if and only if this packet is
265 * Access-Request reply to the previous Access-Challenge */
266 if (e
->last_recv_radius
&&
267 radius_msg_get_hdr(e
->last_recv_radius
)->code
==
268 RADIUS_CODE_ACCESS_CHALLENGE
) {
269 int res
= radius_msg_copy_attr(msg
, e
->last_recv_radius
,
272 printf("Could not copy State attribute from previous "
273 "Access-Challenge\n");
277 wpa_printf(MSG_DEBUG
, " Copied RADIUS State "
282 radius_client_send(e
->radius
, msg
, RADIUS_AUTH
, e
->wpa_s
->own_addr
);
286 radius_msg_free(msg
);
290 static int eapol_test_eapol_send(void *ctx
, int type
, const u8
*buf
,
293 /* struct wpa_supplicant *wpa_s = ctx; */
294 printf("WPA: eapol_test_eapol_send(type=%d len=%lu)\n",
295 type
, (unsigned long) len
);
296 if (type
== IEEE802_1X_TYPE_EAP_PACKET
) {
297 wpa_hexdump(MSG_DEBUG
, "TX EAP -> RADIUS", buf
, len
);
298 ieee802_1x_encapsulate_radius(&eapol_test
, buf
, len
);
304 static void eapol_test_set_config_blob(void *ctx
,
305 struct wpa_config_blob
*blob
)
307 struct wpa_supplicant
*wpa_s
= ctx
;
308 wpa_config_set_blob(wpa_s
->conf
, blob
);
312 static const struct wpa_config_blob
*
313 eapol_test_get_config_blob(void *ctx
, const char *name
)
315 struct wpa_supplicant
*wpa_s
= ctx
;
316 return wpa_config_get_blob(wpa_s
->conf
, name
);
320 static void eapol_test_eapol_done_cb(void *ctx
)
322 printf("WPA: EAPOL processing complete\n");
326 static void eapol_sm_reauth(void *eloop_ctx
, void *timeout_ctx
)
328 struct eapol_test_data
*e
= eloop_ctx
;
329 printf("\n\n\n\n\neapol_test: Triggering EAP reauthentication\n\n");
330 e
->radius_access_accept_received
= 0;
331 send_eap_request_identity(e
->wpa_s
, NULL
);
335 static int eapol_test_compare_pmk(struct eapol_test_data
*e
)
340 if (eapol_sm_get_key(e
->wpa_s
->eapol
, pmk
, PMK_LEN
) == 0) {
341 wpa_hexdump(MSG_DEBUG
, "PMK from EAPOL", pmk
, PMK_LEN
);
342 if (os_memcmp(pmk
, e
->authenticator_pmk
, PMK_LEN
) != 0) {
343 printf("WARNING: PMK mismatch\n");
344 wpa_hexdump(MSG_DEBUG
, "PMK from AS",
345 e
->authenticator_pmk
, PMK_LEN
);
346 } else if (e
->radius_access_accept_received
)
348 } else if (e
->authenticator_pmk_len
== 16 &&
349 eapol_sm_get_key(e
->wpa_s
->eapol
, pmk
, 16) == 0) {
350 wpa_hexdump(MSG_DEBUG
, "LEAP PMK from EAPOL", pmk
, 16);
351 if (os_memcmp(pmk
, e
->authenticator_pmk
, 16) != 0) {
352 printf("WARNING: PMK mismatch\n");
353 wpa_hexdump(MSG_DEBUG
, "PMK from AS",
354 e
->authenticator_pmk
, 16);
355 } else if (e
->radius_access_accept_received
)
357 } else if (e
->radius_access_accept_received
&& e
->no_mppe_keys
) {
358 /* No keying material expected */
362 if (ret
&& !e
->no_mppe_keys
)
363 e
->num_mppe_mismatch
++;
364 else if (!e
->no_mppe_keys
)
371 static void eapol_sm_cb(struct eapol_sm
*eapol
, int success
, void *ctx
)
373 struct eapol_test_data
*e
= ctx
;
374 printf("eapol_sm_cb: success=%d\n", success
);
375 e
->eapol_test_num_reauths
--;
376 if (e
->eapol_test_num_reauths
< 0)
379 eapol_test_compare_pmk(e
);
380 eloop_register_timeout(0, 100000, eapol_sm_reauth
, e
, NULL
);
385 static int test_eapol(struct eapol_test_data
*e
, struct wpa_supplicant
*wpa_s
,
386 struct wpa_ssid
*ssid
)
388 struct eapol_config eapol_conf
;
389 struct eapol_ctx
*ctx
;
391 ctx
= os_zalloc(sizeof(*ctx
));
393 printf("Failed to allocate EAPOL context.\n");
397 ctx
->msg_ctx
= wpa_s
;
398 ctx
->scard_ctx
= wpa_s
->scard
;
399 ctx
->cb
= eapol_sm_cb
;
401 ctx
->eapol_send_ctx
= wpa_s
;
403 ctx
->eapol_done_cb
= eapol_test_eapol_done_cb
;
404 ctx
->eapol_send
= eapol_test_eapol_send
;
405 ctx
->set_config_blob
= eapol_test_set_config_blob
;
406 ctx
->get_config_blob
= eapol_test_get_config_blob
;
407 ctx
->opensc_engine_path
= wpa_s
->conf
->opensc_engine_path
;
408 ctx
->pkcs11_engine_path
= wpa_s
->conf
->pkcs11_engine_path
;
409 ctx
->pkcs11_module_path
= wpa_s
->conf
->pkcs11_module_path
;
411 wpa_s
->eapol
= eapol_sm_init(ctx
);
412 if (wpa_s
->eapol
== NULL
) {
414 printf("Failed to initialize EAPOL state machines.\n");
418 wpa_s
->current_ssid
= ssid
;
419 os_memset(&eapol_conf
, 0, sizeof(eapol_conf
));
420 eapol_conf
.accept_802_1x_keys
= 1;
421 eapol_conf
.required_keys
= 0;
422 eapol_conf
.fast_reauth
= wpa_s
->conf
->fast_reauth
;
423 eapol_conf
.workaround
= ssid
->eap_workaround
;
424 eapol_sm_notify_config(wpa_s
->eapol
, &ssid
->eap
, &eapol_conf
);
425 eapol_sm_register_scard_ctx(wpa_s
->eapol
, wpa_s
->scard
);
428 eapol_sm_notify_portValid(wpa_s
->eapol
, FALSE
);
429 /* 802.1X::portControl = Auto */
430 eapol_sm_notify_portEnabled(wpa_s
->eapol
, TRUE
);
436 static void test_eapol_clean(struct eapol_test_data
*e
,
437 struct wpa_supplicant
*wpa_s
)
439 struct extra_radius_attr
*p
, *prev
;
441 radius_client_deinit(e
->radius
);
442 os_free(e
->last_eap_radius
);
443 radius_msg_free(e
->last_recv_radius
);
444 e
->last_recv_radius
= NULL
;
445 os_free(e
->eap_identity
);
446 e
->eap_identity
= NULL
;
447 eapol_sm_deinit(wpa_s
->eapol
);
449 if (e
->radius_conf
&& e
->radius_conf
->auth_server
) {
450 os_free(e
->radius_conf
->auth_server
->shared_secret
);
451 os_free(e
->radius_conf
->auth_server
);
453 os_free(e
->radius_conf
);
454 e
->radius_conf
= NULL
;
455 scard_deinit(wpa_s
->scard
);
456 if (wpa_s
->ctrl_iface
) {
457 wpa_supplicant_ctrl_iface_deinit(wpa_s
->ctrl_iface
);
458 wpa_s
->ctrl_iface
= NULL
;
460 wpa_config_free(wpa_s
->conf
);
471 static void send_eap_request_identity(void *eloop_ctx
, void *timeout_ctx
)
473 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
475 struct ieee802_1x_hdr
*hdr
;
478 hdr
= (struct ieee802_1x_hdr
*) buf
;
479 hdr
->version
= EAPOL_VERSION
;
480 hdr
->type
= IEEE802_1X_TYPE_EAP_PACKET
;
481 hdr
->length
= htons(5);
483 eap
= (struct eap_hdr
*) (hdr
+ 1);
484 eap
->code
= EAP_CODE_REQUEST
;
486 eap
->length
= htons(5);
487 pos
= (u8
*) (eap
+ 1);
488 *pos
= EAP_TYPE_IDENTITY
;
490 printf("Sending fake EAP-Request-Identity\n");
491 eapol_sm_rx_eapol(wpa_s
->eapol
, wpa_s
->bssid
, buf
,
496 static void eapol_test_timeout(void *eloop_ctx
, void *timeout_ctx
)
498 struct eapol_test_data
*e
= eloop_ctx
;
499 printf("EAPOL test timed out\n");
500 e
->auth_timed_out
= 1;
505 static char *eap_type_text(u8 type
)
508 case EAP_TYPE_IDENTITY
: return "Identity";
509 case EAP_TYPE_NOTIFICATION
: return "Notification";
510 case EAP_TYPE_NAK
: return "Nak";
511 case EAP_TYPE_TLS
: return "TLS";
512 case EAP_TYPE_TTLS
: return "TTLS";
513 case EAP_TYPE_PEAP
: return "PEAP";
514 case EAP_TYPE_SIM
: return "SIM";
515 case EAP_TYPE_GTC
: return "GTC";
516 case EAP_TYPE_MD5
: return "MD5";
517 case EAP_TYPE_OTP
: return "OTP";
518 case EAP_TYPE_FAST
: return "FAST";
519 case EAP_TYPE_SAKE
: return "SAKE";
520 case EAP_TYPE_PSK
: return "PSK";
521 default: return "Unknown";
526 static void ieee802_1x_decapsulate_radius(struct eapol_test_data
*e
)
533 struct radius_msg
*msg
;
535 if (e
->last_recv_radius
== NULL
)
538 msg
= e
->last_recv_radius
;
540 eap
= radius_msg_get_eap(msg
, &len
);
542 /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
543 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
545 wpa_printf(MSG_DEBUG
, "could not extract "
546 "EAP-Message from RADIUS message");
547 os_free(e
->last_eap_radius
);
548 e
->last_eap_radius
= NULL
;
549 e
->last_eap_radius_len
= 0;
553 if (len
< sizeof(*hdr
)) {
554 wpa_printf(MSG_DEBUG
, "too short EAP packet "
555 "received from authentication server");
560 if (len
> sizeof(*hdr
))
561 eap_type
= eap
[sizeof(*hdr
)];
563 hdr
= (struct eap_hdr
*) eap
;
565 case EAP_CODE_REQUEST
:
566 os_snprintf(buf
, sizeof(buf
), "EAP-Request-%s (%d)",
567 eap_type
>= 0 ? eap_type_text(eap_type
) : "??",
570 case EAP_CODE_RESPONSE
:
571 os_snprintf(buf
, sizeof(buf
), "EAP Response-%s (%d)",
572 eap_type
>= 0 ? eap_type_text(eap_type
) : "??",
575 case EAP_CODE_SUCCESS
:
576 os_strlcpy(buf
, "EAP Success", sizeof(buf
));
577 /* LEAP uses EAP Success within an authentication, so must not
578 * stop here with eloop_terminate(); */
580 case EAP_CODE_FAILURE
:
581 os_strlcpy(buf
, "EAP Failure", sizeof(buf
));
585 os_strlcpy(buf
, "unknown EAP code", sizeof(buf
));
586 wpa_hexdump(MSG_DEBUG
, "Decapsulated EAP packet", eap
, len
);
589 wpa_printf(MSG_DEBUG
, "decapsulated EAP packet (code=%d "
590 "id=%d len=%d) from RADIUS server: %s",
591 hdr
->code
, hdr
->identifier
, ntohs(hdr
->length
), buf
);
593 /* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
595 os_free(e
->last_eap_radius
);
596 e
->last_eap_radius
= eap
;
597 e
->last_eap_radius_len
= len
;
600 struct ieee802_1x_hdr
*dot1x
;
601 dot1x
= os_malloc(sizeof(*dot1x
) + len
);
602 assert(dot1x
!= NULL
);
603 dot1x
->version
= EAPOL_VERSION
;
604 dot1x
->type
= IEEE802_1X_TYPE_EAP_PACKET
;
605 dot1x
->length
= htons(len
);
606 os_memcpy((u8
*) (dot1x
+ 1), eap
, len
);
607 eapol_sm_rx_eapol(e
->wpa_s
->eapol
, e
->wpa_s
->bssid
,
608 (u8
*) dot1x
, sizeof(*dot1x
) + len
);
614 static void ieee802_1x_get_keys(struct eapol_test_data
*e
,
615 struct radius_msg
*msg
, struct radius_msg
*req
,
616 const u8
*shared_secret
,
617 size_t shared_secret_len
)
619 struct radius_ms_mppe_keys
*keys
;
621 keys
= radius_msg_get_ms_keys(msg
, req
, shared_secret
,
623 if (keys
&& keys
->send
== NULL
&& keys
->recv
== NULL
) {
625 keys
= radius_msg_get_cisco_keys(msg
, req
, shared_secret
,
631 wpa_hexdump(MSG_DEBUG
, "MS-MPPE-Send-Key (sign)",
632 keys
->send
, keys
->send_len
);
635 wpa_hexdump(MSG_DEBUG
, "MS-MPPE-Recv-Key (crypt)",
636 keys
->recv
, keys
->recv_len
);
637 e
->authenticator_pmk_len
=
638 keys
->recv_len
> PMK_LEN
? PMK_LEN
:
640 os_memcpy(e
->authenticator_pmk
, keys
->recv
,
641 e
->authenticator_pmk_len
);
642 if (e
->authenticator_pmk_len
== 16 && keys
->send
&&
643 keys
->send_len
== 16) {
644 /* MS-CHAP-v2 derives 16 octet keys */
645 wpa_printf(MSG_DEBUG
, "Use MS-MPPE-Send-Key "
646 "to extend PMK to 32 octets");
647 os_memcpy(e
->authenticator_pmk
+
648 e
->authenticator_pmk_len
,
649 keys
->send
, keys
->send_len
);
650 e
->authenticator_pmk_len
+= keys
->send_len
;
661 /* Process the RADIUS frames from Authentication Server */
662 static RadiusRxResult
663 ieee802_1x_receive_auth(struct radius_msg
*msg
, struct radius_msg
*req
,
664 const u8
*shared_secret
, size_t shared_secret_len
,
667 struct eapol_test_data
*e
= data
;
668 struct radius_hdr
*hdr
= radius_msg_get_hdr(msg
);
670 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
671 * present when packet contains an EAP-Message attribute */
672 if (hdr
->code
== RADIUS_CODE_ACCESS_REJECT
&&
673 radius_msg_get_attr(msg
, RADIUS_ATTR_MESSAGE_AUTHENTICATOR
, NULL
,
675 radius_msg_get_attr(msg
, RADIUS_ATTR_EAP_MESSAGE
, NULL
, 0) < 0) {
676 wpa_printf(MSG_DEBUG
, "Allowing RADIUS "
677 "Access-Reject without Message-Authenticator "
678 "since it does not include EAP-Message\n");
679 } else if (radius_msg_verify(msg
, shared_secret
, shared_secret_len
,
681 printf("Incoming RADIUS packet did not have correct "
682 "Message-Authenticator - dropped\n");
683 return RADIUS_RX_UNKNOWN
;
686 if (hdr
->code
!= RADIUS_CODE_ACCESS_ACCEPT
&&
687 hdr
->code
!= RADIUS_CODE_ACCESS_REJECT
&&
688 hdr
->code
!= RADIUS_CODE_ACCESS_CHALLENGE
) {
689 printf("Unknown RADIUS message code\n");
690 return RADIUS_RX_UNKNOWN
;
693 e
->radius_identifier
= -1;
694 wpa_printf(MSG_DEBUG
, "RADIUS packet matching with station");
696 radius_msg_free(e
->last_recv_radius
);
697 e
->last_recv_radius
= msg
;
700 case RADIUS_CODE_ACCESS_ACCEPT
:
701 e
->radius_access_accept_received
= 1;
702 ieee802_1x_get_keys(e
, msg
, req
, shared_secret
,
705 case RADIUS_CODE_ACCESS_REJECT
:
706 e
->radius_access_reject_received
= 1;
710 ieee802_1x_decapsulate_radius(e
);
712 if ((hdr
->code
== RADIUS_CODE_ACCESS_ACCEPT
&&
713 e
->eapol_test_num_reauths
< 0) ||
714 hdr
->code
== RADIUS_CODE_ACCESS_REJECT
) {
718 return RADIUS_RX_QUEUED
;
722 static void wpa_init_conf(struct eapol_test_data
*e
,
723 struct wpa_supplicant
*wpa_s
, const char *authsrv
,
724 int port
, const char *secret
,
725 const char *cli_addr
)
727 struct hostapd_radius_server
*as
;
731 os_memcpy(wpa_s
->own_addr
, e
->own_addr
, ETH_ALEN
);
732 e
->own_ip_addr
.s_addr
= htonl((127 << 24) | 1);
733 os_strlcpy(wpa_s
->ifname
, "test", sizeof(wpa_s
->ifname
));
735 e
->radius_conf
= os_zalloc(sizeof(struct hostapd_radius_servers
));
736 assert(e
->radius_conf
!= NULL
);
737 e
->radius_conf
->num_auth_servers
= 1;
738 as
= os_zalloc(sizeof(struct hostapd_radius_server
));
740 #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
744 sscanf(authsrv
, "%d.%d.%d.%d", &a
[0], &a
[1], &a
[2], &a
[3]);
745 pos
= (u8
*) &as
->addr
.u
.v4
;
751 #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
752 inet_aton(authsrv
, &as
->addr
.u
.v4
);
753 #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
754 as
->addr
.af
= AF_INET
;
756 as
->shared_secret
= (u8
*) os_strdup(secret
);
757 as
->shared_secret_len
= os_strlen(secret
);
758 e
->radius_conf
->auth_server
= as
;
759 e
->radius_conf
->auth_servers
= as
;
760 e
->radius_conf
->msg_dumps
= 1;
762 if (hostapd_parse_ip_addr(cli_addr
,
763 &e
->radius_conf
->client_addr
) == 0)
764 e
->radius_conf
->force_client_addr
= 1;
766 wpa_printf(MSG_ERROR
, "Invalid IP address '%s'",
772 e
->radius
= radius_client_init(wpa_s
, e
->radius_conf
);
773 assert(e
->radius
!= NULL
);
775 res
= radius_client_register(e
->radius
, RADIUS_AUTH
,
776 ieee802_1x_receive_auth
, e
);
781 static int scard_test(void)
783 struct scard_data
*scard
;
786 unsigned char _rand
[16];
788 unsigned char sres
[4];
790 #endif /* PCSC_FUNCS */
791 #define num_triplets 5
792 unsigned char rand_
[num_triplets
][16];
793 unsigned char sres_
[num_triplets
][4];
794 unsigned char kc_
[num_triplets
][8];
798 #define AKA_RAND_LEN 16
799 #define AKA_AUTN_LEN 16
800 #define AKA_AUTS_LEN 14
801 #define RES_MAX_LEN 16
804 unsigned char aka_rand
[AKA_RAND_LEN
];
805 unsigned char aka_autn
[AKA_AUTN_LEN
];
806 unsigned char aka_auts
[AKA_AUTS_LEN
];
807 unsigned char aka_res
[RES_MAX_LEN
];
809 unsigned char aka_ik
[IK_LEN
];
810 unsigned char aka_ck
[CK_LEN
];
812 scard
= scard_init(SCARD_TRY_BOTH
);
815 if (scard_set_pin(scard
, "1234")) {
816 wpa_printf(MSG_WARNING
, "PIN validation failed");
822 if (scard_get_imsi(scard
, imsi
, &len
))
824 wpa_hexdump_ascii(MSG_DEBUG
, "SCARD: IMSI", (u8
*) imsi
, len
);
825 /* NOTE: Permanent Username: 1 | IMSI */
827 os_memset(_rand
, 0, sizeof(_rand
));
828 if (scard_gsm_auth(scard
, _rand
, sres
, kc
))
831 os_memset(_rand
, 0xff, sizeof(_rand
));
832 if (scard_gsm_auth(scard
, _rand
, sres
, kc
))
835 for (i
= 0; i
< num_triplets
; i
++) {
836 os_memset(rand_
[i
], i
, sizeof(rand_
[i
]));
837 if (scard_gsm_auth(scard
, rand_
[i
], sres_
[i
], kc_
[i
]))
841 for (i
= 0; i
< num_triplets
; i
++) {
843 for (j
= 0; j
< len
; j
++)
844 printf("%c", imsi
[j
]);
846 for (j
= 0; j
< 16; j
++)
847 printf("%02X", rand_
[i
][j
]);
849 for (j
= 0; j
< 4; j
++)
850 printf("%02X", sres_
[i
][j
]);
852 for (j
= 0; j
< 8; j
++)
853 printf("%02X", kc_
[i
][j
]);
857 wpa_printf(MSG_DEBUG
, "Trying to use UMTS authentication");
860 os_memset(aka_rand
, 0xaa, 16);
861 os_memcpy(aka_autn
, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
862 "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
864 res
= scard_umts_auth(scard
, aka_rand
, aka_autn
, aka_res
, &aka_res_len
,
865 aka_ik
, aka_ck
, aka_auts
);
867 wpa_printf(MSG_DEBUG
, "UMTS auth completed successfully");
868 wpa_hexdump(MSG_DEBUG
, "RES", aka_res
, aka_res_len
);
869 wpa_hexdump(MSG_DEBUG
, "IK", aka_ik
, IK_LEN
);
870 wpa_hexdump(MSG_DEBUG
, "CK", aka_ck
, CK_LEN
);
871 } else if (res
== -2) {
872 wpa_printf(MSG_DEBUG
, "UMTS auth resulted in synchronization "
874 wpa_hexdump(MSG_DEBUG
, "AUTS", aka_auts
, AKA_AUTS_LEN
);
876 wpa_printf(MSG_DEBUG
, "UMTS auth failed");
887 static int scard_get_triplets(int argc
, char *argv
[])
889 struct scard_data
*scard
;
892 unsigned char _rand
[16];
893 unsigned char sres
[4];
899 if (argc
< 2 || ((num_triplets
= atoi(argv
[1])) <= 0)) {
900 printf("invalid parameters for sim command\n");
904 if (argc
<= 2 || os_strcmp(argv
[2], "debug") != 0) {
905 /* disable debug output */
906 wpa_debug_level
= 99;
909 scard
= scard_init(SCARD_GSM_SIM_ONLY
);
911 printf("Failed to open smartcard connection\n");
914 if (scard_set_pin(scard
, argv
[0])) {
915 wpa_printf(MSG_WARNING
, "PIN validation failed");
921 if (scard_get_imsi(scard
, imsi
, &len
)) {
926 for (i
= 0; i
< num_triplets
; i
++) {
927 os_memset(_rand
, i
, sizeof(_rand
));
928 if (scard_gsm_auth(scard
, _rand
, sres
, kc
))
931 /* IMSI:Kc:SRES:RAND */
932 for (j
= 0; j
< len
; j
++)
933 printf("%c", imsi
[j
]);
935 for (j
= 0; j
< 8; j
++)
936 printf("%02X", kc
[j
]);
938 for (j
= 0; j
< 4; j
++)
939 printf("%02X", sres
[j
]);
941 for (j
= 0; j
< 16; j
++)
942 printf("%02X", _rand
[j
]);
952 static void eapol_test_terminate(int sig
, void *signal_ctx
)
954 struct wpa_supplicant
*wpa_s
= signal_ctx
;
955 wpa_msg(wpa_s
, MSG_INFO
, "Signal %d received - terminating", sig
);
960 static void usage(void)
963 "eapol_test [-nWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
964 "[-s<AS secret>]\\\n"
965 " [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
966 " [-M<client MAC address>] \\\n"
967 " [-N<attr spec>] \\\n"
970 "eapol_test sim <PIN> <num triplets> [debug]\n"
973 " -c<conf> = configuration file\n"
974 " -a<AS IP> = IP address of the authentication server, "
975 "default 127.0.0.1\n"
976 " -p<AS port> = UDP port of the authentication server, "
978 " -s<AS secret> = shared secret with the authentication "
979 "server, default 'radius'\n"
980 " -A<client IP> = IP address of the client, default: select "
982 " -r<count> = number of re-authentications\n"
983 " -W = wait for a control interface monitor before starting\n"
984 " -S = save configuration after authentication\n"
985 " -n = no MPPE keys expected\n"
986 " -t<timeout> = sets timeout in seconds (default: 30 s)\n"
987 " -C<Connect-Info> = RADIUS Connect-Info (default: "
988 "CONNECT 11Mbps 802.11b)\n"
989 " -M<client MAC address> = Set own MAC address "
990 "(Calling-Station-Id,\n"
991 " default: 02:00:00:00:00:01)\n"
992 " -N<attr spec> = send arbitrary attribute specified by:\n"
993 " attr_id:syntax:value or attr_id\n"
994 " attr_id - number id of the attribute\n"
995 " syntax - one of: s, d, x\n"
998 " x = octet string\n"
999 " value - attribute value.\n"
1000 " When only attr_id is specified, NULL will be used as "
1002 " Multiple attributes can be specified by using the "
1003 "option several times.\n");
1007 int main(int argc
, char *argv
[])
1009 struct wpa_supplicant wpa_s
;
1010 int c
, ret
= 1, wait_for_monitor
= 0, save_config
= 0;
1011 char *as_addr
= "127.0.0.1";
1013 char *as_secret
= "radius";
1014 char *cli_addr
= NULL
;
1018 struct extra_radius_attr
*p
= NULL
, *p1
;
1020 if (os_program_init())
1023 hostapd_logger_register_cb(hostapd_logger_cb
);
1025 os_memset(&eapol_test
, 0, sizeof(eapol_test
));
1026 eapol_test
.connect_info
= "CONNECT 11Mbps 802.11b";
1027 os_memcpy(eapol_test
.own_addr
, "\x02\x00\x00\x00\x00\x01", ETH_ALEN
);
1029 wpa_debug_level
= 0;
1030 wpa_debug_show_keys
= 1;
1033 c
= getopt(argc
, argv
, "a:A:c:C:M:nN:p:r:s:St:W");
1047 eapol_test
.connect_info
= optarg
;
1050 if (hwaddr_aton(optarg
, eapol_test
.own_addr
)) {
1056 eapol_test
.no_mppe_keys
++;
1059 as_port
= atoi(optarg
);
1062 eapol_test
.eapol_test_num_reauths
= atoi(optarg
);
1071 timeout
= atoi(optarg
);
1077 p1
= os_zalloc(sizeof(p1
));
1081 eapol_test
.extra_attrs
= p1
;
1086 p
->type
= atoi(optarg
);
1087 pos
= os_strchr(optarg
, ':');
1095 if (pos
[0] == '\0' || pos
[1] != ':') {
1096 printf("Incorrect format of attribute "
1110 if (argc
> optind
&& os_strcmp(argv
[optind
], "scard") == 0) {
1111 return scard_test();
1114 if (argc
> optind
&& os_strcmp(argv
[optind
], "sim") == 0) {
1115 return scard_get_triplets(argc
- optind
- 1,
1121 printf("Configuration file is required.\n");
1125 if (eap_register_methods()) {
1126 wpa_printf(MSG_ERROR
, "Failed to register EAP methods");
1131 wpa_printf(MSG_ERROR
, "Failed to initialize event loop");
1135 os_memset(&wpa_s
, 0, sizeof(wpa_s
));
1136 eapol_test
.wpa_s
= &wpa_s
;
1137 wpa_s
.conf
= wpa_config_read(conf
);
1138 if (wpa_s
.conf
== NULL
) {
1139 printf("Failed to parse configuration file '%s'.\n", conf
);
1142 if (wpa_s
.conf
->ssid
== NULL
) {
1143 printf("No networks defined.\n");
1147 wpa_init_conf(&eapol_test
, &wpa_s
, as_addr
, as_port
, as_secret
,
1149 wpa_s
.ctrl_iface
= wpa_supplicant_ctrl_iface_init(&wpa_s
);
1150 if (wpa_s
.ctrl_iface
== NULL
) {
1151 printf("Failed to initialize control interface '%s'.\n"
1152 "You may have another eapol_test process already "
1153 "running or the file was\n"
1154 "left by an unclean termination of eapol_test in "
1155 "which case you will need\n"
1156 "to manually remove this file before starting "
1157 "eapol_test again.\n",
1158 wpa_s
.conf
->ctrl_interface
);
1161 if (wpa_supplicant_scard_init(&wpa_s
, wpa_s
.conf
->ssid
))
1164 if (test_eapol(&eapol_test
, &wpa_s
, wpa_s
.conf
->ssid
))
1167 if (wait_for_monitor
)
1168 wpa_supplicant_ctrl_iface_wait(wpa_s
.ctrl_iface
);
1170 eloop_register_timeout(timeout
, 0, eapol_test_timeout
, &eapol_test
,
1172 eloop_register_timeout(0, 0, send_eap_request_identity
, &wpa_s
, NULL
);
1173 eloop_register_signal_terminate(eapol_test_terminate
, &wpa_s
);
1174 eloop_register_signal_reconfig(eapol_test_terminate
, &wpa_s
);
1177 eloop_cancel_timeout(eapol_test_timeout
, &eapol_test
, NULL
);
1178 eloop_cancel_timeout(eapol_sm_reauth
, &eapol_test
, NULL
);
1180 if (eapol_test_compare_pmk(&eapol_test
) == 0 ||
1181 eapol_test
.no_mppe_keys
)
1183 if (eapol_test
.auth_timed_out
)
1185 if (eapol_test
.radius_access_reject_received
)
1189 wpa_config_write(conf
, wpa_s
.conf
);
1191 test_eapol_clean(&eapol_test
, &wpa_s
);
1193 eap_peer_unregister_methods();
1197 printf("MPPE keys OK: %d mismatch: %d\n",
1198 eapol_test
.num_mppe_ok
, eapol_test
.num_mppe_mismatch
);
1199 if (eapol_test
.num_mppe_mismatch
)
1202 printf("FAILURE\n");
1204 printf("SUCCESS\n");
1206 os_program_deinit();