]> git.ipfire.org Git - thirdparty/hostap.git/blame - tests/hwsim/test_ap_eap.py
tests: Use time-travel=inf-cpu with UML
[thirdparty/hostap.git] / tests / hwsim / test_ap_eap.py
CommitLineData
eac67440 1# -*- coding: utf-8 -*-
9626962d 2# WPA2-Enterprise tests
fb643190 3# Copyright (c) 2013-2019, Jouni Malinen <j@w1.fi>
9626962d
JM
4#
5# This software may be distributed under the terms of the BSD license.
6# See README for more details.
7
6ea231e6 8import base64
5b3c40a6 9import binascii
9626962d
JM
10import time
11import subprocess
12import logging
c9aa4308 13logger = logging.getLogger()
873e7c29 14import os
c9aba19b 15import signal
d4c3c055 16import socket
9c06eda0
MH
17try:
18 import SocketServer
19except ImportError:
20 import socketserver as SocketServer
98d125ca
JM
21import struct
22import tempfile
9626962d
JM
23
24import hwsim_utils
67e34a28 25from hwsim import HWSimRadio
9626962d 26import hostapd
67e34a28 27from utils import HwsimSkip, alloc_fail, fail_test, skip_with_fips, wait_fail_trigger, require_under_vm
52352802 28from wpasupplicant import WpaSupplicant
0ceff76e 29from test_ap_psk import check_mib, find_wpas_process, read_process_memory, verify_not_present, get_key_locations, set_test_assoc_ie
9626962d 30
ca27ee09
JM
31try:
32 import OpenSSL
33 openssl_imported = True
34except ImportError:
35 openssl_imported = False
36
81e787b7
JM
37def check_hlr_auc_gw_support():
38 if not os.path.exists("/tmp/hlr_auc_gw.sock"):
39 raise HwsimSkip("No hlr_auc_gw available")
40
3b51cc63
JM
41def check_eap_capa(dev, method):
42 res = dev.get_capability("eap")
43 if method not in res:
44 raise HwsimSkip("EAP method %s not supported in the build" % method)
45
506b2f05
JM
46def check_subject_match_support(dev):
47 tls = dev.request("GET tls_library")
d8003dcb 48 if not tls.startswith("OpenSSL") and not tls.startswith("wolfSSL"):
506b2f05
JM
49 raise HwsimSkip("subject_match not supported with this TLS library: " + tls)
50
f4f17e9a
JM
51def check_check_cert_subject_support(dev):
52 tls = dev.request("GET tls_library")
53 if not tls.startswith("OpenSSL"):
54 raise HwsimSkip("check_cert_subject not supported with this TLS library: " + tls)
55
506b2f05
JM
56def check_altsubject_match_support(dev):
57 tls = dev.request("GET tls_library")
d8003dcb 58 if not tls.startswith("OpenSSL") and not tls.startswith("wolfSSL"):
506b2f05
JM
59 raise HwsimSkip("altsubject_match not supported with this TLS library: " + tls)
60
e78eb404
JM
61def check_domain_match(dev):
62 tls = dev.request("GET tls_library")
63 if tls.startswith("internal"):
64 raise HwsimSkip("domain_match not supported with this TLS library: " + tls)
65
66def check_domain_suffix_match(dev):
67 tls = dev.request("GET tls_library")
68 if tls.startswith("internal"):
69 raise HwsimSkip("domain_suffix_match not supported with this TLS library: " + tls)
70
24579e70
JM
71def check_domain_match_full(dev):
72 tls = dev.request("GET tls_library")
d8003dcb 73 if not tls.startswith("OpenSSL") and not tls.startswith("wolfSSL"):
24579e70
JM
74 raise HwsimSkip("domain_suffix_match requires full match with this TLS library: " + tls)
75
4bf4e9db
JM
76def check_cert_probe_support(dev):
77 tls = dev.request("GET tls_library")
0fc1b583 78 if not tls.startswith("OpenSSL") and not tls.startswith("internal"):
4bf4e9db
JM
79 raise HwsimSkip("Certificate probing not supported with this TLS library: " + tls)
80
ca27ee09
JM
81def check_ext_cert_check_support(dev):
82 tls = dev.request("GET tls_library")
83 if not tls.startswith("OpenSSL"):
84 raise HwsimSkip("ext_cert_check not supported with this TLS library: " + tls)
85
0dae8c99
JM
86def check_ocsp_support(dev):
87 tls = dev.request("GET tls_library")
138903f9
JM
88 #if tls.startswith("internal"):
89 # raise HwsimSkip("OCSP not supported with this TLS library: " + tls)
0c6185fc
JM
90 #if "BoringSSL" in tls:
91 # raise HwsimSkip("OCSP not supported with this TLS library: " + tls)
585e728a
JM
92 if tls.startswith("wolfSSL"):
93 raise HwsimSkip("OCSP not supported with this TLS library: " + tls)
0dae8c99 94
969e5250
JM
95def check_pkcs5_v15_support(dev):
96 tls = dev.request("GET tls_library")
2d9ad634 97 if "BoringSSL" in tls or "GnuTLS" in tls:
969e5250
JM
98 raise HwsimSkip("PKCS#5 v1.5 not supported with this TLS library: " + tls)
99
98d125ca
JM
100def check_ocsp_multi_support(dev):
101 tls = dev.request("GET tls_library")
102 if not tls.startswith("internal"):
103 raise HwsimSkip("OCSP-multi not supported with this TLS library: " + tls)
104 as_hapd = hostapd.Hostapd("as")
105 res = as_hapd.request("GET tls_library")
106 del as_hapd
107 if not res.startswith("internal"):
108 raise HwsimSkip("Authentication server does not support ocsp_multi")
109
686eee77
JM
110def check_pkcs12_support(dev):
111 tls = dev.request("GET tls_library")
16c43d2a
JM
112 #if tls.startswith("internal"):
113 # raise HwsimSkip("PKCS#12 not supported with this TLS library: " + tls)
d8003dcb
SP
114 if tls.startswith("wolfSSL"):
115 raise HwsimSkip("PKCS#12 not supported with this TLS library: " + tls)
686eee77 116
404597e6
JM
117def check_dh_dsa_support(dev):
118 tls = dev.request("GET tls_library")
119 if tls.startswith("internal"):
120 raise HwsimSkip("DH DSA not supported with this TLS library: " + tls)
121
6ea231e6
JM
122def read_pem(fname):
123 with open(fname, "r") as f:
124 lines = f.readlines()
125 copy = False
126 cert = ""
127 for l in lines:
128 if "-----END" in l:
129 break
130 if copy:
131 cert = cert + l
132 if "-----BEGIN" in l:
133 copy = True
134 return base64.b64decode(cert)
135
3b3e2687 136def eap_connect(dev, hapd, method, identity,
6f939e59 137 sha256=False, expect_failure=False, local_error_report=False,
f4f17e9a
JM
138 maybe_local_error=False, report_failure=False,
139 expect_cert_error=None, **kwargs):
2bb9e283
JM
140 id = dev.connect("test-wpa2-eap", key_mgmt="WPA-EAP WPA-EAP-SHA256",
141 eap=method, identity=identity,
6f939e59
JM
142 wait_connect=False, scan_freq="2412", ieee80211w="1",
143 **kwargs)
f10ba3b2
JM
144 eap_check_auth(dev, method, True, sha256=sha256,
145 expect_failure=expect_failure,
9dd21d51 146 local_error_report=local_error_report,
a61ee84d 147 maybe_local_error=maybe_local_error,
f4f17e9a
JM
148 report_failure=report_failure,
149 expect_cert_error=expect_cert_error)
f10ba3b2
JM
150 if expect_failure:
151 return id
fab49f61 152 ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=5)
cb33ee14
JM
153 if ev is None:
154 raise Exception("No connection event received from hostapd")
2bb9e283 155 return id
75b2b9cf 156
f10ba3b2 157def eap_check_auth(dev, method, initial, rsn=True, sha256=False,
9dd21d51 158 expect_failure=False, local_error_report=False,
f4f17e9a
JM
159 maybe_local_error=False, report_failure=False,
160 expect_cert_error=None):
412c6030 161 ev = dev.wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=16)
9626962d
JM
162 if ev is None:
163 raise Exception("Association and EAP start timed out")
06cdd1cd
JM
164 ev = dev.wait_event(["CTRL-EVENT-EAP-METHOD",
165 "CTRL-EVENT-EAP-FAILURE"], timeout=10)
9626962d
JM
166 if ev is None:
167 raise Exception("EAP method selection timed out")
06cdd1cd
JM
168 if "CTRL-EVENT-EAP-FAILURE" in ev:
169 if maybe_local_error:
170 return
171 raise Exception("Could not select EAP method")
9626962d
JM
172 if method not in ev:
173 raise Exception("Unexpected EAP method")
f4f17e9a
JM
174 if expect_cert_error is not None:
175 ev = dev.wait_event(["CTRL-EVENT-EAP-TLS-CERT-ERROR",
176 "CTRL-EVENT-EAP-FAILURE",
177 "CTRL-EVENT-EAP-SUCCESS"], timeout=5)
178 if ev is None or "reason=%d " % expect_cert_error not in ev:
179 raise Exception("Expected certificate error not reported")
f10ba3b2 180 if expect_failure:
f4f17e9a
JM
181 ev = dev.wait_event(["CTRL-EVENT-EAP-FAILURE",
182 "CTRL-EVENT-EAP-SUCCESS"], timeout=5)
f10ba3b2
JM
183 if ev is None:
184 raise Exception("EAP failure timed out")
f4f17e9a
JM
185 if "CTRL-EVENT-EAP-SUCCESS" in ev:
186 raise Exception("Unexpected EAP success")
5f35a5e2 187 ev = dev.wait_disconnected(timeout=10)
9dd21d51
JM
188 if maybe_local_error and "locally_generated=1" in ev:
189 return
f10ba3b2
JM
190 if not local_error_report:
191 if "reason=23" not in ev:
192 raise Exception("Proper reason code for disconnection not reported")
193 return
a61ee84d
JM
194 if report_failure:
195 ev = dev.wait_event(["CTRL-EVENT-EAP-SUCCESS",
196 "CTRL-EVENT-EAP-FAILURE"], timeout=10)
197 if ev is None:
198 raise Exception("EAP success timed out")
199 if "CTRL-EVENT-EAP-SUCCESS" not in ev:
200 raise Exception("EAP failed")
201 else:
202 ev = dev.wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
203 if ev is None:
204 raise Exception("EAP success timed out")
9626962d 205
75b2b9cf
JM
206 if initial:
207 ev = dev.wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
75b2b9cf 208 else:
bce774ad
JM
209 ev = dev.wait_event(["WPA: Key negotiation completed"], timeout=10)
210 if ev is None:
211 raise Exception("Association with the AP timed out")
212 status = dev.get_status()
213 if status["wpa_state"] != "COMPLETED":
214 raise Exception("Connection not completed")
75b2b9cf 215
9626962d
JM
216 if status["suppPortStatus"] != "Authorized":
217 raise Exception("Port not authorized")
447fb0b0
JM
218 if "selectedMethod" not in status:
219 logger.info("Status: " + str(status))
220 raise Exception("No selectedMethod in status")
9626962d
JM
221 if method not in status["selectedMethod"]:
222 raise Exception("Incorrect EAP method status")
2b005194
JM
223 if sha256:
224 e = "WPA2-EAP-SHA256"
225 elif rsn:
71390dc8
JM
226 e = "WPA2/IEEE 802.1X/EAP"
227 else:
228 e = "WPA/IEEE 802.1X/EAP"
229 if status["key_mgmt"] != e:
230 raise Exception("Unexpected key_mgmt status: " + status["key_mgmt"])
2fc4749c 231 return status
9626962d 232
5b1aaf6c 233def eap_reauth(dev, method, rsn=True, sha256=False, expect_failure=False):
75b2b9cf 234 dev.request("REAUTHENTICATE")
2fc4749c
JM
235 return eap_check_auth(dev, method, False, rsn=rsn, sha256=sha256,
236 expect_failure=expect_failure)
75b2b9cf 237
9626962d
JM
238def test_ap_wpa2_eap_sim(dev, apdev):
239 """WPA2-Enterprise connection using EAP-SIM"""
81e787b7 240 check_hlr_auc_gw_support()
9626962d 241 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 242 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 243 eap_connect(dev[0], hapd, "SIM", "1232010000000000",
9626962d 244 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581")
a8375c94 245 hwsim_utils.test_connectivity(dev[0], hapd)
75b2b9cf 246 eap_reauth(dev[0], "SIM")
9626962d 247
3b3e2687 248 eap_connect(dev[1], hapd, "SIM", "1232010000000001",
a0f350fd 249 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581")
3b3e2687 250 eap_connect(dev[2], hapd, "SIM", "1232010000000002",
a0f350fd
JM
251 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581",
252 expect_failure=True)
253
f10ba3b2
JM
254 logger.info("Negative test with incorrect key")
255 dev[0].request("REMOVE_NETWORK all")
3b3e2687 256 eap_connect(dev[0], hapd, "SIM", "1232010000000000",
f10ba3b2
JM
257 password="ffdca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581",
258 expect_failure=True)
259
32747a3e
JM
260 logger.info("Invalid GSM-Milenage key")
261 dev[0].request("REMOVE_NETWORK all")
3b3e2687 262 eap_connect(dev[0], hapd, "SIM", "1232010000000000",
32747a3e
JM
263 password="ffdca4eda45b53cf0f12d7c9c3bc6a",
264 expect_failure=True)
265
266 logger.info("Invalid GSM-Milenage key(2)")
267 dev[0].request("REMOVE_NETWORK all")
3b3e2687 268 eap_connect(dev[0], hapd, "SIM", "1232010000000000",
32747a3e
JM
269 password="ffdca4eda45b53cf0f12d7c9c3bc6a8q:cb9cccc4b9258e6dca4760379fb82581",
270 expect_failure=True)
271
272 logger.info("Invalid GSM-Milenage key(3)")
273 dev[0].request("REMOVE_NETWORK all")
3b3e2687 274 eap_connect(dev[0], hapd, "SIM", "1232010000000000",
32747a3e
JM
275 password="ffdca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb8258q",
276 expect_failure=True)
277
278 logger.info("Invalid GSM-Milenage key(4)")
279 dev[0].request("REMOVE_NETWORK all")
3b3e2687 280 eap_connect(dev[0], hapd, "SIM", "1232010000000000",
32747a3e
JM
281 password="ffdca4eda45b53cf0f12d7c9c3bc6a89qcb9cccc4b9258e6dca4760379fb82581",
282 expect_failure=True)
283
284 logger.info("Missing key configuration")
285 dev[0].request("REMOVE_NETWORK all")
3b3e2687 286 eap_connect(dev[0], hapd, "SIM", "1232010000000000",
32747a3e
JM
287 expect_failure=True)
288
5b1aaf6c
JM
289def test_ap_wpa2_eap_sim_sql(dev, apdev, params):
290 """WPA2-Enterprise connection using EAP-SIM (SQL)"""
81e787b7 291 check_hlr_auc_gw_support()
5b1aaf6c
JM
292 try:
293 import sqlite3
294 except ImportError:
81e787b7 295 raise HwsimSkip("No sqlite3 module available")
5b1aaf6c
JM
296 con = sqlite3.connect(os.path.join(params['logdir'], "hostapd.db"))
297 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
298 params['auth_server_port'] = "1814"
3b3e2687
JD
299 hapd = hostapd.add_ap(apdev[0], params)
300 eap_connect(dev[0], hapd, "SIM", "1232010000000000",
5b1aaf6c
JM
301 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581")
302
303 logger.info("SIM fast re-authentication")
304 eap_reauth(dev[0], "SIM")
305
306 logger.info("SIM full auth with pseudonym")
307 with con:
308 cur = con.cursor()
309 cur.execute("DELETE FROM reauth WHERE permanent='1232010000000000'")
310 eap_reauth(dev[0], "SIM")
311
312 logger.info("SIM full auth with permanent identity")
313 with con:
314 cur = con.cursor()
315 cur.execute("DELETE FROM reauth WHERE permanent='1232010000000000'")
316 cur.execute("DELETE FROM pseudonyms WHERE permanent='1232010000000000'")
317 eap_reauth(dev[0], "SIM")
318
319 logger.info("SIM reauth with mismatching MK")
320 with con:
321 cur = con.cursor()
322 cur.execute("UPDATE reauth SET mk='0000000000000000000000000000000000000000' WHERE permanent='1232010000000000'")
323 eap_reauth(dev[0], "SIM", expect_failure=True)
324 dev[0].request("REMOVE_NETWORK all")
325
3b3e2687 326 eap_connect(dev[0], hapd, "SIM", "1232010000000000",
5b1aaf6c
JM
327 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581")
328 with con:
329 cur = con.cursor()
330 cur.execute("UPDATE reauth SET counter='10' WHERE permanent='1232010000000000'")
331 eap_reauth(dev[0], "SIM")
332 with con:
333 cur = con.cursor()
334 cur.execute("UPDATE reauth SET counter='10' WHERE permanent='1232010000000000'")
335 logger.info("SIM reauth with mismatching counter")
336 eap_reauth(dev[0], "SIM")
337 dev[0].request("REMOVE_NETWORK all")
338
3b3e2687 339 eap_connect(dev[0], hapd, "SIM", "1232010000000000",
5b1aaf6c
JM
340 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581")
341 with con:
342 cur = con.cursor()
343 cur.execute("UPDATE reauth SET counter='1001' WHERE permanent='1232010000000000'")
344 logger.info("SIM reauth with max reauth count reached")
345 eap_reauth(dev[0], "SIM")
346
e2a90a4c
JM
347def test_ap_wpa2_eap_sim_config(dev, apdev):
348 """EAP-SIM configuration options"""
349 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687 350 hapd = hostapd.add_ap(apdev[0], params)
e2a90a4c
JM
351 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="SIM",
352 identity="1232010000000000",
353 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581",
354 phase1="sim_min_num_chal=1",
355 wait_connect=False, scan_freq="2412")
356 ev = dev[0].wait_event(["EAP: Failed to initialize EAP method: vendor 0 method 18 (SIM)"], timeout=10)
357 if ev is None:
358 raise Exception("No EAP error message seen")
359 dev[0].request("REMOVE_NETWORK all")
360
361 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="SIM",
362 identity="1232010000000000",
363 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581",
364 phase1="sim_min_num_chal=4",
365 wait_connect=False, scan_freq="2412")
366 ev = dev[0].wait_event(["EAP: Failed to initialize EAP method: vendor 0 method 18 (SIM)"], timeout=10)
367 if ev is None:
368 raise Exception("No EAP error message seen (2)")
369 dev[0].request("REMOVE_NETWORK all")
370
3b3e2687 371 eap_connect(dev[0], hapd, "SIM", "1232010000000000",
e2a90a4c
JM
372 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581",
373 phase1="sim_min_num_chal=2")
3b3e2687 374 eap_connect(dev[1], hapd, "SIM", "1232010000000000",
e2a90a4c
JM
375 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581",
376 anonymous_identity="345678")
377
72cbc684
JM
378def test_ap_wpa2_eap_sim_ext(dev, apdev):
379 """WPA2-Enterprise connection using EAP-SIM and external GSM auth"""
47dcb118 380 try:
81e787b7 381 _test_ap_wpa2_eap_sim_ext(dev, apdev)
47dcb118
JM
382 finally:
383 dev[0].request("SET external_sim 0")
384
385def _test_ap_wpa2_eap_sim_ext(dev, apdev):
81e787b7 386 check_hlr_auc_gw_support()
72cbc684 387 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 388 hostapd.add_ap(apdev[0], params)
72cbc684
JM
389 dev[0].request("SET external_sim 1")
390 id = dev[0].connect("test-wpa2-eap", eap="SIM", key_mgmt="WPA-EAP",
391 identity="1232010000000000",
392 wait_connect=False, scan_freq="2412")
393 ev = dev[0].wait_event(["CTRL-EVENT-EAP-METHOD"], timeout=15)
394 if ev is None:
395 raise Exception("Network connected timed out")
396
397 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
398 if ev is None:
399 raise Exception("Wait for external SIM processing request timed out")
400 p = ev.split(':', 2)
401 if p[1] != "GSM-AUTH":
402 raise Exception("Unexpected CTRL-REQ-SIM type")
403 rid = p[0].split('-')[3]
404
405 # IK:CK:RES
406 resp = "00112233445566778899aabbccddeeff:00112233445566778899aabbccddeeff:0011223344"
407 # This will fail during processing, but the ctrl_iface command succeeds
408 dev[0].request("CTRL-RSP-SIM-" + rid + ":UMTS-AUTH:" + resp)
409 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=15)
410 if ev is None:
411 raise Exception("EAP failure not reported")
412 dev[0].request("DISCONNECT")
90ad11e6
JM
413 dev[0].wait_disconnected()
414 time.sleep(0.1)
72cbc684
JM
415
416 dev[0].select_network(id, freq="2412")
417 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
418 if ev is None:
419 raise Exception("Wait for external SIM processing request timed out")
420 p = ev.split(':', 2)
421 if p[1] != "GSM-AUTH":
422 raise Exception("Unexpected CTRL-REQ-SIM type")
423 rid = p[0].split('-')[3]
424 # This will fail during GSM auth validation
425 if "OK" not in dev[0].request("CTRL-RSP-SIM-" + rid + ":GSM-AUTH:q"):
426 raise Exception("CTRL-RSP-SIM failed")
427 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=15)
428 if ev is None:
429 raise Exception("EAP failure not reported")
430 dev[0].request("DISCONNECT")
90ad11e6
JM
431 dev[0].wait_disconnected()
432 time.sleep(0.1)
72cbc684
JM
433
434 dev[0].select_network(id, freq="2412")
435 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
436 if ev is None:
437 raise Exception("Wait for external SIM processing request timed out")
438 p = ev.split(':', 2)
439 if p[1] != "GSM-AUTH":
440 raise Exception("Unexpected CTRL-REQ-SIM type")
441 rid = p[0].split('-')[3]
442 # This will fail during GSM auth validation
443 if "OK" not in dev[0].request("CTRL-RSP-SIM-" + rid + ":GSM-AUTH:34"):
444 raise Exception("CTRL-RSP-SIM failed")
445 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=15)
446 if ev is None:
447 raise Exception("EAP failure not reported")
448 dev[0].request("DISCONNECT")
90ad11e6
JM
449 dev[0].wait_disconnected()
450 time.sleep(0.1)
72cbc684
JM
451
452 dev[0].select_network(id, freq="2412")
453 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
454 if ev is None:
455 raise Exception("Wait for external SIM processing request timed out")
456 p = ev.split(':', 2)
457 if p[1] != "GSM-AUTH":
458 raise Exception("Unexpected CTRL-REQ-SIM type")
459 rid = p[0].split('-')[3]
460 # This will fail during GSM auth validation
461 if "OK" not in dev[0].request("CTRL-RSP-SIM-" + rid + ":GSM-AUTH:0011223344556677"):
462 raise Exception("CTRL-RSP-SIM failed")
463 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=15)
464 if ev is None:
465 raise Exception("EAP failure not reported")
466 dev[0].request("DISCONNECT")
90ad11e6
JM
467 dev[0].wait_disconnected()
468 time.sleep(0.1)
72cbc684
JM
469
470 dev[0].select_network(id, freq="2412")
471 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
472 if ev is None:
473 raise Exception("Wait for external SIM processing request timed out")
474 p = ev.split(':', 2)
475 if p[1] != "GSM-AUTH":
476 raise Exception("Unexpected CTRL-REQ-SIM type")
477 rid = p[0].split('-')[3]
478 # This will fail during GSM auth validation
479 if "OK" not in dev[0].request("CTRL-RSP-SIM-" + rid + ":GSM-AUTH:0011223344556677:q"):
480 raise Exception("CTRL-RSP-SIM failed")
481 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=15)
482 if ev is None:
483 raise Exception("EAP failure not reported")
484 dev[0].request("DISCONNECT")
90ad11e6
JM
485 dev[0].wait_disconnected()
486 time.sleep(0.1)
72cbc684
JM
487
488 dev[0].select_network(id, freq="2412")
489 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
490 if ev is None:
491 raise Exception("Wait for external SIM processing request timed out")
492 p = ev.split(':', 2)
493 if p[1] != "GSM-AUTH":
494 raise Exception("Unexpected CTRL-REQ-SIM type")
495 rid = p[0].split('-')[3]
496 # This will fail during GSM auth validation
497 if "OK" not in dev[0].request("CTRL-RSP-SIM-" + rid + ":GSM-AUTH:0011223344556677:00112233"):
498 raise Exception("CTRL-RSP-SIM failed")
499 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=15)
500 if ev is None:
501 raise Exception("EAP failure not reported")
502 dev[0].request("DISCONNECT")
90ad11e6
JM
503 dev[0].wait_disconnected()
504 time.sleep(0.1)
72cbc684
JM
505
506 dev[0].select_network(id, freq="2412")
507 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
508 if ev is None:
509 raise Exception("Wait for external SIM processing request timed out")
510 p = ev.split(':', 2)
511 if p[1] != "GSM-AUTH":
512 raise Exception("Unexpected CTRL-REQ-SIM type")
513 rid = p[0].split('-')[3]
514 # This will fail during GSM auth validation
515 if "OK" not in dev[0].request("CTRL-RSP-SIM-" + rid + ":GSM-AUTH:0011223344556677:00112233:q"):
516 raise Exception("CTRL-RSP-SIM failed")
517 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=15)
518 if ev is None:
519 raise Exception("EAP failure not reported")
520
40c654cc
JM
521def test_ap_wpa2_eap_sim_ext_replace_sim(dev, apdev):
522 """EAP-SIM with external GSM auth and replacing SIM without clearing pseudonym id"""
523 try:
524 _test_ap_wpa2_eap_sim_ext_replace_sim(dev, apdev)
525 finally:
526 dev[0].request("SET external_sim 0")
527
528def _test_ap_wpa2_eap_sim_ext_replace_sim(dev, apdev):
529 check_hlr_auc_gw_support()
530 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 531 hostapd.add_ap(apdev[0], params)
40c654cc
JM
532 dev[0].request("SET external_sim 1")
533 id = dev[0].connect("test-wpa2-eap", eap="SIM", key_mgmt="WPA-EAP",
534 identity="1232010000000000",
535 wait_connect=False, scan_freq="2412")
536
537 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
538 if ev is None:
539 raise Exception("Wait for external SIM processing request timed out")
540 p = ev.split(':', 2)
541 if p[1] != "GSM-AUTH":
542 raise Exception("Unexpected CTRL-REQ-SIM type")
543 rid = p[0].split('-')[3]
544 rand = p[2].split(' ')[0]
545
546 res = subprocess.check_output(["../../hostapd/hlr_auc_gw",
547 "-m",
548 "auth_serv/hlr_auc_gw.milenage_db",
d5e6ffd6 549 "GSM-AUTH-REQ 232010000000000 " + rand]).decode()
40c654cc
JM
550 if "GSM-AUTH-RESP" not in res:
551 raise Exception("Unexpected hlr_auc_gw response")
552 resp = res.split(' ')[2].rstrip()
553
554 dev[0].request("CTRL-RSP-SIM-" + rid + ":GSM-AUTH:" + resp)
555 dev[0].wait_connected(timeout=15)
556 dev[0].request("DISCONNECT")
557 dev[0].wait_disconnected()
558
559 # Replace SIM, but forget to drop the previous pseudonym identity
560 dev[0].set_network_quoted(id, "identity", "1232010000000009")
561 dev[0].select_network(id, freq="2412")
562
563 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
564 if ev is None:
565 raise Exception("Wait for external SIM processing request timed out")
566 p = ev.split(':', 2)
567 if p[1] != "GSM-AUTH":
568 raise Exception("Unexpected CTRL-REQ-SIM type")
569 rid = p[0].split('-')[3]
570 rand = p[2].split(' ')[0]
571
572 res = subprocess.check_output(["../../hostapd/hlr_auc_gw",
573 "-m",
574 "auth_serv/hlr_auc_gw.milenage_db",
d5e6ffd6 575 "GSM-AUTH-REQ 232010000000009 " + rand]).decode()
40c654cc
JM
576 if "GSM-AUTH-RESP" not in res:
577 raise Exception("Unexpected hlr_auc_gw response")
578 resp = res.split(' ')[2].rstrip()
579
580 dev[0].request("CTRL-RSP-SIM-" + rid + ":GSM-AUTH:" + resp)
581 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=15)
582 if ev is None:
583 raise Exception("EAP-Failure not reported")
584 dev[0].request("DISCONNECT")
585 dev[0].wait_disconnected()
586
587def test_ap_wpa2_eap_sim_ext_replace_sim2(dev, apdev):
588 """EAP-SIM with external GSM auth and replacing SIM and clearing pseudonym identity"""
589 try:
590 _test_ap_wpa2_eap_sim_ext_replace_sim2(dev, apdev)
591 finally:
592 dev[0].request("SET external_sim 0")
593
594def _test_ap_wpa2_eap_sim_ext_replace_sim2(dev, apdev):
595 check_hlr_auc_gw_support()
596 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 597 hostapd.add_ap(apdev[0], params)
40c654cc
JM
598 dev[0].request("SET external_sim 1")
599 id = dev[0].connect("test-wpa2-eap", eap="SIM", key_mgmt="WPA-EAP",
600 identity="1232010000000000",
601 wait_connect=False, scan_freq="2412")
602
603 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
604 if ev is None:
605 raise Exception("Wait for external SIM processing request timed out")
606 p = ev.split(':', 2)
607 if p[1] != "GSM-AUTH":
608 raise Exception("Unexpected CTRL-REQ-SIM type")
609 rid = p[0].split('-')[3]
610 rand = p[2].split(' ')[0]
611
612 res = subprocess.check_output(["../../hostapd/hlr_auc_gw",
613 "-m",
614 "auth_serv/hlr_auc_gw.milenage_db",
d5e6ffd6 615 "GSM-AUTH-REQ 232010000000000 " + rand]).decode()
40c654cc
JM
616 if "GSM-AUTH-RESP" not in res:
617 raise Exception("Unexpected hlr_auc_gw response")
618 resp = res.split(' ')[2].rstrip()
619
620 dev[0].request("CTRL-RSP-SIM-" + rid + ":GSM-AUTH:" + resp)
621 dev[0].wait_connected(timeout=15)
622 dev[0].request("DISCONNECT")
623 dev[0].wait_disconnected()
624
625 # Replace SIM and drop the previous pseudonym identity
626 dev[0].set_network_quoted(id, "identity", "1232010000000009")
627 dev[0].set_network(id, "anonymous_identity", "NULL")
628 dev[0].select_network(id, freq="2412")
629
630 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
631 if ev is None:
632 raise Exception("Wait for external SIM processing request timed out")
633 p = ev.split(':', 2)
634 if p[1] != "GSM-AUTH":
635 raise Exception("Unexpected CTRL-REQ-SIM type")
636 rid = p[0].split('-')[3]
637 rand = p[2].split(' ')[0]
638
639 res = subprocess.check_output(["../../hostapd/hlr_auc_gw",
640 "-m",
641 "auth_serv/hlr_auc_gw.milenage_db",
d5e6ffd6 642 "GSM-AUTH-REQ 232010000000009 " + rand]).decode()
40c654cc
JM
643 if "GSM-AUTH-RESP" not in res:
644 raise Exception("Unexpected hlr_auc_gw response")
645 resp = res.split(' ')[2].rstrip()
646
647 dev[0].request("CTRL-RSP-SIM-" + rid + ":GSM-AUTH:" + resp)
648 dev[0].wait_connected()
649 dev[0].request("DISCONNECT")
650 dev[0].wait_disconnected()
651
652def test_ap_wpa2_eap_sim_ext_replace_sim3(dev, apdev):
653 """EAP-SIM with external GSM auth, replacing SIM, and no identity in config"""
654 try:
655 _test_ap_wpa2_eap_sim_ext_replace_sim3(dev, apdev)
656 finally:
657 dev[0].request("SET external_sim 0")
658
659def _test_ap_wpa2_eap_sim_ext_replace_sim3(dev, apdev):
660 check_hlr_auc_gw_support()
661 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 662 hostapd.add_ap(apdev[0], params)
40c654cc
JM
663 dev[0].request("SET external_sim 1")
664 id = dev[0].connect("test-wpa2-eap", eap="SIM", key_mgmt="WPA-EAP",
665 wait_connect=False, scan_freq="2412")
666
667 ev = dev[0].wait_event(["CTRL-REQ-IDENTITY"])
668 if ev is None:
669 raise Exception("Request for identity timed out")
670 rid = ev.split(':')[0].split('-')[-1]
671 dev[0].request("CTRL-RSP-IDENTITY-" + rid + ":1232010000000000")
672
673 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
674 if ev is None:
675 raise Exception("Wait for external SIM processing request timed out")
676 p = ev.split(':', 2)
677 if p[1] != "GSM-AUTH":
678 raise Exception("Unexpected CTRL-REQ-SIM type")
679 rid = p[0].split('-')[3]
680 rand = p[2].split(' ')[0]
681
682 res = subprocess.check_output(["../../hostapd/hlr_auc_gw",
683 "-m",
684 "auth_serv/hlr_auc_gw.milenage_db",
d5e6ffd6 685 "GSM-AUTH-REQ 232010000000000 " + rand]).decode()
40c654cc
JM
686 if "GSM-AUTH-RESP" not in res:
687 raise Exception("Unexpected hlr_auc_gw response")
688 resp = res.split(' ')[2].rstrip()
689
690 dev[0].request("CTRL-RSP-SIM-" + rid + ":GSM-AUTH:" + resp)
691 dev[0].wait_connected(timeout=15)
692 dev[0].request("DISCONNECT")
693 dev[0].wait_disconnected()
694
695 # Replace SIM and drop the previous permanent and pseudonym identities
696 dev[0].set_network(id, "identity", "NULL")
697 dev[0].set_network(id, "anonymous_identity", "NULL")
698 dev[0].select_network(id, freq="2412")
699
700 ev = dev[0].wait_event(["CTRL-REQ-IDENTITY"])
701 if ev is None:
702 raise Exception("Request for identity timed out")
703 rid = ev.split(':')[0].split('-')[-1]
704 dev[0].request("CTRL-RSP-IDENTITY-" + rid + ":1232010000000009")
705
706 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
707 if ev is None:
708 raise Exception("Wait for external SIM processing request timed out")
709 p = ev.split(':', 2)
710 if p[1] != "GSM-AUTH":
711 raise Exception("Unexpected CTRL-REQ-SIM type")
712 rid = p[0].split('-')[3]
713 rand = p[2].split(' ')[0]
714
715 res = subprocess.check_output(["../../hostapd/hlr_auc_gw",
716 "-m",
717 "auth_serv/hlr_auc_gw.milenage_db",
d5e6ffd6 718 "GSM-AUTH-REQ 232010000000009 " + rand]).decode()
40c654cc
JM
719 if "GSM-AUTH-RESP" not in res:
720 raise Exception("Unexpected hlr_auc_gw response")
721 resp = res.split(' ')[2].rstrip()
722
723 dev[0].request("CTRL-RSP-SIM-" + rid + ":GSM-AUTH:" + resp)
724 dev[0].wait_connected()
725 dev[0].request("DISCONNECT")
726 dev[0].wait_disconnected()
727
c397edf2
JM
728def test_ap_wpa2_eap_sim_ext_auth_fail(dev, apdev):
729 """EAP-SIM with external GSM auth and auth failing"""
730 try:
731 _test_ap_wpa2_eap_sim_ext_auth_fail(dev, apdev)
732 finally:
733 dev[0].request("SET external_sim 0")
734
735def _test_ap_wpa2_eap_sim_ext_auth_fail(dev, apdev):
736 check_hlr_auc_gw_support()
737 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 738 hostapd.add_ap(apdev[0], params)
c397edf2
JM
739 dev[0].request("SET external_sim 1")
740 id = dev[0].connect("test-wpa2-eap", eap="SIM", key_mgmt="WPA-EAP",
741 identity="1232010000000000",
742 wait_connect=False, scan_freq="2412")
743
744 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
745 if ev is None:
746 raise Exception("Wait for external SIM processing request timed out")
747 p = ev.split(':', 2)
748 rid = p[0].split('-')[3]
749 dev[0].request("CTRL-RSP-SIM-" + rid + ":GSM-FAIL")
750 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
751 if ev is None:
752 raise Exception("EAP failure not reported")
753 dev[0].request("REMOVE_NETWORK all")
754 dev[0].wait_disconnected()
755
6c7fed46
JM
756def test_ap_wpa2_eap_sim_change_bssid(dev, apdev):
757 """EAP-SIM and external GSM auth to check fast reauth with bssid change"""
758 try:
759 _test_ap_wpa2_eap_sim_change_bssid(dev, apdev)
760 finally:
761 dev[0].request("SET external_sim 0")
762
763def _test_ap_wpa2_eap_sim_change_bssid(dev, apdev):
764 check_hlr_auc_gw_support()
765 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 766 hostapd.add_ap(apdev[0], params)
6c7fed46
JM
767 dev[0].request("SET external_sim 1")
768 id = dev[0].connect("test-wpa2-eap", eap="SIM", key_mgmt="WPA-EAP",
769 identity="1232010000000000",
770 wait_connect=False, scan_freq="2412")
771
772 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
773 if ev is None:
774 raise Exception("Wait for external SIM processing request timed out")
775 p = ev.split(':', 2)
776 if p[1] != "GSM-AUTH":
777 raise Exception("Unexpected CTRL-REQ-SIM type")
778 rid = p[0].split('-')[3]
779 rand = p[2].split(' ')[0]
780
781 res = subprocess.check_output(["../../hostapd/hlr_auc_gw",
782 "-m",
783 "auth_serv/hlr_auc_gw.milenage_db",
d5e6ffd6 784 "GSM-AUTH-REQ 232010000000000 " + rand]).decode()
6c7fed46
JM
785 if "GSM-AUTH-RESP" not in res:
786 raise Exception("Unexpected hlr_auc_gw response")
787 resp = res.split(' ')[2].rstrip()
788
789 dev[0].request("CTRL-RSP-SIM-" + rid + ":GSM-AUTH:" + resp)
790 dev[0].wait_connected(timeout=15)
791
792 # Verify that EAP-SIM Reauthentication can be used after a profile change
793 # that does not affect EAP parameters.
794 dev[0].set_network(id, "bssid", "any")
795 eap_reauth(dev[0], "SIM")
796
07f0da30
JM
797def test_ap_wpa2_eap_sim_no_change_set(dev, apdev):
798 """EAP-SIM and external GSM auth to check fast reauth with no-change SET_NETWORK"""
799 try:
800 _test_ap_wpa2_eap_sim_no_change_set(dev, apdev)
801 finally:
802 dev[0].request("SET external_sim 0")
803
804def _test_ap_wpa2_eap_sim_no_change_set(dev, apdev):
805 check_hlr_auc_gw_support()
806 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 807 hostapd.add_ap(apdev[0], params)
07f0da30
JM
808 dev[0].request("SET external_sim 1")
809 id = dev[0].connect("test-wpa2-eap", eap="SIM", key_mgmt="WPA-EAP",
810 identity="1232010000000000",
811 wait_connect=False, scan_freq="2412")
812
813 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
814 if ev is None:
815 raise Exception("Wait for external SIM processing request timed out")
816 p = ev.split(':', 2)
817 if p[1] != "GSM-AUTH":
818 raise Exception("Unexpected CTRL-REQ-SIM type")
819 rid = p[0].split('-')[3]
820 rand = p[2].split(' ')[0]
821
822 res = subprocess.check_output(["../../hostapd/hlr_auc_gw",
823 "-m",
824 "auth_serv/hlr_auc_gw.milenage_db",
d5e6ffd6 825 "GSM-AUTH-REQ 232010000000000 " + rand]).decode()
07f0da30
JM
826 if "GSM-AUTH-RESP" not in res:
827 raise Exception("Unexpected hlr_auc_gw response")
828 resp = res.split(' ')[2].rstrip()
829
830 dev[0].request("CTRL-RSP-SIM-" + rid + ":GSM-AUTH:" + resp)
831 dev[0].wait_connected(timeout=15)
832
833 # Verify that EAP-SIM Reauthentication can be used after network profile
834 # SET_NETWORK commands that do not actually change previously set
835 # parameter values.
836 dev[0].set_network(id, "key_mgmt", "WPA-EAP")
837 dev[0].set_network(id, "eap", "SIM")
838 dev[0].set_network_quoted(id, "identity", "1232010000000000")
839 dev[0].set_network_quoted(id, "ssid", "test-wpa2-eap")
840 eap_reauth(dev[0], "SIM")
841
486f4e3c
JM
842def test_ap_wpa2_eap_sim_oom(dev, apdev):
843 """EAP-SIM and OOM"""
844 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 845 hostapd.add_ap(apdev[0], params)
fab49f61
JM
846 tests = [(1, "milenage_f2345"),
847 (2, "milenage_f2345"),
848 (3, "milenage_f2345"),
849 (4, "milenage_f2345"),
850 (5, "milenage_f2345"),
851 (6, "milenage_f2345"),
852 (7, "milenage_f2345"),
853 (8, "milenage_f2345"),
854 (9, "milenage_f2345"),
855 (10, "milenage_f2345"),
856 (11, "milenage_f2345"),
857 (12, "milenage_f2345")]
486f4e3c 858 for count, func in tests:
7cbc8e67 859 with fail_test(dev[0], count, func):
486f4e3c
JM
860 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="SIM",
861 identity="1232010000000000",
862 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581",
863 wait_connect=False, scan_freq="2412")
864 ev = dev[0].wait_event(["CTRL-EVENT-EAP-METHOD"], timeout=5)
865 if ev is None:
866 raise Exception("EAP method not selected")
867 dev[0].wait_disconnected()
868 dev[0].request("REMOVE_NETWORK all")
869
9626962d
JM
870def test_ap_wpa2_eap_aka(dev, apdev):
871 """WPA2-Enterprise connection using EAP-AKA"""
81e787b7 872 check_hlr_auc_gw_support()
9626962d 873 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 874 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 875 eap_connect(dev[0], hapd, "AKA", "0232010000000000",
9626962d 876 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581:000000000123")
a8375c94 877 hwsim_utils.test_connectivity(dev[0], hapd)
75b2b9cf 878 eap_reauth(dev[0], "AKA")
9626962d 879
f10ba3b2
JM
880 logger.info("Negative test with incorrect key")
881 dev[0].request("REMOVE_NETWORK all")
3b3e2687 882 eap_connect(dev[0], hapd, "AKA", "0232010000000000",
f10ba3b2
JM
883 password="ffdca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581:000000000123",
884 expect_failure=True)
885
32747a3e
JM
886 logger.info("Invalid Milenage key")
887 dev[0].request("REMOVE_NETWORK all")
3b3e2687 888 eap_connect(dev[0], hapd, "AKA", "0232010000000000",
32747a3e
JM
889 password="ffdca4eda45b53cf0f12d7c9c3bc6a",
890 expect_failure=True)
891
892 logger.info("Invalid Milenage key(2)")
3b3e2687 893 eap_connect(dev[0], hapd, "AKA", "0232010000000000",
32747a3e
JM
894 password="ffdca4eda45b53cf0f12d7c9c3bc6a8q:cb9cccc4b9258e6dca4760379fb82581:000000000123",
895 expect_failure=True)
896
897 logger.info("Invalid Milenage key(3)")
3b3e2687 898 eap_connect(dev[0], hapd, "AKA", "0232010000000000",
32747a3e
JM
899 password="ffdca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb8258q:000000000123",
900 expect_failure=True)
901
902 logger.info("Invalid Milenage key(4)")
3b3e2687 903 eap_connect(dev[0], hapd, "AKA", "0232010000000000",
32747a3e
JM
904 password="ffdca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581:00000000012q",
905 expect_failure=True)
906
907 logger.info("Invalid Milenage key(5)")
908 dev[0].request("REMOVE_NETWORK all")
3b3e2687 909 eap_connect(dev[0], hapd, "AKA", "0232010000000000",
32747a3e
JM
910 password="ffdca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581q000000000123",
911 expect_failure=True)
912
913 logger.info("Invalid Milenage key(6)")
914 dev[0].request("REMOVE_NETWORK all")
3b3e2687 915 eap_connect(dev[0], hapd, "AKA", "0232010000000000",
32747a3e
JM
916 password="ffdca4eda45b53cf0f12d7c9c3bc6a89qcb9cccc4b9258e6dca4760379fb82581q000000000123",
917 expect_failure=True)
918
919 logger.info("Missing key configuration")
920 dev[0].request("REMOVE_NETWORK all")
3b3e2687 921 eap_connect(dev[0], hapd, "AKA", "0232010000000000",
32747a3e
JM
922 expect_failure=True)
923
5b1aaf6c
JM
924def test_ap_wpa2_eap_aka_sql(dev, apdev, params):
925 """WPA2-Enterprise connection using EAP-AKA (SQL)"""
81e787b7 926 check_hlr_auc_gw_support()
5b1aaf6c
JM
927 try:
928 import sqlite3
929 except ImportError:
81e787b7 930 raise HwsimSkip("No sqlite3 module available")
5b1aaf6c
JM
931 con = sqlite3.connect(os.path.join(params['logdir'], "hostapd.db"))
932 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
933 params['auth_server_port'] = "1814"
3b3e2687
JD
934 hapd = hostapd.add_ap(apdev[0], params)
935 eap_connect(dev[0], hapd, "AKA", "0232010000000000",
5b1aaf6c
JM
936 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581:000000000123")
937
938 logger.info("AKA fast re-authentication")
939 eap_reauth(dev[0], "AKA")
940
941 logger.info("AKA full auth with pseudonym")
942 with con:
943 cur = con.cursor()
944 cur.execute("DELETE FROM reauth WHERE permanent='0232010000000000'")
945 eap_reauth(dev[0], "AKA")
946
947 logger.info("AKA full auth with permanent identity")
948 with con:
949 cur = con.cursor()
950 cur.execute("DELETE FROM reauth WHERE permanent='0232010000000000'")
951 cur.execute("DELETE FROM pseudonyms WHERE permanent='0232010000000000'")
952 eap_reauth(dev[0], "AKA")
953
954 logger.info("AKA reauth with mismatching MK")
955 with con:
956 cur = con.cursor()
957 cur.execute("UPDATE reauth SET mk='0000000000000000000000000000000000000000' WHERE permanent='0232010000000000'")
958 eap_reauth(dev[0], "AKA", expect_failure=True)
959 dev[0].request("REMOVE_NETWORK all")
960
3b3e2687 961 eap_connect(dev[0], hapd, "AKA", "0232010000000000",
5b1aaf6c
JM
962 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581:000000000123")
963 with con:
964 cur = con.cursor()
965 cur.execute("UPDATE reauth SET counter='10' WHERE permanent='0232010000000000'")
966 eap_reauth(dev[0], "AKA")
967 with con:
968 cur = con.cursor()
969 cur.execute("UPDATE reauth SET counter='10' WHERE permanent='0232010000000000'")
970 logger.info("AKA reauth with mismatching counter")
971 eap_reauth(dev[0], "AKA")
972 dev[0].request("REMOVE_NETWORK all")
973
3b3e2687 974 eap_connect(dev[0], hapd, "AKA", "0232010000000000",
5b1aaf6c
JM
975 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581:000000000123")
976 with con:
977 cur = con.cursor()
978 cur.execute("UPDATE reauth SET counter='1001' WHERE permanent='0232010000000000'")
979 logger.info("AKA reauth with max reauth count reached")
980 eap_reauth(dev[0], "AKA")
981
e2a90a4c
JM
982def test_ap_wpa2_eap_aka_config(dev, apdev):
983 """EAP-AKA configuration options"""
984 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
985 hapd = hostapd.add_ap(apdev[0], params)
986 eap_connect(dev[0], hapd, "AKA", "0232010000000000",
e2a90a4c
JM
987 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581:000000000123",
988 anonymous_identity="2345678")
989
d314bedf
JM
990def test_ap_wpa2_eap_aka_ext(dev, apdev):
991 """WPA2-Enterprise connection using EAP-AKA and external UMTS auth"""
47dcb118 992 try:
81e787b7 993 _test_ap_wpa2_eap_aka_ext(dev, apdev)
47dcb118
JM
994 finally:
995 dev[0].request("SET external_sim 0")
996
997def _test_ap_wpa2_eap_aka_ext(dev, apdev):
81e787b7 998 check_hlr_auc_gw_support()
d314bedf 999 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1000 hostapd.add_ap(apdev[0], params)
d314bedf
JM
1001 dev[0].request("SET external_sim 1")
1002 id = dev[0].connect("test-wpa2-eap", eap="AKA", key_mgmt="WPA-EAP",
1003 identity="0232010000000000",
1004 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581:000000000123",
1005 wait_connect=False, scan_freq="2412")
1006 ev = dev[0].wait_event(["CTRL-EVENT-EAP-METHOD"], timeout=15)
1007 if ev is None:
1008 raise Exception("Network connected timed out")
1009
1010 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
1011 if ev is None:
1012 raise Exception("Wait for external SIM processing request timed out")
1013 p = ev.split(':', 2)
1014 if p[1] != "UMTS-AUTH":
1015 raise Exception("Unexpected CTRL-REQ-SIM type")
1016 rid = p[0].split('-')[3]
1017
1018 # IK:CK:RES
1019 resp = "00112233445566778899aabbccddeeff:00112233445566778899aabbccddeeff:0011223344"
1020 # This will fail during processing, but the ctrl_iface command succeeds
1021 dev[0].request("CTRL-RSP-SIM-" + rid + ":GSM-AUTH:" + resp)
1022 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=15)
1023 if ev is None:
1024 raise Exception("EAP failure not reported")
1025 dev[0].request("DISCONNECT")
584e4197 1026 dev[0].wait_disconnected()
90ad11e6 1027 time.sleep(0.1)
a359c7bb 1028 dev[0].dump_monitor()
d314bedf 1029
d8e02214
JM
1030 dev[0].select_network(id, freq="2412")
1031 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
1032 if ev is None:
1033 raise Exception("Wait for external SIM processing request timed out")
1034 p = ev.split(':', 2)
1035 if p[1] != "UMTS-AUTH":
1036 raise Exception("Unexpected CTRL-REQ-SIM type")
1037 rid = p[0].split('-')[3]
1038 # This will fail during UMTS auth validation
1039 if "OK" not in dev[0].request("CTRL-RSP-SIM-" + rid + ":UMTS-AUTS:112233445566778899aabbccddee"):
1040 raise Exception("CTRL-RSP-SIM failed")
1041 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
1042 if ev is None:
1043 raise Exception("Wait for external SIM processing request timed out")
1044 p = ev.split(':', 2)
1045 if p[1] != "UMTS-AUTH":
1046 raise Exception("Unexpected CTRL-REQ-SIM type")
1047 rid = p[0].split('-')[3]
1048 # This will fail during UMTS auth validation
1049 if "OK" not in dev[0].request("CTRL-RSP-SIM-" + rid + ":UMTS-AUTS:12"):
1050 raise Exception("CTRL-RSP-SIM failed")
1051 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=15)
1052 if ev is None:
1053 raise Exception("EAP failure not reported")
1054 dev[0].request("DISCONNECT")
584e4197 1055 dev[0].wait_disconnected()
90ad11e6 1056 time.sleep(0.1)
a359c7bb 1057 dev[0].dump_monitor()
d8e02214 1058
fab49f61
JM
1059 tests = [":UMTS-AUTH:00112233445566778899aabbccddeeff:00112233445566778899aabbccddeeff:0011223344",
1060 ":UMTS-AUTH:34",
1061 ":UMTS-AUTH:00112233445566778899aabbccddeeff.00112233445566778899aabbccddeeff:0011223344",
1062 ":UMTS-AUTH:00112233445566778899aabbccddeeff:00112233445566778899aabbccddee:0011223344",
1063 ":UMTS-AUTH:00112233445566778899aabbccddeeff:00112233445566778899aabbccddeeff.0011223344",
1064 ":UMTS-AUTH:00112233445566778899aabbccddeeff:00112233445566778899aabbccddeeff:00112233445566778899aabbccddeeff0011223344",
1065 ":UMTS-AUTH:00112233445566778899aabbccddeeff:00112233445566778899aabbccddeeff:001122334q"]
0258cf10
JM
1066 for t in tests:
1067 dev[0].select_network(id, freq="2412")
1068 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
1069 if ev is None:
1070 raise Exception("Wait for external SIM processing request timed out")
1071 p = ev.split(':', 2)
1072 if p[1] != "UMTS-AUTH":
1073 raise Exception("Unexpected CTRL-REQ-SIM type")
1074 rid = p[0].split('-')[3]
1075 # This will fail during UMTS auth validation
1076 if "OK" not in dev[0].request("CTRL-RSP-SIM-" + rid + t):
1077 raise Exception("CTRL-RSP-SIM failed")
1078 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=15)
1079 if ev is None:
1080 raise Exception("EAP failure not reported")
1081 dev[0].request("DISCONNECT")
1082 dev[0].wait_disconnected()
90ad11e6 1083 time.sleep(0.1)
a359c7bb 1084 dev[0].dump_monitor()
d314bedf 1085
c397edf2
JM
1086def test_ap_wpa2_eap_aka_ext_auth_fail(dev, apdev):
1087 """EAP-AKA with external UMTS auth and auth failing"""
1088 try:
1089 _test_ap_wpa2_eap_aka_ext_auth_fail(dev, apdev)
1090 finally:
1091 dev[0].request("SET external_sim 0")
1092
1093def _test_ap_wpa2_eap_aka_ext_auth_fail(dev, apdev):
1094 check_hlr_auc_gw_support()
1095 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1096 hostapd.add_ap(apdev[0], params)
c397edf2
JM
1097 dev[0].request("SET external_sim 1")
1098 id = dev[0].connect("test-wpa2-eap", eap="AKA", key_mgmt="WPA-EAP",
1099 identity="0232010000000000",
1100 wait_connect=False, scan_freq="2412")
1101
1102 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
1103 if ev is None:
1104 raise Exception("Wait for external SIM processing request timed out")
1105 p = ev.split(':', 2)
1106 rid = p[0].split('-')[3]
1107 dev[0].request("CTRL-RSP-SIM-" + rid + ":UMTS-FAIL")
1108 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
1109 if ev is None:
1110 raise Exception("EAP failure not reported")
1111 dev[0].request("REMOVE_NETWORK all")
1112 dev[0].wait_disconnected()
1113
9626962d
JM
1114def test_ap_wpa2_eap_aka_prime(dev, apdev):
1115 """WPA2-Enterprise connection using EAP-AKA'"""
81e787b7 1116 check_hlr_auc_gw_support()
9626962d 1117 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1118 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1119 eap_connect(dev[0], hapd, "AKA'", "6555444333222111",
9626962d 1120 password="5122250214c33e723a5dd523fc145fc0:981d464c7c52eb6e5036234984ad0bcf:000000000123")
a8375c94 1121 hwsim_utils.test_connectivity(dev[0], hapd)
75b2b9cf 1122 eap_reauth(dev[0], "AKA'")
9626962d 1123
8583d664
JM
1124 logger.info("EAP-AKA' bidding protection when EAP-AKA enabled as well")
1125 dev[1].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="AKA' AKA",
1126 identity="6555444333222111@both",
1127 password="5122250214c33e723a5dd523fc145fc0:981d464c7c52eb6e5036234984ad0bcf:000000000123",
1128 wait_connect=False, scan_freq="2412")
5f35a5e2 1129 dev[1].wait_connected(timeout=15)
8583d664 1130
f10ba3b2
JM
1131 logger.info("Negative test with incorrect key")
1132 dev[0].request("REMOVE_NETWORK all")
3b3e2687 1133 eap_connect(dev[0], hapd, "AKA'", "6555444333222111",
f10ba3b2
JM
1134 password="ff22250214c33e723a5dd523fc145fc0:981d464c7c52eb6e5036234984ad0bcf:000000000123",
1135 expect_failure=True)
1136
5b1aaf6c
JM
1137def test_ap_wpa2_eap_aka_prime_sql(dev, apdev, params):
1138 """WPA2-Enterprise connection using EAP-AKA' (SQL)"""
81e787b7 1139 check_hlr_auc_gw_support()
5b1aaf6c
JM
1140 try:
1141 import sqlite3
1142 except ImportError:
81e787b7 1143 raise HwsimSkip("No sqlite3 module available")
5b1aaf6c
JM
1144 con = sqlite3.connect(os.path.join(params['logdir'], "hostapd.db"))
1145 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
1146 params['auth_server_port'] = "1814"
3b3e2687
JD
1147 hapd = hostapd.add_ap(apdev[0], params)
1148 eap_connect(dev[0], hapd, "AKA'", "6555444333222111",
5b1aaf6c
JM
1149 password="5122250214c33e723a5dd523fc145fc0:981d464c7c52eb6e5036234984ad0bcf:000000000123")
1150
1151 logger.info("AKA' fast re-authentication")
1152 eap_reauth(dev[0], "AKA'")
1153
1154 logger.info("AKA' full auth with pseudonym")
1155 with con:
1156 cur = con.cursor()
1157 cur.execute("DELETE FROM reauth WHERE permanent='6555444333222111'")
1158 eap_reauth(dev[0], "AKA'")
1159
1160 logger.info("AKA' full auth with permanent identity")
1161 with con:
1162 cur = con.cursor()
1163 cur.execute("DELETE FROM reauth WHERE permanent='6555444333222111'")
1164 cur.execute("DELETE FROM pseudonyms WHERE permanent='6555444333222111'")
1165 eap_reauth(dev[0], "AKA'")
1166
1167 logger.info("AKA' reauth with mismatching k_aut")
1168 with con:
1169 cur = con.cursor()
1170 cur.execute("UPDATE reauth SET k_aut='0000000000000000000000000000000000000000000000000000000000000000' WHERE permanent='6555444333222111'")
1171 eap_reauth(dev[0], "AKA'", expect_failure=True)
1172 dev[0].request("REMOVE_NETWORK all")
1173
3b3e2687 1174 eap_connect(dev[0], hapd, "AKA'", "6555444333222111",
5b1aaf6c
JM
1175 password="5122250214c33e723a5dd523fc145fc0:981d464c7c52eb6e5036234984ad0bcf:000000000123")
1176 with con:
1177 cur = con.cursor()
1178 cur.execute("UPDATE reauth SET counter='10' WHERE permanent='6555444333222111'")
1179 eap_reauth(dev[0], "AKA'")
1180 with con:
1181 cur = con.cursor()
1182 cur.execute("UPDATE reauth SET counter='10' WHERE permanent='6555444333222111'")
1183 logger.info("AKA' reauth with mismatching counter")
1184 eap_reauth(dev[0], "AKA'")
1185 dev[0].request("REMOVE_NETWORK all")
1186
3b3e2687 1187 eap_connect(dev[0], hapd, "AKA'", "6555444333222111",
5b1aaf6c
JM
1188 password="5122250214c33e723a5dd523fc145fc0:981d464c7c52eb6e5036234984ad0bcf:000000000123")
1189 with con:
1190 cur = con.cursor()
1191 cur.execute("UPDATE reauth SET counter='1001' WHERE permanent='6555444333222111'")
1192 logger.info("AKA' reauth with max reauth count reached")
1193 eap_reauth(dev[0], "AKA'")
1194
c397edf2
JM
1195def test_ap_wpa2_eap_aka_prime_ext_auth_fail(dev, apdev):
1196 """EAP-AKA' with external UMTS auth and auth failing"""
1197 try:
1198 _test_ap_wpa2_eap_aka_prime_ext_auth_fail(dev, apdev)
1199 finally:
1200 dev[0].request("SET external_sim 0")
1201
1202def _test_ap_wpa2_eap_aka_prime_ext_auth_fail(dev, apdev):
1203 check_hlr_auc_gw_support()
1204 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1205 hostapd.add_ap(apdev[0], params)
c397edf2
JM
1206 dev[0].request("SET external_sim 1")
1207 id = dev[0].connect("test-wpa2-eap", eap="AKA'", key_mgmt="WPA-EAP",
1208 identity="6555444333222111",
1209 wait_connect=False, scan_freq="2412")
1210
1211 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
1212 if ev is None:
1213 raise Exception("Wait for external SIM processing request timed out")
1214 p = ev.split(':', 2)
1215 rid = p[0].split('-')[3]
1216 dev[0].request("CTRL-RSP-SIM-" + rid + ":UMTS-FAIL")
1217 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
1218 if ev is None:
1219 raise Exception("EAP failure not reported")
1220 dev[0].request("REMOVE_NETWORK all")
1221 dev[0].wait_disconnected()
1222
c25aada9
JM
1223def test_ap_wpa2_eap_aka_prime_ext(dev, apdev):
1224 """EAP-AKA' with external UMTS auth to hit Synchronization-Failure"""
1225 try:
1226 _test_ap_wpa2_eap_aka_prime_ext(dev, apdev)
1227 finally:
1228 dev[0].request("SET external_sim 0")
1229
1230def _test_ap_wpa2_eap_aka_prime_ext(dev, apdev):
1231 check_hlr_auc_gw_support()
1232 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
1233 hostapd.add_ap(apdev[0], params)
1234 dev[0].request("SET external_sim 1")
1235 id = dev[0].connect("test-wpa2-eap", eap="AKA'", key_mgmt="WPA-EAP",
1236 identity="6555444333222111",
1237 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581:000000000123",
1238 wait_connect=False, scan_freq="2412")
1239 ev = dev[0].wait_event(["CTRL-EVENT-EAP-METHOD"], timeout=15)
1240 if ev is None:
1241 raise Exception("Network connected timed out")
1242
1243 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
1244 if ev is None:
1245 raise Exception("Wait for external SIM processing request timed out")
1246 p = ev.split(':', 2)
1247 if p[1] != "UMTS-AUTH":
1248 raise Exception("Unexpected CTRL-REQ-SIM type")
1249 rid = p[0].split('-')[3]
1250 # This will fail during UMTS auth validation
1251 if "OK" not in dev[0].request("CTRL-RSP-SIM-" + rid + ":UMTS-AUTS:112233445566778899aabbccddee"):
1252 raise Exception("CTRL-RSP-SIM failed")
1253 ev = dev[0].wait_event(["CTRL-REQ-SIM"], timeout=15)
1254 if ev is None:
1255 raise Exception("Wait for external SIM processing request timed out")
1256
9626962d
JM
1257def test_ap_wpa2_eap_ttls_pap(dev, apdev):
1258 """WPA2-Enterprise connection using EAP-TTLS/PAP"""
1259 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1260 hapd = hostapd.add_ap(apdev[0], params)
65038313
JM
1261 key_mgmt = hapd.get_config()['key_mgmt']
1262 if key_mgmt.split(' ')[0] != "WPA-EAP":
1263 raise Exception("Unexpected GET_CONFIG(key_mgmt): " + key_mgmt)
3b3e2687 1264 eap_connect(dev[0], hapd, "TTLS", "pap user",
9626962d 1265 anonymous_identity="ttls", password="password",
506b2f05 1266 ca_cert="auth_serv/ca.pem", phase2="auth=PAP")
a8375c94 1267 hwsim_utils.test_connectivity(dev[0], hapd)
75b2b9cf 1268 eap_reauth(dev[0], "TTLS")
fab49f61
JM
1269 check_mib(dev[0], [("dot11RSNAAuthenticationSuiteRequested", "00-0f-ac-1"),
1270 ("dot11RSNAAuthenticationSuiteSelected", "00-0f-ac-1")])
9626962d 1271
506b2f05
JM
1272def test_ap_wpa2_eap_ttls_pap_subject_match(dev, apdev):
1273 """WPA2-Enterprise connection using EAP-TTLS/PAP and (alt)subject_match"""
1274 check_subject_match_support(dev[0])
1275 check_altsubject_match_support(dev[0])
1276 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1277 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1278 eap_connect(dev[0], hapd, "TTLS", "pap user",
506b2f05
JM
1279 anonymous_identity="ttls", password="password",
1280 ca_cert="auth_serv/ca.pem", phase2="auth=PAP",
1281 subject_match="/C=FI/O=w1.fi/CN=server.w1.fi",
1282 altsubject_match="EMAIL:noone@example.com;DNS:server.w1.fi;URI:http://example.com/")
1283 eap_reauth(dev[0], "TTLS")
1284
f4f17e9a
JM
1285def test_ap_wpa2_eap_ttls_pap_check_cert_subject(dev, apdev):
1286 """EAP-TTLS/PAP and check_cert_subject"""
1287 check_check_cert_subject_support(dev[0])
1288 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
1289 hapd = hostapd.add_ap(apdev[0], params)
fab49f61
JM
1290 tests = ["C=FI/O=w1.fi/CN=server.w1.fi",
1291 "C=FI/O=w1.fi",
1292 "C=FI/CN=server.w1.fi",
1293 "O=w1.fi/CN=server.w1.fi",
1294 "C=FI",
1295 "O=w1.fi",
1296 "O=w1.*",
1297 "CN=server.w1.fi",
1298 "*"]
f4f17e9a
JM
1299 for test in tests:
1300 eap_connect(dev[0], hapd, "TTLS", "pap user",
1301 anonymous_identity="ttls", password="password",
1302 ca_cert="auth_serv/ca.pem", phase2="auth=PAP",
1303 check_cert_subject=test)
1304 dev[0].request("REMOVE_NETWORK all")
1305 dev[0].wait_disconnected()
1306 dev[0].dump_monitor()
1307
1308def test_ap_wpa2_eap_ttls_pap_check_cert_subject_neg(dev, apdev):
1309 """EAP-TTLS/PAP and check_cert_subject (negative)"""
1310 check_check_cert_subject_support(dev[0])
1311 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
1312 hapd = hostapd.add_ap(apdev[0], params)
fab49f61
JM
1313 tests = ["C=US",
1314 "C",
1315 "C=FI1*",
1316 "O=w1.f",
1317 "O=w1.fi1",
1318 "O=w1.fi/O=foo",
1319 "O=foo/O=w1.fi",
1320 "O=w1.fi/O=w1.fi"]
f4f17e9a
JM
1321 for test in tests:
1322 eap_connect(dev[0], hapd, "TTLS", "pap user",
1323 anonymous_identity="ttls", password="password",
1324 ca_cert="auth_serv/ca.pem", phase2="auth=PAP",
1325 expect_failure=True, expect_cert_error=12,
1326 check_cert_subject=test)
1327 dev[0].request("REMOVE_NETWORK all")
1328 dev[0].dump_monitor()
1329
82a8f5b5
JM
1330def test_ap_wpa2_eap_ttls_pap_incorrect_password(dev, apdev):
1331 """WPA2-Enterprise connection using EAP-TTLS/PAP - incorrect password"""
1332 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1333 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1334 eap_connect(dev[0], hapd, "TTLS", "pap user",
82a8f5b5
JM
1335 anonymous_identity="ttls", password="wrong",
1336 ca_cert="auth_serv/ca.pem", phase2="auth=PAP",
1337 expect_failure=True)
3b3e2687 1338 eap_connect(dev[1], hapd, "TTLS", "user",
82a8f5b5
JM
1339 anonymous_identity="ttls", password="password",
1340 ca_cert="auth_serv/ca.pem", phase2="auth=PAP",
1341 expect_failure=True)
1342
9626962d
JM
1343def test_ap_wpa2_eap_ttls_chap(dev, apdev):
1344 """WPA2-Enterprise connection using EAP-TTLS/CHAP"""
ca158ea6 1345 skip_with_fips(dev[0])
9626962d 1346 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1347 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1348 eap_connect(dev[0], hapd, "TTLS", "chap user",
506b2f05
JM
1349 anonymous_identity="ttls", password="password",
1350 ca_cert="auth_serv/ca.der", phase2="auth=CHAP")
1351 hwsim_utils.test_connectivity(dev[0], hapd)
1352 eap_reauth(dev[0], "TTLS")
1353
1354def test_ap_wpa2_eap_ttls_chap_altsubject_match(dev, apdev):
1355 """WPA2-Enterprise connection using EAP-TTLS/CHAP"""
ca158ea6 1356 skip_with_fips(dev[0])
506b2f05
JM
1357 check_altsubject_match_support(dev[0])
1358 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1359 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1360 eap_connect(dev[0], hapd, "TTLS", "chap user",
9626962d 1361 anonymous_identity="ttls", password="password",
5c65e277
JM
1362 ca_cert="auth_serv/ca.der", phase2="auth=CHAP",
1363 altsubject_match="EMAIL:noone@example.com;URI:http://example.com/;DNS:server.w1.fi")
75b2b9cf 1364 eap_reauth(dev[0], "TTLS")
9626962d 1365
82a8f5b5
JM
1366def test_ap_wpa2_eap_ttls_chap_incorrect_password(dev, apdev):
1367 """WPA2-Enterprise connection using EAP-TTLS/CHAP - incorrect password"""
ca158ea6 1368 skip_with_fips(dev[0])
82a8f5b5 1369 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1370 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1371 eap_connect(dev[0], hapd, "TTLS", "chap user",
82a8f5b5
JM
1372 anonymous_identity="ttls", password="wrong",
1373 ca_cert="auth_serv/ca.pem", phase2="auth=CHAP",
1374 expect_failure=True)
3b3e2687 1375 eap_connect(dev[1], hapd, "TTLS", "user",
82a8f5b5
JM
1376 anonymous_identity="ttls", password="password",
1377 ca_cert="auth_serv/ca.pem", phase2="auth=CHAP",
1378 expect_failure=True)
1379
9626962d
JM
1380def test_ap_wpa2_eap_ttls_mschap(dev, apdev):
1381 """WPA2-Enterprise connection using EAP-TTLS/MSCHAP"""
ca158ea6 1382 skip_with_fips(dev[0])
e78eb404 1383 check_domain_suffix_match(dev[0])
9626962d 1384 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1385 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1386 eap_connect(dev[0], hapd, "TTLS", "mschap user",
9626962d 1387 anonymous_identity="ttls", password="password",
72c052d5
JM
1388 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAP",
1389 domain_suffix_match="server.w1.fi")
a8375c94 1390 hwsim_utils.test_connectivity(dev[0], hapd)
75b2b9cf 1391 eap_reauth(dev[0], "TTLS")
6daf5b9c 1392 dev[0].request("REMOVE_NETWORK all")
3b3e2687 1393 eap_connect(dev[0], hapd, "TTLS", "mschap user",
6daf5b9c
JM
1394 anonymous_identity="ttls", password="password",
1395 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAP",
1396 fragment_size="200")
bfdb90d4
JM
1397 dev[0].request("REMOVE_NETWORK all")
1398 dev[0].wait_disconnected()
3b3e2687 1399 eap_connect(dev[0], hapd, "TTLS", "mschap user",
bfdb90d4
JM
1400 anonymous_identity="ttls",
1401 password_hex="hash:8846f7eaee8fb117ad06bdd830b7586c",
1402 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAP")
9626962d 1403
82a8f5b5 1404def test_ap_wpa2_eap_ttls_mschap_incorrect_password(dev, apdev):
ca158ea6
JM
1405 """WPA2-Enterprise connection using EAP-TTLS/MSCHAP - incorrect password"""
1406 skip_with_fips(dev[0])
82a8f5b5 1407 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1408 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1409 eap_connect(dev[0], hapd, "TTLS", "mschap user",
82a8f5b5
JM
1410 anonymous_identity="ttls", password="wrong",
1411 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAP",
1412 expect_failure=True)
3b3e2687 1413 eap_connect(dev[1], hapd, "TTLS", "user",
82a8f5b5
JM
1414 anonymous_identity="ttls", password="password",
1415 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAP",
1416 expect_failure=True)
3b3e2687 1417 eap_connect(dev[2], hapd, "TTLS", "no such user",
82a8f5b5
JM
1418 anonymous_identity="ttls", password="password",
1419 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAP",
1420 expect_failure=True)
1421
9626962d
JM
1422def test_ap_wpa2_eap_ttls_mschapv2(dev, apdev):
1423 """WPA2-Enterprise connection using EAP-TTLS/MSCHAPv2"""
e78eb404 1424 check_domain_suffix_match(dev[0])
ca158ea6 1425 check_eap_capa(dev[0], "MSCHAPV2")
9626962d 1426 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
1427 hapd = hostapd.add_ap(apdev[0], params)
1428 eap_connect(dev[0], hapd, "TTLS", "DOMAIN\mschapv2 user",
9626962d 1429 anonymous_identity="ttls", password="password",
72c052d5 1430 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
24579e70 1431 domain_suffix_match="server.w1.fi")
a8375c94 1432 hwsim_utils.test_connectivity(dev[0], hapd)
5dec879d
JM
1433 sta1 = hapd.get_sta(dev[0].p2p_interface_addr())
1434 eapol1 = hapd.get_sta(dev[0].p2p_interface_addr(), info="eapol")
75b2b9cf 1435 eap_reauth(dev[0], "TTLS")
5dec879d
JM
1436 sta2 = hapd.get_sta(dev[0].p2p_interface_addr())
1437 eapol2 = hapd.get_sta(dev[0].p2p_interface_addr(), info="eapol")
1438 if int(sta2['dot1xAuthEapolFramesRx']) <= int(sta1['dot1xAuthEapolFramesRx']):
1439 raise Exception("dot1xAuthEapolFramesRx did not increase")
1440 if int(eapol2['authAuthEapStartsWhileAuthenticated']) < 1:
1441 raise Exception("authAuthEapStartsWhileAuthenticated did not increase")
1442 if int(eapol2['backendAuthSuccesses']) <= int(eapol1['backendAuthSuccesses']):
1443 raise Exception("backendAuthSuccesses did not increase")
9626962d 1444
fa0ddb14
JM
1445 logger.info("Password as hash value")
1446 dev[0].request("REMOVE_NETWORK all")
3b3e2687 1447 eap_connect(dev[0], hapd, "TTLS", "DOMAIN\mschapv2 user",
fa0ddb14
JM
1448 anonymous_identity="ttls",
1449 password_hex="hash:8846f7eaee8fb117ad06bdd830b7586c",
1450 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2")
1451
c4e06b9b
JM
1452def test_ap_wpa2_eap_ttls_invalid_phase2(dev, apdev):
1453 """EAP-TTLS with invalid phase2 parameter values"""
1454 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1455 hostapd.add_ap(apdev[0], params)
fab49f61
JM
1456 tests = ["auth=MSCHAPv2", "auth=MSCHAPV2 autheap=MD5",
1457 "autheap=MD5 auth=MSCHAPV2", "auth=PAP auth=CHAP",
1458 "autheap=MD5 autheap=FOO autheap=MSCHAPV2"]
c4e06b9b
JM
1459 for t in tests:
1460 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
1461 identity="DOMAIN\mschapv2 user",
1462 anonymous_identity="ttls", password="password",
1463 ca_cert="auth_serv/ca.pem", phase2=t,
1464 wait_connect=False, scan_freq="2412")
1465 ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=10)
1466 if ev is None or "method=21" not in ev:
1467 raise Exception("EAP-TTLS not started")
1468 ev = dev[0].wait_event(["EAP: Failed to initialize EAP method",
1469 "CTRL-EVENT-CONNECTED"], timeout=5)
1470 if ev is None or "CTRL-EVENT-CONNECTED" in ev:
1471 raise Exception("No EAP-TTLS failure reported for phase2=" + t)
1472 dev[0].request("REMOVE_NETWORK all")
1473 dev[0].wait_disconnected()
1474 dev[0].dump_monitor()
1475
24579e70
JM
1476def test_ap_wpa2_eap_ttls_mschapv2_suffix_match(dev, apdev):
1477 """WPA2-Enterprise connection using EAP-TTLS/MSCHAPv2"""
1478 check_domain_match_full(dev[0])
ca158ea6 1479 skip_with_fips(dev[0])
24579e70 1480 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
1481 hapd = hostapd.add_ap(apdev[0], params)
1482 eap_connect(dev[0], hapd, "TTLS", "DOMAIN\mschapv2 user",
24579e70
JM
1483 anonymous_identity="ttls", password="password",
1484 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
1485 domain_suffix_match="w1.fi")
1486 hwsim_utils.test_connectivity(dev[0], hapd)
1487 eap_reauth(dev[0], "TTLS")
1488
061cbb25
JM
1489def test_ap_wpa2_eap_ttls_mschapv2_domain_match(dev, apdev):
1490 """WPA2-Enterprise connection using EAP-TTLS/MSCHAPv2 (domain_match)"""
e78eb404 1491 check_domain_match(dev[0])
ca158ea6 1492 skip_with_fips(dev[0])
061cbb25 1493 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
1494 hapd = hostapd.add_ap(apdev[0], params)
1495 eap_connect(dev[0], hapd, "TTLS", "DOMAIN\mschapv2 user",
061cbb25
JM
1496 anonymous_identity="ttls", password="password",
1497 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
1498 domain_match="Server.w1.fi")
1499 hwsim_utils.test_connectivity(dev[0], hapd)
1500 eap_reauth(dev[0], "TTLS")
1501
82a8f5b5
JM
1502def test_ap_wpa2_eap_ttls_mschapv2_incorrect_password(dev, apdev):
1503 """WPA2-Enterprise connection using EAP-TTLS/MSCHAPv2 - incorrect password"""
ca158ea6 1504 skip_with_fips(dev[0])
82a8f5b5 1505 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1506 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1507 eap_connect(dev[0], hapd, "TTLS", "DOMAIN\mschapv2 user",
f10ba3b2
JM
1508 anonymous_identity="ttls", password="password1",
1509 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
1510 expect_failure=True)
3b3e2687 1511 eap_connect(dev[1], hapd, "TTLS", "user",
82a8f5b5
JM
1512 anonymous_identity="ttls", password="password",
1513 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
1514 expect_failure=True)
f10ba3b2 1515
eac67440
JM
1516def test_ap_wpa2_eap_ttls_mschapv2_utf8(dev, apdev):
1517 """WPA2-Enterprise connection using EAP-TTLS/MSCHAPv2 and UTF-8 password"""
ca158ea6 1518 skip_with_fips(dev[0])
eac67440 1519 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
1520 hapd = hostapd.add_ap(apdev[0], params)
1521 eap_connect(dev[0], hapd, "TTLS", "utf8-user-hash",
eac67440
JM
1522 anonymous_identity="ttls", password="secret-åäö-€-password",
1523 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2")
3b3e2687 1524 eap_connect(dev[1], hapd, "TTLS", "utf8-user",
eac67440
JM
1525 anonymous_identity="ttls",
1526 password_hex="hash:bd5844fad2489992da7fe8c5a01559cf",
1527 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2")
fab49f61 1528 for p in ["80", "41c041e04141e041", 257*"41"]:
0d2a7bad
JM
1529 dev[2].connect("test-wpa2-eap", key_mgmt="WPA-EAP",
1530 eap="TTLS", identity="utf8-user-hash",
1531 anonymous_identity="ttls", password_hex=p,
1532 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
1533 wait_connect=False, scan_freq="2412")
1534 ev = dev[2].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=1)
1535 if ev is None:
1536 raise Exception("No failure reported")
1537 dev[2].request("REMOVE_NETWORK all")
1538 dev[2].wait_disconnected()
eac67440 1539
9626962d
JM
1540def test_ap_wpa2_eap_ttls_eap_gtc(dev, apdev):
1541 """WPA2-Enterprise connection using EAP-TTLS/EAP-GTC"""
1542 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1543 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1544 eap_connect(dev[0], hapd, "TTLS", "user",
9626962d
JM
1545 anonymous_identity="ttls", password="password",
1546 ca_cert="auth_serv/ca.pem", phase2="autheap=GTC")
a8375c94 1547 hwsim_utils.test_connectivity(dev[0], hapd)
75b2b9cf 1548 eap_reauth(dev[0], "TTLS")
9626962d 1549
95a15d79
JM
1550def test_ap_wpa2_eap_ttls_eap_gtc_incorrect_password(dev, apdev):
1551 """WPA2-Enterprise connection using EAP-TTLS/EAP-GTC - incorrect password"""
1552 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1553 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1554 eap_connect(dev[0], hapd, "TTLS", "user",
95a15d79
JM
1555 anonymous_identity="ttls", password="wrong",
1556 ca_cert="auth_serv/ca.pem", phase2="autheap=GTC",
1557 expect_failure=True)
1558
1559def test_ap_wpa2_eap_ttls_eap_gtc_no_password(dev, apdev):
1560 """WPA2-Enterprise connection using EAP-TTLS/EAP-GTC - no password"""
1561 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1562 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1563 eap_connect(dev[0], hapd, "TTLS", "user-no-passwd",
95a15d79
JM
1564 anonymous_identity="ttls", password="password",
1565 ca_cert="auth_serv/ca.pem", phase2="autheap=GTC",
1566 expect_failure=True)
1567
1568def test_ap_wpa2_eap_ttls_eap_gtc_server_oom(dev, apdev):
1569 """WPA2-Enterprise connection using EAP-TTLS/EAP-GTC - server OOM"""
1570 params = int_eap_server_params()
8b8a1864 1571 hapd = hostapd.add_ap(apdev[0], params)
95a15d79 1572 with alloc_fail(hapd, 1, "eap_gtc_init"):
3b3e2687 1573 eap_connect(dev[0], hapd, "TTLS", "user",
95a15d79
JM
1574 anonymous_identity="ttls", password="password",
1575 ca_cert="auth_serv/ca.pem", phase2="autheap=GTC",
1576 expect_failure=True)
1577 dev[0].request("REMOVE_NETWORK all")
1578
1579 with alloc_fail(hapd, 1, "eap_gtc_buildReq"):
1580 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP WPA-EAP-SHA256",
1581 eap="TTLS", identity="user",
1582 anonymous_identity="ttls", password="password",
1583 ca_cert="auth_serv/ca.pem", phase2="autheap=GTC",
1584 wait_connect=False, scan_freq="2412")
1585 # This would eventually time out, but we can stop after having reached
1586 # the allocation failure.
1587 for i in range(20):
1588 time.sleep(0.1)
1589 if hapd.request("GET_ALLOC_FAIL").startswith('0'):
1590 break
1591
ac713c09
JM
1592def test_ap_wpa2_eap_ttls_eap_gtc_oom(dev, apdev):
1593 """WPA2-Enterprise connection using EAP-TTLS/EAP-GTC (OOM)"""
1594 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1595 hapd = hostapd.add_ap(apdev[0], params)
ac713c09 1596
fab49f61
JM
1597 tests = ["eap_gtc_init",
1598 "eap_msg_alloc;eap_gtc_process"]
ac713c09
JM
1599 for func in tests:
1600 with alloc_fail(dev[0], 1, func):
1601 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP",
1602 scan_freq="2412",
1603 eap="TTLS", identity="user",
1604 anonymous_identity="ttls", password="password",
1605 ca_cert="auth_serv/ca.pem", phase2="autheap=GTC",
1606 wait_connect=False)
1607 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
1608 dev[0].request("REMOVE_NETWORK all")
1609 dev[0].wait_disconnected()
1610
9626962d
JM
1611def test_ap_wpa2_eap_ttls_eap_md5(dev, apdev):
1612 """WPA2-Enterprise connection using EAP-TTLS/EAP-MD5"""
e7ac04ce 1613 check_eap_capa(dev[0], "MD5")
9626962d 1614 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1615 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1616 eap_connect(dev[0], hapd, "TTLS", "user",
9626962d
JM
1617 anonymous_identity="ttls", password="password",
1618 ca_cert="auth_serv/ca.pem", phase2="autheap=MD5")
a8375c94 1619 hwsim_utils.test_connectivity(dev[0], hapd)
75b2b9cf 1620 eap_reauth(dev[0], "TTLS")
9626962d 1621
ee9533eb
JM
1622def test_ap_wpa2_eap_ttls_eap_md5_incorrect_password(dev, apdev):
1623 """WPA2-Enterprise connection using EAP-TTLS/EAP-MD5 - incorrect password"""
e7ac04ce 1624 check_eap_capa(dev[0], "MD5")
ee9533eb 1625 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1626 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1627 eap_connect(dev[0], hapd, "TTLS", "user",
ee9533eb
JM
1628 anonymous_identity="ttls", password="wrong",
1629 ca_cert="auth_serv/ca.pem", phase2="autheap=MD5",
1630 expect_failure=True)
1631
95a15d79
JM
1632def test_ap_wpa2_eap_ttls_eap_md5_no_password(dev, apdev):
1633 """WPA2-Enterprise connection using EAP-TTLS/EAP-MD5 - no password"""
e7ac04ce 1634 check_eap_capa(dev[0], "MD5")
95a15d79 1635 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1636 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1637 eap_connect(dev[0], hapd, "TTLS", "user-no-passwd",
95a15d79
JM
1638 anonymous_identity="ttls", password="password",
1639 ca_cert="auth_serv/ca.pem", phase2="autheap=MD5",
1640 expect_failure=True)
1641
ee9533eb
JM
1642def test_ap_wpa2_eap_ttls_eap_md5_server_oom(dev, apdev):
1643 """WPA2-Enterprise connection using EAP-TTLS/EAP-MD5 - server OOM"""
e7ac04ce 1644 check_eap_capa(dev[0], "MD5")
ee9533eb 1645 params = int_eap_server_params()
8b8a1864 1646 hapd = hostapd.add_ap(apdev[0], params)
ee9533eb 1647 with alloc_fail(hapd, 1, "eap_md5_init"):
3b3e2687 1648 eap_connect(dev[0], hapd, "TTLS", "user",
ee9533eb
JM
1649 anonymous_identity="ttls", password="password",
1650 ca_cert="auth_serv/ca.pem", phase2="autheap=MD5",
1651 expect_failure=True)
1652 dev[0].request("REMOVE_NETWORK all")
1653
1654 with alloc_fail(hapd, 1, "eap_md5_buildReq"):
1655 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP WPA-EAP-SHA256",
1656 eap="TTLS", identity="user",
1657 anonymous_identity="ttls", password="password",
1658 ca_cert="auth_serv/ca.pem", phase2="autheap=MD5",
1659 wait_connect=False, scan_freq="2412")
1660 # This would eventually time out, but we can stop after having reached
1661 # the allocation failure.
1662 for i in range(20):
1663 time.sleep(0.1)
1664 if hapd.request("GET_ALLOC_FAIL").startswith('0'):
1665 break
1666
9626962d
JM
1667def test_ap_wpa2_eap_ttls_eap_mschapv2(dev, apdev):
1668 """WPA2-Enterprise connection using EAP-TTLS/EAP-MSCHAPv2"""
e7ac04ce 1669 check_eap_capa(dev[0], "MSCHAPV2")
9626962d 1670 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1671 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1672 eap_connect(dev[0], hapd, "TTLS", "user",
9626962d
JM
1673 anonymous_identity="ttls", password="password",
1674 ca_cert="auth_serv/ca.pem", phase2="autheap=MSCHAPV2")
a8375c94 1675 hwsim_utils.test_connectivity(dev[0], hapd)
75b2b9cf 1676 eap_reauth(dev[0], "TTLS")
9626962d 1677
f10ba3b2
JM
1678 logger.info("Negative test with incorrect password")
1679 dev[0].request("REMOVE_NETWORK all")
3b3e2687 1680 eap_connect(dev[0], hapd, "TTLS", "user",
f10ba3b2
JM
1681 anonymous_identity="ttls", password="password1",
1682 ca_cert="auth_serv/ca.pem", phase2="autheap=MSCHAPV2",
1683 expect_failure=True)
1684
95a15d79
JM
1685def test_ap_wpa2_eap_ttls_eap_mschapv2_no_password(dev, apdev):
1686 """WPA2-Enterprise connection using EAP-TTLS/EAP-MSCHAPv2 - no password"""
e7ac04ce 1687 check_eap_capa(dev[0], "MSCHAPV2")
95a15d79 1688 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1689 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1690 eap_connect(dev[0], hapd, "TTLS", "user-no-passwd",
95a15d79
JM
1691 anonymous_identity="ttls", password="password",
1692 ca_cert="auth_serv/ca.pem", phase2="autheap=MSCHAPV2",
1693 expect_failure=True)
1694
ef318402
JM
1695def test_ap_wpa2_eap_ttls_eap_mschapv2_server_oom(dev, apdev):
1696 """WPA2-Enterprise connection using EAP-TTLS/EAP-MSCHAPv2 - server OOM"""
e7ac04ce 1697 check_eap_capa(dev[0], "MSCHAPV2")
ef318402 1698 params = int_eap_server_params()
8b8a1864 1699 hapd = hostapd.add_ap(apdev[0], params)
ef318402 1700 with alloc_fail(hapd, 1, "eap_mschapv2_init"):
3b3e2687 1701 eap_connect(dev[0], hapd, "TTLS", "user",
ef318402
JM
1702 anonymous_identity="ttls", password="password",
1703 ca_cert="auth_serv/ca.pem", phase2="autheap=MSCHAPV2",
1704 expect_failure=True)
1705 dev[0].request("REMOVE_NETWORK all")
1706
1707 with alloc_fail(hapd, 1, "eap_mschapv2_build_challenge"):
1708 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP WPA-EAP-SHA256",
1709 eap="TTLS", identity="user",
1710 anonymous_identity="ttls", password="password",
1711 ca_cert="auth_serv/ca.pem", phase2="autheap=MSCHAPV2",
1712 wait_connect=False, scan_freq="2412")
1713 # This would eventually time out, but we can stop after having reached
1714 # the allocation failure.
1715 for i in range(20):
1716 time.sleep(0.1)
1717 if hapd.request("GET_ALLOC_FAIL").startswith('0'):
1718 break
1719 dev[0].request("REMOVE_NETWORK all")
1720
1721 with alloc_fail(hapd, 1, "eap_mschapv2_build_success_req"):
1722 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP WPA-EAP-SHA256",
1723 eap="TTLS", identity="user",
1724 anonymous_identity="ttls", password="password",
1725 ca_cert="auth_serv/ca.pem", phase2="autheap=MSCHAPV2",
1726 wait_connect=False, scan_freq="2412")
1727 # This would eventually time out, but we can stop after having reached
1728 # the allocation failure.
1729 for i in range(20):
1730 time.sleep(0.1)
1731 if hapd.request("GET_ALLOC_FAIL").startswith('0'):
1732 break
1733 dev[0].request("REMOVE_NETWORK all")
1734
1735 with alloc_fail(hapd, 1, "eap_mschapv2_build_failure_req"):
1736 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP WPA-EAP-SHA256",
1737 eap="TTLS", identity="user",
1738 anonymous_identity="ttls", password="wrong",
1739 ca_cert="auth_serv/ca.pem", phase2="autheap=MSCHAPV2",
1740 wait_connect=False, scan_freq="2412")
1741 # This would eventually time out, but we can stop after having reached
1742 # the allocation failure.
1743 for i in range(20):
1744 time.sleep(0.1)
1745 if hapd.request("GET_ALLOC_FAIL").startswith('0'):
1746 break
1747 dev[0].request("REMOVE_NETWORK all")
1748
f22bc118
JM
1749def test_ap_wpa2_eap_ttls_eap_sim(dev, apdev):
1750 """WPA2-Enterprise connection using EAP-TTLS/EAP-SIM"""
1751 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
1752 hapd = hostapd.add_ap(apdev[0], params)
1753 eap_connect(dev[0], hapd, "TTLS", "1232010000000000",
1754 anonymous_identity="1232010000000000@ttls",
1755 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581",
1756 ca_cert="auth_serv/ca.pem", phase2="autheap=SIM")
1757 eap_reauth(dev[0], "TTLS")
1758
1759def run_ext_sim_auth(dev):
1760 ev = dev.wait_event(["CTRL-REQ-SIM"], timeout=15)
1761 if ev is None:
1762 raise Exception("Wait for external SIM processing request timed out")
1763 p = ev.split(':', 2)
1764 if p[1] != "GSM-AUTH":
1765 raise Exception("Unexpected CTRL-REQ-SIM type")
1766 rid = p[0].split('-')[3]
1767 rand = p[2].split(' ')[0]
1768
1769 res = subprocess.check_output(["../../hostapd/hlr_auc_gw",
1770 "-m",
1771 "auth_serv/hlr_auc_gw.milenage_db",
d5e6ffd6 1772 "GSM-AUTH-REQ 232010000000000 " + rand]).decode()
f22bc118
JM
1773 if "GSM-AUTH-RESP" not in res:
1774 raise Exception("Unexpected hlr_auc_gw response")
1775 resp = res.split(' ')[2].rstrip()
1776
1777 dev.request("CTRL-RSP-SIM-" + rid + ":GSM-AUTH:" + resp)
1778 dev.wait_connected(timeout=15)
1779
1780 dev.dump_monitor()
1781 dev.request("REAUTHENTICATE")
1782 ev = dev.wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=5)
1783 if ev is None:
1784 raise Exception("EAP reauthentication did not succeed")
1785 ev = dev.wait_event(["WPA: Key negotiation completed"], timeout=5)
1786 if ev is None:
1787 raise Exception("Key negotiation did not complete")
1788 dev.dump_monitor()
1789
1790def test_ap_wpa2_eap_ttls_eap_sim_ext(dev, apdev):
1791 """WPA2-Enterprise connection using EAP-TTLS/EAP-SIM and external GSM auth"""
1792 check_hlr_auc_gw_support()
1793 try:
1794 run_ap_wpa2_eap_ttls_eap_sim_ext(dev, apdev)
1795 finally:
1796 dev[0].request("SET external_sim 0")
1797
1798def run_ap_wpa2_eap_ttls_eap_sim_ext(dev, apdev):
1799 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
1800 hapd = hostapd.add_ap(apdev[0], params)
1801 dev[0].request("SET external_sim 1")
1802 dev[0].connect("test-wpa2-eap", eap="TTLS", key_mgmt="WPA-EAP",
1803 identity="1232010000000000",
1804 anonymous_identity="1232010000000000@ttls",
1805 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581",
1806 ca_cert="auth_serv/ca.pem", phase2="autheap=SIM",
1807 wait_connect=False, scan_freq="2412")
1808 run_ext_sim_auth(dev[0])
1809
1810def test_ap_wpa2_eap_peap_eap_sim(dev, apdev):
1811 """WPA2-Enterprise connection using EAP-PEAP/EAP-SIM"""
1812 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
1813 hapd = hostapd.add_ap(apdev[0], params)
1814 eap_connect(dev[0], hapd, "PEAP", "1232010000000000",
1815 anonymous_identity="1232010000000000@peap",
1816 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581",
1817 ca_cert="auth_serv/ca.pem", phase2="auth=SIM")
1818 eap_reauth(dev[0], "PEAP")
1819
1820def test_ap_wpa2_eap_peap_eap_sim_ext(dev, apdev):
1821 """WPA2-Enterprise connection using EAP-PEAP/EAP-SIM and external GSM auth"""
1822 check_hlr_auc_gw_support()
1823 try:
1824 run_ap_wpa2_eap_peap_eap_sim_ext(dev, apdev)
1825 finally:
1826 dev[0].request("SET external_sim 0")
1827
1828def run_ap_wpa2_eap_peap_eap_sim_ext(dev, apdev):
1829 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
1830 hapd = hostapd.add_ap(apdev[0], params)
1831 dev[0].request("SET external_sim 1")
1832 dev[0].connect("test-wpa2-eap", eap="PEAP", key_mgmt="WPA-EAP",
1833 identity="1232010000000000",
1834 anonymous_identity="1232010000000000@peap",
1835 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581",
1836 ca_cert="auth_serv/ca.pem", phase2="auth=SIM",
1837 wait_connect=False, scan_freq="2412")
1838 run_ext_sim_auth(dev[0])
1839
1840def test_ap_wpa2_eap_fast_eap_sim(dev, apdev):
1841 """WPA2-Enterprise connection using EAP-FAST/EAP-SIM"""
9626bfbb 1842 check_eap_capa(dev[0], "FAST")
f22bc118
JM
1843 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
1844 hapd = hostapd.add_ap(apdev[0], params)
1845 eap_connect(dev[0], hapd, "FAST", "1232010000000000",
1846 anonymous_identity="1232010000000000@fast",
1847 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581",
1848 phase1="fast_provisioning=2",
1849 pac_file="blob://fast_pac_auth_sim",
1850 ca_cert="auth_serv/ca.pem", phase2="auth=SIM")
1851 eap_reauth(dev[0], "FAST")
1852
1853def test_ap_wpa2_eap_fast_eap_sim_ext(dev, apdev):
1854 """WPA2-Enterprise connection using EAP-FAST/EAP-SIM and external GSM auth"""
1855 check_hlr_auc_gw_support()
1856 try:
1857 run_ap_wpa2_eap_fast_eap_sim_ext(dev, apdev)
1858 finally:
1859 dev[0].request("SET external_sim 0")
1860
1861def run_ap_wpa2_eap_fast_eap_sim_ext(dev, apdev):
1862 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
1863 hapd = hostapd.add_ap(apdev[0], params)
1864 dev[0].request("SET external_sim 1")
1865 dev[0].connect("test-wpa2-eap", eap="PEAP", key_mgmt="WPA-EAP",
1866 identity="1232010000000000",
1867 anonymous_identity="1232010000000000@peap",
1868 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581",
1869 phase1="fast_provisioning=2",
1870 pac_file="blob://fast_pac_auth_sim",
1871 ca_cert="auth_serv/ca.pem", phase2="auth=SIM",
1872 wait_connect=False, scan_freq="2412")
1873 run_ext_sim_auth(dev[0])
1874
95fb531c
JM
1875def test_ap_wpa2_eap_ttls_eap_aka(dev, apdev):
1876 """WPA2-Enterprise connection using EAP-TTLS/EAP-AKA"""
1877 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
1878 hapd = hostapd.add_ap(apdev[0], params)
1879 eap_connect(dev[0], hapd, "TTLS", "0232010000000000",
95fb531c
JM
1880 anonymous_identity="0232010000000000@ttls",
1881 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581:000000000123",
1882 ca_cert="auth_serv/ca.pem", phase2="autheap=AKA")
8a303f09 1883 eap_reauth(dev[0], "TTLS")
95fb531c
JM
1884
1885def test_ap_wpa2_eap_peap_eap_aka(dev, apdev):
1886 """WPA2-Enterprise connection using EAP-PEAP/EAP-AKA"""
1887 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
1888 hapd = hostapd.add_ap(apdev[0], params)
1889 eap_connect(dev[0], hapd, "PEAP", "0232010000000000",
95fb531c
JM
1890 anonymous_identity="0232010000000000@peap",
1891 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581:000000000123",
1892 ca_cert="auth_serv/ca.pem", phase2="auth=AKA")
8a303f09 1893 eap_reauth(dev[0], "PEAP")
95fb531c
JM
1894
1895def test_ap_wpa2_eap_fast_eap_aka(dev, apdev):
1896 """WPA2-Enterprise connection using EAP-FAST/EAP-AKA"""
3b51cc63 1897 check_eap_capa(dev[0], "FAST")
95fb531c 1898 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
1899 hapd = hostapd.add_ap(apdev[0], params)
1900 eap_connect(dev[0], hapd, "FAST", "0232010000000000",
95fb531c
JM
1901 anonymous_identity="0232010000000000@fast",
1902 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581:000000000123",
1903 phase1="fast_provisioning=2",
1904 pac_file="blob://fast_pac_auth_aka",
1905 ca_cert="auth_serv/ca.pem", phase2="auth=AKA")
8a303f09 1906 eap_reauth(dev[0], "FAST")
95fb531c 1907
9626962d
JM
1908def test_ap_wpa2_eap_peap_eap_mschapv2(dev, apdev):
1909 """WPA2-Enterprise connection using EAP-PEAP/EAP-MSCHAPv2"""
e7ac04ce 1910 check_eap_capa(dev[0], "MSCHAPV2")
9626962d 1911 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1912 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1913 eap_connect(dev[0], hapd, "PEAP", "user",
698f8324 1914 anonymous_identity="peap", password="password",
9626962d 1915 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2")
a8375c94 1916 hwsim_utils.test_connectivity(dev[0], hapd)
75b2b9cf 1917 eap_reauth(dev[0], "PEAP")
6daf5b9c 1918 dev[0].request("REMOVE_NETWORK all")
3b3e2687 1919 eap_connect(dev[0], hapd, "PEAP", "user",
6daf5b9c
JM
1920 anonymous_identity="peap", password="password",
1921 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
1922 fragment_size="200")
c7afc078 1923
fa0ddb14
JM
1924 logger.info("Password as hash value")
1925 dev[0].request("REMOVE_NETWORK all")
3b3e2687 1926 eap_connect(dev[0], hapd, "PEAP", "user",
fa0ddb14
JM
1927 anonymous_identity="peap",
1928 password_hex="hash:8846f7eaee8fb117ad06bdd830b7586c",
1929 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2")
1930
f10ba3b2
JM
1931 logger.info("Negative test with incorrect password")
1932 dev[0].request("REMOVE_NETWORK all")
3b3e2687 1933 eap_connect(dev[0], hapd, "PEAP", "user",
f10ba3b2
JM
1934 anonymous_identity="peap", password="password1",
1935 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
1936 expect_failure=True)
1937
0d33f504
JM
1938def test_ap_wpa2_eap_peap_eap_mschapv2_domain(dev, apdev):
1939 """WPA2-Enterprise connection using EAP-PEAP/EAP-MSCHAPv2 with domain"""
e7ac04ce 1940 check_eap_capa(dev[0], "MSCHAPV2")
0d33f504 1941 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1942 hapd = hostapd.add_ap(apdev[0], params)
bc664dfc 1943 eap_connect(dev[0], hapd, "PEAP", r"DOMAIN\user3",
0d33f504
JM
1944 anonymous_identity="peap", password="password",
1945 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2")
1946 hwsim_utils.test_connectivity(dev[0], hapd)
1947 eap_reauth(dev[0], "PEAP")
1948
f4cd0f64
JM
1949def test_ap_wpa2_eap_peap_eap_mschapv2_incorrect_password(dev, apdev):
1950 """WPA2-Enterprise connection using EAP-PEAP/EAP-MSCHAPv2 - incorrect password"""
e7ac04ce 1951 check_eap_capa(dev[0], "MSCHAPV2")
f4cd0f64 1952 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1953 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1954 eap_connect(dev[0], hapd, "PEAP", "user",
f4cd0f64
JM
1955 anonymous_identity="peap", password="wrong",
1956 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
1957 expect_failure=True)
1958
698f8324
JM
1959def test_ap_wpa2_eap_peap_crypto_binding(dev, apdev):
1960 """WPA2-Enterprise connection using EAP-PEAPv0/EAP-MSCHAPv2 and crypto binding"""
e7ac04ce 1961 check_eap_capa(dev[0], "MSCHAPV2")
698f8324 1962 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 1963 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 1964 eap_connect(dev[0], hapd, "PEAP", "user", password="password",
698f8324
JM
1965 ca_cert="auth_serv/ca.pem",
1966 phase1="peapver=0 crypto_binding=2",
1967 phase2="auth=MSCHAPV2")
a8375c94 1968 hwsim_utils.test_connectivity(dev[0], hapd)
75b2b9cf 1969 eap_reauth(dev[0], "PEAP")
698f8324 1970
3b3e2687 1971 eap_connect(dev[1], hapd, "PEAP", "user", password="password",
ea6464b0
JM
1972 ca_cert="auth_serv/ca.pem",
1973 phase1="peapver=0 crypto_binding=1",
1974 phase2="auth=MSCHAPV2")
3b3e2687 1975 eap_connect(dev[2], hapd, "PEAP", "user", password="password",
ea6464b0
JM
1976 ca_cert="auth_serv/ca.pem",
1977 phase1="peapver=0 crypto_binding=0",
1978 phase2="auth=MSCHAPV2")
1979
ef318402
JM
1980def test_ap_wpa2_eap_peap_crypto_binding_server_oom(dev, apdev):
1981 """WPA2-Enterprise connection using EAP-PEAPv0/EAP-MSCHAPv2 and crypto binding with server OOM"""
e7ac04ce 1982 check_eap_capa(dev[0], "MSCHAPV2")
ef318402 1983 params = int_eap_server_params()
8b8a1864 1984 hapd = hostapd.add_ap(apdev[0], params)
ef318402 1985 with alloc_fail(hapd, 1, "eap_mschapv2_getKey"):
3b3e2687 1986 eap_connect(dev[0], hapd, "PEAP", "user", password="password",
ef318402
JM
1987 ca_cert="auth_serv/ca.pem",
1988 phase1="peapver=0 crypto_binding=2",
1989 phase2="auth=MSCHAPV2",
1990 expect_failure=True, local_error_report=True)
1991
c4d37011
JM
1992def test_ap_wpa2_eap_peap_params(dev, apdev):
1993 """WPA2-Enterprise connection using EAP-PEAPv0/EAP-MSCHAPv2 and various parameters"""
e7ac04ce 1994 check_eap_capa(dev[0], "MSCHAPV2")
c4d37011 1995 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
1996 hapd = hostapd.add_ap(apdev[0], params)
1997 eap_connect(dev[0], hapd, "PEAP", "user",
c4d37011
JM
1998 anonymous_identity="peap", password="password",
1999 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
2000 phase1="peapver=0 peaplabel=1",
2001 expect_failure=True)
2002 dev[0].request("REMOVE_NETWORK all")
09ad98c5
JM
2003 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="PEAP",
2004 identity="user",
2005 anonymous_identity="peap", password="password",
2006 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
2007 phase1="peap_outer_success=0",
2008 wait_connect=False, scan_freq="2412")
2009 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
2010 if ev is None:
2011 raise Exception("No EAP success seen")
2012 # This won't succeed to connect with peap_outer_success=0, so stop here.
2013 dev[0].request("REMOVE_NETWORK all")
2014 dev[0].wait_disconnected()
3b3e2687 2015 eap_connect(dev[1], hapd, "PEAP", "user", password="password",
c4d37011
JM
2016 ca_cert="auth_serv/ca.pem",
2017 phase1="peap_outer_success=1",
2018 phase2="auth=MSCHAPV2")
3b3e2687 2019 eap_connect(dev[2], hapd, "PEAP", "user", password="password",
c4d37011
JM
2020 ca_cert="auth_serv/ca.pem",
2021 phase1="peap_outer_success=2",
2022 phase2="auth=MSCHAPV2")
2023 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="PEAP",
2024 identity="user",
2025 anonymous_identity="peap", password="password",
2026 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
2027 phase1="peapver=1 peaplabel=1",
2028 wait_connect=False, scan_freq="2412")
2029 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
2030 if ev is None:
2031 raise Exception("No EAP success seen")
8cfc7588
JM
2032 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED",
2033 "CTRL-EVENT-DISCONNECTED"], timeout=1)
2034 if ev and "CTRL-EVENT-CONNECTED" in ev:
c4d37011 2035 raise Exception("Unexpected connection")
8cfc7588 2036 dev[0].request("REMOVE_NETWORK all")
e01a492c 2037 dev[0].disconnect_and_stop_scan()
c4d37011 2038
fab49f61
JM
2039 tests = [("peap-ver0", ""),
2040 ("peap-ver1", ""),
2041 ("peap-ver0", "peapver=0"),
2042 ("peap-ver1", "peapver=1")]
2043 for anon, phase1 in tests:
09a4404a
JM
2044 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="PEAP",
2045 identity="user", anonymous_identity=anon,
2046 password="password", phase1=phase1,
2047 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
2048 scan_freq="2412")
2049 dev[0].request("REMOVE_NETWORK all")
2050 dev[0].wait_disconnected()
2051
fab49f61
JM
2052 tests = [("peap-ver0", "peapver=1"),
2053 ("peap-ver1", "peapver=0")]
2054 for anon, phase1 in tests:
09a4404a
JM
2055 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="PEAP",
2056 identity="user", anonymous_identity=anon,
2057 password="password", phase1=phase1,
2058 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
2059 wait_connect=False, scan_freq="2412")
2060 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=15)
2061 if ev is None:
2062 raise Exception("No EAP-Failure seen")
2063 dev[0].request("REMOVE_NETWORK all")
2064 dev[0].wait_disconnected()
2065
3b3e2687 2066 eap_connect(dev[0], hapd, "PEAP", "user", password="password",
d5f5d260
JM
2067 ca_cert="auth_serv/ca.pem",
2068 phase1="tls_allow_md5=1 tls_disable_session_ticket=1 tls_disable_tlsv1_0=0 tls_disable_tlsv1_1=0 tls_disable_tlsv1_2=0 tls_ext_cert_check=0",
2069 phase2="auth=MSCHAPV2")
2070
836f0dda
JM
2071def test_ap_wpa2_eap_peap_eap_gtc(dev, apdev, params):
2072 """WPA2-Enterprise connection using EAP-PEAP/EAP-GTC"""
2073 p = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
2074 hapd = hostapd.add_ap(apdev[0], p)
2075 eap_connect(dev[0], hapd, "PEAP", "user", phase1="peapver=1",
2076 anonymous_identity="peap", password="password",
2077 ca_cert="auth_serv/ca.pem", phase2="auth=GTC")
2078
d0ce1050
JM
2079def test_ap_wpa2_eap_peap_eap_tls(dev, apdev):
2080 """WPA2-Enterprise connection using EAP-PEAP/EAP-TLS"""
2081 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
2082 hapd = hostapd.add_ap(apdev[0], params)
2083 eap_connect(dev[0], hapd, "PEAP", "cert user",
d0ce1050
JM
2084 ca_cert="auth_serv/ca.pem", phase2="auth=TLS",
2085 ca_cert2="auth_serv/ca.pem",
2086 client_cert2="auth_serv/user.pem",
2087 private_key2="auth_serv/user.key")
2088 eap_reauth(dev[0], "PEAP")
2089
e114c49c
JM
2090def test_ap_wpa2_eap_tls(dev, apdev):
2091 """WPA2-Enterprise connection using EAP-TLS"""
2092 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
2093 hapd = hostapd.add_ap(apdev[0], params)
2094 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
e114c49c
JM
2095 client_cert="auth_serv/user.pem",
2096 private_key="auth_serv/user.key")
75b2b9cf 2097 eap_reauth(dev[0], "TLS")
e114c49c 2098
96bf8fe1
JM
2099def test_eap_tls_pkcs8_pkcs5_v2_des3(dev, apdev):
2100 """WPA2-Enterprise connection using EAP-TLS and PKCS #8, PKCS #5 v2 DES3 key"""
2101 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
2102 hapd = hostapd.add_ap(apdev[0], params)
2103 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
96bf8fe1
JM
2104 client_cert="auth_serv/user.pem",
2105 private_key="auth_serv/user.key.pkcs8",
2106 private_key_passwd="whatever")
2107
2108def test_eap_tls_pkcs8_pkcs5_v15(dev, apdev):
2109 """WPA2-Enterprise connection using EAP-TLS and PKCS #8, PKCS #5 v1.5 key"""
969e5250 2110 check_pkcs5_v15_support(dev[0])
96bf8fe1 2111 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
2112 hapd = hostapd.add_ap(apdev[0], params)
2113 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
96bf8fe1
JM
2114 client_cert="auth_serv/user.pem",
2115 private_key="auth_serv/user.key.pkcs8.pkcs5v15",
2116 private_key_passwd="whatever")
2117
6ea231e6
JM
2118def test_ap_wpa2_eap_tls_blob(dev, apdev):
2119 """WPA2-Enterprise connection using EAP-TLS and config blobs"""
2120 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687 2121 hapd = hostapd.add_ap(apdev[0], params)
6ea231e6 2122 cert = read_pem("auth_serv/ca.pem")
54c58f29 2123 if "OK" not in dev[0].request("SET blob cacert " + binascii.hexlify(cert).decode()):
6ea231e6
JM
2124 raise Exception("Could not set cacert blob")
2125 cert = read_pem("auth_serv/user.pem")
54c58f29 2126 if "OK" not in dev[0].request("SET blob usercert " + binascii.hexlify(cert).decode()):
6ea231e6 2127 raise Exception("Could not set usercert blob")
62750c3e 2128 key = read_pem("auth_serv/user.rsa-key")
54c58f29 2129 if "OK" not in dev[0].request("SET blob userkey " + binascii.hexlify(key).decode()):
6ea231e6 2130 raise Exception("Could not set cacert blob")
3b3e2687 2131 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="blob://cacert",
6ea231e6
JM
2132 client_cert="blob://usercert",
2133 private_key="blob://userkey")
2134
cef42a44
JM
2135def test_ap_wpa2_eap_tls_blob_missing(dev, apdev):
2136 """EAP-TLS and config blob missing"""
2137 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 2138 hostapd.add_ap(apdev[0], params)
cef42a44
JM
2139 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
2140 identity="tls user",
2141 ca_cert="blob://testing-blob-does-not-exist",
2142 client_cert="blob://testing-blob-does-not-exist",
2143 private_key="blob://testing-blob-does-not-exist",
2144 wait_connect=False, scan_freq="2412")
2145 ev = dev[0].wait_event(["EAP: Failed to initialize EAP method"], timeout=10)
2146 if ev is None:
2147 raise Exception("EAP failure not reported")
2148 dev[0].request("REMOVE_NETWORK all")
2149 dev[0].wait_disconnected()
2150
7cb27f89
JM
2151def test_ap_wpa2_eap_tls_with_tls_len(dev, apdev):
2152 """EAP-TLS and TLS Message Length in unfragmented packets"""
2153 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
2154 hapd = hostapd.add_ap(apdev[0], params)
2155 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
7cb27f89
JM
2156 phase1="include_tls_length=1",
2157 client_cert="auth_serv/user.pem",
2158 private_key="auth_serv/user.key")
2159
2d10eb0e
JM
2160def test_ap_wpa2_eap_tls_pkcs12(dev, apdev):
2161 """WPA2-Enterprise connection using EAP-TLS and PKCS#12"""
686eee77 2162 check_pkcs12_support(dev[0])
2d10eb0e 2163 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
2164 hapd = hostapd.add_ap(apdev[0], params)
2165 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
2d10eb0e
JM
2166 private_key="auth_serv/user.pkcs12",
2167 private_key_passwd="whatever")
2168 dev[0].request("REMOVE_NETWORK all")
0c83ae04
JM
2169 dev[0].wait_disconnected()
2170
2d10eb0e
JM
2171 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
2172 identity="tls user",
2173 ca_cert="auth_serv/ca.pem",
2174 private_key="auth_serv/user.pkcs12",
2175 wait_connect=False, scan_freq="2412")
2176 ev = dev[0].wait_event(["CTRL-REQ-PASSPHRASE"])
2177 if ev is None:
2178 raise Exception("Request for private key passphrase timed out")
2179 id = ev.split(':')[0].split('-')[-1]
2180 dev[0].request("CTRL-RSP-PASSPHRASE-" + id + ":whatever")
5f35a5e2 2181 dev[0].wait_connected(timeout=10)
0c83ae04
JM
2182 dev[0].request("REMOVE_NETWORK all")
2183 dev[0].wait_disconnected()
2184
6da3b745
JM
2185 # Run this twice to verify certificate chain handling with OpenSSL. Use two
2186 # different files to cover both cases of the extra certificate being the
2187 # one that signed the client certificate and it being unrelated to the
2188 # client certificate.
2189 for pkcs12 in "auth_serv/user2.pkcs12", "auth_serv/user3.pkcs12":
2190 for i in range(2):
3b3e2687 2191 eap_connect(dev[0], hapd, "TLS", "tls user",
6da3b745
JM
2192 ca_cert="auth_serv/ca.pem",
2193 private_key=pkcs12,
2194 private_key_passwd="whatever")
2195 dev[0].request("REMOVE_NETWORK all")
2196 dev[0].wait_disconnected()
2d10eb0e 2197
6ea231e6
JM
2198def test_ap_wpa2_eap_tls_pkcs12_blob(dev, apdev):
2199 """WPA2-Enterprise connection using EAP-TLS and PKCS#12 from configuration blob"""
686eee77 2200 check_pkcs12_support(dev[0])
6ea231e6 2201 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687 2202 hapd = hostapd.add_ap(apdev[0], params)
6ea231e6 2203 cert = read_pem("auth_serv/ca.pem")
54c58f29 2204 if "OK" not in dev[0].request("SET blob cacert " + binascii.hexlify(cert).decode()):
6ea231e6
JM
2205 raise Exception("Could not set cacert blob")
2206 with open("auth_serv/user.pkcs12", "rb") as f:
54c58f29 2207 if "OK" not in dev[0].request("SET blob pkcs12 " + binascii.hexlify(f.read()).decode()):
6ea231e6 2208 raise Exception("Could not set pkcs12 blob")
3b3e2687 2209 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="blob://cacert",
6ea231e6
JM
2210 private_key="blob://pkcs12",
2211 private_key_passwd="whatever")
2212
c7afc078
JM
2213def test_ap_wpa2_eap_tls_neg_incorrect_trust_root(dev, apdev):
2214 """WPA2-Enterprise negative test - incorrect trust root"""
e7ac04ce 2215 check_eap_capa(dev[0], "MSCHAPV2")
c7afc078 2216 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 2217 hostapd.add_ap(apdev[0], params)
6ea231e6 2218 cert = read_pem("auth_serv/ca-incorrect.pem")
54c58f29 2219 if "OK" not in dev[0].request("SET blob cacert " + binascii.hexlify(cert).decode()):
6ea231e6 2220 raise Exception("Could not set cacert blob")
c7afc078 2221 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
6ea231e6
JM
2222 identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
2223 password="password", phase2="auth=MSCHAPV2",
2224 ca_cert="blob://cacert",
2225 wait_connect=False, scan_freq="2412")
2226 dev[1].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
c7afc078
JM
2227 identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
2228 password="password", phase2="auth=MSCHAPV2",
2229 ca_cert="auth_serv/ca-incorrect.pem",
c65f23ab 2230 wait_connect=False, scan_freq="2412")
c7afc078 2231
6ea231e6 2232 for dev in (dev[0], dev[1]):
412c6030 2233 ev = dev.wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=16)
6ea231e6
JM
2234 if ev is None:
2235 raise Exception("Association and EAP start timed out")
c7afc078 2236
6ea231e6
JM
2237 ev = dev.wait_event(["CTRL-EVENT-EAP-METHOD"], timeout=10)
2238 if ev is None:
2239 raise Exception("EAP method selection timed out")
2240 if "TTLS" not in ev:
2241 raise Exception("Unexpected EAP method")
2242
2243 ev = dev.wait_event(["CTRL-EVENT-EAP-TLS-CERT-ERROR",
2244 "CTRL-EVENT-EAP-SUCCESS",
2245 "CTRL-EVENT-EAP-FAILURE",
2246 "CTRL-EVENT-CONNECTED",
2247 "CTRL-EVENT-DISCONNECTED"], timeout=10)
2248 if ev is None:
2249 raise Exception("EAP result timed out")
2250 if "CTRL-EVENT-EAP-TLS-CERT-ERROR" not in ev:
2251 raise Exception("TLS certificate error not reported")
2252
2253 ev = dev.wait_event(["CTRL-EVENT-EAP-SUCCESS",
2254 "CTRL-EVENT-EAP-FAILURE",
2255 "CTRL-EVENT-CONNECTED",
2256 "CTRL-EVENT-DISCONNECTED"], timeout=10)
2257 if ev is None:
2258 raise Exception("EAP result(2) timed out")
2259 if "CTRL-EVENT-EAP-FAILURE" not in ev:
2260 raise Exception("EAP failure not reported")
c7afc078 2261
6ea231e6
JM
2262 ev = dev.wait_event(["CTRL-EVENT-CONNECTED",
2263 "CTRL-EVENT-DISCONNECTED"], timeout=10)
2264 if ev is None:
2265 raise Exception("EAP result(3) timed out")
2266 if "CTRL-EVENT-DISCONNECTED" not in ev:
2267 raise Exception("Disconnection not reported")
c7afc078 2268
6ea231e6
JM
2269 ev = dev.wait_event(["CTRL-EVENT-SSID-TEMP-DISABLED"], timeout=10)
2270 if ev is None:
2271 raise Exception("Network block disabling not reported")
72c052d5 2272
9a5cfd70
JM
2273def test_ap_wpa2_eap_tls_diff_ca_trust(dev, apdev):
2274 """WPA2-Enterprise connection using EAP-TTLS/PAP and different CA trust"""
2275 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 2276 hapd = hostapd.add_ap(apdev[0], params)
9a5cfd70
JM
2277 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
2278 identity="pap user", anonymous_identity="ttls",
2279 password="password", phase2="auth=PAP",
2280 ca_cert="auth_serv/ca.pem",
2281 wait_connect=True, scan_freq="2412")
2282 id = dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
2283 identity="pap user", anonymous_identity="ttls",
2284 password="password", phase2="auth=PAP",
2285 ca_cert="auth_serv/ca-incorrect.pem",
2286 only_add_network=True, scan_freq="2412")
2287
2288 dev[0].request("DISCONNECT")
90ad11e6 2289 dev[0].wait_disconnected()
9a5cfd70
JM
2290 dev[0].dump_monitor()
2291 dev[0].select_network(id, freq="2412")
2292
2293 ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD vendor=0 method=21"], timeout=15)
2294 if ev is None:
2295 raise Exception("EAP-TTLS not re-started")
db98b587 2296
5f35a5e2 2297 ev = dev[0].wait_disconnected(timeout=15)
9a5cfd70
JM
2298 if "reason=23" not in ev:
2299 raise Exception("Proper reason code for disconnection not reported")
2300
2301def test_ap_wpa2_eap_tls_diff_ca_trust2(dev, apdev):
2302 """WPA2-Enterprise connection using EAP-TTLS/PAP and different CA trust"""
2303 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 2304 hapd = hostapd.add_ap(apdev[0], params)
9a5cfd70
JM
2305 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
2306 identity="pap user", anonymous_identity="ttls",
2307 password="password", phase2="auth=PAP",
2308 wait_connect=True, scan_freq="2412")
2309 id = dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
2310 identity="pap user", anonymous_identity="ttls",
2311 password="password", phase2="auth=PAP",
2312 ca_cert="auth_serv/ca-incorrect.pem",
2313 only_add_network=True, scan_freq="2412")
2314
2315 dev[0].request("DISCONNECT")
90ad11e6 2316 dev[0].wait_disconnected()
9a5cfd70
JM
2317 dev[0].dump_monitor()
2318 dev[0].select_network(id, freq="2412")
2319
2320 ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD vendor=0 method=21"], timeout=15)
2321 if ev is None:
2322 raise Exception("EAP-TTLS not re-started")
db98b587 2323
5f35a5e2 2324 ev = dev[0].wait_disconnected(timeout=15)
9a5cfd70
JM
2325 if "reason=23" not in ev:
2326 raise Exception("Proper reason code for disconnection not reported")
2327
2328def test_ap_wpa2_eap_tls_diff_ca_trust3(dev, apdev):
2329 """WPA2-Enterprise connection using EAP-TTLS/PAP and different CA trust"""
2330 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 2331 hapd = hostapd.add_ap(apdev[0], params)
9a5cfd70
JM
2332 id = dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
2333 identity="pap user", anonymous_identity="ttls",
2334 password="password", phase2="auth=PAP",
2335 ca_cert="auth_serv/ca.pem",
2336 wait_connect=True, scan_freq="2412")
2337 dev[0].request("DISCONNECT")
90ad11e6 2338 dev[0].wait_disconnected()
9a5cfd70
JM
2339 dev[0].dump_monitor()
2340 dev[0].set_network_quoted(id, "ca_cert", "auth_serv/ca-incorrect.pem")
2341 dev[0].select_network(id, freq="2412")
2342
2343 ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD vendor=0 method=21"], timeout=15)
2344 if ev is None:
2345 raise Exception("EAP-TTLS not re-started")
db98b587 2346
5f35a5e2 2347 ev = dev[0].wait_disconnected(timeout=15)
9a5cfd70
JM
2348 if "reason=23" not in ev:
2349 raise Exception("Proper reason code for disconnection not reported")
2350
72c052d5
JM
2351def test_ap_wpa2_eap_tls_neg_suffix_match(dev, apdev):
2352 """WPA2-Enterprise negative test - domain suffix mismatch"""
e78eb404 2353 check_domain_suffix_match(dev[0])
72c052d5 2354 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 2355 hostapd.add_ap(apdev[0], params)
72c052d5
JM
2356 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
2357 identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
2358 password="password", phase2="auth=MSCHAPV2",
2359 ca_cert="auth_serv/ca.pem",
2360 domain_suffix_match="incorrect.example.com",
c65f23ab 2361 wait_connect=False, scan_freq="2412")
72c052d5 2362
412c6030 2363 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=16)
72c052d5
JM
2364 if ev is None:
2365 raise Exception("Association and EAP start timed out")
2366
2367 ev = dev[0].wait_event(["CTRL-EVENT-EAP-METHOD"], timeout=10)
2368 if ev is None:
2369 raise Exception("EAP method selection timed out")
2370 if "TTLS" not in ev:
2371 raise Exception("Unexpected EAP method")
2372
2373 ev = dev[0].wait_event(["CTRL-EVENT-EAP-TLS-CERT-ERROR",
2374 "CTRL-EVENT-EAP-SUCCESS",
2375 "CTRL-EVENT-EAP-FAILURE",
2376 "CTRL-EVENT-CONNECTED",
2377 "CTRL-EVENT-DISCONNECTED"], timeout=10)
2378 if ev is None:
2379 raise Exception("EAP result timed out")
2380 if "CTRL-EVENT-EAP-TLS-CERT-ERROR" not in ev:
2381 raise Exception("TLS certificate error not reported")
2382 if "Domain suffix mismatch" not in ev:
2383 raise Exception("Domain suffix mismatch not reported")
2384
2385 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS",
2386 "CTRL-EVENT-EAP-FAILURE",
2387 "CTRL-EVENT-CONNECTED",
2388 "CTRL-EVENT-DISCONNECTED"], timeout=10)
2389 if ev is None:
2390 raise Exception("EAP result(2) timed out")
2391 if "CTRL-EVENT-EAP-FAILURE" not in ev:
2392 raise Exception("EAP failure not reported")
2393
2394 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED",
2395 "CTRL-EVENT-DISCONNECTED"], timeout=10)
2396 if ev is None:
2397 raise Exception("EAP result(3) timed out")
2398 if "CTRL-EVENT-DISCONNECTED" not in ev:
2399 raise Exception("Disconnection not reported")
2400
2401 ev = dev[0].wait_event(["CTRL-EVENT-SSID-TEMP-DISABLED"], timeout=10)
2402 if ev is None:
2403 raise Exception("Network block disabling not reported")
22b99086 2404
061cbb25
JM
2405def test_ap_wpa2_eap_tls_neg_domain_match(dev, apdev):
2406 """WPA2-Enterprise negative test - domain mismatch"""
e78eb404 2407 check_domain_match(dev[0])
061cbb25 2408 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 2409 hostapd.add_ap(apdev[0], params)
061cbb25
JM
2410 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
2411 identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
2412 password="password", phase2="auth=MSCHAPV2",
2413 ca_cert="auth_serv/ca.pem",
2414 domain_match="w1.fi",
2415 wait_connect=False, scan_freq="2412")
2416
412c6030 2417 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=16)
061cbb25
JM
2418 if ev is None:
2419 raise Exception("Association and EAP start timed out")
2420
2421 ev = dev[0].wait_event(["CTRL-EVENT-EAP-METHOD"], timeout=10)
2422 if ev is None:
2423 raise Exception("EAP method selection timed out")
2424 if "TTLS" not in ev:
2425 raise Exception("Unexpected EAP method")
2426
2427 ev = dev[0].wait_event(["CTRL-EVENT-EAP-TLS-CERT-ERROR",
2428 "CTRL-EVENT-EAP-SUCCESS",
2429 "CTRL-EVENT-EAP-FAILURE",
2430 "CTRL-EVENT-CONNECTED",
2431 "CTRL-EVENT-DISCONNECTED"], timeout=10)
2432 if ev is None:
2433 raise Exception("EAP result timed out")
2434 if "CTRL-EVENT-EAP-TLS-CERT-ERROR" not in ev:
2435 raise Exception("TLS certificate error not reported")
2436 if "Domain mismatch" not in ev:
2437 raise Exception("Domain mismatch not reported")
2438
2439 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS",
2440 "CTRL-EVENT-EAP-FAILURE",
2441 "CTRL-EVENT-CONNECTED",
2442 "CTRL-EVENT-DISCONNECTED"], timeout=10)
2443 if ev is None:
2444 raise Exception("EAP result(2) timed out")
2445 if "CTRL-EVENT-EAP-FAILURE" not in ev:
2446 raise Exception("EAP failure not reported")
2447
2448 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED",
2449 "CTRL-EVENT-DISCONNECTED"], timeout=10)
2450 if ev is None:
2451 raise Exception("EAP result(3) timed out")
2452 if "CTRL-EVENT-DISCONNECTED" not in ev:
2453 raise Exception("Disconnection not reported")
2454
2455 ev = dev[0].wait_event(["CTRL-EVENT-SSID-TEMP-DISABLED"], timeout=10)
2456 if ev is None:
2457 raise Exception("Network block disabling not reported")
2458
3b74982f
JM
2459def test_ap_wpa2_eap_tls_neg_subject_match(dev, apdev):
2460 """WPA2-Enterprise negative test - subject mismatch"""
2461 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 2462 hostapd.add_ap(apdev[0], params)
3b74982f
JM
2463 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
2464 identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
2465 password="password", phase2="auth=MSCHAPV2",
2466 ca_cert="auth_serv/ca.pem",
2467 subject_match="/C=FI/O=w1.fi/CN=example.com",
2468 wait_connect=False, scan_freq="2412")
2469
412c6030 2470 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=16)
3b74982f
JM
2471 if ev is None:
2472 raise Exception("Association and EAP start timed out")
2473
506b2f05
JM
2474 ev = dev[0].wait_event(["CTRL-EVENT-EAP-METHOD",
2475 "EAP: Failed to initialize EAP method"], timeout=10)
3b74982f
JM
2476 if ev is None:
2477 raise Exception("EAP method selection timed out")
506b2f05
JM
2478 if "EAP: Failed to initialize EAP method" in ev:
2479 tls = dev[0].request("GET tls_library")
2480 if tls.startswith("OpenSSL"):
2481 raise Exception("Failed to select EAP method")
2482 logger.info("subject_match not supported - connection failed, so test succeeded")
2483 return
3b74982f
JM
2484 if "TTLS" not in ev:
2485 raise Exception("Unexpected EAP method")
2486
2487 ev = dev[0].wait_event(["CTRL-EVENT-EAP-TLS-CERT-ERROR",
2488 "CTRL-EVENT-EAP-SUCCESS",
2489 "CTRL-EVENT-EAP-FAILURE",
2490 "CTRL-EVENT-CONNECTED",
2491 "CTRL-EVENT-DISCONNECTED"], timeout=10)
2492 if ev is None:
2493 raise Exception("EAP result timed out")
2494 if "CTRL-EVENT-EAP-TLS-CERT-ERROR" not in ev:
2495 raise Exception("TLS certificate error not reported")
2496 if "Subject mismatch" not in ev:
2497 raise Exception("Subject mismatch not reported")
2498
2499 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS",
2500 "CTRL-EVENT-EAP-FAILURE",
2501 "CTRL-EVENT-CONNECTED",
2502 "CTRL-EVENT-DISCONNECTED"], timeout=10)
2503 if ev is None:
2504 raise Exception("EAP result(2) timed out")
2505 if "CTRL-EVENT-EAP-FAILURE" not in ev:
2506 raise Exception("EAP failure not reported")
2507
2508 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED",
2509 "CTRL-EVENT-DISCONNECTED"], timeout=10)
2510 if ev is None:
2511 raise Exception("EAP result(3) timed out")
2512 if "CTRL-EVENT-DISCONNECTED" not in ev:
2513 raise Exception("Disconnection not reported")
2514
2515 ev = dev[0].wait_event(["CTRL-EVENT-SSID-TEMP-DISABLED"], timeout=10)
2516 if ev is None:
2517 raise Exception("Network block disabling not reported")
2518
2519def test_ap_wpa2_eap_tls_neg_altsubject_match(dev, apdev):
2520 """WPA2-Enterprise negative test - altsubject mismatch"""
2521 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 2522 hostapd.add_ap(apdev[0], params)
37d61355 2523
fab49f61
JM
2524 tests = ["incorrect.example.com",
2525 "DNS:incorrect.example.com",
2526 "DNS:w1.fi",
2527 "DNS:erver.w1.fi"]
37d61355
JM
2528 for match in tests:
2529 _test_ap_wpa2_eap_tls_neg_altsubject_match(dev, apdev, match)
2530
2531def _test_ap_wpa2_eap_tls_neg_altsubject_match(dev, apdev, match):
3b74982f
JM
2532 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
2533 identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
2534 password="password", phase2="auth=MSCHAPV2",
2535 ca_cert="auth_serv/ca.pem",
37d61355 2536 altsubject_match=match,
3b74982f
JM
2537 wait_connect=False, scan_freq="2412")
2538
412c6030 2539 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=16)
3b74982f
JM
2540 if ev is None:
2541 raise Exception("Association and EAP start timed out")
2542
506b2f05
JM
2543 ev = dev[0].wait_event(["CTRL-EVENT-EAP-METHOD",
2544 "EAP: Failed to initialize EAP method"], timeout=10)
3b74982f
JM
2545 if ev is None:
2546 raise Exception("EAP method selection timed out")
506b2f05
JM
2547 if "EAP: Failed to initialize EAP method" in ev:
2548 tls = dev[0].request("GET tls_library")
2549 if tls.startswith("OpenSSL"):
2550 raise Exception("Failed to select EAP method")
2551 logger.info("altsubject_match not supported - connection failed, so test succeeded")
2552 return
3b74982f
JM
2553 if "TTLS" not in ev:
2554 raise Exception("Unexpected EAP method")
2555
2556 ev = dev[0].wait_event(["CTRL-EVENT-EAP-TLS-CERT-ERROR",
2557 "CTRL-EVENT-EAP-SUCCESS",
2558 "CTRL-EVENT-EAP-FAILURE",
2559 "CTRL-EVENT-CONNECTED",
2560 "CTRL-EVENT-DISCONNECTED"], timeout=10)
2561 if ev is None:
2562 raise Exception("EAP result timed out")
2563 if "CTRL-EVENT-EAP-TLS-CERT-ERROR" not in ev:
2564 raise Exception("TLS certificate error not reported")
2565 if "AltSubject mismatch" not in ev:
2566 raise Exception("altsubject mismatch not reported")
2567
2568 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS",
2569 "CTRL-EVENT-EAP-FAILURE",
2570 "CTRL-EVENT-CONNECTED",
2571 "CTRL-EVENT-DISCONNECTED"], timeout=10)
2572 if ev is None:
2573 raise Exception("EAP result(2) timed out")
2574 if "CTRL-EVENT-EAP-FAILURE" not in ev:
2575 raise Exception("EAP failure not reported")
2576
2577 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED",
2578 "CTRL-EVENT-DISCONNECTED"], timeout=10)
2579 if ev is None:
2580 raise Exception("EAP result(3) timed out")
2581 if "CTRL-EVENT-DISCONNECTED" not in ev:
2582 raise Exception("Disconnection not reported")
2583
2584 ev = dev[0].wait_event(["CTRL-EVENT-SSID-TEMP-DISABLED"], timeout=10)
2585 if ev is None:
2586 raise Exception("Network block disabling not reported")
2587
37d61355
JM
2588 dev[0].request("REMOVE_NETWORK all")
2589
5a0c1517
JM
2590def test_ap_wpa2_eap_unauth_tls(dev, apdev):
2591 """WPA2-Enterprise connection using UNAUTH-TLS"""
2592 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
2593 hapd = hostapd.add_ap(apdev[0], params)
2594 eap_connect(dev[0], hapd, "UNAUTH-TLS", "unauth-tls",
5a0c1517
JM
2595 ca_cert="auth_serv/ca.pem")
2596 eap_reauth(dev[0], "UNAUTH-TLS")
2597
57be05e1
JM
2598def test_ap_wpa2_eap_ttls_server_cert_hash(dev, apdev):
2599 """WPA2-Enterprise connection using EAP-TTLS and server certificate hash"""
4bf4e9db 2600 check_cert_probe_support(dev[0])
ca158ea6 2601 skip_with_fips(dev[0])
1dd66fc1 2602 srv_cert_hash = "4704e62784f36cc5fd964c6410402f4938773bb471dce9d42939bf22fdbdb2dd"
57be05e1 2603 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687 2604 hapd = hostapd.add_ap(apdev[0], params)
57be05e1
JM
2605 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
2606 identity="probe", ca_cert="probe://",
2607 wait_connect=False, scan_freq="2412")
412c6030 2608 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=16)
57be05e1
JM
2609 if ev is None:
2610 raise Exception("Association and EAP start timed out")
2611 ev = dev[0].wait_event(["CTRL-EVENT-EAP-PEER-CERT depth=0"], timeout=10)
2612 if ev is None:
2613 raise Exception("No peer server certificate event seen")
2614 if "hash=" + srv_cert_hash not in ev:
2615 raise Exception("Expected server certificate hash not reported")
2616 ev = dev[0].wait_event(["CTRL-EVENT-EAP-TLS-CERT-ERROR"], timeout=10)
2617 if ev is None:
2618 raise Exception("EAP result timed out")
2619 if "Server certificate chain probe" not in ev:
2620 raise Exception("Server certificate probe not reported")
5f35a5e2 2621 dev[0].wait_disconnected(timeout=10)
57be05e1
JM
2622 dev[0].request("REMOVE_NETWORK all")
2623
2624 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
2625 identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
2626 password="password", phase2="auth=MSCHAPV2",
2627 ca_cert="hash://server/sha256/5a1bc1296205e6fdbe3979728efe3920798885c1c4590b5f90f43222d239ca6a",
2628 wait_connect=False, scan_freq="2412")
412c6030 2629 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=16)
57be05e1
JM
2630 if ev is None:
2631 raise Exception("Association and EAP start timed out")
2632 ev = dev[0].wait_event(["CTRL-EVENT-EAP-TLS-CERT-ERROR"], timeout=10)
2633 if ev is None:
2634 raise Exception("EAP result timed out")
2635 if "Server certificate mismatch" not in ev:
2636 raise Exception("Server certificate mismatch not reported")
5f35a5e2 2637 dev[0].wait_disconnected(timeout=10)
57be05e1
JM
2638 dev[0].request("REMOVE_NETWORK all")
2639
3b3e2687 2640 eap_connect(dev[0], hapd, "TTLS", "DOMAIN\mschapv2 user",
57be05e1
JM
2641 anonymous_identity="ttls", password="password",
2642 ca_cert="hash://server/sha256/" + srv_cert_hash,
2643 phase2="auth=MSCHAPV2")
2644
2a6a2192
JM
2645def test_ap_wpa2_eap_ttls_server_cert_hash_invalid(dev, apdev):
2646 """WPA2-Enterprise connection using EAP-TTLS and server certificate hash (invalid config)"""
2647 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 2648 hostapd.add_ap(apdev[0], params)
2a6a2192
JM
2649 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
2650 identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
2651 password="password", phase2="auth=MSCHAPV2",
2652 ca_cert="hash://server/md5/5a1bc1296205e6fdbe3979728efe3920798885c1c4590b5f90f43222d239ca6a",
2653 wait_connect=False, scan_freq="2412")
2654 dev[1].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
2655 identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
2656 password="password", phase2="auth=MSCHAPV2",
2657 ca_cert="hash://server/sha256/5a1bc1296205e6fdbe3979728efe3920798885c1c4590b5f90f43222d239ca",
2658 wait_connect=False, scan_freq="2412")
2659 dev[2].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
2660 identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
2661 password="password", phase2="auth=MSCHAPV2",
2662 ca_cert="hash://server/sha256/5a1bc1296205e6fdbe3979728efe3920798885c1c4590b5f90f43222d239ca6Q",
2663 wait_connect=False, scan_freq="2412")
2664 for i in range(0, 3):
412c6030 2665 ev = dev[i].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=16)
2a6a2192
JM
2666 if ev is None:
2667 raise Exception("Association and EAP start timed out")
cbb85a03
JM
2668 ev = dev[i].wait_event(["EAP: Failed to initialize EAP method: vendor 0 method 21 (TTLS)"], timeout=5)
2669 if ev is None:
2670 raise Exception("Did not report EAP method initialization failure")
2a6a2192 2671
22b99086
JM
2672def test_ap_wpa2_eap_pwd(dev, apdev):
2673 """WPA2-Enterprise connection using EAP-pwd"""
3b51cc63 2674 check_eap_capa(dev[0], "PWD")
22b99086 2675 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
2676 hapd = hostapd.add_ap(apdev[0], params)
2677 eap_connect(dev[0], hapd, "PWD", "pwd user", password="secret password")
75b2b9cf 2678 eap_reauth(dev[0], "PWD")
6daf5b9c 2679 dev[0].request("REMOVE_NETWORK all")
0403fa0a 2680
3b3e2687 2681 eap_connect(dev[1], hapd, "PWD",
0403fa0a
JM
2682 "pwd.user@test123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.example.com",
2683 password="secret password",
6daf5b9c
JM
2684 fragment_size="90")
2685
f10ba3b2 2686 logger.info("Negative test with incorrect password")
3b3e2687 2687 eap_connect(dev[2], hapd, "PWD", "pwd user", password="secret-password",
f10ba3b2
JM
2688 expect_failure=True, local_error_report=True)
2689
3b3e2687 2690 eap_connect(dev[0], hapd, "PWD",
0403fa0a
JM
2691 "pwd.user@test123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.example.com",
2692 password="secret password",
2693 fragment_size="31")
2694
b898a6ee
JM
2695def test_ap_wpa2_eap_pwd_nthash(dev, apdev):
2696 """WPA2-Enterprise connection using EAP-pwd and NTHash"""
2697 check_eap_capa(dev[0], "PWD")
0392867b 2698 skip_with_fips(dev[0])
b898a6ee 2699 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
2700 hapd = hostapd.add_ap(apdev[0], params)
2701 eap_connect(dev[0], hapd, "PWD", "pwd-hash", password="secret password")
2702 eap_connect(dev[1], hapd, "PWD", "pwd-hash",
b898a6ee 2703 password_hex="hash:e3718ece8ab74792cbbfffd316d2d19a")
3b3e2687 2704 eap_connect(dev[2], hapd, "PWD", "pwd user",
b898a6ee
JM
2705 password_hex="hash:e3718ece8ab74792cbbfffd316d2d19a",
2706 expect_failure=True, local_error_report=True)
2707
5e597ed9
JM
2708def test_ap_wpa2_eap_pwd_salt_sha1(dev, apdev):
2709 """WPA2-Enterprise connection using EAP-pwd and salted password SHA-1"""
2710 check_eap_capa(dev[0], "PWD")
2711 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
2712 hapd = hostapd.add_ap(apdev[0], params)
2713 eap_connect(dev[0], hapd, "PWD", "pwd-hash-sha1",
2714 password="secret password")
2715
2716def test_ap_wpa2_eap_pwd_salt_sha256(dev, apdev):
2717 """WPA2-Enterprise connection using EAP-pwd and salted password SHA256"""
2718 check_eap_capa(dev[0], "PWD")
2719 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
2720 hapd = hostapd.add_ap(apdev[0], params)
2721 eap_connect(dev[0], hapd, "PWD", "pwd-hash-sha256",
2722 password="secret password")
2723
2724def test_ap_wpa2_eap_pwd_salt_sha512(dev, apdev):
2725 """WPA2-Enterprise connection using EAP-pwd and salted password SHA512"""
2726 check_eap_capa(dev[0], "PWD")
2727 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
2728 hapd = hostapd.add_ap(apdev[0], params)
2729 eap_connect(dev[0], hapd, "PWD", "pwd-hash-sha512",
2730 password="secret password")
2731
c075f040
JM
2732def test_ap_wpa2_eap_pwd_groups(dev, apdev):
2733 """WPA2-Enterprise connection using various EAP-pwd groups"""
3b51cc63 2734 check_eap_capa(dev[0], "PWD")
5f2e4547 2735 tls = dev[0].request("GET tls_library")
fab49f61
JM
2736 params = {"ssid": "test-wpa2-eap", "wpa": "2", "wpa_key_mgmt": "WPA-EAP",
2737 "rsn_pairwise": "CCMP", "ieee8021x": "1",
2738 "eap_server": "1", "eap_user_file": "auth_serv/eap_user.conf"}
caf4d1c9 2739 groups = [19, 20, 21]
f2d789f2 2740 if tls.startswith("OpenSSL") and "build=OpenSSL 1.0.2" in tls and "run=OpenSSL 1.0.2" in tls:
6bf7a547 2741 logger.info("Add Brainpool EC groups since OpenSSL is new enough")
caf4d1c9 2742 groups += [28, 29, 30]
6bf7a547 2743 if tls.startswith("OpenSSL") and "build=OpenSSL 1.1" in tls and "run=OpenSSL 1.1" in tls:
f2d789f2 2744 logger.info("Add Brainpool EC groups since OpenSSL is new enough")
caf4d1c9 2745 groups += [28, 29, 30]
f2d789f2
JM
2746 for i in groups:
2747 logger.info("Group %d" % i)
c075f040 2748 params['pwd_group'] = str(i)
3b3e2687 2749 hapd = hostapd.add_ap(apdev[0], params)
caf4d1c9
JM
2750 eap_connect(dev[0], hapd, "PWD", "pwd user",
2751 password="secret password")
2752 dev[0].request("REMOVE_NETWORK all")
2753 dev[0].wait_disconnected()
2754 dev[0].dump_monitor()
2755 hapd.disable()
c075f040 2756
4b2d2098
JM
2757def test_ap_wpa2_eap_pwd_invalid_group(dev, apdev):
2758 """WPA2-Enterprise connection using invalid EAP-pwd group"""
3b51cc63 2759 check_eap_capa(dev[0], "PWD")
fab49f61
JM
2760 params = {"ssid": "test-wpa2-eap", "wpa": "2", "wpa_key_mgmt": "WPA-EAP",
2761 "rsn_pairwise": "CCMP", "ieee8021x": "1",
2762 "eap_server": "1", "eap_user_file": "auth_serv/eap_user.conf"}
c7c267fa
JM
2763 for i in [0, 25, 26, 27]:
2764 logger.info("Group %d" % i)
2765 params['pwd_group'] = str(i)
2766 hapd = hostapd.add_ap(apdev[0], params)
2767 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="PWD",
2768 identity="pwd user", password="secret password",
2769 scan_freq="2412", wait_connect=False)
2770 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
2771 if ev is None:
2772 raise Exception("Timeout on EAP failure report (group %d)" % i)
2773 dev[0].request("REMOVE_NETWORK all")
2774 dev[0].wait_disconnected()
2775 dev[0].dump_monitor()
2776 hapd.disable()
4b2d2098 2777
8ba89e0a
JM
2778def test_ap_wpa2_eap_pwd_as_frag(dev, apdev):
2779 """WPA2-Enterprise connection using EAP-pwd with server fragmentation"""
3b51cc63 2780 check_eap_capa(dev[0], "PWD")
8ba89e0a 2781 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
fab49f61
JM
2782 params = {"ssid": "test-wpa2-eap", "wpa": "2", "wpa_key_mgmt": "WPA-EAP",
2783 "rsn_pairwise": "CCMP", "ieee8021x": "1",
2784 "eap_server": "1", "eap_user_file": "auth_serv/eap_user.conf",
2785 "pwd_group": "19", "fragment_size": "40"}
3b3e2687
JD
2786 hapd = hostapd.add_ap(apdev[0], params)
2787 eap_connect(dev[0], hapd, "PWD", "pwd user", password="secret password")
8ba89e0a 2788
22b99086
JM
2789def test_ap_wpa2_eap_gpsk(dev, apdev):
2790 """WPA2-Enterprise connection using EAP-GPSK"""
2791 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
2792 hapd = hostapd.add_ap(apdev[0], params)
2793 id = eap_connect(dev[0], hapd, "GPSK", "gpsk user",
369f9c20 2794 password="abcdefghijklmnop0123456789abcdef")
75b2b9cf 2795 eap_reauth(dev[0], "GPSK")
22b99086 2796
369f9c20 2797 logger.info("Test forced algorithm selection")
fab49f61 2798 for phase1 in ["cipher=1", "cipher=2"]:
369f9c20
JM
2799 dev[0].set_network_quoted(id, "phase1", phase1)
2800 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
2801 if ev is None:
2802 raise Exception("EAP success timed out")
5f35a5e2 2803 dev[0].wait_connected(timeout=10)
369f9c20
JM
2804
2805 logger.info("Test failed algorithm negotiation")
2806 dev[0].set_network_quoted(id, "phase1", "cipher=9")
2807 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
2808 if ev is None:
2809 raise Exception("EAP failure timed out")
2810
f10ba3b2
JM
2811 logger.info("Negative test with incorrect password")
2812 dev[0].request("REMOVE_NETWORK all")
3b3e2687 2813 eap_connect(dev[0], hapd, "GPSK", "gpsk user",
f10ba3b2
JM
2814 password="ffcdefghijklmnop0123456789abcdef",
2815 expect_failure=True)
2816
22b99086
JM
2817def test_ap_wpa2_eap_sake(dev, apdev):
2818 """WPA2-Enterprise connection using EAP-SAKE"""
2819 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
2820 hapd = hostapd.add_ap(apdev[0], params)
2821 eap_connect(dev[0], hapd, "SAKE", "sake user",
22b99086 2822 password_hex="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
75b2b9cf 2823 eap_reauth(dev[0], "SAKE")
22b99086 2824
f10ba3b2
JM
2825 logger.info("Negative test with incorrect password")
2826 dev[0].request("REMOVE_NETWORK all")
3b3e2687 2827 eap_connect(dev[0], hapd, "SAKE", "sake user",
f10ba3b2
JM
2828 password_hex="ff23456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
2829 expect_failure=True)
2830
22b99086
JM
2831def test_ap_wpa2_eap_eke(dev, apdev):
2832 """WPA2-Enterprise connection using EAP-EKE"""
2833 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
2834 hapd = hostapd.add_ap(apdev[0], params)
2835 id = eap_connect(dev[0], hapd, "EKE", "eke user", password="hello")
75b2b9cf 2836 eap_reauth(dev[0], "EKE")
22b99086 2837
2bb9e283 2838 logger.info("Test forced algorithm selection")
fab49f61
JM
2839 for phase1 in ["dhgroup=5 encr=1 prf=2 mac=2",
2840 "dhgroup=4 encr=1 prf=2 mac=2",
2841 "dhgroup=3 encr=1 prf=2 mac=2",
2842 "dhgroup=3 encr=1 prf=1 mac=1"]:
2bb9e283
JM
2843 dev[0].set_network_quoted(id, "phase1", phase1)
2844 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
2845 if ev is None:
2846 raise Exception("EAP success timed out")
5f35a5e2 2847 dev[0].wait_connected(timeout=10)
e8d8f4b6 2848 dev[0].dump_monitor()
2bb9e283
JM
2849
2850 logger.info("Test failed algorithm negotiation")
2851 dev[0].set_network_quoted(id, "phase1", "dhgroup=9 encr=9 prf=9 mac=9")
2852 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
2853 if ev is None:
2854 raise Exception("EAP failure timed out")
e8d8f4b6
JM
2855 dev[0].dump_monitor()
2856
2857 logger.info("Test unsupported algorithm proposals")
2858 dev[0].request("REMOVE_NETWORK all")
2859 dev[0].dump_monitor()
2860 eap_connect(dev[0], hapd, "EKE", "eke user", password="hello",
2861 phase1="dhgroup=2 encr=1 prf=1 mac=1", expect_failure=True)
2862 dev[0].request("REMOVE_NETWORK all")
2863 dev[0].dump_monitor()
2864 eap_connect(dev[0], hapd, "EKE", "eke user", password="hello",
2865 phase1="dhgroup=1 encr=1 prf=1 mac=1", expect_failure=True)
2bb9e283 2866
f10ba3b2
JM
2867 logger.info("Negative test with incorrect password")
2868 dev[0].request("REMOVE_NETWORK all")
3b3e2687 2869 eap_connect(dev[0], hapd, "EKE", "eke user", password="hello1",
f10ba3b2
JM
2870 expect_failure=True)
2871
3b6f3b37
JM
2872def test_ap_wpa2_eap_eke_many(dev, apdev, params):
2873 """WPA2-Enterprise connection using EAP-EKE (many connections) [long]"""
2874 if not params['long']:
2875 raise HwsimSkip("Skip test case with long duration due to --long not specified")
2876 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 2877 hostapd.add_ap(apdev[0], params)
3b6f3b37
JM
2878 success = 0
2879 fail = 0
2880 for i in range(100):
2881 for j in range(3):
2882 dev[j].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="EKE",
2883 identity="eke user", password="hello",
2884 phase1="dhgroup=3 encr=1 prf=1 mac=1",
2885 scan_freq="2412", wait_connect=False)
2886 for j in range(3):
2887 ev = dev[j].wait_event(["CTRL-EVENT-CONNECTED",
2888 "CTRL-EVENT-DISCONNECTED"], timeout=15)
2889 if ev is None:
2890 raise Exception("No connected/disconnected event")
2891 if "CTRL-EVENT-DISCONNECTED" in ev:
2892 fail += 1
2893 # The RADIUS server limits on active sessions can be hit when
2894 # going through this test case, so try to give some more time
2895 # for the server to remove sessions.
2896 logger.info("Failed to connect i=%d j=%d" % (i, j))
2897 dev[j].request("REMOVE_NETWORK all")
2898 time.sleep(1)
2899 else:
2900 success += 1
2901 dev[j].request("REMOVE_NETWORK all")
2902 dev[j].wait_disconnected()
2903 dev[j].dump_monitor()
2904 logger.info("Total success=%d failure=%d" % (success, fail))
2905
f7e3c17b
JM
2906def test_ap_wpa2_eap_eke_serverid_nai(dev, apdev):
2907 """WPA2-Enterprise connection using EAP-EKE with serverid NAI"""
2908 params = int_eap_server_params()
2909 params['server_id'] = 'example.server@w1.fi'
3b3e2687
JD
2910 hapd = hostapd.add_ap(apdev[0], params)
2911 eap_connect(dev[0], hapd, "EKE", "eke user", password="hello")
f7e3c17b 2912
5e0bedc6
JM
2913def test_ap_wpa2_eap_eke_server_oom(dev, apdev):
2914 """WPA2-Enterprise connection using EAP-EKE with server OOM"""
2915 params = int_eap_server_params()
8b8a1864 2916 hapd = hostapd.add_ap(apdev[0], params)
5e0bedc6
JM
2917 dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
2918
fab49f61 2919 for count, func in [(1, "eap_eke_build_commit"),
5e0bedc6
JM
2920 (2, "eap_eke_build_commit"),
2921 (3, "eap_eke_build_commit"),
2922 (1, "eap_eke_build_confirm"),
2923 (2, "eap_eke_build_confirm"),
2924 (1, "eap_eke_process_commit"),
2925 (2, "eap_eke_process_commit"),
2926 (1, "eap_eke_process_confirm"),
2927 (1, "eap_eke_process_identity"),
2928 (2, "eap_eke_process_identity"),
2929 (3, "eap_eke_process_identity"),
fab49f61 2930 (4, "eap_eke_process_identity")]:
5e0bedc6 2931 with alloc_fail(hapd, count, func):
3b3e2687 2932 eap_connect(dev[0], hapd, "EKE", "eke user", password="hello",
5e0bedc6
JM
2933 expect_failure=True)
2934 dev[0].request("REMOVE_NETWORK all")
2935
fab49f61
JM
2936 for count, func, pw in [(1, "eap_eke_init", "hello"),
2937 (1, "eap_eke_get_session_id", "hello"),
2938 (1, "eap_eke_getKey", "hello"),
2939 (1, "eap_eke_build_msg", "hello"),
2940 (1, "eap_eke_build_failure", "wrong"),
2941 (1, "eap_eke_build_identity", "hello"),
2942 (2, "eap_eke_build_identity", "hello")]:
5e0bedc6
JM
2943 with alloc_fail(hapd, count, func):
2944 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP WPA-EAP-SHA256",
2945 eap="EKE", identity="eke user", password=pw,
2946 wait_connect=False, scan_freq="2412")
2947 # This would eventually time out, but we can stop after having
2948 # reached the allocation failure.
2949 for i in range(20):
2950 time.sleep(0.1)
2951 if hapd.request("GET_ALLOC_FAIL").startswith('0'):
2952 break
2953 dev[0].request("REMOVE_NETWORK all")
2954
2955 for count in range(1, 1000):
2956 try:
2957 with alloc_fail(hapd, count, "eap_server_sm_step"):
2958 dev[0].connect("test-wpa2-eap",
2959 key_mgmt="WPA-EAP WPA-EAP-SHA256",
2960 eap="EKE", identity="eke user", password=pw,
2961 wait_connect=False, scan_freq="2412")
2962 # This would eventually time out, but we can stop after having
2963 # reached the allocation failure.
2964 for i in range(10):
2965 time.sleep(0.1)
2966 if hapd.request("GET_ALLOC_FAIL").startswith('0'):
2967 break
2968 dev[0].request("REMOVE_NETWORK all")
bab493b9 2969 except Exception as e:
5e0bedc6
JM
2970 if str(e) == "Allocation failure did not trigger":
2971 if count < 30:
2972 raise Exception("Too few allocation failures")
2973 logger.info("%d allocation failures tested" % (count - 1))
2974 break
2975 raise e
2976
22b99086
JM
2977def test_ap_wpa2_eap_ikev2(dev, apdev):
2978 """WPA2-Enterprise connection using EAP-IKEv2"""
c8e82c94 2979 check_eap_capa(dev[0], "IKEV2")
22b99086 2980 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
2981 hapd = hostapd.add_ap(apdev[0], params)
2982 eap_connect(dev[0], hapd, "IKEV2", "ikev2 user",
cb33ee14 2983 password="ike password")
75b2b9cf 2984 eap_reauth(dev[0], "IKEV2")
6daf5b9c 2985 dev[0].request("REMOVE_NETWORK all")
3b3e2687 2986 eap_connect(dev[0], hapd, "IKEV2", "ikev2 user",
47a74ad8 2987 password="ike password", fragment_size="50")
22b99086 2988
f10ba3b2
JM
2989 logger.info("Negative test with incorrect password")
2990 dev[0].request("REMOVE_NETWORK all")
3b3e2687 2991 eap_connect(dev[0], hapd, "IKEV2", "ikev2 user",
f10ba3b2 2992 password="ike-password", expect_failure=True)
35372f6c
JM
2993 dev[0].request("REMOVE_NETWORK all")
2994
3b3e2687 2995 eap_connect(dev[0], hapd, "IKEV2", "ikev2 user",
35372f6c
JM
2996 password="ike password", fragment_size="0")
2997 dev[0].request("REMOVE_NETWORK all")
2998 dev[0].wait_disconnected()
f10ba3b2 2999
47a74ad8
JM
3000def test_ap_wpa2_eap_ikev2_as_frag(dev, apdev):
3001 """WPA2-Enterprise connection using EAP-IKEv2 with server fragmentation"""
c8e82c94 3002 check_eap_capa(dev[0], "IKEV2")
47a74ad8 3003 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
fab49f61
JM
3004 params = {"ssid": "test-wpa2-eap", "wpa": "2", "wpa_key_mgmt": "WPA-EAP",
3005 "rsn_pairwise": "CCMP", "ieee8021x": "1",
3006 "eap_server": "1", "eap_user_file": "auth_serv/eap_user.conf",
3007 "fragment_size": "50"}
3b3e2687
JD
3008 hapd = hostapd.add_ap(apdev[0], params)
3009 eap_connect(dev[0], hapd, "IKEV2", "ikev2 user",
47a74ad8
JM
3010 password="ike password")
3011 eap_reauth(dev[0], "IKEV2")
3012
f1ab79c3
JM
3013def test_ap_wpa2_eap_ikev2_oom(dev, apdev):
3014 """WPA2-Enterprise connection using EAP-IKEv2 and OOM"""
c8e82c94 3015 check_eap_capa(dev[0], "IKEV2")
f1ab79c3 3016 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 3017 hostapd.add_ap(apdev[0], params)
f1ab79c3 3018
fab49f61
JM
3019 tests = [(1, "dh_init"),
3020 (2, "dh_init"),
3021 (1, "dh_derive_shared")]
f1ab79c3
JM
3022 for count, func in tests:
3023 with alloc_fail(dev[0], count, func):
3024 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="IKEV2",
3025 identity="ikev2 user", password="ike password",
3026 wait_connect=False, scan_freq="2412")
3027 ev = dev[0].wait_event(["CTRL-EVENT-EAP-METHOD"], timeout=5)
3028 if ev is None:
3029 raise Exception("EAP method not selected")
3030 for i in range(10):
3031 if "0:" in dev[0].request("GET_ALLOC_FAIL"):
3032 break
3033 time.sleep(0.02)
3034 dev[0].request("REMOVE_NETWORK all")
3035
d8003dcb
SP
3036 tls = dev[0].request("GET tls_library")
3037 if not tls.startswith("wolfSSL"):
fab49f61 3038 tests = [(1, "os_get_random;dh_init")]
d8003dcb 3039 else:
fab49f61 3040 tests = [(1, "crypto_dh_init;dh_init")]
f1ab79c3
JM
3041 for count, func in tests:
3042 with fail_test(dev[0], count, func):
3043 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="IKEV2",
3044 identity="ikev2 user", password="ike password",
3045 wait_connect=False, scan_freq="2412")
3046 ev = dev[0].wait_event(["CTRL-EVENT-EAP-METHOD"], timeout=5)
3047 if ev is None:
3048 raise Exception("EAP method not selected")
3049 for i in range(10):
3050 if "0:" in dev[0].request("GET_FAIL"):
3051 break
3052 time.sleep(0.02)
3053 dev[0].request("REMOVE_NETWORK all")
3054
22b99086
JM
3055def test_ap_wpa2_eap_pax(dev, apdev):
3056 """WPA2-Enterprise connection using EAP-PAX"""
3057 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
3058 hapd = hostapd.add_ap(apdev[0], params)
3059 eap_connect(dev[0], hapd, "PAX", "pax.user@example.com",
22b99086 3060 password_hex="0123456789abcdef0123456789abcdef")
75b2b9cf 3061 eap_reauth(dev[0], "PAX")
22b99086 3062
f10ba3b2
JM
3063 logger.info("Negative test with incorrect password")
3064 dev[0].request("REMOVE_NETWORK all")
3b3e2687 3065 eap_connect(dev[0], hapd, "PAX", "pax.user@example.com",
f10ba3b2
JM
3066 password_hex="ff23456789abcdef0123456789abcdef",
3067 expect_failure=True)
3068
22b99086
JM
3069def test_ap_wpa2_eap_psk(dev, apdev):
3070 """WPA2-Enterprise connection using EAP-PSK"""
3071 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
2b005194
JM
3072 params["wpa_key_mgmt"] = "WPA-EAP-SHA256"
3073 params["ieee80211w"] = "2"
3b3e2687
JD
3074 hapd = hostapd.add_ap(apdev[0], params)
3075 eap_connect(dev[0], hapd, "PSK", "psk.user@example.com",
2b005194
JM
3076 password_hex="0123456789abcdef0123456789abcdef", sha256=True)
3077 eap_reauth(dev[0], "PSK", sha256=True)
fab49f61
JM
3078 check_mib(dev[0], [("dot11RSNAAuthenticationSuiteRequested", "00-0f-ac-5"),
3079 ("dot11RSNAAuthenticationSuiteSelected", "00-0f-ac-5")])
71390dc8 3080
d463c556
JM
3081 bss = dev[0].get_bss(apdev[0]['bssid'])
3082 if 'flags' not in bss:
3083 raise Exception("Could not get BSS flags from BSS table")
3084 if "[WPA2-EAP-SHA256-CCMP]" not in bss['flags']:
3085 raise Exception("Unexpected BSS flags: " + bss['flags'])
3086
f10ba3b2
JM
3087 logger.info("Negative test with incorrect password")
3088 dev[0].request("REMOVE_NETWORK all")
3b3e2687 3089 eap_connect(dev[0], hapd, "PSK", "psk.user@example.com",
f10ba3b2
JM
3090 password_hex="ff23456789abcdef0123456789abcdef", sha256=True,
3091 expect_failure=True)
3092
8c4e4c01
JM
3093def test_ap_wpa2_eap_psk_oom(dev, apdev):
3094 """WPA2-Enterprise connection using EAP-PSK and OOM"""
38934ed1 3095 skip_with_fips(dev[0])
8c4e4c01 3096 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 3097 hostapd.add_ap(apdev[0], params)
fab49f61
JM
3098 tests = [(1, "=aes_128_eax_encrypt"),
3099 (1, "=aes_128_eax_decrypt")]
7cbc8e67
JM
3100 for count, func in tests:
3101 with alloc_fail(dev[0], count, func):
3102 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="PSK",
3103 identity="psk.user@example.com",
3104 password_hex="0123456789abcdef0123456789abcdef",
3105 wait_connect=False, scan_freq="2412")
3106 ev = dev[0].wait_event(["CTRL-EVENT-EAP-METHOD"], timeout=5)
3107 if ev is None:
3108 raise Exception("EAP method not selected")
3109 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL",
3110 note="Failure not triggered: %d:%s" % (count, func))
3111 dev[0].request("REMOVE_NETWORK all")
3112 dev[0].wait_disconnected()
3113
fab49f61
JM
3114 tests = [(1, "aes_ctr_encrypt;aes_128_eax_encrypt"),
3115 (1, "omac1_aes_128;aes_128_eax_encrypt"),
3116 (2, "omac1_aes_128;aes_128_eax_encrypt"),
3117 (3, "omac1_aes_128;aes_128_eax_encrypt"),
3118 (1, "omac1_aes_vector"),
3119 (1, "omac1_aes_128;aes_128_eax_decrypt"),
3120 (2, "omac1_aes_128;aes_128_eax_decrypt"),
3121 (3, "omac1_aes_128;aes_128_eax_decrypt"),
3122 (1, "aes_ctr_encrypt;aes_128_eax_decrypt")]
8c4e4c01 3123 for count, func in tests:
7cbc8e67 3124 with fail_test(dev[0], count, func):
8c4e4c01
JM
3125 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="PSK",
3126 identity="psk.user@example.com",
3127 password_hex="0123456789abcdef0123456789abcdef",
3128 wait_connect=False, scan_freq="2412")
3129 ev = dev[0].wait_event(["CTRL-EVENT-EAP-METHOD"], timeout=5)
3130 if ev is None:
3131 raise Exception("EAP method not selected")
7cbc8e67
JM
3132 wait_fail_trigger(dev[0], "GET_FAIL",
3133 note="Failure not triggered: %d:%s" % (count, func))
8c4e4c01 3134 dev[0].request("REMOVE_NETWORK all")
7cbc8e67 3135 dev[0].wait_disconnected()
8c4e4c01 3136
7cbc8e67 3137 with fail_test(dev[0], 1, "aes_128_encrypt_block"):
8c4e4c01
JM
3138 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="PSK",
3139 identity="psk.user@example.com",
3140 password_hex="0123456789abcdef0123456789abcdef",
3141 wait_connect=False, scan_freq="2412")
3142 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
3143 if ev is None:
3144 raise Exception("EAP method failure not reported")
3145 dev[0].request("REMOVE_NETWORK all")
7cbc8e67 3146 dev[0].wait_disconnected()
8c4e4c01 3147
71390dc8
JM
3148def test_ap_wpa_eap_peap_eap_mschapv2(dev, apdev):
3149 """WPA-Enterprise connection using EAP-PEAP/EAP-MSCHAPv2"""
e7ac04ce 3150 check_eap_capa(dev[0], "MSCHAPV2")
71390dc8 3151 params = hostapd.wpa_eap_params(ssid="test-wpa-eap")
8b8a1864 3152 hapd = hostapd.add_ap(apdev[0], params)
71390dc8
JM
3153 dev[0].connect("test-wpa-eap", key_mgmt="WPA-EAP", eap="PEAP",
3154 identity="user", password="password", phase2="auth=MSCHAPV2",
3155 ca_cert="auth_serv/ca.pem", wait_connect=False,
3156 scan_freq="2412")
3157 eap_check_auth(dev[0], "PEAP", True, rsn=False)
a8375c94 3158 hwsim_utils.test_connectivity(dev[0], hapd)
71390dc8 3159 eap_reauth(dev[0], "PEAP", rsn=False)
fab49f61
JM
3160 check_mib(dev[0], [("dot11RSNAAuthenticationSuiteRequested", "00-50-f2-1"),
3161 ("dot11RSNAAuthenticationSuiteSelected", "00-50-f2-1")])
48bb2e68
JM
3162 status = dev[0].get_status(extra="VERBOSE")
3163 if 'portControl' not in status:
3164 raise Exception("portControl missing from STATUS-VERBOSE")
3165 if status['portControl'] != 'Auto':
3166 raise Exception("Unexpected portControl value: " + status['portControl'])
3167 if 'eap_session_id' not in status:
3168 raise Exception("eap_session_id missing from STATUS-VERBOSE")
3169 if not status['eap_session_id'].startswith("19"):
3170 raise Exception("Unexpected eap_session_id value: " + status['eap_session_id'])
40759604
JM
3171
3172def test_ap_wpa2_eap_interactive(dev, apdev):
3173 """WPA2-Enterprise connection using interactive identity/password entry"""
e7ac04ce 3174 check_eap_capa(dev[0], "MSCHAPV2")
40759604 3175 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
6f334bf7 3176 hapd = hostapd.add_ap(apdev[0], params)
40759604 3177
fab49f61
JM
3178 tests = [("Connection with dynamic TTLS/MSCHAPv2 password entry",
3179 "TTLS", "ttls", "DOMAIN\mschapv2 user", "auth=MSCHAPV2",
3180 None, "password"),
3181 ("Connection with dynamic TTLS/MSCHAPv2 identity and password entry",
3182 "TTLS", "ttls", None, "auth=MSCHAPV2",
3183 "DOMAIN\mschapv2 user", "password"),
3184 ("Connection with dynamic TTLS/EAP-MSCHAPv2 password entry",
3185 "TTLS", "ttls", "user", "autheap=MSCHAPV2", None, "password"),
3186 ("Connection with dynamic TTLS/EAP-MD5 password entry",
3187 "TTLS", "ttls", "user", "autheap=MD5", None, "password"),
3188 ("Connection with dynamic PEAP/EAP-MSCHAPv2 password entry",
3189 "PEAP", None, "user", "auth=MSCHAPV2", None, "password"),
3190 ("Connection with dynamic PEAP/EAP-GTC password entry",
3191 "PEAP", None, "user", "auth=GTC", None, "password")]
3192 for [desc, eap, anon, identity, phase2, req_id, req_pw] in tests:
40759604
JM
3193 logger.info(desc)
3194 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap=eap,
3195 anonymous_identity=anon, identity=identity,
3196 ca_cert="auth_serv/ca.pem", phase2=phase2,
3197 wait_connect=False, scan_freq="2412")
3198 if req_id:
3199 ev = dev[0].wait_event(["CTRL-REQ-IDENTITY"])
3200 if ev is None:
3201 raise Exception("Request for identity timed out")
3202 id = ev.split(':')[0].split('-')[-1]
3203 dev[0].request("CTRL-RSP-IDENTITY-" + id + ":" + req_id)
fab49f61 3204 ev = dev[0].wait_event(["CTRL-REQ-PASSWORD", "CTRL-REQ-OTP"])
40759604
JM
3205 if ev is None:
3206 raise Exception("Request for password timed out")
3207 id = ev.split(':')[0].split('-')[-1]
3208 type = "OTP" if "CTRL-REQ-OTP" in ev else "PASSWORD"
3209 dev[0].request("CTRL-RSP-" + type + "-" + id + ":" + req_pw)
5f35a5e2 3210 dev[0].wait_connected(timeout=10)
40759604 3211 dev[0].request("REMOVE_NETWORK all")
e745c811 3212
f455998a
JM
3213def test_ap_wpa2_eap_ext_enable_network_while_connected(dev, apdev):
3214 """WPA2-Enterprise interactive identity entry and ENABLE_NETWORK"""
3215 check_eap_capa(dev[0], "MSCHAPV2")
3216 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
6f334bf7 3217 hapd = hostapd.add_ap(apdev[0], params)
f455998a
JM
3218
3219 id_other = dev[0].connect("other", key_mgmt="NONE", scan_freq="2412",
3220 only_add_network=True)
3221
3222 req_id = "DOMAIN\mschapv2 user"
3223 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
3224 anonymous_identity="ttls", identity=None,
3225 password="password",
3226 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3227 wait_connect=False, scan_freq="2412")
3228 ev = dev[0].wait_event(["CTRL-REQ-IDENTITY"])
3229 if ev is None:
3230 raise Exception("Request for identity timed out")
3231 id = ev.split(':')[0].split('-')[-1]
3232 dev[0].request("CTRL-RSP-IDENTITY-" + id + ":" + req_id)
3233 dev[0].wait_connected(timeout=10)
3234
3235 if "OK" not in dev[0].request("ENABLE_NETWORK " + str(id_other)):
3236 raise Exception("Failed to enable network")
3237 ev = dev[0].wait_event(["SME: Trying to authenticate"], timeout=1)
3238 if ev is not None:
3239 raise Exception("Unexpected reconnection attempt on ENABLE_NETWORK")
3240 dev[0].request("REMOVE_NETWORK all")
3241
e745c811
JM
3242def test_ap_wpa2_eap_vendor_test(dev, apdev):
3243 """WPA2-Enterprise connection using EAP vendor test"""
3244 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
3245 hapd = hostapd.add_ap(apdev[0], params)
3246 eap_connect(dev[0], hapd, "VENDOR-TEST", "vendor-test")
e745c811 3247 eap_reauth(dev[0], "VENDOR-TEST")
3b3e2687 3248 eap_connect(dev[1], hapd, "VENDOR-TEST", "vendor-test",
467775c5 3249 password="pending")
53a6f06a 3250
79a3973c
JM
3251def test_ap_wpa2_eap_vendor_test_oom(dev, apdev):
3252 """WPA2-Enterprise connection using EAP vendor test (OOM)"""
3253 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 3254 hostapd.add_ap(apdev[0], params)
79a3973c 3255
fab49f61
JM
3256 tests = ["eap_vendor_test_init",
3257 "eap_msg_alloc;eap_vendor_test_process",
3258 "eap_vendor_test_getKey"]
79a3973c
JM
3259 for func in tests:
3260 with alloc_fail(dev[0], 1, func):
3261 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP",
3262 scan_freq="2412",
3263 eap="VENDOR-TEST", identity="vendor-test",
3264 wait_connect=False)
3265 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
3266 dev[0].request("REMOVE_NETWORK all")
3267 dev[0].wait_disconnected()
3268
53a6f06a
JM
3269def test_ap_wpa2_eap_fast_mschapv2_unauth_prov(dev, apdev):
3270 """WPA2-Enterprise connection using EAP-FAST/MSCHAPv2 and unauthenticated provisioning"""
3b51cc63 3271 check_eap_capa(dev[0], "FAST")
53a6f06a 3272 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 3273 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 3274 eap_connect(dev[0], hapd, "FAST", "user",
53a6f06a
JM
3275 anonymous_identity="FAST", password="password",
3276 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3277 phase1="fast_provisioning=1", pac_file="blob://fast_pac")
a8375c94 3278 hwsim_utils.test_connectivity(dev[0], hapd)
2fc4749c
JM
3279 res = eap_reauth(dev[0], "FAST")
3280 if res['tls_session_reused'] != '1':
3281 raise Exception("EAP-FAST could not use PAC session ticket")
53a6f06a 3282
873e7c29
JM
3283def test_ap_wpa2_eap_fast_pac_file(dev, apdev, params):
3284 """WPA2-Enterprise connection using EAP-FAST/MSCHAPv2 and PAC file"""
3b51cc63 3285 check_eap_capa(dev[0], "FAST")
873e7c29
JM
3286 pac_file = os.path.join(params['logdir'], "fast.pac")
3287 pac_file2 = os.path.join(params['logdir'], "fast-bin.pac")
3288 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687 3289 hapd = hostapd.add_ap(apdev[0], params)
873e7c29
JM
3290
3291 try:
3b3e2687 3292 eap_connect(dev[0], hapd, "FAST", "user",
873e7c29
JM
3293 anonymous_identity="FAST", password="password",
3294 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3295 phase1="fast_provisioning=1", pac_file=pac_file)
3296 with open(pac_file, "r") as f:
3297 data = f.read()
3298 if "wpa_supplicant EAP-FAST PAC file - version 1" not in data:
3299 raise Exception("PAC file header missing")
3300 if "PAC-Key=" not in data:
3301 raise Exception("PAC-Key missing from PAC file")
3302 dev[0].request("REMOVE_NETWORK all")
3b3e2687 3303 eap_connect(dev[0], hapd, "FAST", "user",
873e7c29
JM
3304 anonymous_identity="FAST", password="password",
3305 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3306 pac_file=pac_file)
3307
3b3e2687 3308 eap_connect(dev[1], hapd, "FAST", "user",
873e7c29
JM
3309 anonymous_identity="FAST", password="password",
3310 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3311 phase1="fast_provisioning=1 fast_pac_format=binary",
3312 pac_file=pac_file2)
3313 dev[1].request("REMOVE_NETWORK all")
3b3e2687 3314 eap_connect(dev[1], hapd, "FAST", "user",
873e7c29
JM
3315 anonymous_identity="FAST", password="password",
3316 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3317 phase1="fast_pac_format=binary",
3318 pac_file=pac_file2)
3319 finally:
b638f703
JM
3320 try:
3321 os.remove(pac_file)
3322 except:
3323 pass
3324 try:
3325 os.remove(pac_file2)
3326 except:
3327 pass
873e7c29 3328
c6ab1cdb
JM
3329def test_ap_wpa2_eap_fast_binary_pac(dev, apdev):
3330 """WPA2-Enterprise connection using EAP-FAST and binary PAC format"""
3b51cc63 3331 check_eap_capa(dev[0], "FAST")
c6ab1cdb 3332 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
3333 hapd = hostapd.add_ap(apdev[0], params)
3334 eap_connect(dev[0], hapd, "FAST", "user",
c6ab1cdb
JM
3335 anonymous_identity="FAST", password="password",
3336 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3337 phase1="fast_provisioning=1 fast_max_pac_list_len=1 fast_pac_format=binary",
3338 pac_file="blob://fast_pac_bin")
2fc4749c
JM
3339 res = eap_reauth(dev[0], "FAST")
3340 if res['tls_session_reused'] != '1':
3341 raise Exception("EAP-FAST could not use PAC session ticket")
c6ab1cdb 3342
d7ef6e63
JM
3343 # Verify fast_max_pac_list_len=0 special case
3344 dev[0].request("REMOVE_NETWORK all")
3345 dev[0].wait_disconnected()
3b3e2687 3346 eap_connect(dev[0], hapd, "FAST", "user",
d7ef6e63
JM
3347 anonymous_identity="FAST", password="password",
3348 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3349 phase1="fast_provisioning=1 fast_max_pac_list_len=0 fast_pac_format=binary",
3350 pac_file="blob://fast_pac_bin")
3351
46e094bd
JM
3352def test_ap_wpa2_eap_fast_missing_pac_config(dev, apdev):
3353 """WPA2-Enterprise connection using EAP-FAST and missing PAC config"""
3b51cc63 3354 check_eap_capa(dev[0], "FAST")
46e094bd 3355 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 3356 hostapd.add_ap(apdev[0], params)
46e094bd
JM
3357
3358 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="FAST",
3359 identity="user", anonymous_identity="FAST",
3360 password="password",
3361 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3362 pac_file="blob://fast_pac_not_in_use",
3363 wait_connect=False, scan_freq="2412")
3364 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
3365 if ev is None:
3366 raise Exception("Timeout on EAP failure report")
3367 dev[0].request("REMOVE_NETWORK all")
3368
3369 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="FAST",
3370 identity="user", anonymous_identity="FAST",
3371 password="password",
3372 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3373 wait_connect=False, scan_freq="2412")
3374 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
3375 if ev is None:
3376 raise Exception("Timeout on EAP failure report")
3377
93aa1e16
JM
3378def test_ap_wpa2_eap_fast_binary_pac_errors(dev, apdev):
3379 """EAP-FAST and binary PAC errors"""
3380 check_eap_capa(dev[0], "FAST")
3381 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687 3382 hapd = hostapd.add_ap(apdev[0], params)
93aa1e16 3383
fab49f61
JM
3384 tests = [(1, "=eap_fast_save_pac_bin"),
3385 (1, "eap_fast_write_pac"),
3386 (2, "eap_fast_write_pac"),]
93aa1e16
JM
3387 for count, func in tests:
3388 if "OK" not in dev[0].request("SET blob fast_pac_bin_errors "):
3389 raise Exception("Could not set blob")
3390
3391 with alloc_fail(dev[0], count, func):
3b3e2687 3392 eap_connect(dev[0], hapd, "FAST", "user",
93aa1e16
JM
3393 anonymous_identity="FAST", password="password",
3394 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3395 phase1="fast_provisioning=1 fast_pac_format=binary",
3396 pac_file="blob://fast_pac_bin_errors")
3397 dev[0].request("REMOVE_NETWORK all")
3398 dev[0].wait_disconnected()
3399
fab49f61
JM
3400 tests = ["00", "000000000000", "6ae4920c0001",
3401 "6ae4920c000000",
3402 "6ae4920c0000" + "0000" + 32*"00" + "ffff" + "0000",
3403 "6ae4920c0000" + "0000" + 32*"00" + "0001" + "0000",
3404 "6ae4920c0000" + "0000" + 32*"00" + "0000" + "0001",
3405 "6ae4920c0000" + "0000" + 32*"00" + "0000" + "0008" + "00040000" + "0007000100"]
93aa1e16
JM
3406 for t in tests:
3407 if "OK" not in dev[0].request("SET blob fast_pac_bin_errors " + t):
3408 raise Exception("Could not set blob")
3409
3410 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="FAST",
3411 identity="user", anonymous_identity="FAST",
3412 password="password",
3413 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3414 phase1="fast_provisioning=1 fast_pac_format=binary",
3415 pac_file="blob://fast_pac_bin_errors",
3416 scan_freq="2412", wait_connect=False)
3417 ev = dev[0].wait_event(["EAP: Failed to initialize EAP method"],
3418 timeout=5)
3419 if ev is None:
3420 raise Exception("Failure not reported")
3421 dev[0].request("REMOVE_NETWORK all")
3422 dev[0].wait_disconnected()
3423
3424 pac = "6ae4920c0000" + "0000" + 32*"00" + "0000" + "0000"
fab49f61
JM
3425 tests = [(1, "eap_fast_load_pac_bin"),
3426 (2, "eap_fast_load_pac_bin"),
3427 (3, "eap_fast_load_pac_bin")]
93aa1e16
JM
3428 for count, func in tests:
3429 if "OK" not in dev[0].request("SET blob fast_pac_bin_errors " + pac):
3430 raise Exception("Could not set blob")
3431
3432 with alloc_fail(dev[0], count, func):
3433 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="FAST",
3434 identity="user", anonymous_identity="FAST",
3435 password="password",
3436 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3437 phase1="fast_provisioning=1 fast_pac_format=binary",
3438 pac_file="blob://fast_pac_bin_errors",
3439 scan_freq="2412", wait_connect=False)
3440 ev = dev[0].wait_event(["EAP: Failed to initialize EAP method"],
3441 timeout=5)
3442 if ev is None:
3443 raise Exception("Failure not reported")
3444 dev[0].request("REMOVE_NETWORK all")
3445 dev[0].wait_disconnected()
3446
3447 pac = "6ae4920c0000" + "0000" + 32*"00" + "0000" + "0005" + "0011223344"
3448 if "OK" not in dev[0].request("SET blob fast_pac_bin_errors " + pac):
3449 raise Exception("Could not set blob")
3450
3b3e2687 3451 eap_connect(dev[0], hapd, "FAST", "user",
93aa1e16
JM
3452 anonymous_identity="FAST", password="password",
3453 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3454 phase1="fast_provisioning=1 fast_pac_format=binary",
3455 pac_file="blob://fast_pac_bin_errors")
3456 dev[0].request("REMOVE_NETWORK all")
3457 dev[0].wait_disconnected()
3458
3459 pac = "6ae4920c0000" + "0000" + 32*"00" + "0000" + "0009" + "00040000" + "0007000100"
fab49f61
JM
3460 tests = [(1, "eap_fast_pac_get_a_id"),
3461 (2, "eap_fast_pac_get_a_id")]
93aa1e16
JM
3462 for count, func in tests:
3463 if "OK" not in dev[0].request("SET blob fast_pac_bin_errors " + pac):
3464 raise Exception("Could not set blob")
3465 with alloc_fail(dev[0], count, func):
3b3e2687 3466 eap_connect(dev[0], hapd, "FAST", "user",
93aa1e16
JM
3467 anonymous_identity="FAST", password="password",
3468 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3469 phase1="fast_provisioning=1 fast_pac_format=binary",
3470 pac_file="blob://fast_pac_bin_errors")
3471 dev[0].request("REMOVE_NETWORK all")
3472 dev[0].wait_disconnected()
3473
592790bf
JM
3474def test_ap_wpa2_eap_fast_text_pac_errors(dev, apdev):
3475 """EAP-FAST and text PAC errors"""
3476 check_eap_capa(dev[0], "FAST")
3477 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 3478 hostapd.add_ap(apdev[0], params)
592790bf 3479
fab49f61
JM
3480 tests = [(1, "eap_fast_parse_hex;eap_fast_parse_pac_key"),
3481 (1, "eap_fast_parse_hex;eap_fast_parse_pac_opaque"),
3482 (1, "eap_fast_parse_hex;eap_fast_parse_a_id"),
3483 (1, "eap_fast_parse_start"),
3484 (1, "eap_fast_save_pac")]
592790bf
JM
3485 for count, func in tests:
3486 dev[0].request("FLUSH")
3487 if "OK" not in dev[0].request("SET blob fast_pac_text_errors "):
3488 raise Exception("Could not set blob")
3489
3490 with alloc_fail(dev[0], count, func):
3491 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="FAST",
3492 identity="user", anonymous_identity="FAST",
3493 password="password",
3494 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3495 phase1="fast_provisioning=1",
3496 pac_file="blob://fast_pac_text_errors",
3497 scan_freq="2412", wait_connect=False)
3498 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
3499 dev[0].request("REMOVE_NETWORK all")
3500 dev[0].wait_disconnected()
3501
3502 pac = "wpa_supplicant EAP-FAST PAC file - version 1\n"
3503 pac += "START\n"
3504 pac += "PAC-Type\n"
3505 pac += "END\n"
54c58f29 3506 if "OK" not in dev[0].request("SET blob fast_pac_text_errors " + binascii.hexlify(pac.encode()).decode()):
592790bf
JM
3507 raise Exception("Could not set blob")
3508
3509 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="FAST",
3510 identity="user", anonymous_identity="FAST",
3511 password="password",
3512 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3513 phase1="fast_provisioning=1",
3514 pac_file="blob://fast_pac_text_errors",
3515 scan_freq="2412", wait_connect=False)
3516 ev = dev[0].wait_event(["EAP: Failed to initialize EAP method"], timeout=5)
3517 if ev is None:
3518 raise Exception("Failure not reported")
3519 dev[0].request("REMOVE_NETWORK all")
3520 dev[0].wait_disconnected()
3521
3522 dev[0].request("FLUSH")
3523 if "OK" not in dev[0].request("SET blob fast_pac_text_errors "):
3524 raise Exception("Could not set blob")
3525
3526 with alloc_fail(dev[0], 1, "eap_fast_add_pac_data"):
3527 for i in range(3):
3528 params = int_eap_server_params()
3529 params['ssid'] = "test-wpa2-eap-2"
3530 params['pac_opaque_encr_key'] = "000102030405060708090a0b0c0dff%02x" % i
3531 params['eap_fast_a_id'] = "101112131415161718191a1b1c1dff%02x" % i
3532 params['eap_fast_a_id_info'] = "test server %d" % i
3533
8b8a1864 3534 hapd2 = hostapd.add_ap(apdev[1], params)
592790bf
JM
3535
3536 dev[0].connect("test-wpa2-eap-2", key_mgmt="WPA-EAP", eap="FAST",
3537 identity="user", anonymous_identity="FAST",
3538 password="password",
3539 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3540 phase1="fast_provisioning=1",
3541 pac_file="blob://fast_pac_text_errors",
3542 scan_freq="2412", wait_connect=False)
3543 dev[0].wait_connected()
3544 dev[0].request("REMOVE_NETWORK all")
3545 dev[0].wait_disconnected()
3546
3547 hapd2.disable()
3548
3549def test_ap_wpa2_eap_fast_pac_truncate(dev, apdev):
3550 """EAP-FAST and PAC list truncation"""
3551 check_eap_capa(dev[0], "FAST")
3552 if "OK" not in dev[0].request("SET blob fast_pac_truncate "):
3553 raise Exception("Could not set blob")
3554 for i in range(5):
3555 params = int_eap_server_params()
3556 params['pac_opaque_encr_key'] = "000102030405060708090a0b0c0dff%02x" % i
3557 params['eap_fast_a_id'] = "101112131415161718191a1b1c1dff%02x" % i
3558 params['eap_fast_a_id_info'] = "test server %d" % i
8b8a1864 3559 hapd = hostapd.add_ap(apdev[0], params)
592790bf
JM
3560
3561 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="FAST",
3562 identity="user", anonymous_identity="FAST",
3563 password="password",
3564 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3565 phase1="fast_provisioning=1 fast_max_pac_list_len=2",
3566 pac_file="blob://fast_pac_truncate",
3567 scan_freq="2412", wait_connect=False)
3568 dev[0].wait_connected()
3569 dev[0].request("REMOVE_NETWORK all")
3570 dev[0].wait_disconnected()
3571
3572 hapd.disable()
3573
3574def test_ap_wpa2_eap_fast_pac_refresh(dev, apdev):
3575 """EAP-FAST and PAC refresh"""
3576 check_eap_capa(dev[0], "FAST")
3577 if "OK" not in dev[0].request("SET blob fast_pac_refresh "):
3578 raise Exception("Could not set blob")
3579 for i in range(2):
3580 params = int_eap_server_params()
3581 params['pac_opaque_encr_key'] = "000102030405060708090a0b0c0dff%02x" % i
3582 params['eap_fast_a_id'] = "101112131415161718191a1b1c1dff%02x" % i
3583 params['eap_fast_a_id_info'] = "test server %d" % i
3584 params['pac_key_refresh_time'] = "1"
3585 params['pac_key_lifetime'] = "10"
8b8a1864 3586 hapd = hostapd.add_ap(apdev[0], params)
592790bf
JM
3587
3588 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="FAST",
3589 identity="user", anonymous_identity="FAST",
3590 password="password",
3591 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3592 phase1="fast_provisioning=1",
3593 pac_file="blob://fast_pac_refresh",
3594 scan_freq="2412", wait_connect=False)
3595 dev[0].wait_connected()
3596 dev[0].request("REMOVE_NETWORK all")
3597 dev[0].wait_disconnected()
3598
3599 hapd.disable()
3600
3601 for i in range(2):
3602 params = int_eap_server_params()
3603 params['pac_opaque_encr_key'] = "000102030405060708090a0b0c0dff%02x" % i
3604 params['eap_fast_a_id'] = "101112131415161718191a1b1c1dff%02x" % i
3605 params['eap_fast_a_id_info'] = "test server %d" % i
3606 params['pac_key_refresh_time'] = "10"
3607 params['pac_key_lifetime'] = "10"
8b8a1864 3608 hapd = hostapd.add_ap(apdev[0], params)
592790bf
JM
3609
3610 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="FAST",
3611 identity="user", anonymous_identity="FAST",
3612 password="password",
3613 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3614 phase1="fast_provisioning=1",
3615 pac_file="blob://fast_pac_refresh",
3616 scan_freq="2412", wait_connect=False)
3617 dev[0].wait_connected()
3618 dev[0].request("REMOVE_NETWORK all")
3619 dev[0].wait_disconnected()
3620
3621 hapd.disable()
3622
3623def test_ap_wpa2_eap_fast_pac_lifetime(dev, apdev):
3624 """EAP-FAST and PAC lifetime"""
3625 check_eap_capa(dev[0], "FAST")
3626 if "OK" not in dev[0].request("SET blob fast_pac_refresh "):
3627 raise Exception("Could not set blob")
3628
3629 i = 0
3630 params = int_eap_server_params()
3631 params['pac_opaque_encr_key'] = "000102030405060708090a0b0c0dff%02x" % i
3632 params['eap_fast_a_id'] = "101112131415161718191a1b1c1dff%02x" % i
3633 params['eap_fast_a_id_info'] = "test server %d" % i
3634 params['pac_key_refresh_time'] = "0"
3635 params['pac_key_lifetime'] = "2"
8b8a1864 3636 hapd = hostapd.add_ap(apdev[0], params)
592790bf
JM
3637
3638 id = dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="FAST",
3639 identity="user", anonymous_identity="FAST",
3640 password="password",
3641 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3642 phase1="fast_provisioning=2",
3643 pac_file="blob://fast_pac_refresh",
3644 scan_freq="2412", wait_connect=False)
3645 dev[0].wait_connected()
3646 dev[0].request("DISCONNECT")
3647 dev[0].wait_disconnected()
3648
3649 time.sleep(3)
3650 dev[0].request("PMKSA_FLUSH")
3651 dev[0].request("RECONNECT")
3652 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
3653 if ev is None:
3654 raise Exception("No EAP-Failure seen after expired PAC")
3655 dev[0].request("DISCONNECT")
3656 dev[0].wait_disconnected()
3657
3658 dev[0].select_network(id)
3659 dev[0].wait_connected()
3660 dev[0].request("REMOVE_NETWORK all")
3661 dev[0].wait_disconnected()
3662
53a6f06a
JM
3663def test_ap_wpa2_eap_fast_gtc_auth_prov(dev, apdev):
3664 """WPA2-Enterprise connection using EAP-FAST/GTC and authenticated provisioning"""
3b51cc63 3665 check_eap_capa(dev[0], "FAST")
53a6f06a 3666 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 3667 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 3668 eap_connect(dev[0], hapd, "FAST", "user",
53a6f06a
JM
3669 anonymous_identity="FAST", password="password",
3670 ca_cert="auth_serv/ca.pem", phase2="auth=GTC",
3671 phase1="fast_provisioning=2", pac_file="blob://fast_pac_auth")
a8375c94 3672 hwsim_utils.test_connectivity(dev[0], hapd)
2fc4749c
JM
3673 res = eap_reauth(dev[0], "FAST")
3674 if res['tls_session_reused'] != '1':
3675 raise Exception("EAP-FAST could not use PAC session ticket")
d4c7a2b9 3676
95a15d79
JM
3677def test_ap_wpa2_eap_fast_gtc_identity_change(dev, apdev):
3678 """WPA2-Enterprise connection using EAP-FAST/GTC and identity changing"""
3679 check_eap_capa(dev[0], "FAST")
3680 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 3681 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 3682 id = eap_connect(dev[0], hapd, "FAST", "user",
95a15d79
JM
3683 anonymous_identity="FAST", password="password",
3684 ca_cert="auth_serv/ca.pem", phase2="auth=GTC",
3685 phase1="fast_provisioning=2",
3686 pac_file="blob://fast_pac_auth")
3687 dev[0].set_network_quoted(id, "identity", "user2")
3688 dev[0].wait_disconnected()
3689 ev = dev[0].wait_event(["CTRL-EVENT-EAP-METHOD"], timeout=15)
3690 if ev is None:
3691 raise Exception("EAP-FAST not started")
3692 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
3693 if ev is None:
3694 raise Exception("EAP failure not reported")
3695 dev[0].wait_disconnected()
3696
27f2fab0
JM
3697def test_ap_wpa2_eap_fast_prf_oom(dev, apdev):
3698 """WPA2-Enterprise connection using EAP-FAST and OOM in PRF"""
3699 check_eap_capa(dev[0], "FAST")
cc71035f
JM
3700 tls = dev[0].request("GET tls_library")
3701 if tls.startswith("OpenSSL"):
90b4c73f 3702 func = "tls_connection_get_eap_fast_key"
cc71035f
JM
3703 count = 2
3704 elif tls.startswith("internal"):
3705 func = "tls_connection_prf"
3706 count = 1
3707 else:
3708 raise HwsimSkip("Unsupported TLS library")
27f2fab0 3709 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 3710 hapd = hostapd.add_ap(apdev[0], params)
cc71035f 3711 with alloc_fail(dev[0], count, func):
27f2fab0
JM
3712 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="FAST",
3713 identity="user", anonymous_identity="FAST",
3714 password="password", ca_cert="auth_serv/ca.pem",
3715 phase2="auth=GTC",
3716 phase1="fast_provisioning=2",
3717 pac_file="blob://fast_pac_auth",
3718 wait_connect=False, scan_freq="2412")
3719 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=15)
3720 if ev is None:
3721 raise Exception("EAP failure not reported")
3722 dev[0].request("DISCONNECT")
3723
6eddd530
JM
3724def test_ap_wpa2_eap_fast_server_oom(dev, apdev):
3725 """EAP-FAST/MSCHAPv2 and server OOM"""
3726 check_eap_capa(dev[0], "FAST")
3727
3728 params = int_eap_server_params()
3729 params['dh_file'] = 'auth_serv/dh.conf'
3730 params['pac_opaque_encr_key'] = '000102030405060708090a0b0c0d0e0f'
3731 params['eap_fast_a_id'] = '1011'
3732 params['eap_fast_a_id_info'] = 'another test server'
8b8a1864 3733 hapd = hostapd.add_ap(apdev[0], params)
6eddd530
JM
3734
3735 with alloc_fail(hapd, 1, "tls_session_ticket_ext_cb"):
3b3e2687 3736 id = eap_connect(dev[0], hapd, "FAST", "user",
6eddd530
JM
3737 anonymous_identity="FAST", password="password",
3738 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3739 phase1="fast_provisioning=1",
3740 pac_file="blob://fast_pac",
3741 expect_failure=True)
3742 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
3743 if ev is None:
3744 raise Exception("No EAP failure reported")
3745 dev[0].wait_disconnected()
3746 dev[0].request("DISCONNECT")
3747
3748 dev[0].select_network(id, freq="2412")
3749
ecd07de4
JM
3750def test_ap_wpa2_eap_fast_cipher_suites(dev, apdev):
3751 """EAP-FAST and different TLS cipher suites"""
3752 check_eap_capa(dev[0], "FAST")
3753 tls = dev[0].request("GET tls_library")
d8003dcb
SP
3754 if not tls.startswith("OpenSSL") and not tls.startswith("wolfSSL"):
3755 raise HwsimSkip("TLS library is not OpenSSL or wolfSSL: " + tls)
ecd07de4
JM
3756
3757 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 3758 hapd = hostapd.add_ap(apdev[0], params)
ecd07de4
JM
3759
3760 dev[0].request("SET blob fast_pac_ciphers ")
3b3e2687 3761 eap_connect(dev[0], hapd, "FAST", "user",
ecd07de4
JM
3762 anonymous_identity="FAST", password="password",
3763 ca_cert="auth_serv/ca.pem", phase2="auth=GTC",
3764 phase1="fast_provisioning=2",
3765 pac_file="blob://fast_pac_ciphers")
3766 res = dev[0].get_status_field('EAP TLS cipher')
3767 dev[0].request("REMOVE_NETWORK all")
3768 dev[0].wait_disconnected()
3769 if res != "DHE-RSA-AES256-SHA":
3770 raise Exception("Unexpected cipher suite for provisioning: " + res)
3771
fab49f61
JM
3772 tests = ["DHE-RSA-AES128-SHA",
3773 "RC4-SHA",
3774 "AES128-SHA",
3775 "AES256-SHA",
3776 "DHE-RSA-AES256-SHA"]
ecd07de4 3777 for cipher in tests:
71666dc3
JM
3778 dev[0].dump_monitor()
3779 logger.info("Testing " + cipher)
3780 try:
3b3e2687 3781 eap_connect(dev[0], hapd, "FAST", "user",
71666dc3
JM
3782 openssl_ciphers=cipher,
3783 anonymous_identity="FAST", password="password",
3784 ca_cert="auth_serv/ca.pem", phase2="auth=GTC",
a61ee84d
JM
3785 pac_file="blob://fast_pac_ciphers",
3786 report_failure=True)
bab493b9 3787 except Exception as e:
a61ee84d
JM
3788 if cipher == "RC4-SHA" and \
3789 ("Could not select EAP method" in str(e) or \
3790 "EAP failed" in str(e)):
71666dc3
JM
3791 if "run=OpenSSL 1.1" in tls:
3792 logger.info("Allow failure due to missing TLS library support")
3793 dev[0].request("REMOVE_NETWORK all")
3794 dev[0].wait_disconnected()
3795 continue
3796 raise
ecd07de4
JM
3797 res = dev[0].get_status_field('EAP TLS cipher')
3798 dev[0].request("REMOVE_NETWORK all")
3799 dev[0].wait_disconnected()
3800 if res != cipher:
3801 raise Exception("Unexpected TLS cipher info (configured %s): %s" % (cipher, res))
3802
4c626382
JM
3803def test_ap_wpa2_eap_fast_prov(dev, apdev):
3804 """EAP-FAST and provisioning options"""
3805 check_eap_capa(dev[0], "FAST")
3806 if "OK" not in dev[0].request("SET blob fast_pac_prov "):
3807 raise Exception("Could not set blob")
3808
3809 i = 100
3810 params = int_eap_server_params()
3811 params['disable_pmksa_caching'] = '1'
3812 params['pac_opaque_encr_key'] = "000102030405060708090a0b0c0dff%02x" % i
3813 params['eap_fast_a_id'] = "101112131415161718191a1b1c1dff%02x" % i
3814 params['eap_fast_a_id_info'] = "test server %d" % i
3815 params['eap_fast_prov'] = "0"
3816 hapd = hostapd.add_ap(apdev[0], params)
3817
3818 logger.info("Provisioning attempt while server has provisioning disabled")
3819 id = dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="FAST",
3820 identity="user", anonymous_identity="FAST",
3821 password="password",
3822 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3823 phase1="fast_provisioning=2",
3824 pac_file="blob://fast_pac_prov",
3825 scan_freq="2412", wait_connect=False)
3826 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS status='completion'"],
3827 timeout=15)
3828 if ev is None:
3829 raise Exception("EAP result not reported")
3830 if "parameter='failure'" not in ev:
3831 raise Exception("Unexpected EAP result: " + ev)
3832 dev[0].wait_disconnected()
3833 dev[0].request("DISCONNECT")
3834 dev[0].dump_monitor()
3835
3836 hapd.disable()
3837 logger.info("Authenticated provisioning")
3838 hapd.set("eap_fast_prov", "2")
3839 hapd.enable()
3840
3841 dev[0].select_network(id, freq="2412")
3842 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS status='completion'"],
3843 timeout=15)
3844 if ev is None:
3845 raise Exception("EAP result not reported")
3846 if "parameter='success'" not in ev:
3847 raise Exception("Unexpected EAP result: " + ev)
3848 dev[0].wait_connected()
3849 dev[0].request("DISCONNECT")
3850 dev[0].wait_disconnected()
3851 dev[0].dump_monitor()
3852
3853 hapd.disable()
3854 logger.info("Provisioning disabled - using previously provisioned PAC")
3855 hapd.set("eap_fast_prov", "0")
3856 hapd.enable()
3857
3858 dev[0].select_network(id, freq="2412")
3859 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS status='completion'"],
3860 timeout=15)
3861 if ev is None:
3862 raise Exception("EAP result not reported")
3863 if "parameter='success'" not in ev:
3864 raise Exception("Unexpected EAP result: " + ev)
3865 dev[0].wait_connected()
3866 dev[0].request("DISCONNECT")
3867 dev[0].wait_disconnected()
3868 dev[0].dump_monitor()
3869
3870 logger.info("Drop PAC and verify connection failure")
3871 if "OK" not in dev[0].request("SET blob fast_pac_prov "):
3872 raise Exception("Could not set blob")
3873
3874 dev[0].select_network(id, freq="2412")
3875 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS status='completion'"],
3876 timeout=15)
3877 if ev is None:
3878 raise Exception("EAP result not reported")
3879 if "parameter='failure'" not in ev:
3880 raise Exception("Unexpected EAP result: " + ev)
3881 dev[0].wait_disconnected()
3882 dev[0].request("DISCONNECT")
3883 dev[0].dump_monitor()
3884
3885 hapd.disable()
3886 logger.info("Anonymous provisioning")
3887 hapd.set("eap_fast_prov", "1")
3888 hapd.enable()
3889 dev[0].set_network_quoted(id, "phase1", "fast_provisioning=1")
3890 dev[0].select_network(id, freq="2412")
3891 # Anonymous provisioning results in EAP-Failure first
3892 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS status='completion'"],
3893 timeout=15)
3894 if ev is None:
3895 raise Exception("EAP result not reported")
3896 if "parameter='failure'" not in ev:
3897 raise Exception("Unexpected EAP result: " + ev)
3898 dev[0].wait_disconnected()
3899 # And then the actual data connection
3900 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS status='completion'"],
3901 timeout=15)
3902 if ev is None:
3903 raise Exception("EAP result not reported")
3904 if "parameter='success'" not in ev:
3905 raise Exception("Unexpected EAP result: " + ev)
3906 dev[0].wait_connected()
3907 dev[0].request("DISCONNECT")
3908 dev[0].wait_disconnected()
3909 dev[0].dump_monitor()
3910
3911 hapd.disable()
3912 logger.info("Provisioning disabled - using previously provisioned PAC")
3913 hapd.set("eap_fast_prov", "0")
3914 hapd.enable()
3915
3916 dev[0].select_network(id, freq="2412")
3917 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS status='completion'"],
3918 timeout=15)
3919 if ev is None:
3920 raise Exception("EAP result not reported")
3921 if "parameter='success'" not in ev:
3922 raise Exception("Unexpected EAP result: " + ev)
3923 dev[0].wait_connected()
3924 dev[0].request("DISCONNECT")
3925 dev[0].wait_disconnected()
3926 dev[0].dump_monitor()
3927
d4c7a2b9
JM
3928def test_ap_wpa2_eap_tls_ocsp(dev, apdev):
3929 """WPA2-Enterprise connection using EAP-TLS and verifying OCSP"""
0dae8c99 3930 check_ocsp_support(dev[0])
16c43d2a 3931 check_pkcs12_support(dev[0])
d4c7a2b9 3932 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
3933 hapd = hostapd.add_ap(apdev[0], params)
3934 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
d4c7a2b9
JM
3935 private_key="auth_serv/user.pkcs12",
3936 private_key_passwd="whatever", ocsp=2)
3937
98d125ca
JM
3938def test_ap_wpa2_eap_tls_ocsp_multi(dev, apdev):
3939 """WPA2-Enterprise connection using EAP-TLS and verifying OCSP-multi"""
3940 check_ocsp_multi_support(dev[0])
3941 check_pkcs12_support(dev[0])
3942
3943 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
3944 hapd = hostapd.add_ap(apdev[0], params)
3945 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
98d125ca
JM
3946 private_key="auth_serv/user.pkcs12",
3947 private_key_passwd="whatever", ocsp=2)
3948
64e05f96 3949def int_eap_server_params():
fab49f61
JM
3950 params = {"ssid": "test-wpa2-eap", "wpa": "2", "wpa_key_mgmt": "WPA-EAP",
3951 "rsn_pairwise": "CCMP", "ieee8021x": "1",
3952 "eap_server": "1", "eap_user_file": "auth_serv/eap_user.conf",
3953 "ca_cert": "auth_serv/ca.pem",
3954 "server_cert": "auth_serv/server.pem",
3955 "private_key": "auth_serv/server.key",
3956 "dh_file": "auth_serv/dh.conf"}
64e05f96 3957 return params
d2a1047e 3958
58a40620
JM
3959def test_ap_wpa2_eap_tls_ocsp_key_id(dev, apdev, params):
3960 """EAP-TLS and OCSP certificate signed OCSP response using key ID"""
3961 check_ocsp_support(dev[0])
ff7affcc 3962 check_pkcs12_support(dev[0])
58a40620
JM
3963 ocsp = os.path.join(params['logdir'], "ocsp-server-cache-key-id.der")
3964 if not os.path.exists(ocsp):
3965 raise HwsimSkip("No OCSP response available")
3966 params = int_eap_server_params()
3967 params["ocsp_stapling_response"] = ocsp
8b8a1864 3968 hostapd.add_ap(apdev[0], params)
58a40620
JM
3969 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
3970 identity="tls user", ca_cert="auth_serv/ca.pem",
3971 private_key="auth_serv/user.pkcs12",
3972 private_key_passwd="whatever", ocsp=2,
3973 scan_freq="2412")
3974
d79ce4a6
JM
3975def test_ap_wpa2_eap_tls_ocsp_ca_signed_good(dev, apdev, params):
3976 """EAP-TLS and CA signed OCSP response (good)"""
3977 check_ocsp_support(dev[0])
ff7affcc 3978 check_pkcs12_support(dev[0])
d79ce4a6
JM
3979 ocsp = os.path.join(params['logdir'], "ocsp-resp-ca-signed.der")
3980 if not os.path.exists(ocsp):
3981 raise HwsimSkip("No OCSP response available")
3982 params = int_eap_server_params()
3983 params["ocsp_stapling_response"] = ocsp
8b8a1864 3984 hostapd.add_ap(apdev[0], params)
d79ce4a6
JM
3985 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
3986 identity="tls user", ca_cert="auth_serv/ca.pem",
3987 private_key="auth_serv/user.pkcs12",
3988 private_key_passwd="whatever", ocsp=2,
3989 scan_freq="2412")
3990
3991def test_ap_wpa2_eap_tls_ocsp_ca_signed_revoked(dev, apdev, params):
3992 """EAP-TLS and CA signed OCSP response (revoked)"""
3993 check_ocsp_support(dev[0])
ff7affcc 3994 check_pkcs12_support(dev[0])
d79ce4a6
JM
3995 ocsp = os.path.join(params['logdir'], "ocsp-resp-ca-signed-revoked.der")
3996 if not os.path.exists(ocsp):
3997 raise HwsimSkip("No OCSP response available")
3998 params = int_eap_server_params()
3999 params["ocsp_stapling_response"] = ocsp
8b8a1864 4000 hostapd.add_ap(apdev[0], params)
d79ce4a6
JM
4001 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4002 identity="tls user", ca_cert="auth_serv/ca.pem",
4003 private_key="auth_serv/user.pkcs12",
4004 private_key_passwd="whatever", ocsp=2,
4005 wait_connect=False, scan_freq="2412")
4006 count = 0
4007 while True:
4008 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS"])
4009 if ev is None:
4010 raise Exception("Timeout on EAP status")
4011 if 'bad certificate status response' in ev:
4012 break
4013 if 'certificate revoked' in ev:
4014 break
4015 count = count + 1
4016 if count > 10:
4017 raise Exception("Unexpected number of EAP status messages")
4018
4019 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
4020 if ev is None:
4021 raise Exception("Timeout on EAP failure report")
4022
4023def test_ap_wpa2_eap_tls_ocsp_ca_signed_unknown(dev, apdev, params):
4024 """EAP-TLS and CA signed OCSP response (unknown)"""
4025 check_ocsp_support(dev[0])
ff7affcc 4026 check_pkcs12_support(dev[0])
d79ce4a6
JM
4027 ocsp = os.path.join(params['logdir'], "ocsp-resp-ca-signed-unknown.der")
4028 if not os.path.exists(ocsp):
4029 raise HwsimSkip("No OCSP response available")
4030 params = int_eap_server_params()
4031 params["ocsp_stapling_response"] = ocsp
8b8a1864 4032 hostapd.add_ap(apdev[0], params)
d79ce4a6
JM
4033 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4034 identity="tls user", ca_cert="auth_serv/ca.pem",
4035 private_key="auth_serv/user.pkcs12",
4036 private_key_passwd="whatever", ocsp=2,
4037 wait_connect=False, scan_freq="2412")
4038 count = 0
4039 while True:
4040 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS"])
4041 if ev is None:
4042 raise Exception("Timeout on EAP status")
4043 if 'bad certificate status response' in ev:
4044 break
4045 count = count + 1
4046 if count > 10:
4047 raise Exception("Unexpected number of EAP status messages")
4048
4049 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
4050 if ev is None:
4051 raise Exception("Timeout on EAP failure report")
4052
4053def test_ap_wpa2_eap_tls_ocsp_server_signed(dev, apdev, params):
4054 """EAP-TLS and server signed OCSP response"""
4055 check_ocsp_support(dev[0])
ff7affcc 4056 check_pkcs12_support(dev[0])
d79ce4a6
JM
4057 ocsp = os.path.join(params['logdir'], "ocsp-resp-server-signed.der")
4058 if not os.path.exists(ocsp):
4059 raise HwsimSkip("No OCSP response available")
4060 params = int_eap_server_params()
4061 params["ocsp_stapling_response"] = ocsp
8b8a1864 4062 hostapd.add_ap(apdev[0], params)
d79ce4a6
JM
4063 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4064 identity="tls user", ca_cert="auth_serv/ca.pem",
4065 private_key="auth_serv/user.pkcs12",
4066 private_key_passwd="whatever", ocsp=2,
4067 wait_connect=False, scan_freq="2412")
4068 count = 0
4069 while True:
4070 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS"])
4071 if ev is None:
4072 raise Exception("Timeout on EAP status")
4073 if 'bad certificate status response' in ev:
4074 break
4075 count = count + 1
4076 if count > 10:
4077 raise Exception("Unexpected number of EAP status messages")
4078
4079 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
4080 if ev is None:
4081 raise Exception("Timeout on EAP failure report")
4082
d2a1047e
JM
4083def test_ap_wpa2_eap_tls_ocsp_invalid_data(dev, apdev):
4084 """WPA2-Enterprise connection using EAP-TLS and invalid OCSP data"""
0dae8c99 4085 check_ocsp_support(dev[0])
ff7affcc 4086 check_pkcs12_support(dev[0])
d2a1047e
JM
4087 params = int_eap_server_params()
4088 params["ocsp_stapling_response"] = "auth_serv/ocsp-req.der"
8b8a1864 4089 hostapd.add_ap(apdev[0], params)
d2a1047e
JM
4090 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4091 identity="tls user", ca_cert="auth_serv/ca.pem",
4092 private_key="auth_serv/user.pkcs12",
4093 private_key_passwd="whatever", ocsp=2,
4094 wait_connect=False, scan_freq="2412")
4095 count = 0
4096 while True:
4097 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS"])
4098 if ev is None:
4099 raise Exception("Timeout on EAP status")
4100 if 'bad certificate status response' in ev:
4101 break
4102 count = count + 1
4103 if count > 10:
4104 raise Exception("Unexpected number of EAP status messages")
4105
4106 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
4107 if ev is None:
4108 raise Exception("Timeout on EAP failure report")
4109
64e05f96
JM
4110def test_ap_wpa2_eap_tls_ocsp_invalid(dev, apdev):
4111 """WPA2-Enterprise connection using EAP-TLS and invalid OCSP response"""
0dae8c99 4112 check_ocsp_support(dev[0])
ff7affcc 4113 check_pkcs12_support(dev[0])
64e05f96
JM
4114 params = int_eap_server_params()
4115 params["ocsp_stapling_response"] = "auth_serv/ocsp-server-cache.der-invalid"
8b8a1864 4116 hostapd.add_ap(apdev[0], params)
df7ad0fa
JM
4117 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4118 identity="tls user", ca_cert="auth_serv/ca.pem",
4119 private_key="auth_serv/user.pkcs12",
4120 private_key_passwd="whatever", ocsp=2,
4121 wait_connect=False, scan_freq="2412")
4122 count = 0
4123 while True:
4124 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS"])
4125 if ev is None:
4126 raise Exception("Timeout on EAP status")
4127 if 'bad certificate status response' in ev:
4128 break
4129 count = count + 1
4130 if count > 10:
4131 raise Exception("Unexpected number of EAP status messages")
4132
4133 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
4134 if ev is None:
4135 raise Exception("Timeout on EAP failure report")
4136
4137def test_ap_wpa2_eap_tls_ocsp_unknown_sign(dev, apdev):
4138 """WPA2-Enterprise connection using EAP-TLS and unknown OCSP signer"""
0dae8c99 4139 check_ocsp_support(dev[0])
ff7affcc 4140 check_pkcs12_support(dev[0])
df7ad0fa
JM
4141 params = int_eap_server_params()
4142 params["ocsp_stapling_response"] = "auth_serv/ocsp-server-cache.der-unknown-sign"
8b8a1864 4143 hostapd.add_ap(apdev[0], params)
d4c7a2b9
JM
4144 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4145 identity="tls user", ca_cert="auth_serv/ca.pem",
4146 private_key="auth_serv/user.pkcs12",
4147 private_key_passwd="whatever", ocsp=2,
4148 wait_connect=False, scan_freq="2412")
4149 count = 0
4150 while True:
4151 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS"])
4152 if ev is None:
4153 raise Exception("Timeout on EAP status")
4154 if 'bad certificate status response' in ev:
4155 break
4156 count = count + 1
4157 if count > 10:
4158 raise Exception("Unexpected number of EAP status messages")
4159
4160 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
4161 if ev is None:
4162 raise Exception("Timeout on EAP failure report")
64e05f96 4163
37b4a66c
JM
4164def test_ap_wpa2_eap_ttls_ocsp_revoked(dev, apdev, params):
4165 """WPA2-Enterprise connection using EAP-TTLS and OCSP status revoked"""
0dae8c99 4166 check_ocsp_support(dev[0])
37b4a66c
JM
4167 ocsp = os.path.join(params['logdir'], "ocsp-server-cache-revoked.der")
4168 if not os.path.exists(ocsp):
4169 raise HwsimSkip("No OCSP response available")
4170 params = int_eap_server_params()
4171 params["ocsp_stapling_response"] = ocsp
8b8a1864 4172 hostapd.add_ap(apdev[0], params)
37b4a66c
JM
4173 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
4174 identity="pap user", ca_cert="auth_serv/ca.pem",
4175 anonymous_identity="ttls", password="password",
4176 phase2="auth=PAP", ocsp=2,
4177 wait_connect=False, scan_freq="2412")
4178 count = 0
4179 while True:
4180 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS"])
4181 if ev is None:
4182 raise Exception("Timeout on EAP status")
4183 if 'bad certificate status response' in ev:
4184 break
4185 if 'certificate revoked' in ev:
4186 break
4187 count = count + 1
4188 if count > 10:
4189 raise Exception("Unexpected number of EAP status messages")
4190
4191 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
4192 if ev is None:
4193 raise Exception("Timeout on EAP failure report")
4194
4195def test_ap_wpa2_eap_ttls_ocsp_unknown(dev, apdev, params):
4196 """WPA2-Enterprise connection using EAP-TTLS and OCSP status revoked"""
0dae8c99 4197 check_ocsp_support(dev[0])
37b4a66c
JM
4198 ocsp = os.path.join(params['logdir'], "ocsp-server-cache-unknown.der")
4199 if not os.path.exists(ocsp):
4200 raise HwsimSkip("No OCSP response available")
4201 params = int_eap_server_params()
4202 params["ocsp_stapling_response"] = ocsp
8b8a1864 4203 hostapd.add_ap(apdev[0], params)
37b4a66c
JM
4204 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
4205 identity="pap user", ca_cert="auth_serv/ca.pem",
4206 anonymous_identity="ttls", password="password",
4207 phase2="auth=PAP", ocsp=2,
4208 wait_connect=False, scan_freq="2412")
4209 count = 0
4210 while True:
4211 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS"])
4212 if ev is None:
4213 raise Exception("Timeout on EAP status")
4214 if 'bad certificate status response' in ev:
4215 break
4216 count = count + 1
4217 if count > 10:
4218 raise Exception("Unexpected number of EAP status messages")
4219
4220 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
4221 if ev is None:
4222 raise Exception("Timeout on EAP failure report")
4223
4224def test_ap_wpa2_eap_ttls_optional_ocsp_unknown(dev, apdev, params):
4225 """WPA2-Enterprise connection using EAP-TTLS and OCSP status revoked"""
585e728a 4226 check_ocsp_support(dev[0])
37b4a66c
JM
4227 ocsp = os.path.join(params['logdir'], "ocsp-server-cache-unknown.der")
4228 if not os.path.exists(ocsp):
4229 raise HwsimSkip("No OCSP response available")
4230 params = int_eap_server_params()
4231 params["ocsp_stapling_response"] = ocsp
8b8a1864 4232 hostapd.add_ap(apdev[0], params)
37b4a66c
JM
4233 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
4234 identity="pap user", ca_cert="auth_serv/ca.pem",
4235 anonymous_identity="ttls", password="password",
4236 phase2="auth=PAP", ocsp=1, scan_freq="2412")
4237
52811b8c
JM
4238def test_ap_wpa2_eap_tls_intermediate_ca(dev, apdev, params):
4239 """EAP-TLS with intermediate server/user CA"""
4240 params = int_eap_server_params()
4241 params["ca_cert"] = "auth_serv/iCA-server/ca-and-root.pem"
4242 params["server_cert"] = "auth_serv/iCA-server/server.pem"
4243 params["private_key"] = "auth_serv/iCA-server/server.key"
8b8a1864 4244 hostapd.add_ap(apdev[0], params)
b4635f0a 4245 tls = dev[0].request("GET tls_library")
f08362e9 4246 if "GnuTLS" in tls or "wolfSSL" in tls:
b4635f0a
JM
4247 ca_cert = "auth_serv/iCA-user/ca-and-root.pem"
4248 client_cert = "auth_serv/iCA-user/user_and_ica.pem"
4249 else:
4250 ca_cert = "auth_serv/iCA-user/ca-and-root.pem"
4251 client_cert = "auth_serv/iCA-user/user.pem"
52811b8c
JM
4252 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4253 identity="tls user",
b4635f0a
JM
4254 ca_cert=ca_cert,
4255 client_cert=client_cert,
52811b8c
JM
4256 private_key="auth_serv/iCA-user/user.key",
4257 scan_freq="2412")
4258
4259def root_ocsp(cert):
4260 ca = "auth_serv/ca.pem"
4261
4262 fd2, fn2 = tempfile.mkstemp()
4263 os.close(fd2)
4264
fab49f61
JM
4265 arg = ["openssl", "ocsp", "-reqout", fn2, "-issuer", ca, "-sha256",
4266 "-cert", cert, "-no_nonce", "-text"]
d40d959e 4267 logger.info(' '.join(arg))
52811b8c
JM
4268 cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
4269 stderr=subprocess.PIPE)
06faf9e4 4270 res = cmd.stdout.read().decode() + "\n" + cmd.stderr.read().decode()
52811b8c
JM
4271 cmd.stdout.close()
4272 cmd.stderr.close()
d40d959e
JB
4273 cmd.wait()
4274 if cmd.returncode != 0:
4275 raise Exception("bad return code from openssl ocsp\n\n" + res)
52811b8c
JM
4276 logger.info("OCSP request:\n" + res)
4277
4278 fd, fn = tempfile.mkstemp()
4279 os.close(fd)
fab49f61
JM
4280 arg = ["openssl", "ocsp", "-index", "auth_serv/rootCA/index.txt",
4281 "-rsigner", ca, "-rkey", "auth_serv/ca-key.pem",
4282 "-CA", ca, "-issuer", ca, "-verify_other", ca, "-trust_other",
4283 "-ndays", "7", "-reqin", fn2, "-resp_no_certs", "-respout", fn,
4284 "-text"]
52811b8c
JM
4285 cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
4286 stderr=subprocess.PIPE)
06faf9e4 4287 res = cmd.stdout.read().decode() + "\n" + cmd.stderr.read().decode()
52811b8c
JM
4288 cmd.stdout.close()
4289 cmd.stderr.close()
d40d959e
JB
4290 cmd.wait()
4291 if cmd.returncode != 0:
4292 raise Exception("bad return code from openssl ocsp\n\n" + res)
52811b8c
JM
4293 logger.info("OCSP response:\n" + res)
4294 os.unlink(fn2)
4295 return fn
4296
b7288e5d 4297def ica_ocsp(cert, md="-sha256"):
52811b8c
JM
4298 prefix = "auth_serv/iCA-server/"
4299 ca = prefix + "cacert.pem"
4300 cert = prefix + cert
4301
4302 fd2, fn2 = tempfile.mkstemp()
4303 os.close(fd2)
4304
fab49f61
JM
4305 arg = ["openssl", "ocsp", "-reqout", fn2, "-issuer", ca, md,
4306 "-cert", cert, "-no_nonce", "-text"]
52811b8c
JM
4307 cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
4308 stderr=subprocess.PIPE)
04fa9fc7 4309 res = cmd.stdout.read().decode() + "\n" + cmd.stderr.read().decode()
52811b8c
JM
4310 cmd.stdout.close()
4311 cmd.stderr.close()
d40d959e
JB
4312 cmd.wait()
4313 if cmd.returncode != 0:
4314 raise Exception("bad return code from openssl ocsp\n\n" + res)
52811b8c
JM
4315 logger.info("OCSP request:\n" + res)
4316
4317 fd, fn = tempfile.mkstemp()
4318 os.close(fd)
fab49f61
JM
4319 arg = ["openssl", "ocsp", "-index", prefix + "index.txt",
4320 "-rsigner", ca, "-rkey", prefix + "private/cakey.pem",
4321 "-CA", ca, "-issuer", ca, "-verify_other", ca, "-trust_other",
4322 "-ndays", "7", "-reqin", fn2, "-resp_no_certs", "-respout", fn,
4323 "-text"]
52811b8c
JM
4324 cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
4325 stderr=subprocess.PIPE)
04fa9fc7 4326 res = cmd.stdout.read().decode() + "\n" + cmd.stderr.read().decode()
52811b8c
JM
4327 cmd.stdout.close()
4328 cmd.stderr.close()
d40d959e
JB
4329 cmd.wait()
4330 if cmd.returncode != 0:
4331 raise Exception("bad return code from openssl ocsp\n\n" + res)
52811b8c
JM
4332 logger.info("OCSP response:\n" + res)
4333 os.unlink(fn2)
4334 return fn
4335
4336def test_ap_wpa2_eap_tls_intermediate_ca_ocsp(dev, apdev, params):
4337 """EAP-TLS with intermediate server/user CA and OCSP on server certificate"""
b7288e5d
JM
4338 run_ap_wpa2_eap_tls_intermediate_ca_ocsp(dev, apdev, params, "-sha256")
4339
4340def test_ap_wpa2_eap_tls_intermediate_ca_ocsp_sha1(dev, apdev, params):
4341 """EAP-TLS with intermediate server/user CA and OCSP on server certificate )SHA1)"""
4342 run_ap_wpa2_eap_tls_intermediate_ca_ocsp(dev, apdev, params, "-sha1")
4343
4344def run_ap_wpa2_eap_tls_intermediate_ca_ocsp(dev, apdev, params, md):
52811b8c
JM
4345 params = int_eap_server_params()
4346 params["ca_cert"] = "auth_serv/iCA-server/ca-and-root.pem"
4347 params["server_cert"] = "auth_serv/iCA-server/server.pem"
4348 params["private_key"] = "auth_serv/iCA-server/server.key"
b7288e5d 4349 fn = ica_ocsp("server.pem", md)
52811b8c
JM
4350 params["ocsp_stapling_response"] = fn
4351 try:
8b8a1864 4352 hostapd.add_ap(apdev[0], params)
b4635f0a 4353 tls = dev[0].request("GET tls_library")
f08362e9 4354 if "GnuTLS" in tls or "wolfSSL" in tls:
b4635f0a
JM
4355 ca_cert = "auth_serv/iCA-user/ca-and-root.pem"
4356 client_cert = "auth_serv/iCA-user/user_and_ica.pem"
4357 else:
4358 ca_cert = "auth_serv/iCA-user/ca-and-root.pem"
4359 client_cert = "auth_serv/iCA-user/user.pem"
52811b8c
JM
4360 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4361 identity="tls user",
b4635f0a
JM
4362 ca_cert=ca_cert,
4363 client_cert=client_cert,
52811b8c
JM
4364 private_key="auth_serv/iCA-user/user.key",
4365 scan_freq="2412", ocsp=2)
4366 finally:
4367 os.unlink(fn)
4368
4369def test_ap_wpa2_eap_tls_intermediate_ca_ocsp_revoked(dev, apdev, params):
4370 """EAP-TLS with intermediate server/user CA and OCSP on revoked server certificate"""
b7288e5d
JM
4371 run_ap_wpa2_eap_tls_intermediate_ca_ocsp_revoked(dev, apdev, params,
4372 "-sha256")
4373
4374def test_ap_wpa2_eap_tls_intermediate_ca_ocsp_revoked_sha1(dev, apdev, params):
4375 """EAP-TLS with intermediate server/user CA and OCSP on revoked server certificate (SHA1)"""
4376 run_ap_wpa2_eap_tls_intermediate_ca_ocsp_revoked(dev, apdev, params,
4377 "-sha1")
4378
4379def run_ap_wpa2_eap_tls_intermediate_ca_ocsp_revoked(dev, apdev, params, md):
585e728a 4380 check_ocsp_support(dev[0])
52811b8c
JM
4381 params = int_eap_server_params()
4382 params["ca_cert"] = "auth_serv/iCA-server/ca-and-root.pem"
4383 params["server_cert"] = "auth_serv/iCA-server/server-revoked.pem"
4384 params["private_key"] = "auth_serv/iCA-server/server-revoked.key"
b7288e5d 4385 fn = ica_ocsp("server-revoked.pem", md)
52811b8c
JM
4386 params["ocsp_stapling_response"] = fn
4387 try:
8b8a1864 4388 hostapd.add_ap(apdev[0], params)
b4635f0a 4389 tls = dev[0].request("GET tls_library")
f08362e9 4390 if "GnuTLS" in tls or "wolfSSL" in tls:
b4635f0a
JM
4391 ca_cert = "auth_serv/iCA-user/ca-and-root.pem"
4392 client_cert = "auth_serv/iCA-user/user_and_ica.pem"
4393 else:
4394 ca_cert = "auth_serv/iCA-user/ca-and-root.pem"
4395 client_cert = "auth_serv/iCA-user/user.pem"
52811b8c
JM
4396 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4397 identity="tls user",
b4635f0a
JM
4398 ca_cert=ca_cert,
4399 client_cert=client_cert,
52811b8c
JM
4400 private_key="auth_serv/iCA-user/user.key",
4401 scan_freq="2412", ocsp=1, wait_connect=False)
4402 count = 0
4403 while True:
4404 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS",
4405 "CTRL-EVENT-EAP-SUCCESS"])
4406 if ev is None:
4407 raise Exception("Timeout on EAP status")
4408 if "CTRL-EVENT-EAP-SUCCESS" in ev:
4409 raise Exception("Unexpected EAP-Success")
4410 if 'bad certificate status response' in ev:
4411 break
4412 if 'certificate revoked' in ev:
4413 break
4414 count = count + 1
4415 if count > 10:
4416 raise Exception("Unexpected number of EAP status messages")
4417
4418 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
4419 if ev is None:
4420 raise Exception("Timeout on EAP failure report")
4421 dev[0].request("REMOVE_NETWORK all")
4422 dev[0].wait_disconnected()
4423 finally:
4424 os.unlink(fn)
4425
4426def test_ap_wpa2_eap_tls_intermediate_ca_ocsp_multi_missing_resp(dev, apdev, params):
4427 """EAP-TLS with intermediate server/user CA and OCSP multi missing response"""
4428 check_ocsp_support(dev[0])
4429 check_ocsp_multi_support(dev[0])
4430
4431 params = int_eap_server_params()
4432 params["ca_cert"] = "auth_serv/iCA-server/ca-and-root.pem"
4433 params["server_cert"] = "auth_serv/iCA-server/server.pem"
4434 params["private_key"] = "auth_serv/iCA-server/server.key"
4435 fn = ica_ocsp("server.pem")
4436 params["ocsp_stapling_response"] = fn
4437 try:
8b8a1864 4438 hostapd.add_ap(apdev[0], params)
b4635f0a 4439 tls = dev[0].request("GET tls_library")
f08362e9 4440 if "GnuTLS" in tls or "wolfSSL" in tls:
b4635f0a
JM
4441 ca_cert = "auth_serv/iCA-user/ca-and-root.pem"
4442 client_cert = "auth_serv/iCA-user/user_and_ica.pem"
4443 else:
4444 ca_cert = "auth_serv/iCA-user/ca-and-root.pem"
4445 client_cert = "auth_serv/iCA-user/user.pem"
52811b8c
JM
4446 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4447 identity="tls user",
b4635f0a
JM
4448 ca_cert=ca_cert,
4449 client_cert=client_cert,
52811b8c
JM
4450 private_key="auth_serv/iCA-user/user.key",
4451 scan_freq="2412", ocsp=3, wait_connect=False)
4452 count = 0
4453 while True:
4454 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS",
4455 "CTRL-EVENT-EAP-SUCCESS"])
4456 if ev is None:
4457 raise Exception("Timeout on EAP status")
4458 if "CTRL-EVENT-EAP-SUCCESS" in ev:
4459 raise Exception("Unexpected EAP-Success")
4460 if 'bad certificate status response' in ev:
4461 break
4462 if 'certificate revoked' in ev:
4463 break
4464 count = count + 1
4465 if count > 10:
4466 raise Exception("Unexpected number of EAP status messages")
4467
4468 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
4469 if ev is None:
4470 raise Exception("Timeout on EAP failure report")
4471 dev[0].request("REMOVE_NETWORK all")
4472 dev[0].wait_disconnected()
4473 finally:
4474 os.unlink(fn)
4475
4476def test_ap_wpa2_eap_tls_intermediate_ca_ocsp_multi(dev, apdev, params):
4477 """EAP-TLS with intermediate server/user CA and OCSP multi OK"""
4478 check_ocsp_support(dev[0])
4479 check_ocsp_multi_support(dev[0])
4480
4481 params = int_eap_server_params()
4482 params["ca_cert"] = "auth_serv/iCA-server/ca-and-root.pem"
4483 params["server_cert"] = "auth_serv/iCA-server/server.pem"
4484 params["private_key"] = "auth_serv/iCA-server/server.key"
4485 fn = ica_ocsp("server.pem")
4486 fn2 = root_ocsp("auth_serv/iCA-server/cacert.pem")
4487 params["ocsp_stapling_response"] = fn
4488
06faf9e4 4489 with open(fn, "rb") as f:
52811b8c 4490 resp_server = f.read()
06faf9e4 4491 with open(fn2, "rb") as f:
52811b8c
JM
4492 resp_ica = f.read()
4493
4494 fd3, fn3 = tempfile.mkstemp()
4495 try:
06faf9e4 4496 f = os.fdopen(fd3, 'wb')
52811b8c
JM
4497 f.write(struct.pack(">L", len(resp_server))[1:4])
4498 f.write(resp_server)
4499 f.write(struct.pack(">L", len(resp_ica))[1:4])
4500 f.write(resp_ica)
4501 f.close()
4502
4503 params["ocsp_stapling_response_multi"] = fn3
4504
8b8a1864 4505 hostapd.add_ap(apdev[0], params)
b4635f0a 4506 tls = dev[0].request("GET tls_library")
f08362e9 4507 if "GnuTLS" in tls or "wolfSSL" in tls:
b4635f0a
JM
4508 ca_cert = "auth_serv/iCA-user/ca-and-root.pem"
4509 client_cert = "auth_serv/iCA-user/user_and_ica.pem"
4510 else:
4511 ca_cert = "auth_serv/iCA-user/ca-and-root.pem"
4512 client_cert = "auth_serv/iCA-user/user.pem"
52811b8c
JM
4513 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4514 identity="tls user",
b4635f0a
JM
4515 ca_cert=ca_cert,
4516 client_cert=client_cert,
52811b8c 4517 private_key="auth_serv/iCA-user/user.key",
40ae4a2f 4518 scan_freq="2412", ocsp=3)
52811b8c
JM
4519 dev[0].request("REMOVE_NETWORK all")
4520 dev[0].wait_disconnected()
4521 finally:
4522 os.unlink(fn)
4523 os.unlink(fn2)
4524 os.unlink(fn3)
4525
98d125ca
JM
4526def test_ap_wpa2_eap_tls_ocsp_multi_revoked(dev, apdev, params):
4527 """EAP-TLS and CA signed OCSP multi response (revoked)"""
4528 check_ocsp_support(dev[0])
4529 check_ocsp_multi_support(dev[0])
ff7affcc 4530 check_pkcs12_support(dev[0])
98d125ca
JM
4531
4532 ocsp_revoked = os.path.join(params['logdir'],
4533 "ocsp-resp-ca-signed-revoked.der")
4534 if not os.path.exists(ocsp_revoked):
4535 raise HwsimSkip("No OCSP response (revoked) available")
4536 ocsp_unknown = os.path.join(params['logdir'],
4537 "ocsp-resp-ca-signed-unknown.der")
4538 if not os.path.exists(ocsp_unknown):
4539 raise HwsimSkip("No OCSP response(unknown) available")
4540
06faf9e4 4541 with open(ocsp_revoked, "rb") as f:
98d125ca 4542 resp_revoked = f.read()
06faf9e4 4543 with open(ocsp_unknown, "rb") as f:
98d125ca
JM
4544 resp_unknown = f.read()
4545
4546 fd, fn = tempfile.mkstemp()
4547 try:
4548 # This is not really a valid order of the OCSPResponse items in the
4549 # list, but this works for now to verify parsing and processing of
4550 # multiple responses.
06faf9e4 4551 f = os.fdopen(fd, 'wb')
98d125ca
JM
4552 f.write(struct.pack(">L", len(resp_unknown))[1:4])
4553 f.write(resp_unknown)
4554 f.write(struct.pack(">L", len(resp_revoked))[1:4])
4555 f.write(resp_revoked)
4556 f.write(struct.pack(">L", 0)[1:4])
4557 f.write(struct.pack(">L", len(resp_unknown))[1:4])
4558 f.write(resp_unknown)
4559 f.close()
4560
4561 params = int_eap_server_params()
4562 params["ocsp_stapling_response_multi"] = fn
8b8a1864 4563 hostapd.add_ap(apdev[0], params)
98d125ca
JM
4564 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4565 identity="tls user", ca_cert="auth_serv/ca.pem",
4566 private_key="auth_serv/user.pkcs12",
4567 private_key_passwd="whatever", ocsp=1,
4568 wait_connect=False, scan_freq="2412")
4569 count = 0
4570 while True:
4571 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS",
4572 "CTRL-EVENT-EAP-SUCCESS"])
4573 if ev is None:
4574 raise Exception("Timeout on EAP status")
4575 if "CTRL-EVENT-EAP-SUCCESS" in ev:
4576 raise Exception("Unexpected EAP-Success")
4577 if 'bad certificate status response' in ev:
4578 break
4579 if 'certificate revoked' in ev:
4580 break
4581 count = count + 1
4582 if count > 10:
4583 raise Exception("Unexpected number of EAP status messages")
4584 finally:
4585 os.unlink(fn)
4586
24579e70 4587def test_ap_wpa2_eap_tls_domain_suffix_match_cn_full(dev, apdev):
64e05f96 4588 """WPA2-Enterprise using EAP-TLS and domain suffix match (CN)"""
e78eb404 4589 check_domain_match_full(dev[0])
ff7affcc 4590 check_pkcs12_support(dev[0])
64e05f96
JM
4591 params = int_eap_server_params()
4592 params["server_cert"] = "auth_serv/server-no-dnsname.pem"
4593 params["private_key"] = "auth_serv/server-no-dnsname.key"
8b8a1864 4594 hostapd.add_ap(apdev[0], params)
64e05f96
JM
4595 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4596 identity="tls user", ca_cert="auth_serv/ca.pem",
4597 private_key="auth_serv/user.pkcs12",
4598 private_key_passwd="whatever",
4599 domain_suffix_match="server3.w1.fi",
4600 scan_freq="2412")
24579e70 4601
061cbb25
JM
4602def test_ap_wpa2_eap_tls_domain_match_cn(dev, apdev):
4603 """WPA2-Enterprise using EAP-TLS and domainmatch (CN)"""
e78eb404 4604 check_domain_match(dev[0])
ff7affcc 4605 check_pkcs12_support(dev[0])
061cbb25
JM
4606 params = int_eap_server_params()
4607 params["server_cert"] = "auth_serv/server-no-dnsname.pem"
4608 params["private_key"] = "auth_serv/server-no-dnsname.key"
8b8a1864 4609 hostapd.add_ap(apdev[0], params)
061cbb25
JM
4610 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4611 identity="tls user", ca_cert="auth_serv/ca.pem",
4612 private_key="auth_serv/user.pkcs12",
4613 private_key_passwd="whatever",
4614 domain_match="server3.w1.fi",
4615 scan_freq="2412")
4616
24579e70
JM
4617def test_ap_wpa2_eap_tls_domain_suffix_match_cn(dev, apdev):
4618 """WPA2-Enterprise using EAP-TLS and domain suffix match (CN)"""
4619 check_domain_match_full(dev[0])
ff7affcc 4620 check_pkcs12_support(dev[0])
24579e70
JM
4621 params = int_eap_server_params()
4622 params["server_cert"] = "auth_serv/server-no-dnsname.pem"
4623 params["private_key"] = "auth_serv/server-no-dnsname.key"
8b8a1864 4624 hostapd.add_ap(apdev[0], params)
64e05f96
JM
4625 dev[1].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4626 identity="tls user", ca_cert="auth_serv/ca.pem",
4627 private_key="auth_serv/user.pkcs12",
4628 private_key_passwd="whatever",
4629 domain_suffix_match="w1.fi",
4630 scan_freq="2412")
4631
4632def test_ap_wpa2_eap_tls_domain_suffix_mismatch_cn(dev, apdev):
4633 """WPA2-Enterprise using EAP-TLS and domain suffix mismatch (CN)"""
e78eb404 4634 check_domain_suffix_match(dev[0])
ff7affcc 4635 check_pkcs12_support(dev[0])
64e05f96
JM
4636 params = int_eap_server_params()
4637 params["server_cert"] = "auth_serv/server-no-dnsname.pem"
4638 params["private_key"] = "auth_serv/server-no-dnsname.key"
8b8a1864 4639 hostapd.add_ap(apdev[0], params)
64e05f96
JM
4640 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4641 identity="tls user", ca_cert="auth_serv/ca.pem",
4642 private_key="auth_serv/user.pkcs12",
4643 private_key_passwd="whatever",
4644 domain_suffix_match="example.com",
4645 wait_connect=False,
4646 scan_freq="2412")
c61dca40
JM
4647 dev[1].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4648 identity="tls user", ca_cert="auth_serv/ca.pem",
4649 private_key="auth_serv/user.pkcs12",
4650 private_key_passwd="whatever",
4651 domain_suffix_match="erver3.w1.fi",
4652 wait_connect=False,
4653 scan_freq="2412")
64e05f96
JM
4654 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
4655 if ev is None:
4656 raise Exception("Timeout on EAP failure report")
c61dca40
JM
4657 ev = dev[1].wait_event(["CTRL-EVENT-EAP-FAILURE"])
4658 if ev is None:
4659 raise Exception("Timeout on EAP failure report (2)")
6a4d0dbe 4660
061cbb25
JM
4661def test_ap_wpa2_eap_tls_domain_mismatch_cn(dev, apdev):
4662 """WPA2-Enterprise using EAP-TLS and domain mismatch (CN)"""
e78eb404 4663 check_domain_match(dev[0])
ff7affcc 4664 check_pkcs12_support(dev[0])
061cbb25
JM
4665 params = int_eap_server_params()
4666 params["server_cert"] = "auth_serv/server-no-dnsname.pem"
4667 params["private_key"] = "auth_serv/server-no-dnsname.key"
8b8a1864 4668 hostapd.add_ap(apdev[0], params)
061cbb25
JM
4669 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4670 identity="tls user", ca_cert="auth_serv/ca.pem",
4671 private_key="auth_serv/user.pkcs12",
4672 private_key_passwd="whatever",
4673 domain_match="example.com",
4674 wait_connect=False,
4675 scan_freq="2412")
4676 dev[1].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
4677 identity="tls user", ca_cert="auth_serv/ca.pem",
4678 private_key="auth_serv/user.pkcs12",
4679 private_key_passwd="whatever",
4680 domain_match="w1.fi",
4681 wait_connect=False,
4682 scan_freq="2412")
4683 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
4684 if ev is None:
4685 raise Exception("Timeout on EAP failure report")
4686 ev = dev[1].wait_event(["CTRL-EVENT-EAP-FAILURE"])
4687 if ev is None:
4688 raise Exception("Timeout on EAP failure report (2)")
4689
6a4d0dbe
JM
4690def test_ap_wpa2_eap_ttls_expired_cert(dev, apdev):
4691 """WPA2-Enterprise using EAP-TTLS and expired certificate"""
ca158ea6 4692 skip_with_fips(dev[0])
6a4d0dbe
JM
4693 params = int_eap_server_params()
4694 params["server_cert"] = "auth_serv/server-expired.pem"
4695 params["private_key"] = "auth_serv/server-expired.key"
8b8a1864 4696 hostapd.add_ap(apdev[0], params)
6a4d0dbe
JM
4697 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
4698 identity="mschap user", password="password",
4699 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAP",
4700 wait_connect=False,
4701 scan_freq="2412")
4702 ev = dev[0].wait_event(["CTRL-EVENT-EAP-TLS-CERT-ERROR"])
4703 if ev is None:
4704 raise Exception("Timeout on EAP certificate error report")
4705 if "reason=4" not in ev or "certificate has expired" not in ev:
4706 raise Exception("Unexpected failure reason: " + ev)
4707 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
4708 if ev is None:
4709 raise Exception("Timeout on EAP failure report")
4710
4711def test_ap_wpa2_eap_ttls_ignore_expired_cert(dev, apdev):
4712 """WPA2-Enterprise using EAP-TTLS and ignore certificate expiration"""
ca158ea6 4713 skip_with_fips(dev[0])
6a4d0dbe
JM
4714 params = int_eap_server_params()
4715 params["server_cert"] = "auth_serv/server-expired.pem"
4716 params["private_key"] = "auth_serv/server-expired.key"
8b8a1864 4717 hostapd.add_ap(apdev[0], params)
6a4d0dbe
JM
4718 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
4719 identity="mschap user", password="password",
4720 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAP",
4721 phase1="tls_disable_time_checks=1",
4722 scan_freq="2412")
6ab4a7aa 4723
5748d1e5
JM
4724def test_ap_wpa2_eap_ttls_long_duration(dev, apdev):
4725 """WPA2-Enterprise using EAP-TTLS and long certificate duration"""
ca158ea6 4726 skip_with_fips(dev[0])
5748d1e5
JM
4727 params = int_eap_server_params()
4728 params["server_cert"] = "auth_serv/server-long-duration.pem"
4729 params["private_key"] = "auth_serv/server-long-duration.key"
8b8a1864 4730 hostapd.add_ap(apdev[0], params)
5748d1e5
JM
4731 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
4732 identity="mschap user", password="password",
4733 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAP",
4734 scan_freq="2412")
4735
6ab4a7aa
JM
4736def test_ap_wpa2_eap_ttls_server_cert_eku_client(dev, apdev):
4737 """WPA2-Enterprise using EAP-TTLS and server cert with client EKU"""
ca158ea6 4738 skip_with_fips(dev[0])
6ab4a7aa
JM
4739 params = int_eap_server_params()
4740 params["server_cert"] = "auth_serv/server-eku-client.pem"
4741 params["private_key"] = "auth_serv/server-eku-client.key"
8b8a1864 4742 hostapd.add_ap(apdev[0], params)
6ab4a7aa
JM
4743 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
4744 identity="mschap user", password="password",
4745 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAP",
4746 wait_connect=False,
4747 scan_freq="2412")
4748 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
4749 if ev is None:
4750 raise Exception("Timeout on EAP failure report")
242219c5 4751
14bef66d
JM
4752def test_ap_wpa2_eap_ttls_server_cert_eku_client_server(dev, apdev):
4753 """WPA2-Enterprise using EAP-TTLS and server cert with client and server EKU"""
ca158ea6 4754 skip_with_fips(dev[0])
14bef66d
JM
4755 params = int_eap_server_params()
4756 params["server_cert"] = "auth_serv/server-eku-client-server.pem"
4757 params["private_key"] = "auth_serv/server-eku-client-server.key"
8b8a1864 4758 hostapd.add_ap(apdev[0], params)
14bef66d
JM
4759 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
4760 identity="mschap user", password="password",
4761 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAP",
4762 scan_freq="2412")
4763
c37b02fc
JM
4764def test_ap_wpa2_eap_ttls_server_pkcs12(dev, apdev):
4765 """WPA2-Enterprise using EAP-TTLS and server PKCS#12 file"""
ca158ea6 4766 skip_with_fips(dev[0])
c37b02fc
JM
4767 params = int_eap_server_params()
4768 del params["server_cert"]
4769 params["private_key"] = "auth_serv/server.pkcs12"
8b8a1864 4770 hostapd.add_ap(apdev[0], params)
c37b02fc
JM
4771 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
4772 identity="mschap user", password="password",
4773 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAP",
4774 scan_freq="2412")
4775
31dd3153
JM
4776def test_ap_wpa2_eap_ttls_server_pkcs12_extra(dev, apdev):
4777 """EAP-TTLS and server PKCS#12 file with extra certs"""
4778 skip_with_fips(dev[0])
4779 params = int_eap_server_params()
4780 del params["server_cert"]
4781 params["private_key"] = "auth_serv/server-extra.pkcs12"
4782 params["private_key_passwd"] = "whatever"
8b8a1864 4783 hostapd.add_ap(apdev[0], params)
31dd3153
JM
4784 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
4785 identity="mschap user", password="password",
4786 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAP",
4787 scan_freq="2412")
4788
242219c5
JM
4789def test_ap_wpa2_eap_ttls_dh_params(dev, apdev):
4790 """WPA2-Enterprise connection using EAP-TTLS/CHAP and setting DH params"""
4791 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
4792 hapd = hostapd.add_ap(apdev[0], params)
4793 eap_connect(dev[0], hapd, "TTLS", "pap user",
242219c5 4794 anonymous_identity="ttls", password="password",
ca158ea6 4795 ca_cert="auth_serv/ca.der", phase2="auth=PAP",
242219c5 4796 dh_file="auth_serv/dh.conf")
7c50093f 4797
b3ff3dec
JM
4798def test_ap_wpa2_eap_ttls_dh_params_dsa(dev, apdev):
4799 """WPA2-Enterprise connection using EAP-TTLS and setting DH params (DSA)"""
404597e6 4800 check_dh_dsa_support(dev[0])
b3ff3dec 4801 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687
JD
4802 hapd = hostapd.add_ap(apdev[0], params)
4803 eap_connect(dev[0], hapd, "TTLS", "pap user",
b3ff3dec 4804 anonymous_identity="ttls", password="password",
ca158ea6 4805 ca_cert="auth_serv/ca.der", phase2="auth=PAP",
b3ff3dec
JM
4806 dh_file="auth_serv/dsaparam.pem")
4807
4808def test_ap_wpa2_eap_ttls_dh_params_not_found(dev, apdev):
4809 """EAP-TTLS and DH params file not found"""
ca158ea6 4810 skip_with_fips(dev[0])
b3ff3dec 4811 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 4812 hostapd.add_ap(apdev[0], params)
b3ff3dec
JM
4813 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
4814 identity="mschap user", password="password",
4815 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAP",
4816 dh_file="auth_serv/dh-no-such-file.conf",
4817 scan_freq="2412", wait_connect=False)
4818 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
4819 if ev is None:
4820 raise Exception("EAP failure timed out")
4821 dev[0].request("REMOVE_NETWORK all")
4822 dev[0].wait_disconnected()
4823
4824def test_ap_wpa2_eap_ttls_dh_params_invalid(dev, apdev):
4825 """EAP-TTLS and invalid DH params file"""
ca158ea6 4826 skip_with_fips(dev[0])
b3ff3dec 4827 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 4828 hostapd.add_ap(apdev[0], params)
b3ff3dec
JM
4829 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
4830 identity="mschap user", password="password",
4831 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAP",
4832 dh_file="auth_serv/ca.pem",
4833 scan_freq="2412", wait_connect=False)
4834 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
4835 if ev is None:
4836 raise Exception("EAP failure timed out")
4837 dev[0].request("REMOVE_NETWORK all")
4838 dev[0].wait_disconnected()
4839
6ea231e6
JM
4840def test_ap_wpa2_eap_ttls_dh_params_blob(dev, apdev):
4841 """WPA2-Enterprise connection using EAP-TTLS/CHAP and setting DH params from blob"""
4842 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687 4843 hapd = hostapd.add_ap(apdev[0], params)
768ea0bc 4844 dh = read_pem("auth_serv/dh2.conf")
54c58f29 4845 if "OK" not in dev[0].request("SET blob dhparams " + binascii.hexlify(dh).decode()):
6ea231e6 4846 raise Exception("Could not set dhparams blob")
3b3e2687 4847 eap_connect(dev[0], hapd, "TTLS", "pap user",
6ea231e6 4848 anonymous_identity="ttls", password="password",
ca158ea6 4849 ca_cert="auth_serv/ca.der", phase2="auth=PAP",
6ea231e6
JM
4850 dh_file="blob://dhparams")
4851
768ea0bc
JM
4852def test_ap_wpa2_eap_ttls_dh_params_server(dev, apdev):
4853 """WPA2-Enterprise using EAP-TTLS and alternative server dhparams"""
4854 params = int_eap_server_params()
4855 params["dh_file"] = "auth_serv/dh2.conf"
3b3e2687
JD
4856 hapd = hostapd.add_ap(apdev[0], params)
4857 eap_connect(dev[0], hapd, "TTLS", "pap user",
768ea0bc 4858 anonymous_identity="ttls", password="password",
ca158ea6 4859 ca_cert="auth_serv/ca.der", phase2="auth=PAP")
768ea0bc 4860
b3ff3dec
JM
4861def test_ap_wpa2_eap_ttls_dh_params_dsa_server(dev, apdev):
4862 """WPA2-Enterprise using EAP-TTLS and alternative server dhparams (DSA)"""
4863 params = int_eap_server_params()
4864 params["dh_file"] = "auth_serv/dsaparam.pem"
3b3e2687
JD
4865 hapd = hostapd.add_ap(apdev[0], params)
4866 eap_connect(dev[0], hapd, "TTLS", "pap user",
b3ff3dec 4867 anonymous_identity="ttls", password="password",
ca158ea6 4868 ca_cert="auth_serv/ca.der", phase2="auth=PAP")
b3ff3dec
JM
4869
4870def test_ap_wpa2_eap_ttls_dh_params_not_found(dev, apdev):
4871 """EAP-TLS server and dhparams file not found"""
4872 params = int_eap_server_params()
4873 params["dh_file"] = "auth_serv/dh-no-such-file.conf"
8b8a1864 4874 hapd = hostapd.add_ap(apdev[0], params, no_enable=True)
b3ff3dec
JM
4875 if "FAIL" not in hapd.request("ENABLE"):
4876 raise Exception("Invalid configuration accepted")
4877
4878def test_ap_wpa2_eap_ttls_dh_params_invalid(dev, apdev):
4879 """EAP-TLS server and invalid dhparams file"""
4880 params = int_eap_server_params()
4881 params["dh_file"] = "auth_serv/ca.pem"
8b8a1864 4882 hapd = hostapd.add_ap(apdev[0], params, no_enable=True)
b3ff3dec
JM
4883 if "FAIL" not in hapd.request("ENABLE"):
4884 raise Exception("Invalid configuration accepted")
4885
7c50093f
JM
4886def test_ap_wpa2_eap_reauth(dev, apdev):
4887 """WPA2-Enterprise and Authenticator forcing reauthentication"""
4888 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
4889 params['eap_reauth_period'] = '2'
3b3e2687
JD
4890 hapd = hostapd.add_ap(apdev[0], params)
4891 eap_connect(dev[0], hapd, "PAX", "pax.user@example.com",
7c50093f
JM
4892 password_hex="0123456789abcdef0123456789abcdef")
4893 logger.info("Wait for reauthentication")
4894 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=10)
4895 if ev is None:
4896 raise Exception("Timeout on reauthentication")
4897 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
4898 if ev is None:
4899 raise Exception("Timeout on reauthentication")
4900 for i in range(0, 20):
4901 state = dev[0].get_status_field("wpa_state")
4902 if state == "COMPLETED":
4903 break
4904 time.sleep(0.1)
4905 if state != "COMPLETED":
4906 raise Exception("Reauthentication did not complete")
8b56743e
JM
4907
4908def test_ap_wpa2_eap_request_identity_message(dev, apdev):
4909 """Optional displayable message in EAP Request-Identity"""
4910 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
4911 params['eap_message'] = 'hello\\0networkid=netw,nasid=foo,portid=0,NAIRealms=example.com'
3b3e2687
JD
4912 hapd = hostapd.add_ap(apdev[0], params)
4913 eap_connect(dev[0], hapd, "PAX", "pax.user@example.com",
8b56743e 4914 password_hex="0123456789abcdef0123456789abcdef")
910f16ca
JM
4915
4916def test_ap_wpa2_eap_sim_aka_result_ind(dev, apdev):
4917 """WPA2-Enterprise using EAP-SIM/AKA and protected result indication"""
81e787b7 4918 check_hlr_auc_gw_support()
910f16ca
JM
4919 params = int_eap_server_params()
4920 params['eap_sim_db'] = "unix:/tmp/hlr_auc_gw.sock"
4921 params['eap_sim_aka_result_ind'] = "1"
3b3e2687 4922 hapd = hostapd.add_ap(apdev[0], params)
910f16ca 4923
3b3e2687 4924 eap_connect(dev[0], hapd, "SIM", "1232010000000000",
910f16ca
JM
4925 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581",
4926 phase1="result_ind=1")
4927 eap_reauth(dev[0], "SIM")
3b3e2687 4928 eap_connect(dev[1], hapd, "SIM", "1232010000000000",
910f16ca
JM
4929 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581")
4930
4931 dev[0].request("REMOVE_NETWORK all")
4932 dev[1].request("REMOVE_NETWORK all")
4933
3b3e2687 4934 eap_connect(dev[0], hapd, "AKA", "0232010000000000",
910f16ca
JM
4935 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581:000000000123",
4936 phase1="result_ind=1")
4937 eap_reauth(dev[0], "AKA")
3b3e2687 4938 eap_connect(dev[1], hapd, "AKA", "0232010000000000",
910f16ca
JM
4939 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581:000000000123")
4940
4941 dev[0].request("REMOVE_NETWORK all")
4942 dev[1].request("REMOVE_NETWORK all")
4943
3b3e2687 4944 eap_connect(dev[0], hapd, "AKA'", "6555444333222111",
910f16ca
JM
4945 password="5122250214c33e723a5dd523fc145fc0:981d464c7c52eb6e5036234984ad0bcf:000000000123",
4946 phase1="result_ind=1")
4947 eap_reauth(dev[0], "AKA'")
3b3e2687 4948 eap_connect(dev[1], hapd, "AKA'", "6555444333222111",
910f16ca 4949 password="5122250214c33e723a5dd523fc145fc0:981d464c7c52eb6e5036234984ad0bcf:000000000123")
633e364b 4950
a8217972
JM
4951def test_ap_wpa2_eap_sim_zero_db_timeout(dev, apdev):
4952 """WPA2-Enterprise using EAP-SIM with zero database timeout"""
4953 check_hlr_auc_gw_support()
4954 params = int_eap_server_params()
4955 params['eap_sim_db'] = "unix:/tmp/hlr_auc_gw.sock"
4956 params['eap_sim_db_timeout'] = "0"
4957 params['disable_pmksa_caching'] = '1'
4958 hapd = hostapd.add_ap(apdev[0], params)
4959
4960 # Run multiple iterations to make it more likely to hit the case where the
4961 # DB request times out and response is lost.
4962 for i in range(20):
a8217972
JM
4963 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="SIM",
4964 identity="1232010000000000",
4965 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581",
4966 wait_connect=False, scan_freq="2412")
fab49f61
JM
4967 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED",
4968 "CTRL-EVENT-DISCONNECTED"],
a8217972
JM
4969 timeout=15)
4970 if ev is None:
4971 raise Exception("No connection result")
4972 dev[0].request("REMOVE_NETWORK all")
4973 if "CTRL-EVENT-DISCONNECTED" in ev:
4974 break
4975 dev[0].wait_disconnected()
4976 hapd.ping()
4977
633e364b
JM
4978def test_ap_wpa2_eap_too_many_roundtrips(dev, apdev):
4979 """WPA2-Enterprise connection resulting in too many EAP roundtrips"""
ca158ea6 4980 skip_with_fips(dev[0])
633e364b 4981 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 4982 hostapd.add_ap(apdev[0], params)
633e364b
JM
4983 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP WPA-EAP-SHA256",
4984 eap="TTLS", identity="mschap user",
4985 wait_connect=False, scan_freq="2412", ieee80211w="1",
4986 anonymous_identity="ttls", password="password",
4987 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAP",
78d2233f
JM
4988 fragment_size="8")
4989 ev = dev[0].wait_event(["EAP: more than",
4990 "CTRL-EVENT-EAP-SUCCESS"], timeout=20)
4991 if ev is None or "EAP: more than" not in ev:
633e364b 4992 raise Exception("EAP roundtrip limit not reached")
32dca985
JM
4993
4994def test_ap_wpa2_eap_expanded_nak(dev, apdev):
4995 """WPA2-Enterprise connection with EAP resulting in expanded NAK"""
4996 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 4997 hostapd.add_ap(apdev[0], params)
32dca985
JM
4998 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP WPA-EAP-SHA256",
4999 eap="PSK", identity="vendor-test",
5000 password_hex="ff23456789abcdef0123456789abcdef",
5001 wait_connect=False)
5002
5003 found = False
5004 for i in range(0, 5):
412c6030 5005 ev = dev[0].wait_event(["CTRL-EVENT-EAP-STATUS"], timeout=16)
32dca985
JM
5006 if ev is None:
5007 raise Exception("Association and EAP start timed out")
5008 if "refuse proposed method" in ev:
5009 found = True
5010 break
5011 if not found:
5012 raise Exception("Unexpected EAP status: " + ev)
5013
5014 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"])
5015 if ev is None:
5016 raise Exception("EAP failure timed out")
745f8771
JM
5017
5018def test_ap_wpa2_eap_sql(dev, apdev, params):
5019 """WPA2-Enterprise connection using SQLite for user DB"""
ca158ea6 5020 skip_with_fips(dev[0])
745f8771
JM
5021 try:
5022 import sqlite3
5023 except ImportError:
81e787b7 5024 raise HwsimSkip("No sqlite3 module available")
745f8771
JM
5025 dbfile = os.path.join(params['logdir'], "eap-user.db")
5026 try:
5027 os.remove(dbfile)
5028 except:
5029 pass
5030 con = sqlite3.connect(dbfile)
5031 with con:
5032 cur = con.cursor()
5033 cur.execute("CREATE TABLE users(identity TEXT PRIMARY KEY, methods TEXT, password TEXT, remediation TEXT, phase2 INTEGER)")
5034 cur.execute("CREATE TABLE wildcards(identity TEXT PRIMARY KEY, methods TEXT)")
5035 cur.execute("INSERT INTO users(identity,methods,password,phase2) VALUES ('user-pap','TTLS-PAP','password',1)")
5036 cur.execute("INSERT INTO users(identity,methods,password,phase2) VALUES ('user-chap','TTLS-CHAP','password',1)")
5037 cur.execute("INSERT INTO users(identity,methods,password,phase2) VALUES ('user-mschap','TTLS-MSCHAP','password',1)")
5038 cur.execute("INSERT INTO users(identity,methods,password,phase2) VALUES ('user-mschapv2','TTLS-MSCHAPV2','password',1)")
5039 cur.execute("INSERT INTO wildcards(identity,methods) VALUES ('','TTLS,TLS')")
5040 cur.execute("CREATE TABLE authlog(timestamp TEXT, session TEXT, nas_ip TEXT, username TEXT, note TEXT)")
5041
5042 try:
5043 params = int_eap_server_params()
5044 params["eap_user_file"] = "sqlite:" + dbfile
3b3e2687
JD
5045 hapd = hostapd.add_ap(apdev[0], params)
5046 eap_connect(dev[0], hapd, "TTLS", "user-mschapv2",
745f8771
JM
5047 anonymous_identity="ttls", password="password",
5048 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2")
5049 dev[0].request("REMOVE_NETWORK all")
3b3e2687 5050 eap_connect(dev[1], hapd, "TTLS", "user-mschap",
745f8771
JM
5051 anonymous_identity="ttls", password="password",
5052 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAP")
5053 dev[1].request("REMOVE_NETWORK all")
3b3e2687 5054 eap_connect(dev[0], hapd, "TTLS", "user-chap",
745f8771
JM
5055 anonymous_identity="ttls", password="password",
5056 ca_cert="auth_serv/ca.pem", phase2="auth=CHAP")
3b3e2687 5057 eap_connect(dev[1], hapd, "TTLS", "user-pap",
745f8771
JM
5058 anonymous_identity="ttls", password="password",
5059 ca_cert="auth_serv/ca.pem", phase2="auth=PAP")
5060 finally:
5061 os.remove(dbfile)
b246e2af
JM
5062
5063def test_ap_wpa2_eap_non_ascii_identity(dev, apdev):
5064 """WPA2-Enterprise connection attempt using non-ASCII identity"""
5065 params = int_eap_server_params()
8b8a1864 5066 hostapd.add_ap(apdev[0], params)
b246e2af
JM
5067 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
5068 identity="\x80", password="password", wait_connect=False)
5069 dev[1].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
5070 identity="a\x80", password="password", wait_connect=False)
5071 for i in range(0, 2):
412c6030 5072 ev = dev[i].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=16)
b246e2af
JM
5073 if ev is None:
5074 raise Exception("Association and EAP start timed out")
5075 ev = dev[i].wait_event(["CTRL-EVENT-EAP-METHOD"], timeout=10)
5076 if ev is None:
5077 raise Exception("EAP method selection timed out")
5078
5079def test_ap_wpa2_eap_non_ascii_identity2(dev, apdev):
5080 """WPA2-Enterprise connection attempt using non-ASCII identity"""
5081 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 5082 hostapd.add_ap(apdev[0], params)
b246e2af
JM
5083 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
5084 identity="\x80", password="password", wait_connect=False)
5085 dev[1].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
5086 identity="a\x80", password="password", wait_connect=False)
5087 for i in range(0, 2):
412c6030 5088 ev = dev[i].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=16)
b246e2af
JM
5089 if ev is None:
5090 raise Exception("Association and EAP start timed out")
5091 ev = dev[i].wait_event(["CTRL-EVENT-EAP-METHOD"], timeout=10)
5092 if ev is None:
5093 raise Exception("EAP method selection timed out")
89f20842
JM
5094
5095def test_openssl_cipher_suite_config_wpas(dev, apdev):
5096 """OpenSSL cipher suite configuration on wpa_supplicant"""
a783340d
JM
5097 tls = dev[0].request("GET tls_library")
5098 if not tls.startswith("OpenSSL"):
5099 raise HwsimSkip("TLS library is not OpenSSL: " + tls)
89f20842 5100 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 5101 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 5102 eap_connect(dev[0], hapd, "TTLS", "pap user",
89f20842
JM
5103 anonymous_identity="ttls", password="password",
5104 openssl_ciphers="AES128",
5105 ca_cert="auth_serv/ca.pem", phase2="auth=PAP")
3b3e2687 5106 eap_connect(dev[1], hapd, "TTLS", "pap user",
89f20842
JM
5107 anonymous_identity="ttls", password="password",
5108 openssl_ciphers="EXPORT",
5109 ca_cert="auth_serv/ca.pem", phase2="auth=PAP",
9dd21d51 5110 expect_failure=True, maybe_local_error=True)
7be5ec99
JM
5111 dev[2].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
5112 identity="pap user", anonymous_identity="ttls",
5113 password="password",
5114 openssl_ciphers="FOO",
5115 ca_cert="auth_serv/ca.pem", phase2="auth=PAP",
5116 wait_connect=False)
5117 ev = dev[2].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
5118 if ev is None:
5119 raise Exception("EAP failure after invalid openssl_ciphers not reported")
5120 dev[2].request("DISCONNECT")
89f20842
JM
5121
5122def test_openssl_cipher_suite_config_hapd(dev, apdev):
5123 """OpenSSL cipher suite configuration on hostapd"""
a783340d
JM
5124 tls = dev[0].request("GET tls_library")
5125 if not tls.startswith("OpenSSL"):
5126 raise HwsimSkip("wpa_supplicant TLS library is not OpenSSL: " + tls)
89f20842
JM
5127 params = int_eap_server_params()
5128 params['openssl_ciphers'] = "AES256"
8b8a1864 5129 hapd = hostapd.add_ap(apdev[0], params)
a783340d
JM
5130 tls = hapd.request("GET tls_library")
5131 if not tls.startswith("OpenSSL"):
5132 raise HwsimSkip("hostapd TLS library is not OpenSSL: " + tls)
3b3e2687 5133 eap_connect(dev[0], hapd, "TTLS", "pap user",
89f20842
JM
5134 anonymous_identity="ttls", password="password",
5135 ca_cert="auth_serv/ca.pem", phase2="auth=PAP")
3b3e2687 5136 eap_connect(dev[1], hapd, "TTLS", "pap user",
89f20842
JM
5137 anonymous_identity="ttls", password="password",
5138 openssl_ciphers="AES128",
5139 ca_cert="auth_serv/ca.pem", phase2="auth=PAP",
5140 expect_failure=True)
3b3e2687 5141 eap_connect(dev[2], hapd, "TTLS", "pap user",
89f20842
JM
5142 anonymous_identity="ttls", password="password",
5143 openssl_ciphers="HIGH:!ADH",
5144 ca_cert="auth_serv/ca.pem", phase2="auth=PAP")
5b3c40a6 5145
7be5ec99 5146 params['openssl_ciphers'] = "FOO"
8b8a1864 5147 hapd2 = hostapd.add_ap(apdev[1], params, no_enable=True)
7be5ec99 5148 if "FAIL" not in hapd2.request("ENABLE"):
0d34c13a
JM
5149 if "run=OpenSSL 1.1.1" in tls:
5150 logger.info("Ignore acceptance of an invalid openssl_ciphers value with OpenSSL 1.1.1")
5151 else:
5152 raise Exception("Invalid openssl_ciphers value accepted")
7be5ec99 5153
5b3c40a6
JM
5154def test_wpa2_eap_ttls_pap_key_lifetime_in_memory(dev, apdev, params):
5155 """Key lifetime in memory with WPA2-Enterprise using EAP-TTLS/PAP"""
5156 p = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 5157 hapd = hostapd.add_ap(apdev[0], p)
5b3c40a6 5158 password = "63d2d21ac3c09ed567ee004a34490f1d16e7fa5835edf17ddba70a63f1a90a25"
3b3e2687 5159 id = eap_connect(dev[0], hapd, "TTLS", "pap-secret",
5b3c40a6
JM
5160 anonymous_identity="ttls", password=password,
5161 ca_cert="auth_serv/ca.pem", phase2="auth=PAP")
cdc23db2
JM
5162 run_eap_key_lifetime_in_memory(dev, params, id, password)
5163
5164def test_wpa2_eap_peap_gtc_key_lifetime_in_memory(dev, apdev, params):
5165 """Key lifetime in memory with WPA2-Enterprise using PEAP/GTC"""
5166 p = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
5167 hapd = hostapd.add_ap(apdev[0], p)
5168 password = "63d2d21ac3c09ed567ee004a34490f1d16e7fa5835edf17ddba70a63f1a90a25"
5169 id = eap_connect(dev[0], hapd, "PEAP", "user-secret",
5170 anonymous_identity="peap", password=password,
5171 ca_cert="auth_serv/ca.pem", phase2="auth=GTC")
5172 run_eap_key_lifetime_in_memory(dev, params, id, password)
5173
5174def run_eap_key_lifetime_in_memory(dev, params, id, password):
5175 pid = find_wpas_process(dev[0])
5176
8e416cec
JM
5177 # The decrypted copy of GTK is freed only after the CTRL-EVENT-CONNECTED
5178 # event has been delivered, so verify that wpa_supplicant has returned to
5179 # eloop before reading process memory.
54f2cae2 5180 time.sleep(1)
8e416cec 5181 dev[0].ping()
b3361e5d 5182 password = password.encode()
5b3c40a6
JM
5183 buf = read_process_memory(pid, password)
5184
5185 dev[0].request("DISCONNECT")
5186 dev[0].wait_disconnected()
5187
5188 dev[0].relog()
750904dd
JM
5189 msk = None
5190 emsk = None
5b3c40a6
JM
5191 pmk = None
5192 ptk = None
5193 gtk = None
5194 with open(os.path.join(params['logdir'], 'log0'), 'r') as f:
5195 for l in f.readlines():
cdc23db2
JM
5196 if "EAP-TTLS: Derived key - hexdump" in l or \
5197 "EAP-PEAP: Derived key - hexdump" in l:
750904dd
JM
5198 val = l.strip().split(':')[3].replace(' ', '')
5199 msk = binascii.unhexlify(val)
cdc23db2
JM
5200 if "EAP-TTLS: Derived EMSK - hexdump" in l or \
5201 "EAP-PEAP: Derived EMSK - hexdump" in l:
750904dd
JM
5202 val = l.strip().split(':')[3].replace(' ', '')
5203 emsk = binascii.unhexlify(val)
5b3c40a6
JM
5204 if "WPA: PMK - hexdump" in l:
5205 val = l.strip().split(':')[3].replace(' ', '')
5206 pmk = binascii.unhexlify(val)
5207 if "WPA: PTK - hexdump" in l:
5208 val = l.strip().split(':')[3].replace(' ', '')
5209 ptk = binascii.unhexlify(val)
5210 if "WPA: Group Key - hexdump" in l:
5211 val = l.strip().split(':')[3].replace(' ', '')
5212 gtk = binascii.unhexlify(val)
750904dd 5213 if not msk or not emsk or not pmk or not ptk or not gtk:
5b3c40a6
JM
5214 raise Exception("Could not find keys from debug log")
5215 if len(gtk) != 16:
5216 raise Exception("Unexpected GTK length")
5217
5218 kck = ptk[0:16]
5219 kek = ptk[16:32]
5220 tk = ptk[32:48]
5221
5222 fname = os.path.join(params['logdir'],
5223 'wpa2_eap_ttls_pap_key_lifetime_in_memory.memctx-')
5224
5225 logger.info("Checking keys in memory while associated")
5226 get_key_locations(buf, password, "Password")
5227 get_key_locations(buf, pmk, "PMK")
750904dd
JM
5228 get_key_locations(buf, msk, "MSK")
5229 get_key_locations(buf, emsk, "EMSK")
5b3c40a6 5230 if password not in buf:
81e787b7 5231 raise HwsimSkip("Password not found while associated")
5b3c40a6 5232 if pmk not in buf:
81e787b7 5233 raise HwsimSkip("PMK not found while associated")
5b3c40a6
JM
5234 if kck not in buf:
5235 raise Exception("KCK not found while associated")
5236 if kek not in buf:
5237 raise Exception("KEK not found while associated")
b74f82a4
JM
5238 #if tk in buf:
5239 # raise Exception("TK found from memory")
5b3c40a6
JM
5240
5241 logger.info("Checking keys in memory after disassociation")
5242 buf = read_process_memory(pid, password)
5243
5244 # Note: Password is still present in network configuration
5245 # Note: PMK is in PMKSA cache and EAP fast re-auth data
5246
5247 get_key_locations(buf, password, "Password")
5248 get_key_locations(buf, pmk, "PMK")
750904dd
JM
5249 get_key_locations(buf, msk, "MSK")
5250 get_key_locations(buf, emsk, "EMSK")
5b3c40a6
JM
5251 verify_not_present(buf, kck, fname, "KCK")
5252 verify_not_present(buf, kek, fname, "KEK")
5253 verify_not_present(buf, tk, fname, "TK")
6db556b2
JM
5254 if gtk in buf:
5255 get_key_locations(buf, gtk, "GTK")
5b3c40a6
JM
5256 verify_not_present(buf, gtk, fname, "GTK")
5257
5258 dev[0].request("PMKSA_FLUSH")
5259 dev[0].set_network_quoted(id, "identity", "foo")
5260 logger.info("Checking keys in memory after PMKSA cache and EAP fast reauth flush")
5261 buf = read_process_memory(pid, password)
5262 get_key_locations(buf, password, "Password")
5263 get_key_locations(buf, pmk, "PMK")
750904dd
JM
5264 get_key_locations(buf, msk, "MSK")
5265 get_key_locations(buf, emsk, "EMSK")
5b3c40a6
JM
5266 verify_not_present(buf, pmk, fname, "PMK")
5267
5268 dev[0].request("REMOVE_NETWORK all")
5269
5270 logger.info("Checking keys in memory after network profile removal")
5271 buf = read_process_memory(pid, password)
5272
5273 get_key_locations(buf, password, "Password")
5274 get_key_locations(buf, pmk, "PMK")
750904dd
JM
5275 get_key_locations(buf, msk, "MSK")
5276 get_key_locations(buf, emsk, "EMSK")
5b3c40a6
JM
5277 verify_not_present(buf, password, fname, "password")
5278 verify_not_present(buf, pmk, fname, "PMK")
5279 verify_not_present(buf, kck, fname, "KCK")
5280 verify_not_present(buf, kek, fname, "KEK")
5281 verify_not_present(buf, tk, fname, "TK")
5282 verify_not_present(buf, gtk, fname, "GTK")
750904dd
JM
5283 verify_not_present(buf, msk, fname, "MSK")
5284 verify_not_present(buf, emsk, fname, "EMSK")
a08fdb17
JM
5285
5286def test_ap_wpa2_eap_unexpected_wep_eapol_key(dev, apdev):
5287 """WPA2-Enterprise connection and unexpected WEP EAPOL-Key"""
5288 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 5289 hapd = hostapd.add_ap(apdev[0], params)
a08fdb17 5290 bssid = apdev[0]['bssid']
3b3e2687 5291 eap_connect(dev[0], hapd, "TTLS", "pap user",
a08fdb17
JM
5292 anonymous_identity="ttls", password="password",
5293 ca_cert="auth_serv/ca.pem", phase2="auth=PAP")
5294
5295 # Send unexpected WEP EAPOL-Key; this gets dropped
5296 res = dev[0].request("EAPOL_RX " + bssid + " 0203002c0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
5297 if "OK" not in res:
5298 raise Exception("EAPOL_RX to wpa_supplicant failed")
52352802
JM
5299
5300def test_ap_wpa2_eap_in_bridge(dev, apdev):
5301 """WPA2-EAP and wpas interface in a bridge"""
fab49f61
JM
5302 br_ifname = 'sta-br0'
5303 ifname = 'wlan5'
52352802
JM
5304 try:
5305 _test_ap_wpa2_eap_in_bridge(dev, apdev)
5306 finally:
5307 subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'down'])
5308 subprocess.call(['brctl', 'delif', br_ifname, ifname])
5309 subprocess.call(['brctl', 'delbr', br_ifname])
5310 subprocess.call(['iw', ifname, 'set', '4addr', 'off'])
5311
5312def _test_ap_wpa2_eap_in_bridge(dev, apdev):
5313 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 5314 hapd = hostapd.add_ap(apdev[0], params)
52352802 5315
fab49f61
JM
5316 br_ifname = 'sta-br0'
5317 ifname = 'wlan5'
52352802
JM
5318 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
5319 subprocess.call(['brctl', 'addbr', br_ifname])
5320 subprocess.call(['brctl', 'setfd', br_ifname, '0'])
5321 subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'up'])
5322 subprocess.call(['iw', ifname, 'set', '4addr', 'on'])
5323 subprocess.check_call(['brctl', 'addif', br_ifname, ifname])
5324 wpas.interface_add(ifname, br_ifname=br_ifname)
4b9d79b6 5325 wpas.dump_monitor()
52352802 5326
3b3e2687 5327 id = eap_connect(wpas, hapd, "PAX", "pax.user@example.com",
52352802 5328 password_hex="0123456789abcdef0123456789abcdef")
4b9d79b6 5329 wpas.dump_monitor()
52352802 5330 eap_reauth(wpas, "PAX")
4b9d79b6 5331 wpas.dump_monitor()
52352802
JM
5332 # Try again as a regression test for packet socket workaround
5333 eap_reauth(wpas, "PAX")
4b9d79b6 5334 wpas.dump_monitor()
52352802
JM
5335 wpas.request("DISCONNECT")
5336 wpas.wait_disconnected()
4b9d79b6 5337 wpas.dump_monitor()
52352802
JM
5338 wpas.request("RECONNECT")
5339 wpas.wait_connected()
4b9d79b6 5340 wpas.dump_monitor()
febf5752
JM
5341
5342def test_ap_wpa2_eap_session_ticket(dev, apdev):
5343 """WPA2-Enterprise connection using EAP-TTLS and TLS session ticket enabled"""
5344 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 5345 hapd = hostapd.add_ap(apdev[0], params)
febf5752
JM
5346 key_mgmt = hapd.get_config()['key_mgmt']
5347 if key_mgmt.split(' ')[0] != "WPA-EAP":
5348 raise Exception("Unexpected GET_CONFIG(key_mgmt): " + key_mgmt)
3b3e2687 5349 eap_connect(dev[0], hapd, "TTLS", "pap user",
febf5752
JM
5350 anonymous_identity="ttls", password="password",
5351 ca_cert="auth_serv/ca.pem",
5352 phase1="tls_disable_session_ticket=0", phase2="auth=PAP")
5353 eap_reauth(dev[0], "TTLS")
5354
5355def test_ap_wpa2_eap_no_workaround(dev, apdev):
5356 """WPA2-Enterprise connection using EAP-TTLS and eap_workaround=0"""
5357 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 5358 hapd = hostapd.add_ap(apdev[0], params)
febf5752
JM
5359 key_mgmt = hapd.get_config()['key_mgmt']
5360 if key_mgmt.split(' ')[0] != "WPA-EAP":
5361 raise Exception("Unexpected GET_CONFIG(key_mgmt): " + key_mgmt)
3b3e2687 5362 eap_connect(dev[0], hapd, "TTLS", "pap user",
febf5752
JM
5363 anonymous_identity="ttls", password="password",
5364 ca_cert="auth_serv/ca.pem", eap_workaround='0',
5365 phase2="auth=PAP")
5366 eap_reauth(dev[0], "TTLS")
b197a819
JM
5367
5368def test_ap_wpa2_eap_tls_check_crl(dev, apdev):
5369 """EAP-TLS and server checking CRL"""
5370 params = int_eap_server_params()
5371 params['check_crl'] = '1'
8b8a1864 5372 hapd = hostapd.add_ap(apdev[0], params)
b197a819
JM
5373
5374 # check_crl=1 and no CRL available --> reject connection
3b3e2687 5375 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
b197a819
JM
5376 client_cert="auth_serv/user.pem",
5377 private_key="auth_serv/user.key", expect_failure=True)
5378 dev[0].request("REMOVE_NETWORK all")
5379
5380 hapd.disable()
5381 hapd.set("ca_cert", "auth_serv/ca-and-crl.pem")
5382 hapd.enable()
5383
5384 # check_crl=1 and valid CRL --> accept
3b3e2687 5385 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
b197a819
JM
5386 client_cert="auth_serv/user.pem",
5387 private_key="auth_serv/user.key")
5388 dev[0].request("REMOVE_NETWORK all")
5389
5390 hapd.disable()
5391 hapd.set("check_crl", "2")
5392 hapd.enable()
5393
5394 # check_crl=2 and valid CRL --> accept
3b3e2687 5395 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
b197a819
JM
5396 client_cert="auth_serv/user.pem",
5397 private_key="auth_serv/user.key")
5398 dev[0].request("REMOVE_NETWORK all")
b1fb4275 5399
a18d58f4
JM
5400def test_ap_wpa2_eap_tls_crl_reload(dev, apdev, params):
5401 """EAP-TLS and server reloading CRL from ca_cert"""
5402 ca_cert = os.path.join(params['logdir'],
5403 "ap_wpa2_eap_tls_crl_reload.ca_cert")
5404 with open('auth_serv/ca.pem', 'r') as f:
5405 only_cert = f.read()
5406 with open('auth_serv/ca-and-crl.pem', 'r') as f:
5407 cert_and_crl = f.read()
5408 with open(ca_cert, 'w') as f:
5409 f.write(only_cert)
5410 params = int_eap_server_params()
5411 params['ca_cert'] = ca_cert
5412 params['check_crl'] = '1'
5413 params['crl_reload_interval'] = '1'
5414 hapd = hostapd.add_ap(apdev[0], params)
5415
5416 # check_crl=1 and no CRL available --> reject connection
5417 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
5418 client_cert="auth_serv/user.pem",
5419 private_key="auth_serv/user.key", expect_failure=True)
5420 dev[0].request("REMOVE_NETWORK all")
5421 dev[0].dump_monitor()
5422
5423 with open(ca_cert, 'w') as f:
5424 f.write(cert_and_crl)
5425 time.sleep(1)
5426
5427 # check_crl=1 and valid CRL --> accept
5428 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
5429 client_cert="auth_serv/user.pem",
5430 private_key="auth_serv/user.key")
5431 dev[0].request("REMOVE_NETWORK all")
5432 dev[0].wait_disconnected()
5433
f4f17e9a
JM
5434def test_ap_wpa2_eap_tls_check_cert_subject(dev, apdev):
5435 """EAP-TLS and server checking client subject name"""
5436 params = int_eap_server_params()
5437 params['check_cert_subject'] = 'C=FI/O=w1.fi/CN=Test User'
5438 hapd = hostapd.add_ap(apdev[0], params)
5439 check_check_cert_subject_support(hapd)
5440
5441 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
5442 client_cert="auth_serv/user.pem",
5443 private_key="auth_serv/user.key")
5444
5445def test_ap_wpa2_eap_tls_check_cert_subject_neg(dev, apdev):
5446 """EAP-TLS and server checking client subject name (negative)"""
5447 params = int_eap_server_params()
5448 params['check_cert_subject'] = 'C=FI/O=example'
5449 hapd = hostapd.add_ap(apdev[0], params)
5450 check_check_cert_subject_support(hapd)
5451
5452 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
5453 client_cert="auth_serv/user.pem",
5454 private_key="auth_serv/user.key", expect_failure=True)
5455
b1fb4275
JM
5456def test_ap_wpa2_eap_tls_oom(dev, apdev):
5457 """EAP-TLS and OOM"""
5458 check_subject_match_support(dev[0])
5459 check_altsubject_match_support(dev[0])
e78eb404 5460 check_domain_match(dev[0])
b1fb4275
JM
5461 check_domain_match_full(dev[0])
5462
5463 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 5464 hostapd.add_ap(apdev[0], params)
b1fb4275 5465
fab49f61
JM
5466 tests = [(1, "tls_connection_set_subject_match"),
5467 (2, "tls_connection_set_subject_match"),
5468 (3, "tls_connection_set_subject_match"),
5469 (4, "tls_connection_set_subject_match")]
b1fb4275
JM
5470 for count, func in tests:
5471 with alloc_fail(dev[0], count, func):
5472 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
5473 identity="tls user", ca_cert="auth_serv/ca.pem",
5474 client_cert="auth_serv/user.pem",
5475 private_key="auth_serv/user.key",
5476 subject_match="/C=FI/O=w1.fi/CN=server.w1.fi",
5477 altsubject_match="EMAIL:noone@example.com;DNS:server.w1.fi;URI:http://example.com/",
5478 domain_suffix_match="server.w1.fi",
5479 domain_match="server.w1.fi",
5480 wait_connect=False, scan_freq="2412")
5481 # TLS parameter configuration error results in CTRL-REQ-PASSPHRASE
5482 ev = dev[0].wait_event(["CTRL-REQ-PASSPHRASE"], timeout=5)
5483 if ev is None:
5484 raise Exception("No passphrase request")
5485 dev[0].request("REMOVE_NETWORK all")
5486 dev[0].wait_disconnected()
405c621c
JM
5487
5488def test_ap_wpa2_eap_tls_macacl(dev, apdev):
5489 """WPA2-Enterprise connection using MAC ACL"""
5490 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
5491 params["macaddr_acl"] = "2"
3b3e2687
JD
5492 hapd = hostapd.add_ap(apdev[0], params)
5493 eap_connect(dev[1], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
405c621c
JM
5494 client_cert="auth_serv/user.pem",
5495 private_key="auth_serv/user.key")
85774b70
JM
5496
5497def test_ap_wpa2_eap_oom(dev, apdev):
5498 """EAP server and OOM"""
5499 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 5500 hapd = hostapd.add_ap(apdev[0], params)
85774b70
JM
5501 dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
5502
5503 with alloc_fail(hapd, 1, "eapol_auth_alloc"):
5504 # The first attempt fails, but STA will send EAPOL-Start to retry and
5505 # that succeeds.
5506 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
5507 identity="tls user", ca_cert="auth_serv/ca.pem",
5508 client_cert="auth_serv/user.pem",
5509 private_key="auth_serv/user.key",
5510 scan_freq="2412")
6c4b5da4 5511
3b3e2687
JD
5512def check_tls_ver(dev, hapd, phase1, expected):
5513 eap_connect(dev, hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
6c4b5da4
JM
5514 client_cert="auth_serv/user.pem",
5515 private_key="auth_serv/user.key",
5516 phase1=phase1)
5517 ver = dev.get_status_field("eap_tls_version")
5518 if ver != expected:
5519 raise Exception("Unexpected TLS version (expected %s): %s" % (expected, ver))
5520
5521def test_ap_wpa2_eap_tls_versions(dev, apdev):
5522 """EAP-TLS and TLS version configuration"""
5523 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
3b3e2687 5524 hapd = hostapd.add_ap(apdev[0], params)
6c4b5da4
JM
5525
5526 tls = dev[0].request("GET tls_library")
5527 if tls.startswith("OpenSSL"):
41d5af55 5528 if "build=OpenSSL 1.0.1" not in tls and "run=OpenSSL 1.0.1" not in tls:
3b3e2687 5529 check_tls_ver(dev[0], hapd,
6c4b5da4
JM
5530 "tls_disable_tlsv1_0=1 tls_disable_tlsv1_1=1",
5531 "TLSv1.2")
d8003dcb
SP
5532 if tls.startswith("wolfSSL"):
5533 if ("build=3.10.0" in tls and "run=3.10.0" in tls) or \
5534 ("build=3.13.0" in tls and "run=3.13.0" in tls):
5535 check_tls_ver(dev[0], hapd,
5536 "tls_disable_tlsv1_0=1 tls_disable_tlsv1_1=1",
5537 "TLSv1.2")
2286578f 5538 elif tls.startswith("internal"):
3b3e2687 5539 check_tls_ver(dev[0], hapd,
2286578f 5540 "tls_disable_tlsv1_0=1 tls_disable_tlsv1_1=1", "TLSv1.2")
3b3e2687 5541 check_tls_ver(dev[1], hapd,
6c4b5da4 5542 "tls_disable_tlsv1_0=1 tls_disable_tlsv1_2=1", "TLSv1.1")
3b3e2687 5543 check_tls_ver(dev[2], hapd,
6c4b5da4 5544 "tls_disable_tlsv1_1=1 tls_disable_tlsv1_2=1", "TLSv1")
832b736f
JM
5545 if "run=OpenSSL 1.1.1" in tls:
5546 check_tls_ver(dev[0], hapd,
5547 "tls_disable_tlsv1_0=1 tls_disable_tlsv1_1=1 tls_disable_tlsv1_2=1 tls_disable_tlsv1_3=0", "TLSv1.3")
ecafa0cf 5548
6447b874
JM
5549def test_ap_wpa2_eap_tls_13(dev, apdev):
5550 """EAP-TLS and TLS 1.3"""
5551 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
5552 hapd = hostapd.add_ap(apdev[0], params)
5553
5554 tls = dev[0].request("GET tls_library")
5555 if "run=OpenSSL 1.1.1" not in tls:
5556 raise HwsimSkip("TLS v1.3 not supported")
5557 id = eap_connect(dev[0], hapd, "TLS", "tls user",
5558 ca_cert="auth_serv/ca.pem",
5559 client_cert="auth_serv/user.pem",
5560 private_key="auth_serv/user.key",
5561 phase1="tls_disable_tlsv1_0=1 tls_disable_tlsv1_1=1 tls_disable_tlsv1_2=1 tls_disable_tlsv1_3=0")
5562 ver = dev[0].get_status_field("eap_tls_version")
5563 if ver != "TLSv1.3":
5564 raise Exception("Unexpected TLS version")
5565
5566 eap_reauth(dev[0], "TLS")
5567 dev[0].request("DISCONNECT")
5568 dev[0].wait_disconnected()
5569 dev[0].request("PMKSA_FLUSH")
5570 dev[0].request("RECONNECT")
5571 dev[0].wait_connected()
5572
ecafa0cf
JM
5573def test_rsn_ie_proto_eap_sta(dev, apdev):
5574 """RSN element protocol testing for EAP cases on STA side"""
5575 bssid = apdev[0]['bssid']
5576 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
5577 # This is the RSN element used normally by hostapd
5578 params['own_ie_override'] = '30140100000fac040100000fac040100000fac010c00'
8b8a1864 5579 hapd = hostapd.add_ap(apdev[0], params)
ecafa0cf
JM
5580 id = dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="GPSK",
5581 identity="gpsk user",
5582 password="abcdefghijklmnop0123456789abcdef",
5583 scan_freq="2412")
5584
fab49f61
JM
5585 tests = [('No RSN Capabilities field',
5586 '30120100000fac040100000fac040100000fac01'),
5587 ('No AKM Suite fields',
5588 '300c0100000fac040100000fac04'),
5589 ('No Pairwise Cipher Suite fields',
5590 '30060100000fac04'),
5591 ('No Group Data Cipher Suite field',
5592 '30020100')]
5593 for txt, ie in tests:
ecafa0cf
JM
5594 dev[0].request("DISCONNECT")
5595 dev[0].wait_disconnected()
5596 logger.info(txt)
5597 hapd.disable()
5598 hapd.set('own_ie_override', ie)
5599 hapd.enable()
5600 dev[0].request("BSS_FLUSH 0")
5601 dev[0].scan_for_bss(bssid, 2412, force_scan=True, only_new=True)
5602 dev[0].select_network(id, freq=2412)
5603 dev[0].wait_connected()
f9dd43ea 5604
9353f07f
JM
5605 dev[0].request("DISCONNECT")
5606 dev[0].wait_disconnected()
5607 dev[0].flush_scan_cache()
5608
f9dd43ea
JM
5609def check_tls_session_resumption_capa(dev, hapd):
5610 tls = hapd.request("GET tls_library")
5611 if not tls.startswith("OpenSSL"):
d8003dcb 5612 raise HwsimSkip("hostapd TLS library is not OpenSSL or wolfSSL: " + tls)
f9dd43ea
JM
5613
5614 tls = dev.request("GET tls_library")
5615 if not tls.startswith("OpenSSL"):
5616 raise HwsimSkip("Session resumption not supported with this TLS library: " + tls)
5617
5618def test_eap_ttls_pap_session_resumption(dev, apdev):
5619 """EAP-TTLS/PAP session resumption"""
5620 params = int_eap_server_params()
5621 params['tls_session_lifetime'] = '60'
8b8a1864 5622 hapd = hostapd.add_ap(apdev[0], params)
f9dd43ea 5623 check_tls_session_resumption_capa(dev[0], hapd)
3b3e2687 5624 eap_connect(dev[0], hapd, "TTLS", "pap user",
f9dd43ea
JM
5625 anonymous_identity="ttls", password="password",
5626 ca_cert="auth_serv/ca.pem", eap_workaround='0',
5627 phase2="auth=PAP")
5628 if dev[0].get_status_field("tls_session_reused") != '0':
5629 raise Exception("Unexpected session resumption on the first connection")
5630
5631 dev[0].request("REAUTHENTICATE")
5632 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
5633 if ev is None:
5634 raise Exception("EAP success timed out")
5635 ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=10)
5636 if ev is None:
5637 raise Exception("Key handshake with the AP timed out")
5638 if dev[0].get_status_field("tls_session_reused") != '1':
5639 raise Exception("Session resumption not used on the second connection")
720a2e79 5640 hwsim_utils.test_connectivity(dev[0], hapd)
f9dd43ea
JM
5641
5642def test_eap_ttls_chap_session_resumption(dev, apdev):
5643 """EAP-TTLS/CHAP session resumption"""
5644 params = int_eap_server_params()
5645 params['tls_session_lifetime'] = '60'
8b8a1864 5646 hapd = hostapd.add_ap(apdev[0], params)
f9dd43ea 5647 check_tls_session_resumption_capa(dev[0], hapd)
3b3e2687 5648 eap_connect(dev[0], hapd, "TTLS", "chap user",
f9dd43ea
JM
5649 anonymous_identity="ttls", password="password",
5650 ca_cert="auth_serv/ca.der", phase2="auth=CHAP")
5651 if dev[0].get_status_field("tls_session_reused") != '0':
5652 raise Exception("Unexpected session resumption on the first connection")
5653
5654 dev[0].request("REAUTHENTICATE")
5655 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
5656 if ev is None:
5657 raise Exception("EAP success timed out")
5658 ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=10)
5659 if ev is None:
5660 raise Exception("Key handshake with the AP timed out")
5661 if dev[0].get_status_field("tls_session_reused") != '1':
5662 raise Exception("Session resumption not used on the second connection")
5663
5664def test_eap_ttls_mschap_session_resumption(dev, apdev):
5665 """EAP-TTLS/MSCHAP session resumption"""
e78eb404 5666 check_domain_suffix_match(dev[0])
f9dd43ea
JM
5667 params = int_eap_server_params()
5668 params['tls_session_lifetime'] = '60'
8b8a1864 5669 hapd = hostapd.add_ap(apdev[0], params)
f9dd43ea 5670 check_tls_session_resumption_capa(dev[0], hapd)
3b3e2687 5671 eap_connect(dev[0], hapd, "TTLS", "mschap user",
f9dd43ea
JM
5672 anonymous_identity="ttls", password="password",
5673 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAP",
5674 domain_suffix_match="server.w1.fi")
5675 if dev[0].get_status_field("tls_session_reused") != '0':
5676 raise Exception("Unexpected session resumption on the first connection")
5677
5678 dev[0].request("REAUTHENTICATE")
5679 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
5680 if ev is None:
5681 raise Exception("EAP success timed out")
5682 ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=10)
5683 if ev is None:
5684 raise Exception("Key handshake with the AP timed out")
5685 if dev[0].get_status_field("tls_session_reused") != '1':
5686 raise Exception("Session resumption not used on the second connection")
5687
5688def test_eap_ttls_mschapv2_session_resumption(dev, apdev):
5689 """EAP-TTLS/MSCHAPv2 session resumption"""
e78eb404 5690 check_domain_suffix_match(dev[0])
f9dd43ea
JM
5691 check_eap_capa(dev[0], "MSCHAPV2")
5692 params = int_eap_server_params()
5693 params['tls_session_lifetime'] = '60'
8b8a1864 5694 hapd = hostapd.add_ap(apdev[0], params)
f9dd43ea 5695 check_tls_session_resumption_capa(dev[0], hapd)
3b3e2687 5696 eap_connect(dev[0], hapd, "TTLS", "DOMAIN\mschapv2 user",
f9dd43ea
JM
5697 anonymous_identity="ttls", password="password",
5698 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
5699 domain_suffix_match="server.w1.fi")
5700 if dev[0].get_status_field("tls_session_reused") != '0':
5701 raise Exception("Unexpected session resumption on the first connection")
5702
5703 dev[0].request("REAUTHENTICATE")
5704 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
5705 if ev is None:
5706 raise Exception("EAP success timed out")
5707 ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=10)
5708 if ev is None:
5709 raise Exception("Key handshake with the AP timed out")
5710 if dev[0].get_status_field("tls_session_reused") != '1':
5711 raise Exception("Session resumption not used on the second connection")
5712
5713def test_eap_ttls_eap_gtc_session_resumption(dev, apdev):
5714 """EAP-TTLS/EAP-GTC session resumption"""
5715 params = int_eap_server_params()
5716 params['tls_session_lifetime'] = '60'
8b8a1864 5717 hapd = hostapd.add_ap(apdev[0], params)
f9dd43ea 5718 check_tls_session_resumption_capa(dev[0], hapd)
3b3e2687 5719 eap_connect(dev[0], hapd, "TTLS", "user",
f9dd43ea
JM
5720 anonymous_identity="ttls", password="password",
5721 ca_cert="auth_serv/ca.pem", phase2="autheap=GTC")
5722 if dev[0].get_status_field("tls_session_reused") != '0':
5723 raise Exception("Unexpected session resumption on the first connection")
5724
5725 dev[0].request("REAUTHENTICATE")
5726 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
5727 if ev is None:
5728 raise Exception("EAP success timed out")
5729 ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=10)
5730 if ev is None:
5731 raise Exception("Key handshake with the AP timed out")
5732 if dev[0].get_status_field("tls_session_reused") != '1':
5733 raise Exception("Session resumption not used on the second connection")
5734
5735def test_eap_ttls_no_session_resumption(dev, apdev):
5736 """EAP-TTLS session resumption disabled on server"""
5737 params = int_eap_server_params()
5738 params['tls_session_lifetime'] = '0'
8b8a1864 5739 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 5740 eap_connect(dev[0], hapd, "TTLS", "pap user",
f9dd43ea
JM
5741 anonymous_identity="ttls", password="password",
5742 ca_cert="auth_serv/ca.pem", eap_workaround='0',
5743 phase2="auth=PAP")
5744 if dev[0].get_status_field("tls_session_reused") != '0':
5745 raise Exception("Unexpected session resumption on the first connection")
5746
5747 dev[0].request("REAUTHENTICATE")
5748 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
5749 if ev is None:
5750 raise Exception("EAP success timed out")
5751 ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=10)
5752 if ev is None:
5753 raise Exception("Key handshake with the AP timed out")
5754 if dev[0].get_status_field("tls_session_reused") != '0':
5755 raise Exception("Unexpected session resumption on the second connection")
5756
5757def test_eap_peap_session_resumption(dev, apdev):
5758 """EAP-PEAP session resumption"""
ead550b9 5759 check_eap_capa(dev[0], "MSCHAPV2")
f9dd43ea
JM
5760 params = int_eap_server_params()
5761 params['tls_session_lifetime'] = '60'
8b8a1864 5762 hapd = hostapd.add_ap(apdev[0], params)
f9dd43ea 5763 check_tls_session_resumption_capa(dev[0], hapd)
3b3e2687 5764 eap_connect(dev[0], hapd, "PEAP", "user",
f9dd43ea
JM
5765 anonymous_identity="peap", password="password",
5766 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2")
5767 if dev[0].get_status_field("tls_session_reused") != '0':
5768 raise Exception("Unexpected session resumption on the first connection")
5769
5770 dev[0].request("REAUTHENTICATE")
5771 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
5772 if ev is None:
5773 raise Exception("EAP success timed out")
5774 ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=10)
5775 if ev is None:
5776 raise Exception("Key handshake with the AP timed out")
5777 if dev[0].get_status_field("tls_session_reused") != '1':
5778 raise Exception("Session resumption not used on the second connection")
5779
81e1ab85
JM
5780def test_eap_peap_session_resumption_crypto_binding(dev, apdev):
5781 """EAP-PEAP session resumption with crypto binding"""
5782 params = int_eap_server_params()
5783 params['tls_session_lifetime'] = '60'
8b8a1864 5784 hapd = hostapd.add_ap(apdev[0], params)
81e1ab85 5785 check_tls_session_resumption_capa(dev[0], hapd)
3b3e2687 5786 eap_connect(dev[0], hapd, "PEAP", "user",
81e1ab85
JM
5787 anonymous_identity="peap", password="password",
5788 phase1="peapver=0 crypto_binding=2",
5789 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2")
5790 if dev[0].get_status_field("tls_session_reused") != '0':
5791 raise Exception("Unexpected session resumption on the first connection")
5792
5793 dev[0].request("REAUTHENTICATE")
5794 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
5795 if ev is None:
5796 raise Exception("EAP success timed out")
5797 ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=10)
5798 if ev is None:
5799 raise Exception("Key handshake with the AP timed out")
5800 if dev[0].get_status_field("tls_session_reused") != '1':
5801 raise Exception("Session resumption not used on the second connection")
5802
f9dd43ea
JM
5803def test_eap_peap_no_session_resumption(dev, apdev):
5804 """EAP-PEAP session resumption disabled on server"""
5805 params = int_eap_server_params()
8b8a1864 5806 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 5807 eap_connect(dev[0], hapd, "PEAP", "user",
f9dd43ea
JM
5808 anonymous_identity="peap", password="password",
5809 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2")
5810 if dev[0].get_status_field("tls_session_reused") != '0':
5811 raise Exception("Unexpected session resumption on the first connection")
5812
5813 dev[0].request("REAUTHENTICATE")
5814 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
5815 if ev is None:
5816 raise Exception("EAP success timed out")
5817 ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=10)
5818 if ev is None:
5819 raise Exception("Key handshake with the AP timed out")
5820 if dev[0].get_status_field("tls_session_reused") != '0':
5821 raise Exception("Unexpected session resumption on the second connection")
5822
5823def test_eap_tls_session_resumption(dev, apdev):
5824 """EAP-TLS session resumption"""
5825 params = int_eap_server_params()
5826 params['tls_session_lifetime'] = '60'
8b8a1864 5827 hapd = hostapd.add_ap(apdev[0], params)
f9dd43ea 5828 check_tls_session_resumption_capa(dev[0], hapd)
3b3e2687 5829 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
f9dd43ea
JM
5830 client_cert="auth_serv/user.pem",
5831 private_key="auth_serv/user.key")
5832 if dev[0].get_status_field("tls_session_reused") != '0':
5833 raise Exception("Unexpected session resumption on the first connection")
5834
5835 dev[0].request("REAUTHENTICATE")
5836 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
5837 if ev is None:
5838 raise Exception("EAP success timed out")
5839 ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=10)
5840 if ev is None:
5841 raise Exception("Key handshake with the AP timed out")
5842 if dev[0].get_status_field("tls_session_reused") != '1':
5843 raise Exception("Session resumption not used on the second connection")
5844
5845 dev[0].request("REAUTHENTICATE")
5846 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
5847 if ev is None:
5848 raise Exception("EAP success timed out")
5849 ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=10)
5850 if ev is None:
5851 raise Exception("Key handshake with the AP timed out")
5852 if dev[0].get_status_field("tls_session_reused") != '1':
5853 raise Exception("Session resumption not used on the third connection")
5854
5855def test_eap_tls_session_resumption_expiration(dev, apdev):
5856 """EAP-TLS session resumption"""
5857 params = int_eap_server_params()
5858 params['tls_session_lifetime'] = '1'
8b8a1864 5859 hapd = hostapd.add_ap(apdev[0], params)
f9dd43ea 5860 check_tls_session_resumption_capa(dev[0], hapd)
3b3e2687 5861 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
f9dd43ea
JM
5862 client_cert="auth_serv/user.pem",
5863 private_key="auth_serv/user.key")
5864 if dev[0].get_status_field("tls_session_reused") != '0':
5865 raise Exception("Unexpected session resumption on the first connection")
5866
5867 # Allow multiple attempts since OpenSSL may not expire the cached entry
5868 # immediately.
5869 for i in range(10):
5870 time.sleep(1.2)
5871
5872 dev[0].request("REAUTHENTICATE")
5873 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
5874 if ev is None:
5875 raise Exception("EAP success timed out")
5876 ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=10)
5877 if ev is None:
5878 raise Exception("Key handshake with the AP timed out")
5879 if dev[0].get_status_field("tls_session_reused") == '0':
5880 break
5881 if dev[0].get_status_field("tls_session_reused") != '0':
5882 raise Exception("Session resumption used after lifetime expiration")
5883
5884def test_eap_tls_no_session_resumption(dev, apdev):
5885 """EAP-TLS session resumption disabled on server"""
5886 params = int_eap_server_params()
8b8a1864 5887 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 5888 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
f9dd43ea
JM
5889 client_cert="auth_serv/user.pem",
5890 private_key="auth_serv/user.key")
5891 if dev[0].get_status_field("tls_session_reused") != '0':
5892 raise Exception("Unexpected session resumption on the first connection")
5893
5894 dev[0].request("REAUTHENTICATE")
5895 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
5896 if ev is None:
5897 raise Exception("EAP success timed out")
5898 ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=10)
5899 if ev is None:
5900 raise Exception("Key handshake with the AP timed out")
5901 if dev[0].get_status_field("tls_session_reused") != '0':
5902 raise Exception("Unexpected session resumption on the second connection")
5903
5904def test_eap_tls_session_resumption_radius(dev, apdev):
5905 """EAP-TLS session resumption (RADIUS)"""
fab49f61
JM
5906 params = {"ssid": "as", "beacon_int": "2000",
5907 "radius_server_clients": "auth_serv/radius_clients.conf",
5908 "radius_server_auth_port": '18128',
5909 "eap_server": "1",
5910 "eap_user_file": "auth_serv/eap_user.conf",
5911 "ca_cert": "auth_serv/ca.pem",
5912 "server_cert": "auth_serv/server.pem",
5913 "private_key": "auth_serv/server.key",
5914 "tls_session_lifetime": "60"}
8b8a1864 5915 authsrv = hostapd.add_ap(apdev[1], params)
f9dd43ea
JM
5916 check_tls_session_resumption_capa(dev[0], authsrv)
5917
5918 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
5919 params['auth_server_port'] = "18128"
8b8a1864 5920 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 5921 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
f9dd43ea
JM
5922 client_cert="auth_serv/user.pem",
5923 private_key="auth_serv/user.key")
5924 if dev[0].get_status_field("tls_session_reused") != '0':
5925 raise Exception("Unexpected session resumption on the first connection")
5926
5927 dev[0].request("REAUTHENTICATE")
5928 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
5929 if ev is None:
5930 raise Exception("EAP success timed out")
5931 ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=10)
5932 if ev is None:
5933 raise Exception("Key handshake with the AP timed out")
5934 if dev[0].get_status_field("tls_session_reused") != '1':
5935 raise Exception("Session resumption not used on the second connection")
5936
5937def test_eap_tls_no_session_resumption_radius(dev, apdev):
5938 """EAP-TLS session resumption disabled (RADIUS)"""
fab49f61
JM
5939 params = {"ssid": "as", "beacon_int": "2000",
5940 "radius_server_clients": "auth_serv/radius_clients.conf",
5941 "radius_server_auth_port": '18128',
5942 "eap_server": "1",
5943 "eap_user_file": "auth_serv/eap_user.conf",
5944 "ca_cert": "auth_serv/ca.pem",
5945 "server_cert": "auth_serv/server.pem",
5946 "private_key": "auth_serv/server.key",
5947 "tls_session_lifetime": "0"}
8b8a1864 5948 hostapd.add_ap(apdev[1], params)
f9dd43ea
JM
5949
5950 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
5951 params['auth_server_port'] = "18128"
8b8a1864 5952 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 5953 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
f9dd43ea
JM
5954 client_cert="auth_serv/user.pem",
5955 private_key="auth_serv/user.key")
5956 if dev[0].get_status_field("tls_session_reused") != '0':
5957 raise Exception("Unexpected session resumption on the first connection")
5958
5959 dev[0].request("REAUTHENTICATE")
5960 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
5961 if ev is None:
5962 raise Exception("EAP success timed out")
5963 ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=10)
5964 if ev is None:
5965 raise Exception("Key handshake with the AP timed out")
5966 if dev[0].get_status_field("tls_session_reused") != '0':
5967 raise Exception("Unexpected session resumption on the second connection")
7c0d66cf
JM
5968
5969def test_eap_mschapv2_errors(dev, apdev):
5970 """EAP-MSCHAPv2 error cases"""
5971 check_eap_capa(dev[0], "MSCHAPV2")
5972 check_eap_capa(dev[0], "FAST")
5973
5974 params = hostapd.wpa2_eap_params(ssid="test-wpa-eap")
8b8a1864 5975 hapd = hostapd.add_ap(apdev[0], params)
7c0d66cf
JM
5976 dev[0].connect("test-wpa-eap", key_mgmt="WPA-EAP", eap="MSCHAPV2",
5977 identity="phase1-user", password="password",
5978 scan_freq="2412")
5979 dev[0].request("REMOVE_NETWORK all")
5980 dev[0].wait_disconnected()
5981
fab49f61
JM
5982 tests = [(1, "hash_nt_password_hash;mschapv2_derive_response"),
5983 (1, "nt_password_hash;mschapv2_derive_response"),
5984 (1, "nt_password_hash;=mschapv2_derive_response"),
5985 (1, "generate_nt_response;mschapv2_derive_response"),
5986 (1, "generate_authenticator_response;mschapv2_derive_response"),
5987 (1, "nt_password_hash;=mschapv2_derive_response"),
5988 (1, "get_master_key;mschapv2_derive_response"),
5989 (1, "os_get_random;eap_mschapv2_challenge_reply")]
7c0d66cf
JM
5990 for count, func in tests:
5991 with fail_test(dev[0], count, func):
5992 dev[0].connect("test-wpa-eap", key_mgmt="WPA-EAP", eap="MSCHAPV2",
5993 identity="phase1-user", password="password",
5994 wait_connect=False, scan_freq="2412")
5995 wait_fail_trigger(dev[0], "GET_FAIL")
5996 dev[0].request("REMOVE_NETWORK all")
5997 dev[0].wait_disconnected()
5998
fab49f61
JM
5999 tests = [(1, "hash_nt_password_hash;mschapv2_derive_response"),
6000 (1, "hash_nt_password_hash;=mschapv2_derive_response"),
6001 (1, "generate_nt_response_pwhash;mschapv2_derive_response"),
6002 (1, "generate_authenticator_response_pwhash;mschapv2_derive_response")]
7c0d66cf
JM
6003 for count, func in tests:
6004 with fail_test(dev[0], count, func):
6005 dev[0].connect("test-wpa-eap", key_mgmt="WPA-EAP", eap="MSCHAPV2",
6006 identity="phase1-user",
6007 password_hex="hash:8846f7eaee8fb117ad06bdd830b7586c",
6008 wait_connect=False, scan_freq="2412")
6009 wait_fail_trigger(dev[0], "GET_FAIL")
6010 dev[0].request("REMOVE_NETWORK all")
6011 dev[0].wait_disconnected()
6012
fab49f61
JM
6013 tests = [(1, "eap_mschapv2_init"),
6014 (1, "eap_msg_alloc;eap_mschapv2_challenge_reply"),
6015 (1, "eap_msg_alloc;eap_mschapv2_success"),
6016 (1, "eap_mschapv2_getKey")]
7c0d66cf
JM
6017 for count, func in tests:
6018 with alloc_fail(dev[0], count, func):
6019 dev[0].connect("test-wpa-eap", key_mgmt="WPA-EAP", eap="MSCHAPV2",
6020 identity="phase1-user", password="password",
6021 wait_connect=False, scan_freq="2412")
6022 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
6023 dev[0].request("REMOVE_NETWORK all")
6024 dev[0].wait_disconnected()
6025
fab49f61 6026 tests = [(1, "eap_msg_alloc;eap_mschapv2_failure")]
7c0d66cf
JM
6027 for count, func in tests:
6028 with alloc_fail(dev[0], count, func):
6029 dev[0].connect("test-wpa-eap", key_mgmt="WPA-EAP", eap="MSCHAPV2",
6030 identity="phase1-user", password="wrong password",
6031 wait_connect=False, scan_freq="2412")
6032 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
6033 dev[0].request("REMOVE_NETWORK all")
6034 dev[0].wait_disconnected()
6035
fab49f61
JM
6036 tests = [(2, "eap_mschapv2_init"),
6037 (3, "eap_mschapv2_init")]
7c0d66cf
JM
6038 for count, func in tests:
6039 with alloc_fail(dev[0], count, func):
6040 dev[0].connect("test-wpa-eap", key_mgmt="WPA-EAP", eap="FAST",
6041 anonymous_identity="FAST", identity="user",
6042 password="password",
6043 ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
6044 phase1="fast_provisioning=1",
6045 pac_file="blob://fast_pac",
6046 wait_connect=False, scan_freq="2412")
6047 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
6048 dev[0].request("REMOVE_NETWORK all")
6049 dev[0].wait_disconnected()
bf0ec17a
JM
6050
6051def test_eap_gpsk_errors(dev, apdev):
6052 """EAP-GPSK error cases"""
6053 params = hostapd.wpa2_eap_params(ssid="test-wpa-eap")
8b8a1864 6054 hapd = hostapd.add_ap(apdev[0], params)
bf0ec17a
JM
6055 dev[0].connect("test-wpa-eap", key_mgmt="WPA-EAP", eap="GPSK",
6056 identity="gpsk user",
6057 password="abcdefghijklmnop0123456789abcdef",
6058 scan_freq="2412")
6059 dev[0].request("REMOVE_NETWORK all")
6060 dev[0].wait_disconnected()
6061
fab49f61
JM
6062 tests = [(1, "os_get_random;eap_gpsk_send_gpsk_2", None),
6063 (1, "eap_gpsk_derive_session_id;eap_gpsk_send_gpsk_2",
6064 "cipher=1"),
6065 (1, "eap_gpsk_derive_session_id;eap_gpsk_send_gpsk_2",
6066 "cipher=2"),
6067 (1, "eap_gpsk_derive_keys_helper", None),
6068 (2, "eap_gpsk_derive_keys_helper", None),
6069 (1, "eap_gpsk_compute_mic_aes;eap_gpsk_compute_mic;eap_gpsk_send_gpsk_2",
6070 "cipher=1"),
6071 (1, "hmac_sha256;eap_gpsk_compute_mic;eap_gpsk_send_gpsk_2",
6072 "cipher=2"),
6073 (1, "eap_gpsk_compute_mic;eap_gpsk_validate_gpsk_3_mic", None),
6074 (1, "eap_gpsk_compute_mic;eap_gpsk_send_gpsk_4", None),
6075 (1, "eap_gpsk_derive_mid_helper", None)]
bf0ec17a
JM
6076 for count, func, phase1 in tests:
6077 with fail_test(dev[0], count, func):
6078 dev[0].connect("test-wpa-eap", key_mgmt="WPA-EAP", eap="GPSK",
6079 identity="gpsk user",
6080 password="abcdefghijklmnop0123456789abcdef",
6081 phase1=phase1,
6082 wait_connect=False, scan_freq="2412")
6083 wait_fail_trigger(dev[0], "GET_FAIL")
6084 dev[0].request("REMOVE_NETWORK all")
6085 dev[0].wait_disconnected()
6086
fab49f61
JM
6087 tests = [(1, "eap_gpsk_init"),
6088 (2, "eap_gpsk_init"),
6089 (3, "eap_gpsk_init"),
6090 (1, "eap_gpsk_process_id_server"),
6091 (1, "eap_msg_alloc;eap_gpsk_send_gpsk_2"),
6092 (1, "eap_gpsk_derive_session_id;eap_gpsk_send_gpsk_2"),
6093 (1, "eap_gpsk_derive_mid_helper;eap_gpsk_derive_session_id;eap_gpsk_send_gpsk_2"),
6094 (1, "eap_gpsk_derive_keys"),
6095 (1, "eap_gpsk_derive_keys_helper"),
6096 (1, "eap_msg_alloc;eap_gpsk_send_gpsk_4"),
6097 (1, "eap_gpsk_getKey"),
6098 (1, "eap_gpsk_get_emsk"),
6099 (1, "eap_gpsk_get_session_id")]
bf0ec17a
JM
6100 for count, func in tests:
6101 with alloc_fail(dev[0], count, func):
6102 dev[0].request("ERP_FLUSH")
6103 dev[0].connect("test-wpa-eap", key_mgmt="WPA-EAP", eap="GPSK",
0a0c4dc1 6104 identity="gpsk user@domain", erp="1",
bf0ec17a
JM
6105 password="abcdefghijklmnop0123456789abcdef",
6106 wait_connect=False, scan_freq="2412")
6107 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
6108 dev[0].request("REMOVE_NETWORK all")
6109 dev[0].wait_disconnected()
d4c3c055
JM
6110
6111def test_ap_wpa2_eap_sim_db(dev, apdev, params):
6112 """EAP-SIM DB error cases"""
6113 sockpath = '/tmp/hlr_auc_gw.sock-test'
6114 try:
6115 os.remove(sockpath)
6116 except:
6117 pass
6118 hparams = int_eap_server_params()
6119 hparams['eap_sim_db'] = 'unix:' + sockpath
8b8a1864 6120 hapd = hostapd.add_ap(apdev[0], hparams)
d4c3c055
JM
6121
6122 # Initial test with hlr_auc_gw socket not available
6123 id = dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP WPA-EAP-SHA256",
6124 eap="SIM", identity="1232010000000000",
6125 password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581",
6126 scan_freq="2412", wait_connect=False)
5a30fcf5
JM
6127 ev = dev[0].wait_event(["EAP-ERROR-CODE"], timeout=10)
6128 if ev is None:
6129 raise Exception("EAP method specific error code not reported")
6130 if int(ev.split()[1]) != 16384:
6131 raise Exception("Unexpected EAP method specific error code: " + ev)
d4c3c055
JM
6132 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
6133 if ev is None:
6134 raise Exception("EAP-Failure not reported")
6135 dev[0].wait_disconnected()
6136 dev[0].request("DISCONNECT")
6137
6138 # Test with invalid responses and response timeout
6139
6140 class test_handler(SocketServer.DatagramRequestHandler):
6141 def handle(self):
cc02ce96 6142 data = self.request[0].decode().strip()
d4c3c055
JM
6143 socket = self.request[1]
6144 logger.debug("Received hlr_auc_gw request: " + data)
6145 # EAP-SIM DB: Failed to parse response string
cc02ce96 6146 socket.sendto(b"FOO", self.client_address)
d4c3c055 6147 # EAP-SIM DB: Failed to parse response string
cc02ce96 6148 socket.sendto(b"FOO 1", self.client_address)
d4c3c055 6149 # EAP-SIM DB: Unknown external response
cc02ce96 6150 socket.sendto(b"FOO 1 2", self.client_address)
d4c3c055
JM
6151 logger.info("No proper response - wait for pending eap_sim_db request timeout")
6152
6153 server = SocketServer.UnixDatagramServer(sockpath, test_handler)
6154 server.timeout = 1
6155
6156 dev[0].select_network(id)
6157 server.handle_request()
6158 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
6159 if ev is None:
6160 raise Exception("EAP-Failure not reported")
6161 dev[0].wait_disconnected()
6162 dev[0].request("DISCONNECT")
6163
6164 # Test with a valid response
6165
6166 class test_handler2(SocketServer.DatagramRequestHandler):
6167 def handle(self):
cc02ce96 6168 data = self.request[0].decode().strip()
d4c3c055
JM
6169 socket = self.request[1]
6170 logger.debug("Received hlr_auc_gw request: " + data)
6171 fname = os.path.join(params['logdir'],
6172 'hlr_auc_gw.milenage_db')
6173 cmd = subprocess.Popen(['../../hostapd/hlr_auc_gw',
6174 '-m', fname, data],
6175 stdout=subprocess.PIPE)
04fa9fc7 6176 res = cmd.stdout.read().decode().strip()
d4c3c055
JM
6177 cmd.stdout.close()
6178 logger.debug("hlr_auc_gw response: " + res)
cc02ce96 6179 socket.sendto(res.encode(), self.client_address)
d4c3c055
JM
6180
6181 server.RequestHandlerClass = test_handler2
6182
6183 dev[0].select_network(id)
6184 server.handle_request()
6185 dev[0].wait_connected()
6186 dev[0].request("DISCONNECT")
6187 dev[0].wait_disconnected()
d6ba709a
JM
6188
6189def test_eap_tls_sha512(dev, apdev, params):
6190 """EAP-TLS with SHA512 signature"""
6191 params = int_eap_server_params()
6192 params["ca_cert"] = "auth_serv/sha512-ca.pem"
6193 params["server_cert"] = "auth_serv/sha512-server.pem"
6194 params["private_key"] = "auth_serv/sha512-server.key"
8b8a1864 6195 hostapd.add_ap(apdev[0], params)
d6ba709a
JM
6196
6197 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
6198 identity="tls user sha512",
6199 ca_cert="auth_serv/sha512-ca.pem",
6200 client_cert="auth_serv/sha512-user.pem",
6201 private_key="auth_serv/sha512-user.key",
6202 scan_freq="2412")
6203 dev[1].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
6204 identity="tls user sha512",
6205 ca_cert="auth_serv/sha512-ca.pem",
6206 client_cert="auth_serv/sha384-user.pem",
6207 private_key="auth_serv/sha384-user.key",
6208 scan_freq="2412")
6209
6210def test_eap_tls_sha384(dev, apdev, params):
6211 """EAP-TLS with SHA384 signature"""
6212 params = int_eap_server_params()
6213 params["ca_cert"] = "auth_serv/sha512-ca.pem"
6214 params["server_cert"] = "auth_serv/sha384-server.pem"
6215 params["private_key"] = "auth_serv/sha384-server.key"
8b8a1864 6216 hostapd.add_ap(apdev[0], params)
d6ba709a
JM
6217
6218 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
6219 identity="tls user sha512",
6220 ca_cert="auth_serv/sha512-ca.pem",
6221 client_cert="auth_serv/sha512-user.pem",
6222 private_key="auth_serv/sha512-user.key",
6223 scan_freq="2412")
6224 dev[1].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
6225 identity="tls user sha512",
6226 ca_cert="auth_serv/sha512-ca.pem",
6227 client_cert="auth_serv/sha384-user.pem",
6228 private_key="auth_serv/sha384-user.key",
6229 scan_freq="2412")
0ceff76e
JM
6230
6231def test_ap_wpa2_eap_assoc_rsn(dev, apdev):
6232 """WPA2-Enterprise AP and association request RSN IE differences"""
6233 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 6234 hostapd.add_ap(apdev[0], params)
0ceff76e
JM
6235
6236 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap-11w")
6237 params["ieee80211w"] = "2"
8b8a1864 6238 hostapd.add_ap(apdev[1], params)
0ceff76e
JM
6239
6240 # Success cases with optional RSN IE fields removed one by one
fab49f61
JM
6241 tests = [("Normal wpa_supplicant assoc req RSN IE",
6242 "30140100000fac040100000fac040100000fac010000"),
6243 ("Extra PMKIDCount field in RSN IE",
6244 "30160100000fac040100000fac040100000fac0100000000"),
6245 ("Extra Group Management Cipher Suite in RSN IE",
6246 "301a0100000fac040100000fac040100000fac0100000000000fac06"),
6247 ("Extra undefined extension field in RSN IE",
6248 "301c0100000fac040100000fac040100000fac0100000000000fac061122"),
6249 ("RSN IE without RSN Capabilities",
6250 "30120100000fac040100000fac040100000fac01"),
6251 ("RSN IE without AKM", "300c0100000fac040100000fac04"),
6252 ("RSN IE without pairwise", "30060100000fac04"),
6253 ("RSN IE without group", "30020100")]
0ceff76e
JM
6254 for title, ie in tests:
6255 logger.info(title)
6256 set_test_assoc_ie(dev[0], ie)
6257 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="GPSK",
6258 identity="gpsk user",
6259 password="abcdefghijklmnop0123456789abcdef",
6260 scan_freq="2412")
6261 dev[0].request("REMOVE_NETWORK all")
6262 dev[0].wait_disconnected()
6263
fab49f61
JM
6264 tests = [("Normal wpa_supplicant assoc req RSN IE",
6265 "30140100000fac040100000fac040100000fac01cc00"),
6266 ("Group management cipher included in assoc req RSN IE",
6267 "301a0100000fac040100000fac040100000fac01cc000000000fac06")]
0ceff76e
JM
6268 for title, ie in tests:
6269 logger.info(title)
6270 set_test_assoc_ie(dev[0], ie)
6271 dev[0].connect("test-wpa2-eap-11w", key_mgmt="WPA-EAP", ieee80211w="1",
6272 eap="GPSK", identity="gpsk user",
6273 password="abcdefghijklmnop0123456789abcdef",
6274 scan_freq="2412")
6275 dev[0].request("REMOVE_NETWORK all")
6276 dev[0].wait_disconnected()
6277
fab49f61
JM
6278 tests = [("Invalid group cipher", "30060100000fac02", 41),
6279 ("Invalid pairwise cipher", "300c0100000fac040100000fac02", 42)]
0ceff76e
JM
6280 for title, ie, status in tests:
6281 logger.info(title)
6282 set_test_assoc_ie(dev[0], ie)
6283 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="GPSK",
6284 identity="gpsk user",
6285 password="abcdefghijklmnop0123456789abcdef",
6286 scan_freq="2412", wait_connect=False)
6287 ev = dev[0].wait_event(["CTRL-EVENT-ASSOC-REJECT"])
6288 if ev is None:
6289 raise Exception("Association rejection not reported")
6290 if "status_code=" + str(status) not in ev:
6291 raise Exception("Unexpected status code: " + ev)
6292 dev[0].request("REMOVE_NETWORK all")
6293 dev[0].dump_monitor()
6294
fab49f61
JM
6295 tests = [("Management frame protection not enabled",
6296 "30140100000fac040100000fac040100000fac010000", 31),
6297 ("Unsupported management group cipher",
6298 "301a0100000fac040100000fac040100000fac01cc000000000fac0b", 46)]
0ceff76e
JM
6299 for title, ie, status in tests:
6300 logger.info(title)
6301 set_test_assoc_ie(dev[0], ie)
6302 dev[0].connect("test-wpa2-eap-11w", key_mgmt="WPA-EAP", ieee80211w="1",
6303 eap="GPSK", identity="gpsk user",
6304 password="abcdefghijklmnop0123456789abcdef",
6305 scan_freq="2412", wait_connect=False)
6306 ev = dev[0].wait_event(["CTRL-EVENT-ASSOC-REJECT"])
6307 if ev is None:
6308 raise Exception("Association rejection not reported")
6309 if "status_code=" + str(status) not in ev:
6310 raise Exception("Unexpected status code: " + ev)
6311 dev[0].request("REMOVE_NETWORK all")
6312 dev[0].dump_monitor()
ca27ee09
JM
6313
6314def test_eap_tls_ext_cert_check(dev, apdev):
6315 """EAP-TLS and external server certification validation"""
6316 # With internal server certificate chain validation
6317 id = dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
6318 identity="tls user",
6319 ca_cert="auth_serv/ca.pem",
6320 client_cert="auth_serv/user.pem",
6321 private_key="auth_serv/user.key",
6322 phase1="tls_ext_cert_check=1", scan_freq="2412",
6323 only_add_network=True)
6324 run_ext_cert_check(dev, apdev, id)
6325
6326def test_eap_ttls_ext_cert_check(dev, apdev):
6327 """EAP-TTLS and external server certification validation"""
6328 # Without internal server certificate chain validation
6329 id = dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
6330 identity="pap user", anonymous_identity="ttls",
6331 password="password", phase2="auth=PAP",
6332 phase1="tls_ext_cert_check=1", scan_freq="2412",
6333 only_add_network=True)
6334 run_ext_cert_check(dev, apdev, id)
6335
6336def test_eap_peap_ext_cert_check(dev, apdev):
6337 """EAP-PEAP and external server certification validation"""
6338 # With internal server certificate chain validation
6339 id = dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="PEAP",
6340 identity="user", anonymous_identity="peap",
6341 ca_cert="auth_serv/ca.pem",
6342 password="password", phase2="auth=MSCHAPV2",
6343 phase1="tls_ext_cert_check=1", scan_freq="2412",
6344 only_add_network=True)
6345 run_ext_cert_check(dev, apdev, id)
6346
6347def test_eap_fast_ext_cert_check(dev, apdev):
6348 """EAP-FAST and external server certification validation"""
6349 check_eap_capa(dev[0], "FAST")
6350 # With internal server certificate chain validation
6351 dev[0].request("SET blob fast_pac_auth_ext ")
6352 id = dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="FAST",
6353 identity="user", anonymous_identity="FAST",
6354 ca_cert="auth_serv/ca.pem",
6355 password="password", phase2="auth=GTC",
6356 phase1="tls_ext_cert_check=1 fast_provisioning=2",
6357 pac_file="blob://fast_pac_auth_ext",
6358 scan_freq="2412",
6359 only_add_network=True)
6360 run_ext_cert_check(dev, apdev, id)
6361
6362def run_ext_cert_check(dev, apdev, net_id):
6363 check_ext_cert_check_support(dev[0])
6364 if not openssl_imported:
6365 raise HwsimSkip("OpenSSL python method not available")
6366
6367 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 6368 hapd = hostapd.add_ap(apdev[0], params)
ca27ee09
JM
6369
6370 dev[0].select_network(net_id)
6371 certs = {}
6372 while True:
6373 ev = dev[0].wait_event(["CTRL-EVENT-EAP-PEER-CERT",
6374 "CTRL-REQ-EXT_CERT_CHECK",
6375 "CTRL-EVENT-EAP-SUCCESS"], timeout=10)
6376 if ev is None:
6377 raise Exception("No peer server certificate event seen")
6378 if "CTRL-EVENT-EAP-PEER-CERT" in ev:
6379 depth = None
6380 cert = None
6381 vals = ev.split(' ')
6382 for v in vals:
6383 if v.startswith("depth="):
6384 depth = int(v.split('=')[1])
6385 elif v.startswith("cert="):
6386 cert = v.split('=')[1]
6387 if depth is not None and cert:
6388 certs[depth] = binascii.unhexlify(cert)
6389 elif "CTRL-EVENT-EAP-SUCCESS" in ev:
6390 raise Exception("Unexpected EAP-Success")
6391 elif "CTRL-REQ-EXT_CERT_CHECK" in ev:
6392 id = ev.split(':')[0].split('-')[-1]
6393 break
6394 if 0 not in certs:
6395 raise Exception("Server certificate not received")
6396 if 1 not in certs:
6397 raise Exception("Server certificate issuer not received")
6398
6399 cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_ASN1,
6400 certs[0])
6401 cn = cert.get_subject().commonName
6402 logger.info("Server certificate CN=" + cn)
6403
6404 issuer = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_ASN1,
6405 certs[1])
6406 icn = issuer.get_subject().commonName
6407 logger.info("Issuer certificate CN=" + icn)
6408
6409 if cn != "server.w1.fi":
6410 raise Exception("Unexpected server certificate CN: " + cn)
6411 if icn != "Root CA":
6412 raise Exception("Unexpected server certificate issuer CN: " + icn)
6413
6414 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=0.1)
6415 if ev:
6416 raise Exception("Unexpected EAP-Success before external check result indication")
6417
6418 dev[0].request("CTRL-RSP-EXT_CERT_CHECK-" + id + ":good")
6419 dev[0].wait_connected()
6420
6421 dev[0].request("DISCONNECT")
6422 dev[0].wait_disconnected()
6423 if "FAIL" in dev[0].request("PMKSA_FLUSH"):
6424 raise Exception("PMKSA_FLUSH failed")
6425 dev[0].request("SET blob fast_pac_auth_ext ")
6426 dev[0].request("RECONNECT")
6427
6428 ev = dev[0].wait_event(["CTRL-REQ-EXT_CERT_CHECK"], timeout=10)
6429 if ev is None:
6430 raise Exception("No peer server certificate event seen (2)")
6431 id = ev.split(':')[0].split('-')[-1]
6432 dev[0].request("CTRL-RSP-EXT_CERT_CHECK-" + id + ":bad")
6433 ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
6434 if ev is None:
6435 raise Exception("EAP-Failure not reported")
6436 dev[0].request("REMOVE_NETWORK all")
6437 dev[0].wait_disconnected()
a89faedc
JM
6438
6439def test_eap_tls_errors(dev, apdev):
6440 """EAP-TLS error cases"""
6441 params = int_eap_server_params()
6442 params['fragment_size'] = '100'
8b8a1864 6443 hostapd.add_ap(apdev[0], params)
a89faedc
JM
6444 with alloc_fail(dev[0], 1,
6445 "eap_peer_tls_reassemble_fragment"):
6446 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
6447 identity="tls user", ca_cert="auth_serv/ca.pem",
6448 client_cert="auth_serv/user.pem",
6449 private_key="auth_serv/user.key",
6450 wait_connect=False, scan_freq="2412")
6451 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
6452 dev[0].request("REMOVE_NETWORK all")
6453 dev[0].wait_disconnected()
6454
6455 with alloc_fail(dev[0], 1, "eap_tls_init"):
6456 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
6457 identity="tls user", ca_cert="auth_serv/ca.pem",
6458 client_cert="auth_serv/user.pem",
6459 private_key="auth_serv/user.key",
6460 wait_connect=False, scan_freq="2412")
6461 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
6462 dev[0].request("REMOVE_NETWORK all")
6463 dev[0].wait_disconnected()
6464
6465 with alloc_fail(dev[0], 1, "eap_peer_tls_ssl_init"):
6466 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
6467 identity="tls user", ca_cert="auth_serv/ca.pem",
6468 client_cert="auth_serv/user.pem",
6469 private_key="auth_serv/user.key",
6470 engine="1",
6471 wait_connect=False, scan_freq="2412")
6472 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
6473 ev = dev[0].wait_event(["CTRL-REQ-PIN"], timeout=5)
6474 if ev is None:
6475 raise Exception("No CTRL-REQ-PIN seen")
6476 dev[0].request("REMOVE_NETWORK all")
6477 dev[0].wait_disconnected()
6478
fab49f61
JM
6479 tests = ["eap_peer_tls_derive_key;eap_tls_success",
6480 "eap_peer_tls_derive_session_id;eap_tls_success",
6481 "eap_tls_getKey",
6482 "eap_tls_get_emsk",
6483 "eap_tls_get_session_id"]
a89faedc
JM
6484 for func in tests:
6485 with alloc_fail(dev[0], 1, func):
6486 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TLS",
0a0c4dc1
JM
6487 identity="tls user@domain",
6488 ca_cert="auth_serv/ca.pem",
a89faedc
JM
6489 client_cert="auth_serv/user.pem",
6490 private_key="auth_serv/user.key",
6491 erp="1",
6492 wait_connect=False, scan_freq="2412")
6493 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
6494 dev[0].request("REMOVE_NETWORK all")
6495 dev[0].wait_disconnected()
6496
6497 with alloc_fail(dev[0], 1, "eap_unauth_tls_init"):
6498 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="UNAUTH-TLS",
6499 identity="unauth-tls", ca_cert="auth_serv/ca.pem",
6500 wait_connect=False, scan_freq="2412")
6501 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
6502 dev[0].request("REMOVE_NETWORK all")
6503 dev[0].wait_disconnected()
6504
6505 with alloc_fail(dev[0], 1, "eap_peer_tls_ssl_init;eap_unauth_tls_init"):
6506 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="UNAUTH-TLS",
6507 identity="unauth-tls", ca_cert="auth_serv/ca.pem",
6508 wait_connect=False, scan_freq="2412")
6509 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
6510 dev[0].request("REMOVE_NETWORK all")
6511 dev[0].wait_disconnected()
6512
6513 with alloc_fail(dev[0], 1, "eap_wfa_unauth_tls_init"):
6514 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP",
6515 eap="WFA-UNAUTH-TLS",
6516 identity="osen@example.com", ca_cert="auth_serv/ca.pem",
6517 wait_connect=False, scan_freq="2412")
6518 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
6519 dev[0].request("REMOVE_NETWORK all")
6520 dev[0].wait_disconnected()
6521
6522 with alloc_fail(dev[0], 1, "eap_peer_tls_ssl_init;eap_wfa_unauth_tls_init"):
6523 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP",
6524 eap="WFA-UNAUTH-TLS",
6525 identity="osen@example.com", ca_cert="auth_serv/ca.pem",
6526 wait_connect=False, scan_freq="2412")
6527 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
6528 dev[0].request("REMOVE_NETWORK all")
6529 dev[0].wait_disconnected()
0918fe4d
JM
6530
6531def test_ap_wpa2_eap_status(dev, apdev):
6532 """EAP state machine status information"""
6533 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
8b8a1864 6534 hostapd.add_ap(apdev[0], params)
0918fe4d
JM
6535 dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="PEAP",
6536 identity="cert user",
6537 ca_cert="auth_serv/ca.pem", phase2="auth=TLS",
6538 ca_cert2="auth_serv/ca.pem",
6539 client_cert2="auth_serv/user.pem",
6540 private_key2="auth_serv/user.key",
6541 scan_freq="2412", wait_connect=False)
6542 success = False
6543 states = []
6544 method_states = []
6545 decisions = []
6546 req_methods = []
6547 selected_methods = []
6548 for i in range(100000):
6549 s = dev[0].get_status(extra="VERBOSE")
6550 if 'EAP state' in s:
6551 state = s['EAP state']
6552 if state:
6553 if state not in states:
6554 states.append(state)
6555 if state == "SUCCESS":
6556 success = True
6557 break
6558 if 'methodState' in s:
6559 val = s['methodState']
6560 if val not in method_states:
6561 method_states.append(val)
6562 if 'decision' in s:
6563 val = s['decision']
6564 if val not in decisions:
6565 decisions.append(val)
6566 if 'reqMethod' in s:
6567 val = s['reqMethod']
6568 if val not in req_methods:
6569 req_methods.append(val)
6570 if 'selectedMethod' in s:
6571 val = s['selectedMethod']
6572 if val not in selected_methods:
6573 selected_methods.append(val)
6574 logger.info("Iterations: %d" % i)
6575 logger.info("EAP states: " + str(states))
6576 logger.info("methodStates: " + str(method_states))
6577 logger.info("decisions: " + str(decisions))
6578 logger.info("reqMethods: " + str(req_methods))
6579 logger.info("selectedMethods: " + str(selected_methods))
6580 if not success:
6581 raise Exception("EAP did not succeed")
6582 dev[0].wait_connected()
6583 dev[0].request("REMOVE_NETWORK all")
6584 dev[0].wait_disconnected()
29b508e7
JM
6585
6586def test_ap_wpa2_eap_gpsk_ptk_rekey_ap(dev, apdev):
6587 """WPA2-Enterprise with EAP-GPSK and PTK rekey enforced by AP"""
6588 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
6589 params['wpa_ptk_rekey'] = '2'
8b8a1864 6590 hapd = hostapd.add_ap(apdev[0], params)
3b3e2687 6591 id = eap_connect(dev[0], hapd, "GPSK", "gpsk user",
29b508e7
JM
6592 password="abcdefghijklmnop0123456789abcdef")
6593 ev = dev[0].wait_event(["WPA: Key negotiation completed"])
6594 if ev is None:
6595 raise Exception("PTK rekey timed out")
6596 hwsim_utils.test_connectivity(dev[0], hapd)
2833743d
JM
6597
6598def test_ap_wpa2_eap_wildcard_ssid(dev, apdev):
6599 """WPA2-Enterprise connection using EAP-GPSK and wildcard SSID"""
6600 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
6601 hapd = hostapd.add_ap(apdev[0], params)
6602 dev[0].connect(bssid=apdev[0]['bssid'], key_mgmt="WPA-EAP", eap="GPSK",
6603 identity="gpsk user",
6604 password="abcdefghijklmnop0123456789abcdef",
6605 scan_freq="2412")
c9aba19b
JM
6606
6607def test_ap_wpa2_eap_psk_mac_addr_change(dev, apdev):
6608 """WPA2-Enterprise connection using EAP-PSK after MAC address change"""
6609 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
6610 hapd = hostapd.add_ap(apdev[0], params)
6611
6612 cmd = subprocess.Popen(['ps', '-eo', 'pid,command'], stdout=subprocess.PIPE)
04fa9fc7 6613 res = cmd.stdout.read().decode()
c9aba19b
JM
6614 cmd.stdout.close()
6615 pid = 0
6616 for p in res.splitlines():
6617 if "wpa_supplicant" not in p:
6618 continue
6619 if dev[0].ifname not in p:
6620 continue
6621 pid = int(p.strip().split(' ')[0])
6622 if pid == 0:
6623 logger.info("Could not find wpa_supplicant PID")
6624 else:
6625 logger.info("wpa_supplicant PID %d" % pid)
6626
6627 addr = dev[0].get_status_field("address")
6628 subprocess.call(['ip', 'link', 'set', 'dev', dev[0].ifname, 'down'])
6629 subprocess.call(['ip', 'link', 'set', 'dev', dev[0].ifname, 'address',
6630 '02:11:22:33:44:55'])
6631 subprocess.call(['ip', 'link', 'set', 'dev', dev[0].ifname, 'up'])
6632 addr1 = dev[0].get_status_field("address")
6633 if addr1 != '02:11:22:33:44:55':
6634 raise Exception("Failed to change MAC address")
6635
6636 # Scan using the externally set MAC address, stop the wpa_supplicant
6637 # process to avoid it from processing the ifdown event before the interface
6638 # is already UP, change the MAC address back, allow the wpa_supplicant
6639 # process to continue. This will result in the ifdown + ifup sequence of
6640 # RTM_NEWLINK events to be processed while the interface is already UP.
6641 try:
6642 dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
6643 os.kill(pid, signal.SIGSTOP)
6644 time.sleep(0.1)
6645 finally:
6646 subprocess.call(['ip', 'link', 'set', 'dev', dev[0].ifname, 'down'])
6647 subprocess.call(['ip', 'link', 'set', 'dev', dev[0].ifname, 'address',
6648 addr])
6649 subprocess.call(['ip', 'link', 'set', 'dev', dev[0].ifname, 'up'])
6650 time.sleep(0.1)
6651 os.kill(pid, signal.SIGCONT)
6652
6653 eap_connect(dev[0], hapd, "PSK", "psk.user@example.com",
6654 password_hex="0123456789abcdef0123456789abcdef")
6655
6656 addr2 = dev[0].get_status_field("address")
6657 if addr != addr2:
6658 raise Exception("Failed to restore MAC address")
fb643190
JM
6659
6660def test_ap_wpa2_eap_server_get_id(dev, apdev):
6661 """Internal EAP server and dot1xAuthSessionUserName"""
6662 params = int_eap_server_params()
6663 hapd = hostapd.add_ap(apdev[0], params)
6664 eap_connect(dev[0], hapd, "TLS", "tls user", ca_cert="auth_serv/ca.pem",
6665 client_cert="auth_serv/user.pem",
6666 private_key="auth_serv/user.key")
6667 sta = hapd.get_sta(dev[0].own_addr())
6668 if 'dot1xAuthSessionUserName' not in sta:
6669 raise Exception("No dot1xAuthSessionUserName included")
6670 user = sta['dot1xAuthSessionUserName']
6671 if user != "tls user":
6672 raise Exception("Unexpected dot1xAuthSessionUserName value: " + user)
6673
6674def test_ap_wpa2_radius_server_get_id(dev, apdev):
6675 """External RADIUS server and dot1xAuthSessionUserName"""
6676 params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
6677 hapd = hostapd.add_ap(apdev[0], params)
6678 eap_connect(dev[0], hapd, "TTLS", "test-user",
6679 anonymous_identity="ttls", password="password",
6680 ca_cert="auth_serv/ca.pem", phase2="auth=PAP")
6681 sta = hapd.get_sta(dev[0].own_addr())
6682 if 'dot1xAuthSessionUserName' not in sta:
6683 raise Exception("No dot1xAuthSessionUserName included")
6684 user = sta['dot1xAuthSessionUserName']
6685 if user != "real-user":
6686 raise Exception("Unexpected dot1xAuthSessionUserName value: " + user)
67e34a28
JM
6687
6688def test_openssl_systemwide_policy(dev, apdev, test_params):
6689 """OpenSSL systemwide policy and overrides"""
6690 prefix = "openssl_systemwide_policy"
6691 pidfile = os.path.join(test_params['logdir'], prefix + '.pid-wpas')
6692 try:
6693 with HWSimRadio() as (radio, iface):
6694 run_openssl_systemwide_policy(iface, apdev, test_params)
6695 finally:
6696 if os.path.exists(pidfile):
6697 with open(pidfile, 'r') as f:
6698 pid = int(f.read().strip())
6699 os.kill(pid, signal.SIGTERM)
6700
6701def write_openssl_cnf(cnf, MinProtocol=None, CipherString=None):
6702 with open(cnf, "w") as f:
6703 f.write("""openssl_conf = default_conf
6704[default_conf]
6705ssl_conf = ssl_sect
6706[ssl_sect]
6707system_default = system_default_sect
6708[system_default_sect]
6709""")
6710 if MinProtocol:
6711 f.write("MinProtocol = %s\n" % MinProtocol)
6712 if CipherString:
6713 f.write("CipherString = %s\n" % CipherString)
6714
6715def run_openssl_systemwide_policy(iface, apdev, test_params):
6716 prefix = "openssl_systemwide_policy"
6717 logfile = os.path.join(test_params['logdir'], prefix + '.log-wpas')
6718 pidfile = os.path.join(test_params['logdir'], prefix + '.pid-wpas')
6719 conffile = os.path.join(test_params['logdir'], prefix + '.conf')
6720 openssl_cnf = os.path.join(test_params['logdir'], prefix + '.openssl.cnf')
6721
6722 write_openssl_cnf(openssl_cnf, "TLSv1.2", "DEFAULT@SECLEVEL=2")
6723
6724 with open(conffile, 'w') as f:
6725 f.write("ctrl_interface=DIR=/var/run/wpa_supplicant\n")
6726
6727 params = int_eap_server_params()
6728 params['tls_flags'] = "[DISABLE-TLSv1.1][DISABLE-TLSv1.2][DISABLE-TLSv1.3]"
6729
6730 hapd = hostapd.add_ap(apdev[0], params)
6731
6732 prg = os.path.join(test_params['logdir'],
6733 'alt-wpa_supplicant/wpa_supplicant/wpa_supplicant')
6734 if not os.path.exists(prg):
6735 prg = '../../wpa_supplicant/wpa_supplicant'
fab49f61
JM
6736 arg = [prg, '-BddtK', '-P', pidfile, '-f', logfile,
6737 '-Dnl80211', '-c', conffile, '-i', iface]
67e34a28
JM
6738 logger.info("Start wpa_supplicant: " + str(arg))
6739 subprocess.call(arg, env={'OPENSSL_CONF': openssl_cnf})
6740 wpas = WpaSupplicant(ifname=iface)
6741 if "PONG" not in wpas.request("PING"):
6742 raise Exception("Could not PING wpa_supplicant")
6743 tls = wpas.request("GET tls_library")
6744 if not tls.startswith("OpenSSL"):
6745 raise HwsimSkip("Not using OpenSSL")
6746
6747 # Use default configuration without any TLS version overrides. This should
6748 # end up using OpenSSL systemwide policy and result in failure to find a
6749 # compatible protocol version.
6750 ca_file = os.path.join(os.getcwd(), "auth_serv/ca.pem")
6751 id = wpas.connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
6752 identity="pap user", anonymous_identity="ttls",
6753 password="password", phase2="auth=PAP",
6754 ca_cert=ca_file,
6755 scan_freq="2412", wait_connect=False)
6756 ev = wpas.wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=10)
6757 if ev is None:
6758 raise Exception("EAP not started")
6759 ev = wpas.wait_event(["CTRL-EVENT-EAP-STATUS status='local TLS alert'"],
6760 timeout=1)
6761 if ev is None:
6762 raise HwsimSkip("OpenSSL systemwide policy not supported")
6763 wpas.request("DISCONNECT")
6764 wpas.wait_disconnected()
6765 wpas.dump_monitor()
6766
6767 # Explicitly allow TLSv1.0 to be used to override OpenSSL systemwide policy
6768 wpas.set_network_quoted(id, "openssl_ciphers", "DEFAULT@SECLEVEL=1")
6769 wpas.set_network_quoted(id, "phase1", "tls_disable_tlsv1_0=0")
6770 wpas.select_network(id, freq="2412")
6771 wpas.wait_connected()
6772
6773 wpas.request("TERMINATE")