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