test_erp.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. # EAP Re-authentication Protocol (ERP) tests
  2. # Copyright (c) 2014-2015, Jouni Malinen <j@w1.fi>
  3. #
  4. # This software may be distributed under the terms of the BSD license.
  5. # See README for more details.
  6. import binascii
  7. import logging
  8. logger = logging.getLogger()
  9. import os
  10. import time
  11. import hostapd
  12. from utils import HwsimSkip
  13. from test_ap_eap import int_eap_server_params
  14. from test_ap_psk import find_wpas_process, read_process_memory, verify_not_present, get_key_locations
  15. def check_erp_capa(dev):
  16. capab = dev.get_capability("erp")
  17. if not capab or 'ERP' not in capab:
  18. raise HwsimSkip("ERP not supported in the build")
  19. def test_erp_initiate_reauth_start(dev, apdev):
  20. """Authenticator sending EAP-Initiate/Re-auth-Start, but ERP disabled on peer"""
  21. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  22. params['erp_send_reauth_start'] = '1'
  23. params['erp_domain'] = 'example.com'
  24. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  25. dev[0].request("ERP_FLUSH")
  26. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP",
  27. eap="PAX", identity="pax.user@example.com",
  28. password_hex="0123456789abcdef0123456789abcdef",
  29. scan_freq="2412")
  30. def test_erp_enabled_on_server(dev, apdev):
  31. """ERP enabled on internal EAP server, but disabled on peer"""
  32. params = int_eap_server_params()
  33. params['erp_send_reauth_start'] = '1'
  34. params['erp_domain'] = 'example.com'
  35. params['eap_server_erp'] = '1'
  36. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  37. dev[0].request("ERP_FLUSH")
  38. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP",
  39. eap="PAX", identity="pax.user@example.com",
  40. password_hex="0123456789abcdef0123456789abcdef",
  41. scan_freq="2412")
  42. def test_erp(dev, apdev):
  43. """ERP enabled on server and peer"""
  44. check_erp_capa(dev[0])
  45. params = int_eap_server_params()
  46. params['erp_send_reauth_start'] = '1'
  47. params['erp_domain'] = 'example.com'
  48. params['eap_server_erp'] = '1'
  49. params['disable_pmksa_caching'] = '1'
  50. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  51. dev[0].request("ERP_FLUSH")
  52. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP",
  53. eap="PSK", identity="psk.user@example.com",
  54. password_hex="0123456789abcdef0123456789abcdef",
  55. erp="1", scan_freq="2412")
  56. for i in range(3):
  57. dev[0].request("DISCONNECT")
  58. dev[0].wait_disconnected(timeout=15)
  59. dev[0].request("RECONNECT")
  60. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  61. if ev is None:
  62. raise Exception("EAP success timed out")
  63. if "EAP re-authentication completed successfully" not in ev:
  64. raise Exception("Did not use ERP")
  65. dev[0].wait_connected(timeout=15, error="Reconnection timed out")
  66. def test_erp_server_no_match(dev, apdev):
  67. """ERP enabled on server and peer, but server has no key match"""
  68. check_erp_capa(dev[0])
  69. params = int_eap_server_params()
  70. params['erp_send_reauth_start'] = '1'
  71. params['erp_domain'] = 'example.com'
  72. params['eap_server_erp'] = '1'
  73. params['disable_pmksa_caching'] = '1'
  74. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  75. dev[0].request("ERP_FLUSH")
  76. id = dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP",
  77. eap="PSK", identity="psk.user@example.com",
  78. password_hex="0123456789abcdef0123456789abcdef",
  79. erp="1", scan_freq="2412")
  80. dev[0].request("DISCONNECT")
  81. dev[0].wait_disconnected(timeout=15)
  82. hapd.request("ERP_FLUSH")
  83. dev[0].request("RECONNECT")
  84. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS",
  85. "CTRL-EVENT-EAP-FAILURE"], timeout=15)
  86. if ev is None:
  87. raise Exception("EAP result timed out")
  88. if "CTRL-EVENT-EAP-SUCCESS" in ev:
  89. raise Exception("Unexpected EAP success")
  90. dev[0].request("DISCONNECT")
  91. dev[0].select_network(id)
  92. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  93. if ev is None:
  94. raise Exception("EAP success timed out")
  95. if "EAP re-authentication completed successfully" in ev:
  96. raise Exception("Unexpected use of ERP")
  97. dev[0].wait_connected(timeout=15, error="Reconnection timed out")
  98. def start_erp_as(apdev):
  99. params = { "ssid": "as", "beacon_int": "2000",
  100. "radius_server_clients": "auth_serv/radius_clients.conf",
  101. "radius_server_auth_port": '18128',
  102. "eap_server": "1",
  103. "eap_user_file": "auth_serv/eap_user.conf",
  104. "ca_cert": "auth_serv/ca.pem",
  105. "server_cert": "auth_serv/server.pem",
  106. "private_key": "auth_serv/server.key",
  107. "eap_sim_db": "unix:/tmp/hlr_auc_gw.sock",
  108. "dh_file": "auth_serv/dh.conf",
  109. "pac_opaque_encr_key": "000102030405060708090a0b0c0d0e0f",
  110. "eap_fast_a_id": "101112131415161718191a1b1c1d1e1f",
  111. "eap_fast_a_id_info": "test server",
  112. "eap_server_erp": "1",
  113. "erp_domain": "example.com" }
  114. hostapd.add_ap(apdev['ifname'], params)
  115. def test_erp_radius(dev, apdev):
  116. """ERP enabled on RADIUS server and peer"""
  117. check_erp_capa(dev[0])
  118. start_erp_as(apdev[1])
  119. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  120. params['auth_server_port'] = "18128"
  121. params['erp_send_reauth_start'] = '1'
  122. params['erp_domain'] = 'example.com'
  123. params['disable_pmksa_caching'] = '1'
  124. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  125. dev[0].request("ERP_FLUSH")
  126. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP",
  127. eap="PSK", identity="psk.user@example.com",
  128. password_hex="0123456789abcdef0123456789abcdef",
  129. erp="1", scan_freq="2412")
  130. for i in range(3):
  131. dev[0].request("DISCONNECT")
  132. dev[0].wait_disconnected(timeout=15)
  133. dev[0].request("RECONNECT")
  134. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  135. if ev is None:
  136. raise Exception("EAP success timed out")
  137. if "EAP re-authentication completed successfully" not in ev:
  138. raise Exception("Did not use ERP")
  139. dev[0].wait_connected(timeout=15, error="Reconnection timed out")
  140. def erp_test(dev, hapd, **kwargs):
  141. res = dev.get_capability("eap")
  142. if kwargs['eap'] not in res:
  143. logger.info("Skip ERP test with %s due to missing support" % kwargs['eap'])
  144. return
  145. hapd.dump_monitor()
  146. dev.dump_monitor()
  147. dev.request("ERP_FLUSH")
  148. id = dev.connect("test-wpa2-eap", key_mgmt="WPA-EAP", erp="1",
  149. scan_freq="2412", **kwargs)
  150. dev.request("DISCONNECT")
  151. dev.wait_disconnected(timeout=15)
  152. hapd.dump_monitor()
  153. dev.request("RECONNECT")
  154. ev = dev.wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  155. if ev is None:
  156. raise Exception("EAP success timed out")
  157. if "EAP re-authentication completed successfully" not in ev:
  158. raise Exception("Did not use ERP")
  159. dev.wait_connected(timeout=15, error="Reconnection timed out")
  160. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  161. if ev is None:
  162. raise Exception("No connection event received from hostapd")
  163. dev.request("DISCONNECT")
  164. def test_erp_radius_eap_methods(dev, apdev):
  165. """ERP enabled on RADIUS server and peer"""
  166. check_erp_capa(dev[0])
  167. eap_methods = dev[0].get_capability("eap")
  168. start_erp_as(apdev[1])
  169. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  170. params['auth_server_port'] = "18128"
  171. params['erp_send_reauth_start'] = '1'
  172. params['erp_domain'] = 'example.com'
  173. params['disable_pmksa_caching'] = '1'
  174. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  175. erp_test(dev[0], hapd, eap="AKA", identity="0232010000000000@example.com",
  176. password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581:000000000123")
  177. erp_test(dev[0], hapd, eap="AKA'", identity="6555444333222111@example.com",
  178. password="5122250214c33e723a5dd523fc145fc0:981d464c7c52eb6e5036234984ad0bcf:000000000123")
  179. erp_test(dev[0], hapd, eap="EKE", identity="erp-eke@example.com",
  180. password="hello")
  181. if "FAST" in eap_methods:
  182. erp_test(dev[0], hapd, eap="FAST", identity="erp-fast@example.com",
  183. password="password", ca_cert="auth_serv/ca.pem",
  184. phase2="auth=GTC",
  185. phase1="fast_provisioning=2",
  186. pac_file="blob://fast_pac_auth_erp")
  187. erp_test(dev[0], hapd, eap="GPSK", identity="erp-gpsk@example.com",
  188. password="abcdefghijklmnop0123456789abcdef")
  189. erp_test(dev[0], hapd, eap="IKEV2", identity="erp-ikev2@example.com",
  190. password="password")
  191. erp_test(dev[0], hapd, eap="PAX", identity="erp-pax@example.com",
  192. password_hex="0123456789abcdef0123456789abcdef")
  193. # TODO: PEAP (EMSK)
  194. #if "MSCHAPV2" in eap_methods:
  195. # erp_test(dev[0], hapd, eap="PEAP", identity="erp-peap@example.com",
  196. # password="password", ca_cert="auth_serv/ca.pem",
  197. # phase2="auth=MSCHAPV2")
  198. erp_test(dev[0], hapd, eap="PSK", identity="erp-psk@example.com",
  199. password_hex="0123456789abcdef0123456789abcdef")
  200. if "PWD" in eap_methods:
  201. erp_test(dev[0], hapd, eap="PWD", identity="erp-pwd@example.com",
  202. password="secret password")
  203. erp_test(dev[0], hapd, eap="SAKE", identity="erp-sake@example.com",
  204. password_hex="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
  205. erp_test(dev[0], hapd, eap="SIM", identity="1232010000000000@example.com",
  206. password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581")
  207. erp_test(dev[0], hapd, eap="TLS", identity="erp-tls@example.com",
  208. ca_cert="auth_serv/ca.pem", client_cert="auth_serv/user.pem",
  209. private_key="auth_serv/user.key")
  210. erp_test(dev[0], hapd, eap="TTLS", identity="erp-ttls@example.com",
  211. password="password", ca_cert="auth_serv/ca.pem", phase2="auth=PAP")
  212. def test_erp_key_lifetime_in_memory(dev, apdev, params):
  213. """ERP and key lifetime in memory"""
  214. check_erp_capa(dev[0])
  215. p = int_eap_server_params()
  216. p['erp_send_reauth_start'] = '1'
  217. p['erp_domain'] = 'example.com'
  218. p['eap_server_erp'] = '1'
  219. p['disable_pmksa_caching'] = '1'
  220. hapd = hostapd.add_ap(apdev[0]['ifname'], p)
  221. password = "63d2d21ac3c09ed567ee004a34490f1d16e7fa5835edf17ddba70a63f1a90a25"
  222. pid = find_wpas_process(dev[0])
  223. dev[0].request("ERP_FLUSH")
  224. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
  225. identity="pap-secret@example.com", password=password,
  226. ca_cert="auth_serv/ca.pem", phase2="auth=PAP",
  227. erp="1", scan_freq="2412")
  228. time.sleep(1)
  229. buf = read_process_memory(pid, password)
  230. dev[0].request("DISCONNECT")
  231. dev[0].wait_disconnected(timeout=15)
  232. dev[0].relog()
  233. msk = None
  234. emsk = None
  235. rRK = None
  236. rIK = None
  237. pmk = None
  238. ptk = None
  239. gtk = None
  240. with open(os.path.join(params['logdir'], 'log0'), 'r') as f:
  241. for l in f.readlines():
  242. if "EAP-TTLS: Derived key - hexdump" in l:
  243. val = l.strip().split(':')[3].replace(' ', '')
  244. msk = binascii.unhexlify(val)
  245. if "EAP-TTLS: Derived EMSK - hexdump" in l:
  246. val = l.strip().split(':')[3].replace(' ', '')
  247. emsk = binascii.unhexlify(val)
  248. if "EAP: ERP rRK - hexdump" in l:
  249. val = l.strip().split(':')[3].replace(' ', '')
  250. rRK = binascii.unhexlify(val)
  251. if "EAP: ERP rIK - hexdump" in l:
  252. val = l.strip().split(':')[3].replace(' ', '')
  253. rIK = binascii.unhexlify(val)
  254. if "WPA: PMK - hexdump" in l:
  255. val = l.strip().split(':')[3].replace(' ', '')
  256. pmk = binascii.unhexlify(val)
  257. if "WPA: PTK - hexdump" in l:
  258. val = l.strip().split(':')[3].replace(' ', '')
  259. ptk = binascii.unhexlify(val)
  260. if "WPA: Group Key - hexdump" in l:
  261. val = l.strip().split(':')[3].replace(' ', '')
  262. gtk = binascii.unhexlify(val)
  263. if not msk or not emsk or not rIK or not rRK or not pmk or not ptk or not gtk:
  264. raise Exception("Could not find keys from debug log")
  265. if len(gtk) != 16:
  266. raise Exception("Unexpected GTK length")
  267. kck = ptk[0:16]
  268. kek = ptk[16:32]
  269. tk = ptk[32:48]
  270. fname = os.path.join(params['logdir'],
  271. 'erp_key_lifetime_in_memory.memctx-')
  272. logger.info("Checking keys in memory while associated")
  273. get_key_locations(buf, password, "Password")
  274. get_key_locations(buf, pmk, "PMK")
  275. get_key_locations(buf, msk, "MSK")
  276. get_key_locations(buf, emsk, "EMSK")
  277. get_key_locations(buf, rRK, "rRK")
  278. get_key_locations(buf, rIK, "rIK")
  279. if password not in buf:
  280. raise HwsimSkip("Password not found while associated")
  281. if pmk not in buf:
  282. raise HwsimSkip("PMK not found while associated")
  283. if kck not in buf:
  284. raise Exception("KCK not found while associated")
  285. if kek not in buf:
  286. raise Exception("KEK not found while associated")
  287. if tk in buf:
  288. raise Exception("TK found from memory")
  289. if gtk in buf:
  290. raise Exception("GTK found from memory")
  291. logger.info("Checking keys in memory after disassociation")
  292. buf = read_process_memory(pid, password)
  293. # Note: Password is still present in network configuration
  294. # Note: PMK is in EAP fast re-auth data
  295. get_key_locations(buf, password, "Password")
  296. get_key_locations(buf, pmk, "PMK")
  297. get_key_locations(buf, msk, "MSK")
  298. get_key_locations(buf, emsk, "EMSK")
  299. get_key_locations(buf, rRK, "rRK")
  300. get_key_locations(buf, rIK, "rIK")
  301. verify_not_present(buf, kck, fname, "KCK")
  302. verify_not_present(buf, kek, fname, "KEK")
  303. verify_not_present(buf, tk, fname, "TK")
  304. verify_not_present(buf, gtk, fname, "GTK")
  305. dev[0].request("RECONNECT")
  306. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  307. if ev is None:
  308. raise Exception("EAP success timed out")
  309. if "EAP re-authentication completed successfully" not in ev:
  310. raise Exception("Did not use ERP")
  311. dev[0].wait_connected(timeout=15, error="Reconnection timed out")
  312. dev[0].request("DISCONNECT")
  313. dev[0].wait_disconnected(timeout=15)
  314. dev[0].relog()
  315. pmk = None
  316. ptk = None
  317. gtk = None
  318. with open(os.path.join(params['logdir'], 'log0'), 'r') as f:
  319. for l in f.readlines():
  320. if "WPA: PMK - hexdump" in l:
  321. val = l.strip().split(':')[3].replace(' ', '')
  322. pmk = binascii.unhexlify(val)
  323. if "WPA: PTK - hexdump" in l:
  324. val = l.strip().split(':')[3].replace(' ', '')
  325. ptk = binascii.unhexlify(val)
  326. if "WPA: GTK in EAPOL-Key - hexdump" in l:
  327. val = l.strip().split(':')[3].replace(' ', '')
  328. gtk = binascii.unhexlify(val)
  329. if not pmk or not ptk or not gtk:
  330. raise Exception("Could not find keys from debug log")
  331. kck = ptk[0:16]
  332. kek = ptk[16:32]
  333. tk = ptk[32:48]
  334. logger.info("Checking keys in memory after ERP and disassociation")
  335. buf = read_process_memory(pid, password)
  336. # Note: Password is still present in network configuration
  337. get_key_locations(buf, password, "Password")
  338. get_key_locations(buf, pmk, "PMK")
  339. get_key_locations(buf, msk, "MSK")
  340. get_key_locations(buf, emsk, "EMSK")
  341. get_key_locations(buf, rRK, "rRK")
  342. get_key_locations(buf, rIK, "rIK")
  343. verify_not_present(buf, kck, fname, "KCK")
  344. verify_not_present(buf, kek, fname, "KEK")
  345. verify_not_present(buf, tk, fname, "TK")
  346. verify_not_present(buf, gtk, fname, "GTK")
  347. dev[0].request("REMOVE_NETWORK all")
  348. logger.info("Checking keys in memory after network profile removal")
  349. buf = read_process_memory(pid, password)
  350. # Note: rRK and rIK are still in memory
  351. get_key_locations(buf, password, "Password")
  352. get_key_locations(buf, pmk, "PMK")
  353. get_key_locations(buf, msk, "MSK")
  354. get_key_locations(buf, emsk, "EMSK")
  355. get_key_locations(buf, rRK, "rRK")
  356. get_key_locations(buf, rIK, "rIK")
  357. verify_not_present(buf, password, fname, "password")
  358. verify_not_present(buf, pmk, fname, "PMK")
  359. verify_not_present(buf, kck, fname, "KCK")
  360. verify_not_present(buf, kek, fname, "KEK")
  361. verify_not_present(buf, tk, fname, "TK")
  362. verify_not_present(buf, gtk, fname, "GTK")
  363. verify_not_present(buf, msk, fname, "MSK")
  364. verify_not_present(buf, emsk, fname, "EMSK")
  365. dev[0].request("ERP_FLUSH")
  366. logger.info("Checking keys in memory after ERP_FLUSH")
  367. buf = read_process_memory(pid, password)
  368. get_key_locations(buf, rRK, "rRK")
  369. get_key_locations(buf, rIK, "rIK")
  370. verify_not_present(buf, rRK, fname, "rRK")
  371. verify_not_present(buf, rIK, fname, "rIK")