test_ap_ciphers.py 6.1 KB

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