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