test_sae.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. get_key_locations(buf, gtk, "GTK")
  300. raise Exception("GTK found from memory")
  301. verify_not_present(buf, sae_k, fname, "SAE(k)")
  302. verify_not_present(buf, sae_keyseed, fname, "SAE(keyseed)")
  303. verify_not_present(buf, sae_kck, fname, "SAE(KCK)")
  304. logger.info("Checking keys in memory after disassociation")
  305. buf = read_process_memory(pid, password)
  306. # Note: Password is still present in network configuration
  307. # Note: PMK is in PMKSA cache
  308. get_key_locations(buf, password, "Password")
  309. get_key_locations(buf, pmk, "PMK")
  310. verify_not_present(buf, kck, fname, "KCK")
  311. verify_not_present(buf, kek, fname, "KEK")
  312. verify_not_present(buf, tk, fname, "TK")
  313. verify_not_present(buf, gtk, fname, "GTK")
  314. verify_not_present(buf, sae_k, fname, "SAE(k)")
  315. verify_not_present(buf, sae_keyseed, fname, "SAE(keyseed)")
  316. verify_not_present(buf, sae_kck, fname, "SAE(KCK)")
  317. dev[0].request("PMKSA_FLUSH")
  318. logger.info("Checking keys in memory after PMKSA cache flush")
  319. buf = read_process_memory(pid, password)
  320. get_key_locations(buf, password, "Password")
  321. get_key_locations(buf, pmk, "PMK")
  322. verify_not_present(buf, pmk, fname, "PMK")
  323. dev[0].request("REMOVE_NETWORK all")
  324. logger.info("Checking keys in memory after network profile removal")
  325. buf = read_process_memory(pid, password)
  326. get_key_locations(buf, password, "Password")
  327. get_key_locations(buf, pmk, "PMK")
  328. verify_not_present(buf, password, fname, "password")
  329. verify_not_present(buf, pmk, fname, "PMK")
  330. verify_not_present(buf, kck, fname, "KCK")
  331. verify_not_present(buf, kek, fname, "KEK")
  332. verify_not_present(buf, tk, fname, "TK")
  333. verify_not_present(buf, gtk, fname, "GTK")
  334. verify_not_present(buf, sae_k, fname, "SAE(k)")
  335. verify_not_present(buf, sae_keyseed, fname, "SAE(keyseed)")
  336. verify_not_present(buf, sae_kck, fname, "SAE(KCK)")
  337. def test_sae_oom_wpas(dev, apdev):
  338. """SAE and OOM in wpa_supplicant"""
  339. if "SAE" not in dev[0].get_capability("auth_alg"):
  340. raise HwsimSkip("SAE not supported")
  341. params = hostapd.wpa2_params(ssid="test-sae",
  342. passphrase="12345678")
  343. params['wpa_key_mgmt'] = 'SAE'
  344. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  345. dev[0].request("SET sae_groups 25")
  346. tls = dev[0].request("GET tls_library")
  347. if "BoringSSL" in tls:
  348. dev[0].request("SET sae_groups 26")
  349. with alloc_fail(dev[0], 1, "sae_set_group"):
  350. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  351. scan_freq="2412")
  352. dev[0].request("REMOVE_NETWORK all")
  353. dev[0].request("SET sae_groups ")
  354. with alloc_fail(dev[0], 2, "sae_set_group"):
  355. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  356. scan_freq="2412")
  357. dev[0].request("REMOVE_NETWORK all")
  358. def test_sae_proto_ecc(dev, apdev):
  359. """SAE protocol testing (ECC)"""
  360. if "SAE" not in dev[0].get_capability("auth_alg"):
  361. raise HwsimSkip("SAE not supported")
  362. params = hostapd.wpa2_params(ssid="test-sae",
  363. passphrase="12345678")
  364. params['wpa_key_mgmt'] = 'SAE'
  365. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  366. bssid = apdev[0]['bssid']
  367. dev[0].request("SET sae_groups 19")
  368. tests = [ ("Confirm mismatch",
  369. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  370. "0000800edebc3f260dc1fe7e0b20888af2b8a3316252ec37388a8504e25b73dc4240"),
  371. ("Commit without even full cyclic group field",
  372. "13",
  373. None),
  374. ("Too short commit",
  375. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02",
  376. None),
  377. ("Invalid commit scalar (0)",
  378. "1300" + "0000000000000000000000000000000000000000000000000000000000000000" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  379. None),
  380. ("Invalid commit scalar (1)",
  381. "1300" + "0000000000000000000000000000000000000000000000000000000000000001" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  382. None),
  383. ("Invalid commit scalar (> r)",
  384. "1300" + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  385. None),
  386. ("Commit element not on curve",
  387. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728d0000000000000000000000000000000000000000000000000000000000000000",
  388. None),
  389. ("Invalid commit element (y coordinate > P)",
  390. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  391. None),
  392. ("Invalid commit element (x coordinate > P)",
  393. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  394. None),
  395. ("Different group in commit",
  396. "1400" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  397. None),
  398. ("Too short confirm",
  399. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  400. "0000800edebc3f260dc1fe7e0b20888af2b8a3316252ec37388a8504e25b73dc42")]
  401. for (note, commit, confirm) in tests:
  402. logger.info(note)
  403. dev[0].scan_for_bss(bssid, freq=2412)
  404. hapd.set("ext_mgmt_frame_handling", "1")
  405. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  406. scan_freq="2412", wait_connect=False)
  407. logger.info("Commit")
  408. for i in range(0, 10):
  409. req = hapd.mgmt_rx()
  410. if req is None:
  411. raise Exception("MGMT RX wait timed out (commit)")
  412. if req['subtype'] == 11:
  413. break
  414. req = None
  415. if not req:
  416. raise Exception("Authentication frame (commit) not received")
  417. hapd.dump_monitor()
  418. resp = {}
  419. resp['fc'] = req['fc']
  420. resp['da'] = req['sa']
  421. resp['sa'] = req['da']
  422. resp['bssid'] = req['bssid']
  423. resp['payload'] = binascii.unhexlify("030001000000" + commit)
  424. hapd.mgmt_tx(resp)
  425. if confirm:
  426. logger.info("Confirm")
  427. for i in range(0, 10):
  428. req = hapd.mgmt_rx()
  429. if req is None:
  430. raise Exception("MGMT RX wait timed out (confirm)")
  431. if req['subtype'] == 11:
  432. break
  433. req = None
  434. if not req:
  435. raise Exception("Authentication frame (confirm) not received")
  436. hapd.dump_monitor()
  437. resp = {}
  438. resp['fc'] = req['fc']
  439. resp['da'] = req['sa']
  440. resp['sa'] = req['da']
  441. resp['bssid'] = req['bssid']
  442. resp['payload'] = binascii.unhexlify("030002000000" + confirm)
  443. hapd.mgmt_tx(resp)
  444. time.sleep(0.1)
  445. dev[0].request("REMOVE_NETWORK all")
  446. hapd.set("ext_mgmt_frame_handling", "0")
  447. hapd.dump_monitor()
  448. def test_sae_proto_ffc(dev, apdev):
  449. """SAE protocol testing (FFC)"""
  450. if "SAE" not in dev[0].get_capability("auth_alg"):
  451. raise HwsimSkip("SAE not supported")
  452. params = hostapd.wpa2_params(ssid="test-sae",
  453. passphrase="12345678")
  454. params['wpa_key_mgmt'] = 'SAE'
  455. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  456. bssid = apdev[0]['bssid']
  457. dev[0].request("SET sae_groups 2")
  458. tests = [ ("Confirm mismatch",
  459. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "a8c00117493cdffa5dd671e934bc9cb1a69f39e25e9dd9cd9afd3aea2441a0f5491211c7ba50a753563f9ce943b043557cb71193b28e86ed9544f4289c471bf91b70af5c018cf4663e004165b0fd0bc1d8f3f78adf42eee92bcbc55246fd3ee9f107ab965dc7d4986f23eb71d616ebfe6bfe0a6c1ac5dc1718acee17c9a17486",
  460. "0000f3116a9731f1259622e3eb55d4b3b50ba16f8c5f5565b28e609b180c51460251"),
  461. ("Too short commit",
  462. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "a8c00117493cdffa5dd671e934bc9cb1a69f39e25e9dd9cd9afd3aea2441a0f5491211c7ba50a753563f9ce943b043557cb71193b28e86ed9544f4289c471bf91b70af5c018cf4663e004165b0fd0bc1d8f3f78adf42eee92bcbc55246fd3ee9f107ab965dc7d4986f23eb71d616ebfe6bfe0a6c1ac5dc1718acee17c9a174",
  463. None),
  464. ("Invalid element (0) in commit",
  465. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  466. None),
  467. ("Invalid element (1) in commit",
  468. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
  469. None),
  470. ("Invalid element (> P) in commit",
  471. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  472. None) ]
  473. for (note, commit, confirm) in tests:
  474. logger.info(note)
  475. dev[0].scan_for_bss(bssid, freq=2412)
  476. hapd.set("ext_mgmt_frame_handling", "1")
  477. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  478. scan_freq="2412", wait_connect=False)
  479. logger.info("Commit")
  480. for i in range(0, 10):
  481. req = hapd.mgmt_rx()
  482. if req is None:
  483. raise Exception("MGMT RX wait timed out (commit)")
  484. if req['subtype'] == 11:
  485. break
  486. req = None
  487. if not req:
  488. raise Exception("Authentication frame (commit) not received")
  489. hapd.dump_monitor()
  490. resp = {}
  491. resp['fc'] = req['fc']
  492. resp['da'] = req['sa']
  493. resp['sa'] = req['da']
  494. resp['bssid'] = req['bssid']
  495. resp['payload'] = binascii.unhexlify("030001000000" + commit)
  496. hapd.mgmt_tx(resp)
  497. if confirm:
  498. logger.info("Confirm")
  499. for i in range(0, 10):
  500. req = hapd.mgmt_rx()
  501. if req is None:
  502. raise Exception("MGMT RX wait timed out (confirm)")
  503. if req['subtype'] == 11:
  504. break
  505. req = None
  506. if not req:
  507. raise Exception("Authentication frame (confirm) not received")
  508. hapd.dump_monitor()
  509. resp = {}
  510. resp['fc'] = req['fc']
  511. resp['da'] = req['sa']
  512. resp['sa'] = req['da']
  513. resp['bssid'] = req['bssid']
  514. resp['payload'] = binascii.unhexlify("030002000000" + confirm)
  515. hapd.mgmt_tx(resp)
  516. time.sleep(0.1)
  517. dev[0].request("REMOVE_NETWORK all")
  518. hapd.set("ext_mgmt_frame_handling", "0")
  519. hapd.dump_monitor()
  520. def test_sae_no_ffc_by_default(dev, apdev):
  521. """SAE and default groups rejecting FFC"""
  522. if "SAE" not in dev[0].get_capability("auth_alg"):
  523. raise HwsimSkip("SAE not supported")
  524. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  525. params['wpa_key_mgmt'] = 'SAE'
  526. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  527. dev[0].request("SET sae_groups 5")
  528. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE", scan_freq="2412",
  529. wait_connect=False)
  530. ev = dev[0].wait_event(["SME: Trying to authenticate"], timeout=3)
  531. if ev is None:
  532. raise Exception("Did not try to authenticate")
  533. ev = dev[0].wait_event(["SME: Trying to authenticate"], timeout=3)
  534. if ev is None:
  535. raise Exception("Did not try to authenticate (2)")
  536. dev[0].request("REMOVE_NETWORK all")
  537. def sae_reflection_attack(apdev, dev, group):
  538. if "SAE" not in dev.get_capability("auth_alg"):
  539. raise HwsimSkip("SAE not supported")
  540. params = hostapd.wpa2_params(ssid="test-sae",
  541. passphrase="no-knowledge-of-passphrase")
  542. params['wpa_key_mgmt'] = 'SAE'
  543. hapd = hostapd.add_ap(apdev['ifname'], params)
  544. bssid = apdev['bssid']
  545. dev.scan_for_bss(bssid, freq=2412)
  546. hapd.set("ext_mgmt_frame_handling", "1")
  547. dev.request("SET sae_groups %d" % group)
  548. dev.connect("test-sae", psk="reflection-attack", key_mgmt="SAE",
  549. scan_freq="2412", wait_connect=False)
  550. # Commit
  551. for i in range(0, 10):
  552. req = hapd.mgmt_rx()
  553. if req is None:
  554. raise Exception("MGMT RX wait timed out")
  555. if req['subtype'] == 11:
  556. break
  557. req = None
  558. if not req:
  559. raise Exception("Authentication frame not received")
  560. resp = {}
  561. resp['fc'] = req['fc']
  562. resp['da'] = req['sa']
  563. resp['sa'] = req['da']
  564. resp['bssid'] = req['bssid']
  565. resp['payload'] = req['payload']
  566. hapd.mgmt_tx(resp)
  567. # Confirm
  568. req = hapd.mgmt_rx(timeout=0.5)
  569. if req is not None:
  570. if req['subtype'] == 11:
  571. raise Exception("Unexpected Authentication frame seen")
  572. def test_sae_reflection_attack_ecc(dev, apdev):
  573. """SAE reflection attack (ECC)"""
  574. sae_reflection_attack(apdev[0], dev[0], 19)
  575. def test_sae_reflection_attack_ffc(dev, apdev):
  576. """SAE reflection attack (FFC)"""
  577. sae_reflection_attack(apdev[0], dev[0], 5)
  578. def test_sae_anti_clogging_proto(dev, apdev):
  579. """SAE anti clogging protocol testing"""
  580. if "SAE" not in dev[0].get_capability("auth_alg"):
  581. raise HwsimSkip("SAE not supported")
  582. params = hostapd.wpa2_params(ssid="test-sae",
  583. passphrase="no-knowledge-of-passphrase")
  584. params['wpa_key_mgmt'] = 'SAE'
  585. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  586. bssid = apdev[0]['bssid']
  587. dev[0].scan_for_bss(bssid, freq=2412)
  588. hapd.set("ext_mgmt_frame_handling", "1")
  589. dev[0].request("SET sae_groups ")
  590. dev[0].connect("test-sae", psk="anti-cloggign", key_mgmt="SAE",
  591. scan_freq="2412", wait_connect=False)
  592. # Commit
  593. for i in range(0, 10):
  594. req = hapd.mgmt_rx()
  595. if req is None:
  596. raise Exception("MGMT RX wait timed out")
  597. if req['subtype'] == 11:
  598. break
  599. req = None
  600. if not req:
  601. raise Exception("Authentication frame not received")
  602. resp = {}
  603. resp['fc'] = req['fc']
  604. resp['da'] = req['sa']
  605. resp['sa'] = req['da']
  606. resp['bssid'] = req['bssid']
  607. resp['payload'] = binascii.unhexlify("030001004c00" + "ffff00")
  608. hapd.mgmt_tx(resp)
  609. # Confirm (not received due to DH group being rejected)
  610. req = hapd.mgmt_rx(timeout=0.5)
  611. if req is not None:
  612. if req['subtype'] == 11:
  613. raise Exception("Unexpected Authentication frame seen")
  614. def test_sae_no_random(dev, apdev):
  615. """SAE and no random numbers available"""
  616. if "SAE" not in dev[0].get_capability("auth_alg"):
  617. raise HwsimSkip("SAE not supported")
  618. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  619. params['wpa_key_mgmt'] = 'SAE'
  620. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  621. dev[0].request("SET sae_groups ")
  622. tests = [ (1, "os_get_random;sae_get_rand"),
  623. (1, "os_get_random;get_rand_1_to_p_1"),
  624. (1, "os_get_random;get_random_qr_qnr"),
  625. (1, "os_get_random;sae_derive_pwe_ecc") ]
  626. for count, func in tests:
  627. with fail_test(dev[0], count, func):
  628. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  629. scan_freq="2412")
  630. dev[0].request("REMOVE_NETWORK all")
  631. dev[0].wait_disconnected()