test_erp.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. hapd.dump_monitor()
  142. dev.dump_monitor()
  143. dev.request("ERP_FLUSH")
  144. id = dev.connect("test-wpa2-eap", key_mgmt="WPA-EAP", erp="1",
  145. scan_freq="2412", **kwargs)
  146. dev.request("DISCONNECT")
  147. dev.wait_disconnected(timeout=15)
  148. hapd.dump_monitor()
  149. dev.request("RECONNECT")
  150. ev = dev.wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  151. if ev is None:
  152. raise Exception("EAP success timed out")
  153. if "EAP re-authentication completed successfully" not in ev:
  154. raise Exception("Did not use ERP")
  155. dev.wait_connected(timeout=15, error="Reconnection timed out")
  156. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  157. if ev is None:
  158. raise Exception("No connection event received from hostapd")
  159. dev.request("DISCONNECT")
  160. def test_erp_radius_eap_methods(dev, apdev):
  161. """ERP enabled on RADIUS server and peer"""
  162. check_erp_capa(dev[0])
  163. start_erp_as(apdev[1])
  164. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  165. params['auth_server_port'] = "18128"
  166. params['erp_send_reauth_start'] = '1'
  167. params['erp_domain'] = 'example.com'
  168. params['disable_pmksa_caching'] = '1'
  169. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  170. erp_test(dev[0], hapd, eap="AKA", identity="0232010000000000@example.com",
  171. password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581:000000000123")
  172. erp_test(dev[0], hapd, eap="AKA'", identity="6555444333222111@example.com",
  173. password="5122250214c33e723a5dd523fc145fc0:981d464c7c52eb6e5036234984ad0bcf:000000000123")
  174. # TODO: EKE getSession
  175. #erp_test(dev[0], hapd, eap="EKE", identity="erp-eke@example.com",
  176. # password="hello")
  177. if "FAST" in dev[0].get_capability("eap"):
  178. erp_test(dev[0], hapd, eap="FAST", identity="erp-fast@example.com",
  179. password="password", ca_cert="auth_serv/ca.pem",
  180. phase2="auth=GTC",
  181. phase1="fast_provisioning=2",
  182. pac_file="blob://fast_pac_auth_erp")
  183. erp_test(dev[0], hapd, eap="GPSK", identity="erp-gpsk@example.com",
  184. password="abcdefghijklmnop0123456789abcdef")
  185. erp_test(dev[0], hapd, eap="IKEV2", identity="erp-ikev2@example.com",
  186. password="password")
  187. erp_test(dev[0], hapd, eap="PAX", identity="erp-pax@example.com",
  188. password_hex="0123456789abcdef0123456789abcdef")
  189. # TODO: PEAP (EMSK)
  190. #erp_test(dev[0], hapd, eap="PEAP", identity="erp-peap@example.com",
  191. # password="password", ca_cert="auth_serv/ca.pem",
  192. # phase2="auth=MSCHAPV2")
  193. erp_test(dev[0], hapd, eap="PSK", identity="erp-psk@example.com",
  194. password_hex="0123456789abcdef0123456789abcdef")
  195. if "PWD" in dev[0].get_capability("eap"):
  196. erp_test(dev[0], hapd, eap="PWD", identity="erp-pwd@example.com",
  197. password="secret password")
  198. erp_test(dev[0], hapd, eap="SAKE", identity="erp-sake@example.com",
  199. password_hex="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
  200. erp_test(dev[0], hapd, eap="SIM", identity="1232010000000000@example.com",
  201. password="90dca4eda45b53cf0f12d7c9c3bc6a89:cb9cccc4b9258e6dca4760379fb82581")
  202. erp_test(dev[0], hapd, eap="TLS", identity="erp-tls@example.com",
  203. ca_cert="auth_serv/ca.pem", client_cert="auth_serv/user.pem",
  204. private_key="auth_serv/user.key")
  205. erp_test(dev[0], hapd, eap="TTLS", identity="erp-ttls@example.com",
  206. password="password", ca_cert="auth_serv/ca.pem", phase2="auth=PAP")
  207. def test_erp_key_lifetime_in_memory(dev, apdev, params):
  208. """ERP and key lifetime in memory"""
  209. check_erp_capa(dev[0])
  210. p = int_eap_server_params()
  211. p['erp_send_reauth_start'] = '1'
  212. p['erp_domain'] = 'example.com'
  213. p['eap_server_erp'] = '1'
  214. p['disable_pmksa_caching'] = '1'
  215. hapd = hostapd.add_ap(apdev[0]['ifname'], p)
  216. password = "63d2d21ac3c09ed567ee004a34490f1d16e7fa5835edf17ddba70a63f1a90a25"
  217. pid = find_wpas_process(dev[0])
  218. dev[0].request("ERP_FLUSH")
  219. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", eap="TTLS",
  220. identity="pap-secret@example.com", password=password,
  221. ca_cert="auth_serv/ca.pem", phase2="auth=PAP",
  222. erp="1", scan_freq="2412")
  223. time.sleep(0.1)
  224. buf = read_process_memory(pid, password)
  225. dev[0].request("DISCONNECT")
  226. dev[0].wait_disconnected(timeout=15)
  227. dev[0].relog()
  228. msk = None
  229. emsk = None
  230. rRK = None
  231. rIK = None
  232. pmk = None
  233. ptk = None
  234. gtk = None
  235. with open(os.path.join(params['logdir'], 'log0'), 'r') as f:
  236. for l in f.readlines():
  237. if "EAP-TTLS: Derived key - hexdump" in l:
  238. val = l.strip().split(':')[3].replace(' ', '')
  239. msk = binascii.unhexlify(val)
  240. if "EAP-TTLS: Derived EMSK - hexdump" in l:
  241. val = l.strip().split(':')[3].replace(' ', '')
  242. emsk = binascii.unhexlify(val)
  243. if "EAP: ERP rRK - hexdump" in l:
  244. val = l.strip().split(':')[3].replace(' ', '')
  245. rRK = binascii.unhexlify(val)
  246. if "EAP: ERP rIK - hexdump" in l:
  247. val = l.strip().split(':')[3].replace(' ', '')
  248. rIK = binascii.unhexlify(val)
  249. if "WPA: PMK - hexdump" in l:
  250. val = l.strip().split(':')[3].replace(' ', '')
  251. pmk = binascii.unhexlify(val)
  252. if "WPA: PTK - hexdump" in l:
  253. val = l.strip().split(':')[3].replace(' ', '')
  254. ptk = binascii.unhexlify(val)
  255. if "WPA: Group Key - hexdump" in l:
  256. val = l.strip().split(':')[3].replace(' ', '')
  257. gtk = binascii.unhexlify(val)
  258. if not msk or not emsk or not rIK or not rRK or not pmk or not ptk or not gtk:
  259. raise Exception("Could not find keys from debug log")
  260. if len(gtk) != 16:
  261. raise Exception("Unexpected GTK length")
  262. kck = ptk[0:16]
  263. kek = ptk[16:32]
  264. tk = ptk[32:48]
  265. fname = os.path.join(params['logdir'],
  266. 'erp_key_lifetime_in_memory.memctx-')
  267. logger.info("Checking keys in memory while associated")
  268. get_key_locations(buf, password, "Password")
  269. get_key_locations(buf, pmk, "PMK")
  270. get_key_locations(buf, msk, "MSK")
  271. get_key_locations(buf, emsk, "EMSK")
  272. get_key_locations(buf, rRK, "rRK")
  273. get_key_locations(buf, rIK, "rIK")
  274. if password not in buf:
  275. raise HwsimSkip("Password not found while associated")
  276. if pmk not in buf:
  277. raise HwsimSkip("PMK not found while associated")
  278. if kck not in buf:
  279. raise Exception("KCK not found while associated")
  280. if kek not in buf:
  281. raise Exception("KEK not found while associated")
  282. if tk in buf:
  283. raise Exception("TK found from memory")
  284. if gtk in buf:
  285. raise Exception("GTK found from memory")
  286. logger.info("Checking keys in memory after disassociation")
  287. buf = read_process_memory(pid, password)
  288. # Note: Password is still present in network configuration
  289. # Note: PMK is in EAP fast re-auth data
  290. get_key_locations(buf, password, "Password")
  291. get_key_locations(buf, pmk, "PMK")
  292. get_key_locations(buf, msk, "MSK")
  293. get_key_locations(buf, emsk, "EMSK")
  294. get_key_locations(buf, rRK, "rRK")
  295. get_key_locations(buf, rIK, "rIK")
  296. verify_not_present(buf, kck, fname, "KCK")
  297. verify_not_present(buf, kek, fname, "KEK")
  298. verify_not_present(buf, tk, fname, "TK")
  299. verify_not_present(buf, gtk, fname, "GTK")
  300. dev[0].request("RECONNECT")
  301. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  302. if ev is None:
  303. raise Exception("EAP success timed out")
  304. if "EAP re-authentication completed successfully" not in ev:
  305. raise Exception("Did not use ERP")
  306. dev[0].wait_connected(timeout=15, error="Reconnection timed out")
  307. dev[0].request("DISCONNECT")
  308. dev[0].wait_disconnected(timeout=15)
  309. dev[0].relog()
  310. pmk = None
  311. ptk = None
  312. gtk = None
  313. with open(os.path.join(params['logdir'], 'log0'), 'r') as f:
  314. for l in f.readlines():
  315. if "WPA: PMK - hexdump" in l:
  316. val = l.strip().split(':')[3].replace(' ', '')
  317. pmk = binascii.unhexlify(val)
  318. if "WPA: PTK - hexdump" in l:
  319. val = l.strip().split(':')[3].replace(' ', '')
  320. ptk = binascii.unhexlify(val)
  321. if "WPA: GTK in EAPOL-Key - hexdump" in l:
  322. val = l.strip().split(':')[3].replace(' ', '')
  323. gtk = binascii.unhexlify(val)
  324. if not pmk or not ptk or not gtk:
  325. raise Exception("Could not find keys from debug log")
  326. kck = ptk[0:16]
  327. kek = ptk[16:32]
  328. tk = ptk[32:48]
  329. logger.info("Checking keys in memory after ERP and disassociation")
  330. buf = read_process_memory(pid, password)
  331. # Note: Password is still present in network configuration
  332. get_key_locations(buf, password, "Password")
  333. get_key_locations(buf, pmk, "PMK")
  334. get_key_locations(buf, msk, "MSK")
  335. get_key_locations(buf, emsk, "EMSK")
  336. get_key_locations(buf, rRK, "rRK")
  337. get_key_locations(buf, rIK, "rIK")
  338. verify_not_present(buf, kck, fname, "KCK")
  339. verify_not_present(buf, kek, fname, "KEK")
  340. verify_not_present(buf, tk, fname, "TK")
  341. verify_not_present(buf, gtk, fname, "GTK")
  342. dev[0].request("REMOVE_NETWORK all")
  343. logger.info("Checking keys in memory after network profile removal")
  344. buf = read_process_memory(pid, password)
  345. # Note: rRK and rIK are still in memory
  346. get_key_locations(buf, password, "Password")
  347. get_key_locations(buf, pmk, "PMK")
  348. get_key_locations(buf, msk, "MSK")
  349. get_key_locations(buf, emsk, "EMSK")
  350. get_key_locations(buf, rRK, "rRK")
  351. get_key_locations(buf, rIK, "rIK")
  352. verify_not_present(buf, password, fname, "password")
  353. verify_not_present(buf, pmk, fname, "PMK")
  354. verify_not_present(buf, kck, fname, "KCK")
  355. verify_not_present(buf, kek, fname, "KEK")
  356. verify_not_present(buf, tk, fname, "TK")
  357. verify_not_present(buf, gtk, fname, "GTK")
  358. verify_not_present(buf, msk, fname, "MSK")
  359. verify_not_present(buf, emsk, fname, "EMSK")
  360. dev[0].request("ERP_FLUSH")
  361. logger.info("Checking keys in memory after ERP_FLUSH")
  362. buf = read_process_memory(pid, password)
  363. get_key_locations(buf, rRK, "rRK")
  364. get_key_locations(buf, rIK, "rIK")
  365. verify_not_present(buf, rRK, fname, "rRK")
  366. verify_not_present(buf, rIK, fname, "rIK")