test_sae.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. # Test cases for SAE
  2. # Copyright (c) 2013-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 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, fail_test
  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 is the full list of supported groups, but groups 14-16 (2048-4096 bit
  116. # MODP) and group 21 (521-bit random ECP group) are a bit too slow on some
  117. # VMs and can result in hitting the mac80211 authentication timeout, so
  118. # allow them to fail and just report such failures in the debug log.
  119. sae_groups = [ 19, 25, 26, 20, 21, 2, 5, 14, 15, 16, 22, 23, 24 ]
  120. tls = dev[0].request("GET tls_library")
  121. if tls.startswith("OpenSSL") and "build=OpenSSL 1.0.2" in tls and "run=OpenSSL 1.0.2" in tls:
  122. logger.info("Add Brainpool EC groups since OpenSSL is new enough")
  123. sae_groups += [ 27, 28, 29, 30 ]
  124. heavy_groups = [ 14, 15, 16 ]
  125. groups = [str(g) for g in sae_groups]
  126. params = hostapd.wpa2_params(ssid="test-sae-groups",
  127. passphrase="12345678")
  128. params['wpa_key_mgmt'] = 'SAE'
  129. params['sae_groups'] = ' '.join(groups)
  130. hostapd.add_ap(apdev[0]['ifname'], params)
  131. for g in groups:
  132. logger.info("Testing SAE group " + g)
  133. dev[0].request("SET sae_groups " + g)
  134. id = dev[0].connect("test-sae-groups", psk="12345678", key_mgmt="SAE",
  135. scan_freq="2412", wait_connect=False)
  136. if int(g) in heavy_groups:
  137. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=5)
  138. if ev is None:
  139. logger.info("No connection with heavy SAE group %s did not connect - likely hitting timeout in mac80211" % g)
  140. dev[0].remove_network(id)
  141. time.sleep(0.1)
  142. dev[0].dump_monitor()
  143. continue
  144. logger.info("Connection with heavy SAE group " + g)
  145. else:
  146. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
  147. if ev is None:
  148. if "BoringSSL" in tls and int(g) in [ 25 ]:
  149. logger.info("Ignore connection failure with group " + g + " with BoringSSL")
  150. dev[0].remove_network(id)
  151. dev[0].dump_monitor()
  152. continue
  153. raise Exception("Connection timed out with group " + g)
  154. if dev[0].get_status_field('sae_group') != g:
  155. raise Exception("Expected SAE group not used")
  156. dev[0].remove_network(id)
  157. dev[0].wait_disconnected()
  158. dev[0].dump_monitor()
  159. def test_sae_group_nego(dev, apdev):
  160. """SAE group negotiation"""
  161. if "SAE" not in dev[0].get_capability("auth_alg"):
  162. raise HwsimSkip("SAE not supported")
  163. params = hostapd.wpa2_params(ssid="test-sae-group-nego",
  164. passphrase="12345678")
  165. params['wpa_key_mgmt'] = 'SAE'
  166. params['sae_groups'] = '19'
  167. hostapd.add_ap(apdev[0]['ifname'], params)
  168. dev[0].request("SET sae_groups 25 26 20 19")
  169. dev[0].connect("test-sae-group-nego", psk="12345678", key_mgmt="SAE",
  170. scan_freq="2412")
  171. if dev[0].get_status_field('sae_group') != '19':
  172. raise Exception("Expected SAE group not used")
  173. def test_sae_anti_clogging(dev, apdev):
  174. """SAE anti clogging"""
  175. if "SAE" not in dev[0].get_capability("auth_alg"):
  176. raise HwsimSkip("SAE not supported")
  177. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  178. params['wpa_key_mgmt'] = 'SAE'
  179. params['sae_anti_clogging_threshold'] = '1'
  180. hostapd.add_ap(apdev[0]['ifname'], params)
  181. dev[0].request("SET sae_groups ")
  182. dev[1].request("SET sae_groups ")
  183. id = {}
  184. for i in range(0, 2):
  185. dev[i].scan(freq="2412")
  186. id[i] = dev[i].connect("test-sae", psk="12345678", key_mgmt="SAE",
  187. scan_freq="2412", only_add_network=True)
  188. for i in range(0, 2):
  189. dev[i].select_network(id[i])
  190. for i in range(0, 2):
  191. dev[i].wait_connected(timeout=10)
  192. def test_sae_forced_anti_clogging(dev, apdev):
  193. """SAE anti clogging (forced)"""
  194. if "SAE" not in dev[0].get_capability("auth_alg"):
  195. raise HwsimSkip("SAE not supported")
  196. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  197. params['wpa_key_mgmt'] = 'SAE WPA-PSK'
  198. params['sae_anti_clogging_threshold'] = '0'
  199. hostapd.add_ap(apdev[0]['ifname'], params)
  200. dev[2].connect("test-sae", psk="12345678", scan_freq="2412")
  201. for i in range(0, 2):
  202. dev[i].request("SET sae_groups ")
  203. dev[i].connect("test-sae", psk="12345678", key_mgmt="SAE",
  204. scan_freq="2412")
  205. def test_sae_mixed(dev, apdev):
  206. """Mixed SAE and non-SAE network"""
  207. if "SAE" not in dev[0].get_capability("auth_alg"):
  208. raise HwsimSkip("SAE not supported")
  209. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  210. params['wpa_key_mgmt'] = 'SAE WPA-PSK'
  211. params['sae_anti_clogging_threshold'] = '0'
  212. hostapd.add_ap(apdev[0]['ifname'], params)
  213. dev[2].connect("test-sae", psk="12345678", scan_freq="2412")
  214. for i in range(0, 2):
  215. dev[i].request("SET sae_groups ")
  216. dev[i].connect("test-sae", psk="12345678", key_mgmt="SAE",
  217. scan_freq="2412")
  218. def test_sae_missing_password(dev, apdev):
  219. """SAE and missing password"""
  220. if "SAE" not in dev[0].get_capability("auth_alg"):
  221. raise HwsimSkip("SAE not supported")
  222. params = hostapd.wpa2_params(ssid="test-sae",
  223. passphrase="12345678")
  224. params['wpa_key_mgmt'] = 'SAE'
  225. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  226. dev[0].request("SET sae_groups ")
  227. id = dev[0].connect("test-sae",
  228. raw_psk="46b4a73b8a951ad53ebd2e0afdb9c5483257edd4c21d12b7710759da70945858",
  229. key_mgmt="SAE", scan_freq="2412", wait_connect=False)
  230. ev = dev[0].wait_event(['CTRL-EVENT-SSID-TEMP-DISABLED'], timeout=10)
  231. if ev is None:
  232. raise Exception("Invalid network not temporarily disabled")
  233. def test_sae_key_lifetime_in_memory(dev, apdev, params):
  234. """SAE and key lifetime in memory"""
  235. if "SAE" not in dev[0].get_capability("auth_alg"):
  236. raise HwsimSkip("SAE not supported")
  237. password = "5ad144a7c1f5a5503baa6fa01dabc15b1843e8c01662d78d16b70b5cd23cf8b"
  238. p = hostapd.wpa2_params(ssid="test-sae", passphrase=password)
  239. p['wpa_key_mgmt'] = 'SAE'
  240. hapd = hostapd.add_ap(apdev[0]['ifname'], p)
  241. pid = find_wpas_process(dev[0])
  242. dev[0].request("SET sae_groups ")
  243. id = dev[0].connect("test-sae", psk=password, key_mgmt="SAE",
  244. scan_freq="2412")
  245. time.sleep(1)
  246. buf = read_process_memory(pid, password)
  247. dev[0].request("DISCONNECT")
  248. dev[0].wait_disconnected()
  249. dev[0].relog()
  250. sae_k = None
  251. sae_keyseed = None
  252. sae_kck = None
  253. pmk = None
  254. ptk = None
  255. gtk = None
  256. with open(os.path.join(params['logdir'], 'log0'), 'r') as f:
  257. for l in f.readlines():
  258. if "SAE: k - hexdump" in l:
  259. val = l.strip().split(':')[3].replace(' ', '')
  260. sae_k = binascii.unhexlify(val)
  261. if "SAE: keyseed - hexdump" in l:
  262. val = l.strip().split(':')[3].replace(' ', '')
  263. sae_keyseed = binascii.unhexlify(val)
  264. if "SAE: KCK - hexdump" in l:
  265. val = l.strip().split(':')[3].replace(' ', '')
  266. sae_kck = binascii.unhexlify(val)
  267. if "SAE: PMK - hexdump" in l:
  268. val = l.strip().split(':')[3].replace(' ', '')
  269. pmk = binascii.unhexlify(val)
  270. if "WPA: PTK - hexdump" in l:
  271. val = l.strip().split(':')[3].replace(' ', '')
  272. ptk = binascii.unhexlify(val)
  273. if "WPA: Group Key - hexdump" in l:
  274. val = l.strip().split(':')[3].replace(' ', '')
  275. gtk = binascii.unhexlify(val)
  276. if not sae_k or not sae_keyseed or not sae_kck or not pmk or not ptk or not gtk:
  277. raise Exception("Could not find keys from debug log")
  278. if len(gtk) != 16:
  279. raise Exception("Unexpected GTK length")
  280. kck = ptk[0:16]
  281. kek = ptk[16:32]
  282. tk = ptk[32:48]
  283. fname = os.path.join(params['logdir'],
  284. 'sae_key_lifetime_in_memory.memctx-')
  285. logger.info("Checking keys in memory while associated")
  286. get_key_locations(buf, password, "Password")
  287. get_key_locations(buf, pmk, "PMK")
  288. if password not in buf:
  289. raise HwsimSkip("Password not found while associated")
  290. if pmk not in buf:
  291. raise HwsimSkip("PMK not found while associated")
  292. if kck not in buf:
  293. raise Exception("KCK not found while associated")
  294. if kek not in buf:
  295. raise Exception("KEK not found while associated")
  296. if tk in buf:
  297. raise Exception("TK found from memory")
  298. if gtk in buf:
  299. raise Exception("GTK found from memory")
  300. verify_not_present(buf, sae_k, fname, "SAE(k)")
  301. verify_not_present(buf, sae_keyseed, fname, "SAE(keyseed)")
  302. verify_not_present(buf, sae_kck, fname, "SAE(KCK)")
  303. logger.info("Checking keys in memory after disassociation")
  304. buf = read_process_memory(pid, password)
  305. # Note: Password is still present in network configuration
  306. # Note: PMK is in PMKSA cache
  307. get_key_locations(buf, password, "Password")
  308. get_key_locations(buf, pmk, "PMK")
  309. verify_not_present(buf, kck, fname, "KCK")
  310. verify_not_present(buf, kek, fname, "KEK")
  311. verify_not_present(buf, tk, fname, "TK")
  312. verify_not_present(buf, gtk, fname, "GTK")
  313. verify_not_present(buf, sae_k, fname, "SAE(k)")
  314. verify_not_present(buf, sae_keyseed, fname, "SAE(keyseed)")
  315. verify_not_present(buf, sae_kck, fname, "SAE(KCK)")
  316. dev[0].request("PMKSA_FLUSH")
  317. logger.info("Checking keys in memory after PMKSA cache flush")
  318. buf = read_process_memory(pid, password)
  319. get_key_locations(buf, password, "Password")
  320. get_key_locations(buf, pmk, "PMK")
  321. verify_not_present(buf, pmk, fname, "PMK")
  322. dev[0].request("REMOVE_NETWORK all")
  323. logger.info("Checking keys in memory after network profile removal")
  324. buf = read_process_memory(pid, password)
  325. get_key_locations(buf, password, "Password")
  326. get_key_locations(buf, pmk, "PMK")
  327. verify_not_present(buf, password, fname, "password")
  328. verify_not_present(buf, pmk, fname, "PMK")
  329. verify_not_present(buf, kck, fname, "KCK")
  330. verify_not_present(buf, kek, fname, "KEK")
  331. verify_not_present(buf, tk, fname, "TK")
  332. verify_not_present(buf, gtk, fname, "GTK")
  333. verify_not_present(buf, sae_k, fname, "SAE(k)")
  334. verify_not_present(buf, sae_keyseed, fname, "SAE(keyseed)")
  335. verify_not_present(buf, sae_kck, fname, "SAE(KCK)")
  336. def test_sae_oom_wpas(dev, apdev):
  337. """SAE and OOM in wpa_supplicant"""
  338. if "SAE" not in dev[0].get_capability("auth_alg"):
  339. raise HwsimSkip("SAE not supported")
  340. params = hostapd.wpa2_params(ssid="test-sae",
  341. passphrase="12345678")
  342. params['wpa_key_mgmt'] = 'SAE'
  343. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  344. dev[0].request("SET sae_groups 25")
  345. tls = dev[0].request("GET tls_library")
  346. if "BoringSSL" in tls:
  347. dev[0].request("SET sae_groups 26")
  348. with alloc_fail(dev[0], 1, "sae_set_group"):
  349. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  350. scan_freq="2412")
  351. dev[0].request("REMOVE_NETWORK all")
  352. dev[0].request("SET sae_groups ")
  353. with alloc_fail(dev[0], 2, "sae_set_group"):
  354. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  355. scan_freq="2412")
  356. dev[0].request("REMOVE_NETWORK all")
  357. def test_sae_proto_ecc(dev, apdev):
  358. """SAE protocol testing (ECC)"""
  359. if "SAE" not in dev[0].get_capability("auth_alg"):
  360. raise HwsimSkip("SAE not supported")
  361. params = hostapd.wpa2_params(ssid="test-sae",
  362. passphrase="12345678")
  363. params['wpa_key_mgmt'] = 'SAE'
  364. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  365. bssid = apdev[0]['bssid']
  366. dev[0].request("SET sae_groups 19")
  367. tests = [ ("Confirm mismatch",
  368. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  369. "0000800edebc3f260dc1fe7e0b20888af2b8a3316252ec37388a8504e25b73dc4240"),
  370. ("Commit without even full cyclic group field",
  371. "13",
  372. None),
  373. ("Too short commit",
  374. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02",
  375. None),
  376. ("Invalid commit scalar (0)",
  377. "1300" + "0000000000000000000000000000000000000000000000000000000000000000" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  378. None),
  379. ("Invalid commit scalar (1)",
  380. "1300" + "0000000000000000000000000000000000000000000000000000000000000001" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  381. None),
  382. ("Invalid commit scalar (> r)",
  383. "1300" + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  384. None),
  385. ("Commit element not on curve",
  386. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728d0000000000000000000000000000000000000000000000000000000000000000",
  387. None),
  388. ("Invalid commit element (y coordinate > P)",
  389. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  390. None),
  391. ("Invalid commit element (x coordinate > P)",
  392. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  393. None),
  394. ("Different group in commit",
  395. "1400" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  396. None),
  397. ("Too short confirm",
  398. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  399. "0000800edebc3f260dc1fe7e0b20888af2b8a3316252ec37388a8504e25b73dc42")]
  400. for (note, commit, confirm) in tests:
  401. logger.info(note)
  402. dev[0].scan_for_bss(bssid, freq=2412)
  403. hapd.set("ext_mgmt_frame_handling", "1")
  404. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  405. scan_freq="2412", wait_connect=False)
  406. logger.info("Commit")
  407. for i in range(0, 10):
  408. req = hapd.mgmt_rx()
  409. if req is None:
  410. raise Exception("MGMT RX wait timed out (commit)")
  411. if req['subtype'] == 11:
  412. break
  413. req = None
  414. if not req:
  415. raise Exception("Authentication frame (commit) not received")
  416. hapd.dump_monitor()
  417. resp = {}
  418. resp['fc'] = req['fc']
  419. resp['da'] = req['sa']
  420. resp['sa'] = req['da']
  421. resp['bssid'] = req['bssid']
  422. resp['payload'] = binascii.unhexlify("030001000000" + commit)
  423. hapd.mgmt_tx(resp)
  424. if confirm:
  425. logger.info("Confirm")
  426. for i in range(0, 10):
  427. req = hapd.mgmt_rx()
  428. if req is None:
  429. raise Exception("MGMT RX wait timed out (confirm)")
  430. if req['subtype'] == 11:
  431. break
  432. req = None
  433. if not req:
  434. raise Exception("Authentication frame (confirm) not received")
  435. hapd.dump_monitor()
  436. resp = {}
  437. resp['fc'] = req['fc']
  438. resp['da'] = req['sa']
  439. resp['sa'] = req['da']
  440. resp['bssid'] = req['bssid']
  441. resp['payload'] = binascii.unhexlify("030002000000" + confirm)
  442. hapd.mgmt_tx(resp)
  443. time.sleep(0.1)
  444. dev[0].request("REMOVE_NETWORK all")
  445. hapd.set("ext_mgmt_frame_handling", "0")
  446. hapd.dump_monitor()
  447. def test_sae_proto_ffc(dev, apdev):
  448. """SAE protocol testing (FFC)"""
  449. if "SAE" not in dev[0].get_capability("auth_alg"):
  450. raise HwsimSkip("SAE not supported")
  451. params = hostapd.wpa2_params(ssid="test-sae",
  452. passphrase="12345678")
  453. params['wpa_key_mgmt'] = 'SAE'
  454. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  455. bssid = apdev[0]['bssid']
  456. dev[0].request("SET sae_groups 2")
  457. tests = [ ("Confirm mismatch",
  458. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "a8c00117493cdffa5dd671e934bc9cb1a69f39e25e9dd9cd9afd3aea2441a0f5491211c7ba50a753563f9ce943b043557cb71193b28e86ed9544f4289c471bf91b70af5c018cf4663e004165b0fd0bc1d8f3f78adf42eee92bcbc55246fd3ee9f107ab965dc7d4986f23eb71d616ebfe6bfe0a6c1ac5dc1718acee17c9a17486",
  459. "0000f3116a9731f1259622e3eb55d4b3b50ba16f8c5f5565b28e609b180c51460251"),
  460. ("Too short commit",
  461. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "a8c00117493cdffa5dd671e934bc9cb1a69f39e25e9dd9cd9afd3aea2441a0f5491211c7ba50a753563f9ce943b043557cb71193b28e86ed9544f4289c471bf91b70af5c018cf4663e004165b0fd0bc1d8f3f78adf42eee92bcbc55246fd3ee9f107ab965dc7d4986f23eb71d616ebfe6bfe0a6c1ac5dc1718acee17c9a174",
  462. None),
  463. ("Invalid element (0) in commit",
  464. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  465. None),
  466. ("Invalid element (1) in commit",
  467. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
  468. None),
  469. ("Invalid element (> P) in commit",
  470. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  471. None) ]
  472. for (note, commit, confirm) in tests:
  473. logger.info(note)
  474. dev[0].scan_for_bss(bssid, freq=2412)
  475. hapd.set("ext_mgmt_frame_handling", "1")
  476. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  477. scan_freq="2412", wait_connect=False)
  478. logger.info("Commit")
  479. for i in range(0, 10):
  480. req = hapd.mgmt_rx()
  481. if req is None:
  482. raise Exception("MGMT RX wait timed out (commit)")
  483. if req['subtype'] == 11:
  484. break
  485. req = None
  486. if not req:
  487. raise Exception("Authentication frame (commit) not received")
  488. hapd.dump_monitor()
  489. resp = {}
  490. resp['fc'] = req['fc']
  491. resp['da'] = req['sa']
  492. resp['sa'] = req['da']
  493. resp['bssid'] = req['bssid']
  494. resp['payload'] = binascii.unhexlify("030001000000" + commit)
  495. hapd.mgmt_tx(resp)
  496. if confirm:
  497. logger.info("Confirm")
  498. for i in range(0, 10):
  499. req = hapd.mgmt_rx()
  500. if req is None:
  501. raise Exception("MGMT RX wait timed out (confirm)")
  502. if req['subtype'] == 11:
  503. break
  504. req = None
  505. if not req:
  506. raise Exception("Authentication frame (confirm) not received")
  507. hapd.dump_monitor()
  508. resp = {}
  509. resp['fc'] = req['fc']
  510. resp['da'] = req['sa']
  511. resp['sa'] = req['da']
  512. resp['bssid'] = req['bssid']
  513. resp['payload'] = binascii.unhexlify("030002000000" + confirm)
  514. hapd.mgmt_tx(resp)
  515. time.sleep(0.1)
  516. dev[0].request("REMOVE_NETWORK all")
  517. hapd.set("ext_mgmt_frame_handling", "0")
  518. hapd.dump_monitor()
  519. def test_sae_no_ffc_by_default(dev, apdev):
  520. """SAE and default groups rejecting FFC"""
  521. if "SAE" not in dev[0].get_capability("auth_alg"):
  522. raise HwsimSkip("SAE not supported")
  523. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  524. params['wpa_key_mgmt'] = 'SAE'
  525. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  526. dev[0].request("SET sae_groups 5")
  527. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE", scan_freq="2412",
  528. wait_connect=False)
  529. ev = dev[0].wait_event(["SME: Trying to authenticate"], timeout=3)
  530. if ev is None:
  531. raise Exception("Did not try to authenticate")
  532. ev = dev[0].wait_event(["SME: Trying to authenticate"], timeout=3)
  533. if ev is None:
  534. raise Exception("Did not try to authenticate (2)")
  535. dev[0].request("REMOVE_NETWORK all")
  536. def sae_reflection_attack(apdev, dev, group):
  537. if "SAE" not in dev.get_capability("auth_alg"):
  538. raise HwsimSkip("SAE not supported")
  539. params = hostapd.wpa2_params(ssid="test-sae",
  540. passphrase="no-knowledge-of-passphrase")
  541. params['wpa_key_mgmt'] = 'SAE'
  542. hapd = hostapd.add_ap(apdev['ifname'], params)
  543. bssid = apdev['bssid']
  544. dev.scan_for_bss(bssid, freq=2412)
  545. hapd.set("ext_mgmt_frame_handling", "1")
  546. dev.request("SET sae_groups %d" % group)
  547. dev.connect("test-sae", psk="reflection-attack", key_mgmt="SAE",
  548. scan_freq="2412", wait_connect=False)
  549. # Commit
  550. for i in range(0, 10):
  551. req = hapd.mgmt_rx()
  552. if req is None:
  553. raise Exception("MGMT RX wait timed out")
  554. if req['subtype'] == 11:
  555. break
  556. req = None
  557. if not req:
  558. raise Exception("Authentication frame not received")
  559. resp = {}
  560. resp['fc'] = req['fc']
  561. resp['da'] = req['sa']
  562. resp['sa'] = req['da']
  563. resp['bssid'] = req['bssid']
  564. resp['payload'] = req['payload']
  565. hapd.mgmt_tx(resp)
  566. # Confirm
  567. req = hapd.mgmt_rx(timeout=0.5)
  568. if req is not None:
  569. if req['subtype'] == 11:
  570. raise Exception("Unexpected Authentication frame seen")
  571. def test_sae_reflection_attack_ecc(dev, apdev):
  572. """SAE reflection attack (ECC)"""
  573. sae_reflection_attack(apdev[0], dev[0], 19)
  574. def test_sae_reflection_attack_ffc(dev, apdev):
  575. """SAE reflection attack (FFC)"""
  576. sae_reflection_attack(apdev[0], dev[0], 5)
  577. def test_sae_anti_clogging_proto(dev, apdev):
  578. """SAE anti clogging protocol testing"""
  579. if "SAE" not in dev[0].get_capability("auth_alg"):
  580. raise HwsimSkip("SAE not supported")
  581. params = hostapd.wpa2_params(ssid="test-sae",
  582. passphrase="no-knowledge-of-passphrase")
  583. params['wpa_key_mgmt'] = 'SAE'
  584. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  585. bssid = apdev[0]['bssid']
  586. dev[0].scan_for_bss(bssid, freq=2412)
  587. hapd.set("ext_mgmt_frame_handling", "1")
  588. dev[0].request("SET sae_groups ")
  589. dev[0].connect("test-sae", psk="anti-cloggign", key_mgmt="SAE",
  590. scan_freq="2412", wait_connect=False)
  591. # Commit
  592. for i in range(0, 10):
  593. req = hapd.mgmt_rx()
  594. if req is None:
  595. raise Exception("MGMT RX wait timed out")
  596. if req['subtype'] == 11:
  597. break
  598. req = None
  599. if not req:
  600. raise Exception("Authentication frame not received")
  601. resp = {}
  602. resp['fc'] = req['fc']
  603. resp['da'] = req['sa']
  604. resp['sa'] = req['da']
  605. resp['bssid'] = req['bssid']
  606. resp['payload'] = binascii.unhexlify("030001004c00" + "ffff00")
  607. hapd.mgmt_tx(resp)
  608. # Confirm (not received due to DH group being rejected)
  609. req = hapd.mgmt_rx(timeout=0.5)
  610. if req is not None:
  611. if req['subtype'] == 11:
  612. raise Exception("Unexpected Authentication frame seen")
  613. def test_sae_no_random(dev, apdev):
  614. """SAE and no random numbers available"""
  615. if "SAE" not in dev[0].get_capability("auth_alg"):
  616. raise HwsimSkip("SAE not supported")
  617. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  618. params['wpa_key_mgmt'] = 'SAE'
  619. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  620. dev[0].request("SET sae_groups ")
  621. tests = [ (1, "os_get_random;sae_get_rand"),
  622. (1, "os_get_random;get_rand_1_to_p_1"),
  623. (1, "os_get_random;get_random_qr_qnr"),
  624. (1, "os_get_random;sae_derive_pwe_ecc") ]
  625. for count, func in tests:
  626. with fail_test(dev[0], count, func):
  627. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  628. scan_freq="2412")
  629. dev[0].request("REMOVE_NETWORK all")
  630. dev[0].wait_disconnected()