test_ap_ciphers.py 35 KB

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