test_sae.py 11 KB

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