test_ap_ciphers.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. import time
  7. import logging
  8. logger = logging.getLogger()
  9. import os.path
  10. import hwsim_utils
  11. import hostapd
  12. from utils import HwsimSkip, skip_with_fips
  13. from wlantest import Wlantest
  14. def check_cipher(dev, ap, cipher):
  15. if cipher not in dev.get_capability("pairwise"):
  16. raise HwsimSkip("Cipher %s not supported" % cipher)
  17. params = { "ssid": "test-wpa2-psk",
  18. "wpa_passphrase": "12345678",
  19. "wpa": "2",
  20. "wpa_key_mgmt": "WPA-PSK",
  21. "rsn_pairwise": cipher }
  22. hapd = hostapd.add_ap(ap, params)
  23. dev.connect("test-wpa2-psk", psk="12345678",
  24. pairwise=cipher, group=cipher, scan_freq="2412")
  25. hwsim_utils.test_connectivity(dev, hapd)
  26. def check_group_mgmt_cipher(dev, ap, cipher):
  27. if cipher not in dev.get_capability("group_mgmt"):
  28. raise HwsimSkip("Cipher %s not supported" % cipher)
  29. params = { "ssid": "test-wpa2-psk-pmf",
  30. "wpa_passphrase": "12345678",
  31. "wpa": "2",
  32. "ieee80211w": "2",
  33. "wpa_key_mgmt": "WPA-PSK-SHA256",
  34. "rsn_pairwise": "CCMP",
  35. "group_mgmt_cipher": cipher }
  36. hapd = hostapd.add_ap(ap, params)
  37. Wlantest.setup(hapd)
  38. wt = Wlantest()
  39. wt.flush()
  40. wt.add_passphrase("12345678")
  41. dev.connect("test-wpa2-psk-pmf", psk="12345678", ieee80211w="2",
  42. key_mgmt="WPA-PSK-SHA256",
  43. pairwise="CCMP", group="CCMP", scan_freq="2412")
  44. hwsim_utils.test_connectivity(dev, hapd)
  45. hapd.request("DEAUTHENTICATE ff:ff:ff:ff:ff:ff")
  46. dev.wait_disconnected()
  47. if wt.get_bss_counter('valid_bip_mmie', ap['bssid']) < 1:
  48. raise Exception("No valid BIP MMIE seen")
  49. if wt.get_bss_counter('bip_deauth', ap['bssid']) < 1:
  50. raise Exception("No valid BIP deauth seen")
  51. if cipher == "AES-128-CMAC":
  52. group_mgmt = "BIP"
  53. else:
  54. group_mgmt = cipher
  55. res = wt.info_bss('group_mgmt', ap['bssid']).strip()
  56. if res != group_mgmt:
  57. raise Exception("Unexpected group mgmt cipher: " + res)
  58. def test_ap_cipher_tkip(dev, apdev):
  59. """WPA2-PSK/TKIP connection"""
  60. skip_with_fips(dev[0])
  61. check_cipher(dev[0], apdev[0], "TKIP")
  62. def test_ap_cipher_tkip_countermeasures_ap(dev, apdev):
  63. """WPA-PSK/TKIP countermeasures (detected by AP)"""
  64. skip_with_fips(dev[0])
  65. testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (dev[0].get_driver_status_field("phyname"), dev[0].ifname)
  66. if dev[0].cmd_execute([ "ls", testfile ])[0] != 0:
  67. raise HwsimSkip("tkip_mic_test not supported in mac80211")
  68. params = { "ssid": "tkip-countermeasures",
  69. "wpa_passphrase": "12345678",
  70. "wpa": "1",
  71. "wpa_key_mgmt": "WPA-PSK",
  72. "wpa_pairwise": "TKIP" }
  73. hapd = hostapd.add_ap(apdev[0], params)
  74. dev[0].connect("tkip-countermeasures", psk="12345678",
  75. pairwise="TKIP", group="TKIP", scan_freq="2412")
  76. dev[0].dump_monitor()
  77. dev[0].cmd_execute([ "echo", "-n", apdev[0]['bssid'], ">", testfile ],
  78. shell=True)
  79. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
  80. if ev is not None:
  81. raise Exception("Unexpected disconnection on first Michael MIC failure")
  82. dev[0].cmd_execute([ "echo", "-n", "ff:ff:ff:ff:ff:ff", ">", testfile ],
  83. shell=True)
  84. ev = dev[0].wait_disconnected(timeout=10,
  85. error="No disconnection after two Michael MIC failures")
  86. if "reason=14" not in ev:
  87. raise Exception("Unexpected disconnection reason: " + ev)
  88. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  89. if ev is not None:
  90. raise Exception("Unexpected connection during TKIP countermeasures")
  91. def test_ap_cipher_tkip_countermeasures_sta(dev, apdev):
  92. """WPA-PSK/TKIP countermeasures (detected by STA)"""
  93. skip_with_fips(dev[0])
  94. params = { "ssid": "tkip-countermeasures",
  95. "wpa_passphrase": "12345678",
  96. "wpa": "1",
  97. "wpa_key_mgmt": "WPA-PSK",
  98. "wpa_pairwise": "TKIP" }
  99. hapd = hostapd.add_ap(apdev[0], params)
  100. testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (hapd.get_driver_status_field("phyname"), apdev[0]['ifname'])
  101. if hapd.cmd_execute([ "ls", testfile ])[0] != 0:
  102. raise HwsimSkip("tkip_mic_test not supported in mac80211")
  103. dev[0].connect("tkip-countermeasures", psk="12345678",
  104. pairwise="TKIP", group="TKIP", scan_freq="2412")
  105. dev[0].dump_monitor()
  106. hapd.cmd_execute([ "echo", "-n", dev[0].own_addr(), ">", testfile ],
  107. shell=True)
  108. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
  109. if ev is not None:
  110. raise Exception("Unexpected disconnection on first Michael MIC failure")
  111. hapd.cmd_execute([ "echo", "-n", "ff:ff:ff:ff:ff:ff", ">", testfile ],
  112. shell=True)
  113. ev = dev[0].wait_disconnected(timeout=10,
  114. error="No disconnection after two Michael MIC failures")
  115. if "reason=14 locally_generated=1" not in ev:
  116. raise Exception("Unexpected disconnection reason: " + ev)
  117. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  118. if ev is not None:
  119. raise Exception("Unexpected connection during TKIP countermeasures")
  120. def test_ap_cipher_ccmp(dev, apdev):
  121. """WPA2-PSK/CCMP connection"""
  122. check_cipher(dev[0], apdev[0], "CCMP")
  123. def test_ap_cipher_gcmp(dev, apdev):
  124. """WPA2-PSK/GCMP connection"""
  125. check_cipher(dev[0], apdev[0], "GCMP")
  126. def test_ap_cipher_ccmp_256(dev, apdev):
  127. """WPA2-PSK/CCMP-256 connection"""
  128. check_cipher(dev[0], apdev[0], "CCMP-256")
  129. def test_ap_cipher_gcmp_256(dev, apdev):
  130. """WPA2-PSK/GCMP-256 connection"""
  131. check_cipher(dev[0], apdev[0], "GCMP-256")
  132. def test_ap_cipher_mixed_wpa_wpa2(dev, apdev):
  133. """WPA2-PSK/CCMP/ and WPA-PSK/TKIP mixed configuration"""
  134. skip_with_fips(dev[0])
  135. ssid = "test-wpa-wpa2-psk"
  136. passphrase = "12345678"
  137. params = { "ssid": ssid,
  138. "wpa_passphrase": passphrase,
  139. "wpa": "3",
  140. "wpa_key_mgmt": "WPA-PSK",
  141. "rsn_pairwise": "CCMP",
  142. "wpa_pairwise": "TKIP" }
  143. hapd = hostapd.add_ap(apdev[0], params)
  144. dev[0].connect(ssid, psk=passphrase, proto="WPA2",
  145. pairwise="CCMP", group="TKIP", scan_freq="2412")
  146. status = dev[0].get_status()
  147. if status['key_mgmt'] != 'WPA2-PSK':
  148. raise Exception("Incorrect key_mgmt reported")
  149. if status['pairwise_cipher'] != 'CCMP':
  150. raise Exception("Incorrect pairwise_cipher reported")
  151. if status['group_cipher'] != 'TKIP':
  152. raise Exception("Incorrect group_cipher reported")
  153. bss = dev[0].get_bss(apdev[0]['bssid'])
  154. if bss['ssid'] != ssid:
  155. raise Exception("Unexpected SSID in the BSS entry")
  156. if "[WPA-PSK-TKIP]" not in bss['flags']:
  157. raise Exception("Missing BSS flag WPA-PSK-TKIP")
  158. if "[WPA2-PSK-CCMP]" not in bss['flags']:
  159. raise Exception("Missing BSS flag WPA2-PSK-CCMP")
  160. hwsim_utils.test_connectivity(dev[0], hapd)
  161. dev[1].connect(ssid, psk=passphrase, proto="WPA",
  162. pairwise="TKIP", group="TKIP", scan_freq="2412")
  163. status = dev[1].get_status()
  164. if status['key_mgmt'] != 'WPA-PSK':
  165. raise Exception("Incorrect key_mgmt reported")
  166. if status['pairwise_cipher'] != 'TKIP':
  167. raise Exception("Incorrect pairwise_cipher reported")
  168. if status['group_cipher'] != 'TKIP':
  169. raise Exception("Incorrect group_cipher reported")
  170. hwsim_utils.test_connectivity(dev[1], hapd)
  171. hwsim_utils.test_connectivity(dev[0], dev[1])
  172. def test_ap_cipher_bip(dev, apdev):
  173. """WPA2-PSK with BIP"""
  174. check_group_mgmt_cipher(dev[0], apdev[0], "AES-128-CMAC")
  175. def test_ap_cipher_bip_gmac_128(dev, apdev):
  176. """WPA2-PSK with BIP-GMAC-128"""
  177. check_group_mgmt_cipher(dev[0], apdev[0], "BIP-GMAC-128")
  178. def test_ap_cipher_bip_gmac_256(dev, apdev):
  179. """WPA2-PSK with BIP-GMAC-256"""
  180. check_group_mgmt_cipher(dev[0], apdev[0], "BIP-GMAC-256")
  181. def test_ap_cipher_bip_cmac_256(dev, apdev):
  182. """WPA2-PSK with BIP-CMAC-256"""
  183. check_group_mgmt_cipher(dev[0], apdev[0], "BIP-CMAC-256")