test_ap_wps.py 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. # WPS tests
  2. # Copyright (c) 2013-2014, 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 re
  11. import socket
  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. if "PBC Status: Active" not in hapd.request("WPS_GET_STATUS"):
  23. raise Exception("PBC status not shown correctly")
  24. dev[0].dump_monitor()
  25. dev[0].request("WPS_PBC")
  26. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  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. status = hapd.request("WPS_GET_STATUS")
  39. if "PBC Status: Disabled" not in status:
  40. raise Exception("PBC status not shown correctly")
  41. if "Last WPS result: Success" not in status:
  42. raise Exception("Last WPS result not shown correctly")
  43. if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
  44. raise Exception("Peer address not shown correctly")
  45. conf = hapd.request("GET_CONFIG")
  46. if "wps_state=configured" not in conf:
  47. raise Exception("AP not in WPS configured state")
  48. if "rsn_pairwise_cipher=CCMP TKIP" not in conf:
  49. raise Exception("Unexpected rsn_pairwise_cipher")
  50. if "wpa_pairwise_cipher=CCMP TKIP" not in conf:
  51. raise Exception("Unexpected wpa_pairwise_cipher")
  52. if "group_cipher=TKIP" not in conf:
  53. raise Exception("Unexpected group_cipher")
  54. def test_ap_wps_init_2ap_pbc(dev, apdev):
  55. """Initial two-radio AP configuration with first WPS PBC Enrollee"""
  56. ssid = "test-wps"
  57. params = { "ssid": ssid, "eap_server": "1", "wps_state": "1" }
  58. hostapd.add_ap(apdev[0]['ifname'], params)
  59. hostapd.add_ap(apdev[1]['ifname'], params)
  60. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  61. logger.info("WPS provisioning step")
  62. hapd.request("WPS_PBC")
  63. dev[0].scan(freq="2412")
  64. bss = dev[0].get_bss(apdev[0]['bssid'])
  65. if "[WPS-PBC]" not in bss['flags']:
  66. raise Exception("WPS-PBC flag missing from AP1")
  67. bss = dev[0].get_bss(apdev[1]['bssid'])
  68. if "[WPS-PBC]" not in bss['flags']:
  69. raise Exception("WPS-PBC flag missing from AP2")
  70. dev[0].dump_monitor()
  71. dev[0].request("WPS_PBC")
  72. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  73. if ev is None:
  74. raise Exception("Association with the AP timed out")
  75. dev[1].scan(freq="2412")
  76. bss = dev[1].get_bss(apdev[0]['bssid'])
  77. if "[WPS-PBC]" in bss['flags']:
  78. raise Exception("WPS-PBC flag not cleared from AP1")
  79. bss = dev[1].get_bss(apdev[1]['bssid'])
  80. if "[WPS-PBC]" in bss['flags']:
  81. raise Exception("WPS-PBC flag bit ckeared from AP2")
  82. def test_ap_wps_init_2ap_pin(dev, apdev):
  83. """Initial two-radio AP configuration with first WPS PIN Enrollee"""
  84. ssid = "test-wps"
  85. params = { "ssid": ssid, "eap_server": "1", "wps_state": "1" }
  86. hostapd.add_ap(apdev[0]['ifname'], params)
  87. hostapd.add_ap(apdev[1]['ifname'], params)
  88. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  89. logger.info("WPS provisioning step")
  90. pin = dev[0].wps_read_pin()
  91. hapd.request("WPS_PIN any " + pin)
  92. dev[0].scan(freq="2412")
  93. bss = dev[0].get_bss(apdev[0]['bssid'])
  94. if "[WPS-AUTH]" not in bss['flags']:
  95. raise Exception("WPS-AUTH flag missing from AP1")
  96. bss = dev[0].get_bss(apdev[1]['bssid'])
  97. if "[WPS-AUTH]" not in bss['flags']:
  98. raise Exception("WPS-AUTH flag missing from AP2")
  99. dev[0].dump_monitor()
  100. dev[0].request("WPS_PIN any " + pin)
  101. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  102. if ev is None:
  103. raise Exception("Association with the AP timed out")
  104. dev[1].scan(freq="2412")
  105. bss = dev[1].get_bss(apdev[0]['bssid'])
  106. if "[WPS-AUTH]" in bss['flags']:
  107. raise Exception("WPS-AUTH flag not cleared from AP1")
  108. bss = dev[1].get_bss(apdev[1]['bssid'])
  109. if "[WPS-AUTH]" in bss['flags']:
  110. raise Exception("WPS-AUTH flag bit ckeared from AP2")
  111. def test_ap_wps_init_through_wps_config(dev, apdev):
  112. """Initial AP configuration using wps_config command"""
  113. ssid = "test-wps-init-config"
  114. hostapd.add_ap(apdev[0]['ifname'],
  115. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  116. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  117. if "FAIL" in hapd.request("WPS_CONFIG " + ssid.encode("hex") + " WPA2PSK CCMP " + "12345678".encode("hex")):
  118. raise Exception("WPS_CONFIG command failed")
  119. dev[0].connect(ssid, psk="12345678", scan_freq="2412", proto="WPA2",
  120. pairwise="CCMP", group="CCMP")
  121. def test_ap_wps_conf(dev, apdev):
  122. """WPS PBC provisioning with configured AP"""
  123. ssid = "test-wps-conf"
  124. hostapd.add_ap(apdev[0]['ifname'],
  125. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  126. "wpa_passphrase": "12345678", "wpa": "2",
  127. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  128. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  129. logger.info("WPS provisioning step")
  130. hapd.request("WPS_PBC")
  131. dev[0].dump_monitor()
  132. dev[0].request("WPS_PBC")
  133. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  134. if ev is None:
  135. raise Exception("Association with the AP timed out")
  136. status = dev[0].get_status()
  137. if status['wpa_state'] != 'COMPLETED':
  138. raise Exception("Not fully connected")
  139. if status['bssid'] != apdev[0]['bssid']:
  140. raise Exception("Unexpected BSSID")
  141. if status['ssid'] != ssid:
  142. raise Exception("Unexpected SSID")
  143. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  144. raise Exception("Unexpected encryption configuration")
  145. if status['key_mgmt'] != 'WPA2-PSK':
  146. raise Exception("Unexpected key_mgmt")
  147. sta = hapd.get_sta(dev[0].p2p_interface_addr())
  148. if 'wpsDeviceName' not in sta or sta['wpsDeviceName'] != "Device A":
  149. raise Exception("Device name not available in STA command")
  150. def test_ap_wps_twice(dev, apdev):
  151. """WPS provisioning with twice to change passphrase"""
  152. ssid = "test-wps-twice"
  153. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  154. "wpa_passphrase": "12345678", "wpa": "2",
  155. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" }
  156. hostapd.add_ap(apdev[0]['ifname'], params)
  157. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  158. logger.info("WPS provisioning step")
  159. hapd.request("WPS_PBC")
  160. dev[0].dump_monitor()
  161. dev[0].request("WPS_PBC")
  162. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  163. if ev is None:
  164. raise Exception("Association with the AP timed out")
  165. dev[0].request("DISCONNECT")
  166. logger.info("Restart AP with different passphrase and re-run WPS")
  167. hapd_global = hostapd.HostapdGlobal()
  168. hapd_global.remove(apdev[0]['ifname'])
  169. params['wpa_passphrase'] = 'another passphrase'
  170. hostapd.add_ap(apdev[0]['ifname'], params)
  171. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  172. logger.info("WPS provisioning step")
  173. hapd.request("WPS_PBC")
  174. dev[0].dump_monitor()
  175. dev[0].request("WPS_PBC")
  176. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  177. if ev is None:
  178. raise Exception("Association with the AP timed out")
  179. networks = dev[0].list_networks()
  180. if len(networks) > 1:
  181. raise Exception("Unexpected duplicated network block present")
  182. def test_ap_wps_incorrect_pin(dev, apdev):
  183. """WPS PIN provisioning with incorrect PIN"""
  184. ssid = "test-wps-incorrect-pin"
  185. hostapd.add_ap(apdev[0]['ifname'],
  186. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  187. "wpa_passphrase": "12345678", "wpa": "2",
  188. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  189. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  190. logger.info("WPS provisioning attempt 1")
  191. hapd.request("WPS_PIN any 12345670")
  192. dev[0].dump_monitor()
  193. dev[0].request("WPS_PIN any 55554444")
  194. ev = dev[0].wait_event(["WPS-FAIL"], timeout=30)
  195. if ev is None:
  196. raise Exception("WPS operation timed out")
  197. if "config_error=18" not in ev:
  198. raise Exception("Incorrect config_error reported")
  199. if "msg=8" not in ev:
  200. raise Exception("PIN error detected on incorrect message")
  201. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  202. if ev is None:
  203. raise Exception("Timeout on disconnection event")
  204. dev[0].request("WPS_CANCEL")
  205. # if a scan was in progress, wait for it to complete before trying WPS again
  206. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  207. status = hapd.request("WPS_GET_STATUS")
  208. if "Last WPS result: Failed" not in status:
  209. raise Exception("WPS failure result not shown correctly")
  210. logger.info("WPS provisioning attempt 2")
  211. hapd.request("WPS_PIN any 12345670")
  212. dev[0].dump_monitor()
  213. dev[0].request("WPS_PIN any 12344444")
  214. ev = dev[0].wait_event(["WPS-FAIL"], timeout=30)
  215. if ev is None:
  216. raise Exception("WPS operation timed out")
  217. if "config_error=18" not in ev:
  218. raise Exception("Incorrect config_error reported")
  219. if "msg=10" not in ev:
  220. raise Exception("PIN error detected on incorrect message")
  221. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  222. if ev is None:
  223. raise Exception("Timeout on disconnection event")
  224. def test_ap_wps_conf_pin(dev, apdev):
  225. """WPS PIN provisioning with configured AP"""
  226. ssid = "test-wps-conf-pin"
  227. hostapd.add_ap(apdev[0]['ifname'],
  228. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  229. "wpa_passphrase": "12345678", "wpa": "2",
  230. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  231. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  232. logger.info("WPS provisioning step")
  233. pin = dev[0].wps_read_pin()
  234. hapd.request("WPS_PIN any " + pin)
  235. dev[0].dump_monitor()
  236. dev[0].request("WPS_PIN any " + pin)
  237. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  238. if ev is None:
  239. raise Exception("Association with the AP timed out")
  240. status = dev[0].get_status()
  241. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  242. raise Exception("Not fully connected")
  243. if status['ssid'] != ssid:
  244. raise Exception("Unexpected SSID")
  245. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  246. raise Exception("Unexpected encryption configuration")
  247. if status['key_mgmt'] != 'WPA2-PSK':
  248. raise Exception("Unexpected key_mgmt")
  249. dev[1].scan(freq="2412")
  250. bss = dev[1].get_bss(apdev[0]['bssid'])
  251. if "[WPS-AUTH]" in bss['flags']:
  252. raise Exception("WPS-AUTH flag not cleared")
  253. logger.info("Try to connect from another station using the same PIN")
  254. dev[1].request("WPS_PIN any " + pin)
  255. ev = dev[1].wait_event(["WPS-M2D","CTRL-EVENT-CONNECTED"], timeout=30)
  256. if ev is None:
  257. raise Exception("Operation timed out")
  258. if "WPS-M2D" not in ev:
  259. raise Exception("Unexpected WPS operation started")
  260. def test_ap_wps_conf_pin_2sta(dev, apdev):
  261. """Two stations trying to use WPS PIN at the same time"""
  262. ssid = "test-wps-conf-pin2"
  263. hostapd.add_ap(apdev[0]['ifname'],
  264. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  265. "wpa_passphrase": "12345678", "wpa": "2",
  266. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  267. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  268. logger.info("WPS provisioning step")
  269. pin = "12345670"
  270. pin2 = "55554444"
  271. hapd.request("WPS_PIN " + dev[0].get_status_field("uuid") + " " + pin)
  272. hapd.request("WPS_PIN " + dev[1].get_status_field("uuid") + " " + pin)
  273. dev[0].dump_monitor()
  274. dev[1].dump_monitor()
  275. dev[0].request("WPS_PIN any " + pin)
  276. dev[1].request("WPS_PIN any " + pin)
  277. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  278. if ev is None:
  279. raise Exception("Association with the AP timed out")
  280. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  281. if ev is None:
  282. raise Exception("Association with the AP timed out")
  283. def test_ap_wps_reg_connect(dev, apdev):
  284. """WPS registrar using AP PIN to connect"""
  285. ssid = "test-wps-reg-ap-pin"
  286. appin = "12345670"
  287. hostapd.add_ap(apdev[0]['ifname'],
  288. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  289. "wpa_passphrase": "12345678", "wpa": "2",
  290. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  291. "ap_pin": appin})
  292. logger.info("WPS provisioning step")
  293. dev[0].dump_monitor()
  294. dev[0].wps_reg(apdev[0]['bssid'], appin)
  295. status = dev[0].get_status()
  296. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  297. raise Exception("Not fully connected")
  298. if status['ssid'] != ssid:
  299. raise Exception("Unexpected SSID")
  300. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  301. raise Exception("Unexpected encryption configuration")
  302. if status['key_mgmt'] != 'WPA2-PSK':
  303. raise Exception("Unexpected key_mgmt")
  304. def check_wps_reg_failure(dev, ap, appin):
  305. dev.request("WPS_REG " + ap['bssid'] + " " + appin)
  306. ev = dev.wait_event(["WPS-SUCCESS", "WPS-FAIL"], timeout=15)
  307. if ev is None:
  308. raise Exception("WPS operation timed out")
  309. if "WPS-SUCCESS" in ev:
  310. raise Exception("WPS operation succeeded unexpectedly")
  311. if "config_error=15" not in ev:
  312. raise Exception("WPS setup locked state was not reported correctly")
  313. def test_ap_wps_random_ap_pin(dev, apdev):
  314. """WPS registrar using random AP PIN"""
  315. ssid = "test-wps-reg-random-ap-pin"
  316. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  317. hostapd.add_ap(apdev[0]['ifname'],
  318. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  319. "wpa_passphrase": "12345678", "wpa": "2",
  320. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  321. "device_name": "Wireless AP", "manufacturer": "Company",
  322. "model_name": "WAP", "model_number": "123",
  323. "serial_number": "12345", "device_type": "6-0050F204-1",
  324. "os_version": "01020300",
  325. "config_methods": "label push_button",
  326. "uuid": ap_uuid, "upnp_iface": "lo" })
  327. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  328. appin = hapd.request("WPS_AP_PIN random")
  329. if "FAIL" in appin:
  330. raise Exception("Could not generate random AP PIN")
  331. if appin not in hapd.request("WPS_AP_PIN get"):
  332. raise Exception("Could not fetch current AP PIN")
  333. logger.info("WPS provisioning step")
  334. dev[0].wps_reg(apdev[0]['bssid'], appin)
  335. hapd.request("WPS_AP_PIN disable")
  336. logger.info("WPS provisioning step with AP PIN disabled")
  337. check_wps_reg_failure(dev[1], apdev[0], appin)
  338. logger.info("WPS provisioning step with AP PIN reset")
  339. appin = "12345670"
  340. hapd.request("WPS_AP_PIN set " + appin)
  341. dev[1].wps_reg(apdev[0]['bssid'], appin)
  342. dev[0].request("REMOVE_NETWORK all")
  343. dev[1].request("REMOVE_NETWORK all")
  344. dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  345. dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
  346. logger.info("WPS provisioning step after AP PIN timeout")
  347. hapd.request("WPS_AP_PIN disable")
  348. appin = hapd.request("WPS_AP_PIN random 1")
  349. time.sleep(1.1)
  350. if "FAIL" not in hapd.request("WPS_AP_PIN get"):
  351. raise Exception("AP PIN unexpectedly still enabled")
  352. check_wps_reg_failure(dev[0], apdev[0], appin)
  353. logger.info("WPS provisioning step after AP PIN timeout(2)")
  354. hapd.request("WPS_AP_PIN disable")
  355. appin = "12345670"
  356. hapd.request("WPS_AP_PIN set " + appin + " 1")
  357. time.sleep(1.1)
  358. if "FAIL" not in hapd.request("WPS_AP_PIN get"):
  359. raise Exception("AP PIN unexpectedly still enabled")
  360. check_wps_reg_failure(dev[1], apdev[0], appin)
  361. def test_ap_wps_reg_config(dev, apdev):
  362. """WPS registrar configuring and AP using AP PIN"""
  363. ssid = "test-wps-init-ap-pin"
  364. appin = "12345670"
  365. hostapd.add_ap(apdev[0]['ifname'],
  366. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  367. "ap_pin": appin})
  368. logger.info("WPS configuration step")
  369. dev[0].dump_monitor()
  370. new_ssid = "wps-new-ssid"
  371. new_passphrase = "1234567890"
  372. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPA2PSK", "CCMP",
  373. new_passphrase)
  374. status = dev[0].get_status()
  375. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  376. raise Exception("Not fully connected")
  377. if status['ssid'] != new_ssid:
  378. raise Exception("Unexpected SSID")
  379. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  380. raise Exception("Unexpected encryption configuration")
  381. if status['key_mgmt'] != 'WPA2-PSK':
  382. raise Exception("Unexpected key_mgmt")
  383. def test_ap_wps_reg_config_tkip(dev, apdev):
  384. """WPS registrar configuring AP to use TKIP and AP upgrading to TKIP+CCMP"""
  385. ssid = "test-wps-init-ap"
  386. appin = "12345670"
  387. hostapd.add_ap(apdev[0]['ifname'],
  388. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  389. "ap_pin": appin})
  390. logger.info("WPS configuration step")
  391. dev[0].request("SET wps_version_number 0x10")
  392. dev[0].dump_monitor()
  393. new_ssid = "wps-new-ssid-with-tkip"
  394. new_passphrase = "1234567890"
  395. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPAPSK", "TKIP",
  396. new_passphrase)
  397. logger.info("Re-connect to verify WPA2 mixed mode")
  398. dev[0].request("DISCONNECT")
  399. id = 0
  400. dev[0].set_network(id, "pairwise", "CCMP")
  401. dev[0].set_network(id, "proto", "RSN")
  402. dev[0].connect_network(id)
  403. status = dev[0].get_status()
  404. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  405. raise Exception("Not fully connected")
  406. if status['ssid'] != new_ssid:
  407. raise Exception("Unexpected SSID")
  408. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  409. raise Exception("Unexpected encryption configuration")
  410. if status['key_mgmt'] != 'WPA2-PSK':
  411. raise Exception("Unexpected key_mgmt")
  412. def test_ap_wps_setup_locked(dev, apdev):
  413. """WPS registrar locking up AP setup on AP PIN failures"""
  414. ssid = "test-wps-incorrect-ap-pin"
  415. appin = "12345670"
  416. hostapd.add_ap(apdev[0]['ifname'],
  417. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  418. "wpa_passphrase": "12345678", "wpa": "2",
  419. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  420. "ap_pin": appin})
  421. new_ssid = "wps-new-ssid-test"
  422. new_passphrase = "1234567890"
  423. ap_setup_locked=False
  424. for pin in ["55554444", "1234", "12345678", "00000000", "11111111"]:
  425. dev[0].dump_monitor()
  426. logger.info("Try incorrect AP PIN - attempt " + pin)
  427. dev[0].wps_reg(apdev[0]['bssid'], pin, new_ssid, "WPA2PSK",
  428. "CCMP", new_passphrase, no_wait=True)
  429. ev = dev[0].wait_event(["WPS-FAIL", "CTRL-EVENT-CONNECTED"])
  430. if ev is None:
  431. raise Exception("Timeout on receiving WPS operation failure event")
  432. if "CTRL-EVENT-CONNECTED" in ev:
  433. raise Exception("Unexpected connection")
  434. if "config_error=15" in ev:
  435. logger.info("AP Setup Locked")
  436. ap_setup_locked=True
  437. elif "config_error=18" not in ev:
  438. raise Exception("config_error=18 not reported")
  439. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  440. if ev is None:
  441. raise Exception("Timeout on disconnection event")
  442. time.sleep(0.1)
  443. if not ap_setup_locked:
  444. raise Exception("AP setup was not locked")
  445. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  446. status = hapd.request("WPS_GET_STATUS")
  447. if "Last WPS result: Failed" not in status:
  448. raise Exception("WPS failure result not shown correctly")
  449. if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
  450. raise Exception("Peer address not shown correctly")
  451. time.sleep(0.5)
  452. dev[0].dump_monitor()
  453. logger.info("WPS provisioning step")
  454. pin = dev[0].wps_read_pin()
  455. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  456. hapd.request("WPS_PIN any " + pin)
  457. dev[0].request("WPS_PIN any " + pin)
  458. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=30)
  459. if ev is None:
  460. raise Exception("WPS success was not reported")
  461. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  462. if ev is None:
  463. raise Exception("Association with the AP timed out")
  464. def test_ap_wps_pbc_overlap_2ap(dev, apdev):
  465. """WPS PBC session overlap with two active APs"""
  466. hostapd.add_ap(apdev[0]['ifname'],
  467. { "ssid": "wps1", "eap_server": "1", "wps_state": "2",
  468. "wpa_passphrase": "12345678", "wpa": "2",
  469. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  470. "wps_independent": "1"})
  471. hostapd.add_ap(apdev[1]['ifname'],
  472. { "ssid": "wps2", "eap_server": "1", "wps_state": "2",
  473. "wpa_passphrase": "123456789", "wpa": "2",
  474. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  475. "wps_independent": "1"})
  476. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  477. hapd.request("WPS_PBC")
  478. hapd2 = hostapd.Hostapd(apdev[1]['ifname'])
  479. hapd2.request("WPS_PBC")
  480. logger.info("WPS provisioning step")
  481. dev[0].dump_monitor()
  482. dev[0].request("WPS_PBC")
  483. ev = dev[0].wait_event(["WPS-OVERLAP-DETECTED"], timeout=15)
  484. if ev is None:
  485. raise Exception("PBC session overlap not detected")
  486. def test_ap_wps_pbc_overlap_2sta(dev, apdev):
  487. """WPS PBC session overlap with two active STAs"""
  488. ssid = "test-wps-pbc-overlap"
  489. hostapd.add_ap(apdev[0]['ifname'],
  490. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  491. "wpa_passphrase": "12345678", "wpa": "2",
  492. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  493. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  494. logger.info("WPS provisioning step")
  495. hapd.request("WPS_PBC")
  496. dev[0].dump_monitor()
  497. dev[1].dump_monitor()
  498. dev[0].request("WPS_PBC")
  499. dev[1].request("WPS_PBC")
  500. ev = dev[0].wait_event(["WPS-M2D"], timeout=15)
  501. if ev is None:
  502. raise Exception("PBC session overlap not detected (dev0)")
  503. if "config_error=12" not in ev:
  504. raise Exception("PBC session overlap not correctly reported (dev0)")
  505. ev = dev[1].wait_event(["WPS-M2D"], timeout=15)
  506. if ev is None:
  507. raise Exception("PBC session overlap not detected (dev1)")
  508. if "config_error=12" not in ev:
  509. raise Exception("PBC session overlap not correctly reported (dev1)")
  510. hapd.request("WPS_CANCEL")
  511. ret = hapd.request("WPS_PBC")
  512. if "FAIL" not in ret:
  513. raise Exception("PBC mode allowed to be started while PBC overlap still active")
  514. def test_ap_wps_cancel(dev, apdev):
  515. """WPS AP cancelling enabled config method"""
  516. ssid = "test-wps-ap-cancel"
  517. hostapd.add_ap(apdev[0]['ifname'],
  518. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  519. "wpa_passphrase": "12345678", "wpa": "2",
  520. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  521. bssid = apdev[0]['bssid']
  522. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  523. logger.info("Verify PBC enable/cancel")
  524. hapd.request("WPS_PBC")
  525. dev[0].scan(freq="2412")
  526. bss = dev[0].get_bss(apdev[0]['bssid'])
  527. if "[WPS-PBC]" not in bss['flags']:
  528. raise Exception("WPS-PBC flag missing")
  529. if "FAIL" in hapd.request("WPS_CANCEL"):
  530. raise Exception("WPS_CANCEL failed")
  531. dev[0].scan(freq="2412")
  532. bss = dev[0].get_bss(apdev[0]['bssid'])
  533. if "[WPS-PBC]" in bss['flags']:
  534. raise Exception("WPS-PBC flag not cleared")
  535. logger.info("Verify PIN enable/cancel")
  536. hapd.request("WPS_PIN any 12345670")
  537. dev[0].scan(freq="2412")
  538. bss = dev[0].get_bss(apdev[0]['bssid'])
  539. if "[WPS-AUTH]" not in bss['flags']:
  540. raise Exception("WPS-AUTH flag missing")
  541. if "FAIL" in hapd.request("WPS_CANCEL"):
  542. raise Exception("WPS_CANCEL failed")
  543. dev[0].scan(freq="2412")
  544. bss = dev[0].get_bss(apdev[0]['bssid'])
  545. if "[WPS-AUTH]" in bss['flags']:
  546. raise Exception("WPS-AUTH flag not cleared")
  547. def test_ap_wps_er_add_enrollee(dev, apdev):
  548. """WPS ER configuring AP and adding a new enrollee using PIN"""
  549. ssid = "wps-er-add-enrollee"
  550. ap_pin = "12345670"
  551. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  552. hostapd.add_ap(apdev[0]['ifname'],
  553. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  554. "device_name": "Wireless AP", "manufacturer": "Company",
  555. "model_name": "WAP", "model_number": "123",
  556. "serial_number": "12345", "device_type": "6-0050F204-1",
  557. "os_version": "01020300",
  558. "config_methods": "label push_button",
  559. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  560. logger.info("WPS configuration step")
  561. new_passphrase = "1234567890"
  562. dev[0].dump_monitor()
  563. dev[0].wps_reg(apdev[0]['bssid'], ap_pin, ssid, "WPA2PSK", "CCMP",
  564. new_passphrase)
  565. status = dev[0].get_status()
  566. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  567. raise Exception("Not fully connected")
  568. if status['ssid'] != ssid:
  569. raise Exception("Unexpected SSID")
  570. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  571. raise Exception("Unexpected encryption configuration")
  572. if status['key_mgmt'] != 'WPA2-PSK':
  573. raise Exception("Unexpected key_mgmt")
  574. logger.info("Start ER")
  575. dev[0].request("WPS_ER_START ifname=lo")
  576. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  577. if ev is None:
  578. raise Exception("AP discovery timed out")
  579. if ap_uuid not in ev:
  580. raise Exception("Expected AP UUID not found")
  581. logger.info("Learn AP configuration through UPnP")
  582. dev[0].dump_monitor()
  583. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  584. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  585. if ev is None:
  586. raise Exception("AP learn timed out")
  587. if ap_uuid not in ev:
  588. raise Exception("Expected AP UUID not in settings")
  589. if "ssid=" + ssid not in ev:
  590. raise Exception("Expected SSID not in settings")
  591. if "key=" + new_passphrase not in ev:
  592. raise Exception("Expected passphrase not in settings")
  593. logger.info("Add Enrollee using ER")
  594. pin = dev[1].wps_read_pin()
  595. dev[0].dump_monitor()
  596. dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
  597. dev[1].dump_monitor()
  598. dev[1].request("WPS_PIN any " + pin)
  599. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=30)
  600. if ev is None:
  601. raise Exception("Enrollee did not report success")
  602. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  603. if ev is None:
  604. raise Exception("Association with the AP timed out")
  605. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  606. if ev is None:
  607. raise Exception("WPS ER did not report success")
  608. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  609. logger.info("Add a specific Enrollee using ER")
  610. pin = dev[2].wps_read_pin()
  611. addr2 = dev[2].p2p_interface_addr()
  612. dev[0].dump_monitor()
  613. dev[2].dump_monitor()
  614. dev[2].request("WPS_PIN any " + pin)
  615. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=10)
  616. if ev is None:
  617. raise Exception("Enrollee not seen")
  618. if addr2 not in ev:
  619. raise Exception("Unexpected Enrollee MAC address")
  620. dev[0].request("WPS_ER_PIN " + addr2 + " " + pin + " " + addr2)
  621. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  622. if ev is None:
  623. raise Exception("Association with the AP timed out")
  624. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  625. if ev is None:
  626. raise Exception("WPS ER did not report success")
  627. logger.info("Verify registrar selection behavior")
  628. dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
  629. dev[1].request("DISCONNECT")
  630. dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
  631. dev[1].scan(freq="2412")
  632. bss = dev[1].get_bss(apdev[0]['bssid'])
  633. if "[WPS-AUTH]" not in bss['flags']:
  634. raise Exception("WPS-AUTH flag missing")
  635. logger.info("Stop ER")
  636. dev[0].dump_monitor()
  637. dev[0].request("WPS_ER_STOP")
  638. ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"])
  639. if ev is None:
  640. raise Exception("WPS ER unsubscription timed out")
  641. # It takes some time for the UPnP UNSUBSCRIBE command to go through, so wait
  642. # a bit before verifying that the scan results have change.
  643. time.sleep(0.2)
  644. dev[1].scan(freq="2412")
  645. bss = dev[1].get_bss(apdev[0]['bssid'])
  646. if "[WPS-AUTH]" in bss['flags']:
  647. raise Exception("WPS-AUTH flag not removed")
  648. def test_ap_wps_er_add_enrollee_pbc(dev, apdev):
  649. """WPS ER connected to AP and adding a new enrollee using PBC"""
  650. ssid = "wps-er-add-enrollee-pbc"
  651. ap_pin = "12345670"
  652. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  653. hostapd.add_ap(apdev[0]['ifname'],
  654. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  655. "wpa_passphrase": "12345678", "wpa": "2",
  656. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  657. "device_name": "Wireless AP", "manufacturer": "Company",
  658. "model_name": "WAP", "model_number": "123",
  659. "serial_number": "12345", "device_type": "6-0050F204-1",
  660. "os_version": "01020300",
  661. "config_methods": "label push_button",
  662. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  663. logger.info("Learn AP configuration")
  664. dev[0].dump_monitor()
  665. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  666. status = dev[0].get_status()
  667. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  668. raise Exception("Not fully connected")
  669. logger.info("Start ER")
  670. dev[0].request("WPS_ER_START ifname=lo")
  671. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  672. if ev is None:
  673. raise Exception("AP discovery timed out")
  674. if ap_uuid not in ev:
  675. raise Exception("Expected AP UUID not found")
  676. logger.info("Use learned network configuration on ER")
  677. dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  678. logger.info("Add Enrollee using ER and PBC")
  679. dev[0].dump_monitor()
  680. enrollee = dev[1].p2p_interface_addr()
  681. dev[1].dump_monitor()
  682. dev[1].request("WPS_PBC")
  683. for i in range(0, 2):
  684. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=15)
  685. if ev is None:
  686. raise Exception("Enrollee discovery timed out")
  687. if enrollee in ev:
  688. break
  689. if i == 1:
  690. raise Exception("Expected Enrollee not found")
  691. dev[0].request("WPS_ER_PBC " + enrollee)
  692. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=15)
  693. if ev is None:
  694. raise Exception("Enrollee did not report success")
  695. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  696. if ev is None:
  697. raise Exception("Association with the AP timed out")
  698. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  699. if ev is None:
  700. raise Exception("WPS ER did not report success")
  701. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  702. # verify BSSID selection of the AP instead of UUID
  703. if "FAIL" in dev[0].request("WPS_ER_SET_CONFIG " + apdev[0]['bssid'] + " 0"):
  704. raise Exception("Could not select AP based on BSSID")
  705. def test_ap_wps_er_v10_add_enrollee_pin(dev, apdev):
  706. """WPS v1.0 ER connected to AP and adding a new enrollee using PIN"""
  707. ssid = "wps-er-add-enrollee-pbc"
  708. ap_pin = "12345670"
  709. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  710. hostapd.add_ap(apdev[0]['ifname'],
  711. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  712. "wpa_passphrase": "12345678", "wpa": "2",
  713. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  714. "device_name": "Wireless AP", "manufacturer": "Company",
  715. "model_name": "WAP", "model_number": "123",
  716. "serial_number": "12345", "device_type": "6-0050F204-1",
  717. "os_version": "01020300",
  718. "config_methods": "label push_button",
  719. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  720. logger.info("Learn AP configuration")
  721. dev[0].request("SET wps_version_number 0x10")
  722. dev[0].dump_monitor()
  723. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  724. status = dev[0].get_status()
  725. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  726. raise Exception("Not fully connected")
  727. logger.info("Start ER")
  728. dev[0].request("WPS_ER_START ifname=lo")
  729. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  730. if ev is None:
  731. raise Exception("AP discovery timed out")
  732. if ap_uuid not in ev:
  733. raise Exception("Expected AP UUID not found")
  734. logger.info("Use learned network configuration on ER")
  735. dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  736. logger.info("Add Enrollee using ER and PIN")
  737. enrollee = dev[1].p2p_interface_addr()
  738. pin = dev[1].wps_read_pin()
  739. dev[0].dump_monitor()
  740. dev[0].request("WPS_ER_PIN any " + pin + " " + enrollee)
  741. dev[1].dump_monitor()
  742. dev[1].request("WPS_PIN any " + pin)
  743. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  744. if ev is None:
  745. raise Exception("Association with the AP timed out")
  746. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  747. if ev is None:
  748. raise Exception("WPS ER did not report success")
  749. def test_ap_wps_er_config_ap(dev, apdev):
  750. """WPS ER configuring AP over UPnP"""
  751. ssid = "wps-er-ap-config"
  752. ap_pin = "12345670"
  753. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  754. hostapd.add_ap(apdev[0]['ifname'],
  755. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  756. "wpa_passphrase": "12345678", "wpa": "2",
  757. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  758. "device_name": "Wireless AP", "manufacturer": "Company",
  759. "model_name": "WAP", "model_number": "123",
  760. "serial_number": "12345", "device_type": "6-0050F204-1",
  761. "os_version": "01020300",
  762. "config_methods": "label push_button",
  763. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  764. logger.info("Connect ER to the AP")
  765. dev[0].connect(ssid, psk="12345678", scan_freq="2412")
  766. logger.info("WPS configuration step")
  767. dev[0].request("WPS_ER_START ifname=lo")
  768. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  769. if ev is None:
  770. raise Exception("AP discovery timed out")
  771. if ap_uuid not in ev:
  772. raise Exception("Expected AP UUID not found")
  773. new_passphrase = "1234567890"
  774. dev[0].request("WPS_ER_CONFIG " + apdev[0]['bssid'] + " " + ap_pin + " " +
  775. ssid.encode("hex") + " WPA2PSK CCMP " +
  776. new_passphrase.encode("hex"))
  777. ev = dev[0].wait_event(["WPS-SUCCESS"])
  778. if ev is None:
  779. raise Exception("WPS ER configuration operation timed out")
  780. dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
  781. dev[0].connect(ssid, psk="1234567890", scan_freq="2412")
  782. def test_ap_wps_fragmentation(dev, apdev):
  783. """WPS with fragmentation in EAP-WSC and mixed mode WPA+WPA2"""
  784. ssid = "test-wps-fragmentation"
  785. hostapd.add_ap(apdev[0]['ifname'],
  786. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  787. "wpa_passphrase": "12345678", "wpa": "3",
  788. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  789. "wpa_pairwise": "TKIP",
  790. "fragment_size": "50" })
  791. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  792. logger.info("WPS provisioning step")
  793. hapd.request("WPS_PBC")
  794. dev[0].dump_monitor()
  795. dev[0].request("SET wps_fragment_size 50")
  796. dev[0].request("WPS_PBC")
  797. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  798. if ev is None:
  799. raise Exception("Association with the AP timed out")
  800. status = dev[0].get_status()
  801. if status['wpa_state'] != 'COMPLETED':
  802. raise Exception("Not fully connected")
  803. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  804. raise Exception("Unexpected encryption configuration")
  805. if status['key_mgmt'] != 'WPA2-PSK':
  806. raise Exception("Unexpected key_mgmt")
  807. def test_ap_wps_new_version_sta(dev, apdev):
  808. """WPS compatibility with new version number on the station"""
  809. ssid = "test-wps-ver"
  810. hostapd.add_ap(apdev[0]['ifname'],
  811. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  812. "wpa_passphrase": "12345678", "wpa": "2",
  813. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  814. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  815. logger.info("WPS provisioning step")
  816. hapd.request("WPS_PBC")
  817. dev[0].dump_monitor()
  818. dev[0].request("SET wps_version_number 0x43")
  819. dev[0].request("SET wps_vendor_ext_m1 000137100100020001")
  820. dev[0].request("WPS_PBC")
  821. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  822. if ev is None:
  823. raise Exception("Association with the AP timed out")
  824. def test_ap_wps_new_version_ap(dev, apdev):
  825. """WPS compatibility with new version number on the AP"""
  826. ssid = "test-wps-ver"
  827. hostapd.add_ap(apdev[0]['ifname'],
  828. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  829. "wpa_passphrase": "12345678", "wpa": "2",
  830. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  831. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  832. logger.info("WPS provisioning step")
  833. if "FAIL" in hapd.request("SET wps_version_number 0x43"):
  834. raise Exception("Failed to enable test functionality")
  835. hapd.request("WPS_PBC")
  836. dev[0].dump_monitor()
  837. dev[0].request("WPS_PBC")
  838. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  839. hapd.request("SET wps_version_number 0x20")
  840. if ev is None:
  841. raise Exception("Association with the AP timed out")
  842. def test_ap_wps_check_pin(dev, apdev):
  843. """Verify PIN checking through control interface"""
  844. hostapd.add_ap(apdev[0]['ifname'],
  845. { "ssid": "wps", "eap_server": "1", "wps_state": "2",
  846. "wpa_passphrase": "12345678", "wpa": "2",
  847. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  848. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  849. for t in [ ("12345670", "12345670"),
  850. ("12345678", "FAIL-CHECKSUM"),
  851. ("1234-5670", "12345670"),
  852. ("1234 5670", "12345670"),
  853. ("1-2.3:4 5670", "12345670") ]:
  854. res = hapd.request("WPS_CHECK_PIN " + t[0]).rstrip('\n')
  855. res2 = dev[0].request("WPS_CHECK_PIN " + t[0]).rstrip('\n')
  856. if res != res2:
  857. raise Exception("Unexpected difference in WPS_CHECK_PIN responses")
  858. if res != t[1]:
  859. raise Exception("Incorrect WPS_CHECK_PIN response {} (expected {})".format(res, t[1]))
  860. def test_ap_wps_wep_config(dev, apdev):
  861. """WPS 2.0 AP rejecting WEP configuration"""
  862. ssid = "test-wps-config"
  863. appin = "12345670"
  864. hostapd.add_ap(apdev[0]['ifname'],
  865. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  866. "ap_pin": appin})
  867. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  868. dev[0].wps_reg(apdev[0]['bssid'], appin, "wps-new-ssid-wep", "OPEN", "WEP",
  869. "hello", no_wait=True)
  870. ev = hapd.wait_event(["WPS-FAIL"], timeout=15)
  871. if ev is None:
  872. raise Exception("WPS-FAIL timed out")
  873. if "reason=2" not in ev:
  874. raise Exception("Unexpected reason code in WPS-FAIL")
  875. status = hapd.request("WPS_GET_STATUS")
  876. if "Last WPS result: Failed" not in status:
  877. raise Exception("WPS failure result not shown correctly")
  878. if "Failure Reason: WEP Prohibited" not in status:
  879. raise Exception("Failure reason not reported correctly")
  880. if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
  881. raise Exception("Peer address not shown correctly")
  882. def test_ap_wps_ie_fragmentation(dev, apdev):
  883. """WPS AP using fragmented WPS IE"""
  884. ssid = "test-wps-ie-fragmentation"
  885. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  886. "wpa_passphrase": "12345678", "wpa": "2",
  887. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  888. "device_name": "1234567890abcdef1234567890abcdef",
  889. "manufacturer": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
  890. "model_name": "1234567890abcdef1234567890abcdef",
  891. "model_number": "1234567890abcdef1234567890abcdef",
  892. "serial_number": "1234567890abcdef1234567890abcdef" }
  893. hostapd.add_ap(apdev[0]['ifname'], params)
  894. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  895. hapd.request("WPS_PBC")
  896. dev[0].request("WPS_PBC")
  897. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  898. if ev is None:
  899. raise Exception("Association with the AP timed out")
  900. bss = dev[0].get_bss(apdev[0]['bssid'])
  901. if "wps_device_name" not in bss or bss['wps_device_name'] != "1234567890abcdef1234567890abcdef":
  902. raise Exception("Device Name not received correctly")
  903. if len(re.findall("dd..0050f204", bss['ie'])) != 2:
  904. raise Exception("Unexpected number of WPS IEs")
  905. def add_ssdp_ap(ifname, ap_uuid):
  906. ssid = "wps-ssdp"
  907. ap_pin = "12345670"
  908. hostapd.add_ap(ifname,
  909. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  910. "wpa_passphrase": "12345678", "wpa": "2",
  911. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  912. "device_name": "Wireless AP", "manufacturer": "Company",
  913. "model_name": "WAP", "model_number": "123",
  914. "serial_number": "12345", "device_type": "6-0050F204-1",
  915. "os_version": "01020300",
  916. "config_methods": "label push_button",
  917. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo",
  918. "friendly_name": "WPS Access Point",
  919. "manufacturer_url": "http://www.example.com/",
  920. "model_description": "Wireless Access Point",
  921. "model_url": "http://www.example.com/model/",
  922. "upc": "123456789012" })
  923. def ssdp_send(msg, no_recv=False):
  924. socket.setdefaulttimeout(1)
  925. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  926. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  927. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  928. sock.bind(("127.0.0.1", 0))
  929. sock.sendto(msg, ("239.255.255.250", 1900))
  930. if no_recv:
  931. return None
  932. return sock.recv(1000)
  933. def ssdp_send_msearch(st):
  934. msg = '\r\n'.join([
  935. 'M-SEARCH * HTTP/1.1',
  936. 'HOST: 239.255.255.250:1900',
  937. 'MX: 1',
  938. 'MAN: "ssdp:discover"',
  939. 'ST: ' + st,
  940. '', ''])
  941. ssdp_send(msg)
  942. def test_ap_wps_ssdp_msearch(dev, apdev):
  943. """WPS AP and SSDP M-SEARCH messages"""
  944. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  945. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  946. msg = '\r\n'.join([
  947. 'M-SEARCH * HTTP/1.1',
  948. 'Host: 239.255.255.250:1900',
  949. 'Mx: 1',
  950. 'Man: "ssdp:discover"',
  951. 'St: urn:schemas-wifialliance-org:device:WFADevice:1',
  952. '', ''])
  953. ssdp_send(msg)
  954. msg = '\r\n'.join([
  955. 'M-SEARCH * HTTP/1.1',
  956. 'host:\t239.255.255.250:1900\t\t\t\t \t\t',
  957. 'mx: \t1\t\t ',
  958. 'man: \t \t "ssdp:discover" ',
  959. 'st: urn:schemas-wifialliance-org:device:WFADevice:1\t\t',
  960. '', ''])
  961. ssdp_send(msg)
  962. ssdp_send_msearch("ssdp:all")
  963. ssdp_send_msearch("upnp:rootdevice")
  964. ssdp_send_msearch("uuid:" + ap_uuid)
  965. ssdp_send_msearch("urn:schemas-wifialliance-org:service:WFAWLANConfig:1")
  966. ssdp_send_msearch("urn:schemas-wifialliance-org:device:WFADevice:1");
  967. msg = '\r\n'.join([
  968. 'M-SEARCH * HTTP/1.1',
  969. 'HOST:\t239.255.255.250:1900',
  970. 'MAN: "ssdp:discover"',
  971. 'MX: 130',
  972. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  973. '', ''])
  974. ssdp_send(msg, no_recv=True)
  975. def test_ap_wps_ssdp_invalid_msearch(dev, apdev):
  976. """WPS AP and invalid SSDP M-SEARCH messages"""
  977. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  978. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  979. socket.setdefaulttimeout(1)
  980. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  981. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  982. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  983. sock.bind(("127.0.0.1", 0))
  984. logger.debug("Missing MX")
  985. msg = '\r\n'.join([
  986. 'M-SEARCH * HTTP/1.1',
  987. 'HOST: 239.255.255.250:1900',
  988. 'MAN: "ssdp:discover"',
  989. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  990. '', ''])
  991. sock.sendto(msg, ("239.255.255.250", 1900))
  992. logger.debug("Negative MX")
  993. msg = '\r\n'.join([
  994. 'M-SEARCH * HTTP/1.1',
  995. 'HOST: 239.255.255.250:1900',
  996. 'MX: -1',
  997. 'MAN: "ssdp:discover"',
  998. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  999. '', ''])
  1000. sock.sendto(msg, ("239.255.255.250", 1900))
  1001. logger.debug("Invalid MX")
  1002. msg = '\r\n'.join([
  1003. 'M-SEARCH * HTTP/1.1',
  1004. 'HOST: 239.255.255.250:1900',
  1005. 'MX; 1',
  1006. 'MAN: "ssdp:discover"',
  1007. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1008. '', ''])
  1009. sock.sendto(msg, ("239.255.255.250", 1900))
  1010. logger.debug("Missing MAN")
  1011. msg = '\r\n'.join([
  1012. 'M-SEARCH * HTTP/1.1',
  1013. 'HOST: 239.255.255.250:1900',
  1014. 'MX: 1',
  1015. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1016. '', ''])
  1017. sock.sendto(msg, ("239.255.255.250", 1900))
  1018. logger.debug("Invalid MAN")
  1019. msg = '\r\n'.join([
  1020. 'M-SEARCH * HTTP/1.1',
  1021. 'HOST: 239.255.255.250:1900',
  1022. 'MX: 1',
  1023. 'MAN: foo',
  1024. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1025. '', ''])
  1026. sock.sendto(msg, ("239.255.255.250", 1900))
  1027. msg = '\r\n'.join([
  1028. 'M-SEARCH * HTTP/1.1',
  1029. 'HOST: 239.255.255.250:1900',
  1030. 'MX: 1',
  1031. 'MAN; "ssdp:discover"',
  1032. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1033. '', ''])
  1034. sock.sendto(msg, ("239.255.255.250", 1900))
  1035. logger.debug("Missing HOST")
  1036. msg = '\r\n'.join([
  1037. 'M-SEARCH * HTTP/1.1',
  1038. 'MAN: "ssdp:discover"',
  1039. 'MX: 1',
  1040. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1041. '', ''])
  1042. sock.sendto(msg, ("239.255.255.250", 1900))
  1043. logger.debug("Missing ST")
  1044. msg = '\r\n'.join([
  1045. 'M-SEARCH * HTTP/1.1',
  1046. 'HOST: 239.255.255.250:1900',
  1047. 'MAN: "ssdp:discover"',
  1048. 'MX: 1',
  1049. '', ''])
  1050. sock.sendto(msg, ("239.255.255.250", 1900))
  1051. logger.debug("Mismatching ST")
  1052. msg = '\r\n'.join([
  1053. 'M-SEARCH * HTTP/1.1',
  1054. 'HOST: 239.255.255.250:1900',
  1055. 'MAN: "ssdp:discover"',
  1056. 'MX: 1',
  1057. 'ST: uuid:16d5f8a9-4ee4-4f5e-81f9-cc6e2f47f42d',
  1058. '', ''])
  1059. sock.sendto(msg, ("239.255.255.250", 1900))
  1060. msg = '\r\n'.join([
  1061. 'M-SEARCH * HTTP/1.1',
  1062. 'HOST: 239.255.255.250:1900',
  1063. 'MAN: "ssdp:discover"',
  1064. 'MX: 1',
  1065. 'ST: foo:bar',
  1066. '', ''])
  1067. sock.sendto(msg, ("239.255.255.250", 1900))
  1068. msg = '\r\n'.join([
  1069. 'M-SEARCH * HTTP/1.1',
  1070. 'HOST: 239.255.255.250:1900',
  1071. 'MAN: "ssdp:discover"',
  1072. 'MX: 1',
  1073. 'ST: foobar',
  1074. '', ''])
  1075. sock.sendto(msg, ("239.255.255.250", 1900))
  1076. logger.debug("Invalid ST")
  1077. msg = '\r\n'.join([
  1078. 'M-SEARCH * HTTP/1.1',
  1079. 'HOST: 239.255.255.250:1900',
  1080. 'MAN: "ssdp:discover"',
  1081. 'MX: 1',
  1082. 'ST; urn:schemas-wifialliance-org:device:WFADevice:1',
  1083. '', ''])
  1084. sock.sendto(msg, ("239.255.255.250", 1900))
  1085. logger.debug("Invalid M-SEARCH")
  1086. msg = '\r\n'.join([
  1087. 'M+SEARCH * HTTP/1.1',
  1088. 'HOST: 239.255.255.250:1900',
  1089. 'MAN: "ssdp:discover"',
  1090. 'MX: 1',
  1091. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1092. '', ''])
  1093. sock.sendto(msg, ("239.255.255.250", 1900))
  1094. msg = '\r\n'.join([
  1095. 'M-SEARCH-* HTTP/1.1',
  1096. 'HOST: 239.255.255.250:1900',
  1097. 'MAN: "ssdp:discover"',
  1098. 'MX: 1',
  1099. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1100. '', ''])
  1101. sock.sendto(msg, ("239.255.255.250", 1900))
  1102. logger.debug("Invalid message format")
  1103. sock.sendto("NOTIFY * HTTP/1.1", ("239.255.255.250", 1900))
  1104. msg = '\r'.join([
  1105. 'M-SEARCH * HTTP/1.1',
  1106. 'HOST: 239.255.255.250:1900',
  1107. 'MAN: "ssdp:discover"',
  1108. 'MX: 1',
  1109. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1110. '', ''])
  1111. sock.sendto(msg, ("239.255.255.250", 1900))
  1112. try:
  1113. r = sock.recv(1000)
  1114. raise Exception("Unexpected M-SEARCH response: " + r)
  1115. except socket.timeout:
  1116. pass
  1117. logger.debug("Valid M-SEARCH")
  1118. msg = '\r\n'.join([
  1119. 'M-SEARCH * HTTP/1.1',
  1120. 'HOST: 239.255.255.250:1900',
  1121. 'MAN: "ssdp:discover"',
  1122. 'MX: 1',
  1123. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1124. '', ''])
  1125. sock.sendto(msg, ("239.255.255.250", 1900))
  1126. try:
  1127. r = sock.recv(1000)
  1128. pass
  1129. except socket.timeout:
  1130. raise Exception("No SSDP response")
  1131. def test_ap_wps_ssdp_burst(dev, apdev):
  1132. """WPS AP and SSDP burst"""
  1133. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1134. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1135. msg = '\r\n'.join([
  1136. 'M-SEARCH * HTTP/1.1',
  1137. 'HOST: 239.255.255.250:1900',
  1138. 'MAN: "ssdp:discover"',
  1139. 'MX: 1',
  1140. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1141. '', ''])
  1142. socket.setdefaulttimeout(1)
  1143. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  1144. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1145. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  1146. sock.bind(("127.0.0.1", 0))
  1147. for i in range(0, 25):
  1148. sock.sendto(msg, ("239.255.255.250", 1900))
  1149. resp = 0
  1150. while True:
  1151. try:
  1152. r = sock.recv(1000)
  1153. if not r.startswith("HTTP/1.1 200 OK\r\n"):
  1154. raise Exception("Unexpected message: " + r)
  1155. resp += 1
  1156. except socket.timeout:
  1157. break
  1158. if resp < 20:
  1159. raise Exception("Too few SSDP responses")
  1160. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  1161. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1162. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  1163. sock.bind(("127.0.0.1", 0))
  1164. for i in range(0, 25):
  1165. sock.sendto(msg, ("239.255.255.250", 1900))
  1166. while True:
  1167. try:
  1168. r = sock.recv(1000)
  1169. if ap_uuid in r:
  1170. break
  1171. except socket.timeout:
  1172. raise Exception("No SSDP response")