test_ap_wps.py 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  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 httplib
  13. import urlparse
  14. import urllib
  15. import xml.etree.ElementTree as ET
  16. import StringIO
  17. import hwsim_utils
  18. import hostapd
  19. def test_ap_wps_init(dev, apdev):
  20. """Initial AP configuration with first WPS Enrollee"""
  21. ssid = "test-wps"
  22. hostapd.add_ap(apdev[0]['ifname'],
  23. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  24. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  25. logger.info("WPS provisioning step")
  26. hapd.request("WPS_PBC")
  27. if "PBC Status: Active" not in hapd.request("WPS_GET_STATUS"):
  28. raise Exception("PBC status not shown correctly")
  29. dev[0].dump_monitor()
  30. dev[0].request("WPS_PBC")
  31. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  32. if ev is None:
  33. raise Exception("Association with the AP timed out")
  34. status = dev[0].get_status()
  35. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  36. raise Exception("Not fully connected")
  37. if status['ssid'] != ssid:
  38. raise Exception("Unexpected SSID")
  39. if status['pairwise_cipher'] != 'CCMP':
  40. raise Exception("Unexpected encryption configuration")
  41. if status['key_mgmt'] != 'WPA2-PSK':
  42. raise Exception("Unexpected key_mgmt")
  43. status = hapd.request("WPS_GET_STATUS")
  44. if "PBC Status: Disabled" not in status:
  45. raise Exception("PBC status not shown correctly")
  46. if "Last WPS result: Success" not in status:
  47. raise Exception("Last WPS result not shown correctly")
  48. if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
  49. raise Exception("Peer address not shown correctly")
  50. conf = hapd.request("GET_CONFIG")
  51. if "wps_state=configured" not in conf:
  52. raise Exception("AP not in WPS configured state")
  53. if "rsn_pairwise_cipher=CCMP TKIP" not in conf:
  54. raise Exception("Unexpected rsn_pairwise_cipher")
  55. if "wpa_pairwise_cipher=CCMP TKIP" not in conf:
  56. raise Exception("Unexpected wpa_pairwise_cipher")
  57. if "group_cipher=TKIP" not in conf:
  58. raise Exception("Unexpected group_cipher")
  59. def test_ap_wps_init_2ap_pbc(dev, apdev):
  60. """Initial two-radio AP configuration with first WPS PBC Enrollee"""
  61. ssid = "test-wps"
  62. params = { "ssid": ssid, "eap_server": "1", "wps_state": "1" }
  63. hostapd.add_ap(apdev[0]['ifname'], params)
  64. hostapd.add_ap(apdev[1]['ifname'], params)
  65. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  66. logger.info("WPS provisioning step")
  67. hapd.request("WPS_PBC")
  68. dev[0].scan(freq="2412")
  69. bss = dev[0].get_bss(apdev[0]['bssid'])
  70. if "[WPS-PBC]" not in bss['flags']:
  71. raise Exception("WPS-PBC flag missing from AP1")
  72. bss = dev[0].get_bss(apdev[1]['bssid'])
  73. if "[WPS-PBC]" not in bss['flags']:
  74. raise Exception("WPS-PBC flag missing from AP2")
  75. dev[0].dump_monitor()
  76. dev[0].request("WPS_PBC")
  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. dev[1].scan(freq="2412")
  81. bss = dev[1].get_bss(apdev[0]['bssid'])
  82. if "[WPS-PBC]" in bss['flags']:
  83. raise Exception("WPS-PBC flag not cleared from AP1")
  84. bss = dev[1].get_bss(apdev[1]['bssid'])
  85. if "[WPS-PBC]" in bss['flags']:
  86. raise Exception("WPS-PBC flag bit ckeared from AP2")
  87. def test_ap_wps_init_2ap_pin(dev, apdev):
  88. """Initial two-radio AP configuration with first WPS PIN Enrollee"""
  89. ssid = "test-wps"
  90. params = { "ssid": ssid, "eap_server": "1", "wps_state": "1" }
  91. hostapd.add_ap(apdev[0]['ifname'], params)
  92. hostapd.add_ap(apdev[1]['ifname'], params)
  93. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  94. logger.info("WPS provisioning step")
  95. pin = dev[0].wps_read_pin()
  96. hapd.request("WPS_PIN any " + pin)
  97. dev[0].scan(freq="2412")
  98. bss = dev[0].get_bss(apdev[0]['bssid'])
  99. if "[WPS-AUTH]" not in bss['flags']:
  100. raise Exception("WPS-AUTH flag missing from AP1")
  101. bss = dev[0].get_bss(apdev[1]['bssid'])
  102. if "[WPS-AUTH]" not in bss['flags']:
  103. raise Exception("WPS-AUTH flag missing from AP2")
  104. dev[0].dump_monitor()
  105. dev[0].request("WPS_PIN any " + pin)
  106. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  107. if ev is None:
  108. raise Exception("Association with the AP timed out")
  109. dev[1].scan(freq="2412")
  110. bss = dev[1].get_bss(apdev[0]['bssid'])
  111. if "[WPS-AUTH]" in bss['flags']:
  112. raise Exception("WPS-AUTH flag not cleared from AP1")
  113. bss = dev[1].get_bss(apdev[1]['bssid'])
  114. if "[WPS-AUTH]" in bss['flags']:
  115. raise Exception("WPS-AUTH flag bit ckeared from AP2")
  116. def test_ap_wps_init_through_wps_config(dev, apdev):
  117. """Initial AP configuration using wps_config command"""
  118. ssid = "test-wps-init-config"
  119. hostapd.add_ap(apdev[0]['ifname'],
  120. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  121. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  122. if "FAIL" in hapd.request("WPS_CONFIG " + ssid.encode("hex") + " WPA2PSK CCMP " + "12345678".encode("hex")):
  123. raise Exception("WPS_CONFIG command failed")
  124. ev = hapd.wait_event(["WPS-NEW-AP-SETTINGS"], timeout=5)
  125. if ev is None:
  126. raise Exception("Timeout on WPS-NEW-AP-SETTINGS events")
  127. # It takes some time for the AP to update Beacon and Probe Response frames,
  128. # so wait here before requesting the scan to be started to avoid adding
  129. # extra five second wait to the test due to fetching obsolete scan results.
  130. hapd.ping()
  131. time.sleep(0.2)
  132. dev[0].connect(ssid, psk="12345678", scan_freq="2412", proto="WPA2",
  133. pairwise="CCMP", group="CCMP")
  134. def test_ap_wps_conf(dev, apdev):
  135. """WPS PBC provisioning with configured AP"""
  136. ssid = "test-wps-conf"
  137. hostapd.add_ap(apdev[0]['ifname'],
  138. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  139. "wpa_passphrase": "12345678", "wpa": "2",
  140. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  141. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  142. logger.info("WPS provisioning step")
  143. hapd.request("WPS_PBC")
  144. dev[0].dump_monitor()
  145. dev[0].request("WPS_PBC")
  146. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  147. if ev is None:
  148. raise Exception("Association with the AP timed out")
  149. status = dev[0].get_status()
  150. if status['wpa_state'] != 'COMPLETED':
  151. raise Exception("Not fully connected")
  152. if status['bssid'] != apdev[0]['bssid']:
  153. raise Exception("Unexpected BSSID")
  154. if status['ssid'] != ssid:
  155. raise Exception("Unexpected SSID")
  156. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  157. raise Exception("Unexpected encryption configuration")
  158. if status['key_mgmt'] != 'WPA2-PSK':
  159. raise Exception("Unexpected key_mgmt")
  160. sta = hapd.get_sta(dev[0].p2p_interface_addr())
  161. if 'wpsDeviceName' not in sta or sta['wpsDeviceName'] != "Device A":
  162. raise Exception("Device name not available in STA command")
  163. def test_ap_wps_twice(dev, apdev):
  164. """WPS provisioning with twice to change passphrase"""
  165. ssid = "test-wps-twice"
  166. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  167. "wpa_passphrase": "12345678", "wpa": "2",
  168. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" }
  169. hostapd.add_ap(apdev[0]['ifname'], params)
  170. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  171. logger.info("WPS provisioning step")
  172. hapd.request("WPS_PBC")
  173. dev[0].dump_monitor()
  174. dev[0].request("WPS_PBC")
  175. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  176. if ev is None:
  177. raise Exception("Association with the AP timed out")
  178. dev[0].request("DISCONNECT")
  179. logger.info("Restart AP with different passphrase and re-run WPS")
  180. hapd_global = hostapd.HostapdGlobal()
  181. hapd_global.remove(apdev[0]['ifname'])
  182. params['wpa_passphrase'] = 'another passphrase'
  183. hostapd.add_ap(apdev[0]['ifname'], params)
  184. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  185. logger.info("WPS provisioning step")
  186. hapd.request("WPS_PBC")
  187. dev[0].dump_monitor()
  188. dev[0].request("WPS_PBC")
  189. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  190. if ev is None:
  191. raise Exception("Association with the AP timed out")
  192. networks = dev[0].list_networks()
  193. if len(networks) > 1:
  194. raise Exception("Unexpected duplicated network block present")
  195. def test_ap_wps_incorrect_pin(dev, apdev):
  196. """WPS PIN provisioning with incorrect PIN"""
  197. ssid = "test-wps-incorrect-pin"
  198. hostapd.add_ap(apdev[0]['ifname'],
  199. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  200. "wpa_passphrase": "12345678", "wpa": "2",
  201. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  202. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  203. logger.info("WPS provisioning attempt 1")
  204. hapd.request("WPS_PIN any 12345670")
  205. dev[0].dump_monitor()
  206. dev[0].request("WPS_PIN any 55554444")
  207. ev = dev[0].wait_event(["WPS-FAIL"], timeout=30)
  208. if ev is None:
  209. raise Exception("WPS operation timed out")
  210. if "config_error=18" not in ev:
  211. raise Exception("Incorrect config_error reported")
  212. if "msg=8" not in ev:
  213. raise Exception("PIN error detected on incorrect message")
  214. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  215. if ev is None:
  216. raise Exception("Timeout on disconnection event")
  217. dev[0].request("WPS_CANCEL")
  218. # if a scan was in progress, wait for it to complete before trying WPS again
  219. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  220. status = hapd.request("WPS_GET_STATUS")
  221. if "Last WPS result: Failed" not in status:
  222. raise Exception("WPS failure result not shown correctly")
  223. logger.info("WPS provisioning attempt 2")
  224. hapd.request("WPS_PIN any 12345670")
  225. dev[0].dump_monitor()
  226. dev[0].request("WPS_PIN any 12344444")
  227. ev = dev[0].wait_event(["WPS-FAIL"], timeout=30)
  228. if ev is None:
  229. raise Exception("WPS operation timed out")
  230. if "config_error=18" not in ev:
  231. raise Exception("Incorrect config_error reported")
  232. if "msg=10" not in ev:
  233. raise Exception("PIN error detected on incorrect message")
  234. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  235. if ev is None:
  236. raise Exception("Timeout on disconnection event")
  237. def test_ap_wps_conf_pin(dev, apdev):
  238. """WPS PIN provisioning with configured AP"""
  239. ssid = "test-wps-conf-pin"
  240. hostapd.add_ap(apdev[0]['ifname'],
  241. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  242. "wpa_passphrase": "12345678", "wpa": "2",
  243. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  244. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  245. logger.info("WPS provisioning step")
  246. pin = dev[0].wps_read_pin()
  247. hapd.request("WPS_PIN any " + pin)
  248. dev[0].dump_monitor()
  249. dev[0].request("WPS_PIN any " + pin)
  250. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  251. if ev is None:
  252. raise Exception("Association with the AP timed out")
  253. status = dev[0].get_status()
  254. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  255. raise Exception("Not fully connected")
  256. if status['ssid'] != ssid:
  257. raise Exception("Unexpected SSID")
  258. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  259. raise Exception("Unexpected encryption configuration")
  260. if status['key_mgmt'] != 'WPA2-PSK':
  261. raise Exception("Unexpected key_mgmt")
  262. dev[1].scan(freq="2412")
  263. bss = dev[1].get_bss(apdev[0]['bssid'])
  264. if "[WPS-AUTH]" in bss['flags']:
  265. raise Exception("WPS-AUTH flag not cleared")
  266. logger.info("Try to connect from another station using the same PIN")
  267. dev[1].request("WPS_PIN any " + pin)
  268. ev = dev[1].wait_event(["WPS-M2D","CTRL-EVENT-CONNECTED"], timeout=30)
  269. if ev is None:
  270. raise Exception("Operation timed out")
  271. if "WPS-M2D" not in ev:
  272. raise Exception("Unexpected WPS operation started")
  273. def test_ap_wps_conf_pin_2sta(dev, apdev):
  274. """Two stations trying to use WPS PIN at the same time"""
  275. ssid = "test-wps-conf-pin2"
  276. hostapd.add_ap(apdev[0]['ifname'],
  277. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  278. "wpa_passphrase": "12345678", "wpa": "2",
  279. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  280. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  281. logger.info("WPS provisioning step")
  282. pin = "12345670"
  283. pin2 = "55554444"
  284. hapd.request("WPS_PIN " + dev[0].get_status_field("uuid") + " " + pin)
  285. hapd.request("WPS_PIN " + dev[1].get_status_field("uuid") + " " + pin)
  286. dev[0].dump_monitor()
  287. dev[1].dump_monitor()
  288. dev[0].request("WPS_PIN any " + pin)
  289. dev[1].request("WPS_PIN any " + pin)
  290. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  291. if ev is None:
  292. raise Exception("Association with the AP timed out")
  293. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  294. if ev is None:
  295. raise Exception("Association with the AP timed out")
  296. def test_ap_wps_reg_connect(dev, apdev):
  297. """WPS registrar using AP PIN to connect"""
  298. ssid = "test-wps-reg-ap-pin"
  299. appin = "12345670"
  300. hostapd.add_ap(apdev[0]['ifname'],
  301. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  302. "wpa_passphrase": "12345678", "wpa": "2",
  303. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  304. "ap_pin": appin})
  305. logger.info("WPS provisioning step")
  306. dev[0].dump_monitor()
  307. dev[0].wps_reg(apdev[0]['bssid'], appin)
  308. status = dev[0].get_status()
  309. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  310. raise Exception("Not fully connected")
  311. if status['ssid'] != ssid:
  312. raise Exception("Unexpected SSID")
  313. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  314. raise Exception("Unexpected encryption configuration")
  315. if status['key_mgmt'] != 'WPA2-PSK':
  316. raise Exception("Unexpected key_mgmt")
  317. def check_wps_reg_failure(dev, ap, appin):
  318. dev.request("WPS_REG " + ap['bssid'] + " " + appin)
  319. ev = dev.wait_event(["WPS-SUCCESS", "WPS-FAIL"], timeout=15)
  320. if ev is None:
  321. raise Exception("WPS operation timed out")
  322. if "WPS-SUCCESS" in ev:
  323. raise Exception("WPS operation succeeded unexpectedly")
  324. if "config_error=15" not in ev:
  325. raise Exception("WPS setup locked state was not reported correctly")
  326. def test_ap_wps_random_ap_pin(dev, apdev):
  327. """WPS registrar using random AP PIN"""
  328. ssid = "test-wps-reg-random-ap-pin"
  329. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  330. hostapd.add_ap(apdev[0]['ifname'],
  331. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  332. "wpa_passphrase": "12345678", "wpa": "2",
  333. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  334. "device_name": "Wireless AP", "manufacturer": "Company",
  335. "model_name": "WAP", "model_number": "123",
  336. "serial_number": "12345", "device_type": "6-0050F204-1",
  337. "os_version": "01020300",
  338. "config_methods": "label push_button",
  339. "uuid": ap_uuid, "upnp_iface": "lo" })
  340. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  341. appin = hapd.request("WPS_AP_PIN random")
  342. if "FAIL" in appin:
  343. raise Exception("Could not generate random AP PIN")
  344. if appin not in hapd.request("WPS_AP_PIN get"):
  345. raise Exception("Could not fetch current AP PIN")
  346. logger.info("WPS provisioning step")
  347. dev[0].wps_reg(apdev[0]['bssid'], appin)
  348. hapd.request("WPS_AP_PIN disable")
  349. logger.info("WPS provisioning step with AP PIN disabled")
  350. check_wps_reg_failure(dev[1], apdev[0], appin)
  351. logger.info("WPS provisioning step with AP PIN reset")
  352. appin = "12345670"
  353. hapd.request("WPS_AP_PIN set " + appin)
  354. dev[1].wps_reg(apdev[0]['bssid'], appin)
  355. dev[0].request("REMOVE_NETWORK all")
  356. dev[1].request("REMOVE_NETWORK all")
  357. dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  358. dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
  359. logger.info("WPS provisioning step after AP PIN timeout")
  360. hapd.request("WPS_AP_PIN disable")
  361. appin = hapd.request("WPS_AP_PIN random 1")
  362. time.sleep(1.1)
  363. if "FAIL" not in hapd.request("WPS_AP_PIN get"):
  364. raise Exception("AP PIN unexpectedly still enabled")
  365. check_wps_reg_failure(dev[0], apdev[0], appin)
  366. logger.info("WPS provisioning step after AP PIN timeout(2)")
  367. hapd.request("WPS_AP_PIN disable")
  368. appin = "12345670"
  369. hapd.request("WPS_AP_PIN set " + appin + " 1")
  370. time.sleep(1.1)
  371. if "FAIL" not in hapd.request("WPS_AP_PIN get"):
  372. raise Exception("AP PIN unexpectedly still enabled")
  373. check_wps_reg_failure(dev[1], apdev[0], appin)
  374. def test_ap_wps_reg_config(dev, apdev):
  375. """WPS registrar configuring and AP using AP PIN"""
  376. ssid = "test-wps-init-ap-pin"
  377. appin = "12345670"
  378. hostapd.add_ap(apdev[0]['ifname'],
  379. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  380. "ap_pin": appin})
  381. logger.info("WPS configuration step")
  382. dev[0].dump_monitor()
  383. new_ssid = "wps-new-ssid"
  384. new_passphrase = "1234567890"
  385. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPA2PSK", "CCMP",
  386. new_passphrase)
  387. status = dev[0].get_status()
  388. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  389. raise Exception("Not fully connected")
  390. if status['ssid'] != new_ssid:
  391. raise Exception("Unexpected SSID")
  392. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  393. raise Exception("Unexpected encryption configuration")
  394. if status['key_mgmt'] != 'WPA2-PSK':
  395. raise Exception("Unexpected key_mgmt")
  396. def test_ap_wps_reg_config_tkip(dev, apdev):
  397. """WPS registrar configuring AP to use TKIP and AP upgrading to TKIP+CCMP"""
  398. ssid = "test-wps-init-ap"
  399. appin = "12345670"
  400. hostapd.add_ap(apdev[0]['ifname'],
  401. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  402. "ap_pin": appin})
  403. logger.info("WPS configuration step")
  404. dev[0].request("SET wps_version_number 0x10")
  405. dev[0].dump_monitor()
  406. new_ssid = "wps-new-ssid-with-tkip"
  407. new_passphrase = "1234567890"
  408. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPAPSK", "TKIP",
  409. new_passphrase)
  410. logger.info("Re-connect to verify WPA2 mixed mode")
  411. dev[0].request("DISCONNECT")
  412. id = 0
  413. dev[0].set_network(id, "pairwise", "CCMP")
  414. dev[0].set_network(id, "proto", "RSN")
  415. dev[0].connect_network(id)
  416. status = dev[0].get_status()
  417. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  418. raise Exception("Not fully connected")
  419. if status['ssid'] != new_ssid:
  420. raise Exception("Unexpected SSID")
  421. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  422. raise Exception("Unexpected encryption configuration")
  423. if status['key_mgmt'] != 'WPA2-PSK':
  424. raise Exception("Unexpected key_mgmt")
  425. def test_ap_wps_setup_locked(dev, apdev):
  426. """WPS registrar locking up AP setup on AP PIN failures"""
  427. ssid = "test-wps-incorrect-ap-pin"
  428. appin = "12345670"
  429. hostapd.add_ap(apdev[0]['ifname'],
  430. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  431. "wpa_passphrase": "12345678", "wpa": "2",
  432. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  433. "ap_pin": appin})
  434. new_ssid = "wps-new-ssid-test"
  435. new_passphrase = "1234567890"
  436. ap_setup_locked=False
  437. for pin in ["55554444", "1234", "12345678", "00000000", "11111111"]:
  438. dev[0].dump_monitor()
  439. logger.info("Try incorrect AP PIN - attempt " + pin)
  440. dev[0].wps_reg(apdev[0]['bssid'], pin, new_ssid, "WPA2PSK",
  441. "CCMP", new_passphrase, no_wait=True)
  442. ev = dev[0].wait_event(["WPS-FAIL", "CTRL-EVENT-CONNECTED"])
  443. if ev is None:
  444. raise Exception("Timeout on receiving WPS operation failure event")
  445. if "CTRL-EVENT-CONNECTED" in ev:
  446. raise Exception("Unexpected connection")
  447. if "config_error=15" in ev:
  448. logger.info("AP Setup Locked")
  449. ap_setup_locked=True
  450. elif "config_error=18" not in ev:
  451. raise Exception("config_error=18 not reported")
  452. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  453. if ev is None:
  454. raise Exception("Timeout on disconnection event")
  455. time.sleep(0.1)
  456. if not ap_setup_locked:
  457. raise Exception("AP setup was not locked")
  458. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  459. status = hapd.request("WPS_GET_STATUS")
  460. if "Last WPS result: Failed" not in status:
  461. raise Exception("WPS failure result not shown correctly")
  462. if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
  463. raise Exception("Peer address not shown correctly")
  464. time.sleep(0.5)
  465. dev[0].dump_monitor()
  466. logger.info("WPS provisioning step")
  467. pin = dev[0].wps_read_pin()
  468. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  469. hapd.request("WPS_PIN any " + pin)
  470. dev[0].request("WPS_PIN any " + pin)
  471. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=30)
  472. if ev is None:
  473. raise Exception("WPS success was not reported")
  474. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  475. if ev is None:
  476. raise Exception("Association with the AP timed out")
  477. def test_ap_wps_pbc_overlap_2ap(dev, apdev):
  478. """WPS PBC session overlap with two active APs"""
  479. hostapd.add_ap(apdev[0]['ifname'],
  480. { "ssid": "wps1", "eap_server": "1", "wps_state": "2",
  481. "wpa_passphrase": "12345678", "wpa": "2",
  482. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  483. "wps_independent": "1"})
  484. hostapd.add_ap(apdev[1]['ifname'],
  485. { "ssid": "wps2", "eap_server": "1", "wps_state": "2",
  486. "wpa_passphrase": "123456789", "wpa": "2",
  487. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  488. "wps_independent": "1"})
  489. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  490. hapd.request("WPS_PBC")
  491. hapd2 = hostapd.Hostapd(apdev[1]['ifname'])
  492. hapd2.request("WPS_PBC")
  493. logger.info("WPS provisioning step")
  494. dev[0].dump_monitor()
  495. dev[0].request("WPS_PBC")
  496. ev = dev[0].wait_event(["WPS-OVERLAP-DETECTED"], timeout=15)
  497. if ev is None:
  498. raise Exception("PBC session overlap not detected")
  499. def test_ap_wps_pbc_overlap_2sta(dev, apdev):
  500. """WPS PBC session overlap with two active STAs"""
  501. ssid = "test-wps-pbc-overlap"
  502. hostapd.add_ap(apdev[0]['ifname'],
  503. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  504. "wpa_passphrase": "12345678", "wpa": "2",
  505. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  506. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  507. logger.info("WPS provisioning step")
  508. hapd.request("WPS_PBC")
  509. dev[0].dump_monitor()
  510. dev[1].dump_monitor()
  511. dev[0].request("WPS_PBC")
  512. dev[1].request("WPS_PBC")
  513. ev = dev[0].wait_event(["WPS-M2D"], timeout=15)
  514. if ev is None:
  515. raise Exception("PBC session overlap not detected (dev0)")
  516. if "config_error=12" not in ev:
  517. raise Exception("PBC session overlap not correctly reported (dev0)")
  518. ev = dev[1].wait_event(["WPS-M2D"], timeout=15)
  519. if ev is None:
  520. raise Exception("PBC session overlap not detected (dev1)")
  521. if "config_error=12" not in ev:
  522. raise Exception("PBC session overlap not correctly reported (dev1)")
  523. hapd.request("WPS_CANCEL")
  524. ret = hapd.request("WPS_PBC")
  525. if "FAIL" not in ret:
  526. raise Exception("PBC mode allowed to be started while PBC overlap still active")
  527. def test_ap_wps_cancel(dev, apdev):
  528. """WPS AP cancelling enabled config method"""
  529. ssid = "test-wps-ap-cancel"
  530. hostapd.add_ap(apdev[0]['ifname'],
  531. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  532. "wpa_passphrase": "12345678", "wpa": "2",
  533. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  534. bssid = apdev[0]['bssid']
  535. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  536. logger.info("Verify PBC enable/cancel")
  537. hapd.request("WPS_PBC")
  538. dev[0].scan(freq="2412")
  539. bss = dev[0].get_bss(apdev[0]['bssid'])
  540. if "[WPS-PBC]" not in bss['flags']:
  541. raise Exception("WPS-PBC flag missing")
  542. if "FAIL" in hapd.request("WPS_CANCEL"):
  543. raise Exception("WPS_CANCEL failed")
  544. dev[0].scan(freq="2412")
  545. bss = dev[0].get_bss(apdev[0]['bssid'])
  546. if "[WPS-PBC]" in bss['flags']:
  547. raise Exception("WPS-PBC flag not cleared")
  548. logger.info("Verify PIN enable/cancel")
  549. hapd.request("WPS_PIN any 12345670")
  550. dev[0].scan(freq="2412")
  551. bss = dev[0].get_bss(apdev[0]['bssid'])
  552. if "[WPS-AUTH]" not in bss['flags']:
  553. raise Exception("WPS-AUTH flag missing")
  554. if "FAIL" in hapd.request("WPS_CANCEL"):
  555. raise Exception("WPS_CANCEL failed")
  556. dev[0].scan(freq="2412")
  557. bss = dev[0].get_bss(apdev[0]['bssid'])
  558. if "[WPS-AUTH]" in bss['flags']:
  559. raise Exception("WPS-AUTH flag not cleared")
  560. def test_ap_wps_er_add_enrollee(dev, apdev):
  561. """WPS ER configuring AP and adding a new enrollee using PIN"""
  562. ssid = "wps-er-add-enrollee"
  563. ap_pin = "12345670"
  564. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  565. hostapd.add_ap(apdev[0]['ifname'],
  566. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  567. "device_name": "Wireless AP", "manufacturer": "Company",
  568. "model_name": "WAP", "model_number": "123",
  569. "serial_number": "12345", "device_type": "6-0050F204-1",
  570. "os_version": "01020300",
  571. "config_methods": "label push_button",
  572. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  573. logger.info("WPS configuration step")
  574. new_passphrase = "1234567890"
  575. dev[0].dump_monitor()
  576. dev[0].wps_reg(apdev[0]['bssid'], ap_pin, ssid, "WPA2PSK", "CCMP",
  577. new_passphrase)
  578. status = dev[0].get_status()
  579. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  580. raise Exception("Not fully connected")
  581. if status['ssid'] != ssid:
  582. raise Exception("Unexpected SSID")
  583. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  584. raise Exception("Unexpected encryption configuration")
  585. if status['key_mgmt'] != 'WPA2-PSK':
  586. raise Exception("Unexpected key_mgmt")
  587. logger.info("Start ER")
  588. dev[0].request("WPS_ER_START ifname=lo")
  589. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  590. if ev is None:
  591. raise Exception("AP discovery timed out")
  592. if ap_uuid not in ev:
  593. raise Exception("Expected AP UUID not found")
  594. logger.info("Learn AP configuration through UPnP")
  595. dev[0].dump_monitor()
  596. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  597. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  598. if ev is None:
  599. raise Exception("AP learn timed out")
  600. if ap_uuid not in ev:
  601. raise Exception("Expected AP UUID not in settings")
  602. if "ssid=" + ssid not in ev:
  603. raise Exception("Expected SSID not in settings")
  604. if "key=" + new_passphrase not in ev:
  605. raise Exception("Expected passphrase not in settings")
  606. logger.info("Add Enrollee using ER")
  607. pin = dev[1].wps_read_pin()
  608. dev[0].dump_monitor()
  609. dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
  610. dev[1].dump_monitor()
  611. dev[1].request("WPS_PIN any " + pin)
  612. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=30)
  613. if ev is None:
  614. raise Exception("Enrollee did not report success")
  615. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  616. if ev is None:
  617. raise Exception("Association with the AP timed out")
  618. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  619. if ev is None:
  620. raise Exception("WPS ER did not report success")
  621. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  622. logger.info("Add a specific Enrollee using ER")
  623. pin = dev[2].wps_read_pin()
  624. addr2 = dev[2].p2p_interface_addr()
  625. dev[0].dump_monitor()
  626. dev[2].dump_monitor()
  627. dev[2].request("WPS_PIN any " + pin)
  628. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=10)
  629. if ev is None:
  630. raise Exception("Enrollee not seen")
  631. if addr2 not in ev:
  632. raise Exception("Unexpected Enrollee MAC address")
  633. dev[0].request("WPS_ER_PIN " + addr2 + " " + pin + " " + addr2)
  634. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  635. if ev is None:
  636. raise Exception("Association with the AP timed out")
  637. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  638. if ev is None:
  639. raise Exception("WPS ER did not report success")
  640. logger.info("Verify registrar selection behavior")
  641. dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
  642. dev[1].request("DISCONNECT")
  643. dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
  644. dev[1].scan(freq="2412")
  645. bss = dev[1].get_bss(apdev[0]['bssid'])
  646. if "[WPS-AUTH]" not in bss['flags']:
  647. raise Exception("WPS-AUTH flag missing")
  648. logger.info("Stop ER")
  649. dev[0].dump_monitor()
  650. dev[0].request("WPS_ER_STOP")
  651. ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"])
  652. if ev is None:
  653. raise Exception("WPS ER unsubscription timed out")
  654. # It takes some time for the UPnP UNSUBSCRIBE command to go through, so wait
  655. # a bit before verifying that the scan results have change.
  656. time.sleep(0.2)
  657. dev[1].scan(freq="2412")
  658. bss = dev[1].get_bss(apdev[0]['bssid'])
  659. if "[WPS-AUTH]" in bss['flags']:
  660. raise Exception("WPS-AUTH flag not removed")
  661. def test_ap_wps_er_add_enrollee_pbc(dev, apdev):
  662. """WPS ER connected to AP and adding a new enrollee using PBC"""
  663. ssid = "wps-er-add-enrollee-pbc"
  664. ap_pin = "12345670"
  665. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  666. hostapd.add_ap(apdev[0]['ifname'],
  667. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  668. "wpa_passphrase": "12345678", "wpa": "2",
  669. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  670. "device_name": "Wireless AP", "manufacturer": "Company",
  671. "model_name": "WAP", "model_number": "123",
  672. "serial_number": "12345", "device_type": "6-0050F204-1",
  673. "os_version": "01020300",
  674. "config_methods": "label push_button",
  675. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  676. logger.info("Learn AP configuration")
  677. dev[0].dump_monitor()
  678. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  679. status = dev[0].get_status()
  680. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  681. raise Exception("Not fully connected")
  682. logger.info("Start ER")
  683. dev[0].request("WPS_ER_START ifname=lo")
  684. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  685. if ev is None:
  686. raise Exception("AP discovery timed out")
  687. if ap_uuid not in ev:
  688. raise Exception("Expected AP UUID not found")
  689. logger.info("Use learned network configuration on ER")
  690. dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  691. logger.info("Add Enrollee using ER and PBC")
  692. dev[0].dump_monitor()
  693. enrollee = dev[1].p2p_interface_addr()
  694. dev[1].dump_monitor()
  695. dev[1].request("WPS_PBC")
  696. for i in range(0, 2):
  697. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=15)
  698. if ev is None:
  699. raise Exception("Enrollee discovery timed out")
  700. if enrollee in ev:
  701. break
  702. if i == 1:
  703. raise Exception("Expected Enrollee not found")
  704. dev[0].request("WPS_ER_PBC " + enrollee)
  705. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=15)
  706. if ev is None:
  707. raise Exception("Enrollee did not report success")
  708. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  709. if ev is None:
  710. raise Exception("Association with the AP timed out")
  711. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  712. if ev is None:
  713. raise Exception("WPS ER did not report success")
  714. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  715. # verify BSSID selection of the AP instead of UUID
  716. if "FAIL" in dev[0].request("WPS_ER_SET_CONFIG " + apdev[0]['bssid'] + " 0"):
  717. raise Exception("Could not select AP based on BSSID")
  718. def test_ap_wps_er_v10_add_enrollee_pin(dev, apdev):
  719. """WPS v1.0 ER connected to AP and adding a new enrollee using PIN"""
  720. ssid = "wps-er-add-enrollee-pbc"
  721. ap_pin = "12345670"
  722. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  723. hostapd.add_ap(apdev[0]['ifname'],
  724. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  725. "wpa_passphrase": "12345678", "wpa": "2",
  726. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  727. "device_name": "Wireless AP", "manufacturer": "Company",
  728. "model_name": "WAP", "model_number": "123",
  729. "serial_number": "12345", "device_type": "6-0050F204-1",
  730. "os_version": "01020300",
  731. "config_methods": "label push_button",
  732. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  733. logger.info("Learn AP configuration")
  734. dev[0].request("SET wps_version_number 0x10")
  735. dev[0].dump_monitor()
  736. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  737. status = dev[0].get_status()
  738. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  739. raise Exception("Not fully connected")
  740. logger.info("Start ER")
  741. dev[0].request("WPS_ER_START ifname=lo")
  742. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  743. if ev is None:
  744. raise Exception("AP discovery timed out")
  745. if ap_uuid not in ev:
  746. raise Exception("Expected AP UUID not found")
  747. logger.info("Use learned network configuration on ER")
  748. dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  749. logger.info("Add Enrollee using ER and PIN")
  750. enrollee = dev[1].p2p_interface_addr()
  751. pin = dev[1].wps_read_pin()
  752. dev[0].dump_monitor()
  753. dev[0].request("WPS_ER_PIN any " + pin + " " + enrollee)
  754. dev[1].dump_monitor()
  755. dev[1].request("WPS_PIN any " + pin)
  756. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  757. if ev is None:
  758. raise Exception("Association with the AP timed out")
  759. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  760. if ev is None:
  761. raise Exception("WPS ER did not report success")
  762. def test_ap_wps_er_config_ap(dev, apdev):
  763. """WPS ER configuring AP over UPnP"""
  764. ssid = "wps-er-ap-config"
  765. ap_pin = "12345670"
  766. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  767. hostapd.add_ap(apdev[0]['ifname'],
  768. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  769. "wpa_passphrase": "12345678", "wpa": "2",
  770. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  771. "device_name": "Wireless AP", "manufacturer": "Company",
  772. "model_name": "WAP", "model_number": "123",
  773. "serial_number": "12345", "device_type": "6-0050F204-1",
  774. "os_version": "01020300",
  775. "config_methods": "label push_button",
  776. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  777. logger.info("Connect ER to the AP")
  778. dev[0].connect(ssid, psk="12345678", scan_freq="2412")
  779. logger.info("WPS configuration step")
  780. dev[0].request("WPS_ER_START ifname=lo")
  781. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  782. if ev is None:
  783. raise Exception("AP discovery timed out")
  784. if ap_uuid not in ev:
  785. raise Exception("Expected AP UUID not found")
  786. new_passphrase = "1234567890"
  787. dev[0].request("WPS_ER_CONFIG " + apdev[0]['bssid'] + " " + ap_pin + " " +
  788. ssid.encode("hex") + " WPA2PSK CCMP " +
  789. new_passphrase.encode("hex"))
  790. ev = dev[0].wait_event(["WPS-SUCCESS"])
  791. if ev is None:
  792. raise Exception("WPS ER configuration operation timed out")
  793. dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
  794. dev[0].connect(ssid, psk="1234567890", scan_freq="2412")
  795. def test_ap_wps_fragmentation(dev, apdev):
  796. """WPS with fragmentation in EAP-WSC and mixed mode WPA+WPA2"""
  797. ssid = "test-wps-fragmentation"
  798. hostapd.add_ap(apdev[0]['ifname'],
  799. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  800. "wpa_passphrase": "12345678", "wpa": "3",
  801. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  802. "wpa_pairwise": "TKIP",
  803. "fragment_size": "50" })
  804. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  805. logger.info("WPS provisioning step")
  806. hapd.request("WPS_PBC")
  807. dev[0].dump_monitor()
  808. dev[0].request("SET wps_fragment_size 50")
  809. dev[0].request("WPS_PBC")
  810. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  811. if ev is None:
  812. raise Exception("Association with the AP timed out")
  813. status = dev[0].get_status()
  814. if status['wpa_state'] != 'COMPLETED':
  815. raise Exception("Not fully connected")
  816. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  817. raise Exception("Unexpected encryption configuration")
  818. if status['key_mgmt'] != 'WPA2-PSK':
  819. raise Exception("Unexpected key_mgmt")
  820. def test_ap_wps_new_version_sta(dev, apdev):
  821. """WPS compatibility with new version number on the station"""
  822. ssid = "test-wps-ver"
  823. hostapd.add_ap(apdev[0]['ifname'],
  824. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  825. "wpa_passphrase": "12345678", "wpa": "2",
  826. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  827. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  828. logger.info("WPS provisioning step")
  829. hapd.request("WPS_PBC")
  830. dev[0].dump_monitor()
  831. dev[0].request("SET wps_version_number 0x43")
  832. dev[0].request("SET wps_vendor_ext_m1 000137100100020001")
  833. dev[0].request("WPS_PBC")
  834. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  835. if ev is None:
  836. raise Exception("Association with the AP timed out")
  837. def test_ap_wps_new_version_ap(dev, apdev):
  838. """WPS compatibility with new version number on the AP"""
  839. ssid = "test-wps-ver"
  840. hostapd.add_ap(apdev[0]['ifname'],
  841. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  842. "wpa_passphrase": "12345678", "wpa": "2",
  843. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  844. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  845. logger.info("WPS provisioning step")
  846. if "FAIL" in hapd.request("SET wps_version_number 0x43"):
  847. raise Exception("Failed to enable test functionality")
  848. hapd.request("WPS_PBC")
  849. dev[0].dump_monitor()
  850. dev[0].request("WPS_PBC")
  851. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  852. hapd.request("SET wps_version_number 0x20")
  853. if ev is None:
  854. raise Exception("Association with the AP timed out")
  855. def test_ap_wps_check_pin(dev, apdev):
  856. """Verify PIN checking through control interface"""
  857. hostapd.add_ap(apdev[0]['ifname'],
  858. { "ssid": "wps", "eap_server": "1", "wps_state": "2",
  859. "wpa_passphrase": "12345678", "wpa": "2",
  860. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  861. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  862. for t in [ ("12345670", "12345670"),
  863. ("12345678", "FAIL-CHECKSUM"),
  864. ("1234-5670", "12345670"),
  865. ("1234 5670", "12345670"),
  866. ("1-2.3:4 5670", "12345670") ]:
  867. res = hapd.request("WPS_CHECK_PIN " + t[0]).rstrip('\n')
  868. res2 = dev[0].request("WPS_CHECK_PIN " + t[0]).rstrip('\n')
  869. if res != res2:
  870. raise Exception("Unexpected difference in WPS_CHECK_PIN responses")
  871. if res != t[1]:
  872. raise Exception("Incorrect WPS_CHECK_PIN response {} (expected {})".format(res, t[1]))
  873. def test_ap_wps_wep_config(dev, apdev):
  874. """WPS 2.0 AP rejecting WEP configuration"""
  875. ssid = "test-wps-config"
  876. appin = "12345670"
  877. hostapd.add_ap(apdev[0]['ifname'],
  878. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  879. "ap_pin": appin})
  880. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  881. dev[0].wps_reg(apdev[0]['bssid'], appin, "wps-new-ssid-wep", "OPEN", "WEP",
  882. "hello", no_wait=True)
  883. ev = hapd.wait_event(["WPS-FAIL"], timeout=15)
  884. if ev is None:
  885. raise Exception("WPS-FAIL timed out")
  886. if "reason=2" not in ev:
  887. raise Exception("Unexpected reason code in WPS-FAIL")
  888. status = hapd.request("WPS_GET_STATUS")
  889. if "Last WPS result: Failed" not in status:
  890. raise Exception("WPS failure result not shown correctly")
  891. if "Failure Reason: WEP Prohibited" not in status:
  892. raise Exception("Failure reason not reported correctly")
  893. if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
  894. raise Exception("Peer address not shown correctly")
  895. def test_ap_wps_wep_enroll(dev, apdev):
  896. """WPS 2.0 STA rejecting WEP configuration"""
  897. ssid = "test-wps-wep"
  898. hostapd.add_ap(apdev[0]['ifname'],
  899. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  900. "skip_cred_build": "1", "extra_cred": "wps-wep-cred" })
  901. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  902. hapd.request("WPS_PBC")
  903. dev[0].request("WPS_PBC")
  904. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  905. if ev is None:
  906. raise Exception("WPS-FAIL event timed out")
  907. if "msg=12" not in ev or "reason=2 (WEP Prohibited)" not in ev:
  908. raise Exception("Unexpected WPS-FAIL event: " + ev)
  909. def test_ap_wps_ie_fragmentation(dev, apdev):
  910. """WPS AP using fragmented WPS IE"""
  911. ssid = "test-wps-ie-fragmentation"
  912. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  913. "wpa_passphrase": "12345678", "wpa": "2",
  914. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  915. "device_name": "1234567890abcdef1234567890abcdef",
  916. "manufacturer": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
  917. "model_name": "1234567890abcdef1234567890abcdef",
  918. "model_number": "1234567890abcdef1234567890abcdef",
  919. "serial_number": "1234567890abcdef1234567890abcdef" }
  920. hostapd.add_ap(apdev[0]['ifname'], params)
  921. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  922. hapd.request("WPS_PBC")
  923. dev[0].request("WPS_PBC")
  924. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  925. if ev is None:
  926. raise Exception("Association with the AP timed out")
  927. bss = dev[0].get_bss(apdev[0]['bssid'])
  928. if "wps_device_name" not in bss or bss['wps_device_name'] != "1234567890abcdef1234567890abcdef":
  929. raise Exception("Device Name not received correctly")
  930. if len(re.findall("dd..0050f204", bss['ie'])) != 2:
  931. raise Exception("Unexpected number of WPS IEs")
  932. def add_ssdp_ap(ifname, ap_uuid):
  933. ssid = "wps-ssdp"
  934. ap_pin = "12345670"
  935. hostapd.add_ap(ifname,
  936. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  937. "wpa_passphrase": "12345678", "wpa": "2",
  938. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  939. "device_name": "Wireless AP", "manufacturer": "Company",
  940. "model_name": "WAP", "model_number": "123",
  941. "serial_number": "12345", "device_type": "6-0050F204-1",
  942. "os_version": "01020300",
  943. "config_methods": "label push_button",
  944. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo",
  945. "friendly_name": "WPS Access Point",
  946. "manufacturer_url": "http://www.example.com/",
  947. "model_description": "Wireless Access Point",
  948. "model_url": "http://www.example.com/model/",
  949. "upc": "123456789012" })
  950. def ssdp_send(msg, no_recv=False):
  951. socket.setdefaulttimeout(1)
  952. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  953. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  954. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  955. sock.bind(("127.0.0.1", 0))
  956. sock.sendto(msg, ("239.255.255.250", 1900))
  957. if no_recv:
  958. return None
  959. return sock.recv(1000)
  960. def ssdp_send_msearch(st):
  961. msg = '\r\n'.join([
  962. 'M-SEARCH * HTTP/1.1',
  963. 'HOST: 239.255.255.250:1900',
  964. 'MX: 1',
  965. 'MAN: "ssdp:discover"',
  966. 'ST: ' + st,
  967. '', ''])
  968. return ssdp_send(msg)
  969. def test_ap_wps_ssdp_msearch(dev, apdev):
  970. """WPS AP and SSDP M-SEARCH messages"""
  971. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  972. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  973. msg = '\r\n'.join([
  974. 'M-SEARCH * HTTP/1.1',
  975. 'Host: 239.255.255.250:1900',
  976. 'Mx: 1',
  977. 'Man: "ssdp:discover"',
  978. 'St: urn:schemas-wifialliance-org:device:WFADevice:1',
  979. '', ''])
  980. ssdp_send(msg)
  981. msg = '\r\n'.join([
  982. 'M-SEARCH * HTTP/1.1',
  983. 'host:\t239.255.255.250:1900\t\t\t\t \t\t',
  984. 'mx: \t1\t\t ',
  985. 'man: \t \t "ssdp:discover" ',
  986. 'st: urn:schemas-wifialliance-org:device:WFADevice:1\t\t',
  987. '', ''])
  988. ssdp_send(msg)
  989. ssdp_send_msearch("ssdp:all")
  990. ssdp_send_msearch("upnp:rootdevice")
  991. ssdp_send_msearch("uuid:" + ap_uuid)
  992. ssdp_send_msearch("urn:schemas-wifialliance-org:service:WFAWLANConfig:1")
  993. ssdp_send_msearch("urn:schemas-wifialliance-org:device:WFADevice:1");
  994. msg = '\r\n'.join([
  995. 'M-SEARCH * HTTP/1.1',
  996. 'HOST:\t239.255.255.250:1900',
  997. 'MAN: "ssdp:discover"',
  998. 'MX: 130',
  999. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1000. '', ''])
  1001. ssdp_send(msg, no_recv=True)
  1002. def test_ap_wps_ssdp_invalid_msearch(dev, apdev):
  1003. """WPS AP and invalid SSDP M-SEARCH messages"""
  1004. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1005. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1006. socket.setdefaulttimeout(1)
  1007. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  1008. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1009. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  1010. sock.bind(("127.0.0.1", 0))
  1011. logger.debug("Missing MX")
  1012. msg = '\r\n'.join([
  1013. 'M-SEARCH * HTTP/1.1',
  1014. 'HOST: 239.255.255.250:1900',
  1015. 'MAN: "ssdp:discover"',
  1016. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1017. '', ''])
  1018. sock.sendto(msg, ("239.255.255.250", 1900))
  1019. logger.debug("Negative MX")
  1020. msg = '\r\n'.join([
  1021. 'M-SEARCH * HTTP/1.1',
  1022. 'HOST: 239.255.255.250:1900',
  1023. 'MX: -1',
  1024. 'MAN: "ssdp:discover"',
  1025. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1026. '', ''])
  1027. sock.sendto(msg, ("239.255.255.250", 1900))
  1028. logger.debug("Invalid MX")
  1029. msg = '\r\n'.join([
  1030. 'M-SEARCH * HTTP/1.1',
  1031. 'HOST: 239.255.255.250:1900',
  1032. 'MX; 1',
  1033. 'MAN: "ssdp:discover"',
  1034. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1035. '', ''])
  1036. sock.sendto(msg, ("239.255.255.250", 1900))
  1037. logger.debug("Missing MAN")
  1038. msg = '\r\n'.join([
  1039. 'M-SEARCH * HTTP/1.1',
  1040. 'HOST: 239.255.255.250:1900',
  1041. 'MX: 1',
  1042. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1043. '', ''])
  1044. sock.sendto(msg, ("239.255.255.250", 1900))
  1045. logger.debug("Invalid MAN")
  1046. msg = '\r\n'.join([
  1047. 'M-SEARCH * HTTP/1.1',
  1048. 'HOST: 239.255.255.250:1900',
  1049. 'MX: 1',
  1050. 'MAN: foo',
  1051. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1052. '', ''])
  1053. sock.sendto(msg, ("239.255.255.250", 1900))
  1054. msg = '\r\n'.join([
  1055. 'M-SEARCH * HTTP/1.1',
  1056. 'HOST: 239.255.255.250:1900',
  1057. 'MX: 1',
  1058. 'MAN; "ssdp:discover"',
  1059. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1060. '', ''])
  1061. sock.sendto(msg, ("239.255.255.250", 1900))
  1062. logger.debug("Missing HOST")
  1063. msg = '\r\n'.join([
  1064. 'M-SEARCH * HTTP/1.1',
  1065. 'MAN: "ssdp:discover"',
  1066. 'MX: 1',
  1067. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1068. '', ''])
  1069. sock.sendto(msg, ("239.255.255.250", 1900))
  1070. logger.debug("Missing ST")
  1071. msg = '\r\n'.join([
  1072. 'M-SEARCH * HTTP/1.1',
  1073. 'HOST: 239.255.255.250:1900',
  1074. 'MAN: "ssdp:discover"',
  1075. 'MX: 1',
  1076. '', ''])
  1077. sock.sendto(msg, ("239.255.255.250", 1900))
  1078. logger.debug("Mismatching ST")
  1079. msg = '\r\n'.join([
  1080. 'M-SEARCH * HTTP/1.1',
  1081. 'HOST: 239.255.255.250:1900',
  1082. 'MAN: "ssdp:discover"',
  1083. 'MX: 1',
  1084. 'ST: uuid:16d5f8a9-4ee4-4f5e-81f9-cc6e2f47f42d',
  1085. '', ''])
  1086. sock.sendto(msg, ("239.255.255.250", 1900))
  1087. msg = '\r\n'.join([
  1088. 'M-SEARCH * HTTP/1.1',
  1089. 'HOST: 239.255.255.250:1900',
  1090. 'MAN: "ssdp:discover"',
  1091. 'MX: 1',
  1092. 'ST: foo:bar',
  1093. '', ''])
  1094. sock.sendto(msg, ("239.255.255.250", 1900))
  1095. msg = '\r\n'.join([
  1096. 'M-SEARCH * HTTP/1.1',
  1097. 'HOST: 239.255.255.250:1900',
  1098. 'MAN: "ssdp:discover"',
  1099. 'MX: 1',
  1100. 'ST: foobar',
  1101. '', ''])
  1102. sock.sendto(msg, ("239.255.255.250", 1900))
  1103. logger.debug("Invalid ST")
  1104. msg = '\r\n'.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. logger.debug("Invalid M-SEARCH")
  1113. msg = '\r\n'.join([
  1114. 'M+SEARCH * HTTP/1.1',
  1115. 'HOST: 239.255.255.250:1900',
  1116. 'MAN: "ssdp:discover"',
  1117. 'MX: 1',
  1118. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1119. '', ''])
  1120. sock.sendto(msg, ("239.255.255.250", 1900))
  1121. msg = '\r\n'.join([
  1122. 'M-SEARCH-* HTTP/1.1',
  1123. 'HOST: 239.255.255.250:1900',
  1124. 'MAN: "ssdp:discover"',
  1125. 'MX: 1',
  1126. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1127. '', ''])
  1128. sock.sendto(msg, ("239.255.255.250", 1900))
  1129. logger.debug("Invalid message format")
  1130. sock.sendto("NOTIFY * HTTP/1.1", ("239.255.255.250", 1900))
  1131. msg = '\r'.join([
  1132. 'M-SEARCH * HTTP/1.1',
  1133. 'HOST: 239.255.255.250:1900',
  1134. 'MAN: "ssdp:discover"',
  1135. 'MX: 1',
  1136. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1137. '', ''])
  1138. sock.sendto(msg, ("239.255.255.250", 1900))
  1139. try:
  1140. r = sock.recv(1000)
  1141. raise Exception("Unexpected M-SEARCH response: " + r)
  1142. except socket.timeout:
  1143. pass
  1144. logger.debug("Valid M-SEARCH")
  1145. msg = '\r\n'.join([
  1146. 'M-SEARCH * HTTP/1.1',
  1147. 'HOST: 239.255.255.250:1900',
  1148. 'MAN: "ssdp:discover"',
  1149. 'MX: 1',
  1150. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1151. '', ''])
  1152. sock.sendto(msg, ("239.255.255.250", 1900))
  1153. try:
  1154. r = sock.recv(1000)
  1155. pass
  1156. except socket.timeout:
  1157. raise Exception("No SSDP response")
  1158. def test_ap_wps_ssdp_burst(dev, apdev):
  1159. """WPS AP and SSDP burst"""
  1160. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1161. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1162. msg = '\r\n'.join([
  1163. 'M-SEARCH * HTTP/1.1',
  1164. 'HOST: 239.255.255.250:1900',
  1165. 'MAN: "ssdp:discover"',
  1166. 'MX: 1',
  1167. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1168. '', ''])
  1169. socket.setdefaulttimeout(1)
  1170. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  1171. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1172. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  1173. sock.bind(("127.0.0.1", 0))
  1174. for i in range(0, 25):
  1175. sock.sendto(msg, ("239.255.255.250", 1900))
  1176. resp = 0
  1177. while True:
  1178. try:
  1179. r = sock.recv(1000)
  1180. if not r.startswith("HTTP/1.1 200 OK\r\n"):
  1181. raise Exception("Unexpected message: " + r)
  1182. resp += 1
  1183. except socket.timeout:
  1184. break
  1185. if resp < 20:
  1186. raise Exception("Too few SSDP responses")
  1187. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  1188. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1189. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  1190. sock.bind(("127.0.0.1", 0))
  1191. for i in range(0, 25):
  1192. sock.sendto(msg, ("239.255.255.250", 1900))
  1193. while True:
  1194. try:
  1195. r = sock.recv(1000)
  1196. if ap_uuid in r:
  1197. break
  1198. except socket.timeout:
  1199. raise Exception("No SSDP response")
  1200. def ssdp_get_location(uuid):
  1201. res = ssdp_send_msearch("uuid:" + uuid)
  1202. location = None
  1203. for l in res.splitlines():
  1204. if l.lower().startswith("location:"):
  1205. location = l.split(':', 1)[1].strip()
  1206. break
  1207. if location is None:
  1208. raise Exception("No UPnP location found")
  1209. return location
  1210. def upnp_get_urls(location):
  1211. conn = urllib.urlopen(location)
  1212. tree = ET.parse(conn)
  1213. root = tree.getroot()
  1214. urn = '{urn:schemas-upnp-org:device-1-0}'
  1215. service = root.find("./" + urn + "device/" + urn + "serviceList/" + urn + "service")
  1216. res = {}
  1217. res['scpd_url'] = urlparse.urljoin(location, service.find(urn + 'SCPDURL').text)
  1218. res['control_url'] = urlparse.urljoin(location, service.find(urn + 'controlURL').text)
  1219. res['event_sub_url'] = urlparse.urljoin(location, service.find(urn + 'eventSubURL').text)
  1220. return res
  1221. def upnp_soap_action(conn, path, action, include_soap_action=True, soap_action_override=None):
  1222. soapns = 'http://schemas.xmlsoap.org/soap/envelope/'
  1223. wpsns = 'urn:schemas-wifialliance-org:service:WFAWLANConfig:1'
  1224. ET.register_namespace('soapenv', soapns)
  1225. ET.register_namespace('wfa', wpsns)
  1226. attrib = {}
  1227. attrib['{%s}encodingStyle' % soapns] = 'http://schemas.xmlsoap.org/soap/encoding/'
  1228. root = ET.Element("{%s}Envelope" % soapns, attrib=attrib)
  1229. body = ET.SubElement(root, "{%s}Body" % soapns)
  1230. act = ET.SubElement(body, "{%s}%s" % (wpsns, action))
  1231. tree = ET.ElementTree(root)
  1232. soap = StringIO.StringIO()
  1233. tree.write(soap, xml_declaration=True, encoding='utf-8')
  1234. headers = { "Content-type": 'text/xml; charset="utf-8"' }
  1235. if include_soap_action:
  1236. headers["SOAPAction"] = '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1#%s"' % action
  1237. elif soap_action_override:
  1238. headers["SOAPAction"] = soap_action_override
  1239. conn.request("POST", path, soap.getvalue(), headers)
  1240. return conn.getresponse()
  1241. def test_ap_wps_upnp(dev, apdev):
  1242. """WPS AP and UPnP operations"""
  1243. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1244. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1245. location = ssdp_get_location(ap_uuid)
  1246. urls = upnp_get_urls(location)
  1247. conn = urllib.urlopen(urls['scpd_url'])
  1248. scpd = conn.read()
  1249. conn = urllib.urlopen(urlparse.urljoin(location, "unknown.html"))
  1250. if conn.getcode() != 404:
  1251. raise Exception("Unexpected HTTP response to GET unknown URL")
  1252. url = urlparse.urlparse(location)
  1253. conn = httplib.HTTPConnection(url.netloc)
  1254. #conn.set_debuglevel(1)
  1255. headers = { "Content-type": 'text/xml; charset="utf-8"',
  1256. "SOAPAction": '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1#GetDeviceInfo"' }
  1257. conn.request("POST", "hello", "\r\n\r\n", headers)
  1258. resp = conn.getresponse()
  1259. if resp.status != 404:
  1260. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1261. conn.request("UNKNOWN", "hello", "\r\n\r\n", headers)
  1262. resp = conn.getresponse()
  1263. if resp.status != 501:
  1264. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1265. headers = { "Content-type": 'text/xml; charset="utf-8"',
  1266. "SOAPAction": '"urn:some-unknown-action#GetDeviceInfo"' }
  1267. ctrlurl = urlparse.urlparse(urls['control_url'])
  1268. conn.request("POST", ctrlurl.path, "\r\n\r\n", headers)
  1269. resp = conn.getresponse()
  1270. if resp.status != 401:
  1271. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1272. logger.debug("GetDeviceInfo without SOAPAction header")
  1273. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo",
  1274. include_soap_action=False)
  1275. if resp.status != 401:
  1276. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1277. logger.debug("GetDeviceInfo with invalid SOAPAction header")
  1278. for act in [ "foo",
  1279. "urn:schemas-wifialliance-org:service:WFAWLANConfig:1#GetDeviceInfo",
  1280. '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1"',
  1281. '"urn:schemas-wifialliance-org:service:WFAWLANConfig:123#GetDevice']:
  1282. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo",
  1283. include_soap_action=False,
  1284. soap_action_override=act)
  1285. if resp.status != 401:
  1286. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1287. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo")
  1288. if resp.status != 200:
  1289. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1290. dev = resp.read()
  1291. if "NewDeviceInfo" not in dev:
  1292. raise Exception("Unexpected GetDeviceInfo response")
  1293. logger.debug("PutMessage without required parameters")
  1294. resp = upnp_soap_action(conn, ctrlurl.path, "PutMessage")
  1295. if resp.status != 600:
  1296. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1297. logger.debug("PutWLANResponse without required parameters")
  1298. resp = upnp_soap_action(conn, ctrlurl.path, "PutWLANResponse")
  1299. if resp.status != 600:
  1300. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1301. logger.debug("SetSelectedRegistrar from unregistered ER")
  1302. resp = upnp_soap_action(conn, ctrlurl.path, "SetSelectedRegistrar")
  1303. if resp.status != 501:
  1304. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1305. logger.debug("Unknown action")
  1306. resp = upnp_soap_action(conn, ctrlurl.path, "Unknown")
  1307. if resp.status != 401:
  1308. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1309. def test_ap_wps_upnp_subscribe(dev, apdev):
  1310. """WPS AP and UPnP event subscription"""
  1311. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1312. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1313. location = ssdp_get_location(ap_uuid)
  1314. urls = upnp_get_urls(location)
  1315. eventurl = urlparse.urlparse(urls['event_sub_url'])
  1316. url = urlparse.urlparse(location)
  1317. conn = httplib.HTTPConnection(url.netloc)
  1318. #conn.set_debuglevel(1)
  1319. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1320. "timeout": "Second-1234" }
  1321. conn.request("SUBSCRIBE", "hello", "\r\n\r\n", headers)
  1322. resp = conn.getresponse()
  1323. if resp.status != 412:
  1324. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1325. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1326. resp = conn.getresponse()
  1327. if resp.status != 412:
  1328. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1329. headers = { "NT": "upnp:event",
  1330. "timeout": "Second-1234" }
  1331. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1332. resp = conn.getresponse()
  1333. if resp.status != 412:
  1334. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1335. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1336. "NT": "upnp:foobar",
  1337. "timeout": "Second-1234" }
  1338. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1339. resp = conn.getresponse()
  1340. if resp.status != 400:
  1341. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1342. logger.debug("Valid subscription")
  1343. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1344. "NT": "upnp:event",
  1345. "timeout": "Second-1234" }
  1346. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1347. resp = conn.getresponse()
  1348. if resp.status != 200:
  1349. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1350. sid = resp.getheader("sid")
  1351. logger.debug("Subscription SID " + sid)
  1352. logger.debug("Invalid re-subscription")
  1353. headers = { "NT": "upnp:event",
  1354. "sid": "123456734567854",
  1355. "timeout": "Second-1234" }
  1356. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1357. resp = conn.getresponse()
  1358. if resp.status != 400:
  1359. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1360. logger.debug("Invalid re-subscription")
  1361. headers = { "NT": "upnp:event",
  1362. "sid": "uuid:123456734567854",
  1363. "timeout": "Second-1234" }
  1364. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1365. resp = conn.getresponse()
  1366. if resp.status != 400:
  1367. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1368. logger.debug("Invalid re-subscription")
  1369. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1370. "NT": "upnp:event",
  1371. "sid": sid,
  1372. "timeout": "Second-1234" }
  1373. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1374. resp = conn.getresponse()
  1375. if resp.status != 400:
  1376. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1377. logger.debug("SID mismatch in re-subscription")
  1378. headers = { "NT": "upnp:event",
  1379. "sid": "uuid:4c2bca79-1ff4-4e43-85d4-952a2b8a51fb",
  1380. "timeout": "Second-1234" }
  1381. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1382. resp = conn.getresponse()
  1383. if resp.status != 412:
  1384. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1385. logger.debug("Valid re-subscription")
  1386. headers = { "NT": "upnp:event",
  1387. "sid": sid,
  1388. "timeout": "Second-1234" }
  1389. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1390. resp = conn.getresponse()
  1391. if resp.status != 200:
  1392. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1393. sid2 = resp.getheader("sid")
  1394. logger.debug("Subscription SID " + sid2)
  1395. if sid != sid2:
  1396. raise Exception("Unexpected SID change")
  1397. logger.debug("Valid re-subscription")
  1398. headers = { "NT": "upnp:event",
  1399. "sid": "uuid: \t \t" + sid.split(':')[1],
  1400. "timeout": "Second-1234" }
  1401. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1402. resp = conn.getresponse()
  1403. if resp.status != 200:
  1404. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1405. logger.debug("Invalid unsubscription")
  1406. headers = { "sid": sid }
  1407. conn.request("UNSUBSCRIBE", "/hello", "\r\n\r\n", headers)
  1408. resp = conn.getresponse()
  1409. if resp.status != 412:
  1410. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1411. headers = { "foo": "bar" }
  1412. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1413. resp = conn.getresponse()
  1414. if resp.status != 412:
  1415. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1416. logger.debug("Valid unsubscription")
  1417. headers = { "sid": sid }
  1418. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1419. resp = conn.getresponse()
  1420. if resp.status != 200:
  1421. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1422. logger.debug("Unsubscription for not existing SID")
  1423. headers = { "sid": sid }
  1424. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1425. resp = conn.getresponse()
  1426. if resp.status != 412:
  1427. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1428. logger.debug("Invalid unsubscription")
  1429. headers = { "sid": " \t \tfoo" }
  1430. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1431. resp = conn.getresponse()
  1432. if resp.status != 400:
  1433. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1434. logger.debug("Invalid unsubscription")
  1435. headers = { "sid": "uuid:\t \tfoo" }
  1436. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1437. resp = conn.getresponse()
  1438. if resp.status != 400:
  1439. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1440. logger.debug("Invalid unsubscription")
  1441. headers = { "NT": "upnp:event",
  1442. "sid": sid }
  1443. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1444. resp = conn.getresponse()
  1445. if resp.status != 400:
  1446. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1447. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1448. "sid": sid }
  1449. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1450. resp = conn.getresponse()
  1451. if resp.status != 400:
  1452. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1453. logger.debug("Valid subscription with multiple callbacks")
  1454. headers = { "callback": '<http://127.0.0.1:12345/event> <http://127.0.0.1:12345/event>\t<http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event>',
  1455. "NT": "upnp:event",
  1456. "timeout": "Second-1234" }
  1457. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1458. resp = conn.getresponse()
  1459. if resp.status != 200:
  1460. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1461. sid = resp.getheader("sid")
  1462. logger.debug("Subscription SID " + sid)