test_sae.py 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. # Test cases for SAE
  2. # Copyright (c) 2013-2016, 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 logging
  10. logger = logging.getLogger()
  11. import hwsim_utils
  12. import hostapd
  13. from utils import HwsimSkip, alloc_fail, fail_test, wait_fail_trigger
  14. from test_ap_psk import find_wpas_process, read_process_memory, verify_not_present, get_key_locations
  15. def test_sae(dev, apdev):
  16. """SAE with default group"""
  17. if "SAE" not in dev[0].get_capability("auth_alg"):
  18. raise HwsimSkip("SAE not supported")
  19. params = hostapd.wpa2_params(ssid="test-sae",
  20. passphrase="12345678")
  21. params['wpa_key_mgmt'] = 'SAE'
  22. hapd = hostapd.add_ap(apdev[0], params)
  23. key_mgmt = hapd.get_config()['key_mgmt']
  24. if key_mgmt.split(' ')[0] != "SAE":
  25. raise Exception("Unexpected GET_CONFIG(key_mgmt): " + key_mgmt)
  26. dev[0].request("SET sae_groups ")
  27. id = dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  28. scan_freq="2412")
  29. if dev[0].get_status_field('sae_group') != '19':
  30. raise Exception("Expected default SAE group not used")
  31. bss = dev[0].get_bss(apdev[0]['bssid'])
  32. if 'flags' not in bss:
  33. raise Exception("Could not get BSS flags from BSS table")
  34. if "[WPA2-SAE-CCMP]" not in bss['flags']:
  35. raise Exception("Unexpected BSS flags: " + bss['flags'])
  36. def test_sae_password_ecc(dev, apdev):
  37. """SAE with number of different passwords (ECC)"""
  38. if "SAE" not in dev[0].get_capability("auth_alg"):
  39. raise HwsimSkip("SAE not supported")
  40. params = hostapd.wpa2_params(ssid="test-sae",
  41. passphrase="12345678")
  42. params['wpa_key_mgmt'] = 'SAE'
  43. hapd = hostapd.add_ap(apdev[0], params)
  44. dev[0].request("SET sae_groups 19")
  45. for i in range(10):
  46. password = "12345678-" + str(i)
  47. hapd.set("wpa_passphrase", password)
  48. dev[0].connect("test-sae", psk=password, key_mgmt="SAE",
  49. scan_freq="2412")
  50. dev[0].request("REMOVE_NETWORK all")
  51. dev[0].wait_disconnected()
  52. def test_sae_password_ffc(dev, apdev):
  53. """SAE with number of different passwords (FFC)"""
  54. if "SAE" not in dev[0].get_capability("auth_alg"):
  55. raise HwsimSkip("SAE not supported")
  56. params = hostapd.wpa2_params(ssid="test-sae",
  57. passphrase="12345678")
  58. params['wpa_key_mgmt'] = 'SAE'
  59. params['sae_groups'] = '22'
  60. hapd = hostapd.add_ap(apdev[0], params)
  61. dev[0].request("SET sae_groups 22")
  62. for i in range(10):
  63. password = "12345678-" + str(i)
  64. hapd.set("wpa_passphrase", password)
  65. dev[0].connect("test-sae", psk=password, key_mgmt="SAE",
  66. scan_freq="2412")
  67. dev[0].request("REMOVE_NETWORK all")
  68. dev[0].wait_disconnected()
  69. def test_sae_pmksa_caching(dev, apdev):
  70. """SAE and PMKSA caching"""
  71. if "SAE" not in dev[0].get_capability("auth_alg"):
  72. raise HwsimSkip("SAE not supported")
  73. params = hostapd.wpa2_params(ssid="test-sae",
  74. passphrase="12345678")
  75. params['wpa_key_mgmt'] = 'SAE'
  76. hapd = hostapd.add_ap(apdev[0], params)
  77. dev[0].request("SET sae_groups ")
  78. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  79. scan_freq="2412")
  80. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  81. if ev is None:
  82. raise Exception("No connection event received from hostapd")
  83. dev[0].request("DISCONNECT")
  84. dev[0].wait_disconnected()
  85. dev[0].request("RECONNECT")
  86. dev[0].wait_connected(timeout=15, error="Reconnect timed out")
  87. if dev[0].get_status_field('sae_group') is not None:
  88. raise Exception("SAE group claimed to have been used")
  89. def test_sae_pmksa_caching_disabled(dev, apdev):
  90. """SAE and PMKSA caching disabled"""
  91. if "SAE" not in dev[0].get_capability("auth_alg"):
  92. raise HwsimSkip("SAE not supported")
  93. params = hostapd.wpa2_params(ssid="test-sae",
  94. passphrase="12345678")
  95. params['wpa_key_mgmt'] = 'SAE'
  96. params['disable_pmksa_caching'] = '1'
  97. hapd = hostapd.add_ap(apdev[0], params)
  98. dev[0].request("SET sae_groups ")
  99. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  100. scan_freq="2412")
  101. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  102. if ev is None:
  103. raise Exception("No connection event received from hostapd")
  104. dev[0].request("DISCONNECT")
  105. dev[0].wait_disconnected()
  106. dev[0].request("RECONNECT")
  107. dev[0].wait_connected(timeout=15, error="Reconnect timed out")
  108. if dev[0].get_status_field('sae_group') != '19':
  109. raise Exception("Expected default SAE group not used")
  110. def test_sae_groups(dev, apdev):
  111. """SAE with all supported groups"""
  112. if "SAE" not in dev[0].get_capability("auth_alg"):
  113. raise HwsimSkip("SAE not supported")
  114. # This is the full list of supported groups, but groups 14-16 (2048-4096 bit
  115. # MODP) and group 21 (521-bit random ECP group) are a bit too slow on some
  116. # VMs and can result in hitting the mac80211 authentication timeout, so
  117. # allow them to fail and just report such failures in the debug log.
  118. sae_groups = [ 19, 25, 26, 20, 21, 2, 5, 14, 15, 16, 22, 23, 24 ]
  119. tls = dev[0].request("GET tls_library")
  120. if tls.startswith("OpenSSL") and "build=OpenSSL 1.0.2" in tls and "run=OpenSSL 1.0.2" in tls:
  121. logger.info("Add Brainpool EC groups since OpenSSL is new enough")
  122. sae_groups += [ 27, 28, 29, 30 ]
  123. heavy_groups = [ 14, 15, 16 ]
  124. groups = [str(g) for g in sae_groups]
  125. params = hostapd.wpa2_params(ssid="test-sae-groups",
  126. passphrase="12345678")
  127. params['wpa_key_mgmt'] = 'SAE'
  128. params['sae_groups'] = ' '.join(groups)
  129. hostapd.add_ap(apdev[0], params)
  130. for g in groups:
  131. logger.info("Testing SAE group " + g)
  132. dev[0].request("SET sae_groups " + g)
  133. id = dev[0].connect("test-sae-groups", psk="12345678", key_mgmt="SAE",
  134. scan_freq="2412", wait_connect=False)
  135. if int(g) in heavy_groups:
  136. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=5)
  137. if ev is None:
  138. logger.info("No connection with heavy SAE group %s did not connect - likely hitting timeout in mac80211" % g)
  139. dev[0].remove_network(id)
  140. time.sleep(0.1)
  141. dev[0].dump_monitor()
  142. continue
  143. logger.info("Connection with heavy SAE group " + g)
  144. else:
  145. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
  146. if ev is None:
  147. if "BoringSSL" in tls and int(g) in [ 25 ]:
  148. logger.info("Ignore connection failure with group " + g + " with BoringSSL")
  149. dev[0].remove_network(id)
  150. dev[0].dump_monitor()
  151. continue
  152. raise Exception("Connection timed out with group " + g)
  153. if dev[0].get_status_field('sae_group') != g:
  154. raise Exception("Expected SAE group not used")
  155. dev[0].remove_network(id)
  156. dev[0].wait_disconnected()
  157. dev[0].dump_monitor()
  158. def test_sae_group_nego(dev, apdev):
  159. """SAE group negotiation"""
  160. if "SAE" not in dev[0].get_capability("auth_alg"):
  161. raise HwsimSkip("SAE not supported")
  162. params = hostapd.wpa2_params(ssid="test-sae-group-nego",
  163. passphrase="12345678")
  164. params['wpa_key_mgmt'] = 'SAE'
  165. params['sae_groups'] = '19'
  166. hostapd.add_ap(apdev[0], params)
  167. dev[0].request("SET sae_groups 25 26 20 19")
  168. dev[0].connect("test-sae-group-nego", psk="12345678", key_mgmt="SAE",
  169. scan_freq="2412")
  170. if dev[0].get_status_field('sae_group') != '19':
  171. raise Exception("Expected SAE group not used")
  172. def test_sae_anti_clogging(dev, apdev):
  173. """SAE anti clogging"""
  174. if "SAE" not in dev[0].get_capability("auth_alg"):
  175. raise HwsimSkip("SAE not supported")
  176. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  177. params['wpa_key_mgmt'] = 'SAE'
  178. params['sae_anti_clogging_threshold'] = '1'
  179. hostapd.add_ap(apdev[0], params)
  180. dev[0].request("SET sae_groups ")
  181. dev[1].request("SET sae_groups ")
  182. id = {}
  183. for i in range(0, 2):
  184. dev[i].scan(freq="2412")
  185. id[i] = dev[i].connect("test-sae", psk="12345678", key_mgmt="SAE",
  186. scan_freq="2412", only_add_network=True)
  187. for i in range(0, 2):
  188. dev[i].select_network(id[i])
  189. for i in range(0, 2):
  190. dev[i].wait_connected(timeout=10)
  191. def test_sae_forced_anti_clogging(dev, apdev):
  192. """SAE anti clogging (forced)"""
  193. if "SAE" not in dev[0].get_capability("auth_alg"):
  194. raise HwsimSkip("SAE not supported")
  195. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  196. params['wpa_key_mgmt'] = 'SAE WPA-PSK'
  197. params['sae_anti_clogging_threshold'] = '0'
  198. hostapd.add_ap(apdev[0], params)
  199. dev[2].connect("test-sae", psk="12345678", scan_freq="2412")
  200. for i in range(0, 2):
  201. dev[i].request("SET sae_groups ")
  202. dev[i].connect("test-sae", psk="12345678", key_mgmt="SAE",
  203. scan_freq="2412")
  204. def test_sae_mixed(dev, apdev):
  205. """Mixed SAE and non-SAE network"""
  206. if "SAE" not in dev[0].get_capability("auth_alg"):
  207. raise HwsimSkip("SAE not supported")
  208. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  209. params['wpa_key_mgmt'] = 'SAE WPA-PSK'
  210. params['sae_anti_clogging_threshold'] = '0'
  211. hostapd.add_ap(apdev[0], params)
  212. dev[2].connect("test-sae", psk="12345678", scan_freq="2412")
  213. for i in range(0, 2):
  214. dev[i].request("SET sae_groups ")
  215. dev[i].connect("test-sae", psk="12345678", key_mgmt="SAE",
  216. scan_freq="2412")
  217. def test_sae_missing_password(dev, apdev):
  218. """SAE and missing password"""
  219. if "SAE" not in dev[0].get_capability("auth_alg"):
  220. raise HwsimSkip("SAE not supported")
  221. params = hostapd.wpa2_params(ssid="test-sae",
  222. passphrase="12345678")
  223. params['wpa_key_mgmt'] = 'SAE'
  224. hapd = hostapd.add_ap(apdev[0], params)
  225. dev[0].request("SET sae_groups ")
  226. id = dev[0].connect("test-sae",
  227. raw_psk="46b4a73b8a951ad53ebd2e0afdb9c5483257edd4c21d12b7710759da70945858",
  228. key_mgmt="SAE", scan_freq="2412", wait_connect=False)
  229. ev = dev[0].wait_event(['CTRL-EVENT-SSID-TEMP-DISABLED'], timeout=10)
  230. if ev is None:
  231. raise Exception("Invalid network not temporarily disabled")
  232. def test_sae_key_lifetime_in_memory(dev, apdev, params):
  233. """SAE and key lifetime in memory"""
  234. if "SAE" not in dev[0].get_capability("auth_alg"):
  235. raise HwsimSkip("SAE not supported")
  236. password = "5ad144a7c1f5a5503baa6fa01dabc15b1843e8c01662d78d16b70b5cd23cf8b"
  237. p = hostapd.wpa2_params(ssid="test-sae", passphrase=password)
  238. p['wpa_key_mgmt'] = 'SAE'
  239. hapd = hostapd.add_ap(apdev[0], p)
  240. pid = find_wpas_process(dev[0])
  241. dev[0].request("SET sae_groups ")
  242. id = dev[0].connect("test-sae", psk=password, key_mgmt="SAE",
  243. scan_freq="2412")
  244. # The decrypted copy of GTK is freed only after the CTRL-EVENT-CONNECTED
  245. # event has been delivered, so verify that wpa_supplicant has returned to
  246. # eloop before reading process memory.
  247. time.sleep(1)
  248. dev[0].ping()
  249. buf = read_process_memory(pid, password)
  250. dev[0].request("DISCONNECT")
  251. dev[0].wait_disconnected()
  252. dev[0].relog()
  253. sae_k = None
  254. sae_keyseed = None
  255. sae_kck = None
  256. pmk = None
  257. ptk = None
  258. gtk = None
  259. with open(os.path.join(params['logdir'], 'log0'), 'r') as f:
  260. for l in f.readlines():
  261. if "SAE: k - hexdump" in l:
  262. val = l.strip().split(':')[3].replace(' ', '')
  263. sae_k = binascii.unhexlify(val)
  264. if "SAE: keyseed - hexdump" in l:
  265. val = l.strip().split(':')[3].replace(' ', '')
  266. sae_keyseed = binascii.unhexlify(val)
  267. if "SAE: KCK - hexdump" in l:
  268. val = l.strip().split(':')[3].replace(' ', '')
  269. sae_kck = binascii.unhexlify(val)
  270. if "SAE: PMK - hexdump" in l:
  271. val = l.strip().split(':')[3].replace(' ', '')
  272. pmk = binascii.unhexlify(val)
  273. if "WPA: PTK - hexdump" in l:
  274. val = l.strip().split(':')[3].replace(' ', '')
  275. ptk = binascii.unhexlify(val)
  276. if "WPA: Group Key - hexdump" in l:
  277. val = l.strip().split(':')[3].replace(' ', '')
  278. gtk = binascii.unhexlify(val)
  279. if not sae_k or not sae_keyseed or not sae_kck or not pmk or not ptk or not gtk:
  280. raise Exception("Could not find keys from debug log")
  281. if len(gtk) != 16:
  282. raise Exception("Unexpected GTK length")
  283. kck = ptk[0:16]
  284. kek = ptk[16:32]
  285. tk = ptk[32:48]
  286. fname = os.path.join(params['logdir'],
  287. 'sae_key_lifetime_in_memory.memctx-')
  288. logger.info("Checking keys in memory while associated")
  289. get_key_locations(buf, password, "Password")
  290. get_key_locations(buf, pmk, "PMK")
  291. if password not in buf:
  292. raise HwsimSkip("Password not found while associated")
  293. if pmk not in buf:
  294. raise HwsimSkip("PMK not found while associated")
  295. if kck not in buf:
  296. raise Exception("KCK not found while associated")
  297. if kek not in buf:
  298. raise Exception("KEK not found while associated")
  299. if tk in buf:
  300. raise Exception("TK found from memory")
  301. if gtk in buf:
  302. get_key_locations(buf, gtk, "GTK")
  303. raise Exception("GTK found from memory")
  304. verify_not_present(buf, sae_k, fname, "SAE(k)")
  305. verify_not_present(buf, sae_keyseed, fname, "SAE(keyseed)")
  306. verify_not_present(buf, sae_kck, fname, "SAE(KCK)")
  307. logger.info("Checking keys in memory after disassociation")
  308. buf = read_process_memory(pid, password)
  309. # Note: Password is still present in network configuration
  310. # Note: PMK is in PMKSA cache
  311. get_key_locations(buf, password, "Password")
  312. get_key_locations(buf, pmk, "PMK")
  313. verify_not_present(buf, kck, fname, "KCK")
  314. verify_not_present(buf, kek, fname, "KEK")
  315. verify_not_present(buf, tk, fname, "TK")
  316. verify_not_present(buf, gtk, fname, "GTK")
  317. verify_not_present(buf, sae_k, fname, "SAE(k)")
  318. verify_not_present(buf, sae_keyseed, fname, "SAE(keyseed)")
  319. verify_not_present(buf, sae_kck, fname, "SAE(KCK)")
  320. dev[0].request("PMKSA_FLUSH")
  321. logger.info("Checking keys in memory after PMKSA cache flush")
  322. buf = read_process_memory(pid, password)
  323. get_key_locations(buf, password, "Password")
  324. get_key_locations(buf, pmk, "PMK")
  325. verify_not_present(buf, pmk, fname, "PMK")
  326. dev[0].request("REMOVE_NETWORK all")
  327. logger.info("Checking keys in memory after network profile removal")
  328. buf = read_process_memory(pid, password)
  329. get_key_locations(buf, password, "Password")
  330. get_key_locations(buf, pmk, "PMK")
  331. verify_not_present(buf, password, fname, "password")
  332. verify_not_present(buf, pmk, fname, "PMK")
  333. verify_not_present(buf, kck, fname, "KCK")
  334. verify_not_present(buf, kek, fname, "KEK")
  335. verify_not_present(buf, tk, fname, "TK")
  336. verify_not_present(buf, gtk, fname, "GTK")
  337. verify_not_present(buf, sae_k, fname, "SAE(k)")
  338. verify_not_present(buf, sae_keyseed, fname, "SAE(keyseed)")
  339. verify_not_present(buf, sae_kck, fname, "SAE(KCK)")
  340. def test_sae_oom_wpas(dev, apdev):
  341. """SAE and OOM in wpa_supplicant"""
  342. if "SAE" not in dev[0].get_capability("auth_alg"):
  343. raise HwsimSkip("SAE not supported")
  344. params = hostapd.wpa2_params(ssid="test-sae",
  345. passphrase="12345678")
  346. params['wpa_key_mgmt'] = 'SAE'
  347. hapd = hostapd.add_ap(apdev[0], params)
  348. dev[0].request("SET sae_groups 25")
  349. tls = dev[0].request("GET tls_library")
  350. if "BoringSSL" in tls:
  351. dev[0].request("SET sae_groups 26")
  352. with alloc_fail(dev[0], 1, "sae_set_group"):
  353. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  354. scan_freq="2412")
  355. dev[0].request("REMOVE_NETWORK all")
  356. dev[0].request("SET sae_groups ")
  357. with alloc_fail(dev[0], 2, "sae_set_group"):
  358. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  359. scan_freq="2412")
  360. dev[0].request("REMOVE_NETWORK all")
  361. def test_sae_proto_ecc(dev, apdev):
  362. """SAE protocol testing (ECC)"""
  363. if "SAE" not in dev[0].get_capability("auth_alg"):
  364. raise HwsimSkip("SAE not supported")
  365. params = hostapd.wpa2_params(ssid="test-sae",
  366. passphrase="12345678")
  367. params['wpa_key_mgmt'] = 'SAE'
  368. hapd = hostapd.add_ap(apdev[0], params)
  369. bssid = apdev[0]['bssid']
  370. dev[0].request("SET sae_groups 19")
  371. tests = [ ("Confirm mismatch",
  372. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  373. "0000800edebc3f260dc1fe7e0b20888af2b8a3316252ec37388a8504e25b73dc4240"),
  374. ("Commit without even full cyclic group field",
  375. "13",
  376. None),
  377. ("Too short commit",
  378. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02",
  379. None),
  380. ("Invalid commit scalar (0)",
  381. "1300" + "0000000000000000000000000000000000000000000000000000000000000000" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  382. None),
  383. ("Invalid commit scalar (1)",
  384. "1300" + "0000000000000000000000000000000000000000000000000000000000000001" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  385. None),
  386. ("Invalid commit scalar (> r)",
  387. "1300" + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  388. None),
  389. ("Commit element not on curve",
  390. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728d0000000000000000000000000000000000000000000000000000000000000000",
  391. None),
  392. ("Invalid commit element (y coordinate > P)",
  393. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  394. None),
  395. ("Invalid commit element (x coordinate > P)",
  396. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  397. None),
  398. ("Different group in commit",
  399. "1400" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  400. None),
  401. ("Too short confirm",
  402. "1300" + "033d3635b39666ed427fd4a3e7d37acec2810afeaf1687f746a14163ff0e6d03" + "559cb8928db4ce4e3cbd6555e837591995e5ebe503ef36b503d9ca519d63728dd3c7c676b8e8081831b6bc3a64bdf136061a7de175e17d1965bfa41983ed02f8",
  403. "0000800edebc3f260dc1fe7e0b20888af2b8a3316252ec37388a8504e25b73dc42")]
  404. for (note, commit, confirm) in tests:
  405. logger.info(note)
  406. dev[0].scan_for_bss(bssid, freq=2412)
  407. hapd.set("ext_mgmt_frame_handling", "1")
  408. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  409. scan_freq="2412", wait_connect=False)
  410. logger.info("Commit")
  411. for i in range(0, 10):
  412. req = hapd.mgmt_rx()
  413. if req is None:
  414. raise Exception("MGMT RX wait timed out (commit)")
  415. if req['subtype'] == 11:
  416. break
  417. req = None
  418. if not req:
  419. raise Exception("Authentication frame (commit) not received")
  420. hapd.dump_monitor()
  421. resp = {}
  422. resp['fc'] = req['fc']
  423. resp['da'] = req['sa']
  424. resp['sa'] = req['da']
  425. resp['bssid'] = req['bssid']
  426. resp['payload'] = binascii.unhexlify("030001000000" + commit)
  427. hapd.mgmt_tx(resp)
  428. if confirm:
  429. logger.info("Confirm")
  430. for i in range(0, 10):
  431. req = hapd.mgmt_rx()
  432. if req is None:
  433. raise Exception("MGMT RX wait timed out (confirm)")
  434. if req['subtype'] == 11:
  435. break
  436. req = None
  437. if not req:
  438. raise Exception("Authentication frame (confirm) not received")
  439. hapd.dump_monitor()
  440. resp = {}
  441. resp['fc'] = req['fc']
  442. resp['da'] = req['sa']
  443. resp['sa'] = req['da']
  444. resp['bssid'] = req['bssid']
  445. resp['payload'] = binascii.unhexlify("030002000000" + confirm)
  446. hapd.mgmt_tx(resp)
  447. time.sleep(0.1)
  448. dev[0].request("REMOVE_NETWORK all")
  449. hapd.set("ext_mgmt_frame_handling", "0")
  450. hapd.dump_monitor()
  451. def test_sae_proto_ffc(dev, apdev):
  452. """SAE protocol testing (FFC)"""
  453. if "SAE" not in dev[0].get_capability("auth_alg"):
  454. raise HwsimSkip("SAE not supported")
  455. params = hostapd.wpa2_params(ssid="test-sae",
  456. passphrase="12345678")
  457. params['wpa_key_mgmt'] = 'SAE'
  458. hapd = hostapd.add_ap(apdev[0], params)
  459. bssid = apdev[0]['bssid']
  460. dev[0].request("SET sae_groups 2")
  461. tests = [ ("Confirm mismatch",
  462. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "a8c00117493cdffa5dd671e934bc9cb1a69f39e25e9dd9cd9afd3aea2441a0f5491211c7ba50a753563f9ce943b043557cb71193b28e86ed9544f4289c471bf91b70af5c018cf4663e004165b0fd0bc1d8f3f78adf42eee92bcbc55246fd3ee9f107ab965dc7d4986f23eb71d616ebfe6bfe0a6c1ac5dc1718acee17c9a17486",
  463. "0000f3116a9731f1259622e3eb55d4b3b50ba16f8c5f5565b28e609b180c51460251"),
  464. ("Too short commit",
  465. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "a8c00117493cdffa5dd671e934bc9cb1a69f39e25e9dd9cd9afd3aea2441a0f5491211c7ba50a753563f9ce943b043557cb71193b28e86ed9544f4289c471bf91b70af5c018cf4663e004165b0fd0bc1d8f3f78adf42eee92bcbc55246fd3ee9f107ab965dc7d4986f23eb71d616ebfe6bfe0a6c1ac5dc1718acee17c9a174",
  466. None),
  467. ("Invalid element (0) in commit",
  468. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  469. None),
  470. ("Invalid element (1) in commit",
  471. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
  472. None),
  473. ("Invalid element (> P) in commit",
  474. "0200" + "0c70519d874e3e4930a917cc5e17ea7a26028211159f217bab28b8d6c56691805e49f03249b2c6e22c7c9f86b30e04ccad2deedd5e5108ae07b737c00001c59cd0eb08b1dfc7f1b06a1542e2b6601a963c066e0c65940983a03917ae57a101ce84b5cbbc76ff33ebb990aac2e54aa0f0ab6ec0a58113d927683502b2cb2347d2" + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  475. None) ]
  476. for (note, commit, confirm) in tests:
  477. logger.info(note)
  478. dev[0].scan_for_bss(bssid, freq=2412)
  479. hapd.set("ext_mgmt_frame_handling", "1")
  480. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  481. scan_freq="2412", wait_connect=False)
  482. logger.info("Commit")
  483. for i in range(0, 10):
  484. req = hapd.mgmt_rx()
  485. if req is None:
  486. raise Exception("MGMT RX wait timed out (commit)")
  487. if req['subtype'] == 11:
  488. break
  489. req = None
  490. if not req:
  491. raise Exception("Authentication frame (commit) not received")
  492. hapd.dump_monitor()
  493. resp = {}
  494. resp['fc'] = req['fc']
  495. resp['da'] = req['sa']
  496. resp['sa'] = req['da']
  497. resp['bssid'] = req['bssid']
  498. resp['payload'] = binascii.unhexlify("030001000000" + commit)
  499. hapd.mgmt_tx(resp)
  500. if confirm:
  501. logger.info("Confirm")
  502. for i in range(0, 10):
  503. req = hapd.mgmt_rx()
  504. if req is None:
  505. raise Exception("MGMT RX wait timed out (confirm)")
  506. if req['subtype'] == 11:
  507. break
  508. req = None
  509. if not req:
  510. raise Exception("Authentication frame (confirm) not received")
  511. hapd.dump_monitor()
  512. resp = {}
  513. resp['fc'] = req['fc']
  514. resp['da'] = req['sa']
  515. resp['sa'] = req['da']
  516. resp['bssid'] = req['bssid']
  517. resp['payload'] = binascii.unhexlify("030002000000" + confirm)
  518. hapd.mgmt_tx(resp)
  519. time.sleep(0.1)
  520. dev[0].request("REMOVE_NETWORK all")
  521. hapd.set("ext_mgmt_frame_handling", "0")
  522. hapd.dump_monitor()
  523. def test_sae_no_ffc_by_default(dev, apdev):
  524. """SAE and default groups rejecting FFC"""
  525. if "SAE" not in dev[0].get_capability("auth_alg"):
  526. raise HwsimSkip("SAE not supported")
  527. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  528. params['wpa_key_mgmt'] = 'SAE'
  529. hapd = hostapd.add_ap(apdev[0], params)
  530. dev[0].request("SET sae_groups 5")
  531. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE", scan_freq="2412",
  532. wait_connect=False)
  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")
  536. ev = dev[0].wait_event(["SME: Trying to authenticate"], timeout=3)
  537. if ev is None:
  538. raise Exception("Did not try to authenticate (2)")
  539. dev[0].request("REMOVE_NETWORK all")
  540. def sae_reflection_attack(apdev, dev, group):
  541. if "SAE" not in dev.get_capability("auth_alg"):
  542. raise HwsimSkip("SAE not supported")
  543. params = hostapd.wpa2_params(ssid="test-sae",
  544. passphrase="no-knowledge-of-passphrase")
  545. params['wpa_key_mgmt'] = 'SAE'
  546. hapd = hostapd.add_ap(apdev, params)
  547. bssid = apdev['bssid']
  548. dev.scan_for_bss(bssid, freq=2412)
  549. hapd.set("ext_mgmt_frame_handling", "1")
  550. dev.request("SET sae_groups %d" % group)
  551. dev.connect("test-sae", psk="reflection-attack", key_mgmt="SAE",
  552. scan_freq="2412", wait_connect=False)
  553. # Commit
  554. for i in range(0, 10):
  555. req = hapd.mgmt_rx()
  556. if req is None:
  557. raise Exception("MGMT RX wait timed out")
  558. if req['subtype'] == 11:
  559. break
  560. req = None
  561. if not req:
  562. raise Exception("Authentication frame not received")
  563. resp = {}
  564. resp['fc'] = req['fc']
  565. resp['da'] = req['sa']
  566. resp['sa'] = req['da']
  567. resp['bssid'] = req['bssid']
  568. resp['payload'] = req['payload']
  569. hapd.mgmt_tx(resp)
  570. # Confirm
  571. req = hapd.mgmt_rx(timeout=0.5)
  572. if req is not None:
  573. if req['subtype'] == 11:
  574. raise Exception("Unexpected Authentication frame seen")
  575. def test_sae_reflection_attack_ecc(dev, apdev):
  576. """SAE reflection attack (ECC)"""
  577. sae_reflection_attack(apdev[0], dev[0], 19)
  578. def test_sae_reflection_attack_ffc(dev, apdev):
  579. """SAE reflection attack (FFC)"""
  580. sae_reflection_attack(apdev[0], dev[0], 5)
  581. def test_sae_anti_clogging_proto(dev, apdev):
  582. """SAE anti clogging protocol testing"""
  583. if "SAE" not in dev[0].get_capability("auth_alg"):
  584. raise HwsimSkip("SAE not supported")
  585. params = hostapd.wpa2_params(ssid="test-sae",
  586. passphrase="no-knowledge-of-passphrase")
  587. params['wpa_key_mgmt'] = 'SAE'
  588. hapd = hostapd.add_ap(apdev[0], params)
  589. bssid = apdev[0]['bssid']
  590. dev[0].scan_for_bss(bssid, freq=2412)
  591. hapd.set("ext_mgmt_frame_handling", "1")
  592. dev[0].request("SET sae_groups ")
  593. dev[0].connect("test-sae", psk="anti-cloggign", key_mgmt="SAE",
  594. scan_freq="2412", wait_connect=False)
  595. # Commit
  596. for i in range(0, 10):
  597. req = hapd.mgmt_rx()
  598. if req is None:
  599. raise Exception("MGMT RX wait timed out")
  600. if req['subtype'] == 11:
  601. break
  602. req = None
  603. if not req:
  604. raise Exception("Authentication frame not received")
  605. resp = {}
  606. resp['fc'] = req['fc']
  607. resp['da'] = req['sa']
  608. resp['sa'] = req['da']
  609. resp['bssid'] = req['bssid']
  610. resp['payload'] = binascii.unhexlify("030001004c00" + "ffff00")
  611. hapd.mgmt_tx(resp)
  612. # Confirm (not received due to DH group being rejected)
  613. req = hapd.mgmt_rx(timeout=0.5)
  614. if req is not None:
  615. if req['subtype'] == 11:
  616. raise Exception("Unexpected Authentication frame seen")
  617. def test_sae_no_random(dev, apdev):
  618. """SAE and no random numbers available"""
  619. if "SAE" not in dev[0].get_capability("auth_alg"):
  620. raise HwsimSkip("SAE not supported")
  621. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  622. params['wpa_key_mgmt'] = 'SAE'
  623. hapd = hostapd.add_ap(apdev[0], params)
  624. dev[0].request("SET sae_groups ")
  625. tests = [ (1, "os_get_random;sae_get_rand"),
  626. (1, "os_get_random;get_rand_1_to_p_1"),
  627. (1, "os_get_random;get_random_qr_qnr"),
  628. (1, "os_get_random;sae_derive_pwe_ecc") ]
  629. for count, func in tests:
  630. with fail_test(dev[0], count, func):
  631. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  632. scan_freq="2412")
  633. dev[0].request("REMOVE_NETWORK all")
  634. dev[0].wait_disconnected()
  635. def test_sae_pwe_failure(dev, apdev):
  636. """SAE and pwe failure"""
  637. if "SAE" not in dev[0].get_capability("auth_alg"):
  638. raise HwsimSkip("SAE not supported")
  639. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  640. params['wpa_key_mgmt'] = 'SAE'
  641. params['sae_groups'] = '19 5'
  642. hapd = hostapd.add_ap(apdev[0], params)
  643. dev[0].request("SET sae_groups 19")
  644. with fail_test(dev[0], 1, "hmac_sha256_vector;sae_derive_pwe_ecc"):
  645. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  646. scan_freq="2412")
  647. dev[0].request("REMOVE_NETWORK all")
  648. dev[0].wait_disconnected()
  649. with fail_test(dev[0], 1, "sae_test_pwd_seed_ecc"):
  650. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  651. scan_freq="2412")
  652. dev[0].request("REMOVE_NETWORK all")
  653. dev[0].wait_disconnected()
  654. dev[0].request("SET sae_groups 5")
  655. with fail_test(dev[0], 1, "hmac_sha256_vector;sae_derive_pwe_ffc"):
  656. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  657. scan_freq="2412")
  658. dev[0].request("REMOVE_NETWORK all")
  659. dev[0].wait_disconnected()
  660. dev[0].request("SET sae_groups 5")
  661. with fail_test(dev[0], 1, "sae_test_pwd_seed_ffc"):
  662. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  663. scan_freq="2412")
  664. dev[0].request("REMOVE_NETWORK all")
  665. dev[0].wait_disconnected()
  666. with fail_test(dev[0], 2, "sae_test_pwd_seed_ffc"):
  667. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  668. scan_freq="2412")
  669. dev[0].request("REMOVE_NETWORK all")
  670. dev[0].wait_disconnected()
  671. def test_sae_bignum_failure(dev, apdev):
  672. """SAE and bignum failure"""
  673. if "SAE" not in dev[0].get_capability("auth_alg"):
  674. raise HwsimSkip("SAE not supported")
  675. params = hostapd.wpa2_params(ssid="test-sae", passphrase="12345678")
  676. params['wpa_key_mgmt'] = 'SAE'
  677. params['sae_groups'] = '19 5 22'
  678. hapd = hostapd.add_ap(apdev[0], params)
  679. dev[0].request("SET sae_groups 19")
  680. tests = [ (1, "crypto_bignum_init_set;get_rand_1_to_p_1"),
  681. (1, "crypto_bignum_init;is_quadratic_residue_blind"),
  682. (1, "crypto_bignum_mulmod;is_quadratic_residue_blind"),
  683. (2, "crypto_bignum_mulmod;is_quadratic_residue_blind"),
  684. (3, "crypto_bignum_mulmod;is_quadratic_residue_blind"),
  685. (1, "crypto_bignum_legendre;is_quadratic_residue_blind"),
  686. (1, "crypto_bignum_init_set;sae_test_pwd_seed_ecc"),
  687. (1, "crypto_ec_point_compute_y_sqr;sae_test_pwd_seed_ecc"),
  688. (1, "crypto_bignum_init_set;get_random_qr_qnr"),
  689. (1, "crypto_bignum_to_bin;sae_derive_pwe_ecc"),
  690. (1, "crypto_ec_point_init;sae_derive_pwe_ecc"),
  691. (1, "crypto_ec_point_solve_y_coord;sae_derive_pwe_ecc"),
  692. (1, "crypto_ec_point_init;sae_derive_commit_element_ecc"),
  693. (1, "crypto_ec_point_mul;sae_derive_commit_element_ecc"),
  694. (1, "crypto_ec_point_invert;sae_derive_commit_element_ecc"),
  695. (1, "crypto_bignum_init;=sae_derive_commit"),
  696. (1, "crypto_ec_point_init;sae_derive_k_ecc"),
  697. (1, "crypto_ec_point_mul;sae_derive_k_ecc"),
  698. (1, "crypto_ec_point_add;sae_derive_k_ecc"),
  699. (2, "crypto_ec_point_mul;sae_derive_k_ecc"),
  700. (1, "crypto_ec_point_to_bin;sae_derive_k_ecc"),
  701. (1, "crypto_bignum_legendre;get_random_qr_qnr"),
  702. (1, "sha256_prf;sae_derive_keys"),
  703. (1, "crypto_bignum_init;sae_derive_keys"),
  704. (1, "crypto_bignum_init_set;sae_parse_commit_scalar"),
  705. (1, "crypto_bignum_to_bin;sae_parse_commit_element_ecc"),
  706. (1, "crypto_ec_point_from_bin;sae_parse_commit_element_ecc") ]
  707. for count, func in tests:
  708. with fail_test(dev[0], count, func):
  709. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  710. scan_freq="2412", wait_connect=False)
  711. wait_fail_trigger(dev[0], "GET_FAIL")
  712. dev[0].request("REMOVE_NETWORK all")
  713. dev[0].request("SET sae_groups 5")
  714. tests = [ (1, "crypto_bignum_init_set;sae_set_group"),
  715. (2, "crypto_bignum_init_set;sae_set_group"),
  716. (1, "crypto_bignum_init_set;sae_get_rand"),
  717. (1, "crypto_bignum_init_set;sae_test_pwd_seed_ffc"),
  718. (1, "crypto_bignum_exptmod;sae_test_pwd_seed_ffc"),
  719. (1, "crypto_bignum_init;sae_derive_pwe_ffc"),
  720. (1, "crypto_bignum_init;sae_derive_commit_element_ffc"),
  721. (1, "crypto_bignum_exptmod;sae_derive_commit_element_ffc"),
  722. (1, "crypto_bignum_inverse;sae_derive_commit_element_ffc"),
  723. (1, "crypto_bignum_init;sae_derive_k_ffc"),
  724. (1, "crypto_bignum_exptmod;sae_derive_k_ffc"),
  725. (1, "crypto_bignum_mulmod;sae_derive_k_ffc"),
  726. (2, "crypto_bignum_exptmod;sae_derive_k_ffc"),
  727. (1, "crypto_bignum_to_bin;sae_derive_k_ffc"),
  728. (1, "crypto_bignum_init_set;sae_parse_commit_element_ffc"),
  729. (1, "crypto_bignum_init;sae_parse_commit_element_ffc"),
  730. (2, "crypto_bignum_init_set;sae_parse_commit_element_ffc"),
  731. (1, "crypto_bignum_exptmod;sae_parse_commit_element_ffc") ]
  732. for count, func in tests:
  733. with fail_test(dev[0], count, func):
  734. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  735. scan_freq="2412", wait_connect=False)
  736. wait_fail_trigger(dev[0], "GET_FAIL")
  737. dev[0].request("REMOVE_NETWORK all")
  738. dev[0].request("SET sae_groups 22")
  739. tests = [ (1, "crypto_bignum_init_set;sae_test_pwd_seed_ffc"),
  740. (1, "crypto_bignum_sub;sae_test_pwd_seed_ffc"),
  741. (1, "crypto_bignum_div;sae_test_pwd_seed_ffc") ]
  742. for count, func in tests:
  743. with fail_test(dev[0], count, func):
  744. dev[0].connect("test-sae", psk="12345678", key_mgmt="SAE",
  745. scan_freq="2412", wait_connect=False)
  746. wait_fail_trigger(dev[0], "GET_FAIL")
  747. dev[0].request("REMOVE_NETWORK all")