]> git.ipfire.org Git - thirdparty/hostap.git/blob - tests/hwsim/test_sae.py
tests: SAE and confirm after invalid commit
[thirdparty/hostap.git] / tests / hwsim / test_sae.py
1 # Test cases for SAE
2 # Copyright (c) 2013-2016, Jouni Malinen <j@w1.fi>
3 #
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
6
7 from remotehost import remote_compatible
8 import binascii
9 import os
10 import time
11 import logging
12 logger = logging.getLogger()
13 import socket
14 import struct
15 import subprocess
16
17 import hwsim_utils
18 import hostapd
19 from wpasupplicant import WpaSupplicant
20 from utils import HwsimSkip, alloc_fail, fail_test, wait_fail_trigger
21 from test_ap_psk import find_wpas_process, read_process_memory, verify_not_present, get_key_locations
22
23 @remote_compatible
24 def test_sae(dev, apdev):
25 """SAE with default group"""
26 if "SAE" not in dev[0].get_capability("auth_alg"):
27 raise HwsimSkip("SAE not supported")
28 params = hostapd.wpa2_params(ssid="test-sae",
29 passphrase="12345678")
30 params['wpa_key_mgmt'] = 'SAE'
31 hapd = hostapd.add_ap(apdev[0], params)
32 key_mgmt = hapd.get_config()['key_mgmt']
33 if key_mgmt.split(' ')[0] != "SAE":
34 raise Exception("Unexpected GET_CONFIG(key_mgmt): " + key_mgmt)
35
36 dev[0].request("SET sae_groups ")
37 id = dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
38 scan_freq="2412")
39 if dev[0].get_status_field('sae_group') != '19':
40 raise Exception("Expected default SAE group not used")
41 bss = dev[0].get_bss(apdev[0]['bssid'])
42 if 'flags' not in bss:
43 raise Exception("Could not get BSS flags from BSS table")
44 if "[WPA2-SAE-CCMP]" not in bss['flags']:
45 raise Exception("Unexpected BSS flags: " + bss['flags'])
46
47 res = hapd.request("STA-FIRST")
48 if "sae_group=19" not in res.splitlines():
49 raise Exception("hostapd STA output did not specify SAE group")
50
51 @remote_compatible
52 def test_sae_password_ecc(dev, apdev):
53 """SAE with number of different passwords (ECC)"""
54 if "SAE" not in dev[0].get_capability("auth_alg"):
55 raise HwsimSkip("SAE not supported")
56 params = hostapd.wpa2_params(ssid="test-sae",
57 passphrase="12345678")
58 params['wpa_key_mgmt'] = 'SAE'
59 hapd = hostapd.add_ap(apdev[0], params)
60
61 dev[0].request("SET sae_groups 19")
62
63 for i in range(10):
64 password = "12345678-" + str(i)
65 hapd.set("wpa_passphrase", password)
66 dev[0].connect("test-sae", psk=password, key_mgmt="SAE",
67 scan_freq="2412")
68 dev[0].request("REMOVE_NETWORK all")
69 dev[0].wait_disconnected()
70
71 @remote_compatible
72 def test_sae_password_ffc(dev, apdev):
73 """SAE with number of different passwords (FFC)"""
74 if "SAE" not in dev[0].get_capability("auth_alg"):
75 raise HwsimSkip("SAE not supported")
76 params = hostapd.wpa2_params(ssid="test-sae",
77 passphrase="12345678")
78 params['wpa_key_mgmt'] = 'SAE'
79 params['sae_groups'] = '15'
80 hapd = hostapd.add_ap(apdev[0], params)
81
82 dev[0].request("SET sae_groups 15")
83
84 for i in range(10):
85 password = "12345678-" + str(i)
86 hapd.set("wpa_passphrase", password)
87 dev[0].connect("test-sae", psk=password, key_mgmt="SAE",
88 scan_freq="2412")
89 dev[0].request("REMOVE_NETWORK all")
90 dev[0].wait_disconnected()
91
92 @remote_compatible
93 def test_sae_pmksa_caching(dev, apdev):
94 """SAE and PMKSA caching"""
95 if "SAE" not in dev[0].get_capability("auth_alg"):
96 raise HwsimSkip("SAE not supported")
97 params = hostapd.wpa2_params(ssid="test-sae",
98 passphrase="12345678")
99 params['wpa_key_mgmt'] = 'SAE'
100 hapd = hostapd.add_ap(apdev[0], params)
101
102 dev[0].request("SET sae_groups ")
103 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
104 scan_freq="2412")
105 ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=5)
106 if ev is None:
107 raise Exception("No connection event received from hostapd")
108 dev[0].request("DISCONNECT")
109 dev[0].wait_disconnected()
110 dev[0].request("RECONNECT")
111 dev[0].wait_connected(timeout=15, error="Reconnect timed out")
112 if dev[0].get_status_field('sae_group') is not None:
113 raise Exception("SAE group claimed to have been used")
114
115 @remote_compatible
116 def test_sae_pmksa_caching_disabled(dev, apdev):
117 """SAE and PMKSA caching disabled"""
118 if "SAE" not in dev[0].get_capability("auth_alg"):
119 raise HwsimSkip("SAE not supported")
120 params = hostapd.wpa2_params(ssid="test-sae",
121 passphrase="12345678")
122 params['wpa_key_mgmt'] = 'SAE'
123 params['disable_pmksa_caching'] = '1'
124 hapd = hostapd.add_ap(apdev[0], params)
125
126 dev[0].request("SET sae_groups ")
127 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
128 scan_freq="2412")
129 ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=5)
130 if ev is None:
131 raise Exception("No connection event received from hostapd")
132 dev[0].request("DISCONNECT")
133 dev[0].wait_disconnected()
134 dev[0].request("RECONNECT")
135 dev[0].wait_connected(timeout=15, error="Reconnect timed out")
136 if dev[0].get_status_field('sae_group') != '19':
137 raise Exception("Expected default SAE group not used")
138
139 def test_sae_groups(dev, apdev):
140 """SAE with all supported groups"""
141 if "SAE" not in dev[0].get_capability("auth_alg"):
142 raise HwsimSkip("SAE not supported")
143 # This is the full list of supported groups, but groups 14-16 (2048-4096 bit
144 # MODP) and group 21 (521-bit random ECP group) are a bit too slow on some
145 # VMs and can result in hitting the mac80211 authentication timeout, so
146 # allow them to fail and just report such failures in the debug log.
147 sae_groups = [19, 25, 26, 20, 21, 1, 2, 5, 14, 15, 16, 22, 23, 24]
148 tls = dev[0].request("GET tls_library")
149 if tls.startswith("OpenSSL") and "run=OpenSSL 1." in tls:
150 logger.info("Add Brainpool EC groups since OpenSSL is new enough")
151 sae_groups += [27, 28, 29, 30]
152 heavy_groups = [14, 15, 16]
153 suitable_groups = [15, 16, 17, 18, 19, 20, 21, 28, 29, 30]
154 groups = [str(g) for g in sae_groups]
155 params = hostapd.wpa2_params(ssid="test-sae-groups",
156 passphrase="12345678")
157 params['wpa_key_mgmt'] = 'SAE'
158 params['sae_groups'] = ' '.join(groups)
159 hostapd.add_ap(apdev[0], params)
160
161 for g in groups:
162 logger.info("Testing SAE group " + g)
163 dev[0].request("SET sae_groups " + g)
164 id = dev[0].connect("test-sae-groups", psk="12345678", key_mgmt="SAE",
165 scan_freq="2412", wait_connect=False)
166 if int(g) in heavy_groups:
167 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=5)
168 if ev is None:
169 logger.info("No connection with heavy SAE group %s did not connect - likely hitting timeout in mac80211" % g)
170 dev[0].remove_network(id)
171 time.sleep(0.1)
172 dev[0].dump_monitor()
173 continue
174 logger.info("Connection with heavy SAE group " + g)
175 else:
176 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
177 if ev is None:
178 if "BoringSSL" in tls and int(g) in [25]:
179 logger.info("Ignore connection failure with group " + g + " with BoringSSL")
180 dev[0].remove_network(id)
181 dev[0].dump_monitor()
182 continue
183 if int(g) not in suitable_groups:
184 logger.info("Ignore connection failure with unsuitable group " + g)
185 dev[0].remove_network(id)
186 dev[0].dump_monitor()
187 continue
188 raise Exception("Connection timed out with group " + g)
189 if dev[0].get_status_field('sae_group') != g:
190 raise Exception("Expected SAE group not used")
191 dev[0].remove_network(id)
192 dev[0].wait_disconnected()
193 dev[0].dump_monitor()
194
195 @remote_compatible
196 def test_sae_group_nego(dev, apdev):
197 """SAE group negotiation"""
198 if "SAE" not in dev[0].get_capability("auth_alg"):
199 raise HwsimSkip("SAE not supported")
200 params = hostapd.wpa2_params(ssid="test-sae-group-nego",
201 passphrase="12345678")
202 params['wpa_key_mgmt'] = 'SAE'
203 params['sae_groups'] = '19'
204 hostapd.add_ap(apdev[0], params)
205
206 dev[0].request("SET sae_groups 25 26 20 19")
207 dev[0].connect("test-sae-group-nego", psk="12345678", key_mgmt="SAE",
208 scan_freq="2412")
209 if dev[0].get_status_field('sae_group') != '19':
210 raise Exception("Expected SAE group not used")
211
212 def test_sae_group_nego_no_match(dev, apdev):
213 """SAE group negotiation (no match)"""
214 if "SAE" not in dev[0].get_capability("auth_alg"):
215 raise HwsimSkip("SAE not supported")
216 params = hostapd.wpa2_params(ssid="test-sae-group-nego",
217 passphrase="12345678")
218 params['wpa_key_mgmt'] = 'SAE'
219 # None-existing SAE group to force all attempts to be rejected
220 params['sae_groups'] = '0'
221 hostapd.add_ap(apdev[0], params)
222
223 dev[0].request("SET sae_groups ")
224 dev[0].connect("test-sae-group-nego", psk="12345678", key_mgmt="SAE",
225 scan_freq="2412", wait_connect=False)
226 ev = dev[0].wait_event(["CTRL-EVENT-SSID-TEMP-DISABLED"], timeout=10)
227 dev[0].request("REMOVE_NETWORK all")
228 if ev is None:
229 raise Exception("Network profile disabling not reported")
230
231 @remote_compatible
232 def test_sae_anti_clogging(dev, apdev):
233 """SAE anti clogging"""
234 if "SAE" not in dev[0].get_capability("auth_alg"):
235 raise HwsimSkip("SAE not supported")
236 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
237 params['wpa_key_mgmt'] = 'SAE'
238 params['sae_anti_clogging_threshold'] = '1'
239 hostapd.add_ap(apdev[0], params)
240
241 dev[0].request("SET sae_groups ")
242 dev[1].request("SET sae_groups ")
243 id = {}
244 for i in range(0, 2):
245 dev[i].scan(freq="2412")
246 id[i] = dev[i].connect("test-sae", psk="12345678", key_mgmt="SAE",
247 scan_freq="2412", only_add_network=True)
248 for i in range(0, 2):
249 dev[i].select_network(id[i])
250 for i in range(0, 2):
251 dev[i].wait_connected(timeout=10)
252
253 def test_sae_forced_anti_clogging(dev, apdev):
254 """SAE anti clogging (forced)"""
255 if "SAE" not in dev[0].get_capability("auth_alg"):
256 raise HwsimSkip("SAE not supported")
257 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
258 params['wpa_key_mgmt'] = 'SAE WPA-PSK'
259 params['sae_anti_clogging_threshold'] = '0'
260 hostapd.add_ap(apdev[0], params)
261 dev[2].connect("test-sae", psk="12345678", scan_freq="2412")
262 for i in range(0, 2):
263 dev[i].request("SET sae_groups ")
264 dev[i].connect("test-sae", psk="12345678", key_mgmt="SAE",
265 scan_freq="2412")
266
267 def test_sae_mixed(dev, apdev):
268 """Mixed SAE and non-SAE network"""
269 if "SAE" not in dev[0].get_capability("auth_alg"):
270 raise HwsimSkip("SAE not supported")
271 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
272 params['wpa_key_mgmt'] = 'SAE WPA-PSK'
273 params['sae_anti_clogging_threshold'] = '0'
274 hostapd.add_ap(apdev[0], params)
275
276 dev[2].connect("test-sae", psk="12345678", scan_freq="2412")
277 for i in range(0, 2):
278 dev[i].request("SET sae_groups ")
279 dev[i].connect("test-sae", psk="12345678", key_mgmt="SAE",
280 scan_freq="2412")
281
282 def test_sae_and_psk(dev, apdev):
283 """SAE and PSK enabled in network profile"""
284 if "SAE" not in dev[0].get_capability("auth_alg"):
285 raise HwsimSkip("SAE not supported")
286 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
287 params['wpa_key_mgmt'] = 'SAE'
288 hostapd.add_ap(apdev[0], params)
289
290 dev[0].request("SET sae_groups ")
291 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE WPA-PSK",
292 scan_freq="2412")
293
294 def test_sae_and_psk2(dev, apdev):
295 """SAE and PSK enabled in network profile (use PSK)"""
296 if "SAE" not in dev[0].get_capability("auth_alg"):
297 raise HwsimSkip("SAE not supported")
298 params = hostapd.wpa2_params(ssid="test-psk", passphrase="12345678")
299 hostapd.add_ap(apdev[0], params)
300
301 dev[0].request("SET sae_groups ")
302 dev[0].connect("test-psk", psk="12345678", key_mgmt="SAE WPA-PSK",
303 scan_freq="2412")
304
305 def test_sae_mixed_mfp(dev, apdev):
306 """Mixed SAE and non-SAE network and MFP required with SAE"""
307 if "SAE" not in dev[0].get_capability("auth_alg"):
308 raise HwsimSkip("SAE not supported")
309 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
310 params['wpa_key_mgmt'] = 'SAE WPA-PSK'
311 params["ieee80211w"] = "1"
312 params['sae_require_mfp'] = '1'
313 hostapd.add_ap(apdev[0], params)
314
315 dev[0].request("SET sae_groups ")
316 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE", ieee80211w="2",
317 scan_freq="2412")
318 dev[0].dump_monitor()
319
320 dev[1].request("SET sae_groups ")
321 dev[1].connect("test-sae", psk="12345678", key_mgmt="SAE", ieee80211w="0",
322 scan_freq="2412", wait_connect=False)
323 ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED",
324 "CTRL-EVENT-ASSOC-REJECT"], timeout=10)
325 if ev is None:
326 raise Exception("No connection result reported")
327 if "CTRL-EVENT-ASSOC-REJECT" not in ev:
328 raise Exception("SAE connection without MFP was not rejected")
329 if "status_code=31" not in ev:
330 raise Exception("Unexpected status code in rejection: " + ev)
331 dev[1].request("DISCONNECT")
332 dev[1].dump_monitor()
333
334 dev[2].connect("test-sae", psk="12345678", ieee80211w="0", scan_freq="2412")
335 dev[2].dump_monitor()
336
337 def test_sae_mfp(dev, apdev):
338 """SAE and MFP enabled without sae_require_mfp"""
339 if "SAE" not in dev[0].get_capability("auth_alg"):
340 raise HwsimSkip("SAE not supported")
341 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
342 params['wpa_key_mgmt'] = 'SAE'
343 params["ieee80211w"] = "1"
344 hostapd.add_ap(apdev[0], params)
345
346 dev[0].request("SET sae_groups ")
347 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE", ieee80211w="2",
348 scan_freq="2412")
349
350 dev[1].request("SET sae_groups ")
351 dev[1].connect("test-sae", psk="12345678", key_mgmt="SAE", ieee80211w="0",
352 scan_freq="2412")
353
354 @remote_compatible
355 def test_sae_missing_password(dev, apdev):
356 """SAE and missing password"""
357 if "SAE" not in dev[0].get_capability("auth_alg"):
358 raise HwsimSkip("SAE not supported")
359 params = hostapd.wpa2_params(ssid="test-sae",
360 passphrase="12345678")
361 params['wpa_key_mgmt'] = 'SAE'
362 hapd = hostapd.add_ap(apdev[0], params)
363
364 dev[0].request("SET sae_groups ")
365 id = dev[0].connect("test-sae",
366 raw_psk="46b4a73b8a951ad53ebd2e0afdb9c5483257edd4c21d12b7710759da70945858",
367 key_mgmt="SAE", scan_freq="2412", wait_connect=False)
368 ev = dev[0].wait_event(['CTRL-EVENT-SSID-TEMP-DISABLED'], timeout=10)
369 if ev is None:
370 raise Exception("Invalid network not temporarily disabled")
371
372
373 def test_sae_key_lifetime_in_memory(dev, apdev, params):
374 """SAE and key lifetime in memory"""
375 if "SAE" not in dev[0].get_capability("auth_alg"):
376 raise HwsimSkip("SAE not supported")
377 password = "5ad144a7c1f5a5503baa6fa01dabc15b1843e8c01662d78d16b70b5cd23cf8b"
378 p = hostapd.wpa2_params(ssid="test-sae", passphrase=password)
379 p['wpa_key_mgmt'] = 'SAE'
380 hapd = hostapd.add_ap(apdev[0], p)
381
382 pid = find_wpas_process(dev[0])
383
384 dev[0].request("SET sae_groups ")
385 id = dev[0].connect("test-sae", psk=password, key_mgmt="SAE",
386 scan_freq="2412")
387
388 # The decrypted copy of GTK is freed only after the CTRL-EVENT-CONNECTED
389 # event has been delivered, so verify that wpa_supplicant has returned to
390 # eloop before reading process memory.
391 time.sleep(1)
392 dev[0].ping()
393 password = password.encode()
394 buf = read_process_memory(pid, password)
395
396 dev[0].request("DISCONNECT")
397 dev[0].wait_disconnected()
398
399 dev[0].relog()
400 sae_k = None
401 sae_keyseed = None
402 sae_kck = None
403 pmk = None
404 ptk = None
405 gtk = None
406 with open(os.path.join(params['logdir'], 'log0'), 'r') as f:
407 for l in f.readlines():
408 if "SAE: k - hexdump" in l:
409 val = l.strip().split(':')[3].replace(' ', '')
410 sae_k = binascii.unhexlify(val)
411 if "SAE: keyseed - hexdump" in l:
412 val = l.strip().split(':')[3].replace(' ', '')
413 sae_keyseed = binascii.unhexlify(val)
414 if "SAE: KCK - hexdump" in l:
415 val = l.strip().split(':')[3].replace(' ', '')
416 sae_kck = binascii.unhexlify(val)
417 if "SAE: PMK - hexdump" in l:
418 val = l.strip().split(':')[3].replace(' ', '')
419 pmk = binascii.unhexlify(val)
420 if "WPA: PTK - hexdump" in l:
421 val = l.strip().split(':')[3].replace(' ', '')
422 ptk = binascii.unhexlify(val)
423 if "WPA: Group Key - hexdump" in l:
424 val = l.strip().split(':')[3].replace(' ', '')
425 gtk = binascii.unhexlify(val)
426 if not sae_k or not sae_keyseed or not sae_kck or not pmk or not ptk or not gtk:
427 raise Exception("Could not find keys from debug log")
428 if len(gtk) != 16:
429 raise Exception("Unexpected GTK length")
430
431 kck = ptk[0:16]
432 kek = ptk[16:32]
433 tk = ptk[32:48]
434
435 fname = os.path.join(params['logdir'],
436 'sae_key_lifetime_in_memory.memctx-')
437
438 logger.info("Checking keys in memory while associated")
439 get_key_locations(buf, password, "Password")
440 get_key_locations(buf, pmk, "PMK")
441 if password not in buf:
442 raise HwsimSkip("Password not found while associated")
443 if pmk not in buf:
444 raise HwsimSkip("PMK not found while associated")
445 if kck not in buf:
446 raise Exception("KCK not found while associated")
447 if kek not in buf:
448 raise Exception("KEK not found while associated")
449 #if tk in buf:
450 # raise Exception("TK found from memory")
451 verify_not_present(buf, sae_k, fname, "SAE(k)")
452 verify_not_present(buf, sae_keyseed, fname, "SAE(keyseed)")
453 verify_not_present(buf, sae_kck, fname, "SAE(KCK)")
454
455 logger.info("Checking keys in memory after disassociation")
456 buf = read_process_memory(pid, password)
457
458 # Note: Password is still present in network configuration
459 # Note: PMK is in PMKSA cache
460
461 get_key_locations(buf, password, "Password")
462 get_key_locations(buf, pmk, "PMK")
463 verify_not_present(buf, kck, fname, "KCK")
464 verify_not_present(buf, kek, fname, "KEK")
465 verify_not_present(buf, tk, fname, "TK")
466 if gtk in buf:
467 get_key_locations(buf, gtk, "GTK")
468 verify_not_present(buf, gtk, fname, "GTK")
469 verify_not_present(buf, sae_k, fname, "SAE(k)")
470 verify_not_present(buf, sae_keyseed, fname, "SAE(keyseed)")
471 verify_not_present(buf, sae_kck, fname, "SAE(KCK)")
472
473 dev[0].request("PMKSA_FLUSH")
474 logger.info("Checking keys in memory after PMKSA cache flush")
475 buf = read_process_memory(pid, password)
476 get_key_locations(buf, password, "Password")
477 get_key_locations(buf, pmk, "PMK")
478 verify_not_present(buf, pmk, fname, "PMK")
479
480 dev[0].request("REMOVE_NETWORK all")
481
482 logger.info("Checking keys in memory after network profile removal")
483 buf = read_process_memory(pid, password)
484
485 get_key_locations(buf, password, "Password")
486 get_key_locations(buf, pmk, "PMK")
487 verify_not_present(buf, password, fname, "password")
488 verify_not_present(buf, pmk, fname, "PMK")
489 verify_not_present(buf, kck, fname, "KCK")
490 verify_not_present(buf, kek, fname, "KEK")
491 verify_not_present(buf, tk, fname, "TK")
492 verify_not_present(buf, gtk, fname, "GTK")
493 verify_not_present(buf, sae_k, fname, "SAE(k)")
494 verify_not_present(buf, sae_keyseed, fname, "SAE(keyseed)")
495 verify_not_present(buf, sae_kck, fname, "SAE(KCK)")
496
497 @remote_compatible
498 def test_sae_oom_wpas(dev, apdev):
499 """SAE and OOM in wpa_supplicant"""
500 if "SAE" not in dev[0].get_capability("auth_alg"):
501 raise HwsimSkip("SAE not supported")
502 params = hostapd.wpa2_params(ssid="test-sae",
503 passphrase="12345678")
504 params['wpa_key_mgmt'] = 'SAE'
505 params['sae_groups'] = '19 25 26 20'
506 hapd = hostapd.add_ap(apdev[0], params)
507
508 dev[0].request("SET sae_groups 20")
509 with alloc_fail(dev[0], 1, "sae_set_group"):
510 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
511 scan_freq="2412")
512 dev[0].request("REMOVE_NETWORK all")
513
514 dev[0].request("SET sae_groups ")
515 with alloc_fail(dev[0], 2, "sae_set_group"):
516 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
517 scan_freq="2412")
518 dev[0].request("REMOVE_NETWORK all")
519
520 with alloc_fail(dev[0], 1, "wpabuf_alloc;sme_auth_build_sae_commit"):
521 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
522 scan_freq="2412")
523 dev[0].request("REMOVE_NETWORK all")
524
525 with alloc_fail(dev[0], 1, "wpabuf_alloc;sme_auth_build_sae_confirm"):
526 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
527 scan_freq="2412", wait_connect=False)
528 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
529 dev[0].request("REMOVE_NETWORK all")
530
531 with alloc_fail(dev[0], 1, "=sme_authenticate"):
532 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
533 scan_freq="2412", wait_connect=False)
534 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
535 dev[0].request("REMOVE_NETWORK all")
536
537 with alloc_fail(dev[0], 1, "radio_add_work;sme_authenticate"):
538 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
539 scan_freq="2412", wait_connect=False)
540 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
541 dev[0].request("REMOVE_NETWORK all")
542
543 @remote_compatible
544 def test_sae_proto_ecc(dev, apdev):
545 """SAE protocol testing (ECC)"""
546 if "SAE" not in dev[0].get_capability("auth_alg"):
547 raise HwsimSkip("SAE not supported")
548 params = hostapd.wpa2_params(ssid="test-sae",
549 passphrase="12345678")
550 params['wpa_key_mgmt'] = 'SAE'
551 hapd = hostapd.add_ap(apdev[0], params)
552 bssid = apdev[0]['bssid']
553
554 dev[0].request("SET sae_groups 19")
555
556 tests = [("Confirm mismatch",
557 "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
558 "0000800edebc3f260dc1fe7e0b20888af2b8a3316252ec37388a8504e25b73dc4240"),
559 ("Commit without even full cyclic group field",
560 "13",
561 None),
562 ("Too short commit",
563 "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02",
564 None),
565 ("Invalid commit scalar (0)",
566 "1300" + "0000000000000000000000000000000000000000000000000000000000000000" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
567 None),
568 ("Invalid commit scalar (1)",
569 "1300" + "0000000000000000000000000000000000000000000000000000000000000001" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
570 None),
571 ("Invalid commit scalar (> r)",
572 "1300" + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
573 None),
574 ("Commit element not on curve",
575 "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728d0000000000000000000000000000000000000000000000000000000000000000",
576 None),
577 ("Invalid commit element (y coordinate > P)",
578 "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
579 None),
580 ("Invalid commit element (x coordinate > P)",
581 "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
582 None),
583 ("Different group in commit",
584 "1400" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
585 None),
586 ("Too short confirm",
587 "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
588 "0000800edebc3f260dc1fe7e0b20888af2b8a3316252ec37388a8504e25b73dc42")]
589 for (note, commit, confirm) in tests:
590 logger.info(note)
591 dev[0].scan_for_bss(bssid, freq=2412)
592 hapd.set("ext_mgmt_frame_handling", "1")
593 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
594 scan_freq="2412", wait_connect=False)
595
596 logger.info("Commit")
597 for i in range(0, 10):
598 req = hapd.mgmt_rx()
599 if req is None:
600 raise Exception("MGMT RX wait timed out (commit)")
601 if req['subtype'] == 11:
602 break
603 req = None
604 if not req:
605 raise Exception("Authentication frame (commit) not received")
606
607 hapd.dump_monitor()
608 resp = {}
609 resp['fc'] = req['fc']
610 resp['da'] = req['sa']
611 resp['sa'] = req['da']
612 resp['bssid'] = req['bssid']
613 resp['payload'] = binascii.unhexlify("030001000000" + commit)
614 hapd.mgmt_tx(resp)
615
616 if confirm:
617 logger.info("Confirm")
618 for i in range(0, 10):
619 req = hapd.mgmt_rx()
620 if req is None:
621 raise Exception("MGMT RX wait timed out (confirm)")
622 if req['subtype'] == 11:
623 break
624 req = None
625 if not req:
626 raise Exception("Authentication frame (confirm) not received")
627
628 hapd.dump_monitor()
629 resp = {}
630 resp['fc'] = req['fc']
631 resp['da'] = req['sa']
632 resp['sa'] = req['da']
633 resp['bssid'] = req['bssid']
634 resp['payload'] = binascii.unhexlify("030002000000" + confirm)
635 hapd.mgmt_tx(resp)
636
637 time.sleep(0.1)
638 dev[0].request("REMOVE_NETWORK all")
639 hapd.set("ext_mgmt_frame_handling", "0")
640 hapd.dump_monitor()
641
642 @remote_compatible
643 def test_sae_proto_ffc(dev, apdev):
644 """SAE protocol testing (FFC)"""
645 if "SAE" not in dev[0].get_capability("auth_alg"):
646 raise HwsimSkip("SAE not supported")
647 params = hostapd.wpa2_params(ssid="test-sae",
648 passphrase="12345678")
649 params['wpa_key_mgmt'] = 'SAE'
650 hapd = hostapd.add_ap(apdev[0], params)
651 bssid = apdev[0]['bssid']
652
653 dev[0].request("SET sae_groups 2")
654
655 tests = [("Confirm mismatch",
656 "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "a8c00117493cdffa5dd671e934bc9cb1a69f39e25e9dd9cd9afd3aea2441a0f5491211c7ba50a753563f9ce943b043557cb71193b28e86ed9544f4289c471bf91b70af5c018cf4663e004165b0fd0bc1d8f3f78adf42eee92bcbc55246fd3ee9f107ab965dc7d4986f23eb71d616ebfe6bfe0a6c1ac5dc1718acee17c9a17486",
657 "0000f3116a9731f1259622e3eb55d4b3b50ba16f8c5f5565b28e609b180c51460251"),
658 ("Too short commit",
659 "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "a8c00117493cdffa5dd671e934bc9cb1a69f39e25e9dd9cd9afd3aea2441a0f5491211c7ba50a753563f9ce943b043557cb71193b28e86ed9544f4289c471bf91b70af5c018cf4663e004165b0fd0bc1d8f3f78adf42eee92bcbc55246fd3ee9f107ab965dc7d4986f23eb71d616ebfe6bfe0a6c1ac5dc1718acee17c9a174",
660 None),
661 ("Invalid element (0) in commit",
662 "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
663 None),
664 ("Invalid element (1) in commit",
665 "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
666 None),
667 ("Invalid element (> P) in commit",
668 "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
669 None)]
670 for (note, commit, confirm) in tests:
671 logger.info(note)
672 dev[0].scan_for_bss(bssid, freq=2412)
673 hapd.set("ext_mgmt_frame_handling", "1")
674 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
675 scan_freq="2412", wait_connect=False)
676
677 logger.info("Commit")
678 for i in range(0, 10):
679 req = hapd.mgmt_rx()
680 if req is None:
681 raise Exception("MGMT RX wait timed out (commit)")
682 if req['subtype'] == 11:
683 break
684 req = None
685 if not req:
686 raise Exception("Authentication frame (commit) not received")
687
688 hapd.dump_monitor()
689 resp = {}
690 resp['fc'] = req['fc']
691 resp['da'] = req['sa']
692 resp['sa'] = req['da']
693 resp['bssid'] = req['bssid']
694 resp['payload'] = binascii.unhexlify("030001000000" + commit)
695 hapd.mgmt_tx(resp)
696
697 if confirm:
698 logger.info("Confirm")
699 for i in range(0, 10):
700 req = hapd.mgmt_rx()
701 if req is None:
702 raise Exception("MGMT RX wait timed out (confirm)")
703 if req['subtype'] == 11:
704 break
705 req = None
706 if not req:
707 raise Exception("Authentication frame (confirm) not received")
708
709 hapd.dump_monitor()
710 resp = {}
711 resp['fc'] = req['fc']
712 resp['da'] = req['sa']
713 resp['sa'] = req['da']
714 resp['bssid'] = req['bssid']
715 resp['payload'] = binascii.unhexlify("030002000000" + confirm)
716 hapd.mgmt_tx(resp)
717
718 time.sleep(0.1)
719 dev[0].request("REMOVE_NETWORK all")
720 hapd.set("ext_mgmt_frame_handling", "0")
721 hapd.dump_monitor()
722
723 def test_sae_proto_confirm_replay(dev, apdev):
724 """SAE protocol testing - Confirm replay"""
725 if "SAE" not in dev[0].get_capability("auth_alg"):
726 raise HwsimSkip("SAE not supported")
727 params = hostapd.wpa2_params(ssid="test-sae",
728 passphrase="12345678")
729 params['wpa_key_mgmt'] = 'SAE'
730 hapd = hostapd.add_ap(apdev[0], params)
731 bssid = apdev[0]['bssid']
732
733 dev[0].request("SET sae_groups 19")
734
735 dev[0].scan_for_bss(bssid, freq=2412)
736 hapd.set("ext_mgmt_frame_handling", "1")
737 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
738 scan_freq="2412", wait_connect=False)
739
740 logger.info("Commit")
741 for i in range(0, 10):
742 req = hapd.mgmt_rx()
743 if req is None:
744 raise Exception("MGMT RX wait timed out (commit)")
745 if req['subtype'] == 11:
746 break
747 req = None
748 if not req:
749 raise Exception("Authentication frame (commit) not received")
750
751 bssid = hapd.own_addr().replace(':', '')
752 addr = dev[0].own_addr().replace(':', '')
753 hdr = "b0003a01" + bssid + addr + bssid + "1000"
754
755 hapd.dump_monitor()
756 hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=" + binascii.hexlify(req['frame']).decode())
757
758 logger.info("Confirm")
759 for i in range(0, 10):
760 req = hapd.mgmt_rx()
761 if req is None:
762 raise Exception("MGMT RX wait timed out (confirm)")
763 if req['subtype'] == 11:
764 break
765 req = None
766 if not req:
767 raise Exception("Authentication frame (confirm) not received")
768
769 hapd.dump_monitor()
770 hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=" + binascii.hexlify(req['frame']).decode())
771
772 logger.info("Replay Confirm")
773 hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=" + binascii.hexlify(req['frame']).decode())
774
775 logger.info("Association Request")
776 for i in range(0, 10):
777 req = hapd.mgmt_rx()
778 if req is None:
779 raise Exception("MGMT RX wait timed out (AssocReq)")
780 if req['subtype'] == 0:
781 break
782 req = None
783 if not req:
784 raise Exception("Association Request frame not received")
785
786 hapd.dump_monitor()
787 hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=" + binascii.hexlify(req['frame']).decode())
788 ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
789 if ev is None:
790 raise Exception("Management frame TX status not reported (1)")
791 if "stype=1 ok=1" not in ev:
792 raise Exception("Unexpected management frame TX status (1): " + ev)
793 cmd = "MGMT_TX_STATUS_PROCESS %s" % (" ".join(ev.split(' ')[1:4]))
794 if "OK" not in hapd.request(cmd):
795 raise Exception("MGMT_TX_STATUS_PROCESS failed")
796
797 hapd.set("ext_mgmt_frame_handling", "0")
798
799 dev[0].wait_connected()
800
801 def test_sae_proto_hostapd(dev, apdev):
802 """SAE protocol testing with hostapd"""
803 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
804 params['wpa_key_mgmt'] = 'SAE'
805 params['sae_groups'] = "19 65535"
806 hapd = hostapd.add_ap(apdev[0], params)
807 hapd.set("ext_mgmt_frame_handling", "1")
808 bssid = hapd.own_addr().replace(':', '')
809 addr = "020000000000"
810 addr2 = "020000000001"
811 hdr = "b0003a01" + bssid + addr + bssid + "1000"
812 hdr2 = "b0003a01" + bssid + addr2 + bssid + "1000"
813 group = "1300"
814 scalar = "f7df19f4a7fef1d3b895ea1de150b7c5a7a705c8ebb31a52b623e0057908bd93"
815 element_x = "21931572027f2e953e2a49fab3d992944102cc95aa19515fc068b394fb25ae3c"
816 element_y = "cb4eeb94d7b0b789abfdb73a67ab9d6d5efa94dd553e0e724a6289821cbce530"
817 hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=" + hdr + "030001000000" + group + scalar + element_x + element_y)
818 # "SAE: Not enough data for scalar"
819 hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=" + hdr + "030001000000" + group + scalar[:-2])
820 # "SAE: Do not allow group to be changed"
821 hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=" + hdr + "030001000000" + "ffff" + scalar[:-2])
822 # "SAE: Unsupported Finite Cyclic Group 65535"
823 hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=" + hdr2 + "030001000000" + "ffff" + scalar[:-2])
824
825 def test_sae_proto_hostapd_ecc(dev, apdev):
826 """SAE protocol testing with hostapd (ECC)"""
827 params = hostapd.wpa2_params(ssid="test-sae", passphrase="foofoofoo")
828 params['wpa_key_mgmt'] = 'SAE'
829 params['sae_groups'] = "19"
830 hapd = hostapd.add_ap(apdev[0], params)
831 hapd.set("ext_mgmt_frame_handling", "1")
832 bssid = hapd.own_addr().replace(':', '')
833 addr = "020000000000"
834 addr2 = "020000000001"
835 hdr = "b0003a01" + bssid + addr + bssid + "1000"
836 hdr2 = "b0003a01" + bssid + addr2 + bssid + "1000"
837 group = "1300"
838 scalar = "9e9a959bf2dda875a4a29ce9b2afef46f2d83060930124cd9e39ddce798cd69a"
839 element_x = "dfc55fd8622b91d362f4d1fc9646474d7fba0ff7cce6ca58b8e96a931e070220"
840 element_y = "dac8a4e80724f167c1349cc9e1f9dd82a7c77b29d49789b63b72b4c849301a28"
841 # sae_parse_commit_element_ecc() failure to parse peer element
842 # (depending on crypto library, either crypto_ec_point_from_bin() failure
843 # or crypto_ec_point_is_on_curve() returning 0)
844 hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=" + hdr + "030001000000" + group + scalar + element_x + element_y)
845 # Unexpected continuation of the connection attempt with confirm
846 hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=" + hdr + "030002000000" + "0000" + "fd7b081ff4e8676f03612a4140eedcd3c179ab3a13b93863c6f7ca451340b9ae")
847
848 def test_sae_proto_hostapd_ffc(dev, apdev):
849 """SAE protocol testing with hostapd (FFC)"""
850 params = hostapd.wpa2_params(ssid="test-sae", passphrase="foofoofoo")
851 params['wpa_key_mgmt'] = 'SAE'
852 params['sae_groups'] = "22"
853 hapd = hostapd.add_ap(apdev[0], params)
854 hapd.set("ext_mgmt_frame_handling", "1")
855 bssid = hapd.own_addr().replace(':', '')
856 addr = "020000000000"
857 addr2 = "020000000001"
858 hdr = "b0003a01" + bssid + addr + bssid + "1000"
859 hdr2 = "b0003a01" + bssid + addr2 + bssid + "1000"
860 group = "1600"
861 scalar = "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044cc46a73c07ef479dc66ec1f5e8ccf25131fa40"
862 element = "0f1d67025e12fc874cf718c35b19d1ab2db858215623f1ce661cbd1d7b1d7a09ceda7dba46866cf37044259b5cac4db15e7feb778edc8098854b93a84347c1850c02ee4d7dac46db79c477c731085d5b39f56803cda1eeac4a2fbbccb9a546379e258c00ebe93dfdd0a34cf8ce5c55cf905a89564a590b7e159fb89198e9d5cd"
863 # sae_parse_commit_element_ffc() failure to parse peer element
864 hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=" + hdr + "030001000000" + group + scalar + element)
865 # Unexpected continuation of the connection attempt with confirm
866 hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=" + hdr + "030002000000" + "0000" + "fd7b081ff4e8676f03612a4140eedcd3c179ab3a13b93863c6f7ca451340b9ae")
867
868 @remote_compatible
869 def test_sae_no_ffc_by_default(dev, apdev):
870 """SAE and default groups rejecting FFC"""
871 if "SAE" not in dev[0].get_capability("auth_alg"):
872 raise HwsimSkip("SAE not supported")
873 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
874 params['wpa_key_mgmt'] = 'SAE'
875 hapd = hostapd.add_ap(apdev[0], params)
876
877 dev[0].request("SET sae_groups 15")
878 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE", scan_freq="2412",
879 wait_connect=False)
880 ev = dev[0].wait_event(["SME: Trying to authenticate"], timeout=3)
881 if ev is None:
882 raise Exception("Did not try to authenticate")
883 ev = dev[0].wait_event(["SME: Trying to authenticate"], timeout=3)
884 if ev is None:
885 raise Exception("Did not try to authenticate (2)")
886 dev[0].request("REMOVE_NETWORK all")
887
888 def sae_reflection_attack(apdev, dev, group):
889 if "SAE" not in dev.get_capability("auth_alg"):
890 raise HwsimSkip("SAE not supported")
891 params = hostapd.wpa2_params(ssid="test-sae",
892 passphrase="no-knowledge-of-passphrase")
893 params['wpa_key_mgmt'] = 'SAE'
894 hapd = hostapd.add_ap(apdev, params)
895 bssid = apdev['bssid']
896
897 dev.scan_for_bss(bssid, freq=2412)
898 hapd.set("ext_mgmt_frame_handling", "1")
899
900 dev.request("SET sae_groups %d" % group)
901 dev.connect("test-sae", psk="reflection-attack", key_mgmt="SAE",
902 scan_freq="2412", wait_connect=False)
903
904 # Commit
905 for i in range(0, 10):
906 req = hapd.mgmt_rx()
907 if req is None:
908 raise Exception("MGMT RX wait timed out")
909 if req['subtype'] == 11:
910 break
911 req = None
912 if not req:
913 raise Exception("Authentication frame not received")
914
915 resp = {}
916 resp['fc'] = req['fc']
917 resp['da'] = req['sa']
918 resp['sa'] = req['da']
919 resp['bssid'] = req['bssid']
920 resp['payload'] = req['payload']
921 hapd.mgmt_tx(resp)
922
923 # Confirm
924 req = hapd.mgmt_rx(timeout=0.5)
925 if req is not None:
926 if req['subtype'] == 11:
927 raise Exception("Unexpected Authentication frame seen")
928
929 @remote_compatible
930 def test_sae_reflection_attack_ecc(dev, apdev):
931 """SAE reflection attack (ECC)"""
932 sae_reflection_attack(apdev[0], dev[0], 19)
933
934 @remote_compatible
935 def test_sae_reflection_attack_ffc(dev, apdev):
936 """SAE reflection attack (FFC)"""
937 sae_reflection_attack(apdev[0], dev[0], 15)
938
939 def sae_reflection_attack_internal(apdev, dev, group):
940 if "SAE" not in dev.get_capability("auth_alg"):
941 raise HwsimSkip("SAE not supported")
942 params = hostapd.wpa2_params(ssid="test-sae",
943 passphrase="no-knowledge-of-passphrase")
944 params['wpa_key_mgmt'] = 'SAE'
945 params['sae_reflection_attack'] = '1'
946 hapd = hostapd.add_ap(apdev, params)
947 bssid = apdev['bssid']
948
949 dev.scan_for_bss(bssid, freq=2412)
950 dev.request("SET sae_groups %d" % group)
951 dev.connect("test-sae", psk="reflection-attack", key_mgmt="SAE",
952 scan_freq="2412", wait_connect=False)
953 ev = dev.wait_event(["SME: Trying to authenticate"], timeout=10)
954 if ev is None:
955 raise Exception("No authentication attempt seen")
956 ev = dev.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
957 if ev is not None:
958 raise Exception("Unexpected connection")
959
960 @remote_compatible
961 def test_sae_reflection_attack_ecc_internal(dev, apdev):
962 """SAE reflection attack (ECC) - internal"""
963 sae_reflection_attack_internal(apdev[0], dev[0], 19)
964
965 @remote_compatible
966 def test_sae_reflection_attack_ffc_internal(dev, apdev):
967 """SAE reflection attack (FFC) - internal"""
968 sae_reflection_attack_internal(apdev[0], dev[0], 15)
969
970 @remote_compatible
971 def test_sae_commit_override(dev, apdev):
972 """SAE commit override (hostapd)"""
973 if "SAE" not in dev[0].get_capability("auth_alg"):
974 raise HwsimSkip("SAE not supported")
975 params = hostapd.wpa2_params(ssid="test-sae",
976 passphrase="12345678")
977 params['wpa_key_mgmt'] = 'SAE'
978 params['sae_commit_override'] = '13ffbad00d215867a7c5ff37d87bb9bdb7cb116e520f71e8d7a794ca2606d537ddc6c099c40e7a25372b80a8fd443cd7dd222c8ea21b8ef372d4b3e316c26a73fd999cc79ad483eb826e7b3893ea332da68fa13224bcdeb4fb18b0584dd100a2c514'
979 hapd = hostapd.add_ap(apdev[0], params)
980 dev[0].request("SET sae_groups ")
981 dev[0].connect("test-sae", psk="test-sae", key_mgmt="SAE",
982 scan_freq="2412", wait_connect=False)
983 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
984 if ev is not None:
985 raise Exception("Unexpected connection")
986
987 @remote_compatible
988 def test_sae_commit_override2(dev, apdev):
989 """SAE commit override (wpa_supplicant)"""
990 if "SAE" not in dev[0].get_capability("auth_alg"):
991 raise HwsimSkip("SAE not supported")
992 params = hostapd.wpa2_params(ssid="test-sae",
993 passphrase="12345678")
994 params['wpa_key_mgmt'] = 'SAE'
995 hapd = hostapd.add_ap(apdev[0], params)
996 dev[0].request("SET sae_groups ")
997 dev[0].set('sae_commit_override', '13ffbad00d215867a7c5ff37d87bb9bdb7cb116e520f71e8d7a794ca2606d537ddc6c099c40e7a25372b80a8fd443cd7dd222c8ea21b8ef372d4b3e316c26a73fd999cc79ad483eb826e7b3893ea332da68fa13224bcdeb4fb18b0584dd100a2c514')
998 dev[0].connect("test-sae", psk="test-sae", key_mgmt="SAE",
999 scan_freq="2412", wait_connect=False)
1000 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
1001 if ev is not None:
1002 raise Exception("Unexpected connection")
1003
1004 @remote_compatible
1005 def test_sae_anti_clogging_proto(dev, apdev):
1006 """SAE anti clogging protocol testing"""
1007 if "SAE" not in dev[0].get_capability("auth_alg"):
1008 raise HwsimSkip("SAE not supported")
1009 params = hostapd.wpa2_params(ssid="test-sae",
1010 passphrase="no-knowledge-of-passphrase")
1011 params['wpa_key_mgmt'] = 'SAE'
1012 hapd = hostapd.add_ap(apdev[0], params)
1013 bssid = apdev[0]['bssid']
1014
1015 dev[0].scan_for_bss(bssid, freq=2412)
1016 hapd.set("ext_mgmt_frame_handling", "1")
1017
1018 dev[0].request("SET sae_groups ")
1019 dev[0].connect("test-sae", psk="anti-cloggign", key_mgmt="SAE",
1020 scan_freq="2412", wait_connect=False)
1021
1022 # Commit
1023 for i in range(0, 10):
1024 req = hapd.mgmt_rx()
1025 if req is None:
1026 raise Exception("MGMT RX wait timed out")
1027 if req['subtype'] == 11:
1028 break
1029 req = None
1030 if not req:
1031 raise Exception("Authentication frame not received")
1032
1033 resp = {}
1034 resp['fc'] = req['fc']
1035 resp['da'] = req['sa']
1036 resp['sa'] = req['da']
1037 resp['bssid'] = req['bssid']
1038 resp['payload'] = binascii.unhexlify("030001004c00" + "ffff00")
1039 hapd.mgmt_tx(resp)
1040
1041 # Confirm (not received due to DH group being rejected)
1042 req = hapd.mgmt_rx(timeout=0.5)
1043 if req is not None:
1044 if req['subtype'] == 11:
1045 raise Exception("Unexpected Authentication frame seen")
1046
1047 @remote_compatible
1048 def test_sae_no_random(dev, apdev):
1049 """SAE and no random numbers available"""
1050 if "SAE" not in dev[0].get_capability("auth_alg"):
1051 raise HwsimSkip("SAE not supported")
1052 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
1053 params['wpa_key_mgmt'] = 'SAE'
1054 hapd = hostapd.add_ap(apdev[0], params)
1055
1056 dev[0].request("SET sae_groups ")
1057 tests = [(1, "os_get_random;sae_get_rand"),
1058 (1, "os_get_random;get_rand_1_to_p_1"),
1059 (1, "os_get_random;get_random_qr_qnr"),
1060 (1, "os_get_random;sae_derive_pwe_ecc")]
1061 for count, func in tests:
1062 with fail_test(dev[0], count, func):
1063 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1064 scan_freq="2412")
1065 dev[0].request("REMOVE_NETWORK all")
1066 dev[0].wait_disconnected()
1067
1068 @remote_compatible
1069 def test_sae_pwe_failure(dev, apdev):
1070 """SAE and pwe failure"""
1071 if "SAE" not in dev[0].get_capability("auth_alg"):
1072 raise HwsimSkip("SAE not supported")
1073 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
1074 params['wpa_key_mgmt'] = 'SAE'
1075 params['sae_groups'] = '19 15'
1076 hapd = hostapd.add_ap(apdev[0], params)
1077
1078 dev[0].request("SET sae_groups 19")
1079 with fail_test(dev[0], 1, "hmac_sha256_vector;sae_derive_pwe_ecc"):
1080 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1081 scan_freq="2412")
1082 dev[0].request("REMOVE_NETWORK all")
1083 dev[0].wait_disconnected()
1084 with fail_test(dev[0], 1, "sae_test_pwd_seed_ecc"):
1085 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1086 scan_freq="2412")
1087 dev[0].request("REMOVE_NETWORK all")
1088 dev[0].wait_disconnected()
1089
1090 dev[0].request("SET sae_groups 15")
1091 with fail_test(dev[0], 1, "hmac_sha256_vector;sae_derive_pwe_ffc"):
1092 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1093 scan_freq="2412")
1094 dev[0].request("REMOVE_NETWORK all")
1095 dev[0].wait_disconnected()
1096
1097 dev[0].request("SET sae_groups 15")
1098 with fail_test(dev[0], 1, "sae_test_pwd_seed_ffc"):
1099 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1100 scan_freq="2412")
1101 dev[0].request("REMOVE_NETWORK all")
1102 dev[0].wait_disconnected()
1103 with fail_test(dev[0], 2, "sae_test_pwd_seed_ffc"):
1104 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1105 scan_freq="2412")
1106 dev[0].request("REMOVE_NETWORK all")
1107 dev[0].wait_disconnected()
1108
1109 @remote_compatible
1110 def test_sae_bignum_failure(dev, apdev):
1111 """SAE and bignum failure"""
1112 if "SAE" not in dev[0].get_capability("auth_alg"):
1113 raise HwsimSkip("SAE not supported")
1114 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
1115 params['wpa_key_mgmt'] = 'SAE'
1116 params['sae_groups'] = '19 15 22'
1117 hapd = hostapd.add_ap(apdev[0], params)
1118
1119 dev[0].request("SET sae_groups 19")
1120 tests = [(1, "crypto_bignum_init_set;get_rand_1_to_p_1"),
1121 (1, "crypto_bignum_init;is_quadratic_residue_blind"),
1122 (1, "crypto_bignum_mulmod;is_quadratic_residue_blind"),
1123 (2, "crypto_bignum_mulmod;is_quadratic_residue_blind"),
1124 (3, "crypto_bignum_mulmod;is_quadratic_residue_blind"),
1125 (1, "crypto_bignum_legendre;is_quadratic_residue_blind"),
1126 (1, "crypto_bignum_init_set;sae_test_pwd_seed_ecc"),
1127 (1, "crypto_ec_point_compute_y_sqr;sae_test_pwd_seed_ecc"),
1128 (1, "crypto_bignum_init_set;get_random_qr_qnr"),
1129 (1, "crypto_bignum_to_bin;sae_derive_pwe_ecc"),
1130 (1, "crypto_ec_point_init;sae_derive_pwe_ecc"),
1131 (1, "crypto_ec_point_solve_y_coord;sae_derive_pwe_ecc"),
1132 (1, "crypto_ec_point_init;sae_derive_commit_element_ecc"),
1133 (1, "crypto_ec_point_mul;sae_derive_commit_element_ecc"),
1134 (1, "crypto_ec_point_invert;sae_derive_commit_element_ecc"),
1135 (1, "crypto_bignum_init;=sae_derive_commit"),
1136 (1, "crypto_ec_point_init;sae_derive_k_ecc"),
1137 (1, "crypto_ec_point_mul;sae_derive_k_ecc"),
1138 (1, "crypto_ec_point_add;sae_derive_k_ecc"),
1139 (2, "crypto_ec_point_mul;sae_derive_k_ecc"),
1140 (1, "crypto_ec_point_to_bin;sae_derive_k_ecc"),
1141 (1, "crypto_bignum_legendre;get_random_qr_qnr"),
1142 (1, "sha256_prf;sae_derive_keys"),
1143 (1, "crypto_bignum_init;sae_derive_keys"),
1144 (1, "crypto_bignum_init_set;sae_parse_commit_scalar"),
1145 (1, "crypto_bignum_to_bin;sae_parse_commit_element_ecc"),
1146 (1, "crypto_ec_point_from_bin;sae_parse_commit_element_ecc")]
1147 for count, func in tests:
1148 with fail_test(dev[0], count, func):
1149 hapd.request("NOTE STA failure testing %d:%s" % (count, func))
1150 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1151 scan_freq="2412", wait_connect=False)
1152 wait_fail_trigger(dev[0], "GET_FAIL")
1153 dev[0].request("REMOVE_NETWORK all")
1154 dev[0].dump_monitor()
1155 hapd.dump_monitor()
1156
1157 dev[0].request("SET sae_groups 15")
1158 tests = [(1, "crypto_bignum_init_set;sae_set_group"),
1159 (2, "crypto_bignum_init_set;sae_set_group"),
1160 (1, "crypto_bignum_init_set;sae_get_rand"),
1161 (1, "crypto_bignum_init_set;sae_test_pwd_seed_ffc"),
1162 (1, "crypto_bignum_exptmod;sae_test_pwd_seed_ffc"),
1163 (1, "crypto_bignum_init;sae_derive_pwe_ffc"),
1164 (1, "crypto_bignum_init;sae_derive_commit_element_ffc"),
1165 (1, "crypto_bignum_exptmod;sae_derive_commit_element_ffc"),
1166 (1, "crypto_bignum_inverse;sae_derive_commit_element_ffc"),
1167 (1, "crypto_bignum_init;sae_derive_k_ffc"),
1168 (1, "crypto_bignum_exptmod;sae_derive_k_ffc"),
1169 (1, "crypto_bignum_mulmod;sae_derive_k_ffc"),
1170 (2, "crypto_bignum_exptmod;sae_derive_k_ffc"),
1171 (1, "crypto_bignum_to_bin;sae_derive_k_ffc"),
1172 (1, "crypto_bignum_init_set;sae_parse_commit_element_ffc"),
1173 (1, "crypto_bignum_init;sae_parse_commit_element_ffc"),
1174 (2, "crypto_bignum_init_set;sae_parse_commit_element_ffc"),
1175 (1, "crypto_bignum_exptmod;sae_parse_commit_element_ffc")]
1176 for count, func in tests:
1177 with fail_test(dev[0], count, func):
1178 hapd.request("NOTE STA failure testing %d:%s" % (count, func))
1179 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1180 scan_freq="2412", wait_connect=False)
1181 wait_fail_trigger(dev[0], "GET_FAIL")
1182 dev[0].request("REMOVE_NETWORK all")
1183 dev[0].dump_monitor()
1184 hapd.dump_monitor()
1185
1186 def test_sae_bignum_failure_unsafe_group(dev, apdev):
1187 """SAE and bignum failure unsafe group"""
1188 if "SAE" not in dev[0].get_capability("auth_alg"):
1189 raise HwsimSkip("SAE not supported")
1190 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
1191 params['wpa_key_mgmt'] = 'SAE'
1192 params['sae_groups'] = '22'
1193 hapd = hostapd.add_ap(apdev[0], params)
1194
1195 dev[0].request("SET sae_groups 22")
1196 tests = [(1, "crypto_bignum_init_set;sae_test_pwd_seed_ffc"),
1197 (1, "crypto_bignum_sub;sae_test_pwd_seed_ffc"),
1198 (1, "crypto_bignum_div;sae_test_pwd_seed_ffc")]
1199 for count, func in tests:
1200 with fail_test(dev[0], count, func):
1201 hapd.request("NOTE STA failure testing %d:%s" % (count, func))
1202 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1203 scan_freq="2412", wait_connect=False)
1204 wait_fail_trigger(dev[0], "GET_FAIL")
1205 dev[0].request("REMOVE_NETWORK all")
1206 dev[0].dump_monitor()
1207 hapd.dump_monitor()
1208
1209 def test_sae_invalid_anti_clogging_token_req(dev, apdev):
1210 """SAE and invalid anti-clogging token request"""
1211 if "SAE" not in dev[0].get_capability("auth_alg"):
1212 raise HwsimSkip("SAE not supported")
1213 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
1214 params['wpa_key_mgmt'] = 'SAE'
1215 # Beacon more frequently since Probe Request frames are practically ignored
1216 # in this test setup (ext_mgmt_frame_handled=1 on hostapd side) and
1217 # wpa_supplicant scans may end up getting ignored if no new results are
1218 # available due to the missing Probe Response frames.
1219 params['beacon_int'] = '20'
1220 hapd = hostapd.add_ap(apdev[0], params)
1221 bssid = apdev[0]['bssid']
1222
1223 dev[0].request("SET sae_groups 19")
1224 dev[0].scan_for_bss(bssid, freq=2412)
1225 hapd.set("ext_mgmt_frame_handling", "1")
1226 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1227 scan_freq="2412", wait_connect=False)
1228 ev = dev[0].wait_event(["SME: Trying to authenticate"])
1229 if ev is None:
1230 raise Exception("No authentication attempt seen (1)")
1231 dev[0].dump_monitor()
1232
1233 for i in range(0, 10):
1234 req = hapd.mgmt_rx()
1235 if req is None:
1236 raise Exception("MGMT RX wait timed out (commit)")
1237 if req['subtype'] == 11:
1238 break
1239 req = None
1240 if not req:
1241 raise Exception("Authentication frame (commit) not received")
1242
1243 hapd.dump_monitor()
1244 resp = {}
1245 resp['fc'] = req['fc']
1246 resp['da'] = req['sa']
1247 resp['sa'] = req['da']
1248 resp['bssid'] = req['bssid']
1249 resp['payload'] = binascii.unhexlify("030001004c0013")
1250 hapd.mgmt_tx(resp)
1251 ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
1252 if ev is None:
1253 raise Exception("Management frame TX status not reported (1)")
1254 if "stype=11 ok=1" not in ev:
1255 raise Exception("Unexpected management frame TX status (1): " + ev)
1256
1257 ev = dev[0].wait_event(["SME: Trying to authenticate"])
1258 if ev is None:
1259 raise Exception("No authentication attempt seen (2)")
1260 dev[0].dump_monitor()
1261
1262 for i in range(0, 10):
1263 req = hapd.mgmt_rx()
1264 if req is None:
1265 raise Exception("MGMT RX wait timed out (commit) (2)")
1266 if req['subtype'] == 11:
1267 break
1268 req = None
1269 if not req:
1270 raise Exception("Authentication frame (commit) not received (2)")
1271
1272 hapd.dump_monitor()
1273 resp = {}
1274 resp['fc'] = req['fc']
1275 resp['da'] = req['sa']
1276 resp['sa'] = req['da']
1277 resp['bssid'] = req['bssid']
1278 resp['payload'] = binascii.unhexlify("030001000100")
1279 hapd.mgmt_tx(resp)
1280 ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
1281 if ev is None:
1282 raise Exception("Management frame TX status not reported (1)")
1283 if "stype=11 ok=1" not in ev:
1284 raise Exception("Unexpected management frame TX status (1): " + ev)
1285
1286 ev = dev[0].wait_event(["SME: Trying to authenticate"])
1287 if ev is None:
1288 raise Exception("No authentication attempt seen (3)")
1289 dev[0].dump_monitor()
1290
1291 dev[0].request("DISCONNECT")
1292
1293 def test_sae_password(dev, apdev):
1294 """SAE and sae_password in hostapd configuration"""
1295 if "SAE" not in dev[0].get_capability("auth_alg"):
1296 raise HwsimSkip("SAE not supported")
1297 params = hostapd.wpa2_params(ssid="test-sae",
1298 passphrase="12345678")
1299 params['wpa_key_mgmt'] = 'SAE WPA-PSK'
1300 params['sae_password'] = "sae-password"
1301 hapd = hostapd.add_ap(apdev[0], params)
1302
1303 dev[0].request("SET sae_groups ")
1304 dev[0].connect("test-sae", psk="sae-password", key_mgmt="SAE",
1305 scan_freq="2412")
1306 dev[1].connect("test-sae", psk="12345678", scan_freq="2412")
1307 dev[2].request("SET sae_groups ")
1308 dev[2].connect("test-sae", sae_password="sae-password", key_mgmt="SAE",
1309 scan_freq="2412")
1310
1311 def test_sae_password_short(dev, apdev):
1312 """SAE and short password"""
1313 if "SAE" not in dev[0].get_capability("auth_alg"):
1314 raise HwsimSkip("SAE not supported")
1315 params = hostapd.wpa2_params(ssid="test-sae")
1316 params['wpa_key_mgmt'] = 'SAE'
1317 params['sae_password'] = "secret"
1318 hapd = hostapd.add_ap(apdev[0], params)
1319
1320 dev[0].request("SET sae_groups ")
1321 dev[0].connect("test-sae", sae_password="secret", key_mgmt="SAE",
1322 scan_freq="2412")
1323
1324 def test_sae_password_long(dev, apdev):
1325 """SAE and long password"""
1326 if "SAE" not in dev[0].get_capability("auth_alg"):
1327 raise HwsimSkip("SAE not supported")
1328 params = hostapd.wpa2_params(ssid="test-sae")
1329 params['wpa_key_mgmt'] = 'SAE'
1330 params['sae_password'] = 100*"A"
1331 hapd = hostapd.add_ap(apdev[0], params)
1332
1333 dev[0].request("SET sae_groups ")
1334 dev[0].connect("test-sae", sae_password=100*"A", key_mgmt="SAE",
1335 scan_freq="2412")
1336
1337 def test_sae_connect_cmd(dev, apdev):
1338 """SAE with connect command"""
1339 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
1340 wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
1341 if "SAE" not in wpas.get_capability("auth_alg"):
1342 raise HwsimSkip("SAE not supported")
1343 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
1344 params['wpa_key_mgmt'] = 'SAE'
1345 hapd = hostapd.add_ap(apdev[0], params)
1346
1347 wpas.request("SET sae_groups ")
1348 wpas.connect("test-sae", psk="12345678", key_mgmt="SAE",
1349 scan_freq="2412", wait_connect=False)
1350 # mac80211_hwsim does not support SAE offload, so accept both a successful
1351 # connection and association rejection.
1352 ev = wpas.wait_event(["CTRL-EVENT-CONNECTED", "CTRL-EVENT-ASSOC-REJECT",
1353 "Association request to the driver failed"],
1354 timeout=15)
1355 if ev is None:
1356 raise Exception("No connection result reported")
1357
1358 def run_sae_password_id(dev, apdev, groups=None):
1359 if "SAE" not in dev[0].get_capability("auth_alg"):
1360 raise HwsimSkip("SAE not supported")
1361 params = hostapd.wpa2_params(ssid="test-sae")
1362 params['wpa_key_mgmt'] = 'SAE'
1363 if groups:
1364 params['sae_groups'] = groups
1365 else:
1366 groups = ""
1367 params['sae_password'] = ['secret|mac=ff:ff:ff:ff:ff:ff|id=pw id',
1368 'foo|mac=02:02:02:02:02:02',
1369 'another secret|mac=ff:ff:ff:ff:ff:ff|id=' + 29*'A']
1370 hapd = hostapd.add_ap(apdev[0], params)
1371
1372 dev[0].request("SET sae_groups " + groups)
1373 dev[0].connect("test-sae", sae_password="secret", sae_password_id="pw id",
1374 key_mgmt="SAE", scan_freq="2412")
1375 dev[0].request("REMOVE_NETWORK all")
1376 dev[0].wait_disconnected()
1377
1378 # SAE Password Identifier element with the exact same length as the
1379 # optional Anti-Clogging Token field
1380 dev[0].connect("test-sae", sae_password="another secret",
1381 sae_password_id=29*'A',
1382 key_mgmt="SAE", scan_freq="2412")
1383 dev[0].request("REMOVE_NETWORK all")
1384 dev[0].wait_disconnected()
1385
1386 dev[0].connect("test-sae", sae_password="secret", sae_password_id="unknown",
1387 key_mgmt="SAE", scan_freq="2412", wait_connect=False)
1388
1389 ev = dev[0].wait_event(["CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER"],
1390 timeout=10)
1391 if ev is None:
1392 raise Exception("Unknown password identifier not reported")
1393 dev[0].request("REMOVE_NETWORK all")
1394
1395 def test_sae_password_id(dev, apdev):
1396 """SAE and password identifier"""
1397 run_sae_password_id(dev, apdev, "")
1398
1399 def test_sae_password_id_ecc(dev, apdev):
1400 """SAE and password identifier (ECC)"""
1401 run_sae_password_id(dev, apdev, "19")
1402
1403 def test_sae_password_id_ffc(dev, apdev):
1404 """SAE and password identifier (FFC)"""
1405 run_sae_password_id(dev, apdev, "15")
1406
1407 def test_sae_password_id_only(dev, apdev):
1408 """SAE and password identifier (exclusively)"""
1409 if "SAE" not in dev[0].get_capability("auth_alg"):
1410 raise HwsimSkip("SAE not supported")
1411 params = hostapd.wpa2_params(ssid="test-sae")
1412 params['wpa_key_mgmt'] = 'SAE'
1413 params['sae_password'] = 'secret|id=pw id'
1414 hapd = hostapd.add_ap(apdev[0], params)
1415
1416 dev[0].request("SET sae_groups ")
1417 dev[0].connect("test-sae", sae_password="secret", sae_password_id="pw id",
1418 key_mgmt="SAE", scan_freq="2412")
1419
1420 def test_sae_forced_anti_clogging_pw_id(dev, apdev):
1421 """SAE anti clogging (forced and Password Identifier)"""
1422 if "SAE" not in dev[0].get_capability("auth_alg"):
1423 raise HwsimSkip("SAE not supported")
1424 params = hostapd.wpa2_params(ssid="test-sae")
1425 params['wpa_key_mgmt'] = 'SAE'
1426 params['sae_anti_clogging_threshold'] = '0'
1427 params['sae_password'] = 'secret|id=' + 29*'A'
1428 hostapd.add_ap(apdev[0], params)
1429 for i in range(0, 2):
1430 dev[i].request("SET sae_groups ")
1431 dev[i].connect("test-sae", sae_password="secret",
1432 sae_password_id=29*'A', key_mgmt="SAE", scan_freq="2412")
1433
1434 def test_sae_reauth(dev, apdev):
1435 """SAE reauthentication"""
1436 if "SAE" not in dev[0].get_capability("auth_alg"):
1437 raise HwsimSkip("SAE not supported")
1438 params = hostapd.wpa2_params(ssid="test-sae",
1439 passphrase="12345678")
1440 params['wpa_key_mgmt'] = 'SAE'
1441 params["ieee80211w"] = "2"
1442 hapd = hostapd.add_ap(apdev[0], params)
1443
1444 dev[0].request("SET sae_groups ")
1445 id = dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1446 ieee80211w="2", scan_freq="2412")
1447
1448 hapd.set("ext_mgmt_frame_handling", "1")
1449 dev[0].request("DISCONNECT")
1450 dev[0].wait_disconnected(timeout=10)
1451 hapd.set("ext_mgmt_frame_handling", "0")
1452 dev[0].request("PMKSA_FLUSH")
1453 dev[0].request("REASSOCIATE")
1454 dev[0].wait_connected(timeout=10, error="Timeout on re-connection")
1455
1456 def test_sae_anti_clogging_during_attack(dev, apdev):
1457 """SAE anti clogging during an attack"""
1458 try:
1459 run_sae_anti_clogging_during_attack(dev, apdev)
1460 finally:
1461 stop_monitor(apdev[1]["ifname"])
1462
1463 def build_sae_commit(bssid, addr, group=21, token=None):
1464 if group == 19:
1465 scalar = binascii.unhexlify("7332d3ebff24804005ccd8c56141e3ed8d84f40638aa31cd2fac11d4d2e89e7b")
1466 element = binascii.unhexlify("954d0f4457066bff3168376a1d7174f4e66620d1792406f613055b98513a7f03a538c13dfbaf2029e2adc6aa96aa0ddcf08ac44887b02f004b7f29b9dbf4b7d9")
1467 elif group == 21:
1468 scalar = binascii.unhexlify("001eec673111b902f5c8a61c8cb4c1c4793031aeea8c8c319410903bc64bcbaea134ab01c4e016d51436f5b5426f7e2af635759a3033fb4031ea79f89a62a3e2f828")
1469 element = binascii.unhexlify("00580eb4b448ea600ea277d5e66e4ed37db82bb04ac90442e9c3727489f366ba4b82f0a472d02caf4cdd142e96baea5915d71374660ee23acbaca38cf3fe8c5fb94b01abbc5278121635d7c06911c5dad8f18d516e1fbe296c179b7c87a1dddfab393337d3d215ed333dd396da6d8f20f798c60d054f1093c24d9c2d98e15c030cc375f0")
1470 pass
1471 frame = binascii.unhexlify("b0003a01")
1472 frame += bssid + addr + bssid
1473 frame += binascii.unhexlify("1000")
1474 auth_alg = 3
1475 transact = 1
1476 status = 0
1477 frame += struct.pack("<HHHH", auth_alg, transact, status, group)
1478 if token:
1479 frame += token
1480 frame += scalar + element
1481 return frame
1482
1483 def sae_rx_commit_token_req(sock, radiotap, send_two=False):
1484 msg = sock.recv(1500)
1485 ver, pad, len, present = struct.unpack('<BBHL', msg[0:8])
1486 frame = msg[len:]
1487 fc, duration = struct.unpack('<HH', frame[0:4])
1488 if fc != 0xb0:
1489 return False
1490 frame = frame[4:]
1491 da = frame[0:6]
1492 if da[0] != 0xf2:
1493 return False
1494 sa = frame[6:12]
1495 bssid = frame[12:18]
1496 body = frame[20:]
1497
1498 alg, seq, status, group = struct.unpack('<HHHH', body[0:8])
1499 if alg != 3 or seq != 1 or status != 76:
1500 return False
1501 token = body[8:]
1502
1503 frame = build_sae_commit(bssid, da, token=token)
1504 sock.send(radiotap + frame)
1505 if send_two:
1506 sock.send(radiotap + frame)
1507 return True
1508
1509 def radiotap_build():
1510 radiotap_payload = struct.pack('BB', 0x08, 0)
1511 radiotap_payload += struct.pack('BB', 0, 0)
1512 radiotap_payload += struct.pack('BB', 0, 0)
1513 radiotap_hdr = struct.pack('<BBHL', 0, 0, 8 + len(radiotap_payload),
1514 0xc002)
1515 return radiotap_hdr + radiotap_payload
1516
1517 def start_monitor(ifname, freq=2412):
1518 subprocess.check_call(["iw", ifname, "set", "type", "monitor"])
1519 subprocess.call(["ip", "link", "set", "dev", ifname, "up"])
1520 subprocess.check_call(["iw", ifname, "set", "freq", str(freq)])
1521
1522 ETH_P_ALL = 3
1523 sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW,
1524 socket.htons(ETH_P_ALL))
1525 sock.bind((ifname, 0))
1526 sock.settimeout(0.5)
1527 return sock
1528
1529 def stop_monitor(ifname):
1530 subprocess.call(["ip", "link", "set", "dev", ifname, "down"])
1531 subprocess.call(["iw", ifname, "set", "type", "managed"])
1532
1533 def run_sae_anti_clogging_during_attack(dev, apdev):
1534 if "SAE" not in dev[0].get_capability("auth_alg"):
1535 raise HwsimSkip("SAE not supported")
1536 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
1537 params['wpa_key_mgmt'] = 'SAE'
1538 params['sae_groups'] = '21'
1539 hapd = hostapd.add_ap(apdev[0], params)
1540
1541 dev[0].scan_for_bss(hapd.own_addr(), freq=2412)
1542 dev[0].request("SET sae_groups 21")
1543 dev[1].scan_for_bss(hapd.own_addr(), freq=2412)
1544 dev[1].request("SET sae_groups 21")
1545
1546 sock = start_monitor(apdev[1]["ifname"])
1547 radiotap = radiotap_build()
1548
1549 bssid = binascii.unhexlify(hapd.own_addr().replace(':', ''))
1550 for i in range(16):
1551 addr = binascii.unhexlify("f2%010x" % i)
1552 frame = build_sae_commit(bssid, addr)
1553 sock.send(radiotap + frame)
1554 sock.send(radiotap + frame)
1555
1556 count = 0
1557 for i in range(150):
1558 if sae_rx_commit_token_req(sock, radiotap, send_two=True):
1559 count += 1
1560 logger.info("Number of token responses sent: %d" % count)
1561 if count < 10:
1562 raise Exception("Too few token responses seen: %d" % count)
1563
1564 for i in range(16):
1565 addr = binascii.unhexlify("f201%08x" % i)
1566 frame = build_sae_commit(bssid, addr)
1567 sock.send(radiotap + frame)
1568
1569 count = 0
1570 for i in range(150):
1571 if sae_rx_commit_token_req(sock, radiotap):
1572 count += 1
1573 if count == 10:
1574 break
1575 if count < 5:
1576 raise Exception("Too few token responses in second round: %d" % count)
1577
1578 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1579 scan_freq="2412", wait_connect=False)
1580 dev[1].connect("test-sae", psk="12345678", key_mgmt="SAE",
1581 scan_freq="2412", wait_connect=False)
1582
1583 count = 0
1584 connected0 = False
1585 connected1 = False
1586 for i in range(1000):
1587 if sae_rx_commit_token_req(sock, radiotap):
1588 count += 1
1589 addr = binascii.unhexlify("f202%08x" % i)
1590 frame = build_sae_commit(bssid, addr)
1591 sock.send(radiotap + frame)
1592 while dev[0].mon.pending():
1593 ev = dev[0].mon.recv()
1594 logger.debug("EV0: " + ev)
1595 if "CTRL-EVENT-CONNECTED" in ev:
1596 connected0 = True
1597 while dev[1].mon.pending():
1598 ev = dev[1].mon.recv()
1599 logger.debug("EV1: " + ev)
1600 if "CTRL-EVENT-CONNECTED" in ev:
1601 connected1 = True
1602 if connected0 and connected1:
1603 break
1604 if not connected0:
1605 raise Exception("Real station(0) did not get connected")
1606 if not connected1:
1607 raise Exception("Real station(1) did not get connected")
1608 if count < 1:
1609 raise Exception("Too few token responses in third round: %d" % count)