Share WPA IE parser function for RSN authenticator/supplicant
[mech_eap.git] / src / common / wpa_common.c
1 /*
2  * WPA/RSN - Shared functions for supplicant and authenticator
3  * Copyright (c) 2002-2008, 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 "crypto/md5.h"
19 #include "crypto/sha1.h"
20 #include "crypto/sha256.h"
21 #include "crypto/aes_wrap.h"
22 #include "crypto/crypto.h"
23 #include "ieee802_11_defs.h"
24 #include "defs.h"
25 #include "wpa_common.h"
26
27
28 /**
29  * wpa_eapol_key_mic - Calculate EAPOL-Key MIC
30  * @key: EAPOL-Key Key Confirmation Key (KCK)
31  * @ver: Key descriptor version (WPA_KEY_INFO_TYPE_*)
32  * @buf: Pointer to the beginning of the EAPOL header (version field)
33  * @len: Length of the EAPOL frame (from EAPOL header to the end of the frame)
34  * @mic: Pointer to the buffer to which the EAPOL-Key MIC is written
35  * Returns: 0 on success, -1 on failure
36  *
37  * Calculate EAPOL-Key MIC for an EAPOL-Key packet. The EAPOL-Key MIC field has
38  * to be cleared (all zeroes) when calling this function.
39  *
40  * Note: 'IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames' has an error in the
41  * description of the Key MIC calculation. It includes packet data from the
42  * beginning of the EAPOL-Key header, not EAPOL header. This incorrect change
43  * happened during final editing of the standard and the correct behavior is
44  * defined in the last draft (IEEE 802.11i/D10).
45  */
46 int wpa_eapol_key_mic(const u8 *key, int ver, const u8 *buf, size_t len,
47                       u8 *mic)
48 {
49         u8 hash[SHA1_MAC_LEN];
50
51         switch (ver) {
52         case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4:
53                 return hmac_md5(key, 16, buf, len, mic);
54         case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES:
55                 if (hmac_sha1(key, 16, buf, len, hash))
56                         return -1;
57                 os_memcpy(mic, hash, MD5_MAC_LEN);
58                 break;
59 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
60         case WPA_KEY_INFO_TYPE_AES_128_CMAC:
61                 return omac1_aes_128(key, buf, len, mic);
62 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
63         default:
64                 return -1;
65         }
66
67         return 0;
68 }
69
70
71 /**
72  * wpa_pmk_to_ptk - Calculate PTK from PMK, addresses, and nonces
73  * @pmk: Pairwise master key
74  * @pmk_len: Length of PMK
75  * @label: Label to use in derivation
76  * @addr1: AA or SA
77  * @addr2: SA or AA
78  * @nonce1: ANonce or SNonce
79  * @nonce2: SNonce or ANonce
80  * @ptk: Buffer for pairwise transient key
81  * @ptk_len: Length of PTK
82  * @use_sha256: Whether to use SHA256-based KDF
83  *
84  * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
85  * PTK = PRF-X(PMK, "Pairwise key expansion",
86  *             Min(AA, SA) || Max(AA, SA) ||
87  *             Min(ANonce, SNonce) || Max(ANonce, SNonce))
88  *
89  * STK = PRF-X(SMK, "Peer key expansion",
90  *             Min(MAC_I, MAC_P) || Max(MAC_I, MAC_P) ||
91  *             Min(INonce, PNonce) || Max(INonce, PNonce))
92  */
93 void wpa_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const char *label,
94                     const u8 *addr1, const u8 *addr2,
95                     const u8 *nonce1, const u8 *nonce2,
96                     u8 *ptk, size_t ptk_len, int use_sha256)
97 {
98         u8 data[2 * ETH_ALEN + 2 * WPA_NONCE_LEN];
99
100         if (os_memcmp(addr1, addr2, ETH_ALEN) < 0) {
101                 os_memcpy(data, addr1, ETH_ALEN);
102                 os_memcpy(data + ETH_ALEN, addr2, ETH_ALEN);
103         } else {
104                 os_memcpy(data, addr2, ETH_ALEN);
105                 os_memcpy(data + ETH_ALEN, addr1, ETH_ALEN);
106         }
107
108         if (os_memcmp(nonce1, nonce2, WPA_NONCE_LEN) < 0) {
109                 os_memcpy(data + 2 * ETH_ALEN, nonce1, WPA_NONCE_LEN);
110                 os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce2,
111                           WPA_NONCE_LEN);
112         } else {
113                 os_memcpy(data + 2 * ETH_ALEN, nonce2, WPA_NONCE_LEN);
114                 os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce1,
115                           WPA_NONCE_LEN);
116         }
117
118 #ifdef CONFIG_IEEE80211W
119         if (use_sha256)
120                 sha256_prf(pmk, pmk_len, label, data, sizeof(data),
121                            ptk, ptk_len);
122         else
123 #endif /* CONFIG_IEEE80211W */
124                 sha1_prf(pmk, pmk_len, label, data, sizeof(data), ptk,
125                          ptk_len);
126
127         wpa_printf(MSG_DEBUG, "WPA: PTK derivation - A1=" MACSTR " A2=" MACSTR,
128                    MAC2STR(addr1), MAC2STR(addr2));
129         wpa_hexdump_key(MSG_DEBUG, "WPA: PMK", pmk, pmk_len);
130         wpa_hexdump_key(MSG_DEBUG, "WPA: PTK", ptk, ptk_len);
131 }
132
133
134 #ifdef CONFIG_IEEE80211R
135 int wpa_ft_mic(const u8 *kck, const u8 *sta_addr, const u8 *ap_addr,
136                u8 transaction_seqnum, const u8 *mdie, size_t mdie_len,
137                const u8 *ftie, size_t ftie_len,
138                const u8 *rsnie, size_t rsnie_len,
139                const u8 *ric, size_t ric_len, u8 *mic)
140 {
141         u8 *buf, *pos;
142         size_t buf_len;
143
144         buf_len = 2 * ETH_ALEN + 1 + mdie_len + ftie_len + rsnie_len + ric_len;
145         buf = os_malloc(buf_len);
146         if (buf == NULL)
147                 return -1;
148
149         pos = buf;
150         os_memcpy(pos, sta_addr, ETH_ALEN);
151         pos += ETH_ALEN;
152         os_memcpy(pos, ap_addr, ETH_ALEN);
153         pos += ETH_ALEN;
154         *pos++ = transaction_seqnum;
155         if (rsnie) {
156                 os_memcpy(pos, rsnie, rsnie_len);
157                 pos += rsnie_len;
158         }
159         if (mdie) {
160                 os_memcpy(pos, mdie, mdie_len);
161                 pos += mdie_len;
162         }
163         if (ftie) {
164                 struct rsn_ftie *_ftie;
165                 os_memcpy(pos, ftie, ftie_len);
166                 if (ftie_len < 2 + sizeof(*_ftie)) {
167                         os_free(buf);
168                         return -1;
169                 }
170                 _ftie = (struct rsn_ftie *) (pos + 2);
171                 os_memset(_ftie->mic, 0, sizeof(_ftie->mic));
172                 pos += ftie_len;
173         }
174         if (ric) {
175                 os_memcpy(pos, ric, ric_len);
176                 pos += ric_len;
177         }
178
179         wpa_hexdump(MSG_MSGDUMP, "FT: MIC data", buf, pos - buf);
180         if (omac1_aes_128(kck, buf, pos - buf, mic)) {
181                 os_free(buf);
182                 return -1;
183         }
184
185         os_free(buf);
186
187         return 0;
188 }
189 #endif /* CONFIG_IEEE80211R */
190
191
192 #ifndef CONFIG_NO_WPA2
193 static int rsn_selector_to_bitfield(const u8 *s)
194 {
195         if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_NONE)
196                 return WPA_CIPHER_NONE;
197         if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_WEP40)
198                 return WPA_CIPHER_WEP40;
199         if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_TKIP)
200                 return WPA_CIPHER_TKIP;
201         if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_CCMP)
202                 return WPA_CIPHER_CCMP;
203         if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_WEP104)
204                 return WPA_CIPHER_WEP104;
205 #ifdef CONFIG_IEEE80211W
206         if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_AES_128_CMAC)
207                 return WPA_CIPHER_AES_128_CMAC;
208 #endif /* CONFIG_IEEE80211W */
209         return 0;
210 }
211
212
213 static int rsn_key_mgmt_to_bitfield(const u8 *s)
214 {
215         if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_UNSPEC_802_1X)
216                 return WPA_KEY_MGMT_IEEE8021X;
217         if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X)
218                 return WPA_KEY_MGMT_PSK;
219 #ifdef CONFIG_IEEE80211R
220         if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_802_1X)
221                 return WPA_KEY_MGMT_FT_IEEE8021X;
222         if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_PSK)
223                 return WPA_KEY_MGMT_FT_PSK;
224 #endif /* CONFIG_IEEE80211R */
225 #ifdef CONFIG_IEEE80211W
226         if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SHA256)
227                 return WPA_KEY_MGMT_IEEE8021X_SHA256;
228         if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_SHA256)
229                 return WPA_KEY_MGMT_PSK_SHA256;
230 #endif /* CONFIG_IEEE80211W */
231         return 0;
232 }
233 #endif /* CONFIG_NO_WPA2 */
234
235
236 /**
237  * wpa_parse_wpa_ie_rsn - Parse RSN IE
238  * @rsn_ie: Buffer containing RSN IE
239  * @rsn_ie_len: RSN IE buffer length (including IE number and length octets)
240  * @data: Pointer to structure that will be filled in with parsed data
241  * Returns: 0 on success, <0 on failure
242  */
243 int wpa_parse_wpa_ie_rsn(const u8 *rsn_ie, size_t rsn_ie_len,
244                          struct wpa_ie_data *data)
245 {
246 #ifndef CONFIG_NO_WPA2
247         const struct rsn_ie_hdr *hdr;
248         const u8 *pos;
249         int left;
250         int i, count;
251
252         os_memset(data, 0, sizeof(*data));
253         data->proto = WPA_PROTO_RSN;
254         data->pairwise_cipher = WPA_CIPHER_CCMP;
255         data->group_cipher = WPA_CIPHER_CCMP;
256         data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
257         data->capabilities = 0;
258         data->pmkid = NULL;
259         data->num_pmkid = 0;
260 #ifdef CONFIG_IEEE80211W
261         data->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
262 #else /* CONFIG_IEEE80211W */
263         data->mgmt_group_cipher = 0;
264 #endif /* CONFIG_IEEE80211W */
265
266         if (rsn_ie_len == 0) {
267                 /* No RSN IE - fail silently */
268                 return -1;
269         }
270
271         if (rsn_ie_len < sizeof(struct rsn_ie_hdr)) {
272                 wpa_printf(MSG_DEBUG, "%s: ie len too short %lu",
273                            __func__, (unsigned long) rsn_ie_len);
274                 return -1;
275         }
276
277         hdr = (const struct rsn_ie_hdr *) rsn_ie;
278
279         if (hdr->elem_id != WLAN_EID_RSN ||
280             hdr->len != rsn_ie_len - 2 ||
281             WPA_GET_LE16(hdr->version) != RSN_VERSION) {
282                 wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version",
283                            __func__);
284                 return -2;
285         }
286
287         pos = (const u8 *) (hdr + 1);
288         left = rsn_ie_len - sizeof(*hdr);
289
290         if (left >= RSN_SELECTOR_LEN) {
291                 data->group_cipher = rsn_selector_to_bitfield(pos);
292 #ifdef CONFIG_IEEE80211W
293                 if (data->group_cipher == WPA_CIPHER_AES_128_CMAC) {
294                         wpa_printf(MSG_DEBUG, "%s: AES-128-CMAC used as group "
295                                    "cipher", __func__);
296                         return -1;
297                 }
298 #endif /* CONFIG_IEEE80211W */
299                 pos += RSN_SELECTOR_LEN;
300                 left -= RSN_SELECTOR_LEN;
301         } else if (left > 0) {
302                 wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much",
303                            __func__, left);
304                 return -3;
305         }
306
307         if (left >= 2) {
308                 data->pairwise_cipher = 0;
309                 count = WPA_GET_LE16(pos);
310                 pos += 2;
311                 left -= 2;
312                 if (count == 0 || left < count * RSN_SELECTOR_LEN) {
313                         wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), "
314                                    "count %u left %u", __func__, count, left);
315                         return -4;
316                 }
317                 for (i = 0; i < count; i++) {
318                         data->pairwise_cipher |= rsn_selector_to_bitfield(pos);
319                         pos += RSN_SELECTOR_LEN;
320                         left -= RSN_SELECTOR_LEN;
321                 }
322 #ifdef CONFIG_IEEE80211W
323                 if (data->pairwise_cipher & WPA_CIPHER_AES_128_CMAC) {
324                         wpa_printf(MSG_DEBUG, "%s: AES-128-CMAC used as "
325                                    "pairwise cipher", __func__);
326                         return -1;
327                 }
328 #endif /* CONFIG_IEEE80211W */
329         } else if (left == 1) {
330                 wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)",
331                            __func__);
332                 return -5;
333         }
334
335         if (left >= 2) {
336                 data->key_mgmt = 0;
337                 count = WPA_GET_LE16(pos);
338                 pos += 2;
339                 left -= 2;
340                 if (count == 0 || left < count * RSN_SELECTOR_LEN) {
341                         wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), "
342                                    "count %u left %u", __func__, count, left);
343                         return -6;
344                 }
345                 for (i = 0; i < count; i++) {
346                         data->key_mgmt |= rsn_key_mgmt_to_bitfield(pos);
347                         pos += RSN_SELECTOR_LEN;
348                         left -= RSN_SELECTOR_LEN;
349                 }
350         } else if (left == 1) {
351                 wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)",
352                            __func__);
353                 return -7;
354         }
355
356         if (left >= 2) {
357                 data->capabilities = WPA_GET_LE16(pos);
358                 pos += 2;
359                 left -= 2;
360         }
361
362         if (left >= 2) {
363                 data->num_pmkid = WPA_GET_LE16(pos);
364                 pos += 2;
365                 left -= 2;
366                 if (left < (int) data->num_pmkid * PMKID_LEN) {
367                         wpa_printf(MSG_DEBUG, "%s: PMKID underflow "
368                                    "(num_pmkid=%lu left=%d)",
369                                    __func__, (unsigned long) data->num_pmkid,
370                                    left);
371                         data->num_pmkid = 0;
372                         return -9;
373                 } else {
374                         data->pmkid = pos;
375                         pos += data->num_pmkid * PMKID_LEN;
376                         left -= data->num_pmkid * PMKID_LEN;
377                 }
378         }
379
380 #ifdef CONFIG_IEEE80211W
381         if (left >= 4) {
382                 data->mgmt_group_cipher = rsn_selector_to_bitfield(pos);
383                 if (data->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC) {
384                         wpa_printf(MSG_DEBUG, "%s: Unsupported management "
385                                    "group cipher 0x%x", __func__,
386                                    data->mgmt_group_cipher);
387                         return -10;
388                 }
389                 pos += RSN_SELECTOR_LEN;
390                 left -= RSN_SELECTOR_LEN;
391         }
392 #endif /* CONFIG_IEEE80211W */
393
394         if (left > 0) {
395                 wpa_printf(MSG_DEBUG, "%s: ie has %u trailing bytes - ignored",
396                            __func__, left);
397         }
398
399         return 0;
400 #else /* CONFIG_NO_WPA2 */
401         return -1;
402 #endif /* CONFIG_NO_WPA2 */
403 }
404
405
406 static int wpa_selector_to_bitfield(const u8 *s)
407 {
408         if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_NONE)
409                 return WPA_CIPHER_NONE;
410         if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_WEP40)
411                 return WPA_CIPHER_WEP40;
412         if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_TKIP)
413                 return WPA_CIPHER_TKIP;
414         if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_CCMP)
415                 return WPA_CIPHER_CCMP;
416         if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_WEP104)
417                 return WPA_CIPHER_WEP104;
418         return 0;
419 }
420
421
422 static int wpa_key_mgmt_to_bitfield(const u8 *s)
423 {
424         if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_UNSPEC_802_1X)
425                 return WPA_KEY_MGMT_IEEE8021X;
426         if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X)
427                 return WPA_KEY_MGMT_PSK;
428         if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_NONE)
429                 return WPA_KEY_MGMT_WPA_NONE;
430         return 0;
431 }
432
433
434 int wpa_parse_wpa_ie_wpa(const u8 *wpa_ie, size_t wpa_ie_len,
435                          struct wpa_ie_data *data)
436 {
437         const struct wpa_ie_hdr *hdr;
438         const u8 *pos;
439         int left;
440         int i, count;
441
442         os_memset(data, 0, sizeof(*data));
443         data->proto = WPA_PROTO_WPA;
444         data->pairwise_cipher = WPA_CIPHER_TKIP;
445         data->group_cipher = WPA_CIPHER_TKIP;
446         data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
447         data->capabilities = 0;
448         data->pmkid = NULL;
449         data->num_pmkid = 0;
450         data->mgmt_group_cipher = 0;
451
452         if (wpa_ie_len == 0) {
453                 /* No WPA IE - fail silently */
454                 return -1;
455         }
456
457         if (wpa_ie_len < sizeof(struct wpa_ie_hdr)) {
458                 wpa_printf(MSG_DEBUG, "%s: ie len too short %lu",
459                            __func__, (unsigned long) wpa_ie_len);
460                 return -1;
461         }
462
463         hdr = (const struct wpa_ie_hdr *) wpa_ie;
464
465         if (hdr->elem_id != WLAN_EID_VENDOR_SPECIFIC ||
466             hdr->len != wpa_ie_len - 2 ||
467             RSN_SELECTOR_GET(hdr->oui) != WPA_OUI_TYPE ||
468             WPA_GET_LE16(hdr->version) != WPA_VERSION) {
469                 wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version",
470                            __func__);
471                 return -2;
472         }
473
474         pos = (const u8 *) (hdr + 1);
475         left = wpa_ie_len - sizeof(*hdr);
476
477         if (left >= WPA_SELECTOR_LEN) {
478                 data->group_cipher = wpa_selector_to_bitfield(pos);
479                 pos += WPA_SELECTOR_LEN;
480                 left -= WPA_SELECTOR_LEN;
481         } else if (left > 0) {
482                 wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much",
483                            __func__, left);
484                 return -3;
485         }
486
487         if (left >= 2) {
488                 data->pairwise_cipher = 0;
489                 count = WPA_GET_LE16(pos);
490                 pos += 2;
491                 left -= 2;
492                 if (count == 0 || left < count * WPA_SELECTOR_LEN) {
493                         wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), "
494                                    "count %u left %u", __func__, count, left);
495                         return -4;
496                 }
497                 for (i = 0; i < count; i++) {
498                         data->pairwise_cipher |= wpa_selector_to_bitfield(pos);
499                         pos += WPA_SELECTOR_LEN;
500                         left -= WPA_SELECTOR_LEN;
501                 }
502         } else if (left == 1) {
503                 wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)",
504                            __func__);
505                 return -5;
506         }
507
508         if (left >= 2) {
509                 data->key_mgmt = 0;
510                 count = WPA_GET_LE16(pos);
511                 pos += 2;
512                 left -= 2;
513                 if (count == 0 || left < count * WPA_SELECTOR_LEN) {
514                         wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), "
515                                    "count %u left %u", __func__, count, left);
516                         return -6;
517                 }
518                 for (i = 0; i < count; i++) {
519                         data->key_mgmt |= wpa_key_mgmt_to_bitfield(pos);
520                         pos += WPA_SELECTOR_LEN;
521                         left -= WPA_SELECTOR_LEN;
522                 }
523         } else if (left == 1) {
524                 wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)",
525                            __func__);
526                 return -7;
527         }
528
529         if (left >= 2) {
530                 data->capabilities = WPA_GET_LE16(pos);
531                 pos += 2;
532                 left -= 2;
533         }
534
535         if (left > 0) {
536                 wpa_printf(MSG_DEBUG, "%s: ie has %u trailing bytes - ignored",
537                            __func__, left);
538         }
539
540         return 0;
541 }
542
543
544 #ifdef CONFIG_IEEE80211R
545
546 /**
547  * wpa_derive_pmk_r0 - Derive PMK-R0 and PMKR0Name
548  *
549  * IEEE Std 802.11r-2008 - 8.5.1.5.3
550  */
551 void wpa_derive_pmk_r0(const u8 *xxkey, size_t xxkey_len,
552                        const u8 *ssid, size_t ssid_len,
553                        const u8 *mdid, const u8 *r0kh_id, size_t r0kh_id_len,
554                        const u8 *s0kh_id, u8 *pmk_r0, u8 *pmk_r0_name)
555 {
556         u8 buf[1 + WPA_MAX_SSID_LEN + MOBILITY_DOMAIN_ID_LEN + 1 +
557                FT_R0KH_ID_MAX_LEN + ETH_ALEN];
558         u8 *pos, r0_key_data[48], hash[32];
559         const u8 *addr[2];
560         size_t len[2];
561
562         /*
563          * R0-Key-Data = KDF-384(XXKey, "FT-R0",
564          *                       SSIDlength || SSID || MDID || R0KHlength ||
565          *                       R0KH-ID || S0KH-ID)
566          * XXKey is either the second 256 bits of MSK or PSK.
567          * PMK-R0 = L(R0-Key-Data, 0, 256)
568          * PMK-R0Name-Salt = L(R0-Key-Data, 256, 128)
569          */
570         if (ssid_len > WPA_MAX_SSID_LEN || r0kh_id_len > FT_R0KH_ID_MAX_LEN)
571                 return;
572         pos = buf;
573         *pos++ = ssid_len;
574         os_memcpy(pos, ssid, ssid_len);
575         pos += ssid_len;
576         os_memcpy(pos, mdid, MOBILITY_DOMAIN_ID_LEN);
577         pos += MOBILITY_DOMAIN_ID_LEN;
578         *pos++ = r0kh_id_len;
579         os_memcpy(pos, r0kh_id, r0kh_id_len);
580         pos += r0kh_id_len;
581         os_memcpy(pos, s0kh_id, ETH_ALEN);
582         pos += ETH_ALEN;
583
584         sha256_prf(xxkey, xxkey_len, "FT-R0", buf, pos - buf,
585                    r0_key_data, sizeof(r0_key_data));
586         os_memcpy(pmk_r0, r0_key_data, PMK_LEN);
587
588         /*
589          * PMKR0Name = Truncate-128(SHA-256("FT-R0N" || PMK-R0Name-Salt)
590          */
591         addr[0] = (const u8 *) "FT-R0N";
592         len[0] = 6;
593         addr[1] = r0_key_data + PMK_LEN;
594         len[1] = 16;
595
596         sha256_vector(2, addr, len, hash);
597         os_memcpy(pmk_r0_name, hash, WPA_PMK_NAME_LEN);
598 }
599
600
601 /**
602  * wpa_derive_pmk_r1_name - Derive PMKR1Name
603  *
604  * IEEE Std 802.11r-2008 - 8.5.1.5.4
605  */
606 void wpa_derive_pmk_r1_name(const u8 *pmk_r0_name, const u8 *r1kh_id,
607                             const u8 *s1kh_id, u8 *pmk_r1_name)
608 {
609         u8 hash[32];
610         const u8 *addr[4];
611         size_t len[4];
612
613         /*
614          * PMKR1Name = Truncate-128(SHA-256("FT-R1N" || PMKR0Name ||
615          *                                  R1KH-ID || S1KH-ID))
616          */
617         addr[0] = (const u8 *) "FT-R1N";
618         len[0] = 6;
619         addr[1] = pmk_r0_name;
620         len[1] = WPA_PMK_NAME_LEN;
621         addr[2] = r1kh_id;
622         len[2] = FT_R1KH_ID_LEN;
623         addr[3] = s1kh_id;
624         len[3] = ETH_ALEN;
625
626         sha256_vector(4, addr, len, hash);
627         os_memcpy(pmk_r1_name, hash, WPA_PMK_NAME_LEN);
628 }
629
630
631 /**
632  * wpa_derive_pmk_r1 - Derive PMK-R1 and PMKR1Name from PMK-R0
633  *
634  * IEEE Std 802.11r-2008 - 8.5.1.5.4
635  */
636 void wpa_derive_pmk_r1(const u8 *pmk_r0, const u8 *pmk_r0_name,
637                        const u8 *r1kh_id, const u8 *s1kh_id,
638                        u8 *pmk_r1, u8 *pmk_r1_name)
639 {
640         u8 buf[FT_R1KH_ID_LEN + ETH_ALEN];
641         u8 *pos;
642
643         /* PMK-R1 = KDF-256(PMK-R0, "FT-R1", R1KH-ID || S1KH-ID) */
644         pos = buf;
645         os_memcpy(pos, r1kh_id, FT_R1KH_ID_LEN);
646         pos += FT_R1KH_ID_LEN;
647         os_memcpy(pos, s1kh_id, ETH_ALEN);
648         pos += ETH_ALEN;
649
650         sha256_prf(pmk_r0, PMK_LEN, "FT-R1", buf, pos - buf, pmk_r1, PMK_LEN);
651
652         wpa_derive_pmk_r1_name(pmk_r0_name, r1kh_id, s1kh_id, pmk_r1_name);
653 }
654
655
656 /**
657  * wpa_pmk_r1_to_ptk - Derive PTK and PTKName from PMK-R1
658  *
659  * IEEE Std 802.11r-2008 - 8.5.1.5.5
660  */
661 void wpa_pmk_r1_to_ptk(const u8 *pmk_r1, const u8 *snonce, const u8 *anonce,
662                        const u8 *sta_addr, const u8 *bssid,
663                        const u8 *pmk_r1_name,
664                        u8 *ptk, size_t ptk_len, u8 *ptk_name)
665 {
666         u8 buf[2 * WPA_NONCE_LEN + 2 * ETH_ALEN];
667         u8 *pos, hash[32];
668         const u8 *addr[6];
669         size_t len[6];
670
671         /*
672          * PTK = KDF-PTKLen(PMK-R1, "FT-PTK", SNonce || ANonce ||
673          *                  BSSID || STA-ADDR)
674          */
675         pos = buf;
676         os_memcpy(pos, snonce, WPA_NONCE_LEN);
677         pos += WPA_NONCE_LEN;
678         os_memcpy(pos, anonce, WPA_NONCE_LEN);
679         pos += WPA_NONCE_LEN;
680         os_memcpy(pos, bssid, ETH_ALEN);
681         pos += ETH_ALEN;
682         os_memcpy(pos, sta_addr, ETH_ALEN);
683         pos += ETH_ALEN;
684
685         sha256_prf(pmk_r1, PMK_LEN, "FT-PTK", buf, pos - buf, ptk, ptk_len);
686
687         /*
688          * PTKName = Truncate-128(SHA-256(PMKR1Name || "FT-PTKN" || SNonce ||
689          *                                ANonce || BSSID || STA-ADDR))
690          */
691         addr[0] = pmk_r1_name;
692         len[0] = WPA_PMK_NAME_LEN;
693         addr[1] = (const u8 *) "FT-PTKN";
694         len[1] = 7;
695         addr[2] = snonce;
696         len[2] = WPA_NONCE_LEN;
697         addr[3] = anonce;
698         len[3] = WPA_NONCE_LEN;
699         addr[4] = bssid;
700         len[4] = ETH_ALEN;
701         addr[5] = sta_addr;
702         len[5] = ETH_ALEN;
703
704         sha256_vector(6, addr, len, hash);
705         os_memcpy(ptk_name, hash, WPA_PMK_NAME_LEN);
706 }
707
708 #endif /* CONFIG_IEEE80211R */
709
710
711 /**
712  * rsn_pmkid - Calculate PMK identifier
713  * @pmk: Pairwise master key
714  * @pmk_len: Length of pmk in bytes
715  * @aa: Authenticator address
716  * @spa: Supplicant address
717  * @pmkid: Buffer for PMKID
718  * @use_sha256: Whether to use SHA256-based KDF
719  *
720  * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
721  * PMKID = HMAC-SHA1-128(PMK, "PMK Name" || AA || SPA)
722  */
723 void rsn_pmkid(const u8 *pmk, size_t pmk_len, const u8 *aa, const u8 *spa,
724                u8 *pmkid, int use_sha256)
725 {
726         char *title = "PMK Name";
727         const u8 *addr[3];
728         const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
729         unsigned char hash[SHA256_MAC_LEN];
730
731         addr[0] = (u8 *) title;
732         addr[1] = aa;
733         addr[2] = spa;
734
735 #ifdef CONFIG_IEEE80211W
736         if (use_sha256)
737                 hmac_sha256_vector(pmk, pmk_len, 3, addr, len, hash);
738         else
739 #endif /* CONFIG_IEEE80211W */
740                 hmac_sha1_vector(pmk, pmk_len, 3, addr, len, hash);
741         os_memcpy(pmkid, hash, PMKID_LEN);
742 }
743
744
745 /**
746  * wpa_cipher_txt - Convert cipher suite to a text string
747  * @cipher: Cipher suite (WPA_CIPHER_* enum)
748  * Returns: Pointer to a text string of the cipher suite name
749  */
750 const char * wpa_cipher_txt(int cipher)
751 {
752         switch (cipher) {
753         case WPA_CIPHER_NONE:
754                 return "NONE";
755         case WPA_CIPHER_WEP40:
756                 return "WEP-40";
757         case WPA_CIPHER_WEP104:
758                 return "WEP-104";
759         case WPA_CIPHER_TKIP:
760                 return "TKIP";
761         case WPA_CIPHER_CCMP:
762                 return "CCMP";
763         case WPA_CIPHER_CCMP | WPA_CIPHER_TKIP:
764                 return "CCMP+TKIP";
765         default:
766                 return "UNKNOWN";
767         }
768 }
769
770
771 /**
772  * wpa_key_mgmt_txt - Convert key management suite to a text string
773  * @key_mgmt: Key management suite (WPA_KEY_MGMT_* enum)
774  * @proto: WPA/WPA2 version (WPA_PROTO_*)
775  * Returns: Pointer to a text string of the key management suite name
776  */
777 const char * wpa_key_mgmt_txt(int key_mgmt, int proto)
778 {
779         switch (key_mgmt) {
780         case WPA_KEY_MGMT_IEEE8021X:
781                 if (proto == (WPA_PROTO_RSN | WPA_PROTO_WPA))
782                         return "WPA2+WPA/IEEE 802.1X/EAP";
783                 return proto == WPA_PROTO_RSN ?
784                         "WPA2/IEEE 802.1X/EAP" : "WPA/IEEE 802.1X/EAP";
785         case WPA_KEY_MGMT_PSK:
786                 if (proto == (WPA_PROTO_RSN | WPA_PROTO_WPA))
787                         return "WPA2-PSK+WPA-PSK";
788                 return proto == WPA_PROTO_RSN ?
789                         "WPA2-PSK" : "WPA-PSK";
790         case WPA_KEY_MGMT_NONE:
791                 return "NONE";
792         case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
793                 return "IEEE 802.1X (no WPA)";
794 #ifdef CONFIG_IEEE80211R
795         case WPA_KEY_MGMT_FT_IEEE8021X:
796                 return "FT-EAP";
797         case WPA_KEY_MGMT_FT_PSK:
798                 return "FT-PSK";
799 #endif /* CONFIG_IEEE80211R */
800 #ifdef CONFIG_IEEE80211W
801         case WPA_KEY_MGMT_IEEE8021X_SHA256:
802                 return "WPA2-EAP-SHA256";
803         case WPA_KEY_MGMT_PSK_SHA256:
804                 return "WPA2-PSK-SHA256";
805 #endif /* CONFIG_IEEE80211W */
806         default:
807                 return "UNKNOWN";
808         }
809 }
810
811
812 int wpa_compare_rsn_ie(int ft_initial_assoc,
813                        const u8 *ie1, size_t ie1len,
814                        const u8 *ie2, size_t ie2len)
815 {
816         if (ie1 == NULL || ie2 == NULL)
817                 return -1;
818
819         if (ie1len == ie2len && os_memcmp(ie1, ie2, ie1len) == 0)
820                 return 0; /* identical IEs */
821
822 #ifdef CONFIG_IEEE80211R
823         if (ft_initial_assoc) {
824                 struct wpa_ie_data ie1d, ie2d;
825                 /*
826                  * The PMKID-List in RSN IE is different between Beacon/Probe
827                  * Response/(Re)Association Request frames and EAPOL-Key
828                  * messages in FT initial mobility domain association. Allow
829                  * for this, but verify that other parts of the RSN IEs are
830                  * identical.
831                  */
832                 if (wpa_parse_wpa_ie_rsn(ie1, ie1len, &ie1d) < 0 ||
833                     wpa_parse_wpa_ie_rsn(ie2, ie2len, &ie2d) < 0)
834                         return -1;
835                 if (ie1d.proto == ie2d.proto &&
836                     ie1d.pairwise_cipher == ie2d.pairwise_cipher &&
837                     ie1d.group_cipher == ie2d.group_cipher &&
838                     ie1d.key_mgmt == ie2d.key_mgmt &&
839                     ie1d.capabilities == ie2d.capabilities &&
840                     ie1d.mgmt_group_cipher == ie2d.mgmt_group_cipher)
841                         return 0;
842         }
843 #endif /* CONFIG_IEEE80211R */
844
845         return -1;
846 }
847
848
849 #ifdef CONFIG_IEEE80211R
850 int wpa_insert_pmkid(u8 *ies, size_t ies_len, const u8 *pmkid)
851 {
852         u8 *start, *end, *rpos, *rend;
853         int added = 0;
854
855         start = ies;
856         end = ies + ies_len;
857
858         while (start < end) {
859                 if (*start == WLAN_EID_RSN)
860                         break;
861                 start += 2 + start[1];
862         }
863         if (start >= end) {
864                 wpa_printf(MSG_ERROR, "FT: Could not find RSN IE in "
865                            "IEs data");
866                 return -1;
867         }
868         wpa_hexdump(MSG_DEBUG, "FT: RSN IE before modification",
869                     start, 2 + start[1]);
870
871         /* Find start of PMKID-Count */
872         rpos = start + 2;
873         rend = rpos + start[1];
874
875         /* Skip Version and Group Data Cipher Suite */
876         rpos += 2 + 4;
877         /* Skip Pairwise Cipher Suite Count and List */
878         rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN;
879         /* Skip AKM Suite Count and List */
880         rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN;
881
882         if (rpos == rend) {
883                 /* Add RSN Capabilities */
884                 os_memmove(rpos + 2, rpos, end - rpos);
885                 *rpos++ = 0;
886                 *rpos++ = 0;
887         } else {
888                 /* Skip RSN Capabilities */
889                 rpos += 2;
890                 if (rpos > rend) {
891                         wpa_printf(MSG_ERROR, "FT: Could not parse RSN IE in "
892                                    "IEs data");
893                         return -1;
894                 }
895         }
896
897         if (rpos == rend) {
898                 /* No PMKID-Count field included; add it */
899                 os_memmove(rpos + 2 + PMKID_LEN, rpos, end - rpos);
900                 WPA_PUT_LE16(rpos, 1);
901                 rpos += 2;
902                 os_memcpy(rpos, pmkid, PMKID_LEN);
903                 added += 2 + PMKID_LEN;
904                 start[1] += 2 + PMKID_LEN;
905         } else {
906                 /* PMKID-Count was included; use it */
907                 if (WPA_GET_LE16(rpos) != 0) {
908                         wpa_printf(MSG_ERROR, "FT: Unexpected PMKID "
909                                    "in RSN IE in EAPOL-Key data");
910                         return -1;
911                 }
912                 WPA_PUT_LE16(rpos, 1);
913                 rpos += 2;
914                 os_memmove(rpos + PMKID_LEN, rpos, end - rpos);
915                 os_memcpy(rpos, pmkid, PMKID_LEN);
916                 added += PMKID_LEN;
917                 start[1] += PMKID_LEN;
918         }
919
920         wpa_hexdump(MSG_DEBUG, "FT: RSN IE after modification "
921                     "(PMKID inserted)", start, 2 + start[1]);
922
923         return added;
924 }
925 #endif /* CONFIG_IEEE80211R */