Re-enable hardware UDP/TCP checksum calculation with pseudo header on
[dragonfly/port-amd64.git] / contrib / wpa_supplicant-0.4.9 / wpa.c
blobae3d982a743cc64a790c3dfa55ffb5e679f94a6c
1 /*
2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
3 * Copyright (c) 2003-2005, Jouni Malinen <jkmaline@cc.hut.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include <stdlib.h>
16 #include <stdio.h>
17 #ifndef CONFIG_NATIVE_WINDOWS
18 #include <netinet/in.h>
19 #endif /* CONFIG_NATIVE_WINDOWS */
20 #include <string.h>
22 #include "common.h"
23 #include "md5.h"
24 #include "sha1.h"
25 #include "rc4.h"
26 #include "aes_wrap.h"
27 #include "wpa.h"
28 #include "eloop.h"
29 #include "wpa_supplicant.h"
30 #include "config.h"
31 #include "l2_packet.h"
32 #include "eapol_sm.h"
33 #include "preauth.h"
34 #include "wpa_i.h"
37 static const int WPA_SELECTOR_LEN = 4;
38 static const u8 WPA_OUI_TYPE[] = { 0x00, 0x50, 0xf2, 1 };
39 static const u16 WPA_VERSION = 1;
40 static const u8 WPA_AUTH_KEY_MGMT_NONE[] = { 0x00, 0x50, 0xf2, 0 };
41 static const u8 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X[] = { 0x00, 0x50, 0xf2, 1 };
42 static const u8 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X[] = { 0x00, 0x50, 0xf2, 2 };
43 static const u8 WPA_CIPHER_SUITE_NONE[] = { 0x00, 0x50, 0xf2, 0 };
44 static const u8 WPA_CIPHER_SUITE_WEP40[] = { 0x00, 0x50, 0xf2, 1 };
45 static const u8 WPA_CIPHER_SUITE_TKIP[] = { 0x00, 0x50, 0xf2, 2 };
46 static const u8 WPA_CIPHER_SUITE_WRAP[] = { 0x00, 0x50, 0xf2, 3 };
47 static const u8 WPA_CIPHER_SUITE_CCMP[] = { 0x00, 0x50, 0xf2, 4 };
48 static const u8 WPA_CIPHER_SUITE_WEP104[] = { 0x00, 0x50, 0xf2, 5 };
50 /* WPA IE version 1
51 * 00-50-f2:1 (OUI:OUI type)
52 * 0x01 0x00 (version; little endian)
53 * (all following fields are optional:)
54 * Group Suite Selector (4 octets) (default: TKIP)
55 * Pairwise Suite Count (2 octets, little endian) (default: 1)
56 * Pairwise Suite List (4 * n octets) (default: TKIP)
57 * Authenticated Key Management Suite Count (2 octets, little endian)
58 * (default: 1)
59 * Authenticated Key Management Suite List (4 * n octets)
60 * (default: unspec 802.1X)
61 * WPA Capabilities (2 octets, little endian) (default: 0)
64 struct wpa_ie_hdr {
65 u8 elem_id;
66 u8 len;
67 u8 oui[3];
68 u8 oui_type;
69 u8 version[2];
70 } __attribute__ ((packed));
73 static const int RSN_SELECTOR_LEN = 4;
74 static const u16 RSN_VERSION = 1;
75 static const u8 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X[] = { 0x00, 0x0f, 0xac, 1 };
76 static const u8 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X[] = { 0x00, 0x0f, 0xac, 2 };
77 static const u8 RSN_CIPHER_SUITE_NONE[] = { 0x00, 0x0f, 0xac, 0 };
78 static const u8 RSN_CIPHER_SUITE_WEP40[] = { 0x00, 0x0f, 0xac, 1 };
79 static const u8 RSN_CIPHER_SUITE_TKIP[] = { 0x00, 0x0f, 0xac, 2 };
80 static const u8 RSN_CIPHER_SUITE_WRAP[] = { 0x00, 0x0f, 0xac, 3 };
81 static const u8 RSN_CIPHER_SUITE_CCMP[] = { 0x00, 0x0f, 0xac, 4 };
82 static const u8 RSN_CIPHER_SUITE_WEP104[] = { 0x00, 0x0f, 0xac, 5 };
84 /* EAPOL-Key Key Data Encapsulation
85 * GroupKey and STAKey require encryption, otherwise, encryption is optional.
87 static const u8 RSN_KEY_DATA_GROUPKEY[] = { 0x00, 0x0f, 0xac, 1 };
88 static const u8 RSN_KEY_DATA_STAKEY[] = { 0x00, 0x0f, 0xac, 2 };
89 static const u8 RSN_KEY_DATA_MAC_ADDR[] = { 0x00, 0x0f, 0xac, 3 };
90 static const u8 RSN_KEY_DATA_PMKID[] = { 0x00, 0x0f, 0xac, 4 };
92 /* 1/4: PMKID
93 * 2/4: RSN IE
94 * 3/4: one or two RSN IEs + GTK IE (encrypted)
95 * 4/4: empty
96 * 1/2: GTK IE (encrypted)
97 * 2/2: empty
100 /* RSN IE version 1
101 * 0x01 0x00 (version; little endian)
102 * (all following fields are optional:)
103 * Group Suite Selector (4 octets) (default: CCMP)
104 * Pairwise Suite Count (2 octets, little endian) (default: 1)
105 * Pairwise Suite List (4 * n octets) (default: CCMP)
106 * Authenticated Key Management Suite Count (2 octets, little endian)
107 * (default: 1)
108 * Authenticated Key Management Suite List (4 * n octets)
109 * (default: unspec 802.1X)
110 * RSN Capabilities (2 octets, little endian) (default: 0)
111 * PMKID Count (2 octets) (default: 0)
112 * PMKID List (16 * n octets)
115 struct rsn_ie_hdr {
116 u8 elem_id; /* WLAN_EID_RSN */
117 u8 len;
118 u8 version[2];
119 } __attribute__ ((packed));
122 struct wpa_eapol_key {
123 u8 type;
124 /* Note: key_info, key_length, and key_data_length are unaligned */
125 u8 key_info[2];
126 u8 key_length[2];
127 u8 replay_counter[WPA_REPLAY_COUNTER_LEN];
128 u8 key_nonce[WPA_NONCE_LEN];
129 u8 key_iv[16];
130 u8 key_rsc[8];
131 u8 key_id[8]; /* Reserved in IEEE 802.11i/RSN */
132 u8 key_mic[16];
133 u8 key_data_length[2];
134 /* followed by key_data_length bytes of key_data */
135 } __attribute__ ((packed));
137 #define WPA_KEY_INFO_TYPE_MASK (BIT(0) | BIT(1) | BIT(2))
138 #define WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 BIT(0)
139 #define WPA_KEY_INFO_TYPE_HMAC_SHA1_AES BIT(1)
140 #define WPA_KEY_INFO_KEY_TYPE BIT(3) /* 1 = Pairwise, 0 = Group key */
141 /* bit4..5 is used in WPA, but is reserved in IEEE 802.11i/RSN */
142 #define WPA_KEY_INFO_KEY_INDEX_MASK (BIT(4) | BIT(5))
143 #define WPA_KEY_INFO_KEY_INDEX_SHIFT 4
144 #define WPA_KEY_INFO_INSTALL BIT(6) /* pairwise */
145 #define WPA_KEY_INFO_TXRX BIT(6) /* group */
146 #define WPA_KEY_INFO_ACK BIT(7)
147 #define WPA_KEY_INFO_MIC BIT(8)
148 #define WPA_KEY_INFO_SECURE BIT(9)
149 #define WPA_KEY_INFO_ERROR BIT(10)
150 #define WPA_KEY_INFO_REQUEST BIT(11)
151 #define WPA_KEY_INFO_ENCR_KEY_DATA BIT(12) /* IEEE 802.11i/RSN only */
156 * wpa_cipher_txt - Convert cipher suite to a text string
157 * @cipher: Cipher suite (WPA_CIPHER_* enum)
158 * Returns: Pointer to a text string of the cipher suite name
160 static const char * wpa_cipher_txt(int cipher)
162 switch (cipher) {
163 case WPA_CIPHER_NONE:
164 return "NONE";
165 case WPA_CIPHER_WEP40:
166 return "WEP-40";
167 case WPA_CIPHER_WEP104:
168 return "WEP-104";
169 case WPA_CIPHER_TKIP:
170 return "TKIP";
171 case WPA_CIPHER_CCMP:
172 return "CCMP";
173 default:
174 return "UNKNOWN";
180 * wpa_key_mgmt_txt - Convert key management suite to a text string
181 * @key_mgmt: Key management suite (WPA_KEY_MGMT_* enum)
182 * @proto: WPA/WPA2 version (WPA_PROTO_*)
183 * Returns: Pointer to a text string of the key management suite name
185 static const char * wpa_key_mgmt_txt(int key_mgmt, int proto)
187 switch (key_mgmt) {
188 case WPA_KEY_MGMT_IEEE8021X:
189 return proto == WPA_PROTO_RSN ?
190 "WPA2/IEEE 802.1X/EAP" : "WPA/IEEE 802.1X/EAP";
191 case WPA_KEY_MGMT_PSK:
192 return proto == WPA_PROTO_RSN ?
193 "WPA2-PSK" : "WPA-PSK";
194 case WPA_KEY_MGMT_NONE:
195 return "NONE";
196 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
197 return "IEEE 802.1X (no WPA)";
198 default:
199 return "UNKNOWN";
204 static int wpa_selector_to_bitfield(const u8 *s)
206 if (memcmp(s, WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN) == 0)
207 return WPA_CIPHER_NONE;
208 if (memcmp(s, WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN) == 0)
209 return WPA_CIPHER_WEP40;
210 if (memcmp(s, WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN) == 0)
211 return WPA_CIPHER_TKIP;
212 if (memcmp(s, WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN) == 0)
213 return WPA_CIPHER_CCMP;
214 if (memcmp(s, WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN) == 0)
215 return WPA_CIPHER_WEP104;
216 return 0;
220 static int wpa_key_mgmt_to_bitfield(const u8 *s)
222 if (memcmp(s, WPA_AUTH_KEY_MGMT_UNSPEC_802_1X, WPA_SELECTOR_LEN) == 0)
223 return WPA_KEY_MGMT_IEEE8021X;
224 if (memcmp(s, WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X, WPA_SELECTOR_LEN) ==
226 return WPA_KEY_MGMT_PSK;
227 if (memcmp(s, WPA_AUTH_KEY_MGMT_NONE, WPA_SELECTOR_LEN) == 0)
228 return WPA_KEY_MGMT_WPA_NONE;
229 return 0;
233 static int rsn_selector_to_bitfield(const u8 *s)
235 if (memcmp(s, RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN) == 0)
236 return WPA_CIPHER_NONE;
237 if (memcmp(s, RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN) == 0)
238 return WPA_CIPHER_WEP40;
239 if (memcmp(s, RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN) == 0)
240 return WPA_CIPHER_TKIP;
241 if (memcmp(s, RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN) == 0)
242 return WPA_CIPHER_CCMP;
243 if (memcmp(s, RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN) == 0)
244 return WPA_CIPHER_WEP104;
245 return 0;
249 static int rsn_key_mgmt_to_bitfield(const u8 *s)
251 if (memcmp(s, RSN_AUTH_KEY_MGMT_UNSPEC_802_1X, RSN_SELECTOR_LEN) == 0)
252 return WPA_KEY_MGMT_IEEE8021X;
253 if (memcmp(s, RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X, RSN_SELECTOR_LEN) ==
255 return WPA_KEY_MGMT_PSK;
256 return 0;
260 static int wpa_parse_wpa_ie_wpa(const u8 *wpa_ie, size_t wpa_ie_len,
261 struct wpa_ie_data *data)
263 const struct wpa_ie_hdr *hdr;
264 const u8 *pos;
265 int left;
266 int i, count;
268 data->proto = WPA_PROTO_WPA;
269 data->pairwise_cipher = WPA_CIPHER_TKIP;
270 data->group_cipher = WPA_CIPHER_TKIP;
271 data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
272 data->capabilities = 0;
273 data->pmkid = NULL;
274 data->num_pmkid = 0;
276 if (wpa_ie_len == 0) {
277 /* No WPA IE - fail silently */
278 return -1;
281 if (wpa_ie_len < sizeof(struct wpa_ie_hdr)) {
282 wpa_printf(MSG_DEBUG, "%s: ie len too short %lu",
283 __func__, (unsigned long) wpa_ie_len);
284 return -1;
287 hdr = (const struct wpa_ie_hdr *) wpa_ie;
289 if (hdr->elem_id != GENERIC_INFO_ELEM ||
290 hdr->len != wpa_ie_len - 2 ||
291 memcmp(&hdr->oui, WPA_OUI_TYPE, WPA_SELECTOR_LEN) != 0 ||
292 WPA_GET_LE16(hdr->version) != WPA_VERSION) {
293 wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version",
294 __func__);
295 return -1;
298 pos = (const u8 *) (hdr + 1);
299 left = wpa_ie_len - sizeof(*hdr);
301 if (left >= WPA_SELECTOR_LEN) {
302 data->group_cipher = wpa_selector_to_bitfield(pos);
303 pos += WPA_SELECTOR_LEN;
304 left -= WPA_SELECTOR_LEN;
305 } else if (left > 0) {
306 wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much",
307 __func__, left);
308 return -1;
311 if (left >= 2) {
312 data->pairwise_cipher = 0;
313 count = WPA_GET_LE16(pos);
314 pos += 2;
315 left -= 2;
316 if (count == 0 || left < count * WPA_SELECTOR_LEN) {
317 wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), "
318 "count %u left %u", __func__, count, left);
319 return -1;
321 for (i = 0; i < count; i++) {
322 data->pairwise_cipher |= wpa_selector_to_bitfield(pos);
323 pos += WPA_SELECTOR_LEN;
324 left -= WPA_SELECTOR_LEN;
326 } else if (left == 1) {
327 wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)",
328 __func__);
329 return -1;
332 if (left >= 2) {
333 data->key_mgmt = 0;
334 count = WPA_GET_LE16(pos);
335 pos += 2;
336 left -= 2;
337 if (count == 0 || left < count * WPA_SELECTOR_LEN) {
338 wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), "
339 "count %u left %u", __func__, count, left);
340 return -1;
342 for (i = 0; i < count; i++) {
343 data->key_mgmt |= wpa_key_mgmt_to_bitfield(pos);
344 pos += WPA_SELECTOR_LEN;
345 left -= WPA_SELECTOR_LEN;
347 } else if (left == 1) {
348 wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)",
349 __func__);
350 return -1;
353 if (left >= 2) {
354 data->capabilities = WPA_GET_LE16(pos);
355 pos += 2;
356 left -= 2;
359 if (left > 0) {
360 wpa_printf(MSG_DEBUG, "%s: ie has %u trailing bytes - ignored",
361 __func__, left);
364 return 0;
368 static int wpa_parse_wpa_ie_rsn(const u8 *rsn_ie, size_t rsn_ie_len,
369 struct wpa_ie_data *data)
371 const struct rsn_ie_hdr *hdr;
372 const u8 *pos;
373 int left;
374 int i, count;
376 data->proto = WPA_PROTO_RSN;
377 data->pairwise_cipher = WPA_CIPHER_CCMP;
378 data->group_cipher = WPA_CIPHER_CCMP;
379 data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
380 data->capabilities = 0;
381 data->pmkid = NULL;
382 data->num_pmkid = 0;
384 if (rsn_ie_len == 0) {
385 /* No RSN IE - fail silently */
386 return -1;
389 if (rsn_ie_len < sizeof(struct rsn_ie_hdr)) {
390 wpa_printf(MSG_DEBUG, "%s: ie len too short %lu",
391 __func__, (unsigned long) rsn_ie_len);
392 return -1;
395 hdr = (const struct rsn_ie_hdr *) rsn_ie;
397 if (hdr->elem_id != RSN_INFO_ELEM ||
398 hdr->len != rsn_ie_len - 2 ||
399 WPA_GET_LE16(hdr->version) != RSN_VERSION) {
400 wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version",
401 __func__);
402 return -1;
405 pos = (const u8 *) (hdr + 1);
406 left = rsn_ie_len - sizeof(*hdr);
408 if (left >= RSN_SELECTOR_LEN) {
409 data->group_cipher = rsn_selector_to_bitfield(pos);
410 pos += RSN_SELECTOR_LEN;
411 left -= RSN_SELECTOR_LEN;
412 } else if (left > 0) {
413 wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much",
414 __func__, left);
415 return -1;
418 if (left >= 2) {
419 data->pairwise_cipher = 0;
420 count = WPA_GET_LE16(pos);
421 pos += 2;
422 left -= 2;
423 if (count == 0 || left < count * RSN_SELECTOR_LEN) {
424 wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), "
425 "count %u left %u", __func__, count, left);
426 return -1;
428 for (i = 0; i < count; i++) {
429 data->pairwise_cipher |= rsn_selector_to_bitfield(pos);
430 pos += RSN_SELECTOR_LEN;
431 left -= RSN_SELECTOR_LEN;
433 } else if (left == 1) {
434 wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)",
435 __func__);
436 return -1;
439 if (left >= 2) {
440 data->key_mgmt = 0;
441 count = WPA_GET_LE16(pos);
442 pos += 2;
443 left -= 2;
444 if (count == 0 || left < count * RSN_SELECTOR_LEN) {
445 wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), "
446 "count %u left %u", __func__, count, left);
447 return -1;
449 for (i = 0; i < count; i++) {
450 data->key_mgmt |= rsn_key_mgmt_to_bitfield(pos);
451 pos += RSN_SELECTOR_LEN;
452 left -= RSN_SELECTOR_LEN;
454 } else if (left == 1) {
455 wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)",
456 __func__);
457 return -1;
460 if (left >= 2) {
461 data->capabilities = WPA_GET_LE16(pos);
462 pos += 2;
463 left -= 2;
466 if (left >= 2) {
467 data->num_pmkid = WPA_GET_LE16(pos);
468 pos += 2;
469 left -= 2;
470 if (left < data->num_pmkid * PMKID_LEN) {
471 wpa_printf(MSG_DEBUG, "%s: PMKID underflow "
472 "(num_pmkid=%d left=%d)",
473 __func__, data->num_pmkid, left);
474 data->num_pmkid = 0;
475 } else {
476 data->pmkid = pos;
477 pos += data->num_pmkid * PMKID_LEN;
478 left -= data->num_pmkid * PMKID_LEN;
482 if (left > 0) {
483 wpa_printf(MSG_DEBUG, "%s: ie has %u trailing bytes - ignored",
484 __func__, left);
487 return 0;
492 * wpa_parse_wpa_ie - Parse WPA/RSN IE
493 * @wpa_ie: Pointer to WPA or RSN IE
494 * @wpa_ie_len: Length of the WPA/RSN IE
495 * @data: Pointer to data area for parsing results
496 * Returns: 0 on success, -1 on failure
498 * Parse the contents of WPA or RSN IE and write the parsed data into data.
500 int wpa_parse_wpa_ie(const u8 *wpa_ie, size_t wpa_ie_len,
501 struct wpa_ie_data *data)
503 if (wpa_ie_len >= 1 && wpa_ie[0] == RSN_INFO_ELEM)
504 return wpa_parse_wpa_ie_rsn(wpa_ie, wpa_ie_len, data);
505 else
506 return wpa_parse_wpa_ie_wpa(wpa_ie, wpa_ie_len, data);
510 static int wpa_gen_wpa_ie_wpa(u8 *wpa_ie, size_t wpa_ie_len,
511 int pairwise_cipher, int group_cipher,
512 int key_mgmt)
514 u8 *pos;
515 struct wpa_ie_hdr *hdr;
517 if (wpa_ie_len < sizeof(*hdr) + WPA_SELECTOR_LEN +
518 2 + WPA_SELECTOR_LEN + 2 + WPA_SELECTOR_LEN)
519 return -1;
521 hdr = (struct wpa_ie_hdr *) wpa_ie;
522 hdr->elem_id = GENERIC_INFO_ELEM;
523 memcpy(&hdr->oui, WPA_OUI_TYPE, WPA_SELECTOR_LEN);
524 WPA_PUT_LE16(hdr->version, WPA_VERSION);
525 pos = (u8 *) (hdr + 1);
527 if (group_cipher == WPA_CIPHER_CCMP) {
528 memcpy(pos, WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN);
529 } else if (group_cipher == WPA_CIPHER_TKIP) {
530 memcpy(pos, WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN);
531 } else if (group_cipher == WPA_CIPHER_WEP104) {
532 memcpy(pos, WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN);
533 } else if (group_cipher == WPA_CIPHER_WEP40) {
534 memcpy(pos, WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN);
535 } else {
536 wpa_printf(MSG_WARNING, "Invalid group cipher (%d).",
537 group_cipher);
538 return -1;
540 pos += WPA_SELECTOR_LEN;
542 *pos++ = 1;
543 *pos++ = 0;
544 if (pairwise_cipher == WPA_CIPHER_CCMP) {
545 memcpy(pos, WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN);
546 } else if (pairwise_cipher == WPA_CIPHER_TKIP) {
547 memcpy(pos, WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN);
548 } else if (pairwise_cipher == WPA_CIPHER_NONE) {
549 memcpy(pos, WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN);
550 } else {
551 wpa_printf(MSG_WARNING, "Invalid pairwise cipher (%d).",
552 pairwise_cipher);
553 return -1;
555 pos += WPA_SELECTOR_LEN;
557 *pos++ = 1;
558 *pos++ = 0;
559 if (key_mgmt == WPA_KEY_MGMT_IEEE8021X) {
560 memcpy(pos, WPA_AUTH_KEY_MGMT_UNSPEC_802_1X, WPA_SELECTOR_LEN);
561 } else if (key_mgmt == WPA_KEY_MGMT_PSK) {
562 memcpy(pos, WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X,
563 WPA_SELECTOR_LEN);
564 } else if (key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
565 memcpy(pos, WPA_AUTH_KEY_MGMT_NONE, WPA_SELECTOR_LEN);
566 } else {
567 wpa_printf(MSG_WARNING, "Invalid key management type (%d).",
568 key_mgmt);
569 return -1;
571 pos += WPA_SELECTOR_LEN;
573 /* WPA Capabilities; use defaults, so no need to include it */
575 hdr->len = (pos - wpa_ie) - 2;
577 WPA_ASSERT(pos - wpa_ie <= wpa_ie_len);
579 return pos - wpa_ie;
583 static int wpa_gen_wpa_ie_rsn(u8 *rsn_ie, size_t rsn_ie_len,
584 int pairwise_cipher, int group_cipher,
585 int key_mgmt, struct wpa_sm *sm)
587 u8 *pos;
588 struct rsn_ie_hdr *hdr;
590 if (rsn_ie_len < sizeof(*hdr) + RSN_SELECTOR_LEN +
591 2 + RSN_SELECTOR_LEN + 2 + RSN_SELECTOR_LEN + 2 +
592 (sm->cur_pmksa ? 2 + PMKID_LEN : 0))
593 return -1;
595 hdr = (struct rsn_ie_hdr *) rsn_ie;
596 hdr->elem_id = RSN_INFO_ELEM;
597 WPA_PUT_LE16(hdr->version, RSN_VERSION);
598 pos = (u8 *) (hdr + 1);
600 if (group_cipher == WPA_CIPHER_CCMP) {
601 memcpy(pos, RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN);
602 } else if (group_cipher == WPA_CIPHER_TKIP) {
603 memcpy(pos, RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN);
604 } else if (group_cipher == WPA_CIPHER_WEP104) {
605 memcpy(pos, RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN);
606 } else if (group_cipher == WPA_CIPHER_WEP40) {
607 memcpy(pos, RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN);
608 } else {
609 wpa_printf(MSG_WARNING, "Invalid group cipher (%d).",
610 group_cipher);
611 return -1;
613 pos += RSN_SELECTOR_LEN;
615 *pos++ = 1;
616 *pos++ = 0;
617 if (pairwise_cipher == WPA_CIPHER_CCMP) {
618 memcpy(pos, RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN);
619 } else if (pairwise_cipher == WPA_CIPHER_TKIP) {
620 memcpy(pos, RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN);
621 } else if (pairwise_cipher == WPA_CIPHER_NONE) {
622 memcpy(pos, RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN);
623 } else {
624 wpa_printf(MSG_WARNING, "Invalid pairwise cipher (%d).",
625 pairwise_cipher);
626 return -1;
628 pos += RSN_SELECTOR_LEN;
630 *pos++ = 1;
631 *pos++ = 0;
632 if (key_mgmt == WPA_KEY_MGMT_IEEE8021X) {
633 memcpy(pos, RSN_AUTH_KEY_MGMT_UNSPEC_802_1X, RSN_SELECTOR_LEN);
634 } else if (key_mgmt == WPA_KEY_MGMT_PSK) {
635 memcpy(pos, RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X,
636 RSN_SELECTOR_LEN);
637 } else {
638 wpa_printf(MSG_WARNING, "Invalid key management type (%d).",
639 key_mgmt);
640 return -1;
642 pos += RSN_SELECTOR_LEN;
644 /* RSN Capabilities */
645 *pos++ = 0;
646 *pos++ = 0;
648 if (sm->cur_pmksa) {
649 /* PMKID Count (2 octets, little endian) */
650 *pos++ = 1;
651 *pos++ = 0;
652 /* PMKID */
653 memcpy(pos, sm->cur_pmksa->pmkid, PMKID_LEN);
654 pos += PMKID_LEN;
657 hdr->len = (pos - rsn_ie) - 2;
659 WPA_ASSERT(pos - rsn_ie <= rsn_ie_len);
661 return pos - rsn_ie;
666 * wpa_gen_wpa_ie - Generate WPA/RSN IE based on current security policy
667 * @sm: Pointer to WPA state machine data from wpa_sm_init()
668 * @wpa_ie: Pointer to memory area for the generated WPA/RSN IE
669 * @wpa_ie_len: Maximum length of the generated WPA/RSN IE
670 * Returns: Length of the generated WPA/RSN IE or -1 on failure
672 static int wpa_gen_wpa_ie(struct wpa_sm *sm, u8 *wpa_ie, size_t wpa_ie_len)
674 if (sm->proto == WPA_PROTO_RSN)
675 return wpa_gen_wpa_ie_rsn(wpa_ie, wpa_ie_len,
676 sm->pairwise_cipher,
677 sm->group_cipher,
678 sm->key_mgmt, sm);
679 else
680 return wpa_gen_wpa_ie_wpa(wpa_ie, wpa_ie_len,
681 sm->pairwise_cipher,
682 sm->group_cipher,
683 sm->key_mgmt);
688 * wpa_pmk_to_ptk - Calculate PTK from PMK, addresses, and nonces
689 * @pmk: Pairwise master key
690 * @addr1: AA or SA
691 * @addr2: SA or AA
692 * @nonce1: ANonce or SNonce
693 * @nonce2: SNonce or ANonce
694 * @ptk: Buffer for pairwise transient key
695 * @ptk_len: Length of PTK
697 * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
698 * PTK = PRF-X(PMK, "Pairwise key expansion",
699 * Min(AA, SA) || Max(AA, SA) ||
700 * Min(ANonce, SNonce) || Max(ANonce, SNonce))
702 static void wpa_pmk_to_ptk(const u8 *pmk, size_t pmk_len,
703 const u8 *addr1, const u8 *addr2,
704 const u8 *nonce1, const u8 *nonce2,
705 u8 *ptk, size_t ptk_len)
707 u8 data[2 * ETH_ALEN + 2 * 32];
709 if (memcmp(addr1, addr2, ETH_ALEN) < 0) {
710 memcpy(data, addr1, ETH_ALEN);
711 memcpy(data + ETH_ALEN, addr2, ETH_ALEN);
712 } else {
713 memcpy(data, addr2, ETH_ALEN);
714 memcpy(data + ETH_ALEN, addr1, ETH_ALEN);
717 if (memcmp(nonce1, nonce2, 32) < 0) {
718 memcpy(data + 2 * ETH_ALEN, nonce1, 32);
719 memcpy(data + 2 * ETH_ALEN + 32, nonce2, 32);
720 } else {
721 memcpy(data + 2 * ETH_ALEN, nonce2, 32);
722 memcpy(data + 2 * ETH_ALEN + 32, nonce1, 32);
725 sha1_prf(pmk, pmk_len, "Pairwise key expansion", data, sizeof(data),
726 ptk, ptk_len);
728 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK", pmk, pmk_len);
729 wpa_hexdump_key(MSG_DEBUG, "WPA: PTK", ptk, ptk_len);
734 * wpa_eapol_key_mic - Calculate EAPOL-Key MIC
735 * @key: EAPOL-Key Key Confirmation Key (KCK)
736 * @ver: Key descriptor version (WPA_KEY_INFO_TYPE_*)
737 * @buf: Pointer to the beginning of the EAPOL header (version field)
738 * @len: Length of the EAPOL frame (from EAPOL header to the end of the frame)
739 * @mic: Pointer to the buffer to which the EAPOL-Key MIC is written
741 * Calculate EAPOL-Key MIC for an EAPOL-Key packet. The EAPOL-Key MIC field has
742 * to be cleared (all zeroes) when calling this function.
744 * Note: 'IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames' has an error in the
745 * description of the Key MIC calculation. It includes packet data from the
746 * beginning of the EAPOL-Key header, not EAPOL header. This incorrect change
747 * happened during final editing of the standard and the correct behavior is
748 * defined in the last draft (IEEE 802.11i/D10).
750 static void wpa_eapol_key_mic(const u8 *key, int ver,
751 const u8 *buf, size_t len, u8 *mic)
753 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
754 hmac_md5(key, 16, buf, len, mic);
755 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
756 u8 hash[SHA1_MAC_LEN];
757 hmac_sha1(key, 16, buf, len, hash);
758 memcpy(mic, hash, MD5_MAC_LEN);
763 static void wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck,
764 int ver, const u8 *dest, u16 proto,
765 u8 *msg, size_t msg_len, u8 *key_mic)
767 if (key_mic) {
768 wpa_eapol_key_mic(kck, ver, msg, msg_len, key_mic);
770 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
771 wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
772 eapol_sm_notify_tx_eapol_key(sm->eapol);
773 free(msg);
778 * wpa_sm_key_request - Send EAPOL-Key Request
779 * @sm: Pointer to WPA state machine data from wpa_sm_init()
780 * @error: Indicate whether this is an Michael MIC error report
781 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
782 * Returns: Pointer to the current network structure or %NULL on failure
784 * Send an EAPOL-Key Request to the current authenticator. This function is
785 * used to request rekeying and it is usually called when a local Michael MIC
786 * failure is detected.
788 void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
790 size_t rlen;
791 struct wpa_eapol_key *reply;
792 int key_info, ver;
793 u8 bssid[ETH_ALEN], *rbuf;
795 if (sm->pairwise_cipher == WPA_CIPHER_CCMP)
796 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
797 else
798 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
800 if (wpa_sm_get_bssid(sm, bssid) < 0) {
801 wpa_printf(MSG_WARNING, "Failed to read BSSID for EAPOL-Key "
802 "request");
803 return;
806 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
807 sizeof(*reply), &rlen, (void *) &reply);
808 if (rbuf == NULL)
809 return;
811 reply->type = sm->proto == WPA_PROTO_RSN ?
812 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
813 key_info = WPA_KEY_INFO_REQUEST | ver;
814 if (sm->ptk_set)
815 key_info |= WPA_KEY_INFO_MIC;
816 if (error)
817 key_info |= WPA_KEY_INFO_ERROR;
818 if (pairwise)
819 key_info |= WPA_KEY_INFO_KEY_TYPE;
820 WPA_PUT_BE16(reply->key_info, key_info);
821 WPA_PUT_BE16(reply->key_length, 0);
822 memcpy(reply->replay_counter, sm->request_counter,
823 WPA_REPLAY_COUNTER_LEN);
824 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
826 WPA_PUT_BE16(reply->key_data_length, 0);
828 wpa_printf(MSG_INFO, "WPA: Sending EAPOL-Key Request (error=%d "
829 "pairwise=%d ptk_set=%d len=%lu)",
830 error, pairwise, sm->ptk_set, (unsigned long) rlen);
831 wpa_eapol_key_send(sm, sm->ptk.kck, ver, bssid, ETH_P_EAPOL,
832 rbuf, rlen, key_info & WPA_KEY_INFO_MIC ?
833 reply->key_mic : NULL);
837 struct wpa_eapol_ie_parse {
838 const u8 *wpa_ie;
839 size_t wpa_ie_len;
840 const u8 *rsn_ie;
841 size_t rsn_ie_len;
842 const u8 *pmkid;
843 const u8 *gtk;
844 size_t gtk_len;
849 * wpa_supplicant_parse_generic - Parse EAPOL-Key Key Data Generic IEs
850 * @pos: Pointer to the IE header
851 * @end: Pointer to the end of the Key Data buffer
852 * @ie: Pointer to parsed IE data
853 * Returns: 0 on success, 1 if end mark is found, -1 on failure
855 static int wpa_supplicant_parse_generic(const u8 *pos, const u8 *end,
856 struct wpa_eapol_ie_parse *ie)
858 if (pos[1] == 0)
859 return 1;
861 if (pos[1] >= 6 &&
862 memcmp(pos + 2, WPA_OUI_TYPE, WPA_SELECTOR_LEN) == 0 &&
863 pos[2 + WPA_SELECTOR_LEN] == 1 &&
864 pos[2 + WPA_SELECTOR_LEN + 1] == 0) {
865 ie->wpa_ie = pos;
866 ie->wpa_ie_len = pos[1] + 2;
867 return 0;
870 if (pos + 1 + RSN_SELECTOR_LEN < end &&
871 pos[1] >= RSN_SELECTOR_LEN + PMKID_LEN &&
872 memcmp(pos + 2, RSN_KEY_DATA_PMKID, RSN_SELECTOR_LEN) == 0) {
873 ie->pmkid = pos + 2 + RSN_SELECTOR_LEN;
874 return 0;
877 if (pos[1] > RSN_SELECTOR_LEN + 2 &&
878 memcmp(pos + 2, RSN_KEY_DATA_GROUPKEY, RSN_SELECTOR_LEN) == 0) {
879 ie->gtk = pos + 2 + RSN_SELECTOR_LEN;
880 ie->gtk_len = pos[1] - RSN_SELECTOR_LEN;
883 return 0;
888 * wpa_supplicant_parse_ies - Parse EAPOL-Key Key Data IEs
889 * @buf: Pointer to the Key Data buffer
890 * @len: Key Data Length
891 * @ie: Pointer to parsed IE data
892 * Returns: 0 on success, -1 on failure
894 static int wpa_supplicant_parse_ies(const u8 *buf, size_t len,
895 struct wpa_eapol_ie_parse *ie)
897 const u8 *pos, *end;
898 int ret = 0;
900 memset(ie, 0, sizeof(*ie));
901 for (pos = buf, end = pos + len; pos + 1 < end; pos += 2 + pos[1]) {
902 if (pos + 2 + pos[1] > end) {
903 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key Key Data "
904 "underflow (ie=%d len=%d)", pos[0], pos[1]);
905 ret = -1;
906 break;
908 if (*pos == RSN_INFO_ELEM) {
909 ie->rsn_ie = pos;
910 ie->rsn_ie_len = pos[1] + 2;
911 } else if (*pos == GENERIC_INFO_ELEM) {
912 ret = wpa_supplicant_parse_generic(pos, end, ie);
913 if (ret < 0)
914 break;
915 if (ret > 0) {
916 ret = 0;
917 break;
919 } else {
920 wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized EAPOL-Key "
921 "Key Data IE", pos, 2 + pos[1]);
925 return ret;
929 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
930 const unsigned char *src_addr,
931 const u8 *pmkid)
933 int abort_cached = 0;
935 if (pmkid && !sm->cur_pmksa) {
936 /* When using drivers that generate RSN IE, wpa_supplicant may
937 * not have enough time to get the association information
938 * event before receiving this 1/4 message, so try to find a
939 * matching PMKSA cache entry here. */
940 sm->cur_pmksa = pmksa_cache_get(sm, src_addr, pmkid);
941 if (sm->cur_pmksa) {
942 wpa_printf(MSG_DEBUG, "RSN: found matching PMKID from "
943 "PMKSA cache");
944 } else {
945 wpa_printf(MSG_DEBUG, "RSN: no matching PMKID found");
946 abort_cached = 1;
950 if (pmkid && sm->cur_pmksa &&
951 memcmp(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
952 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
953 wpa_sm_set_pmk_from_pmksa(sm);
954 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
955 sm->pmk, sm->pmk_len);
956 eapol_sm_notify_cached(sm->eapol);
957 } else if (sm->key_mgmt == WPA_KEY_MGMT_IEEE8021X && sm->eapol) {
958 int res, pmk_len;
959 pmk_len = PMK_LEN;
960 res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
961 #ifdef EAP_LEAP
962 if (res) {
963 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
964 pmk_len = 16;
966 #endif /* EAP_LEAP */
967 if (res == 0) {
968 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
969 "machines", sm->pmk, pmk_len);
970 sm->pmk_len = pmk_len;
971 pmksa_cache_add(sm, sm->pmk, pmk_len, src_addr,
972 sm->own_addr, sm->cur_ssid);
973 if (!sm->cur_pmksa && pmkid &&
974 pmksa_cache_get(sm, src_addr, pmkid)) {
975 wpa_printf(MSG_DEBUG, "RSN: the new PMK "
976 "matches with the PMKID");
977 abort_cached = 0;
979 } else {
980 wpa_msg(sm->ctx->ctx, MSG_WARNING,
981 "WPA: Failed to get master session key from "
982 "EAPOL state machines");
983 wpa_msg(sm->ctx->ctx, MSG_WARNING,
984 "WPA: Key handshake aborted");
985 if (sm->cur_pmksa) {
986 wpa_printf(MSG_DEBUG, "RSN: Cancelled PMKSA "
987 "caching attempt");
988 sm->cur_pmksa = NULL;
989 abort_cached = 1;
990 } else {
991 return -1;
996 if (abort_cached && sm->key_mgmt == WPA_KEY_MGMT_IEEE8021X) {
997 /* Send EAPOL-Start to trigger full EAP authentication. */
998 u8 *buf;
999 size_t buflen;
1001 wpa_printf(MSG_DEBUG, "RSN: no PMKSA entry found - trigger "
1002 "full EAP authentication");
1003 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
1004 NULL, 0, &buflen, NULL);
1005 if (buf) {
1006 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
1007 buf, buflen);
1008 free(buf);
1011 return -1;
1014 return 0;
1018 static int wpa_supplicant_send_2_of_4(struct wpa_sm *sm,
1019 const unsigned char *src_addr,
1020 const struct wpa_eapol_key *key,
1021 int ver)
1023 size_t rlen;
1024 struct wpa_eapol_key *reply;
1025 struct wpa_ptk *ptk;
1026 u8 buf[8], *rbuf, *wpa_ie;
1027 int wpa_ie_len;
1029 if (sm->assoc_wpa_ie == NULL) {
1030 wpa_printf(MSG_WARNING, "WPA: No assoc_wpa_ie set - cannot "
1031 "generate msg 2/4");
1032 return -1;
1035 wpa_ie = sm->assoc_wpa_ie;
1036 wpa_ie_len = sm->assoc_wpa_ie_len;
1037 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
1039 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
1040 NULL, sizeof(*reply) + wpa_ie_len,
1041 &rlen, (void *) &reply);
1042 if (rbuf == NULL)
1043 return -1;
1045 reply->type = sm->proto == WPA_PROTO_RSN ?
1046 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1047 WPA_PUT_BE16(reply->key_info,
1048 ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
1049 if (sm->proto == WPA_PROTO_RSN)
1050 WPA_PUT_BE16(reply->key_length, 0);
1051 else
1052 memcpy(reply->key_length, key->key_length, 2);
1053 memcpy(reply->replay_counter, key->replay_counter,
1054 WPA_REPLAY_COUNTER_LEN);
1056 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
1057 memcpy(reply + 1, wpa_ie, wpa_ie_len);
1059 if (sm->renew_snonce) {
1060 if (hostapd_get_rand(sm->snonce, WPA_NONCE_LEN)) {
1061 wpa_msg(sm->ctx->ctx, MSG_WARNING,
1062 "WPA: Failed to get random data for SNonce");
1063 free(rbuf);
1064 return -1;
1066 sm->renew_snonce = 0;
1067 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
1068 sm->snonce, WPA_NONCE_LEN);
1070 memcpy(reply->key_nonce, sm->snonce, WPA_NONCE_LEN);
1072 /* Calculate PTK which will be stored as a temporary PTK until it has
1073 * been verified when processing message 3/4. */
1074 ptk = &sm->tptk;
1075 wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, src_addr,
1076 sm->snonce, key->key_nonce,
1077 (u8 *) ptk, sizeof(*ptk));
1078 /* Supplicant: swap tx/rx Mic keys */
1079 memcpy(buf, ptk->u.auth.tx_mic_key, 8);
1080 memcpy(ptk->u.auth.tx_mic_key, ptk->u.auth.rx_mic_key, 8);
1081 memcpy(ptk->u.auth.rx_mic_key, buf, 8);
1082 sm->tptk_set = 1;
1084 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
1085 wpa_eapol_key_send(sm, ptk->kck, ver, src_addr, ETH_P_EAPOL,
1086 rbuf, rlen, reply->key_mic);
1088 return 0;
1092 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
1093 const unsigned char *src_addr,
1094 const struct wpa_eapol_key *key,
1095 u16 ver)
1097 struct wpa_eapol_ie_parse ie;
1099 if (wpa_sm_get_ssid(sm) == NULL) {
1100 wpa_printf(MSG_WARNING, "WPA: No SSID info found (msg 1 of "
1101 "4).");
1102 return;
1105 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1106 wpa_printf(MSG_DEBUG, "WPA: RX message 1 of 4-Way Handshake from "
1107 MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1109 memset(&ie, 0, sizeof(ie));
1111 if (sm->proto == WPA_PROTO_RSN) {
1112 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
1113 const u8 *buf = (const u8 *) (key + 1);
1114 size_t len = WPA_GET_BE16(key->key_data_length);
1115 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", buf, len);
1116 wpa_supplicant_parse_ies(buf, len, &ie);
1117 if (ie.pmkid) {
1118 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
1119 "Authenticator", ie.pmkid, PMKID_LEN);
1123 if (wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid))
1124 return;
1126 if (wpa_supplicant_send_2_of_4(sm, src_addr, key, ver))
1127 return;
1129 memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
1133 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
1135 struct wpa_sm *sm = eloop_ctx;
1136 rsn_preauth_candidate_process(sm);
1140 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
1141 const u8 *addr, int secure)
1143 wpa_msg(sm->ctx->ctx, MSG_INFO, "WPA: Key negotiation completed with "
1144 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
1145 wpa_cipher_txt(sm->pairwise_cipher),
1146 wpa_cipher_txt(sm->group_cipher));
1147 eloop_cancel_timeout(sm->ctx->scan, sm->ctx->ctx, NULL);
1148 wpa_sm_cancel_auth_timeout(sm);
1149 wpa_sm_set_state(sm, WPA_COMPLETED);
1151 if (secure) {
1152 /* MLME.SETPROTECTION.request(TA, Tx_Rx) */
1153 eapol_sm_notify_portValid(sm->eapol, TRUE);
1154 if (sm->key_mgmt == WPA_KEY_MGMT_PSK)
1155 eapol_sm_notify_eap_success(sm->eapol, TRUE);
1157 * Start preauthentication after a short wait to avoid a
1158 * possible race condition between the data receive and key
1159 * configuration after the 4-Way Handshake. This increases the
1160 * likelyhood of the first preauth EAPOL-Start frame getting to
1161 * the target AP.
1163 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
1166 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
1167 wpa_printf(MSG_DEBUG, "RSN: Authenticator accepted "
1168 "opportunistic PMKSA entry - marking it valid");
1169 sm->cur_pmksa->opportunistic = 0;
1174 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
1175 const unsigned char *src_addr,
1176 const struct wpa_eapol_key *key)
1178 int alg, keylen, rsclen;
1179 const u8 *key_rsc;
1180 u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1182 wpa_printf(MSG_DEBUG, "WPA: Installing PTK to the driver.");
1184 switch (sm->pairwise_cipher) {
1185 case WPA_CIPHER_CCMP:
1186 alg = WPA_ALG_CCMP;
1187 keylen = 16;
1188 rsclen = 6;
1189 break;
1190 case WPA_CIPHER_TKIP:
1191 alg = WPA_ALG_TKIP;
1192 keylen = 32;
1193 rsclen = 6;
1194 break;
1195 case WPA_CIPHER_NONE:
1196 wpa_printf(MSG_DEBUG, "WPA: Pairwise Cipher Suite: "
1197 "NONE - do not use pairwise keys");
1198 return 0;
1199 default:
1200 wpa_printf(MSG_WARNING, "WPA: Unsupported pairwise cipher %d",
1201 sm->pairwise_cipher);
1202 return -1;
1205 if (sm->proto == WPA_PROTO_RSN) {
1206 key_rsc = null_rsc;
1207 } else {
1208 key_rsc = key->key_rsc;
1209 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
1212 if (wpa_sm_set_key(sm, alg, src_addr, 0, 1, key_rsc, rsclen,
1213 (u8 *) &sm->ptk.tk1, keylen) < 0) {
1214 wpa_printf(MSG_WARNING, "WPA: Failed to set PTK to the "
1215 "driver.");
1216 return -1;
1218 return 0;
1222 static int wpa_supplicant_check_group_cipher(int group_cipher,
1223 int keylen, int maxkeylen,
1224 int *key_rsc_len, int *alg)
1226 int ret = 0;
1228 switch (group_cipher) {
1229 case WPA_CIPHER_CCMP:
1230 if (keylen != 16 || maxkeylen < 16) {
1231 ret = -1;
1232 break;
1234 *key_rsc_len = 6;
1235 *alg = WPA_ALG_CCMP;
1236 break;
1237 case WPA_CIPHER_TKIP:
1238 if (keylen != 32 || maxkeylen < 32) {
1239 ret = -1;
1240 break;
1242 *key_rsc_len = 6;
1243 *alg = WPA_ALG_TKIP;
1244 break;
1245 case WPA_CIPHER_WEP104:
1246 if (keylen != 13 || maxkeylen < 13) {
1247 ret = -1;
1248 break;
1250 *key_rsc_len = 0;
1251 *alg = WPA_ALG_WEP;
1252 break;
1253 case WPA_CIPHER_WEP40:
1254 if (keylen != 5 || maxkeylen < 5) {
1255 ret = -1;
1256 break;
1258 *key_rsc_len = 0;
1259 *alg = WPA_ALG_WEP;
1260 break;
1261 default:
1262 wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
1263 group_cipher);
1264 return -1;
1267 if (ret < 0 ) {
1268 wpa_printf(MSG_WARNING, "WPA: Unsupported %s Group Cipher key "
1269 "length %d (%d).",
1270 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
1273 return ret;
1277 struct wpa_gtk_data {
1278 int alg, tx, key_rsc_len, keyidx;
1279 u8 gtk[32];
1280 int gtk_len;
1284 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
1285 const struct wpa_gtk_data *gd,
1286 const u8 *key_rsc)
1288 const u8 *_gtk = gd->gtk;
1289 u8 gtk_buf[32];
1291 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
1292 wpa_printf(MSG_DEBUG, "WPA: Installing GTK to the driver "
1293 "(keyidx=%d tx=%d).", gd->keyidx, gd->tx);
1294 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
1295 if (sm->group_cipher == WPA_CIPHER_TKIP) {
1296 /* Swap Tx/Rx keys for Michael MIC */
1297 memcpy(gtk_buf, gd->gtk, 16);
1298 memcpy(gtk_buf + 16, gd->gtk + 24, 8);
1299 memcpy(gtk_buf + 24, gd->gtk + 16, 8);
1300 _gtk = gtk_buf;
1302 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
1303 if (wpa_sm_set_key(sm, gd->alg,
1304 (u8 *) "\xff\xff\xff\xff\xff\xff",
1305 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
1306 _gtk, gd->gtk_len) < 0) {
1307 wpa_printf(MSG_WARNING, "WPA: Failed to set "
1308 "GTK to the driver (Group only).");
1309 return -1;
1311 } else if (wpa_sm_set_key(sm, gd->alg,
1312 (u8 *) "\xff\xff\xff\xff\xff\xff",
1313 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
1314 _gtk, gd->gtk_len) < 0) {
1315 wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to "
1316 "the driver.");
1317 return -1;
1320 return 0;
1324 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
1325 int tx)
1327 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
1328 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
1329 * seemed to set this bit (incorrectly, since Tx is only when
1330 * doing Group Key only APs) and without this workaround, the
1331 * data connection does not work because wpa_supplicant
1332 * configured non-zero keyidx to be used for unicast. */
1333 wpa_printf(MSG_INFO, "WPA: Tx bit set for GTK, but pairwise "
1334 "keys are used - ignore Tx bit");
1335 return 0;
1337 return tx;
1341 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
1342 const unsigned char *src_addr,
1343 const struct wpa_eapol_key *key,
1344 const u8 *gtk, int gtk_len,
1345 int key_info)
1347 struct wpa_gtk_data gd;
1350 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
1351 * GTK KDE format:
1352 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
1353 * Reserved [bits 0-7]
1354 * GTK
1357 memset(&gd, 0, sizeof(gd));
1358 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
1359 gtk, gtk_len);
1361 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
1362 return -1;
1364 gd.keyidx = gtk[0] & 0x3;
1365 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1366 !!(gtk[0] & BIT(2)));
1367 gtk += 2;
1368 gtk_len -= 2;
1370 memcpy(gd.gtk, gtk, gtk_len);
1371 gd.gtk_len = gtk_len;
1373 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
1374 gtk_len, gtk_len,
1375 &gd.key_rsc_len, &gd.alg) ||
1376 wpa_supplicant_install_gtk(sm, &gd, key->key_rsc)) {
1377 wpa_printf(MSG_DEBUG, "RSN: Failed to install GTK");
1378 return -1;
1381 wpa_supplicant_key_neg_complete(sm, src_addr,
1382 key_info & WPA_KEY_INFO_SECURE);
1383 return 0;
1387 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1388 const char *reason, const u8 *src_addr,
1389 const u8 *wpa_ie, size_t wpa_ie_len,
1390 const u8 *rsn_ie, size_t rsn_ie_len)
1392 wpa_msg(sm->ctx->ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1393 reason, MAC2STR(src_addr));
1395 if (sm->ap_wpa_ie) {
1396 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1397 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1399 if (wpa_ie) {
1400 if (!sm->ap_wpa_ie) {
1401 wpa_printf(MSG_INFO, "WPA: No WPA IE in "
1402 "Beacon/ProbeResp");
1404 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1405 wpa_ie, wpa_ie_len);
1408 if (sm->ap_rsn_ie) {
1409 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1410 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1412 if (rsn_ie) {
1413 if (!sm->ap_rsn_ie) {
1414 wpa_printf(MSG_INFO, "WPA: No RSN IE in "
1415 "Beacon/ProbeResp");
1417 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1418 rsn_ie, rsn_ie_len);
1421 wpa_sm_disassociate(sm, REASON_IE_IN_4WAY_DIFFERS);
1422 wpa_sm_req_scan(sm, 0, 0);
1426 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1427 const unsigned char *src_addr,
1428 struct wpa_eapol_ie_parse *ie)
1430 struct wpa_ssid *ssid = sm->cur_ssid;
1432 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1433 wpa_printf(MSG_DEBUG, "WPA: No WPA/RSN IE for this AP known. "
1434 "Trying to get from scan results");
1435 if (wpa_sm_get_beacon_ie(sm) < 0) {
1436 wpa_printf(MSG_WARNING, "WPA: Could not find AP from "
1437 "the scan results");
1438 } else {
1439 wpa_printf(MSG_DEBUG, "WPA: Found the current AP from "
1440 "updated scan results");
1444 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1445 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1446 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1447 "with IE in Beacon/ProbeResp (no IE?)",
1448 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1449 ie->rsn_ie, ie->rsn_ie_len);
1450 return -1;
1453 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1454 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1455 memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1456 (ie->rsn_ie && sm->ap_rsn_ie &&
1457 (ie->rsn_ie_len != sm->ap_rsn_ie_len ||
1458 memcmp(ie->rsn_ie, sm->ap_rsn_ie, ie->rsn_ie_len) != 0))) {
1459 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1460 "with IE in Beacon/ProbeResp",
1461 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1462 ie->rsn_ie, ie->rsn_ie_len);
1463 return -1;
1466 if (sm->proto == WPA_PROTO_WPA &&
1467 ie->rsn_ie && sm->ap_rsn_ie == NULL &&
1468 ssid && (ssid->proto & WPA_PROTO_RSN)) {
1469 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1470 "detected - RSN was enabled and RSN IE "
1471 "was in msg 3/4, but not in "
1472 "Beacon/ProbeResp",
1473 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1474 ie->rsn_ie, ie->rsn_ie_len);
1475 return -1;
1478 return 0;
1482 static int wpa_supplicant_send_4_of_4(struct wpa_sm *sm,
1483 const unsigned char *src_addr,
1484 const struct wpa_eapol_key *key,
1485 u16 ver, u16 key_info)
1487 size_t rlen;
1488 struct wpa_eapol_key *reply;
1489 u8 *rbuf;
1491 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1492 sizeof(*reply), &rlen, (void *) &reply);
1493 if (rbuf == NULL)
1494 return -1;
1496 reply->type = sm->proto == WPA_PROTO_RSN ?
1497 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1498 key_info &= WPA_KEY_INFO_SECURE;
1499 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
1500 WPA_PUT_BE16(reply->key_info, key_info);
1501 if (sm->proto == WPA_PROTO_RSN)
1502 WPA_PUT_BE16(reply->key_length, 0);
1503 else
1504 memcpy(reply->key_length, key->key_length, 2);
1505 memcpy(reply->replay_counter, key->replay_counter,
1506 WPA_REPLAY_COUNTER_LEN);
1508 WPA_PUT_BE16(reply->key_data_length, 0);
1510 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
1511 wpa_eapol_key_send(sm, sm->ptk.kck, ver, src_addr, ETH_P_EAPOL,
1512 rbuf, rlen, reply->key_mic);
1514 return 0;
1518 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1519 const unsigned char *src_addr,
1520 const struct wpa_eapol_key *key,
1521 int extra_len, u16 ver)
1523 u16 key_info, keylen, len;
1524 const u8 *pos;
1525 struct wpa_eapol_ie_parse ie;
1527 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1528 wpa_printf(MSG_DEBUG, "WPA: RX message 3 of 4-Way Handshake from "
1529 MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1531 key_info = WPA_GET_BE16(key->key_info);
1533 pos = (const u8 *) (key + 1);
1534 len = WPA_GET_BE16(key->key_data_length);
1535 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", pos, len);
1536 wpa_supplicant_parse_ies(pos, len, &ie);
1537 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1538 wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
1539 return;
1542 if (wpa_supplicant_validate_ie(sm, src_addr, &ie) < 0)
1543 return;
1545 if (memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1546 wpa_printf(MSG_WARNING, "WPA: ANonce from message 1 of 4-Way "
1547 "Handshake differs from 3 of 4-Way Handshake - drop"
1548 " packet (src=" MACSTR ")", MAC2STR(src_addr));
1549 return;
1552 keylen = WPA_GET_BE16(key->key_length);
1553 switch (sm->pairwise_cipher) {
1554 case WPA_CIPHER_CCMP:
1555 if (keylen != 16) {
1556 wpa_printf(MSG_WARNING, "WPA: Invalid CCMP key length "
1557 "%d (src=" MACSTR ")",
1558 keylen, MAC2STR(src_addr));
1559 return;
1561 break;
1562 case WPA_CIPHER_TKIP:
1563 if (keylen != 32) {
1564 wpa_printf(MSG_WARNING, "WPA: Invalid TKIP key length "
1565 "%d (src=" MACSTR ")",
1566 keylen, MAC2STR(src_addr));
1567 return;
1569 break;
1572 if (wpa_supplicant_send_4_of_4(sm, src_addr, key, ver, key_info))
1573 return;
1575 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1576 * for the next 4-Way Handshake. If msg 3 is received again, the old
1577 * SNonce will still be used to avoid changing PTK. */
1578 sm->renew_snonce = 1;
1580 if (key_info & WPA_KEY_INFO_INSTALL) {
1581 wpa_supplicant_install_ptk(sm, src_addr, key);
1584 if (key_info & WPA_KEY_INFO_SECURE) {
1585 /* MLME.SETPROTECTION.request(TA, Tx_Rx) */
1586 eapol_sm_notify_portValid(sm->eapol, TRUE);
1588 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1590 if (ie.gtk &&
1591 wpa_supplicant_pairwise_gtk(sm, src_addr, key,
1592 ie.gtk, ie.gtk_len, key_info) < 0) {
1593 wpa_printf(MSG_INFO, "RSN: Failed to configure GTK");
1598 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1599 const u8 *keydata,
1600 size_t keydatalen,
1601 int key_info,
1602 struct wpa_gtk_data *gd)
1604 int maxkeylen;
1605 struct wpa_eapol_ie_parse ie;
1607 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
1608 wpa_supplicant_parse_ies(keydata, keydatalen, &ie);
1609 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1610 wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
1611 return -1;
1613 if (ie.gtk == NULL) {
1614 wpa_printf(MSG_INFO, "WPA: No GTK IE in Group Key msg 1/2");
1615 return -1;
1617 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1619 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
1620 gd->gtk_len, maxkeylen,
1621 &gd->key_rsc_len, &gd->alg))
1622 return -1;
1624 wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
1625 ie.gtk, ie.gtk_len);
1626 gd->keyidx = ie.gtk[0] & 0x3;
1627 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1628 !!(ie.gtk[0] & BIT(2)));
1629 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1630 wpa_printf(MSG_INFO, "RSN: Too long GTK in GTK IE "
1631 "(len=%lu)", (unsigned long) ie.gtk_len - 2);
1632 return -1;
1634 memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1636 return 0;
1640 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1641 const struct wpa_eapol_key *key,
1642 size_t keydatalen, int key_info,
1643 int extra_len, u16 ver,
1644 struct wpa_gtk_data *gd)
1646 int maxkeylen;
1647 u8 ek[32];
1649 gd->gtk_len = WPA_GET_BE16(key->key_length);
1650 maxkeylen = keydatalen;
1651 if (keydatalen > extra_len) {
1652 wpa_printf(MSG_INFO, "WPA: Truncated EAPOL-Key packet:"
1653 " key_data_length=%lu > extra_len=%d",
1654 (unsigned long) keydatalen, extra_len);
1655 return -1;
1657 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES)
1658 maxkeylen -= 8;
1660 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
1661 gd->gtk_len, maxkeylen,
1662 &gd->key_rsc_len, &gd->alg))
1663 return -1;
1665 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1666 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1667 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1668 memcpy(ek, key->key_iv, 16);
1669 memcpy(ek + 16, sm->ptk.kek, 16);
1670 if (keydatalen > sizeof(gd->gtk)) {
1671 wpa_printf(MSG_WARNING, "WPA: RC4 key data "
1672 "too long (%lu)",
1673 (unsigned long) keydatalen);
1674 return -1;
1676 memcpy(gd->gtk, key + 1, keydatalen);
1677 rc4_skip(ek, 32, 256, gd->gtk, keydatalen);
1678 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1679 if (keydatalen % 8) {
1680 wpa_printf(MSG_WARNING, "WPA: Unsupported AES-WRAP "
1681 "len %lu", (unsigned long) keydatalen);
1682 return -1;
1684 if (maxkeylen > sizeof(gd->gtk)) {
1685 wpa_printf(MSG_WARNING, "WPA: AES-WRAP key data "
1686 "too long (keydatalen=%lu maxkeylen=%lu)",
1687 (unsigned long) keydatalen,
1688 (unsigned long) maxkeylen);
1689 return -1;
1691 if (aes_unwrap(sm->ptk.kek, maxkeylen / 8,
1692 (const u8 *) (key + 1), gd->gtk)) {
1693 wpa_printf(MSG_WARNING, "WPA: AES unwrap "
1694 "failed - could not decrypt GTK");
1695 return -1;
1698 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1699 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1700 return 0;
1704 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1705 const unsigned char *src_addr,
1706 const struct wpa_eapol_key *key,
1707 int ver, u16 key_info)
1709 size_t rlen;
1710 struct wpa_eapol_key *reply;
1711 u8 *rbuf;
1713 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1714 sizeof(*reply), &rlen, (void *) &reply);
1715 if (rbuf == NULL)
1716 return -1;
1718 reply->type = sm->proto == WPA_PROTO_RSN ?
1719 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1720 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1721 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1722 WPA_PUT_BE16(reply->key_info, key_info);
1723 if (sm->proto == WPA_PROTO_RSN)
1724 WPA_PUT_BE16(reply->key_length, 0);
1725 else
1726 memcpy(reply->key_length, key->key_length, 2);
1727 memcpy(reply->replay_counter, key->replay_counter,
1728 WPA_REPLAY_COUNTER_LEN);
1730 WPA_PUT_BE16(reply->key_data_length, 0);
1732 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1733 wpa_eapol_key_send(sm, sm->ptk.kck, ver, src_addr, ETH_P_EAPOL,
1734 rbuf, rlen, reply->key_mic);
1736 return 0;
1740 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1741 const unsigned char *src_addr,
1742 const struct wpa_eapol_key *key,
1743 int extra_len, u16 ver)
1745 u16 key_info, keydatalen;
1746 int rekey;
1747 struct wpa_gtk_data gd;
1749 memset(&gd, 0, sizeof(gd));
1751 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1752 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1753 wpa_printf(MSG_DEBUG, "WPA: RX message 1 of Group Key Handshake from "
1754 MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1756 key_info = WPA_GET_BE16(key->key_info);
1757 keydatalen = WPA_GET_BE16(key->key_data_length);
1759 if (sm->proto == WPA_PROTO_RSN) {
1760 if (wpa_supplicant_process_1_of_2_rsn(sm,
1761 (const u8 *) (key + 1),
1762 keydatalen, key_info,
1763 &gd))
1764 return;
1765 } else {
1766 if (wpa_supplicant_process_1_of_2_wpa(sm, key, keydatalen,
1767 key_info, extra_len,
1768 ver, &gd))
1769 return;
1772 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1773 wpa_supplicant_send_2_of_2(sm, src_addr, key, ver, key_info))
1774 return;
1776 if (rekey) {
1777 wpa_msg(sm->ctx->ctx, MSG_INFO, "WPA: Group rekeying "
1778 "completed with " MACSTR " [GTK=%s]",
1779 MAC2STR(src_addr), wpa_cipher_txt(sm->group_cipher));
1780 wpa_sm_set_state(sm, WPA_COMPLETED);
1781 } else {
1782 wpa_supplicant_key_neg_complete(sm, src_addr,
1783 key_info &
1784 WPA_KEY_INFO_SECURE);
1789 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1790 struct wpa_eapol_key *key,
1791 u16 ver,
1792 const u8 *buf, size_t len)
1794 u8 mic[16];
1795 int ok = 0;
1797 memcpy(mic, key->key_mic, 16);
1798 if (sm->tptk_set) {
1799 memset(key->key_mic, 0, 16);
1800 wpa_eapol_key_mic(sm->tptk.kck, ver, buf, len,
1801 key->key_mic);
1802 if (memcmp(mic, key->key_mic, 16) != 0) {
1803 wpa_printf(MSG_WARNING, "WPA: Invalid EAPOL-Key MIC "
1804 "when using TPTK - ignoring TPTK");
1805 } else {
1806 ok = 1;
1807 sm->tptk_set = 0;
1808 sm->ptk_set = 1;
1809 memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1813 if (!ok && sm->ptk_set) {
1814 memset(key->key_mic, 0, 16);
1815 wpa_eapol_key_mic(sm->ptk.kck, ver, buf, len,
1816 key->key_mic);
1817 if (memcmp(mic, key->key_mic, 16) != 0) {
1818 wpa_printf(MSG_WARNING, "WPA: Invalid EAPOL-Key MIC "
1819 "- dropping packet");
1820 return -1;
1822 ok = 1;
1825 if (!ok) {
1826 wpa_printf(MSG_WARNING, "WPA: Could not verify EAPOL-Key MIC "
1827 "- dropping packet");
1828 return -1;
1831 memcpy(sm->rx_replay_counter, key->replay_counter,
1832 WPA_REPLAY_COUNTER_LEN);
1833 sm->rx_replay_counter_set = 1;
1834 return 0;
1838 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1839 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1840 struct wpa_eapol_key *key, u16 ver)
1842 u16 keydatalen = WPA_GET_BE16(key->key_data_length);
1844 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1845 (u8 *) (key + 1), keydatalen);
1846 if (!sm->ptk_set) {
1847 wpa_printf(MSG_WARNING, "WPA: PTK not available, "
1848 "cannot decrypt EAPOL-Key key data.");
1849 return -1;
1852 /* Decrypt key data here so that this operation does not need
1853 * to be implemented separately for each message type. */
1854 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1855 u8 ek[32];
1856 memcpy(ek, key->key_iv, 16);
1857 memcpy(ek + 16, sm->ptk.kek, 16);
1858 rc4_skip(ek, 32, 256, (u8 *) (key + 1), keydatalen);
1859 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1860 u8 *buf;
1861 if (keydatalen % 8) {
1862 wpa_printf(MSG_WARNING, "WPA: Unsupported "
1863 "AES-WRAP len %d", keydatalen);
1864 return -1;
1866 keydatalen -= 8; /* AES-WRAP adds 8 bytes */
1867 buf = malloc(keydatalen);
1868 if (buf == NULL) {
1869 wpa_printf(MSG_WARNING, "WPA: No memory for "
1870 "AES-UNWRAP buffer");
1871 return -1;
1873 if (aes_unwrap(sm->ptk.kek, keydatalen / 8,
1874 (u8 *) (key + 1), buf)) {
1875 free(buf);
1876 wpa_printf(MSG_WARNING, "WPA: AES unwrap failed - "
1877 "could not decrypt EAPOL-Key key data");
1878 return -1;
1880 memcpy(key + 1, buf, keydatalen);
1881 free(buf);
1882 WPA_PUT_BE16(key->key_data_length, keydatalen);
1884 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1885 (u8 *) (key + 1), keydatalen);
1886 return 0;
1891 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1892 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1894 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1896 if (sm && sm->cur_pmksa) {
1897 wpa_printf(MSG_DEBUG, "RSN: Cancelling PMKSA caching attempt");
1898 sm->cur_pmksa = NULL;
1903 static void wpa_eapol_key_dump(const struct wpa_eapol_key *key)
1905 #ifndef CONFIG_NO_STDOUT_DEBUG
1906 u16 key_info = WPA_GET_BE16(key->key_info);
1908 wpa_printf(MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1909 wpa_printf(MSG_DEBUG, " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s"
1910 "%s%s%s%s%s%s%s)",
1911 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1912 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1913 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1914 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1915 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1916 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1917 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1918 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1919 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1920 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1921 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1922 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1923 wpa_printf(MSG_DEBUG, " key_length=%u key_data_length=%u",
1924 WPA_GET_BE16(key->key_length),
1925 WPA_GET_BE16(key->key_data_length));
1926 wpa_hexdump(MSG_DEBUG, " replay_counter",
1927 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1928 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1929 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1930 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1931 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1932 wpa_hexdump(MSG_DEBUG, " key_mic", key->key_mic, 16);
1933 #endif /* CONFIG_NO_STDOUT_DEBUG */
1938 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1939 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1940 * @src_addr: Source MAC address of the EAPOL packet
1941 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1942 * @len: Length of the EAPOL frame
1943 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1945 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1946 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1947 * only processing WPA and WPA2 EAPOL-Key frames.
1949 * The received EAPOL-Key packets are validated and valid packets are replied
1950 * to. In addition, key material (PTK, GTK) is configured at the end of a
1951 * successful key handshake.
1953 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1954 const u8 *buf, size_t len)
1956 size_t plen, data_len, extra_len;
1957 struct ieee802_1x_hdr *hdr;
1958 struct wpa_eapol_key *key;
1959 u16 key_info, ver;
1960 u8 *tmp;
1961 int ret = -1;
1963 if (len < sizeof(*hdr) + sizeof(*key)) {
1964 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame too short to be a WPA "
1965 "EAPOL-Key (len %lu, expecting at least %lu)",
1966 (unsigned long) len,
1967 (unsigned long) sizeof(*hdr) + sizeof(*key));
1968 return 0;
1971 tmp = malloc(len);
1972 if (tmp == NULL)
1973 return -1;
1974 memcpy(tmp, buf, len);
1976 hdr = (struct ieee802_1x_hdr *) tmp;
1977 key = (struct wpa_eapol_key *) (hdr + 1);
1978 plen = ntohs(hdr->length);
1979 data_len = plen + sizeof(*hdr);
1980 wpa_printf(MSG_DEBUG, "IEEE 802.1X RX: version=%d type=%d length=%lu",
1981 hdr->version, hdr->type, (unsigned long) plen);
1983 if (hdr->version < EAPOL_VERSION) {
1984 /* TODO: backwards compatibility */
1986 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1987 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame (type %u) discarded, "
1988 "not a Key frame", hdr->type);
1989 ret = 0;
1990 goto out;
1992 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
1993 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame payload size %lu "
1994 "invalid (frame size %lu)",
1995 (unsigned long) plen, (unsigned long) len);
1996 ret = 0;
1997 goto out;
2000 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
2002 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key type (%d) unknown, "
2003 "discarded", key->type);
2004 ret = 0;
2005 goto out;
2007 wpa_eapol_key_dump(key);
2009 eapol_sm_notify_lower_layer_success(sm->eapol);
2010 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", tmp, len);
2011 if (data_len < len) {
2012 wpa_printf(MSG_DEBUG, "WPA: ignoring %lu bytes after the IEEE "
2013 "802.1X data", (unsigned long) len - data_len);
2015 key_info = WPA_GET_BE16(key->key_info);
2016 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
2017 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
2018 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
2019 wpa_printf(MSG_INFO, "WPA: Unsupported EAPOL-Key descriptor "
2020 "version %d.", ver);
2021 goto out;
2024 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
2025 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
2026 wpa_printf(MSG_INFO, "WPA: CCMP is used, but EAPOL-Key "
2027 "descriptor version (%d) is not 2.", ver);
2028 if (sm->group_cipher != WPA_CIPHER_CCMP &&
2029 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
2030 /* Earlier versions of IEEE 802.11i did not explicitly
2031 * require version 2 descriptor for all EAPOL-Key
2032 * packets, so allow group keys to use version 1 if
2033 * CCMP is not used for them. */
2034 wpa_printf(MSG_INFO, "WPA: Backwards compatibility: "
2035 "allow invalid version for non-CCMP group "
2036 "keys");
2037 } else
2038 goto out;
2041 if (sm->rx_replay_counter_set &&
2042 memcmp(key->replay_counter, sm->rx_replay_counter,
2043 WPA_REPLAY_COUNTER_LEN) <= 0) {
2044 wpa_printf(MSG_WARNING, "WPA: EAPOL-Key Replay Counter did not"
2045 " increase - dropping packet");
2046 goto out;
2049 if (!(key_info & WPA_KEY_INFO_ACK)) {
2050 wpa_printf(MSG_INFO, "WPA: No Ack bit in key_info");
2051 goto out;
2054 if (key_info & WPA_KEY_INFO_REQUEST) {
2055 wpa_printf(MSG_INFO, "WPA: EAPOL-Key with Request bit - "
2056 "dropped");
2057 goto out;
2060 if ((key_info & WPA_KEY_INFO_MIC) &&
2061 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
2062 goto out;
2064 extra_len = data_len - sizeof(*hdr) - sizeof(*key);
2066 if (WPA_GET_BE16(key->key_data_length) > extra_len) {
2067 wpa_msg(sm->ctx->ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
2068 "frame - key_data overflow (%d > %lu)",
2069 WPA_GET_BE16(key->key_data_length),
2070 (unsigned long) extra_len);
2071 goto out;
2074 if (sm->proto == WPA_PROTO_RSN &&
2075 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) &&
2076 wpa_supplicant_decrypt_key_data(sm, key, ver))
2077 goto out;
2079 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2080 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2081 wpa_printf(MSG_WARNING, "WPA: Ignored EAPOL-Key "
2082 "(Pairwise) with non-zero key index");
2083 goto out;
2085 if (key_info & WPA_KEY_INFO_MIC) {
2086 /* 3/4 4-Way Handshake */
2087 wpa_supplicant_process_3_of_4(sm, src_addr, key,
2088 extra_len, ver);
2089 } else {
2090 /* 1/4 4-Way Handshake */
2091 wpa_supplicant_process_1_of_4(sm, src_addr, key,
2092 ver);
2094 } else {
2095 if (key_info & WPA_KEY_INFO_MIC) {
2096 /* 1/2 Group Key Handshake */
2097 wpa_supplicant_process_1_of_2(sm, src_addr, key,
2098 extra_len, ver);
2099 } else {
2100 wpa_printf(MSG_WARNING, "WPA: EAPOL-Key (Group) "
2101 "without Mic bit - dropped");
2105 ret = 1;
2107 out:
2108 free(tmp);
2109 return ret;
2113 static int wpa_cipher_bits(int cipher)
2115 switch (cipher) {
2116 case WPA_CIPHER_CCMP:
2117 return 128;
2118 case WPA_CIPHER_TKIP:
2119 return 256;
2120 case WPA_CIPHER_WEP104:
2121 return 104;
2122 case WPA_CIPHER_WEP40:
2123 return 40;
2124 default:
2125 return 0;
2130 static const u8 * wpa_key_mgmt_suite(struct wpa_sm *sm)
2132 static const u8 *dummy = (u8 *) "\x00\x00\x00\x00";
2133 switch (sm->key_mgmt) {
2134 case WPA_KEY_MGMT_IEEE8021X:
2135 return (sm->proto == WPA_PROTO_RSN ?
2136 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2137 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2138 case WPA_KEY_MGMT_PSK:
2139 return (sm->proto == WPA_PROTO_RSN ?
2140 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2141 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2142 case WPA_KEY_MGMT_WPA_NONE:
2143 return WPA_AUTH_KEY_MGMT_NONE;
2144 default:
2145 return dummy;
2150 static const u8 * wpa_cipher_suite(struct wpa_sm *sm, int cipher)
2152 static const u8 *dummy = (u8 *) "\x00\x00\x00\x00";
2153 switch (cipher) {
2154 case WPA_CIPHER_CCMP:
2155 return (sm->proto == WPA_PROTO_RSN ?
2156 RSN_CIPHER_SUITE_CCMP : WPA_CIPHER_SUITE_CCMP);
2157 case WPA_CIPHER_TKIP:
2158 return (sm->proto == WPA_PROTO_RSN ?
2159 RSN_CIPHER_SUITE_TKIP : WPA_CIPHER_SUITE_TKIP);
2160 case WPA_CIPHER_WEP104:
2161 return (sm->proto == WPA_PROTO_RSN ?
2162 RSN_CIPHER_SUITE_WEP104 : WPA_CIPHER_SUITE_WEP104);
2163 case WPA_CIPHER_WEP40:
2164 return (sm->proto == WPA_PROTO_RSN ?
2165 RSN_CIPHER_SUITE_WEP40 : WPA_CIPHER_SUITE_WEP40);
2166 case WPA_CIPHER_NONE:
2167 return (sm->proto == WPA_PROTO_RSN ?
2168 RSN_CIPHER_SUITE_NONE : WPA_CIPHER_SUITE_NONE);
2169 default:
2170 return dummy;
2175 #define RSN_SUITE "%02x-%02x-%02x-%d"
2176 #define RSN_SUITE_ARG(s) (s)[0], (s)[1], (s)[2], (s)[3]
2179 * wpa_sm_get_mib - Dump text list of MIB entries
2180 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2181 * @buf: Buffer for the list
2182 * @buflen: Length of the buffer
2183 * Returns: Number of bytes written to buffer
2185 * This function is used fetch dot11 MIB variables.
2187 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2189 int len, i;
2190 char pmkid_txt[PMKID_LEN * 2 + 1];
2191 int rsna;
2193 if (sm->cur_pmksa) {
2194 char *pos = pmkid_txt;
2195 for (i = 0; i < PMKID_LEN; i++) {
2196 pos += sprintf(pos, "%02x", sm->cur_pmksa->pmkid[i]);
2198 } else
2199 pmkid_txt[0] = '\0';
2201 if ((sm->key_mgmt == WPA_KEY_MGMT_PSK ||
2202 sm->key_mgmt == WPA_KEY_MGMT_IEEE8021X) &&
2203 sm->proto == WPA_PROTO_RSN)
2204 rsna = 1;
2205 else
2206 rsna = 0;
2208 len = snprintf(buf, buflen,
2209 "dot11RSNAOptionImplemented=TRUE\n"
2210 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2211 "dot11RSNAEnabled=%s\n"
2212 "dot11RSNAPreauthenticationEnabled=%s\n"
2213 "dot11RSNAConfigVersion=%d\n"
2214 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2215 "dot11RSNAConfigGroupCipherSize=%d\n"
2216 "dot11RSNAConfigPMKLifetime=%d\n"
2217 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2218 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2219 "dot11RSNAConfigSATimeout=%d\n"
2220 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2221 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2222 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2223 "dot11RSNAPMKIDUsed=%s\n"
2224 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2225 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2226 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2227 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2228 "dot11RSNA4WayHandshakeFailures=%u\n",
2229 rsna ? "TRUE" : "FALSE",
2230 rsna ? "TRUE" : "FALSE",
2231 RSN_VERSION,
2232 wpa_cipher_bits(sm->group_cipher),
2233 sm->dot11RSNAConfigPMKLifetime,
2234 sm->dot11RSNAConfigPMKReauthThreshold,
2235 sm->dot11RSNAConfigSATimeout,
2236 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2237 RSN_SUITE_ARG(wpa_cipher_suite(sm,
2238 sm->pairwise_cipher)),
2239 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
2240 pmkid_txt,
2241 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2242 RSN_SUITE_ARG(wpa_cipher_suite(sm,
2243 sm->pairwise_cipher)),
2244 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
2245 sm->dot11RSNA4WayHandshakeFailures);
2246 return len;
2251 * wpa_sm_init - Initialize WPA state machine
2252 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2253 * Returns: Pointer to the allocated WPA state machine data
2255 * This function is used to allocate a new WPA state machine and the returned
2256 * value is passed to all WPA state machine calls.
2258 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2260 struct wpa_sm *sm;
2262 sm = malloc(sizeof(*sm));
2263 if (sm == NULL)
2264 return NULL;
2265 memset(sm, 0, sizeof(*sm));
2266 sm->renew_snonce = 1;
2267 sm->ctx = ctx;
2269 sm->dot11RSNAConfigPMKLifetime = 43200;
2270 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2271 sm->dot11RSNAConfigSATimeout = 60;
2273 return sm;
2278 * wpa_sm_deinit - Deinitialize WPA state machine
2279 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2281 void wpa_sm_deinit(struct wpa_sm *sm)
2283 if (sm == NULL)
2284 return;
2285 eloop_cancel_timeout(wpa_sm_start_preauth, sm, 0);
2286 free(sm->assoc_wpa_ie);
2287 free(sm->ap_wpa_ie);
2288 free(sm->ap_rsn_ie);
2289 free(sm->ctx);
2290 free(sm);
2295 * wpa_sm_notify_assoc - Notify WPA state machine about association
2296 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2297 * @bssid: The BSSID of the new association
2299 * This function is called to let WPA state machine know that the connection
2300 * was established.
2302 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2304 if (sm == NULL)
2305 return;
2307 wpa_printf(MSG_DEBUG, "WPA: Association event - clear replay counter");
2308 memcpy(sm->bssid, bssid, ETH_ALEN);
2309 memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2310 sm->rx_replay_counter_set = 0;
2311 sm->renew_snonce = 1;
2312 if (memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2313 rsn_preauth_deinit(sm);
2318 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2319 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2321 * This function is called to let WPA state machine know that the connection
2322 * was lost. This will abort any existing pre-authentication session.
2324 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2326 rsn_preauth_deinit(sm);
2327 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2328 sm->dot11RSNA4WayHandshakeFailures++;
2333 * wpa_sm_set_pmk - Set PMK
2334 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2335 * @pmk: The new PMK
2336 * @pmk_len: The length of the new PMK in bytes
2338 * Configure the PMK for WPA state machine.
2340 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len)
2342 if (sm == NULL)
2343 return;
2345 sm->pmk_len = pmk_len;
2346 memcpy(sm->pmk, pmk, pmk_len);
2351 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2352 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2354 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2355 * will be cleared.
2357 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2359 if (sm == NULL)
2360 return;
2362 if (sm->cur_pmksa) {
2363 sm->pmk_len = sm->cur_pmksa->pmk_len;
2364 memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2365 } else {
2366 sm->pmk_len = PMK_LEN;
2367 memset(sm->pmk, 0, PMK_LEN);
2373 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2374 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2375 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2377 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2379 if (sm)
2380 sm->fast_reauth = fast_reauth;
2385 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2386 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2387 * @scard_ctx: Context pointer for smartcard related callback functions
2389 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2391 if (sm == NULL)
2392 return;
2393 sm->scard_ctx = scard_ctx;
2394 if (sm->preauth_eapol)
2395 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2400 * wpa_sm_set_config - Notification of current configration change
2401 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2402 * @config: Pointer to current network configuration
2404 * Notify WPA state machine that configuration has changed. config will be
2405 * stored as a backpointer to network configuration. This can be %NULL to clear
2406 * the stored pointed.
2408 void wpa_sm_set_config(struct wpa_sm *sm, struct wpa_ssid *config)
2410 if (sm)
2411 sm->cur_ssid = config;
2416 * wpa_sm_set_own_addr - Set own MAC address
2417 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2418 * @addr: Own MAC address
2420 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2422 if (sm)
2423 memcpy(sm->own_addr, addr, ETH_ALEN);
2428 * wpa_sm_set_ifname - Set network interface name
2429 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2430 * @ifname: Interface name
2432 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname)
2434 if (sm)
2435 sm->ifname = ifname;
2440 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2441 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2442 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2444 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2446 if (sm)
2447 sm->eapol = eapol;
2452 * wpa_sm_set_param - Set WPA state machine parameters
2453 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2454 * @param: Parameter field
2455 * @value: Parameter value
2456 * Returns: 0 on success, -1 on failure
2458 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2459 unsigned int value)
2461 int ret = 0;
2463 if (sm == NULL)
2464 return -1;
2466 switch (param) {
2467 case RSNA_PMK_LIFETIME:
2468 if (value > 0)
2469 sm->dot11RSNAConfigPMKLifetime = value;
2470 else
2471 ret = -1;
2472 break;
2473 case RSNA_PMK_REAUTH_THRESHOLD:
2474 if (value > 0 && value <= 100)
2475 sm->dot11RSNAConfigPMKReauthThreshold = value;
2476 else
2477 ret = -1;
2478 break;
2479 case RSNA_SA_TIMEOUT:
2480 if (value > 0)
2481 sm->dot11RSNAConfigSATimeout = value;
2482 else
2483 ret = -1;
2484 break;
2485 case WPA_PARAM_PROTO:
2486 sm->proto = value;
2487 break;
2488 case WPA_PARAM_PAIRWISE:
2489 sm->pairwise_cipher = value;
2490 break;
2491 case WPA_PARAM_GROUP:
2492 sm->group_cipher = value;
2493 break;
2494 case WPA_PARAM_KEY_MGMT:
2495 sm->key_mgmt = value;
2496 break;
2499 return ret;
2504 * wpa_sm_get_param - Get WPA state machine parameters
2505 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2506 * @param: Parameter field
2507 * Returns: Parameter value
2509 unsigned int wpa_sm_get_param(struct wpa_sm *sm, enum wpa_sm_conf_params param)
2511 if (sm == NULL)
2512 return 0;
2514 switch (param) {
2515 case RSNA_PMK_LIFETIME:
2516 return sm->dot11RSNAConfigPMKLifetime;
2517 case RSNA_PMK_REAUTH_THRESHOLD:
2518 return sm->dot11RSNAConfigPMKReauthThreshold;
2519 case RSNA_SA_TIMEOUT:
2520 return sm->dot11RSNAConfigSATimeout;
2521 case WPA_PARAM_PROTO:
2522 return sm->proto;
2523 case WPA_PARAM_PAIRWISE:
2524 return sm->pairwise_cipher;
2525 case WPA_PARAM_GROUP:
2526 return sm->group_cipher;
2527 case WPA_PARAM_KEY_MGMT:
2528 return sm->key_mgmt;
2529 default:
2530 return 0;
2536 * wpa_sm_get_status - Get WPA state machine
2537 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2538 * @buf: Buffer for status information
2539 * @buflen: Maximum buffer length
2540 * @verbose: Whether to include verbose status information
2541 * Returns: Number of bytes written to buf.
2543 * Query WPA state machine for status information. This function fills in
2544 * a text area with current status information. If the buffer (buf) is not
2545 * large enough, status information will be truncated to fit the buffer.
2547 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2548 int verbose)
2550 char *pos = buf, *end = buf + buflen;
2552 pos += snprintf(pos, end - pos,
2553 "pairwise_cipher=%s\n"
2554 "group_cipher=%s\n"
2555 "key_mgmt=%s\n",
2556 wpa_cipher_txt(sm->pairwise_cipher),
2557 wpa_cipher_txt(sm->group_cipher),
2558 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2559 return pos - buf;
2564 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2565 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2566 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2567 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2568 * Returns: 0 on success, -1 on failure
2570 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2571 * Request frame. The IE will be used to override the default value generated
2572 * with wpa_sm_set_assoc_wpa_ie_default().
2574 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2575 size_t *wpa_ie_len)
2577 int res;
2579 if (sm == NULL)
2580 return -1;
2582 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2583 if (res < 0)
2584 return -1;
2585 *wpa_ie_len = res;
2587 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2588 wpa_ie, *wpa_ie_len);
2590 if (sm->assoc_wpa_ie == NULL) {
2592 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2593 * the correct version of the IE even if PMKSA caching is
2594 * aborted (which would remove PMKID from IE generation).
2596 sm->assoc_wpa_ie = malloc(*wpa_ie_len);
2597 if (sm->assoc_wpa_ie == NULL)
2598 return -1;
2600 memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2601 sm->assoc_wpa_ie_len = *wpa_ie_len;
2604 return 0;
2609 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2610 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2611 * @ie: Pointer to IE data (starting from id)
2612 * @len: IE length
2613 * Returns: 0 on success, -1 on failure
2615 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2616 * Request frame. The IE will be used to override the default value generated
2617 * with wpa_sm_set_assoc_wpa_ie_default().
2619 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2621 if (sm == NULL)
2622 return -1;
2624 free(sm->assoc_wpa_ie);
2625 if (ie == NULL || len == 0) {
2626 wpa_printf(MSG_DEBUG, "WPA: clearing own WPA/RSN IE");
2627 sm->assoc_wpa_ie = NULL;
2628 sm->assoc_wpa_ie_len = 0;
2629 } else {
2630 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2631 sm->assoc_wpa_ie = malloc(len);
2632 if (sm->assoc_wpa_ie == NULL)
2633 return -1;
2635 memcpy(sm->assoc_wpa_ie, ie, len);
2636 sm->assoc_wpa_ie_len = len;
2639 return 0;
2644 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2645 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2646 * @ie: Pointer to IE data (starting from id)
2647 * @len: IE length
2648 * Returns: 0 on success, -1 on failure
2650 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2651 * frame.
2653 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2655 if (sm == NULL)
2656 return -1;
2658 free(sm->ap_wpa_ie);
2659 if (ie == NULL || len == 0) {
2660 wpa_printf(MSG_DEBUG, "WPA: clearing AP WPA IE");
2661 sm->ap_wpa_ie = NULL;
2662 sm->ap_wpa_ie_len = 0;
2663 } else {
2664 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2665 sm->ap_wpa_ie = malloc(len);
2666 if (sm->ap_wpa_ie == NULL)
2667 return -1;
2669 memcpy(sm->ap_wpa_ie, ie, len);
2670 sm->ap_wpa_ie_len = len;
2673 return 0;
2678 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2679 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2680 * @ie: Pointer to IE data (starting from id)
2681 * @len: IE length
2682 * Returns: 0 on success, -1 on failure
2684 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2685 * frame.
2687 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2689 if (sm == NULL)
2690 return -1;
2692 free(sm->ap_rsn_ie);
2693 if (ie == NULL || len == 0) {
2694 wpa_printf(MSG_DEBUG, "WPA: clearing AP RSN IE");
2695 sm->ap_rsn_ie = NULL;
2696 sm->ap_rsn_ie_len = 0;
2697 } else {
2698 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2699 sm->ap_rsn_ie = malloc(len);
2700 if (sm->ap_rsn_ie == NULL)
2701 return -1;
2703 memcpy(sm->ap_rsn_ie, ie, len);
2704 sm->ap_rsn_ie_len = len;
2707 return 0;
2712 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2713 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2714 * @data: Pointer to data area for parsing results
2715 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2717 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2718 * parsed data into data.
2720 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2722 if (sm == NULL || sm->assoc_wpa_ie == NULL) {
2723 wpa_printf(MSG_DEBUG, "WPA: No WPA/RSN IE available from "
2724 "association info");
2725 return -1;
2727 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2728 return -2;
2729 return 0;