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