blob: c9d0ccb7ef6312bcf0ae227509ff38fae821d90e [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA/RSN - Shared functions for supplicant and authenticator
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
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) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070046#ifndef CONFIG_FIPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070047 case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4:
48 return hmac_md5(key, 16, buf, len, mic);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070049#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050 case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES:
51 if (hmac_sha1(key, 16, buf, len, hash))
52 return -1;
53 os_memcpy(mic, hash, MD5_MAC_LEN);
54 break;
55#if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
56 case WPA_KEY_INFO_TYPE_AES_128_CMAC:
57 return omac1_aes_128(key, buf, len, mic);
58#endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080059#ifdef CONFIG_HS20
60 case WPA_KEY_INFO_TYPE_AKM_DEFINED:
61 /* FIX: This should be based on negotiated AKM */
62 return omac1_aes_128(key, buf, len, mic);
63#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064 default:
65 return -1;
66 }
67
68 return 0;
69}
70
71
72/**
73 * wpa_pmk_to_ptk - Calculate PTK from PMK, addresses, and nonces
74 * @pmk: Pairwise master key
75 * @pmk_len: Length of PMK
76 * @label: Label to use in derivation
77 * @addr1: AA or SA
78 * @addr2: SA or AA
79 * @nonce1: ANonce or SNonce
80 * @nonce2: SNonce or ANonce
81 * @ptk: Buffer for pairwise transient key
82 * @ptk_len: Length of PTK
83 * @use_sha256: Whether to use SHA256-based KDF
84 *
85 * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
86 * PTK = PRF-X(PMK, "Pairwise key expansion",
87 * Min(AA, SA) || Max(AA, SA) ||
88 * Min(ANonce, SNonce) || Max(ANonce, SNonce))
89 *
90 * STK = PRF-X(SMK, "Peer key expansion",
91 * Min(MAC_I, MAC_P) || Max(MAC_I, MAC_P) ||
92 * Min(INonce, PNonce) || Max(INonce, PNonce))
93 */
94void wpa_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const char *label,
95 const u8 *addr1, const u8 *addr2,
96 const u8 *nonce1, const u8 *nonce2,
97 u8 *ptk, size_t ptk_len, int use_sha256)
98{
99 u8 data[2 * ETH_ALEN + 2 * WPA_NONCE_LEN];
100
101 if (os_memcmp(addr1, addr2, ETH_ALEN) < 0) {
102 os_memcpy(data, addr1, ETH_ALEN);
103 os_memcpy(data + ETH_ALEN, addr2, ETH_ALEN);
104 } else {
105 os_memcpy(data, addr2, ETH_ALEN);
106 os_memcpy(data + ETH_ALEN, addr1, ETH_ALEN);
107 }
108
109 if (os_memcmp(nonce1, nonce2, WPA_NONCE_LEN) < 0) {
110 os_memcpy(data + 2 * ETH_ALEN, nonce1, WPA_NONCE_LEN);
111 os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce2,
112 WPA_NONCE_LEN);
113 } else {
114 os_memcpy(data + 2 * ETH_ALEN, nonce2, WPA_NONCE_LEN);
115 os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce1,
116 WPA_NONCE_LEN);
117 }
118
119#ifdef CONFIG_IEEE80211W
120 if (use_sha256)
121 sha256_prf(pmk, pmk_len, label, data, sizeof(data),
122 ptk, ptk_len);
123 else
124#endif /* CONFIG_IEEE80211W */
125 sha1_prf(pmk, pmk_len, label, data, sizeof(data), ptk,
126 ptk_len);
127
128 wpa_printf(MSG_DEBUG, "WPA: PTK derivation - A1=" MACSTR " A2=" MACSTR,
129 MAC2STR(addr1), MAC2STR(addr2));
130 wpa_hexdump(MSG_DEBUG, "WPA: Nonce1", nonce1, WPA_NONCE_LEN);
131 wpa_hexdump(MSG_DEBUG, "WPA: Nonce2", nonce2, WPA_NONCE_LEN);
132 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK", pmk, pmk_len);
133 wpa_hexdump_key(MSG_DEBUG, "WPA: PTK", ptk, ptk_len);
134}
135
136
137#ifdef CONFIG_IEEE80211R
138int wpa_ft_mic(const u8 *kck, const u8 *sta_addr, const u8 *ap_addr,
139 u8 transaction_seqnum, const u8 *mdie, size_t mdie_len,
140 const u8 *ftie, size_t ftie_len,
141 const u8 *rsnie, size_t rsnie_len,
142 const u8 *ric, size_t ric_len, u8 *mic)
143{
144 u8 *buf, *pos;
145 size_t buf_len;
146
147 buf_len = 2 * ETH_ALEN + 1 + mdie_len + ftie_len + rsnie_len + ric_len;
148 buf = os_malloc(buf_len);
149 if (buf == NULL)
150 return -1;
151
152 pos = buf;
153 os_memcpy(pos, sta_addr, ETH_ALEN);
154 pos += ETH_ALEN;
155 os_memcpy(pos, ap_addr, ETH_ALEN);
156 pos += ETH_ALEN;
157 *pos++ = transaction_seqnum;
158 if (rsnie) {
159 os_memcpy(pos, rsnie, rsnie_len);
160 pos += rsnie_len;
161 }
162 if (mdie) {
163 os_memcpy(pos, mdie, mdie_len);
164 pos += mdie_len;
165 }
166 if (ftie) {
167 struct rsn_ftie *_ftie;
168 os_memcpy(pos, ftie, ftie_len);
169 if (ftie_len < 2 + sizeof(*_ftie)) {
170 os_free(buf);
171 return -1;
172 }
173 _ftie = (struct rsn_ftie *) (pos + 2);
174 os_memset(_ftie->mic, 0, sizeof(_ftie->mic));
175 pos += ftie_len;
176 }
177 if (ric) {
178 os_memcpy(pos, ric, ric_len);
179 pos += ric_len;
180 }
181
182 wpa_hexdump(MSG_MSGDUMP, "FT: MIC data", buf, pos - buf);
183 if (omac1_aes_128(kck, buf, pos - buf, mic)) {
184 os_free(buf);
185 return -1;
186 }
187
188 os_free(buf);
189
190 return 0;
191}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800192
193
194static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len,
195 struct wpa_ft_ies *parse)
196{
197 const u8 *end, *pos;
198
199 parse->ftie = ie;
200 parse->ftie_len = ie_len;
201
202 pos = ie + sizeof(struct rsn_ftie);
203 end = ie + ie_len;
204
205 while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
206 switch (pos[0]) {
207 case FTIE_SUBELEM_R1KH_ID:
208 if (pos[1] != FT_R1KH_ID_LEN) {
209 wpa_printf(MSG_DEBUG, "FT: Invalid R1KH-ID "
210 "length in FTIE: %d", pos[1]);
211 return -1;
212 }
213 parse->r1kh_id = pos + 2;
214 break;
215 case FTIE_SUBELEM_GTK:
216 parse->gtk = pos + 2;
217 parse->gtk_len = pos[1];
218 break;
219 case FTIE_SUBELEM_R0KH_ID:
220 if (pos[1] < 1 || pos[1] > FT_R0KH_ID_MAX_LEN) {
221 wpa_printf(MSG_DEBUG, "FT: Invalid R0KH-ID "
222 "length in FTIE: %d", pos[1]);
223 return -1;
224 }
225 parse->r0kh_id = pos + 2;
226 parse->r0kh_id_len = pos[1];
227 break;
228#ifdef CONFIG_IEEE80211W
229 case FTIE_SUBELEM_IGTK:
230 parse->igtk = pos + 2;
231 parse->igtk_len = pos[1];
232 break;
233#endif /* CONFIG_IEEE80211W */
234 }
235
236 pos += 2 + pos[1];
237 }
238
239 return 0;
240}
241
242
243int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
244 struct wpa_ft_ies *parse)
245{
246 const u8 *end, *pos;
247 struct wpa_ie_data data;
248 int ret;
249 const struct rsn_ftie *ftie;
250 int prot_ie_count = 0;
251
252 os_memset(parse, 0, sizeof(*parse));
253 if (ies == NULL)
254 return 0;
255
256 pos = ies;
257 end = ies + ies_len;
258 while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
259 switch (pos[0]) {
260 case WLAN_EID_RSN:
261 parse->rsn = pos + 2;
262 parse->rsn_len = pos[1];
263 ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2,
264 parse->rsn_len + 2,
265 &data);
266 if (ret < 0) {
267 wpa_printf(MSG_DEBUG, "FT: Failed to parse "
268 "RSN IE: %d", ret);
269 return -1;
270 }
271 if (data.num_pmkid == 1 && data.pmkid)
272 parse->rsn_pmkid = data.pmkid;
273 break;
274 case WLAN_EID_MOBILITY_DOMAIN:
275 parse->mdie = pos + 2;
276 parse->mdie_len = pos[1];
277 break;
278 case WLAN_EID_FAST_BSS_TRANSITION:
279 if (pos[1] < sizeof(*ftie))
280 return -1;
281 ftie = (const struct rsn_ftie *) (pos + 2);
282 prot_ie_count = ftie->mic_control[1];
283 if (wpa_ft_parse_ftie(pos + 2, pos[1], parse) < 0)
284 return -1;
285 break;
286 case WLAN_EID_TIMEOUT_INTERVAL:
287 parse->tie = pos + 2;
288 parse->tie_len = pos[1];
289 break;
290 case WLAN_EID_RIC_DATA:
291 if (parse->ric == NULL)
292 parse->ric = pos;
293 break;
294 }
295
296 pos += 2 + pos[1];
297 }
298
299 if (prot_ie_count == 0)
300 return 0; /* no MIC */
301
302 /*
303 * Check that the protected IE count matches with IEs included in the
304 * frame.
305 */
306 if (parse->rsn)
307 prot_ie_count--;
308 if (parse->mdie)
309 prot_ie_count--;
310 if (parse->ftie)
311 prot_ie_count--;
312 if (prot_ie_count < 0) {
313 wpa_printf(MSG_DEBUG, "FT: Some required IEs not included in "
314 "the protected IE count");
315 return -1;
316 }
317
318 if (prot_ie_count == 0 && parse->ric) {
319 wpa_printf(MSG_DEBUG, "FT: RIC IE(s) in the frame, but not "
320 "included in protected IE count");
321 return -1;
322 }
323
324 /* Determine the end of the RIC IE(s) */
325 pos = parse->ric;
326 while (pos && pos + 2 <= end && pos + 2 + pos[1] <= end &&
327 prot_ie_count) {
328 prot_ie_count--;
329 pos += 2 + pos[1];
330 }
331 parse->ric_len = pos - parse->ric;
332 if (prot_ie_count) {
333 wpa_printf(MSG_DEBUG, "FT: %d protected IEs missing from "
334 "frame", (int) prot_ie_count);
335 return -1;
336 }
337
338 return 0;
339}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340#endif /* CONFIG_IEEE80211R */
341
342
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343static int rsn_selector_to_bitfield(const u8 *s)
344{
345 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_NONE)
346 return WPA_CIPHER_NONE;
347 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_WEP40)
348 return WPA_CIPHER_WEP40;
349 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_TKIP)
350 return WPA_CIPHER_TKIP;
351 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_CCMP)
352 return WPA_CIPHER_CCMP;
353 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_WEP104)
354 return WPA_CIPHER_WEP104;
355#ifdef CONFIG_IEEE80211W
356 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_AES_128_CMAC)
357 return WPA_CIPHER_AES_128_CMAC;
358#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700359 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_GCMP)
360 return WPA_CIPHER_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800361 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_CCMP_256)
362 return WPA_CIPHER_CCMP_256;
363 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_GCMP_256)
364 return WPA_CIPHER_GCMP_256;
365 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_GMAC_128)
366 return WPA_CIPHER_BIP_GMAC_128;
367 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_GMAC_256)
368 return WPA_CIPHER_BIP_GMAC_256;
369 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_CMAC_256)
370 return WPA_CIPHER_BIP_CMAC_256;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371 return 0;
372}
373
374
375static int rsn_key_mgmt_to_bitfield(const u8 *s)
376{
377 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_UNSPEC_802_1X)
378 return WPA_KEY_MGMT_IEEE8021X;
379 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X)
380 return WPA_KEY_MGMT_PSK;
381#ifdef CONFIG_IEEE80211R
382 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_802_1X)
383 return WPA_KEY_MGMT_FT_IEEE8021X;
384 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_PSK)
385 return WPA_KEY_MGMT_FT_PSK;
386#endif /* CONFIG_IEEE80211R */
387#ifdef CONFIG_IEEE80211W
388 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SHA256)
389 return WPA_KEY_MGMT_IEEE8021X_SHA256;
390 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_SHA256)
391 return WPA_KEY_MGMT_PSK_SHA256;
392#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800393#ifdef CONFIG_SAE
394 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_SAE)
395 return WPA_KEY_MGMT_SAE;
396 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_SAE)
397 return WPA_KEY_MGMT_FT_SAE;
398#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399 return 0;
400}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700401
402
403/**
404 * wpa_parse_wpa_ie_rsn - Parse RSN IE
405 * @rsn_ie: Buffer containing RSN IE
406 * @rsn_ie_len: RSN IE buffer length (including IE number and length octets)
407 * @data: Pointer to structure that will be filled in with parsed data
408 * Returns: 0 on success, <0 on failure
409 */
410int wpa_parse_wpa_ie_rsn(const u8 *rsn_ie, size_t rsn_ie_len,
411 struct wpa_ie_data *data)
412{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413 const struct rsn_ie_hdr *hdr;
414 const u8 *pos;
415 int left;
416 int i, count;
417
418 os_memset(data, 0, sizeof(*data));
419 data->proto = WPA_PROTO_RSN;
420 data->pairwise_cipher = WPA_CIPHER_CCMP;
421 data->group_cipher = WPA_CIPHER_CCMP;
422 data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
423 data->capabilities = 0;
424 data->pmkid = NULL;
425 data->num_pmkid = 0;
426#ifdef CONFIG_IEEE80211W
427 data->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
428#else /* CONFIG_IEEE80211W */
429 data->mgmt_group_cipher = 0;
430#endif /* CONFIG_IEEE80211W */
431
432 if (rsn_ie_len == 0) {
433 /* No RSN IE - fail silently */
434 return -1;
435 }
436
437 if (rsn_ie_len < sizeof(struct rsn_ie_hdr)) {
438 wpa_printf(MSG_DEBUG, "%s: ie len too short %lu",
439 __func__, (unsigned long) rsn_ie_len);
440 return -1;
441 }
442
443 hdr = (const struct rsn_ie_hdr *) rsn_ie;
444
445 if (hdr->elem_id != WLAN_EID_RSN ||
446 hdr->len != rsn_ie_len - 2 ||
447 WPA_GET_LE16(hdr->version) != RSN_VERSION) {
448 wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version",
449 __func__);
450 return -2;
451 }
452
453 pos = (const u8 *) (hdr + 1);
454 left = rsn_ie_len - sizeof(*hdr);
455
456 if (left >= RSN_SELECTOR_LEN) {
457 data->group_cipher = rsn_selector_to_bitfield(pos);
458#ifdef CONFIG_IEEE80211W
459 if (data->group_cipher == WPA_CIPHER_AES_128_CMAC) {
460 wpa_printf(MSG_DEBUG, "%s: AES-128-CMAC used as group "
461 "cipher", __func__);
462 return -1;
463 }
464#endif /* CONFIG_IEEE80211W */
465 pos += RSN_SELECTOR_LEN;
466 left -= RSN_SELECTOR_LEN;
467 } else if (left > 0) {
468 wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much",
469 __func__, left);
470 return -3;
471 }
472
473 if (left >= 2) {
474 data->pairwise_cipher = 0;
475 count = WPA_GET_LE16(pos);
476 pos += 2;
477 left -= 2;
478 if (count == 0 || left < count * RSN_SELECTOR_LEN) {
479 wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), "
480 "count %u left %u", __func__, count, left);
481 return -4;
482 }
483 for (i = 0; i < count; i++) {
484 data->pairwise_cipher |= rsn_selector_to_bitfield(pos);
485 pos += RSN_SELECTOR_LEN;
486 left -= RSN_SELECTOR_LEN;
487 }
488#ifdef CONFIG_IEEE80211W
489 if (data->pairwise_cipher & WPA_CIPHER_AES_128_CMAC) {
490 wpa_printf(MSG_DEBUG, "%s: AES-128-CMAC used as "
491 "pairwise cipher", __func__);
492 return -1;
493 }
494#endif /* CONFIG_IEEE80211W */
495 } else if (left == 1) {
496 wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)",
497 __func__);
498 return -5;
499 }
500
501 if (left >= 2) {
502 data->key_mgmt = 0;
503 count = WPA_GET_LE16(pos);
504 pos += 2;
505 left -= 2;
506 if (count == 0 || left < count * RSN_SELECTOR_LEN) {
507 wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), "
508 "count %u left %u", __func__, count, left);
509 return -6;
510 }
511 for (i = 0; i < count; i++) {
512 data->key_mgmt |= rsn_key_mgmt_to_bitfield(pos);
513 pos += RSN_SELECTOR_LEN;
514 left -= RSN_SELECTOR_LEN;
515 }
516 } else if (left == 1) {
517 wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)",
518 __func__);
519 return -7;
520 }
521
522 if (left >= 2) {
523 data->capabilities = WPA_GET_LE16(pos);
524 pos += 2;
525 left -= 2;
526 }
527
528 if (left >= 2) {
529 data->num_pmkid = WPA_GET_LE16(pos);
530 pos += 2;
531 left -= 2;
532 if (left < (int) data->num_pmkid * PMKID_LEN) {
533 wpa_printf(MSG_DEBUG, "%s: PMKID underflow "
534 "(num_pmkid=%lu left=%d)",
535 __func__, (unsigned long) data->num_pmkid,
536 left);
537 data->num_pmkid = 0;
538 return -9;
539 } else {
540 data->pmkid = pos;
541 pos += data->num_pmkid * PMKID_LEN;
542 left -= data->num_pmkid * PMKID_LEN;
543 }
544 }
545
546#ifdef CONFIG_IEEE80211W
547 if (left >= 4) {
548 data->mgmt_group_cipher = rsn_selector_to_bitfield(pos);
549 if (data->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC) {
550 wpa_printf(MSG_DEBUG, "%s: Unsupported management "
551 "group cipher 0x%x", __func__,
552 data->mgmt_group_cipher);
553 return -10;
554 }
555 pos += RSN_SELECTOR_LEN;
556 left -= RSN_SELECTOR_LEN;
557 }
558#endif /* CONFIG_IEEE80211W */
559
560 if (left > 0) {
561 wpa_printf(MSG_DEBUG, "%s: ie has %u trailing bytes - ignored",
562 __func__, left);
563 }
564
565 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700566}
567
568
569static int wpa_selector_to_bitfield(const u8 *s)
570{
571 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_NONE)
572 return WPA_CIPHER_NONE;
573 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_WEP40)
574 return WPA_CIPHER_WEP40;
575 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_TKIP)
576 return WPA_CIPHER_TKIP;
577 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_CCMP)
578 return WPA_CIPHER_CCMP;
579 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_WEP104)
580 return WPA_CIPHER_WEP104;
581 return 0;
582}
583
584
585static int wpa_key_mgmt_to_bitfield(const u8 *s)
586{
587 if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_UNSPEC_802_1X)
588 return WPA_KEY_MGMT_IEEE8021X;
589 if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X)
590 return WPA_KEY_MGMT_PSK;
591 if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_NONE)
592 return WPA_KEY_MGMT_WPA_NONE;
593 return 0;
594}
595
596
597int wpa_parse_wpa_ie_wpa(const u8 *wpa_ie, size_t wpa_ie_len,
598 struct wpa_ie_data *data)
599{
600 const struct wpa_ie_hdr *hdr;
601 const u8 *pos;
602 int left;
603 int i, count;
604
605 os_memset(data, 0, sizeof(*data));
606 data->proto = WPA_PROTO_WPA;
607 data->pairwise_cipher = WPA_CIPHER_TKIP;
608 data->group_cipher = WPA_CIPHER_TKIP;
609 data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
610 data->capabilities = 0;
611 data->pmkid = NULL;
612 data->num_pmkid = 0;
613 data->mgmt_group_cipher = 0;
614
615 if (wpa_ie_len == 0) {
616 /* No WPA IE - fail silently */
617 return -1;
618 }
619
620 if (wpa_ie_len < sizeof(struct wpa_ie_hdr)) {
621 wpa_printf(MSG_DEBUG, "%s: ie len too short %lu",
622 __func__, (unsigned long) wpa_ie_len);
623 return -1;
624 }
625
626 hdr = (const struct wpa_ie_hdr *) wpa_ie;
627
628 if (hdr->elem_id != WLAN_EID_VENDOR_SPECIFIC ||
629 hdr->len != wpa_ie_len - 2 ||
630 RSN_SELECTOR_GET(hdr->oui) != WPA_OUI_TYPE ||
631 WPA_GET_LE16(hdr->version) != WPA_VERSION) {
632 wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version",
633 __func__);
634 return -2;
635 }
636
637 pos = (const u8 *) (hdr + 1);
638 left = wpa_ie_len - sizeof(*hdr);
639
640 if (left >= WPA_SELECTOR_LEN) {
641 data->group_cipher = wpa_selector_to_bitfield(pos);
642 pos += WPA_SELECTOR_LEN;
643 left -= WPA_SELECTOR_LEN;
644 } else if (left > 0) {
645 wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much",
646 __func__, left);
647 return -3;
648 }
649
650 if (left >= 2) {
651 data->pairwise_cipher = 0;
652 count = WPA_GET_LE16(pos);
653 pos += 2;
654 left -= 2;
655 if (count == 0 || left < count * WPA_SELECTOR_LEN) {
656 wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), "
657 "count %u left %u", __func__, count, left);
658 return -4;
659 }
660 for (i = 0; i < count; i++) {
661 data->pairwise_cipher |= wpa_selector_to_bitfield(pos);
662 pos += WPA_SELECTOR_LEN;
663 left -= WPA_SELECTOR_LEN;
664 }
665 } else if (left == 1) {
666 wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)",
667 __func__);
668 return -5;
669 }
670
671 if (left >= 2) {
672 data->key_mgmt = 0;
673 count = WPA_GET_LE16(pos);
674 pos += 2;
675 left -= 2;
676 if (count == 0 || left < count * WPA_SELECTOR_LEN) {
677 wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), "
678 "count %u left %u", __func__, count, left);
679 return -6;
680 }
681 for (i = 0; i < count; i++) {
682 data->key_mgmt |= wpa_key_mgmt_to_bitfield(pos);
683 pos += WPA_SELECTOR_LEN;
684 left -= WPA_SELECTOR_LEN;
685 }
686 } else if (left == 1) {
687 wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)",
688 __func__);
689 return -7;
690 }
691
692 if (left >= 2) {
693 data->capabilities = WPA_GET_LE16(pos);
694 pos += 2;
695 left -= 2;
696 }
697
698 if (left > 0) {
699 wpa_printf(MSG_DEBUG, "%s: ie has %u trailing bytes - ignored",
700 __func__, left);
701 }
702
703 return 0;
704}
705
706
707#ifdef CONFIG_IEEE80211R
708
709/**
710 * wpa_derive_pmk_r0 - Derive PMK-R0 and PMKR0Name
711 *
712 * IEEE Std 802.11r-2008 - 8.5.1.5.3
713 */
714void wpa_derive_pmk_r0(const u8 *xxkey, size_t xxkey_len,
715 const u8 *ssid, size_t ssid_len,
716 const u8 *mdid, const u8 *r0kh_id, size_t r0kh_id_len,
717 const u8 *s0kh_id, u8 *pmk_r0, u8 *pmk_r0_name)
718{
719 u8 buf[1 + WPA_MAX_SSID_LEN + MOBILITY_DOMAIN_ID_LEN + 1 +
720 FT_R0KH_ID_MAX_LEN + ETH_ALEN];
721 u8 *pos, r0_key_data[48], hash[32];
722 const u8 *addr[2];
723 size_t len[2];
724
725 /*
726 * R0-Key-Data = KDF-384(XXKey, "FT-R0",
727 * SSIDlength || SSID || MDID || R0KHlength ||
728 * R0KH-ID || S0KH-ID)
729 * XXKey is either the second 256 bits of MSK or PSK.
730 * PMK-R0 = L(R0-Key-Data, 0, 256)
731 * PMK-R0Name-Salt = L(R0-Key-Data, 256, 128)
732 */
733 if (ssid_len > WPA_MAX_SSID_LEN || r0kh_id_len > FT_R0KH_ID_MAX_LEN)
734 return;
735 pos = buf;
736 *pos++ = ssid_len;
737 os_memcpy(pos, ssid, ssid_len);
738 pos += ssid_len;
739 os_memcpy(pos, mdid, MOBILITY_DOMAIN_ID_LEN);
740 pos += MOBILITY_DOMAIN_ID_LEN;
741 *pos++ = r0kh_id_len;
742 os_memcpy(pos, r0kh_id, r0kh_id_len);
743 pos += r0kh_id_len;
744 os_memcpy(pos, s0kh_id, ETH_ALEN);
745 pos += ETH_ALEN;
746
747 sha256_prf(xxkey, xxkey_len, "FT-R0", buf, pos - buf,
748 r0_key_data, sizeof(r0_key_data));
749 os_memcpy(pmk_r0, r0_key_data, PMK_LEN);
750
751 /*
752 * PMKR0Name = Truncate-128(SHA-256("FT-R0N" || PMK-R0Name-Salt)
753 */
754 addr[0] = (const u8 *) "FT-R0N";
755 len[0] = 6;
756 addr[1] = r0_key_data + PMK_LEN;
757 len[1] = 16;
758
759 sha256_vector(2, addr, len, hash);
760 os_memcpy(pmk_r0_name, hash, WPA_PMK_NAME_LEN);
761}
762
763
764/**
765 * wpa_derive_pmk_r1_name - Derive PMKR1Name
766 *
767 * IEEE Std 802.11r-2008 - 8.5.1.5.4
768 */
769void wpa_derive_pmk_r1_name(const u8 *pmk_r0_name, const u8 *r1kh_id,
770 const u8 *s1kh_id, u8 *pmk_r1_name)
771{
772 u8 hash[32];
773 const u8 *addr[4];
774 size_t len[4];
775
776 /*
777 * PMKR1Name = Truncate-128(SHA-256("FT-R1N" || PMKR0Name ||
778 * R1KH-ID || S1KH-ID))
779 */
780 addr[0] = (const u8 *) "FT-R1N";
781 len[0] = 6;
782 addr[1] = pmk_r0_name;
783 len[1] = WPA_PMK_NAME_LEN;
784 addr[2] = r1kh_id;
785 len[2] = FT_R1KH_ID_LEN;
786 addr[3] = s1kh_id;
787 len[3] = ETH_ALEN;
788
789 sha256_vector(4, addr, len, hash);
790 os_memcpy(pmk_r1_name, hash, WPA_PMK_NAME_LEN);
791}
792
793
794/**
795 * wpa_derive_pmk_r1 - Derive PMK-R1 and PMKR1Name from PMK-R0
796 *
797 * IEEE Std 802.11r-2008 - 8.5.1.5.4
798 */
799void wpa_derive_pmk_r1(const u8 *pmk_r0, const u8 *pmk_r0_name,
800 const u8 *r1kh_id, const u8 *s1kh_id,
801 u8 *pmk_r1, u8 *pmk_r1_name)
802{
803 u8 buf[FT_R1KH_ID_LEN + ETH_ALEN];
804 u8 *pos;
805
806 /* PMK-R1 = KDF-256(PMK-R0, "FT-R1", R1KH-ID || S1KH-ID) */
807 pos = buf;
808 os_memcpy(pos, r1kh_id, FT_R1KH_ID_LEN);
809 pos += FT_R1KH_ID_LEN;
810 os_memcpy(pos, s1kh_id, ETH_ALEN);
811 pos += ETH_ALEN;
812
813 sha256_prf(pmk_r0, PMK_LEN, "FT-R1", buf, pos - buf, pmk_r1, PMK_LEN);
814
815 wpa_derive_pmk_r1_name(pmk_r0_name, r1kh_id, s1kh_id, pmk_r1_name);
816}
817
818
819/**
820 * wpa_pmk_r1_to_ptk - Derive PTK and PTKName from PMK-R1
821 *
822 * IEEE Std 802.11r-2008 - 8.5.1.5.5
823 */
824void wpa_pmk_r1_to_ptk(const u8 *pmk_r1, const u8 *snonce, const u8 *anonce,
825 const u8 *sta_addr, const u8 *bssid,
826 const u8 *pmk_r1_name,
827 u8 *ptk, size_t ptk_len, u8 *ptk_name)
828{
829 u8 buf[2 * WPA_NONCE_LEN + 2 * ETH_ALEN];
830 u8 *pos, hash[32];
831 const u8 *addr[6];
832 size_t len[6];
833
834 /*
835 * PTK = KDF-PTKLen(PMK-R1, "FT-PTK", SNonce || ANonce ||
836 * BSSID || STA-ADDR)
837 */
838 pos = buf;
839 os_memcpy(pos, snonce, WPA_NONCE_LEN);
840 pos += WPA_NONCE_LEN;
841 os_memcpy(pos, anonce, WPA_NONCE_LEN);
842 pos += WPA_NONCE_LEN;
843 os_memcpy(pos, bssid, ETH_ALEN);
844 pos += ETH_ALEN;
845 os_memcpy(pos, sta_addr, ETH_ALEN);
846 pos += ETH_ALEN;
847
848 sha256_prf(pmk_r1, PMK_LEN, "FT-PTK", buf, pos - buf, ptk, ptk_len);
849
850 /*
851 * PTKName = Truncate-128(SHA-256(PMKR1Name || "FT-PTKN" || SNonce ||
852 * ANonce || BSSID || STA-ADDR))
853 */
854 addr[0] = pmk_r1_name;
855 len[0] = WPA_PMK_NAME_LEN;
856 addr[1] = (const u8 *) "FT-PTKN";
857 len[1] = 7;
858 addr[2] = snonce;
859 len[2] = WPA_NONCE_LEN;
860 addr[3] = anonce;
861 len[3] = WPA_NONCE_LEN;
862 addr[4] = bssid;
863 len[4] = ETH_ALEN;
864 addr[5] = sta_addr;
865 len[5] = ETH_ALEN;
866
867 sha256_vector(6, addr, len, hash);
868 os_memcpy(ptk_name, hash, WPA_PMK_NAME_LEN);
869}
870
871#endif /* CONFIG_IEEE80211R */
872
873
874/**
875 * rsn_pmkid - Calculate PMK identifier
876 * @pmk: Pairwise master key
877 * @pmk_len: Length of pmk in bytes
878 * @aa: Authenticator address
879 * @spa: Supplicant address
880 * @pmkid: Buffer for PMKID
881 * @use_sha256: Whether to use SHA256-based KDF
882 *
883 * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
884 * PMKID = HMAC-SHA1-128(PMK, "PMK Name" || AA || SPA)
885 */
886void rsn_pmkid(const u8 *pmk, size_t pmk_len, const u8 *aa, const u8 *spa,
887 u8 *pmkid, int use_sha256)
888{
889 char *title = "PMK Name";
890 const u8 *addr[3];
891 const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
892 unsigned char hash[SHA256_MAC_LEN];
893
894 addr[0] = (u8 *) title;
895 addr[1] = aa;
896 addr[2] = spa;
897
898#ifdef CONFIG_IEEE80211W
899 if (use_sha256)
900 hmac_sha256_vector(pmk, pmk_len, 3, addr, len, hash);
901 else
902#endif /* CONFIG_IEEE80211W */
903 hmac_sha1_vector(pmk, pmk_len, 3, addr, len, hash);
904 os_memcpy(pmkid, hash, PMKID_LEN);
905}
906
907
908/**
909 * wpa_cipher_txt - Convert cipher suite to a text string
910 * @cipher: Cipher suite (WPA_CIPHER_* enum)
911 * Returns: Pointer to a text string of the cipher suite name
912 */
913const char * wpa_cipher_txt(int cipher)
914{
915 switch (cipher) {
916 case WPA_CIPHER_NONE:
917 return "NONE";
918 case WPA_CIPHER_WEP40:
919 return "WEP-40";
920 case WPA_CIPHER_WEP104:
921 return "WEP-104";
922 case WPA_CIPHER_TKIP:
923 return "TKIP";
924 case WPA_CIPHER_CCMP:
925 return "CCMP";
926 case WPA_CIPHER_CCMP | WPA_CIPHER_TKIP:
927 return "CCMP+TKIP";
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700928 case WPA_CIPHER_GCMP:
929 return "GCMP";
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800930 case WPA_CIPHER_GCMP_256:
931 return "GCMP-256";
932 case WPA_CIPHER_CCMP_256:
933 return "CCMP-256";
934 case WPA_CIPHER_GTK_NOT_USED:
935 return "GTK_NOT_USED";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936 default:
937 return "UNKNOWN";
938 }
939}
940
941
942/**
943 * wpa_key_mgmt_txt - Convert key management suite to a text string
944 * @key_mgmt: Key management suite (WPA_KEY_MGMT_* enum)
945 * @proto: WPA/WPA2 version (WPA_PROTO_*)
946 * Returns: Pointer to a text string of the key management suite name
947 */
948const char * wpa_key_mgmt_txt(int key_mgmt, int proto)
949{
950 switch (key_mgmt) {
951 case WPA_KEY_MGMT_IEEE8021X:
952 if (proto == (WPA_PROTO_RSN | WPA_PROTO_WPA))
953 return "WPA2+WPA/IEEE 802.1X/EAP";
954 return proto == WPA_PROTO_RSN ?
955 "WPA2/IEEE 802.1X/EAP" : "WPA/IEEE 802.1X/EAP";
956 case WPA_KEY_MGMT_PSK:
957 if (proto == (WPA_PROTO_RSN | WPA_PROTO_WPA))
958 return "WPA2-PSK+WPA-PSK";
959 return proto == WPA_PROTO_RSN ?
960 "WPA2-PSK" : "WPA-PSK";
961 case WPA_KEY_MGMT_NONE:
962 return "NONE";
963 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
964 return "IEEE 802.1X (no WPA)";
965#ifdef CONFIG_IEEE80211R
966 case WPA_KEY_MGMT_FT_IEEE8021X:
967 return "FT-EAP";
968 case WPA_KEY_MGMT_FT_PSK:
969 return "FT-PSK";
970#endif /* CONFIG_IEEE80211R */
971#ifdef CONFIG_IEEE80211W
972 case WPA_KEY_MGMT_IEEE8021X_SHA256:
973 return "WPA2-EAP-SHA256";
974 case WPA_KEY_MGMT_PSK_SHA256:
975 return "WPA2-PSK-SHA256";
976#endif /* CONFIG_IEEE80211W */
977 default:
978 return "UNKNOWN";
979 }
980}
981
982
983int wpa_compare_rsn_ie(int ft_initial_assoc,
984 const u8 *ie1, size_t ie1len,
985 const u8 *ie2, size_t ie2len)
986{
987 if (ie1 == NULL || ie2 == NULL)
988 return -1;
989
990 if (ie1len == ie2len && os_memcmp(ie1, ie2, ie1len) == 0)
991 return 0; /* identical IEs */
992
993#ifdef CONFIG_IEEE80211R
994 if (ft_initial_assoc) {
995 struct wpa_ie_data ie1d, ie2d;
996 /*
997 * The PMKID-List in RSN IE is different between Beacon/Probe
998 * Response/(Re)Association Request frames and EAPOL-Key
999 * messages in FT initial mobility domain association. Allow
1000 * for this, but verify that other parts of the RSN IEs are
1001 * identical.
1002 */
1003 if (wpa_parse_wpa_ie_rsn(ie1, ie1len, &ie1d) < 0 ||
1004 wpa_parse_wpa_ie_rsn(ie2, ie2len, &ie2d) < 0)
1005 return -1;
1006 if (ie1d.proto == ie2d.proto &&
1007 ie1d.pairwise_cipher == ie2d.pairwise_cipher &&
1008 ie1d.group_cipher == ie2d.group_cipher &&
1009 ie1d.key_mgmt == ie2d.key_mgmt &&
1010 ie1d.capabilities == ie2d.capabilities &&
1011 ie1d.mgmt_group_cipher == ie2d.mgmt_group_cipher)
1012 return 0;
1013 }
1014#endif /* CONFIG_IEEE80211R */
1015
1016 return -1;
1017}
1018
1019
1020#ifdef CONFIG_IEEE80211R
1021int wpa_insert_pmkid(u8 *ies, size_t ies_len, const u8 *pmkid)
1022{
1023 u8 *start, *end, *rpos, *rend;
1024 int added = 0;
1025
1026 start = ies;
1027 end = ies + ies_len;
1028
1029 while (start < end) {
1030 if (*start == WLAN_EID_RSN)
1031 break;
1032 start += 2 + start[1];
1033 }
1034 if (start >= end) {
1035 wpa_printf(MSG_ERROR, "FT: Could not find RSN IE in "
1036 "IEs data");
1037 return -1;
1038 }
1039 wpa_hexdump(MSG_DEBUG, "FT: RSN IE before modification",
1040 start, 2 + start[1]);
1041
1042 /* Find start of PMKID-Count */
1043 rpos = start + 2;
1044 rend = rpos + start[1];
1045
1046 /* Skip Version and Group Data Cipher Suite */
1047 rpos += 2 + 4;
1048 /* Skip Pairwise Cipher Suite Count and List */
1049 rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN;
1050 /* Skip AKM Suite Count and List */
1051 rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN;
1052
1053 if (rpos == rend) {
1054 /* Add RSN Capabilities */
1055 os_memmove(rpos + 2, rpos, end - rpos);
1056 *rpos++ = 0;
1057 *rpos++ = 0;
1058 } else {
1059 /* Skip RSN Capabilities */
1060 rpos += 2;
1061 if (rpos > rend) {
1062 wpa_printf(MSG_ERROR, "FT: Could not parse RSN IE in "
1063 "IEs data");
1064 return -1;
1065 }
1066 }
1067
1068 if (rpos == rend) {
1069 /* No PMKID-Count field included; add it */
1070 os_memmove(rpos + 2 + PMKID_LEN, rpos, end - rpos);
1071 WPA_PUT_LE16(rpos, 1);
1072 rpos += 2;
1073 os_memcpy(rpos, pmkid, PMKID_LEN);
1074 added += 2 + PMKID_LEN;
1075 start[1] += 2 + PMKID_LEN;
1076 } else {
1077 /* PMKID-Count was included; use it */
1078 if (WPA_GET_LE16(rpos) != 0) {
1079 wpa_printf(MSG_ERROR, "FT: Unexpected PMKID "
1080 "in RSN IE in EAPOL-Key data");
1081 return -1;
1082 }
1083 WPA_PUT_LE16(rpos, 1);
1084 rpos += 2;
1085 os_memmove(rpos + PMKID_LEN, rpos, end - rpos);
1086 os_memcpy(rpos, pmkid, PMKID_LEN);
1087 added += PMKID_LEN;
1088 start[1] += PMKID_LEN;
1089 }
1090
1091 wpa_hexdump(MSG_DEBUG, "FT: RSN IE after modification "
1092 "(PMKID inserted)", start, 2 + start[1]);
1093
1094 return added;
1095}
1096#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001097
1098
1099int wpa_cipher_key_len(int cipher)
1100{
1101 switch (cipher) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001102 case WPA_CIPHER_CCMP_256:
1103 case WPA_CIPHER_GCMP_256:
1104 return 32;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001105 case WPA_CIPHER_CCMP:
1106 case WPA_CIPHER_GCMP:
1107 return 16;
1108 case WPA_CIPHER_TKIP:
1109 return 32;
1110 case WPA_CIPHER_WEP104:
1111 return 13;
1112 case WPA_CIPHER_WEP40:
1113 return 5;
1114 }
1115
1116 return 0;
1117}
1118
1119
1120int wpa_cipher_rsc_len(int cipher)
1121{
1122 switch (cipher) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001123 case WPA_CIPHER_CCMP_256:
1124 case WPA_CIPHER_GCMP_256:
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001125 case WPA_CIPHER_CCMP:
1126 case WPA_CIPHER_GCMP:
1127 case WPA_CIPHER_TKIP:
1128 return 6;
1129 case WPA_CIPHER_WEP104:
1130 case WPA_CIPHER_WEP40:
1131 return 0;
1132 }
1133
1134 return 0;
1135}
1136
1137
1138int wpa_cipher_to_alg(int cipher)
1139{
1140 switch (cipher) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001141 case WPA_CIPHER_CCMP_256:
1142 return WPA_ALG_CCMP_256;
1143 case WPA_CIPHER_GCMP_256:
1144 return WPA_ALG_GCMP_256;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001145 case WPA_CIPHER_CCMP:
1146 return WPA_ALG_CCMP;
1147 case WPA_CIPHER_GCMP:
1148 return WPA_ALG_GCMP;
1149 case WPA_CIPHER_TKIP:
1150 return WPA_ALG_TKIP;
1151 case WPA_CIPHER_WEP104:
1152 case WPA_CIPHER_WEP40:
1153 return WPA_ALG_WEP;
1154 }
1155 return WPA_ALG_NONE;
1156}
1157
1158
1159int wpa_cipher_valid_pairwise(int cipher)
1160{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001161 return cipher == WPA_CIPHER_CCMP_256 ||
1162 cipher == WPA_CIPHER_GCMP_256 ||
1163 cipher == WPA_CIPHER_CCMP ||
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001164 cipher == WPA_CIPHER_GCMP ||
1165 cipher == WPA_CIPHER_TKIP;
1166}
1167
1168
1169u32 wpa_cipher_to_suite(int proto, int cipher)
1170{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001171 if (cipher & WPA_CIPHER_CCMP_256)
1172 return RSN_CIPHER_SUITE_CCMP_256;
1173 if (cipher & WPA_CIPHER_GCMP_256)
1174 return RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001175 if (cipher & WPA_CIPHER_CCMP)
1176 return (proto == WPA_PROTO_RSN ?
1177 RSN_CIPHER_SUITE_CCMP : WPA_CIPHER_SUITE_CCMP);
1178 if (cipher & WPA_CIPHER_GCMP)
1179 return RSN_CIPHER_SUITE_GCMP;
1180 if (cipher & WPA_CIPHER_TKIP)
1181 return (proto == WPA_PROTO_RSN ?
1182 RSN_CIPHER_SUITE_TKIP : WPA_CIPHER_SUITE_TKIP);
1183 if (cipher & WPA_CIPHER_WEP104)
1184 return (proto == WPA_PROTO_RSN ?
1185 RSN_CIPHER_SUITE_WEP104 : WPA_CIPHER_SUITE_WEP104);
1186 if (cipher & WPA_CIPHER_WEP40)
1187 return (proto == WPA_PROTO_RSN ?
1188 RSN_CIPHER_SUITE_WEP40 : WPA_CIPHER_SUITE_WEP40);
1189 if (cipher & WPA_CIPHER_NONE)
1190 return (proto == WPA_PROTO_RSN ?
1191 RSN_CIPHER_SUITE_NONE : WPA_CIPHER_SUITE_NONE);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001192 if (cipher & WPA_CIPHER_GTK_NOT_USED)
1193 return RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001194 return 0;
1195}
1196
1197
1198int rsn_cipher_put_suites(u8 *pos, int ciphers)
1199{
1200 int num_suites = 0;
1201
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001202 if (ciphers & WPA_CIPHER_CCMP_256) {
1203 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP_256);
1204 pos += RSN_SELECTOR_LEN;
1205 num_suites++;
1206 }
1207 if (ciphers & WPA_CIPHER_GCMP_256) {
1208 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_GCMP_256);
1209 pos += RSN_SELECTOR_LEN;
1210 num_suites++;
1211 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001212 if (ciphers & WPA_CIPHER_CCMP) {
1213 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
1214 pos += RSN_SELECTOR_LEN;
1215 num_suites++;
1216 }
1217 if (ciphers & WPA_CIPHER_GCMP) {
1218 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_GCMP);
1219 pos += RSN_SELECTOR_LEN;
1220 num_suites++;
1221 }
1222 if (ciphers & WPA_CIPHER_TKIP) {
1223 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_TKIP);
1224 pos += RSN_SELECTOR_LEN;
1225 num_suites++;
1226 }
1227 if (ciphers & WPA_CIPHER_NONE) {
1228 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NONE);
1229 pos += RSN_SELECTOR_LEN;
1230 num_suites++;
1231 }
1232
1233 return num_suites;
1234}
1235
1236
1237int wpa_cipher_put_suites(u8 *pos, int ciphers)
1238{
1239 int num_suites = 0;
1240
1241 if (ciphers & WPA_CIPHER_CCMP) {
1242 RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_CCMP);
1243 pos += WPA_SELECTOR_LEN;
1244 num_suites++;
1245 }
1246 if (ciphers & WPA_CIPHER_TKIP) {
1247 RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_TKIP);
1248 pos += WPA_SELECTOR_LEN;
1249 num_suites++;
1250 }
1251 if (ciphers & WPA_CIPHER_NONE) {
1252 RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_NONE);
1253 pos += WPA_SELECTOR_LEN;
1254 num_suites++;
1255 }
1256
1257 return num_suites;
1258}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001259
1260
1261int wpa_pick_pairwise_cipher(int ciphers, int none_allowed)
1262{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001263 if (ciphers & WPA_CIPHER_CCMP_256)
1264 return WPA_CIPHER_CCMP_256;
1265 if (ciphers & WPA_CIPHER_GCMP_256)
1266 return WPA_CIPHER_GCMP_256;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001267 if (ciphers & WPA_CIPHER_CCMP)
1268 return WPA_CIPHER_CCMP;
1269 if (ciphers & WPA_CIPHER_GCMP)
1270 return WPA_CIPHER_GCMP;
1271 if (ciphers & WPA_CIPHER_TKIP)
1272 return WPA_CIPHER_TKIP;
1273 if (none_allowed && (ciphers & WPA_CIPHER_NONE))
1274 return WPA_CIPHER_NONE;
1275 return -1;
1276}
1277
1278
1279int wpa_pick_group_cipher(int ciphers)
1280{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001281 if (ciphers & WPA_CIPHER_CCMP_256)
1282 return WPA_CIPHER_CCMP_256;
1283 if (ciphers & WPA_CIPHER_GCMP_256)
1284 return WPA_CIPHER_GCMP_256;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001285 if (ciphers & WPA_CIPHER_CCMP)
1286 return WPA_CIPHER_CCMP;
1287 if (ciphers & WPA_CIPHER_GCMP)
1288 return WPA_CIPHER_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001289 if (ciphers & WPA_CIPHER_GTK_NOT_USED)
1290 return WPA_CIPHER_GTK_NOT_USED;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001291 if (ciphers & WPA_CIPHER_TKIP)
1292 return WPA_CIPHER_TKIP;
1293 if (ciphers & WPA_CIPHER_WEP104)
1294 return WPA_CIPHER_WEP104;
1295 if (ciphers & WPA_CIPHER_WEP40)
1296 return WPA_CIPHER_WEP40;
1297 return -1;
1298}
1299
1300
1301int wpa_parse_cipher(const char *value)
1302{
1303 int val = 0, last;
1304 char *start, *end, *buf;
1305
1306 buf = os_strdup(value);
1307 if (buf == NULL)
1308 return -1;
1309 start = buf;
1310
1311 while (*start != '\0') {
1312 while (*start == ' ' || *start == '\t')
1313 start++;
1314 if (*start == '\0')
1315 break;
1316 end = start;
1317 while (*end != ' ' && *end != '\t' && *end != '\0')
1318 end++;
1319 last = *end == '\0';
1320 *end = '\0';
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001321 if (os_strcmp(start, "CCMP-256") == 0)
1322 val |= WPA_CIPHER_CCMP_256;
1323 else if (os_strcmp(start, "GCMP-256") == 0)
1324 val |= WPA_CIPHER_GCMP_256;
1325 else if (os_strcmp(start, "CCMP") == 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001326 val |= WPA_CIPHER_CCMP;
1327 else if (os_strcmp(start, "GCMP") == 0)
1328 val |= WPA_CIPHER_GCMP;
1329 else if (os_strcmp(start, "TKIP") == 0)
1330 val |= WPA_CIPHER_TKIP;
1331 else if (os_strcmp(start, "WEP104") == 0)
1332 val |= WPA_CIPHER_WEP104;
1333 else if (os_strcmp(start, "WEP40") == 0)
1334 val |= WPA_CIPHER_WEP40;
1335 else if (os_strcmp(start, "NONE") == 0)
1336 val |= WPA_CIPHER_NONE;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001337 else if (os_strcmp(start, "GTK_NOT_USED") == 0)
1338 val |= WPA_CIPHER_GTK_NOT_USED;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001339 else {
1340 os_free(buf);
1341 return -1;
1342 }
1343
1344 if (last)
1345 break;
1346 start = end + 1;
1347 }
1348 os_free(buf);
1349
1350 return val;
1351}
1352
1353
1354int wpa_write_ciphers(char *start, char *end, int ciphers, const char *delim)
1355{
1356 char *pos = start;
1357 int ret;
1358
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001359 if (ciphers & WPA_CIPHER_CCMP_256) {
1360 ret = os_snprintf(pos, end - pos, "%sCCMP-256",
1361 pos == start ? "" : delim);
1362 if (ret < 0 || ret >= end - pos)
1363 return -1;
1364 pos += ret;
1365 }
1366 if (ciphers & WPA_CIPHER_GCMP_256) {
1367 ret = os_snprintf(pos, end - pos, "%sGCMP-256",
1368 pos == start ? "" : delim);
1369 if (ret < 0 || ret >= end - pos)
1370 return -1;
1371 pos += ret;
1372 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001373 if (ciphers & WPA_CIPHER_CCMP) {
1374 ret = os_snprintf(pos, end - pos, "%sCCMP",
1375 pos == start ? "" : delim);
1376 if (ret < 0 || ret >= end - pos)
1377 return -1;
1378 pos += ret;
1379 }
1380 if (ciphers & WPA_CIPHER_GCMP) {
1381 ret = os_snprintf(pos, end - pos, "%sGCMP",
1382 pos == start ? "" : delim);
1383 if (ret < 0 || ret >= end - pos)
1384 return -1;
1385 pos += ret;
1386 }
1387 if (ciphers & WPA_CIPHER_TKIP) {
1388 ret = os_snprintf(pos, end - pos, "%sTKIP",
1389 pos == start ? "" : delim);
1390 if (ret < 0 || ret >= end - pos)
1391 return -1;
1392 pos += ret;
1393 }
1394 if (ciphers & WPA_CIPHER_WEP104) {
1395 ret = os_snprintf(pos, end - pos, "%sWEP104",
1396 pos == start ? "" : delim);
1397 if (ret < 0 || ret >= end - pos)
1398 return -1;
1399 pos += ret;
1400 }
1401 if (ciphers & WPA_CIPHER_WEP40) {
1402 ret = os_snprintf(pos, end - pos, "%sWEP40",
1403 pos == start ? "" : delim);
1404 if (ret < 0 || ret >= end - pos)
1405 return -1;
1406 pos += ret;
1407 }
1408 if (ciphers & WPA_CIPHER_NONE) {
1409 ret = os_snprintf(pos, end - pos, "%sNONE",
1410 pos == start ? "" : delim);
1411 if (ret < 0 || ret >= end - pos)
1412 return -1;
1413 pos += ret;
1414 }
1415
1416 return pos - start;
1417}
1418
1419
1420int wpa_select_ap_group_cipher(int wpa, int wpa_pairwise, int rsn_pairwise)
1421{
1422 int pairwise = 0;
1423
1424 /* Select group cipher based on the enabled pairwise cipher suites */
1425 if (wpa & 1)
1426 pairwise |= wpa_pairwise;
1427 if (wpa & 2)
1428 pairwise |= rsn_pairwise;
1429
1430 if (pairwise & WPA_CIPHER_TKIP)
1431 return WPA_CIPHER_TKIP;
1432 if ((pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)) == WPA_CIPHER_GCMP)
1433 return WPA_CIPHER_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001434 if ((pairwise & (WPA_CIPHER_GCMP_256 | WPA_CIPHER_CCMP |
1435 WPA_CIPHER_GCMP)) == WPA_CIPHER_GCMP_256)
1436 return WPA_CIPHER_GCMP_256;
1437 if ((pairwise & (WPA_CIPHER_CCMP_256 | WPA_CIPHER_CCMP |
1438 WPA_CIPHER_GCMP)) == WPA_CIPHER_CCMP_256)
1439 return WPA_CIPHER_CCMP_256;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001440 return WPA_CIPHER_CCMP;
1441}