test_ap_wps.py 65 KB

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