test_ap_ciphers.py 8.1 KB

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