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