test_sae.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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, alloc_fail
  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_password_ecc(dev, apdev):
  38. """SAE with number of different passwords (ECC)"""
  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 19")
  46. for i in range(10):
  47. password = "12345678-" + str(i)
  48. hapd.set("wpa_passphrase", password)
  49. dev[0].connect("test-sae", psk=password, key_mgmt="SAE",
  50. scan_freq="2412")
  51. dev[0].request("REMOVE_NETWORK all")
  52. dev[0].wait_disconnected()
  53. def test_sae_password_ffc(dev, apdev):
  54. """SAE with number of different passwords (FFC)"""
  55. if "SAE" not in dev[0].get_capability("auth_alg"):
  56. raise HwsimSkip("SAE not supported")
  57. params = hostapd.wpa2_params(ssid="test-sae",
  58. passphrase="12345678")
  59. params['wpa_key_mgmt'] = 'SAE'
  60. params['sae_groups'] = '22'
  61. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  62. dev[0].request("SET sae_groups 22")
  63. for i in range(10):
  64. password = "12345678-" + str(i)
  65. hapd.set("wpa_passphrase", password)
  66. dev[0].connect("test-sae", psk=password, key_mgmt="SAE",
  67. scan_freq="2412")
  68. dev[0].request("REMOVE_NETWORK all")
  69. dev[0].wait_disconnected()
  70. def test_sae_pmksa_caching(dev, apdev):
  71. """SAE and PMKSA caching"""
  72. if "SAE" not in dev[0].get_capability("auth_alg"):
  73. raise HwsimSkip("SAE not supported")
  74. params = hostapd.wpa2_params(ssid="test-sae",
  75. passphrase="12345678")
  76. params['wpa_key_mgmt'] = 'SAE'
  77. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  78. dev[0].request("SET sae_groups ")
  79. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  80. scan_freq="2412")
  81. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  82. if ev is None:
  83. raise Exception("No connection event received from hostapd")
  84. dev[0].request("DISCONNECT")
  85. dev[0].wait_disconnected()
  86. dev[0].request("RECONNECT")
  87. dev[0].wait_connected(timeout=15, error="Reconnect timed out")
  88. if dev[0].get_status_field('sae_group') is not None:
  89. raise Exception("SAE group claimed to have been used")
  90. def test_sae_pmksa_caching_disabled(dev, apdev):
  91. """SAE and PMKSA caching disabled"""
  92. if "SAE" not in dev[0].get_capability("auth_alg"):
  93. raise HwsimSkip("SAE not supported")
  94. params = hostapd.wpa2_params(ssid="test-sae",
  95. passphrase="12345678")
  96. params['wpa_key_mgmt'] = 'SAE'
  97. params['disable_pmksa_caching'] = '1'
  98. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  99. dev[0].request("SET sae_groups ")
  100. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  101. scan_freq="2412")
  102. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  103. if ev is None:
  104. raise Exception("No connection event received from hostapd")
  105. dev[0].request("DISCONNECT")
  106. dev[0].wait_disconnected()
  107. dev[0].request("RECONNECT")
  108. dev[0].wait_connected(timeout=15, error="Reconnect timed out")
  109. if dev[0].get_status_field('sae_group') != '19':
  110. raise Exception("Expected default SAE group not used")
  111. def test_sae_groups(dev, apdev):
  112. """SAE with all supported groups"""
  113. if "SAE" not in dev[0].get_capability("auth_alg"):
  114. raise HwsimSkip("SAE not supported")
  115. # This would be the full list of supported groups, but groups 14-16
  116. # (2048-4096 bit MODP) are a bit too slow on some VMs and can result in
  117. # hitting mac80211 authentication timeout, so skip them for now.
  118. #sae_groups = [ 19, 25, 26, 20, 21, 2, 5, 14, 15, 16, 22, 23, 24 ]
  119. sae_groups = [ 19, 25, 26, 20, 21, 2, 5, 22, 23, 24 ]
  120. groups = [str(g) for g in sae_groups]
  121. params = hostapd.wpa2_params(ssid="test-sae-groups",
  122. passphrase="12345678")
  123. params['wpa_key_mgmt'] = 'SAE'
  124. params['sae_groups'] = ' '.join(groups)
  125. hostapd.add_ap(apdev[0]['ifname'], params)
  126. for g in groups:
  127. logger.info("Testing SAE group " + g)
  128. dev[0].request("SET sae_groups " + g)
  129. id = dev[0].connect("test-sae-groups", psk="12345678", key_mgmt="SAE",
  130. scan_freq="2412")
  131. if dev[0].get_status_field('sae_group') != g:
  132. raise Exception("Expected SAE group not used")
  133. dev[0].remove_network(id)
  134. def test_sae_group_nego(dev, apdev):
  135. """SAE group negotiation"""
  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-group-nego",
  139. passphrase="12345678")
  140. params['wpa_key_mgmt'] = 'SAE'
  141. params['sae_groups'] = '19'
  142. hostapd.add_ap(apdev[0]['ifname'], params)
  143. dev[0].request("SET sae_groups 25 26 20 19")
  144. dev[0].connect("test-sae-group-nego", psk="12345678", key_mgmt="SAE",
  145. scan_freq="2412")
  146. if dev[0].get_status_field('sae_group') != '19':
  147. raise Exception("Expected SAE group not used")
  148. def test_sae_anti_clogging(dev, apdev):
  149. """SAE anti clogging"""
  150. if "SAE" not in dev[0].get_capability("auth_alg"):
  151. raise HwsimSkip("SAE not supported")
  152. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  153. params['wpa_key_mgmt'] = 'SAE'
  154. params['sae_anti_clogging_threshold'] = '1'
  155. hostapd.add_ap(apdev[0]['ifname'], params)
  156. dev[0].request("SET sae_groups ")
  157. dev[1].request("SET sae_groups ")
  158. id = {}
  159. for i in range(0, 2):
  160. dev[i].scan(freq="2412")
  161. id[i] = dev[i].connect("test-sae", psk="12345678", key_mgmt="SAE",
  162. scan_freq="2412", only_add_network=True)
  163. for i in range(0, 2):
  164. dev[i].select_network(id[i])
  165. for i in range(0, 2):
  166. dev[i].wait_connected(timeout=10)
  167. def test_sae_forced_anti_clogging(dev, apdev):
  168. """SAE anti clogging (forced)"""
  169. if "SAE" not in dev[0].get_capability("auth_alg"):
  170. raise HwsimSkip("SAE not supported")
  171. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  172. params['wpa_key_mgmt'] = 'SAE WPA-PSK'
  173. params['sae_anti_clogging_threshold'] = '0'
  174. hostapd.add_ap(apdev[0]['ifname'], params)
  175. dev[2].connect("test-sae", psk="12345678", scan_freq="2412")
  176. for i in range(0, 2):
  177. dev[i].request("SET sae_groups ")
  178. dev[i].connect("test-sae", psk="12345678", key_mgmt="SAE",
  179. scan_freq="2412")
  180. def test_sae_mixed(dev, apdev):
  181. """Mixed SAE and non-SAE network"""
  182. if "SAE" not in dev[0].get_capability("auth_alg"):
  183. raise HwsimSkip("SAE not supported")
  184. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  185. params['wpa_key_mgmt'] = 'SAE WPA-PSK'
  186. params['sae_anti_clogging_threshold'] = '0'
  187. hostapd.add_ap(apdev[0]['ifname'], params)
  188. dev[2].connect("test-sae", psk="12345678", scan_freq="2412")
  189. for i in range(0, 2):
  190. dev[i].request("SET sae_groups ")
  191. dev[i].connect("test-sae", psk="12345678", key_mgmt="SAE",
  192. scan_freq="2412")
  193. def test_sae_missing_password(dev, apdev):
  194. """SAE and missing password"""
  195. if "SAE" not in dev[0].get_capability("auth_alg"):
  196. raise HwsimSkip("SAE not supported")
  197. params = hostapd.wpa2_params(ssid="test-sae",
  198. passphrase="12345678")
  199. params['wpa_key_mgmt'] = 'SAE'
  200. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  201. dev[0].request("SET sae_groups ")
  202. id = dev[0].connect("test-sae",
  203. raw_psk="46b4a73b8a951ad53ebd2e0afdb9c5483257edd4c21d12b7710759da70945858",
  204. key_mgmt="SAE", scan_freq="2412", wait_connect=False)
  205. ev = dev[0].wait_event(['CTRL-EVENT-SSID-TEMP-DISABLED'], timeout=10)
  206. if ev is None:
  207. raise Exception("Invalid network not temporarily disabled")
  208. def test_sae_key_lifetime_in_memory(dev, apdev, params):
  209. """SAE and key lifetime in memory"""
  210. if "SAE" not in dev[0].get_capability("auth_alg"):
  211. raise HwsimSkip("SAE not supported")
  212. password = "5ad144a7c1f5a5503baa6fa01dabc15b1843e8c01662d78d16b70b5cd23cf8b"
  213. p = hostapd.wpa2_params(ssid="test-sae", passphrase=password)
  214. p['wpa_key_mgmt'] = 'SAE'
  215. hapd = hostapd.add_ap(apdev[0]['ifname'], p)
  216. pid = find_wpas_process(dev[0])
  217. dev[0].request("SET sae_groups ")
  218. id = dev[0].connect("test-sae", psk=password, key_mgmt="SAE",
  219. scan_freq="2412")
  220. time.sleep(1)
  221. buf = read_process_memory(pid, password)
  222. dev[0].request("DISCONNECT")
  223. dev[0].wait_disconnected()
  224. dev[0].relog()
  225. sae_k = None
  226. sae_keyseed = None
  227. sae_kck = None
  228. pmk = None
  229. ptk = None
  230. gtk = None
  231. with open(os.path.join(params['logdir'], 'log0'), 'r') as f:
  232. for l in f.readlines():
  233. if "SAE: k - hexdump" in l:
  234. val = l.strip().split(':')[3].replace(' ', '')
  235. sae_k = binascii.unhexlify(val)
  236. if "SAE: keyseed - hexdump" in l:
  237. val = l.strip().split(':')[3].replace(' ', '')
  238. sae_keyseed = binascii.unhexlify(val)
  239. if "SAE: KCK - hexdump" in l:
  240. val = l.strip().split(':')[3].replace(' ', '')
  241. sae_kck = binascii.unhexlify(val)
  242. if "SAE: PMK - hexdump" in l:
  243. val = l.strip().split(':')[3].replace(' ', '')
  244. pmk = binascii.unhexlify(val)
  245. if "WPA: PTK - hexdump" in l:
  246. val = l.strip().split(':')[3].replace(' ', '')
  247. ptk = binascii.unhexlify(val)
  248. if "WPA: Group Key - hexdump" in l:
  249. val = l.strip().split(':')[3].replace(' ', '')
  250. gtk = binascii.unhexlify(val)
  251. if not sae_k or not sae_keyseed or not sae_kck or not pmk or not ptk or not gtk:
  252. raise Exception("Could not find keys from debug log")
  253. if len(gtk) != 16:
  254. raise Exception("Unexpected GTK length")
  255. kck = ptk[0:16]
  256. kek = ptk[16:32]
  257. tk = ptk[32:48]
  258. fname = os.path.join(params['logdir'],
  259. 'sae_key_lifetime_in_memory.memctx-')
  260. logger.info("Checking keys in memory while associated")
  261. get_key_locations(buf, password, "Password")
  262. get_key_locations(buf, pmk, "PMK")
  263. if password not in buf:
  264. raise HwsimSkip("Password not found while associated")
  265. if pmk not in buf:
  266. raise HwsimSkip("PMK not found while associated")
  267. if kck not in buf:
  268. raise Exception("KCK not found while associated")
  269. if kek not in buf:
  270. raise Exception("KEK not found while associated")
  271. if tk in buf:
  272. raise Exception("TK found from memory")
  273. if gtk in buf:
  274. raise Exception("GTK found from memory")
  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)")
  278. logger.info("Checking keys in memory after disassociation")
  279. buf = read_process_memory(pid, password)
  280. # Note: Password is still present in network configuration
  281. # Note: PMK is in PMKSA cache
  282. get_key_locations(buf, password, "Password")
  283. get_key_locations(buf, pmk, "PMK")
  284. verify_not_present(buf, kck, fname, "KCK")
  285. verify_not_present(buf, kek, fname, "KEK")
  286. verify_not_present(buf, tk, fname, "TK")
  287. verify_not_present(buf, gtk, fname, "GTK")
  288. verify_not_present(buf, sae_k, fname, "SAE(k)")
  289. verify_not_present(buf, sae_keyseed, fname, "SAE(keyseed)")
  290. verify_not_present(buf, sae_kck, fname, "SAE(KCK)")
  291. dev[0].request("PMKSA_FLUSH")
  292. logger.info("Checking keys in memory after PMKSA cache flush")
  293. buf = read_process_memory(pid, password)
  294. get_key_locations(buf, password, "Password")
  295. get_key_locations(buf, pmk, "PMK")
  296. verify_not_present(buf, pmk, fname, "PMK")
  297. dev[0].request("REMOVE_NETWORK all")
  298. logger.info("Checking keys in memory after network profile removal")
  299. buf = read_process_memory(pid, password)
  300. get_key_locations(buf, password, "Password")
  301. get_key_locations(buf, pmk, "PMK")
  302. verify_not_present(buf, password, fname, "password")
  303. verify_not_present(buf, pmk, fname, "PMK")
  304. verify_not_present(buf, kck, fname, "KCK")
  305. verify_not_present(buf, kek, fname, "KEK")
  306. verify_not_present(buf, tk, fname, "TK")
  307. verify_not_present(buf, gtk, fname, "GTK")
  308. verify_not_present(buf, sae_k, fname, "SAE(k)")
  309. verify_not_present(buf, sae_keyseed, fname, "SAE(keyseed)")
  310. verify_not_present(buf, sae_kck, fname, "SAE(KCK)")
  311. def test_sae_oom_wpas(dev, apdev):
  312. """SAE and OOM in wpa_supplicant"""
  313. if "SAE" not in dev[0].get_capability("auth_alg"):
  314. raise HwsimSkip("SAE not supported")
  315. params = hostapd.wpa2_params(ssid="test-sae",
  316. passphrase="12345678")
  317. params['wpa_key_mgmt'] = 'SAE'
  318. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  319. dev[0].request("SET sae_groups 25")
  320. with alloc_fail(dev[0], 1, "sae_set_group"):
  321. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  322. scan_freq="2412")
  323. dev[0].request("REMOVE_NETWORK all")
  324. dev[0].request("SET sae_groups ")
  325. with alloc_fail(dev[0], 2, "sae_set_group"):
  326. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  327. scan_freq="2412")
  328. dev[0].request("REMOVE_NETWORK all")
  329. def test_sae_proto_ecc(dev, apdev):
  330. """SAE protocol testing (ECC)"""
  331. if "SAE" not in dev[0].get_capability("auth_alg"):
  332. raise HwsimSkip("SAE not supported")
  333. params = hostapd.wpa2_params(ssid="test-sae",
  334. passphrase="12345678")
  335. params['wpa_key_mgmt'] = 'SAE'
  336. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  337. bssid = apdev[0]['bssid']
  338. dev[0].request("SET sae_groups 19")
  339. tests = [ ("Confirm mismatch",
  340. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  341. "0000800edebc3f260dc1fe7e0b20888af2b8a3316252ec37388a8504e25b73dc4240"),
  342. ("Commit without even full cyclic group field",
  343. "13",
  344. None),
  345. ("Too short commit",
  346. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02",
  347. None),
  348. ("Invalid commit scalar (0)",
  349. "1300" + "0000000000000000000000000000000000000000000000000000000000000000" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  350. None),
  351. ("Invalid commit scalar (> r)",
  352. "1300" + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  353. None),
  354. ("Commit element not on curve",
  355. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728d0000000000000000000000000000000000000000000000000000000000000000",
  356. None),
  357. ("Invalid commit element (y coordinate > P)",
  358. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  359. None),
  360. ("Invalid commit element (x coordinate > P)",
  361. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  362. None),
  363. ("Different group in commit",
  364. "1400" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  365. None),
  366. ("Too short confirm",
  367. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  368. "0000800edebc3f260dc1fe7e0b20888af2b8a3316252ec37388a8504e25b73dc42")]
  369. for (note, commit, confirm) in tests:
  370. logger.info(note)
  371. dev[0].scan_for_bss(bssid, freq=2412)
  372. hapd.set("ext_mgmt_frame_handling", "1")
  373. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  374. scan_freq="2412", wait_connect=False)
  375. logger.info("Commit")
  376. for i in range(0, 10):
  377. req = hapd.mgmt_rx()
  378. if req is None:
  379. raise Exception("MGMT RX wait timed out (commit)")
  380. if req['subtype'] == 11:
  381. break
  382. req = None
  383. if not req:
  384. raise Exception("Authentication frame (commit) not received")
  385. hapd.dump_monitor()
  386. resp = {}
  387. resp['fc'] = req['fc']
  388. resp['da'] = req['sa']
  389. resp['sa'] = req['da']
  390. resp['bssid'] = req['bssid']
  391. resp['payload'] = binascii.unhexlify("030001000000" + commit)
  392. hapd.mgmt_tx(resp)
  393. if confirm:
  394. logger.info("Confirm")
  395. for i in range(0, 10):
  396. req = hapd.mgmt_rx()
  397. if req is None:
  398. raise Exception("MGMT RX wait timed out (confirm)")
  399. if req['subtype'] == 11:
  400. break
  401. req = None
  402. if not req:
  403. raise Exception("Authentication frame (confirm) not received")
  404. hapd.dump_monitor()
  405. resp = {}
  406. resp['fc'] = req['fc']
  407. resp['da'] = req['sa']
  408. resp['sa'] = req['da']
  409. resp['bssid'] = req['bssid']
  410. resp['payload'] = binascii.unhexlify("030002000000" + confirm)
  411. hapd.mgmt_tx(resp)
  412. time.sleep(0.1)
  413. dev[0].request("REMOVE_NETWORK all")
  414. hapd.set("ext_mgmt_frame_handling", "0")
  415. hapd.dump_monitor()
  416. def test_sae_proto_ffc(dev, apdev):
  417. """SAE protocol testing (FFC)"""
  418. if "SAE" not in dev[0].get_capability("auth_alg"):
  419. raise HwsimSkip("SAE not supported")
  420. params = hostapd.wpa2_params(ssid="test-sae",
  421. passphrase="12345678")
  422. params['wpa_key_mgmt'] = 'SAE'
  423. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  424. bssid = apdev[0]['bssid']
  425. dev[0].request("SET sae_groups 2")
  426. tests = [ ("Confirm mismatch",
  427. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "a8c00117493cdffa5dd671e934bc9cb1a69f39e25e9dd9cd9afd3aea2441a0f5491211c7ba50a753563f9ce943b043557cb71193b28e86ed9544f4289c471bf91b70af5c018cf4663e004165b0fd0bc1d8f3f78adf42eee92bcbc55246fd3ee9f107ab965dc7d4986f23eb71d616ebfe6bfe0a6c1ac5dc1718acee17c9a17486",
  428. "0000f3116a9731f1259622e3eb55d4b3b50ba16f8c5f5565b28e609b180c51460251"),
  429. ("Too short commit",
  430. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "a8c00117493cdffa5dd671e934bc9cb1a69f39e25e9dd9cd9afd3aea2441a0f5491211c7ba50a753563f9ce943b043557cb71193b28e86ed9544f4289c471bf91b70af5c018cf4663e004165b0fd0bc1d8f3f78adf42eee92bcbc55246fd3ee9f107ab965dc7d4986f23eb71d616ebfe6bfe0a6c1ac5dc1718acee17c9a174",
  431. None),
  432. ("Invalid element (0) in commit",
  433. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  434. None),
  435. ("Invalid element (1) in commit",
  436. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
  437. None),
  438. ("Invalid element (> P) in commit",
  439. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  440. None) ]
  441. for (note, commit, confirm) in tests:
  442. logger.info(note)
  443. dev[0].scan_for_bss(bssid, freq=2412)
  444. hapd.set("ext_mgmt_frame_handling", "1")
  445. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  446. scan_freq="2412", wait_connect=False)
  447. logger.info("Commit")
  448. for i in range(0, 10):
  449. req = hapd.mgmt_rx()
  450. if req is None:
  451. raise Exception("MGMT RX wait timed out (commit)")
  452. if req['subtype'] == 11:
  453. break
  454. req = None
  455. if not req:
  456. raise Exception("Authentication frame (commit) not received")
  457. hapd.dump_monitor()
  458. resp = {}
  459. resp['fc'] = req['fc']
  460. resp['da'] = req['sa']
  461. resp['sa'] = req['da']
  462. resp['bssid'] = req['bssid']
  463. resp['payload'] = binascii.unhexlify("030001000000" + commit)
  464. hapd.mgmt_tx(resp)
  465. if confirm:
  466. logger.info("Confirm")
  467. for i in range(0, 10):
  468. req = hapd.mgmt_rx()
  469. if req is None:
  470. raise Exception("MGMT RX wait timed out (confirm)")
  471. if req['subtype'] == 11:
  472. break
  473. req = None
  474. if not req:
  475. raise Exception("Authentication frame (confirm) not received")
  476. hapd.dump_monitor()
  477. resp = {}
  478. resp['fc'] = req['fc']
  479. resp['da'] = req['sa']
  480. resp['sa'] = req['da']
  481. resp['bssid'] = req['bssid']
  482. resp['payload'] = binascii.unhexlify("030002000000" + confirm)
  483. hapd.mgmt_tx(resp)
  484. time.sleep(0.1)
  485. dev[0].request("REMOVE_NETWORK all")
  486. hapd.set("ext_mgmt_frame_handling", "0")
  487. hapd.dump_monitor()
  488. def test_sae_no_ffc_by_default(dev, apdev):
  489. """SAE and default groups rejecting FFC"""
  490. if "SAE" not in dev[0].get_capability("auth_alg"):
  491. raise HwsimSkip("SAE not supported")
  492. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  493. params['wpa_key_mgmt'] = 'SAE'
  494. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  495. dev[0].request("SET sae_groups 5")
  496. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE", scan_freq="2412",
  497. wait_connect=False)
  498. ev = dev[0].wait_event(["SME: Trying to authenticate"], timeout=3)
  499. if ev is None:
  500. raise Exception("Did not try to authenticate")
  501. ev = dev[0].wait_event(["SME: Trying to authenticate"], timeout=3)
  502. if ev is None:
  503. raise Exception("Did not try to authenticate (2)")
  504. dev[0].request("REMOVE_NETWORK all")
  505. def sae_reflection_attack(apdev, dev, group):
  506. if "SAE" not in dev.get_capability("auth_alg"):
  507. raise HwsimSkip("SAE not supported")
  508. params = hostapd.wpa2_params(ssid="test-sae",
  509. passphrase="no-knowledge-of-passphrase")
  510. params['wpa_key_mgmt'] = 'SAE'
  511. hapd = hostapd.add_ap(apdev['ifname'], params)
  512. bssid = apdev['bssid']
  513. dev.scan_for_bss(bssid, freq=2412)
  514. hapd.set("ext_mgmt_frame_handling", "1")
  515. dev.request("SET sae_groups %d" % group)
  516. dev.connect("test-sae", psk="reflection-attack", key_mgmt="SAE",
  517. scan_freq="2412", wait_connect=False)
  518. # Commit
  519. for i in range(0, 10):
  520. req = hapd.mgmt_rx()
  521. if req is None:
  522. raise Exception("MGMT RX wait timed out")
  523. if req['subtype'] == 11:
  524. break
  525. req = None
  526. if not req:
  527. raise Exception("Authentication frame not received")
  528. resp = {}
  529. resp['fc'] = req['fc']
  530. resp['da'] = req['sa']
  531. resp['sa'] = req['da']
  532. resp['bssid'] = req['bssid']
  533. resp['payload'] = req['payload']
  534. hapd.mgmt_tx(resp)
  535. # Confirm
  536. req = hapd.mgmt_rx(timeout=0.5)
  537. if req is not None:
  538. if req['subtype'] == 11:
  539. raise Exception("Unexpected Authentication frame seen")
  540. def test_sae_reflection_attack_ecc(dev, apdev):
  541. """SAE reflection attack (ECC)"""
  542. sae_reflection_attack(apdev[0], dev[0], 19)
  543. def test_sae_reflection_attack_ffc(dev, apdev):
  544. """SAE reflection attack (FFC)"""
  545. sae_reflection_attack(apdev[0], dev[0], 5)
  546. def test_sae_anti_clogging_proto(dev, apdev):
  547. """SAE anti clogging protocol testing"""
  548. if "SAE" not in dev[0].get_capability("auth_alg"):
  549. raise HwsimSkip("SAE not supported")
  550. params = hostapd.wpa2_params(ssid="test-sae",
  551. passphrase="no-knowledge-of-passphrase")
  552. params['wpa_key_mgmt'] = 'SAE'
  553. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  554. bssid = apdev[0]['bssid']
  555. dev[0].scan_for_bss(bssid, freq=2412)
  556. hapd.set("ext_mgmt_frame_handling", "1")
  557. dev[0].request("SET sae_groups ")
  558. dev[0].connect("test-sae", psk="anti-cloggign", key_mgmt="SAE",
  559. scan_freq="2412", wait_connect=False)
  560. # Commit
  561. for i in range(0, 10):
  562. req = hapd.mgmt_rx()
  563. if req is None:
  564. raise Exception("MGMT RX wait timed out")
  565. if req['subtype'] == 11:
  566. break
  567. req = None
  568. if not req:
  569. raise Exception("Authentication frame not received")
  570. resp = {}
  571. resp['fc'] = req['fc']
  572. resp['da'] = req['sa']
  573. resp['sa'] = req['da']
  574. resp['bssid'] = req['bssid']
  575. resp['payload'] = binascii.unhexlify("030001004c00" + "ffff00")
  576. hapd.mgmt_tx(resp)
  577. # Confirm (not received due to DH group being rejected)
  578. req = hapd.mgmt_rx(timeout=0.5)
  579. if req is not None:
  580. if req['subtype'] == 11:
  581. raise Exception("Unexpected Authentication frame seen")