]>
Commit | Line | Data |
---|---|---|
c89d9ebb JM |
1 | # WPA2-Personal tests |
2 | # Copyright (c) 2014, Qualcomm Atheros, Inc. | |
3 | # | |
4 | # This software may be distributed under the terms of the BSD license. | |
5 | # See README for more details. | |
6 | ||
9fd6804d | 7 | from remotehost import remote_compatible |
821490f5 | 8 | import binascii |
e0c46c8e | 9 | from Crypto.Cipher import AES |
821490f5 JM |
10 | import hashlib |
11 | import hmac | |
c89d9ebb JM |
12 | import logging |
13 | logger = logging.getLogger() | |
138ec97e | 14 | import os |
5b3c40a6 | 15 | import re |
821490f5 | 16 | import struct |
d1fc5736 JM |
17 | import subprocess |
18 | import time | |
c89d9ebb JM |
19 | |
20 | import hostapd | |
a1eabc74 | 21 | from utils import HwsimSkip, fail_test, skip_with_fips |
fb5c8cea | 22 | import hwsim_utils |
a1512a0c | 23 | from wpasupplicant import WpaSupplicant |
c89d9ebb | 24 | |
eaf3f9b1 JM |
25 | def check_mib(dev, vals): |
26 | mib = dev.get_mib() | |
27 | for v in vals: | |
28 | if mib[v[0]] != v[1]: | |
29 | raise Exception("Unexpected {} = {} (expected {})".format(v[0], mib[v[0]], v[1])) | |
30 | ||
9fd6804d | 31 | @remote_compatible |
c89d9ebb JM |
32 | def test_ap_wpa2_psk(dev, apdev): |
33 | """WPA2-PSK AP with PSK instead of passphrase""" | |
34 | ssid = "test-wpa2-psk" | |
35 | passphrase = 'qwertyuiop' | |
36 | psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6' | |
37 | params = hostapd.wpa2_params(ssid=ssid) | |
38 | params['wpa_psk'] = psk | |
8b8a1864 | 39 | hapd = hostapd.add_ap(apdev[0], params) |
65038313 JM |
40 | key_mgmt = hapd.get_config()['key_mgmt'] |
41 | if key_mgmt.split(' ')[0] != "WPA-PSK": | |
42 | raise Exception("Unexpected GET_CONFIG(key_mgmt): " + key_mgmt) | |
c89d9ebb JM |
43 | dev[0].connect(ssid, raw_psk=psk, scan_freq="2412") |
44 | dev[1].connect(ssid, psk=passphrase, scan_freq="2412") | |
45 | ||
de748924 JM |
46 | sig = dev[0].request("SIGNAL_POLL").splitlines() |
47 | pkt = dev[0].request("PKTCNT_POLL").splitlines() | |
48 | if "FREQUENCY=2412" not in sig: | |
49 | raise Exception("Unexpected SIGNAL_POLL value: " + str(sig)) | |
50 | if "TXBAD=0" not in pkt: | |
51 | raise Exception("Unexpected TXBAD value: " + str(pkt)) | |
52 | ||
c89d9ebb JM |
53 | def test_ap_wpa2_psk_file(dev, apdev): |
54 | """WPA2-PSK AP with PSK from a file""" | |
55 | ssid = "test-wpa2-psk" | |
56 | passphrase = 'qwertyuiop' | |
57 | psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6' | |
58 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
59 | params['wpa_psk_file'] = 'hostapd.wpa_psk' | |
8b8a1864 | 60 | hostapd.add_ap(apdev[0], params) |
c89d9ebb JM |
61 | dev[1].connect(ssid, psk="very secret", scan_freq="2412", wait_connect=False) |
62 | dev[2].connect(ssid, raw_psk=psk, scan_freq="2412") | |
63 | dev[2].request("REMOVE_NETWORK all") | |
64 | dev[0].connect(ssid, psk="very secret", scan_freq="2412") | |
65 | dev[0].request("REMOVE_NETWORK all") | |
66 | dev[2].connect(ssid, psk="another passphrase for all STAs", scan_freq="2412") | |
67 | dev[0].connect(ssid, psk="another passphrase for all STAs", scan_freq="2412") | |
68 | ev = dev[1].wait_event(["WPA: 4-Way Handshake failed"], timeout=10) | |
69 | if ev is None: | |
70 | raise Exception("Timed out while waiting for failure report") | |
71 | dev[1].request("REMOVE_NETWORK all") | |
fb5c8cea | 72 | |
9fd6804d | 73 | @remote_compatible |
53f4ed68 JM |
74 | def test_ap_wpa2_psk_mem(dev, apdev): |
75 | """WPA2-PSK AP with passphrase only in memory""" | |
76 | try: | |
77 | _test_ap_wpa2_psk_mem(dev, apdev) | |
78 | finally: | |
79 | dev[0].request("SCAN_INTERVAL 5") | |
80 | dev[1].request("SCAN_INTERVAL 5") | |
81 | ||
82 | def _test_ap_wpa2_psk_mem(dev, apdev): | |
83 | ssid = "test-wpa2-psk" | |
84 | passphrase = 'qwertyuiop' | |
85 | psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6' | |
86 | params = hostapd.wpa2_params(ssid=ssid) | |
87 | params['wpa_psk'] = psk | |
8b8a1864 | 88 | hapd = hostapd.add_ap(apdev[0], params) |
53f4ed68 JM |
89 | |
90 | dev[0].connect(ssid, mem_only_psk="1", scan_freq="2412", wait_connect=False) | |
91 | dev[0].request("SCAN_INTERVAL 1") | |
92 | ev = dev[0].wait_event(["CTRL-REQ-PSK_PASSPHRASE"], timeout=10) | |
93 | if ev is None: | |
94 | raise Exception("Request for PSK/passphrase timed out") | |
95 | id = ev.split(':')[0].split('-')[-1] | |
96 | dev[0].request("CTRL-RSP-PSK_PASSPHRASE-" + id + ':"' + passphrase + '"') | |
97 | dev[0].wait_connected(timeout=10) | |
98 | ||
99 | dev[1].connect(ssid, mem_only_psk="1", scan_freq="2412", wait_connect=False) | |
100 | dev[1].request("SCAN_INTERVAL 1") | |
101 | ev = dev[1].wait_event(["CTRL-REQ-PSK_PASSPHRASE"], timeout=10) | |
102 | if ev is None: | |
103 | raise Exception("Request for PSK/passphrase timed out(2)") | |
104 | id = ev.split(':')[0].split('-')[-1] | |
105 | dev[1].request("CTRL-RSP-PSK_PASSPHRASE-" + id + ':' + psk) | |
106 | dev[1].wait_connected(timeout=10) | |
107 | ||
9fd6804d | 108 | @remote_compatible |
d1635d97 JM |
109 | def test_ap_wpa2_ptk_rekey(dev, apdev): |
110 | """WPA2-PSK AP and PTK rekey enforced by station""" | |
111 | ssid = "test-wpa2-psk" | |
112 | passphrase = 'qwertyuiop' | |
113 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
8b8a1864 | 114 | hapd = hostapd.add_ap(apdev[0], params) |
d1635d97 JM |
115 | dev[0].connect(ssid, psk=passphrase, wpa_ptk_rekey="1", scan_freq="2412") |
116 | ev = dev[0].wait_event(["WPA: Key negotiation completed"]) | |
117 | if ev is None: | |
118 | raise Exception("PTK rekey timed out") | |
a8375c94 | 119 | hwsim_utils.test_connectivity(dev[0], hapd) |
d1635d97 | 120 | |
9fd6804d | 121 | @remote_compatible |
6c87b4b8 JM |
122 | def test_ap_wpa2_ptk_rekey_ap(dev, apdev): |
123 | """WPA2-PSK AP and PTK rekey enforced by AP""" | |
124 | ssid = "test-wpa2-psk" | |
125 | passphrase = 'qwertyuiop' | |
126 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
127 | params['wpa_ptk_rekey'] = '2' | |
8b8a1864 | 128 | hapd = hostapd.add_ap(apdev[0], params) |
6c87b4b8 JM |
129 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412") |
130 | ev = dev[0].wait_event(["WPA: Key negotiation completed"]) | |
131 | if ev is None: | |
132 | raise Exception("PTK rekey timed out") | |
a8375c94 | 133 | hwsim_utils.test_connectivity(dev[0], hapd) |
6c87b4b8 | 134 | |
9fd6804d | 135 | @remote_compatible |
d1635d97 JM |
136 | def test_ap_wpa2_sha256_ptk_rekey(dev, apdev): |
137 | """WPA2-PSK/SHA256 AKM AP and PTK rekey enforced by station""" | |
138 | ssid = "test-wpa2-psk" | |
139 | passphrase = 'qwertyuiop' | |
140 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
141 | params["wpa_key_mgmt"] = "WPA-PSK-SHA256" | |
8b8a1864 | 142 | hapd = hostapd.add_ap(apdev[0], params) |
d1635d97 JM |
143 | dev[0].connect(ssid, psk=passphrase, key_mgmt="WPA-PSK-SHA256", |
144 | wpa_ptk_rekey="1", scan_freq="2412") | |
145 | ev = dev[0].wait_event(["WPA: Key negotiation completed"]) | |
146 | if ev is None: | |
147 | raise Exception("PTK rekey timed out") | |
a8375c94 | 148 | hwsim_utils.test_connectivity(dev[0], hapd) |
eaf3f9b1 JM |
149 | check_mib(dev[0], [ ("dot11RSNAAuthenticationSuiteRequested", "00-0f-ac-6"), |
150 | ("dot11RSNAAuthenticationSuiteSelected", "00-0f-ac-6") ]) | |
d1635d97 | 151 | |
9fd6804d | 152 | @remote_compatible |
6c87b4b8 JM |
153 | def test_ap_wpa2_sha256_ptk_rekey_ap(dev, apdev): |
154 | """WPA2-PSK/SHA256 AKM AP and PTK rekey enforced by AP""" | |
155 | ssid = "test-wpa2-psk" | |
156 | passphrase = 'qwertyuiop' | |
157 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
158 | params["wpa_key_mgmt"] = "WPA-PSK-SHA256" | |
159 | params['wpa_ptk_rekey'] = '2' | |
8b8a1864 | 160 | hapd = hostapd.add_ap(apdev[0], params) |
6c87b4b8 JM |
161 | dev[0].connect(ssid, psk=passphrase, key_mgmt="WPA-PSK-SHA256", |
162 | scan_freq="2412") | |
163 | ev = dev[0].wait_event(["WPA: Key negotiation completed"]) | |
164 | if ev is None: | |
165 | raise Exception("PTK rekey timed out") | |
a8375c94 | 166 | hwsim_utils.test_connectivity(dev[0], hapd) |
6c87b4b8 JM |
167 | check_mib(dev[0], [ ("dot11RSNAAuthenticationSuiteRequested", "00-0f-ac-6"), |
168 | ("dot11RSNAAuthenticationSuiteSelected", "00-0f-ac-6") ]) | |
169 | ||
9fd6804d | 170 | @remote_compatible |
fb5c8cea JM |
171 | def test_ap_wpa_ptk_rekey(dev, apdev): |
172 | """WPA-PSK/TKIP AP and PTK rekey enforced by station""" | |
a1eabc74 | 173 | skip_with_fips(dev[0]) |
fb5c8cea JM |
174 | ssid = "test-wpa-psk" |
175 | passphrase = 'qwertyuiop' | |
176 | params = hostapd.wpa_params(ssid=ssid, passphrase=passphrase) | |
8b8a1864 | 177 | hapd = hostapd.add_ap(apdev[0], params) |
fb5c8cea | 178 | dev[0].connect(ssid, psk=passphrase, wpa_ptk_rekey="1", scan_freq="2412") |
91bc6c36 JM |
179 | if "[WPA-PSK-TKIP]" not in dev[0].request("SCAN_RESULTS"): |
180 | raise Exception("Scan results missing WPA element info") | |
fb5c8cea JM |
181 | ev = dev[0].wait_event(["WPA: Key negotiation completed"]) |
182 | if ev is None: | |
183 | raise Exception("PTK rekey timed out") | |
a8375c94 | 184 | hwsim_utils.test_connectivity(dev[0], hapd) |
138ec97e | 185 | |
9fd6804d | 186 | @remote_compatible |
6c87b4b8 JM |
187 | def test_ap_wpa_ptk_rekey_ap(dev, apdev): |
188 | """WPA-PSK/TKIP AP and PTK rekey enforced by AP""" | |
a1eabc74 | 189 | skip_with_fips(dev[0]) |
6c87b4b8 JM |
190 | ssid = "test-wpa-psk" |
191 | passphrase = 'qwertyuiop' | |
192 | params = hostapd.wpa_params(ssid=ssid, passphrase=passphrase) | |
193 | params['wpa_ptk_rekey'] = '2' | |
8b8a1864 | 194 | hapd = hostapd.add_ap(apdev[0], params) |
6c87b4b8 JM |
195 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412") |
196 | ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=10) | |
197 | if ev is None: | |
198 | raise Exception("PTK rekey timed out") | |
a8375c94 | 199 | hwsim_utils.test_connectivity(dev[0], hapd) |
6c87b4b8 | 200 | |
9fd6804d | 201 | @remote_compatible |
12124240 JM |
202 | def test_ap_wpa_ccmp(dev, apdev): |
203 | """WPA-PSK/CCMP""" | |
204 | ssid = "test-wpa-psk" | |
205 | passphrase = 'qwertyuiop' | |
206 | params = hostapd.wpa_params(ssid=ssid, passphrase=passphrase) | |
207 | params['wpa_pairwise'] = "CCMP" | |
8b8a1864 | 208 | hapd = hostapd.add_ap(apdev[0], params) |
12124240 | 209 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412") |
a8375c94 | 210 | hwsim_utils.test_connectivity(dev[0], hapd) |
eaf3f9b1 JM |
211 | check_mib(dev[0], [ ("dot11RSNAConfigGroupCipherSize", "128"), |
212 | ("dot11RSNAGroupCipherRequested", "00-50-f2-4"), | |
213 | ("dot11RSNAPairwiseCipherRequested", "00-50-f2-4"), | |
214 | ("dot11RSNAAuthenticationSuiteRequested", "00-50-f2-2"), | |
215 | ("dot11RSNAGroupCipherSelected", "00-50-f2-4"), | |
216 | ("dot11RSNAPairwiseCipherSelected", "00-50-f2-4"), | |
217 | ("dot11RSNAAuthenticationSuiteSelected", "00-50-f2-2"), | |
218 | ("dot1xSuppSuppControlledPortStatus", "Authorized") ]) | |
12124240 | 219 | |
79f846a7 | 220 | def test_ap_wpa2_psk_file_errors(dev, apdev): |
138ec97e | 221 | """WPA2-PSK AP with various PSK file error and success cases""" |
18945a8c B |
222 | addr0 = dev[0].own_addr() |
223 | addr1 = dev[1].own_addr() | |
224 | addr2 = dev[2].own_addr() | |
138ec97e JM |
225 | ssid = "psk" |
226 | pskfile = "/tmp/ap_wpa2_psk_file_errors.psk_file" | |
227 | try: | |
228 | os.remove(pskfile) | |
229 | except: | |
230 | pass | |
231 | ||
232 | params = { "ssid": ssid, "wpa": "2", "wpa_key_mgmt": "WPA-PSK", | |
233 | "rsn_pairwise": "CCMP", "wpa_psk_file": pskfile } | |
234 | ||
235 | try: | |
236 | # missing PSK file | |
8b8a1864 | 237 | hapd = hostapd.add_ap(apdev[0], params, no_enable=True) |
138ec97e JM |
238 | if "FAIL" not in hapd.request("ENABLE"): |
239 | raise Exception("Unexpected ENABLE success") | |
240 | hapd.request("DISABLE") | |
241 | ||
242 | # invalid MAC address | |
243 | with open(pskfile, "w") as f: | |
244 | f.write("\n") | |
245 | f.write("foo\n") | |
246 | if "FAIL" not in hapd.request("ENABLE"): | |
247 | raise Exception("Unexpected ENABLE success") | |
248 | hapd.request("DISABLE") | |
249 | ||
250 | # no PSK on line | |
251 | with open(pskfile, "w") as f: | |
252 | f.write("00:11:22:33:44:55\n") | |
253 | if "FAIL" not in hapd.request("ENABLE"): | |
254 | raise Exception("Unexpected ENABLE success") | |
255 | hapd.request("DISABLE") | |
256 | ||
257 | # invalid PSK | |
258 | with open(pskfile, "w") as f: | |
259 | f.write("00:11:22:33:44:55 1234567\n") | |
260 | if "FAIL" not in hapd.request("ENABLE"): | |
261 | raise Exception("Unexpected ENABLE success") | |
262 | hapd.request("DISABLE") | |
263 | ||
264 | # valid PSK file | |
265 | with open(pskfile, "w") as f: | |
266 | f.write("00:11:22:33:44:55 12345678\n") | |
267 | f.write(addr0 + " 123456789\n") | |
268 | f.write(addr1 + " 123456789a\n") | |
269 | f.write(addr2 + " 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\n") | |
270 | if "FAIL" in hapd.request("ENABLE"): | |
271 | raise Exception("Unexpected ENABLE failure") | |
272 | ||
273 | dev[0].connect(ssid, psk="123456789", scan_freq="2412") | |
274 | dev[1].connect(ssid, psk="123456789a", scan_freq="2412") | |
275 | dev[2].connect(ssid, raw_psk="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", scan_freq="2412") | |
276 | ||
277 | finally: | |
278 | try: | |
279 | os.remove(pskfile) | |
280 | except: | |
281 | pass | |
6796e502 | 282 | |
9fd6804d | 283 | @remote_compatible |
6796e502 JM |
284 | def test_ap_wpa2_psk_wildcard_ssid(dev, apdev): |
285 | """WPA2-PSK AP and wildcard SSID configuration""" | |
286 | ssid = "test-wpa2-psk" | |
287 | passphrase = 'qwertyuiop' | |
288 | psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6' | |
289 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
8b8a1864 | 290 | hapd = hostapd.add_ap(apdev[0], params) |
6796e502 JM |
291 | dev[0].connect("", bssid=apdev[0]['bssid'], psk=passphrase, |
292 | scan_freq="2412") | |
293 | dev[1].connect("", bssid=apdev[0]['bssid'], raw_psk=psk, scan_freq="2412") | |
3b25ad4c | 294 | |
9fd6804d | 295 | @remote_compatible |
3b25ad4c JM |
296 | def test_ap_wpa2_gtk_rekey(dev, apdev): |
297 | """WPA2-PSK AP and GTK rekey enforced by AP""" | |
298 | ssid = "test-wpa2-psk" | |
299 | passphrase = 'qwertyuiop' | |
300 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
301 | params['wpa_group_rekey'] = '1' | |
8b8a1864 | 302 | hapd = hostapd.add_ap(apdev[0], params) |
3b25ad4c JM |
303 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412") |
304 | ev = dev[0].wait_event(["WPA: Group rekeying completed"], timeout=2) | |
305 | if ev is None: | |
306 | raise Exception("GTK rekey timed out") | |
a8375c94 | 307 | hwsim_utils.test_connectivity(dev[0], hapd) |
3b25ad4c | 308 | |
9fd6804d | 309 | @remote_compatible |
3b25ad4c JM |
310 | def test_ap_wpa_gtk_rekey(dev, apdev): |
311 | """WPA-PSK/TKIP AP and GTK rekey enforced by AP""" | |
a1eabc74 | 312 | skip_with_fips(dev[0]) |
3b25ad4c JM |
313 | ssid = "test-wpa-psk" |
314 | passphrase = 'qwertyuiop' | |
315 | params = hostapd.wpa_params(ssid=ssid, passphrase=passphrase) | |
316 | params['wpa_group_rekey'] = '1' | |
8b8a1864 | 317 | hapd = hostapd.add_ap(apdev[0], params) |
3b25ad4c JM |
318 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412") |
319 | ev = dev[0].wait_event(["WPA: Group rekeying completed"], timeout=2) | |
320 | if ev is None: | |
321 | raise Exception("GTK rekey timed out") | |
a8375c94 | 322 | hwsim_utils.test_connectivity(dev[0], hapd) |
3b25ad4c | 323 | |
9fd6804d | 324 | @remote_compatible |
3b25ad4c JM |
325 | def test_ap_wpa2_gmk_rekey(dev, apdev): |
326 | """WPA2-PSK AP and GMK and GTK rekey enforced by AP""" | |
327 | ssid = "test-wpa2-psk" | |
328 | passphrase = 'qwertyuiop' | |
329 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
330 | params['wpa_group_rekey'] = '1' | |
331 | params['wpa_gmk_rekey'] = '2' | |
8b8a1864 | 332 | hapd = hostapd.add_ap(apdev[0], params) |
3b25ad4c JM |
333 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412") |
334 | for i in range(0, 3): | |
335 | ev = dev[0].wait_event(["WPA: Group rekeying completed"], timeout=2) | |
336 | if ev is None: | |
337 | raise Exception("GTK rekey timed out") | |
a8375c94 | 338 | hwsim_utils.test_connectivity(dev[0], hapd) |
3b25ad4c | 339 | |
9fd6804d | 340 | @remote_compatible |
3b25ad4c JM |
341 | def test_ap_wpa2_strict_rekey(dev, apdev): |
342 | """WPA2-PSK AP and strict GTK rekey enforced by AP""" | |
343 | ssid = "test-wpa2-psk" | |
344 | passphrase = 'qwertyuiop' | |
345 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
346 | params['wpa_strict_rekey'] = '1' | |
8b8a1864 | 347 | hapd = hostapd.add_ap(apdev[0], params) |
3b25ad4c JM |
348 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412") |
349 | dev[1].connect(ssid, psk=passphrase, scan_freq="2412") | |
350 | dev[1].request("DISCONNECT") | |
351 | ev = dev[0].wait_event(["WPA: Group rekeying completed"], timeout=2) | |
352 | if ev is None: | |
353 | raise Exception("GTK rekey timed out") | |
a8375c94 | 354 | hwsim_utils.test_connectivity(dev[0], hapd) |
d1fc5736 | 355 | |
9fd6804d | 356 | @remote_compatible |
d1fc5736 JM |
357 | def test_ap_wpa2_bridge_fdb(dev, apdev): |
358 | """Bridge FDB entry removal""" | |
bb04a9a9 | 359 | hapd = None |
d1fc5736 JM |
360 | try: |
361 | ssid = "test-wpa2-psk" | |
362 | passphrase = "12345678" | |
363 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
364 | params['bridge'] = 'ap-br0' | |
bb04a9a9 JA |
365 | hapd = hostapd.add_ap(apdev[0], params) |
366 | hapd.cmd_execute(['brctl', 'setfd', 'ap-br0', '0']) | |
367 | hapd.cmd_execute(['ip', 'link', 'set', 'dev', 'ap-br0', 'up']) | |
d1fc5736 JM |
368 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412", |
369 | bssid=apdev[0]['bssid']) | |
370 | dev[1].connect(ssid, psk=passphrase, scan_freq="2412", | |
371 | bssid=apdev[0]['bssid']) | |
372 | addr0 = dev[0].p2p_interface_addr() | |
373 | hwsim_utils.test_connectivity_sta(dev[0], dev[1]) | |
bb04a9a9 JA |
374 | err, macs1 = hapd.cmd_execute(['brctl', 'showmacs', 'ap-br0']) |
375 | hapd.cmd_execute(['brctl', 'setageing', 'ap-br0', '1']) | |
d1fc5736 JM |
376 | dev[0].request("DISCONNECT") |
377 | dev[1].request("DISCONNECT") | |
378 | time.sleep(1) | |
bb04a9a9 | 379 | err, macs2 = hapd.cmd_execute(['brctl', 'showmacs', 'ap-br0']) |
d1fc5736 JM |
380 | |
381 | addr1 = dev[1].p2p_interface_addr() | |
382 | if addr0 not in macs1 or addr1 not in macs1: | |
383 | raise Exception("Bridge FDB entry missing") | |
384 | if addr0 in macs2 or addr1 in macs2: | |
385 | raise Exception("Bridge FDB entry was not removed") | |
386 | finally: | |
bb04a9a9 JA |
387 | hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev', 'ap-br0', |
388 | 'down']) | |
389 | hostapd.cmd_execute(apdev[0], ['brctl', 'delbr', 'ap-br0']) | |
cf0b9c86 | 390 | |
9fd6804d | 391 | @remote_compatible |
8619c334 JM |
392 | def test_ap_wpa2_already_in_bridge(dev, apdev): |
393 | """hostapd behavior with interface already in bridge""" | |
394 | ifname = apdev[0]['ifname'] | |
395 | br_ifname = 'ext-ap-br0' | |
396 | try: | |
397 | ssid = "test-wpa2-psk" | |
398 | passphrase = "12345678" | |
bb04a9a9 JA |
399 | hostapd.cmd_execute(apdev[0], ['brctl', 'addbr', br_ifname]) |
400 | hostapd.cmd_execute(apdev[0], ['brctl', 'setfd', br_ifname, '0']) | |
401 | hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev', br_ifname, | |
402 | 'up']) | |
403 | hostapd.cmd_execute(apdev[0], ['iw', ifname, 'set', 'type', '__ap']) | |
404 | hostapd.cmd_execute(apdev[0], ['brctl', 'addif', br_ifname, ifname]) | |
8619c334 | 405 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) |
41ba40e7 | 406 | hapd = hostapd.add_ap(apdev[0], params) |
8619c334 JM |
407 | if hapd.get_driver_status_field('brname') != br_ifname: |
408 | raise Exception("Bridge name not identified correctly") | |
409 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412") | |
410 | finally: | |
bb04a9a9 JA |
411 | hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev', br_ifname, |
412 | 'down']) | |
413 | hostapd.cmd_execute(apdev[0], ['brctl', 'delif', br_ifname, ifname]) | |
414 | hostapd.cmd_execute(apdev[0], ['iw', ifname, 'set', 'type', 'station']) | |
415 | hostapd.cmd_execute(apdev[0], ['brctl', 'delbr', br_ifname]) | |
8619c334 | 416 | |
9fd6804d | 417 | @remote_compatible |
542452a9 JM |
418 | def test_ap_wpa2_in_different_bridge(dev, apdev): |
419 | """hostapd behavior with interface in different bridge""" | |
420 | ifname = apdev[0]['ifname'] | |
421 | br_ifname = 'ext-ap-br0' | |
422 | try: | |
423 | ssid = "test-wpa2-psk" | |
424 | passphrase = "12345678" | |
bb04a9a9 JA |
425 | hostapd.cmd_execute(apdev[0], ['brctl', 'addbr', br_ifname]) |
426 | hostapd.cmd_execute(apdev[0], ['brctl', 'setfd', br_ifname, '0']) | |
427 | hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev', br_ifname, | |
428 | 'up']) | |
429 | hostapd.cmd_execute(apdev[0], ['iw', ifname, 'set', 'type', '__ap']) | |
430 | hostapd.cmd_execute(apdev[0], ['brctl', 'addif', br_ifname, ifname]) | |
542452a9 JM |
431 | time.sleep(0.5) |
432 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
433 | params['bridge'] = 'ap-br0' | |
41ba40e7 | 434 | hapd = hostapd.add_ap(apdev[0], params) |
bb04a9a9 JA |
435 | hostapd.cmd_execute(apdev[0], ['brctl', 'setfd', 'ap-br0', '0']) |
436 | hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev', 'ap-br0', | |
437 | 'up']) | |
542452a9 JM |
438 | brname = hapd.get_driver_status_field('brname') |
439 | if brname != 'ap-br0': | |
440 | raise Exception("Incorrect bridge: " + brname) | |
441 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412") | |
442 | hwsim_utils.test_connectivity_iface(dev[0], hapd, "ap-br0") | |
443 | if hapd.get_driver_status_field("added_bridge") != "1": | |
444 | raise Exception("Unexpected added_bridge value") | |
445 | if hapd.get_driver_status_field("added_if_into_bridge") != "1": | |
446 | raise Exception("Unexpected added_if_into_bridge value") | |
447 | dev[0].request("DISCONNECT") | |
448 | hapd.disable() | |
542452a9 | 449 | finally: |
bb04a9a9 JA |
450 | hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev', br_ifname, |
451 | 'down']) | |
452 | hostapd.cmd_execute(apdev[0], ['brctl', 'delif', br_ifname, ifname, | |
453 | "2>", "/dev/null"], shell=True) | |
454 | hostapd.cmd_execute(apdev[0], ['brctl', 'delbr', br_ifname]) | |
542452a9 | 455 | |
9fd6804d | 456 | @remote_compatible |
8619c334 JM |
457 | def test_ap_wpa2_ext_add_to_bridge(dev, apdev): |
458 | """hostapd behavior with interface added to bridge externally""" | |
459 | ifname = apdev[0]['ifname'] | |
460 | br_ifname = 'ext-ap-br0' | |
461 | try: | |
462 | ssid = "test-wpa2-psk" | |
463 | passphrase = "12345678" | |
464 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
41ba40e7 | 465 | hapd = hostapd.add_ap(apdev[0], params) |
8619c334 | 466 | |
bb04a9a9 JA |
467 | hostapd.cmd_execute(apdev[0], ['brctl', 'addbr', br_ifname]) |
468 | hostapd.cmd_execute(apdev[0], ['brctl', 'setfd', br_ifname, '0']) | |
469 | hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev', br_ifname, | |
470 | 'up']) | |
471 | hostapd.cmd_execute(apdev[0], ['brctl', 'addif', br_ifname, ifname]) | |
8619c334 JM |
472 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412") |
473 | if hapd.get_driver_status_field('brname') != br_ifname: | |
474 | raise Exception("Bridge name not identified correctly") | |
475 | finally: | |
bb04a9a9 JA |
476 | hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev', br_ifname, |
477 | 'down']) | |
478 | hostapd.cmd_execute(apdev[0], ['brctl', 'delif', br_ifname, ifname]) | |
479 | hostapd.cmd_execute(apdev[0], ['brctl', 'delbr', br_ifname]) | |
8619c334 | 480 | |
cf0b9c86 JM |
481 | def test_ap_wpa2_psk_ext(dev, apdev): |
482 | """WPA2-PSK AP using external EAPOL I/O""" | |
483 | bssid = apdev[0]['bssid'] | |
484 | ssid = "test-wpa2-psk" | |
485 | passphrase = 'qwertyuiop' | |
486 | psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6' | |
487 | params = hostapd.wpa2_params(ssid=ssid) | |
488 | params['wpa_psk'] = psk | |
8b8a1864 | 489 | hapd = hostapd.add_ap(apdev[0], params) |
cf0b9c86 JM |
490 | hapd.request("SET ext_eapol_frame_io 1") |
491 | dev[0].request("SET ext_eapol_frame_io 1") | |
492 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False) | |
493 | addr = dev[0].p2p_interface_addr() | |
494 | while True: | |
495 | ev = hapd.wait_event(["EAPOL-TX", "AP-STA-CONNECTED"], timeout=15) | |
496 | if ev is None: | |
497 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
498 | if "AP-STA-CONNECTED" in ev: | |
5f35a5e2 | 499 | dev[0].wait_connected(timeout=15) |
cf0b9c86 JM |
500 | break |
501 | res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2]) | |
502 | if "OK" not in res: | |
503 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
504 | ev = dev[0].wait_event(["EAPOL-TX", "CTRL-EVENT-CONNECTED"], timeout=15) | |
505 | if ev is None: | |
506 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
507 | if "CTRL-EVENT-CONNECTED" in ev: | |
508 | break | |
509 | res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2]) | |
510 | if "OK" not in res: | |
511 | raise Exception("EAPOL_RX to hostapd failed") | |
821490f5 | 512 | |
242339de JM |
513 | def test_ap_wpa2_psk_ext_retry_msg_3(dev, apdev): |
514 | """WPA2-PSK AP using external EAPOL I/O and retry for EAPOL-Key msg 3/4""" | |
515 | bssid = apdev[0]['bssid'] | |
516 | ssid = "test-wpa2-psk" | |
517 | passphrase = 'qwertyuiop' | |
518 | psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6' | |
519 | params = hostapd.wpa2_params(ssid=ssid) | |
520 | params['wpa_psk'] = psk | |
8b8a1864 | 521 | hapd = hostapd.add_ap(apdev[0], params) |
242339de JM |
522 | hapd.request("SET ext_eapol_frame_io 1") |
523 | dev[0].request("SET ext_eapol_frame_io 1") | |
524 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False) | |
525 | addr = dev[0].p2p_interface_addr() | |
526 | ||
527 | # EAPOL-Key msg 1/4 | |
528 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
529 | if ev is None: | |
530 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
531 | res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2]) | |
532 | if "OK" not in res: | |
533 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
534 | ||
535 | # EAPOL-Key msg 2/4 | |
536 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=15) | |
537 | if ev is None: | |
538 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
539 | res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2]) | |
540 | if "OK" not in res: | |
541 | raise Exception("EAPOL_RX to hostapd failed") | |
542 | ||
543 | # EAPOL-Key msg 3/4 | |
544 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
545 | if ev is None: | |
546 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
547 | res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2]) | |
548 | if "OK" not in res: | |
549 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
550 | ||
551 | # EAPOL-Key msg 4/4 | |
552 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=15) | |
553 | if ev is None: | |
554 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
555 | # Do not send to the AP | |
556 | dev[0].wait_connected(timeout=15) | |
557 | ||
558 | # EAPOL-Key msg 3/4 (retry) | |
559 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
560 | if ev is None: | |
561 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
562 | res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2]) | |
563 | if "OK" not in res: | |
564 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
565 | ||
566 | # EAPOL-Key msg 4/4 | |
567 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=15) | |
568 | if ev is None: | |
569 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
570 | res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2]) | |
571 | if "OK" not in res: | |
572 | raise Exception("EAPOL_RX to hostapd failed") | |
573 | ||
574 | ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15) | |
575 | if ev is None: | |
576 | raise Exception("Timeout on AP-STA-CONNECTED from hostapd") | |
577 | ||
578 | hwsim_utils.test_connectivity(dev[0], hapd) | |
579 | ||
c29475a9 JM |
580 | def test_ap_wpa2_psk_ext_retry_msg_3b(dev, apdev): |
581 | """WPA2-PSK AP using external EAPOL I/O and retry for EAPOL-Key msg 3/4 (b)""" | |
582 | bssid = apdev[0]['bssid'] | |
583 | ssid = "test-wpa2-psk" | |
584 | passphrase = 'qwertyuiop' | |
585 | psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6' | |
586 | params = hostapd.wpa2_params(ssid=ssid) | |
587 | params['wpa_psk'] = psk | |
588 | hapd = hostapd.add_ap(apdev[0], params) | |
589 | hapd.request("SET ext_eapol_frame_io 1") | |
590 | dev[0].request("SET ext_eapol_frame_io 1") | |
591 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False) | |
592 | addr = dev[0].p2p_interface_addr() | |
593 | ||
594 | # EAPOL-Key msg 1/4 | |
595 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
596 | if ev is None: | |
597 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
598 | res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2]) | |
599 | if "OK" not in res: | |
600 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
601 | ||
602 | # EAPOL-Key msg 2/4 | |
603 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=15) | |
604 | if ev is None: | |
605 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
606 | res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2]) | |
607 | if "OK" not in res: | |
608 | raise Exception("EAPOL_RX to hostapd failed") | |
609 | ||
610 | # EAPOL-Key msg 3/4 | |
611 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
612 | if ev is None: | |
613 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
614 | # Do not send the first msg 3/4 to the STA yet; wait for retransmission | |
615 | # from AP. | |
616 | msg3_1 = ev | |
617 | ||
618 | # EAPOL-Key msg 3/4 (retry) | |
619 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
620 | if ev is None: | |
621 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
622 | msg3_2 = ev | |
623 | ||
624 | # Send the first msg 3/4 to STA | |
625 | res = dev[0].request("EAPOL_RX " + bssid + " " + msg3_1.split(' ')[2]) | |
626 | if "OK" not in res: | |
627 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
628 | ||
629 | # EAPOL-Key msg 4/4 | |
630 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=15) | |
631 | if ev is None: | |
632 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
633 | res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2]) | |
634 | if "OK" not in res: | |
635 | raise Exception("EAPOL_RX to hostapd failed") | |
636 | dev[0].wait_connected(timeout=15) | |
637 | ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15) | |
638 | if ev is None: | |
639 | raise Exception("Timeout on AP-STA-CONNECTED from hostapd") | |
640 | ||
641 | hwsim_utils.test_connectivity(dev[0], hapd) | |
642 | ||
643 | # Send the second msg 3/4 to STA | |
644 | res = dev[0].request("EAPOL_RX " + bssid + " " + msg3_2.split(' ')[2]) | |
645 | if "OK" not in res: | |
646 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
647 | # EAPOL-Key msg 4/4 | |
648 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=15) | |
649 | if ev is None: | |
650 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
651 | # Do not send the second msg 4/4 to the AP | |
652 | ||
653 | hwsim_utils.test_connectivity(dev[0], hapd) | |
654 | ||
f4528fbf JM |
655 | def test_ap_wpa2_psk_ext_retry_msg_3c(dev, apdev): |
656 | """WPA2-PSK AP using external EAPOL I/O and retry for EAPOL-Key msg 3/4 (c)""" | |
657 | bssid = apdev[0]['bssid'] | |
658 | ssid = "test-wpa2-psk" | |
659 | passphrase = 'qwertyuiop' | |
660 | psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6' | |
661 | params = hostapd.wpa2_params(ssid=ssid) | |
662 | params['wpa_psk'] = psk | |
663 | hapd = hostapd.add_ap(apdev[0], params) | |
664 | hapd.request("SET ext_eapol_frame_io 1") | |
665 | dev[0].request("SET ext_eapol_frame_io 1") | |
666 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False) | |
667 | addr = dev[0].p2p_interface_addr() | |
668 | ||
669 | # EAPOL-Key msg 1/4 | |
670 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
671 | if ev is None: | |
672 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
673 | msg1 = ev.split(' ')[2] | |
674 | res = dev[0].request("EAPOL_RX " + bssid + " " + msg1) | |
675 | if "OK" not in res: | |
676 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
677 | ||
678 | # EAPOL-Key msg 2/4 | |
679 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=15) | |
680 | if ev is None: | |
681 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
682 | res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2]) | |
683 | if "OK" not in res: | |
684 | raise Exception("EAPOL_RX to hostapd failed") | |
685 | ||
686 | # EAPOL-Key msg 3/4 | |
687 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
688 | if ev is None: | |
689 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
690 | res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2]) | |
691 | if "OK" not in res: | |
692 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
693 | ||
694 | # EAPOL-Key msg 4/4 | |
695 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=15) | |
696 | if ev is None: | |
697 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
698 | msg4 = ev.split(' ')[2] | |
699 | # Do not send msg 4/4 to hostapd to trigger retry | |
700 | ||
701 | # STA believes everything is ready | |
702 | dev[0].wait_connected() | |
703 | ||
704 | # EAPOL-Key msg 3/4 (retry) | |
705 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
706 | if ev is None: | |
707 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
708 | msg3 = ev.split(' ')[2] | |
709 | ||
710 | # Send a forged msg 1/4 to STA (update replay counter) | |
711 | msg1b = msg1[0:18] + msg3[18:34] + msg1[34:] | |
712 | # and replace nonce (this results in "WPA: ANonce from message 1 of | |
713 | # 4-Way Handshake differs from 3 of 4-Way Handshake - drop packet" when | |
714 | # wpa_supplicant processed msg 3/4 afterwards) | |
715 | #msg1b = msg1[0:18] + msg3[18:34] + 32*"ff" + msg1[98:] | |
716 | res = dev[0].request("EAPOL_RX " + bssid + " " + msg1b) | |
717 | if "OK" not in res: | |
718 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
719 | # EAPOL-Key msg 2/4 | |
720 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=1) | |
721 | if ev is None: | |
722 | # wpa_supplicant seems to have ignored the forged message. This means | |
723 | # the attack would fail. | |
724 | logger.info("wpa_supplicant ignored forged EAPOL-Key msg 1/4") | |
725 | return | |
726 | # Do not send msg 2/4 to hostapd | |
727 | ||
728 | # Send previously received msg 3/4 to STA | |
729 | res = dev[0].request("EAPOL_RX " + bssid + " " + msg3) | |
730 | if "OK" not in res: | |
731 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
732 | ||
733 | # EAPOL-Key msg 4/4 | |
734 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=15) | |
735 | if ev is None: | |
736 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
737 | res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2]) | |
738 | if "OK" not in res: | |
739 | raise Exception("EAPOL_RX to hostapd failed") | |
740 | ||
741 | ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15) | |
742 | if ev is None: | |
743 | raise Exception("Timeout on AP-STA-CONNECTED from hostapd") | |
744 | ||
745 | hwsim_utils.test_connectivity(dev[0], hapd) | |
746 | ||
747 | def test_ap_wpa2_psk_ext_retry_msg_3d(dev, apdev): | |
748 | """WPA2-PSK AP using external EAPOL I/O and retry for EAPOL-Key msg 3/4 (d)""" | |
749 | bssid = apdev[0]['bssid'] | |
750 | ssid = "test-wpa2-psk" | |
751 | passphrase = 'qwertyuiop' | |
752 | psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6' | |
753 | params = hostapd.wpa2_params(ssid=ssid) | |
754 | params['wpa_psk'] = psk | |
755 | hapd = hostapd.add_ap(apdev[0], params) | |
756 | hapd.request("SET ext_eapol_frame_io 1") | |
757 | dev[0].request("SET ext_eapol_frame_io 1") | |
758 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False) | |
759 | addr = dev[0].p2p_interface_addr() | |
760 | ||
761 | # EAPOL-Key msg 1/4 | |
762 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
763 | if ev is None: | |
764 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
765 | msg1 = ev.split(' ')[2] | |
766 | res = dev[0].request("EAPOL_RX " + bssid + " " + msg1) | |
767 | if "OK" not in res: | |
768 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
769 | ||
770 | # EAPOL-Key msg 2/4 | |
771 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=15) | |
772 | if ev is None: | |
773 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
774 | res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2]) | |
775 | if "OK" not in res: | |
776 | raise Exception("EAPOL_RX to hostapd failed") | |
777 | ||
778 | # EAPOL-Key msg 3/4 | |
779 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
780 | if ev is None: | |
781 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
782 | res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2]) | |
783 | if "OK" not in res: | |
784 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
785 | ||
786 | # EAPOL-Key msg 4/4 | |
787 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=15) | |
788 | if ev is None: | |
789 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
790 | msg4 = ev.split(' ')[2] | |
791 | # Do not send msg 4/4 to hostapd to trigger retry | |
792 | ||
793 | # STA believes everything is ready | |
794 | dev[0].wait_connected() | |
795 | ||
796 | # EAPOL-Key msg 3/4 (retry) | |
797 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
798 | if ev is None: | |
799 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
800 | msg3 = ev.split(' ')[2] | |
801 | ||
802 | # Send a forged msg 1/4 to STA (update replay counter) | |
803 | msg1b = msg1[0:18] + msg3[18:34] + msg1[34:] | |
804 | res = dev[0].request("EAPOL_RX " + bssid + " " + msg1b) | |
805 | if "OK" not in res: | |
806 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
807 | # EAPOL-Key msg 2/4 | |
808 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=1) | |
809 | if ev is None: | |
810 | # wpa_supplicant seems to have ignored the forged message. This means | |
811 | # the attack would fail. | |
812 | logger.info("wpa_supplicant ignored forged EAPOL-Key msg 1/4") | |
813 | return | |
814 | # Do not send msg 2/4 to hostapd | |
815 | ||
816 | # EAPOL-Key msg 3/4 (retry 2) | |
817 | # New one needed to get the correct Replay Counter value | |
818 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
819 | if ev is None: | |
820 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
821 | msg3 = ev.split(' ')[2] | |
822 | ||
823 | # Send msg 3/4 to STA | |
824 | res = dev[0].request("EAPOL_RX " + bssid + " " + msg3) | |
825 | if "OK" not in res: | |
826 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
827 | ||
828 | # EAPOL-Key msg 4/4 | |
829 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=15) | |
830 | if ev is None: | |
831 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
832 | res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2]) | |
833 | if "OK" not in res: | |
834 | raise Exception("EAPOL_RX to hostapd failed") | |
835 | ||
836 | ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15) | |
837 | if ev is None: | |
838 | raise Exception("Timeout on AP-STA-CONNECTED from hostapd") | |
839 | ||
840 | hwsim_utils.test_connectivity(dev[0], hapd) | |
841 | ||
842 | def test_ap_wpa2_psk_ext_retry_msg_3e(dev, apdev): | |
843 | """WPA2-PSK AP using external EAPOL I/O and retry for EAPOL-Key msg 3/4 (e)""" | |
844 | bssid = apdev[0]['bssid'] | |
845 | ssid = "test-wpa2-psk" | |
846 | passphrase = 'qwertyuiop' | |
847 | psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6' | |
848 | params = hostapd.wpa2_params(ssid=ssid) | |
849 | params['wpa_psk'] = psk | |
850 | hapd = hostapd.add_ap(apdev[0], params) | |
851 | hapd.request("SET ext_eapol_frame_io 1") | |
852 | dev[0].request("SET ext_eapol_frame_io 1") | |
853 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False) | |
854 | addr = dev[0].p2p_interface_addr() | |
855 | ||
856 | # EAPOL-Key msg 1/4 | |
857 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
858 | if ev is None: | |
859 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
860 | msg1 = ev.split(' ')[2] | |
861 | res = dev[0].request("EAPOL_RX " + bssid + " " + msg1) | |
862 | if "OK" not in res: | |
863 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
864 | ||
865 | # EAPOL-Key msg 2/4 | |
866 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=15) | |
867 | if ev is None: | |
868 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
869 | res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2]) | |
870 | if "OK" not in res: | |
871 | raise Exception("EAPOL_RX to hostapd failed") | |
872 | ||
873 | # EAPOL-Key msg 3/4 | |
874 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
875 | if ev is None: | |
876 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
877 | res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2]) | |
878 | if "OK" not in res: | |
879 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
880 | ||
881 | # EAPOL-Key msg 4/4 | |
882 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=15) | |
883 | if ev is None: | |
884 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
885 | msg4 = ev.split(' ')[2] | |
886 | # Do not send msg 4/4 to hostapd to trigger retry | |
887 | ||
888 | # STA believes everything is ready | |
889 | dev[0].wait_connected() | |
890 | ||
891 | # EAPOL-Key msg 3/4 (retry) | |
892 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
893 | if ev is None: | |
894 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
895 | msg3 = ev.split(' ')[2] | |
896 | ||
897 | # Send a forged msg 1/4 to STA (update replay counter and replace ANonce) | |
898 | msg1b = msg1[0:18] + msg3[18:34] + 32*"ff" + msg1[98:] | |
899 | res = dev[0].request("EAPOL_RX " + bssid + " " + msg1b) | |
900 | if "OK" not in res: | |
901 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
902 | # EAPOL-Key msg 2/4 | |
903 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=1) | |
904 | if ev is None: | |
905 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
906 | # Do not send msg 2/4 to hostapd | |
907 | ||
908 | # Send a forged msg 1/4 to STA (back to previously used ANonce) | |
909 | msg1b = msg1[0:18] + msg3[18:34] + msg1[34:] | |
910 | res = dev[0].request("EAPOL_RX " + bssid + " " + msg1b) | |
911 | if "OK" not in res: | |
912 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
913 | # EAPOL-Key msg 2/4 | |
914 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=1) | |
915 | if ev is None: | |
916 | # wpa_supplicant seems to have ignored the forged message. This means | |
917 | # the attack would fail. | |
918 | logger.info("wpa_supplicant ignored forged EAPOL-Key msg 1/4") | |
919 | return | |
920 | # Do not send msg 2/4 to hostapd | |
921 | ||
922 | # EAPOL-Key msg 3/4 (retry 2) | |
923 | # New one needed to get the correct Replay Counter value | |
924 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
925 | if ev is None: | |
926 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
927 | msg3 = ev.split(' ')[2] | |
928 | ||
929 | # Send msg 3/4 to STA | |
930 | res = dev[0].request("EAPOL_RX " + bssid + " " + msg3) | |
931 | if "OK" not in res: | |
932 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
933 | ||
934 | # EAPOL-Key msg 4/4 | |
935 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=15) | |
936 | if ev is None: | |
937 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
938 | res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2]) | |
939 | if "OK" not in res: | |
940 | raise Exception("EAPOL_RX to hostapd failed") | |
941 | ||
942 | ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15) | |
943 | if ev is None: | |
944 | raise Exception("Timeout on AP-STA-CONNECTED from hostapd") | |
945 | ||
946 | hwsim_utils.test_connectivity(dev[0], hapd) | |
947 | ||
821490f5 JM |
948 | def parse_eapol(data): |
949 | (version, type, length) = struct.unpack('>BBH', data[0:4]) | |
950 | payload = data[4:] | |
951 | if length > len(payload): | |
952 | raise Exception("Invalid EAPOL length") | |
953 | if length < len(payload): | |
954 | payload = payload[0:length] | |
955 | eapol = {} | |
956 | eapol['version'] = version | |
957 | eapol['type'] = type | |
958 | eapol['length'] = length | |
959 | eapol['payload'] = payload | |
960 | if type == 3: | |
961 | # EAPOL-Key | |
962 | (eapol['descr_type'],) = struct.unpack('B', payload[0:1]) | |
963 | payload = payload[1:] | |
a52fd1c3 | 964 | if eapol['descr_type'] == 2 or eapol['descr_type'] == 254: |
821490f5 JM |
965 | # RSN EAPOL-Key |
966 | (key_info, key_len) = struct.unpack('>HH', payload[0:4]) | |
967 | eapol['rsn_key_info'] = key_info | |
968 | eapol['rsn_key_len'] = key_len | |
969 | eapol['rsn_replay_counter'] = payload[4:12] | |
970 | eapol['rsn_key_nonce'] = payload[12:44] | |
971 | eapol['rsn_key_iv'] = payload[44:60] | |
972 | eapol['rsn_key_rsc'] = payload[60:68] | |
973 | eapol['rsn_key_id'] = payload[68:76] | |
974 | eapol['rsn_key_mic'] = payload[76:92] | |
975 | payload = payload[92:] | |
976 | (eapol['rsn_key_data_len'],) = struct.unpack('>H', payload[0:2]) | |
977 | payload = payload[2:] | |
978 | eapol['rsn_key_data'] = payload | |
979 | return eapol | |
980 | ||
981 | def build_eapol(msg): | |
982 | data = struct.pack(">BBH", msg['version'], msg['type'], msg['length']) | |
983 | if msg['type'] == 3: | |
984 | data += struct.pack('>BHH', msg['descr_type'], msg['rsn_key_info'], | |
985 | msg['rsn_key_len']) | |
986 | data += msg['rsn_replay_counter'] | |
987 | data += msg['rsn_key_nonce'] | |
988 | data += msg['rsn_key_iv'] | |
989 | data += msg['rsn_key_rsc'] | |
990 | data += msg['rsn_key_id'] | |
991 | data += msg['rsn_key_mic'] | |
992 | data += struct.pack('>H', msg['rsn_key_data_len']) | |
993 | data += msg['rsn_key_data'] | |
994 | else: | |
995 | data += msg['payload'] | |
996 | return data | |
997 | ||
998 | def sha1_prf(key, label, data, outlen): | |
999 | res = '' | |
1000 | counter = 0 | |
1001 | while outlen > 0: | |
1002 | m = hmac.new(key, label, hashlib.sha1) | |
1003 | m.update(struct.pack('B', 0)) | |
1004 | m.update(data) | |
1005 | m.update(struct.pack('B', counter)) | |
1006 | counter += 1 | |
1007 | hash = m.digest() | |
1008 | if outlen > len(hash): | |
1009 | res += hash | |
1010 | outlen -= len(hash) | |
1011 | else: | |
1012 | res += hash[0:outlen] | |
1013 | outlen = 0 | |
1014 | return res | |
1015 | ||
1016 | def pmk_to_ptk(pmk, addr1, addr2, nonce1, nonce2): | |
1017 | if addr1 < addr2: | |
1018 | data = binascii.unhexlify(addr1.replace(':','')) + binascii.unhexlify(addr2.replace(':','')) | |
1019 | else: | |
1020 | data = binascii.unhexlify(addr2.replace(':','')) + binascii.unhexlify(addr1.replace(':','')) | |
1021 | if nonce1 < nonce2: | |
1022 | data += nonce1 + nonce2 | |
1023 | else: | |
1024 | data += nonce2 + nonce1 | |
1025 | label = "Pairwise key expansion" | |
1026 | ptk = sha1_prf(pmk, label, data, 48) | |
1027 | kck = ptk[0:16] | |
1028 | kek = ptk[16:32] | |
1029 | return (ptk, kck, kek) | |
1030 | ||
1031 | def eapol_key_mic(kck, msg): | |
1032 | msg['rsn_key_mic'] = binascii.unhexlify('00000000000000000000000000000000') | |
1033 | data = build_eapol(msg) | |
1034 | m = hmac.new(kck, data, hashlib.sha1) | |
1035 | msg['rsn_key_mic'] = m.digest()[0:16] | |
1036 | ||
1037 | def rsn_eapol_key_set(msg, key_info, key_len, nonce, data): | |
1038 | msg['rsn_key_info'] = key_info | |
1039 | msg['rsn_key_len'] = key_len | |
1040 | if nonce: | |
1041 | msg['rsn_key_nonce'] = nonce | |
1042 | else: | |
1043 | msg['rsn_key_nonce'] = binascii.unhexlify('0000000000000000000000000000000000000000000000000000000000000000') | |
1044 | if data: | |
1045 | msg['rsn_key_data_len'] = len(data) | |
1046 | msg['rsn_key_data'] = data | |
1047 | msg['length'] = 95 + len(data) | |
1048 | else: | |
1049 | msg['rsn_key_data_len'] = 0 | |
1050 | msg['rsn_key_data'] = '' | |
1051 | msg['length'] = 95 | |
1052 | ||
1053 | def recv_eapol(hapd): | |
1054 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
1055 | if ev is None: | |
1056 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
1057 | eapol = binascii.unhexlify(ev.split(' ')[2]) | |
1058 | return parse_eapol(eapol) | |
1059 | ||
1060 | def send_eapol(hapd, addr, data): | |
1061 | res = hapd.request("EAPOL_RX " + addr + " " + binascii.hexlify(data)) | |
1062 | if "OK" not in res: | |
1063 | raise Exception("EAPOL_RX to hostapd failed") | |
1064 | ||
1065 | def reply_eapol(info, hapd, addr, msg, key_info, nonce, data, kck): | |
1066 | logger.info("Send EAPOL-Key msg " + info) | |
1067 | rsn_eapol_key_set(msg, key_info, 0, nonce, data) | |
1068 | eapol_key_mic(kck, msg) | |
1069 | send_eapol(hapd, addr, build_eapol(msg)) | |
1070 | ||
1071 | def hapd_connected(hapd): | |
1072 | ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15) | |
1073 | if ev is None: | |
1074 | raise Exception("Timeout on AP-STA-CONNECTED from hostapd") | |
1075 | ||
a52fd1c3 | 1076 | def eapol_test(apdev, dev, wpa2=True): |
821490f5 | 1077 | bssid = apdev['bssid'] |
a52fd1c3 JM |
1078 | if wpa2: |
1079 | ssid = "test-wpa2-psk" | |
1080 | else: | |
1081 | ssid = "test-wpa-psk" | |
821490f5 JM |
1082 | psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6' |
1083 | pmk = binascii.unhexlify(psk) | |
a52fd1c3 JM |
1084 | if wpa2: |
1085 | params = hostapd.wpa2_params(ssid=ssid) | |
1086 | else: | |
1087 | params = hostapd.wpa_params(ssid=ssid) | |
821490f5 | 1088 | params['wpa_psk'] = psk |
afc26df2 | 1089 | hapd = hostapd.add_ap(apdev, params) |
821490f5 JM |
1090 | hapd.request("SET ext_eapol_frame_io 1") |
1091 | dev.request("SET ext_eapol_frame_io 1") | |
e0c46c8e | 1092 | dev.connect(ssid, raw_psk=psk, scan_freq="2412", wait_connect=False) |
821490f5 | 1093 | addr = dev.p2p_interface_addr() |
a52fd1c3 JM |
1094 | if wpa2: |
1095 | rsne = binascii.unhexlify('30140100000fac040100000fac040100000fac020000') | |
1096 | else: | |
1097 | rsne = binascii.unhexlify('dd160050f20101000050f20201000050f20201000050f202') | |
821490f5 JM |
1098 | snonce = binascii.unhexlify('1111111111111111111111111111111111111111111111111111111111111111') |
1099 | return (bssid,ssid,hapd,snonce,pmk,addr,rsne) | |
1100 | ||
9fd6804d | 1101 | @remote_compatible |
821490f5 JM |
1102 | def test_ap_wpa2_psk_ext_eapol(dev, apdev): |
1103 | """WPA2-PSK AP using external EAPOL supplicant""" | |
1104 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1105 | ||
1106 | msg = recv_eapol(hapd) | |
1107 | anonce = msg['rsn_key_nonce'] | |
1108 | logger.info("Replay same data back") | |
1109 | send_eapol(hapd, addr, build_eapol(msg)) | |
1110 | ||
1111 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1112 | ||
1113 | logger.info("Truncated Key Data in EAPOL-Key msg 2/4") | |
1114 | rsn_eapol_key_set(msg, 0x0101, 0, snonce, rsne) | |
1115 | msg['length'] = 95 + 22 - 1 | |
1116 | send_eapol(hapd, addr, build_eapol(msg)) | |
1117 | ||
1118 | reply_eapol("2/4", hapd, addr, msg, 0x010a, snonce, rsne, kck) | |
1119 | ||
1120 | msg = recv_eapol(hapd) | |
1121 | if anonce != msg['rsn_key_nonce']: | |
1122 | raise Exception("ANonce changed") | |
1123 | logger.info("Replay same data back") | |
1124 | send_eapol(hapd, addr, build_eapol(msg)) | |
1125 | ||
1126 | reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck) | |
1127 | hapd_connected(hapd) | |
1128 | ||
9fd6804d | 1129 | @remote_compatible |
821490f5 JM |
1130 | def test_ap_wpa2_psk_ext_eapol_retry1(dev, apdev): |
1131 | """WPA2 4-way handshake with EAPOL-Key 1/4 retransmitted""" | |
1132 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1133 | ||
1134 | msg1 = recv_eapol(hapd) | |
1135 | anonce = msg1['rsn_key_nonce'] | |
1136 | ||
1137 | msg2 = recv_eapol(hapd) | |
1138 | if anonce != msg2['rsn_key_nonce']: | |
1139 | raise Exception("ANonce changed") | |
1140 | ||
1141 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1142 | ||
1143 | logger.info("Send EAPOL-Key msg 2/4") | |
1144 | msg = msg2 | |
1145 | rsn_eapol_key_set(msg, 0x010a, 0, snonce, rsne) | |
1146 | eapol_key_mic(kck, msg) | |
1147 | send_eapol(hapd, addr, build_eapol(msg)) | |
1148 | ||
1149 | msg = recv_eapol(hapd) | |
1150 | if anonce != msg['rsn_key_nonce']: | |
1151 | raise Exception("ANonce changed") | |
1152 | ||
1153 | reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck) | |
1154 | hapd_connected(hapd) | |
1155 | ||
9fd6804d | 1156 | @remote_compatible |
821490f5 JM |
1157 | def test_ap_wpa2_psk_ext_eapol_retry1b(dev, apdev): |
1158 | """WPA2 4-way handshake with EAPOL-Key 1/4 and 2/4 retransmitted""" | |
1159 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1160 | ||
1161 | msg1 = recv_eapol(hapd) | |
1162 | anonce = msg1['rsn_key_nonce'] | |
1163 | msg2 = recv_eapol(hapd) | |
1164 | if anonce != msg2['rsn_key_nonce']: | |
1165 | raise Exception("ANonce changed") | |
1166 | ||
1167 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1168 | reply_eapol("2/4 (a)", hapd, addr, msg1, 0x010a, snonce, rsne, kck) | |
1169 | reply_eapol("2/4 (b)", hapd, addr, msg2, 0x010a, snonce, rsne, kck) | |
1170 | ||
1171 | msg = recv_eapol(hapd) | |
1172 | if anonce != msg['rsn_key_nonce']: | |
1173 | raise Exception("ANonce changed") | |
1174 | ||
1175 | reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck) | |
1176 | hapd_connected(hapd) | |
1177 | ||
9fd6804d | 1178 | @remote_compatible |
821490f5 JM |
1179 | def test_ap_wpa2_psk_ext_eapol_retry1c(dev, apdev): |
1180 | """WPA2 4-way handshake with EAPOL-Key 1/4 and 2/4 retransmitted and SNonce changing""" | |
1181 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1182 | ||
1183 | msg1 = recv_eapol(hapd) | |
1184 | anonce = msg1['rsn_key_nonce'] | |
1185 | ||
1186 | msg2 = recv_eapol(hapd) | |
1187 | if anonce != msg2['rsn_key_nonce']: | |
1188 | raise Exception("ANonce changed") | |
1189 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1190 | reply_eapol("2/4 (a)", hapd, addr, msg1, 0x010a, snonce, rsne, kck) | |
1191 | ||
1192 | snonce2 = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222') | |
1193 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce2, anonce) | |
1194 | reply_eapol("2/4 (b)", hapd, addr, msg2, 0x010a, snonce2, rsne, kck) | |
1195 | ||
1196 | msg = recv_eapol(hapd) | |
1197 | if anonce != msg['rsn_key_nonce']: | |
1198 | raise Exception("ANonce changed") | |
1199 | reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck) | |
1200 | hapd_connected(hapd) | |
1201 | ||
9fd6804d | 1202 | @remote_compatible |
821490f5 JM |
1203 | def test_ap_wpa2_psk_ext_eapol_retry1d(dev, apdev): |
1204 | """WPA2 4-way handshake with EAPOL-Key 1/4 and 2/4 retransmitted and SNonce changing and older used""" | |
1205 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1206 | ||
1207 | msg1 = recv_eapol(hapd) | |
1208 | anonce = msg1['rsn_key_nonce'] | |
1209 | msg2 = recv_eapol(hapd) | |
1210 | if anonce != msg2['rsn_key_nonce']: | |
1211 | raise Exception("ANonce changed") | |
1212 | ||
1213 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1214 | reply_eapol("2/4 (a)", hapd, addr, msg1, 0x010a, snonce, rsne, kck) | |
1215 | ||
1216 | snonce2 = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222') | |
1217 | (ptk2, kck2, kek2) = pmk_to_ptk(pmk, addr, bssid, snonce2, anonce) | |
1218 | ||
1219 | reply_eapol("2/4 (b)", hapd, addr, msg2, 0x010a, snonce2, rsne, kck2) | |
1220 | msg = recv_eapol(hapd) | |
1221 | if anonce != msg['rsn_key_nonce']: | |
1222 | raise Exception("ANonce changed") | |
1223 | reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck) | |
1224 | hapd_connected(hapd) | |
53b9bedb | 1225 | |
9fd6804d | 1226 | @remote_compatible |
53b9bedb JM |
1227 | def test_ap_wpa2_psk_ext_eapol_type_diff(dev, apdev): |
1228 | """WPA2 4-way handshake using external EAPOL supplicant""" | |
1229 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1230 | ||
1231 | msg = recv_eapol(hapd) | |
1232 | anonce = msg['rsn_key_nonce'] | |
1233 | ||
1234 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1235 | ||
1236 | # Incorrect descriptor type (frame dropped) | |
1237 | msg['descr_type'] = 253 | |
1238 | rsn_eapol_key_set(msg, 0x010a, 0, snonce, rsne) | |
1239 | eapol_key_mic(kck, msg) | |
1240 | send_eapol(hapd, addr, build_eapol(msg)) | |
1241 | ||
1242 | # Incorrect descriptor type, but with a workaround (frame processed) | |
1243 | msg['descr_type'] = 254 | |
1244 | rsn_eapol_key_set(msg, 0x010a, 0, snonce, rsne) | |
1245 | eapol_key_mic(kck, msg) | |
1246 | send_eapol(hapd, addr, build_eapol(msg)) | |
1247 | ||
1248 | msg = recv_eapol(hapd) | |
1249 | if anonce != msg['rsn_key_nonce']: | |
1250 | raise Exception("ANonce changed") | |
1251 | logger.info("Replay same data back") | |
1252 | send_eapol(hapd, addr, build_eapol(msg)) | |
1253 | ||
1254 | reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck) | |
1255 | hapd_connected(hapd) | |
a52fd1c3 | 1256 | |
9fd6804d | 1257 | @remote_compatible |
a52fd1c3 JM |
1258 | def test_ap_wpa_psk_ext_eapol(dev, apdev): |
1259 | """WPA2-PSK AP using external EAPOL supplicant""" | |
1260 | (bssid,ssid,hapd,snonce,pmk,addr,wpae) = eapol_test(apdev[0], dev[0], | |
1261 | wpa2=False) | |
1262 | ||
1263 | msg = recv_eapol(hapd) | |
1264 | anonce = msg['rsn_key_nonce'] | |
1265 | logger.info("Replay same data back") | |
1266 | send_eapol(hapd, addr, build_eapol(msg)) | |
1267 | logger.info("Too short data") | |
1268 | send_eapol(hapd, addr, build_eapol(msg)[0:98]) | |
1269 | ||
1270 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1271 | msg['descr_type'] = 2 | |
1272 | reply_eapol("2/4(invalid type)", hapd, addr, msg, 0x010a, snonce, wpae, kck) | |
1273 | msg['descr_type'] = 254 | |
1274 | reply_eapol("2/4", hapd, addr, msg, 0x010a, snonce, wpae, kck) | |
1275 | ||
1276 | msg = recv_eapol(hapd) | |
1277 | if anonce != msg['rsn_key_nonce']: | |
1278 | raise Exception("ANonce changed") | |
1279 | logger.info("Replay same data back") | |
1280 | send_eapol(hapd, addr, build_eapol(msg)) | |
1281 | ||
1282 | reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck) | |
1283 | hapd_connected(hapd) | |
64d04af5 | 1284 | |
9fd6804d | 1285 | @remote_compatible |
64d04af5 JM |
1286 | def test_ap_wpa2_psk_ext_eapol_key_info(dev, apdev): |
1287 | """WPA2-PSK 4-way handshake with strange key info values""" | |
1288 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1289 | ||
1290 | msg = recv_eapol(hapd) | |
1291 | anonce = msg['rsn_key_nonce'] | |
1292 | ||
1293 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1294 | rsn_eapol_key_set(msg, 0x0000, 0, snonce, rsne) | |
1295 | send_eapol(hapd, addr, build_eapol(msg)) | |
1296 | rsn_eapol_key_set(msg, 0xffff, 0, snonce, rsne) | |
1297 | send_eapol(hapd, addr, build_eapol(msg)) | |
1298 | # SMK M1 | |
1299 | rsn_eapol_key_set(msg, 0x2802, 0, snonce, rsne) | |
1300 | send_eapol(hapd, addr, build_eapol(msg)) | |
1301 | # SMK M3 | |
1302 | rsn_eapol_key_set(msg, 0x2002, 0, snonce, rsne) | |
1303 | send_eapol(hapd, addr, build_eapol(msg)) | |
1304 | # Request | |
1305 | rsn_eapol_key_set(msg, 0x0902, 0, snonce, rsne) | |
1306 | send_eapol(hapd, addr, build_eapol(msg)) | |
1307 | # Request | |
1308 | rsn_eapol_key_set(msg, 0x0902, 0, snonce, rsne) | |
1309 | tmp_kck = binascii.unhexlify('00000000000000000000000000000000') | |
1310 | eapol_key_mic(tmp_kck, msg) | |
1311 | send_eapol(hapd, addr, build_eapol(msg)) | |
1312 | ||
1313 | reply_eapol("2/4", hapd, addr, msg, 0x010a, snonce, rsne, kck) | |
1314 | ||
1315 | msg = recv_eapol(hapd) | |
1316 | if anonce != msg['rsn_key_nonce']: | |
1317 | raise Exception("ANonce changed") | |
1318 | ||
1319 | # Request (valic MIC) | |
1320 | rsn_eapol_key_set(msg, 0x0902, 0, snonce, rsne) | |
1321 | eapol_key_mic(kck, msg) | |
1322 | send_eapol(hapd, addr, build_eapol(msg)) | |
1323 | # Request (valid MIC, replayed counter) | |
1324 | rsn_eapol_key_set(msg, 0x0902, 0, snonce, rsne) | |
1325 | eapol_key_mic(kck, msg) | |
1326 | send_eapol(hapd, addr, build_eapol(msg)) | |
1327 | ||
1328 | reply_eapol("4/4", hapd, addr, msg, 0x030a, None, None, kck) | |
1329 | hapd_connected(hapd) | |
5b3c40a6 | 1330 | |
e0c46c8e JM |
1331 | def build_eapol_key_1_4(anonce, replay_counter=1, key_data='', key_len=16): |
1332 | msg = {} | |
1333 | msg['version'] = 2 | |
1334 | msg['type'] = 3 | |
1335 | msg['length'] = 95 + len(key_data) | |
1336 | ||
1337 | msg['descr_type'] = 2 | |
1338 | msg['rsn_key_info'] = 0x8a | |
1339 | msg['rsn_key_len'] = key_len | |
1340 | msg['rsn_replay_counter'] = struct.pack('>Q', replay_counter) | |
1341 | msg['rsn_key_nonce'] = anonce | |
1342 | msg['rsn_key_iv'] = binascii.unhexlify('00000000000000000000000000000000') | |
1343 | msg['rsn_key_rsc'] = binascii.unhexlify('0000000000000000') | |
1344 | msg['rsn_key_id'] = binascii.unhexlify('0000000000000000') | |
1345 | msg['rsn_key_mic'] = binascii.unhexlify('00000000000000000000000000000000') | |
1346 | msg['rsn_key_data_len'] = len(key_data) | |
1347 | msg['rsn_key_data'] = key_data | |
1348 | return msg | |
1349 | ||
1350 | def build_eapol_key_3_4(anonce, kck, key_data, replay_counter=2, | |
1351 | key_info=0x13ca, extra_len=0, descr_type=2, key_len=16): | |
1352 | msg = {} | |
1353 | msg['version'] = 2 | |
1354 | msg['type'] = 3 | |
1355 | msg['length'] = 95 + len(key_data) + extra_len | |
1356 | ||
1357 | msg['descr_type'] = descr_type | |
1358 | msg['rsn_key_info'] = key_info | |
1359 | msg['rsn_key_len'] = key_len | |
1360 | msg['rsn_replay_counter'] = struct.pack('>Q', replay_counter) | |
1361 | msg['rsn_key_nonce'] = anonce | |
1362 | msg['rsn_key_iv'] = binascii.unhexlify('00000000000000000000000000000000') | |
1363 | msg['rsn_key_rsc'] = binascii.unhexlify('0000000000000000') | |
1364 | msg['rsn_key_id'] = binascii.unhexlify('0000000000000000') | |
1365 | msg['rsn_key_data_len'] = len(key_data) | |
1366 | msg['rsn_key_data'] = key_data | |
1367 | eapol_key_mic(kck, msg) | |
1368 | return msg | |
1369 | ||
1370 | def aes_wrap(kek, plain): | |
1371 | n = len(plain) / 8 | |
1372 | a = 0xa6a6a6a6a6a6a6a6 | |
1373 | enc = AES.new(kek).encrypt | |
1374 | r = [plain[i * 8:(i + 1) * 8] for i in range(0, n)] | |
1375 | for j in range(6): | |
1376 | for i in range(1, n + 1): | |
1377 | b = enc(struct.pack('>Q', a) + r[i - 1]) | |
1378 | a = struct.unpack('>Q', b[:8])[0] ^ (n * j + i) | |
1379 | r[i - 1] =b[8:] | |
1380 | return struct.pack('>Q', a) + ''.join(r) | |
1381 | ||
1382 | def pad_key_data(plain): | |
1383 | pad_len = len(plain) % 8 | |
1384 | if pad_len: | |
1385 | pad_len = 8 - pad_len | |
1386 | plain += '\xdd' | |
1387 | pad_len -= 1 | |
1388 | plain += pad_len * '\0' | |
1389 | return plain | |
1390 | ||
1391 | def test_ap_wpa2_psk_supp_proto(dev, apdev): | |
1392 | """WPA2-PSK 4-way handshake protocol testing for supplicant""" | |
1393 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1394 | ||
1395 | # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated | |
1396 | msg = recv_eapol(hapd) | |
1397 | dev[0].dump_monitor() | |
1398 | ||
1399 | # Build own EAPOL-Key msg 1/4 | |
1400 | anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222') | |
1401 | counter = 1 | |
1402 | msg = build_eapol_key_1_4(anonce, replay_counter=counter) | |
1403 | counter += 1 | |
069daec4 | 1404 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1405 | msg = recv_eapol(dev[0]) |
1406 | snonce = msg['rsn_key_nonce'] | |
1407 | ||
1408 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1409 | ||
1410 | logger.debug("Invalid AES wrap data length 0") | |
1411 | dev[0].dump_monitor() | |
1412 | msg = build_eapol_key_3_4(anonce, kck, '', replay_counter=counter) | |
1413 | counter += 1 | |
069daec4 | 1414 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1415 | ev = dev[0].wait_event(["WPA: Unsupported AES-WRAP len 0"]) |
1416 | if ev is None: | |
1417 | raise Exception("Unsupported AES-WRAP len 0 not reported") | |
1418 | ||
1419 | logger.debug("Invalid AES wrap data length 1") | |
1420 | dev[0].dump_monitor() | |
1421 | msg = build_eapol_key_3_4(anonce, kck, '1', replay_counter=counter) | |
1422 | counter += 1 | |
069daec4 | 1423 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1424 | ev = dev[0].wait_event(["WPA: Unsupported AES-WRAP len 1"]) |
1425 | if ev is None: | |
1426 | raise Exception("Unsupported AES-WRAP len 1 not reported") | |
1427 | ||
1428 | logger.debug("Invalid AES wrap data length 9") | |
1429 | dev[0].dump_monitor() | |
1430 | msg = build_eapol_key_3_4(anonce, kck, '123456789', replay_counter=counter) | |
1431 | counter += 1 | |
069daec4 | 1432 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1433 | ev = dev[0].wait_event(["WPA: Unsupported AES-WRAP len 9"]) |
1434 | if ev is None: | |
1435 | raise Exception("Unsupported AES-WRAP len 9 not reported") | |
1436 | ||
1437 | logger.debug("Invalid AES wrap data payload") | |
1438 | dev[0].dump_monitor() | |
1439 | msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter) | |
1440 | # do not increment counter to test replay protection | |
069daec4 | 1441 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1442 | ev = dev[0].wait_event(["WPA: AES unwrap failed"]) |
1443 | if ev is None: | |
1444 | raise Exception("AES unwrap failure not reported") | |
1445 | ||
1446 | logger.debug("Replay Count not increasing") | |
1447 | dev[0].dump_monitor() | |
1448 | msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter) | |
1449 | counter += 1 | |
069daec4 | 1450 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1451 | ev = dev[0].wait_event(["WPA: EAPOL-Key Replay Counter did not increase"]) |
1452 | if ev is None: | |
1453 | raise Exception("Replay Counter replay not reported") | |
1454 | ||
1455 | logger.debug("Missing Ack bit in key info") | |
1456 | dev[0].dump_monitor() | |
1457 | msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter, | |
1458 | key_info=0x134a) | |
1459 | counter += 1 | |
069daec4 | 1460 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1461 | ev = dev[0].wait_event(["WPA: No Ack bit in key_info"]) |
1462 | if ev is None: | |
1463 | raise Exception("Missing Ack bit not reported") | |
1464 | ||
1465 | logger.debug("Unexpected Request bit in key info") | |
1466 | dev[0].dump_monitor() | |
1467 | msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter, | |
1468 | key_info=0x1bca) | |
1469 | counter += 1 | |
069daec4 | 1470 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1471 | ev = dev[0].wait_event(["WPA: EAPOL-Key with Request bit"]) |
1472 | if ev is None: | |
1473 | raise Exception("Request bit not reported") | |
1474 | ||
1475 | logger.debug("Unsupported key descriptor version 0") | |
1476 | dev[0].dump_monitor() | |
1477 | msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef', | |
1478 | replay_counter=counter, key_info=0x13c8) | |
1479 | counter += 1 | |
069daec4 | 1480 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1481 | ev = dev[0].wait_event(["WPA: Unsupported EAPOL-Key descriptor version 0"]) |
1482 | if ev is None: | |
1483 | raise Exception("Unsupported EAPOL-Key descriptor version 0 not reported") | |
1484 | ||
1485 | logger.debug("Key descriptor version 1 not allowed with CCMP") | |
1486 | dev[0].dump_monitor() | |
1487 | msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef', | |
1488 | replay_counter=counter, key_info=0x13c9) | |
1489 | counter += 1 | |
069daec4 | 1490 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1491 | ev = dev[0].wait_event(["WPA: CCMP is used, but EAPOL-Key descriptor version (1) is not 2"]) |
1492 | if ev is None: | |
1493 | raise Exception("Not allowed EAPOL-Key descriptor version not reported") | |
1494 | ||
1495 | logger.debug("Invalid AES wrap payload with key descriptor version 2") | |
1496 | dev[0].dump_monitor() | |
1497 | msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef', | |
1498 | replay_counter=counter, key_info=0x13ca) | |
1499 | counter += 1 | |
069daec4 | 1500 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1501 | ev = dev[0].wait_event(["WPA: AES unwrap failed"]) |
1502 | if ev is None: | |
1503 | raise Exception("AES unwrap failure not reported") | |
1504 | ||
1505 | logger.debug("Key descriptor version 3 workaround") | |
1506 | dev[0].dump_monitor() | |
1507 | msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef', | |
1508 | replay_counter=counter, key_info=0x13cb) | |
1509 | counter += 1 | |
069daec4 | 1510 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1511 | ev = dev[0].wait_event(["WPA: CCMP is used, but EAPOL-Key descriptor version (3) is not 2"]) |
1512 | if ev is None: | |
1513 | raise Exception("CCMP key descriptor mismatch not reported") | |
1514 | ev = dev[0].wait_event(["WPA: Interoperability workaround"]) | |
1515 | if ev is None: | |
1516 | raise Exception("AES-128-CMAC workaround not reported") | |
1517 | ev = dev[0].wait_event(["WPA: Invalid EAPOL-Key MIC - dropping packet"]) | |
1518 | if ev is None: | |
1519 | raise Exception("MIC failure with AES-128-CMAC workaround not reported") | |
1520 | ||
1521 | logger.debug("Unsupported key descriptor version 4") | |
1522 | dev[0].dump_monitor() | |
1523 | msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef', | |
1524 | replay_counter=counter, key_info=0x13cc) | |
1525 | counter += 1 | |
069daec4 | 1526 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1527 | ev = dev[0].wait_event(["WPA: Unsupported EAPOL-Key descriptor version 4"]) |
1528 | if ev is None: | |
1529 | raise Exception("Unsupported EAPOL-Key descriptor version 4 not reported") | |
1530 | ||
1531 | logger.debug("Unsupported key descriptor version 7") | |
1532 | dev[0].dump_monitor() | |
1533 | msg = build_eapol_key_3_4(anonce, kck, '0123456789abcdef', | |
1534 | replay_counter=counter, key_info=0x13cf) | |
1535 | counter += 1 | |
069daec4 | 1536 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1537 | ev = dev[0].wait_event(["WPA: Unsupported EAPOL-Key descriptor version 7"]) |
1538 | if ev is None: | |
1539 | raise Exception("Unsupported EAPOL-Key descriptor version 7 not reported") | |
1540 | ||
1541 | logger.debug("Too short EAPOL header length") | |
1542 | dev[0].dump_monitor() | |
1543 | msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter, | |
1544 | extra_len=-1) | |
1545 | counter += 1 | |
069daec4 | 1546 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1547 | ev = dev[0].wait_event(["WPA: Invalid EAPOL-Key frame - key_data overflow (8 > 7)"]) |
1548 | if ev is None: | |
1549 | raise Exception("Key data overflow not reported") | |
1550 | ||
1551 | logger.debug("Too long EAPOL header length") | |
1552 | msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter, | |
1553 | extra_len=1) | |
1554 | counter += 1 | |
069daec4 | 1555 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1556 | |
1557 | logger.debug("Unsupported descriptor type 0") | |
1558 | msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter, | |
1559 | descr_type=0) | |
1560 | counter += 1 | |
069daec4 | 1561 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1562 | |
1563 | logger.debug("WPA descriptor type 0") | |
1564 | msg = build_eapol_key_3_4(anonce, kck, '12345678', replay_counter=counter, | |
1565 | descr_type=254) | |
1566 | counter += 1 | |
069daec4 | 1567 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1568 | |
1569 | logger.debug("Non-zero key index for pairwise key") | |
1570 | dev[0].dump_monitor() | |
1571 | wrapped = aes_wrap(kek, 16*'z') | |
1572 | msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter, | |
1573 | key_info=0x13ea) | |
1574 | counter += 1 | |
069daec4 | 1575 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1576 | ev = dev[0].wait_event(["WPA: Ignored EAPOL-Key (Pairwise) with non-zero key index"]) |
1577 | if ev is None: | |
1578 | raise Exception("Non-zero key index not reported") | |
1579 | ||
1580 | logger.debug("Invalid Key Data plaintext payload --> disconnect") | |
1581 | dev[0].dump_monitor() | |
1582 | wrapped = aes_wrap(kek, 16*'z') | |
1583 | msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter) | |
1584 | counter += 1 | |
069daec4 | 1585 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1586 | dev[0].wait_disconnected(timeout=1) |
1587 | ||
1588 | def test_ap_wpa2_psk_supp_proto_no_ie(dev, apdev): | |
1589 | """WPA2-PSK supplicant protocol testing: IE not included""" | |
1590 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1591 | ||
1592 | # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated | |
1593 | msg = recv_eapol(hapd) | |
1594 | dev[0].dump_monitor() | |
1595 | ||
1596 | # Build own EAPOL-Key msg 1/4 | |
1597 | anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222') | |
1598 | counter = 1 | |
1599 | msg = build_eapol_key_1_4(anonce, replay_counter=counter) | |
1600 | counter += 1 | |
069daec4 | 1601 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1602 | msg = recv_eapol(dev[0]) |
1603 | snonce = msg['rsn_key_nonce'] | |
1604 | ||
1605 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1606 | ||
1607 | logger.debug("No IEs in msg 3/4 --> disconnect") | |
1608 | dev[0].dump_monitor() | |
1609 | wrapped = aes_wrap(kek, 16*'\0') | |
1610 | msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter) | |
1611 | counter += 1 | |
069daec4 | 1612 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1613 | dev[0].wait_disconnected(timeout=1) |
1614 | ||
1615 | def test_ap_wpa2_psk_supp_proto_ie_mismatch(dev, apdev): | |
1616 | """WPA2-PSK supplicant protocol testing: IE mismatch""" | |
1617 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1618 | ||
1619 | # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated | |
1620 | msg = recv_eapol(hapd) | |
1621 | dev[0].dump_monitor() | |
1622 | ||
1623 | # Build own EAPOL-Key msg 1/4 | |
1624 | anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222') | |
1625 | counter = 1 | |
1626 | msg = build_eapol_key_1_4(anonce, replay_counter=counter) | |
1627 | counter += 1 | |
069daec4 | 1628 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1629 | msg = recv_eapol(dev[0]) |
1630 | snonce = msg['rsn_key_nonce'] | |
1631 | ||
1632 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1633 | ||
1634 | logger.debug("Msg 3/4 with mismatching IE") | |
1635 | dev[0].dump_monitor() | |
1636 | wrapped = aes_wrap(kek, pad_key_data(binascii.unhexlify('30060100000fac04dd16000fac010100dc11188831bf4aa4a8678d2b41498618'))) | |
1637 | msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter) | |
1638 | counter += 1 | |
069daec4 | 1639 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1640 | dev[0].wait_disconnected(timeout=1) |
1641 | ||
1642 | def test_ap_wpa2_psk_supp_proto_ok(dev, apdev): | |
1643 | """WPA2-PSK supplicant protocol testing: success""" | |
1644 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1645 | ||
1646 | # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated | |
1647 | msg = recv_eapol(hapd) | |
1648 | dev[0].dump_monitor() | |
1649 | ||
1650 | # Build own EAPOL-Key msg 1/4 | |
1651 | anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222') | |
1652 | counter = 1 | |
1653 | msg = build_eapol_key_1_4(anonce, replay_counter=counter) | |
1654 | counter += 1 | |
069daec4 | 1655 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1656 | msg = recv_eapol(dev[0]) |
1657 | snonce = msg['rsn_key_nonce'] | |
1658 | ||
1659 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1660 | ||
1661 | logger.debug("Valid EAPOL-Key msg 3/4") | |
1662 | dev[0].dump_monitor() | |
1663 | plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010100dc11188831bf4aa4a8678d2b41498618') | |
1664 | wrapped = aes_wrap(kek, pad_key_data(plain)) | |
1665 | msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter) | |
1666 | counter += 1 | |
069daec4 | 1667 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1668 | dev[0].wait_connected(timeout=1) |
1669 | ||
1670 | def test_ap_wpa2_psk_supp_proto_no_gtk(dev, apdev): | |
1671 | """WPA2-PSK supplicant protocol testing: no GTK""" | |
1672 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1673 | ||
1674 | # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated | |
1675 | msg = recv_eapol(hapd) | |
1676 | dev[0].dump_monitor() | |
1677 | ||
1678 | # Build own EAPOL-Key msg 1/4 | |
1679 | anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222') | |
1680 | counter = 1 | |
1681 | msg = build_eapol_key_1_4(anonce, replay_counter=counter) | |
1682 | counter += 1 | |
069daec4 | 1683 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1684 | msg = recv_eapol(dev[0]) |
1685 | snonce = msg['rsn_key_nonce'] | |
1686 | ||
1687 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1688 | ||
1689 | logger.debug("EAPOL-Key msg 3/4 without GTK KDE") | |
1690 | dev[0].dump_monitor() | |
1691 | plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00') | |
1692 | wrapped = aes_wrap(kek, pad_key_data(plain)) | |
1693 | msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter) | |
1694 | counter += 1 | |
069daec4 | 1695 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1696 | ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1) |
1697 | if ev is not None: | |
1698 | raise Exception("Unexpected connection completion reported") | |
1699 | ||
1700 | def test_ap_wpa2_psk_supp_proto_anonce_change(dev, apdev): | |
1701 | """WPA2-PSK supplicant protocol testing: ANonce change""" | |
1702 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1703 | ||
1704 | # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated | |
1705 | msg = recv_eapol(hapd) | |
1706 | dev[0].dump_monitor() | |
1707 | ||
1708 | # Build own EAPOL-Key msg 1/4 | |
1709 | anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222') | |
1710 | counter = 1 | |
1711 | msg = build_eapol_key_1_4(anonce, replay_counter=counter) | |
1712 | counter += 1 | |
069daec4 | 1713 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1714 | msg = recv_eapol(dev[0]) |
1715 | snonce = msg['rsn_key_nonce'] | |
1716 | ||
1717 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1718 | ||
1719 | logger.debug("Valid EAPOL-Key msg 3/4") | |
1720 | dev[0].dump_monitor() | |
1721 | anonce2 = binascii.unhexlify('3333333333333333333333333333333333333333333333333333333333333333') | |
1722 | plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010100dc11188831bf4aa4a8678d2b41498618') | |
1723 | wrapped = aes_wrap(kek, pad_key_data(plain)) | |
1724 | msg = build_eapol_key_3_4(anonce2, kck, wrapped, replay_counter=counter) | |
1725 | counter += 1 | |
069daec4 | 1726 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1727 | ev = dev[0].wait_event(["WPA: ANonce from message 1 of 4-Way Handshake differs from 3 of 4-Way Handshake"]) |
1728 | if ev is None: | |
1729 | raise Exception("ANonce change not reported") | |
1730 | ||
1731 | def test_ap_wpa2_psk_supp_proto_unexpected_group_msg(dev, apdev): | |
1732 | """WPA2-PSK supplicant protocol testing: unexpected group message""" | |
1733 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1734 | ||
1735 | # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated | |
1736 | msg = recv_eapol(hapd) | |
1737 | dev[0].dump_monitor() | |
1738 | ||
1739 | # Build own EAPOL-Key msg 1/4 | |
1740 | anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222') | |
1741 | counter = 1 | |
1742 | msg = build_eapol_key_1_4(anonce, replay_counter=counter) | |
1743 | counter += 1 | |
069daec4 | 1744 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1745 | msg = recv_eapol(dev[0]) |
1746 | snonce = msg['rsn_key_nonce'] | |
1747 | ||
1748 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1749 | ||
1750 | logger.debug("Group key 1/2 instead of msg 3/4") | |
1751 | dev[0].dump_monitor() | |
1752 | wrapped = aes_wrap(kek, binascii.unhexlify('dd16000fac010100dc11188831bf4aa4a8678d2b41498618')) | |
1753 | msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter, | |
1754 | key_info=0x13c2) | |
1755 | counter += 1 | |
069daec4 | 1756 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1757 | ev = dev[0].wait_event(["WPA: Group Key Handshake started prior to completion of 4-way handshake"]) |
1758 | if ev is None: | |
1759 | raise Exception("Unexpected group key message not reported") | |
1760 | dev[0].wait_disconnected(timeout=1) | |
1761 | ||
9fd6804d | 1762 | @remote_compatible |
e0c46c8e JM |
1763 | def test_ap_wpa2_psk_supp_proto_msg_1_invalid_kde(dev, apdev): |
1764 | """WPA2-PSK supplicant protocol testing: invalid KDE in msg 1/4""" | |
1765 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1766 | ||
1767 | # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated | |
1768 | msg = recv_eapol(hapd) | |
1769 | dev[0].dump_monitor() | |
1770 | ||
1771 | # Build own EAPOL-Key msg 1/4 with invalid KDE | |
1772 | anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222') | |
1773 | counter = 1 | |
1774 | msg = build_eapol_key_1_4(anonce, replay_counter=counter, | |
1775 | key_data=binascii.unhexlify('5555')) | |
1776 | counter += 1 | |
069daec4 | 1777 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1778 | dev[0].wait_disconnected(timeout=1) |
1779 | ||
1780 | def test_ap_wpa2_psk_supp_proto_wrong_pairwise_key_len(dev, apdev): | |
1781 | """WPA2-PSK supplicant protocol testing: wrong pairwise key length""" | |
1782 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1783 | ||
1784 | # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated | |
1785 | msg = recv_eapol(hapd) | |
1786 | dev[0].dump_monitor() | |
1787 | ||
1788 | # Build own EAPOL-Key msg 1/4 | |
1789 | anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222') | |
1790 | counter = 1 | |
1791 | msg = build_eapol_key_1_4(anonce, replay_counter=counter) | |
1792 | counter += 1 | |
069daec4 | 1793 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1794 | msg = recv_eapol(dev[0]) |
1795 | snonce = msg['rsn_key_nonce'] | |
1796 | ||
1797 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1798 | ||
1799 | logger.debug("Valid EAPOL-Key msg 3/4") | |
1800 | dev[0].dump_monitor() | |
1801 | plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010100dc11188831bf4aa4a8678d2b41498618') | |
1802 | wrapped = aes_wrap(kek, pad_key_data(plain)) | |
1803 | msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter, | |
1804 | key_len=15) | |
1805 | counter += 1 | |
069daec4 | 1806 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1807 | ev = dev[0].wait_event(["WPA: Invalid CCMP key length 15"]) |
1808 | if ev is None: | |
1809 | raise Exception("Invalid CCMP key length not reported") | |
1810 | dev[0].wait_disconnected(timeout=1) | |
1811 | ||
1812 | def test_ap_wpa2_psk_supp_proto_wrong_group_key_len(dev, apdev): | |
1813 | """WPA2-PSK supplicant protocol testing: wrong group key length""" | |
1814 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1815 | ||
1816 | # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated | |
1817 | msg = recv_eapol(hapd) | |
1818 | dev[0].dump_monitor() | |
1819 | ||
1820 | # Build own EAPOL-Key msg 1/4 | |
1821 | anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222') | |
1822 | counter = 1 | |
1823 | msg = build_eapol_key_1_4(anonce, replay_counter=counter) | |
1824 | counter += 1 | |
069daec4 | 1825 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1826 | msg = recv_eapol(dev[0]) |
1827 | snonce = msg['rsn_key_nonce'] | |
1828 | ||
1829 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1830 | ||
1831 | logger.debug("Valid EAPOL-Key msg 3/4") | |
1832 | dev[0].dump_monitor() | |
1833 | plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd15000fac010100dc11188831bf4aa4a8678d2b414986') | |
1834 | wrapped = aes_wrap(kek, pad_key_data(plain)) | |
1835 | msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter) | |
1836 | counter += 1 | |
069daec4 | 1837 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1838 | ev = dev[0].wait_event(["WPA: Unsupported CCMP Group Cipher key length 15"]) |
1839 | if ev is None: | |
1840 | raise Exception("Invalid CCMP key length not reported") | |
1841 | dev[0].wait_disconnected(timeout=1) | |
1842 | ||
1843 | def test_ap_wpa2_psk_supp_proto_gtk_tx_bit_workaround(dev, apdev): | |
1844 | """WPA2-PSK supplicant protocol testing: GTK TX bit workaround""" | |
1845 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1846 | ||
1847 | # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated | |
1848 | msg = recv_eapol(hapd) | |
1849 | dev[0].dump_monitor() | |
1850 | ||
1851 | # Build own EAPOL-Key msg 1/4 | |
1852 | anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222') | |
1853 | counter = 1 | |
1854 | msg = build_eapol_key_1_4(anonce, replay_counter=counter) | |
1855 | counter += 1 | |
069daec4 | 1856 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1857 | msg = recv_eapol(dev[0]) |
1858 | snonce = msg['rsn_key_nonce'] | |
1859 | ||
1860 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1861 | ||
1862 | logger.debug("Valid EAPOL-Key msg 3/4") | |
1863 | dev[0].dump_monitor() | |
1864 | plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010500dc11188831bf4aa4a8678d2b41498618') | |
1865 | wrapped = aes_wrap(kek, pad_key_data(plain)) | |
1866 | msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter) | |
1867 | counter += 1 | |
069daec4 | 1868 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1869 | ev = dev[0].wait_event(["WPA: Tx bit set for GTK, but pairwise keys are used - ignore Tx bit"]) |
1870 | if ev is None: | |
1871 | raise Exception("GTK Tx bit workaround not reported") | |
1872 | dev[0].wait_connected(timeout=1) | |
1873 | ||
1874 | def test_ap_wpa2_psk_supp_proto_gtk_keyidx_0_and_3(dev, apdev): | |
1875 | """WPA2-PSK supplicant protocol testing: GTK key index 0 and 3""" | |
1876 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1877 | ||
1878 | # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated | |
1879 | msg = recv_eapol(hapd) | |
1880 | dev[0].dump_monitor() | |
1881 | ||
1882 | # Build own EAPOL-Key msg 1/4 | |
1883 | anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222') | |
1884 | counter = 1 | |
1885 | msg = build_eapol_key_1_4(anonce, replay_counter=counter) | |
1886 | counter += 1 | |
069daec4 | 1887 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1888 | msg = recv_eapol(dev[0]) |
1889 | snonce = msg['rsn_key_nonce'] | |
1890 | ||
1891 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1892 | ||
1893 | logger.debug("Valid EAPOL-Key msg 3/4 (GTK keyidx 0)") | |
1894 | dev[0].dump_monitor() | |
1895 | plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010000dc11188831bf4aa4a8678d2b41498618') | |
1896 | wrapped = aes_wrap(kek, pad_key_data(plain)) | |
1897 | msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter) | |
1898 | counter += 1 | |
069daec4 | 1899 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1900 | dev[0].wait_connected(timeout=1) |
1901 | ||
1902 | logger.debug("Valid EAPOL-Key group msg 1/2 (GTK keyidx 3)") | |
1903 | dev[0].dump_monitor() | |
1904 | plain = binascii.unhexlify('dd16000fac010300dc11188831bf4aa4a8678d2b41498618') | |
1905 | wrapped = aes_wrap(kek, pad_key_data(plain)) | |
1906 | msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter, | |
1907 | key_info=0x13c2) | |
1908 | counter += 1 | |
069daec4 | 1909 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1910 | msg = recv_eapol(dev[0]) |
1911 | ev = dev[0].wait_event(["WPA: Group rekeying completed"]) | |
1912 | if ev is None: | |
1913 | raise Exception("GTK rekeing not reported") | |
1914 | ||
1915 | logger.debug("Unencrypted GTK KDE in group msg 1/2") | |
1916 | dev[0].dump_monitor() | |
1917 | plain = binascii.unhexlify('dd16000fac010300dc11188831bf4aa4a8678d2b41498618') | |
1918 | msg = build_eapol_key_3_4(anonce, kck, plain, replay_counter=counter, | |
1919 | key_info=0x03c2) | |
1920 | counter += 1 | |
069daec4 | 1921 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1922 | ev = dev[0].wait_event(["WPA: GTK IE in unencrypted key data"]) |
1923 | if ev is None: | |
1924 | raise Exception("Unencrypted GTK KDE not reported") | |
1925 | dev[0].wait_disconnected(timeout=1) | |
1926 | ||
1927 | def test_ap_wpa2_psk_supp_proto_no_gtk_in_group_msg(dev, apdev): | |
1928 | """WPA2-PSK supplicant protocol testing: GTK KDE missing from group msg""" | |
1929 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1930 | ||
1931 | # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated | |
1932 | msg = recv_eapol(hapd) | |
1933 | dev[0].dump_monitor() | |
1934 | ||
1935 | # Build own EAPOL-Key msg 1/4 | |
1936 | anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222') | |
1937 | counter = 1 | |
1938 | msg = build_eapol_key_1_4(anonce, replay_counter=counter) | |
1939 | counter += 1 | |
069daec4 | 1940 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1941 | msg = recv_eapol(dev[0]) |
1942 | snonce = msg['rsn_key_nonce'] | |
1943 | ||
1944 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1945 | ||
1946 | logger.debug("Valid EAPOL-Key msg 3/4 (GTK keyidx 0)") | |
1947 | dev[0].dump_monitor() | |
1948 | plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010000dc11188831bf4aa4a8678d2b41498618') | |
1949 | wrapped = aes_wrap(kek, pad_key_data(plain)) | |
1950 | msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter) | |
1951 | counter += 1 | |
069daec4 | 1952 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1953 | dev[0].wait_connected(timeout=1) |
1954 | ||
1955 | logger.debug("No GTK KDE in EAPOL-Key group msg 1/2") | |
1956 | dev[0].dump_monitor() | |
1957 | plain = binascii.unhexlify('dd00dd00dd00dd00dd00dd00dd00dd00') | |
1958 | wrapped = aes_wrap(kek, pad_key_data(plain)) | |
1959 | msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter, | |
1960 | key_info=0x13c2) | |
1961 | counter += 1 | |
069daec4 | 1962 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1963 | ev = dev[0].wait_event(["WPA: No GTK IE in Group Key msg 1/2"]) |
1964 | if ev is None: | |
1965 | raise Exception("Missing GTK KDE not reported") | |
1966 | dev[0].wait_disconnected(timeout=1) | |
1967 | ||
1968 | def test_ap_wpa2_psk_supp_proto_too_long_gtk_in_group_msg(dev, apdev): | |
1969 | """WPA2-PSK supplicant protocol testing: too long GTK KDE in group msg""" | |
1970 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
1971 | ||
1972 | # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated | |
1973 | msg = recv_eapol(hapd) | |
1974 | dev[0].dump_monitor() | |
1975 | ||
1976 | # Build own EAPOL-Key msg 1/4 | |
1977 | anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222') | |
1978 | counter = 1 | |
1979 | msg = build_eapol_key_1_4(anonce, replay_counter=counter) | |
1980 | counter += 1 | |
069daec4 | 1981 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1982 | msg = recv_eapol(dev[0]) |
1983 | snonce = msg['rsn_key_nonce'] | |
1984 | ||
1985 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
1986 | ||
1987 | logger.debug("Valid EAPOL-Key msg 3/4 (GTK keyidx 0)") | |
1988 | dev[0].dump_monitor() | |
1989 | plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010000dc11188831bf4aa4a8678d2b41498618') | |
1990 | wrapped = aes_wrap(kek, pad_key_data(plain)) | |
1991 | msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter) | |
1992 | counter += 1 | |
069daec4 | 1993 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
1994 | dev[0].wait_connected(timeout=1) |
1995 | ||
1996 | logger.debug("EAPOL-Key group msg 1/2 with too long GTK KDE") | |
1997 | dev[0].dump_monitor() | |
1998 | plain = binascii.unhexlify('dd27000fac010100ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff') | |
1999 | wrapped = aes_wrap(kek, pad_key_data(plain)) | |
2000 | msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter, | |
2001 | key_info=0x13c2) | |
2002 | counter += 1 | |
069daec4 | 2003 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
2004 | ev = dev[0].wait_event(["WPA: Unsupported CCMP Group Cipher key length 33"]) |
2005 | if ev is None: | |
2006 | raise Exception("Too long GTK KDE not reported") | |
2007 | dev[0].wait_disconnected(timeout=1) | |
2008 | ||
2009 | def test_ap_wpa2_psk_supp_proto_too_long_gtk_kde(dev, apdev): | |
2010 | """WPA2-PSK supplicant protocol testing: too long GTK KDE""" | |
2011 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
2012 | ||
2013 | # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated | |
2014 | msg = recv_eapol(hapd) | |
2015 | dev[0].dump_monitor() | |
2016 | ||
2017 | # Build own EAPOL-Key msg 1/4 | |
2018 | anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222') | |
2019 | counter = 1 | |
2020 | msg = build_eapol_key_1_4(anonce, replay_counter=counter) | |
2021 | counter += 1 | |
069daec4 | 2022 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
2023 | msg = recv_eapol(dev[0]) |
2024 | snonce = msg['rsn_key_nonce'] | |
2025 | ||
2026 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
2027 | ||
2028 | logger.debug("EAPOL-Key msg 3/4 with too short GTK KDE") | |
2029 | dev[0].dump_monitor() | |
2030 | plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd27000fac010100ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff') | |
2031 | wrapped = aes_wrap(kek, pad_key_data(plain)) | |
2032 | msg = build_eapol_key_3_4(anonce, kck, wrapped, replay_counter=counter) | |
2033 | counter += 1 | |
069daec4 | 2034 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
2035 | dev[0].wait_disconnected(timeout=1) |
2036 | ||
2037 | def test_ap_wpa2_psk_supp_proto_gtk_not_encrypted(dev, apdev): | |
2038 | """WPA2-PSK supplicant protocol testing: GTK KDE not encrypted""" | |
2039 | (bssid,ssid,hapd,snonce,pmk,addr,rsne) = eapol_test(apdev[0], dev[0]) | |
2040 | ||
2041 | # Wait for EAPOL-Key msg 1/4 from hostapd to determine when associated | |
2042 | msg = recv_eapol(hapd) | |
2043 | dev[0].dump_monitor() | |
2044 | ||
2045 | # Build own EAPOL-Key msg 1/4 | |
2046 | anonce = binascii.unhexlify('2222222222222222222222222222222222222222222222222222222222222222') | |
2047 | counter = 1 | |
2048 | msg = build_eapol_key_1_4(anonce, replay_counter=counter) | |
2049 | counter += 1 | |
069daec4 | 2050 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
2051 | msg = recv_eapol(dev[0]) |
2052 | snonce = msg['rsn_key_nonce'] | |
2053 | ||
2054 | (ptk, kck, kek) = pmk_to_ptk(pmk, addr, bssid, snonce, anonce) | |
2055 | ||
2056 | logger.debug("Valid EAPOL-Key msg 3/4") | |
2057 | dev[0].dump_monitor() | |
2058 | plain = binascii.unhexlify('30140100000fac040100000fac040100000fac020c00dd16000fac010100dc11188831bf4aa4a8678d2b41498618') | |
2059 | msg = build_eapol_key_3_4(anonce, kck, plain, replay_counter=counter, | |
2060 | key_info=0x03ca) | |
2061 | counter += 1 | |
069daec4 | 2062 | send_eapol(dev[0], bssid, build_eapol(msg)) |
e0c46c8e JM |
2063 | ev = dev[0].wait_event(["WPA: GTK IE in unencrypted key data"]) |
2064 | if ev is None: | |
2065 | raise Exception("Unencrypted GTK KDE not reported") | |
2066 | dev[0].wait_disconnected(timeout=1) | |
2067 | ||
5b3c40a6 JM |
2068 | def find_wpas_process(dev): |
2069 | ifname = dev.ifname | |
525f8293 | 2070 | err, data = dev.cmd_execute(['ps', 'ax']) |
5b3c40a6 JM |
2071 | for l in data.splitlines(): |
2072 | if "wpa_supplicant" not in l: | |
2073 | continue | |
2074 | if "-i" + ifname not in l: | |
2075 | continue | |
2076 | return int(l.strip().split(' ')[0]) | |
2077 | raise Exception("Could not find wpa_supplicant process") | |
2078 | ||
2079 | def read_process_memory(pid, key=None): | |
2080 | buf = bytes() | |
f089cdf9 | 2081 | logger.info("Reading process memory (pid=%d)" % pid) |
5b3c40a6 JM |
2082 | with open('/proc/%d/maps' % pid, 'r') as maps, \ |
2083 | open('/proc/%d/mem' % pid, 'r') as mem: | |
2084 | for l in maps.readlines(): | |
2085 | m = re.match(r'([0-9a-f]+)-([0-9a-f]+) ([-r][-w][-x][-p])', l) | |
2086 | if not m: | |
2087 | continue | |
2088 | start = int(m.group(1), 16) | |
2089 | end = int(m.group(2), 16) | |
2090 | perm = m.group(3) | |
2091 | if start > 0xffffffffffff: | |
2092 | continue | |
2093 | if end < start: | |
2094 | continue | |
2095 | if not perm.startswith('rw'): | |
2096 | continue | |
f089cdf9 JM |
2097 | for name in [ "[heap]", "[stack]" ]: |
2098 | if name in l: | |
2099 | logger.info("%s 0x%x-0x%x is at %d-%d" % (name, start, end, len(buf), len(buf) + (end - start))) | |
5b3c40a6 JM |
2100 | mem.seek(start) |
2101 | data = mem.read(end - start) | |
2102 | buf += data | |
2103 | if key and key in data: | |
2104 | logger.info("Key found in " + l) | |
f089cdf9 | 2105 | logger.info("Total process memory read: %d bytes" % len(buf)) |
5b3c40a6 JM |
2106 | return buf |
2107 | ||
2108 | def verify_not_present(buf, key, fname, keyname): | |
2109 | pos = buf.find(key) | |
2110 | if pos < 0: | |
2111 | return | |
2112 | ||
2113 | prefix = 2048 if pos > 2048 else pos | |
2114 | with open(fname + keyname, 'w') as f: | |
2115 | f.write(buf[pos - prefix:pos + 2048]) | |
2116 | raise Exception(keyname + " found after disassociation") | |
2117 | ||
2118 | def get_key_locations(buf, key, keyname): | |
2119 | count = 0 | |
2120 | pos = 0 | |
2121 | while True: | |
2122 | pos = buf.find(key, pos) | |
2123 | if pos < 0: | |
2124 | break | |
2125 | logger.info("Found %s at %d" % (keyname, pos)) | |
bc6e3288 | 2126 | context = 128 |
f089cdf9 JM |
2127 | start = pos - context if pos > context else 0 |
2128 | before = binascii.hexlify(buf[start:pos]) | |
2129 | context += len(key) | |
2130 | end = pos + context if pos < len(buf) - context else len(buf) - context | |
2131 | after = binascii.hexlify(buf[pos + len(key):end]) | |
2132 | logger.debug("Memory context %d-%d: %s|%s|%s" % (start, end, before, binascii.hexlify(key), after)) | |
5b3c40a6 JM |
2133 | count += 1 |
2134 | pos += len(key) | |
2135 | return count | |
2136 | ||
2137 | def test_wpa2_psk_key_lifetime_in_memory(dev, apdev, params): | |
2138 | """WPA2-PSK and PSK/PTK lifetime in memory""" | |
2139 | ssid = "test-wpa2-psk" | |
2140 | passphrase = 'qwertyuiop' | |
2141 | psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6' | |
2142 | pmk = binascii.unhexlify(psk) | |
2143 | p = hostapd.wpa2_params(ssid=ssid) | |
2144 | p['wpa_psk'] = psk | |
8b8a1864 | 2145 | hapd = hostapd.add_ap(apdev[0], p) |
5b3c40a6 JM |
2146 | |
2147 | pid = find_wpas_process(dev[0]) | |
2148 | ||
2149 | id = dev[0].connect(ssid, raw_psk=psk, scan_freq="2412", | |
2150 | only_add_network=True) | |
2151 | ||
2152 | logger.info("Checking keys in memory after network profile configuration") | |
2153 | buf = read_process_memory(pid, pmk) | |
2154 | get_key_locations(buf, pmk, "PMK") | |
2155 | ||
2156 | dev[0].request("REMOVE_NETWORK all") | |
2157 | logger.info("Checking keys in memory after network profile removal") | |
2158 | buf = read_process_memory(pid, pmk) | |
2159 | get_key_locations(buf, pmk, "PMK") | |
2160 | ||
2161 | id = dev[0].connect(ssid, psk=passphrase, scan_freq="2412", | |
2162 | only_add_network=True) | |
2163 | ||
2164 | logger.info("Checking keys in memory before connection") | |
2165 | buf = read_process_memory(pid, pmk) | |
2166 | get_key_locations(buf, pmk, "PMK") | |
2167 | ||
2168 | dev[0].connect_network(id, timeout=20) | |
8e416cec JM |
2169 | # The decrypted copy of GTK is freed only after the CTRL-EVENT-CONNECTED |
2170 | # event has been delivered, so verify that wpa_supplicant has returned to | |
2171 | # eloop before reading process memory. | |
54f2cae2 | 2172 | time.sleep(1) |
8e416cec | 2173 | dev[0].ping() |
5b3c40a6 JM |
2174 | |
2175 | buf = read_process_memory(pid, pmk) | |
2176 | ||
2177 | dev[0].request("DISCONNECT") | |
2178 | dev[0].wait_disconnected() | |
2179 | ||
2180 | dev[0].relog() | |
2181 | ptk = None | |
2182 | gtk = None | |
2183 | with open(os.path.join(params['logdir'], 'log0'), 'r') as f: | |
2184 | for l in f.readlines(): | |
2185 | if "WPA: PTK - hexdump" in l: | |
2186 | val = l.strip().split(':')[3].replace(' ', '') | |
2187 | ptk = binascii.unhexlify(val) | |
2188 | if "WPA: Group Key - hexdump" in l: | |
2189 | val = l.strip().split(':')[3].replace(' ', '') | |
2190 | gtk = binascii.unhexlify(val) | |
2191 | if not pmk or not ptk or not gtk: | |
2192 | raise Exception("Could not find keys from debug log") | |
2193 | if len(gtk) != 16: | |
2194 | raise Exception("Unexpected GTK length") | |
2195 | ||
2196 | kck = ptk[0:16] | |
2197 | kek = ptk[16:32] | |
2198 | tk = ptk[32:48] | |
2199 | ||
2200 | logger.info("Checking keys in memory while associated") | |
2201 | get_key_locations(buf, pmk, "PMK") | |
2202 | if pmk not in buf: | |
81e787b7 | 2203 | raise HwsimSkip("PMK not found while associated") |
5b3c40a6 JM |
2204 | if kck not in buf: |
2205 | raise Exception("KCK not found while associated") | |
2206 | if kek not in buf: | |
2207 | raise Exception("KEK not found while associated") | |
b74f82a4 JM |
2208 | #if tk in buf: |
2209 | # raise Exception("TK found from memory") | |
5b3c40a6 JM |
2210 | |
2211 | logger.info("Checking keys in memory after disassociation") | |
2212 | buf = read_process_memory(pid, pmk) | |
2213 | get_key_locations(buf, pmk, "PMK") | |
2214 | ||
2215 | # Note: PMK/PSK is still present in network configuration | |
2216 | ||
2217 | fname = os.path.join(params['logdir'], | |
2218 | 'wpa2_psk_key_lifetime_in_memory.memctx-') | |
2219 | verify_not_present(buf, kck, fname, "KCK") | |
2220 | verify_not_present(buf, kek, fname, "KEK") | |
2221 | verify_not_present(buf, tk, fname, "TK") | |
6db556b2 JM |
2222 | if gtk in buf: |
2223 | get_key_locations(buf, gtk, "GTK") | |
5b3c40a6 JM |
2224 | verify_not_present(buf, gtk, fname, "GTK") |
2225 | ||
2226 | dev[0].request("REMOVE_NETWORK all") | |
2227 | ||
2228 | logger.info("Checking keys in memory after network profile removal") | |
2229 | buf = read_process_memory(pid, pmk) | |
2230 | get_key_locations(buf, pmk, "PMK") | |
2231 | ||
2232 | verify_not_present(buf, pmk, fname, "PMK") | |
2233 | verify_not_present(buf, kck, fname, "KCK") | |
2234 | verify_not_present(buf, kek, fname, "KEK") | |
2235 | verify_not_present(buf, tk, fname, "TK") | |
2236 | verify_not_present(buf, gtk, fname, "GTK") | |
214457de | 2237 | |
9fd6804d | 2238 | @remote_compatible |
214457de JM |
2239 | def test_ap_wpa2_psk_wep(dev, apdev): |
2240 | """WPA2-PSK AP and WEP enabled""" | |
2241 | ssid = "test-wpa2-psk" | |
2242 | passphrase = 'qwertyuiop' | |
2243 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
8b8a1864 | 2244 | hapd = hostapd.add_ap(apdev[0], params) |
214457de JM |
2245 | try: |
2246 | hapd.set('wep_key0', '"hello"') | |
2247 | raise Exception("WEP key accepted to WPA2 network") | |
2248 | except Exception: | |
2249 | pass | |
a1512a0c JM |
2250 | |
2251 | def test_ap_wpa2_psk_wpas_in_bridge(dev, apdev): | |
2252 | """WPA2-PSK AP and wpas interface in a bridge""" | |
2253 | br_ifname='sta-br0' | |
2254 | ifname='wlan5' | |
2255 | try: | |
2256 | _test_ap_wpa2_psk_wpas_in_bridge(dev, apdev) | |
2257 | finally: | |
2258 | subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'down']) | |
2259 | subprocess.call(['brctl', 'delif', br_ifname, ifname]) | |
2260 | subprocess.call(['brctl', 'delbr', br_ifname]) | |
f245b450 | 2261 | subprocess.call(['iw', ifname, 'set', '4addr', 'off']) |
a1512a0c JM |
2262 | |
2263 | def _test_ap_wpa2_psk_wpas_in_bridge(dev, apdev): | |
2264 | ssid = "test-wpa2-psk" | |
2265 | passphrase = 'qwertyuiop' | |
2266 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
8b8a1864 | 2267 | hapd = hostapd.add_ap(apdev[0], params) |
a1512a0c JM |
2268 | |
2269 | br_ifname='sta-br0' | |
2270 | ifname='wlan5' | |
2271 | wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5') | |
2272 | subprocess.call(['brctl', 'addbr', br_ifname]) | |
2273 | subprocess.call(['brctl', 'setfd', br_ifname, '0']) | |
2274 | subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'up']) | |
2275 | subprocess.call(['iw', ifname, 'set', '4addr', 'on']) | |
2276 | subprocess.check_call(['brctl', 'addif', br_ifname, ifname]) | |
2277 | wpas.interface_add(ifname, br_ifname=br_ifname) | |
4b9d79b6 | 2278 | wpas.dump_monitor() |
a1512a0c JM |
2279 | |
2280 | wpas.connect(ssid, psk=passphrase, scan_freq="2412") | |
4b9d79b6 | 2281 | wpas.dump_monitor() |
eb88a5ba | 2282 | |
9fd6804d | 2283 | @remote_compatible |
eb88a5ba JM |
2284 | def test_ap_wpa2_psk_ifdown(dev, apdev): |
2285 | """AP with open mode and external ifconfig down""" | |
2286 | ssid = "test-wpa2-psk" | |
2287 | passphrase = 'qwertyuiop' | |
2288 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
8b8a1864 | 2289 | hapd = hostapd.add_ap(apdev[0], params) |
eb88a5ba JM |
2290 | bssid = apdev[0]['bssid'] |
2291 | ||
2292 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412") | |
10e09d83 | 2293 | hapd.cmd_execute(['ip', 'link', 'set', 'dev', apdev[0]['ifname'], 'down']) |
eb88a5ba JM |
2294 | ev = hapd.wait_event(["INTERFACE-DISABLED"], timeout=10) |
2295 | if ev is None: | |
2296 | raise Exception("No INTERFACE-DISABLED event") | |
2297 | # this wait tests beacon loss detection in mac80211 | |
2298 | dev[0].wait_disconnected() | |
10e09d83 | 2299 | hapd.cmd_execute(['ip', 'link', 'set', 'dev', apdev[0]['ifname'], 'up']) |
eb88a5ba JM |
2300 | ev = hapd.wait_event(["INTERFACE-ENABLED"], timeout=10) |
2301 | if ev is None: | |
2302 | raise Exception("No INTERFACE-ENABLED event") | |
2303 | dev[0].wait_connected() | |
2304 | hwsim_utils.test_connectivity(dev[0], hapd) | |
0f74bd41 JM |
2305 | |
2306 | def test_ap_wpa2_psk_drop_first_msg_4(dev, apdev): | |
2307 | """WPA2-PSK and first EAPOL-Key msg 4/4 dropped""" | |
2308 | bssid = apdev[0]['bssid'] | |
2309 | ssid = "test-wpa2-psk" | |
2310 | passphrase = 'qwertyuiop' | |
2311 | psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6' | |
2312 | params = hostapd.wpa2_params(ssid=ssid) | |
2313 | params['wpa_psk'] = psk | |
8b8a1864 | 2314 | hapd = hostapd.add_ap(apdev[0], params) |
0f74bd41 JM |
2315 | hapd.request("SET ext_eapol_frame_io 1") |
2316 | dev[0].request("SET ext_eapol_frame_io 1") | |
2317 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412", wait_connect=False) | |
2318 | addr = dev[0].own_addr() | |
2319 | ||
2320 | # EAPOL-Key msg 1/4 | |
2321 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
2322 | if ev is None: | |
2323 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
2324 | res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2]) | |
2325 | if "OK" not in res: | |
2326 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
2327 | ||
2328 | # EAPOL-Key msg 2/4 | |
2329 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=15) | |
2330 | if ev is None: | |
2331 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
2332 | res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2]) | |
2333 | if "OK" not in res: | |
2334 | raise Exception("EAPOL_RX to hostapd failed") | |
2335 | ||
2336 | # EAPOL-Key msg 3/4 | |
2337 | ev = hapd.wait_event(["EAPOL-TX"], timeout=15) | |
2338 | if ev is None: | |
2339 | raise Exception("Timeout on EAPOL-TX from hostapd") | |
2340 | res = dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2]) | |
2341 | if "OK" not in res: | |
2342 | raise Exception("EAPOL_RX to wpa_supplicant failed") | |
2343 | ||
2344 | # EAPOL-Key msg 4/4 | |
2345 | ev = dev[0].wait_event(["EAPOL-TX"], timeout=15) | |
2346 | if ev is None: | |
2347 | raise Exception("Timeout on EAPOL-TX from wpa_supplicant") | |
2348 | logger.info("Drop the first EAPOL-Key msg 4/4") | |
2349 | ||
2350 | # wpa_supplicant believes now that 4-way handshake succeeded; hostapd | |
2351 | # doesn't. Use normal EAPOL TX/RX to handle retries. | |
2352 | hapd.request("SET ext_eapol_frame_io 0") | |
2353 | dev[0].request("SET ext_eapol_frame_io 0") | |
2354 | dev[0].wait_connected() | |
2355 | ||
2356 | ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=15) | |
2357 | if ev is None: | |
2358 | raise Exception("Timeout on AP-STA-CONNECTED from hostapd") | |
2359 | ||
2360 | ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.1) | |
2361 | if ev is not None: | |
2362 | logger.info("Disconnection detected") | |
2363 | # The EAPOL-Key retries are supposed to allow the connection to be | |
2364 | # established without having to reassociate. However, this does not | |
2365 | # currently work since mac80211 ends up encrypting EAPOL-Key msg 4/4 | |
2366 | # after the pairwise key has been configured and AP will drop those and | |
2367 | # disconnect the station after reaching retransmission limit. Connection | |
2368 | # is then established after reassociation. Once that behavior has been | |
2369 | # optimized to prevent EAPOL-Key frame encryption for retransmission | |
2370 | # case, this exception can be uncommented here. | |
2371 | #raise Exception("Unexpected disconnection") | |
a14a5f24 | 2372 | |
9fd6804d | 2373 | @remote_compatible |
a14a5f24 JM |
2374 | def test_ap_wpa2_psk_disable_enable(dev, apdev): |
2375 | """WPA2-PSK AP getting disabled and re-enabled""" | |
2376 | ssid = "test-wpa2-psk" | |
2377 | passphrase = 'qwertyuiop' | |
2378 | psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6' | |
2379 | params = hostapd.wpa2_params(ssid=ssid) | |
2380 | params['wpa_psk'] = psk | |
8b8a1864 | 2381 | hapd = hostapd.add_ap(apdev[0], params) |
a14a5f24 JM |
2382 | dev[0].connect(ssid, raw_psk=psk, scan_freq="2412") |
2383 | ||
2384 | for i in range(2): | |
2385 | hapd.request("DISABLE") | |
2386 | dev[0].wait_disconnected() | |
2387 | hapd.request("ENABLE") | |
2388 | dev[0].wait_connected() | |
2389 | hwsim_utils.test_connectivity(dev[0], hapd) | |
97c6d0d8 | 2390 | |
9fd6804d | 2391 | @remote_compatible |
97c6d0d8 JM |
2392 | def test_ap_wpa2_psk_incorrect_passphrase(dev, apdev): |
2393 | """WPA2-PSK AP and station using incorrect passphrase""" | |
2394 | ssid = "test-wpa2-psk" | |
2395 | passphrase = 'qwertyuiop' | |
2396 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
8b8a1864 | 2397 | hapd = hostapd.add_ap(apdev[0], params) |
97c6d0d8 JM |
2398 | dev[0].connect(ssid, psk="incorrect passphrase", scan_freq="2412", |
2399 | wait_connect=False) | |
2400 | ev = hapd.wait_event(["AP-STA-POSSIBLE-PSK-MISMATCH"], timeout=10) | |
2401 | if ev is None: | |
2402 | raise Exception("No AP-STA-POSSIBLE-PSK-MISMATCH reported") | |
a539d3f7 JM |
2403 | dev[0].dump_monitor() |
2404 | ||
2405 | hapd.disable() | |
2406 | hapd.set("wpa_passphrase", "incorrect passphrase") | |
2407 | hapd.enable() | |
2408 | ||
2409 | dev[0].wait_connected(timeout=20) | |
4b0e0c53 | 2410 | |
9fd6804d | 2411 | @remote_compatible |
4b0e0c53 JM |
2412 | def test_ap_wpa_ie_parsing(dev, apdev): |
2413 | """WPA IE parsing""" | |
a1eabc74 | 2414 | skip_with_fips(dev[0]) |
4b0e0c53 JM |
2415 | ssid = "test-wpa-psk" |
2416 | passphrase = 'qwertyuiop' | |
2417 | params = hostapd.wpa_params(ssid=ssid, passphrase=passphrase) | |
8b8a1864 | 2418 | hapd = hostapd.add_ap(apdev[0], params) |
4b0e0c53 JM |
2419 | id = dev[0].connect(ssid, psk=passphrase, scan_freq="2412", |
2420 | only_add_network=True) | |
2421 | ||
2422 | tests = [ "dd040050f201", | |
2423 | "dd050050f20101", | |
2424 | "dd060050f2010100", | |
2425 | "dd060050f2010001", | |
2426 | "dd070050f201010000", | |
2427 | "dd080050f20101000050", | |
2428 | "dd090050f20101000050f2", | |
2429 | "dd0a0050f20101000050f202", | |
2430 | "dd0b0050f20101000050f20201", | |
2431 | "dd0c0050f20101000050f2020100", | |
2432 | "dd0c0050f20101000050f2020000", | |
2433 | "dd0c0050f20101000050f202ffff", | |
2434 | "dd0d0050f20101000050f202010000", | |
2435 | "dd0e0050f20101000050f20201000050", | |
2436 | "dd0f0050f20101000050f20201000050f2", | |
2437 | "dd100050f20101000050f20201000050f202", | |
2438 | "dd110050f20101000050f20201000050f20201", | |
2439 | "dd120050f20101000050f20201000050f2020100", | |
2440 | "dd120050f20101000050f20201000050f2020000", | |
2441 | "dd120050f20101000050f20201000050f202ffff", | |
2442 | "dd130050f20101000050f20201000050f202010000", | |
2443 | "dd140050f20101000050f20201000050f20201000050", | |
2444 | "dd150050f20101000050f20201000050f20201000050f2" ] | |
2445 | for t in tests: | |
2446 | try: | |
2447 | if "OK" not in dev[0].request("VENDOR_ELEM_ADD 13 " + t): | |
2448 | raise Exception("VENDOR_ELEM_ADD failed") | |
2449 | dev[0].select_network(id) | |
2450 | ev = dev[0].wait_event(["CTRL-EVENT-ASSOC-REJECT"], timeout=10) | |
2451 | if ev is None: | |
2452 | raise Exception("Association rejection not reported") | |
2453 | dev[0].request("DISCONNECT") | |
a359c7bb | 2454 | dev[0].dump_monitor() |
4b0e0c53 JM |
2455 | finally: |
2456 | dev[0].request("VENDOR_ELEM_REMOVE 13 *") | |
2457 | ||
2458 | tests = [ "dd170050f20101000050f20201000050f20201000050f202ff", | |
2459 | "dd180050f20101000050f20201000050f20201000050f202ffff", | |
2460 | "dd190050f20101000050f20201000050f20201000050f202ffffff" ] | |
2461 | for t in tests: | |
2462 | try: | |
2463 | if "OK" not in dev[0].request("VENDOR_ELEM_ADD 13 " + t): | |
2464 | raise Exception("VENDOR_ELEM_ADD failed") | |
2465 | dev[0].select_network(id) | |
2466 | dev[0].wait_connected() | |
2467 | dev[0].request("DISCONNECT") | |
a359c7bb | 2468 | dev[0].dump_monitor() |
4b0e0c53 JM |
2469 | finally: |
2470 | dev[0].request("VENDOR_ELEM_REMOVE 13 *") | |
bf7071bb | 2471 | |
9fd6804d | 2472 | @remote_compatible |
bf7071bb JM |
2473 | def test_ap_wpa2_psk_no_random(dev, apdev): |
2474 | """WPA2-PSK AP and no random numbers available""" | |
2475 | ssid = "test-wpa2-psk" | |
2476 | passphrase = 'qwertyuiop' | |
2477 | psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6' | |
2478 | params = hostapd.wpa2_params(ssid=ssid) | |
2479 | params['wpa_psk'] = psk | |
8b8a1864 | 2480 | hapd = hostapd.add_ap(apdev[0], params) |
bf7071bb JM |
2481 | with fail_test(hapd, 1, "wpa_gmk_to_gtk"): |
2482 | id = dev[0].connect(ssid, raw_psk=psk, scan_freq="2412", | |
2483 | wait_connect=False) | |
2484 | ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=15) | |
2485 | if ev is None: | |
2486 | raise Exception("Disconnection event not reported") | |
2487 | dev[0].request("DISCONNECT") | |
2488 | dev[0].select_network(id, freq=2412) | |
2489 | dev[0].wait_connected() | |
ecafa0cf | 2490 | |
9fd6804d | 2491 | @remote_compatible |
ecafa0cf JM |
2492 | def test_rsn_ie_proto_psk_sta(dev, apdev): |
2493 | """RSN element protocol testing for PSK cases on STA side""" | |
2494 | bssid = apdev[0]['bssid'] | |
2495 | ssid = "test-wpa2-psk" | |
2496 | passphrase = 'qwertyuiop' | |
2497 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
2498 | # This is the RSN element used normally by hostapd | |
2499 | params['own_ie_override'] = '30140100000fac040100000fac040100000fac020c00' | |
8b8a1864 | 2500 | hapd = hostapd.add_ap(apdev[0], params) |
ecafa0cf JM |
2501 | if "FAIL" not in hapd.request("SET own_ie_override qwerty"): |
2502 | raise Exception("Invalid own_ie_override value accepted") | |
2503 | id = dev[0].connect(ssid, psk=passphrase, scan_freq="2412") | |
2504 | ||
2505 | tests = [ ('No RSN Capabilities field', | |
2506 | '30120100000fac040100000fac040100000fac02'), | |
2507 | ('Reserved RSN Capabilities bits set', | |
2508 | '30140100000fac040100000fac040100000fac023cff'), | |
2509 | ('Extra pairwise cipher suite (unsupported)', | |
2510 | '30180100000fac040200ffffffff000fac040100000fac020c00'), | |
2511 | ('Extra AKM suite (unsupported)', | |
2512 | '30180100000fac040100000fac040200ffffffff000fac020c00'), | |
2513 | ('PMKIDCount field included', | |
2514 | '30160100000fac040100000fac040100000fac020c000000'), | |
2515 | ('Unexpected Group Management Cipher Suite with PMF disabled', | |
2516 | '301a0100000fac040100000fac040100000fac020c000000000fac06'), | |
2517 | ('Extra octet after defined fields (future extensibility)', | |
2518 | '301b0100000fac040100000fac040100000fac020c000000000fac0600') ] | |
2519 | for txt,ie in tests: | |
2520 | dev[0].request("DISCONNECT") | |
2521 | dev[0].wait_disconnected() | |
2522 | logger.info(txt) | |
2523 | hapd.disable() | |
2524 | hapd.set('own_ie_override', ie) | |
2525 | hapd.enable() | |
2526 | dev[0].request("BSS_FLUSH 0") | |
2527 | dev[0].scan_for_bss(bssid, 2412, force_scan=True, only_new=True) | |
2528 | dev[0].select_network(id, freq=2412) | |
2529 | dev[0].wait_connected() | |
b1f69186 | 2530 | |
9fd6804d | 2531 | @remote_compatible |
b1f69186 JB |
2532 | def test_ap_cli_order(dev, apdev): |
2533 | ssid = "test-rsn-setup" | |
2534 | passphrase = 'zzzzzzzz' | |
b1f69186 | 2535 | |
84f3f3a5 | 2536 | hapd = hostapd.add_ap(apdev[0], {}, no_enable=True) |
b1f69186 JB |
2537 | hapd.set('ssid', ssid) |
2538 | hapd.set('wpa_passphrase', passphrase) | |
2539 | hapd.set('rsn_pairwise', 'CCMP') | |
2540 | hapd.set('wpa_key_mgmt', 'WPA-PSK') | |
2541 | hapd.set('wpa', '2') | |
2542 | hapd.enable() | |
2543 | cfg = hapd.get_config() | |
2544 | if cfg['group_cipher'] != 'CCMP': | |
2545 | raise Exception("Unexpected group_cipher: " + cfg['group_cipher']) | |
2546 | if cfg['rsn_pairwise_cipher'] != 'CCMP': | |
2547 | raise Exception("Unexpected rsn_pairwise_cipher: " + cfg['rsn_pairwise_cipher']) | |
2548 | ||
2549 | ev = hapd.wait_event(["AP-ENABLED", "AP-DISABLED"], timeout=30) | |
2550 | if ev is None: | |
2551 | raise Exception("AP startup timed out") | |
2552 | if "AP-ENABLED" not in ev: | |
2553 | raise Exception("AP startup failed") | |
2554 | ||
2555 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412") | |
0ceff76e JM |
2556 | |
2557 | def set_test_assoc_ie(dev, ie): | |
2558 | if "OK" not in dev.request("TEST_ASSOC_IE " + ie): | |
2559 | raise Exception("Could not set TEST_ASSOC_IE") | |
2560 | ||
9fd6804d | 2561 | @remote_compatible |
0ceff76e JM |
2562 | def test_ap_wpa2_psk_assoc_rsn(dev, apdev): |
2563 | """WPA2-PSK AP and association request RSN IE differences""" | |
2564 | ssid = "test-wpa2-psk" | |
2565 | passphrase = 'qwertyuiop' | |
2566 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
8b8a1864 | 2567 | hapd = hostapd.add_ap(apdev[0], params) |
0ceff76e JM |
2568 | |
2569 | tests = [ ("Normal wpa_supplicant assoc req RSN IE", | |
2570 | "30140100000fac040100000fac040100000fac020000"), | |
2571 | ("RSN IE without RSN Capabilities", | |
2572 | "30120100000fac040100000fac040100000fac02") ] | |
2573 | for title, ie in tests: | |
2574 | logger.info(title) | |
2575 | set_test_assoc_ie(dev[0], ie) | |
2576 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412") | |
2577 | dev[0].request("REMOVE_NETWORK all") | |
2578 | dev[0].wait_disconnected() | |
2579 | ||
2580 | tests = [ ("WPA IE instead of RSN IE and only RSN enabled on AP", | |
2581 | "dd160050f20101000050f20201000050f20201000050f202", 40), | |
2582 | ("Empty RSN IE", "3000", 40), | |
2583 | ("RSN IE with truncated Version", "300101", 40), | |
2584 | ("RSN IE with only Version", "30020100", 43) ] | |
2585 | for title, ie, status in tests: | |
2586 | logger.info(title) | |
2587 | set_test_assoc_ie(dev[0], ie) | |
2588 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412", | |
2589 | wait_connect=False) | |
2590 | ev = dev[0].wait_event(["CTRL-EVENT-ASSOC-REJECT"]) | |
2591 | if ev is None: | |
2592 | raise Exception("Association rejection not reported") | |
2593 | if "status_code=" + str(status) not in ev: | |
2594 | raise Exception("Unexpected status code: " + ev) | |
2595 | dev[0].request("REMOVE_NETWORK all") | |
2596 | dev[0].dump_monitor() | |
50bb5c86 JM |
2597 | |
2598 | def test_ap_wpa_psk_rsn_pairwise(dev, apdev): | |
2599 | """WPA-PSK AP and only rsn_pairwise set""" | |
2600 | params = { "ssid": "wpapsk", "wpa": "1", "wpa_key_mgmt": "WPA-PSK", | |
2601 | "rsn_pairwise": "TKIP", "wpa_passphrase": "1234567890" } | |
2602 | hapd = hostapd.add_ap(apdev[0], params) | |
2603 | dev[0].connect("wpapsk", psk="1234567890", proto="WPA", pairwise="TKIP", | |
2604 | scan_freq="2412") | |
ac723b35 JM |
2605 | |
2606 | def test_ap_wpa2_eapol_retry_limit(dev, apdev): | |
2607 | """WPA2-PSK EAPOL-Key retry limit configuration""" | |
2608 | ssid = "test-wpa2-psk" | |
2609 | passphrase = 'qwertyuiop' | |
2610 | params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase) | |
2611 | params['wpa_ptk_rekey'] = '2' | |
2612 | params['wpa_group_update_count'] = '1' | |
2613 | params['wpa_pairwise_update_count'] = '1' | |
2614 | hapd = hostapd.add_ap(apdev[0], params) | |
2615 | dev[0].connect(ssid, psk=passphrase, scan_freq="2412") | |
2616 | ev = dev[0].wait_event(["WPA: Key negotiation completed"]) | |
2617 | if ev is None: | |
2618 | raise Exception("PTK rekey timed out") | |
2619 | ||
2620 | if "FAIL" not in hapd.request("SET wpa_group_update_count 0"): | |
2621 | raise Exception("Invalid wpa_group_update_count value accepted") | |
2622 | if "FAIL" not in hapd.request("SET wpa_pairwise_update_count 0"): | |
2623 | raise Exception("Invalid wpa_pairwise_update_count value accepted") |