2 * WPA Supplicant - test code
3 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
8 * IEEE 802.1X Supplicant test code (to be used in place of wpa_supplicant.c.
9 * Not used in production version.
16 #include "utils/ext_password.h"
18 #include "eapol_supp/eapol_supp_sm.h"
19 #include "eap_peer/eap.h"
20 #include "eap_server/eap_methods.h"
22 #include "utils/base64.h"
23 #include "rsn_supp/wpa.h"
24 #include "eap_peer/eap_i.h"
25 #include "wpa_supplicant_i.h"
26 #include "radius/radius.h"
27 #include "radius/radius_client.h"
28 #include "common/wpa_ctrl.h"
29 #include "ctrl_iface.h"
30 #include "pcsc_funcs.h"
33 extern int wpa_debug_level
;
34 extern int wpa_debug_show_keys
;
36 struct wpa_driver_ops
*wpa_drivers
[] = { NULL
};
39 struct extra_radius_attr
{
43 struct extra_radius_attr
*next
;
46 struct eapol_test_data
{
47 struct wpa_supplicant
*wpa_s
;
49 int eapol_test_num_reauths
;
51 int num_mppe_ok
, num_mppe_mismatch
;
54 struct radius_msg
*last_recv_radius
;
55 struct in_addr own_ip_addr
;
56 struct radius_client_data
*radius
;
57 struct hostapd_radius_servers
*radius_conf
;
59 /* last received EAP Response from Authentication Server */
60 struct wpabuf
*last_eap_radius
;
62 u8 authenticator_pmk
[PMK_LEN
];
63 size_t authenticator_pmk_len
;
64 int radius_access_accept_received
;
65 int radius_access_reject_received
;
69 size_t eap_identity_len
;
72 u8 own_addr
[ETH_ALEN
];
73 struct extra_radius_attr
*extra_attrs
;
75 FILE *server_cert_file
;
78 static struct eapol_test_data eapol_test
;
81 static void send_eap_request_identity(void *eloop_ctx
, void *timeout_ctx
);
84 static void hostapd_logger_cb(void *ctx
, const u8
*addr
, unsigned int module
,
85 int level
, const char *txt
, size_t len
)
88 wpa_printf(MSG_DEBUG
, "STA " MACSTR
": %s\n",
91 wpa_printf(MSG_DEBUG
, "%s", txt
);
95 static int add_extra_attr(struct radius_msg
*msg
,
96 struct extra_radius_attr
*attr
)
101 char buf
[RADIUS_MAX_ATTR_LEN
+ 1];
103 switch (attr
->syntax
) {
105 os_snprintf(buf
, sizeof(buf
), "%s", attr
->data
);
106 len
= os_strlen(buf
);
114 if (pos
[0] == '0' && pos
[1] == 'x')
116 len
= os_strlen(pos
);
117 if ((len
& 1) || (len
/ 2) > RADIUS_MAX_ATTR_LEN
) {
118 printf("Invalid extra attribute hexstring\n");
122 if (hexstr2bin(pos
, (u8
*) buf
, len
) < 0) {
123 printf("Invalid extra attribute hexstring\n");
128 val
= htonl(atoi(attr
->data
));
129 os_memcpy(buf
, &val
, 4);
133 printf("Incorrect extra attribute syntax specification\n");
137 if (!radius_msg_add_attr(msg
, attr
->type
, (u8
*) buf
, len
)) {
138 printf("Could not add attribute %d\n", attr
->type
);
146 static int add_extra_attrs(struct radius_msg
*msg
,
147 struct extra_radius_attr
*attrs
)
149 struct extra_radius_attr
*p
;
150 for (p
= attrs
; p
; p
= p
->next
) {
151 if (add_extra_attr(msg
, p
) < 0)
158 static struct extra_radius_attr
*
159 find_extra_attr(struct extra_radius_attr
*attrs
, u8 type
)
161 struct extra_radius_attr
*p
;
162 for (p
= attrs
; p
; p
= p
->next
) {
170 static void ieee802_1x_encapsulate_radius(struct eapol_test_data
*e
,
171 const u8
*eap
, size_t len
)
173 struct radius_msg
*msg
;
174 char buf
[RADIUS_MAX_ATTR_LEN
+ 1];
175 const struct eap_hdr
*hdr
;
178 wpa_printf(MSG_DEBUG
, "Encapsulating EAP message into a RADIUS "
181 e
->radius_identifier
= radius_client_get_id(e
->radius
);
182 msg
= radius_msg_new(RADIUS_CODE_ACCESS_REQUEST
,
183 e
->radius_identifier
);
185 printf("Could not create net RADIUS packet\n");
189 radius_msg_make_authenticator(msg
, (u8
*) e
, sizeof(*e
));
191 hdr
= (const struct eap_hdr
*) eap
;
192 pos
= (const u8
*) (hdr
+ 1);
193 if (len
> sizeof(*hdr
) && hdr
->code
== EAP_CODE_RESPONSE
&&
194 pos
[0] == EAP_TYPE_IDENTITY
) {
196 os_free(e
->eap_identity
);
197 e
->eap_identity_len
= len
- sizeof(*hdr
) - 1;
198 e
->eap_identity
= os_malloc(e
->eap_identity_len
);
199 if (e
->eap_identity
) {
200 os_memcpy(e
->eap_identity
, pos
, e
->eap_identity_len
);
201 wpa_hexdump(MSG_DEBUG
, "Learned identity from "
202 "EAP-Response-Identity",
203 e
->eap_identity
, e
->eap_identity_len
);
207 if (e
->eap_identity
&&
208 !radius_msg_add_attr(msg
, RADIUS_ATTR_USER_NAME
,
209 e
->eap_identity
, e
->eap_identity_len
)) {
210 printf("Could not add User-Name\n");
214 if (!find_extra_attr(e
->extra_attrs
, RADIUS_ATTR_NAS_IP_ADDRESS
) &&
215 !radius_msg_add_attr(msg
, RADIUS_ATTR_NAS_IP_ADDRESS
,
216 (u8
*) &e
->own_ip_addr
, 4)) {
217 printf("Could not add NAS-IP-Address\n");
221 os_snprintf(buf
, sizeof(buf
), RADIUS_802_1X_ADDR_FORMAT
,
222 MAC2STR(e
->wpa_s
->own_addr
));
223 if (!find_extra_attr(e
->extra_attrs
, RADIUS_ATTR_CALLING_STATION_ID
)
225 !radius_msg_add_attr(msg
, RADIUS_ATTR_CALLING_STATION_ID
,
226 (u8
*) buf
, os_strlen(buf
))) {
227 printf("Could not add Calling-Station-Id\n");
231 /* TODO: should probably check MTU from driver config; 2304 is max for
232 * IEEE 802.11, but use 1400 to avoid problems with too large packets
234 if (!find_extra_attr(e
->extra_attrs
, RADIUS_ATTR_FRAMED_MTU
) &&
235 !radius_msg_add_attr_int32(msg
, RADIUS_ATTR_FRAMED_MTU
, 1400)) {
236 printf("Could not add Framed-MTU\n");
240 if (!find_extra_attr(e
->extra_attrs
, RADIUS_ATTR_NAS_PORT_TYPE
) &&
241 !radius_msg_add_attr_int32(msg
, RADIUS_ATTR_NAS_PORT_TYPE
,
242 RADIUS_NAS_PORT_TYPE_IEEE_802_11
)) {
243 printf("Could not add NAS-Port-Type\n");
247 os_snprintf(buf
, sizeof(buf
), "%s", e
->connect_info
);
248 if (!find_extra_attr(e
->extra_attrs
, RADIUS_ATTR_CONNECT_INFO
) &&
249 !radius_msg_add_attr(msg
, RADIUS_ATTR_CONNECT_INFO
,
250 (u8
*) buf
, os_strlen(buf
))) {
251 printf("Could not add Connect-Info\n");
255 if (add_extra_attrs(msg
, e
->extra_attrs
) < 0)
258 if (eap
&& !radius_msg_add_eap(msg
, eap
, len
)) {
259 printf("Could not add EAP-Message\n");
263 /* State attribute must be copied if and only if this packet is
264 * Access-Request reply to the previous Access-Challenge */
265 if (e
->last_recv_radius
&&
266 radius_msg_get_hdr(e
->last_recv_radius
)->code
==
267 RADIUS_CODE_ACCESS_CHALLENGE
) {
268 int res
= radius_msg_copy_attr(msg
, e
->last_recv_radius
,
271 printf("Could not copy State attribute from previous "
272 "Access-Challenge\n");
276 wpa_printf(MSG_DEBUG
, " Copied RADIUS State "
281 if (radius_client_send(e
->radius
, msg
, RADIUS_AUTH
, e
->wpa_s
->own_addr
)
287 radius_msg_free(msg
);
291 static int eapol_test_eapol_send(void *ctx
, int type
, const u8
*buf
,
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 eapol_test_data
*e
= ctx
;
308 wpa_config_set_blob(e
->wpa_s
->conf
, blob
);
312 static const struct wpa_config_blob
*
313 eapol_test_get_config_blob(void *ctx
, const char *name
)
315 struct eapol_test_data
*e
= ctx
;
316 return wpa_config_get_blob(e
->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 void eapol_test_write_cert(FILE *f
, const char *subject
,
386 const struct wpabuf
*cert
)
388 unsigned char *encoded
;
390 encoded
= base64_encode(wpabuf_head(cert
), wpabuf_len(cert
), NULL
);
393 fprintf(f
, "%s\n-----BEGIN CERTIFICATE-----\n%s"
394 "-----END CERTIFICATE-----\n\n", subject
, encoded
);
399 static void eapol_test_cert_cb(void *ctx
, int depth
, const char *subject
,
400 const char *cert_hash
,
401 const struct wpabuf
*cert
)
403 struct eapol_test_data
*e
= ctx
;
405 wpa_msg(e
->wpa_s
, MSG_INFO
, WPA_EVENT_EAP_PEER_CERT
406 "depth=%d subject='%s'%s%s",
408 cert_hash
? " hash=" : "",
409 cert_hash
? cert_hash
: "");
413 size_t len
= wpabuf_len(cert
) * 2 + 1;
414 cert_hex
= os_malloc(len
);
416 wpa_snprintf_hex(cert_hex
, len
, wpabuf_head(cert
),
418 wpa_msg_ctrl(e
->wpa_s
, MSG_INFO
,
419 WPA_EVENT_EAP_PEER_CERT
420 "depth=%d subject='%s' cert=%s",
421 depth
, subject
, cert_hex
);
425 if (e
->server_cert_file
)
426 eapol_test_write_cert(e
->server_cert_file
,
432 static void eapol_test_set_anon_id(void *ctx
, const u8
*id
, size_t len
)
434 struct eapol_test_data
*e
= ctx
;
435 struct wpa_supplicant
*wpa_s
= e
->wpa_s
;
439 wpa_hexdump_ascii(MSG_DEBUG
, "EAP method updated anonymous_identity",
442 if (wpa_s
->current_ssid
== NULL
)
446 if (wpa_config_set(wpa_s
->current_ssid
, "anonymous_identity",
450 str
= os_malloc(len
* 2 + 1);
453 wpa_snprintf_hex(str
, len
* 2 + 1, id
, len
);
454 res
= wpa_config_set(wpa_s
->current_ssid
, "anonymous_identity",
463 static int test_eapol(struct eapol_test_data
*e
, struct wpa_supplicant
*wpa_s
,
464 struct wpa_ssid
*ssid
)
466 struct eapol_config eapol_conf
;
467 struct eapol_ctx
*ctx
;
469 ctx
= os_zalloc(sizeof(*ctx
));
471 printf("Failed to allocate EAPOL context.\n");
475 ctx
->msg_ctx
= wpa_s
;
476 ctx
->scard_ctx
= wpa_s
->scard
;
477 ctx
->cb
= eapol_sm_cb
;
479 ctx
->eapol_send_ctx
= wpa_s
;
481 ctx
->eapol_done_cb
= eapol_test_eapol_done_cb
;
482 ctx
->eapol_send
= eapol_test_eapol_send
;
483 ctx
->set_config_blob
= eapol_test_set_config_blob
;
484 ctx
->get_config_blob
= eapol_test_get_config_blob
;
485 ctx
->opensc_engine_path
= wpa_s
->conf
->opensc_engine_path
;
486 ctx
->pkcs11_engine_path
= wpa_s
->conf
->pkcs11_engine_path
;
487 ctx
->pkcs11_module_path
= wpa_s
->conf
->pkcs11_module_path
;
488 ctx
->cert_cb
= eapol_test_cert_cb
;
490 ctx
->set_anon_id
= eapol_test_set_anon_id
;
492 wpa_s
->eapol
= eapol_sm_init(ctx
);
493 if (wpa_s
->eapol
== NULL
) {
495 printf("Failed to initialize EAPOL state machines.\n");
499 wpa_s
->current_ssid
= ssid
;
500 os_memset(&eapol_conf
, 0, sizeof(eapol_conf
));
501 eapol_conf
.accept_802_1x_keys
= 1;
502 eapol_conf
.required_keys
= 0;
503 eapol_conf
.fast_reauth
= wpa_s
->conf
->fast_reauth
;
504 eapol_conf
.workaround
= ssid
->eap_workaround
;
505 eapol_sm_notify_config(wpa_s
->eapol
, &ssid
->eap
, &eapol_conf
);
506 eapol_sm_register_scard_ctx(wpa_s
->eapol
, wpa_s
->scard
);
509 eapol_sm_notify_portValid(wpa_s
->eapol
, FALSE
);
510 /* 802.1X::portControl = Auto */
511 eapol_sm_notify_portEnabled(wpa_s
->eapol
, TRUE
);
517 static void test_eapol_clean(struct eapol_test_data
*e
,
518 struct wpa_supplicant
*wpa_s
)
520 struct extra_radius_attr
*p
, *prev
;
522 radius_client_deinit(e
->radius
);
523 wpabuf_free(e
->last_eap_radius
);
524 radius_msg_free(e
->last_recv_radius
);
525 e
->last_recv_radius
= NULL
;
526 os_free(e
->eap_identity
);
527 e
->eap_identity
= NULL
;
528 eapol_sm_deinit(wpa_s
->eapol
);
530 if (e
->radius_conf
&& e
->radius_conf
->auth_server
) {
531 os_free(e
->radius_conf
->auth_server
->shared_secret
);
532 os_free(e
->radius_conf
->auth_server
);
534 os_free(e
->radius_conf
);
535 e
->radius_conf
= NULL
;
536 scard_deinit(wpa_s
->scard
);
537 if (wpa_s
->ctrl_iface
) {
538 wpa_supplicant_ctrl_iface_deinit(wpa_s
->ctrl_iface
);
539 wpa_s
->ctrl_iface
= NULL
;
542 ext_password_deinit(wpa_s
->ext_pw
);
543 wpa_s
->ext_pw
= NULL
;
545 wpa_config_free(wpa_s
->conf
);
556 static void send_eap_request_identity(void *eloop_ctx
, void *timeout_ctx
)
558 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
560 struct ieee802_1x_hdr
*hdr
;
563 hdr
= (struct ieee802_1x_hdr
*) buf
;
564 hdr
->version
= EAPOL_VERSION
;
565 hdr
->type
= IEEE802_1X_TYPE_EAP_PACKET
;
566 hdr
->length
= htons(5);
568 eap
= (struct eap_hdr
*) (hdr
+ 1);
569 eap
->code
= EAP_CODE_REQUEST
;
571 eap
->length
= htons(5);
572 pos
= (u8
*) (eap
+ 1);
573 *pos
= EAP_TYPE_IDENTITY
;
575 printf("Sending fake EAP-Request-Identity\n");
576 eapol_sm_rx_eapol(wpa_s
->eapol
, wpa_s
->bssid
, buf
,
581 static void eapol_test_timeout(void *eloop_ctx
, void *timeout_ctx
)
583 struct eapol_test_data
*e
= eloop_ctx
;
584 printf("EAPOL test timed out\n");
585 e
->auth_timed_out
= 1;
590 static char *eap_type_text(u8 type
)
593 case EAP_TYPE_IDENTITY
: return "Identity";
594 case EAP_TYPE_NOTIFICATION
: return "Notification";
595 case EAP_TYPE_NAK
: return "Nak";
596 case EAP_TYPE_TLS
: return "TLS";
597 case EAP_TYPE_TTLS
: return "TTLS";
598 case EAP_TYPE_PEAP
: return "PEAP";
599 case EAP_TYPE_SIM
: return "SIM";
600 case EAP_TYPE_GTC
: return "GTC";
601 case EAP_TYPE_MD5
: return "MD5";
602 case EAP_TYPE_OTP
: return "OTP";
603 case EAP_TYPE_FAST
: return "FAST";
604 case EAP_TYPE_SAKE
: return "SAKE";
605 case EAP_TYPE_PSK
: return "PSK";
606 default: return "Unknown";
611 static void ieee802_1x_decapsulate_radius(struct eapol_test_data
*e
)
614 const struct eap_hdr
*hdr
;
617 struct radius_msg
*msg
;
619 if (e
->last_recv_radius
== NULL
)
622 msg
= e
->last_recv_radius
;
624 eap
= radius_msg_get_eap(msg
);
626 /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
627 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
629 wpa_printf(MSG_DEBUG
, "could not extract "
630 "EAP-Message from RADIUS message");
631 wpabuf_free(e
->last_eap_radius
);
632 e
->last_eap_radius
= NULL
;
636 if (wpabuf_len(eap
) < sizeof(*hdr
)) {
637 wpa_printf(MSG_DEBUG
, "too short EAP packet "
638 "received from authentication server");
643 if (wpabuf_len(eap
) > sizeof(*hdr
))
644 eap_type
= (wpabuf_head_u8(eap
))[sizeof(*hdr
)];
646 hdr
= wpabuf_head(eap
);
648 case EAP_CODE_REQUEST
:
649 os_snprintf(buf
, sizeof(buf
), "EAP-Request-%s (%d)",
650 eap_type
>= 0 ? eap_type_text(eap_type
) : "??",
653 case EAP_CODE_RESPONSE
:
654 os_snprintf(buf
, sizeof(buf
), "EAP Response-%s (%d)",
655 eap_type
>= 0 ? eap_type_text(eap_type
) : "??",
658 case EAP_CODE_SUCCESS
:
659 os_strlcpy(buf
, "EAP Success", sizeof(buf
));
660 /* LEAP uses EAP Success within an authentication, so must not
661 * stop here with eloop_terminate(); */
663 case EAP_CODE_FAILURE
:
664 os_strlcpy(buf
, "EAP Failure", sizeof(buf
));
668 os_strlcpy(buf
, "unknown EAP code", sizeof(buf
));
669 wpa_hexdump_buf(MSG_DEBUG
, "Decapsulated EAP packet", eap
);
672 wpa_printf(MSG_DEBUG
, "decapsulated EAP packet (code=%d "
673 "id=%d len=%d) from RADIUS server: %s",
674 hdr
->code
, hdr
->identifier
, ntohs(hdr
->length
), buf
);
676 /* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
678 wpabuf_free(e
->last_eap_radius
);
679 e
->last_eap_radius
= eap
;
682 struct ieee802_1x_hdr
*dot1x
;
683 dot1x
= os_malloc(sizeof(*dot1x
) + wpabuf_len(eap
));
684 assert(dot1x
!= NULL
);
685 dot1x
->version
= EAPOL_VERSION
;
686 dot1x
->type
= IEEE802_1X_TYPE_EAP_PACKET
;
687 dot1x
->length
= htons(wpabuf_len(eap
));
688 os_memcpy((u8
*) (dot1x
+ 1), wpabuf_head(eap
),
690 eapol_sm_rx_eapol(e
->wpa_s
->eapol
, e
->wpa_s
->bssid
,
692 sizeof(*dot1x
) + wpabuf_len(eap
));
698 static void ieee802_1x_get_keys(struct eapol_test_data
*e
,
699 struct radius_msg
*msg
, struct radius_msg
*req
,
700 const u8
*shared_secret
,
701 size_t shared_secret_len
)
703 struct radius_ms_mppe_keys
*keys
;
705 keys
= radius_msg_get_ms_keys(msg
, req
, shared_secret
,
707 if (keys
&& keys
->send
== NULL
&& keys
->recv
== NULL
) {
709 keys
= radius_msg_get_cisco_keys(msg
, req
, shared_secret
,
715 wpa_hexdump(MSG_DEBUG
, "MS-MPPE-Send-Key (sign)",
716 keys
->send
, keys
->send_len
);
719 wpa_hexdump(MSG_DEBUG
, "MS-MPPE-Recv-Key (crypt)",
720 keys
->recv
, keys
->recv_len
);
721 e
->authenticator_pmk_len
=
722 keys
->recv_len
> PMK_LEN
? PMK_LEN
:
724 os_memcpy(e
->authenticator_pmk
, keys
->recv
,
725 e
->authenticator_pmk_len
);
726 if (e
->authenticator_pmk_len
== 16 && keys
->send
&&
727 keys
->send_len
== 16) {
728 /* MS-CHAP-v2 derives 16 octet keys */
729 wpa_printf(MSG_DEBUG
, "Use MS-MPPE-Send-Key "
730 "to extend PMK to 32 octets");
731 os_memcpy(e
->authenticator_pmk
+
732 e
->authenticator_pmk_len
,
733 keys
->send
, keys
->send_len
);
734 e
->authenticator_pmk_len
+= keys
->send_len
;
745 /* Process the RADIUS frames from Authentication Server */
746 static RadiusRxResult
747 ieee802_1x_receive_auth(struct radius_msg
*msg
, struct radius_msg
*req
,
748 const u8
*shared_secret
, size_t shared_secret_len
,
751 struct eapol_test_data
*e
= data
;
752 struct radius_hdr
*hdr
= radius_msg_get_hdr(msg
);
754 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
755 * present when packet contains an EAP-Message attribute */
756 if (hdr
->code
== RADIUS_CODE_ACCESS_REJECT
&&
757 radius_msg_get_attr(msg
, RADIUS_ATTR_MESSAGE_AUTHENTICATOR
, NULL
,
759 radius_msg_get_attr(msg
, RADIUS_ATTR_EAP_MESSAGE
, NULL
, 0) < 0) {
760 wpa_printf(MSG_DEBUG
, "Allowing RADIUS "
761 "Access-Reject without Message-Authenticator "
762 "since it does not include EAP-Message\n");
763 } else if (radius_msg_verify(msg
, shared_secret
, shared_secret_len
,
765 printf("Incoming RADIUS packet did not have correct "
766 "Message-Authenticator - dropped\n");
767 return RADIUS_RX_UNKNOWN
;
770 if (hdr
->code
!= RADIUS_CODE_ACCESS_ACCEPT
&&
771 hdr
->code
!= RADIUS_CODE_ACCESS_REJECT
&&
772 hdr
->code
!= RADIUS_CODE_ACCESS_CHALLENGE
) {
773 printf("Unknown RADIUS message code\n");
774 return RADIUS_RX_UNKNOWN
;
777 e
->radius_identifier
= -1;
778 wpa_printf(MSG_DEBUG
, "RADIUS packet matching with station");
780 radius_msg_free(e
->last_recv_radius
);
781 e
->last_recv_radius
= msg
;
784 case RADIUS_CODE_ACCESS_ACCEPT
:
785 e
->radius_access_accept_received
= 1;
786 ieee802_1x_get_keys(e
, msg
, req
, shared_secret
,
789 case RADIUS_CODE_ACCESS_REJECT
:
790 e
->radius_access_reject_received
= 1;
794 ieee802_1x_decapsulate_radius(e
);
796 if ((hdr
->code
== RADIUS_CODE_ACCESS_ACCEPT
&&
797 e
->eapol_test_num_reauths
< 0) ||
798 hdr
->code
== RADIUS_CODE_ACCESS_REJECT
) {
802 return RADIUS_RX_QUEUED
;
806 static void wpa_init_conf(struct eapol_test_data
*e
,
807 struct wpa_supplicant
*wpa_s
, const char *authsrv
,
808 int port
, const char *secret
,
809 const char *cli_addr
)
811 struct hostapd_radius_server
*as
;
815 os_memcpy(wpa_s
->own_addr
, e
->own_addr
, ETH_ALEN
);
816 e
->own_ip_addr
.s_addr
= htonl((127 << 24) | 1);
817 os_strlcpy(wpa_s
->ifname
, "test", sizeof(wpa_s
->ifname
));
819 e
->radius_conf
= os_zalloc(sizeof(struct hostapd_radius_servers
));
820 assert(e
->radius_conf
!= NULL
);
821 e
->radius_conf
->num_auth_servers
= 1;
822 as
= os_zalloc(sizeof(struct hostapd_radius_server
));
824 #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
828 sscanf(authsrv
, "%d.%d.%d.%d", &a
[0], &a
[1], &a
[2], &a
[3]);
829 pos
= (u8
*) &as
->addr
.u
.v4
;
835 #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
836 inet_aton(authsrv
, &as
->addr
.u
.v4
);
837 #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
838 as
->addr
.af
= AF_INET
;
840 as
->shared_secret
= (u8
*) os_strdup(secret
);
841 as
->shared_secret_len
= os_strlen(secret
);
842 e
->radius_conf
->auth_server
= as
;
843 e
->radius_conf
->auth_servers
= as
;
844 e
->radius_conf
->msg_dumps
= 1;
846 if (hostapd_parse_ip_addr(cli_addr
,
847 &e
->radius_conf
->client_addr
) == 0)
848 e
->radius_conf
->force_client_addr
= 1;
850 wpa_printf(MSG_ERROR
, "Invalid IP address '%s'",
856 e
->radius
= radius_client_init(wpa_s
, e
->radius_conf
);
857 assert(e
->radius
!= NULL
);
859 res
= radius_client_register(e
->radius
, RADIUS_AUTH
,
860 ieee802_1x_receive_auth
, e
);
865 static int scard_test(void)
867 struct scard_data
*scard
;
870 unsigned char _rand
[16];
872 unsigned char sres
[4];
874 #endif /* PCSC_FUNCS */
875 #define num_triplets 5
876 unsigned char rand_
[num_triplets
][16];
877 unsigned char sres_
[num_triplets
][4];
878 unsigned char kc_
[num_triplets
][8];
882 #define AKA_RAND_LEN 16
883 #define AKA_AUTN_LEN 16
884 #define AKA_AUTS_LEN 14
885 #define RES_MAX_LEN 16
888 unsigned char aka_rand
[AKA_RAND_LEN
];
889 unsigned char aka_autn
[AKA_AUTN_LEN
];
890 unsigned char aka_auts
[AKA_AUTS_LEN
];
891 unsigned char aka_res
[RES_MAX_LEN
];
893 unsigned char aka_ik
[IK_LEN
];
894 unsigned char aka_ck
[CK_LEN
];
896 scard
= scard_init(SCARD_TRY_BOTH
, NULL
);
899 if (scard_set_pin(scard
, "1234")) {
900 wpa_printf(MSG_WARNING
, "PIN validation failed");
906 if (scard_get_imsi(scard
, imsi
, &len
))
908 wpa_hexdump_ascii(MSG_DEBUG
, "SCARD: IMSI", (u8
*) imsi
, len
);
909 /* NOTE: Permanent Username: 1 | IMSI */
911 wpa_printf(MSG_DEBUG
, "SCARD: MNC length %d",
912 scard_get_mnc_len(scard
));
914 os_memset(_rand
, 0, sizeof(_rand
));
915 if (scard_gsm_auth(scard
, _rand
, sres
, kc
))
918 os_memset(_rand
, 0xff, sizeof(_rand
));
919 if (scard_gsm_auth(scard
, _rand
, sres
, kc
))
922 for (i
= 0; i
< num_triplets
; i
++) {
923 os_memset(rand_
[i
], i
, sizeof(rand_
[i
]));
924 if (scard_gsm_auth(scard
, rand_
[i
], sres_
[i
], kc_
[i
]))
928 for (i
= 0; i
< num_triplets
; i
++) {
930 for (j
= 0; j
< len
; j
++)
931 printf("%c", imsi
[j
]);
933 for (j
= 0; j
< 16; j
++)
934 printf("%02X", rand_
[i
][j
]);
936 for (j
= 0; j
< 4; j
++)
937 printf("%02X", sres_
[i
][j
]);
939 for (j
= 0; j
< 8; j
++)
940 printf("%02X", kc_
[i
][j
]);
944 wpa_printf(MSG_DEBUG
, "Trying to use UMTS authentication");
947 os_memset(aka_rand
, 0xaa, 16);
948 os_memcpy(aka_autn
, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
949 "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
951 res
= scard_umts_auth(scard
, aka_rand
, aka_autn
, aka_res
, &aka_res_len
,
952 aka_ik
, aka_ck
, aka_auts
);
954 wpa_printf(MSG_DEBUG
, "UMTS auth completed successfully");
955 wpa_hexdump(MSG_DEBUG
, "RES", aka_res
, aka_res_len
);
956 wpa_hexdump(MSG_DEBUG
, "IK", aka_ik
, IK_LEN
);
957 wpa_hexdump(MSG_DEBUG
, "CK", aka_ck
, CK_LEN
);
958 } else if (res
== -2) {
959 wpa_printf(MSG_DEBUG
, "UMTS auth resulted in synchronization "
961 wpa_hexdump(MSG_DEBUG
, "AUTS", aka_auts
, AKA_AUTS_LEN
);
963 wpa_printf(MSG_DEBUG
, "UMTS auth failed");
974 static int scard_get_triplets(int argc
, char *argv
[])
976 struct scard_data
*scard
;
979 unsigned char _rand
[16];
980 unsigned char sres
[4];
986 if (argc
< 2 || ((num_triplets
= atoi(argv
[1])) <= 0)) {
987 printf("invalid parameters for sim command\n");
991 if (argc
<= 2 || os_strcmp(argv
[2], "debug") != 0) {
992 /* disable debug output */
993 wpa_debug_level
= 99;
996 scard
= scard_init(SCARD_GSM_SIM_ONLY
, NULL
);
998 printf("Failed to open smartcard connection\n");
1001 if (scard_set_pin(scard
, argv
[0])) {
1002 wpa_printf(MSG_WARNING
, "PIN validation failed");
1003 scard_deinit(scard
);
1008 if (scard_get_imsi(scard
, imsi
, &len
)) {
1009 scard_deinit(scard
);
1013 for (i
= 0; i
< num_triplets
; i
++) {
1014 os_memset(_rand
, i
, sizeof(_rand
));
1015 if (scard_gsm_auth(scard
, _rand
, sres
, kc
))
1018 /* IMSI:Kc:SRES:RAND */
1019 for (j
= 0; j
< len
; j
++)
1020 printf("%c", imsi
[j
]);
1022 for (j
= 0; j
< 8; j
++)
1023 printf("%02X", kc
[j
]);
1025 for (j
= 0; j
< 4; j
++)
1026 printf("%02X", sres
[j
]);
1028 for (j
= 0; j
< 16; j
++)
1029 printf("%02X", _rand
[j
]);
1033 scard_deinit(scard
);
1039 static void eapol_test_terminate(int sig
, void *signal_ctx
)
1041 struct wpa_supplicant
*wpa_s
= signal_ctx
;
1042 wpa_msg(wpa_s
, MSG_INFO
, "Signal %d received - terminating", sig
);
1047 static void usage(void)
1050 "eapol_test [-nWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
1051 "[-s<AS secret>]\\\n"
1052 " [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
1053 " [-M<client MAC address>] [-o<server cert file] \\\n"
1054 " [-N<attr spec>] \\\n"
1055 " [-A<client IP>]\n"
1056 "eapol_test scard\n"
1057 "eapol_test sim <PIN> <num triplets> [debug]\n"
1060 " -c<conf> = configuration file\n"
1061 " -a<AS IP> = IP address of the authentication server, "
1062 "default 127.0.0.1\n"
1063 " -p<AS port> = UDP port of the authentication server, "
1065 " -s<AS secret> = shared secret with the authentication "
1066 "server, default 'radius'\n"
1067 " -A<client IP> = IP address of the client, default: select "
1069 " -r<count> = number of re-authentications\n"
1070 " -W = wait for a control interface monitor before starting\n"
1071 " -S = save configuration after authentication\n"
1072 " -n = no MPPE keys expected\n"
1073 " -t<timeout> = sets timeout in seconds (default: 30 s)\n"
1074 " -C<Connect-Info> = RADIUS Connect-Info (default: "
1075 "CONNECT 11Mbps 802.11b)\n"
1076 " -M<client MAC address> = Set own MAC address "
1077 "(Calling-Station-Id,\n"
1078 " default: 02:00:00:00:00:01)\n"
1079 " -o<server cert file> = Write received server certificate\n"
1080 " chain to the specified file\n"
1081 " -N<attr spec> = send arbitrary attribute specified by:\n"
1082 " attr_id:syntax:value or attr_id\n"
1083 " attr_id - number id of the attribute\n"
1084 " syntax - one of: s, d, x\n"
1087 " x = octet string\n"
1088 " value - attribute value.\n"
1089 " When only attr_id is specified, NULL will be used as "
1091 " Multiple attributes can be specified by using the "
1092 "option several times.\n");
1096 int main(int argc
, char *argv
[])
1098 struct wpa_supplicant wpa_s
;
1099 int c
, ret
= 1, wait_for_monitor
= 0, save_config
= 0;
1100 char *as_addr
= "127.0.0.1";
1102 char *as_secret
= "radius";
1103 char *cli_addr
= NULL
;
1107 struct extra_radius_attr
*p
= NULL
, *p1
;
1109 if (os_program_init())
1112 hostapd_logger_register_cb(hostapd_logger_cb
);
1114 os_memset(&eapol_test
, 0, sizeof(eapol_test
));
1115 eapol_test
.connect_info
= "CONNECT 11Mbps 802.11b";
1116 os_memcpy(eapol_test
.own_addr
, "\x02\x00\x00\x00\x00\x01", ETH_ALEN
);
1118 wpa_debug_level
= 0;
1119 wpa_debug_show_keys
= 1;
1122 c
= getopt(argc
, argv
, "a:A:c:C:M:nN:o:p:r:s:St:W");
1136 eapol_test
.connect_info
= optarg
;
1139 if (hwaddr_aton(optarg
, eapol_test
.own_addr
)) {
1145 eapol_test
.no_mppe_keys
++;
1148 if (eapol_test
.server_cert_file
)
1149 fclose(eapol_test
.server_cert_file
);
1150 eapol_test
.server_cert_file
= fopen(optarg
, "w");
1151 if (eapol_test
.server_cert_file
== NULL
) {
1152 printf("Could not open '%s' for writing\n",
1158 as_port
= atoi(optarg
);
1161 eapol_test
.eapol_test_num_reauths
= atoi(optarg
);
1170 timeout
= atoi(optarg
);
1176 p1
= os_zalloc(sizeof(*p1
));
1180 eapol_test
.extra_attrs
= p1
;
1185 p
->type
= atoi(optarg
);
1186 pos
= os_strchr(optarg
, ':');
1194 if (pos
[0] == '\0' || pos
[1] != ':') {
1195 printf("Incorrect format of attribute "
1209 if (argc
> optind
&& os_strcmp(argv
[optind
], "scard") == 0) {
1210 return scard_test();
1213 if (argc
> optind
&& os_strcmp(argv
[optind
], "sim") == 0) {
1214 return scard_get_triplets(argc
- optind
- 1,
1220 printf("Configuration file is required.\n");
1224 if (eap_register_methods()) {
1225 wpa_printf(MSG_ERROR
, "Failed to register EAP methods");
1230 wpa_printf(MSG_ERROR
, "Failed to initialize event loop");
1234 os_memset(&wpa_s
, 0, sizeof(wpa_s
));
1235 eapol_test
.wpa_s
= &wpa_s
;
1236 wpa_s
.conf
= wpa_config_read(conf
);
1237 if (wpa_s
.conf
== NULL
) {
1238 printf("Failed to parse configuration file '%s'.\n", conf
);
1241 if (wpa_s
.conf
->ssid
== NULL
) {
1242 printf("No networks defined.\n");
1246 wpa_init_conf(&eapol_test
, &wpa_s
, as_addr
, as_port
, as_secret
,
1248 wpa_s
.ctrl_iface
= wpa_supplicant_ctrl_iface_init(&wpa_s
);
1249 if (wpa_s
.ctrl_iface
== NULL
) {
1250 printf("Failed to initialize control interface '%s'.\n"
1251 "You may have another eapol_test process already "
1252 "running or the file was\n"
1253 "left by an unclean termination of eapol_test in "
1254 "which case you will need\n"
1255 "to manually remove this file before starting "
1256 "eapol_test again.\n",
1257 wpa_s
.conf
->ctrl_interface
);
1260 if (wpa_supplicant_scard_init(&wpa_s
, wpa_s
.conf
->ssid
))
1263 if (test_eapol(&eapol_test
, &wpa_s
, wpa_s
.conf
->ssid
))
1266 if (wpas_init_ext_pw(&wpa_s
) < 0)
1269 if (wait_for_monitor
)
1270 wpa_supplicant_ctrl_iface_wait(wpa_s
.ctrl_iface
);
1272 eloop_register_timeout(timeout
, 0, eapol_test_timeout
, &eapol_test
,
1274 eloop_register_timeout(0, 0, send_eap_request_identity
, &wpa_s
, NULL
);
1275 eloop_register_signal_terminate(eapol_test_terminate
, &wpa_s
);
1276 eloop_register_signal_reconfig(eapol_test_terminate
, &wpa_s
);
1279 eloop_cancel_timeout(eapol_test_timeout
, &eapol_test
, NULL
);
1280 eloop_cancel_timeout(eapol_sm_reauth
, &eapol_test
, NULL
);
1282 if (eapol_test_compare_pmk(&eapol_test
) == 0 ||
1283 eapol_test
.no_mppe_keys
)
1285 if (eapol_test
.auth_timed_out
)
1287 if (eapol_test
.radius_access_reject_received
)
1291 wpa_config_write(conf
, wpa_s
.conf
);
1293 test_eapol_clean(&eapol_test
, &wpa_s
);
1295 eap_peer_unregister_methods();
1297 eap_server_unregister_methods();
1298 #endif /* CONFIG_AP */
1302 if (eapol_test
.server_cert_file
)
1303 fclose(eapol_test
.server_cert_file
);
1305 printf("MPPE keys OK: %d mismatch: %d\n",
1306 eapol_test
.num_mppe_ok
, eapol_test
.num_mppe_mismatch
);
1307 if (eapol_test
.num_mppe_mismatch
)
1310 printf("FAILURE\n");
1312 printf("SUCCESS\n");
1314 os_program_deinit();