1 /*
2  * WPA Supplicant - test code
3  * Copyright (c) 2003-2013, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  *
8  * IEEE 802.1X Supplicant test code (to be used in place of wpa_supplicant.c.
9  * Not used in production version.
10  */
11 
12 #include "includes.h"
13 #include <assert.h>
14 
15 #include "common.h"
16 #include "utils/ext_password.h"
17 #include "common/version.h"
18 #include "crypto/tls.h"
19 #include "config.h"
20 #include "eapol_supp/eapol_supp_sm.h"
21 #include "eap_peer/eap.h"
22 #include "eap_server/eap_methods.h"
23 #include "eloop.h"
24 #include "utils/base64.h"
25 #include "rsn_supp/wpa.h"
26 #include "wpa_supplicant_i.h"
27 #include "radius/radius.h"
28 #include "radius/radius_client.h"
29 #include "common/wpa_ctrl.h"
30 #include "ctrl_iface.h"
31 #include "pcsc_funcs.h"
32 #include "wpas_glue.h"
33 
34 
35 const struct wpa_driver_ops *const wpa_drivers[] = { NULL };
36 
37 
38 struct extra_radius_attr {
39 	u8 type;
40 	char syntax;
41 	char *data;
42 	struct extra_radius_attr *next;
43 };
44 
45 struct eapol_test_data {
46 	struct wpa_supplicant *wpa_s;
47 
48 	int eapol_test_num_reauths;
49 	int no_mppe_keys;
50 	int num_mppe_ok, num_mppe_mismatch;
51 	int req_eap_key_name;
52 
53 	u8 radius_identifier;
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;
58 
59 	 /* last received EAP Response from Authentication Server */
60 	struct wpabuf *last_eap_radius;
61 
62 	u8 authenticator_pmk[PMK_LEN];
63 	size_t authenticator_pmk_len;
64 	u8 authenticator_eap_key_name[256];
65 	size_t authenticator_eap_key_name_len;
66 	int radius_access_accept_received;
67 	int radius_access_reject_received;
68 	int auth_timed_out;
69 
70 	u8 *eap_identity;
71 	size_t eap_identity_len;
72 
73 	char *connect_info;
74 	u8 own_addr[ETH_ALEN];
75 	struct extra_radius_attr *extra_attrs;
76 
77 	FILE *server_cert_file;
78 
79 	const char *pcsc_reader;
80 	const char *pcsc_pin;
81 
82 	unsigned int ctrl_iface:1;
83 	unsigned int id_req_sent:1;
84 };
85 
86 static struct eapol_test_data eapol_test;
87 
88 
89 static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx);
90 
91 
hostapd_logger_cb(void * ctx,const u8 * addr,unsigned int module,int level,const char * txt,size_t len)92 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
93 			      int level, const char *txt, size_t len)
94 {
95 	if (addr)
96 		wpa_printf(MSG_DEBUG, "STA " MACSTR ": %s\n",
97 			   MAC2STR(addr), txt);
98 	else
99 		wpa_printf(MSG_DEBUG, "%s", txt);
100 }
101 
102 
add_extra_attr(struct radius_msg * msg,struct extra_radius_attr * attr)103 static int add_extra_attr(struct radius_msg *msg,
104 			  struct extra_radius_attr *attr)
105 {
106 	size_t len;
107 	char *pos;
108 	u32 val;
109 	char buf[RADIUS_MAX_ATTR_LEN + 1];
110 
111 	switch (attr->syntax) {
112 	case 's':
113 		os_snprintf(buf, sizeof(buf), "%s", attr->data);
114 		len = os_strlen(buf);
115 		break;
116 	case 'n':
117 		buf[0] = '\0';
118 		len = 1;
119 		break;
120 	case 'x':
121 		pos = attr->data;
122 		if (pos[0] == '0' && pos[1] == 'x')
123 			pos += 2;
124 		len = os_strlen(pos);
125 		if ((len & 1) || (len / 2) > RADIUS_MAX_ATTR_LEN) {
126 			printf("Invalid extra attribute hexstring\n");
127 			return -1;
128 		}
129 		len /= 2;
130 		if (hexstr2bin(pos, (u8 *) buf, len) < 0) {
131 			printf("Invalid extra attribute hexstring\n");
132 			return -1;
133 		}
134 		break;
135 	case 'd':
136 		val = htonl(atoi(attr->data));
137 		os_memcpy(buf, &val, 4);
138 		len = 4;
139 		break;
140 	default:
141 		printf("Incorrect extra attribute syntax specification\n");
142 		return -1;
143 	}
144 
145 	if (!radius_msg_add_attr(msg, attr->type, (u8 *) buf, len)) {
146 		printf("Could not add attribute %d\n", attr->type);
147 		return -1;
148 	}
149 
150 	return 0;
151 }
152 
153 
add_extra_attrs(struct radius_msg * msg,struct extra_radius_attr * attrs)154 static int add_extra_attrs(struct radius_msg *msg,
155 			   struct extra_radius_attr *attrs)
156 {
157 	struct extra_radius_attr *p;
158 	for (p = attrs; p; p = p->next) {
159 		if (add_extra_attr(msg, p) < 0)
160 			return -1;
161 	}
162 	return 0;
163 }
164 
165 
166 static struct extra_radius_attr *
find_extra_attr(struct extra_radius_attr * attrs,u8 type)167 find_extra_attr(struct extra_radius_attr *attrs, u8 type)
168 {
169 	struct extra_radius_attr *p;
170 	for (p = attrs; p; p = p->next) {
171 		if (p->type == type)
172 			return p;
173 	}
174 	return NULL;
175 }
176 
177 
ieee802_1x_encapsulate_radius(struct eapol_test_data * e,const u8 * eap,size_t len)178 static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
179 					  const u8 *eap, size_t len)
180 {
181 	struct radius_msg *msg;
182 	char buf[RADIUS_MAX_ATTR_LEN + 1];
183 	const struct eap_hdr *hdr;
184 	const u8 *pos;
185 
186 	wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
187 		   "packet");
188 
189 	e->radius_identifier = radius_client_get_id(e->radius);
190 	msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
191 			     e->radius_identifier);
192 	if (msg == NULL) {
193 		printf("Could not create net RADIUS packet\n");
194 		return;
195 	}
196 
197 	radius_msg_make_authenticator(msg);
198 
199 	hdr = (const struct eap_hdr *) eap;
200 	pos = (const u8 *) (hdr + 1);
201 	if (len > sizeof(*hdr) && hdr->code == EAP_CODE_RESPONSE &&
202 	    pos[0] == EAP_TYPE_IDENTITY) {
203 		pos++;
204 		os_free(e->eap_identity);
205 		e->eap_identity_len = len - sizeof(*hdr) - 1;
206 		e->eap_identity = os_malloc(e->eap_identity_len);
207 		if (e->eap_identity) {
208 			os_memcpy(e->eap_identity, pos, e->eap_identity_len);
209 			wpa_hexdump(MSG_DEBUG, "Learned identity from "
210 				    "EAP-Response-Identity",
211 				    e->eap_identity, e->eap_identity_len);
212 		}
213 	}
214 
215 	if (e->eap_identity &&
216 	    !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
217 				 e->eap_identity, e->eap_identity_len)) {
218 		printf("Could not add User-Name\n");
219 		goto fail;
220 	}
221 
222 	if (e->req_eap_key_name &&
223 	    !radius_msg_add_attr(msg, RADIUS_ATTR_EAP_KEY_NAME, (u8 *) "\0",
224 				 1)) {
225 		printf("Could not add EAP-Key-Name\n");
226 		goto fail;
227 	}
228 
229 	if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_NAS_IP_ADDRESS) &&
230 	    !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
231 				 (u8 *) &e->own_ip_addr, 4)) {
232 		printf("Could not add NAS-IP-Address\n");
233 		goto fail;
234 	}
235 
236 	os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
237 		    MAC2STR(e->wpa_s->own_addr));
238 	if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_CALLING_STATION_ID)
239 	    &&
240 	    !radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
241 				 (u8 *) buf, os_strlen(buf))) {
242 		printf("Could not add Calling-Station-Id\n");
243 		goto fail;
244 	}
245 
246 	/* TODO: should probably check MTU from driver config; 2304 is max for
247 	 * IEEE 802.11, but use 1400 to avoid problems with too large packets
248 	 */
249 	if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_FRAMED_MTU) &&
250 	    !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
251 		printf("Could not add Framed-MTU\n");
252 		goto fail;
253 	}
254 
255 	if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_NAS_PORT_TYPE) &&
256 	    !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
257 				       RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
258 		printf("Could not add NAS-Port-Type\n");
259 		goto fail;
260 	}
261 
262 	if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_SERVICE_TYPE) &&
263 	    !radius_msg_add_attr_int32(msg, RADIUS_ATTR_SERVICE_TYPE,
264 				       RADIUS_SERVICE_TYPE_FRAMED)) {
265 		printf("Could not add Service-Type\n");
266 		goto fail;
267 	}
268 
269 	os_snprintf(buf, sizeof(buf), "%s", e->connect_info);
270 	if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_CONNECT_INFO) &&
271 	    !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
272 				 (u8 *) buf, os_strlen(buf))) {
273 		printf("Could not add Connect-Info\n");
274 		goto fail;
275 	}
276 
277 	if (add_extra_attrs(msg, e->extra_attrs) < 0)
278 		goto fail;
279 
280 	if (eap && !radius_msg_add_eap(msg, eap, len)) {
281 		printf("Could not add EAP-Message\n");
282 		goto fail;
283 	}
284 
285 	/* State attribute must be copied if and only if this packet is
286 	 * Access-Request reply to the previous Access-Challenge */
287 	if (e->last_recv_radius &&
288 	    radius_msg_get_hdr(e->last_recv_radius)->code ==
289 	    RADIUS_CODE_ACCESS_CHALLENGE) {
290 		int res = radius_msg_copy_attr(msg, e->last_recv_radius,
291 					       RADIUS_ATTR_STATE);
292 		if (res < 0) {
293 			printf("Could not copy State attribute from previous "
294 			       "Access-Challenge\n");
295 			goto fail;
296 		}
297 		if (res > 0) {
298 			wpa_printf(MSG_DEBUG, "  Copied RADIUS State "
299 				   "Attribute");
300 		}
301 	}
302 
303 	if (radius_client_send(e->radius, msg, RADIUS_AUTH, e->wpa_s->own_addr)
304 	    < 0)
305 		goto fail;
306 	return;
307 
308  fail:
309 	radius_msg_free(msg);
310 }
311 
312 
eapol_test_eapol_send(void * ctx,int type,const u8 * buf,size_t len)313 static int eapol_test_eapol_send(void *ctx, int type, const u8 *buf,
314 				 size_t len)
315 {
316 	printf("WPA: eapol_test_eapol_send(type=%d len=%lu)\n",
317 	       type, (unsigned long) len);
318 	if (type == IEEE802_1X_TYPE_EAP_PACKET) {
319 		wpa_hexdump(MSG_DEBUG, "TX EAP -> RADIUS", buf, len);
320 		ieee802_1x_encapsulate_radius(&eapol_test, buf, len);
321 	}
322 	return 0;
323 }
324 
325 
eapol_test_set_config_blob(void * ctx,struct wpa_config_blob * blob)326 static void eapol_test_set_config_blob(void *ctx,
327 				       struct wpa_config_blob *blob)
328 {
329 	struct eapol_test_data *e = ctx;
330 	wpa_config_set_blob(e->wpa_s->conf, blob);
331 }
332 
333 
334 static const struct wpa_config_blob *
eapol_test_get_config_blob(void * ctx,const char * name)335 eapol_test_get_config_blob(void *ctx, const char *name)
336 {
337 	struct eapol_test_data *e = ctx;
338 	return wpa_config_get_blob(e->wpa_s->conf, name);
339 }
340 
341 
eapol_test_eapol_done_cb(void * ctx)342 static void eapol_test_eapol_done_cb(void *ctx)
343 {
344 	struct eapol_test_data *e = ctx;
345 
346 	printf("WPA: EAPOL processing complete\n");
347 	wpa_supplicant_cancel_auth_timeout(e->wpa_s);
348 	wpa_supplicant_set_state(e->wpa_s, WPA_COMPLETED);
349 }
350 
351 
eapol_sm_reauth(void * eloop_ctx,void * timeout_ctx)352 static void eapol_sm_reauth(void *eloop_ctx, void *timeout_ctx)
353 {
354 	struct eapol_test_data *e = eloop_ctx;
355 	printf("\n\n\n\n\neapol_test: Triggering EAP reauthentication\n\n");
356 	e->radius_access_accept_received = 0;
357 	send_eap_request_identity(e->wpa_s, NULL);
358 }
359 
360 
eapol_test_compare_pmk(struct eapol_test_data * e)361 static int eapol_test_compare_pmk(struct eapol_test_data *e)
362 {
363 	u8 pmk[PMK_LEN];
364 	int ret = 1;
365 	const u8 *sess_id;
366 	size_t sess_id_len;
367 
368 	if (eapol_sm_get_key(e->wpa_s->eapol, pmk, PMK_LEN) == 0) {
369 		wpa_hexdump(MSG_DEBUG, "PMK from EAPOL", pmk, PMK_LEN);
370 		if (os_memcmp(pmk, e->authenticator_pmk, PMK_LEN) != 0) {
371 			printf("WARNING: PMK mismatch\n");
372 			wpa_hexdump(MSG_DEBUG, "PMK from AS",
373 				    e->authenticator_pmk, PMK_LEN);
374 		} else if (e->radius_access_accept_received)
375 			ret = 0;
376 	} else if (e->authenticator_pmk_len == 16 &&
377 		   eapol_sm_get_key(e->wpa_s->eapol, pmk, 16) == 0) {
378 		wpa_hexdump(MSG_DEBUG, "LEAP PMK from EAPOL", pmk, 16);
379 		if (os_memcmp(pmk, e->authenticator_pmk, 16) != 0) {
380 			printf("WARNING: PMK mismatch\n");
381 			wpa_hexdump(MSG_DEBUG, "PMK from AS",
382 				    e->authenticator_pmk, 16);
383 		} else if (e->radius_access_accept_received)
384 			ret = 0;
385 	} else if (e->radius_access_accept_received && e->no_mppe_keys) {
386 		/* No keying material expected */
387 		ret = 0;
388 	}
389 
390 	if (ret && !e->no_mppe_keys)
391 		e->num_mppe_mismatch++;
392 	else if (!e->no_mppe_keys)
393 		e->num_mppe_ok++;
394 
395 	sess_id = eapol_sm_get_session_id(e->wpa_s->eapol, &sess_id_len);
396 	if (!sess_id)
397 		return ret;
398 	if (e->authenticator_eap_key_name_len == 0) {
399 		wpa_printf(MSG_INFO, "No EAP-Key-Name received from server");
400 		return ret;
401 	}
402 
403 	if (e->authenticator_eap_key_name_len != sess_id_len ||
404 	    os_memcmp(e->authenticator_eap_key_name, sess_id, sess_id_len) != 0)
405 	{
406 		wpa_printf(MSG_INFO,
407 			   "Locally derived EAP Session-Id does not match EAP-Key-Name from server");
408 		wpa_hexdump(MSG_DEBUG, "EAP Session-Id", sess_id, sess_id_len);
409 		wpa_hexdump(MSG_DEBUG, "EAP-Key-Name from server",
410 			    e->authenticator_eap_key_name,
411 			    e->authenticator_eap_key_name_len);
412 	} else {
413 		wpa_printf(MSG_INFO,
414 			   "Locally derived EAP Session-Id matches EAP-Key-Name from server");
415 	}
416 
417 	return ret;
418 }
419 
420 
eapol_sm_cb(struct eapol_sm * eapol,enum eapol_supp_result result,void * ctx)421 static void eapol_sm_cb(struct eapol_sm *eapol, enum eapol_supp_result result,
422 			void *ctx)
423 {
424 	struct eapol_test_data *e = ctx;
425 	printf("eapol_sm_cb: result=%d\n", result);
426 	e->id_req_sent = 0;
427 	if (e->ctrl_iface)
428 		return;
429 	e->eapol_test_num_reauths--;
430 	if (e->eapol_test_num_reauths < 0)
431 		eloop_terminate();
432 	else {
433 		eapol_test_compare_pmk(e);
434 		eloop_register_timeout(0, 100000, eapol_sm_reauth, e, NULL);
435 	}
436 }
437 
438 
eapol_test_write_cert(FILE * f,const char * subject,const struct wpabuf * cert)439 static void eapol_test_write_cert(FILE *f, const char *subject,
440 				  const struct wpabuf *cert)
441 {
442 	char *encoded;
443 
444 	encoded = base64_encode(wpabuf_head(cert), wpabuf_len(cert), NULL);
445 	if (encoded == NULL)
446 		return;
447 	fprintf(f, "%s\n-----BEGIN CERTIFICATE-----\n%s"
448 		"-----END CERTIFICATE-----\n\n", subject, encoded);
449 	os_free(encoded);
450 }
451 
452 
453 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
eapol_test_eap_param_needed(void * ctx,enum wpa_ctrl_req_type field,const char * default_txt)454 static void eapol_test_eap_param_needed(void *ctx, enum wpa_ctrl_req_type field,
455 					const char *default_txt)
456 {
457 	struct eapol_test_data *e = ctx;
458 	struct wpa_supplicant *wpa_s = e->wpa_s;
459 	struct wpa_ssid *ssid = wpa_s->current_ssid;
460 	const char *field_name, *txt = NULL;
461 	char *buf;
462 	size_t buflen;
463 	int len;
464 
465 	if (ssid == NULL)
466 		return;
467 
468 	field_name = wpa_supplicant_ctrl_req_to_string(field, default_txt,
469 						       &txt);
470 	if (field_name == NULL) {
471 		wpa_printf(MSG_WARNING, "Unhandled EAP param %d needed",
472 			   field);
473 		return;
474 	}
475 
476 	buflen = 100 + os_strlen(txt) + ssid->ssid_len;
477 	buf = os_malloc(buflen);
478 	if (buf == NULL)
479 		return;
480 	len = os_snprintf(buf, buflen,
481 			  WPA_CTRL_REQ "%s-%d:%s needed for SSID ",
482 			  field_name, ssid->id, txt);
483 	if (os_snprintf_error(buflen, len)) {
484 		os_free(buf);
485 		return;
486 	}
487 	if (ssid->ssid && buflen > len + ssid->ssid_len) {
488 		os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
489 		len += ssid->ssid_len;
490 		buf[len] = '\0';
491 	}
492 	buf[buflen - 1] = '\0';
493 	wpa_msg(wpa_s, MSG_INFO, "%s", buf);
494 	os_free(buf);
495 }
496 #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
497 #define eapol_test_eap_param_needed NULL
498 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
499 
500 
eapol_test_cert_cb(void * ctx,struct tls_cert_data * cert,const char * cert_hash)501 static void eapol_test_cert_cb(void *ctx, struct tls_cert_data *cert,
502 			       const char *cert_hash)
503 {
504 	struct eapol_test_data *e = ctx;
505 	int i;
506 
507 	wpa_msg(e->wpa_s, MSG_INFO, WPA_EVENT_EAP_PEER_CERT
508 		"depth=%d subject='%s'%s%s",
509 		cert->depth, cert->subject,
510 		cert_hash ? " hash=" : "",
511 		cert_hash ? cert_hash : "");
512 
513 	if (cert->cert) {
514 		char *cert_hex;
515 		size_t len = wpabuf_len(cert->cert) * 2 + 1;
516 		cert_hex = os_malloc(len);
517 		if (cert_hex) {
518 			wpa_snprintf_hex(cert_hex, len, wpabuf_head(cert->cert),
519 					 wpabuf_len(cert->cert));
520 			wpa_msg_ctrl(e->wpa_s, MSG_INFO,
521 				     WPA_EVENT_EAP_PEER_CERT
522 				     "depth=%d subject='%s' cert=%s",
523 				     cert->depth, cert->subject, cert_hex);
524 			os_free(cert_hex);
525 		}
526 
527 		if (e->server_cert_file)
528 			eapol_test_write_cert(e->server_cert_file,
529 					      cert->subject, cert->cert);
530 	}
531 
532 	for (i = 0; i < cert->num_altsubject; i++)
533 		wpa_msg(e->wpa_s, MSG_INFO, WPA_EVENT_EAP_PEER_ALT
534 			"depth=%d %s", cert->depth, cert->altsubject[i]);
535 }
536 
537 
eapol_test_set_anon_id(void * ctx,const u8 * id,size_t len)538 static void eapol_test_set_anon_id(void *ctx, const u8 *id, size_t len)
539 {
540 	struct eapol_test_data *e = ctx;
541 	struct wpa_supplicant *wpa_s = e->wpa_s;
542 	char *str;
543 	int res;
544 
545 	wpa_hexdump_ascii(MSG_DEBUG, "EAP method updated anonymous_identity",
546 			  id, len);
547 
548 	if (wpa_s->current_ssid == NULL)
549 		return;
550 
551 	if (id == NULL) {
552 		if (wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
553 				   "NULL", 0) < 0)
554 			return;
555 	} else {
556 		str = os_malloc(len * 2 + 1);
557 		if (str == NULL)
558 			return;
559 		wpa_snprintf_hex(str, len * 2 + 1, id, len);
560 		res = wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
561 				     str, 0);
562 		os_free(str);
563 		if (res < 0)
564 			return;
565 	}
566 }
567 
568 
eapol_test_get_state(void * ctx)569 static enum wpa_states eapol_test_get_state(void *ctx)
570 {
571 	struct eapol_test_data *e = ctx;
572 	struct wpa_supplicant *wpa_s = e->wpa_s;
573 
574 	return wpa_s->wpa_state;
575 }
576 
577 
test_eapol(struct eapol_test_data * e,struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)578 static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
579 		      struct wpa_ssid *ssid)
580 {
581 	struct eapol_config eapol_conf;
582 	struct eapol_ctx *ctx;
583 	struct wpa_sm_ctx *wctx;
584 
585 	ctx = os_zalloc(sizeof(*ctx));
586 	if (ctx == NULL) {
587 		printf("Failed to allocate EAPOL context.\n");
588 		return -1;
589 	}
590 	ctx->ctx = e;
591 	ctx->msg_ctx = wpa_s;
592 	ctx->scard_ctx = wpa_s->scard;
593 	ctx->cb = eapol_sm_cb;
594 	ctx->cb_ctx = e;
595 	ctx->eapol_send_ctx = wpa_s;
596 	ctx->preauth = 0;
597 	ctx->eapol_done_cb = eapol_test_eapol_done_cb;
598 	ctx->eapol_send = eapol_test_eapol_send;
599 	ctx->set_config_blob = eapol_test_set_config_blob;
600 	ctx->get_config_blob = eapol_test_get_config_blob;
601 	ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
602 	ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
603 	ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
604 	ctx->openssl_ciphers = wpa_s->conf->openssl_ciphers;
605 	ctx->eap_param_needed = eapol_test_eap_param_needed;
606 	ctx->cert_cb = eapol_test_cert_cb;
607 	ctx->cert_in_cb = 1;
608 	ctx->set_anon_id = eapol_test_set_anon_id;
609 
610 	wpa_s->eapol = eapol_sm_init(ctx);
611 	if (wpa_s->eapol == NULL) {
612 		os_free(ctx);
613 		printf("Failed to initialize EAPOL state machines.\n");
614 		return -1;
615 	}
616 
617 	wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
618 	wctx = os_zalloc(sizeof(*wctx));
619 	if (wctx == NULL) {
620 		os_free(ctx);
621 		return -1;
622 	}
623 	wctx->ctx = e;
624 	wctx->msg_ctx = wpa_s;
625 	wctx->get_state = eapol_test_get_state;
626 	wpa_s->wpa = wpa_sm_init(wctx);
627 	if (!wpa_s->wpa) {
628 		os_free(ctx);
629 		os_free(wctx);
630 		return -1;
631 	}
632 
633 	if (!ssid)
634 		return 0;
635 
636 	wpa_s->current_ssid = ssid;
637 	os_memset(&eapol_conf, 0, sizeof(eapol_conf));
638 	eapol_conf.accept_802_1x_keys = 1;
639 	eapol_conf.required_keys = 0;
640 	eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
641 	eapol_conf.workaround = ssid->eap_workaround;
642 	eapol_conf.external_sim = wpa_s->conf->external_sim;
643 	eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
644 	eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
645 
646 
647 	eapol_sm_notify_portValid(wpa_s->eapol, false);
648 	/* 802.1X::portControl = Auto */
649 	eapol_sm_notify_portEnabled(wpa_s->eapol, true);
650 
651 	return 0;
652 }
653 
654 
test_eapol_clean(struct eapol_test_data * e,struct wpa_supplicant * wpa_s)655 static void test_eapol_clean(struct eapol_test_data *e,
656 			     struct wpa_supplicant *wpa_s)
657 {
658 	struct extra_radius_attr *p, *prev;
659 
660 	wpa_sm_deinit(wpa_s->wpa);
661 	wpa_s->wpa = NULL;
662 	radius_client_deinit(e->radius);
663 	wpabuf_free(e->last_eap_radius);
664 	radius_msg_free(e->last_recv_radius);
665 	e->last_recv_radius = NULL;
666 	os_free(e->eap_identity);
667 	e->eap_identity = NULL;
668 	eapol_sm_deinit(wpa_s->eapol);
669 	wpa_s->eapol = NULL;
670 	if (e->radius_conf && e->radius_conf->auth_server) {
671 		os_free(e->radius_conf->auth_server->shared_secret);
672 		os_free(e->radius_conf->auth_server);
673 	}
674 	os_free(e->radius_conf);
675 	e->radius_conf = NULL;
676 	scard_deinit(wpa_s->scard);
677 	wpa_supplicant_ctrl_iface_deinit(wpa_s, wpa_s->ctrl_iface);
678 	wpa_s->ctrl_iface = NULL;
679 
680 	ext_password_deinit(wpa_s->ext_pw);
681 	wpa_s->ext_pw = NULL;
682 
683 	wpa_config_free(wpa_s->conf);
684 
685 	p = e->extra_attrs;
686 	while (p) {
687 		prev = p;
688 		p = p->next;
689 		os_free(prev);
690 	}
691 }
692 
693 
send_eap_request_identity(void * eloop_ctx,void * timeout_ctx)694 static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx)
695 {
696 	struct wpa_supplicant *wpa_s = eloop_ctx;
697 	u8 buf[100], *pos;
698 	struct ieee802_1x_hdr *hdr;
699 	struct eap_hdr *eap;
700 
701 	hdr = (struct ieee802_1x_hdr *) buf;
702 	hdr->version = EAPOL_VERSION;
703 	hdr->type = IEEE802_1X_TYPE_EAP_PACKET;
704 	hdr->length = htons(5);
705 
706 	eap = (struct eap_hdr *) (hdr + 1);
707 	eap->code = EAP_CODE_REQUEST;
708 	if (os_get_random((u8 *) &eap->identifier, sizeof(eap->identifier)) < 0)
709 		eap->identifier = os_random() & 0xff;
710 	eap->length = htons(5);
711 	pos = (u8 *) (eap + 1);
712 	*pos = EAP_TYPE_IDENTITY;
713 
714 	printf("Sending fake EAP-Request-Identity\n");
715 	eapol_sm_rx_eapol(wpa_s->eapol, wpa_s->bssid, buf,
716 			  sizeof(*hdr) + 5);
717 }
718 
719 
eapol_test_timeout(void * eloop_ctx,void * timeout_ctx)720 static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
721 {
722 	struct eapol_test_data *e = eloop_ctx;
723 	printf("EAPOL test timed out\n");
724 	e->auth_timed_out = 1;
725 	eloop_terminate();
726 }
727 
728 
eap_type_text(u8 type)729 static char *eap_type_text(u8 type)
730 {
731 	switch (type) {
732 	case EAP_TYPE_IDENTITY: return "Identity";
733 	case EAP_TYPE_NOTIFICATION: return "Notification";
734 	case EAP_TYPE_NAK: return "Nak";
735 	case EAP_TYPE_TLS: return "TLS";
736 	case EAP_TYPE_TTLS: return "TTLS";
737 	case EAP_TYPE_PEAP: return "PEAP";
738 	case EAP_TYPE_SIM: return "SIM";
739 	case EAP_TYPE_GTC: return "GTC";
740 	case EAP_TYPE_MD5: return "MD5";
741 	case EAP_TYPE_OTP: return "OTP";
742 	case EAP_TYPE_FAST: return "FAST";
743 	case EAP_TYPE_SAKE: return "SAKE";
744 	case EAP_TYPE_PSK: return "PSK";
745 	default: return "Unknown";
746 	}
747 }
748 
749 
ieee802_1x_decapsulate_radius(struct eapol_test_data * e)750 static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
751 {
752 	struct wpabuf *eap;
753 	const struct eap_hdr *hdr;
754 	int eap_type = -1;
755 	char buf[64];
756 	struct radius_msg *msg;
757 
758 	if (e->last_recv_radius == NULL)
759 		return;
760 
761 	msg = e->last_recv_radius;
762 
763 	eap = radius_msg_get_eap(msg);
764 	if (eap == NULL) {
765 		/* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
766 		 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
767 		 * attribute */
768 		wpa_printf(MSG_DEBUG, "could not extract "
769 			       "EAP-Message from RADIUS message");
770 		wpabuf_free(e->last_eap_radius);
771 		e->last_eap_radius = NULL;
772 		return;
773 	}
774 
775 	if (wpabuf_len(eap) < sizeof(*hdr)) {
776 		wpa_printf(MSG_DEBUG, "too short EAP packet "
777 			       "received from authentication server");
778 		wpabuf_free(eap);
779 		return;
780 	}
781 
782 	if (wpabuf_len(eap) > sizeof(*hdr))
783 		eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
784 
785 	hdr = wpabuf_head(eap);
786 	switch (hdr->code) {
787 	case EAP_CODE_REQUEST:
788 		os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
789 			    eap_type >= 0 ? eap_type_text(eap_type) : "??",
790 			    eap_type);
791 		break;
792 	case EAP_CODE_RESPONSE:
793 		os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
794 			    eap_type >= 0 ? eap_type_text(eap_type) : "??",
795 			    eap_type);
796 		break;
797 	case EAP_CODE_SUCCESS:
798 		os_strlcpy(buf, "EAP Success", sizeof(buf));
799 		/* LEAP uses EAP Success within an authentication, so must not
800 		 * stop here with eloop_terminate(); */
801 		break;
802 	case EAP_CODE_FAILURE:
803 		os_strlcpy(buf, "EAP Failure", sizeof(buf));
804 		if (e->ctrl_iface)
805 			break;
806 		eloop_terminate();
807 		break;
808 	default:
809 		os_strlcpy(buf, "unknown EAP code", sizeof(buf));
810 		wpa_hexdump_buf(MSG_DEBUG, "Decapsulated EAP packet", eap);
811 		break;
812 	}
813 	wpa_printf(MSG_DEBUG, "decapsulated EAP packet (code=%d "
814 		       "id=%d len=%d) from RADIUS server: %s",
815 		      hdr->code, hdr->identifier, ntohs(hdr->length), buf);
816 
817 	/* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
818 
819 	wpabuf_free(e->last_eap_radius);
820 	e->last_eap_radius = eap;
821 
822 	{
823 		struct ieee802_1x_hdr *dot1x;
824 		dot1x = os_malloc(sizeof(*dot1x) + wpabuf_len(eap));
825 		assert(dot1x != NULL);
826 		dot1x->version = EAPOL_VERSION;
827 		dot1x->type = IEEE802_1X_TYPE_EAP_PACKET;
828 		dot1x->length = htons(wpabuf_len(eap));
829 		os_memcpy((u8 *) (dot1x + 1), wpabuf_head(eap),
830 			  wpabuf_len(eap));
831 		eapol_sm_rx_eapol(e->wpa_s->eapol, e->wpa_s->bssid,
832 				  (u8 *) dot1x,
833 				  sizeof(*dot1x) + wpabuf_len(eap));
834 		os_free(dot1x);
835 	}
836 }
837 
838 
ieee802_1x_get_keys(struct eapol_test_data * e,struct radius_msg * msg,struct radius_msg * req,const u8 * shared_secret,size_t shared_secret_len)839 static void ieee802_1x_get_keys(struct eapol_test_data *e,
840 				struct radius_msg *msg, struct radius_msg *req,
841 				const u8 *shared_secret,
842 				size_t shared_secret_len)
843 {
844 	struct radius_ms_mppe_keys *keys;
845 	u8 *buf;
846 	size_t len;
847 
848 	keys = radius_msg_get_ms_keys(msg, req, shared_secret,
849 				      shared_secret_len);
850 	if (keys && keys->send == NULL && keys->recv == NULL) {
851 		os_free(keys);
852 		keys = radius_msg_get_cisco_keys(msg, req, shared_secret,
853 						 shared_secret_len);
854 	}
855 
856 	if (keys) {
857 		if (keys->send) {
858 			wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)",
859 				    keys->send, keys->send_len);
860 		}
861 		if (keys->recv) {
862 			wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)",
863 				    keys->recv, keys->recv_len);
864 			e->authenticator_pmk_len =
865 				keys->recv_len > PMK_LEN ? PMK_LEN :
866 				keys->recv_len;
867 			os_memcpy(e->authenticator_pmk, keys->recv,
868 				  e->authenticator_pmk_len);
869 			if (e->authenticator_pmk_len == 16 && keys->send &&
870 			    keys->send_len == 16) {
871 				/* MS-CHAP-v2 derives 16 octet keys */
872 				wpa_printf(MSG_DEBUG, "Use MS-MPPE-Send-Key "
873 					   "to extend PMK to 32 octets");
874 				os_memcpy(e->authenticator_pmk +
875 					  e->authenticator_pmk_len,
876 					  keys->send, keys->send_len);
877 				e->authenticator_pmk_len += keys->send_len;
878 			}
879 		}
880 
881 		os_free(keys->send);
882 		os_free(keys->recv);
883 		os_free(keys);
884 	}
885 
886 	if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_EAP_KEY_NAME, &buf, &len,
887 				    NULL) == 0) {
888 		os_memcpy(e->authenticator_eap_key_name, buf, len);
889 		e->authenticator_eap_key_name_len = len;
890 	} else {
891 		e->authenticator_eap_key_name_len = 0;
892 	}
893 }
894 
895 
896 /* Process the RADIUS frames from Authentication Server */
897 static RadiusRxResult
ieee802_1x_receive_auth(struct radius_msg * msg,struct radius_msg * req,const u8 * shared_secret,size_t shared_secret_len,void * data)898 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
899 			const u8 *shared_secret, size_t shared_secret_len,
900 			void *data)
901 {
902 	struct eapol_test_data *e = data;
903 	struct radius_hdr *hdr = radius_msg_get_hdr(msg);
904 
905 	/* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
906 	 * present when packet contains an EAP-Message attribute */
907 	if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
908 	    radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
909 				0) < 0 &&
910 	    radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
911 		wpa_printf(MSG_DEBUG, "Allowing RADIUS "
912 			      "Access-Reject without Message-Authenticator "
913 			      "since it does not include EAP-Message\n");
914 	} else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
915 				     req, 1)) {
916 		printf("Incoming RADIUS packet did not have correct "
917 		       "Message-Authenticator - dropped\n");
918 		return RADIUS_RX_UNKNOWN;
919 	}
920 
921 	if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
922 	    hdr->code != RADIUS_CODE_ACCESS_REJECT &&
923 	    hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
924 		printf("Unknown RADIUS message code\n");
925 		return RADIUS_RX_UNKNOWN;
926 	}
927 
928 	e->radius_identifier = -1;
929 	wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
930 
931 	radius_msg_free(e->last_recv_radius);
932 	e->last_recv_radius = msg;
933 
934 	switch (hdr->code) {
935 	case RADIUS_CODE_ACCESS_ACCEPT:
936 		e->radius_access_accept_received = 1;
937 		ieee802_1x_get_keys(e, msg, req, shared_secret,
938 				    shared_secret_len);
939 		break;
940 	case RADIUS_CODE_ACCESS_REJECT:
941 		e->radius_access_reject_received = 1;
942 		break;
943 	}
944 
945 	ieee802_1x_decapsulate_radius(e);
946 
947 	if ((hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
948 	     e->eapol_test_num_reauths < 0) ||
949 	    hdr->code == RADIUS_CODE_ACCESS_REJECT) {
950 		if (!e->ctrl_iface)
951 			eloop_terminate();
952 	}
953 
954 	return RADIUS_RX_QUEUED;
955 }
956 
957 
driver_get_ssid(void * priv,u8 * ssid)958 static int driver_get_ssid(void *priv, u8 *ssid)
959 {
960 	ssid[0] = 0;
961 	return 0;
962 }
963 
964 
driver_get_bssid(void * priv,u8 * bssid)965 static int driver_get_bssid(void *priv, u8 *bssid)
966 {
967 	struct eapol_test_data *e = priv;
968 
969 	if (e->ctrl_iface && !e->id_req_sent) {
970 		eloop_register_timeout(0, 0, send_eap_request_identity,
971 				       e->wpa_s, NULL);
972 		e->id_req_sent = 1;
973 	}
974 
975 	os_memset(bssid, 0, ETH_ALEN);
976 	bssid[5] = 1;
977 	return 0;
978 }
979 
980 
driver_get_capa(void * priv,struct wpa_driver_capa * capa)981 static int driver_get_capa(void *priv, struct wpa_driver_capa *capa)
982 {
983 	os_memset(capa, 0, sizeof(*capa));
984 	capa->flags = WPA_DRIVER_FLAGS_WIRED;
985 	return 0;
986 }
987 
988 
989 struct wpa_driver_ops eapol_test_drv_ops = {
990 	.name = "test",
991 	.get_ssid = driver_get_ssid,
992 	.get_bssid = driver_get_bssid,
993 	.get_capa = driver_get_capa,
994 };
995 
wpa_init_conf(struct eapol_test_data * e,struct wpa_supplicant * wpa_s,const char * authsrv,int port,const char * secret,const char * cli_addr,const char * ifname)996 static void wpa_init_conf(struct eapol_test_data *e,
997 			  struct wpa_supplicant *wpa_s, const char *authsrv,
998 			  int port, const char *secret,
999 			  const char *cli_addr, const char *ifname)
1000 {
1001 	struct hostapd_radius_server *as;
1002 	int res;
1003 
1004 	wpa_s->driver = &eapol_test_drv_ops;
1005 	wpa_s->drv_priv = e;
1006 	wpa_s->bssid[5] = 1;
1007 	os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN);
1008 	e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
1009 	os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
1010 
1011 	e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers));
1012 	assert(e->radius_conf != NULL);
1013 	e->radius_conf->num_auth_servers = 1;
1014 	as = os_zalloc(sizeof(struct hostapd_radius_server));
1015 	assert(as != NULL);
1016 #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
1017 	{
1018 		int a[4];
1019 		u8 *pos;
1020 		sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
1021 		pos = (u8 *) &as->addr.u.v4;
1022 		*pos++ = a[0];
1023 		*pos++ = a[1];
1024 		*pos++ = a[2];
1025 		*pos++ = a[3];
1026 		as->addr.af = AF_INET;
1027 	}
1028 #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
1029 	if (hostapd_parse_ip_addr(authsrv, &as->addr) < 0) {
1030 		wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
1031 			   authsrv);
1032 		assert(0);
1033 	}
1034 #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
1035 	as->port = port;
1036 	as->shared_secret = (u8 *) os_strdup(secret);
1037 	as->shared_secret_len = os_strlen(secret);
1038 	e->radius_conf->auth_server = as;
1039 	e->radius_conf->auth_servers = as;
1040 	e->radius_conf->msg_dumps = 1;
1041 	if (cli_addr) {
1042 		if (hostapd_parse_ip_addr(cli_addr,
1043 					  &e->radius_conf->client_addr) == 0)
1044 			e->radius_conf->force_client_addr = 1;
1045 		else {
1046 			wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
1047 				   cli_addr);
1048 			assert(0);
1049 		}
1050 	}
1051 
1052 	e->radius = radius_client_init(wpa_s, e->radius_conf);
1053 	assert(e->radius != NULL);
1054 
1055 	res = radius_client_register(e->radius, RADIUS_AUTH,
1056 				     ieee802_1x_receive_auth, e);
1057 	assert(res == 0);
1058 }
1059 
1060 
scard_test(struct eapol_test_data * e)1061 static int scard_test(struct eapol_test_data *e)
1062 {
1063 	struct scard_data *scard;
1064 	size_t len;
1065 	char imsi[20];
1066 	unsigned char _rand[16];
1067 #ifdef PCSC_FUNCS
1068 	unsigned char sres[4];
1069 	unsigned char kc[8];
1070 #endif /* PCSC_FUNCS */
1071 #define num_triplets 5
1072 	unsigned char rand_[num_triplets][16];
1073 	unsigned char sres_[num_triplets][4];
1074 	unsigned char kc_[num_triplets][8];
1075 	int i, res;
1076 	size_t j;
1077 
1078 #define AKA_RAND_LEN 16
1079 #define AKA_AUTN_LEN 16
1080 #define AKA_AUTS_LEN 14
1081 #define RES_MAX_LEN 16
1082 #define IK_LEN 16
1083 #define CK_LEN 16
1084 	unsigned char aka_rand[AKA_RAND_LEN];
1085 	unsigned char aka_autn[AKA_AUTN_LEN];
1086 	unsigned char aka_auts[AKA_AUTS_LEN];
1087 	unsigned char aka_res[RES_MAX_LEN];
1088 	size_t aka_res_len;
1089 	unsigned char aka_ik[IK_LEN];
1090 	unsigned char aka_ck[CK_LEN];
1091 
1092 	scard = scard_init(e->pcsc_reader);
1093 	if (scard == NULL)
1094 		return -1;
1095 	if (scard_set_pin(scard, e->pcsc_pin)) {
1096 		wpa_printf(MSG_WARNING, "PIN validation failed");
1097 		scard_deinit(scard);
1098 		return -1;
1099 	}
1100 
1101 	len = sizeof(imsi);
1102 	if (scard_get_imsi(scard, imsi, &len))
1103 		goto failed;
1104 	wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
1105 	/* NOTE: Permanent Username: 1 | IMSI */
1106 
1107 	wpa_printf(MSG_DEBUG, "SCARD: MNC length %d",
1108 		   scard_get_mnc_len(scard));
1109 
1110 	os_memset(_rand, 0, sizeof(_rand));
1111 	if (scard_gsm_auth(scard, _rand, sres, kc))
1112 		goto failed;
1113 
1114 	os_memset(_rand, 0xff, sizeof(_rand));
1115 	if (scard_gsm_auth(scard, _rand, sres, kc))
1116 		goto failed;
1117 
1118 	for (i = 0; i < num_triplets; i++) {
1119 		os_memset(rand_[i], i, sizeof(rand_[i]));
1120 		if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
1121 			goto failed;
1122 	}
1123 
1124 	for (i = 0; i < num_triplets; i++) {
1125 		printf("1");
1126 		for (j = 0; j < len; j++)
1127 			printf("%c", imsi[j]);
1128 		printf(",");
1129 		for (j = 0; j < 16; j++)
1130 			printf("%02X", rand_[i][j]);
1131 		printf(",");
1132 		for (j = 0; j < 4; j++)
1133 			printf("%02X", sres_[i][j]);
1134 		printf(",");
1135 		for (j = 0; j < 8; j++)
1136 			printf("%02X", kc_[i][j]);
1137 		printf("\n");
1138 	}
1139 
1140 	wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
1141 
1142 	/* seq 39 (0x28) */
1143 	os_memset(aka_rand, 0xaa, 16);
1144 	os_memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
1145 		  "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
1146 
1147 	res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
1148 			      aka_ik, aka_ck, aka_auts);
1149 	if (res == 0) {
1150 		wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
1151 		wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
1152 		wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
1153 		wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
1154 	} else if (res == -2) {
1155 		wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
1156 			   "failure");
1157 		wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
1158 	} else {
1159 		wpa_printf(MSG_DEBUG, "UMTS auth failed");
1160 	}
1161 
1162 failed:
1163 	scard_deinit(scard);
1164 
1165 	return 0;
1166 #undef num_triplets
1167 }
1168 
1169 
scard_get_triplets(struct eapol_test_data * e,int argc,char * argv[])1170 static int scard_get_triplets(struct eapol_test_data *e, int argc, char *argv[])
1171 {
1172 	struct scard_data *scard;
1173 	size_t len;
1174 	char imsi[20];
1175 	unsigned char _rand[16];
1176 	unsigned char sres[4];
1177 	unsigned char kc[8];
1178 	int num_triplets;
1179 	int i;
1180 	size_t j;
1181 
1182 	if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
1183 		printf("invalid parameters for sim command\n");
1184 		return -1;
1185 	}
1186 
1187 	if (argc <= 2 || os_strcmp(argv[2], "debug") != 0) {
1188 		/* disable debug output */
1189 		wpa_debug_level = 99;
1190 	}
1191 
1192 	scard = scard_init(e->pcsc_reader);
1193 	if (scard == NULL) {
1194 		printf("Failed to open smartcard connection\n");
1195 		return -1;
1196 	}
1197 	if (scard_set_pin(scard, argv[0])) {
1198 		wpa_printf(MSG_WARNING, "PIN validation failed");
1199 		scard_deinit(scard);
1200 		return -1;
1201 	}
1202 
1203 	len = sizeof(imsi);
1204 	if (scard_get_imsi(scard, imsi, &len)) {
1205 		scard_deinit(scard);
1206 		return -1;
1207 	}
1208 
1209 	for (i = 0; i < num_triplets; i++) {
1210 		os_memset(_rand, i, sizeof(_rand));
1211 		if (scard_gsm_auth(scard, _rand, sres, kc))
1212 			break;
1213 
1214 		/* IMSI:Kc:SRES:RAND */
1215 		for (j = 0; j < len; j++)
1216 			printf("%c", imsi[j]);
1217 		printf(":");
1218 		for (j = 0; j < 8; j++)
1219 			printf("%02X", kc[j]);
1220 		printf(":");
1221 		for (j = 0; j < 4; j++)
1222 			printf("%02X", sres[j]);
1223 		printf(":");
1224 		for (j = 0; j < 16; j++)
1225 			printf("%02X", _rand[j]);
1226 		printf("\n");
1227 	}
1228 
1229 	scard_deinit(scard);
1230 
1231 	return 0;
1232 }
1233 
1234 
eapol_test_terminate(int sig,void * signal_ctx)1235 static void eapol_test_terminate(int sig, void *signal_ctx)
1236 {
1237 	struct wpa_supplicant *wpa_s = signal_ctx;
1238 	wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
1239 	eloop_terminate();
1240 }
1241 
1242 
usage(void)1243 static void usage(void)
1244 {
1245 	printf("usage:\n"
1246 	       "eapol_test [-enWSv] -c<conf> [-a<AS IP>] [-p<AS port>] "
1247 	       "[-s<AS secret>]\\\n"
1248 	       "           [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
1249 	       "           [-M<client MAC address>] [-o<server cert file] \\\n"
1250 	       "           [-N<attr spec>] [-R<PC/SC reader>] "
1251 	       "[-P<PC/SC PIN>] \\\n"
1252 	       "           [-A<client IP>] [-i<ifname>] [-T<ctrl_iface>]\n"
1253 	       "eapol_test scard\n"
1254 	       "eapol_test sim <PIN> <num triplets> [debug]\n"
1255 	       "\n");
1256 	printf("options:\n"
1257 	       "  -c<conf> = configuration file\n"
1258 	       "  -a<AS IP> = IP address of the authentication server, "
1259 	       "default 127.0.0.1\n"
1260 	       "  -p<AS port> = UDP port of the authentication server, "
1261 	       "default 1812\n"
1262 	       "  -s<AS secret> = shared secret with the authentication "
1263 	       "server, default 'radius'\n"
1264 	       "  -A<client IP> = IP address of the client, default: select "
1265 	       "automatically\n"
1266 	       "  -r<count> = number of re-authentications\n"
1267 	       "  -e = Request EAP-Key-Name\n"
1268 	       "  -W = wait for a control interface monitor before starting\n"
1269 	       "  -S = save configuration after authentication\n"
1270 	       "  -n = no MPPE keys expected\n"
1271 	       "  -v = show version\n"
1272 	       "  -t<timeout> = sets timeout in seconds (default: 30 s)\n"
1273 	       "  -C<Connect-Info> = RADIUS Connect-Info (default: "
1274 	       "CONNECT 11Mbps 802.11b)\n"
1275 	       "  -M<client MAC address> = Set own MAC address "
1276 	       "(Calling-Station-Id,\n"
1277 	       "                           default: 02:00:00:00:00:01)\n"
1278 	       "  -o<server cert file> = Write received server certificate\n"
1279 	       "                         chain to the specified file\n"
1280 	       "  -N<attr spec> = send arbitrary attribute specified by:\n"
1281 	       "                  attr_id:syntax:value or attr_id\n"
1282 	       "                  attr_id - number id of the attribute\n"
1283 	       "                  syntax - one of: s, d, x\n"
1284 	       "                     s = string\n"
1285 	       "                     d = integer\n"
1286 	       "                     x = octet string\n"
1287 	       "                  value - attribute value.\n"
1288 	       "       When only attr_id is specified, NULL will be used as "
1289 	       "value.\n"
1290 	       "       Multiple attributes can be specified by using the "
1291 	       "option several times.\n");
1292 }
1293 
1294 
main(int argc,char * argv[])1295 int main(int argc, char *argv[])
1296 {
1297 	struct wpa_global global;
1298 	struct wpa_supplicant wpa_s;
1299 	int c, ret = 1, wait_for_monitor = 0, save_config = 0;
1300 	char *as_addr = "127.0.0.1";
1301 	int as_port = 1812;
1302 	char *as_secret = "radius";
1303 	char *cli_addr = NULL;
1304 	char *conf = NULL;
1305 	int timeout = 30;
1306 	char *pos;
1307 	struct extra_radius_attr *p = NULL, *p1;
1308 	const char *ifname = "test";
1309 	const char *ctrl_iface = NULL;
1310 
1311 	if (os_program_init())
1312 		return -1;
1313 
1314 	hostapd_logger_register_cb(hostapd_logger_cb);
1315 
1316 	os_memset(&eapol_test, 0, sizeof(eapol_test));
1317 	eapol_test.connect_info = "CONNECT 11Mbps 802.11b";
1318 	os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN);
1319 	eapol_test.pcsc_pin = "1234";
1320 
1321 	wpa_debug_level = 0;
1322 	wpa_debug_show_keys = 1;
1323 
1324 	for (;;) {
1325 		c = getopt(argc, argv, "a:A:c:C:ei:M:nN:o:p:P:r:R:s:St:T:vW");
1326 		if (c < 0)
1327 			break;
1328 		switch (c) {
1329 		case 'a':
1330 			as_addr = optarg;
1331 			break;
1332 		case 'A':
1333 			cli_addr = optarg;
1334 			break;
1335 		case 'c':
1336 			conf = optarg;
1337 			break;
1338 		case 'C':
1339 			eapol_test.connect_info = optarg;
1340 			break;
1341 		case 'e':
1342 			eapol_test.req_eap_key_name = 1;
1343 			break;
1344 		case 'i':
1345 			ifname = optarg;
1346 			break;
1347 		case 'M':
1348 			if (hwaddr_aton(optarg, eapol_test.own_addr)) {
1349 				usage();
1350 				return -1;
1351 			}
1352 			break;
1353 		case 'n':
1354 			eapol_test.no_mppe_keys++;
1355 			break;
1356 		case 'o':
1357 			if (eapol_test.server_cert_file)
1358 				fclose(eapol_test.server_cert_file);
1359 			eapol_test.server_cert_file = fopen(optarg, "w");
1360 			if (eapol_test.server_cert_file == NULL) {
1361 				printf("Could not open '%s' for writing\n",
1362 				       optarg);
1363 				return -1;
1364 			}
1365 			break;
1366 		case 'p':
1367 			as_port = atoi(optarg);
1368 			break;
1369 		case 'P':
1370 			eapol_test.pcsc_pin = optarg;
1371 			break;
1372 		case 'r':
1373 			eapol_test.eapol_test_num_reauths = atoi(optarg);
1374 			break;
1375 		case 'R':
1376 			eapol_test.pcsc_reader = optarg;
1377 			break;
1378 		case 's':
1379 			as_secret = optarg;
1380 			break;
1381 		case 'S':
1382 			save_config++;
1383 			break;
1384 		case 't':
1385 			timeout = atoi(optarg);
1386 			break;
1387 		case 'T':
1388 			ctrl_iface = optarg;
1389 			eapol_test.ctrl_iface = 1;
1390 			break;
1391 		case 'v':
1392 			printf("eapol_test v%s\n", VERSION_STR);
1393 			return 0;
1394 		case 'W':
1395 			wait_for_monitor++;
1396 			break;
1397 		case 'N':
1398 			p1 = os_zalloc(sizeof(*p1));
1399 			if (p1 == NULL)
1400 				break;
1401 			if (!p)
1402 				eapol_test.extra_attrs = p1;
1403 			else
1404 				p->next = p1;
1405 			p = p1;
1406 
1407 			p->type = atoi(optarg);
1408 			pos = os_strchr(optarg, ':');
1409 			if (pos == NULL) {
1410 				p->syntax = 'n';
1411 				p->data = NULL;
1412 				break;
1413 			}
1414 
1415 			pos++;
1416 			if (pos[0] == '\0' || pos[1] != ':') {
1417 				printf("Incorrect format of attribute "
1418 				       "specification\n");
1419 				break;
1420 			}
1421 
1422 			p->syntax = pos[0];
1423 			p->data = pos + 2;
1424 			break;
1425 		default:
1426 			usage();
1427 			return -1;
1428 		}
1429 	}
1430 
1431 	if (argc > optind && os_strcmp(argv[optind], "scard") == 0) {
1432 		return scard_test(&eapol_test);
1433 	}
1434 
1435 	if (argc > optind && os_strcmp(argv[optind], "sim") == 0) {
1436 		return scard_get_triplets(&eapol_test, argc - optind - 1,
1437 					  &argv[optind + 1]);
1438 	}
1439 
1440 	if (conf == NULL && !ctrl_iface) {
1441 		usage();
1442 		printf("Configuration file is required.\n");
1443 		return -1;
1444 	}
1445 
1446 	if (eap_register_methods()) {
1447 		wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1448 		return -1;
1449 	}
1450 
1451 	if (eloop_init()) {
1452 		wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1453 		return -1;
1454 	}
1455 
1456 	os_memset(&global, 0, sizeof(global));
1457 	os_memset(&wpa_s, 0, sizeof(wpa_s));
1458 	wpa_s.global = &global;
1459 	eapol_test.wpa_s = &wpa_s;
1460 	dl_list_init(&wpa_s.bss);
1461 	dl_list_init(&wpa_s.bss_id);
1462 	if (conf)
1463 		wpa_s.conf = wpa_config_read(conf, NULL);
1464 	else
1465 		wpa_s.conf = wpa_config_alloc_empty(ctrl_iface, NULL);
1466 	if (wpa_s.conf == NULL) {
1467 		printf("Failed to parse configuration file '%s'.\n", conf);
1468 		return -1;
1469 	}
1470 	if (!ctrl_iface && wpa_s.conf->ssid == NULL) {
1471 		printf("No networks defined.\n");
1472 		return -1;
1473 	}
1474 
1475 	if (eapol_test.pcsc_reader) {
1476 		os_free(wpa_s.conf->pcsc_reader);
1477 		wpa_s.conf->pcsc_reader = os_strdup(eapol_test.pcsc_reader);
1478 	}
1479 
1480 	wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret,
1481 		      cli_addr, ifname);
1482 	wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
1483 	if (wpa_s.ctrl_iface == NULL) {
1484 		printf("Failed to initialize control interface '%s'.\n"
1485 		       "You may have another eapol_test process already "
1486 		       "running or the file was\n"
1487 		       "left by an unclean termination of eapol_test in "
1488 		       "which case you will need\n"
1489 		       "to manually remove this file before starting "
1490 		       "eapol_test again.\n",
1491 		       wpa_s.conf->ctrl_interface);
1492 		return -1;
1493 	}
1494 	if (wpa_s.conf->ssid &&
1495 	    wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
1496 		return -1;
1497 
1498 	if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
1499 		return -1;
1500 
1501 	if (wpas_init_ext_pw(&wpa_s) < 0)
1502 		return -1;
1503 
1504 	if (wait_for_monitor)
1505 		wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface);
1506 
1507 	if (!ctrl_iface) {
1508 		eloop_register_timeout(timeout, 0, eapol_test_timeout,
1509 				       &eapol_test, NULL);
1510 		eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s,
1511 				       NULL);
1512 	}
1513 	eloop_register_signal_terminate(eapol_test_terminate, &wpa_s);
1514 	eloop_register_signal_reconfig(eapol_test_terminate, &wpa_s);
1515 	eloop_run();
1516 
1517 	eloop_cancel_timeout(eapol_test_timeout, &eapol_test, NULL);
1518 	eloop_cancel_timeout(eapol_sm_reauth, &eapol_test, NULL);
1519 
1520 	if (eapol_test_compare_pmk(&eapol_test) == 0 ||
1521 	    eapol_test.no_mppe_keys)
1522 		ret = 0;
1523 	if (eapol_test.auth_timed_out)
1524 		ret = -2;
1525 	if (eapol_test.radius_access_reject_received)
1526 		ret = -3;
1527 
1528 	if (save_config)
1529 		wpa_config_write(conf, wpa_s.conf);
1530 
1531 	test_eapol_clean(&eapol_test, &wpa_s);
1532 
1533 	eap_peer_unregister_methods();
1534 #ifdef CONFIG_AP
1535 	eap_server_unregister_methods();
1536 #endif /* CONFIG_AP */
1537 
1538 	eloop_destroy();
1539 
1540 	if (eapol_test.server_cert_file)
1541 		fclose(eapol_test.server_cert_file);
1542 
1543 	printf("MPPE keys OK: %d  mismatch: %d\n",
1544 	       eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
1545 	if (eapol_test.num_mppe_mismatch)
1546 		ret = -4;
1547 	if (ret)
1548 		printf("FAILURE\n");
1549 	else
1550 		printf("SUCCESS\n");
1551 
1552 	os_program_deinit();
1553 
1554 	return ret;
1555 }
1556