test_ap_ciphers.py 33 KB

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