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