test_ap_ciphers.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 subprocess
  8. import logging
  9. logger = logging.getLogger()
  10. import os.path
  11. import hwsim_utils
  12. import hostapd
  13. def check_cipher(dev, ap, cipher):
  14. if cipher not in dev.get_capability("pairwise"):
  15. return "skip"
  16. params = { "ssid": "test-wpa2-psk",
  17. "wpa_passphrase": "12345678",
  18. "wpa": "2",
  19. "wpa_key_mgmt": "WPA-PSK",
  20. "rsn_pairwise": cipher }
  21. hostapd.add_ap(ap['ifname'], params)
  22. dev.connect("test-wpa2-psk", psk="12345678",
  23. pairwise=cipher, group=cipher, scan_freq="2412")
  24. hwsim_utils.test_connectivity(dev.ifname, ap['ifname'])
  25. def test_ap_cipher_tkip(dev, apdev):
  26. """WPA2-PSK/TKIP connection"""
  27. return check_cipher(dev[0], apdev[0], "TKIP")
  28. def test_ap_cipher_tkip_countermeasures_ap(dev, apdev):
  29. """WPA-PSK/TKIP countermeasures (detected by AP)"""
  30. testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (dev[0].get_driver_status_field("phyname"), dev[0].ifname)
  31. if not os.path.exists(testfile):
  32. return "skip"
  33. params = { "ssid": "tkip-countermeasures",
  34. "wpa_passphrase": "12345678",
  35. "wpa": "1",
  36. "wpa_key_mgmt": "WPA-PSK",
  37. "wpa_pairwise": "TKIP" }
  38. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  39. dev[0].connect("tkip-countermeasures", psk="12345678",
  40. pairwise="TKIP", group="TKIP", scan_freq="2412")
  41. dev[0].dump_monitor()
  42. cmd = subprocess.Popen(["sudo", "tee", testfile],
  43. stdin=subprocess.PIPE)
  44. cmd.stdin.write(apdev[0]['bssid'])
  45. cmd.stdin.close()
  46. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
  47. if ev is not None:
  48. raise Exception("Unexpected disconnection on first Michael MIC failure")
  49. cmd = subprocess.Popen(["sudo", "tee", testfile],
  50. stdin=subprocess.PIPE)
  51. cmd.stdin.write("ff:ff:ff:ff:ff:ff")
  52. cmd.stdin.close()
  53. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=10)
  54. if ev is None:
  55. raise Exception("No disconnection after two Michael MIC failures")
  56. if "reason=14" not in ev:
  57. raise Exception("Unexpected disconnection reason: " + ev)
  58. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  59. if ev is not None:
  60. raise Exception("Unexpected connection during TKIP countermeasures")
  61. def test_ap_cipher_tkip_countermeasures_sta(dev, apdev):
  62. """WPA-PSK/TKIP countermeasures (detected by STA)"""
  63. params = { "ssid": "tkip-countermeasures",
  64. "wpa_passphrase": "12345678",
  65. "wpa": "1",
  66. "wpa_key_mgmt": "WPA-PSK",
  67. "wpa_pairwise": "TKIP" }
  68. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  69. testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (hapd.get_driver_status_field("phyname"), apdev[0]['ifname'])
  70. if not os.path.exists(testfile):
  71. return "skip"
  72. dev[0].connect("tkip-countermeasures", psk="12345678",
  73. pairwise="TKIP", group="TKIP", scan_freq="2412")
  74. dev[0].dump_monitor()
  75. cmd = subprocess.Popen(["sudo", "tee", testfile],
  76. stdin=subprocess.PIPE)
  77. cmd.stdin.write(dev[0].p2p_dev_addr())
  78. cmd.stdin.close()
  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. cmd = subprocess.Popen(["sudo", "tee", testfile],
  83. stdin=subprocess.PIPE)
  84. cmd.stdin.write("ff:ff:ff:ff:ff:ff")
  85. cmd.stdin.close()
  86. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=10)
  87. if ev is None:
  88. raise Exception("No disconnection after two Michael MIC failures")
  89. if "reason=14 locally_generated=1" not in ev:
  90. raise Exception("Unexpected disconnection reason: " + ev)
  91. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  92. if ev is not None:
  93. raise Exception("Unexpected connection during TKIP countermeasures")
  94. def test_ap_cipher_ccmp(dev, apdev):
  95. """WPA2-PSK/CCMP connection"""
  96. return check_cipher(dev[0], apdev[0], "CCMP")
  97. def test_ap_cipher_gcmp(dev, apdev):
  98. """WPA2-PSK/GCMP connection"""
  99. return check_cipher(dev[0], apdev[0], "GCMP")
  100. def test_ap_cipher_ccmp_256(dev, apdev):
  101. """WPA2-PSK/CCMP-256 connection"""
  102. return check_cipher(dev[0], apdev[0], "CCMP-256")
  103. def test_ap_cipher_gcmp_256(dev, apdev):
  104. """WPA2-PSK/GCMP-256 connection"""
  105. return check_cipher(dev[0], apdev[0], "GCMP-256")
  106. def test_ap_cipher_mixed_wpa_wpa2(dev, apdev):
  107. """WPA2-PSK/CCMP/ and WPA-PSK/TKIP mixed configuration"""
  108. ssid = "test-wpa-wpa2-psk"
  109. passphrase = "12345678"
  110. params = { "ssid": ssid,
  111. "wpa_passphrase": passphrase,
  112. "wpa": "3",
  113. "wpa_key_mgmt": "WPA-PSK",
  114. "rsn_pairwise": "CCMP",
  115. "wpa_pairwise": "TKIP" }
  116. hostapd.add_ap(apdev[0]['ifname'], params)
  117. dev[0].connect(ssid, psk=passphrase, proto="WPA2",
  118. pairwise="CCMP", group="TKIP", scan_freq="2412")
  119. status = dev[0].get_status()
  120. if status['key_mgmt'] != 'WPA2-PSK':
  121. raise Exception("Incorrect key_mgmt reported")
  122. if status['pairwise_cipher'] != 'CCMP':
  123. raise Exception("Incorrect pairwise_cipher reported")
  124. if status['group_cipher'] != 'TKIP':
  125. raise Exception("Incorrect group_cipher reported")
  126. bss = dev[0].get_bss(apdev[0]['bssid'])
  127. if bss['ssid'] != ssid:
  128. raise Exception("Unexpected SSID in the BSS entry")
  129. if "[WPA-PSK-TKIP]" not in bss['flags']:
  130. raise Exception("Missing BSS flag WPA-PSK-TKIP")
  131. if "[WPA2-PSK-CCMP]" not in bss['flags']:
  132. raise Exception("Missing BSS flag WPA2-PSK-CCMP")
  133. hwsim_utils.test_connectivity(dev[0].ifname, apdev[0]['ifname'])
  134. dev[1].connect(ssid, psk=passphrase, proto="WPA",
  135. pairwise="TKIP", group="TKIP", scan_freq="2412")
  136. status = dev[1].get_status()
  137. if status['key_mgmt'] != 'WPA-PSK':
  138. raise Exception("Incorrect key_mgmt reported")
  139. if status['pairwise_cipher'] != 'TKIP':
  140. raise Exception("Incorrect pairwise_cipher reported")
  141. if status['group_cipher'] != 'TKIP':
  142. raise Exception("Incorrect group_cipher reported")
  143. hwsim_utils.test_connectivity(dev[1].ifname, apdev[0]['ifname'])
  144. hwsim_utils.test_connectivity(dev[0].ifname, dev[1].ifname)