1 /* 2 * hostapd - IEEE 802.11r - Fast BSS Transition 3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * Alternatively, this software may be distributed under the terms of BSD 10 * license. 11 * 12 * See README and COPYING for more details. 13 */ 14 15 #include "utils/includes.h" 16 17 #include "utils/common.h" 18 #include "common/ieee802_11_defs.h" 19 #include "common/ieee802_11_common.h" 20 #include "crypto/aes_wrap.h" 21 #include "crypto/random.h" 22 #include "ap_config.h" 23 #include "ieee802_11.h" 24 #include "wmm.h" 25 #include "wpa_auth.h" 26 #include "wpa_auth_i.h" 27 #include "wpa_auth_ie.h" 28 29 30 #ifdef CONFIG_IEEE80211R 31 32 static int wpa_ft_rrb_send(struct wpa_authenticator *wpa_auth, const u8 *dst, 33 const u8 *data, size_t data_len) 34 { 35 if (wpa_auth->cb.send_ether == NULL) 36 return -1; 37 wpa_printf(MSG_DEBUG, "FT: RRB send to " MACSTR, MAC2STR(dst)); 38 return wpa_auth->cb.send_ether(wpa_auth->cb.ctx, dst, ETH_P_RRB, 39 data, data_len); 40 } 41 42 43 static int wpa_ft_action_send(struct wpa_authenticator *wpa_auth, 44 const u8 *dst, const u8 *data, size_t data_len) 45 { 46 if (wpa_auth->cb.send_ft_action == NULL) 47 return -1; 48 return wpa_auth->cb.send_ft_action(wpa_auth->cb.ctx, dst, 49 data, data_len); 50 } 51 52 53 static struct wpa_state_machine * 54 wpa_ft_add_sta(struct wpa_authenticator *wpa_auth, const u8 *sta_addr) 55 { 56 if (wpa_auth->cb.add_sta == NULL) 57 return NULL; 58 return wpa_auth->cb.add_sta(wpa_auth->cb.ctx, sta_addr); 59 } 60 61 62 int wpa_write_mdie(struct wpa_auth_config *conf, u8 *buf, size_t len) 63 { 64 u8 *pos = buf; 65 u8 capab; 66 if (len < 2 + sizeof(struct rsn_mdie)) 67 return -1; 68 69 *pos++ = WLAN_EID_MOBILITY_DOMAIN; 70 *pos++ = MOBILITY_DOMAIN_ID_LEN + 1; 71 os_memcpy(pos, conf->mobility_domain, MOBILITY_DOMAIN_ID_LEN); 72 pos += MOBILITY_DOMAIN_ID_LEN; 73 capab = 0; 74 if (conf->ft_over_ds) 75 capab |= RSN_FT_CAPAB_FT_OVER_DS; 76 *pos++ = capab; 77 78 return pos - buf; 79 } 80 81 82 int wpa_write_ftie(struct wpa_auth_config *conf, const u8 *r0kh_id, 83 size_t r0kh_id_len, 84 const u8 *anonce, const u8 *snonce, 85 u8 *buf, size_t len, const u8 *subelem, 86 size_t subelem_len) 87 { 88 u8 *pos = buf, *ielen; 89 struct rsn_ftie *hdr; 90 91 if (len < 2 + sizeof(*hdr) + 2 + FT_R1KH_ID_LEN + 2 + r0kh_id_len + 92 subelem_len) 93 return -1; 94 95 *pos++ = WLAN_EID_FAST_BSS_TRANSITION; 96 ielen = pos++; 97 98 hdr = (struct rsn_ftie *) pos; 99 os_memset(hdr, 0, sizeof(*hdr)); 100 pos += sizeof(*hdr); 101 WPA_PUT_LE16(hdr->mic_control, 0); 102 if (anonce) 103 os_memcpy(hdr->anonce, anonce, WPA_NONCE_LEN); 104 if (snonce) 105 os_memcpy(hdr->snonce, snonce, WPA_NONCE_LEN); 106 107 /* Optional Parameters */ 108 *pos++ = FTIE_SUBELEM_R1KH_ID; 109 *pos++ = FT_R1KH_ID_LEN; 110 os_memcpy(pos, conf->r1_key_holder, FT_R1KH_ID_LEN); 111 pos += FT_R1KH_ID_LEN; 112 113 if (r0kh_id) { 114 *pos++ = FTIE_SUBELEM_R0KH_ID; 115 *pos++ = r0kh_id_len; 116 os_memcpy(pos, r0kh_id, r0kh_id_len); 117 pos += r0kh_id_len; 118 } 119 120 if (subelem) { 121 os_memcpy(pos, subelem, subelem_len); 122 pos += subelem_len; 123 } 124 125 *ielen = pos - buf - 2; 126 127 return pos - buf; 128 } 129 130 131 struct wpa_ft_pmk_r0_sa { 132 struct wpa_ft_pmk_r0_sa *next; 133 u8 pmk_r0[PMK_LEN]; 134 u8 pmk_r0_name[WPA_PMK_NAME_LEN]; 135 u8 spa[ETH_ALEN]; 136 int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */ 137 /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */ 138 int pmk_r1_pushed; 139 }; 140 141 struct wpa_ft_pmk_r1_sa { 142 struct wpa_ft_pmk_r1_sa *next; 143 u8 pmk_r1[PMK_LEN]; 144 u8 pmk_r1_name[WPA_PMK_NAME_LEN]; 145 u8 spa[ETH_ALEN]; 146 int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */ 147 /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */ 148 }; 149 150 struct wpa_ft_pmk_cache { 151 struct wpa_ft_pmk_r0_sa *pmk_r0; 152 struct wpa_ft_pmk_r1_sa *pmk_r1; 153 }; 154 155 struct wpa_ft_pmk_cache * wpa_ft_pmk_cache_init(void) 156 { 157 struct wpa_ft_pmk_cache *cache; 158 159 cache = os_zalloc(sizeof(*cache)); 160 161 return cache; 162 } 163 164 165 void wpa_ft_pmk_cache_deinit(struct wpa_ft_pmk_cache *cache) 166 { 167 struct wpa_ft_pmk_r0_sa *r0, *r0prev; 168 struct wpa_ft_pmk_r1_sa *r1, *r1prev; 169 170 r0 = cache->pmk_r0; 171 while (r0) { 172 r0prev = r0; 173 r0 = r0->next; 174 os_memset(r0prev->pmk_r0, 0, PMK_LEN); 175 os_free(r0prev); 176 } 177 178 r1 = cache->pmk_r1; 179 while (r1) { 180 r1prev = r1; 181 r1 = r1->next; 182 os_memset(r1prev->pmk_r1, 0, PMK_LEN); 183 os_free(r1prev); 184 } 185 186 os_free(cache); 187 } 188 189 190 static int wpa_ft_store_pmk_r0(struct wpa_authenticator *wpa_auth, 191 const u8 *spa, const u8 *pmk_r0, 192 const u8 *pmk_r0_name, int pairwise) 193 { 194 struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache; 195 struct wpa_ft_pmk_r0_sa *r0; 196 197 /* TODO: add expiration and limit on number of entries in cache */ 198 199 r0 = os_zalloc(sizeof(*r0)); 200 if (r0 == NULL) 201 return -1; 202 203 os_memcpy(r0->pmk_r0, pmk_r0, PMK_LEN); 204 os_memcpy(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN); 205 os_memcpy(r0->spa, spa, ETH_ALEN); 206 r0->pairwise = pairwise; 207 208 r0->next = cache->pmk_r0; 209 cache->pmk_r0 = r0; 210 211 return 0; 212 } 213 214 215 static int wpa_ft_fetch_pmk_r0(struct wpa_authenticator *wpa_auth, 216 const u8 *spa, const u8 *pmk_r0_name, 217 u8 *pmk_r0, int *pairwise) 218 { 219 struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache; 220 struct wpa_ft_pmk_r0_sa *r0; 221 222 r0 = cache->pmk_r0; 223 while (r0) { 224 if (os_memcmp(r0->spa, spa, ETH_ALEN) == 0 && 225 os_memcmp(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN) 226 == 0) { 227 os_memcpy(pmk_r0, r0->pmk_r0, PMK_LEN); 228 if (pairwise) 229 *pairwise = r0->pairwise; 230 return 0; 231 } 232 233 r0 = r0->next; 234 } 235 236 return -1; 237 } 238 239 240 static int wpa_ft_store_pmk_r1(struct wpa_authenticator *wpa_auth, 241 const u8 *spa, const u8 *pmk_r1, 242 const u8 *pmk_r1_name, int pairwise) 243 { 244 struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache; 245 struct wpa_ft_pmk_r1_sa *r1; 246 247 /* TODO: add expiration and limit on number of entries in cache */ 248 249 r1 = os_zalloc(sizeof(*r1)); 250 if (r1 == NULL) 251 return -1; 252 253 os_memcpy(r1->pmk_r1, pmk_r1, PMK_LEN); 254 os_memcpy(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN); 255 os_memcpy(r1->spa, spa, ETH_ALEN); 256 r1->pairwise = pairwise; 257 258 r1->next = cache->pmk_r1; 259 cache->pmk_r1 = r1; 260 261 return 0; 262 } 263 264 265 static int wpa_ft_fetch_pmk_r1(struct wpa_authenticator *wpa_auth, 266 const u8 *spa, const u8 *pmk_r1_name, 267 u8 *pmk_r1, int *pairwise) 268 { 269 struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache; 270 struct wpa_ft_pmk_r1_sa *r1; 271 272 r1 = cache->pmk_r1; 273 while (r1) { 274 if (os_memcmp(r1->spa, spa, ETH_ALEN) == 0 && 275 os_memcmp(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN) 276 == 0) { 277 os_memcpy(pmk_r1, r1->pmk_r1, PMK_LEN); 278 if (pairwise) 279 *pairwise = r1->pairwise; 280 return 0; 281 } 282 283 r1 = r1->next; 284 } 285 286 return -1; 287 } 288 289 290 static int wpa_ft_pull_pmk_r1(struct wpa_authenticator *wpa_auth, 291 const u8 *s1kh_id, const u8 *r0kh_id, 292 size_t r0kh_id_len, const u8 *pmk_r0_name) 293 { 294 struct ft_remote_r0kh *r0kh; 295 struct ft_r0kh_r1kh_pull_frame frame, f; 296 297 r0kh = wpa_auth->conf.r0kh_list; 298 while (r0kh) { 299 if (r0kh->id_len == r0kh_id_len && 300 os_memcmp(r0kh->id, r0kh_id, r0kh_id_len) == 0) 301 break; 302 r0kh = r0kh->next; 303 } 304 if (r0kh == NULL) 305 return -1; 306 307 wpa_printf(MSG_DEBUG, "FT: Send PMK-R1 pull request to remote R0KH " 308 "address " MACSTR, MAC2STR(r0kh->addr)); 309 310 os_memset(&frame, 0, sizeof(frame)); 311 frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB; 312 frame.packet_type = FT_PACKET_R0KH_R1KH_PULL; 313 frame.data_length = host_to_le16(FT_R0KH_R1KH_PULL_DATA_LEN); 314 os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN); 315 316 /* aes_wrap() does not support inplace encryption, so use a temporary 317 * buffer for the data. */ 318 if (random_get_bytes(f.nonce, sizeof(f.nonce))) { 319 wpa_printf(MSG_DEBUG, "FT: Failed to get random data for " 320 "nonce"); 321 return -1; 322 } 323 os_memcpy(f.pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN); 324 os_memcpy(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN); 325 os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN); 326 327 if (aes_wrap(r0kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8, 328 f.nonce, frame.nonce) < 0) 329 return -1; 330 331 wpa_ft_rrb_send(wpa_auth, r0kh->addr, (u8 *) &frame, sizeof(frame)); 332 333 return 0; 334 } 335 336 337 int wpa_auth_derive_ptk_ft(struct wpa_state_machine *sm, const u8 *pmk, 338 struct wpa_ptk *ptk, size_t ptk_len) 339 { 340 u8 pmk_r0[PMK_LEN], pmk_r0_name[WPA_PMK_NAME_LEN]; 341 u8 pmk_r1[PMK_LEN]; 342 u8 ptk_name[WPA_PMK_NAME_LEN]; 343 const u8 *mdid = sm->wpa_auth->conf.mobility_domain; 344 const u8 *r0kh = sm->wpa_auth->conf.r0_key_holder; 345 size_t r0kh_len = sm->wpa_auth->conf.r0_key_holder_len; 346 const u8 *r1kh = sm->wpa_auth->conf.r1_key_holder; 347 const u8 *ssid = sm->wpa_auth->conf.ssid; 348 size_t ssid_len = sm->wpa_auth->conf.ssid_len; 349 350 351 if (sm->xxkey_len == 0) { 352 wpa_printf(MSG_DEBUG, "FT: XXKey not available for key " 353 "derivation"); 354 return -1; 355 } 356 357 wpa_derive_pmk_r0(sm->xxkey, sm->xxkey_len, ssid, ssid_len, mdid, 358 r0kh, r0kh_len, sm->addr, pmk_r0, pmk_r0_name); 359 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", pmk_r0, PMK_LEN); 360 wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name", pmk_r0_name, WPA_PMK_NAME_LEN); 361 wpa_ft_store_pmk_r0(sm->wpa_auth, sm->addr, pmk_r0, pmk_r0_name, 362 sm->pairwise); 363 364 wpa_derive_pmk_r1(pmk_r0, pmk_r0_name, r1kh, sm->addr, 365 pmk_r1, sm->pmk_r1_name); 366 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", pmk_r1, PMK_LEN); 367 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", sm->pmk_r1_name, 368 WPA_PMK_NAME_LEN); 369 wpa_ft_store_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1, sm->pmk_r1_name, 370 sm->pairwise); 371 372 wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr, 373 sm->wpa_auth->addr, sm->pmk_r1_name, 374 (u8 *) ptk, ptk_len, ptk_name); 375 wpa_hexdump_key(MSG_DEBUG, "FT: PTK", (u8 *) ptk, ptk_len); 376 wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN); 377 378 return 0; 379 } 380 381 382 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth, 383 const u8 *addr, int idx, u8 *seq) 384 { 385 if (wpa_auth->cb.get_seqnum == NULL) 386 return -1; 387 return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq); 388 } 389 390 391 static u8 * wpa_ft_gtk_subelem(struct wpa_state_machine *sm, size_t *len) 392 { 393 u8 *subelem; 394 struct wpa_group *gsm = sm->group; 395 size_t subelem_len, pad_len; 396 const u8 *key; 397 size_t key_len; 398 u8 keybuf[32]; 399 400 key_len = gsm->GTK_len; 401 if (key_len > sizeof(keybuf)) 402 return NULL; 403 404 /* 405 * Pad key for AES Key Wrap if it is not multiple of 8 bytes or is less 406 * than 16 bytes. 407 */ 408 pad_len = key_len % 8; 409 if (pad_len) 410 pad_len = 8 - pad_len; 411 if (key_len + pad_len < 16) 412 pad_len += 8; 413 if (pad_len) { 414 os_memcpy(keybuf, gsm->GTK[gsm->GN - 1], key_len); 415 os_memset(keybuf + key_len, 0, pad_len); 416 keybuf[key_len] = 0xdd; 417 key_len += pad_len; 418 key = keybuf; 419 } else 420 key = gsm->GTK[gsm->GN - 1]; 421 422 /* 423 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] | 424 * Key[5..32]. 425 */ 426 subelem_len = 13 + key_len + 8; 427 subelem = os_zalloc(subelem_len); 428 if (subelem == NULL) 429 return NULL; 430 431 subelem[0] = FTIE_SUBELEM_GTK; 432 subelem[1] = 11 + key_len + 8; 433 /* Key ID in B0-B1 of Key Info */ 434 WPA_PUT_LE16(&subelem[2], gsm->GN & 0x03); 435 subelem[4] = gsm->GTK_len; 436 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, subelem + 5); 437 if (aes_wrap(sm->PTK.kek, key_len / 8, key, subelem + 13)) { 438 os_free(subelem); 439 return NULL; 440 } 441 442 *len = subelem_len; 443 return subelem; 444 } 445 446 447 #ifdef CONFIG_IEEE80211W 448 static u8 * wpa_ft_igtk_subelem(struct wpa_state_machine *sm, size_t *len) 449 { 450 u8 *subelem, *pos; 451 struct wpa_group *gsm = sm->group; 452 size_t subelem_len; 453 454 /* Sub-elem ID[1] | Length[1] | KeyID[2] | IPN[6] | Key Length[1] | 455 * Key[16+8] */ 456 subelem_len = 1 + 1 + 2 + 6 + 1 + WPA_IGTK_LEN + 8; 457 subelem = os_zalloc(subelem_len); 458 if (subelem == NULL) 459 return NULL; 460 461 pos = subelem; 462 *pos++ = FTIE_SUBELEM_IGTK; 463 *pos++ = subelem_len - 2; 464 WPA_PUT_LE16(pos, gsm->GN_igtk); 465 pos += 2; 466 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos); 467 pos += 6; 468 *pos++ = WPA_IGTK_LEN; 469 if (aes_wrap(sm->PTK.kek, WPA_IGTK_LEN / 8, 470 gsm->IGTK[gsm->GN_igtk - 4], pos)) { 471 os_free(subelem); 472 return NULL; 473 } 474 475 *len = subelem_len; 476 return subelem; 477 } 478 #endif /* CONFIG_IEEE80211W */ 479 480 481 static u8 * wpa_ft_process_rdie(u8 *pos, u8 *end, u8 id, u8 descr_count, 482 const u8 *ies, size_t ies_len) 483 { 484 struct ieee802_11_elems parse; 485 struct rsn_rdie *rdie; 486 487 wpa_printf(MSG_DEBUG, "FT: Resource Request: id=%d descr_count=%d", 488 id, descr_count); 489 wpa_hexdump(MSG_MSGDUMP, "FT: Resource descriptor IE(s)", 490 ies, ies_len); 491 492 if (end - pos < (int) sizeof(*rdie)) { 493 wpa_printf(MSG_ERROR, "FT: Not enough room for response RDIE"); 494 return pos; 495 } 496 497 *pos++ = WLAN_EID_RIC_DATA; 498 *pos++ = sizeof(*rdie); 499 rdie = (struct rsn_rdie *) pos; 500 rdie->id = id; 501 rdie->descr_count = 0; 502 rdie->status_code = host_to_le16(WLAN_STATUS_SUCCESS); 503 pos += sizeof(*rdie); 504 505 if (ieee802_11_parse_elems((u8 *) ies, ies_len, &parse, 1) == 506 ParseFailed) { 507 wpa_printf(MSG_DEBUG, "FT: Failed to parse request IEs"); 508 rdie->status_code = 509 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE); 510 return pos; 511 } 512 513 #ifdef NEED_AP_MLME 514 if (parse.wmm_tspec) { 515 struct wmm_tspec_element *tspec; 516 int res; 517 518 if (parse.wmm_tspec_len + 2 < (int) sizeof(*tspec)) { 519 wpa_printf(MSG_DEBUG, "FT: Too short WMM TSPEC IE " 520 "(%d)", (int) parse.wmm_tspec_len); 521 rdie->status_code = 522 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE); 523 return pos; 524 } 525 if (end - pos < (int) sizeof(*tspec)) { 526 wpa_printf(MSG_ERROR, "FT: Not enough room for " 527 "response TSPEC"); 528 rdie->status_code = 529 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE); 530 return pos; 531 } 532 tspec = (struct wmm_tspec_element *) pos; 533 os_memcpy(tspec, parse.wmm_tspec - 2, sizeof(*tspec)); 534 res = wmm_process_tspec(tspec); 535 wpa_printf(MSG_DEBUG, "FT: ADDTS processing result: %d", res); 536 if (res == WMM_ADDTS_STATUS_INVALID_PARAMETERS) 537 rdie->status_code = 538 host_to_le16(WLAN_STATUS_INVALID_PARAMETERS); 539 else if (res == WMM_ADDTS_STATUS_REFUSED) 540 rdie->status_code = 541 host_to_le16(WLAN_STATUS_REQUEST_DECLINED); 542 else { 543 /* TSPEC accepted; include updated TSPEC in response */ 544 rdie->descr_count = 1; 545 pos += sizeof(*tspec); 546 } 547 return pos; 548 } 549 #endif /* NEED_AP_MLME */ 550 551 wpa_printf(MSG_DEBUG, "FT: No supported resource requested"); 552 rdie->status_code = host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE); 553 return pos; 554 } 555 556 557 static u8 * wpa_ft_process_ric(u8 *pos, u8 *end, const u8 *ric, size_t ric_len) 558 { 559 const u8 *rpos, *start; 560 const struct rsn_rdie *rdie; 561 562 wpa_hexdump(MSG_MSGDUMP, "FT: RIC Request", ric, ric_len); 563 564 rpos = ric; 565 while (rpos + sizeof(*rdie) < ric + ric_len) { 566 if (rpos[0] != WLAN_EID_RIC_DATA || rpos[1] < sizeof(*rdie) || 567 rpos + 2 + rpos[1] > ric + ric_len) 568 break; 569 rdie = (const struct rsn_rdie *) (rpos + 2); 570 rpos += 2 + rpos[1]; 571 start = rpos; 572 573 while (rpos + 2 <= ric + ric_len && 574 rpos + 2 + rpos[1] <= ric + ric_len) { 575 if (rpos[0] == WLAN_EID_RIC_DATA) 576 break; 577 rpos += 2 + rpos[1]; 578 } 579 pos = wpa_ft_process_rdie(pos, end, rdie->id, 580 rdie->descr_count, 581 start, rpos - start); 582 } 583 584 return pos; 585 } 586 587 588 u8 * wpa_sm_write_assoc_resp_ies(struct wpa_state_machine *sm, u8 *pos, 589 size_t max_len, int auth_alg, 590 const u8 *req_ies, size_t req_ies_len) 591 { 592 u8 *end, *mdie, *ftie, *rsnie = NULL, *r0kh_id, *subelem = NULL; 593 size_t mdie_len, ftie_len, rsnie_len = 0, r0kh_id_len, subelem_len = 0; 594 int res; 595 struct wpa_auth_config *conf; 596 struct rsn_ftie *_ftie; 597 struct wpa_ft_ies parse; 598 u8 *ric_start; 599 u8 *anonce, *snonce; 600 601 if (sm == NULL) 602 return pos; 603 604 conf = &sm->wpa_auth->conf; 605 606 if (sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X && 607 sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_PSK) 608 return pos; 609 610 end = pos + max_len; 611 612 if (auth_alg == WLAN_AUTH_FT) { 613 /* 614 * RSN (only present if this is a Reassociation Response and 615 * part of a fast BSS transition) 616 */ 617 res = wpa_write_rsn_ie(conf, pos, end - pos, sm->pmk_r1_name); 618 if (res < 0) 619 return pos; 620 rsnie = pos; 621 rsnie_len = res; 622 pos += res; 623 } 624 625 /* Mobility Domain Information */ 626 res = wpa_write_mdie(conf, pos, end - pos); 627 if (res < 0) 628 return pos; 629 mdie = pos; 630 mdie_len = res; 631 pos += res; 632 633 /* Fast BSS Transition Information */ 634 if (auth_alg == WLAN_AUTH_FT) { 635 subelem = wpa_ft_gtk_subelem(sm, &subelem_len); 636 r0kh_id = sm->r0kh_id; 637 r0kh_id_len = sm->r0kh_id_len; 638 anonce = sm->ANonce; 639 snonce = sm->SNonce; 640 #ifdef CONFIG_IEEE80211W 641 if (sm->mgmt_frame_prot) { 642 u8 *igtk; 643 size_t igtk_len; 644 u8 *nbuf; 645 igtk = wpa_ft_igtk_subelem(sm, &igtk_len); 646 if (igtk == NULL) { 647 os_free(subelem); 648 return pos; 649 } 650 nbuf = os_realloc(subelem, subelem_len + igtk_len); 651 if (nbuf == NULL) { 652 os_free(subelem); 653 os_free(igtk); 654 return pos; 655 } 656 subelem = nbuf; 657 os_memcpy(subelem + subelem_len, igtk, igtk_len); 658 subelem_len += igtk_len; 659 os_free(igtk); 660 } 661 #endif /* CONFIG_IEEE80211W */ 662 } else { 663 r0kh_id = conf->r0_key_holder; 664 r0kh_id_len = conf->r0_key_holder_len; 665 anonce = NULL; 666 snonce = NULL; 667 } 668 res = wpa_write_ftie(conf, r0kh_id, r0kh_id_len, anonce, snonce, pos, 669 end - pos, subelem, subelem_len); 670 os_free(subelem); 671 if (res < 0) 672 return pos; 673 ftie = pos; 674 ftie_len = res; 675 pos += res; 676 677 os_free(sm->assoc_resp_ftie); 678 sm->assoc_resp_ftie = os_malloc(ftie_len); 679 if (sm->assoc_resp_ftie) 680 os_memcpy(sm->assoc_resp_ftie, ftie, ftie_len); 681 682 _ftie = (struct rsn_ftie *) (ftie + 2); 683 if (auth_alg == WLAN_AUTH_FT) 684 _ftie->mic_control[1] = 3; /* Information element count */ 685 686 ric_start = pos; 687 if (wpa_ft_parse_ies(req_ies, req_ies_len, &parse) == 0 && parse.ric) { 688 pos = wpa_ft_process_ric(pos, end, parse.ric, parse.ric_len); 689 if (auth_alg == WLAN_AUTH_FT) 690 _ftie->mic_control[1] += 691 ieee802_11_ie_count(ric_start, 692 pos - ric_start); 693 } 694 if (ric_start == pos) 695 ric_start = NULL; 696 697 if (auth_alg == WLAN_AUTH_FT && 698 wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 6, 699 mdie, mdie_len, ftie, ftie_len, 700 rsnie, rsnie_len, 701 ric_start, ric_start ? pos - ric_start : 0, 702 _ftie->mic) < 0) 703 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC"); 704 705 return pos; 706 } 707 708 709 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth, 710 int vlan_id, 711 enum wpa_alg alg, const u8 *addr, int idx, 712 u8 *key, size_t key_len) 713 { 714 if (wpa_auth->cb.set_key == NULL) 715 return -1; 716 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx, 717 key, key_len); 718 } 719 720 721 void wpa_ft_install_ptk(struct wpa_state_machine *sm) 722 { 723 enum wpa_alg alg; 724 int klen; 725 726 /* MLME-SETKEYS.request(PTK) */ 727 if (sm->pairwise == WPA_CIPHER_TKIP) { 728 alg = WPA_ALG_TKIP; 729 klen = 32; 730 } else if (sm->pairwise == WPA_CIPHER_CCMP) { 731 alg = WPA_ALG_CCMP; 732 klen = 16; 733 } else { 734 wpa_printf(MSG_DEBUG, "FT: Unknown pairwise alg 0x%x - skip " 735 "PTK configuration", sm->pairwise); 736 return; 737 } 738 739 /* FIX: add STA entry to kernel/driver here? The set_key will fail 740 * most likely without this.. At the moment, STA entry is added only 741 * after association has been completed. This function will be called 742 * again after association to get the PTK configured, but that could be 743 * optimized by adding the STA entry earlier. 744 */ 745 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0, 746 sm->PTK.tk1, klen)) 747 return; 748 749 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */ 750 sm->pairwise_set = TRUE; 751 } 752 753 754 static u16 wpa_ft_process_auth_req(struct wpa_state_machine *sm, 755 const u8 *ies, size_t ies_len, 756 u8 **resp_ies, size_t *resp_ies_len) 757 { 758 struct rsn_mdie *mdie; 759 struct rsn_ftie *ftie; 760 u8 pmk_r1[PMK_LEN], pmk_r1_name[WPA_PMK_NAME_LEN]; 761 u8 ptk_name[WPA_PMK_NAME_LEN]; 762 struct wpa_auth_config *conf; 763 struct wpa_ft_ies parse; 764 size_t buflen, ptk_len; 765 int ret; 766 u8 *pos, *end; 767 int pairwise; 768 769 *resp_ies = NULL; 770 *resp_ies_len = 0; 771 772 sm->pmk_r1_name_valid = 0; 773 conf = &sm->wpa_auth->conf; 774 775 wpa_hexdump(MSG_DEBUG, "FT: Received authentication frame IEs", 776 ies, ies_len); 777 778 if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) { 779 wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs"); 780 return WLAN_STATUS_UNSPECIFIED_FAILURE; 781 } 782 783 mdie = (struct rsn_mdie *) parse.mdie; 784 if (mdie == NULL || parse.mdie_len < sizeof(*mdie) || 785 os_memcmp(mdie->mobility_domain, 786 sm->wpa_auth->conf.mobility_domain, 787 MOBILITY_DOMAIN_ID_LEN) != 0) { 788 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE"); 789 return WLAN_STATUS_INVALID_MDIE; 790 } 791 792 ftie = (struct rsn_ftie *) parse.ftie; 793 if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) { 794 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE"); 795 return WLAN_STATUS_INVALID_FTIE; 796 } 797 798 os_memcpy(sm->SNonce, ftie->snonce, WPA_NONCE_LEN); 799 800 if (parse.r0kh_id == NULL) { 801 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE - no R0KH-ID"); 802 return WLAN_STATUS_INVALID_FTIE; 803 } 804 805 wpa_hexdump(MSG_DEBUG, "FT: STA R0KH-ID", 806 parse.r0kh_id, parse.r0kh_id_len); 807 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len); 808 sm->r0kh_id_len = parse.r0kh_id_len; 809 810 if (parse.rsn_pmkid == NULL) { 811 wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE"); 812 return WLAN_STATUS_INVALID_PMKID; 813 } 814 815 wpa_hexdump(MSG_DEBUG, "FT: Requested PMKR0Name", 816 parse.rsn_pmkid, WPA_PMK_NAME_LEN); 817 wpa_derive_pmk_r1_name(parse.rsn_pmkid, 818 sm->wpa_auth->conf.r1_key_holder, sm->addr, 819 pmk_r1_name); 820 wpa_hexdump(MSG_DEBUG, "FT: Derived requested PMKR1Name", 821 pmk_r1_name, WPA_PMK_NAME_LEN); 822 823 if (wpa_ft_fetch_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1_name, pmk_r1, 824 &pairwise) < 0) { 825 if (wpa_ft_pull_pmk_r1(sm->wpa_auth, sm->addr, sm->r0kh_id, 826 sm->r0kh_id_len, parse.rsn_pmkid) < 0) { 827 wpa_printf(MSG_DEBUG, "FT: Did not have matching " 828 "PMK-R1 and unknown R0KH-ID"); 829 return WLAN_STATUS_INVALID_PMKID; 830 } 831 832 /* 833 * TODO: Should return "status pending" (and the caller should 834 * not send out response now). The real response will be sent 835 * once the response from R0KH is received. 836 */ 837 return WLAN_STATUS_INVALID_PMKID; 838 } 839 840 wpa_hexdump_key(MSG_DEBUG, "FT: Selected PMK-R1", pmk_r1, PMK_LEN); 841 sm->pmk_r1_name_valid = 1; 842 os_memcpy(sm->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN); 843 844 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) { 845 wpa_printf(MSG_DEBUG, "FT: Failed to get random data for " 846 "ANonce"); 847 return WLAN_STATUS_UNSPECIFIED_FAILURE; 848 } 849 850 wpa_hexdump(MSG_DEBUG, "FT: Received SNonce", 851 sm->SNonce, WPA_NONCE_LEN); 852 wpa_hexdump(MSG_DEBUG, "FT: Generated ANonce", 853 sm->ANonce, WPA_NONCE_LEN); 854 855 ptk_len = pairwise != WPA_CIPHER_CCMP ? 64 : 48; 856 wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr, 857 sm->wpa_auth->addr, pmk_r1_name, 858 (u8 *) &sm->PTK, ptk_len, ptk_name); 859 wpa_hexdump_key(MSG_DEBUG, "FT: PTK", 860 (u8 *) &sm->PTK, ptk_len); 861 wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN); 862 863 sm->pairwise = pairwise; 864 wpa_ft_install_ptk(sm); 865 866 buflen = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) + 867 2 + FT_R1KH_ID_LEN + 200; 868 *resp_ies = os_zalloc(buflen); 869 if (*resp_ies == NULL) { 870 return WLAN_STATUS_UNSPECIFIED_FAILURE; 871 } 872 873 pos = *resp_ies; 874 end = *resp_ies + buflen; 875 876 ret = wpa_write_rsn_ie(conf, pos, end - pos, parse.rsn_pmkid); 877 if (ret < 0) { 878 os_free(*resp_ies); 879 *resp_ies = NULL; 880 return WLAN_STATUS_UNSPECIFIED_FAILURE; 881 } 882 pos += ret; 883 884 ret = wpa_write_mdie(conf, pos, end - pos); 885 if (ret < 0) { 886 os_free(*resp_ies); 887 *resp_ies = NULL; 888 return WLAN_STATUS_UNSPECIFIED_FAILURE; 889 } 890 pos += ret; 891 892 ret = wpa_write_ftie(conf, parse.r0kh_id, parse.r0kh_id_len, 893 sm->ANonce, sm->SNonce, pos, end - pos, NULL, 0); 894 if (ret < 0) { 895 os_free(*resp_ies); 896 *resp_ies = NULL; 897 return WLAN_STATUS_UNSPECIFIED_FAILURE; 898 } 899 pos += ret; 900 901 *resp_ies_len = pos - *resp_ies; 902 903 return WLAN_STATUS_SUCCESS; 904 } 905 906 907 void wpa_ft_process_auth(struct wpa_state_machine *sm, const u8 *bssid, 908 u16 auth_transaction, const u8 *ies, size_t ies_len, 909 void (*cb)(void *ctx, const u8 *dst, const u8 *bssid, 910 u16 auth_transaction, u16 status, 911 const u8 *ies, size_t ies_len), 912 void *ctx) 913 { 914 u16 status; 915 u8 *resp_ies; 916 size_t resp_ies_len; 917 918 if (sm == NULL) { 919 wpa_printf(MSG_DEBUG, "FT: Received authentication frame, but " 920 "WPA SM not available"); 921 return; 922 } 923 924 wpa_printf(MSG_DEBUG, "FT: Received authentication frame: STA=" MACSTR 925 " BSSID=" MACSTR " transaction=%d", 926 MAC2STR(sm->addr), MAC2STR(bssid), auth_transaction); 927 status = wpa_ft_process_auth_req(sm, ies, ies_len, &resp_ies, 928 &resp_ies_len); 929 930 wpa_printf(MSG_DEBUG, "FT: FT authentication response: dst=" MACSTR 931 " auth_transaction=%d status=%d", 932 MAC2STR(sm->addr), auth_transaction + 1, status); 933 wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len); 934 cb(ctx, sm->addr, bssid, auth_transaction + 1, status, 935 resp_ies, resp_ies_len); 936 os_free(resp_ies); 937 } 938 939 940 u16 wpa_ft_validate_reassoc(struct wpa_state_machine *sm, const u8 *ies, 941 size_t ies_len) 942 { 943 struct wpa_ft_ies parse; 944 struct rsn_mdie *mdie; 945 struct rsn_ftie *ftie; 946 u8 mic[16]; 947 unsigned int count; 948 949 if (sm == NULL) 950 return WLAN_STATUS_UNSPECIFIED_FAILURE; 951 952 wpa_hexdump(MSG_DEBUG, "FT: Reassoc Req IEs", ies, ies_len); 953 954 if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) { 955 wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs"); 956 return WLAN_STATUS_UNSPECIFIED_FAILURE; 957 } 958 959 if (parse.rsn == NULL) { 960 wpa_printf(MSG_DEBUG, "FT: No RSNIE in Reassoc Req"); 961 return WLAN_STATUS_UNSPECIFIED_FAILURE; 962 } 963 964 if (parse.rsn_pmkid == NULL) { 965 wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE"); 966 return WLAN_STATUS_INVALID_PMKID; 967 } 968 969 if (os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0) 970 { 971 wpa_printf(MSG_DEBUG, "FT: PMKID in Reassoc Req did not match " 972 "with the PMKR1Name derived from auth request"); 973 return WLAN_STATUS_INVALID_PMKID; 974 } 975 976 mdie = (struct rsn_mdie *) parse.mdie; 977 if (mdie == NULL || parse.mdie_len < sizeof(*mdie) || 978 os_memcmp(mdie->mobility_domain, 979 sm->wpa_auth->conf.mobility_domain, 980 MOBILITY_DOMAIN_ID_LEN) != 0) { 981 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE"); 982 return WLAN_STATUS_INVALID_MDIE; 983 } 984 985 ftie = (struct rsn_ftie *) parse.ftie; 986 if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) { 987 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE"); 988 return WLAN_STATUS_INVALID_FTIE; 989 } 990 991 if (os_memcmp(ftie->snonce, sm->SNonce, WPA_NONCE_LEN) != 0) { 992 wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE"); 993 wpa_hexdump(MSG_DEBUG, "FT: Received SNonce", 994 ftie->snonce, WPA_NONCE_LEN); 995 wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce", 996 sm->SNonce, WPA_NONCE_LEN); 997 return -1; 998 } 999 1000 if (os_memcmp(ftie->anonce, sm->ANonce, WPA_NONCE_LEN) != 0) { 1001 wpa_printf(MSG_DEBUG, "FT: ANonce mismatch in FTIE"); 1002 wpa_hexdump(MSG_DEBUG, "FT: Received ANonce", 1003 ftie->anonce, WPA_NONCE_LEN); 1004 wpa_hexdump(MSG_DEBUG, "FT: Expected ANonce", 1005 sm->ANonce, WPA_NONCE_LEN); 1006 return -1; 1007 } 1008 1009 1010 if (parse.r0kh_id == NULL) { 1011 wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE"); 1012 return -1; 1013 } 1014 1015 if (parse.r0kh_id_len != sm->r0kh_id_len || 1016 os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) { 1017 wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with " 1018 "the current R0KH-ID"); 1019 wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE", 1020 parse.r0kh_id, parse.r0kh_id_len); 1021 wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID", 1022 sm->r0kh_id, sm->r0kh_id_len); 1023 return -1; 1024 } 1025 1026 if (parse.r1kh_id == NULL) { 1027 wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE"); 1028 return -1; 1029 } 1030 1031 if (os_memcmp(parse.r1kh_id, sm->wpa_auth->conf.r1_key_holder, 1032 FT_R1KH_ID_LEN) != 0) { 1033 wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in " 1034 "ReassocReq"); 1035 wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID in FTIE", 1036 parse.r1kh_id, FT_R1KH_ID_LEN); 1037 wpa_hexdump(MSG_DEBUG, "FT: Expected R1KH-ID", 1038 sm->wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN); 1039 return -1; 1040 } 1041 1042 if (parse.rsn_pmkid == NULL || 1043 os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN)) { 1044 wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in " 1045 "RSNIE (pmkid=%d)", !!parse.rsn_pmkid); 1046 return -1; 1047 } 1048 1049 count = 3; 1050 if (parse.ric) 1051 count += ieee802_11_ie_count(parse.ric, parse.ric_len); 1052 if (ftie->mic_control[1] != count) { 1053 wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in MIC " 1054 "Control: received %u expected %u", 1055 ftie->mic_control[1], count); 1056 return -1; 1057 } 1058 1059 if (wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 5, 1060 parse.mdie - 2, parse.mdie_len + 2, 1061 parse.ftie - 2, parse.ftie_len + 2, 1062 parse.rsn - 2, parse.rsn_len + 2, 1063 parse.ric, parse.ric_len, 1064 mic) < 0) { 1065 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC"); 1066 return WLAN_STATUS_UNSPECIFIED_FAILURE; 1067 } 1068 1069 if (os_memcmp(mic, ftie->mic, 16) != 0) { 1070 wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE"); 1071 wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16); 1072 wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16); 1073 return WLAN_STATUS_INVALID_FTIE; 1074 } 1075 1076 return WLAN_STATUS_SUCCESS; 1077 } 1078 1079 1080 int wpa_ft_action_rx(struct wpa_state_machine *sm, const u8 *data, size_t len) 1081 { 1082 const u8 *sta_addr, *target_ap; 1083 const u8 *ies; 1084 size_t ies_len; 1085 u8 action; 1086 struct ft_rrb_frame *frame; 1087 1088 if (sm == NULL) 1089 return -1; 1090 1091 /* 1092 * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6] 1093 * FT Request action frame body[variable] 1094 */ 1095 1096 if (len < 14) { 1097 wpa_printf(MSG_DEBUG, "FT: Too short FT Action frame " 1098 "(len=%lu)", (unsigned long) len); 1099 return -1; 1100 } 1101 1102 action = data[1]; 1103 sta_addr = data + 2; 1104 target_ap = data + 8; 1105 ies = data + 14; 1106 ies_len = len - 14; 1107 1108 wpa_printf(MSG_DEBUG, "FT: Received FT Action frame (STA=" MACSTR 1109 " Target AP=" MACSTR " Action=%d)", 1110 MAC2STR(sta_addr), MAC2STR(target_ap), action); 1111 1112 if (os_memcmp(sta_addr, sm->addr, ETH_ALEN) != 0) { 1113 wpa_printf(MSG_DEBUG, "FT: Mismatch in FT Action STA address: " 1114 "STA=" MACSTR " STA-Address=" MACSTR, 1115 MAC2STR(sm->addr), MAC2STR(sta_addr)); 1116 return -1; 1117 } 1118 1119 /* 1120 * Do some sanity checking on the target AP address (not own and not 1121 * broadcast. This could be extended to filter based on a list of known 1122 * APs in the MD (if such a list were configured). 1123 */ 1124 if ((target_ap[0] & 0x01) || 1125 os_memcmp(target_ap, sm->wpa_auth->addr, ETH_ALEN) == 0) { 1126 wpa_printf(MSG_DEBUG, "FT: Invalid Target AP in FT Action " 1127 "frame"); 1128 return -1; 1129 } 1130 1131 wpa_hexdump(MSG_MSGDUMP, "FT: Action frame body", ies, ies_len); 1132 1133 /* RRB - Forward action frame to the target AP */ 1134 frame = os_malloc(sizeof(*frame) + len); 1135 frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB; 1136 frame->packet_type = FT_PACKET_REQUEST; 1137 frame->action_length = host_to_le16(len); 1138 os_memcpy(frame->ap_address, sm->wpa_auth->addr, ETH_ALEN); 1139 os_memcpy(frame + 1, data, len); 1140 1141 wpa_ft_rrb_send(sm->wpa_auth, target_ap, (u8 *) frame, 1142 sizeof(*frame) + len); 1143 os_free(frame); 1144 1145 return 0; 1146 } 1147 1148 1149 static int wpa_ft_rrb_rx_request(struct wpa_authenticator *wpa_auth, 1150 const u8 *current_ap, const u8 *sta_addr, 1151 const u8 *body, size_t len) 1152 { 1153 struct wpa_state_machine *sm; 1154 u16 status; 1155 u8 *resp_ies, *pos; 1156 size_t resp_ies_len, rlen; 1157 struct ft_rrb_frame *frame; 1158 1159 sm = wpa_ft_add_sta(wpa_auth, sta_addr); 1160 if (sm == NULL) { 1161 wpa_printf(MSG_DEBUG, "FT: Failed to add new STA based on " 1162 "RRB Request"); 1163 return -1; 1164 } 1165 1166 wpa_hexdump(MSG_MSGDUMP, "FT: RRB Request Frame body", body, len); 1167 1168 status = wpa_ft_process_auth_req(sm, body, len, &resp_ies, 1169 &resp_ies_len); 1170 1171 wpa_printf(MSG_DEBUG, "FT: RRB authentication response: STA=" MACSTR 1172 " CurrentAP=" MACSTR " status=%d", 1173 MAC2STR(sm->addr), MAC2STR(current_ap), status); 1174 wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len); 1175 1176 /* RRB - Forward action frame response to the Current AP */ 1177 1178 /* 1179 * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6] 1180 * Status_Code[2] FT Request action frame body[variable] 1181 */ 1182 rlen = 2 + 2 * ETH_ALEN + 2 + resp_ies_len; 1183 1184 frame = os_malloc(sizeof(*frame) + rlen); 1185 frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB; 1186 frame->packet_type = FT_PACKET_RESPONSE; 1187 frame->action_length = host_to_le16(rlen); 1188 os_memcpy(frame->ap_address, wpa_auth->addr, ETH_ALEN); 1189 pos = (u8 *) (frame + 1); 1190 *pos++ = WLAN_ACTION_FT; 1191 *pos++ = 2; /* Action: Response */ 1192 os_memcpy(pos, sta_addr, ETH_ALEN); 1193 pos += ETH_ALEN; 1194 os_memcpy(pos, wpa_auth->addr, ETH_ALEN); 1195 pos += ETH_ALEN; 1196 WPA_PUT_LE16(pos, status); 1197 pos += 2; 1198 if (resp_ies) { 1199 os_memcpy(pos, resp_ies, resp_ies_len); 1200 os_free(resp_ies); 1201 } 1202 1203 wpa_ft_rrb_send(wpa_auth, current_ap, (u8 *) frame, 1204 sizeof(*frame) + rlen); 1205 os_free(frame); 1206 1207 return 0; 1208 } 1209 1210 1211 static int wpa_ft_rrb_rx_pull(struct wpa_authenticator *wpa_auth, 1212 const u8 *src_addr, 1213 const u8 *data, size_t data_len) 1214 { 1215 struct ft_r0kh_r1kh_pull_frame *frame, f; 1216 struct ft_remote_r1kh *r1kh; 1217 struct ft_r0kh_r1kh_resp_frame resp, r; 1218 u8 pmk_r0[PMK_LEN]; 1219 int pairwise; 1220 1221 wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull"); 1222 1223 if (data_len < sizeof(*frame)) 1224 return -1; 1225 1226 r1kh = wpa_auth->conf.r1kh_list; 1227 while (r1kh) { 1228 if (os_memcmp(r1kh->addr, src_addr, ETH_ALEN) == 0) 1229 break; 1230 r1kh = r1kh->next; 1231 } 1232 if (r1kh == NULL) { 1233 wpa_printf(MSG_DEBUG, "FT: No matching R1KH address found for " 1234 "PMK-R1 pull source address " MACSTR, 1235 MAC2STR(src_addr)); 1236 return -1; 1237 } 1238 1239 frame = (struct ft_r0kh_r1kh_pull_frame *) data; 1240 /* aes_unwrap() does not support inplace decryption, so use a temporary 1241 * buffer for the data. */ 1242 if (aes_unwrap(r1kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8, 1243 frame->nonce, f.nonce) < 0) { 1244 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull " 1245 "request from " MACSTR, MAC2STR(src_addr)); 1246 return -1; 1247 } 1248 1249 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce", 1250 f.nonce, sizeof(f.nonce)); 1251 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR0Name", 1252 f.pmk_r0_name, WPA_PMK_NAME_LEN); 1253 wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID=" 1254 MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id)); 1255 1256 os_memset(&resp, 0, sizeof(resp)); 1257 resp.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB; 1258 resp.packet_type = FT_PACKET_R0KH_R1KH_RESP; 1259 resp.data_length = host_to_le16(FT_R0KH_R1KH_RESP_DATA_LEN); 1260 os_memcpy(resp.ap_address, wpa_auth->addr, ETH_ALEN); 1261 1262 /* aes_wrap() does not support inplace encryption, so use a temporary 1263 * buffer for the data. */ 1264 os_memcpy(r.nonce, f.nonce, sizeof(f.nonce)); 1265 os_memcpy(r.r1kh_id, f.r1kh_id, FT_R1KH_ID_LEN); 1266 os_memcpy(r.s1kh_id, f.s1kh_id, ETH_ALEN); 1267 if (wpa_ft_fetch_pmk_r0(wpa_auth, f.s1kh_id, f.pmk_r0_name, pmk_r0, 1268 &pairwise) < 0) { 1269 wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name found for " 1270 "PMK-R1 pull"); 1271 return -1; 1272 } 1273 1274 wpa_derive_pmk_r1(pmk_r0, f.pmk_r0_name, f.r1kh_id, f.s1kh_id, 1275 r.pmk_r1, r.pmk_r1_name); 1276 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", r.pmk_r1, PMK_LEN); 1277 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", r.pmk_r1_name, 1278 WPA_PMK_NAME_LEN); 1279 r.pairwise = host_to_le16(pairwise); 1280 1281 if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8, 1282 r.nonce, resp.nonce) < 0) { 1283 os_memset(pmk_r0, 0, PMK_LEN); 1284 return -1; 1285 } 1286 1287 os_memset(pmk_r0, 0, PMK_LEN); 1288 1289 wpa_ft_rrb_send(wpa_auth, src_addr, (u8 *) &resp, sizeof(resp)); 1290 1291 return 0; 1292 } 1293 1294 1295 static int wpa_ft_rrb_rx_resp(struct wpa_authenticator *wpa_auth, 1296 const u8 *src_addr, 1297 const u8 *data, size_t data_len) 1298 { 1299 struct ft_r0kh_r1kh_resp_frame *frame, f; 1300 struct ft_remote_r0kh *r0kh; 1301 int pairwise; 1302 1303 wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull response"); 1304 1305 if (data_len < sizeof(*frame)) 1306 return -1; 1307 1308 r0kh = wpa_auth->conf.r0kh_list; 1309 while (r0kh) { 1310 if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0) 1311 break; 1312 r0kh = r0kh->next; 1313 } 1314 if (r0kh == NULL) { 1315 wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for " 1316 "PMK-R0 pull response source address " MACSTR, 1317 MAC2STR(src_addr)); 1318 return -1; 1319 } 1320 1321 frame = (struct ft_r0kh_r1kh_resp_frame *) data; 1322 /* aes_unwrap() does not support inplace decryption, so use a temporary 1323 * buffer for the data. */ 1324 if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8, 1325 frame->nonce, f.nonce) < 0) { 1326 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull " 1327 "response from " MACSTR, MAC2STR(src_addr)); 1328 return -1; 1329 } 1330 1331 if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN) 1332 != 0) { 1333 wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull response did not use a " 1334 "matching R1KH-ID"); 1335 return -1; 1336 } 1337 1338 /* TODO: verify that <nonce,s1kh_id> matches with a pending request 1339 * and call this requests callback function to finish request 1340 * processing */ 1341 1342 pairwise = le_to_host16(f.pairwise); 1343 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce", 1344 f.nonce, sizeof(f.nonce)); 1345 wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID=" 1346 MACSTR " pairwise=0x%x", 1347 MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id), pairwise); 1348 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 pull - PMK-R1", 1349 f.pmk_r1, PMK_LEN); 1350 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR1Name", 1351 f.pmk_r1_name, WPA_PMK_NAME_LEN); 1352 1353 wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name, 1354 pairwise); 1355 os_memset(f.pmk_r1, 0, PMK_LEN); 1356 1357 return 0; 1358 } 1359 1360 1361 static int wpa_ft_rrb_rx_push(struct wpa_authenticator *wpa_auth, 1362 const u8 *src_addr, 1363 const u8 *data, size_t data_len) 1364 { 1365 struct ft_r0kh_r1kh_push_frame *frame, f; 1366 struct ft_remote_r0kh *r0kh; 1367 struct os_time now; 1368 os_time_t tsend; 1369 int pairwise; 1370 1371 wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 push"); 1372 1373 if (data_len < sizeof(*frame)) 1374 return -1; 1375 1376 r0kh = wpa_auth->conf.r0kh_list; 1377 while (r0kh) { 1378 if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0) 1379 break; 1380 r0kh = r0kh->next; 1381 } 1382 if (r0kh == NULL) { 1383 wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for " 1384 "PMK-R0 push source address " MACSTR, 1385 MAC2STR(src_addr)); 1386 return -1; 1387 } 1388 1389 frame = (struct ft_r0kh_r1kh_push_frame *) data; 1390 /* aes_unwrap() does not support inplace decryption, so use a temporary 1391 * buffer for the data. */ 1392 if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8, 1393 frame->timestamp, f.timestamp) < 0) { 1394 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 push from " 1395 MACSTR, MAC2STR(src_addr)); 1396 return -1; 1397 } 1398 1399 os_get_time(&now); 1400 tsend = WPA_GET_LE32(f.timestamp); 1401 if ((now.sec > tsend && now.sec - tsend > 60) || 1402 (now.sec < tsend && tsend - now.sec > 60)) { 1403 wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not have a valid " 1404 "timestamp: sender time %d own time %d\n", 1405 (int) tsend, (int) now.sec); 1406 return -1; 1407 } 1408 1409 if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN) 1410 != 0) { 1411 wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not use a matching " 1412 "R1KH-ID (received " MACSTR " own " MACSTR ")", 1413 MAC2STR(f.r1kh_id), 1414 MAC2STR(wpa_auth->conf.r1_key_holder)); 1415 return -1; 1416 } 1417 1418 pairwise = le_to_host16(f.pairwise); 1419 wpa_printf(MSG_DEBUG, "FT: PMK-R1 push - R1KH-ID=" MACSTR " S1KH-ID=" 1420 MACSTR " pairwise=0x%x", 1421 MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id), pairwise); 1422 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 push - PMK-R1", 1423 f.pmk_r1, PMK_LEN); 1424 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 push - PMKR1Name", 1425 f.pmk_r1_name, WPA_PMK_NAME_LEN); 1426 1427 wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name, 1428 pairwise); 1429 os_memset(f.pmk_r1, 0, PMK_LEN); 1430 1431 return 0; 1432 } 1433 1434 1435 int wpa_ft_rrb_rx(struct wpa_authenticator *wpa_auth, const u8 *src_addr, 1436 const u8 *data, size_t data_len) 1437 { 1438 struct ft_rrb_frame *frame; 1439 u16 alen; 1440 const u8 *pos, *end, *start; 1441 u8 action; 1442 const u8 *sta_addr, *target_ap_addr; 1443 1444 wpa_printf(MSG_DEBUG, "FT: RRB received frame from remote AP " MACSTR, 1445 MAC2STR(src_addr)); 1446 1447 if (data_len < sizeof(*frame)) { 1448 wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (data_len=%lu)", 1449 (unsigned long) data_len); 1450 return -1; 1451 } 1452 1453 pos = data; 1454 frame = (struct ft_rrb_frame *) pos; 1455 pos += sizeof(*frame); 1456 1457 alen = le_to_host16(frame->action_length); 1458 wpa_printf(MSG_DEBUG, "FT: RRB frame - frame_type=%d packet_type=%d " 1459 "action_length=%d ap_address=" MACSTR, 1460 frame->frame_type, frame->packet_type, alen, 1461 MAC2STR(frame->ap_address)); 1462 1463 if (frame->frame_type != RSN_REMOTE_FRAME_TYPE_FT_RRB) { 1464 /* Discard frame per IEEE Std 802.11r-2008, 11A.10.3 */ 1465 wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with " 1466 "unrecognized type %d", frame->frame_type); 1467 return -1; 1468 } 1469 1470 if (alen > data_len - sizeof(*frame)) { 1471 wpa_printf(MSG_DEBUG, "FT: RRB frame too short for action " 1472 "frame"); 1473 return -1; 1474 } 1475 1476 if (frame->packet_type == FT_PACKET_R0KH_R1KH_PULL) 1477 return wpa_ft_rrb_rx_pull(wpa_auth, src_addr, data, data_len); 1478 if (frame->packet_type == FT_PACKET_R0KH_R1KH_RESP) 1479 return wpa_ft_rrb_rx_resp(wpa_auth, src_addr, data, data_len); 1480 if (frame->packet_type == FT_PACKET_R0KH_R1KH_PUSH) 1481 return wpa_ft_rrb_rx_push(wpa_auth, src_addr, data, data_len); 1482 1483 wpa_hexdump(MSG_MSGDUMP, "FT: RRB - FT Action frame", pos, alen); 1484 1485 if (alen < 1 + 1 + 2 * ETH_ALEN) { 1486 wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (not enough " 1487 "room for Action Frame body); alen=%lu", 1488 (unsigned long) alen); 1489 return -1; 1490 } 1491 start = pos; 1492 end = pos + alen; 1493 1494 if (*pos != WLAN_ACTION_FT) { 1495 wpa_printf(MSG_DEBUG, "FT: Unexpected Action frame category " 1496 "%d", *pos); 1497 return -1; 1498 } 1499 1500 pos++; 1501 action = *pos++; 1502 sta_addr = pos; 1503 pos += ETH_ALEN; 1504 target_ap_addr = pos; 1505 pos += ETH_ALEN; 1506 wpa_printf(MSG_DEBUG, "FT: RRB Action Frame: action=%d sta_addr=" 1507 MACSTR " target_ap_addr=" MACSTR, 1508 action, MAC2STR(sta_addr), MAC2STR(target_ap_addr)); 1509 1510 if (frame->packet_type == FT_PACKET_REQUEST) { 1511 wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Request"); 1512 1513 if (action != 1) { 1514 wpa_printf(MSG_DEBUG, "FT: Unexpected Action %d in " 1515 "RRB Request", action); 1516 return -1; 1517 } 1518 1519 if (os_memcmp(target_ap_addr, wpa_auth->addr, ETH_ALEN) != 0) { 1520 wpa_printf(MSG_DEBUG, "FT: Target AP address in the " 1521 "RRB Request does not match with own " 1522 "address"); 1523 return -1; 1524 } 1525 1526 if (wpa_ft_rrb_rx_request(wpa_auth, frame->ap_address, 1527 sta_addr, pos, end - pos) < 0) 1528 return -1; 1529 } else if (frame->packet_type == FT_PACKET_RESPONSE) { 1530 u16 status_code; 1531 1532 if (end - pos < 2) { 1533 wpa_printf(MSG_DEBUG, "FT: Not enough room for status " 1534 "code in RRB Response"); 1535 return -1; 1536 } 1537 status_code = WPA_GET_LE16(pos); 1538 pos += 2; 1539 1540 wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Response " 1541 "(status_code=%d)", status_code); 1542 1543 if (wpa_ft_action_send(wpa_auth, sta_addr, start, alen) < 0) 1544 return -1; 1545 } else { 1546 wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with unknown " 1547 "packet_type %d", frame->packet_type); 1548 return -1; 1549 } 1550 1551 return 0; 1552 } 1553 1554 1555 static void wpa_ft_generate_pmk_r1(struct wpa_authenticator *wpa_auth, 1556 struct wpa_ft_pmk_r0_sa *pmk_r0, 1557 struct ft_remote_r1kh *r1kh, 1558 const u8 *s1kh_id, int pairwise) 1559 { 1560 struct ft_r0kh_r1kh_push_frame frame, f; 1561 struct os_time now; 1562 1563 os_memset(&frame, 0, sizeof(frame)); 1564 frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB; 1565 frame.packet_type = FT_PACKET_R0KH_R1KH_PUSH; 1566 frame.data_length = host_to_le16(FT_R0KH_R1KH_PUSH_DATA_LEN); 1567 os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN); 1568 1569 /* aes_wrap() does not support inplace encryption, so use a temporary 1570 * buffer for the data. */ 1571 os_memcpy(f.r1kh_id, r1kh->id, FT_R1KH_ID_LEN); 1572 os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN); 1573 os_memcpy(f.pmk_r0_name, pmk_r0->pmk_r0_name, WPA_PMK_NAME_LEN); 1574 wpa_derive_pmk_r1(pmk_r0->pmk_r0, pmk_r0->pmk_r0_name, r1kh->id, 1575 s1kh_id, f.pmk_r1, f.pmk_r1_name); 1576 wpa_printf(MSG_DEBUG, "FT: R1KH-ID " MACSTR, MAC2STR(r1kh->id)); 1577 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", f.pmk_r1, PMK_LEN); 1578 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", f.pmk_r1_name, 1579 WPA_PMK_NAME_LEN); 1580 os_get_time(&now); 1581 WPA_PUT_LE32(f.timestamp, now.sec); 1582 f.pairwise = host_to_le16(pairwise); 1583 if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8, 1584 f.timestamp, frame.timestamp) < 0) 1585 return; 1586 1587 wpa_ft_rrb_send(wpa_auth, r1kh->addr, (u8 *) &frame, sizeof(frame)); 1588 } 1589 1590 1591 void wpa_ft_push_pmk_r1(struct wpa_authenticator *wpa_auth, const u8 *addr) 1592 { 1593 struct wpa_ft_pmk_r0_sa *r0; 1594 struct ft_remote_r1kh *r1kh; 1595 1596 if (!wpa_auth->conf.pmk_r1_push) 1597 return; 1598 1599 r0 = wpa_auth->ft_pmk_cache->pmk_r0; 1600 while (r0) { 1601 if (os_memcmp(r0->spa, addr, ETH_ALEN) == 0) 1602 break; 1603 r0 = r0->next; 1604 } 1605 1606 if (r0 == NULL || r0->pmk_r1_pushed) 1607 return; 1608 r0->pmk_r1_pushed = 1; 1609 1610 wpa_printf(MSG_DEBUG, "FT: Deriving and pushing PMK-R1 keys to R1KHs " 1611 "for STA " MACSTR, MAC2STR(addr)); 1612 1613 r1kh = wpa_auth->conf.r1kh_list; 1614 while (r1kh) { 1615 wpa_ft_generate_pmk_r1(wpa_auth, r0, r1kh, addr, r0->pairwise); 1616 r1kh = r1kh->next; 1617 } 1618 } 1619 1620 #endif /* CONFIG_IEEE80211R */ 1621