test_ap_wps.py 69 KB

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