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