test_nfc_wps.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. # WPS+NFC tests
  2. # Copyright (c) 2013, 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 hwsim_utils
  11. import hostapd
  12. def check_wpa2_connection(sta, ap, hapd, ssid, mixed=False):
  13. status = sta.get_status()
  14. if status['wpa_state'] != 'COMPLETED':
  15. raise Exception("Not fully connected")
  16. if status['bssid'] != ap['bssid']:
  17. raise Exception("Unexpected BSSID")
  18. if status['ssid'] != ssid:
  19. raise Exception("Unexpected SSID")
  20. if status['pairwise_cipher'] != 'CCMP':
  21. raise Exception("Unexpected encryption configuration")
  22. if status['group_cipher'] != 'CCMP' and not mixed:
  23. raise Exception("Unexpected encryption configuration")
  24. if status['key_mgmt'] != 'WPA2-PSK':
  25. raise Exception("Unexpected key_mgmt")
  26. hwsim_utils.test_connectivity(sta, hapd)
  27. def ap_wps_params(ssid):
  28. return { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  29. "wpa_passphrase": "12345678", "wpa": "2",
  30. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"}
  31. def test_nfc_wps_password_token_sta(dev, apdev):
  32. """NFC tag with password token on the station/Enrollee"""
  33. ssid = "test-wps-nfc-pw-token-conf"
  34. params = ap_wps_params(ssid)
  35. hostapd.add_ap(apdev[0]['ifname'], params)
  36. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  37. logger.info("WPS provisioning step using password token from station")
  38. wps = dev[0].request("WPS_NFC_TOKEN WPS").rstrip()
  39. if "FAIL" in wps:
  40. raise Exception("Failed to generate password token (WPS only)")
  41. pw = dev[0].request("WPS_NFC_TOKEN NDEF").rstrip()
  42. if "FAIL" in pw:
  43. raise Exception("Failed to generate password token")
  44. res = hapd.request("WPS_NFC_TAG_READ " + pw)
  45. if "FAIL" in res:
  46. raise Exception("Failed to provide NFC tag contents to hostapd")
  47. dev[0].dump_monitor()
  48. res = dev[0].request("WPS_NFC")
  49. if "FAIL" in res:
  50. raise Exception("Failed to start Enrollee using NFC password token")
  51. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  52. if ev is None:
  53. raise Exception("Association with the AP timed out")
  54. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  55. def test_nfc_wps_config_token(dev, apdev):
  56. """NFC tag with configuration token from AP"""
  57. ssid = "test-wps-nfc-conf-token"
  58. params = ap_wps_params(ssid)
  59. hostapd.add_ap(apdev[0]['ifname'], params)
  60. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  61. logger.info("NFC configuration token from AP to station")
  62. conf = hapd.request("WPS_NFC_CONFIG_TOKEN NDEF").rstrip()
  63. if "FAIL" in conf:
  64. raise Exception("Failed to generate configuration token")
  65. dev[0].dump_monitor()
  66. res = dev[0].request("WPS_NFC_TAG_READ " + conf)
  67. if "FAIL" in res:
  68. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  69. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  70. if ev is None:
  71. raise Exception("Association with the AP timed out")
  72. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  73. def test_nfc_wps_config_token_init(dev, apdev):
  74. """NFC tag with configuration token from AP with auto configuration"""
  75. ssid = "test-wps-nfc-conf-token-init"
  76. hostapd.add_ap(apdev[0]['ifname'],
  77. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  78. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  79. logger.info("NFC configuration token from AP to station")
  80. conf = hapd.request("WPS_NFC_CONFIG_TOKEN NDEF").rstrip()
  81. if "FAIL" in conf:
  82. raise Exception("Failed to generate configuration token")
  83. dev[0].dump_monitor()
  84. res = dev[0].request("WPS_NFC_TAG_READ " + conf)
  85. if "FAIL" in res:
  86. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  87. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  88. if ev is None:
  89. raise Exception("Association with the AP timed out")
  90. check_wpa2_connection(dev[0], apdev[0], hapd, ssid, mixed=True)
  91. def test_nfc_wps_password_token_sta_init(dev, apdev):
  92. """Initial AP configuration with first WPS NFC Enrollee"""
  93. ssid = "test-wps-nfc-pw-token-init"
  94. hostapd.add_ap(apdev[0]['ifname'],
  95. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  96. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  97. logger.info("WPS provisioning step using password token from station")
  98. pw = dev[0].request("WPS_NFC_TOKEN NDEF").rstrip()
  99. if "FAIL" in pw:
  100. raise Exception("Failed to generate password token")
  101. res = hapd.request("WPS_NFC_TAG_READ " + pw)
  102. if "FAIL" in res:
  103. raise Exception("Failed to provide NFC tag contents to hostapd")
  104. dev[0].dump_monitor()
  105. res = dev[0].request("WPS_NFC")
  106. if "FAIL" in res:
  107. raise Exception("Failed to start Enrollee using NFC password token")
  108. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  109. if ev is None:
  110. raise Exception("Association with the AP timed out")
  111. check_wpa2_connection(dev[0], apdev[0], hapd, ssid, mixed=True)
  112. def test_nfc_wps_password_token_ap(dev, apdev):
  113. """WPS registrar configuring an AP using AP password token"""
  114. ssid = "test-wps-nfc-pw-token-init"
  115. hostapd.add_ap(apdev[0]['ifname'],
  116. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  117. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  118. logger.info("WPS configuration step")
  119. pw = hapd.request("WPS_NFC_TOKEN NDEF").rstrip()
  120. if "FAIL" in pw:
  121. raise Exception("Failed to generate password token")
  122. res = hapd.request("WPS_NFC_TOKEN enable")
  123. if "FAIL" in pw:
  124. raise Exception("Failed to enable AP password token")
  125. res = dev[0].request("WPS_NFC_TAG_READ " + pw)
  126. if "FAIL" in res:
  127. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  128. dev[0].dump_monitor()
  129. new_ssid = "test-wps-nfc-pw-token-new-ssid"
  130. new_passphrase = "1234567890"
  131. res = dev[0].request("WPS_REG " + apdev[0]['bssid'] + " nfc-pw " + new_ssid.encode("hex") + " WPA2PSK CCMP " + new_passphrase.encode("hex"))
  132. if "FAIL" in res:
  133. raise Exception("Failed to start Registrar using NFC password token")
  134. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  135. if ev is None:
  136. raise Exception("Association with the AP timed out")
  137. check_wpa2_connection(dev[0], apdev[0], hapd, new_ssid, mixed=True)
  138. if "FAIL" in hapd.request("WPS_NFC_TOKEN disable"):
  139. raise Exception("Failed to disable AP password token")
  140. if "FAIL" in hapd.request("WPS_NFC_TOKEN WPS"):
  141. raise Exception("Unexpected WPS_NFC_TOKEN WPS failure")
  142. def test_nfc_wps_handover_init(dev, apdev):
  143. """Connect to WPS AP with NFC connection handover and move to configured state"""
  144. dev[0].request("SET ignore_old_scan_res 1")
  145. ssid = "test-wps-nfc-handover-init"
  146. hostapd.add_ap(apdev[0]['ifname'],
  147. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  148. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  149. logger.info("NFC connection handover")
  150. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  151. if "FAIL" in req:
  152. raise Exception("Failed to generate NFC connection handover request")
  153. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  154. if "FAIL" in sel:
  155. raise Exception("Failed to generate NFC connection handover select")
  156. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  157. if "FAIL" in res:
  158. raise Exception("Failed to report NFC connection handover to to hostapd")
  159. dev[0].dump_monitor()
  160. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  161. if "FAIL" in res:
  162. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  163. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  164. if ev is None:
  165. raise Exception("Association with the AP timed out")
  166. check_wpa2_connection(dev[0], apdev[0], hapd, ssid, mixed=True)
  167. def test_nfc_wps_handover_errors(dev, apdev):
  168. """WPS AP NFC handover report error cases"""
  169. ssid = "test-wps-nfc-handover"
  170. hostapd.add_ap(apdev[0]['ifname'],
  171. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  172. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  173. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  174. if "FAIL" in sel:
  175. raise Exception("Failed to generate NFC connection handover select")
  176. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER "):
  177. raise Exception("Unexpected handover report success")
  178. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER RESP"):
  179. raise Exception("Unexpected handover report success")
  180. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER RESP WPS"):
  181. raise Exception("Unexpected handover report success")
  182. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER RESP WPS 001122"):
  183. raise Exception("Unexpected handover report success")
  184. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER RESP WPS 001122 00"):
  185. raise Exception("Unexpected handover report success")
  186. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER RESP WPS 0 00"):
  187. raise Exception("Unexpected handover report success")
  188. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER RESP WPS 001122 0"):
  189. raise Exception("Unexpected handover report success")
  190. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER RESP WPS 00q122 001122"):
  191. raise Exception("Unexpected handover report success")
  192. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER RESP WPS 001122 001q22"):
  193. raise Exception("Unexpected handover report success")
  194. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER RESP FOO 001122 00"):
  195. raise Exception("Unexpected handover report success")
  196. def test_nfc_wps_handover(dev, apdev):
  197. """Connect to WPS AP with NFC connection handover"""
  198. ssid = "test-wps-nfc-handover"
  199. params = ap_wps_params(ssid)
  200. hostapd.add_ap(apdev[0]['ifname'], params)
  201. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  202. logger.info("NFC connection handover")
  203. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  204. if "FAIL" in req:
  205. raise Exception("Failed to generate NFC connection handover request")
  206. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  207. if "FAIL" in sel:
  208. raise Exception("Failed to generate NFC connection handover select")
  209. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  210. if "FAIL" in res:
  211. raise Exception("Failed to report NFC connection handover to to hostapd")
  212. dev[0].dump_monitor()
  213. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  214. if "FAIL" in res:
  215. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  216. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  217. if ev is None:
  218. raise Exception("Association with the AP timed out")
  219. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  220. def test_nfc_wps_handover_5ghz(dev, apdev):
  221. """Connect to WPS AP with NFC connection handover on 5 GHz band"""
  222. try:
  223. ssid = "test-wps-nfc-handover"
  224. params = ap_wps_params(ssid)
  225. params["country_code"] = "FI"
  226. params["hw_mode"] = "a"
  227. params["channel"] = "36"
  228. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  229. logger.info("NFC connection handover")
  230. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  231. if "FAIL" in req:
  232. raise Exception("Failed to generate NFC connection handover request")
  233. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  234. if "FAIL" in sel:
  235. raise Exception("Failed to generate NFC connection handover select")
  236. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  237. if "FAIL" in res:
  238. raise Exception("Failed to report NFC connection handover to to hostapd")
  239. dev[0].dump_monitor()
  240. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  241. if "FAIL" in res:
  242. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  243. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  244. if ev is None:
  245. raise Exception("Association with the AP timed out")
  246. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  247. finally:
  248. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  249. def test_nfc_wps_handover_chan14(dev, apdev):
  250. """Connect to WPS AP with NFC connection handover on channel 14"""
  251. try:
  252. ssid = "test-wps-nfc-handover"
  253. params = ap_wps_params(ssid)
  254. params["country_code"] = "JP"
  255. params["hw_mode"] = "b"
  256. params["channel"] = "14"
  257. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  258. logger.info("NFC connection handover")
  259. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  260. if "FAIL" in req:
  261. raise Exception("Failed to generate NFC connection handover request")
  262. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  263. if "FAIL" in sel:
  264. raise Exception("Failed to generate NFC connection handover select")
  265. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  266. if "FAIL" in res:
  267. raise Exception("Failed to report NFC connection handover to to hostapd")
  268. dev[0].dump_monitor()
  269. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  270. if "FAIL" in res:
  271. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  272. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  273. if ev is None:
  274. raise Exception("Association with the AP timed out")
  275. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  276. finally:
  277. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  278. def test_nfc_wps_handover_with_pw_token_set(dev, apdev):
  279. """Connect to WPS AP with NFC connection handover (wps_nfc_* set)"""
  280. ssid = "test-wps-nfc-handover2"
  281. params = ap_wps_params(ssid)
  282. hostapd.add_ap(apdev[0]['ifname'], params)
  283. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  284. # enable a password token (which won't be used in this test case)
  285. pw = hapd.request("WPS_NFC_TOKEN NDEF").rstrip()
  286. if "FAIL" in pw:
  287. raise Exception("Failed to generate password token")
  288. res = hapd.request("WPS_NFC_TOKEN enable")
  289. if "FAIL" in pw:
  290. raise Exception("Failed to enable AP password token")
  291. logger.info("NFC connection handover")
  292. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  293. if "FAIL" in req:
  294. raise Exception("Failed to generate NFC connection handover request")
  295. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  296. if "FAIL" in sel:
  297. raise Exception("Failed to generate NFC connection handover select")
  298. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  299. if "FAIL" in res:
  300. raise Exception("Failed to report NFC connection handover to to hostapd")
  301. dev[0].dump_monitor()
  302. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  303. if "FAIL" in res:
  304. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  305. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  306. if ev is None:
  307. raise Exception("Association with the AP timed out")
  308. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  309. def test_nfc_wps_handover_pk_hash_mismatch_sta(dev, apdev):
  310. """WPS NFC connection handover with invalid pkhash from station (negative)"""
  311. ssid = "wps-nfc-handover-pkhash-sta"
  312. if "FAIL" in dev[0].request("SET wps_corrupt_pkhash 1"):
  313. raise Exception("Could not enable wps_corrupt_pkhash")
  314. params = ap_wps_params(ssid)
  315. hostapd.add_ap(apdev[0]['ifname'], params)
  316. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  317. logger.info("NFC connection handover")
  318. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  319. if "FAIL" in req:
  320. raise Exception("Failed to generate NFC connection handover request")
  321. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  322. if "FAIL" in sel:
  323. raise Exception("Failed to generate NFC connection handover select")
  324. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  325. if "FAIL" in res:
  326. raise Exception("Failed to report NFC connection handover to to hostapd")
  327. dev[0].dump_monitor()
  328. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  329. if "FAIL" in res:
  330. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  331. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "WPS-FAIL"], timeout=15)
  332. if ev is None:
  333. raise Exception("Timed out")
  334. if "WPS-FAIL" not in ev:
  335. raise Exception("Public key hash mismatch not detected")
  336. def test_nfc_wps_handover_pk_hash_mismatch_ap(dev, apdev):
  337. """WPS NFC connection handover with invalid pkhash from AP (negative)"""
  338. ssid = "wps-nfc-handover-pkhash-ap"
  339. params = ap_wps_params(ssid)
  340. hostapd.add_ap(apdev[0]['ifname'], params)
  341. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  342. if "FAIL" in hapd.request("SET wps_corrupt_pkhash 1"):
  343. raise Exception("Could not enable wps_corrupt_pkhash")
  344. logger.info("NFC connection handover")
  345. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  346. if "FAIL" in req:
  347. raise Exception("Failed to generate NFC connection handover request")
  348. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  349. if "FAIL" in sel:
  350. raise Exception("Failed to generate NFC connection handover select")
  351. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  352. if "FAIL" in res:
  353. raise Exception("Failed to report NFC connection handover to to hostapd")
  354. dev[0].dump_monitor()
  355. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  356. if "FAIL" in res:
  357. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  358. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "WPS-FAIL"], timeout=15)
  359. if ev is None:
  360. raise Exception("Timed out")
  361. if "WPS-FAIL" not in ev:
  362. raise Exception("Public key hash mismatch not detected")
  363. def start_ap_er(er, ap, ssid):
  364. ap_pin = "12345670"
  365. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  366. hostapd.add_ap(ap['ifname'],
  367. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  368. "wpa_passphrase": "12345678", "wpa": "2",
  369. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  370. "device_name": "Wireless AP", "manufacturer": "Company",
  371. "model_name": "WAP", "model_number": "123",
  372. "serial_number": "12345", "device_type": "6-0050F204-1",
  373. "os_version": "01020300",
  374. "config_methods": "label push_button",
  375. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  376. logger.info("Learn AP configuration")
  377. er.dump_monitor()
  378. er.request("SET ignore_old_scan_res 1")
  379. er.wps_reg(ap['bssid'], ap_pin)
  380. logger.info("Start ER")
  381. er.request("WPS_ER_STOP")
  382. time.sleep(1)
  383. er.request("WPS_ER_START ifname=lo")
  384. ev = er.wait_event(["WPS-ER-AP-ADD"], timeout=15)
  385. if ev is None:
  386. raise Exception("AP discovery timed out")
  387. if ap_uuid not in ev:
  388. raise Exception("Expected AP UUID not found")
  389. logger.info("Use learned network configuration on ER")
  390. er.request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  391. def test_nfc_wps_er_pw_token(dev, apdev):
  392. """WPS NFC password token from Enrollee to ER"""
  393. ssid = "wps-nfc-er-pw-token"
  394. start_ap_er(dev[0], apdev[0], ssid)
  395. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  396. logger.info("WPS provisioning step using password token from station")
  397. dev[1].request("SET ignore_old_scan_res 1")
  398. pw = dev[1].request("WPS_NFC_TOKEN NDEF").rstrip()
  399. if "FAIL" in pw:
  400. raise Exception("Failed to generate password token")
  401. res = dev[0].request("WPS_NFC_TAG_READ " + pw)
  402. if "FAIL" in res:
  403. raise Exception("Failed to provide NFC tag contents to WPS ER")
  404. dev[0].dump_monitor()
  405. res = dev[1].request("WPS_NFC")
  406. if "FAIL" in res:
  407. raise Exception("Failed to start Enrollee using NFC password token")
  408. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  409. if ev is None:
  410. raise Exception("WPS ER did not report success")
  411. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  412. if ev is None:
  413. raise Exception("Association with the AP timed out")
  414. check_wpa2_connection(dev[1], apdev[0], hapd, ssid)
  415. def test_nfc_wps_er_config_token(dev, apdev):
  416. """WPS NFC configuration token from ER to Enrollee"""
  417. ssid = "wps-nfc-er-config-token"
  418. start_ap_er(dev[0], apdev[0], ssid)
  419. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  420. logger.info("WPS provisioning step using configuration token from ER")
  421. wps = dev[0].request("WPS_ER_NFC_CONFIG_TOKEN WPS " + apdev[0]['bssid']).rstrip()
  422. if "FAIL" in wps:
  423. raise Exception("Failed to generate configuration token (WPS format)")
  424. conf = dev[0].request("WPS_ER_NFC_CONFIG_TOKEN NDEF " + apdev[0]['bssid']).rstrip()
  425. if "FAIL" in conf:
  426. raise Exception("Failed to generate configuration token")
  427. dev[1].request("SET ignore_old_scan_res 1")
  428. res = dev[1].request("WPS_NFC_TAG_READ " + conf)
  429. if "FAIL" in res:
  430. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  431. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  432. if ev is None:
  433. raise Exception("Association with the AP timed out")
  434. check_wpa2_connection(dev[1], apdev[0], hapd, ssid)
  435. def test_nfc_wps_er_handover(dev, apdev):
  436. """WPS NFC connection handover between Enrollee and ER"""
  437. ssid = "wps-nfc-er-handover"
  438. start_ap_er(dev[0], apdev[0], ssid)
  439. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  440. logger.info("WPS provisioning step using connection handover")
  441. req = dev[1].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  442. if "FAIL" in req:
  443. raise Exception("Failed to generate NFC connection handover request")
  444. sel = dev[0].request("NFC_GET_HANDOVER_SEL NDEF WPS-CR " + apdev[0]['bssid']).rstrip()
  445. if "FAIL" in sel:
  446. raise Exception("Failed to generate NFC connection handover select")
  447. res = dev[0].request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  448. if "FAIL" in res:
  449. raise Exception("Failed to report NFC connection handover to to hostapd")
  450. dev[1].dump_monitor()
  451. res = dev[1].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  452. if "FAIL" in res:
  453. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  454. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  455. if ev is None:
  456. raise Exception("Association with the AP timed out")
  457. check_wpa2_connection(dev[1], apdev[0], hapd, ssid)
  458. def test_nfc_wps_er_handover_pk_hash_mismatch_sta(dev, apdev):
  459. """WPS NFC connection handover with invalid pkhash from station to ER (negative)"""
  460. ssid = "wps-nfc-er-handover-pkhash-sta"
  461. start_ap_er(dev[0], apdev[0], ssid)
  462. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  463. logger.info("WPS provisioning step using connection handover")
  464. if "FAIL" in dev[1].request("SET wps_corrupt_pkhash 1"):
  465. raise Exception("Could not enable wps_corrupt_pkhash")
  466. dev[1].request("SET ignore_old_scan_res 1")
  467. req = dev[1].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  468. if "FAIL" in req:
  469. raise Exception("Failed to generate NFC connection handover request")
  470. sel = dev[0].request("NFC_GET_HANDOVER_SEL NDEF WPS-CR " + apdev[0]['bssid']).rstrip()
  471. if "FAIL" in sel:
  472. raise Exception("Failed to generate NFC connection handover select")
  473. res = dev[0].request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  474. if "FAIL" in res:
  475. raise Exception("Failed to report NFC connection handover to to hostapd")
  476. dev[1].dump_monitor()
  477. res = dev[1].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  478. if "FAIL" in res:
  479. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  480. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED", "WPS-FAIL"], timeout=15)
  481. if ev is None:
  482. raise Exception("Timed out")
  483. if "WPS-FAIL" not in ev:
  484. raise Exception("Public key hash mismatch not detected")
  485. def test_nfc_wps_er_handover_pk_hash_mismatch_er(dev, apdev):
  486. """WPS NFC connection handover with invalid pkhash from ER to station (negative)"""
  487. ssid = "wps-nfc-er-handover-pkhash-er"
  488. start_ap_er(dev[0], apdev[0], ssid)
  489. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  490. logger.info("WPS provisioning step using connection handover")
  491. if "FAIL" in dev[0].request("SET wps_corrupt_pkhash 1"):
  492. raise Exception("Could not enable wps_corrupt_pkhash")
  493. dev[1].request("SET ignore_old_scan_res 1")
  494. req = dev[1].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  495. if "FAIL" in req:
  496. raise Exception("Failed to generate NFC connection handover request")
  497. sel = dev[0].request("NFC_GET_HANDOVER_SEL NDEF WPS-CR " + apdev[0]['bssid']).rstrip()
  498. if "FAIL" in sel:
  499. raise Exception("Failed to generate NFC connection handover select")
  500. res = dev[0].request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  501. if "FAIL" in res:
  502. raise Exception("Failed to report NFC connection handover to to hostapd")
  503. dev[1].dump_monitor()
  504. res = dev[1].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  505. if "FAIL" in res:
  506. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  507. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED", "WPS-FAIL"], timeout=15)
  508. if ev is None:
  509. raise Exception("Timed out")
  510. if "WPS-FAIL" not in ev:
  511. raise Exception("Public key hash mismatch not detected")