• 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, FRAME_ENCRYPTION_UNKNOWN);
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 				  FRAME_ENCRYPTION_UNKNOWN);
847 		os_free(dot1x);
848 	}
849 }
850 
851 
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)852 static void ieee802_1x_get_keys(struct eapol_test_data *e,
853 				struct radius_msg *msg, struct radius_msg *req,
854 				const u8 *shared_secret,
855 				size_t shared_secret_len)
856 {
857 	struct radius_ms_mppe_keys *keys;
858 	u8 *buf;
859 	size_t len;
860 
861 	keys = radius_msg_get_ms_keys(msg, req, shared_secret,
862 				      shared_secret_len);
863 	if (keys && !keys->send && !keys->recv) {
864 		os_free(keys);
865 		keys = radius_msg_get_cisco_keys(msg, req, shared_secret,
866 						 shared_secret_len);
867 	}
868 
869 	if (keys) {
870 		if (keys->send) {
871 			wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)",
872 				    keys->send, keys->send_len);
873 		}
874 		if (keys->recv) {
875 			wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)",
876 				    keys->recv, keys->recv_len);
877 			e->authenticator_pmk_len =
878 				keys->recv_len > PMK_LEN ? PMK_LEN :
879 				keys->recv_len;
880 			os_memcpy(e->authenticator_pmk, keys->recv,
881 				  e->authenticator_pmk_len);
882 			if (e->authenticator_pmk_len == 16 && keys->send &&
883 			    keys->send_len == 16) {
884 				/* MS-CHAP-v2 derives 16 octet keys */
885 				wpa_printf(MSG_DEBUG, "Use MS-MPPE-Send-Key "
886 					   "to extend PMK to 32 octets");
887 				os_memcpy(e->authenticator_pmk +
888 					  e->authenticator_pmk_len,
889 					  keys->send, keys->send_len);
890 				e->authenticator_pmk_len += keys->send_len;
891 			}
892 		}
893 
894 		os_free(keys->send);
895 		os_free(keys->recv);
896 		os_free(keys);
897 	}
898 
899 	if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_EAP_KEY_NAME, &buf, &len,
900 				    NULL) == 0) {
901 		os_memcpy(e->authenticator_eap_key_name, buf, len);
902 		e->authenticator_eap_key_name_len = len;
903 	} else {
904 		e->authenticator_eap_key_name_len = 0;
905 	}
906 }
907 
908 
909 /* Process the RADIUS frames from Authentication Server */
910 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)911 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
912 			const u8 *shared_secret, size_t shared_secret_len,
913 			void *data)
914 {
915 	struct eapol_test_data *e = data;
916 	struct radius_hdr *hdr = radius_msg_get_hdr(msg);
917 
918 	/* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
919 	 * present when packet contains an EAP-Message attribute */
920 	if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
921 	    radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
922 				0) < 0 &&
923 	    radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
924 		wpa_printf(MSG_DEBUG,
925 			   "Allowing RADIUS Access-Reject without Message-Authenticator since it does not include EAP-Message");
926 	} else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
927 				     req, 1)) {
928 		wpa_printf(MSG_INFO,
929 			   "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
930 		return RADIUS_RX_INVALID_AUTHENTICATOR;
931 	}
932 
933 	if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
934 	    hdr->code != RADIUS_CODE_ACCESS_REJECT &&
935 	    hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
936 		wpa_printf(MSG_INFO, "Unknown RADIUS message code");
937 		return RADIUS_RX_UNKNOWN;
938 	}
939 
940 	e->radius_identifier = -1;
941 	wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
942 
943 	radius_msg_free(e->last_recv_radius);
944 	e->last_recv_radius = msg;
945 
946 	switch (hdr->code) {
947 	case RADIUS_CODE_ACCESS_ACCEPT:
948 		e->radius_access_accept_received = 1;
949 		ieee802_1x_get_keys(e, msg, req, shared_secret,
950 				    shared_secret_len);
951 		break;
952 	case RADIUS_CODE_ACCESS_REJECT:
953 		e->radius_access_reject_received = 1;
954 		break;
955 	}
956 
957 	ieee802_1x_decapsulate_radius(e);
958 
959 	if ((hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
960 	     e->eapol_test_num_reauths < 0) ||
961 	    hdr->code == RADIUS_CODE_ACCESS_REJECT) {
962 		if (!e->ctrl_iface)
963 			eloop_terminate();
964 	}
965 
966 	return RADIUS_RX_QUEUED;
967 }
968 
969 
driver_get_ssid(void * priv,u8 * ssid)970 static int driver_get_ssid(void *priv, u8 *ssid)
971 {
972 	ssid[0] = 0;
973 	return 0;
974 }
975 
976 
driver_get_bssid(void * priv,u8 * bssid)977 static int driver_get_bssid(void *priv, u8 *bssid)
978 {
979 	struct eapol_test_data *e = priv;
980 
981 	if (e->ctrl_iface && !e->id_req_sent) {
982 		eloop_register_timeout(0, 0, send_eap_request_identity,
983 				       e->wpa_s, NULL);
984 		e->id_req_sent = 1;
985 	}
986 
987 	os_memset(bssid, 0, ETH_ALEN);
988 	bssid[5] = 1;
989 	return 0;
990 }
991 
992 
driver_get_capa(void * priv,struct wpa_driver_capa * capa)993 static int driver_get_capa(void *priv, struct wpa_driver_capa *capa)
994 {
995 	os_memset(capa, 0, sizeof(*capa));
996 	capa->flags = WPA_DRIVER_FLAGS_WIRED;
997 	return 0;
998 }
999 
1000 
1001 struct wpa_driver_ops eapol_test_drv_ops = {
1002 	.name = "test",
1003 	.get_ssid = driver_get_ssid,
1004 	.get_bssid = driver_get_bssid,
1005 	.get_capa = driver_get_capa,
1006 };
1007 
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)1008 static void wpa_init_conf(struct eapol_test_data *e,
1009 			  struct wpa_supplicant *wpa_s, const char *authsrv,
1010 			  int port, const char *secret,
1011 			  const char *cli_addr, const char *ifname)
1012 {
1013 	struct hostapd_radius_server *as;
1014 	int res;
1015 
1016 	wpa_s->driver = &eapol_test_drv_ops;
1017 	wpa_s->drv_priv = e;
1018 	wpa_s->bssid[5] = 1;
1019 	os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN);
1020 	e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
1021 	os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
1022 
1023 	e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers));
1024 	assert(e->radius_conf != NULL);
1025 	e->radius_conf->num_auth_servers = 1;
1026 	as = os_zalloc(sizeof(struct hostapd_radius_server));
1027 	assert(as != NULL);
1028 #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
1029 	{
1030 		int a[4];
1031 		u8 *pos;
1032 		sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
1033 		pos = (u8 *) &as->addr.u.v4;
1034 		*pos++ = a[0];
1035 		*pos++ = a[1];
1036 		*pos++ = a[2];
1037 		*pos++ = a[3];
1038 		as->addr.af = AF_INET;
1039 	}
1040 #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
1041 	if (hostapd_parse_ip_addr(authsrv, &as->addr) < 0) {
1042 		wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
1043 			   authsrv);
1044 		assert(0);
1045 	}
1046 #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
1047 	as->port = port;
1048 	as->shared_secret = (u8 *) os_strdup(secret);
1049 	as->shared_secret_len = os_strlen(secret);
1050 	e->radius_conf->auth_server = as;
1051 	e->radius_conf->auth_servers = as;
1052 	e->radius_conf->msg_dumps = 1;
1053 	if (cli_addr) {
1054 		if (hostapd_parse_ip_addr(cli_addr,
1055 					  &e->radius_conf->client_addr) == 0)
1056 			e->radius_conf->force_client_addr = 1;
1057 		else {
1058 			wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
1059 				   cli_addr);
1060 			assert(0);
1061 		}
1062 	}
1063 
1064 	e->radius = radius_client_init(wpa_s, e->radius_conf);
1065 	assert(e->radius != NULL);
1066 
1067 	res = radius_client_register(e->radius, RADIUS_AUTH,
1068 				     ieee802_1x_receive_auth, e);
1069 	assert(res == 0);
1070 }
1071 
1072 
scard_test(struct eapol_test_data * e)1073 static int scard_test(struct eapol_test_data *e)
1074 {
1075 	struct scard_data *scard;
1076 	size_t len;
1077 	char imsi[20];
1078 	unsigned char _rand[16];
1079 #ifdef PCSC_FUNCS
1080 	unsigned char sres[4];
1081 	unsigned char kc[8];
1082 #endif /* PCSC_FUNCS */
1083 #define num_triplets 5
1084 	unsigned char rand_[num_triplets][16];
1085 	unsigned char sres_[num_triplets][4];
1086 	unsigned char kc_[num_triplets][8];
1087 	int i, res;
1088 	size_t j;
1089 
1090 #define AKA_RAND_LEN 16
1091 #define AKA_AUTN_LEN 16
1092 #define AKA_AUTS_LEN 14
1093 #define RES_MAX_LEN 16
1094 #define IK_LEN 16
1095 #define CK_LEN 16
1096 	unsigned char aka_rand[AKA_RAND_LEN];
1097 	unsigned char aka_autn[AKA_AUTN_LEN];
1098 	unsigned char aka_auts[AKA_AUTS_LEN];
1099 	unsigned char aka_res[RES_MAX_LEN];
1100 	size_t aka_res_len;
1101 	unsigned char aka_ik[IK_LEN];
1102 	unsigned char aka_ck[CK_LEN];
1103 
1104 	scard = scard_init(e->pcsc_reader);
1105 	if (scard == NULL)
1106 		return -1;
1107 	if (scard_set_pin(scard, e->pcsc_pin)) {
1108 		wpa_printf(MSG_WARNING, "PIN validation failed");
1109 		scard_deinit(scard);
1110 		return -1;
1111 	}
1112 
1113 	len = sizeof(imsi);
1114 	if (scard_get_imsi(scard, imsi, &len))
1115 		goto failed;
1116 	wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
1117 	/* NOTE: Permanent Username: 1 | IMSI */
1118 
1119 	wpa_printf(MSG_DEBUG, "SCARD: MNC length %d",
1120 		   scard_get_mnc_len(scard));
1121 
1122 	os_memset(_rand, 0, sizeof(_rand));
1123 	if (scard_gsm_auth(scard, _rand, sres, kc))
1124 		goto failed;
1125 
1126 	os_memset(_rand, 0xff, sizeof(_rand));
1127 	if (scard_gsm_auth(scard, _rand, sres, kc))
1128 		goto failed;
1129 
1130 	for (i = 0; i < num_triplets; i++) {
1131 		os_memset(rand_[i], i, sizeof(rand_[i]));
1132 		if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
1133 			goto failed;
1134 	}
1135 
1136 	for (i = 0; i < num_triplets; i++) {
1137 		printf("1");
1138 		for (j = 0; j < len; j++)
1139 			printf("%c", imsi[j]);
1140 		printf(",");
1141 		for (j = 0; j < 16; j++)
1142 			printf("%02X", rand_[i][j]);
1143 		printf(",");
1144 		for (j = 0; j < 4; j++)
1145 			printf("%02X", sres_[i][j]);
1146 		printf(",");
1147 		for (j = 0; j < 8; j++)
1148 			printf("%02X", kc_[i][j]);
1149 		printf("\n");
1150 	}
1151 
1152 	wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
1153 
1154 	/* seq 39 (0x28) */
1155 	os_memset(aka_rand, 0xaa, 16);
1156 	os_memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
1157 		  "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
1158 
1159 	res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
1160 			      aka_ik, aka_ck, aka_auts);
1161 	if (res == 0) {
1162 		wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
1163 		wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
1164 		wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
1165 		wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
1166 	} else if (res == -2) {
1167 		wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
1168 			   "failure");
1169 		wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
1170 	} else {
1171 		wpa_printf(MSG_DEBUG, "UMTS auth failed");
1172 	}
1173 
1174 failed:
1175 	scard_deinit(scard);
1176 
1177 	return 0;
1178 #undef num_triplets
1179 }
1180 
1181 
scard_get_triplets(struct eapol_test_data * e,int argc,char * argv[])1182 static int scard_get_triplets(struct eapol_test_data *e, int argc, char *argv[])
1183 {
1184 	struct scard_data *scard;
1185 	size_t len;
1186 	char imsi[20];
1187 	unsigned char _rand[16];
1188 	unsigned char sres[4];
1189 	unsigned char kc[8];
1190 	int num_triplets;
1191 	int i;
1192 	size_t j;
1193 
1194 	if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
1195 		printf("invalid parameters for sim command\n");
1196 		return -1;
1197 	}
1198 
1199 	if (argc <= 2 || os_strcmp(argv[2], "debug") != 0) {
1200 		/* disable debug output */
1201 		wpa_debug_level = 99;
1202 	}
1203 
1204 	scard = scard_init(e->pcsc_reader);
1205 	if (scard == NULL) {
1206 		printf("Failed to open smartcard connection\n");
1207 		return -1;
1208 	}
1209 	if (scard_set_pin(scard, argv[0])) {
1210 		wpa_printf(MSG_WARNING, "PIN validation failed");
1211 		scard_deinit(scard);
1212 		return -1;
1213 	}
1214 
1215 	len = sizeof(imsi);
1216 	if (scard_get_imsi(scard, imsi, &len)) {
1217 		scard_deinit(scard);
1218 		return -1;
1219 	}
1220 
1221 	for (i = 0; i < num_triplets; i++) {
1222 		os_memset(_rand, i, sizeof(_rand));
1223 		if (scard_gsm_auth(scard, _rand, sres, kc))
1224 			break;
1225 
1226 		/* IMSI:Kc:SRES:RAND */
1227 		for (j = 0; j < len; j++)
1228 			printf("%c", imsi[j]);
1229 		printf(":");
1230 		for (j = 0; j < 8; j++)
1231 			printf("%02X", kc[j]);
1232 		printf(":");
1233 		for (j = 0; j < 4; j++)
1234 			printf("%02X", sres[j]);
1235 		printf(":");
1236 		for (j = 0; j < 16; j++)
1237 			printf("%02X", _rand[j]);
1238 		printf("\n");
1239 	}
1240 
1241 	scard_deinit(scard);
1242 
1243 	return 0;
1244 }
1245 
1246 
eapol_test_terminate(int sig,void * signal_ctx)1247 static void eapol_test_terminate(int sig, void *signal_ctx)
1248 {
1249 	struct wpa_supplicant *wpa_s = signal_ctx;
1250 	wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
1251 	eloop_terminate();
1252 }
1253 
1254 
usage(void)1255 static void usage(void)
1256 {
1257 	printf("usage:\n"
1258 	       "eapol_test [-enWSv] -c<conf> [-a<AS IP>] [-p<AS port>] "
1259 	       "[-s<AS secret>]\\\n"
1260 	       "           [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
1261 	       "           [-M<client MAC address>] [-o<server cert file] \\\n"
1262 	       "           [-N<attr spec>] [-R<PC/SC reader>] "
1263 	       "[-P<PC/SC PIN>] \\\n"
1264 	       "           [-A<client IP>] [-i<ifname>] [-T<ctrl_iface>]\n"
1265 	       "eapol_test scard\n"
1266 	       "eapol_test sim <PIN> <num triplets> [debug]\n"
1267 	       "\n");
1268 	printf("options:\n"
1269 	       "  -c<conf> = configuration file\n"
1270 	       "  -a<AS IP> = IP address of the authentication server, "
1271 	       "default 127.0.0.1\n"
1272 	       "  -p<AS port> = UDP port of the authentication server, "
1273 	       "default 1812\n"
1274 	       "  -s<AS secret> = shared secret with the authentication "
1275 	       "server, default 'radius'\n"
1276 	       "  -A<client IP> = IP address of the client, default: select "
1277 	       "automatically\n"
1278 	       "  -r<count> = number of re-authentications\n"
1279 	       "  -e = Request EAP-Key-Name\n"
1280 	       "  -W = wait for a control interface monitor before starting\n"
1281 	       "  -S = save configuration after authentication\n"
1282 	       "  -n = no MPPE keys expected\n"
1283 	       "  -v = show version\n"
1284 	       "  -t<timeout> = sets timeout in seconds (default: 30 s)\n"
1285 	       "  -C<Connect-Info> = RADIUS Connect-Info (default: "
1286 	       "CONNECT 11Mbps 802.11b)\n"
1287 	       "  -M<client MAC address> = Set own MAC address "
1288 	       "(Calling-Station-Id,\n"
1289 	       "                           default: 02:00:00:00:00:01)\n"
1290 	       "  -o<server cert file> = Write received server certificate\n"
1291 	       "                         chain to the specified file\n"
1292 	       "  -N<attr spec> = send arbitrary attribute specified by:\n"
1293 	       "                  attr_id:syntax:value or attr_id\n"
1294 	       "                  attr_id - number id of the attribute\n"
1295 	       "                  syntax - one of: s, d, x\n"
1296 	       "                     s = string\n"
1297 	       "                     d = integer\n"
1298 	       "                     x = octet string\n"
1299 	       "                  value - attribute value.\n"
1300 	       "       When only attr_id is specified, NULL will be used as "
1301 	       "value.\n"
1302 	       "       Multiple attributes can be specified by using the "
1303 	       "option several times.\n");
1304 }
1305 
1306 
main(int argc,char * argv[])1307 int main(int argc, char *argv[])
1308 {
1309 	struct wpa_global global;
1310 	struct wpa_supplicant wpa_s;
1311 	int c, ret = 1, wait_for_monitor = 0, save_config = 0;
1312 	char *as_addr = "127.0.0.1";
1313 	int as_port = 1812;
1314 	char *as_secret = "radius";
1315 	char *cli_addr = NULL;
1316 	char *conf = NULL;
1317 	int timeout = 30;
1318 	char *pos;
1319 	struct extra_radius_attr *p = NULL, *p1;
1320 	const char *ifname = "test";
1321 	const char *ctrl_iface = NULL;
1322 
1323 	if (os_program_init())
1324 		return -1;
1325 
1326 	hostapd_logger_register_cb(hostapd_logger_cb);
1327 
1328 	os_memset(&eapol_test, 0, sizeof(eapol_test));
1329 	eapol_test.connect_info = "CONNECT 11Mbps 802.11b";
1330 	os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN);
1331 	eapol_test.pcsc_pin = "1234";
1332 
1333 	wpa_debug_level = 0;
1334 	wpa_debug_show_keys = 1;
1335 
1336 	for (;;) {
1337 		c = getopt(argc, argv, "a:A:c:C:ei:M:nN:o:p:P:r:R:s:St:T:vW");
1338 		if (c < 0)
1339 			break;
1340 		switch (c) {
1341 		case 'a':
1342 			as_addr = optarg;
1343 			break;
1344 		case 'A':
1345 			cli_addr = optarg;
1346 			break;
1347 		case 'c':
1348 			conf = optarg;
1349 			break;
1350 		case 'C':
1351 			eapol_test.connect_info = optarg;
1352 			break;
1353 		case 'e':
1354 			eapol_test.req_eap_key_name = 1;
1355 			break;
1356 		case 'i':
1357 			ifname = optarg;
1358 			break;
1359 		case 'M':
1360 			if (hwaddr_aton(optarg, eapol_test.own_addr)) {
1361 				usage();
1362 				return -1;
1363 			}
1364 			break;
1365 		case 'n':
1366 			eapol_test.no_mppe_keys++;
1367 			break;
1368 		case 'o':
1369 			if (eapol_test.server_cert_file)
1370 				fclose(eapol_test.server_cert_file);
1371 			eapol_test.server_cert_file = fopen(optarg, "w");
1372 			if (eapol_test.server_cert_file == NULL) {
1373 				printf("Could not open '%s' for writing\n",
1374 				       optarg);
1375 				return -1;
1376 			}
1377 			break;
1378 		case 'p':
1379 			as_port = atoi(optarg);
1380 			break;
1381 		case 'P':
1382 			eapol_test.pcsc_pin = optarg;
1383 			break;
1384 		case 'r':
1385 			eapol_test.eapol_test_num_reauths = atoi(optarg);
1386 			break;
1387 		case 'R':
1388 			eapol_test.pcsc_reader = optarg;
1389 			break;
1390 		case 's':
1391 			as_secret = optarg;
1392 			break;
1393 		case 'S':
1394 			save_config++;
1395 			break;
1396 		case 't':
1397 			timeout = atoi(optarg);
1398 			break;
1399 		case 'T':
1400 			ctrl_iface = optarg;
1401 			eapol_test.ctrl_iface = 1;
1402 			break;
1403 		case 'v':
1404 			printf("eapol_test v%s\n", VERSION_STR);
1405 			return 0;
1406 		case 'W':
1407 			wait_for_monitor++;
1408 			break;
1409 		case 'N':
1410 			p1 = os_zalloc(sizeof(*p1));
1411 			if (p1 == NULL)
1412 				break;
1413 			if (!p)
1414 				eapol_test.extra_attrs = p1;
1415 			else
1416 				p->next = p1;
1417 			p = p1;
1418 
1419 			p->type = atoi(optarg);
1420 			pos = os_strchr(optarg, ':');
1421 			if (pos == NULL) {
1422 				p->syntax = 'n';
1423 				p->data = NULL;
1424 				break;
1425 			}
1426 
1427 			pos++;
1428 			if (pos[0] == '\0' || pos[1] != ':') {
1429 				printf("Incorrect format of attribute "
1430 				       "specification\n");
1431 				break;
1432 			}
1433 
1434 			p->syntax = pos[0];
1435 			p->data = pos + 2;
1436 			break;
1437 		default:
1438 			usage();
1439 			return -1;
1440 		}
1441 	}
1442 
1443 	if (argc > optind && os_strcmp(argv[optind], "scard") == 0) {
1444 		return scard_test(&eapol_test);
1445 	}
1446 
1447 	if (argc > optind && os_strcmp(argv[optind], "sim") == 0) {
1448 		return scard_get_triplets(&eapol_test, argc - optind - 1,
1449 					  &argv[optind + 1]);
1450 	}
1451 
1452 	if (conf == NULL && !ctrl_iface) {
1453 		usage();
1454 		printf("Configuration file is required.\n");
1455 		return -1;
1456 	}
1457 
1458 	if (eap_register_methods()) {
1459 		wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1460 		return -1;
1461 	}
1462 
1463 	if (eloop_init()) {
1464 		wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1465 		return -1;
1466 	}
1467 
1468 	os_memset(&global, 0, sizeof(global));
1469 	os_memset(&wpa_s, 0, sizeof(wpa_s));
1470 	wpa_s.global = &global;
1471 	eapol_test.wpa_s = &wpa_s;
1472 	dl_list_init(&wpa_s.bss);
1473 	dl_list_init(&wpa_s.bss_id);
1474 	if (conf)
1475 		wpa_s.conf = wpa_config_read(conf, NULL, false);
1476 	else
1477 		wpa_s.conf = wpa_config_alloc_empty(ctrl_iface, NULL);
1478 	if (wpa_s.conf == NULL) {
1479 		printf("Failed to parse configuration file '%s'.\n", conf);
1480 		return -1;
1481 	}
1482 	if (!ctrl_iface && wpa_s.conf->ssid == NULL) {
1483 		printf("No networks defined.\n");
1484 		return -1;
1485 	}
1486 
1487 	if (eapol_test.pcsc_reader) {
1488 		os_free(wpa_s.conf->pcsc_reader);
1489 		wpa_s.conf->pcsc_reader = os_strdup(eapol_test.pcsc_reader);
1490 	}
1491 
1492 	wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret,
1493 		      cli_addr, ifname);
1494 	wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
1495 	if (wpa_s.ctrl_iface == NULL) {
1496 		printf("Failed to initialize control interface '%s'.\n"
1497 		       "You may have another eapol_test process already "
1498 		       "running or the file was\n"
1499 		       "left by an unclean termination of eapol_test in "
1500 		       "which case you will need\n"
1501 		       "to manually remove this file before starting "
1502 		       "eapol_test again.\n",
1503 		       wpa_s.conf->ctrl_interface);
1504 		return -1;
1505 	}
1506 	if (wpa_s.conf->ssid &&
1507 	    wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
1508 		return -1;
1509 
1510 	if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
1511 		return -1;
1512 
1513 	if (wpas_init_ext_pw(&wpa_s) < 0)
1514 		return -1;
1515 
1516 	if (wait_for_monitor)
1517 		wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface);
1518 
1519 	if (!ctrl_iface) {
1520 		eloop_register_timeout(timeout, 0, eapol_test_timeout,
1521 				       &eapol_test, NULL);
1522 		eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s,
1523 				       NULL);
1524 	}
1525 	eloop_register_signal_terminate(eapol_test_terminate, &wpa_s);
1526 	eloop_register_signal_reconfig(eapol_test_terminate, &wpa_s);
1527 	eloop_run();
1528 
1529 	eloop_cancel_timeout(eapol_test_timeout, &eapol_test, NULL);
1530 	eloop_cancel_timeout(eapol_sm_reauth, &eapol_test, NULL);
1531 
1532 	if (eapol_test_compare_pmk(&eapol_test) == 0 ||
1533 	    eapol_test.no_mppe_keys)
1534 		ret = 0;
1535 	if (eapol_test.auth_timed_out)
1536 		ret = -2;
1537 	if (eapol_test.radius_access_reject_received)
1538 		ret = -3;
1539 
1540 	if (save_config)
1541 		wpa_config_write(conf, wpa_s.conf);
1542 
1543 	test_eapol_clean(&eapol_test, &wpa_s);
1544 
1545 	eap_peer_unregister_methods();
1546 #ifdef CONFIG_AP
1547 	eap_server_unregister_methods();
1548 #endif /* CONFIG_AP */
1549 
1550 	eloop_destroy();
1551 
1552 	if (eapol_test.server_cert_file)
1553 		fclose(eapol_test.server_cert_file);
1554 
1555 	printf("MPPE keys OK: %d  mismatch: %d\n",
1556 	       eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
1557 	if (eapol_test.num_mppe_mismatch)
1558 		ret = -4;
1559 	if (ret)
1560 		printf("FAILURE\n");
1561 	else
1562 		printf("SUCCESS\n");
1563 
1564 	crypto_unload();
1565 	os_program_deinit();
1566 
1567 	return ret;
1568 }
1569