test_ap_ciphers.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. # Cipher suite tests
  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. from remotehost import remote_compatible
  7. import time
  8. import logging
  9. logger = logging.getLogger()
  10. import os
  11. import subprocess
  12. import hwsim_utils
  13. import hostapd
  14. from utils import HwsimSkip, skip_with_fips, require_under_vm
  15. from wlantest import Wlantest
  16. def check_cipher(dev, ap, cipher):
  17. if cipher not in dev.get_capability("pairwise"):
  18. raise HwsimSkip("Cipher %s not supported" % cipher)
  19. params = { "ssid": "test-wpa2-psk",
  20. "wpa_passphrase": "12345678",
  21. "wpa": "2",
  22. "wpa_key_mgmt": "WPA-PSK",
  23. "rsn_pairwise": cipher }
  24. hapd = hostapd.add_ap(ap, params)
  25. dev.connect("test-wpa2-psk", psk="12345678",
  26. pairwise=cipher, group=cipher, scan_freq="2412")
  27. hwsim_utils.test_connectivity(dev, hapd)
  28. def check_group_mgmt_cipher(dev, ap, cipher, sta_req_cipher=None):
  29. if cipher not in dev.get_capability("group_mgmt"):
  30. raise HwsimSkip("Cipher %s not supported" % cipher)
  31. params = { "ssid": "test-wpa2-psk-pmf",
  32. "wpa_passphrase": "12345678",
  33. "wpa": "2",
  34. "ieee80211w": "2",
  35. "wpa_key_mgmt": "WPA-PSK-SHA256",
  36. "rsn_pairwise": "CCMP",
  37. "group_mgmt_cipher": cipher }
  38. hapd = hostapd.add_ap(ap, params)
  39. Wlantest.setup(hapd)
  40. wt = Wlantest()
  41. wt.flush()
  42. wt.add_passphrase("12345678")
  43. dev.connect("test-wpa2-psk-pmf", psk="12345678", ieee80211w="2",
  44. key_mgmt="WPA-PSK-SHA256", group_mgmt=sta_req_cipher,
  45. pairwise="CCMP", group="CCMP", scan_freq="2412")
  46. hwsim_utils.test_connectivity(dev, hapd)
  47. hapd.request("DEAUTHENTICATE ff:ff:ff:ff:ff:ff")
  48. dev.wait_disconnected()
  49. if wt.get_bss_counter('valid_bip_mmie', ap['bssid']) < 1:
  50. raise Exception("No valid BIP MMIE seen")
  51. if wt.get_bss_counter('bip_deauth', ap['bssid']) < 1:
  52. raise Exception("No valid BIP deauth seen")
  53. if cipher == "AES-128-CMAC":
  54. group_mgmt = "BIP"
  55. else:
  56. group_mgmt = cipher
  57. res = wt.info_bss('group_mgmt', ap['bssid']).strip()
  58. if res != group_mgmt:
  59. raise Exception("Unexpected group mgmt cipher: " + res)
  60. @remote_compatible
  61. def test_ap_cipher_tkip(dev, apdev):
  62. """WPA2-PSK/TKIP connection"""
  63. skip_with_fips(dev[0])
  64. check_cipher(dev[0], apdev[0], "TKIP")
  65. @remote_compatible
  66. def test_ap_cipher_tkip_countermeasures_ap(dev, apdev):
  67. """WPA-PSK/TKIP countermeasures (detected by AP)"""
  68. skip_with_fips(dev[0])
  69. testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (dev[0].get_driver_status_field("phyname"), dev[0].ifname)
  70. if dev[0].cmd_execute([ "ls", testfile ])[0] != 0:
  71. raise HwsimSkip("tkip_mic_test not supported in mac80211")
  72. params = { "ssid": "tkip-countermeasures",
  73. "wpa_passphrase": "12345678",
  74. "wpa": "1",
  75. "wpa_key_mgmt": "WPA-PSK",
  76. "wpa_pairwise": "TKIP" }
  77. hapd = hostapd.add_ap(apdev[0], params)
  78. dev[0].connect("tkip-countermeasures", psk="12345678",
  79. pairwise="TKIP", group="TKIP", scan_freq="2412")
  80. dev[0].dump_monitor()
  81. dev[0].cmd_execute([ "echo", "-n", apdev[0]['bssid'], ">", testfile ],
  82. shell=True)
  83. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
  84. if ev is not None:
  85. raise Exception("Unexpected disconnection on first Michael MIC failure")
  86. dev[0].cmd_execute([ "echo", "-n", "ff:ff:ff:ff:ff:ff", ">", testfile ],
  87. shell=True)
  88. ev = dev[0].wait_disconnected(timeout=10,
  89. error="No disconnection after two Michael MIC failures")
  90. if "reason=14" not in ev:
  91. raise Exception("Unexpected disconnection reason: " + ev)
  92. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  93. if ev is not None:
  94. raise Exception("Unexpected connection during TKIP countermeasures")
  95. @remote_compatible
  96. def test_ap_cipher_tkip_countermeasures_sta(dev, apdev):
  97. """WPA-PSK/TKIP countermeasures (detected by STA)"""
  98. skip_with_fips(dev[0])
  99. params = { "ssid": "tkip-countermeasures",
  100. "wpa_passphrase": "12345678",
  101. "wpa": "1",
  102. "wpa_key_mgmt": "WPA-PSK",
  103. "wpa_pairwise": "TKIP" }
  104. hapd = hostapd.add_ap(apdev[0], params)
  105. testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (hapd.get_driver_status_field("phyname"), apdev[0]['ifname'])
  106. if hapd.cmd_execute([ "ls", testfile ])[0] != 0:
  107. raise HwsimSkip("tkip_mic_test not supported in mac80211")
  108. dev[0].connect("tkip-countermeasures", psk="12345678",
  109. pairwise="TKIP", group="TKIP", scan_freq="2412")
  110. dev[0].dump_monitor()
  111. hapd.cmd_execute([ "echo", "-n", dev[0].own_addr(), ">", testfile ],
  112. shell=True)
  113. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
  114. if ev is not None:
  115. raise Exception("Unexpected disconnection on first Michael MIC failure")
  116. hapd.cmd_execute([ "echo", "-n", "ff:ff:ff:ff:ff:ff", ">", testfile ],
  117. shell=True)
  118. ev = dev[0].wait_disconnected(timeout=10,
  119. error="No disconnection after two Michael MIC failures")
  120. if "reason=14 locally_generated=1" not in ev:
  121. raise Exception("Unexpected disconnection reason: " + ev)
  122. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  123. if ev is not None:
  124. raise Exception("Unexpected connection during TKIP countermeasures")
  125. def test_ap_cipher_tkip_countermeasures_sta2(dev, apdev, params):
  126. """WPA-PSK/TKIP countermeasures (detected by two STAs) [long]"""
  127. if not params['long']:
  128. raise HwsimSkip("Skip test case with long duration due to --long not specified")
  129. skip_with_fips(dev[0])
  130. params = { "ssid": "tkip-countermeasures",
  131. "wpa_passphrase": "12345678",
  132. "wpa": "1",
  133. "wpa_key_mgmt": "WPA-PSK",
  134. "wpa_pairwise": "TKIP" }
  135. hapd = hostapd.add_ap(apdev[0], params)
  136. testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (hapd.get_driver_status_field("phyname"), apdev[0]['ifname'])
  137. if hapd.cmd_execute([ "ls", testfile ])[0] != 0:
  138. raise HwsimSkip("tkip_mic_test not supported in mac80211")
  139. dev[0].connect("tkip-countermeasures", psk="12345678",
  140. pairwise="TKIP", group="TKIP", scan_freq="2412")
  141. dev[0].dump_monitor()
  142. id = dev[1].connect("tkip-countermeasures", psk="12345678",
  143. pairwise="TKIP", group="TKIP", scan_freq="2412")
  144. dev[1].dump_monitor()
  145. hapd.cmd_execute([ "echo", "-n", "ff:ff:ff:ff:ff:ff", ">", testfile ],
  146. shell=True)
  147. ev = dev[0].wait_disconnected(timeout=10,
  148. error="No disconnection after two Michael MIC failure")
  149. if "reason=14" not in ev:
  150. raise Exception("Unexpected disconnection reason: " + ev)
  151. ev = dev[1].wait_disconnected(timeout=5,
  152. error="No disconnection after two Michael MIC failure")
  153. if "reason=14" not in ev:
  154. raise Exception("Unexpected disconnection reason: " + ev)
  155. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  156. if ev is not None:
  157. raise Exception("Unexpected connection during TKIP countermeasures")
  158. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  159. if ev is not None:
  160. raise Exception("Unexpected connection during TKIP countermeasures")
  161. dev[0].request("REMOVE_NETWORK all")
  162. logger.info("Waiting for TKIP countermeasures to end")
  163. connected = False
  164. start = os.times()[4]
  165. while True:
  166. now = os.times()[4]
  167. if start + 70 < now:
  168. break
  169. dev[0].connect("tkip-countermeasures", psk="12345678",
  170. pairwise="TKIP", group="TKIP", scan_freq="2412",
  171. wait_connect=False)
  172. ev = dev[0].wait_event(["CTRL-EVENT-AUTH-REJECT",
  173. "CTRL-EVENT-CONNECTED"], timeout=10)
  174. if ev is None:
  175. raise Exception("No connection result")
  176. if "CTRL-EVENT-CONNECTED" in ev:
  177. connected = True
  178. break
  179. if "status_code=14" not in ev:
  180. raise Exception("Unexpected connection failure reason during TKIP countermeasures: " + ev)
  181. dev[0].request("REMOVE_NETWORK all")
  182. time.sleep(1)
  183. dev[0].dump_monitor()
  184. dev[1].dump_monitor()
  185. if not connected:
  186. raise Exception("No connection after TKIP countermeasures terminated")
  187. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  188. if ev is None:
  189. dev[1].request("DISCONNECT")
  190. dev[1].select_network(id)
  191. dev[1].wait_connected()
  192. @remote_compatible
  193. def test_ap_cipher_ccmp(dev, apdev):
  194. """WPA2-PSK/CCMP connection"""
  195. check_cipher(dev[0], apdev[0], "CCMP")
  196. def test_ap_cipher_gcmp(dev, apdev):
  197. """WPA2-PSK/GCMP connection"""
  198. check_cipher(dev[0], apdev[0], "GCMP")
  199. def test_ap_cipher_ccmp_256(dev, apdev):
  200. """WPA2-PSK/CCMP-256 connection"""
  201. check_cipher(dev[0], apdev[0], "CCMP-256")
  202. def test_ap_cipher_gcmp_256(dev, apdev):
  203. """WPA2-PSK/GCMP-256 connection"""
  204. check_cipher(dev[0], apdev[0], "GCMP-256")
  205. @remote_compatible
  206. def test_ap_cipher_mixed_wpa_wpa2(dev, apdev):
  207. """WPA2-PSK/CCMP/ and WPA-PSK/TKIP mixed configuration"""
  208. skip_with_fips(dev[0])
  209. ssid = "test-wpa-wpa2-psk"
  210. passphrase = "12345678"
  211. params = { "ssid": ssid,
  212. "wpa_passphrase": passphrase,
  213. "wpa": "3",
  214. "wpa_key_mgmt": "WPA-PSK",
  215. "rsn_pairwise": "CCMP",
  216. "wpa_pairwise": "TKIP" }
  217. hapd = hostapd.add_ap(apdev[0], params)
  218. dev[0].connect(ssid, psk=passphrase, proto="WPA2",
  219. pairwise="CCMP", group="TKIP", scan_freq="2412")
  220. status = dev[0].get_status()
  221. if status['key_mgmt'] != 'WPA2-PSK':
  222. raise Exception("Incorrect key_mgmt reported")
  223. if status['pairwise_cipher'] != 'CCMP':
  224. raise Exception("Incorrect pairwise_cipher reported")
  225. if status['group_cipher'] != 'TKIP':
  226. raise Exception("Incorrect group_cipher reported")
  227. bss = dev[0].get_bss(apdev[0]['bssid'])
  228. if bss['ssid'] != ssid:
  229. raise Exception("Unexpected SSID in the BSS entry")
  230. if "[WPA-PSK-TKIP]" not in bss['flags']:
  231. raise Exception("Missing BSS flag WPA-PSK-TKIP")
  232. if "[WPA2-PSK-CCMP]" not in bss['flags']:
  233. raise Exception("Missing BSS flag WPA2-PSK-CCMP")
  234. hwsim_utils.test_connectivity(dev[0], hapd)
  235. dev[1].connect(ssid, psk=passphrase, proto="WPA",
  236. pairwise="TKIP", group="TKIP", scan_freq="2412")
  237. status = dev[1].get_status()
  238. if status['key_mgmt'] != 'WPA-PSK':
  239. raise Exception("Incorrect key_mgmt reported")
  240. if status['pairwise_cipher'] != 'TKIP':
  241. raise Exception("Incorrect pairwise_cipher reported")
  242. if status['group_cipher'] != 'TKIP':
  243. raise Exception("Incorrect group_cipher reported")
  244. hwsim_utils.test_connectivity(dev[1], hapd)
  245. hwsim_utils.test_connectivity(dev[0], dev[1])
  246. @remote_compatible
  247. def test_ap_cipher_bip(dev, apdev):
  248. """WPA2-PSK with BIP"""
  249. check_group_mgmt_cipher(dev[0], apdev[0], "AES-128-CMAC")
  250. def test_ap_cipher_bip_req(dev, apdev):
  251. """WPA2-PSK with BIP required"""
  252. check_group_mgmt_cipher(dev[0], apdev[0], "AES-128-CMAC", "AES-128-CMAC")
  253. def test_ap_cipher_bip_req2(dev, apdev):
  254. """WPA2-PSK with BIP required (2)"""
  255. check_group_mgmt_cipher(dev[0], apdev[0], "AES-128-CMAC",
  256. "AES-128-CMAC BIP-GMAC-128 BIP-GMAC-256 BIP-CMAC-256")
  257. def test_ap_cipher_bip_gmac_128(dev, apdev):
  258. """WPA2-PSK with BIP-GMAC-128"""
  259. check_group_mgmt_cipher(dev[0], apdev[0], "BIP-GMAC-128")
  260. def test_ap_cipher_bip_gmac_128_req(dev, apdev):
  261. """WPA2-PSK with BIP-GMAC-128 required"""
  262. check_group_mgmt_cipher(dev[0], apdev[0], "BIP-GMAC-128", "BIP-GMAC-128")
  263. def test_ap_cipher_bip_gmac_256(dev, apdev):
  264. """WPA2-PSK with BIP-GMAC-256"""
  265. check_group_mgmt_cipher(dev[0], apdev[0], "BIP-GMAC-256")
  266. def test_ap_cipher_bip_gmac_256_req(dev, apdev):
  267. """WPA2-PSK with BIP-GMAC-256 required"""
  268. check_group_mgmt_cipher(dev[0], apdev[0], "BIP-GMAC-256", "BIP-GMAC-256")
  269. def test_ap_cipher_bip_cmac_256(dev, apdev):
  270. """WPA2-PSK with BIP-CMAC-256"""
  271. check_group_mgmt_cipher(dev[0], apdev[0], "BIP-CMAC-256")
  272. def test_ap_cipher_bip_cmac_256_req(dev, apdev):
  273. """WPA2-PSK with BIP-CMAC-256 required"""
  274. check_group_mgmt_cipher(dev[0], apdev[0], "BIP-CMAC-256", "BIP-CMAC-256")
  275. def test_ap_cipher_bip_req_mismatch(dev, apdev):
  276. """WPA2-PSK with BIP cipher mismatch"""
  277. group_mgmt = dev[0].get_capability("group_mgmt")
  278. for cipher in [ "AES-128-CMAC", "BIP-GMAC-256" ]:
  279. if cipher not in group_mgmt:
  280. raise HwsimSkip("Cipher %s not supported" % cipher)
  281. params = { "ssid": "test-wpa2-psk-pmf",
  282. "wpa_passphrase": "12345678",
  283. "wpa": "2",
  284. "ieee80211w": "2",
  285. "wpa_key_mgmt": "WPA-PSK-SHA256",
  286. "rsn_pairwise": "CCMP",
  287. "group_mgmt_cipher": "AES-128-CMAC" }
  288. hapd = hostapd.add_ap(apdev[0], params)
  289. dev[0].scan_for_bss(hapd.own_addr(), 2412)
  290. id = dev[0].connect("test-wpa2-psk-pmf", psk="12345678", ieee80211w="2",
  291. key_mgmt="WPA-PSK-SHA256", group_mgmt="BIP-GMAC-256",
  292. pairwise="CCMP", group="CCMP", scan_freq="2412",
  293. wait_connect=False)
  294. ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND",
  295. "CTRL-EVENT-CONNECTED"], timeout=10)
  296. if ev is None:
  297. raise Exception("Network selection result not indicated")
  298. if "CTRL-EVENT-CONNECTED" in ev:
  299. raise Exception("Unexpected connection")
  300. dev[0].request("DISCONNECT")
  301. dev[0].set_network(id, "group_mgmt", "AES-128-CMAC")
  302. dev[0].select_network(id)
  303. dev[0].wait_connected()
  304. def get_rx_spec(phy, gtk=False):
  305. keys = "/sys/kernel/debug/ieee80211/%s/keys" % (phy)
  306. try:
  307. for key in os.listdir(keys):
  308. keydir = keys + "/" + key
  309. files = os.listdir(keydir)
  310. if not gtk and "station" not in files:
  311. continue
  312. if gtk and "station" in files:
  313. continue
  314. with open(keydir + "/rx_spec") as f:
  315. return f.read()
  316. except OSError, e:
  317. raise HwsimSkip("debugfs not supported in mac80211")
  318. return None
  319. def get_tk_replay_counter(phy, gtk=False):
  320. keys = "/sys/kernel/debug/ieee80211/%s/keys" % (phy)
  321. try:
  322. for key in os.listdir(keys):
  323. keydir = keys + "/" + key
  324. files = os.listdir(keydir)
  325. if not gtk and "station" not in files:
  326. continue
  327. if gtk and "station" in files:
  328. continue
  329. with open(keydir + "/replays") as f:
  330. return int(f.read())
  331. except OSError, e:
  332. raise HwsimSkip("debugfs not supported in mac80211")
  333. return None
  334. def test_ap_cipher_replay_protection_ap_ccmp(dev, apdev):
  335. """CCMP replay protection on AP"""
  336. run_ap_cipher_replay_protection_ap(dev, apdev, "CCMP")
  337. def test_ap_cipher_replay_protection_ap_tkip(dev, apdev):
  338. """TKIP replay protection on AP"""
  339. run_ap_cipher_replay_protection_ap(dev, apdev, "TKIP")
  340. def test_ap_cipher_replay_protection_ap_gcmp(dev, apdev):
  341. """GCMP replay protection on AP"""
  342. if "GCMP" not in dev[0].get_capability("pairwise"):
  343. raise HwsimSkip("GCMP not supported")
  344. run_ap_cipher_replay_protection_ap(dev, apdev, "GCMP")
  345. def run_ap_cipher_replay_protection_ap(dev, apdev, cipher):
  346. params = { "ssid": "test-wpa2-psk",
  347. "wpa_passphrase": "12345678",
  348. "wpa": "2",
  349. "wpa_key_mgmt": "WPA-PSK",
  350. "rsn_pairwise": cipher }
  351. hapd = hostapd.add_ap(apdev[0], params)
  352. phy = hapd.get_driver_status_field("phyname")
  353. Wlantest.setup(hapd)
  354. wt = Wlantest()
  355. wt.flush()
  356. wt.add_passphrase("12345678")
  357. dev[0].connect("test-wpa2-psk", psk="12345678",
  358. pairwise=cipher, group=cipher, scan_freq="2412")
  359. if cipher != "TKIP":
  360. replays = get_tk_replay_counter(phy)
  361. if replays != 0:
  362. raise Exception("Unexpected replay reported (1)")
  363. for i in range(5):
  364. hwsim_utils.test_connectivity(dev[0], hapd)
  365. if cipher != "TKIP":
  366. replays = get_tk_replay_counter(phy)
  367. if replays != 0:
  368. raise Exception("Unexpected replay reported (2)")
  369. if "OK" not in dev[0].request("RESET_PN"):
  370. raise Exception("RESET_PN failed")
  371. time.sleep(0.1)
  372. hwsim_utils.test_connectivity(dev[0], hapd, timeout=1,
  373. success_expected=False)
  374. if cipher != "TKIP":
  375. replays = get_tk_replay_counter(phy)
  376. if replays < 1:
  377. raise Exception("Replays not reported")
  378. def test_ap_cipher_replay_protection_sta_ccmp(dev, apdev):
  379. """CCMP replay protection on STA (TK)"""
  380. run_ap_cipher_replay_protection_sta(dev, apdev, "CCMP")
  381. def test_ap_cipher_replay_protection_sta_tkip(dev, apdev):
  382. """TKIP replay protection on STA (TK)"""
  383. run_ap_cipher_replay_protection_sta(dev, apdev, "TKIP")
  384. def test_ap_cipher_replay_protection_sta_gcmp(dev, apdev):
  385. """GCMP replay protection on STA (TK)"""
  386. if "GCMP" not in dev[0].get_capability("pairwise"):
  387. raise HwsimSkip("GCMP not supported")
  388. run_ap_cipher_replay_protection_sta(dev, apdev, "GCMP")
  389. def test_ap_cipher_replay_protection_sta_gtk_ccmp(dev, apdev):
  390. """CCMP replay protection on STA (GTK)"""
  391. run_ap_cipher_replay_protection_sta(dev, apdev, "CCMP", gtk=True)
  392. def test_ap_cipher_replay_protection_sta_gtk_tkip(dev, apdev):
  393. """TKIP replay protection on STA (GTK)"""
  394. run_ap_cipher_replay_protection_sta(dev, apdev, "TKIP", gtk=True)
  395. def test_ap_cipher_replay_protection_sta_gtk_gcmp(dev, apdev):
  396. """GCMP replay protection on STA (GTK)"""
  397. if "GCMP" not in dev[0].get_capability("pairwise"):
  398. raise HwsimSkip("GCMP not supported")
  399. run_ap_cipher_replay_protection_sta(dev, apdev, "GCMP", gtk=True)
  400. def run_ap_cipher_replay_protection_sta(dev, apdev, cipher, gtk=False):
  401. params = { "ssid": "test-wpa2-psk",
  402. "wpa_passphrase": "12345678",
  403. "wpa": "2",
  404. "wpa_key_mgmt": "WPA-PSK",
  405. "rsn_pairwise": cipher }
  406. hapd = hostapd.add_ap(apdev[0], params)
  407. Wlantest.setup(hapd)
  408. wt = Wlantest()
  409. wt.flush()
  410. wt.add_passphrase("12345678")
  411. phy = dev[0].get_driver_status_field("phyname")
  412. dev[0].connect("test-wpa2-psk", psk="12345678",
  413. pairwise=cipher, group=cipher, scan_freq="2412")
  414. if cipher != "TKIP":
  415. replays = get_tk_replay_counter(phy, gtk)
  416. if replays != 0:
  417. raise Exception("Unexpected replay reported (1)")
  418. for i in range(5):
  419. hwsim_utils.test_connectivity(dev[0], hapd)
  420. if cipher != "TKIP":
  421. replays = get_tk_replay_counter(phy, gtk)
  422. if replays != 0:
  423. raise Exception("Unexpected replay reported (2)")
  424. addr = "ff:ff:ff:ff:ff:ff" if gtk else dev[0].own_addr()
  425. if "OK" not in hapd.request("RESET_PN " + addr):
  426. raise Exception("RESET_PN failed")
  427. time.sleep(0.1)
  428. hwsim_utils.test_connectivity(dev[0], hapd, timeout=1,
  429. success_expected=False)
  430. if cipher != "TKIP":
  431. replays = get_tk_replay_counter(phy, gtk)
  432. if replays < 1:
  433. raise Exception("Replays not reported")
  434. def test_ap_wpa2_delayed_m3_retransmission(dev, apdev):
  435. """Delayed M3 retransmission"""
  436. require_under_vm()
  437. try:
  438. subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=1'],
  439. stdout=open('/dev/null', 'w'))
  440. subprocess.call(['sysctl', '-w',
  441. 'net.ipv6.conf.default.disable_ipv6=1'],
  442. stdout=open('/dev/null', 'w'))
  443. run_ap_wpa2_delayed_m3_retransmission(dev, apdev)
  444. finally:
  445. subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=0'],
  446. stdout=open('/dev/null', 'w'))
  447. subprocess.call(['sysctl', '-w',
  448. 'net.ipv6.conf.default.disable_ipv6=0'],
  449. stdout=open('/dev/null', 'w'))
  450. def run_ap_wpa2_delayed_m3_retransmission(dev, apdev):
  451. params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
  452. hapd = hostapd.add_ap(apdev[0], params)
  453. Wlantest.setup(hapd)
  454. wt = Wlantest()
  455. wt.flush()
  456. wt.add_passphrase("12345678")
  457. phy = dev[0].get_driver_status_field("phyname")
  458. dev[0].connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
  459. for i in range(5):
  460. hwsim_utils.test_connectivity(dev[0], hapd)
  461. time.sleep(0.1)
  462. before_tk = get_rx_spec(phy, gtk=False).splitlines()
  463. before_gtk = get_rx_spec(phy, gtk=True).splitlines()
  464. addr = dev[0].own_addr()
  465. if "OK" not in hapd.request("RESEND_M3 " + addr):
  466. raise Exception("RESEND_M3 failed")
  467. time.sleep(0.1)
  468. after_tk = get_rx_spec(phy, gtk=False).splitlines()
  469. after_gtk = get_rx_spec(phy, gtk=True).splitlines()
  470. if "OK" not in hapd.request("RESET_PN " + addr):
  471. raise Exception("RESET_PN failed")
  472. time.sleep(0.1)
  473. hwsim_utils.test_connectivity(dev[0], hapd, timeout=1,
  474. success_expected=False)
  475. dev[0].request("DISCONNECT")
  476. dev[0].wait_disconnected()
  477. for i in range(len(before_tk)):
  478. b = int(before_tk[i], 16)
  479. a = int(after_tk[i], 16)
  480. if a < b:
  481. raise Exception("TK RX counter decreased: idx=%d before=%d after=%d" % (i, b, a))
  482. for i in range(len(before_gtk)):
  483. b = int(before_gtk[i], 16)
  484. a = int(after_gtk[i], 16)
  485. if a < b:
  486. raise Exception("GTK RX counter decreased: idx=%d before=%d after=%d" % (i, b, a))
  487. def test_ap_wpa2_delayed_m1_m3_retransmission(dev, apdev):
  488. """Delayed M1+M3 retransmission"""
  489. require_under_vm()
  490. try:
  491. subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=1'],
  492. stdout=open('/dev/null', 'w'))
  493. subprocess.call(['sysctl', '-w',
  494. 'net.ipv6.conf.default.disable_ipv6=1'],
  495. stdout=open('/dev/null', 'w'))
  496. run_ap_wpa2_delayed_m1_m3_retransmission(dev, apdev)
  497. finally:
  498. subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=0'],
  499. stdout=open('/dev/null', 'w'))
  500. subprocess.call(['sysctl', '-w',
  501. 'net.ipv6.conf.default.disable_ipv6=0'],
  502. stdout=open('/dev/null', 'w'))
  503. def test_ap_wpa2_delayed_m1_m3_retransmission2(dev, apdev):
  504. """Delayed M1+M3 retransmission (change M1 ANonce)"""
  505. require_under_vm()
  506. try:
  507. subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=1'],
  508. stdout=open('/dev/null', 'w'))
  509. subprocess.call(['sysctl', '-w',
  510. 'net.ipv6.conf.default.disable_ipv6=1'],
  511. stdout=open('/dev/null', 'w'))
  512. run_ap_wpa2_delayed_m1_m3_retransmission(dev, apdev, True)
  513. finally:
  514. subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=0'],
  515. stdout=open('/dev/null', 'w'))
  516. subprocess.call(['sysctl', '-w',
  517. 'net.ipv6.conf.default.disable_ipv6=0'],
  518. stdout=open('/dev/null', 'w'))
  519. def run_ap_wpa2_delayed_m1_m3_retransmission(dev, apdev,
  520. change_m1_anonce=False):
  521. params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
  522. hapd = hostapd.add_ap(apdev[0], params)
  523. Wlantest.setup(hapd)
  524. wt = Wlantest()
  525. wt.flush()
  526. wt.add_passphrase("12345678")
  527. phy = dev[0].get_driver_status_field("phyname")
  528. dev[0].connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
  529. for i in range(5):
  530. hwsim_utils.test_connectivity(dev[0], hapd)
  531. time.sleep(0.1)
  532. before_tk = get_rx_spec(phy, gtk=False).splitlines()
  533. before_gtk = get_rx_spec(phy, gtk=True).splitlines()
  534. addr = dev[0].own_addr()
  535. if change_m1_anonce:
  536. if "OK" not in hapd.request("RESEND_M1 " + addr + " change-anonce"):
  537. raise Exception("RESEND_M1 failed")
  538. if "OK" not in hapd.request("RESEND_M1 " + addr):
  539. raise Exception("RESEND_M1 failed")
  540. if "OK" not in hapd.request("RESEND_M3 " + addr):
  541. raise Exception("RESEND_M3 failed")
  542. time.sleep(0.1)
  543. after_tk = get_rx_spec(phy, gtk=False).splitlines()
  544. after_gtk = get_rx_spec(phy, gtk=True).splitlines()
  545. if "OK" not in hapd.request("RESET_PN " + addr):
  546. raise Exception("RESET_PN failed")
  547. time.sleep(0.1)
  548. hwsim_utils.test_connectivity(dev[0], hapd, timeout=1,
  549. success_expected=False)
  550. dev[0].request("DISCONNECT")
  551. dev[0].wait_disconnected()
  552. for i in range(len(before_tk)):
  553. b = int(before_tk[i], 16)
  554. a = int(after_tk[i], 16)
  555. if a < b:
  556. raise Exception("TK RX counter decreased: idx=%d before=%d after=%d" % (i, b, a))
  557. for i in range(len(before_gtk)):
  558. b = int(before_gtk[i], 16)
  559. a = int(after_gtk[i], 16)
  560. if a < b:
  561. raise Exception("GTK RX counter decreased: idx=%d before=%d after=%d" % (i, b, a))
  562. def test_ap_wpa2_delayed_group_m1_retransmission(dev, apdev):
  563. """Delayed group M1 retransmission"""
  564. require_under_vm()
  565. try:
  566. subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=1'],
  567. stdout=open('/dev/null', 'w'))
  568. subprocess.call(['sysctl', '-w',
  569. 'net.ipv6.conf.default.disable_ipv6=1'],
  570. stdout=open('/dev/null', 'w'))
  571. run_ap_wpa2_delayed_group_m1_retransmission(dev, apdev)
  572. finally:
  573. subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=0'],
  574. stdout=open('/dev/null', 'w'))
  575. subprocess.call(['sysctl', '-w',
  576. 'net.ipv6.conf.default.disable_ipv6=0'],
  577. stdout=open('/dev/null', 'w'))
  578. def run_ap_wpa2_delayed_group_m1_retransmission(dev, apdev):
  579. params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
  580. hapd = hostapd.add_ap(apdev[0], params)
  581. Wlantest.setup(hapd)
  582. wt = Wlantest()
  583. wt.flush()
  584. wt.add_passphrase("12345678")
  585. phy = dev[0].get_driver_status_field("phyname")
  586. dev[0].connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
  587. for i in range(5):
  588. hwsim_utils.test_connectivity(dev[0], hapd)
  589. time.sleep(0.1)
  590. before = get_rx_spec(phy, gtk=True).splitlines()
  591. addr = dev[0].own_addr()
  592. if "OK" not in hapd.request("RESEND_GROUP_M1 " + addr):
  593. raise Exception("RESEND_GROUP_M1 failed")
  594. time.sleep(0.1)
  595. after = get_rx_spec(phy, gtk=True).splitlines()
  596. if "OK" not in hapd.request("RESET_PN " + addr):
  597. raise Exception("RESET_PN failed")
  598. time.sleep(0.1)
  599. hwsim_utils.test_connectivity(dev[0], hapd, timeout=1,
  600. success_expected=False)
  601. dev[0].request("DISCONNECT")
  602. dev[0].wait_disconnected()
  603. for i in range(len(before)):
  604. b = int(before[i], 16)
  605. a = int(after[i], 16)
  606. if a < b:
  607. raise Exception("RX counter decreased: idx=%d before=%d after=%d" % (i, b, a))
  608. def test_ap_wpa2_delayed_m1_m3_zero_tk(dev, apdev):
  609. """Delayed M1+M3 retransmission and zero TK"""
  610. params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
  611. hapd = hostapd.add_ap(apdev[0], params)
  612. Wlantest.setup(hapd)
  613. wt = Wlantest()
  614. wt.flush()
  615. wt.add_passphrase("12345678")
  616. dev[0].connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
  617. hwsim_utils.test_connectivity(dev[0], hapd)
  618. addr = dev[0].own_addr()
  619. if "OK" not in hapd.request("RESEND_M1 " + addr + " change-anonce"):
  620. raise Exception("RESEND_M1 failed")
  621. if "OK" not in hapd.request("RESEND_M1 " + addr):
  622. raise Exception("RESEND_M1 failed")
  623. if "OK" not in hapd.request("RESEND_M3 " + addr):
  624. raise Exception("RESEND_M3 failed")
  625. if "OK" not in hapd.request("SET_KEY 3 %s %d %d %s %s" % (addr, 0, 1, 6*"00", 16*"00")):
  626. raise Exception("SET_KEY failed")
  627. time.sleep(0.1)
  628. hwsim_utils.test_connectivity(dev[0], hapd, timeout=1, broadcast=False,
  629. success_expected=False)
  630. dev[0].request("DISCONNECT")
  631. dev[0].wait_disconnected()
  632. def test_ap_wpa2_plaintext_m1_m3(dev, apdev):
  633. """Plaintext M1/M3 during PTK rekey"""
  634. params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
  635. hapd = hostapd.add_ap(apdev[0], params)
  636. Wlantest.setup(hapd)
  637. wt = Wlantest()
  638. wt.flush()
  639. wt.add_passphrase("12345678")
  640. dev[0].connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
  641. time.sleep(0.1)
  642. addr = dev[0].own_addr()
  643. if "OK" not in hapd.request("RESEND_M1 " + addr + " plaintext"):
  644. raise Exception("RESEND_M1 failed")
  645. time.sleep(0.1)
  646. if "OK" not in hapd.request("RESEND_M3 " + addr + " plaintext"):
  647. raise Exception("RESEND_M3 failed")
  648. time.sleep(0.1)
  649. def test_ap_wpa2_plaintext_m3(dev, apdev):
  650. """Plaintext M3 during PTK rekey"""
  651. params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
  652. hapd = hostapd.add_ap(apdev[0], params)
  653. Wlantest.setup(hapd)
  654. wt = Wlantest()
  655. wt.flush()
  656. wt.add_passphrase("12345678")
  657. dev[0].connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
  658. time.sleep(0.1)
  659. addr = dev[0].own_addr()
  660. if "OK" not in hapd.request("RESEND_M1 " + addr):
  661. raise Exception("RESEND_M1 failed")
  662. time.sleep(0.1)
  663. if "OK" not in hapd.request("RESEND_M3 " + addr + " plaintext"):
  664. raise Exception("RESEND_M3 failed")
  665. time.sleep(0.1)
  666. def test_ap_wpa2_plaintext_group_m1(dev, apdev):
  667. """Plaintext group M1"""
  668. params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
  669. hapd = hostapd.add_ap(apdev[0], params)
  670. Wlantest.setup(hapd)
  671. wt = Wlantest()
  672. wt.flush()
  673. wt.add_passphrase("12345678")
  674. dev[0].connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
  675. time.sleep(0.1)
  676. addr = dev[0].own_addr()
  677. if "OK" not in hapd.request("RESEND_GROUP_M1 " + addr + " plaintext"):
  678. raise Exception("RESEND_GROUP_M1 failed")
  679. time.sleep(0.2)
  680. if "OK" not in hapd.request("RESEND_GROUP_M1 " + addr):
  681. raise Exception("RESEND_GROUP_M1 failed")
  682. time.sleep(0.1)