]>
Commit | Line | Data |
---|---|---|
6fc6879b JM |
1 | /* |
2 | * WPA Supplicant - WPA state machine and EAPOL-Key processing | |
0a614799 | 3 | * Copyright (c) 2003-2017, Jouni Malinen <j@w1.fi> |
73ed03f3 | 4 | * Copyright(c) 2015 Intel Deutschland GmbH |
6fc6879b | 5 | * |
0f3d578e JM |
6 | * This software may be distributed under the terms of the BSD license. |
7 | * See README for more details. | |
6fc6879b JM |
8 | */ |
9 | ||
10 | #include "includes.h" | |
11 | ||
12 | #include "common.h" | |
2022f1d0 | 13 | #include "crypto/aes.h" |
03da66bd JM |
14 | #include "crypto/aes_wrap.h" |
15 | #include "crypto/crypto.h" | |
3642c431 | 16 | #include "crypto/random.h" |
2022f1d0 | 17 | #include "crypto/aes_siv.h" |
0a614799 | 18 | #include "crypto/sha256.h" |
ec9f4837 JM |
19 | #include "crypto/sha384.h" |
20 | #include "crypto/sha512.h" | |
03da66bd | 21 | #include "common/ieee802_11_defs.h" |
a6609937 | 22 | #include "common/ieee802_11_common.h" |
fcd3d6ce | 23 | #include "eap_common/eap_defs.h" |
03da66bd | 24 | #include "eapol_supp/eapol_supp_sm.h" |
6fc6879b JM |
25 | #include "wpa.h" |
26 | #include "eloop.h" | |
6fc6879b JM |
27 | #include "preauth.h" |
28 | #include "pmksa_cache.h" | |
29 | #include "wpa_i.h" | |
30 | #include "wpa_ie.h" | |
6fc6879b JM |
31 | |
32 | ||
73ed03f3 MS |
33 | static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
34 | ||
35 | ||
6fc6879b JM |
36 | /** |
37 | * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message | |
38 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
1049af7e | 39 | * @ptk: PTK for Key Confirmation/Encryption Key |
6fc6879b JM |
40 | * @ver: Version field from Key Info |
41 | * @dest: Destination address for the frame | |
42 | * @proto: Ethertype (usually ETH_P_EAPOL) | |
43 | * @msg: EAPOL-Key message | |
44 | * @msg_len: Length of message | |
45 | * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written | |
c93b7e18 | 46 | * Returns: >= 0 on success, < 0 on failure |
6fc6879b | 47 | */ |
1049af7e | 48 | int wpa_eapol_key_send(struct wpa_sm *sm, struct wpa_ptk *ptk, |
c93b7e18 AA |
49 | int ver, const u8 *dest, u16 proto, |
50 | u8 *msg, size_t msg_len, u8 *key_mic) | |
6fc6879b | 51 | { |
c93b7e18 | 52 | int ret = -1; |
567da5bb | 53 | size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); |
98cd3d1c | 54 | |
ef9627cb JM |
55 | wpa_printf(MSG_DEBUG, "WPA: Send EAPOL-Key frame to " MACSTR |
56 | " ver=%d mic_len=%d key_mgmt=0x%x", | |
57 | MAC2STR(dest), ver, (int) mic_len, sm->key_mgmt); | |
a8e16edc | 58 | if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) { |
6fc6879b JM |
59 | /* |
60 | * Association event was not yet received; try to fetch | |
61 | * BSSID from the driver. | |
62 | */ | |
63 | if (wpa_sm_get_bssid(sm, sm->bssid) < 0) { | |
f049052b BG |
64 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
65 | "WPA: Failed to read BSSID for " | |
66 | "EAPOL-Key destination address"); | |
6fc6879b JM |
67 | } else { |
68 | dest = sm->bssid; | |
f049052b BG |
69 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
70 | "WPA: Use BSSID (" MACSTR | |
71 | ") as the destination for EAPOL-Key", | |
72 | MAC2STR(dest)); | |
6fc6879b JM |
73 | } |
74 | } | |
2022f1d0 JM |
75 | |
76 | if (mic_len) { | |
77 | if (key_mic && (!ptk || !ptk->kck_len)) | |
78 | goto out; | |
79 | ||
80 | if (key_mic && | |
81 | wpa_eapol_key_mic(ptk->kck, ptk->kck_len, sm->key_mgmt, ver, | |
82 | msg, msg_len, key_mic)) { | |
83 | wpa_msg(sm->ctx->msg_ctx, MSG_ERROR, | |
84 | "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC", | |
85 | ver, sm->key_mgmt); | |
86 | goto out; | |
87 | } | |
28fb9bb1 JM |
88 | if (ptk) |
89 | wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", | |
90 | ptk->kck, ptk->kck_len); | |
2022f1d0 JM |
91 | wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC", |
92 | key_mic, mic_len); | |
93 | } else { | |
94 | #ifdef CONFIG_FILS | |
95 | /* AEAD cipher - Key MIC field not used */ | |
96 | struct ieee802_1x_hdr *s_hdr, *hdr; | |
97 | struct wpa_eapol_key *s_key, *key; | |
98 | u8 *buf, *s_key_data, *key_data; | |
99 | size_t buf_len = msg_len + AES_BLOCK_SIZE; | |
100 | size_t key_data_len; | |
101 | u16 eapol_len; | |
102 | const u8 *aad[1]; | |
103 | size_t aad_len[1]; | |
104 | ||
105 | if (!ptk || !ptk->kek_len) | |
106 | goto out; | |
107 | ||
108 | key_data_len = msg_len - sizeof(struct ieee802_1x_hdr) - | |
109 | sizeof(struct wpa_eapol_key) - 2; | |
110 | ||
111 | buf = os_malloc(buf_len); | |
112 | if (!buf) | |
113 | goto out; | |
114 | ||
115 | os_memcpy(buf, msg, msg_len); | |
116 | hdr = (struct ieee802_1x_hdr *) buf; | |
117 | key = (struct wpa_eapol_key *) (hdr + 1); | |
118 | key_data = ((u8 *) (key + 1)) + 2; | |
119 | ||
120 | /* Update EAPOL header to include AES-SIV overhead */ | |
121 | eapol_len = be_to_host16(hdr->length); | |
122 | eapol_len += AES_BLOCK_SIZE; | |
123 | hdr->length = host_to_be16(eapol_len); | |
124 | ||
125 | /* Update Key Data Length field to include AES-SIV overhead */ | |
126 | WPA_PUT_BE16((u8 *) (key + 1), AES_BLOCK_SIZE + key_data_len); | |
127 | ||
128 | s_hdr = (struct ieee802_1x_hdr *) msg; | |
129 | s_key = (struct wpa_eapol_key *) (s_hdr + 1); | |
130 | s_key_data = ((u8 *) (s_key + 1)) + 2; | |
131 | ||
132 | wpa_hexdump_key(MSG_DEBUG, "WPA: Plaintext Key Data", | |
133 | s_key_data, key_data_len); | |
134 | ||
135 | wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len); | |
136 | /* AES-SIV AAD from EAPOL protocol version field (inclusive) to | |
137 | * to Key Data (exclusive). */ | |
138 | aad[0] = buf; | |
139 | aad_len[0] = key_data - buf; | |
140 | if (aes_siv_encrypt(ptk->kek, ptk->kek_len, | |
141 | s_key_data, key_data_len, | |
142 | 1, aad, aad_len, key_data) < 0) { | |
143 | os_free(buf); | |
144 | goto out; | |
145 | } | |
146 | ||
147 | wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV", | |
148 | key_data, AES_BLOCK_SIZE + key_data_len); | |
149 | ||
150 | os_free(msg); | |
151 | msg = buf; | |
152 | msg_len = buf_len; | |
153 | #else /* CONFIG_FILS */ | |
04b6b3ed | 154 | goto out; |
2022f1d0 | 155 | #endif /* CONFIG_FILS */ |
04b6b3ed | 156 | } |
2022f1d0 | 157 | |
6fc6879b | 158 | wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len); |
c93b7e18 | 159 | ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len); |
6fc6879b | 160 | eapol_sm_notify_tx_eapol_key(sm->eapol); |
04b6b3ed | 161 | out: |
6fc6879b | 162 | os_free(msg); |
c93b7e18 | 163 | return ret; |
6fc6879b JM |
164 | } |
165 | ||
166 | ||
167 | /** | |
168 | * wpa_sm_key_request - Send EAPOL-Key Request | |
169 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
170 | * @error: Indicate whether this is an Michael MIC error report | |
171 | * @pairwise: 1 = error report for pairwise packet, 0 = for group packet | |
6fc6879b JM |
172 | * |
173 | * Send an EAPOL-Key Request to the current authenticator. This function is | |
174 | * used to request rekeying and it is usually called when a local Michael MIC | |
175 | * failure is detected. | |
176 | */ | |
177 | void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise) | |
178 | { | |
5e3b5197 | 179 | size_t mic_len, hdrlen, rlen; |
6fc6879b JM |
180 | struct wpa_eapol_key *reply; |
181 | int key_info, ver; | |
6d014ffc | 182 | u8 bssid[ETH_ALEN], *rbuf, *key_mic, *mic; |
6fc6879b | 183 | |
929a2ea5 JM |
184 | if (sm->key_mgmt == WPA_KEY_MGMT_OSEN || |
185 | wpa_key_mgmt_suite_b(sm->key_mgmt)) | |
df0f01d9 JM |
186 | ver = WPA_KEY_INFO_TYPE_AKM_DEFINED; |
187 | else if (wpa_key_mgmt_ft(sm->key_mgmt) || | |
188 | wpa_key_mgmt_sha256(sm->key_mgmt)) | |
6fc6879b | 189 | ver = WPA_KEY_INFO_TYPE_AES_128_CMAC; |
eb7719ff | 190 | else if (sm->pairwise_cipher != WPA_CIPHER_TKIP) |
6fc6879b JM |
191 | ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES; |
192 | else | |
193 | ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4; | |
194 | ||
195 | if (wpa_sm_get_bssid(sm, bssid) < 0) { | |
f049052b BG |
196 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
197 | "Failed to read BSSID for EAPOL-Key request"); | |
6fc6879b JM |
198 | return; |
199 | } | |
200 | ||
567da5bb | 201 | mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); |
6d014ffc | 202 | hdrlen = sizeof(*reply) + mic_len + 2; |
6fc6879b | 203 | rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL, |
5e3b5197 | 204 | hdrlen, &rlen, (void *) &reply); |
6fc6879b JM |
205 | if (rbuf == NULL) |
206 | return; | |
207 | ||
a14896e8 JM |
208 | reply->type = (sm->proto == WPA_PROTO_RSN || |
209 | sm->proto == WPA_PROTO_OSEN) ? | |
6fc6879b JM |
210 | EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA; |
211 | key_info = WPA_KEY_INFO_REQUEST | ver; | |
212 | if (sm->ptk_set) | |
4a26ccda JM |
213 | key_info |= WPA_KEY_INFO_SECURE; |
214 | if (sm->ptk_set && mic_len) | |
215 | key_info |= WPA_KEY_INFO_MIC; | |
6fc6879b JM |
216 | if (error) |
217 | key_info |= WPA_KEY_INFO_ERROR; | |
218 | if (pairwise) | |
219 | key_info |= WPA_KEY_INFO_KEY_TYPE; | |
220 | WPA_PUT_BE16(reply->key_info, key_info); | |
221 | WPA_PUT_BE16(reply->key_length, 0); | |
222 | os_memcpy(reply->replay_counter, sm->request_counter, | |
223 | WPA_REPLAY_COUNTER_LEN); | |
224 | inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN); | |
225 | ||
6d014ffc JM |
226 | mic = (u8 *) (reply + 1); |
227 | WPA_PUT_BE16(mic + mic_len, 0); | |
5e3b5197 JM |
228 | if (!(key_info & WPA_KEY_INFO_MIC)) |
229 | key_mic = NULL; | |
230 | else | |
6d014ffc | 231 | key_mic = mic; |
6fc6879b | 232 | |
f049052b BG |
233 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
234 | "WPA: Sending EAPOL-Key Request (error=%d " | |
235 | "pairwise=%d ptk_set=%d len=%lu)", | |
236 | error, pairwise, sm->ptk_set, (unsigned long) rlen); | |
1049af7e JM |
237 | wpa_eapol_key_send(sm, &sm->ptk, ver, bssid, ETH_P_EAPOL, rbuf, rlen, |
238 | key_mic); | |
6fc6879b JM |
239 | } |
240 | ||
241 | ||
b41f2684 CL |
242 | static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm) |
243 | { | |
244 | #ifdef CONFIG_IEEE80211R | |
245 | if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) { | |
246 | if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len)) | |
247 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, | |
248 | "RSN: Cannot set low order 256 bits of MSK for key management offload"); | |
249 | } else { | |
250 | #endif /* CONFIG_IEEE80211R */ | |
251 | if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len)) | |
252 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, | |
253 | "RSN: Cannot set PMK for key management offload"); | |
254 | #ifdef CONFIG_IEEE80211R | |
255 | } | |
256 | #endif /* CONFIG_IEEE80211R */ | |
257 | } | |
258 | ||
259 | ||
6fc6879b JM |
260 | static int wpa_supplicant_get_pmk(struct wpa_sm *sm, |
261 | const unsigned char *src_addr, | |
262 | const u8 *pmkid) | |
263 | { | |
264 | int abort_cached = 0; | |
265 | ||
266 | if (pmkid && !sm->cur_pmksa) { | |
267 | /* When using drivers that generate RSN IE, wpa_supplicant may | |
268 | * not have enough time to get the association information | |
269 | * event before receiving this 1/4 message, so try to find a | |
270 | * matching PMKSA cache entry here. */ | |
96efeeb6 JM |
271 | sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid, |
272 | NULL); | |
6fc6879b | 273 | if (sm->cur_pmksa) { |
f049052b BG |
274 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
275 | "RSN: found matching PMKID from PMKSA cache"); | |
6fc6879b | 276 | } else { |
f049052b BG |
277 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
278 | "RSN: no matching PMKID found"); | |
6fc6879b JM |
279 | abort_cached = 1; |
280 | } | |
281 | } | |
282 | ||
283 | if (pmkid && sm->cur_pmksa && | |
0d15b69f | 284 | os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) { |
6fc6879b JM |
285 | wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN); |
286 | wpa_sm_set_pmk_from_pmksa(sm); | |
287 | wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache", | |
288 | sm->pmk, sm->pmk_len); | |
289 | eapol_sm_notify_cached(sm->eapol); | |
290 | #ifdef CONFIG_IEEE80211R | |
291 | sm->xxkey_len = 0; | |
292 | #endif /* CONFIG_IEEE80211R */ | |
56586197 | 293 | } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) { |
6fc6879b | 294 | int res, pmk_len; |
207976f0 | 295 | |
834c5d68 | 296 | if (wpa_key_mgmt_sha384(sm->key_mgmt)) |
207976f0 JM |
297 | pmk_len = PMK_LEN_SUITE_B_192; |
298 | else | |
299 | pmk_len = PMK_LEN; | |
300 | res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len); | |
6fc6879b | 301 | if (res) { |
207976f0 JM |
302 | if (pmk_len == PMK_LEN) { |
303 | /* | |
304 | * EAP-LEAP is an exception from other EAP | |
305 | * methods: it uses only 16-byte PMK. | |
306 | */ | |
307 | res = eapol_sm_get_key(sm->eapol, sm->pmk, 16); | |
308 | pmk_len = 16; | |
309 | } | |
6fc6879b JM |
310 | } else { |
311 | #ifdef CONFIG_IEEE80211R | |
312 | u8 buf[2 * PMK_LEN]; | |
313 | if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0) | |
314 | { | |
315 | os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN); | |
316 | sm->xxkey_len = PMK_LEN; | |
317 | os_memset(buf, 0, sizeof(buf)); | |
318 | } | |
319 | #endif /* CONFIG_IEEE80211R */ | |
320 | } | |
321 | if (res == 0) { | |
a7f10d65 | 322 | struct rsn_pmksa_cache_entry *sa = NULL; |
869af307 JM |
323 | const u8 *fils_cache_id = NULL; |
324 | ||
325 | #ifdef CONFIG_FILS | |
326 | if (sm->fils_cache_id_set) | |
327 | fils_cache_id = sm->fils_cache_id; | |
328 | #endif /* CONFIG_FILS */ | |
329 | ||
6fc6879b JM |
330 | wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state " |
331 | "machines", sm->pmk, pmk_len); | |
332 | sm->pmk_len = pmk_len; | |
b41f2684 | 333 | wpa_supplicant_key_mgmt_set_pmk(sm); |
715ed737 | 334 | if (sm->proto == WPA_PROTO_RSN && |
087a1f4e | 335 | !wpa_key_mgmt_suite_b(sm->key_mgmt) && |
715ed737 | 336 | !wpa_key_mgmt_ft(sm->key_mgmt)) { |
a7f10d65 | 337 | sa = pmksa_cache_add(sm->pmksa, |
70c93963 | 338 | sm->pmk, pmk_len, NULL, |
087a1f4e | 339 | NULL, 0, |
a7f10d65 JM |
340 | src_addr, sm->own_addr, |
341 | sm->network_ctx, | |
869af307 JM |
342 | sm->key_mgmt, |
343 | fils_cache_id); | |
f5a51b58 | 344 | } |
6fc6879b | 345 | if (!sm->cur_pmksa && pmkid && |
96efeeb6 JM |
346 | pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL)) |
347 | { | |
f049052b BG |
348 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
349 | "RSN: the new PMK matches with the " | |
350 | "PMKID"); | |
6fc6879b | 351 | abort_cached = 0; |
bddc51e8 JM |
352 | } else if (sa && !sm->cur_pmksa && pmkid) { |
353 | /* | |
354 | * It looks like the authentication server | |
355 | * derived mismatching MSK. This should not | |
356 | * really happen, but bugs happen.. There is not | |
357 | * much we can do here without knowing what | |
358 | * exactly caused the server to misbehave. | |
359 | */ | |
62417667 | 360 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
bddc51e8 JM |
361 | "RSN: PMKID mismatch - authentication server may have derived different MSK?!"); |
362 | return -1; | |
6fc6879b | 363 | } |
a7f10d65 JM |
364 | |
365 | if (!sm->cur_pmksa) | |
366 | sm->cur_pmksa = sa; | |
6fc6879b | 367 | } else { |
0f057fb2 | 368 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
6fc6879b | 369 | "WPA: Failed to get master session key from " |
f049052b BG |
370 | "EAPOL state machines - key handshake " |
371 | "aborted"); | |
6fc6879b | 372 | if (sm->cur_pmksa) { |
f049052b BG |
373 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
374 | "RSN: Cancelled PMKSA caching " | |
375 | "attempt"); | |
6fc6879b JM |
376 | sm->cur_pmksa = NULL; |
377 | abort_cached = 1; | |
1ac2d4a9 | 378 | } else if (!abort_cached) { |
6fc6879b JM |
379 | return -1; |
380 | } | |
381 | } | |
382 | } | |
383 | ||
715ed737 | 384 | if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && |
087a1f4e | 385 | !wpa_key_mgmt_suite_b(sm->key_mgmt) && |
a14896e8 JM |
386 | !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN) |
387 | { | |
6fc6879b JM |
388 | /* Send EAPOL-Start to trigger full EAP authentication. */ |
389 | u8 *buf; | |
390 | size_t buflen; | |
391 | ||
f049052b BG |
392 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
393 | "RSN: no PMKSA entry found - trigger " | |
394 | "full EAP authentication"); | |
6fc6879b JM |
395 | buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START, |
396 | NULL, 0, &buflen, NULL); | |
397 | if (buf) { | |
398 | wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL, | |
399 | buf, buflen); | |
400 | os_free(buf); | |
b4a1256d | 401 | return -2; |
6fc6879b JM |
402 | } |
403 | ||
404 | return -1; | |
405 | } | |
406 | ||
407 | return 0; | |
408 | } | |
409 | ||
410 | ||
411 | /** | |
412 | * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake | |
413 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
414 | * @dst: Destination address for the frame | |
415 | * @key: Pointer to the EAPOL-Key frame header | |
416 | * @ver: Version bits from EAPOL-Key Key Info | |
417 | * @nonce: Nonce value for the EAPOL-Key frame | |
418 | * @wpa_ie: WPA/RSN IE | |
419 | * @wpa_ie_len: Length of the WPA/RSN IE | |
420 | * @ptk: PTK to use for keyed hash and encryption | |
c93b7e18 | 421 | * Returns: >= 0 on success, < 0 on failure |
6fc6879b JM |
422 | */ |
423 | int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst, | |
424 | const struct wpa_eapol_key *key, | |
425 | int ver, const u8 *nonce, | |
426 | const u8 *wpa_ie, size_t wpa_ie_len, | |
427 | struct wpa_ptk *ptk) | |
428 | { | |
5e3b5197 | 429 | size_t mic_len, hdrlen, rlen; |
6fc6879b | 430 | struct wpa_eapol_key *reply; |
5e3b5197 | 431 | u8 *rbuf, *key_mic; |
26e23750 | 432 | u8 *rsn_ie_buf = NULL; |
4a26ccda | 433 | u16 key_info; |
6fc6879b JM |
434 | |
435 | if (wpa_ie == NULL) { | |
f049052b BG |
436 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - " |
437 | "cannot generate msg 2/4"); | |
6fc6879b JM |
438 | return -1; |
439 | } | |
440 | ||
26e23750 JM |
441 | #ifdef CONFIG_IEEE80211R |
442 | if (wpa_key_mgmt_ft(sm->key_mgmt)) { | |
443 | int res; | |
444 | ||
55046414 JM |
445 | /* |
446 | * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and | |
447 | * FTIE from (Re)Association Response. | |
448 | */ | |
449 | rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN + | |
450 | sm->assoc_resp_ies_len); | |
26e23750 JM |
451 | if (rsn_ie_buf == NULL) |
452 | return -1; | |
453 | os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len); | |
59e78c24 | 454 | res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len, |
26e23750 JM |
455 | sm->pmk_r1_name); |
456 | if (res < 0) { | |
457 | os_free(rsn_ie_buf); | |
458 | return -1; | |
459 | } | |
55046414 JM |
460 | |
461 | if (sm->assoc_resp_ies) { | |
462 | os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies, | |
463 | sm->assoc_resp_ies_len); | |
464 | wpa_ie_len += sm->assoc_resp_ies_len; | |
465 | } | |
26e23750 JM |
466 | |
467 | wpa_ie = rsn_ie_buf; | |
26e23750 JM |
468 | } |
469 | #endif /* CONFIG_IEEE80211R */ | |
470 | ||
6fc6879b JM |
471 | wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len); |
472 | ||
567da5bb | 473 | mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); |
6d014ffc | 474 | hdrlen = sizeof(*reply) + mic_len + 2; |
6fc6879b | 475 | rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, |
5e3b5197 | 476 | NULL, hdrlen + wpa_ie_len, |
6fc6879b | 477 | &rlen, (void *) &reply); |
26e23750 JM |
478 | if (rbuf == NULL) { |
479 | os_free(rsn_ie_buf); | |
6fc6879b | 480 | return -1; |
26e23750 | 481 | } |
6fc6879b | 482 | |
a14896e8 JM |
483 | reply->type = (sm->proto == WPA_PROTO_RSN || |
484 | sm->proto == WPA_PROTO_OSEN) ? | |
6fc6879b | 485 | EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA; |
4a26ccda JM |
486 | key_info = ver | WPA_KEY_INFO_KEY_TYPE; |
487 | if (mic_len) | |
488 | key_info |= WPA_KEY_INFO_MIC; | |
2022f1d0 JM |
489 | else |
490 | key_info |= WPA_KEY_INFO_ENCR_KEY_DATA; | |
4a26ccda | 491 | WPA_PUT_BE16(reply->key_info, key_info); |
a14896e8 | 492 | if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) |
6fc6879b JM |
493 | WPA_PUT_BE16(reply->key_length, 0); |
494 | else | |
495 | os_memcpy(reply->key_length, key->key_length, 2); | |
496 | os_memcpy(reply->replay_counter, key->replay_counter, | |
497 | WPA_REPLAY_COUNTER_LEN); | |
bc8318ac JM |
498 | wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter, |
499 | WPA_REPLAY_COUNTER_LEN); | |
6fc6879b | 500 | |
6d014ffc JM |
501 | key_mic = (u8 *) (reply + 1); |
502 | WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */ | |
503 | os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */ | |
26e23750 | 504 | os_free(rsn_ie_buf); |
6fc6879b JM |
505 | |
506 | os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN); | |
507 | ||
f049052b | 508 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4"); |
1049af7e JM |
509 | return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen, |
510 | key_mic); | |
6fc6879b JM |
511 | } |
512 | ||
513 | ||
514 | static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr, | |
98cd3d1c | 515 | const struct wpa_eapol_key *key, struct wpa_ptk *ptk) |
6fc6879b JM |
516 | { |
517 | #ifdef CONFIG_IEEE80211R | |
56586197 | 518 | if (wpa_key_mgmt_ft(sm->key_mgmt)) |
98cd3d1c | 519 | return wpa_derive_ptk_ft(sm, src_addr, key, ptk); |
6fc6879b JM |
520 | #endif /* CONFIG_IEEE80211R */ |
521 | ||
98cd3d1c JM |
522 | return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion", |
523 | sm->own_addr, sm->bssid, sm->snonce, | |
524 | key->key_nonce, ptk, sm->key_mgmt, | |
525 | sm->pairwise_cipher); | |
6fc6879b JM |
526 | } |
527 | ||
528 | ||
529 | static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm, | |
530 | const unsigned char *src_addr, | |
531 | const struct wpa_eapol_key *key, | |
e6270129 JM |
532 | u16 ver, const u8 *key_data, |
533 | size_t key_data_len) | |
6fc6879b JM |
534 | { |
535 | struct wpa_eapol_ie_parse ie; | |
536 | struct wpa_ptk *ptk; | |
b4a1256d | 537 | int res; |
25ef8529 JM |
538 | u8 *kde, *kde_buf = NULL; |
539 | size_t kde_len; | |
6fc6879b JM |
540 | |
541 | if (wpa_sm_get_network_ctx(sm) == NULL) { | |
f049052b BG |
542 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info " |
543 | "found (msg 1 of 4)"); | |
6fc6879b JM |
544 | return; |
545 | } | |
546 | ||
547 | wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE); | |
f049052b BG |
548 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way " |
549 | "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver); | |
6fc6879b JM |
550 | |
551 | os_memset(&ie, 0, sizeof(ie)); | |
552 | ||
a14896e8 | 553 | if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) { |
6fc6879b | 554 | /* RSN: msg 1/4 should contain PMKID for the selected PMK */ |
e6270129 JM |
555 | wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", |
556 | key_data, key_data_len); | |
557 | if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0) | |
d627a939 | 558 | goto failed; |
6fc6879b JM |
559 | if (ie.pmkid) { |
560 | wpa_hexdump(MSG_DEBUG, "RSN: PMKID from " | |
561 | "Authenticator", ie.pmkid, PMKID_LEN); | |
562 | } | |
563 | } | |
6fc6879b | 564 | |
b4a1256d JM |
565 | res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid); |
566 | if (res == -2) { | |
f049052b BG |
567 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to " |
568 | "msg 1/4 - requesting full EAP authentication"); | |
b4a1256d JM |
569 | return; |
570 | } | |
571 | if (res) | |
83935317 | 572 | goto failed; |
6fc6879b JM |
573 | |
574 | if (sm->renew_snonce) { | |
3642c431 | 575 | if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) { |
0f057fb2 | 576 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
6fc6879b | 577 | "WPA: Failed to get random data for SNonce"); |
83935317 | 578 | goto failed; |
6fc6879b JM |
579 | } |
580 | sm->renew_snonce = 0; | |
581 | wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce", | |
582 | sm->snonce, WPA_NONCE_LEN); | |
583 | } | |
584 | ||
585 | /* Calculate PTK which will be stored as a temporary PTK until it has | |
586 | * been verified when processing message 3/4. */ | |
587 | ptk = &sm->tptk; | |
588 | wpa_derive_ptk(sm, src_addr, key, ptk); | |
3b9c5176 | 589 | if (sm->pairwise_cipher == WPA_CIPHER_TKIP) { |
d2c33b91 | 590 | u8 buf[8]; |
3b9c5176 | 591 | /* Supplicant: swap tx/rx Mic keys */ |
98cd3d1c JM |
592 | os_memcpy(buf, &ptk->tk[16], 8); |
593 | os_memcpy(&ptk->tk[16], &ptk->tk[24], 8); | |
594 | os_memcpy(&ptk->tk[24], buf, 8); | |
d2c33b91 | 595 | os_memset(buf, 0, sizeof(buf)); |
3b9c5176 | 596 | } |
6fc6879b JM |
597 | sm->tptk_set = 1; |
598 | ||
25ef8529 JM |
599 | kde = sm->assoc_wpa_ie; |
600 | kde_len = sm->assoc_wpa_ie_len; | |
601 | ||
602 | #ifdef CONFIG_P2P | |
603 | if (sm->p2p) { | |
604 | kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1); | |
605 | if (kde_buf) { | |
606 | u8 *pos; | |
607 | wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE " | |
608 | "into EAPOL-Key 2/4"); | |
609 | os_memcpy(kde_buf, kde, kde_len); | |
610 | kde = kde_buf; | |
611 | pos = kde + kde_len; | |
612 | *pos++ = WLAN_EID_VENDOR_SPECIFIC; | |
613 | *pos++ = RSN_SELECTOR_LEN + 1; | |
614 | RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ); | |
615 | pos += RSN_SELECTOR_LEN; | |
616 | *pos++ = 0x01; | |
617 | kde_len = pos - kde; | |
618 | } | |
619 | } | |
620 | #endif /* CONFIG_P2P */ | |
621 | ||
6fc6879b | 622 | if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce, |
c93b7e18 | 623 | kde, kde_len, ptk) < 0) |
83935317 | 624 | goto failed; |
6fc6879b | 625 | |
25ef8529 | 626 | os_free(kde_buf); |
6fc6879b | 627 | os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN); |
83935317 JM |
628 | return; |
629 | ||
630 | failed: | |
25ef8529 | 631 | os_free(kde_buf); |
83935317 | 632 | wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED); |
6fc6879b JM |
633 | } |
634 | ||
635 | ||
636 | static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx) | |
637 | { | |
638 | struct wpa_sm *sm = eloop_ctx; | |
639 | rsn_preauth_candidate_process(sm); | |
640 | } | |
641 | ||
642 | ||
643 | static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm, | |
644 | const u8 *addr, int secure) | |
645 | { | |
0f057fb2 JM |
646 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
647 | "WPA: Key negotiation completed with " | |
6fc6879b JM |
648 | MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr), |
649 | wpa_cipher_txt(sm->pairwise_cipher), | |
650 | wpa_cipher_txt(sm->group_cipher)); | |
6fc6879b JM |
651 | wpa_sm_cancel_auth_timeout(sm); |
652 | wpa_sm_set_state(sm, WPA_COMPLETED); | |
653 | ||
654 | if (secure) { | |
655 | wpa_sm_mlme_setprotection( | |
656 | sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX, | |
657 | MLME_SETPROTECTION_KEY_TYPE_PAIRWISE); | |
658 | eapol_sm_notify_portValid(sm->eapol, TRUE); | |
a1ea1b45 | 659 | if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) || |
567da5bb | 660 | sm->key_mgmt == WPA_KEY_MGMT_DPP || |
a1ea1b45 | 661 | sm->key_mgmt == WPA_KEY_MGMT_OWE) |
6fc6879b JM |
662 | eapol_sm_notify_eap_success(sm->eapol, TRUE); |
663 | /* | |
664 | * Start preauthentication after a short wait to avoid a | |
665 | * possible race condition between the data receive and key | |
666 | * configuration after the 4-Way Handshake. This increases the | |
ffbf1eaa | 667 | * likelihood of the first preauth EAPOL-Start frame getting to |
6fc6879b JM |
668 | * the target AP. |
669 | */ | |
670 | eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL); | |
671 | } | |
672 | ||
673 | if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) { | |
f049052b BG |
674 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
675 | "RSN: Authenticator accepted " | |
676 | "opportunistic PMKSA entry - marking it valid"); | |
6fc6879b JM |
677 | sm->cur_pmksa->opportunistic = 0; |
678 | } | |
679 | ||
680 | #ifdef CONFIG_IEEE80211R | |
56586197 | 681 | if (wpa_key_mgmt_ft(sm->key_mgmt)) { |
6fc6879b | 682 | /* Prepare for the next transition */ |
76b7981d | 683 | wpa_ft_prepare_auth_request(sm, NULL); |
6fc6879b JM |
684 | } |
685 | #endif /* CONFIG_IEEE80211R */ | |
686 | } | |
687 | ||
688 | ||
581a8cde JM |
689 | static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx) |
690 | { | |
691 | struct wpa_sm *sm = eloop_ctx; | |
f049052b | 692 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying"); |
581a8cde JM |
693 | wpa_sm_key_request(sm, 0, 1); |
694 | } | |
695 | ||
696 | ||
6fc6879b JM |
697 | static int wpa_supplicant_install_ptk(struct wpa_sm *sm, |
698 | const struct wpa_eapol_key *key) | |
699 | { | |
700 | int keylen, rsclen; | |
71934751 | 701 | enum wpa_alg alg; |
6fc6879b | 702 | const u8 *key_rsc; |
6fc6879b | 703 | |
53bb18cc | 704 | if (sm->ptk.installed) { |
ad00d64e JM |
705 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
706 | "WPA: Do not re-install same PTK to the driver"); | |
707 | return 0; | |
708 | } | |
709 | ||
f049052b BG |
710 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
711 | "WPA: Installing PTK to the driver"); | |
6fc6879b | 712 | |
c3550295 | 713 | if (sm->pairwise_cipher == WPA_CIPHER_NONE) { |
f049052b BG |
714 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher " |
715 | "Suite: NONE - do not use pairwise keys"); | |
6fc6879b | 716 | return 0; |
c3550295 JM |
717 | } |
718 | ||
719 | if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) { | |
f049052b BG |
720 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
721 | "WPA: Unsupported pairwise cipher %d", | |
722 | sm->pairwise_cipher); | |
6fc6879b JM |
723 | return -1; |
724 | } | |
725 | ||
c3550295 JM |
726 | alg = wpa_cipher_to_alg(sm->pairwise_cipher); |
727 | keylen = wpa_cipher_key_len(sm->pairwise_cipher); | |
728 | rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher); | |
729 | ||
a14896e8 | 730 | if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) { |
6fc6879b JM |
731 | key_rsc = null_rsc; |
732 | } else { | |
733 | key_rsc = key->key_rsc; | |
734 | wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen); | |
735 | } | |
736 | ||
737 | if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen, | |
98cd3d1c | 738 | sm->ptk.tk, keylen) < 0) { |
f049052b BG |
739 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
740 | "WPA: Failed to set PTK to the " | |
741 | "driver (alg=%d keylen=%d bssid=" MACSTR ")", | |
742 | alg, keylen, MAC2STR(sm->bssid)); | |
6fc6879b JM |
743 | return -1; |
744 | } | |
581a8cde | 745 | |
7d711541 | 746 | /* TK is not needed anymore in supplicant */ |
98cd3d1c | 747 | os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN); |
53bb18cc | 748 | sm->ptk.installed = 1; |
7d711541 | 749 | |
581a8cde JM |
750 | if (sm->wpa_ptk_rekey) { |
751 | eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL); | |
752 | eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk, | |
753 | sm, NULL); | |
754 | } | |
755 | ||
6fc6879b JM |
756 | return 0; |
757 | } | |
758 | ||
759 | ||
f049052b BG |
760 | static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm, |
761 | int group_cipher, | |
6fc6879b | 762 | int keylen, int maxkeylen, |
71934751 JM |
763 | int *key_rsc_len, |
764 | enum wpa_alg *alg) | |
6fc6879b | 765 | { |
c3550295 | 766 | int klen; |
6fc6879b | 767 | |
c3550295 JM |
768 | *alg = wpa_cipher_to_alg(group_cipher); |
769 | if (*alg == WPA_ALG_NONE) { | |
f049052b BG |
770 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
771 | "WPA: Unsupported Group Cipher %d", | |
772 | group_cipher); | |
6fc6879b JM |
773 | return -1; |
774 | } | |
c3550295 | 775 | *key_rsc_len = wpa_cipher_rsc_len(group_cipher); |
6fc6879b | 776 | |
c3550295 JM |
777 | klen = wpa_cipher_key_len(group_cipher); |
778 | if (keylen != klen || maxkeylen < klen) { | |
f049052b BG |
779 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
780 | "WPA: Unsupported %s Group Cipher key length %d (%d)", | |
781 | wpa_cipher_txt(group_cipher), keylen, maxkeylen); | |
c3550295 | 782 | return -1; |
6fc6879b | 783 | } |
c3550295 | 784 | return 0; |
6fc6879b JM |
785 | } |
786 | ||
787 | ||
788 | struct wpa_gtk_data { | |
71934751 | 789 | enum wpa_alg alg; |
6fc6879b JM |
790 | int tx, key_rsc_len, keyidx; |
791 | u8 gtk[32]; | |
792 | int gtk_len; | |
793 | }; | |
794 | ||
795 | ||
796 | static int wpa_supplicant_install_gtk(struct wpa_sm *sm, | |
797 | const struct wpa_gtk_data *gd, | |
87e2db16 | 798 | const u8 *key_rsc, int wnm_sleep) |
6fc6879b JM |
799 | { |
800 | const u8 *_gtk = gd->gtk; | |
801 | u8 gtk_buf[32]; | |
802 | ||
cb5132bb | 803 | /* Detect possible key reinstallation */ |
87e2db16 JM |
804 | if ((sm->gtk.gtk_len == (size_t) gd->gtk_len && |
805 | os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) || | |
806 | (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len && | |
807 | os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk, | |
808 | sm->gtk_wnm_sleep.gtk_len) == 0)) { | |
cb5132bb MV |
809 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
810 | "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)", | |
811 | gd->keyidx, gd->tx, gd->gtk_len); | |
812 | return 0; | |
813 | } | |
814 | ||
6fc6879b | 815 | wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len); |
f049052b BG |
816 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
817 | "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)", | |
818 | gd->keyidx, gd->tx, gd->gtk_len); | |
6fc6879b JM |
819 | wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len); |
820 | if (sm->group_cipher == WPA_CIPHER_TKIP) { | |
821 | /* Swap Tx/Rx keys for Michael MIC */ | |
822 | os_memcpy(gtk_buf, gd->gtk, 16); | |
823 | os_memcpy(gtk_buf + 16, gd->gtk + 24, 8); | |
824 | os_memcpy(gtk_buf + 24, gd->gtk + 16, 8); | |
825 | _gtk = gtk_buf; | |
826 | } | |
827 | if (sm->pairwise_cipher == WPA_CIPHER_NONE) { | |
0382097e | 828 | if (wpa_sm_set_key(sm, gd->alg, NULL, |
6fc6879b JM |
829 | gd->keyidx, 1, key_rsc, gd->key_rsc_len, |
830 | _gtk, gd->gtk_len) < 0) { | |
f049052b BG |
831 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
832 | "WPA: Failed to set GTK to the driver " | |
833 | "(Group only)"); | |
d2c33b91 | 834 | os_memset(gtk_buf, 0, sizeof(gtk_buf)); |
6fc6879b JM |
835 | return -1; |
836 | } | |
0382097e | 837 | } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr, |
6fc6879b JM |
838 | gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len, |
839 | _gtk, gd->gtk_len) < 0) { | |
f049052b BG |
840 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
841 | "WPA: Failed to set GTK to " | |
842 | "the driver (alg=%d keylen=%d keyidx=%d)", | |
843 | gd->alg, gd->gtk_len, gd->keyidx); | |
d2c33b91 | 844 | os_memset(gtk_buf, 0, sizeof(gtk_buf)); |
6fc6879b JM |
845 | return -1; |
846 | } | |
d2c33b91 | 847 | os_memset(gtk_buf, 0, sizeof(gtk_buf)); |
6fc6879b | 848 | |
87e2db16 JM |
849 | if (wnm_sleep) { |
850 | sm->gtk_wnm_sleep.gtk_len = gd->gtk_len; | |
851 | os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk, | |
852 | sm->gtk_wnm_sleep.gtk_len); | |
853 | } else { | |
854 | sm->gtk.gtk_len = gd->gtk_len; | |
855 | os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len); | |
856 | } | |
cb5132bb | 857 | |
6fc6879b JM |
858 | return 0; |
859 | } | |
860 | ||
861 | ||
862 | static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm, | |
863 | int tx) | |
864 | { | |
865 | if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) { | |
866 | /* Ignore Tx bit for GTK if a pairwise key is used. One AP | |
867 | * seemed to set this bit (incorrectly, since Tx is only when | |
868 | * doing Group Key only APs) and without this workaround, the | |
869 | * data connection does not work because wpa_supplicant | |
870 | * configured non-zero keyidx to be used for unicast. */ | |
f049052b BG |
871 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
872 | "WPA: Tx bit set for GTK, but pairwise " | |
873 | "keys are used - ignore Tx bit"); | |
6fc6879b JM |
874 | return 0; |
875 | } | |
876 | return tx; | |
877 | } | |
878 | ||
879 | ||
73ed03f3 MS |
880 | static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm, |
881 | const u8 *rsc) | |
882 | { | |
883 | int rsclen; | |
884 | ||
885 | if (!sm->wpa_rsc_relaxation) | |
886 | return 0; | |
887 | ||
888 | rsclen = wpa_cipher_rsc_len(sm->group_cipher); | |
889 | ||
890 | /* | |
891 | * Try to detect RSC (endian) corruption issue where the AP sends | |
892 | * the RSC bytes in EAPOL-Key message in the wrong order, both if | |
893 | * it's actually a 6-byte field (as it should be) and if it treats | |
894 | * it as an 8-byte field. | |
895 | * An AP model known to have this bug is the Sapido RB-1632. | |
896 | */ | |
897 | if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) { | |
898 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, | |
899 | "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0", | |
900 | rsc[0], rsc[1], rsc[2], rsc[3], | |
901 | rsc[4], rsc[5], rsc[6], rsc[7]); | |
902 | ||
903 | return 1; | |
904 | } | |
905 | ||
906 | return 0; | |
907 | } | |
908 | ||
909 | ||
6fc6879b JM |
910 | static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm, |
911 | const struct wpa_eapol_key *key, | |
912 | const u8 *gtk, size_t gtk_len, | |
913 | int key_info) | |
914 | { | |
6fc6879b | 915 | struct wpa_gtk_data gd; |
73ed03f3 | 916 | const u8 *key_rsc; |
6fc6879b JM |
917 | |
918 | /* | |
919 | * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x | |
920 | * GTK KDE format: | |
921 | * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7] | |
922 | * Reserved [bits 0-7] | |
923 | * GTK | |
924 | */ | |
925 | ||
926 | os_memset(&gd, 0, sizeof(gd)); | |
927 | wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake", | |
928 | gtk, gtk_len); | |
929 | ||
930 | if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk)) | |
931 | return -1; | |
932 | ||
933 | gd.keyidx = gtk[0] & 0x3; | |
934 | gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm, | |
935 | !!(gtk[0] & BIT(2))); | |
936 | gtk += 2; | |
937 | gtk_len -= 2; | |
938 | ||
939 | os_memcpy(gd.gtk, gtk, gtk_len); | |
940 | gd.gtk_len = gtk_len; | |
941 | ||
73ed03f3 MS |
942 | key_rsc = key->key_rsc; |
943 | if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc)) | |
944 | key_rsc = null_rsc; | |
945 | ||
dff1e285 JM |
946 | if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED && |
947 | (wpa_supplicant_check_group_cipher(sm, sm->group_cipher, | |
948 | gtk_len, gtk_len, | |
949 | &gd.key_rsc_len, &gd.alg) || | |
87e2db16 | 950 | wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) { |
f049052b BG |
951 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
952 | "RSN: Failed to install GTK"); | |
d2c33b91 | 953 | os_memset(&gd, 0, sizeof(gd)); |
6fc6879b JM |
954 | return -1; |
955 | } | |
d2c33b91 | 956 | os_memset(&gd, 0, sizeof(gd)); |
6fc6879b JM |
957 | |
958 | wpa_supplicant_key_neg_complete(sm, sm->bssid, | |
959 | key_info & WPA_KEY_INFO_SECURE); | |
960 | return 0; | |
6fc6879b JM |
961 | } |
962 | ||
963 | ||
cb5132bb MV |
964 | #ifdef CONFIG_IEEE80211W |
965 | static int wpa_supplicant_install_igtk(struct wpa_sm *sm, | |
87e2db16 JM |
966 | const struct wpa_igtk_kde *igtk, |
967 | int wnm_sleep) | |
cb5132bb MV |
968 | { |
969 | size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher); | |
970 | u16 keyidx = WPA_GET_LE16(igtk->keyid); | |
971 | ||
972 | /* Detect possible key reinstallation */ | |
87e2db16 JM |
973 | if ((sm->igtk.igtk_len == len && |
974 | os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) || | |
975 | (sm->igtk_wnm_sleep.igtk_len == len && | |
976 | os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk, | |
977 | sm->igtk_wnm_sleep.igtk_len) == 0)) { | |
cb5132bb MV |
978 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
979 | "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)", | |
980 | keyidx); | |
981 | return 0; | |
982 | } | |
983 | ||
984 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, | |
985 | "WPA: IGTK keyid %d pn %02x%02x%02x%02x%02x%02x", | |
986 | keyidx, MAC2STR(igtk->pn)); | |
987 | wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len); | |
988 | if (keyidx > 4095) { | |
989 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, | |
990 | "WPA: Invalid IGTK KeyID %d", keyidx); | |
991 | return -1; | |
992 | } | |
993 | if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher), | |
994 | broadcast_ether_addr, | |
995 | keyidx, 0, igtk->pn, sizeof(igtk->pn), | |
996 | igtk->igtk, len) < 0) { | |
997 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, | |
998 | "WPA: Failed to configure IGTK to the driver"); | |
999 | return -1; | |
1000 | } | |
1001 | ||
87e2db16 JM |
1002 | if (wnm_sleep) { |
1003 | sm->igtk_wnm_sleep.igtk_len = len; | |
1004 | os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk, | |
1005 | sm->igtk_wnm_sleep.igtk_len); | |
1006 | } else { | |
1007 | sm->igtk.igtk_len = len; | |
1008 | os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len); | |
1009 | } | |
cb5132bb MV |
1010 | |
1011 | return 0; | |
1012 | } | |
1013 | #endif /* CONFIG_IEEE80211W */ | |
1014 | ||
1015 | ||
6fc6879b JM |
1016 | static int ieee80211w_set_keys(struct wpa_sm *sm, |
1017 | struct wpa_eapol_ie_parse *ie) | |
1018 | { | |
1019 | #ifdef CONFIG_IEEE80211W | |
8dd9f9cd | 1020 | if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher)) |
6fc6879b JM |
1021 | return 0; |
1022 | ||
1023 | if (ie->igtk) { | |
8dd9f9cd | 1024 | size_t len; |
6fc6879b | 1025 | const struct wpa_igtk_kde *igtk; |
cb5132bb | 1026 | |
8dd9f9cd JM |
1027 | len = wpa_cipher_key_len(sm->mgmt_group_cipher); |
1028 | if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len) | |
6fc6879b | 1029 | return -1; |
cb5132bb | 1030 | |
6fc6879b | 1031 | igtk = (const struct wpa_igtk_kde *) ie->igtk; |
87e2db16 | 1032 | if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0) |
6fc6879b | 1033 | return -1; |
6fc6879b JM |
1034 | } |
1035 | ||
1036 | return 0; | |
1037 | #else /* CONFIG_IEEE80211W */ | |
1038 | return 0; | |
1039 | #endif /* CONFIG_IEEE80211W */ | |
1040 | } | |
1041 | ||
1042 | ||
1043 | static void wpa_report_ie_mismatch(struct wpa_sm *sm, | |
1044 | const char *reason, const u8 *src_addr, | |
1045 | const u8 *wpa_ie, size_t wpa_ie_len, | |
1046 | const u8 *rsn_ie, size_t rsn_ie_len) | |
1047 | { | |
0f057fb2 | 1048 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")", |
6fc6879b JM |
1049 | reason, MAC2STR(src_addr)); |
1050 | ||
1051 | if (sm->ap_wpa_ie) { | |
1052 | wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp", | |
1053 | sm->ap_wpa_ie, sm->ap_wpa_ie_len); | |
1054 | } | |
1055 | if (wpa_ie) { | |
1056 | if (!sm->ap_wpa_ie) { | |
f049052b BG |
1057 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
1058 | "WPA: No WPA IE in Beacon/ProbeResp"); | |
6fc6879b JM |
1059 | } |
1060 | wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg", | |
1061 | wpa_ie, wpa_ie_len); | |
1062 | } | |
1063 | ||
1064 | if (sm->ap_rsn_ie) { | |
1065 | wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp", | |
1066 | sm->ap_rsn_ie, sm->ap_rsn_ie_len); | |
1067 | } | |
1068 | if (rsn_ie) { | |
1069 | if (!sm->ap_rsn_ie) { | |
f049052b BG |
1070 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
1071 | "WPA: No RSN IE in Beacon/ProbeResp"); | |
6fc6879b JM |
1072 | } |
1073 | wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg", | |
1074 | rsn_ie, rsn_ie_len); | |
1075 | } | |
1076 | ||
3da372fa | 1077 | wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS); |
6fc6879b JM |
1078 | } |
1079 | ||
1080 | ||
5af8187e JM |
1081 | #ifdef CONFIG_IEEE80211R |
1082 | ||
1083 | static int ft_validate_mdie(struct wpa_sm *sm, | |
1084 | const unsigned char *src_addr, | |
3b4f6dac JM |
1085 | struct wpa_eapol_ie_parse *ie, |
1086 | const u8 *assoc_resp_mdie) | |
5af8187e JM |
1087 | { |
1088 | struct rsn_mdie *mdie; | |
1089 | ||
5af8187e JM |
1090 | mdie = (struct rsn_mdie *) (ie->mdie + 2); |
1091 | if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) || | |
1092 | os_memcmp(mdie->mobility_domain, sm->mobility_domain, | |
1093 | MOBILITY_DOMAIN_ID_LEN) != 0) { | |
f049052b BG |
1094 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did " |
1095 | "not match with the current mobility domain"); | |
5af8187e JM |
1096 | return -1; |
1097 | } | |
1098 | ||
3b4f6dac JM |
1099 | if (assoc_resp_mdie && |
1100 | (assoc_resp_mdie[1] != ie->mdie[1] || | |
1101 | os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) { | |
f049052b | 1102 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch"); |
3b4f6dac JM |
1103 | wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4", |
1104 | ie->mdie, 2 + ie->mdie[1]); | |
1105 | wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response", | |
1106 | assoc_resp_mdie, 2 + assoc_resp_mdie[1]); | |
1107 | return -1; | |
1108 | } | |
1109 | ||
1110 | return 0; | |
1111 | } | |
1112 | ||
1113 | ||
1114 | static int ft_validate_ftie(struct wpa_sm *sm, | |
1115 | const unsigned char *src_addr, | |
1116 | struct wpa_eapol_ie_parse *ie, | |
1117 | const u8 *assoc_resp_ftie) | |
1118 | { | |
1119 | if (ie->ftie == NULL) { | |
f049052b BG |
1120 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
1121 | "FT: No FTIE in EAPOL-Key msg 3/4"); | |
3b4f6dac JM |
1122 | return -1; |
1123 | } | |
1124 | ||
1125 | if (assoc_resp_ftie == NULL) | |
1126 | return 0; | |
1127 | ||
1128 | if (assoc_resp_ftie[1] != ie->ftie[1] || | |
1129 | os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) { | |
f049052b | 1130 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch"); |
3b4f6dac JM |
1131 | wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4", |
1132 | ie->ftie, 2 + ie->ftie[1]); | |
1133 | wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response", | |
1134 | assoc_resp_ftie, 2 + assoc_resp_ftie[1]); | |
1135 | return -1; | |
1136 | } | |
1137 | ||
5af8187e JM |
1138 | return 0; |
1139 | } | |
1140 | ||
1141 | ||
1142 | static int ft_validate_rsnie(struct wpa_sm *sm, | |
1143 | const unsigned char *src_addr, | |
1144 | struct wpa_eapol_ie_parse *ie) | |
1145 | { | |
1146 | struct wpa_ie_data rsn; | |
1147 | ||
1148 | if (!ie->rsn_ie) | |
1149 | return 0; | |
1150 | ||
1151 | /* | |
1152 | * Verify that PMKR1Name from EAPOL-Key message 3/4 | |
1153 | * matches with the value we derived. | |
1154 | */ | |
1155 | if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 || | |
1156 | rsn.num_pmkid != 1 || rsn.pmkid == NULL) { | |
f049052b BG |
1157 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in " |
1158 | "FT 4-way handshake message 3/4"); | |
5af8187e JM |
1159 | return -1; |
1160 | } | |
1161 | ||
0d15b69f JM |
1162 | if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0) |
1163 | { | |
f049052b BG |
1164 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
1165 | "FT: PMKR1Name mismatch in " | |
1166 | "FT 4-way handshake message 3/4"); | |
5af8187e JM |
1167 | wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator", |
1168 | rsn.pmkid, WPA_PMK_NAME_LEN); | |
1169 | wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name", | |
1170 | sm->pmk_r1_name, WPA_PMK_NAME_LEN); | |
1171 | return -1; | |
1172 | } | |
1173 | ||
1174 | return 0; | |
1175 | } | |
1176 | ||
1177 | ||
1178 | static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm, | |
1179 | const unsigned char *src_addr, | |
1180 | struct wpa_eapol_ie_parse *ie) | |
1181 | { | |
3b4f6dac JM |
1182 | const u8 *pos, *end, *mdie = NULL, *ftie = NULL; |
1183 | ||
1184 | if (sm->assoc_resp_ies) { | |
1185 | pos = sm->assoc_resp_ies; | |
1186 | end = pos + sm->assoc_resp_ies_len; | |
2461724c JM |
1187 | while (end - pos > 2) { |
1188 | if (2 + pos[1] > end - pos) | |
3b4f6dac JM |
1189 | break; |
1190 | switch (*pos) { | |
1191 | case WLAN_EID_MOBILITY_DOMAIN: | |
1192 | mdie = pos; | |
1193 | break; | |
1194 | case WLAN_EID_FAST_BSS_TRANSITION: | |
1195 | ftie = pos; | |
1196 | break; | |
1197 | } | |
1198 | pos += 2 + pos[1]; | |
1199 | } | |
1200 | } | |
1201 | ||
1202 | if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 || | |
1203 | ft_validate_ftie(sm, src_addr, ie, ftie) < 0 || | |
5af8187e JM |
1204 | ft_validate_rsnie(sm, src_addr, ie) < 0) |
1205 | return -1; | |
1206 | ||
1207 | return 0; | |
1208 | } | |
1209 | ||
1210 | #endif /* CONFIG_IEEE80211R */ | |
1211 | ||
1212 | ||
6fc6879b JM |
1213 | static int wpa_supplicant_validate_ie(struct wpa_sm *sm, |
1214 | const unsigned char *src_addr, | |
1215 | struct wpa_eapol_ie_parse *ie) | |
1216 | { | |
1217 | if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) { | |
f049052b BG |
1218 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
1219 | "WPA: No WPA/RSN IE for this AP known. " | |
1220 | "Trying to get from scan results"); | |
6fc6879b | 1221 | if (wpa_sm_get_beacon_ie(sm) < 0) { |
f049052b BG |
1222 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
1223 | "WPA: Could not find AP from " | |
1224 | "the scan results"); | |
6fc6879b | 1225 | } else { |
f049052b BG |
1226 | wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, |
1227 | "WPA: Found the current AP from " | |
1228 | "updated scan results"); | |
6fc6879b JM |
1229 | } |
1230 | } | |
1231 | ||
1232 | if (ie->wpa_ie == NULL && ie->rsn_ie == NULL && | |
1233 | (sm->ap_wpa_ie || sm->ap_rsn_ie)) { | |
1234 | wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match " | |
1235 | "with IE in Beacon/ProbeResp (no IE?)", | |
1236 | src_addr, ie->wpa_ie, ie->wpa_ie_len, | |
1237 | ie->rsn_ie, ie->rsn_ie_len); | |
1238 | return -1; | |
1239 | } | |
1240 | ||
1241 | if ((ie->wpa_ie && sm->ap_wpa_ie && | |
1242 | (ie->wpa_ie_len != sm->ap_wpa_ie_len || | |
1243 | os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) || | |
1244 | (ie->rsn_ie && sm->ap_rsn_ie && | |
26e23750 JM |
1245 | wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt), |
1246 | sm->ap_rsn_ie, sm->ap_rsn_ie_len, | |
1247 | ie->rsn_ie, ie->rsn_ie_len))) { | |
6fc6879b JM |
1248 | wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match " |
1249 | "with IE in Beacon/ProbeResp", | |
1250 | src_addr, ie->wpa_ie, ie->wpa_ie_len, | |
1251 | ie->rsn_ie, ie->rsn_ie_len); | |
1252 | return -1; | |
1253 | } | |
1254 | ||
1255 | if (sm->proto == WPA_PROTO_WPA && | |
1256 | ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) { | |
1257 | wpa_report_ie_mismatch(sm, "Possible downgrade attack " | |
1258 | "detected - RSN was enabled and RSN IE " | |
1259 | "was in msg 3/4, but not in " | |
1260 | "Beacon/ProbeResp", | |
1261 | src_addr, ie->wpa_ie, ie->wpa_ie_len, | |
1262 | ie->rsn_ie, ie->rsn_ie_len); | |
1263 | return -1; | |
1264 | } | |
1265 | ||
1266 | #ifdef CONFIG_IEEE80211R | |
5af8187e JM |
1267 | if (wpa_key_mgmt_ft(sm->key_mgmt) && |
1268 | wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0) | |
1269 | return -1; | |
6fc6879b JM |
1270 | #endif /* CONFIG_IEEE80211R */ |
1271 | ||
1272 | return 0; | |
1273 | } | |
1274 | ||
1275 | ||
1276 | /** | |
1277 | * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake | |
1278 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
1279 | * @dst: Destination address for the frame | |
1280 | * @key: Pointer to the EAPOL-Key frame header | |
1281 | * @ver: Version bits from EAPOL-Key Key Info | |
1282 | * @key_info: Key Info | |
6fc6879b | 1283 | * @ptk: PTK to use for keyed hash and encryption |
c93b7e18 | 1284 | * Returns: >= 0 on success, < 0 on failure |
6fc6879b JM |
1285 | */ |
1286 | int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst, | |
1287 | const struct wpa_eapol_key *key, | |
1288 | u16 ver, u16 key_info, | |
6fc6879b JM |
1289 | struct wpa_ptk *ptk) |
1290 | { | |
5e3b5197 | 1291 | size_t mic_len, hdrlen, rlen; |
6fc6879b | 1292 | struct wpa_eapol_key *reply; |
5e3b5197 | 1293 | u8 *rbuf, *key_mic; |
6fc6879b | 1294 | |
567da5bb | 1295 | mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); |
6d014ffc | 1296 | hdrlen = sizeof(*reply) + mic_len + 2; |
6fc6879b | 1297 | rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL, |
5e3b5197 | 1298 | hdrlen, &rlen, (void *) &reply); |
6fc6879b JM |
1299 | if (rbuf == NULL) |
1300 | return -1; | |
1301 | ||
a14896e8 JM |
1302 | reply->type = (sm->proto == WPA_PROTO_RSN || |
1303 | sm->proto == WPA_PROTO_OSEN) ? | |
6fc6879b JM |
1304 | EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA; |
1305 | key_info &= WPA_KEY_INFO_SECURE; | |
4a26ccda JM |
1306 | key_info |= ver | WPA_KEY_INFO_KEY_TYPE; |
1307 | if (mic_len) | |
1308 | key_info |= WPA_KEY_INFO_MIC; | |
2022f1d0 JM |
1309 | else |
1310 | key_info |= WPA_KEY_INFO_ENCR_KEY_DATA; | |
6fc6879b | 1311 | WPA_PUT_BE16(reply->key_info, key_info); |
a14896e8 | 1312 | if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) |
6fc6879b JM |
1313 | WPA_PUT_BE16(reply->key_length, 0); |
1314 | else | |
1315 | os_memcpy(reply->key_length, key->key_length, 2); | |
1316 | os_memcpy(reply->replay_counter, key->replay_counter, | |
1317 | WPA_REPLAY_COUNTER_LEN); | |
1318 | ||
6d014ffc JM |
1319 | key_mic = (u8 *) (reply + 1); |
1320 | WPA_PUT_BE16(key_mic + mic_len, 0); | |
6fc6879b | 1321 | |
f049052b | 1322 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4"); |
1049af7e JM |
1323 | return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen, |
1324 | key_mic); | |
6fc6879b JM |
1325 | } |
1326 | ||
1327 | ||
1328 | static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm, | |
1329 | const struct wpa_eapol_key *key, | |
e6270129 JM |
1330 | u16 ver, const u8 *key_data, |
1331 | size_t key_data_len) | |
6fc6879b | 1332 | { |
e6270129 | 1333 | u16 key_info, keylen; |
6fc6879b JM |
1334 | struct wpa_eapol_ie_parse ie; |
1335 | ||
1336 | wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE); | |
f049052b BG |
1337 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way " |
1338 | "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver); | |
6fc6879b JM |
1339 | |
1340 | key_info = WPA_GET_BE16(key->key_info); | |
1341 | ||
e6270129 JM |
1342 | wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len); |
1343 | if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0) | |
d627a939 | 1344 | goto failed; |
6fc6879b | 1345 | if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) { |
f049052b BG |
1346 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
1347 | "WPA: GTK IE in unencrypted key data"); | |
83935317 | 1348 | goto failed; |
6fc6879b JM |
1349 | } |
1350 | #ifdef CONFIG_IEEE80211W | |
1351 | if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) { | |
f049052b BG |
1352 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
1353 | "WPA: IGTK KDE in unencrypted key data"); | |
83935317 | 1354 | goto failed; |
6fc6879b JM |
1355 | } |
1356 | ||
8dd9f9cd JM |
1357 | if (ie.igtk && |
1358 | wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) && | |
1359 | ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN + | |
1360 | (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) { | |
f049052b BG |
1361 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
1362 | "WPA: Invalid IGTK KDE length %lu", | |
1363 | (unsigned long) ie.igtk_len); | |
83935317 | 1364 | goto failed; |
6fc6879b JM |
1365 | } |
1366 | #endif /* CONFIG_IEEE80211W */ | |
1367 | ||
1368 | if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0) | |
83935317 | 1369 | goto failed; |
6fc6879b JM |
1370 | |
1371 | if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) { | |
f049052b BG |
1372 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
1373 | "WPA: ANonce from message 1 of 4-Way Handshake " | |
1374 | "differs from 3 of 4-Way Handshake - drop packet (src=" | |
1375 | MACSTR ")", MAC2STR(sm->bssid)); | |
83935317 | 1376 | goto failed; |
6fc6879b JM |
1377 | } |
1378 | ||
1379 | keylen = WPA_GET_BE16(key->key_length); | |
c3550295 JM |
1380 | if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) { |
1381 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, | |
1382 | "WPA: Invalid %s key length %d (src=" MACSTR | |
1383 | ")", wpa_cipher_txt(sm->pairwise_cipher), keylen, | |
1384 | MAC2STR(sm->bssid)); | |
1385 | goto failed; | |
6fc6879b JM |
1386 | } |
1387 | ||
25ef8529 JM |
1388 | #ifdef CONFIG_P2P |
1389 | if (ie.ip_addr_alloc) { | |
1390 | os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4); | |
1391 | wpa_hexdump(MSG_DEBUG, "P2P: IP address info", | |
1392 | sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr)); | |
1393 | } | |
1394 | #endif /* CONFIG_P2P */ | |
1395 | ||
6fc6879b | 1396 | if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info, |
c93b7e18 | 1397 | &sm->ptk) < 0) { |
83935317 JM |
1398 | goto failed; |
1399 | } | |
6fc6879b JM |
1400 | |
1401 | /* SNonce was successfully used in msg 3/4, so mark it to be renewed | |
1402 | * for the next 4-Way Handshake. If msg 3 is received again, the old | |
1403 | * SNonce will still be used to avoid changing PTK. */ | |
1404 | sm->renew_snonce = 1; | |
1405 | ||
1406 | if (key_info & WPA_KEY_INFO_INSTALL) { | |
83935317 JM |
1407 | if (wpa_supplicant_install_ptk(sm, key)) |
1408 | goto failed; | |
6fc6879b JM |
1409 | } |
1410 | ||
1411 | if (key_info & WPA_KEY_INFO_SECURE) { | |
1412 | wpa_sm_mlme_setprotection( | |
1413 | sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX, | |
1414 | MLME_SETPROTECTION_KEY_TYPE_PAIRWISE); | |
1415 | eapol_sm_notify_portValid(sm->eapol, TRUE); | |
1416 | } | |
1417 | wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE); | |
1418 | ||
dff1e285 JM |
1419 | if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) { |
1420 | wpa_supplicant_key_neg_complete(sm, sm->bssid, | |
1421 | key_info & WPA_KEY_INFO_SECURE); | |
1422 | } else if (ie.gtk && | |
6fc6879b JM |
1423 | wpa_supplicant_pairwise_gtk(sm, key, |
1424 | ie.gtk, ie.gtk_len, key_info) < 0) { | |
f049052b BG |
1425 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
1426 | "RSN: Failed to configure GTK"); | |
83935317 | 1427 | goto failed; |
6fc6879b JM |
1428 | } |
1429 | ||
83935317 | 1430 | if (ieee80211w_set_keys(sm, &ie) < 0) { |
f049052b BG |
1431 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
1432 | "RSN: Failed to configure IGTK"); | |
83935317 JM |
1433 | goto failed; |
1434 | } | |
1435 | ||
392e68e8 SD |
1436 | if (ie.gtk) |
1437 | wpa_sm_set_rekey_offload(sm); | |
b14a210c | 1438 | |
087a1f4e JM |
1439 | if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt)) { |
1440 | struct rsn_pmksa_cache_entry *sa; | |
1441 | ||
70c93963 | 1442 | sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL, |
98cd3d1c | 1443 | sm->ptk.kck, sm->ptk.kck_len, |
087a1f4e | 1444 | sm->bssid, sm->own_addr, |
869af307 | 1445 | sm->network_ctx, sm->key_mgmt, NULL); |
087a1f4e JM |
1446 | if (!sm->cur_pmksa) |
1447 | sm->cur_pmksa = sa; | |
1448 | } | |
1449 | ||
761396e4 | 1450 | sm->msg_3_of_4_ok = 1; |
83935317 JM |
1451 | return; |
1452 | ||
1453 | failed: | |
1454 | wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED); | |
6fc6879b JM |
1455 | } |
1456 | ||
1457 | ||
1458 | static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm, | |
1459 | const u8 *keydata, | |
1460 | size_t keydatalen, | |
1461 | u16 key_info, | |
1462 | struct wpa_gtk_data *gd) | |
1463 | { | |
1464 | int maxkeylen; | |
1465 | struct wpa_eapol_ie_parse ie; | |
1466 | ||
ecbdc1a1 JM |
1467 | wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data", |
1468 | keydata, keydatalen); | |
d627a939 JM |
1469 | if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0) |
1470 | return -1; | |
6fc6879b | 1471 | if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) { |
f049052b BG |
1472 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
1473 | "WPA: GTK IE in unencrypted key data"); | |
6fc6879b JM |
1474 | return -1; |
1475 | } | |
1476 | if (ie.gtk == NULL) { | |
f049052b BG |
1477 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
1478 | "WPA: No GTK IE in Group Key msg 1/2"); | |
6fc6879b JM |
1479 | return -1; |
1480 | } | |
1481 | maxkeylen = gd->gtk_len = ie.gtk_len - 2; | |
1482 | ||
f049052b | 1483 | if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher, |
6fc6879b JM |
1484 | gd->gtk_len, maxkeylen, |
1485 | &gd->key_rsc_len, &gd->alg)) | |
1486 | return -1; | |
1487 | ||
7cb9bb4d JM |
1488 | wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake", |
1489 | ie.gtk, ie.gtk_len); | |
6fc6879b JM |
1490 | gd->keyidx = ie.gtk[0] & 0x3; |
1491 | gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm, | |
1492 | !!(ie.gtk[0] & BIT(2))); | |
1493 | if (ie.gtk_len - 2 > sizeof(gd->gtk)) { | |
f049052b BG |
1494 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
1495 | "RSN: Too long GTK in GTK IE (len=%lu)", | |
1496 | (unsigned long) ie.gtk_len - 2); | |
6fc6879b JM |
1497 | return -1; |
1498 | } | |
1499 | os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2); | |
1500 | ||
1501 | if (ieee80211w_set_keys(sm, &ie) < 0) | |
f049052b BG |
1502 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
1503 | "RSN: Failed to configure IGTK"); | |
6fc6879b JM |
1504 | |
1505 | return 0; | |
1506 | } | |
1507 | ||
1508 | ||
1509 | static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm, | |
1510 | const struct wpa_eapol_key *key, | |
e6270129 JM |
1511 | const u8 *key_data, |
1512 | size_t key_data_len, u16 key_info, | |
1513 | u16 ver, struct wpa_gtk_data *gd) | |
6fc6879b JM |
1514 | { |
1515 | size_t maxkeylen; | |
c397eff8 | 1516 | u16 gtk_len; |
6fc6879b | 1517 | |
c397eff8 | 1518 | gtk_len = WPA_GET_BE16(key->key_length); |
e6270129 | 1519 | maxkeylen = key_data_len; |
6fc6879b JM |
1520 | if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) { |
1521 | if (maxkeylen < 8) { | |
f049052b BG |
1522 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
1523 | "WPA: Too short maxkeylen (%lu)", | |
1524 | (unsigned long) maxkeylen); | |
6fc6879b JM |
1525 | return -1; |
1526 | } | |
1527 | maxkeylen -= 8; | |
1528 | } | |
1529 | ||
c397eff8 JM |
1530 | if (gtk_len > maxkeylen || |
1531 | wpa_supplicant_check_group_cipher(sm, sm->group_cipher, | |
1532 | gtk_len, maxkeylen, | |
6fc6879b JM |
1533 | &gd->key_rsc_len, &gd->alg)) |
1534 | return -1; | |
1535 | ||
c397eff8 | 1536 | gd->gtk_len = gtk_len; |
6fc6879b JM |
1537 | gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >> |
1538 | WPA_KEY_INFO_KEY_INDEX_SHIFT; | |
98cd3d1c | 1539 | if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) { |
7cb53ded JM |
1540 | #ifdef CONFIG_NO_RC4 |
1541 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, | |
1542 | "WPA: RC4 not supported in the build"); | |
1543 | return -1; | |
1544 | #else /* CONFIG_NO_RC4 */ | |
d2c33b91 | 1545 | u8 ek[32]; |
e6270129 | 1546 | if (key_data_len > sizeof(gd->gtk)) { |
f049052b BG |
1547 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
1548 | "WPA: RC4 key data too long (%lu)", | |
e6270129 | 1549 | (unsigned long) key_data_len); |
6fc6879b JM |
1550 | return -1; |
1551 | } | |
d2c33b91 | 1552 | os_memcpy(ek, key->key_iv, 16); |
98cd3d1c | 1553 | os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len); |
e6270129 JM |
1554 | os_memcpy(gd->gtk, key_data, key_data_len); |
1555 | if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) { | |
d2c33b91 | 1556 | os_memset(ek, 0, sizeof(ek)); |
f049052b BG |
1557 | wpa_msg(sm->ctx->msg_ctx, MSG_ERROR, |
1558 | "WPA: RC4 failed"); | |
7a215dfc JM |
1559 | return -1; |
1560 | } | |
d2c33b91 | 1561 | os_memset(ek, 0, sizeof(ek)); |
7cb53ded | 1562 | #endif /* CONFIG_NO_RC4 */ |
6fc6879b | 1563 | } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) { |
e6270129 | 1564 | if (maxkeylen % 8) { |
f049052b BG |
1565 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
1566 | "WPA: Unsupported AES-WRAP len %lu", | |
e6270129 | 1567 | (unsigned long) maxkeylen); |
6fc6879b JM |
1568 | return -1; |
1569 | } | |
1570 | if (maxkeylen > sizeof(gd->gtk)) { | |
f049052b BG |
1571 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
1572 | "WPA: AES-WRAP key data " | |
1573 | "too long (keydatalen=%lu maxkeylen=%lu)", | |
e6270129 | 1574 | (unsigned long) key_data_len, |
f049052b | 1575 | (unsigned long) maxkeylen); |
6fc6879b JM |
1576 | return -1; |
1577 | } | |
98cd3d1c JM |
1578 | if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8, |
1579 | key_data, gd->gtk)) { | |
f049052b BG |
1580 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
1581 | "WPA: AES unwrap failed - could not decrypt " | |
1582 | "GTK"); | |
6fc6879b JM |
1583 | return -1; |
1584 | } | |
1585 | } else { | |
f049052b BG |
1586 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
1587 | "WPA: Unsupported key_info type %d", ver); | |
6fc6879b JM |
1588 | return -1; |
1589 | } | |
1590 | gd->tx = wpa_supplicant_gtk_tx_bit_workaround( | |
1591 | sm, !!(key_info & WPA_KEY_INFO_TXRX)); | |
1592 | return 0; | |
1593 | } | |
1594 | ||
1595 | ||
1596 | static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm, | |
1597 | const struct wpa_eapol_key *key, | |
1598 | int ver, u16 key_info) | |
1599 | { | |
5e3b5197 | 1600 | size_t mic_len, hdrlen, rlen; |
6fc6879b | 1601 | struct wpa_eapol_key *reply; |
5e3b5197 | 1602 | u8 *rbuf, *key_mic; |
6fc6879b | 1603 | |
567da5bb | 1604 | mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); |
6d014ffc | 1605 | hdrlen = sizeof(*reply) + mic_len + 2; |
6fc6879b | 1606 | rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL, |
5e3b5197 | 1607 | hdrlen, &rlen, (void *) &reply); |
6fc6879b JM |
1608 | if (rbuf == NULL) |
1609 | return -1; | |
1610 | ||
a14896e8 JM |
1611 | reply->type = (sm->proto == WPA_PROTO_RSN || |
1612 | sm->proto == WPA_PROTO_OSEN) ? | |
6fc6879b JM |
1613 | EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA; |
1614 | key_info &= WPA_KEY_INFO_KEY_INDEX_MASK; | |
4a26ccda JM |
1615 | key_info |= ver | WPA_KEY_INFO_SECURE; |
1616 | if (mic_len) | |
1617 | key_info |= WPA_KEY_INFO_MIC; | |
b44d1efd VK |
1618 | else |
1619 | key_info |= WPA_KEY_INFO_ENCR_KEY_DATA; | |
6fc6879b | 1620 | WPA_PUT_BE16(reply->key_info, key_info); |
a14896e8 | 1621 | if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) |
6fc6879b JM |
1622 | WPA_PUT_BE16(reply->key_length, 0); |
1623 | else | |
1624 | os_memcpy(reply->key_length, key->key_length, 2); | |
1625 | os_memcpy(reply->replay_counter, key->replay_counter, | |
1626 | WPA_REPLAY_COUNTER_LEN); | |
1627 | ||
6d014ffc JM |
1628 | key_mic = (u8 *) (reply + 1); |
1629 | WPA_PUT_BE16(key_mic + mic_len, 0); | |
6fc6879b | 1630 | |
f049052b | 1631 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2"); |
1049af7e JM |
1632 | return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL, |
1633 | rbuf, rlen, key_mic); | |
6fc6879b JM |
1634 | } |
1635 | ||
1636 | ||
1637 | static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm, | |
1638 | const unsigned char *src_addr, | |
1639 | const struct wpa_eapol_key *key, | |
e6270129 JM |
1640 | const u8 *key_data, |
1641 | size_t key_data_len, u16 ver) | |
6fc6879b | 1642 | { |
e6270129 | 1643 | u16 key_info; |
6fc6879b JM |
1644 | int rekey, ret; |
1645 | struct wpa_gtk_data gd; | |
73ed03f3 | 1646 | const u8 *key_rsc; |
6fc6879b | 1647 | |
04243740 | 1648 | if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) { |
761396e4 JM |
1649 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
1650 | "WPA: Group Key Handshake started prior to completion of 4-way handshake"); | |
1651 | goto failed; | |
1652 | } | |
1653 | ||
6fc6879b JM |
1654 | os_memset(&gd, 0, sizeof(gd)); |
1655 | ||
1656 | rekey = wpa_sm_get_state(sm) == WPA_COMPLETED; | |
f049052b BG |
1657 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key " |
1658 | "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver); | |
6fc6879b JM |
1659 | |
1660 | key_info = WPA_GET_BE16(key->key_info); | |
6fc6879b | 1661 | |
a14896e8 | 1662 | if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) { |
e6270129 JM |
1663 | ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data, |
1664 | key_data_len, key_info, | |
6fc6879b JM |
1665 | &gd); |
1666 | } else { | |
e6270129 JM |
1667 | ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data, |
1668 | key_data_len, | |
1669 | key_info, ver, &gd); | |
6fc6879b JM |
1670 | } |
1671 | ||
1672 | wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE); | |
1673 | ||
1674 | if (ret) | |
83935317 | 1675 | goto failed; |
6fc6879b | 1676 | |
73ed03f3 MS |
1677 | key_rsc = key->key_rsc; |
1678 | if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc)) | |
1679 | key_rsc = null_rsc; | |
1680 | ||
87e2db16 | 1681 | if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) || |
c93b7e18 | 1682 | wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0) |
83935317 | 1683 | goto failed; |
fbfc974c | 1684 | os_memset(&gd, 0, sizeof(gd)); |
6fc6879b JM |
1685 | |
1686 | if (rekey) { | |
0f057fb2 | 1687 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying " |
6fc6879b JM |
1688 | "completed with " MACSTR " [GTK=%s]", |
1689 | MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher)); | |
1690 | wpa_sm_cancel_auth_timeout(sm); | |
1691 | wpa_sm_set_state(sm, WPA_COMPLETED); | |
1692 | } else { | |
1693 | wpa_supplicant_key_neg_complete(sm, sm->bssid, | |
1694 | key_info & | |
1695 | WPA_KEY_INFO_SECURE); | |
1696 | } | |
392e68e8 SD |
1697 | |
1698 | wpa_sm_set_rekey_offload(sm); | |
1699 | ||
83935317 JM |
1700 | return; |
1701 | ||
1702 | failed: | |
fbfc974c | 1703 | os_memset(&gd, 0, sizeof(gd)); |
83935317 | 1704 | wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED); |
6fc6879b JM |
1705 | } |
1706 | ||
1707 | ||
1708 | static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm, | |
6d014ffc | 1709 | struct wpa_eapol_key *key, |
6fc6879b JM |
1710 | u16 ver, |
1711 | const u8 *buf, size_t len) | |
1712 | { | |
98cd3d1c | 1713 | u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN]; |
6fc6879b | 1714 | int ok = 0; |
567da5bb | 1715 | size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); |
6fc6879b | 1716 | |
6d014ffc | 1717 | os_memcpy(mic, key + 1, mic_len); |
6fc6879b | 1718 | if (sm->tptk_set) { |
6d014ffc | 1719 | os_memset(key + 1, 0, mic_len); |
98cd3d1c | 1720 | wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len, sm->key_mgmt, |
6d014ffc JM |
1721 | ver, buf, len, (u8 *) (key + 1)); |
1722 | if (os_memcmp_const(mic, key + 1, mic_len) != 0) { | |
f049052b BG |
1723 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
1724 | "WPA: Invalid EAPOL-Key MIC " | |
1725 | "when using TPTK - ignoring TPTK"); | |
6fc6879b JM |
1726 | } else { |
1727 | ok = 1; | |
1728 | sm->tptk_set = 0; | |
1729 | sm->ptk_set = 1; | |
1730 | os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk)); | |
d2c33b91 | 1731 | os_memset(&sm->tptk, 0, sizeof(sm->tptk)); |
a00e946c MV |
1732 | /* |
1733 | * This assures the same TPTK in sm->tptk can never be | |
1734 | * copied twice to sm->pkt as the new PTK. In | |
1735 | * combination with the installed flag in the wpa_ptk | |
1736 | * struct, this assures the same PTK is only installed | |
1737 | * once. | |
1738 | */ | |
1739 | sm->renew_snonce = 1; | |
6fc6879b JM |
1740 | } |
1741 | } | |
1742 | ||
1743 | if (!ok && sm->ptk_set) { | |
6d014ffc | 1744 | os_memset(key + 1, 0, mic_len); |
98cd3d1c | 1745 | wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len, sm->key_mgmt, |
6d014ffc JM |
1746 | ver, buf, len, (u8 *) (key + 1)); |
1747 | if (os_memcmp_const(mic, key + 1, mic_len) != 0) { | |
f049052b BG |
1748 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
1749 | "WPA: Invalid EAPOL-Key MIC - " | |
1750 | "dropping packet"); | |
6fc6879b JM |
1751 | return -1; |
1752 | } | |
1753 | ok = 1; | |
1754 | } | |
1755 | ||
1756 | if (!ok) { | |
f049052b BG |
1757 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
1758 | "WPA: Could not verify EAPOL-Key MIC - " | |
1759 | "dropping packet"); | |
6fc6879b JM |
1760 | return -1; |
1761 | } | |
1762 | ||
1763 | os_memcpy(sm->rx_replay_counter, key->replay_counter, | |
1764 | WPA_REPLAY_COUNTER_LEN); | |
1765 | sm->rx_replay_counter_set = 1; | |
1766 | return 0; | |
1767 | } | |
1768 | ||
1769 | ||
1770 | /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */ | |
1771 | static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm, | |
6d014ffc JM |
1772 | struct wpa_eapol_key *key, |
1773 | size_t mic_len, u16 ver, | |
e6270129 | 1774 | u8 *key_data, size_t *key_data_len) |
6fc6879b | 1775 | { |
6fc6879b | 1776 | wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data", |
e6270129 | 1777 | key_data, *key_data_len); |
6fc6879b | 1778 | if (!sm->ptk_set) { |
f049052b BG |
1779 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
1780 | "WPA: PTK not available, cannot decrypt EAPOL-Key Key " | |
1781 | "Data"); | |
6fc6879b JM |
1782 | return -1; |
1783 | } | |
1784 | ||
1785 | /* Decrypt key data here so that this operation does not need | |
1786 | * to be implemented separately for each message type. */ | |
98cd3d1c | 1787 | if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) { |
7cb53ded JM |
1788 | #ifdef CONFIG_NO_RC4 |
1789 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, | |
1790 | "WPA: RC4 not supported in the build"); | |
1791 | return -1; | |
1792 | #else /* CONFIG_NO_RC4 */ | |
6fc6879b | 1793 | u8 ek[32]; |
ef9627cb JM |
1794 | |
1795 | wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4"); | |
6fc6879b | 1796 | os_memcpy(ek, key->key_iv, 16); |
98cd3d1c | 1797 | os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len); |
e6270129 | 1798 | if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) { |
d2c33b91 | 1799 | os_memset(ek, 0, sizeof(ek)); |
f049052b BG |
1800 | wpa_msg(sm->ctx->msg_ctx, MSG_ERROR, |
1801 | "WPA: RC4 failed"); | |
7a215dfc JM |
1802 | return -1; |
1803 | } | |
d2c33b91 | 1804 | os_memset(ek, 0, sizeof(ek)); |
7cb53ded | 1805 | #endif /* CONFIG_NO_RC4 */ |
6fc6879b | 1806 | } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES || |
df0f01d9 | 1807 | ver == WPA_KEY_INFO_TYPE_AES_128_CMAC || |
07a5fe82 | 1808 | sm->key_mgmt == WPA_KEY_MGMT_OWE || |
567da5bb | 1809 | sm->key_mgmt == WPA_KEY_MGMT_DPP || |
929a2ea5 JM |
1810 | sm->key_mgmt == WPA_KEY_MGMT_OSEN || |
1811 | wpa_key_mgmt_suite_b(sm->key_mgmt)) { | |
6fc6879b | 1812 | u8 *buf; |
ef9627cb JM |
1813 | |
1814 | wpa_printf(MSG_DEBUG, | |
1815 | "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)", | |
1816 | (unsigned int) sm->ptk.kek_len); | |
e6270129 | 1817 | if (*key_data_len < 8 || *key_data_len % 8) { |
f049052b | 1818 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
e6270129 JM |
1819 | "WPA: Unsupported AES-WRAP len %u", |
1820 | (unsigned int) *key_data_len); | |
6fc6879b JM |
1821 | return -1; |
1822 | } | |
e6270129 JM |
1823 | *key_data_len -= 8; /* AES-WRAP adds 8 bytes */ |
1824 | buf = os_malloc(*key_data_len); | |
6fc6879b | 1825 | if (buf == NULL) { |
f049052b BG |
1826 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
1827 | "WPA: No memory for AES-UNWRAP buffer"); | |
6fc6879b JM |
1828 | return -1; |
1829 | } | |
98cd3d1c | 1830 | if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8, |
e6270129 | 1831 | key_data, buf)) { |
cd5895e8 | 1832 | bin_clear_free(buf, *key_data_len); |
f049052b BG |
1833 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
1834 | "WPA: AES unwrap failed - " | |
1835 | "could not decrypt EAPOL-Key key data"); | |
6fc6879b JM |
1836 | return -1; |
1837 | } | |
e6270129 | 1838 | os_memcpy(key_data, buf, *key_data_len); |
cd5895e8 | 1839 | bin_clear_free(buf, *key_data_len); |
6d014ffc | 1840 | WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len); |
6fc6879b | 1841 | } else { |
f049052b BG |
1842 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
1843 | "WPA: Unsupported key_info type %d", ver); | |
6fc6879b JM |
1844 | return -1; |
1845 | } | |
1846 | wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data", | |
e6270129 | 1847 | key_data, *key_data_len); |
6fc6879b JM |
1848 | return 0; |
1849 | } | |
1850 | ||
1851 | ||
1852 | /** | |
1853 | * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted | |
1854 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
1855 | */ | |
1856 | void wpa_sm_aborted_cached(struct wpa_sm *sm) | |
1857 | { | |
1858 | if (sm && sm->cur_pmksa) { | |
f049052b BG |
1859 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
1860 | "RSN: Cancelling PMKSA caching attempt"); | |
6fc6879b JM |
1861 | sm->cur_pmksa = NULL; |
1862 | } | |
1863 | } | |
1864 | ||
1865 | ||
f049052b | 1866 | static void wpa_eapol_key_dump(struct wpa_sm *sm, |
5e3b5197 JM |
1867 | const struct wpa_eapol_key *key, |
1868 | unsigned int key_data_len, | |
1869 | const u8 *mic, unsigned int mic_len) | |
6fc6879b JM |
1870 | { |
1871 | #ifndef CONFIG_NO_STDOUT_DEBUG | |
1872 | u16 key_info = WPA_GET_BE16(key->key_info); | |
1873 | ||
f049052b BG |
1874 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type); |
1875 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, | |
1876 | " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)", | |
1877 | key_info, key_info & WPA_KEY_INFO_TYPE_MASK, | |
1878 | (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >> | |
1879 | WPA_KEY_INFO_KEY_INDEX_SHIFT, | |
1880 | (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13, | |
1881 | key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group", | |
1882 | key_info & WPA_KEY_INFO_INSTALL ? " Install" : "", | |
1883 | key_info & WPA_KEY_INFO_ACK ? " Ack" : "", | |
1884 | key_info & WPA_KEY_INFO_MIC ? " MIC" : "", | |
1885 | key_info & WPA_KEY_INFO_SECURE ? " Secure" : "", | |
1886 | key_info & WPA_KEY_INFO_ERROR ? " Error" : "", | |
1887 | key_info & WPA_KEY_INFO_REQUEST ? " Request" : "", | |
1888 | key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : ""); | |
1889 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, | |
1890 | " key_length=%u key_data_length=%u", | |
5e3b5197 | 1891 | WPA_GET_BE16(key->key_length), key_data_len); |
6fc6879b JM |
1892 | wpa_hexdump(MSG_DEBUG, " replay_counter", |
1893 | key->replay_counter, WPA_REPLAY_COUNTER_LEN); | |
1894 | wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN); | |
1895 | wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16); | |
1896 | wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8); | |
1897 | wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8); | |
5e3b5197 | 1898 | wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len); |
6fc6879b JM |
1899 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
1900 | } | |
1901 | ||
1902 | ||
0ab1dd01 JM |
1903 | #ifdef CONFIG_FILS |
1904 | static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len, | |
1905 | size_t *key_data_len) | |
1906 | { | |
1907 | struct wpa_ptk *ptk; | |
1908 | struct ieee802_1x_hdr *hdr; | |
1909 | struct wpa_eapol_key *key; | |
1910 | u8 *pos, *tmp; | |
1911 | const u8 *aad[1]; | |
1912 | size_t aad_len[1]; | |
1913 | ||
1914 | if (*key_data_len < AES_BLOCK_SIZE) { | |
1915 | wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame"); | |
1916 | return -1; | |
1917 | } | |
1918 | ||
1919 | if (sm->tptk_set) | |
1920 | ptk = &sm->tptk; | |
1921 | else if (sm->ptk_set) | |
1922 | ptk = &sm->ptk; | |
1923 | else | |
1924 | return -1; | |
1925 | ||
1926 | hdr = (struct ieee802_1x_hdr *) buf; | |
1927 | key = (struct wpa_eapol_key *) (hdr + 1); | |
1928 | pos = (u8 *) (key + 1); | |
1929 | pos += 2; /* Pointing at the Encrypted Key Data field */ | |
1930 | ||
1931 | tmp = os_malloc(*key_data_len); | |
1932 | if (!tmp) | |
1933 | return -1; | |
1934 | ||
1935 | /* AES-SIV AAD from EAPOL protocol version field (inclusive) to | |
1936 | * to Key Data (exclusive). */ | |
1937 | aad[0] = buf; | |
1938 | aad_len[0] = pos - buf; | |
1939 | if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len, | |
1940 | 1, aad, aad_len, tmp) < 0) { | |
1941 | wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame"); | |
1942 | bin_clear_free(tmp, *key_data_len); | |
1943 | return -1; | |
1944 | } | |
1945 | ||
1946 | /* AEAD decryption and validation completed successfully */ | |
1947 | (*key_data_len) -= AES_BLOCK_SIZE; | |
1948 | wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data", | |
1949 | tmp, *key_data_len); | |
1950 | ||
1951 | /* Replace Key Data field with the decrypted version */ | |
1952 | os_memcpy(pos, tmp, *key_data_len); | |
1953 | pos -= 2; /* Key Data Length field */ | |
1954 | WPA_PUT_BE16(pos, *key_data_len); | |
1955 | bin_clear_free(tmp, *key_data_len); | |
1956 | ||
1957 | if (sm->tptk_set) { | |
1958 | sm->tptk_set = 0; | |
1959 | sm->ptk_set = 1; | |
1960 | os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk)); | |
1961 | os_memset(&sm->tptk, 0, sizeof(sm->tptk)); | |
1962 | } | |
1963 | ||
1964 | os_memcpy(sm->rx_replay_counter, key->replay_counter, | |
1965 | WPA_REPLAY_COUNTER_LEN); | |
1966 | sm->rx_replay_counter_set = 1; | |
1967 | ||
1968 | return 0; | |
1969 | } | |
1970 | #endif /* CONFIG_FILS */ | |
1971 | ||
1972 | ||
6fc6879b JM |
1973 | /** |
1974 | * wpa_sm_rx_eapol - Process received WPA EAPOL frames | |
1975 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
1976 | * @src_addr: Source MAC address of the EAPOL packet | |
1977 | * @buf: Pointer to the beginning of the EAPOL data (EAPOL header) | |
1978 | * @len: Length of the EAPOL frame | |
1979 | * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure | |
1980 | * | |
1981 | * This function is called for each received EAPOL frame. Other than EAPOL-Key | |
1982 | * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is | |
1983 | * only processing WPA and WPA2 EAPOL-Key frames. | |
1984 | * | |
1985 | * The received EAPOL-Key packets are validated and valid packets are replied | |
1986 | * to. In addition, key material (PTK, GTK) is configured at the end of a | |
1987 | * successful key handshake. | |
1988 | */ | |
1989 | int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr, | |
1990 | const u8 *buf, size_t len) | |
1991 | { | |
e6270129 | 1992 | size_t plen, data_len, key_data_len; |
d56d7e56 | 1993 | const struct ieee802_1x_hdr *hdr; |
6fc6879b JM |
1994 | struct wpa_eapol_key *key; |
1995 | u16 key_info, ver; | |
d56d7e56 | 1996 | u8 *tmp = NULL; |
6fc6879b | 1997 | int ret = -1; |
6d014ffc | 1998 | u8 *mic, *key_data; |
5e3b5197 | 1999 | size_t mic_len, keyhdrlen; |
6fc6879b JM |
2000 | |
2001 | #ifdef CONFIG_IEEE80211R | |
2002 | sm->ft_completed = 0; | |
2003 | #endif /* CONFIG_IEEE80211R */ | |
2004 | ||
567da5bb | 2005 | mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); |
6d014ffc | 2006 | keyhdrlen = sizeof(*key) + mic_len + 2; |
5e3b5197 JM |
2007 | |
2008 | if (len < sizeof(*hdr) + keyhdrlen) { | |
f049052b BG |
2009 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
2010 | "WPA: EAPOL frame too short to be a WPA " | |
2011 | "EAPOL-Key (len %lu, expecting at least %lu)", | |
2012 | (unsigned long) len, | |
5e3b5197 | 2013 | (unsigned long) sizeof(*hdr) + keyhdrlen); |
6fc6879b JM |
2014 | return 0; |
2015 | } | |
2016 | ||
d56d7e56 | 2017 | hdr = (const struct ieee802_1x_hdr *) buf; |
6fc6879b JM |
2018 | plen = be_to_host16(hdr->length); |
2019 | data_len = plen + sizeof(*hdr); | |
f049052b BG |
2020 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
2021 | "IEEE 802.1X RX: version=%d type=%d length=%lu", | |
2022 | hdr->version, hdr->type, (unsigned long) plen); | |
6fc6879b JM |
2023 | |
2024 | if (hdr->version < EAPOL_VERSION) { | |
2025 | /* TODO: backwards compatibility */ | |
2026 | } | |
2027 | if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) { | |
f049052b BG |
2028 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
2029 | "WPA: EAPOL frame (type %u) discarded, " | |
6fc6879b JM |
2030 | "not a Key frame", hdr->type); |
2031 | ret = 0; | |
2032 | goto out; | |
2033 | } | |
d56d7e56 | 2034 | wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len); |
5e3b5197 | 2035 | if (plen > len - sizeof(*hdr) || plen < keyhdrlen) { |
f049052b BG |
2036 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
2037 | "WPA: EAPOL frame payload size %lu " | |
2038 | "invalid (frame size %lu)", | |
2039 | (unsigned long) plen, (unsigned long) len); | |
6fc6879b JM |
2040 | ret = 0; |
2041 | goto out; | |
2042 | } | |
d56d7e56 JM |
2043 | if (data_len < len) { |
2044 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, | |
2045 | "WPA: ignoring %lu bytes after the IEEE 802.1X data", | |
2046 | (unsigned long) len - data_len); | |
2047 | } | |
2048 | ||
2049 | /* | |
2050 | * Make a copy of the frame since we need to modify the buffer during | |
2051 | * MAC validation and Key Data decryption. | |
2052 | */ | |
a1f11e34 | 2053 | tmp = os_memdup(buf, data_len); |
d56d7e56 JM |
2054 | if (tmp == NULL) |
2055 | goto out; | |
d56d7e56 | 2056 | key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr)); |
6d014ffc JM |
2057 | mic = (u8 *) (key + 1); |
2058 | key_data = mic + mic_len + 2; | |
6fc6879b JM |
2059 | |
2060 | if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN) | |
2061 | { | |
f049052b BG |
2062 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
2063 | "WPA: EAPOL-Key type (%d) unknown, discarded", | |
2064 | key->type); | |
6fc6879b JM |
2065 | ret = 0; |
2066 | goto out; | |
2067 | } | |
6fc6879b | 2068 | |
6d014ffc JM |
2069 | key_data_len = WPA_GET_BE16(mic + mic_len); |
2070 | wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len); | |
5e3b5197 JM |
2071 | |
2072 | if (key_data_len > plen - keyhdrlen) { | |
d56d7e56 JM |
2073 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key " |
2074 | "frame - key_data overflow (%u > %u)", | |
e6270129 | 2075 | (unsigned int) key_data_len, |
5e3b5197 | 2076 | (unsigned int) (plen - keyhdrlen)); |
d56d7e56 | 2077 | goto out; |
6fc6879b | 2078 | } |
d56d7e56 JM |
2079 | |
2080 | eapol_sm_notify_lower_layer_success(sm->eapol, 0); | |
6fc6879b JM |
2081 | key_info = WPA_GET_BE16(key->key_info); |
2082 | ver = key_info & WPA_KEY_INFO_TYPE_MASK; | |
2083 | if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && | |
a20088e5 | 2084 | #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W) |
6fc6879b | 2085 | ver != WPA_KEY_INFO_TYPE_AES_128_CMAC && |
a20088e5 | 2086 | #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */ |
df0f01d9 | 2087 | ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES && |
929a2ea5 | 2088 | !wpa_key_mgmt_suite_b(sm->key_mgmt) && |
352caf00 | 2089 | !wpa_key_mgmt_fils(sm->key_mgmt) && |
07a5fe82 | 2090 | sm->key_mgmt != WPA_KEY_MGMT_OWE && |
567da5bb | 2091 | sm->key_mgmt != WPA_KEY_MGMT_DPP && |
df0f01d9 | 2092 | sm->key_mgmt != WPA_KEY_MGMT_OSEN) { |
f049052b BG |
2093 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
2094 | "WPA: Unsupported EAPOL-Key descriptor version %d", | |
2095 | ver); | |
6fc6879b JM |
2096 | goto out; |
2097 | } | |
2098 | ||
df0f01d9 JM |
2099 | if (sm->key_mgmt == WPA_KEY_MGMT_OSEN && |
2100 | ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) { | |
2101 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, | |
2102 | "OSEN: Unsupported EAPOL-Key descriptor version %d", | |
2103 | ver); | |
2104 | goto out; | |
2105 | } | |
2106 | ||
352caf00 | 2107 | if ((wpa_key_mgmt_suite_b(sm->key_mgmt) || |
07a5fe82 | 2108 | wpa_key_mgmt_fils(sm->key_mgmt) || |
567da5bb | 2109 | sm->key_mgmt == WPA_KEY_MGMT_DPP || |
07a5fe82 | 2110 | sm->key_mgmt == WPA_KEY_MGMT_OWE) && |
929a2ea5 JM |
2111 | ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) { |
2112 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, | |
2113 | "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)", | |
2114 | ver); | |
2115 | goto out; | |
2116 | } | |
2117 | ||
6fc6879b | 2118 | #ifdef CONFIG_IEEE80211R |
56586197 | 2119 | if (wpa_key_mgmt_ft(sm->key_mgmt)) { |
6fc6879b JM |
2120 | /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */ |
2121 | if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) { | |
f049052b BG |
2122 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
2123 | "FT: AP did not use AES-128-CMAC"); | |
6fc6879b JM |
2124 | goto out; |
2125 | } | |
2126 | } else | |
2127 | #endif /* CONFIG_IEEE80211R */ | |
56586197 JM |
2128 | #ifdef CONFIG_IEEE80211W |
2129 | if (wpa_key_mgmt_sha256(sm->key_mgmt)) { | |
df0f01d9 | 2130 | if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC && |
929a2ea5 | 2131 | sm->key_mgmt != WPA_KEY_MGMT_OSEN && |
352caf00 | 2132 | !wpa_key_mgmt_fils(sm->key_mgmt) && |
929a2ea5 | 2133 | !wpa_key_mgmt_suite_b(sm->key_mgmt)) { |
f049052b BG |
2134 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
2135 | "WPA: AP did not use the " | |
2136 | "negotiated AES-128-CMAC"); | |
56586197 JM |
2137 | goto out; |
2138 | } | |
2139 | } else | |
2140 | #endif /* CONFIG_IEEE80211W */ | |
6fc6879b | 2141 | if (sm->pairwise_cipher == WPA_CIPHER_CCMP && |
929a2ea5 | 2142 | !wpa_key_mgmt_suite_b(sm->key_mgmt) && |
352caf00 | 2143 | !wpa_key_mgmt_fils(sm->key_mgmt) && |
07a5fe82 | 2144 | sm->key_mgmt != WPA_KEY_MGMT_OWE && |
567da5bb | 2145 | sm->key_mgmt != WPA_KEY_MGMT_DPP && |
6fc6879b | 2146 | ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) { |
f049052b BG |
2147 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
2148 | "WPA: CCMP is used, but EAPOL-Key " | |
2149 | "descriptor version (%d) is not 2", ver); | |
6fc6879b JM |
2150 | if (sm->group_cipher != WPA_CIPHER_CCMP && |
2151 | !(key_info & WPA_KEY_INFO_KEY_TYPE)) { | |
2152 | /* Earlier versions of IEEE 802.11i did not explicitly | |
2153 | * require version 2 descriptor for all EAPOL-Key | |
2154 | * packets, so allow group keys to use version 1 if | |
2155 | * CCMP is not used for them. */ | |
f049052b BG |
2156 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
2157 | "WPA: Backwards compatibility: allow invalid " | |
2158 | "version for non-CCMP group keys"); | |
9f6a7cdd JM |
2159 | } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) { |
2160 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, | |
2161 | "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used"); | |
6fc6879b JM |
2162 | } else |
2163 | goto out; | |
801e1173 | 2164 | } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP && |
929a2ea5 | 2165 | !wpa_key_mgmt_suite_b(sm->key_mgmt) && |
801e1173 | 2166 | ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) { |
eb7719ff JM |
2167 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
2168 | "WPA: GCMP is used, but EAPOL-Key " | |
2169 | "descriptor version (%d) is not 2", ver); | |
2170 | goto out; | |
2171 | } | |
6fc6879b | 2172 | |
a0bf1b68 | 2173 | if (sm->rx_replay_counter_set && |
6fc6879b JM |
2174 | os_memcmp(key->replay_counter, sm->rx_replay_counter, |
2175 | WPA_REPLAY_COUNTER_LEN) <= 0) { | |
f049052b BG |
2176 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
2177 | "WPA: EAPOL-Key Replay Counter did not increase - " | |
2178 | "dropping packet"); | |
6fc6879b JM |
2179 | goto out; |
2180 | } | |
2181 | ||
a0bf1b68 JM |
2182 | if (key_info & WPA_KEY_INFO_SMK_MESSAGE) { |
2183 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, | |
2184 | "WPA: Unsupported SMK bit in key_info"); | |
2185 | goto out; | |
2186 | } | |
2187 | ||
2188 | if (!(key_info & WPA_KEY_INFO_ACK)) { | |
f049052b BG |
2189 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
2190 | "WPA: No Ack bit in key_info"); | |
6fc6879b JM |
2191 | goto out; |
2192 | } | |
2193 | ||
2194 | if (key_info & WPA_KEY_INFO_REQUEST) { | |
f049052b BG |
2195 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
2196 | "WPA: EAPOL-Key with Request bit - dropped"); | |
6fc6879b JM |
2197 | goto out; |
2198 | } | |
2199 | ||
a0bf1b68 | 2200 | if ((key_info & WPA_KEY_INFO_MIC) && |
6d014ffc | 2201 | wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len)) |
6fc6879b JM |
2202 | goto out; |
2203 | ||
0ab1dd01 JM |
2204 | #ifdef CONFIG_FILS |
2205 | if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) { | |
2206 | if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len)) | |
2207 | goto out; | |
2208 | } | |
2209 | #endif /* CONFIG_FILS */ | |
2210 | ||
a14896e8 | 2211 | if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) && |
0ab1dd01 | 2212 | (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) { |
6d014ffc JM |
2213 | if (wpa_supplicant_decrypt_key_data(sm, key, mic_len, |
2214 | ver, key_data, | |
e6270129 | 2215 | &key_data_len)) |
d56d7e56 | 2216 | goto out; |
6fc6879b JM |
2217 | } |
2218 | ||
2219 | if (key_info & WPA_KEY_INFO_KEY_TYPE) { | |
2220 | if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) { | |
f049052b BG |
2221 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
2222 | "WPA: Ignored EAPOL-Key (Pairwise) with " | |
2223 | "non-zero key index"); | |
6fc6879b JM |
2224 | goto out; |
2225 | } | |
a0bf1b68 JM |
2226 | if (key_info & (WPA_KEY_INFO_MIC | |
2227 | WPA_KEY_INFO_ENCR_KEY_DATA)) { | |
6fc6879b | 2228 | /* 3/4 4-Way Handshake */ |
e6270129 JM |
2229 | wpa_supplicant_process_3_of_4(sm, key, ver, key_data, |
2230 | key_data_len); | |
6fc6879b JM |
2231 | } else { |
2232 | /* 1/4 4-Way Handshake */ | |
2233 | wpa_supplicant_process_1_of_4(sm, src_addr, key, | |
e6270129 JM |
2234 | ver, key_data, |
2235 | key_data_len); | |
6fc6879b | 2236 | } |
6fc6879b | 2237 | } else { |
16eb4858 JM |
2238 | if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) || |
2239 | (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) { | |
6fc6879b JM |
2240 | /* 1/2 Group Key Handshake */ |
2241 | wpa_supplicant_process_1_of_2(sm, src_addr, key, | |
e6270129 JM |
2242 | key_data, key_data_len, |
2243 | ver); | |
6fc6879b | 2244 | } else { |
f049052b | 2245 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
16eb4858 | 2246 | "WPA: EAPOL-Key (Group) without Mic/Encr bit - " |
f049052b | 2247 | "dropped"); |
6fc6879b JM |
2248 | } |
2249 | } | |
2250 | ||
2251 | ret = 1; | |
2252 | ||
2253 | out: | |
fbfc974c | 2254 | bin_clear_free(tmp, data_len); |
6fc6879b JM |
2255 | return ret; |
2256 | } | |
2257 | ||
2258 | ||
2259 | #ifdef CONFIG_CTRL_IFACE | |
6fc6879b JM |
2260 | static u32 wpa_key_mgmt_suite(struct wpa_sm *sm) |
2261 | { | |
2262 | switch (sm->key_mgmt) { | |
2263 | case WPA_KEY_MGMT_IEEE8021X: | |
a14896e8 JM |
2264 | return ((sm->proto == WPA_PROTO_RSN || |
2265 | sm->proto == WPA_PROTO_OSEN) ? | |
6fc6879b JM |
2266 | RSN_AUTH_KEY_MGMT_UNSPEC_802_1X : |
2267 | WPA_AUTH_KEY_MGMT_UNSPEC_802_1X); | |
2268 | case WPA_KEY_MGMT_PSK: | |
2269 | return (sm->proto == WPA_PROTO_RSN ? | |
2270 | RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X : | |
2271 | WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X); | |
2272 | #ifdef CONFIG_IEEE80211R | |
2273 | case WPA_KEY_MGMT_FT_IEEE8021X: | |
2274 | return RSN_AUTH_KEY_MGMT_FT_802_1X; | |
2275 | case WPA_KEY_MGMT_FT_PSK: | |
2276 | return RSN_AUTH_KEY_MGMT_FT_PSK; | |
2277 | #endif /* CONFIG_IEEE80211R */ | |
56586197 JM |
2278 | #ifdef CONFIG_IEEE80211W |
2279 | case WPA_KEY_MGMT_IEEE8021X_SHA256: | |
2280 | return RSN_AUTH_KEY_MGMT_802_1X_SHA256; | |
2281 | case WPA_KEY_MGMT_PSK_SHA256: | |
2282 | return RSN_AUTH_KEY_MGMT_PSK_SHA256; | |
2283 | #endif /* CONFIG_IEEE80211W */ | |
369c8d7b JM |
2284 | case WPA_KEY_MGMT_CCKM: |
2285 | return (sm->proto == WPA_PROTO_RSN ? | |
2286 | RSN_AUTH_KEY_MGMT_CCKM: | |
2287 | WPA_AUTH_KEY_MGMT_CCKM); | |
6fc6879b JM |
2288 | case WPA_KEY_MGMT_WPA_NONE: |
2289 | return WPA_AUTH_KEY_MGMT_NONE; | |
666497c8 JM |
2290 | case WPA_KEY_MGMT_IEEE8021X_SUITE_B: |
2291 | return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B; | |
5e3b5197 JM |
2292 | case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192: |
2293 | return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192; | |
6fc6879b JM |
2294 | default: |
2295 | return 0; | |
2296 | } | |
2297 | } | |
2298 | ||
2299 | ||
6fc6879b JM |
2300 | #define RSN_SUITE "%02x-%02x-%02x-%d" |
2301 | #define RSN_SUITE_ARG(s) \ | |
2302 | ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff | |
2303 | ||
2304 | /** | |
2305 | * wpa_sm_get_mib - Dump text list of MIB entries | |
2306 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
2307 | * @buf: Buffer for the list | |
2308 | * @buflen: Length of the buffer | |
2309 | * Returns: Number of bytes written to buffer | |
2310 | * | |
2311 | * This function is used fetch dot11 MIB variables. | |
2312 | */ | |
2313 | int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen) | |
2314 | { | |
2315 | char pmkid_txt[PMKID_LEN * 2 + 1]; | |
2316 | int rsna, ret; | |
2317 | size_t len; | |
2318 | ||
2319 | if (sm->cur_pmksa) { | |
2320 | wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt), | |
2321 | sm->cur_pmksa->pmkid, PMKID_LEN); | |
2322 | } else | |
2323 | pmkid_txt[0] = '\0'; | |
2324 | ||
56586197 JM |
2325 | if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) || |
2326 | wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) && | |
6fc6879b JM |
2327 | sm->proto == WPA_PROTO_RSN) |
2328 | rsna = 1; | |
2329 | else | |
2330 | rsna = 0; | |
2331 | ||
2332 | ret = os_snprintf(buf, buflen, | |
2333 | "dot11RSNAOptionImplemented=TRUE\n" | |
2334 | "dot11RSNAPreauthenticationImplemented=TRUE\n" | |
2335 | "dot11RSNAEnabled=%s\n" | |
2336 | "dot11RSNAPreauthenticationEnabled=%s\n" | |
2337 | "dot11RSNAConfigVersion=%d\n" | |
2338 | "dot11RSNAConfigPairwiseKeysSupported=5\n" | |
2339 | "dot11RSNAConfigGroupCipherSize=%d\n" | |
2340 | "dot11RSNAConfigPMKLifetime=%d\n" | |
2341 | "dot11RSNAConfigPMKReauthThreshold=%d\n" | |
2342 | "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n" | |
2343 | "dot11RSNAConfigSATimeout=%d\n", | |
2344 | rsna ? "TRUE" : "FALSE", | |
2345 | rsna ? "TRUE" : "FALSE", | |
2346 | RSN_VERSION, | |
c3550295 | 2347 | wpa_cipher_key_len(sm->group_cipher) * 8, |
6fc6879b JM |
2348 | sm->dot11RSNAConfigPMKLifetime, |
2349 | sm->dot11RSNAConfigPMKReauthThreshold, | |
2350 | sm->dot11RSNAConfigSATimeout); | |
d85e1fc8 | 2351 | if (os_snprintf_error(buflen, ret)) |
6fc6879b JM |
2352 | return 0; |
2353 | len = ret; | |
2354 | ||
2355 | ret = os_snprintf( | |
2356 | buf + len, buflen - len, | |
2357 | "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n" | |
2358 | "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n" | |
2359 | "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n" | |
2360 | "dot11RSNAPMKIDUsed=%s\n" | |
2361 | "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n" | |
2362 | "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n" | |
2363 | "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n" | |
2364 | "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n" | |
2365 | "dot11RSNA4WayHandshakeFailures=%u\n", | |
2366 | RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)), | |
c3550295 JM |
2367 | RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto, |
2368 | sm->pairwise_cipher)), | |
2369 | RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto, | |
2370 | sm->group_cipher)), | |
6fc6879b JM |
2371 | pmkid_txt, |
2372 | RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)), | |
c3550295 JM |
2373 | RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto, |
2374 | sm->pairwise_cipher)), | |
2375 | RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto, | |
2376 | sm->group_cipher)), | |
6fc6879b | 2377 | sm->dot11RSNA4WayHandshakeFailures); |
1f102d3b | 2378 | if (!os_snprintf_error(buflen - len, ret)) |
6fc6879b JM |
2379 | len += ret; |
2380 | ||
2381 | return (int) len; | |
2382 | } | |
2383 | #endif /* CONFIG_CTRL_IFACE */ | |
2384 | ||
2385 | ||
2386 | static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry, | |
6aaac006 | 2387 | void *ctx, enum pmksa_free_reason reason) |
6fc6879b JM |
2388 | { |
2389 | struct wpa_sm *sm = ctx; | |
6aaac006 | 2390 | int deauth = 0; |
6fc6879b | 2391 | |
6aaac006 DW |
2392 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: " |
2393 | MACSTR " reason=%d", MAC2STR(entry->aa), reason); | |
2394 | ||
2395 | if (sm->cur_pmksa == entry) { | |
2396 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, | |
2397 | "RSN: %s current PMKSA entry", | |
2398 | reason == PMKSA_REPLACE ? "replaced" : "removed"); | |
2399 | pmksa_cache_clear_current(sm); | |
2400 | ||
2401 | /* | |
2402 | * If an entry is simply being replaced, there's no need to | |
2403 | * deauthenticate because it will be immediately re-added. | |
2404 | * This happens when EAP authentication is completed again | |
2405 | * (reauth or failed PMKSA caching attempt). | |
2406 | */ | |
2407 | if (reason != PMKSA_REPLACE) | |
2408 | deauth = 1; | |
2409 | } | |
2410 | ||
2411 | if (reason == PMKSA_EXPIRE && | |
6fc6879b JM |
2412 | (sm->pmk_len == entry->pmk_len && |
2413 | os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) { | |
f049052b | 2414 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
6aaac006 DW |
2415 | "RSN: deauthenticating due to expired PMK"); |
2416 | pmksa_cache_clear_current(sm); | |
2417 | deauth = 1; | |
2418 | } | |
6fc6879b | 2419 | |
6aaac006 | 2420 | if (deauth) { |
6fc6879b JM |
2421 | os_memset(sm->pmk, 0, sizeof(sm->pmk)); |
2422 | wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED); | |
6fc6879b JM |
2423 | } |
2424 | } | |
2425 | ||
2426 | ||
2427 | /** | |
2428 | * wpa_sm_init - Initialize WPA state machine | |
2429 | * @ctx: Context pointer for callbacks; this needs to be an allocated buffer | |
2430 | * Returns: Pointer to the allocated WPA state machine data | |
2431 | * | |
2432 | * This function is used to allocate a new WPA state machine and the returned | |
2433 | * value is passed to all WPA state machine calls. | |
2434 | */ | |
2435 | struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx) | |
2436 | { | |
2437 | struct wpa_sm *sm; | |
2438 | ||
2439 | sm = os_zalloc(sizeof(*sm)); | |
2440 | if (sm == NULL) | |
2441 | return NULL; | |
c5b26e33 | 2442 | dl_list_init(&sm->pmksa_candidates); |
6fc6879b JM |
2443 | sm->renew_snonce = 1; |
2444 | sm->ctx = ctx; | |
2445 | ||
2446 | sm->dot11RSNAConfigPMKLifetime = 43200; | |
2447 | sm->dot11RSNAConfigPMKReauthThreshold = 70; | |
2448 | sm->dot11RSNAConfigSATimeout = 60; | |
2449 | ||
2450 | sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm); | |
2451 | if (sm->pmksa == NULL) { | |
f049052b BG |
2452 | wpa_msg(sm->ctx->msg_ctx, MSG_ERROR, |
2453 | "RSN: PMKSA cache initialization failed"); | |
6fc6879b JM |
2454 | os_free(sm); |
2455 | return NULL; | |
2456 | } | |
2457 | ||
2458 | return sm; | |
2459 | } | |
2460 | ||
2461 | ||
2462 | /** | |
2463 | * wpa_sm_deinit - Deinitialize WPA state machine | |
2464 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
2465 | */ | |
2466 | void wpa_sm_deinit(struct wpa_sm *sm) | |
2467 | { | |
2468 | if (sm == NULL) | |
2469 | return; | |
2470 | pmksa_cache_deinit(sm->pmksa); | |
2471 | eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL); | |
581a8cde | 2472 | eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL); |
6fc6879b JM |
2473 | os_free(sm->assoc_wpa_ie); |
2474 | os_free(sm->ap_wpa_ie); | |
2475 | os_free(sm->ap_rsn_ie); | |
71932544 | 2476 | wpa_sm_drop_sa(sm); |
6fc6879b | 2477 | os_free(sm->ctx); |
55046414 JM |
2478 | #ifdef CONFIG_IEEE80211R |
2479 | os_free(sm->assoc_resp_ies); | |
2480 | #endif /* CONFIG_IEEE80211R */ | |
651c6a84 JM |
2481 | #ifdef CONFIG_TESTING_OPTIONS |
2482 | wpabuf_free(sm->test_assoc_ie); | |
2483 | #endif /* CONFIG_TESTING_OPTIONS */ | |
0119d442 | 2484 | #ifdef CONFIG_FILS_SK_PFS |
76e20f4f | 2485 | crypto_ecdh_deinit(sm->fils_ecdh); |
0119d442 | 2486 | #endif /* CONFIG_FILS_SK_PFS */ |
7d440a3b JM |
2487 | #ifdef CONFIG_FILS |
2488 | wpabuf_free(sm->fils_ft_ies); | |
2489 | #endif /* CONFIG_FILS */ | |
0a614799 JM |
2490 | #ifdef CONFIG_OWE |
2491 | crypto_ecdh_deinit(sm->owe_ecdh); | |
2492 | #endif /* CONFIG_OWE */ | |
6fc6879b JM |
2493 | os_free(sm); |
2494 | } | |
2495 | ||
2496 | ||
2497 | /** | |
2498 | * wpa_sm_notify_assoc - Notify WPA state machine about association | |
2499 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
2500 | * @bssid: The BSSID of the new association | |
2501 | * | |
2502 | * This function is called to let WPA state machine know that the connection | |
2503 | * was established. | |
2504 | */ | |
2505 | void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid) | |
2506 | { | |
cb5132bb | 2507 | int clear_keys = 1; |
58a98fb0 | 2508 | |
6fc6879b JM |
2509 | if (sm == NULL) |
2510 | return; | |
2511 | ||
f049052b BG |
2512 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
2513 | "WPA: Association event - clear replay counter"); | |
6fc6879b JM |
2514 | os_memcpy(sm->bssid, bssid, ETH_ALEN); |
2515 | os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN); | |
2516 | sm->rx_replay_counter_set = 0; | |
2517 | sm->renew_snonce = 1; | |
2518 | if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0) | |
2519 | rsn_preauth_deinit(sm); | |
2520 | ||
2521 | #ifdef CONFIG_IEEE80211R | |
58a98fb0 | 2522 | if (wpa_ft_is_completed(sm)) { |
5d5a9f00 JM |
2523 | /* |
2524 | * Clear portValid to kick EAPOL state machine to re-enter | |
2525 | * AUTHENTICATED state to get the EAPOL port Authorized. | |
2526 | */ | |
2527 | eapol_sm_notify_portValid(sm->eapol, FALSE); | |
6fc6879b JM |
2528 | wpa_supplicant_key_neg_complete(sm, sm->bssid, 1); |
2529 | ||
2530 | /* Prepare for the next transition */ | |
76b7981d | 2531 | wpa_ft_prepare_auth_request(sm, NULL); |
58a98fb0 | 2532 | |
cb5132bb | 2533 | clear_keys = 0; |
6fc6879b JM |
2534 | } |
2535 | #endif /* CONFIG_IEEE80211R */ | |
706df429 JM |
2536 | #ifdef CONFIG_FILS |
2537 | if (sm->fils_completed) { | |
2538 | /* | |
2539 | * Clear portValid to kick EAPOL state machine to re-enter | |
2540 | * AUTHENTICATED state to get the EAPOL port Authorized. | |
2541 | */ | |
2542 | wpa_supplicant_key_neg_complete(sm, sm->bssid, 1); | |
cb5132bb | 2543 | clear_keys = 0; |
706df429 JM |
2544 | } |
2545 | #endif /* CONFIG_FILS */ | |
58a98fb0 | 2546 | |
cb5132bb | 2547 | if (clear_keys) { |
58a98fb0 JM |
2548 | /* |
2549 | * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if | |
2550 | * this is not part of a Fast BSS Transition. | |
2551 | */ | |
f049052b | 2552 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK"); |
58a98fb0 | 2553 | sm->ptk_set = 0; |
d2c33b91 | 2554 | os_memset(&sm->ptk, 0, sizeof(sm->ptk)); |
58a98fb0 | 2555 | sm->tptk_set = 0; |
d2c33b91 | 2556 | os_memset(&sm->tptk, 0, sizeof(sm->tptk)); |
cb5132bb | 2557 | os_memset(&sm->gtk, 0, sizeof(sm->gtk)); |
87e2db16 | 2558 | os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep)); |
cb5132bb MV |
2559 | #ifdef CONFIG_IEEE80211W |
2560 | os_memset(&sm->igtk, 0, sizeof(sm->igtk)); | |
87e2db16 | 2561 | os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep)); |
cb5132bb | 2562 | #endif /* CONFIG_IEEE80211W */ |
58a98fb0 | 2563 | } |
40cf22e6 JM |
2564 | |
2565 | #ifdef CONFIG_TDLS | |
2566 | wpa_tdls_assoc(sm); | |
2567 | #endif /* CONFIG_TDLS */ | |
25ef8529 JM |
2568 | |
2569 | #ifdef CONFIG_P2P | |
2570 | os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr)); | |
2571 | #endif /* CONFIG_P2P */ | |
6fc6879b JM |
2572 | } |
2573 | ||
2574 | ||
2575 | /** | |
2576 | * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation | |
2577 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
2578 | * | |
2579 | * This function is called to let WPA state machine know that the connection | |
2580 | * was lost. This will abort any existing pre-authentication session. | |
2581 | */ | |
2582 | void wpa_sm_notify_disassoc(struct wpa_sm *sm) | |
2583 | { | |
ac8e074e JM |
2584 | eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL); |
2585 | eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL); | |
6fc6879b | 2586 | rsn_preauth_deinit(sm); |
0639970d | 2587 | pmksa_cache_clear_current(sm); |
6fc6879b JM |
2588 | if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE) |
2589 | sm->dot11RSNA4WayHandshakeFailures++; | |
40cf22e6 JM |
2590 | #ifdef CONFIG_TDLS |
2591 | wpa_tdls_disassoc(sm); | |
2592 | #endif /* CONFIG_TDLS */ | |
706df429 JM |
2593 | #ifdef CONFIG_FILS |
2594 | sm->fils_completed = 0; | |
2595 | #endif /* CONFIG_FILS */ | |
2a9c5217 JM |
2596 | #ifdef CONFIG_IEEE80211R |
2597 | sm->ft_reassoc_completed = 0; | |
2598 | #endif /* CONFIG_IEEE80211R */ | |
71932544 JM |
2599 | |
2600 | /* Keys are not needed in the WPA state machine anymore */ | |
2601 | wpa_sm_drop_sa(sm); | |
761396e4 JM |
2602 | |
2603 | sm->msg_3_of_4_ok = 0; | |
6fc6879b JM |
2604 | } |
2605 | ||
2606 | ||
2607 | /** | |
2608 | * wpa_sm_set_pmk - Set PMK | |
2609 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
2610 | * @pmk: The new PMK | |
2611 | * @pmk_len: The length of the new PMK in bytes | |
70c93963 | 2612 | * @pmkid: Calculated PMKID |
bc26ac50 | 2613 | * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK |
6fc6879b JM |
2614 | * |
2615 | * Configure the PMK for WPA state machine. | |
2616 | */ | |
bc26ac50 | 2617 | void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len, |
70c93963 | 2618 | const u8 *pmkid, const u8 *bssid) |
6fc6879b JM |
2619 | { |
2620 | if (sm == NULL) | |
2621 | return; | |
2622 | ||
2623 | sm->pmk_len = pmk_len; | |
2624 | os_memcpy(sm->pmk, pmk, pmk_len); | |
2625 | ||
2626 | #ifdef CONFIG_IEEE80211R | |
2627 | /* Set XXKey to be PSK for FT key derivation */ | |
2628 | sm->xxkey_len = pmk_len; | |
2629 | os_memcpy(sm->xxkey, pmk, pmk_len); | |
2630 | #endif /* CONFIG_IEEE80211R */ | |
bc26ac50 JM |
2631 | |
2632 | if (bssid) { | |
70c93963 | 2633 | pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0, |
087a1f4e | 2634 | bssid, sm->own_addr, |
869af307 | 2635 | sm->network_ctx, sm->key_mgmt, NULL); |
bc26ac50 | 2636 | } |
6fc6879b JM |
2637 | } |
2638 | ||
2639 | ||
2640 | /** | |
2641 | * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA | |
2642 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
2643 | * | |
2644 | * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK | |
2645 | * will be cleared. | |
2646 | */ | |
2647 | void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm) | |
2648 | { | |
2649 | if (sm == NULL) | |
2650 | return; | |
2651 | ||
2652 | if (sm->cur_pmksa) { | |
2653 | sm->pmk_len = sm->cur_pmksa->pmk_len; | |
2654 | os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len); | |
2655 | } else { | |
2656 | sm->pmk_len = PMK_LEN; | |
2657 | os_memset(sm->pmk, 0, PMK_LEN); | |
2658 | } | |
2659 | } | |
2660 | ||
2661 | ||
2662 | /** | |
2663 | * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled | |
2664 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
2665 | * @fast_reauth: Whether fast reauthentication (EAP) is allowed | |
2666 | */ | |
2667 | void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth) | |
2668 | { | |
2669 | if (sm) | |
2670 | sm->fast_reauth = fast_reauth; | |
2671 | } | |
2672 | ||
2673 | ||
2674 | /** | |
2675 | * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks | |
2676 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
2677 | * @scard_ctx: Context pointer for smartcard related callback functions | |
2678 | */ | |
2679 | void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx) | |
2680 | { | |
2681 | if (sm == NULL) | |
2682 | return; | |
2683 | sm->scard_ctx = scard_ctx; | |
2684 | if (sm->preauth_eapol) | |
2685 | eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx); | |
2686 | } | |
2687 | ||
2688 | ||
2689 | /** | |
2690 | * wpa_sm_set_config - Notification of current configration change | |
2691 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
2692 | * @config: Pointer to current network configuration | |
2693 | * | |
2694 | * Notify WPA state machine that configuration has changed. config will be | |
2695 | * stored as a backpointer to network configuration. This can be %NULL to clear | |
2696 | * the stored pointed. | |
2697 | */ | |
2698 | void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config) | |
2699 | { | |
2700 | if (!sm) | |
2701 | return; | |
2702 | ||
6fc6879b | 2703 | if (config) { |
886a807f | 2704 | sm->network_ctx = config->network_ctx; |
6fc6879b JM |
2705 | sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher; |
2706 | sm->proactive_key_caching = config->proactive_key_caching; | |
2707 | sm->eap_workaround = config->eap_workaround; | |
2708 | sm->eap_conf_ctx = config->eap_conf_ctx; | |
2709 | if (config->ssid) { | |
2710 | os_memcpy(sm->ssid, config->ssid, config->ssid_len); | |
2711 | sm->ssid_len = config->ssid_len; | |
2712 | } else | |
2713 | sm->ssid_len = 0; | |
581a8cde | 2714 | sm->wpa_ptk_rekey = config->wpa_ptk_rekey; |
25ef8529 | 2715 | sm->p2p = config->p2p; |
73ed03f3 | 2716 | sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation; |
869af307 JM |
2717 | #ifdef CONFIG_FILS |
2718 | if (config->fils_cache_id) { | |
2719 | sm->fils_cache_id_set = 1; | |
2720 | os_memcpy(sm->fils_cache_id, config->fils_cache_id, | |
2721 | FILS_CACHE_ID_LEN); | |
2722 | } else { | |
2723 | sm->fils_cache_id_set = 0; | |
2724 | } | |
2725 | #endif /* CONFIG_FILS */ | |
6fc6879b | 2726 | } else { |
886a807f | 2727 | sm->network_ctx = NULL; |
6fc6879b JM |
2728 | sm->allowed_pairwise_cipher = 0; |
2729 | sm->proactive_key_caching = 0; | |
2730 | sm->eap_workaround = 0; | |
2731 | sm->eap_conf_ctx = NULL; | |
2732 | sm->ssid_len = 0; | |
581a8cde | 2733 | sm->wpa_ptk_rekey = 0; |
25ef8529 | 2734 | sm->p2p = 0; |
73ed03f3 | 2735 | sm->wpa_rsc_relaxation = 0; |
6fc6879b | 2736 | } |
6fc6879b JM |
2737 | } |
2738 | ||
2739 | ||
2740 | /** | |
2741 | * wpa_sm_set_own_addr - Set own MAC address | |
2742 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
2743 | * @addr: Own MAC address | |
2744 | */ | |
2745 | void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr) | |
2746 | { | |
2747 | if (sm) | |
2748 | os_memcpy(sm->own_addr, addr, ETH_ALEN); | |
2749 | } | |
2750 | ||
2751 | ||
2752 | /** | |
2753 | * wpa_sm_set_ifname - Set network interface name | |
2754 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
2755 | * @ifname: Interface name | |
2756 | * @bridge_ifname: Optional bridge interface name (for pre-auth) | |
2757 | */ | |
2758 | void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname, | |
2759 | const char *bridge_ifname) | |
2760 | { | |
2761 | if (sm) { | |
2762 | sm->ifname = ifname; | |
2763 | sm->bridge_ifname = bridge_ifname; | |
2764 | } | |
2765 | } | |
2766 | ||
2767 | ||
2768 | /** | |
2769 | * wpa_sm_set_eapol - Set EAPOL state machine pointer | |
2770 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
2771 | * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init() | |
2772 | */ | |
2773 | void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol) | |
2774 | { | |
2775 | if (sm) | |
2776 | sm->eapol = eapol; | |
2777 | } | |
2778 | ||
2779 | ||
2780 | /** | |
2781 | * wpa_sm_set_param - Set WPA state machine parameters | |
2782 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
2783 | * @param: Parameter field | |
2784 | * @value: Parameter value | |
2785 | * Returns: 0 on success, -1 on failure | |
2786 | */ | |
2787 | int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param, | |
2788 | unsigned int value) | |
2789 | { | |
2790 | int ret = 0; | |
2791 | ||
2792 | if (sm == NULL) | |
2793 | return -1; | |
2794 | ||
2795 | switch (param) { | |
2796 | case RSNA_PMK_LIFETIME: | |
2797 | if (value > 0) | |
2798 | sm->dot11RSNAConfigPMKLifetime = value; | |
2799 | else | |
2800 | ret = -1; | |
2801 | break; | |
2802 | case RSNA_PMK_REAUTH_THRESHOLD: | |
2803 | if (value > 0 && value <= 100) | |
2804 | sm->dot11RSNAConfigPMKReauthThreshold = value; | |
2805 | else | |
2806 | ret = -1; | |
2807 | break; | |
2808 | case RSNA_SA_TIMEOUT: | |
2809 | if (value > 0) | |
2810 | sm->dot11RSNAConfigSATimeout = value; | |
2811 | else | |
2812 | ret = -1; | |
2813 | break; | |
2814 | case WPA_PARAM_PROTO: | |
2815 | sm->proto = value; | |
2816 | break; | |
2817 | case WPA_PARAM_PAIRWISE: | |
2818 | sm->pairwise_cipher = value; | |
2819 | break; | |
2820 | case WPA_PARAM_GROUP: | |
2821 | sm->group_cipher = value; | |
2822 | break; | |
2823 | case WPA_PARAM_KEY_MGMT: | |
2824 | sm->key_mgmt = value; | |
2825 | break; | |
2826 | #ifdef CONFIG_IEEE80211W | |
2827 | case WPA_PARAM_MGMT_GROUP: | |
2828 | sm->mgmt_group_cipher = value; | |
2829 | break; | |
2830 | #endif /* CONFIG_IEEE80211W */ | |
2831 | case WPA_PARAM_RSN_ENABLED: | |
2832 | sm->rsn_enabled = value; | |
2833 | break; | |
e820cf95 JM |
2834 | case WPA_PARAM_MFP: |
2835 | sm->mfp = value; | |
2836 | break; | |
6fc6879b JM |
2837 | default: |
2838 | break; | |
2839 | } | |
2840 | ||
2841 | return ret; | |
2842 | } | |
2843 | ||
2844 | ||
6fc6879b JM |
2845 | /** |
2846 | * wpa_sm_get_status - Get WPA state machine | |
2847 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
2848 | * @buf: Buffer for status information | |
2849 | * @buflen: Maximum buffer length | |
2850 | * @verbose: Whether to include verbose status information | |
2851 | * Returns: Number of bytes written to buf. | |
2852 | * | |
2853 | * Query WPA state machine for status information. This function fills in | |
2854 | * a text area with current status information. If the buffer (buf) is not | |
2855 | * large enough, status information will be truncated to fit the buffer. | |
2856 | */ | |
2857 | int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen, | |
2858 | int verbose) | |
2859 | { | |
2860 | char *pos = buf, *end = buf + buflen; | |
2861 | int ret; | |
2862 | ||
2863 | ret = os_snprintf(pos, end - pos, | |
2864 | "pairwise_cipher=%s\n" | |
2865 | "group_cipher=%s\n" | |
2866 | "key_mgmt=%s\n", | |
2867 | wpa_cipher_txt(sm->pairwise_cipher), | |
2868 | wpa_cipher_txt(sm->group_cipher), | |
2869 | wpa_key_mgmt_txt(sm->key_mgmt, sm->proto)); | |
d85e1fc8 | 2870 | if (os_snprintf_error(end - pos, ret)) |
6fc6879b JM |
2871 | return pos - buf; |
2872 | pos += ret; | |
13e1d2e2 JM |
2873 | |
2874 | if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) { | |
2875 | struct wpa_ie_data rsn; | |
2876 | if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) | |
2877 | >= 0 && | |
2878 | rsn.capabilities & (WPA_CAPABILITY_MFPR | | |
2879 | WPA_CAPABILITY_MFPC)) { | |
3a5954ef JM |
2880 | ret = os_snprintf(pos, end - pos, "pmf=%d\n" |
2881 | "mgmt_group_cipher=%s\n", | |
13e1d2e2 | 2882 | (rsn.capabilities & |
3a5954ef JM |
2883 | WPA_CAPABILITY_MFPR) ? 2 : 1, |
2884 | wpa_cipher_txt( | |
2885 | sm->mgmt_group_cipher)); | |
d85e1fc8 | 2886 | if (os_snprintf_error(end - pos, ret)) |
13e1d2e2 JM |
2887 | return pos - buf; |
2888 | pos += ret; | |
2889 | } | |
2890 | } | |
2891 | ||
6fc6879b JM |
2892 | return pos - buf; |
2893 | } | |
2894 | ||
2895 | ||
ae8535b6 JM |
2896 | int wpa_sm_pmf_enabled(struct wpa_sm *sm) |
2897 | { | |
2898 | struct wpa_ie_data rsn; | |
2899 | ||
2900 | if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie) | |
2901 | return 0; | |
2902 | ||
2903 | if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 && | |
2904 | rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC)) | |
2905 | return 1; | |
2906 | ||
2907 | return 0; | |
2908 | } | |
2909 | ||
2910 | ||
6fc6879b JM |
2911 | /** |
2912 | * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration | |
2913 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
2914 | * @wpa_ie: Pointer to buffer for WPA/RSN IE | |
2915 | * @wpa_ie_len: Pointer to the length of the wpa_ie buffer | |
2916 | * Returns: 0 on success, -1 on failure | |
2917 | */ | |
2918 | int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie, | |
2919 | size_t *wpa_ie_len) | |
2920 | { | |
2921 | int res; | |
2922 | ||
2923 | if (sm == NULL) | |
2924 | return -1; | |
2925 | ||
651c6a84 JM |
2926 | #ifdef CONFIG_TESTING_OPTIONS |
2927 | if (sm->test_assoc_ie) { | |
2928 | wpa_printf(MSG_DEBUG, | |
2929 | "TESTING: Replace association WPA/RSN IE"); | |
2930 | if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie)) | |
2931 | return -1; | |
2932 | os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie), | |
2933 | wpabuf_len(sm->test_assoc_ie)); | |
2934 | res = wpabuf_len(sm->test_assoc_ie); | |
2935 | } else | |
2936 | #endif /* CONFIG_TESTING_OPTIONS */ | |
6fc6879b JM |
2937 | res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len); |
2938 | if (res < 0) | |
2939 | return -1; | |
2940 | *wpa_ie_len = res; | |
2941 | ||
2942 | wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default", | |
2943 | wpa_ie, *wpa_ie_len); | |
2944 | ||
2945 | if (sm->assoc_wpa_ie == NULL) { | |
2946 | /* | |
2947 | * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets | |
2948 | * the correct version of the IE even if PMKSA caching is | |
2949 | * aborted (which would remove PMKID from IE generation). | |
2950 | */ | |
a1f11e34 | 2951 | sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len); |
6fc6879b JM |
2952 | if (sm->assoc_wpa_ie == NULL) |
2953 | return -1; | |
2954 | ||
6fc6879b | 2955 | sm->assoc_wpa_ie_len = *wpa_ie_len; |
0866ed00 JM |
2956 | } else { |
2957 | wpa_hexdump(MSG_DEBUG, | |
2958 | "WPA: Leave previously set WPA IE default", | |
2959 | sm->assoc_wpa_ie, sm->assoc_wpa_ie_len); | |
6fc6879b JM |
2960 | } |
2961 | ||
2962 | return 0; | |
2963 | } | |
2964 | ||
2965 | ||
2966 | /** | |
2967 | * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq | |
2968 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
2969 | * @ie: Pointer to IE data (starting from id) | |
2970 | * @len: IE length | |
2971 | * Returns: 0 on success, -1 on failure | |
2972 | * | |
2973 | * Inform WPA state machine about the WPA/RSN IE used in (Re)Association | |
2974 | * Request frame. The IE will be used to override the default value generated | |
2975 | * with wpa_sm_set_assoc_wpa_ie_default(). | |
2976 | */ | |
2977 | int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len) | |
2978 | { | |
2979 | if (sm == NULL) | |
2980 | return -1; | |
2981 | ||
2982 | os_free(sm->assoc_wpa_ie); | |
2983 | if (ie == NULL || len == 0) { | |
f049052b BG |
2984 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
2985 | "WPA: clearing own WPA/RSN IE"); | |
6fc6879b JM |
2986 | sm->assoc_wpa_ie = NULL; |
2987 | sm->assoc_wpa_ie_len = 0; | |
2988 | } else { | |
2989 | wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len); | |
a1f11e34 | 2990 | sm->assoc_wpa_ie = os_memdup(ie, len); |
6fc6879b JM |
2991 | if (sm->assoc_wpa_ie == NULL) |
2992 | return -1; | |
2993 | ||
6fc6879b JM |
2994 | sm->assoc_wpa_ie_len = len; |
2995 | } | |
2996 | ||
2997 | return 0; | |
2998 | } | |
2999 | ||
3000 | ||
3001 | /** | |
3002 | * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp | |
3003 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
3004 | * @ie: Pointer to IE data (starting from id) | |
3005 | * @len: IE length | |
3006 | * Returns: 0 on success, -1 on failure | |
3007 | * | |
3008 | * Inform WPA state machine about the WPA IE used in Beacon / Probe Response | |
3009 | * frame. | |
3010 | */ | |
3011 | int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len) | |
3012 | { | |
3013 | if (sm == NULL) | |
3014 | return -1; | |
3015 | ||
3016 | os_free(sm->ap_wpa_ie); | |
3017 | if (ie == NULL || len == 0) { | |
f049052b BG |
3018 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
3019 | "WPA: clearing AP WPA IE"); | |
6fc6879b JM |
3020 | sm->ap_wpa_ie = NULL; |
3021 | sm->ap_wpa_ie_len = 0; | |
3022 | } else { | |
3023 | wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len); | |
a1f11e34 | 3024 | sm->ap_wpa_ie = os_memdup(ie, len); |
6fc6879b JM |
3025 | if (sm->ap_wpa_ie == NULL) |
3026 | return -1; | |
3027 | ||
6fc6879b JM |
3028 | sm->ap_wpa_ie_len = len; |
3029 | } | |
3030 | ||
3031 | return 0; | |
3032 | } | |
3033 | ||
3034 | ||
3035 | /** | |
3036 | * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp | |
3037 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
3038 | * @ie: Pointer to IE data (starting from id) | |
3039 | * @len: IE length | |
3040 | * Returns: 0 on success, -1 on failure | |
3041 | * | |
3042 | * Inform WPA state machine about the RSN IE used in Beacon / Probe Response | |
3043 | * frame. | |
3044 | */ | |
3045 | int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len) | |
3046 | { | |
3047 | if (sm == NULL) | |
3048 | return -1; | |
3049 | ||
3050 | os_free(sm->ap_rsn_ie); | |
3051 | if (ie == NULL || len == 0) { | |
f049052b BG |
3052 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
3053 | "WPA: clearing AP RSN IE"); | |
6fc6879b JM |
3054 | sm->ap_rsn_ie = NULL; |
3055 | sm->ap_rsn_ie_len = 0; | |
3056 | } else { | |
3057 | wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len); | |
a1f11e34 | 3058 | sm->ap_rsn_ie = os_memdup(ie, len); |
6fc6879b JM |
3059 | if (sm->ap_rsn_ie == NULL) |
3060 | return -1; | |
3061 | ||
6fc6879b JM |
3062 | sm->ap_rsn_ie_len = len; |
3063 | } | |
3064 | ||
3065 | return 0; | |
3066 | } | |
3067 | ||
3068 | ||
3069 | /** | |
3070 | * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE | |
3071 | * @sm: Pointer to WPA state machine data from wpa_sm_init() | |
3072 | * @data: Pointer to data area for parsing results | |
3073 | * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure | |
3074 | * | |
3075 | * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the | |
3076 | * parsed data into data. | |
3077 | */ | |
3078 | int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data) | |
3079 | { | |
2a522e71 JM |
3080 | if (sm == NULL) |
3081 | return -1; | |
3082 | ||
3083 | if (sm->assoc_wpa_ie == NULL) { | |
f049052b BG |
3084 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
3085 | "WPA: No WPA/RSN IE available from association info"); | |
6fc6879b JM |
3086 | return -1; |
3087 | } | |
3088 | if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data)) | |
3089 | return -2; | |
3090 | return 0; | |
3091 | } | |
540264a7 JM |
3092 | |
3093 | ||
3094 | int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len) | |
3095 | { | |
540264a7 | 3096 | return pmksa_cache_list(sm->pmksa, buf, len); |
540264a7 | 3097 | } |
32d5295f JM |
3098 | |
3099 | ||
3459381d JM |
3100 | struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm) |
3101 | { | |
3102 | return pmksa_cache_head(sm->pmksa); | |
3103 | } | |
3104 | ||
3105 | ||
3106 | struct rsn_pmksa_cache_entry * | |
3107 | wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm, | |
3108 | struct rsn_pmksa_cache_entry * entry) | |
3109 | { | |
3110 | return pmksa_cache_add_entry(sm->pmksa, entry); | |
3111 | } | |
3112 | ||
3113 | ||
f705f41b VK |
3114 | void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len, |
3115 | const u8 *pmkid, const u8 *bssid, | |
3116 | const u8 *fils_cache_id) | |
3117 | { | |
3118 | sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0, | |
3119 | bssid, sm->own_addr, sm->network_ctx, | |
3120 | sm->key_mgmt, fils_cache_id); | |
3121 | } | |
3122 | ||
3123 | ||
567da5bb JM |
3124 | int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid, |
3125 | const void *network_ctx) | |
3126 | { | |
3127 | return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx) != NULL; | |
3128 | } | |
3129 | ||
3130 | ||
32d5295f JM |
3131 | void wpa_sm_drop_sa(struct wpa_sm *sm) |
3132 | { | |
f049052b | 3133 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK"); |
32d5295f JM |
3134 | sm->ptk_set = 0; |
3135 | sm->tptk_set = 0; | |
3136 | os_memset(sm->pmk, 0, sizeof(sm->pmk)); | |
3137 | os_memset(&sm->ptk, 0, sizeof(sm->ptk)); | |
3138 | os_memset(&sm->tptk, 0, sizeof(sm->tptk)); | |
cb5132bb | 3139 | os_memset(&sm->gtk, 0, sizeof(sm->gtk)); |
87e2db16 | 3140 | os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep)); |
cb5132bb MV |
3141 | #ifdef CONFIG_IEEE80211W |
3142 | os_memset(&sm->igtk, 0, sizeof(sm->igtk)); | |
87e2db16 | 3143 | os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep)); |
cb5132bb | 3144 | #endif /* CONFIG_IEEE80211W */ |
71932544 JM |
3145 | #ifdef CONFIG_IEEE80211R |
3146 | os_memset(sm->xxkey, 0, sizeof(sm->xxkey)); | |
3147 | os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0)); | |
3148 | os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1)); | |
3149 | #endif /* CONFIG_IEEE80211R */ | |
32d5295f | 3150 | } |
0d7b4409 JM |
3151 | |
3152 | ||
3153 | int wpa_sm_has_ptk(struct wpa_sm *sm) | |
3154 | { | |
3155 | if (sm == NULL) | |
3156 | return 0; | |
3157 | return sm->ptk_set; | |
3158 | } | |
b14a210c JB |
3159 | |
3160 | ||
3161 | void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr) | |
3162 | { | |
3163 | os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN); | |
3164 | } | |
d8a790b9 JM |
3165 | |
3166 | ||
3167 | void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx) | |
3168 | { | |
4033935d | 3169 | pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0); |
d8a790b9 | 3170 | } |
75cad1a0 XC |
3171 | |
3172 | ||
ad3872a3 | 3173 | #ifdef CONFIG_WNM |
75cad1a0 XC |
3174 | int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf) |
3175 | { | |
75cad1a0 XC |
3176 | u16 keyinfo; |
3177 | u8 keylen; /* plaintext key len */ | |
75cad1a0 XC |
3178 | u8 *key_rsc; |
3179 | ||
75cad1a0 | 3180 | if (subelem_id == WNM_SLEEP_SUBELEM_GTK) { |
d2c33b91 JM |
3181 | struct wpa_gtk_data gd; |
3182 | ||
3183 | os_memset(&gd, 0, sizeof(gd)); | |
3184 | keylen = wpa_cipher_key_len(sm->group_cipher); | |
3185 | gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher); | |
3186 | gd.alg = wpa_cipher_to_alg(sm->group_cipher); | |
3187 | if (gd.alg == WPA_ALG_NONE) { | |
3188 | wpa_printf(MSG_DEBUG, "Unsupported group cipher suite"); | |
3189 | return -1; | |
3190 | } | |
3191 | ||
75cad1a0 | 3192 | key_rsc = buf + 5; |
68db9ab0 | 3193 | keyinfo = WPA_GET_LE16(buf + 2); |
75cad1a0 XC |
3194 | gd.gtk_len = keylen; |
3195 | if (gd.gtk_len != buf[4]) { | |
3196 | wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d", | |
3197 | gd.gtk_len, buf[4]); | |
3198 | return -1; | |
3199 | } | |
3200 | gd.keyidx = keyinfo & 0x03; /* B0 - B1 */ | |
3201 | gd.tx = wpa_supplicant_gtk_tx_bit_workaround( | |
3202 | sm, !!(keyinfo & WPA_KEY_INFO_TXRX)); | |
3203 | ||
68db9ab0 | 3204 | os_memcpy(gd.gtk, buf + 13, gd.gtk_len); |
75cad1a0 XC |
3205 | |
3206 | wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)", | |
3207 | gd.gtk, gd.gtk_len); | |
87e2db16 | 3208 | if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) { |
d2c33b91 | 3209 | os_memset(&gd, 0, sizeof(gd)); |
75cad1a0 XC |
3210 | wpa_printf(MSG_DEBUG, "Failed to install the GTK in " |
3211 | "WNM mode"); | |
3212 | return -1; | |
3213 | } | |
d2c33b91 | 3214 | os_memset(&gd, 0, sizeof(gd)); |
75cad1a0 XC |
3215 | #ifdef CONFIG_IEEE80211W |
3216 | } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) { | |
cb5132bb MV |
3217 | const struct wpa_igtk_kde *igtk; |
3218 | ||
3219 | igtk = (const struct wpa_igtk_kde *) (buf + 2); | |
87e2db16 | 3220 | if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0) |
75cad1a0 | 3221 | return -1; |
75cad1a0 XC |
3222 | #endif /* CONFIG_IEEE80211W */ |
3223 | } else { | |
3224 | wpa_printf(MSG_DEBUG, "Unknown element id"); | |
3225 | return -1; | |
3226 | } | |
3227 | ||
3228 | return 0; | |
3229 | } | |
ad3872a3 | 3230 | #endif /* CONFIG_WNM */ |
db76aa64 JM |
3231 | |
3232 | ||
25ef8529 JM |
3233 | #ifdef CONFIG_P2P |
3234 | ||
3235 | int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf) | |
3236 | { | |
3237 | if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0) | |
3238 | return -1; | |
3239 | os_memcpy(buf, sm->p2p_ip_addr, 3 * 4); | |
3240 | return 0; | |
3241 | } | |
3242 | ||
3243 | #endif /* CONFIG_P2P */ | |
b41f2684 CL |
3244 | |
3245 | ||
3246 | void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter) | |
3247 | { | |
3248 | if (rx_replay_counter == NULL) | |
3249 | return; | |
3250 | ||
3251 | os_memcpy(sm->rx_replay_counter, rx_replay_counter, | |
3252 | WPA_REPLAY_COUNTER_LEN); | |
3253 | sm->rx_replay_counter_set = 1; | |
3254 | wpa_printf(MSG_DEBUG, "Updated key replay counter"); | |
3255 | } | |
3256 | ||
3257 | ||
98cd3d1c JM |
3258 | void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm, |
3259 | const u8 *ptk_kck, size_t ptk_kck_len, | |
3260 | const u8 *ptk_kek, size_t ptk_kek_len) | |
b41f2684 | 3261 | { |
98cd3d1c JM |
3262 | if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) { |
3263 | os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len); | |
3264 | sm->ptk.kck_len = ptk_kck_len; | |
b41f2684 CL |
3265 | wpa_printf(MSG_DEBUG, "Updated PTK KCK"); |
3266 | } | |
98cd3d1c JM |
3267 | if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) { |
3268 | os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len); | |
3269 | sm->ptk.kek_len = ptk_kek_len; | |
b41f2684 CL |
3270 | wpa_printf(MSG_DEBUG, "Updated PTK KEK"); |
3271 | } | |
3272 | sm->ptk_set = 1; | |
3273 | } | |
651c6a84 JM |
3274 | |
3275 | ||
3276 | #ifdef CONFIG_TESTING_OPTIONS | |
3277 | void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf) | |
3278 | { | |
3279 | wpabuf_free(sm->test_assoc_ie); | |
3280 | sm->test_assoc_ie = buf; | |
3281 | } | |
3282 | #endif /* CONFIG_TESTING_OPTIONS */ | |
f00b9b88 JM |
3283 | |
3284 | ||
3285 | #ifdef CONFIG_FILS | |
3286 | ||
af3e362f | 3287 | struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md) |
f00b9b88 JM |
3288 | { |
3289 | struct wpabuf *buf = NULL; | |
3290 | struct wpabuf *erp_msg; | |
76e20f4f | 3291 | struct wpabuf *pub = NULL; |
f00b9b88 JM |
3292 | |
3293 | erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol); | |
3294 | if (!erp_msg && !sm->cur_pmksa) { | |
3295 | wpa_printf(MSG_DEBUG, | |
3296 | "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS"); | |
3297 | goto fail; | |
3298 | } | |
3299 | ||
3300 | wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)", | |
3301 | erp_msg != NULL, sm->cur_pmksa != NULL); | |
3302 | ||
706df429 JM |
3303 | sm->fils_completed = 0; |
3304 | ||
f00b9b88 JM |
3305 | if (!sm->assoc_wpa_ie) { |
3306 | wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS"); | |
3307 | goto fail; | |
3308 | } | |
3309 | ||
3310 | if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 || | |
3311 | random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0) | |
3312 | goto fail; | |
3313 | ||
3314 | wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce", | |
3315 | sm->fils_nonce, FILS_NONCE_LEN); | |
3316 | wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session", | |
3317 | sm->fils_session, FILS_SESSION_LEN); | |
3318 | ||
76e20f4f JM |
3319 | #ifdef CONFIG_FILS_SK_PFS |
3320 | sm->fils_dh_group = dh_group; | |
3321 | if (dh_group) { | |
3322 | crypto_ecdh_deinit(sm->fils_ecdh); | |
3323 | sm->fils_ecdh = crypto_ecdh_init(dh_group); | |
3324 | if (!sm->fils_ecdh) { | |
3325 | wpa_printf(MSG_INFO, | |
3326 | "FILS: Could not initialize ECDH with group %d", | |
3327 | dh_group); | |
3328 | goto fail; | |
3329 | } | |
3330 | pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1); | |
3331 | if (!pub) | |
3332 | goto fail; | |
3333 | wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)", | |
3334 | pub); | |
3335 | sm->fils_dh_elem_len = wpabuf_len(pub); | |
3336 | } | |
3337 | #endif /* CONFIG_FILS_SK_PFS */ | |
3338 | ||
3339 | buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len + | |
3340 | (pub ? wpabuf_len(pub) : 0)); | |
f00b9b88 JM |
3341 | if (!buf) |
3342 | goto fail; | |
3343 | ||
3344 | /* Fields following the Authentication algorithm number field */ | |
3345 | ||
3346 | /* Authentication Transaction seq# */ | |
3347 | wpabuf_put_le16(buf, 1); | |
3348 | ||
3349 | /* Status Code */ | |
3350 | wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS); | |
3351 | ||
76e20f4f JM |
3352 | /* TODO: FILS PK */ |
3353 | #ifdef CONFIG_FILS_SK_PFS | |
3354 | if (dh_group) { | |
3355 | /* Finite Cyclic Group */ | |
3356 | wpabuf_put_le16(buf, dh_group); | |
3357 | /* Element */ | |
3358 | wpabuf_put_buf(buf, pub); | |
3359 | } | |
3360 | #endif /* CONFIG_FILS_SK_PFS */ | |
f00b9b88 JM |
3361 | |
3362 | /* RSNE */ | |
3363 | wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame", | |
3364 | sm->assoc_wpa_ie, sm->assoc_wpa_ie_len); | |
3365 | wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len); | |
3366 | ||
af3e362f JM |
3367 | if (md) { |
3368 | /* MDE when using FILS for FT initial association */ | |
3369 | struct rsn_mdie *mdie; | |
3370 | ||
3371 | wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN); | |
3372 | wpabuf_put_u8(buf, sizeof(*mdie)); | |
3373 | mdie = wpabuf_put(buf, sizeof(*mdie)); | |
3374 | os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN); | |
3375 | mdie->ft_capab = 0; | |
3376 | } | |
f00b9b88 JM |
3377 | |
3378 | /* FILS Nonce */ | |
3379 | wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */ | |
3380 | wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */ | |
3381 | /* Element ID Extension */ | |
3382 | wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE); | |
3383 | wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN); | |
3384 | ||
3385 | /* FILS Session */ | |
3386 | wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */ | |
3387 | wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */ | |
3388 | /* Element ID Extension */ | |
3389 | wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION); | |
3390 | wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN); | |
3391 | ||
3392 | /* FILS Wrapped Data */ | |
fcd3d6ce | 3393 | sm->fils_erp_pmkid_set = 0; |
f00b9b88 JM |
3394 | if (erp_msg) { |
3395 | wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */ | |
3396 | wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */ | |
3397 | /* Element ID Extension */ | |
3398 | wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_WRAPPED_DATA); | |
3399 | wpabuf_put_buf(buf, erp_msg); | |
fcd3d6ce JM |
3400 | /* Calculate pending PMKID here so that we do not need to |
3401 | * maintain a copy of the EAP-Initiate/Reauth message. */ | |
3402 | if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg), | |
3403 | wpabuf_len(erp_msg), | |
3404 | sm->fils_erp_pmkid) == 0) | |
3405 | sm->fils_erp_pmkid_set = 1; | |
f00b9b88 JM |
3406 | } |
3407 | ||
3408 | wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame", | |
3409 | buf); | |
3410 | ||
3411 | fail: | |
3412 | wpabuf_free(erp_msg); | |
76e20f4f | 3413 | wpabuf_free(pub); |
f00b9b88 JM |
3414 | return buf; |
3415 | } | |
3416 | ||
a6609937 | 3417 | |
ba9774bd JM |
3418 | int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data, |
3419 | size_t len) | |
a6609937 JM |
3420 | { |
3421 | const u8 *pos, *end; | |
3422 | struct ieee802_11_elems elems; | |
3423 | struct wpa_ie_data rsn; | |
3424 | int pmkid_match = 0; | |
3425 | u8 ick[FILS_ICK_MAX_LEN]; | |
3426 | size_t ick_len; | |
3427 | int res; | |
76e20f4f | 3428 | struct wpabuf *dh_ss = NULL; |
e6b62313 JM |
3429 | const u8 *g_sta = NULL; |
3430 | size_t g_sta_len = 0; | |
3431 | const u8 *g_ap = NULL; | |
3432 | size_t g_ap_len = 0; | |
3433 | struct wpabuf *pub = NULL; | |
a6609937 | 3434 | |
ba9774bd JM |
3435 | os_memcpy(sm->bssid, bssid, ETH_ALEN); |
3436 | ||
a6609937 JM |
3437 | wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields", |
3438 | data, len); | |
3439 | pos = data; | |
3440 | end = data + len; | |
3441 | ||
76e20f4f JM |
3442 | /* TODO: FILS PK */ |
3443 | #ifdef CONFIG_FILS_SK_PFS | |
3444 | if (sm->fils_dh_group) { | |
3445 | u16 group; | |
3446 | ||
3447 | /* Using FILS PFS */ | |
3448 | ||
3449 | /* Finite Cyclic Group */ | |
3450 | if (end - pos < 2) { | |
3451 | wpa_printf(MSG_DEBUG, | |
3452 | "FILS: No room for Finite Cyclic Group"); | |
3453 | goto fail; | |
3454 | } | |
3455 | group = WPA_GET_LE16(pos); | |
3456 | pos += 2; | |
3457 | if (group != sm->fils_dh_group) { | |
3458 | wpa_printf(MSG_DEBUG, | |
3459 | "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)", | |
3460 | group, sm->fils_dh_group); | |
3461 | goto fail; | |
3462 | } | |
3463 | ||
3464 | /* Element */ | |
3465 | if ((size_t) (end - pos) < sm->fils_dh_elem_len) { | |
3466 | wpa_printf(MSG_DEBUG, "FILS: No room for Element"); | |
3467 | goto fail; | |
3468 | } | |
3469 | ||
3470 | if (!sm->fils_ecdh) { | |
3471 | wpa_printf(MSG_DEBUG, "FILS: No ECDH state available"); | |
3472 | goto fail; | |
3473 | } | |
3474 | dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos, | |
3475 | sm->fils_dh_elem_len); | |
3476 | if (!dh_ss) { | |
3477 | wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed"); | |
3478 | goto fail; | |
3479 | } | |
3480 | wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss); | |
e6b62313 JM |
3481 | g_ap = pos; |
3482 | g_ap_len = sm->fils_dh_elem_len; | |
76e20f4f JM |
3483 | pos += sm->fils_dh_elem_len; |
3484 | } | |
3485 | #endif /* CONFIG_FILS_SK_PFS */ | |
a6609937 JM |
3486 | |
3487 | wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos); | |
3488 | if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) { | |
3489 | wpa_printf(MSG_DEBUG, "FILS: Could not parse elements"); | |
76e20f4f | 3490 | goto fail; |
a6609937 JM |
3491 | } |
3492 | ||
3493 | /* RSNE */ | |
3494 | wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie, | |
3495 | elems.rsn_ie_len); | |
3496 | if (!elems.rsn_ie || | |
3497 | wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2, | |
3498 | &rsn) < 0) { | |
3499 | wpa_printf(MSG_DEBUG, "FILS: No RSN element"); | |
76e20f4f | 3500 | goto fail; |
a6609937 JM |
3501 | } |
3502 | ||
3503 | if (!elems.fils_nonce) { | |
3504 | wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field"); | |
76e20f4f | 3505 | goto fail; |
a6609937 JM |
3506 | } |
3507 | os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN); | |
3508 | wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN); | |
3509 | ||
7d440a3b JM |
3510 | if (wpa_key_mgmt_ft(sm->key_mgmt)) { |
3511 | struct wpa_ft_ies parse; | |
3512 | ||
3513 | if (!elems.mdie || !elems.ftie) { | |
3514 | wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE"); | |
3515 | goto fail; | |
3516 | } | |
3517 | ||
3518 | if (wpa_ft_parse_ies(pos, end - pos, &parse) < 0) { | |
3519 | wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs"); | |
3520 | goto fail; | |
3521 | } | |
3522 | ||
3523 | if (!parse.r0kh_id) { | |
3524 | wpa_printf(MSG_DEBUG, | |
3525 | "FILS+FT: No R0KH-ID subelem in FTE"); | |
3526 | goto fail; | |
3527 | } | |
3528 | os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len); | |
3529 | sm->r0kh_id_len = parse.r0kh_id_len; | |
3530 | wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID", | |
3531 | sm->r0kh_id, sm->r0kh_id_len); | |
3532 | ||
3533 | if (!parse.r1kh_id) { | |
3534 | wpa_printf(MSG_DEBUG, | |
3535 | "FILS+FT: No R1KH-ID subelem in FTE"); | |
3536 | goto fail; | |
3537 | } | |
3538 | os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN); | |
3539 | wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID", | |
3540 | sm->r1kh_id, FT_R1KH_ID_LEN); | |
3541 | ||
3542 | /* TODO: Check MDE and FTE payload */ | |
3543 | ||
3544 | wpabuf_free(sm->fils_ft_ies); | |
3545 | sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len + | |
3546 | 2 + elems.ftie_len); | |
3547 | if (!sm->fils_ft_ies) | |
3548 | goto fail; | |
3549 | wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2, | |
3550 | 2 + elems.mdie_len); | |
3551 | wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2, | |
3552 | 2 + elems.ftie_len); | |
3553 | } else { | |
3554 | wpabuf_free(sm->fils_ft_ies); | |
3555 | sm->fils_ft_ies = NULL; | |
3556 | } | |
a6609937 JM |
3557 | |
3558 | /* PMKID List */ | |
3559 | if (rsn.pmkid && rsn.num_pmkid > 0) { | |
3560 | wpa_hexdump(MSG_DEBUG, "FILS: PMKID List", | |
3561 | rsn.pmkid, rsn.num_pmkid * PMKID_LEN); | |
3562 | ||
3563 | if (rsn.num_pmkid != 1) { | |
3564 | wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection"); | |
76e20f4f | 3565 | goto fail; |
a6609937 JM |
3566 | } |
3567 | wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN); | |
3568 | if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0) | |
3569 | { | |
3570 | wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch"); | |
3571 | wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID", | |
3572 | sm->cur_pmksa->pmkid, PMKID_LEN); | |
76e20f4f | 3573 | goto fail; |
a6609937 JM |
3574 | } |
3575 | wpa_printf(MSG_DEBUG, | |
3576 | "FILS: Matching PMKID - continue using PMKSA caching"); | |
3577 | pmkid_match = 1; | |
3578 | } | |
3579 | if (!pmkid_match && sm->cur_pmksa) { | |
3580 | wpa_printf(MSG_DEBUG, | |
3581 | "FILS: No PMKID match - cannot use cached PMKSA entry"); | |
3582 | sm->cur_pmksa = NULL; | |
3583 | } | |
3584 | ||
3585 | /* FILS Session */ | |
3586 | if (!elems.fils_session) { | |
3587 | wpa_printf(MSG_DEBUG, "FILS: No FILS Session element"); | |
76e20f4f | 3588 | goto fail; |
a6609937 JM |
3589 | } |
3590 | wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session, | |
3591 | FILS_SESSION_LEN); | |
3592 | if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN) | |
3593 | != 0) { | |
3594 | wpa_printf(MSG_DEBUG, "FILS: Session mismatch"); | |
3595 | wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session", | |
3596 | sm->fils_session, FILS_SESSION_LEN); | |
76e20f4f | 3597 | goto fail; |
a6609937 JM |
3598 | } |
3599 | ||
3600 | /* FILS Wrapped Data */ | |
3601 | if (!sm->cur_pmksa && elems.fils_wrapped_data) { | |
fcd3d6ce JM |
3602 | u8 rmsk[ERP_MAX_KEY_LEN]; |
3603 | size_t rmsk_len; | |
3604 | ||
a6609937 JM |
3605 | wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data", |
3606 | elems.fils_wrapped_data, | |
3607 | elems.fils_wrapped_data_len); | |
3608 | eapol_sm_process_erp_finish(sm->eapol, elems.fils_wrapped_data, | |
3609 | elems.fils_wrapped_data_len); | |
3610 | if (eapol_sm_failed(sm->eapol)) | |
76e20f4f | 3611 | goto fail; |
a6609937 | 3612 | |
fcd3d6ce JM |
3613 | rmsk_len = ERP_MAX_KEY_LEN; |
3614 | res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len); | |
3615 | if (res == PMK_LEN) { | |
3616 | rmsk_len = PMK_LEN; | |
3617 | res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len); | |
3618 | } | |
a6609937 | 3619 | if (res) |
76e20f4f | 3620 | goto fail; |
a6609937 | 3621 | |
fcd3d6ce | 3622 | res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len, |
76e20f4f JM |
3623 | sm->fils_nonce, sm->fils_anonce, |
3624 | dh_ss ? wpabuf_head(dh_ss) : NULL, | |
3625 | dh_ss ? wpabuf_len(dh_ss) : 0, | |
fcd3d6ce JM |
3626 | sm->pmk, &sm->pmk_len); |
3627 | os_memset(rmsk, 0, sizeof(rmsk)); | |
4cada9dc JM |
3628 | |
3629 | /* Don't use DHss in PTK derivation if PMKSA caching is not | |
3630 | * used. */ | |
76e20f4f JM |
3631 | wpabuf_clear_free(dh_ss); |
3632 | dh_ss = NULL; | |
4cada9dc | 3633 | |
275cc942 | 3634 | if (res) |
76e20f4f | 3635 | goto fail; |
fcd3d6ce JM |
3636 | |
3637 | if (!sm->fils_erp_pmkid_set) { | |
3638 | wpa_printf(MSG_DEBUG, "FILS: PMKID not available"); | |
76e20f4f | 3639 | goto fail; |
fcd3d6ce JM |
3640 | } |
3641 | wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid, | |
3642 | PMKID_LEN); | |
a6609937 | 3643 | wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result"); |
fcd3d6ce JM |
3644 | sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, |
3645 | sm->fils_erp_pmkid, NULL, 0, | |
3646 | sm->bssid, sm->own_addr, | |
869af307 JM |
3647 | sm->network_ctx, sm->key_mgmt, |
3648 | NULL); | |
a6609937 JM |
3649 | } |
3650 | ||
3651 | if (!sm->cur_pmksa) { | |
3652 | wpa_printf(MSG_DEBUG, | |
3653 | "FILS: No remaining options to continue FILS authentication"); | |
76e20f4f | 3654 | goto fail; |
a6609937 JM |
3655 | } |
3656 | ||
3657 | if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid, | |
4cada9dc JM |
3658 | sm->fils_nonce, sm->fils_anonce, |
3659 | dh_ss ? wpabuf_head(dh_ss) : NULL, | |
3660 | dh_ss ? wpabuf_len(dh_ss) : 0, | |
3661 | &sm->ptk, ick, &ick_len, | |
3662 | sm->key_mgmt, sm->pairwise_cipher, | |
7d440a3b | 3663 | sm->fils_ft, &sm->fils_ft_len) < 0) { |
a6609937 | 3664 | wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK"); |
76e20f4f | 3665 | goto fail; |
a6609937 | 3666 | } |
4cada9dc JM |
3667 | |
3668 | wpabuf_clear_free(dh_ss); | |
3669 | dh_ss = NULL; | |
3670 | ||
a6609937 JM |
3671 | sm->ptk_set = 1; |
3672 | sm->tptk_set = 0; | |
3673 | os_memset(&sm->tptk, 0, sizeof(sm->tptk)); | |
3674 | ||
e6b62313 JM |
3675 | #ifdef CONFIG_FILS_SK_PFS |
3676 | if (sm->fils_dh_group) { | |
3677 | if (!sm->fils_ecdh) { | |
3678 | wpa_printf(MSG_INFO, "FILS: ECDH not initialized"); | |
3679 | goto fail; | |
3680 | } | |
3681 | pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1); | |
3682 | if (!pub) | |
3683 | goto fail; | |
3684 | wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub); | |
3685 | g_sta = wpabuf_head(pub); | |
3686 | g_sta_len = wpabuf_len(pub); | |
3687 | if (!g_ap) { | |
3688 | wpa_printf(MSG_INFO, "FILS: gAP not available"); | |
3689 | goto fail; | |
3690 | } | |
3691 | wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len); | |
3692 | } | |
3693 | #endif /* CONFIG_FILS_SK_PFS */ | |
3694 | ||
a6609937 JM |
3695 | res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce, |
3696 | sm->fils_anonce, sm->own_addr, sm->bssid, | |
e6b62313 | 3697 | g_sta, g_sta_len, g_ap, g_ap_len, |
a6609937 JM |
3698 | sm->key_mgmt, sm->fils_key_auth_sta, |
3699 | sm->fils_key_auth_ap, | |
3700 | &sm->fils_key_auth_len); | |
e6b62313 | 3701 | wpabuf_free(pub); |
a6609937 JM |
3702 | os_memset(ick, 0, sizeof(ick)); |
3703 | return res; | |
76e20f4f | 3704 | fail: |
e6b62313 | 3705 | wpabuf_free(pub); |
76e20f4f JM |
3706 | wpabuf_clear_free(dh_ss); |
3707 | return -1; | |
a6609937 JM |
3708 | } |
3709 | ||
86cd6928 | 3710 | |
7d440a3b JM |
3711 | #ifdef CONFIG_IEEE80211R |
3712 | static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf) | |
3713 | { | |
3714 | struct rsn_ie_hdr *rsnie; | |
3715 | u16 capab; | |
3716 | u8 *pos; | |
3717 | ||
3718 | /* RSNIE[PMKR0Name/PMKR1Name] */ | |
3719 | rsnie = wpabuf_put(buf, sizeof(*rsnie)); | |
3720 | rsnie->elem_id = WLAN_EID_RSN; | |
3721 | WPA_PUT_LE16(rsnie->version, RSN_VERSION); | |
3722 | ||
3723 | /* Group Suite Selector */ | |
3724 | if (!wpa_cipher_valid_group(sm->group_cipher)) { | |
3725 | wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)", | |
3726 | sm->group_cipher); | |
3727 | return -1; | |
3728 | } | |
3729 | pos = wpabuf_put(buf, RSN_SELECTOR_LEN); | |
3730 | RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN, | |
3731 | sm->group_cipher)); | |
3732 | ||
3733 | /* Pairwise Suite Count */ | |
3734 | wpabuf_put_le16(buf, 1); | |
3735 | ||
3736 | /* Pairwise Suite List */ | |
3737 | if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) { | |
3738 | wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)", | |
3739 | sm->pairwise_cipher); | |
3740 | return -1; | |
3741 | } | |
3742 | pos = wpabuf_put(buf, RSN_SELECTOR_LEN); | |
3743 | RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN, | |
3744 | sm->pairwise_cipher)); | |
3745 | ||
3746 | /* Authenticated Key Management Suite Count */ | |
3747 | wpabuf_put_le16(buf, 1); | |
3748 | ||
3749 | /* Authenticated Key Management Suite List */ | |
3750 | pos = wpabuf_put(buf, RSN_SELECTOR_LEN); | |
3751 | if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256) | |
3752 | RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256); | |
3753 | else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384) | |
3754 | RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384); | |
3755 | else { | |
3756 | wpa_printf(MSG_WARNING, | |
3757 | "FILS+FT: Invalid key management type (%d)", | |
3758 | sm->key_mgmt); | |
3759 | return -1; | |
3760 | } | |
3761 | ||
3762 | /* RSN Capabilities */ | |
3763 | capab = 0; | |
3764 | #ifdef CONFIG_IEEE80211W | |
3765 | if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) | |
3766 | capab |= WPA_CAPABILITY_MFPC; | |
3767 | #endif /* CONFIG_IEEE80211W */ | |
3768 | wpabuf_put_le16(buf, capab); | |
3769 | ||
3770 | /* PMKID Count */ | |
3771 | wpabuf_put_le16(buf, 1); | |
3772 | ||
3773 | /* PMKID List [PMKR1Name] */ | |
3774 | wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)", | |
3775 | sm->fils_ft, sm->fils_ft_len); | |
3776 | wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len); | |
3777 | wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID", | |
3778 | sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN); | |
3779 | wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID", | |
3780 | sm->r0kh_id, sm->r0kh_id_len); | |
3781 | if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid, | |
3782 | sm->ssid_len, sm->mobility_domain, | |
3783 | sm->r0kh_id, sm->r0kh_id_len, sm->own_addr, | |
3784 | sm->pmk_r0, sm->pmk_r0_name) < 0) { | |
3785 | wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0"); | |
3786 | return -1; | |
3787 | } | |
3788 | wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0", sm->pmk_r0, PMK_LEN); | |
3789 | wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name", | |
3790 | sm->pmk_r0_name, WPA_PMK_NAME_LEN); | |
3791 | wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR, | |
3792 | MAC2STR(sm->r1kh_id)); | |
3793 | pos = wpabuf_put(buf, WPA_PMK_NAME_LEN); | |
3794 | if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr, | |
3795 | pos) < 0) { | |
3796 | wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name"); | |
3797 | return -1; | |
3798 | } | |
3799 | wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", pos, WPA_PMK_NAME_LEN); | |
3800 | ||
3801 | #ifdef CONFIG_IEEE80211W | |
3802 | if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) { | |
3803 | /* Management Group Cipher Suite */ | |
3804 | pos = wpabuf_put(buf, RSN_SELECTOR_LEN); | |
3805 | RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC); | |
3806 | } | |
3807 | #endif /* CONFIG_IEEE80211W */ | |
3808 | ||
3809 | rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2; | |
3810 | return 0; | |
3811 | } | |
3812 | #endif /* CONFIG_IEEE80211R */ | |
3813 | ||
3814 | ||
86cd6928 JM |
3815 | struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek, |
3816 | size_t *kek_len, const u8 **snonce, | |
5732b770 JM |
3817 | const u8 **anonce, |
3818 | const struct wpabuf **hlp, | |
3819 | unsigned int num_hlp) | |
86cd6928 JM |
3820 | { |
3821 | struct wpabuf *buf; | |
5732b770 JM |
3822 | size_t len; |
3823 | unsigned int i; | |
86cd6928 | 3824 | |
5732b770 | 3825 | len = 1000; |
7d440a3b JM |
3826 | #ifdef CONFIG_IEEE80211R |
3827 | if (sm->fils_ft_ies) | |
3828 | len += wpabuf_len(sm->fils_ft_ies); | |
3829 | if (wpa_key_mgmt_ft(sm->key_mgmt)) | |
3830 | len += 256; | |
3831 | #endif /* CONFIG_IEEE80211R */ | |
5732b770 JM |
3832 | for (i = 0; hlp && i < num_hlp; i++) |
3833 | len += 10 + wpabuf_len(hlp[i]); | |
3834 | buf = wpabuf_alloc(len); | |
86cd6928 JM |
3835 | if (!buf) |
3836 | return NULL; | |
3837 | ||
7d440a3b JM |
3838 | #ifdef CONFIG_IEEE80211R |
3839 | if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) { | |
3840 | /* MDE and FTE when using FILS+FT */ | |
3841 | wpabuf_put_buf(buf, sm->fils_ft_ies); | |
3842 | /* RSNE with PMKR1Name in PMKID field */ | |
3843 | if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) { | |
3844 | wpabuf_free(buf); | |
3845 | return NULL; | |
3846 | } | |
3847 | } | |
3848 | #endif /* CONFIG_IEEE80211R */ | |
3849 | ||
86cd6928 JM |
3850 | /* FILS Session */ |
3851 | wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */ | |
3852 | wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */ | |
3853 | /* Element ID Extension */ | |
3854 | wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION); | |
3855 | wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN); | |
3856 | ||
3857 | /* Everything after FILS Session element gets encrypted in the driver | |
3858 | * with KEK. The buffer returned from here is the plaintext version. */ | |
3859 | ||
3860 | /* TODO: FILS Public Key */ | |
3861 | ||
3862 | /* FILS Key Confirm */ | |
3863 | wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */ | |
3864 | wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */ | |
3865 | /* Element ID Extension */ | |
3866 | wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM); | |
3867 | wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len); | |
3868 | ||
5732b770 JM |
3869 | /* FILS HLP Container */ |
3870 | for (i = 0; hlp && i < num_hlp; i++) { | |
3871 | const u8 *pos = wpabuf_head(hlp[i]); | |
3872 | size_t left = wpabuf_len(hlp[i]); | |
3873 | ||
3874 | wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */ | |
3875 | if (left <= 254) | |
3876 | len = 1 + left; | |
3877 | else | |
3878 | len = 255; | |
3879 | wpabuf_put_u8(buf, len); /* Length */ | |
3880 | /* Element ID Extension */ | |
3881 | wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER); | |
3882 | /* Destination MAC Address, Source MAC Address, HLP Packet. | |
3883 | * HLP Packet is in MSDU format (i.e., included the LLC/SNAP | |
3884 | * header when LPD is used). */ | |
3885 | wpabuf_put_data(buf, pos, len - 1); | |
3886 | pos += len - 1; | |
3887 | left -= len - 1; | |
3888 | while (left) { | |
3889 | wpabuf_put_u8(buf, WLAN_EID_FRAGMENT); | |
3890 | len = left > 255 ? 255 : left; | |
3891 | wpabuf_put_u8(buf, len); | |
3892 | wpabuf_put_data(buf, pos, len); | |
3893 | pos += len; | |
3894 | left -= len; | |
3895 | } | |
3896 | } | |
86cd6928 JM |
3897 | |
3898 | /* TODO: FILS IP Address Assignment */ | |
3899 | ||
3900 | wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf); | |
3901 | ||
3902 | *kek = sm->ptk.kek; | |
3903 | *kek_len = sm->ptk.kek_len; | |
3904 | wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len); | |
3905 | *snonce = sm->fils_nonce; | |
3906 | wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD", | |
3907 | *snonce, FILS_NONCE_LEN); | |
3908 | *anonce = sm->fils_anonce; | |
3909 | wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD", | |
3910 | *anonce, FILS_NONCE_LEN); | |
3911 | ||
3912 | return buf; | |
3913 | } | |
3914 | ||
706df429 | 3915 | |
124ddfa1 JM |
3916 | static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len) |
3917 | { | |
3918 | const u8 *pos, *end; | |
3919 | ||
3920 | wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len); | |
3921 | if (len < 2 * ETH_ALEN) | |
3922 | return; | |
3923 | pos = resp + 2 * ETH_ALEN; | |
3924 | end = resp + len; | |
3925 | if (end - pos >= 6 && | |
3926 | os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0) | |
3927 | pos += 6; /* Remove SNAP/LLC header */ | |
3928 | wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos); | |
3929 | } | |
3930 | ||
3931 | ||
3932 | static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos, | |
3933 | size_t len) | |
3934 | { | |
3935 | const u8 *end = pos + len; | |
3936 | u8 *tmp, *tmp_pos; | |
3937 | ||
3938 | /* Check if there are any FILS HLP Container elements */ | |
3939 | while (end - pos >= 2) { | |
3940 | if (2 + pos[1] > end - pos) | |
3941 | return; | |
3942 | if (pos[0] == WLAN_EID_EXTENSION && | |
3943 | pos[1] >= 1 + 2 * ETH_ALEN && | |
3944 | pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER) | |
3945 | break; | |
3946 | pos += 2 + pos[1]; | |
3947 | } | |
3948 | if (end - pos < 2) | |
3949 | return; /* No FILS HLP Container elements */ | |
3950 | ||
3951 | tmp = os_malloc(end - pos); | |
3952 | if (!tmp) | |
3953 | return; | |
3954 | ||
3955 | while (end - pos >= 2) { | |
3956 | if (2 + pos[1] > end - pos || | |
3957 | pos[0] != WLAN_EID_EXTENSION || | |
3958 | pos[1] < 1 + 2 * ETH_ALEN || | |
3959 | pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER) | |
3960 | break; | |
3961 | tmp_pos = tmp; | |
3962 | os_memcpy(tmp_pos, pos + 3, pos[1] - 1); | |
3963 | tmp_pos += pos[1] - 1; | |
3964 | pos += 2 + pos[1]; | |
3965 | ||
3966 | /* Add possible fragments */ | |
3967 | while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT && | |
3968 | 2 + pos[1] <= end - pos) { | |
3969 | os_memcpy(tmp_pos, pos + 2, pos[1]); | |
3970 | tmp_pos += pos[1]; | |
3971 | pos += 2 + pos[1]; | |
3972 | } | |
3973 | ||
3974 | fils_process_hlp_resp(sm, tmp, tmp_pos - tmp); | |
3975 | } | |
3976 | ||
3977 | os_free(tmp); | |
3978 | } | |
3979 | ||
3980 | ||
706df429 JM |
3981 | int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len) |
3982 | { | |
3983 | const struct ieee80211_mgmt *mgmt; | |
3984 | const u8 *end, *ie_start; | |
3985 | struct ieee802_11_elems elems; | |
3986 | int keylen, rsclen; | |
3987 | enum wpa_alg alg; | |
3988 | struct wpa_gtk_data gd; | |
3989 | int maxkeylen; | |
3990 | struct wpa_eapol_ie_parse kde; | |
3991 | ||
3992 | if (!sm || !sm->ptk_set) { | |
3993 | wpa_printf(MSG_DEBUG, "FILS: No KEK available"); | |
3994 | return -1; | |
3995 | } | |
3996 | ||
3997 | if (!wpa_key_mgmt_fils(sm->key_mgmt)) { | |
3998 | wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM"); | |
3999 | return -1; | |
4000 | } | |
4001 | ||
e7608511 JM |
4002 | if (sm->fils_completed) { |
4003 | wpa_printf(MSG_DEBUG, | |
4004 | "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission"); | |
4005 | return -1; | |
4006 | } | |
4007 | ||
706df429 JM |
4008 | wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame", |
4009 | resp, len); | |
4010 | ||
4011 | mgmt = (const struct ieee80211_mgmt *) resp; | |
4012 | if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp)) | |
4013 | return -1; | |
4014 | ||
4015 | end = resp + len; | |
4016 | /* Same offset for Association Response and Reassociation Response */ | |
4017 | ie_start = mgmt->u.assoc_resp.variable; | |
4018 | ||
4019 | if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) == | |
4020 | ParseFailed) { | |
4021 | wpa_printf(MSG_DEBUG, | |
4022 | "FILS: Failed to parse decrypted elements"); | |
4023 | goto fail; | |
4024 | } | |
4025 | ||
4026 | if (!elems.fils_session) { | |
4027 | wpa_printf(MSG_DEBUG, "FILS: No FILS Session element"); | |
4028 | return -1; | |
4029 | } | |
4030 | if (os_memcmp(elems.fils_session, sm->fils_session, | |
4031 | FILS_SESSION_LEN) != 0) { | |
4032 | wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch"); | |
4033 | wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session", | |
4034 | elems.fils_session, FILS_SESSION_LEN); | |
4035 | wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session", | |
4036 | sm->fils_session, FILS_SESSION_LEN); | |
4037 | } | |
4038 | ||
4039 | /* TODO: FILS Public Key */ | |
4040 | ||
4041 | if (!elems.fils_key_confirm) { | |
4042 | wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element"); | |
4043 | goto fail; | |
4044 | } | |
4045 | if (elems.fils_key_confirm_len != sm->fils_key_auth_len) { | |
4046 | wpa_printf(MSG_DEBUG, | |
4047 | "FILS: Unexpected Key-Auth length %d (expected %d)", | |
4048 | elems.fils_key_confirm_len, | |
4049 | (int) sm->fils_key_auth_len); | |
4050 | goto fail; | |
4051 | } | |
4052 | if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap, | |
4053 | sm->fils_key_auth_len) != 0) { | |
4054 | wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch"); | |
4055 | wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth", | |
4056 | elems.fils_key_confirm, | |
4057 | elems.fils_key_confirm_len); | |
4058 | wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth", | |
4059 | sm->fils_key_auth_ap, sm->fils_key_auth_len); | |
4060 | goto fail; | |
4061 | } | |
4062 | ||
4063 | /* Key Delivery */ | |
4064 | if (!elems.key_delivery) { | |
4065 | wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element"); | |
4066 | goto fail; | |
4067 | } | |
4068 | ||
4069 | /* Parse GTK and set the key to the driver */ | |
4070 | os_memset(&gd, 0, sizeof(gd)); | |
4071 | if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN, | |
4072 | elems.key_delivery_len - WPA_KEY_RSC_LEN, | |
4073 | &kde) < 0) { | |
4074 | wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs"); | |
4075 | goto fail; | |
4076 | } | |
4077 | if (!kde.gtk) { | |
4078 | wpa_printf(MSG_DEBUG, "FILS: No GTK KDE"); | |
4079 | goto fail; | |
4080 | } | |
4081 | maxkeylen = gd.gtk_len = kde.gtk_len - 2; | |
4082 | if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher, | |
4083 | gd.gtk_len, maxkeylen, | |
4084 | &gd.key_rsc_len, &gd.alg)) | |
4085 | goto fail; | |
4086 | ||
4087 | wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len); | |
4088 | gd.keyidx = kde.gtk[0] & 0x3; | |
4089 | gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm, | |
4090 | !!(kde.gtk[0] & BIT(2))); | |
4091 | if (kde.gtk_len - 2 > sizeof(gd.gtk)) { | |
4092 | wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)", | |
4093 | (unsigned long) kde.gtk_len - 2); | |
4094 | goto fail; | |
4095 | } | |
4096 | os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2); | |
4097 | ||
4098 | wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver"); | |
87e2db16 | 4099 | if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) { |
706df429 JM |
4100 | wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK"); |
4101 | goto fail; | |
4102 | } | |
4103 | ||
4104 | if (ieee80211w_set_keys(sm, &kde) < 0) { | |
4105 | wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK"); | |
4106 | goto fail; | |
4107 | } | |
4108 | ||
4109 | alg = wpa_cipher_to_alg(sm->pairwise_cipher); | |
4110 | keylen = wpa_cipher_key_len(sm->pairwise_cipher); | |
4111 | rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher); | |
4112 | wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver", | |
4113 | sm->ptk.tk, keylen); | |
4114 | if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen, | |
4115 | sm->ptk.tk, keylen) < 0) { | |
4116 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, | |
4117 | "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid=" | |
4118 | MACSTR ")", | |
4119 | alg, keylen, MAC2STR(sm->bssid)); | |
4120 | goto fail; | |
4121 | } | |
4122 | ||
4123 | /* TODO: TK could be cleared after auth frame exchange now that driver | |
4124 | * takes care of association frame encryption/decryption. */ | |
4125 | /* TK is not needed anymore in supplicant */ | |
4126 | os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN); | |
53bb18cc | 4127 | sm->ptk.installed = 1; |
706df429 | 4128 | |
124ddfa1 JM |
4129 | /* FILS HLP Container */ |
4130 | fils_process_hlp_container(sm, ie_start, end - ie_start); | |
706df429 JM |
4131 | |
4132 | /* TODO: FILS IP Address Assignment */ | |
4133 | ||
4134 | wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully"); | |
4135 | sm->fils_completed = 1; | |
4136 | ||
4137 | return 0; | |
4138 | fail: | |
4139 | return -1; | |
4140 | } | |
4141 | ||
5538fc93 VK |
4142 | |
4143 | void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set) | |
4144 | { | |
4145 | if (sm) | |
4146 | sm->fils_completed = !!set; | |
4147 | } | |
4148 | ||
706df429 JM |
4149 | #endif /* CONFIG_FILS */ |
4150 | ||
4151 | ||
4152 | int wpa_fils_is_completed(struct wpa_sm *sm) | |
4153 | { | |
4154 | #ifdef CONFIG_FILS | |
4155 | return sm && sm->fils_completed; | |
4156 | #else /* CONFIG_FILS */ | |
4157 | return 0; | |
f00b9b88 | 4158 | #endif /* CONFIG_FILS */ |
706df429 | 4159 | } |
0a614799 JM |
4160 | |
4161 | ||
4162 | #ifdef CONFIG_OWE | |
4163 | ||
ec9f4837 | 4164 | struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group) |
0a614799 JM |
4165 | { |
4166 | struct wpabuf *ie = NULL, *pub = NULL; | |
ec9f4837 JM |
4167 | size_t prime_len; |
4168 | ||
4169 | if (group == 19) | |
4170 | prime_len = 32; | |
4171 | else if (group == 20) | |
4172 | prime_len = 48; | |
4173 | else if (group == 21) | |
4174 | prime_len = 66; | |
4175 | else | |
4176 | return NULL; | |
0a614799 JM |
4177 | |
4178 | crypto_ecdh_deinit(sm->owe_ecdh); | |
ec9f4837 | 4179 | sm->owe_ecdh = crypto_ecdh_init(group); |
0a614799 JM |
4180 | if (!sm->owe_ecdh) |
4181 | goto fail; | |
ec9f4837 | 4182 | sm->owe_group = group; |
0a614799 | 4183 | pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0); |
ec9f4837 | 4184 | pub = wpabuf_zeropad(pub, prime_len); |
0a614799 JM |
4185 | if (!pub) |
4186 | goto fail; | |
4187 | ||
4188 | ie = wpabuf_alloc(5 + wpabuf_len(pub)); | |
4189 | if (!ie) | |
4190 | goto fail; | |
4191 | wpabuf_put_u8(ie, WLAN_EID_EXTENSION); | |
4192 | wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub)); | |
4193 | wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM); | |
ec9f4837 | 4194 | wpabuf_put_le16(ie, group); |
0a614799 JM |
4195 | wpabuf_put_buf(ie, pub); |
4196 | wpabuf_free(pub); | |
4197 | wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element", | |
4198 | ie); | |
4199 | ||
4200 | return ie; | |
4201 | fail: | |
4202 | wpabuf_free(pub); | |
4203 | crypto_ecdh_deinit(sm->owe_ecdh); | |
4204 | sm->owe_ecdh = NULL; | |
4205 | return NULL; | |
4206 | } | |
4207 | ||
4208 | ||
5a78c361 JM |
4209 | int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid, |
4210 | const u8 *resp_ies, size_t resp_ies_len) | |
0a614799 JM |
4211 | { |
4212 | struct ieee802_11_elems elems; | |
4213 | u16 group; | |
4214 | struct wpabuf *secret, *pub, *hkey; | |
4215 | int res; | |
ec9f4837 | 4216 | u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN]; |
0a614799 JM |
4217 | const char *info = "OWE Key Generation"; |
4218 | const u8 *addr[2]; | |
4219 | size_t len[2]; | |
ec9f4837 | 4220 | size_t hash_len, prime_len; |
5a78c361 | 4221 | struct wpa_ie_data data; |
0a614799 JM |
4222 | |
4223 | if (!resp_ies || | |
4224 | ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) == | |
5a78c361 JM |
4225 | ParseFailed) { |
4226 | wpa_printf(MSG_INFO, | |
4227 | "OWE: Could not parse Association Response frame elements"); | |
4228 | return -1; | |
4229 | } | |
4230 | ||
4231 | if (sm->cur_pmksa && elems.rsn_ie && | |
4232 | wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len, | |
4233 | &data) == 0 && | |
4234 | data.num_pmkid == 1 && data.pmkid && | |
4235 | os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) { | |
4236 | wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching"); | |
4237 | wpa_sm_set_pmk_from_pmksa(sm); | |
4238 | return 0; | |
4239 | } | |
4240 | ||
4241 | if (!elems.owe_dh) { | |
0a614799 JM |
4242 | wpa_printf(MSG_INFO, |
4243 | "OWE: No Diffie-Hellman Parameter element found in Association Response frame"); | |
4244 | return -1; | |
4245 | } | |
4246 | ||
4247 | group = WPA_GET_LE16(elems.owe_dh); | |
ec9f4837 | 4248 | if (group != sm->owe_group) { |
0a614799 JM |
4249 | wpa_printf(MSG_INFO, |
4250 | "OWE: Unexpected Diffie-Hellman group in response: %u", | |
4251 | group); | |
4252 | return -1; | |
4253 | } | |
4254 | ||
4255 | if (!sm->owe_ecdh) { | |
4256 | wpa_printf(MSG_INFO, "OWE: No ECDH state available"); | |
4257 | return -1; | |
4258 | } | |
4259 | ||
ec9f4837 JM |
4260 | if (group == 19) |
4261 | prime_len = 32; | |
4262 | else if (group == 20) | |
4263 | prime_len = 48; | |
4264 | else if (group == 21) | |
4265 | prime_len = 66; | |
4266 | else | |
4267 | return -1; | |
4268 | ||
0a614799 JM |
4269 | secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0, |
4270 | elems.owe_dh + 2, | |
4271 | elems.owe_dh_len - 2); | |
ec9f4837 | 4272 | secret = wpabuf_zeropad(secret, prime_len); |
0a614799 JM |
4273 | if (!secret) { |
4274 | wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key"); | |
4275 | return -1; | |
4276 | } | |
4277 | wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret); | |
4278 | ||
4279 | /* prk = HKDF-extract(C | A | group, z) */ | |
4280 | ||
4281 | pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0); | |
4282 | if (!pub) { | |
4283 | wpabuf_clear_free(secret); | |
4284 | return -1; | |
4285 | } | |
4286 | ||
4287 | /* PMKID = Truncate-128(Hash(C | A)) */ | |
4288 | addr[0] = wpabuf_head(pub); | |
4289 | len[0] = wpabuf_len(pub); | |
4290 | addr[1] = elems.owe_dh + 2; | |
4291 | len[1] = elems.owe_dh_len - 2; | |
ec9f4837 JM |
4292 | if (group == 19) { |
4293 | res = sha256_vector(2, addr, len, pmkid); | |
4294 | hash_len = SHA256_MAC_LEN; | |
4295 | } else if (group == 20) { | |
4296 | res = sha384_vector(2, addr, len, pmkid); | |
4297 | hash_len = SHA384_MAC_LEN; | |
4298 | } else if (group == 21) { | |
4299 | res = sha512_vector(2, addr, len, pmkid); | |
4300 | hash_len = SHA512_MAC_LEN; | |
4301 | } else { | |
4302 | res = -1; | |
4303 | hash_len = 0; | |
4304 | } | |
4305 | pub = wpabuf_zeropad(pub, prime_len); | |
4306 | if (res < 0 || !pub) { | |
0a614799 JM |
4307 | wpabuf_free(pub); |
4308 | wpabuf_clear_free(secret); | |
4309 | return -1; | |
4310 | } | |
4311 | ||
4312 | hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2); | |
4313 | if (!hkey) { | |
4314 | wpabuf_free(pub); | |
4315 | wpabuf_clear_free(secret); | |
4316 | return -1; | |
4317 | } | |
4318 | ||
4319 | wpabuf_put_buf(hkey, pub); /* C */ | |
4320 | wpabuf_free(pub); | |
4321 | wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */ | |
ec9f4837 JM |
4322 | wpabuf_put_le16(hkey, sm->owe_group); /* group */ |
4323 | if (group == 19) | |
4324 | res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey), | |
4325 | wpabuf_head(secret), wpabuf_len(secret), prk); | |
4326 | else if (group == 20) | |
4327 | res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey), | |
4328 | wpabuf_head(secret), wpabuf_len(secret), prk); | |
4329 | else if (group == 21) | |
4330 | res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey), | |
4331 | wpabuf_head(secret), wpabuf_len(secret), prk); | |
0a614799 JM |
4332 | wpabuf_clear_free(hkey); |
4333 | wpabuf_clear_free(secret); | |
4334 | if (res < 0) | |
4335 | return -1; | |
4336 | ||
ec9f4837 | 4337 | wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len); |
0a614799 JM |
4338 | |
4339 | /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */ | |
4340 | ||
ec9f4837 JM |
4341 | if (group == 19) |
4342 | res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info, | |
4343 | os_strlen(info), sm->pmk, hash_len); | |
4344 | else if (group == 20) | |
4345 | res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info, | |
4346 | os_strlen(info), sm->pmk, hash_len); | |
4347 | else if (group == 21) | |
4348 | res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info, | |
4349 | os_strlen(info), sm->pmk, hash_len); | |
4350 | os_memset(prk, 0, SHA512_MAC_LEN); | |
0a614799 JM |
4351 | if (res < 0) |
4352 | return -1; | |
ec9f4837 | 4353 | sm->pmk_len = hash_len; |
0a614799 | 4354 | |
ec9f4837 | 4355 | wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len); |
0a614799 | 4356 | wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN); |
5a78c361 JM |
4357 | pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0, |
4358 | bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt, | |
4359 | NULL); | |
0a614799 JM |
4360 | |
4361 | return 0; | |
4362 | } | |
4363 | ||
4364 | #endif /* CONFIG_OWE */ | |
bbe7969d VK |
4365 | |
4366 | ||
4367 | void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id) | |
4368 | { | |
4369 | #ifdef CONFIG_FILS | |
4370 | if (sm && fils_cache_id) { | |
4371 | sm->fils_cache_id_set = 1; | |
4372 | os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN); | |
4373 | } | |
4374 | #endif /* CONFIG_FILS */ | |
4375 | } |