test_ap_ciphers.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. for key in os.listdir(keys):
  307. keydir = keys + "/" + key
  308. files = os.listdir(keydir)
  309. if not gtk and "station" not in files:
  310. continue
  311. if gtk and "station" in files:
  312. continue
  313. with open(keydir + "/rx_spec") as f:
  314. return f.read()
  315. return None
  316. def test_ap_wpa2_delayed_group_m1_retransmission(dev, apdev):
  317. """Delayed group M1 retransmission"""
  318. require_under_vm()
  319. try:
  320. subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=1'],
  321. stdout=open('/dev/null', 'w'))
  322. subprocess.call(['sysctl', '-w',
  323. 'net.ipv6.conf.default.disable_ipv6=1'],
  324. stdout=open('/dev/null', 'w'))
  325. run_ap_wpa2_delayed_group_m1_retransmission(dev, apdev)
  326. finally:
  327. subprocess.call(['sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=0'],
  328. stdout=open('/dev/null', 'w'))
  329. subprocess.call(['sysctl', '-w',
  330. 'net.ipv6.conf.default.disable_ipv6=0'],
  331. stdout=open('/dev/null', 'w'))
  332. def run_ap_wpa2_delayed_group_m1_retransmission(dev, apdev):
  333. params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
  334. hapd = hostapd.add_ap(apdev[0], params)
  335. Wlantest.setup(hapd)
  336. wt = Wlantest()
  337. wt.flush()
  338. wt.add_passphrase("12345678")
  339. phy = dev[0].get_driver_status_field("phyname")
  340. dev[0].connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
  341. for i in range(5):
  342. hwsim_utils.test_connectivity(dev[0], hapd)
  343. time.sleep(0.1)
  344. before = get_rx_spec(phy, gtk=True).splitlines()
  345. addr = dev[0].own_addr()
  346. if "OK" not in hapd.request("RESEND_GROUP_M1 " + addr):
  347. raise Exception("RESEND_GROUP_M1 failed")
  348. time.sleep(0.1)
  349. after = get_rx_spec(phy, gtk=True).splitlines()
  350. if "OK" not in hapd.request("RESET_PN " + addr):
  351. raise Exception("RESET_PN failed")
  352. time.sleep(0.1)
  353. hwsim_utils.test_connectivity(dev[0], hapd, timeout=1,
  354. success_expected=False)
  355. dev[0].request("DISCONNECT")
  356. dev[0].wait_disconnected()
  357. for i in range(len(before)):
  358. b = int(before[i], 16)
  359. a = int(after[i], 16)
  360. if a < b:
  361. raise Exception("RX counter decreased: idx=%d before=%d after=%d" % (i, b, a))