2 * WPA Supplicant - test code
3 * Copyright (c) 2003-2013, 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 "wpa_supplicant_i.h"
25 #include "radius/radius.h"
26 #include "radius/radius_client.h"
27 #include "common/wpa_ctrl.h"
28 #include "ctrl_iface.h"
29 #include "pcsc_funcs.h"
30 #include "wpas_glue.h"
33 struct wpa_driver_ops
*wpa_drivers
[] = { NULL
};
36 struct extra_radius_attr
{
40 struct extra_radius_attr
*next
;
43 struct eapol_test_data
{
44 struct wpa_supplicant
*wpa_s
;
46 int eapol_test_num_reauths
;
48 int num_mppe_ok
, num_mppe_mismatch
;
52 struct radius_msg
*last_recv_radius
;
53 struct in_addr own_ip_addr
;
54 struct radius_client_data
*radius
;
55 struct hostapd_radius_servers
*radius_conf
;
57 /* last received EAP Response from Authentication Server */
58 struct wpabuf
*last_eap_radius
;
60 u8 authenticator_pmk
[PMK_LEN
];
61 size_t authenticator_pmk_len
;
62 u8 authenticator_eap_key_name
[256];
63 size_t authenticator_eap_key_name_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
;
77 const char *pcsc_reader
;
81 static struct eapol_test_data eapol_test
;
84 static void send_eap_request_identity(void *eloop_ctx
, void *timeout_ctx
);
87 static void hostapd_logger_cb(void *ctx
, const u8
*addr
, unsigned int module
,
88 int level
, const char *txt
, size_t len
)
91 wpa_printf(MSG_DEBUG
, "STA " MACSTR
": %s\n",
94 wpa_printf(MSG_DEBUG
, "%s", txt
);
98 static int add_extra_attr(struct radius_msg
*msg
,
99 struct extra_radius_attr
*attr
)
104 char buf
[RADIUS_MAX_ATTR_LEN
+ 1];
106 switch (attr
->syntax
) {
108 os_snprintf(buf
, sizeof(buf
), "%s", attr
->data
);
109 len
= os_strlen(buf
);
117 if (pos
[0] == '0' && pos
[1] == 'x')
119 len
= os_strlen(pos
);
120 if ((len
& 1) || (len
/ 2) > RADIUS_MAX_ATTR_LEN
) {
121 printf("Invalid extra attribute hexstring\n");
125 if (hexstr2bin(pos
, (u8
*) buf
, len
) < 0) {
126 printf("Invalid extra attribute hexstring\n");
131 val
= htonl(atoi(attr
->data
));
132 os_memcpy(buf
, &val
, 4);
136 printf("Incorrect extra attribute syntax specification\n");
140 if (!radius_msg_add_attr(msg
, attr
->type
, (u8
*) buf
, len
)) {
141 printf("Could not add attribute %d\n", attr
->type
);
149 static int add_extra_attrs(struct radius_msg
*msg
,
150 struct extra_radius_attr
*attrs
)
152 struct extra_radius_attr
*p
;
153 for (p
= attrs
; p
; p
= p
->next
) {
154 if (add_extra_attr(msg
, p
) < 0)
161 static struct extra_radius_attr
*
162 find_extra_attr(struct extra_radius_attr
*attrs
, u8 type
)
164 struct extra_radius_attr
*p
;
165 for (p
= attrs
; p
; p
= p
->next
) {
173 static void ieee802_1x_encapsulate_radius(struct eapol_test_data
*e
,
174 const u8
*eap
, size_t len
)
176 struct radius_msg
*msg
;
177 char buf
[RADIUS_MAX_ATTR_LEN
+ 1];
178 const struct eap_hdr
*hdr
;
181 wpa_printf(MSG_DEBUG
, "Encapsulating EAP message into a RADIUS "
184 e
->radius_identifier
= radius_client_get_id(e
->radius
);
185 msg
= radius_msg_new(RADIUS_CODE_ACCESS_REQUEST
,
186 e
->radius_identifier
);
188 printf("Could not create net RADIUS packet\n");
192 radius_msg_make_authenticator(msg
, (u8
*) e
, sizeof(*e
));
194 hdr
= (const struct eap_hdr
*) eap
;
195 pos
= (const u8
*) (hdr
+ 1);
196 if (len
> sizeof(*hdr
) && hdr
->code
== EAP_CODE_RESPONSE
&&
197 pos
[0] == EAP_TYPE_IDENTITY
) {
199 os_free(e
->eap_identity
);
200 e
->eap_identity_len
= len
- sizeof(*hdr
) - 1;
201 e
->eap_identity
= os_malloc(e
->eap_identity_len
);
202 if (e
->eap_identity
) {
203 os_memcpy(e
->eap_identity
, pos
, e
->eap_identity_len
);
204 wpa_hexdump(MSG_DEBUG
, "Learned identity from "
205 "EAP-Response-Identity",
206 e
->eap_identity
, e
->eap_identity_len
);
210 if (e
->eap_identity
&&
211 !radius_msg_add_attr(msg
, RADIUS_ATTR_USER_NAME
,
212 e
->eap_identity
, e
->eap_identity_len
)) {
213 printf("Could not add User-Name\n");
217 if (e
->req_eap_key_name
&&
218 !radius_msg_add_attr(msg
, RADIUS_ATTR_EAP_KEY_NAME
, (u8
*) "\0",
220 printf("Could not add EAP-Key-Name\n");
224 if (!find_extra_attr(e
->extra_attrs
, RADIUS_ATTR_NAS_IP_ADDRESS
) &&
225 !radius_msg_add_attr(msg
, RADIUS_ATTR_NAS_IP_ADDRESS
,
226 (u8
*) &e
->own_ip_addr
, 4)) {
227 printf("Could not add NAS-IP-Address\n");
231 os_snprintf(buf
, sizeof(buf
), RADIUS_802_1X_ADDR_FORMAT
,
232 MAC2STR(e
->wpa_s
->own_addr
));
233 if (!find_extra_attr(e
->extra_attrs
, RADIUS_ATTR_CALLING_STATION_ID
)
235 !radius_msg_add_attr(msg
, RADIUS_ATTR_CALLING_STATION_ID
,
236 (u8
*) buf
, os_strlen(buf
))) {
237 printf("Could not add Calling-Station-Id\n");
241 /* TODO: should probably check MTU from driver config; 2304 is max for
242 * IEEE 802.11, but use 1400 to avoid problems with too large packets
244 if (!find_extra_attr(e
->extra_attrs
, RADIUS_ATTR_FRAMED_MTU
) &&
245 !radius_msg_add_attr_int32(msg
, RADIUS_ATTR_FRAMED_MTU
, 1400)) {
246 printf("Could not add Framed-MTU\n");
250 if (!find_extra_attr(e
->extra_attrs
, RADIUS_ATTR_NAS_PORT_TYPE
) &&
251 !radius_msg_add_attr_int32(msg
, RADIUS_ATTR_NAS_PORT_TYPE
,
252 RADIUS_NAS_PORT_TYPE_IEEE_802_11
)) {
253 printf("Could not add NAS-Port-Type\n");
257 os_snprintf(buf
, sizeof(buf
), "%s", e
->connect_info
);
258 if (!find_extra_attr(e
->extra_attrs
, RADIUS_ATTR_CONNECT_INFO
) &&
259 !radius_msg_add_attr(msg
, RADIUS_ATTR_CONNECT_INFO
,
260 (u8
*) buf
, os_strlen(buf
))) {
261 printf("Could not add Connect-Info\n");
265 if (add_extra_attrs(msg
, e
->extra_attrs
) < 0)
268 if (eap
&& !radius_msg_add_eap(msg
, eap
, len
)) {
269 printf("Could not add EAP-Message\n");
273 /* State attribute must be copied if and only if this packet is
274 * Access-Request reply to the previous Access-Challenge */
275 if (e
->last_recv_radius
&&
276 radius_msg_get_hdr(e
->last_recv_radius
)->code
==
277 RADIUS_CODE_ACCESS_CHALLENGE
) {
278 int res
= radius_msg_copy_attr(msg
, e
->last_recv_radius
,
281 printf("Could not copy State attribute from previous "
282 "Access-Challenge\n");
286 wpa_printf(MSG_DEBUG
, " Copied RADIUS State "
291 if (radius_client_send(e
->radius
, msg
, RADIUS_AUTH
, e
->wpa_s
->own_addr
)
297 radius_msg_free(msg
);
301 static int eapol_test_eapol_send(void *ctx
, int type
, const u8
*buf
,
304 printf("WPA: eapol_test_eapol_send(type=%d len=%lu)\n",
305 type
, (unsigned long) len
);
306 if (type
== IEEE802_1X_TYPE_EAP_PACKET
) {
307 wpa_hexdump(MSG_DEBUG
, "TX EAP -> RADIUS", buf
, len
);
308 ieee802_1x_encapsulate_radius(&eapol_test
, buf
, len
);
314 static void eapol_test_set_config_blob(void *ctx
,
315 struct wpa_config_blob
*blob
)
317 struct eapol_test_data
*e
= ctx
;
318 wpa_config_set_blob(e
->wpa_s
->conf
, blob
);
322 static const struct wpa_config_blob
*
323 eapol_test_get_config_blob(void *ctx
, const char *name
)
325 struct eapol_test_data
*e
= ctx
;
326 return wpa_config_get_blob(e
->wpa_s
->conf
, name
);
330 static void eapol_test_eapol_done_cb(void *ctx
)
332 printf("WPA: EAPOL processing complete\n");
336 static void eapol_sm_reauth(void *eloop_ctx
, void *timeout_ctx
)
338 struct eapol_test_data
*e
= eloop_ctx
;
339 printf("\n\n\n\n\neapol_test: Triggering EAP reauthentication\n\n");
340 e
->radius_access_accept_received
= 0;
341 send_eap_request_identity(e
->wpa_s
, NULL
);
345 static int eapol_test_compare_pmk(struct eapol_test_data
*e
)
352 if (eapol_sm_get_key(e
->wpa_s
->eapol
, pmk
, PMK_LEN
) == 0) {
353 wpa_hexdump(MSG_DEBUG
, "PMK from EAPOL", pmk
, PMK_LEN
);
354 if (os_memcmp(pmk
, e
->authenticator_pmk
, PMK_LEN
) != 0) {
355 printf("WARNING: PMK mismatch\n");
356 wpa_hexdump(MSG_DEBUG
, "PMK from AS",
357 e
->authenticator_pmk
, PMK_LEN
);
358 } else if (e
->radius_access_accept_received
)
360 } else if (e
->authenticator_pmk_len
== 16 &&
361 eapol_sm_get_key(e
->wpa_s
->eapol
, pmk
, 16) == 0) {
362 wpa_hexdump(MSG_DEBUG
, "LEAP PMK from EAPOL", pmk
, 16);
363 if (os_memcmp(pmk
, e
->authenticator_pmk
, 16) != 0) {
364 printf("WARNING: PMK mismatch\n");
365 wpa_hexdump(MSG_DEBUG
, "PMK from AS",
366 e
->authenticator_pmk
, 16);
367 } else if (e
->radius_access_accept_received
)
369 } else if (e
->radius_access_accept_received
&& e
->no_mppe_keys
) {
370 /* No keying material expected */
374 if (ret
&& !e
->no_mppe_keys
)
375 e
->num_mppe_mismatch
++;
376 else if (!e
->no_mppe_keys
)
379 sess_id
= eapol_sm_get_session_id(e
->wpa_s
->eapol
, &sess_id_len
);
382 if (e
->authenticator_eap_key_name_len
== 0) {
383 wpa_printf(MSG_INFO
, "No EAP-Key-Name received from server");
387 if (e
->authenticator_eap_key_name_len
!= sess_id_len
||
388 os_memcmp(e
->authenticator_eap_key_name
, sess_id
, sess_id_len
) != 0)
391 "Locally derived EAP Session-Id does not match EAP-Key-Name from server");
392 wpa_hexdump(MSG_DEBUG
, "EAP Session-Id", sess_id
, sess_id_len
);
393 wpa_hexdump(MSG_DEBUG
, "EAP-Key-Name from server",
394 e
->authenticator_eap_key_name
,
395 e
->authenticator_eap_key_name_len
);
398 "Locally derived EAP Session-Id matches EAP-Key-Name from server");
405 static void eapol_sm_cb(struct eapol_sm
*eapol
, enum eapol_supp_result result
,
408 struct eapol_test_data
*e
= ctx
;
409 printf("eapol_sm_cb: result=%d\n", result
);
410 e
->eapol_test_num_reauths
--;
411 if (e
->eapol_test_num_reauths
< 0)
414 eapol_test_compare_pmk(e
);
415 eloop_register_timeout(0, 100000, eapol_sm_reauth
, e
, NULL
);
420 static void eapol_test_write_cert(FILE *f
, const char *subject
,
421 const struct wpabuf
*cert
)
423 unsigned char *encoded
;
425 encoded
= base64_encode(wpabuf_head(cert
), wpabuf_len(cert
), NULL
);
428 fprintf(f
, "%s\n-----BEGIN CERTIFICATE-----\n%s"
429 "-----END CERTIFICATE-----\n\n", subject
, encoded
);
434 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
435 static void eapol_test_eap_param_needed(void *ctx
, enum wpa_ctrl_req_type field
,
436 const char *default_txt
)
438 struct eapol_test_data
*e
= ctx
;
439 struct wpa_supplicant
*wpa_s
= e
->wpa_s
;
440 struct wpa_ssid
*ssid
= wpa_s
->current_ssid
;
441 const char *field_name
, *txt
= NULL
;
449 field_name
= wpa_supplicant_ctrl_req_to_string(field
, default_txt
,
451 if (field_name
== NULL
) {
452 wpa_printf(MSG_WARNING
, "Unhandled EAP param %d needed",
457 buflen
= 100 + os_strlen(txt
) + ssid
->ssid_len
;
458 buf
= os_malloc(buflen
);
461 len
= os_snprintf(buf
, buflen
,
462 WPA_CTRL_REQ
"%s-%d:%s needed for SSID ",
463 field_name
, ssid
->id
, txt
);
464 if (os_snprintf_error(buflen
, len
)) {
468 if (ssid
->ssid
&& buflen
> len
+ ssid
->ssid_len
) {
469 os_memcpy(buf
+ len
, ssid
->ssid
, ssid
->ssid_len
);
470 len
+= ssid
->ssid_len
;
473 buf
[buflen
- 1] = '\0';
474 wpa_msg(wpa_s
, MSG_INFO
, "%s", buf
);
477 #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
478 #define eapol_test_eap_param_needed NULL
479 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
482 static void eapol_test_cert_cb(void *ctx
, int depth
, const char *subject
,
483 const char *cert_hash
,
484 const struct wpabuf
*cert
)
486 struct eapol_test_data
*e
= ctx
;
488 wpa_msg(e
->wpa_s
, MSG_INFO
, WPA_EVENT_EAP_PEER_CERT
489 "depth=%d subject='%s'%s%s",
491 cert_hash
? " hash=" : "",
492 cert_hash
? cert_hash
: "");
496 size_t len
= wpabuf_len(cert
) * 2 + 1;
497 cert_hex
= os_malloc(len
);
499 wpa_snprintf_hex(cert_hex
, len
, wpabuf_head(cert
),
501 wpa_msg_ctrl(e
->wpa_s
, MSG_INFO
,
502 WPA_EVENT_EAP_PEER_CERT
503 "depth=%d subject='%s' cert=%s",
504 depth
, subject
, cert_hex
);
508 if (e
->server_cert_file
)
509 eapol_test_write_cert(e
->server_cert_file
,
515 static void eapol_test_set_anon_id(void *ctx
, const u8
*id
, size_t len
)
517 struct eapol_test_data
*e
= ctx
;
518 struct wpa_supplicant
*wpa_s
= e
->wpa_s
;
522 wpa_hexdump_ascii(MSG_DEBUG
, "EAP method updated anonymous_identity",
525 if (wpa_s
->current_ssid
== NULL
)
529 if (wpa_config_set(wpa_s
->current_ssid
, "anonymous_identity",
533 str
= os_malloc(len
* 2 + 1);
536 wpa_snprintf_hex(str
, len
* 2 + 1, id
, len
);
537 res
= wpa_config_set(wpa_s
->current_ssid
, "anonymous_identity",
546 static int test_eapol(struct eapol_test_data
*e
, struct wpa_supplicant
*wpa_s
,
547 struct wpa_ssid
*ssid
)
549 struct eapol_config eapol_conf
;
550 struct eapol_ctx
*ctx
;
552 ctx
= os_zalloc(sizeof(*ctx
));
554 printf("Failed to allocate EAPOL context.\n");
558 ctx
->msg_ctx
= wpa_s
;
559 ctx
->scard_ctx
= wpa_s
->scard
;
560 ctx
->cb
= eapol_sm_cb
;
562 ctx
->eapol_send_ctx
= wpa_s
;
564 ctx
->eapol_done_cb
= eapol_test_eapol_done_cb
;
565 ctx
->eapol_send
= eapol_test_eapol_send
;
566 ctx
->set_config_blob
= eapol_test_set_config_blob
;
567 ctx
->get_config_blob
= eapol_test_get_config_blob
;
568 ctx
->opensc_engine_path
= wpa_s
->conf
->opensc_engine_path
;
569 ctx
->pkcs11_engine_path
= wpa_s
->conf
->pkcs11_engine_path
;
570 ctx
->pkcs11_module_path
= wpa_s
->conf
->pkcs11_module_path
;
571 ctx
->openssl_ciphers
= wpa_s
->conf
->openssl_ciphers
;
572 ctx
->eap_param_needed
= eapol_test_eap_param_needed
;
573 ctx
->cert_cb
= eapol_test_cert_cb
;
575 ctx
->set_anon_id
= eapol_test_set_anon_id
;
577 wpa_s
->eapol
= eapol_sm_init(ctx
);
578 if (wpa_s
->eapol
== NULL
) {
580 printf("Failed to initialize EAPOL state machines.\n");
584 wpa_s
->current_ssid
= ssid
;
585 os_memset(&eapol_conf
, 0, sizeof(eapol_conf
));
586 eapol_conf
.accept_802_1x_keys
= 1;
587 eapol_conf
.required_keys
= 0;
588 eapol_conf
.fast_reauth
= wpa_s
->conf
->fast_reauth
;
589 eapol_conf
.workaround
= ssid
->eap_workaround
;
590 eapol_conf
.external_sim
= wpa_s
->conf
->external_sim
;
591 eapol_sm_notify_config(wpa_s
->eapol
, &ssid
->eap
, &eapol_conf
);
592 eapol_sm_register_scard_ctx(wpa_s
->eapol
, wpa_s
->scard
);
595 eapol_sm_notify_portValid(wpa_s
->eapol
, FALSE
);
596 /* 802.1X::portControl = Auto */
597 eapol_sm_notify_portEnabled(wpa_s
->eapol
, TRUE
);
603 static void test_eapol_clean(struct eapol_test_data
*e
,
604 struct wpa_supplicant
*wpa_s
)
606 struct extra_radius_attr
*p
, *prev
;
608 radius_client_deinit(e
->radius
);
609 wpabuf_free(e
->last_eap_radius
);
610 radius_msg_free(e
->last_recv_radius
);
611 e
->last_recv_radius
= NULL
;
612 os_free(e
->eap_identity
);
613 e
->eap_identity
= NULL
;
614 eapol_sm_deinit(wpa_s
->eapol
);
616 if (e
->radius_conf
&& e
->radius_conf
->auth_server
) {
617 os_free(e
->radius_conf
->auth_server
->shared_secret
);
618 os_free(e
->radius_conf
->auth_server
);
620 os_free(e
->radius_conf
);
621 e
->radius_conf
= NULL
;
622 scard_deinit(wpa_s
->scard
);
623 if (wpa_s
->ctrl_iface
) {
624 wpa_supplicant_ctrl_iface_deinit(wpa_s
->ctrl_iface
);
625 wpa_s
->ctrl_iface
= NULL
;
628 ext_password_deinit(wpa_s
->ext_pw
);
629 wpa_s
->ext_pw
= NULL
;
631 wpa_config_free(wpa_s
->conf
);
642 static void send_eap_request_identity(void *eloop_ctx
, void *timeout_ctx
)
644 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
646 struct ieee802_1x_hdr
*hdr
;
649 hdr
= (struct ieee802_1x_hdr
*) buf
;
650 hdr
->version
= EAPOL_VERSION
;
651 hdr
->type
= IEEE802_1X_TYPE_EAP_PACKET
;
652 hdr
->length
= htons(5);
654 eap
= (struct eap_hdr
*) (hdr
+ 1);
655 eap
->code
= EAP_CODE_REQUEST
;
657 eap
->length
= htons(5);
658 pos
= (u8
*) (eap
+ 1);
659 *pos
= EAP_TYPE_IDENTITY
;
661 printf("Sending fake EAP-Request-Identity\n");
662 eapol_sm_rx_eapol(wpa_s
->eapol
, wpa_s
->bssid
, buf
,
667 static void eapol_test_timeout(void *eloop_ctx
, void *timeout_ctx
)
669 struct eapol_test_data
*e
= eloop_ctx
;
670 printf("EAPOL test timed out\n");
671 e
->auth_timed_out
= 1;
676 static char *eap_type_text(u8 type
)
679 case EAP_TYPE_IDENTITY
: return "Identity";
680 case EAP_TYPE_NOTIFICATION
: return "Notification";
681 case EAP_TYPE_NAK
: return "Nak";
682 case EAP_TYPE_TLS
: return "TLS";
683 case EAP_TYPE_TTLS
: return "TTLS";
684 case EAP_TYPE_PEAP
: return "PEAP";
685 case EAP_TYPE_SIM
: return "SIM";
686 case EAP_TYPE_GTC
: return "GTC";
687 case EAP_TYPE_MD5
: return "MD5";
688 case EAP_TYPE_OTP
: return "OTP";
689 case EAP_TYPE_FAST
: return "FAST";
690 case EAP_TYPE_SAKE
: return "SAKE";
691 case EAP_TYPE_PSK
: return "PSK";
692 default: return "Unknown";
697 static void ieee802_1x_decapsulate_radius(struct eapol_test_data
*e
)
700 const struct eap_hdr
*hdr
;
703 struct radius_msg
*msg
;
705 if (e
->last_recv_radius
== NULL
)
708 msg
= e
->last_recv_radius
;
710 eap
= radius_msg_get_eap(msg
);
712 /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
713 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
715 wpa_printf(MSG_DEBUG
, "could not extract "
716 "EAP-Message from RADIUS message");
717 wpabuf_free(e
->last_eap_radius
);
718 e
->last_eap_radius
= NULL
;
722 if (wpabuf_len(eap
) < sizeof(*hdr
)) {
723 wpa_printf(MSG_DEBUG
, "too short EAP packet "
724 "received from authentication server");
729 if (wpabuf_len(eap
) > sizeof(*hdr
))
730 eap_type
= (wpabuf_head_u8(eap
))[sizeof(*hdr
)];
732 hdr
= wpabuf_head(eap
);
734 case EAP_CODE_REQUEST
:
735 os_snprintf(buf
, sizeof(buf
), "EAP-Request-%s (%d)",
736 eap_type
>= 0 ? eap_type_text(eap_type
) : "??",
739 case EAP_CODE_RESPONSE
:
740 os_snprintf(buf
, sizeof(buf
), "EAP Response-%s (%d)",
741 eap_type
>= 0 ? eap_type_text(eap_type
) : "??",
744 case EAP_CODE_SUCCESS
:
745 os_strlcpy(buf
, "EAP Success", sizeof(buf
));
746 /* LEAP uses EAP Success within an authentication, so must not
747 * stop here with eloop_terminate(); */
749 case EAP_CODE_FAILURE
:
750 os_strlcpy(buf
, "EAP Failure", sizeof(buf
));
754 os_strlcpy(buf
, "unknown EAP code", sizeof(buf
));
755 wpa_hexdump_buf(MSG_DEBUG
, "Decapsulated EAP packet", eap
);
758 wpa_printf(MSG_DEBUG
, "decapsulated EAP packet (code=%d "
759 "id=%d len=%d) from RADIUS server: %s",
760 hdr
->code
, hdr
->identifier
, ntohs(hdr
->length
), buf
);
762 /* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
764 wpabuf_free(e
->last_eap_radius
);
765 e
->last_eap_radius
= eap
;
768 struct ieee802_1x_hdr
*dot1x
;
769 dot1x
= os_malloc(sizeof(*dot1x
) + wpabuf_len(eap
));
770 assert(dot1x
!= NULL
);
771 dot1x
->version
= EAPOL_VERSION
;
772 dot1x
->type
= IEEE802_1X_TYPE_EAP_PACKET
;
773 dot1x
->length
= htons(wpabuf_len(eap
));
774 os_memcpy((u8
*) (dot1x
+ 1), wpabuf_head(eap
),
776 eapol_sm_rx_eapol(e
->wpa_s
->eapol
, e
->wpa_s
->bssid
,
778 sizeof(*dot1x
) + wpabuf_len(eap
));
784 static void ieee802_1x_get_keys(struct eapol_test_data
*e
,
785 struct radius_msg
*msg
, struct radius_msg
*req
,
786 const u8
*shared_secret
,
787 size_t shared_secret_len
)
789 struct radius_ms_mppe_keys
*keys
;
793 keys
= radius_msg_get_ms_keys(msg
, req
, shared_secret
,
795 if (keys
&& keys
->send
== NULL
&& keys
->recv
== NULL
) {
797 keys
= radius_msg_get_cisco_keys(msg
, req
, shared_secret
,
803 wpa_hexdump(MSG_DEBUG
, "MS-MPPE-Send-Key (sign)",
804 keys
->send
, keys
->send_len
);
807 wpa_hexdump(MSG_DEBUG
, "MS-MPPE-Recv-Key (crypt)",
808 keys
->recv
, keys
->recv_len
);
809 e
->authenticator_pmk_len
=
810 keys
->recv_len
> PMK_LEN
? PMK_LEN
:
812 os_memcpy(e
->authenticator_pmk
, keys
->recv
,
813 e
->authenticator_pmk_len
);
814 if (e
->authenticator_pmk_len
== 16 && keys
->send
&&
815 keys
->send_len
== 16) {
816 /* MS-CHAP-v2 derives 16 octet keys */
817 wpa_printf(MSG_DEBUG
, "Use MS-MPPE-Send-Key "
818 "to extend PMK to 32 octets");
819 os_memcpy(e
->authenticator_pmk
+
820 e
->authenticator_pmk_len
,
821 keys
->send
, keys
->send_len
);
822 e
->authenticator_pmk_len
+= keys
->send_len
;
831 if (radius_msg_get_attr_ptr(msg
, RADIUS_ATTR_EAP_KEY_NAME
, &buf
, &len
,
833 os_memcpy(e
->authenticator_eap_key_name
, buf
, len
);
834 e
->authenticator_eap_key_name_len
= len
;
836 e
->authenticator_eap_key_name_len
= 0;
841 /* Process the RADIUS frames from Authentication Server */
842 static RadiusRxResult
843 ieee802_1x_receive_auth(struct radius_msg
*msg
, struct radius_msg
*req
,
844 const u8
*shared_secret
, size_t shared_secret_len
,
847 struct eapol_test_data
*e
= data
;
848 struct radius_hdr
*hdr
= radius_msg_get_hdr(msg
);
850 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
851 * present when packet contains an EAP-Message attribute */
852 if (hdr
->code
== RADIUS_CODE_ACCESS_REJECT
&&
853 radius_msg_get_attr(msg
, RADIUS_ATTR_MESSAGE_AUTHENTICATOR
, NULL
,
855 radius_msg_get_attr(msg
, RADIUS_ATTR_EAP_MESSAGE
, NULL
, 0) < 0) {
856 wpa_printf(MSG_DEBUG
, "Allowing RADIUS "
857 "Access-Reject without Message-Authenticator "
858 "since it does not include EAP-Message\n");
859 } else if (radius_msg_verify(msg
, shared_secret
, shared_secret_len
,
861 printf("Incoming RADIUS packet did not have correct "
862 "Message-Authenticator - dropped\n");
863 return RADIUS_RX_UNKNOWN
;
866 if (hdr
->code
!= RADIUS_CODE_ACCESS_ACCEPT
&&
867 hdr
->code
!= RADIUS_CODE_ACCESS_REJECT
&&
868 hdr
->code
!= RADIUS_CODE_ACCESS_CHALLENGE
) {
869 printf("Unknown RADIUS message code\n");
870 return RADIUS_RX_UNKNOWN
;
873 e
->radius_identifier
= -1;
874 wpa_printf(MSG_DEBUG
, "RADIUS packet matching with station");
876 radius_msg_free(e
->last_recv_radius
);
877 e
->last_recv_radius
= msg
;
880 case RADIUS_CODE_ACCESS_ACCEPT
:
881 e
->radius_access_accept_received
= 1;
882 ieee802_1x_get_keys(e
, msg
, req
, shared_secret
,
885 case RADIUS_CODE_ACCESS_REJECT
:
886 e
->radius_access_reject_received
= 1;
890 ieee802_1x_decapsulate_radius(e
);
892 if ((hdr
->code
== RADIUS_CODE_ACCESS_ACCEPT
&&
893 e
->eapol_test_num_reauths
< 0) ||
894 hdr
->code
== RADIUS_CODE_ACCESS_REJECT
) {
898 return RADIUS_RX_QUEUED
;
902 static void wpa_init_conf(struct eapol_test_data
*e
,
903 struct wpa_supplicant
*wpa_s
, const char *authsrv
,
904 int port
, const char *secret
,
905 const char *cli_addr
)
907 struct hostapd_radius_server
*as
;
911 os_memcpy(wpa_s
->own_addr
, e
->own_addr
, ETH_ALEN
);
912 e
->own_ip_addr
.s_addr
= htonl((127 << 24) | 1);
913 os_strlcpy(wpa_s
->ifname
, "test", sizeof(wpa_s
->ifname
));
915 e
->radius_conf
= os_zalloc(sizeof(struct hostapd_radius_servers
));
916 assert(e
->radius_conf
!= NULL
);
917 e
->radius_conf
->num_auth_servers
= 1;
918 as
= os_zalloc(sizeof(struct hostapd_radius_server
));
920 #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
924 sscanf(authsrv
, "%d.%d.%d.%d", &a
[0], &a
[1], &a
[2], &a
[3]);
925 pos
= (u8
*) &as
->addr
.u
.v4
;
931 #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
932 if (inet_aton(authsrv
, &as
->addr
.u
.v4
) < 0) {
933 wpa_printf(MSG_ERROR
, "Invalid IP address '%s'",
937 #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
938 as
->addr
.af
= AF_INET
;
940 as
->shared_secret
= (u8
*) os_strdup(secret
);
941 as
->shared_secret_len
= os_strlen(secret
);
942 e
->radius_conf
->auth_server
= as
;
943 e
->radius_conf
->auth_servers
= as
;
944 e
->radius_conf
->msg_dumps
= 1;
946 if (hostapd_parse_ip_addr(cli_addr
,
947 &e
->radius_conf
->client_addr
) == 0)
948 e
->radius_conf
->force_client_addr
= 1;
950 wpa_printf(MSG_ERROR
, "Invalid IP address '%s'",
956 e
->radius
= radius_client_init(wpa_s
, e
->radius_conf
);
957 assert(e
->radius
!= NULL
);
959 res
= radius_client_register(e
->radius
, RADIUS_AUTH
,
960 ieee802_1x_receive_auth
, e
);
965 static int scard_test(struct eapol_test_data
*e
)
967 struct scard_data
*scard
;
970 unsigned char _rand
[16];
972 unsigned char sres
[4];
974 #endif /* PCSC_FUNCS */
975 #define num_triplets 5
976 unsigned char rand_
[num_triplets
][16];
977 unsigned char sres_
[num_triplets
][4];
978 unsigned char kc_
[num_triplets
][8];
982 #define AKA_RAND_LEN 16
983 #define AKA_AUTN_LEN 16
984 #define AKA_AUTS_LEN 14
985 #define RES_MAX_LEN 16
988 unsigned char aka_rand
[AKA_RAND_LEN
];
989 unsigned char aka_autn
[AKA_AUTN_LEN
];
990 unsigned char aka_auts
[AKA_AUTS_LEN
];
991 unsigned char aka_res
[RES_MAX_LEN
];
993 unsigned char aka_ik
[IK_LEN
];
994 unsigned char aka_ck
[CK_LEN
];
996 scard
= scard_init(e
->pcsc_reader
);
999 if (scard_set_pin(scard
, e
->pcsc_pin
)) {
1000 wpa_printf(MSG_WARNING
, "PIN validation failed");
1001 scard_deinit(scard
);
1006 if (scard_get_imsi(scard
, imsi
, &len
))
1008 wpa_hexdump_ascii(MSG_DEBUG
, "SCARD: IMSI", (u8
*) imsi
, len
);
1009 /* NOTE: Permanent Username: 1 | IMSI */
1011 wpa_printf(MSG_DEBUG
, "SCARD: MNC length %d",
1012 scard_get_mnc_len(scard
));
1014 os_memset(_rand
, 0, sizeof(_rand
));
1015 if (scard_gsm_auth(scard
, _rand
, sres
, kc
))
1018 os_memset(_rand
, 0xff, sizeof(_rand
));
1019 if (scard_gsm_auth(scard
, _rand
, sres
, kc
))
1022 for (i
= 0; i
< num_triplets
; i
++) {
1023 os_memset(rand_
[i
], i
, sizeof(rand_
[i
]));
1024 if (scard_gsm_auth(scard
, rand_
[i
], sres_
[i
], kc_
[i
]))
1028 for (i
= 0; i
< num_triplets
; i
++) {
1030 for (j
= 0; j
< len
; j
++)
1031 printf("%c", imsi
[j
]);
1033 for (j
= 0; j
< 16; j
++)
1034 printf("%02X", rand_
[i
][j
]);
1036 for (j
= 0; j
< 4; j
++)
1037 printf("%02X", sres_
[i
][j
]);
1039 for (j
= 0; j
< 8; j
++)
1040 printf("%02X", kc_
[i
][j
]);
1044 wpa_printf(MSG_DEBUG
, "Trying to use UMTS authentication");
1047 os_memset(aka_rand
, 0xaa, 16);
1048 os_memcpy(aka_autn
, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
1049 "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
1051 res
= scard_umts_auth(scard
, aka_rand
, aka_autn
, aka_res
, &aka_res_len
,
1052 aka_ik
, aka_ck
, aka_auts
);
1054 wpa_printf(MSG_DEBUG
, "UMTS auth completed successfully");
1055 wpa_hexdump(MSG_DEBUG
, "RES", aka_res
, aka_res_len
);
1056 wpa_hexdump(MSG_DEBUG
, "IK", aka_ik
, IK_LEN
);
1057 wpa_hexdump(MSG_DEBUG
, "CK", aka_ck
, CK_LEN
);
1058 } else if (res
== -2) {
1059 wpa_printf(MSG_DEBUG
, "UMTS auth resulted in synchronization "
1061 wpa_hexdump(MSG_DEBUG
, "AUTS", aka_auts
, AKA_AUTS_LEN
);
1063 wpa_printf(MSG_DEBUG
, "UMTS auth failed");
1067 scard_deinit(scard
);
1074 static int scard_get_triplets(struct eapol_test_data
*e
, int argc
, char *argv
[])
1076 struct scard_data
*scard
;
1079 unsigned char _rand
[16];
1080 unsigned char sres
[4];
1081 unsigned char kc
[8];
1086 if (argc
< 2 || ((num_triplets
= atoi(argv
[1])) <= 0)) {
1087 printf("invalid parameters for sim command\n");
1091 if (argc
<= 2 || os_strcmp(argv
[2], "debug") != 0) {
1092 /* disable debug output */
1093 wpa_debug_level
= 99;
1096 scard
= scard_init(e
->pcsc_reader
);
1097 if (scard
== NULL
) {
1098 printf("Failed to open smartcard connection\n");
1101 if (scard_set_pin(scard
, argv
[0])) {
1102 wpa_printf(MSG_WARNING
, "PIN validation failed");
1103 scard_deinit(scard
);
1108 if (scard_get_imsi(scard
, imsi
, &len
)) {
1109 scard_deinit(scard
);
1113 for (i
= 0; i
< num_triplets
; i
++) {
1114 os_memset(_rand
, i
, sizeof(_rand
));
1115 if (scard_gsm_auth(scard
, _rand
, sres
, kc
))
1118 /* IMSI:Kc:SRES:RAND */
1119 for (j
= 0; j
< len
; j
++)
1120 printf("%c", imsi
[j
]);
1122 for (j
= 0; j
< 8; j
++)
1123 printf("%02X", kc
[j
]);
1125 for (j
= 0; j
< 4; j
++)
1126 printf("%02X", sres
[j
]);
1128 for (j
= 0; j
< 16; j
++)
1129 printf("%02X", _rand
[j
]);
1133 scard_deinit(scard
);
1139 static void eapol_test_terminate(int sig
, void *signal_ctx
)
1141 struct wpa_supplicant
*wpa_s
= signal_ctx
;
1142 wpa_msg(wpa_s
, MSG_INFO
, "Signal %d received - terminating", sig
);
1147 static void usage(void)
1150 "eapol_test [-enWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
1151 "[-s<AS secret>]\\\n"
1152 " [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
1153 " [-M<client MAC address>] [-o<server cert file] \\\n"
1154 " [-N<attr spec>] [-R<PC/SC reader>] "
1155 "[-P<PC/SC PIN>] \\\n"
1156 " [-A<client IP>]\n"
1157 "eapol_test scard\n"
1158 "eapol_test sim <PIN> <num triplets> [debug]\n"
1161 " -c<conf> = configuration file\n"
1162 " -a<AS IP> = IP address of the authentication server, "
1163 "default 127.0.0.1\n"
1164 " -p<AS port> = UDP port of the authentication server, "
1166 " -s<AS secret> = shared secret with the authentication "
1167 "server, default 'radius'\n"
1168 " -A<client IP> = IP address of the client, default: select "
1170 " -r<count> = number of re-authentications\n"
1171 " -e = Request EAP-Key-Name\n"
1172 " -W = wait for a control interface monitor before starting\n"
1173 " -S = save configuration after authentication\n"
1174 " -n = no MPPE keys expected\n"
1175 " -t<timeout> = sets timeout in seconds (default: 30 s)\n"
1176 " -C<Connect-Info> = RADIUS Connect-Info (default: "
1177 "CONNECT 11Mbps 802.11b)\n"
1178 " -M<client MAC address> = Set own MAC address "
1179 "(Calling-Station-Id,\n"
1180 " default: 02:00:00:00:00:01)\n"
1181 " -o<server cert file> = Write received server certificate\n"
1182 " chain to the specified file\n"
1183 " -N<attr spec> = send arbitrary attribute specified by:\n"
1184 " attr_id:syntax:value or attr_id\n"
1185 " attr_id - number id of the attribute\n"
1186 " syntax - one of: s, d, x\n"
1189 " x = octet string\n"
1190 " value - attribute value.\n"
1191 " When only attr_id is specified, NULL will be used as "
1193 " Multiple attributes can be specified by using the "
1194 "option several times.\n");
1198 int main(int argc
, char *argv
[])
1200 struct wpa_global global
;
1201 struct wpa_supplicant wpa_s
;
1202 int c
, ret
= 1, wait_for_monitor
= 0, save_config
= 0;
1203 char *as_addr
= "127.0.0.1";
1205 char *as_secret
= "radius";
1206 char *cli_addr
= NULL
;
1210 struct extra_radius_attr
*p
= NULL
, *p1
;
1212 if (os_program_init())
1215 hostapd_logger_register_cb(hostapd_logger_cb
);
1217 os_memset(&eapol_test
, 0, sizeof(eapol_test
));
1218 eapol_test
.connect_info
= "CONNECT 11Mbps 802.11b";
1219 os_memcpy(eapol_test
.own_addr
, "\x02\x00\x00\x00\x00\x01", ETH_ALEN
);
1220 eapol_test
.pcsc_pin
= "1234";
1222 wpa_debug_level
= 0;
1223 wpa_debug_show_keys
= 1;
1226 c
= getopt(argc
, argv
, "a:A:c:C:eM:nN:o:p:P:r:R:s:St:W");
1240 eapol_test
.connect_info
= optarg
;
1243 eapol_test
.req_eap_key_name
= 1;
1246 if (hwaddr_aton(optarg
, eapol_test
.own_addr
)) {
1252 eapol_test
.no_mppe_keys
++;
1255 if (eapol_test
.server_cert_file
)
1256 fclose(eapol_test
.server_cert_file
);
1257 eapol_test
.server_cert_file
= fopen(optarg
, "w");
1258 if (eapol_test
.server_cert_file
== NULL
) {
1259 printf("Could not open '%s' for writing\n",
1265 as_port
= atoi(optarg
);
1268 eapol_test
.pcsc_pin
= optarg
;
1271 eapol_test
.eapol_test_num_reauths
= atoi(optarg
);
1274 eapol_test
.pcsc_reader
= optarg
;
1283 timeout
= atoi(optarg
);
1289 p1
= os_zalloc(sizeof(*p1
));
1293 eapol_test
.extra_attrs
= p1
;
1298 p
->type
= atoi(optarg
);
1299 pos
= os_strchr(optarg
, ':');
1307 if (pos
[0] == '\0' || pos
[1] != ':') {
1308 printf("Incorrect format of attribute "
1322 if (argc
> optind
&& os_strcmp(argv
[optind
], "scard") == 0) {
1323 return scard_test(&eapol_test
);
1326 if (argc
> optind
&& os_strcmp(argv
[optind
], "sim") == 0) {
1327 return scard_get_triplets(&eapol_test
, argc
- optind
- 1,
1333 printf("Configuration file is required.\n");
1337 if (eap_register_methods()) {
1338 wpa_printf(MSG_ERROR
, "Failed to register EAP methods");
1343 wpa_printf(MSG_ERROR
, "Failed to initialize event loop");
1347 os_memset(&global
, 0, sizeof(global
));
1348 os_memset(&wpa_s
, 0, sizeof(wpa_s
));
1349 wpa_s
.global
= &global
;
1350 eapol_test
.wpa_s
= &wpa_s
;
1351 dl_list_init(&wpa_s
.bss
);
1352 dl_list_init(&wpa_s
.bss_id
);
1353 wpa_s
.conf
= wpa_config_read(conf
, NULL
);
1354 if (wpa_s
.conf
== NULL
) {
1355 printf("Failed to parse configuration file '%s'.\n", conf
);
1358 if (wpa_s
.conf
->ssid
== NULL
) {
1359 printf("No networks defined.\n");
1363 if (eapol_test
.pcsc_reader
) {
1364 os_free(wpa_s
.conf
->pcsc_reader
);
1365 wpa_s
.conf
->pcsc_reader
= os_strdup(eapol_test
.pcsc_reader
);
1368 wpa_init_conf(&eapol_test
, &wpa_s
, as_addr
, as_port
, as_secret
,
1370 wpa_s
.ctrl_iface
= wpa_supplicant_ctrl_iface_init(&wpa_s
);
1371 if (wpa_s
.ctrl_iface
== NULL
) {
1372 printf("Failed to initialize control interface '%s'.\n"
1373 "You may have another eapol_test process already "
1374 "running or the file was\n"
1375 "left by an unclean termination of eapol_test in "
1376 "which case you will need\n"
1377 "to manually remove this file before starting "
1378 "eapol_test again.\n",
1379 wpa_s
.conf
->ctrl_interface
);
1382 if (wpa_supplicant_scard_init(&wpa_s
, wpa_s
.conf
->ssid
))
1385 if (test_eapol(&eapol_test
, &wpa_s
, wpa_s
.conf
->ssid
))
1388 if (wpas_init_ext_pw(&wpa_s
) < 0)
1391 if (wait_for_monitor
)
1392 wpa_supplicant_ctrl_iface_wait(wpa_s
.ctrl_iface
);
1394 eloop_register_timeout(timeout
, 0, eapol_test_timeout
, &eapol_test
,
1396 eloop_register_timeout(0, 0, send_eap_request_identity
, &wpa_s
, NULL
);
1397 eloop_register_signal_terminate(eapol_test_terminate
, &wpa_s
);
1398 eloop_register_signal_reconfig(eapol_test_terminate
, &wpa_s
);
1401 eloop_cancel_timeout(eapol_test_timeout
, &eapol_test
, NULL
);
1402 eloop_cancel_timeout(eapol_sm_reauth
, &eapol_test
, NULL
);
1404 if (eapol_test_compare_pmk(&eapol_test
) == 0 ||
1405 eapol_test
.no_mppe_keys
)
1407 if (eapol_test
.auth_timed_out
)
1409 if (eapol_test
.radius_access_reject_received
)
1413 wpa_config_write(conf
, wpa_s
.conf
);
1415 test_eapol_clean(&eapol_test
, &wpa_s
);
1417 eap_peer_unregister_methods();
1419 eap_server_unregister_methods();
1420 #endif /* CONFIG_AP */
1424 if (eapol_test
.server_cert_file
)
1425 fclose(eapol_test
.server_cert_file
);
1427 printf("MPPE keys OK: %d mismatch: %d\n",
1428 eapol_test
.num_mppe_ok
, eapol_test
.num_mppe_mismatch
);
1429 if (eapol_test
.num_mppe_mismatch
)
1432 printf("FAILURE\n");
1434 printf("SUCCESS\n");
1436 os_program_deinit();