test_ap_wps.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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()
  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_twice(dev, apdev):
  64. """WPS provisioning with twice to change passphrase"""
  65. ssid = "test-wps-twice"
  66. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  67. "wpa_passphrase": "12345678", "wpa": "2",
  68. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" }
  69. hostapd.add_ap(apdev[0]['ifname'], params)
  70. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  71. logger.info("WPS provisioning step")
  72. hapd.request("WPS_PBC")
  73. dev[0].request("SET ignore_old_scan_res 1")
  74. dev[0].dump_monitor()
  75. dev[0].request("WPS_PBC")
  76. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  77. if ev is None:
  78. raise Exception("Association with the AP timed out")
  79. dev[0].request("DISCONNECT")
  80. logger.info("Restart AP with different passphrase and re-run WPS")
  81. hapd_global = hostapd.HostapdGlobal()
  82. hapd_global.remove(apdev[0]['ifname'])
  83. params['wpa_passphrase'] = 'another passphrase'
  84. hostapd.add_ap(apdev[0]['ifname'], params)
  85. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  86. logger.info("WPS provisioning step")
  87. hapd.request("WPS_PBC")
  88. dev[0].dump_monitor()
  89. dev[0].request("WPS_PBC")
  90. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  91. if ev is None:
  92. raise Exception("Association with the AP timed out")
  93. networks = dev[0].list_networks()
  94. if len(networks) > 1:
  95. raise Exception("Unexpected duplicated network block present")
  96. def test_ap_wps_conf_pin(dev, apdev):
  97. """WPS PIN provisioning with configured AP"""
  98. ssid = "test-wps-conf-pin"
  99. hostapd.add_ap(apdev[0]['ifname'],
  100. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  101. "wpa_passphrase": "12345678", "wpa": "2",
  102. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  103. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  104. logger.info("WPS provisioning step")
  105. pin = dev[0].wps_read_pin()
  106. hapd.request("WPS_PIN any " + pin)
  107. dev[0].request("SET ignore_old_scan_res 1")
  108. dev[0].dump_monitor()
  109. dev[0].request("WPS_PIN any " + pin)
  110. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  111. if ev is None:
  112. raise Exception("Association with the AP timed out")
  113. status = dev[0].get_status()
  114. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  115. raise Exception("Not fully connected")
  116. if status['ssid'] != ssid:
  117. raise Exception("Unexpected SSID")
  118. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  119. raise Exception("Unexpected encryption configuration")
  120. if status['key_mgmt'] != 'WPA2-PSK':
  121. raise Exception("Unexpected key_mgmt")
  122. def test_ap_wps_reg_connect(dev, apdev):
  123. """WPS registrar using AP PIN to connect"""
  124. ssid = "test-wps-reg-ap-pin"
  125. appin = "12345670"
  126. hostapd.add_ap(apdev[0]['ifname'],
  127. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  128. "wpa_passphrase": "12345678", "wpa": "2",
  129. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  130. "ap_pin": appin})
  131. logger.info("WPS provisioning step")
  132. dev[0].request("SET ignore_old_scan_res 1")
  133. dev[0].dump_monitor()
  134. dev[0].wps_reg(apdev[0]['bssid'], appin)
  135. status = dev[0].get_status()
  136. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  137. raise Exception("Not fully connected")
  138. if status['ssid'] != ssid:
  139. raise Exception("Unexpected SSID")
  140. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  141. raise Exception("Unexpected encryption configuration")
  142. if status['key_mgmt'] != 'WPA2-PSK':
  143. raise Exception("Unexpected key_mgmt")
  144. def test_ap_wps_reg_config(dev, apdev):
  145. """WPS registrar configuring and AP using AP PIN"""
  146. ssid = "test-wps-init-ap-pin"
  147. appin = "12345670"
  148. hostapd.add_ap(apdev[0]['ifname'],
  149. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  150. "ap_pin": appin})
  151. logger.info("WPS configuration step")
  152. dev[0].request("SET ignore_old_scan_res 1")
  153. dev[0].dump_monitor()
  154. new_ssid = "wps-new-ssid"
  155. new_passphrase = "1234567890"
  156. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPA2PSK", "CCMP",
  157. new_passphrase)
  158. status = dev[0].get_status()
  159. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  160. raise Exception("Not fully connected")
  161. if status['ssid'] != new_ssid:
  162. raise Exception("Unexpected SSID")
  163. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  164. raise Exception("Unexpected encryption configuration")
  165. if status['key_mgmt'] != 'WPA2-PSK':
  166. raise Exception("Unexpected key_mgmt")
  167. def test_ap_wps_pbc_overlap_2ap(dev, apdev):
  168. """WPS PBC session overlap with two active APs"""
  169. hostapd.add_ap(apdev[0]['ifname'],
  170. { "ssid": "wps1", "eap_server": "1", "wps_state": "2",
  171. "wpa_passphrase": "12345678", "wpa": "2",
  172. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  173. "wps_independent": "1"})
  174. hostapd.add_ap(apdev[1]['ifname'],
  175. { "ssid": "wps2", "eap_server": "1", "wps_state": "2",
  176. "wpa_passphrase": "123456789", "wpa": "2",
  177. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  178. "wps_independent": "1"})
  179. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  180. hapd.request("WPS_PBC")
  181. hapd2 = hostapd.Hostapd(apdev[1]['ifname'])
  182. hapd2.request("WPS_PBC")
  183. logger.info("WPS provisioning step")
  184. dev[0].dump_monitor()
  185. dev[0].request("WPS_PBC")
  186. ev = dev[0].wait_event(["WPS-OVERLAP-DETECTED"], timeout=15)
  187. if ev is None:
  188. raise Exception("PBC session overlap not detected")
  189. def test_ap_wps_pbc_overlap_2sta(dev, apdev):
  190. """WPS PBC session overlap with two active STAs"""
  191. ssid = "test-wps-pbc-overlap"
  192. hostapd.add_ap(apdev[0]['ifname'],
  193. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  194. "wpa_passphrase": "12345678", "wpa": "2",
  195. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  196. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  197. logger.info("WPS provisioning step")
  198. hapd.request("WPS_PBC")
  199. dev[0].request("SET ignore_old_scan_res 1")
  200. dev[1].request("SET ignore_old_scan_res 1")
  201. dev[0].dump_monitor()
  202. dev[1].dump_monitor()
  203. dev[0].request("WPS_PBC")
  204. dev[1].request("WPS_PBC")
  205. ev = dev[0].wait_event(["WPS-M2D"], timeout=15)
  206. if ev is None:
  207. raise Exception("PBC session overlap not detected (dev0)")
  208. if "config_error=12" not in ev:
  209. raise Exception("PBC session overlap not correctly reported (dev0)")
  210. ev = dev[1].wait_event(["WPS-M2D"], timeout=15)
  211. if ev is None:
  212. raise Exception("PBC session overlap not detected (dev1)")
  213. if "config_error=12" not in ev:
  214. raise Exception("PBC session overlap not correctly reported (dev1)")
  215. def test_ap_wps_er_add_enrollee(dev, apdev):
  216. """WPS ER configuring AP and adding a new enrollee using PIN"""
  217. ssid = "wps-er-add-enrollee"
  218. ap_pin = "12345670"
  219. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  220. hostapd.add_ap(apdev[0]['ifname'],
  221. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  222. "device_name": "Wireless AP", "manufacturer": "Company",
  223. "model_name": "WAP", "model_number": "123",
  224. "serial_number": "12345", "device_type": "6-0050F204-1",
  225. "os_version": "01020300",
  226. "config_methods": "label push_button",
  227. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  228. logger.info("WPS configuration step")
  229. new_passphrase = "1234567890"
  230. dev[0].dump_monitor()
  231. dev[0].request("SET ignore_old_scan_res 1")
  232. dev[0].wps_reg(apdev[0]['bssid'], ap_pin, ssid, "WPA2PSK", "CCMP",
  233. new_passphrase)
  234. status = dev[0].get_status()
  235. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  236. raise Exception("Not fully connected")
  237. if status['ssid'] != ssid:
  238. raise Exception("Unexpected SSID")
  239. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  240. raise Exception("Unexpected encryption configuration")
  241. if status['key_mgmt'] != 'WPA2-PSK':
  242. raise Exception("Unexpected key_mgmt")
  243. logger.info("Start ER")
  244. dev[0].request("WPS_ER_START ifname=lo")
  245. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  246. if ev is None:
  247. raise Exception("AP discovery timed out")
  248. if ap_uuid not in ev:
  249. raise Exception("Expected AP UUID not found")
  250. logger.info("Learn AP configuration through UPnP")
  251. dev[0].dump_monitor()
  252. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  253. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  254. if ev is None:
  255. raise Exception("AP learn timed out")
  256. if ap_uuid not in ev:
  257. raise Exception("Expected AP UUID not in settings")
  258. if "ssid=" + ssid not in ev:
  259. raise Exception("Expected SSID not in settings")
  260. if "key=" + new_passphrase not in ev:
  261. raise Exception("Expected passphrase not in settings")
  262. logger.info("Add Enrollee using ER")
  263. pin = dev[1].wps_read_pin()
  264. dev[0].dump_monitor()
  265. dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
  266. dev[1].request("SET ignore_old_scan_res 1")
  267. dev[1].dump_monitor()
  268. dev[1].request("WPS_PIN any " + pin)
  269. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=30)
  270. if ev is None:
  271. raise Exception("Enrollee did not report success")
  272. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  273. if ev is None:
  274. raise Exception("Association with the AP timed out")
  275. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  276. if ev is None:
  277. raise Exception("WPS ER did not report success")
  278. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  279. def test_ap_wps_er_add_enrollee_pbc(dev, apdev):
  280. """WPS ER connected to AP and adding a new enrollee using PBC"""
  281. ssid = "wps-er-add-enrollee-pbc"
  282. ap_pin = "12345670"
  283. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  284. hostapd.add_ap(apdev[0]['ifname'],
  285. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  286. "wpa_passphrase": "12345678", "wpa": "2",
  287. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  288. "device_name": "Wireless AP", "manufacturer": "Company",
  289. "model_name": "WAP", "model_number": "123",
  290. "serial_number": "12345", "device_type": "6-0050F204-1",
  291. "os_version": "01020300",
  292. "config_methods": "label push_button",
  293. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  294. logger.info("Learn AP configuration")
  295. dev[0].dump_monitor()
  296. dev[0].request("SET ignore_old_scan_res 1")
  297. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  298. status = dev[0].get_status()
  299. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  300. raise Exception("Not fully connected")
  301. logger.info("Start ER")
  302. dev[0].request("WPS_ER_START ifname=lo")
  303. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  304. if ev is None:
  305. raise Exception("AP discovery timed out")
  306. if ap_uuid not in ev:
  307. raise Exception("Expected AP UUID not found")
  308. logger.info("Use learned network configuration on ER")
  309. dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  310. logger.info("Add Enrollee using ER and PBC")
  311. dev[0].dump_monitor()
  312. enrollee = dev[1].p2p_interface_addr()
  313. dev[1].request("SET ignore_old_scan_res 1")
  314. dev[1].dump_monitor()
  315. dev[1].request("WPS_PBC")
  316. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=15)
  317. if ev is None:
  318. raise Exception("Enrollee discovery timed out")
  319. if enrollee not in ev:
  320. raise Exception("Expected Enrollee not found")
  321. dev[0].request("WPS_ER_PBC " + enrollee)
  322. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=15)
  323. if ev is None:
  324. raise Exception("Enrollee did not report success")
  325. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  326. if ev is None:
  327. raise Exception("Association with the AP timed out")
  328. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  329. if ev is None:
  330. raise Exception("WPS ER did not report success")
  331. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  332. def test_ap_wps_fragmentation(dev, apdev):
  333. """WPS with fragmentation in EAP-WSC and mixed mode WPA+WPA2"""
  334. ssid = "test-wps-fragmentation"
  335. hostapd.add_ap(apdev[0]['ifname'],
  336. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  337. "wpa_passphrase": "12345678", "wpa": "3",
  338. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  339. "wpa_pairwise": "TKIP",
  340. "fragment_size": "50" })
  341. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  342. logger.info("WPS provisioning step")
  343. hapd.request("WPS_PBC")
  344. dev[0].request("SET ignore_old_scan_res 1")
  345. dev[0].dump_monitor()
  346. dev[0].request("SET wps_fragment_size 50")
  347. dev[0].request("WPS_PBC")
  348. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  349. if ev is None:
  350. raise Exception("Association with the AP timed out")
  351. status = dev[0].get_status()
  352. if status['wpa_state'] != 'COMPLETED':
  353. raise Exception("Not fully connected")
  354. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  355. raise Exception("Unexpected encryption configuration")
  356. if status['key_mgmt'] != 'WPA2-PSK':
  357. raise Exception("Unexpected key_mgmt")