test_ap_wps.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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("SET ignore_old_scan_res 1")
  23. dev[0].dump_monitor()
  24. dev[0].request("WPS_PBC")
  25. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  26. if ev is None:
  27. raise Exception("Association with the AP timed out")
  28. status = dev[0].get_status()
  29. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  30. raise Exception("Not fully connected")
  31. if status['ssid'] != ssid:
  32. raise Exception("Unexpected SSID")
  33. if status['pairwise_cipher'] != 'CCMP':
  34. raise Exception("Unexpected encryption configuration")
  35. if status['key_mgmt'] != 'WPA2-PSK':
  36. raise Exception("Unexpected key_mgmt")
  37. def test_ap_wps_conf(dev, apdev):
  38. """WPS PBC provisioning with configured AP"""
  39. ssid = "test-wps-conf"
  40. hostapd.add_ap(apdev[0]['ifname'],
  41. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  42. "wpa_passphrase": "12345678", "wpa": "2",
  43. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  44. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  45. logger.info("WPS provisioning step")
  46. hapd.request("WPS_PBC")
  47. dev[0].dump_monitor()
  48. dev[0].request("WPS_PBC")
  49. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  50. if ev is None:
  51. raise Exception("Association with the AP timed out")
  52. status = dev[0].get_status()
  53. if status['wpa_state'] != 'COMPLETED':
  54. raise Exception("Not fully connected")
  55. if status['bssid'] != apdev[0]['bssid']:
  56. raise Exception("Unexpected BSSID")
  57. if status['ssid'] != ssid:
  58. raise Exception("Unexpected SSID")
  59. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  60. raise Exception("Unexpected encryption configuration")
  61. if status['key_mgmt'] != 'WPA2-PSK':
  62. raise Exception("Unexpected key_mgmt")
  63. def test_ap_wps_conf_pin(dev, apdev):
  64. """WPS PIN provisioning with configured AP"""
  65. ssid = "test-wps-conf-pin"
  66. hostapd.add_ap(apdev[0]['ifname'],
  67. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  68. "wpa_passphrase": "12345678", "wpa": "2",
  69. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  70. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  71. logger.info("WPS provisioning step")
  72. pin = dev[0].wps_read_pin()
  73. hapd.request("WPS_PIN any " + pin)
  74. dev[0].request("SET ignore_old_scan_res 1")
  75. dev[0].dump_monitor()
  76. dev[0].request("WPS_PIN any " + pin)
  77. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  78. if ev is None:
  79. raise Exception("Association with the AP timed out")
  80. status = dev[0].get_status()
  81. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  82. raise Exception("Not fully connected")
  83. if status['ssid'] != ssid:
  84. raise Exception("Unexpected SSID")
  85. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  86. raise Exception("Unexpected encryption configuration")
  87. if status['key_mgmt'] != 'WPA2-PSK':
  88. raise Exception("Unexpected key_mgmt")
  89. def test_ap_wps_reg_connect(dev, apdev):
  90. """WPS registrar using AP PIN to connect"""
  91. ssid = "test-wps-reg-ap-pin"
  92. appin = "12345670"
  93. hostapd.add_ap(apdev[0]['ifname'],
  94. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  95. "wpa_passphrase": "12345678", "wpa": "2",
  96. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  97. "ap_pin": appin})
  98. logger.info("WPS provisioning step")
  99. dev[0].request("SET ignore_old_scan_res 1")
  100. dev[0].dump_monitor()
  101. dev[0].wps_reg(apdev[0]['bssid'], appin)
  102. status = dev[0].get_status()
  103. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  104. raise Exception("Not fully connected")
  105. if status['ssid'] != ssid:
  106. raise Exception("Unexpected SSID")
  107. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  108. raise Exception("Unexpected encryption configuration")
  109. if status['key_mgmt'] != 'WPA2-PSK':
  110. raise Exception("Unexpected key_mgmt")
  111. def test_ap_wps_reg_config(dev, apdev):
  112. """WPS registrar configuring and AP using AP PIN"""
  113. ssid = "test-wps-init-ap-pin"
  114. appin = "12345670"
  115. hostapd.add_ap(apdev[0]['ifname'],
  116. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  117. "ap_pin": appin})
  118. logger.info("WPS configuration step")
  119. dev[0].request("SET ignore_old_scan_res 1")
  120. dev[0].dump_monitor()
  121. new_ssid = "wps-new-ssid"
  122. new_passphrase = "1234567890"
  123. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPA2PSK", "CCMP",
  124. new_passphrase)
  125. status = dev[0].get_status()
  126. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  127. raise Exception("Not fully connected")
  128. if status['ssid'] != new_ssid:
  129. raise Exception("Unexpected SSID")
  130. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  131. raise Exception("Unexpected encryption configuration")
  132. if status['key_mgmt'] != 'WPA2-PSK':
  133. raise Exception("Unexpected key_mgmt")
  134. def test_ap_wps_pbc_overlap_2ap(dev, apdev):
  135. """WPS PBC session overlap with two active APs"""
  136. hostapd.add_ap(apdev[0]['ifname'],
  137. { "ssid": "wps1", "eap_server": "1", "wps_state": "2",
  138. "wpa_passphrase": "12345678", "wpa": "2",
  139. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  140. "wps_independent": "1"})
  141. hostapd.add_ap(apdev[1]['ifname'],
  142. { "ssid": "wps2", "eap_server": "1", "wps_state": "2",
  143. "wpa_passphrase": "123456789", "wpa": "2",
  144. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  145. "wps_independent": "1"})
  146. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  147. hapd.request("WPS_PBC")
  148. hapd2 = hostapd.Hostapd(apdev[1]['ifname'])
  149. hapd2.request("WPS_PBC")
  150. logger.info("WPS provisioning step")
  151. dev[0].dump_monitor()
  152. dev[0].request("WPS_PBC")
  153. ev = dev[0].wait_event(["WPS-OVERLAP-DETECTED"], timeout=15)
  154. if ev is None:
  155. raise Exception("PBC session overlap not detected")
  156. def test_ap_wps_pbc_overlap_2sta(dev, apdev):
  157. """WPS PBC session overlap with two active STAs"""
  158. ssid = "test-wps-pbc-overlap"
  159. hostapd.add_ap(apdev[0]['ifname'],
  160. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  161. "wpa_passphrase": "12345678", "wpa": "2",
  162. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  163. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  164. logger.info("WPS provisioning step")
  165. hapd.request("WPS_PBC")
  166. dev[0].request("SET ignore_old_scan_res 1")
  167. dev[1].request("SET ignore_old_scan_res 1")
  168. dev[0].dump_monitor()
  169. dev[1].dump_monitor()
  170. dev[0].request("WPS_PBC")
  171. dev[1].request("WPS_PBC")
  172. ev = dev[0].wait_event(["WPS-M2D"], timeout=15)
  173. if ev is None:
  174. raise Exception("PBC session overlap not detected (dev0)")
  175. if "config_error=12" not in ev:
  176. raise Exception("PBC session overlap not correctly reported (dev0)")
  177. ev = dev[1].wait_event(["WPS-M2D"], timeout=15)
  178. if ev is None:
  179. raise Exception("PBC session overlap not detected (dev1)")
  180. if "config_error=12" not in ev:
  181. raise Exception("PBC session overlap not correctly reported (dev1)")
  182. def test_ap_wps_er_add_enrollee(dev, apdev):
  183. """WPS ER configuring AP and adding a new enrollee using PIN"""
  184. ssid = "wps-er-add-enrollee"
  185. ap_pin = "12345670"
  186. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  187. hostapd.add_ap(apdev[0]['ifname'],
  188. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  189. "device_name": "Wireless AP", "manufacturer": "Company",
  190. "model_name": "WAP", "model_number": "123",
  191. "serial_number": "12345", "device_type": "6-0050F204-1",
  192. "os_version": "01020300",
  193. "config_methods": "label push_button",
  194. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  195. logger.info("WPS configuration step")
  196. new_passphrase = "1234567890"
  197. dev[0].dump_monitor()
  198. dev[0].request("SET ignore_old_scan_res 1")
  199. dev[0].wps_reg(apdev[0]['bssid'], ap_pin, ssid, "WPA2PSK", "CCMP",
  200. new_passphrase)
  201. status = dev[0].get_status()
  202. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  203. raise Exception("Not fully connected")
  204. if status['ssid'] != ssid:
  205. raise Exception("Unexpected SSID")
  206. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  207. raise Exception("Unexpected encryption configuration")
  208. if status['key_mgmt'] != 'WPA2-PSK':
  209. raise Exception("Unexpected key_mgmt")
  210. logger.info("Start ER")
  211. dev[0].request("WPS_ER_START ifname=lo")
  212. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  213. if ev is None:
  214. raise Exception("AP discovery timed out")
  215. if ap_uuid not in ev:
  216. raise Exception("Expected AP UUID not found")
  217. logger.info("Learn AP configuration through UPnP")
  218. dev[0].dump_monitor()
  219. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  220. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  221. if ev is None:
  222. raise Exception("AP learn timed out")
  223. if ap_uuid not in ev:
  224. raise Exception("Expected AP UUID not in settings")
  225. if "ssid=" + ssid not in ev:
  226. raise Exception("Expected SSID not in settings")
  227. if "key=" + new_passphrase not in ev:
  228. raise Exception("Expected passphrase not in settings")
  229. logger.info("Add Enrollee using ER")
  230. pin = dev[1].wps_read_pin()
  231. dev[0].dump_monitor()
  232. dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
  233. dev[1].request("SET ignore_old_scan_res 1")
  234. dev[1].dump_monitor()
  235. dev[1].request("WPS_PIN any " + pin)
  236. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=30)
  237. if ev is None:
  238. raise Exception("Enrollee did not report success")
  239. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  240. if ev is None:
  241. raise Exception("Association with the AP timed out")
  242. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  243. if ev is None:
  244. raise Exception("WPS ER did not report success")
  245. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  246. def test_ap_wps_er_add_enrollee_pbc(dev, apdev):
  247. """WPS ER connected to AP and adding a new enrollee using PBC"""
  248. ssid = "wps-er-add-enrollee-pbc"
  249. ap_pin = "12345670"
  250. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  251. hostapd.add_ap(apdev[0]['ifname'],
  252. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  253. "wpa_passphrase": "12345678", "wpa": "2",
  254. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  255. "device_name": "Wireless AP", "manufacturer": "Company",
  256. "model_name": "WAP", "model_number": "123",
  257. "serial_number": "12345", "device_type": "6-0050F204-1",
  258. "os_version": "01020300",
  259. "config_methods": "label push_button",
  260. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  261. logger.info("Learn AP configuration")
  262. dev[0].dump_monitor()
  263. dev[0].request("SET ignore_old_scan_res 1")
  264. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  265. status = dev[0].get_status()
  266. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  267. raise Exception("Not fully connected")
  268. logger.info("Start ER")
  269. dev[0].request("WPS_ER_START ifname=lo")
  270. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  271. if ev is None:
  272. raise Exception("AP discovery timed out")
  273. if ap_uuid not in ev:
  274. raise Exception("Expected AP UUID not found")
  275. logger.info("Use learned network configuration on ER")
  276. dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  277. logger.info("Add Enrollee using ER and PBC")
  278. dev[0].dump_monitor()
  279. enrollee = dev[1].p2p_interface_addr()
  280. dev[1].request("SET ignore_old_scan_res 1")
  281. dev[1].dump_monitor()
  282. dev[1].request("WPS_PBC")
  283. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=15)
  284. if ev is None:
  285. raise Exception("Enrollee discovery timed out")
  286. if enrollee not in ev:
  287. raise Exception("Expected Enrollee not found")
  288. dev[0].request("WPS_ER_PBC " + enrollee)
  289. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=15)
  290. if ev is None:
  291. raise Exception("Enrollee did not report success")
  292. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  293. if ev is None:
  294. raise Exception("Association with the AP timed out")
  295. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  296. if ev is None:
  297. raise Exception("WPS ER did not report success")
  298. hwsim_utils.test_connectivity_sta(dev[0], dev[1])