test_ap_wps.py 84 KB

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