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