test_ap_wps.py 84 KB

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