]> git.ipfire.org Git - thirdparty/hostap.git/blame - tests/hwsim/test_sae.py
tests: Protocol testing for supplicant PMF/IGTK KDE handling
[thirdparty/hostap.git] / tests / hwsim / test_sae.py
CommitLineData
1640a2e4 1# Test cases for SAE
4f6985de 2# Copyright (c) 2013-2016, Jouni Malinen <j@w1.fi>
1640a2e4
JM
3#
4# This software may be distributed under the terms of the BSD license.
5# See README for more details.
6
9fd6804d 7from remotehost import remote_compatible
5b3c40a6
JM
8import binascii
9import os
1640a2e4 10import time
1640a2e4
JM
11import logging
12logger = logging.getLogger()
e43352ff
JM
13import socket
14import struct
15import subprocess
1640a2e4
JM
16
17import hwsim_utils
18import hostapd
33822240 19from wpasupplicant import WpaSupplicant
8030e2b5 20from utils import HwsimSkip, alloc_fail, fail_test, wait_fail_trigger, start_monitor, stop_monitor, radiotap_build
5b3c40a6 21from test_ap_psk import find_wpas_process, read_process_memory, verify_not_present, get_key_locations
1640a2e4 22
9fd6804d 23@remote_compatible
1640a2e4
JM
24def test_sae(dev, apdev):
25 """SAE with default group"""
b9749b6a
JM
26 if "SAE" not in dev[0].get_capability("auth_alg"):
27 raise HwsimSkip("SAE not supported")
1640a2e4
JM
28 params = hostapd.wpa2_params(ssid="test-sae",
29 passphrase="12345678")
30 params['wpa_key_mgmt'] = 'SAE'
8b8a1864 31 hapd = hostapd.add_ap(apdev[0], params)
65038313
JM
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)
1640a2e4
JM
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")
d463c556
JM
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'])
1640a2e4 46
7b28c408
JM
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
9fd6804d 51@remote_compatible
33dcced5
JM
52def 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'
8b8a1864 59 hapd = hostapd.add_ap(apdev[0], params)
33dcced5
JM
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
9fd6804d 71@remote_compatible
33dcced5
JM
72def 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'
8e607b1b 79 params['sae_groups'] = '15'
8b8a1864 80 hapd = hostapd.add_ap(apdev[0], params)
33dcced5 81
8e607b1b 82 dev[0].request("SET sae_groups 15")
33dcced5
JM
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
9fd6804d 92@remote_compatible
19d3a6e3
JM
93def test_sae_pmksa_caching(dev, apdev):
94 """SAE and PMKSA caching"""
b9749b6a
JM
95 if "SAE" not in dev[0].get_capability("auth_alg"):
96 raise HwsimSkip("SAE not supported")
19d3a6e3
JM
97 params = hostapd.wpa2_params(ssid="test-sae",
98 passphrase="12345678")
99 params['wpa_key_mgmt'] = 'SAE'
8b8a1864 100 hapd = hostapd.add_ap(apdev[0], params)
19d3a6e3
JM
101
102 dev[0].request("SET sae_groups ")
103 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
104 scan_freq="2412")
fab49f61 105 ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=5)
7cc9a81f
JM
106 if ev is None:
107 raise Exception("No connection event received from hostapd")
19d3a6e3 108 dev[0].request("DISCONNECT")
7cc9a81f 109 dev[0].wait_disconnected()
19d3a6e3 110 dev[0].request("RECONNECT")
5f35a5e2 111 dev[0].wait_connected(timeout=15, error="Reconnect timed out")
19d3a6e3
JM
112 if dev[0].get_status_field('sae_group') is not None:
113 raise Exception("SAE group claimed to have been used")
114
9fd6804d 115@remote_compatible
19d3a6e3
JM
116def test_sae_pmksa_caching_disabled(dev, apdev):
117 """SAE and PMKSA caching disabled"""
b9749b6a
JM
118 if "SAE" not in dev[0].get_capability("auth_alg"):
119 raise HwsimSkip("SAE not supported")
19d3a6e3
JM
120 params = hostapd.wpa2_params(ssid="test-sae",
121 passphrase="12345678")
122 params['wpa_key_mgmt'] = 'SAE'
123 params['disable_pmksa_caching'] = '1'
8b8a1864 124 hapd = hostapd.add_ap(apdev[0], params)
19d3a6e3
JM
125
126 dev[0].request("SET sae_groups ")
127 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
128 scan_freq="2412")
fab49f61 129 ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=5)
7cc9a81f
JM
130 if ev is None:
131 raise Exception("No connection event received from hostapd")
19d3a6e3 132 dev[0].request("DISCONNECT")
7cc9a81f 133 dev[0].wait_disconnected()
19d3a6e3 134 dev[0].request("RECONNECT")
5f35a5e2 135 dev[0].wait_connected(timeout=15, error="Reconnect timed out")
19d3a6e3
JM
136 if dev[0].get_status_field('sae_group') != '19':
137 raise Exception("Expected default SAE group not used")
138
1640a2e4
JM
139def test_sae_groups(dev, apdev):
140 """SAE with all supported groups"""
b9749b6a
JM
141 if "SAE" not in dev[0].get_capability("auth_alg"):
142 raise HwsimSkip("SAE not supported")
b1f487cb
JM
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.
fab49f61 147 sae_groups = [19, 25, 26, 20, 21, 1, 2, 5, 14, 15, 16, 22, 23, 24]
9e286d5e 148 tls = dev[0].request("GET tls_library")
a053ab95 149 if tls.startswith("OpenSSL") and "run=OpenSSL 1." in tls:
9e286d5e 150 logger.info("Add Brainpool EC groups since OpenSSL is new enough")
fab49f61
JM
151 sae_groups += [27, 28, 29, 30]
152 heavy_groups = [14, 15, 16]
8e607b1b 153 suitable_groups = [15, 16, 17, 18, 19, 20, 21, 28, 29, 30]
1640a2e4
JM
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)
8b8a1864 159 hostapd.add_ap(apdev[0], params)
1640a2e4
JM
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",
b1f487cb
JM
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:
a68d1792
JM
176 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
177 if ev is None:
fab49f61 178 if "BoringSSL" in tls and int(g) in [25]:
a68d1792
JM
179 logger.info("Ignore connection failure with group " + g + " with BoringSSL")
180 dev[0].remove_network(id)
181 dev[0].dump_monitor()
182 continue
8e607b1b
JM
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
a68d1792 188 raise Exception("Connection timed out with group " + g)
1640a2e4
JM
189 if dev[0].get_status_field('sae_group') != g:
190 raise Exception("Expected SAE group not used")
191 dev[0].remove_network(id)
b1f487cb
JM
192 dev[0].wait_disconnected()
193 dev[0].dump_monitor()
1640a2e4 194
9fd6804d 195@remote_compatible
1640a2e4
JM
196def test_sae_group_nego(dev, apdev):
197 """SAE group negotiation"""
b9749b6a
JM
198 if "SAE" not in dev[0].get_capability("auth_alg"):
199 raise HwsimSkip("SAE not supported")
1640a2e4
JM
200 params = hostapd.wpa2_params(ssid="test-sae-group-nego",
201 passphrase="12345678")
202 params['wpa_key_mgmt'] = 'SAE'
203 params['sae_groups'] = '19'
8b8a1864 204 hostapd.add_ap(apdev[0], params)
1640a2e4
JM
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")
a6cf5cd6 211
914d8eca
JM
212def 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)
fab49f61 226 ev = dev[0].wait_event(["CTRL-EVENT-SSID-TEMP-DISABLED"], timeout=10)
914d8eca
JM
227 dev[0].request("REMOVE_NETWORK all")
228 if ev is None:
229 raise Exception("Network profile disabling not reported")
230
9fd6804d 231@remote_compatible
a6cf5cd6
JM
232def test_sae_anti_clogging(dev, apdev):
233 """SAE anti clogging"""
b9749b6a
JM
234 if "SAE" not in dev[0].get_capability("auth_alg"):
235 raise HwsimSkip("SAE not supported")
a6cf5cd6
JM
236 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
237 params['wpa_key_mgmt'] = 'SAE'
238 params['sae_anti_clogging_threshold'] = '1'
8b8a1864 239 hostapd.add_ap(apdev[0], params)
a6cf5cd6
JM
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):
5f35a5e2 251 dev[i].wait_connected(timeout=10)
d05ff960
JM
252
253def test_sae_forced_anti_clogging(dev, apdev):
254 """SAE anti clogging (forced)"""
b9749b6a
JM
255 if "SAE" not in dev[0].get_capability("auth_alg"):
256 raise HwsimSkip("SAE not supported")
d05ff960 257 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
fd4709ff 258 params['wpa_key_mgmt'] = 'SAE WPA-PSK'
d05ff960 259 params['sae_anti_clogging_threshold'] = '0'
8b8a1864 260 hostapd.add_ap(apdev[0], params)
fd4709ff 261 dev[2].connect("test-sae", psk="12345678", scan_freq="2412")
d05ff960
JM
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
267def test_sae_mixed(dev, apdev):
268 """Mixed SAE and non-SAE network"""
b9749b6a
JM
269 if "SAE" not in dev[0].get_capability("auth_alg"):
270 raise HwsimSkip("SAE not supported")
d05ff960
JM
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'
8b8a1864 274 hostapd.add_ap(apdev[0], params)
d05ff960
JM
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")
acb63c75 281
fa617ee6
JM
282def 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
294def 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
5c8df74f
JM
305def 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
c481e1cb
JM
337def 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
9fd6804d 354@remote_compatible
acb63c75
JM
355def test_sae_missing_password(dev, apdev):
356 """SAE and missing password"""
b9749b6a
JM
357 if "SAE" not in dev[0].get_capability("auth_alg"):
358 raise HwsimSkip("SAE not supported")
acb63c75
JM
359 params = hostapd.wpa2_params(ssid="test-sae",
360 passphrase="12345678")
361 params['wpa_key_mgmt'] = 'SAE'
8b8a1864 362 hapd = hostapd.add_ap(apdev[0], params)
acb63c75
JM
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")
5b3c40a6
JM
371
372
373def test_sae_key_lifetime_in_memory(dev, apdev, params):
374 """SAE and key lifetime in memory"""
b9749b6a
JM
375 if "SAE" not in dev[0].get_capability("auth_alg"):
376 raise HwsimSkip("SAE not supported")
5b3c40a6
JM
377 password = "5ad144a7c1f5a5503baa6fa01dabc15b1843e8c01662d78d16b70b5cd23cf8b"
378 p = hostapd.wpa2_params(ssid="test-sae", passphrase=password)
379 p['wpa_key_mgmt'] = 'SAE'
8b8a1864 380 hapd = hostapd.add_ap(apdev[0], p)
5b3c40a6
JM
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
8e416cec
JM
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.
54f2cae2 391 time.sleep(1)
8e416cec 392 dev[0].ping()
b3361e5d 393 password = password.encode()
5b3c40a6
JM
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:
81e787b7 442 raise HwsimSkip("Password not found while associated")
5b3c40a6 443 if pmk not in buf:
81e787b7 444 raise HwsimSkip("PMK not found while associated")
5b3c40a6
JM
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")
b74f82a4
JM
449 #if tk in buf:
450 # raise Exception("TK found from memory")
5b3c40a6
JM
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")
6db556b2
JM
466 if gtk in buf:
467 get_key_locations(buf, gtk, "GTK")
5b3c40a6
JM
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)")
eb6d3532 496
9fd6804d 497@remote_compatible
eb6d3532
JM
498def 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'
8e607b1b 505 params['sae_groups'] = '19 25 26 20'
8b8a1864 506 hapd = hostapd.add_ap(apdev[0], params)
eb6d3532 507
8e607b1b 508 dev[0].request("SET sae_groups 20")
eb6d3532
JM
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")
5527a391 519
2fd44db7
JM
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
9fd6804d 543@remote_compatible
5527a391
JM
544def 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'
8b8a1864 551 hapd = hostapd.add_ap(apdev[0], params)
5527a391
JM
552 bssid = apdev[0]['bssid']
553
554 dev[0].request("SET sae_groups 19")
555
fab49f61
JM
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")]
5527a391
JM
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
9fd6804d 642@remote_compatible
5527a391
JM
643def 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'
8b8a1864 650 hapd = hostapd.add_ap(apdev[0], params)
5527a391
JM
651 bssid = apdev[0]['bssid']
652
653 dev[0].request("SET sae_groups 2")
654
fab49f61
JM
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)]
5527a391
JM
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
2d0a04a8
JM
723def 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()
54c58f29 756 hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=" + binascii.hexlify(req['frame']).decode())
2d0a04a8
JM
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()
54c58f29 770 hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=" + binascii.hexlify(req['frame']).decode())
2d0a04a8
JM
771
772 logger.info("Replay Confirm")
54c58f29 773 hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=" + binascii.hexlify(req['frame']).decode())
2d0a04a8
JM
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()
54c58f29 787 hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=" + binascii.hexlify(req['frame']).decode())
2d0a04a8
JM
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
7a92dbd7
JM
801def 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
2b70a82c
JM
825def 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
848def 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
9fd6804d 868@remote_compatible
5527a391
JM
869def 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'
8b8a1864 875 hapd = hostapd.add_ap(apdev[0], params)
5527a391 876
8e607b1b 877 dev[0].request("SET sae_groups 15")
5527a391
JM
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")
939527b5
JM
887
888def 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'
afc26df2 894 hapd = hostapd.add_ap(apdev, params)
939527b5
JM
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
9fd6804d 929@remote_compatible
939527b5
JM
930def test_sae_reflection_attack_ecc(dev, apdev):
931 """SAE reflection attack (ECC)"""
932 sae_reflection_attack(apdev[0], dev[0], 19)
933
9fd6804d 934@remote_compatible
939527b5
JM
935def test_sae_reflection_attack_ffc(dev, apdev):
936 """SAE reflection attack (FFC)"""
8e607b1b 937 sae_reflection_attack(apdev[0], dev[0], 15)
1965e196 938
1342c47a
JM
939def 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)
8e607b1b
JM
953 ev = dev.wait_event(["SME: Trying to authenticate"], timeout=10)
954 if ev is None:
955 raise Exception("No authentication attempt seen")
1342c47a
JM
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
961def 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
966def test_sae_reflection_attack_ffc_internal(dev, apdev):
967 """SAE reflection attack (FFC) - internal"""
8e607b1b 968 sae_reflection_attack_internal(apdev[0], dev[0], 15)
1342c47a 969
cd06e266
JM
970@remote_compatible
971def 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
eea62048
JM
987@remote_compatible
988def 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
faddd025
JM
1004def test_sae_commit_invalid_scalar_element_ap(dev, apdev):
1005 """SAE commit invalid scalar/element from AP"""
1006 if "SAE" not in dev[0].get_capability("auth_alg"):
1007 raise HwsimSkip("SAE not supported")
1008 params = hostapd.wpa2_params(ssid="test-sae",
1009 passphrase="12345678")
1010 params['wpa_key_mgmt'] = 'SAE'
1011 params['sae_commit_override'] = '1300' + 96*'00'
1012 hapd = hostapd.add_ap(apdev[0], params)
1013 dev[0].request("SET sae_groups ")
1014 dev[0].connect("test-sae", psk="test-sae", key_mgmt="SAE",
1015 scan_freq="2412", wait_connect=False)
1016 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
1017 if ev is not None:
1018 raise Exception("Unexpected connection")
1019
1020def test_sae_commit_invalid_element_ap(dev, apdev):
1021 """SAE commit invalid element from AP"""
1022 if "SAE" not in dev[0].get_capability("auth_alg"):
1023 raise HwsimSkip("SAE not supported")
1024 params = hostapd.wpa2_params(ssid="test-sae",
1025 passphrase="12345678")
1026 params['wpa_key_mgmt'] = 'SAE'
1027 params['sae_commit_override'] = '1300' + 31*'00' + '02' + 64*'00'
1028 hapd = hostapd.add_ap(apdev[0], params)
1029 dev[0].request("SET sae_groups ")
1030 dev[0].connect("test-sae", psk="test-sae", key_mgmt="SAE",
1031 scan_freq="2412", wait_connect=False)
1032 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
1033 if ev is not None:
1034 raise Exception("Unexpected connection")
1035
1036def test_sae_commit_invalid_scalar_element_sta(dev, apdev):
1037 """SAE commit invalid scalar/element from STA"""
1038 if "SAE" not in dev[0].get_capability("auth_alg"):
1039 raise HwsimSkip("SAE not supported")
1040 params = hostapd.wpa2_params(ssid="test-sae",
1041 passphrase="12345678")
1042 params['wpa_key_mgmt'] = 'SAE'
1043 hapd = hostapd.add_ap(apdev[0], params)
1044 dev[0].request("SET sae_groups ")
1045 dev[0].set('sae_commit_override', '1300' + 96*'00')
1046 dev[0].connect("test-sae", psk="test-sae", key_mgmt="SAE",
1047 scan_freq="2412", wait_connect=False)
1048 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
1049 if ev is not None:
1050 raise Exception("Unexpected connection")
1051
1052def test_sae_commit_invalid_element_sta(dev, apdev):
1053 """SAE commit invalid element from STA"""
1054 if "SAE" not in dev[0].get_capability("auth_alg"):
1055 raise HwsimSkip("SAE not supported")
1056 params = hostapd.wpa2_params(ssid="test-sae",
1057 passphrase="12345678")
1058 params['wpa_key_mgmt'] = 'SAE'
1059 hapd = hostapd.add_ap(apdev[0], params)
1060 dev[0].request("SET sae_groups ")
1061 dev[0].set('sae_commit_override', '1300' + 31*'00' + '02' + 64*'00')
1062 dev[0].connect("test-sae", psk="test-sae", key_mgmt="SAE",
1063 scan_freq="2412", wait_connect=False)
1064 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
1065 if ev is not None:
1066 raise Exception("Unexpected connection")
1067
9fd6804d 1068@remote_compatible
1965e196
JM
1069def test_sae_anti_clogging_proto(dev, apdev):
1070 """SAE anti clogging protocol testing"""
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",
1074 passphrase="no-knowledge-of-passphrase")
1075 params['wpa_key_mgmt'] = 'SAE'
8b8a1864 1076 hapd = hostapd.add_ap(apdev[0], params)
1965e196
JM
1077 bssid = apdev[0]['bssid']
1078
1079 dev[0].scan_for_bss(bssid, freq=2412)
1080 hapd.set("ext_mgmt_frame_handling", "1")
1081
1082 dev[0].request("SET sae_groups ")
1083 dev[0].connect("test-sae", psk="anti-cloggign", key_mgmt="SAE",
1084 scan_freq="2412", wait_connect=False)
1085
1086 # Commit
1087 for i in range(0, 10):
1088 req = hapd.mgmt_rx()
1089 if req is None:
1090 raise Exception("MGMT RX wait timed out")
1091 if req['subtype'] == 11:
1092 break
1093 req = None
1094 if not req:
1095 raise Exception("Authentication frame not received")
1096
1097 resp = {}
1098 resp['fc'] = req['fc']
1099 resp['da'] = req['sa']
1100 resp['sa'] = req['da']
1101 resp['bssid'] = req['bssid']
1102 resp['payload'] = binascii.unhexlify("030001004c00" + "ffff00")
1103 hapd.mgmt_tx(resp)
1104
1105 # Confirm (not received due to DH group being rejected)
1106 req = hapd.mgmt_rx(timeout=0.5)
1107 if req is not None:
1108 if req['subtype'] == 11:
1109 raise Exception("Unexpected Authentication frame seen")
10ba4ae4 1110
9fd6804d 1111@remote_compatible
10ba4ae4
JM
1112def test_sae_no_random(dev, apdev):
1113 """SAE and no random numbers available"""
1114 if "SAE" not in dev[0].get_capability("auth_alg"):
1115 raise HwsimSkip("SAE not supported")
1116 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
1117 params['wpa_key_mgmt'] = 'SAE'
8b8a1864 1118 hapd = hostapd.add_ap(apdev[0], params)
10ba4ae4
JM
1119
1120 dev[0].request("SET sae_groups ")
fab49f61
JM
1121 tests = [(1, "os_get_random;sae_get_rand"),
1122 (1, "os_get_random;get_rand_1_to_p_1"),
1123 (1, "os_get_random;get_random_qr_qnr"),
1124 (1, "os_get_random;sae_derive_pwe_ecc")]
10ba4ae4
JM
1125 for count, func in tests:
1126 with fail_test(dev[0], count, func):
1127 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1128 scan_freq="2412")
1129 dev[0].request("REMOVE_NETWORK all")
1130 dev[0].wait_disconnected()
4f6985de 1131
9fd6804d 1132@remote_compatible
4f6985de
JM
1133def test_sae_pwe_failure(dev, apdev):
1134 """SAE and pwe failure"""
1135 if "SAE" not in dev[0].get_capability("auth_alg"):
1136 raise HwsimSkip("SAE not supported")
1137 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
1138 params['wpa_key_mgmt'] = 'SAE'
8e607b1b 1139 params['sae_groups'] = '19 15'
8b8a1864 1140 hapd = hostapd.add_ap(apdev[0], params)
4f6985de
JM
1141
1142 dev[0].request("SET sae_groups 19")
1143 with fail_test(dev[0], 1, "hmac_sha256_vector;sae_derive_pwe_ecc"):
1144 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1145 scan_freq="2412")
1146 dev[0].request("REMOVE_NETWORK all")
1147 dev[0].wait_disconnected()
51761ba2
JM
1148 with fail_test(dev[0], 1, "sae_test_pwd_seed_ecc"):
1149 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1150 scan_freq="2412")
1151 dev[0].request("REMOVE_NETWORK all")
1152 dev[0].wait_disconnected()
4f6985de 1153
8e607b1b 1154 dev[0].request("SET sae_groups 15")
4f6985de
JM
1155 with fail_test(dev[0], 1, "hmac_sha256_vector;sae_derive_pwe_ffc"):
1156 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1157 scan_freq="2412")
1158 dev[0].request("REMOVE_NETWORK all")
1159 dev[0].wait_disconnected()
1160
8e607b1b 1161 dev[0].request("SET sae_groups 15")
4f6985de
JM
1162 with fail_test(dev[0], 1, "sae_test_pwd_seed_ffc"):
1163 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1164 scan_freq="2412")
1165 dev[0].request("REMOVE_NETWORK all")
1166 dev[0].wait_disconnected()
1167 with fail_test(dev[0], 2, "sae_test_pwd_seed_ffc"):
1168 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1169 scan_freq="2412")
1170 dev[0].request("REMOVE_NETWORK all")
1171 dev[0].wait_disconnected()
51761ba2 1172
9fd6804d 1173@remote_compatible
51761ba2
JM
1174def test_sae_bignum_failure(dev, apdev):
1175 """SAE and bignum failure"""
1176 if "SAE" not in dev[0].get_capability("auth_alg"):
1177 raise HwsimSkip("SAE not supported")
1178 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
1179 params['wpa_key_mgmt'] = 'SAE'
8e607b1b 1180 params['sae_groups'] = '19 15 22'
8b8a1864 1181 hapd = hostapd.add_ap(apdev[0], params)
51761ba2
JM
1182
1183 dev[0].request("SET sae_groups 19")
fab49f61
JM
1184 tests = [(1, "crypto_bignum_init_set;get_rand_1_to_p_1"),
1185 (1, "crypto_bignum_init;is_quadratic_residue_blind"),
1186 (1, "crypto_bignum_mulmod;is_quadratic_residue_blind"),
1187 (2, "crypto_bignum_mulmod;is_quadratic_residue_blind"),
1188 (3, "crypto_bignum_mulmod;is_quadratic_residue_blind"),
1189 (1, "crypto_bignum_legendre;is_quadratic_residue_blind"),
1190 (1, "crypto_bignum_init_set;sae_test_pwd_seed_ecc"),
1191 (1, "crypto_ec_point_compute_y_sqr;sae_test_pwd_seed_ecc"),
1192 (1, "crypto_bignum_init_set;get_random_qr_qnr"),
1193 (1, "crypto_bignum_to_bin;sae_derive_pwe_ecc"),
1194 (1, "crypto_ec_point_init;sae_derive_pwe_ecc"),
1195 (1, "crypto_ec_point_solve_y_coord;sae_derive_pwe_ecc"),
1196 (1, "crypto_ec_point_init;sae_derive_commit_element_ecc"),
1197 (1, "crypto_ec_point_mul;sae_derive_commit_element_ecc"),
1198 (1, "crypto_ec_point_invert;sae_derive_commit_element_ecc"),
1199 (1, "crypto_bignum_init;=sae_derive_commit"),
1200 (1, "crypto_ec_point_init;sae_derive_k_ecc"),
1201 (1, "crypto_ec_point_mul;sae_derive_k_ecc"),
1202 (1, "crypto_ec_point_add;sae_derive_k_ecc"),
1203 (2, "crypto_ec_point_mul;sae_derive_k_ecc"),
1204 (1, "crypto_ec_point_to_bin;sae_derive_k_ecc"),
1205 (1, "crypto_bignum_legendre;get_random_qr_qnr"),
1206 (1, "sha256_prf;sae_derive_keys"),
1207 (1, "crypto_bignum_init;sae_derive_keys"),
1208 (1, "crypto_bignum_init_set;sae_parse_commit_scalar"),
1209 (1, "crypto_bignum_to_bin;sae_parse_commit_element_ecc"),
1210 (1, "crypto_ec_point_from_bin;sae_parse_commit_element_ecc")]
51761ba2
JM
1211 for count, func in tests:
1212 with fail_test(dev[0], count, func):
e96fa197 1213 hapd.request("NOTE STA failure testing %d:%s" % (count, func))
51761ba2
JM
1214 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1215 scan_freq="2412", wait_connect=False)
1216 wait_fail_trigger(dev[0], "GET_FAIL")
1217 dev[0].request("REMOVE_NETWORK all")
e96fa197
JM
1218 dev[0].dump_monitor()
1219 hapd.dump_monitor()
51761ba2 1220
8e607b1b 1221 dev[0].request("SET sae_groups 15")
fab49f61
JM
1222 tests = [(1, "crypto_bignum_init_set;sae_set_group"),
1223 (2, "crypto_bignum_init_set;sae_set_group"),
1224 (1, "crypto_bignum_init_set;sae_get_rand"),
1225 (1, "crypto_bignum_init_set;sae_test_pwd_seed_ffc"),
1226 (1, "crypto_bignum_exptmod;sae_test_pwd_seed_ffc"),
1227 (1, "crypto_bignum_init;sae_derive_pwe_ffc"),
1228 (1, "crypto_bignum_init;sae_derive_commit_element_ffc"),
1229 (1, "crypto_bignum_exptmod;sae_derive_commit_element_ffc"),
1230 (1, "crypto_bignum_inverse;sae_derive_commit_element_ffc"),
1231 (1, "crypto_bignum_init;sae_derive_k_ffc"),
1232 (1, "crypto_bignum_exptmod;sae_derive_k_ffc"),
1233 (1, "crypto_bignum_mulmod;sae_derive_k_ffc"),
1234 (2, "crypto_bignum_exptmod;sae_derive_k_ffc"),
1235 (1, "crypto_bignum_to_bin;sae_derive_k_ffc"),
1236 (1, "crypto_bignum_init_set;sae_parse_commit_element_ffc"),
1237 (1, "crypto_bignum_init;sae_parse_commit_element_ffc"),
1238 (2, "crypto_bignum_init_set;sae_parse_commit_element_ffc"),
1239 (1, "crypto_bignum_exptmod;sae_parse_commit_element_ffc")]
51761ba2
JM
1240 for count, func in tests:
1241 with fail_test(dev[0], count, func):
e96fa197 1242 hapd.request("NOTE STA failure testing %d:%s" % (count, func))
51761ba2
JM
1243 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1244 scan_freq="2412", wait_connect=False)
1245 wait_fail_trigger(dev[0], "GET_FAIL")
1246 dev[0].request("REMOVE_NETWORK all")
e96fa197
JM
1247 dev[0].dump_monitor()
1248 hapd.dump_monitor()
51761ba2 1249
8e607b1b
JM
1250def test_sae_bignum_failure_unsafe_group(dev, apdev):
1251 """SAE and bignum failure unsafe group"""
1252 if "SAE" not in dev[0].get_capability("auth_alg"):
1253 raise HwsimSkip("SAE not supported")
1254 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
1255 params['wpa_key_mgmt'] = 'SAE'
1256 params['sae_groups'] = '22'
1257 hapd = hostapd.add_ap(apdev[0], params)
1258
51761ba2 1259 dev[0].request("SET sae_groups 22")
fab49f61
JM
1260 tests = [(1, "crypto_bignum_init_set;sae_test_pwd_seed_ffc"),
1261 (1, "crypto_bignum_sub;sae_test_pwd_seed_ffc"),
1262 (1, "crypto_bignum_div;sae_test_pwd_seed_ffc")]
51761ba2
JM
1263 for count, func in tests:
1264 with fail_test(dev[0], count, func):
e96fa197 1265 hapd.request("NOTE STA failure testing %d:%s" % (count, func))
51761ba2
JM
1266 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1267 scan_freq="2412", wait_connect=False)
1268 wait_fail_trigger(dev[0], "GET_FAIL")
1269 dev[0].request("REMOVE_NETWORK all")
e96fa197
JM
1270 dev[0].dump_monitor()
1271 hapd.dump_monitor()
28be769b
JM
1272
1273def test_sae_invalid_anti_clogging_token_req(dev, apdev):
1274 """SAE and invalid anti-clogging token request"""
1275 if "SAE" not in dev[0].get_capability("auth_alg"):
1276 raise HwsimSkip("SAE not supported")
1277 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
1278 params['wpa_key_mgmt'] = 'SAE'
77f52098
JM
1279 # Beacon more frequently since Probe Request frames are practically ignored
1280 # in this test setup (ext_mgmt_frame_handled=1 on hostapd side) and
1281 # wpa_supplicant scans may end up getting ignored if no new results are
1282 # available due to the missing Probe Response frames.
1283 params['beacon_int'] = '20'
28be769b
JM
1284 hapd = hostapd.add_ap(apdev[0], params)
1285 bssid = apdev[0]['bssid']
1286
1287 dev[0].request("SET sae_groups 19")
1288 dev[0].scan_for_bss(bssid, freq=2412)
1289 hapd.set("ext_mgmt_frame_handling", "1")
1290 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1291 scan_freq="2412", wait_connect=False)
1292 ev = dev[0].wait_event(["SME: Trying to authenticate"])
1293 if ev is None:
77f52098 1294 raise Exception("No authentication attempt seen (1)")
28be769b
JM
1295 dev[0].dump_monitor()
1296
1297 for i in range(0, 10):
1298 req = hapd.mgmt_rx()
1299 if req is None:
1300 raise Exception("MGMT RX wait timed out (commit)")
1301 if req['subtype'] == 11:
1302 break
1303 req = None
1304 if not req:
1305 raise Exception("Authentication frame (commit) not received")
1306
1307 hapd.dump_monitor()
1308 resp = {}
1309 resp['fc'] = req['fc']
1310 resp['da'] = req['sa']
1311 resp['sa'] = req['da']
1312 resp['bssid'] = req['bssid']
1313 resp['payload'] = binascii.unhexlify("030001004c0013")
1314 hapd.mgmt_tx(resp)
77f52098
JM
1315 ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
1316 if ev is None:
1317 raise Exception("Management frame TX status not reported (1)")
1318 if "stype=11 ok=1" not in ev:
1319 raise Exception("Unexpected management frame TX status (1): " + ev)
28be769b
JM
1320
1321 ev = dev[0].wait_event(["SME: Trying to authenticate"])
1322 if ev is None:
77f52098 1323 raise Exception("No authentication attempt seen (2)")
28be769b
JM
1324 dev[0].dump_monitor()
1325
1326 for i in range(0, 10):
1327 req = hapd.mgmt_rx()
1328 if req is None:
1329 raise Exception("MGMT RX wait timed out (commit) (2)")
1330 if req['subtype'] == 11:
1331 break
1332 req = None
1333 if not req:
1334 raise Exception("Authentication frame (commit) not received (2)")
1335
1336 hapd.dump_monitor()
1337 resp = {}
1338 resp['fc'] = req['fc']
1339 resp['da'] = req['sa']
1340 resp['sa'] = req['da']
1341 resp['bssid'] = req['bssid']
1342 resp['payload'] = binascii.unhexlify("030001000100")
1343 hapd.mgmt_tx(resp)
77f52098
JM
1344 ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
1345 if ev is None:
1346 raise Exception("Management frame TX status not reported (1)")
1347 if "stype=11 ok=1" not in ev:
1348 raise Exception("Unexpected management frame TX status (1): " + ev)
28be769b
JM
1349
1350 ev = dev[0].wait_event(["SME: Trying to authenticate"])
1351 if ev is None:
77f52098 1352 raise Exception("No authentication attempt seen (3)")
28be769b
JM
1353 dev[0].dump_monitor()
1354
1355 dev[0].request("DISCONNECT")
606ef7d3
JM
1356
1357def test_sae_password(dev, apdev):
1358 """SAE and sae_password in hostapd configuration"""
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 passphrase="12345678")
1363 params['wpa_key_mgmt'] = 'SAE WPA-PSK'
1364 params['sae_password'] = "sae-password"
1365 hapd = hostapd.add_ap(apdev[0], params)
1366
1367 dev[0].request("SET sae_groups ")
1368 dev[0].connect("test-sae", psk="sae-password", key_mgmt="SAE",
1369 scan_freq="2412")
1370 dev[1].connect("test-sae", psk="12345678", scan_freq="2412")
1371 dev[2].request("SET sae_groups ")
1372 dev[2].connect("test-sae", sae_password="sae-password", key_mgmt="SAE",
1373 scan_freq="2412")
1374
1375def test_sae_password_short(dev, apdev):
1376 """SAE and short password"""
1377 if "SAE" not in dev[0].get_capability("auth_alg"):
1378 raise HwsimSkip("SAE not supported")
1379 params = hostapd.wpa2_params(ssid="test-sae")
1380 params['wpa_key_mgmt'] = 'SAE'
1381 params['sae_password'] = "secret"
1382 hapd = hostapd.add_ap(apdev[0], params)
1383
1384 dev[0].request("SET sae_groups ")
1385 dev[0].connect("test-sae", sae_password="secret", key_mgmt="SAE",
1386 scan_freq="2412")
1387
1388def test_sae_password_long(dev, apdev):
1389 """SAE and long password"""
1390 if "SAE" not in dev[0].get_capability("auth_alg"):
1391 raise HwsimSkip("SAE not supported")
1392 params = hostapd.wpa2_params(ssid="test-sae")
1393 params['wpa_key_mgmt'] = 'SAE'
1394 params['sae_password'] = 100*"A"
1395 hapd = hostapd.add_ap(apdev[0], params)
1396
1397 dev[0].request("SET sae_groups ")
1398 dev[0].connect("test-sae", sae_password=100*"A", key_mgmt="SAE",
1399 scan_freq="2412")
33822240
JM
1400
1401def test_sae_connect_cmd(dev, apdev):
1402 """SAE with connect command"""
1403 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
1404 wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
1405 if "SAE" not in wpas.get_capability("auth_alg"):
1406 raise HwsimSkip("SAE not supported")
1407 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
1408 params['wpa_key_mgmt'] = 'SAE'
1409 hapd = hostapd.add_ap(apdev[0], params)
1410
1411 wpas.request("SET sae_groups ")
1412 wpas.connect("test-sae", psk="12345678", key_mgmt="SAE",
1413 scan_freq="2412", wait_connect=False)
1414 # mac80211_hwsim does not support SAE offload, so accept both a successful
1415 # connection and association rejection.
1416 ev = wpas.wait_event(["CTRL-EVENT-CONNECTED", "CTRL-EVENT-ASSOC-REJECT",
1417 "Association request to the driver failed"],
1418 timeout=15)
1419 if ev is None:
1420 raise Exception("No connection result reported")
9a0ae89d 1421
be5e7a07 1422def run_sae_password_id(dev, apdev, groups=None):
9a0ae89d
JM
1423 if "SAE" not in dev[0].get_capability("auth_alg"):
1424 raise HwsimSkip("SAE not supported")
1425 params = hostapd.wpa2_params(ssid="test-sae")
1426 params['wpa_key_mgmt'] = 'SAE'
be5e7a07
JM
1427 if groups:
1428 params['sae_groups'] = groups
1429 else:
1430 groups = ""
fab49f61
JM
1431 params['sae_password'] = ['secret|mac=ff:ff:ff:ff:ff:ff|id=pw id',
1432 'foo|mac=02:02:02:02:02:02',
1433 'another secret|mac=ff:ff:ff:ff:ff:ff|id=' + 29*'A']
9a0ae89d
JM
1434 hapd = hostapd.add_ap(apdev[0], params)
1435
be5e7a07 1436 dev[0].request("SET sae_groups " + groups)
9a0ae89d
JM
1437 dev[0].connect("test-sae", sae_password="secret", sae_password_id="pw id",
1438 key_mgmt="SAE", scan_freq="2412")
1439 dev[0].request("REMOVE_NETWORK all")
1440 dev[0].wait_disconnected()
1441
1442 # SAE Password Identifier element with the exact same length as the
1443 # optional Anti-Clogging Token field
1444 dev[0].connect("test-sae", sae_password="another secret",
1445 sae_password_id=29*'A',
1446 key_mgmt="SAE", scan_freq="2412")
1447 dev[0].request("REMOVE_NETWORK all")
1448 dev[0].wait_disconnected()
1449
1450 dev[0].connect("test-sae", sae_password="secret", sae_password_id="unknown",
1451 key_mgmt="SAE", scan_freq="2412", wait_connect=False)
1452
1453 ev = dev[0].wait_event(["CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER"],
1454 timeout=10)
1455 if ev is None:
1456 raise Exception("Unknown password identifier not reported")
1457 dev[0].request("REMOVE_NETWORK all")
1458
be5e7a07
JM
1459def test_sae_password_id(dev, apdev):
1460 """SAE and password identifier"""
1461 run_sae_password_id(dev, apdev, "")
1462
1463def test_sae_password_id_ecc(dev, apdev):
1464 """SAE and password identifier (ECC)"""
1465 run_sae_password_id(dev, apdev, "19")
1466
1467def test_sae_password_id_ffc(dev, apdev):
1468 """SAE and password identifier (FFC)"""
8e607b1b 1469 run_sae_password_id(dev, apdev, "15")
be5e7a07 1470
6de2a809
JM
1471def test_sae_password_id_only(dev, apdev):
1472 """SAE and password identifier (exclusively)"""
1473 if "SAE" not in dev[0].get_capability("auth_alg"):
1474 raise HwsimSkip("SAE not supported")
1475 params = hostapd.wpa2_params(ssid="test-sae")
1476 params['wpa_key_mgmt'] = 'SAE'
1477 params['sae_password'] = 'secret|id=pw id'
1478 hapd = hostapd.add_ap(apdev[0], params)
1479
1480 dev[0].request("SET sae_groups ")
1481 dev[0].connect("test-sae", sae_password="secret", sae_password_id="pw id",
1482 key_mgmt="SAE", scan_freq="2412")
1483
9a0ae89d
JM
1484def test_sae_forced_anti_clogging_pw_id(dev, apdev):
1485 """SAE anti clogging (forced and Password Identifier)"""
1486 if "SAE" not in dev[0].get_capability("auth_alg"):
1487 raise HwsimSkip("SAE not supported")
1488 params = hostapd.wpa2_params(ssid="test-sae")
1489 params['wpa_key_mgmt'] = 'SAE'
1490 params['sae_anti_clogging_threshold'] = '0'
1491 params['sae_password'] = 'secret|id=' + 29*'A'
1492 hostapd.add_ap(apdev[0], params)
1493 for i in range(0, 2):
1494 dev[i].request("SET sae_groups ")
1495 dev[i].connect("test-sae", sae_password="secret",
1496 sae_password_id=29*'A', key_mgmt="SAE", scan_freq="2412")
fe102801
JM
1497
1498def test_sae_reauth(dev, apdev):
1499 """SAE reauthentication"""
1500 if "SAE" not in dev[0].get_capability("auth_alg"):
1501 raise HwsimSkip("SAE not supported")
1502 params = hostapd.wpa2_params(ssid="test-sae",
1503 passphrase="12345678")
1504 params['wpa_key_mgmt'] = 'SAE'
1505 params["ieee80211w"] = "2"
1506 hapd = hostapd.add_ap(apdev[0], params)
1507
1508 dev[0].request("SET sae_groups ")
1509 id = dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1510 ieee80211w="2", scan_freq="2412")
1511
1512 hapd.set("ext_mgmt_frame_handling", "1")
1513 dev[0].request("DISCONNECT")
1514 dev[0].wait_disconnected(timeout=10)
1515 hapd.set("ext_mgmt_frame_handling", "0")
1516 dev[0].request("PMKSA_FLUSH")
1517 dev[0].request("REASSOCIATE")
1518 dev[0].wait_connected(timeout=10, error="Timeout on re-connection")
e43352ff
JM
1519
1520def test_sae_anti_clogging_during_attack(dev, apdev):
1521 """SAE anti clogging during an attack"""
1522 try:
1523 run_sae_anti_clogging_during_attack(dev, apdev)
1524 finally:
3507968f 1525 stop_monitor(apdev[1]["ifname"])
e43352ff
JM
1526
1527def build_sae_commit(bssid, addr, group=21, token=None):
1528 if group == 19:
1529 scalar = binascii.unhexlify("7332d3ebff24804005ccd8c56141e3ed8d84f40638aa31cd2fac11d4d2e89e7b")
1530 element = binascii.unhexlify("954d0f4457066bff3168376a1d7174f4e66620d1792406f613055b98513a7f03a538c13dfbaf2029e2adc6aa96aa0ddcf08ac44887b02f004b7f29b9dbf4b7d9")
1531 elif group == 21:
1532 scalar = binascii.unhexlify("001eec673111b902f5c8a61c8cb4c1c4793031aeea8c8c319410903bc64bcbaea134ab01c4e016d51436f5b5426f7e2af635759a3033fb4031ea79f89a62a3e2f828")
1533 element = binascii.unhexlify("00580eb4b448ea600ea277d5e66e4ed37db82bb04ac90442e9c3727489f366ba4b82f0a472d02caf4cdd142e96baea5915d71374660ee23acbaca38cf3fe8c5fb94b01abbc5278121635d7c06911c5dad8f18d516e1fbe296c179b7c87a1dddfab393337d3d215ed333dd396da6d8f20f798c60d054f1093c24d9c2d98e15c030cc375f0")
1534 pass
1535 frame = binascii.unhexlify("b0003a01")
1536 frame += bssid + addr + bssid
1537 frame += binascii.unhexlify("1000")
1538 auth_alg = 3
1539 transact = 1
1540 status = 0
1541 frame += struct.pack("<HHHH", auth_alg, transact, status, group)
1542 if token:
1543 frame += token
1544 frame += scalar + element
1545 return frame
1546
1547def sae_rx_commit_token_req(sock, radiotap, send_two=False):
1548 msg = sock.recv(1500)
fab49f61 1549 ver, pad, len, present = struct.unpack('<BBHL', msg[0:8])
e43352ff 1550 frame = msg[len:]
fab49f61 1551 fc, duration = struct.unpack('<HH', frame[0:4])
e43352ff
JM
1552 if fc != 0xb0:
1553 return False
1554 frame = frame[4:]
1555 da = frame[0:6]
1556 if da[0] != 0xf2:
1557 return False
1558 sa = frame[6:12]
1559 bssid = frame[12:18]
1560 body = frame[20:]
1561
fab49f61 1562 alg, seq, status, group = struct.unpack('<HHHH', body[0:8])
e43352ff
JM
1563 if alg != 3 or seq != 1 or status != 76:
1564 return False
1565 token = body[8:]
1566
1567 frame = build_sae_commit(bssid, da, token=token)
1568 sock.send(radiotap + frame)
1569 if send_two:
1570 sock.send(radiotap + frame)
1571 return True
1572
1573def run_sae_anti_clogging_during_attack(dev, apdev):
1574 if "SAE" not in dev[0].get_capability("auth_alg"):
1575 raise HwsimSkip("SAE not supported")
1576 params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
1577 params['wpa_key_mgmt'] = 'SAE'
1578 params['sae_groups'] = '21'
1579 hapd = hostapd.add_ap(apdev[0], params)
1580
1581 dev[0].scan_for_bss(hapd.own_addr(), freq=2412)
1582 dev[0].request("SET sae_groups 21")
1583 dev[1].scan_for_bss(hapd.own_addr(), freq=2412)
1584 dev[1].request("SET sae_groups 21")
1585
3507968f
JM
1586 sock = start_monitor(apdev[1]["ifname"])
1587 radiotap = radiotap_build()
e43352ff
JM
1588
1589 bssid = binascii.unhexlify(hapd.own_addr().replace(':', ''))
1590 for i in range(16):
1591 addr = binascii.unhexlify("f2%010x" % i)
1592 frame = build_sae_commit(bssid, addr)
1593 sock.send(radiotap + frame)
1594 sock.send(radiotap + frame)
1595
1596 count = 0
1597 for i in range(150):
1598 if sae_rx_commit_token_req(sock, radiotap, send_two=True):
1599 count += 1
1600 logger.info("Number of token responses sent: %d" % count)
1601 if count < 10:
1602 raise Exception("Too few token responses seen: %d" % count)
1603
1604 for i in range(16):
1605 addr = binascii.unhexlify("f201%08x" % i)
1606 frame = build_sae_commit(bssid, addr)
1607 sock.send(radiotap + frame)
1608
1609 count = 0
1610 for i in range(150):
1611 if sae_rx_commit_token_req(sock, radiotap):
1612 count += 1
1613 if count == 10:
1614 break
a1983aa7 1615 if count < 5:
e43352ff
JM
1616 raise Exception("Too few token responses in second round: %d" % count)
1617
1618 dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
1619 scan_freq="2412", wait_connect=False)
1620 dev[1].connect("test-sae", psk="12345678", key_mgmt="SAE",
1621 scan_freq="2412", wait_connect=False)
1622
1623 count = 0
1624 connected0 = False
1625 connected1 = False
1626 for i in range(1000):
1627 if sae_rx_commit_token_req(sock, radiotap):
1628 count += 1
1629 addr = binascii.unhexlify("f202%08x" % i)
1630 frame = build_sae_commit(bssid, addr)
1631 sock.send(radiotap + frame)
1632 while dev[0].mon.pending():
1633 ev = dev[0].mon.recv()
1634 logger.debug("EV0: " + ev)
1635 if "CTRL-EVENT-CONNECTED" in ev:
1636 connected0 = True
1637 while dev[1].mon.pending():
1638 ev = dev[1].mon.recv()
1639 logger.debug("EV1: " + ev)
1640 if "CTRL-EVENT-CONNECTED" in ev:
1641 connected1 = True
1642 if connected0 and connected1:
1643 break
1644 if not connected0:
1645 raise Exception("Real station(0) did not get connected")
1646 if not connected1:
1647 raise Exception("Real station(1) did not get connected")
1648 if count < 1:
1649 raise Exception("Too few token responses in third round: %d" % count)