test_ap_ciphers.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Cipher suite tests
  2. # Copyright (c) 2013, 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
  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['ifname'], 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['ifname'], 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. check_cipher(dev[0], apdev[0], "TKIP")
  60. def test_ap_cipher_tkip_countermeasures_ap(dev, apdev):
  61. """WPA-PSK/TKIP countermeasures (detected by AP)"""
  62. testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (dev[0].get_driver_status_field("phyname"), dev[0].ifname)
  63. if not os.path.exists(testfile):
  64. raise HwsimSkip("tkip_mic_test not supported in mac80211")
  65. params = { "ssid": "tkip-countermeasures",
  66. "wpa_passphrase": "12345678",
  67. "wpa": "1",
  68. "wpa_key_mgmt": "WPA-PSK",
  69. "wpa_pairwise": "TKIP" }
  70. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  71. dev[0].connect("tkip-countermeasures", psk="12345678",
  72. pairwise="TKIP", group="TKIP", scan_freq="2412")
  73. dev[0].dump_monitor()
  74. with open(testfile, "w") as f:
  75. f.write(apdev[0]['bssid'])
  76. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
  77. if ev is not None:
  78. raise Exception("Unexpected disconnection on first Michael MIC failure")
  79. with open(testfile, "w") as f:
  80. f.write("ff:ff:ff:ff:ff:ff")
  81. ev = dev[0].wait_disconnected(timeout=10,
  82. error="No disconnection after two Michael MIC failures")
  83. if "reason=14" not in ev:
  84. raise Exception("Unexpected disconnection reason: " + ev)
  85. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  86. if ev is not None:
  87. raise Exception("Unexpected connection during TKIP countermeasures")
  88. def test_ap_cipher_tkip_countermeasures_sta(dev, apdev):
  89. """WPA-PSK/TKIP countermeasures (detected by STA)"""
  90. params = { "ssid": "tkip-countermeasures",
  91. "wpa_passphrase": "12345678",
  92. "wpa": "1",
  93. "wpa_key_mgmt": "WPA-PSK",
  94. "wpa_pairwise": "TKIP" }
  95. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  96. testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (hapd.get_driver_status_field("phyname"), apdev[0]['ifname'])
  97. if not os.path.exists(testfile):
  98. raise HwsimSkip("tkip_mic_test not supported in mac80211")
  99. dev[0].connect("tkip-countermeasures", psk="12345678",
  100. pairwise="TKIP", group="TKIP", scan_freq="2412")
  101. dev[0].dump_monitor()
  102. with open(testfile, "w") as f:
  103. f.write(dev[0].p2p_dev_addr())
  104. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
  105. if ev is not None:
  106. raise Exception("Unexpected disconnection on first Michael MIC failure")
  107. with open(testfile, "w") as f:
  108. f.write("ff:ff:ff:ff:ff:ff")
  109. ev = dev[0].wait_disconnected(timeout=10,
  110. error="No disconnection after two Michael MIC failures")
  111. if "reason=14 locally_generated=1" not in ev:
  112. raise Exception("Unexpected disconnection reason: " + ev)
  113. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  114. if ev is not None:
  115. raise Exception("Unexpected connection during TKIP countermeasures")
  116. def test_ap_cipher_ccmp(dev, apdev):
  117. """WPA2-PSK/CCMP connection"""
  118. check_cipher(dev[0], apdev[0], "CCMP")
  119. def test_ap_cipher_gcmp(dev, apdev):
  120. """WPA2-PSK/GCMP connection"""
  121. check_cipher(dev[0], apdev[0], "GCMP")
  122. def test_ap_cipher_ccmp_256(dev, apdev):
  123. """WPA2-PSK/CCMP-256 connection"""
  124. check_cipher(dev[0], apdev[0], "CCMP-256")
  125. def test_ap_cipher_gcmp_256(dev, apdev):
  126. """WPA2-PSK/GCMP-256 connection"""
  127. check_cipher(dev[0], apdev[0], "GCMP-256")
  128. def test_ap_cipher_mixed_wpa_wpa2(dev, apdev):
  129. """WPA2-PSK/CCMP/ and WPA-PSK/TKIP mixed configuration"""
  130. ssid = "test-wpa-wpa2-psk"
  131. passphrase = "12345678"
  132. params = { "ssid": ssid,
  133. "wpa_passphrase": passphrase,
  134. "wpa": "3",
  135. "wpa_key_mgmt": "WPA-PSK",
  136. "rsn_pairwise": "CCMP",
  137. "wpa_pairwise": "TKIP" }
  138. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  139. dev[0].connect(ssid, psk=passphrase, proto="WPA2",
  140. pairwise="CCMP", group="TKIP", scan_freq="2412")
  141. status = dev[0].get_status()
  142. if status['key_mgmt'] != 'WPA2-PSK':
  143. raise Exception("Incorrect key_mgmt reported")
  144. if status['pairwise_cipher'] != 'CCMP':
  145. raise Exception("Incorrect pairwise_cipher reported")
  146. if status['group_cipher'] != 'TKIP':
  147. raise Exception("Incorrect group_cipher reported")
  148. bss = dev[0].get_bss(apdev[0]['bssid'])
  149. if bss['ssid'] != ssid:
  150. raise Exception("Unexpected SSID in the BSS entry")
  151. if "[WPA-PSK-TKIP]" not in bss['flags']:
  152. raise Exception("Missing BSS flag WPA-PSK-TKIP")
  153. if "[WPA2-PSK-CCMP]" not in bss['flags']:
  154. raise Exception("Missing BSS flag WPA2-PSK-CCMP")
  155. hwsim_utils.test_connectivity(dev[0], hapd)
  156. dev[1].connect(ssid, psk=passphrase, proto="WPA",
  157. pairwise="TKIP", group="TKIP", scan_freq="2412")
  158. status = dev[1].get_status()
  159. if status['key_mgmt'] != 'WPA-PSK':
  160. raise Exception("Incorrect key_mgmt reported")
  161. if status['pairwise_cipher'] != 'TKIP':
  162. raise Exception("Incorrect pairwise_cipher reported")
  163. if status['group_cipher'] != 'TKIP':
  164. raise Exception("Incorrect group_cipher reported")
  165. hwsim_utils.test_connectivity(dev[1], hapd)
  166. hwsim_utils.test_connectivity(dev[0], dev[1])
  167. def test_ap_cipher_bip(dev, apdev):
  168. """WPA2-PSK with BIP"""
  169. check_group_mgmt_cipher(dev[0], apdev[0], "AES-128-CMAC")
  170. def test_ap_cipher_bip_gmac_128(dev, apdev):
  171. """WPA2-PSK with BIP-GMAC-128"""
  172. check_group_mgmt_cipher(dev[0], apdev[0], "BIP-GMAC-128")
  173. def test_ap_cipher_bip_gmac_256(dev, apdev):
  174. """WPA2-PSK with BIP-GMAC-256"""
  175. check_group_mgmt_cipher(dev[0], apdev[0], "BIP-GMAC-256")
  176. def test_ap_cipher_bip_cmac_256(dev, apdev):
  177. """WPA2-PSK with BIP-CMAC-256"""
  178. check_group_mgmt_cipher(dev[0], apdev[0], "BIP-CMAC-256")