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