test_ap_wps.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/usr/bin/python
  2. #
  3. # WPS tests
  4. # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
  5. #
  6. # This software may be distributed under the terms of the BSD license.
  7. # See README for more details.
  8. import time
  9. import subprocess
  10. import logging
  11. logger = logging.getLogger(__name__)
  12. import hwsim_utils
  13. import hostapd
  14. def test_ap_wps_init(dev, apdev):
  15. """Initial AP configuration with first WPS Enrollee"""
  16. ssid = "test-wps"
  17. hostapd.add_ap(apdev[0]['ifname'],
  18. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  19. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  20. logger.info("WPS provisioning step")
  21. hapd.request("WPS_PBC")
  22. dev[0].request("BSS_FLUSH 0")
  23. dev[0].request("SET ignore_old_scan_res 1")
  24. dev[0].dump_monitor()
  25. dev[0].request("WPS_PBC")
  26. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  27. if ev is None:
  28. raise Exception("Association with the AP timed out")
  29. status = dev[0].get_status()
  30. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  31. raise Exception("Not fully connected")
  32. if status['ssid'] != ssid:
  33. raise Exception("Unexpected SSID")
  34. if status['pairwise_cipher'] != 'CCMP':
  35. raise Exception("Unexpected encryption configuration")
  36. if status['key_mgmt'] != 'WPA2-PSK':
  37. raise Exception("Unexpected key_mgmt")
  38. def test_ap_wps_conf(dev, apdev):
  39. """WPS PBC provisioning with configured AP"""
  40. ssid = "test-wps-conf"
  41. hostapd.add_ap(apdev[0]['ifname'],
  42. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  43. "wpa_passphrase": "12345678", "wpa": "2",
  44. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  45. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  46. logger.info("WPS provisioning step")
  47. hapd.request("WPS_PBC")
  48. dev[0].dump_monitor()
  49. dev[0].request("WPS_PBC")
  50. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  51. if ev is None:
  52. raise Exception("Association with the AP timed out")
  53. status = dev[0].get_status()
  54. if status['wpa_state'] != 'COMPLETED':
  55. raise Exception("Not fully connected")
  56. if status['bssid'] != apdev[0]['bssid']:
  57. raise Exception("Unexpected BSSID")
  58. if status['ssid'] != ssid:
  59. raise Exception("Unexpected SSID")
  60. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  61. raise Exception("Unexpected encryption configuration")
  62. if status['key_mgmt'] != 'WPA2-PSK':
  63. raise Exception("Unexpected key_mgmt")
  64. def test_ap_wps_conf_pin(dev, apdev):
  65. """WPS PIN provisioning with configured AP"""
  66. ssid = "test-wps-conf-pin"
  67. hostapd.add_ap(apdev[0]['ifname'],
  68. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  69. "wpa_passphrase": "12345678", "wpa": "2",
  70. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  71. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  72. logger.info("WPS provisioning step")
  73. pin = dev[0].wps_read_pin()
  74. hapd.request("WPS_PIN any " + pin)
  75. dev[0].request("BSS_FLUSH 0")
  76. dev[0].request("SET ignore_old_scan_res 1")
  77. dev[0].dump_monitor()
  78. dev[0].request("WPS_PIN any " + pin)
  79. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  80. if ev is None:
  81. raise Exception("Association with the AP timed out")
  82. status = dev[0].get_status()
  83. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  84. raise Exception("Not fully connected")
  85. if status['ssid'] != ssid:
  86. raise Exception("Unexpected SSID")
  87. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  88. raise Exception("Unexpected encryption configuration")
  89. if status['key_mgmt'] != 'WPA2-PSK':
  90. raise Exception("Unexpected key_mgmt")
  91. def test_ap_wps_reg_connect(dev, apdev):
  92. """WPS registrar using AP PIN to connect"""
  93. ssid = "test-wps-reg-ap-pin"
  94. appin = "12345670"
  95. hostapd.add_ap(apdev[0]['ifname'],
  96. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  97. "wpa_passphrase": "12345678", "wpa": "2",
  98. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  99. "ap_pin": appin})
  100. logger.info("WPS provisioning step")
  101. dev[0].request("BSS_FLUSH 0")
  102. dev[0].request("SET ignore_old_scan_res 1")
  103. dev[0].dump_monitor()
  104. dev[0].request("WPS_REG " + apdev[0]['bssid'] + " " + appin)
  105. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  106. if ev is None:
  107. raise Exception("Association with the AP timed out")
  108. status = dev[0].get_status()
  109. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  110. raise Exception("Not fully connected")
  111. if status['ssid'] != ssid:
  112. raise Exception("Unexpected SSID")
  113. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  114. raise Exception("Unexpected encryption configuration")
  115. if status['key_mgmt'] != 'WPA2-PSK':
  116. raise Exception("Unexpected key_mgmt")
  117. def test_ap_wps_reg_config(dev, apdev):
  118. """WPS registrar configuring and AP using AP PIN"""
  119. ssid = "test-wps-init-ap-pin"
  120. appin = "12345670"
  121. hostapd.add_ap(apdev[0]['ifname'],
  122. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  123. "ap_pin": appin})
  124. logger.info("WPS configuration step")
  125. dev[0].request("BSS_FLUSH 0")
  126. dev[0].request("SET ignore_old_scan_res 1")
  127. dev[0].dump_monitor()
  128. new_ssid = "wps-new-ssid"
  129. new_passphrase = "1234567890"
  130. dev[0].request("WPS_REG " + apdev[0]['bssid'] + " " + appin + " " + new_ssid.encode("hex") + " WPA2PSK CCMP " + new_passphrase.encode("hex"))
  131. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  132. if ev is None:
  133. raise Exception("Association with the AP timed out")
  134. status = dev[0].get_status()
  135. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  136. raise Exception("Not fully connected")
  137. if status['ssid'] != new_ssid:
  138. raise Exception("Unexpected SSID")
  139. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  140. raise Exception("Unexpected encryption configuration")
  141. if status['key_mgmt'] != 'WPA2-PSK':
  142. raise Exception("Unexpected key_mgmt")
  143. def test_ap_wps_pbc_overlap_2ap(dev, apdev):
  144. """WPS PBC session overlap with two active APs"""
  145. hostapd.add_ap(apdev[0]['ifname'],
  146. { "ssid": "wps1", "eap_server": "1", "wps_state": "2",
  147. "wpa_passphrase": "12345678", "wpa": "2",
  148. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  149. "wps_independent": "1"})
  150. hostapd.add_ap(apdev[1]['ifname'],
  151. { "ssid": "wps2", "eap_server": "1", "wps_state": "2",
  152. "wpa_passphrase": "123456789", "wpa": "2",
  153. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  154. "wps_independent": "1"})
  155. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  156. hapd.request("WPS_PBC")
  157. hapd2 = hostapd.Hostapd(apdev[1]['ifname'])
  158. hapd2.request("WPS_PBC")
  159. logger.info("WPS provisioning step")
  160. dev[0].dump_monitor()
  161. dev[0].request("WPS_PBC")
  162. ev = dev[0].wait_event(["WPS-OVERLAP-DETECTED"], timeout=15)
  163. if ev is None:
  164. raise Exception("PBC session overlap not detected")
  165. def test_ap_wps_pbc_overlap_2sta(dev, apdev):
  166. """WPS PBC session overlap with two active STAs"""
  167. ssid = "test-wps-pbc-overlap"
  168. hostapd.add_ap(apdev[0]['ifname'],
  169. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  170. "wpa_passphrase": "12345678", "wpa": "2",
  171. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  172. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  173. logger.info("WPS provisioning step")
  174. hapd.request("WPS_PBC")
  175. dev[0].request("SET ignore_old_scan_res 1")
  176. dev[0].request("BSS_FLUSH 0")
  177. dev[1].request("SET ignore_old_scan_res 1")
  178. dev[1].request("BSS_FLUSH 0")
  179. dev[0].dump_monitor()
  180. dev[1].dump_monitor()
  181. dev[0].request("WPS_PBC")
  182. dev[1].request("WPS_PBC")
  183. ev = dev[0].wait_event(["WPS-M2D"], timeout=15)
  184. if ev is None:
  185. raise Exception("PBC session overlap not detected (dev0)")
  186. if "config_error=12" not in ev:
  187. raise Exception("PBC session overlap not correctly reported (dev0)")
  188. ev = dev[1].wait_event(["WPS-M2D"], timeout=15)
  189. if ev is None:
  190. raise Exception("PBC session overlap not detected (dev1)")
  191. if "config_error=12" not in ev:
  192. raise Exception("PBC session overlap not correctly reported (dev1)")