test_sae.py 32 KB

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