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