test_sae.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. # Test cases for SAE
  2. # Copyright (c) 2013-2014, 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 os
  8. import time
  9. import subprocess
  10. import logging
  11. logger = logging.getLogger()
  12. import hwsim_utils
  13. import hostapd
  14. from utils import HwsimSkip
  15. from test_ap_psk import find_wpas_process, read_process_memory, verify_not_present, get_key_locations
  16. def test_sae(dev, apdev):
  17. """SAE with default group"""
  18. if "SAE" not in dev[0].get_capability("auth_alg"):
  19. raise HwsimSkip("SAE not supported")
  20. params = hostapd.wpa2_params(ssid="test-sae",
  21. passphrase="12345678")
  22. params['wpa_key_mgmt'] = 'SAE'
  23. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  24. key_mgmt = hapd.get_config()['key_mgmt']
  25. if key_mgmt.split(' ')[0] != "SAE":
  26. raise Exception("Unexpected GET_CONFIG(key_mgmt): " + key_mgmt)
  27. dev[0].request("SET sae_groups ")
  28. id = dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  29. scan_freq="2412")
  30. if dev[0].get_status_field('sae_group') != '19':
  31. raise Exception("Expected default SAE group not used")
  32. bss = dev[0].get_bss(apdev[0]['bssid'])
  33. if 'flags' not in bss:
  34. raise Exception("Could not get BSS flags from BSS table")
  35. if "[WPA2-SAE-CCMP]" not in bss['flags']:
  36. raise Exception("Unexpected BSS flags: " + bss['flags'])
  37. def test_sae_pmksa_caching(dev, apdev):
  38. """SAE and PMKSA caching"""
  39. if "SAE" not in dev[0].get_capability("auth_alg"):
  40. raise HwsimSkip("SAE not supported")
  41. params = hostapd.wpa2_params(ssid="test-sae",
  42. passphrase="12345678")
  43. params['wpa_key_mgmt'] = 'SAE'
  44. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  45. dev[0].request("SET sae_groups ")
  46. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  47. scan_freq="2412")
  48. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  49. if ev is None:
  50. raise Exception("No connection event received from hostapd")
  51. dev[0].request("DISCONNECT")
  52. dev[0].wait_disconnected()
  53. dev[0].request("RECONNECT")
  54. dev[0].wait_connected(timeout=15, error="Reconnect timed out")
  55. if dev[0].get_status_field('sae_group') is not None:
  56. raise Exception("SAE group claimed to have been used")
  57. def test_sae_pmksa_caching_disabled(dev, apdev):
  58. """SAE and PMKSA caching disabled"""
  59. if "SAE" not in dev[0].get_capability("auth_alg"):
  60. raise HwsimSkip("SAE not supported")
  61. params = hostapd.wpa2_params(ssid="test-sae",
  62. passphrase="12345678")
  63. params['wpa_key_mgmt'] = 'SAE'
  64. params['disable_pmksa_caching'] = '1'
  65. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  66. dev[0].request("SET sae_groups ")
  67. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  68. scan_freq="2412")
  69. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  70. if ev is None:
  71. raise Exception("No connection event received from hostapd")
  72. dev[0].request("DISCONNECT")
  73. dev[0].wait_disconnected()
  74. dev[0].request("RECONNECT")
  75. dev[0].wait_connected(timeout=15, error="Reconnect timed out")
  76. if dev[0].get_status_field('sae_group') != '19':
  77. raise Exception("Expected default SAE group not used")
  78. def test_sae_groups(dev, apdev):
  79. """SAE with all supported groups"""
  80. if "SAE" not in dev[0].get_capability("auth_alg"):
  81. raise HwsimSkip("SAE not supported")
  82. # This would be the full list of supported groups, but groups 14-16
  83. # (2048-4096 bit MODP) are a bit too slow on some VMs and can result in
  84. # hitting mac80211 authentication timeout, so skip them for now.
  85. #sae_groups = [ 19, 25, 26, 20, 21, 2, 5, 14, 15, 16, 22, 23, 24 ]
  86. sae_groups = [ 19, 25, 26, 20, 21, 2, 5, 22, 23, 24 ]
  87. groups = [str(g) for g in sae_groups]
  88. params = hostapd.wpa2_params(ssid="test-sae-groups",
  89. passphrase="12345678")
  90. params['wpa_key_mgmt'] = 'SAE'
  91. params['sae_groups'] = ' '.join(groups)
  92. hostapd.add_ap(apdev[0]['ifname'], params)
  93. for g in groups:
  94. logger.info("Testing SAE group " + g)
  95. dev[0].request("SET sae_groups " + g)
  96. id = dev[0].connect("test-sae-groups", psk="12345678", key_mgmt="SAE",
  97. scan_freq="2412")
  98. if dev[0].get_status_field('sae_group') != g:
  99. raise Exception("Expected SAE group not used")
  100. dev[0].remove_network(id)
  101. def test_sae_group_nego(dev, apdev):
  102. """SAE group negotiation"""
  103. if "SAE" not in dev[0].get_capability("auth_alg"):
  104. raise HwsimSkip("SAE not supported")
  105. params = hostapd.wpa2_params(ssid="test-sae-group-nego",
  106. passphrase="12345678")
  107. params['wpa_key_mgmt'] = 'SAE'
  108. params['sae_groups'] = '19'
  109. hostapd.add_ap(apdev[0]['ifname'], params)
  110. dev[0].request("SET sae_groups 25 26 20 19")
  111. dev[0].connect("test-sae-group-nego", psk="12345678", key_mgmt="SAE",
  112. scan_freq="2412")
  113. if dev[0].get_status_field('sae_group') != '19':
  114. raise Exception("Expected SAE group not used")
  115. def test_sae_anti_clogging(dev, apdev):
  116. """SAE anti clogging"""
  117. if "SAE" not in dev[0].get_capability("auth_alg"):
  118. raise HwsimSkip("SAE not supported")
  119. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  120. params['wpa_key_mgmt'] = 'SAE'
  121. params['sae_anti_clogging_threshold'] = '1'
  122. hostapd.add_ap(apdev[0]['ifname'], params)
  123. dev[0].request("SET sae_groups ")
  124. dev[1].request("SET sae_groups ")
  125. id = {}
  126. for i in range(0, 2):
  127. dev[i].scan(freq="2412")
  128. id[i] = dev[i].connect("test-sae", psk="12345678", key_mgmt="SAE",
  129. scan_freq="2412", only_add_network=True)
  130. for i in range(0, 2):
  131. dev[i].select_network(id[i])
  132. for i in range(0, 2):
  133. dev[i].wait_connected(timeout=10)
  134. def test_sae_forced_anti_clogging(dev, apdev):
  135. """SAE anti clogging (forced)"""
  136. if "SAE" not in dev[0].get_capability("auth_alg"):
  137. raise HwsimSkip("SAE not supported")
  138. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  139. params['wpa_key_mgmt'] = 'SAE WPA-PSK'
  140. params['sae_anti_clogging_threshold'] = '0'
  141. hostapd.add_ap(apdev[0]['ifname'], params)
  142. dev[2].connect("test-sae", psk="12345678", scan_freq="2412")
  143. for i in range(0, 2):
  144. dev[i].request("SET sae_groups ")
  145. dev[i].connect("test-sae", psk="12345678", key_mgmt="SAE",
  146. scan_freq="2412")
  147. def test_sae_mixed(dev, apdev):
  148. """Mixed SAE and non-SAE network"""
  149. if "SAE" not in dev[0].get_capability("auth_alg"):
  150. raise HwsimSkip("SAE not supported")
  151. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  152. params['wpa_key_mgmt'] = 'SAE WPA-PSK'
  153. params['sae_anti_clogging_threshold'] = '0'
  154. hostapd.add_ap(apdev[0]['ifname'], params)
  155. dev[2].connect("test-sae", psk="12345678", scan_freq="2412")
  156. for i in range(0, 2):
  157. dev[i].request("SET sae_groups ")
  158. dev[i].connect("test-sae", psk="12345678", key_mgmt="SAE",
  159. scan_freq="2412")
  160. def test_sae_missing_password(dev, apdev):
  161. """SAE and missing password"""
  162. if "SAE" not in dev[0].get_capability("auth_alg"):
  163. raise HwsimSkip("SAE not supported")
  164. params = hostapd.wpa2_params(ssid="test-sae",
  165. passphrase="12345678")
  166. params['wpa_key_mgmt'] = 'SAE'
  167. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  168. dev[0].request("SET sae_groups ")
  169. id = dev[0].connect("test-sae",
  170. raw_psk="46b4a73b8a951ad53ebd2e0afdb9c5483257edd4c21d12b7710759da70945858",
  171. key_mgmt="SAE", scan_freq="2412", wait_connect=False)
  172. ev = dev[0].wait_event(['CTRL-EVENT-SSID-TEMP-DISABLED'], timeout=10)
  173. if ev is None:
  174. raise Exception("Invalid network not temporarily disabled")
  175. def test_sae_key_lifetime_in_memory(dev, apdev, params):
  176. """SAE and key lifetime in memory"""
  177. if "SAE" not in dev[0].get_capability("auth_alg"):
  178. raise HwsimSkip("SAE not supported")
  179. password = "5ad144a7c1f5a5503baa6fa01dabc15b1843e8c01662d78d16b70b5cd23cf8b"
  180. p = hostapd.wpa2_params(ssid="test-sae", passphrase=password)
  181. p['wpa_key_mgmt'] = 'SAE'
  182. hapd = hostapd.add_ap(apdev[0]['ifname'], p)
  183. pid = find_wpas_process(dev[0])
  184. dev[0].request("SET sae_groups ")
  185. id = dev[0].connect("test-sae", psk=password, key_mgmt="SAE",
  186. scan_freq="2412")
  187. time.sleep(0.1)
  188. buf = read_process_memory(pid, password)
  189. dev[0].request("DISCONNECT")
  190. dev[0].wait_disconnected()
  191. dev[0].relog()
  192. sae_k = None
  193. sae_keyseed = None
  194. sae_kck = None
  195. pmk = None
  196. ptk = None
  197. gtk = None
  198. with open(os.path.join(params['logdir'], 'log0'), 'r') as f:
  199. for l in f.readlines():
  200. if "SAE: k - hexdump" in l:
  201. val = l.strip().split(':')[3].replace(' ', '')
  202. sae_k = binascii.unhexlify(val)
  203. if "SAE: keyseed - hexdump" in l:
  204. val = l.strip().split(':')[3].replace(' ', '')
  205. sae_keyseed = binascii.unhexlify(val)
  206. if "SAE: KCK - hexdump" in l:
  207. val = l.strip().split(':')[3].replace(' ', '')
  208. sae_kck = binascii.unhexlify(val)
  209. if "SAE: PMK - hexdump" in l:
  210. val = l.strip().split(':')[3].replace(' ', '')
  211. pmk = binascii.unhexlify(val)
  212. if "WPA: PTK - hexdump" in l:
  213. val = l.strip().split(':')[3].replace(' ', '')
  214. ptk = binascii.unhexlify(val)
  215. if "WPA: Group Key - hexdump" in l:
  216. val = l.strip().split(':')[3].replace(' ', '')
  217. gtk = binascii.unhexlify(val)
  218. if not sae_k or not sae_keyseed or not sae_kck or not pmk or not ptk or not gtk:
  219. raise Exception("Could not find keys from debug log")
  220. if len(gtk) != 16:
  221. raise Exception("Unexpected GTK length")
  222. kck = ptk[0:16]
  223. kek = ptk[16:32]
  224. tk = ptk[32:48]
  225. fname = os.path.join(params['logdir'],
  226. 'sae_key_lifetime_in_memory.memctx-')
  227. logger.info("Checking keys in memory while associated")
  228. get_key_locations(buf, password, "Password")
  229. get_key_locations(buf, pmk, "PMK")
  230. if password not in buf:
  231. raise HwsimSkip("Password not found while associated")
  232. if pmk not in buf:
  233. raise HwsimSkip("PMK not found while associated")
  234. if kck not in buf:
  235. raise Exception("KCK not found while associated")
  236. if kek not in buf:
  237. raise Exception("KEK not found while associated")
  238. if tk in buf:
  239. raise Exception("TK found from memory")
  240. if gtk in buf:
  241. raise Exception("GTK found from memory")
  242. verify_not_present(buf, sae_k, fname, "SAE(k)")
  243. verify_not_present(buf, sae_keyseed, fname, "SAE(keyseed)")
  244. verify_not_present(buf, sae_kck, fname, "SAE(KCK)")
  245. logger.info("Checking keys in memory after disassociation")
  246. buf = read_process_memory(pid, password)
  247. # Note: Password is still present in network configuration
  248. # Note: PMK is in PMKSA cache
  249. get_key_locations(buf, password, "Password")
  250. get_key_locations(buf, pmk, "PMK")
  251. verify_not_present(buf, kck, fname, "KCK")
  252. verify_not_present(buf, kek, fname, "KEK")
  253. verify_not_present(buf, tk, fname, "TK")
  254. verify_not_present(buf, gtk, fname, "GTK")
  255. verify_not_present(buf, sae_k, fname, "SAE(k)")
  256. verify_not_present(buf, sae_keyseed, fname, "SAE(keyseed)")
  257. verify_not_present(buf, sae_kck, fname, "SAE(KCK)")
  258. dev[0].request("PMKSA_FLUSH")
  259. logger.info("Checking keys in memory after PMKSA cache flush")
  260. buf = read_process_memory(pid, password)
  261. get_key_locations(buf, password, "Password")
  262. get_key_locations(buf, pmk, "PMK")
  263. verify_not_present(buf, pmk, fname, "PMK")
  264. dev[0].request("REMOVE_NETWORK all")
  265. logger.info("Checking keys in memory after network profile removal")
  266. buf = read_process_memory(pid, password)
  267. get_key_locations(buf, password, "Password")
  268. get_key_locations(buf, pmk, "PMK")
  269. verify_not_present(buf, password, fname, "password")
  270. verify_not_present(buf, pmk, fname, "PMK")
  271. verify_not_present(buf, kck, fname, "KCK")
  272. verify_not_present(buf, kek, fname, "KEK")
  273. verify_not_present(buf, tk, fname, "TK")
  274. verify_not_present(buf, gtk, fname, "GTK")
  275. verify_not_present(buf, sae_k, fname, "SAE(k)")
  276. verify_not_present(buf, sae_keyseed, fname, "SAE(keyseed)")
  277. verify_not_present(buf, sae_kck, fname, "SAE(KCK)")