test_ap_wps.py 72 KB

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