test_nfc_wps.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. from utils import alloc_fail, fail_test
  13. def check_wpa2_connection(sta, ap, hapd, ssid, mixed=False):
  14. status = sta.get_status()
  15. if status['wpa_state'] != 'COMPLETED':
  16. raise Exception("Not fully connected")
  17. if status['bssid'] != ap['bssid']:
  18. raise Exception("Unexpected BSSID")
  19. if status['ssid'] != ssid:
  20. raise Exception("Unexpected SSID")
  21. if status['pairwise_cipher'] != 'CCMP':
  22. raise Exception("Unexpected encryption configuration")
  23. if status['group_cipher'] != 'CCMP' and not mixed:
  24. raise Exception("Unexpected encryption configuration")
  25. if status['key_mgmt'] != 'WPA2-PSK':
  26. raise Exception("Unexpected key_mgmt")
  27. hwsim_utils.test_connectivity(sta, hapd)
  28. def ap_wps_params(ssid):
  29. return { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  30. "wpa_passphrase": "12345678", "wpa": "2",
  31. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"}
  32. def test_nfc_wps_password_token_sta(dev, apdev):
  33. """NFC tag with password token on the station/Enrollee"""
  34. ssid = "test-wps-nfc-pw-token-conf"
  35. params = ap_wps_params(ssid)
  36. hapd = hostapd.add_ap(apdev[0], params)
  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. dev[0].wait_connected(timeout=30)
  52. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  53. def test_nfc_wps_config_token(dev, apdev):
  54. """NFC tag with configuration token from AP"""
  55. ssid = "test-wps-nfc-conf-token"
  56. params = ap_wps_params(ssid)
  57. hapd = hostapd.add_ap(apdev[0], params)
  58. logger.info("NFC configuration token from AP to station")
  59. conf = hapd.request("WPS_NFC_CONFIG_TOKEN NDEF").rstrip()
  60. if "FAIL" in conf:
  61. raise Exception("Failed to generate configuration token")
  62. dev[0].dump_monitor()
  63. res = dev[0].request("WPS_NFC_TAG_READ " + conf)
  64. if "FAIL" in res:
  65. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  66. dev[0].wait_connected(timeout=15)
  67. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  68. with alloc_fail(hapd, 1, "wps_get_oob_cred"):
  69. conf = hapd.request("WPS_NFC_CONFIG_TOKEN NDEF").rstrip()
  70. if "FAIL" not in conf:
  71. raise Exception("Unexpected configuration token received during OOM")
  72. def test_nfc_wps_config_token_init(dev, apdev):
  73. """NFC tag with configuration token from AP with auto configuration"""
  74. ssid = "test-wps-nfc-conf-token-init"
  75. hapd = hostapd.add_ap(apdev[0],
  76. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  77. logger.info("NFC configuration token from AP to station")
  78. conf = hapd.request("WPS_NFC_CONFIG_TOKEN NDEF").rstrip()
  79. if "FAIL" in conf:
  80. raise Exception("Failed to generate configuration token")
  81. dev[0].dump_monitor()
  82. res = dev[0].request("WPS_NFC_TAG_READ " + conf)
  83. if "FAIL" in res:
  84. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  85. dev[0].wait_connected(timeout=15)
  86. check_wpa2_connection(dev[0], apdev[0], hapd, ssid, mixed=True)
  87. def test_nfc_wps_password_token_sta_init(dev, apdev):
  88. """Initial AP configuration with first WPS NFC Enrollee"""
  89. ssid = "test-wps-nfc-pw-token-init"
  90. hapd = hostapd.add_ap(apdev[0],
  91. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  92. logger.info("WPS provisioning step using password token from station")
  93. pw = dev[0].request("WPS_NFC_TOKEN NDEF").rstrip()
  94. if "FAIL" in pw:
  95. raise Exception("Failed to generate password token")
  96. res = hapd.request("WPS_NFC_TAG_READ " + pw)
  97. if "FAIL" in res:
  98. raise Exception("Failed to provide NFC tag contents to hostapd")
  99. dev[0].dump_monitor()
  100. res = dev[0].request("WPS_NFC")
  101. if "FAIL" in res:
  102. raise Exception("Failed to start Enrollee using NFC password token")
  103. dev[0].wait_connected(timeout=30)
  104. check_wpa2_connection(dev[0], apdev[0], hapd, ssid, mixed=True)
  105. def test_nfc_wps_password_token_ap(dev, apdev):
  106. """WPS registrar configuring an AP using AP password token"""
  107. ssid = "test-wps-nfc-pw-token-init"
  108. hapd = hostapd.add_ap(apdev[0],
  109. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  110. logger.info("WPS configuration step")
  111. pw = hapd.request("WPS_NFC_TOKEN NDEF").rstrip()
  112. if "FAIL" in pw:
  113. raise Exception("Failed to generate password token")
  114. res = hapd.request("WPS_NFC_TOKEN enable")
  115. if "FAIL" in pw:
  116. raise Exception("Failed to enable AP password token")
  117. res = dev[0].request("WPS_NFC_TAG_READ " + pw)
  118. if "FAIL" in res:
  119. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  120. dev[0].dump_monitor()
  121. new_ssid = "test-wps-nfc-pw-token-new-ssid"
  122. new_passphrase = "1234567890"
  123. res = dev[0].request("WPS_REG " + apdev[0]['bssid'] + " nfc-pw " + new_ssid.encode("hex") + " WPA2PSK CCMP " + new_passphrase.encode("hex"))
  124. if "FAIL" in res:
  125. raise Exception("Failed to start Registrar using NFC password token")
  126. dev[0].wait_connected(timeout=30)
  127. check_wpa2_connection(dev[0], apdev[0], hapd, new_ssid, mixed=True)
  128. if "FAIL" in hapd.request("WPS_NFC_TOKEN disable"):
  129. raise Exception("Failed to disable AP password token")
  130. if "FAIL" in hapd.request("WPS_NFC_TOKEN WPS"):
  131. raise Exception("Unexpected WPS_NFC_TOKEN WPS failure")
  132. with fail_test(hapd, 1, "os_get_random;wps_nfc_token_gen"):
  133. if "FAIL" not in hapd.request("WPS_NFC_TOKEN WPS"):
  134. raise Exception("Unexpected WPS_NFC_TOKEN success")
  135. with fail_test(hapd, 2, "os_get_random;wps_nfc_token_gen"):
  136. if "FAIL" not in hapd.request("WPS_NFC_TOKEN WPS"):
  137. raise Exception("Unexpected WPS_NFC_TOKEN success")
  138. def test_nfc_wps_handover_init(dev, apdev):
  139. """Connect to WPS AP with NFC connection handover and move to configured state"""
  140. try:
  141. _test_nfc_wps_handover_init(dev, apdev)
  142. finally:
  143. dev[0].request("SET ignore_old_scan_res 0")
  144. def _test_nfc_wps_handover_init(dev, apdev):
  145. dev[0].request("SET ignore_old_scan_res 1")
  146. ssid = "test-wps-nfc-handover-init"
  147. hapd = hostapd.add_ap(apdev[0],
  148. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  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. dev[0].wait_connected(timeout=15)
  164. check_wpa2_connection(dev[0], apdev[0], hapd, ssid, mixed=True)
  165. with alloc_fail(hapd, 1, "wps_build_nfc_handover_sel"):
  166. if "FAIL" not in hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR"):
  167. raise Exception("Unexpected NFC_GET_HANDOVER_SEL success during OOM")
  168. def test_nfc_wps_handover_errors(dev, apdev):
  169. """WPS AP NFC handover report error cases"""
  170. ssid = "test-wps-nfc-handover"
  171. hapd = hostapd.add_ap(apdev[0],
  172. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  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. hapd = hostapd.add_ap(apdev[0], params)
  201. logger.info("NFC connection handover")
  202. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  203. if "FAIL" in req:
  204. raise Exception("Failed to generate NFC connection handover request")
  205. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  206. if "FAIL" in sel:
  207. raise Exception("Failed to generate NFC connection handover select")
  208. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  209. if "FAIL" in res:
  210. raise Exception("Failed to report NFC connection handover to to hostapd")
  211. dev[0].dump_monitor()
  212. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  213. if "FAIL" in res:
  214. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  215. dev[0].wait_connected(timeout=30)
  216. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  217. def test_nfc_wps_handover_5ghz(dev, apdev):
  218. """Connect to WPS AP with NFC connection handover on 5 GHz band"""
  219. try:
  220. ssid = "test-wps-nfc-handover"
  221. params = ap_wps_params(ssid)
  222. params["country_code"] = "FI"
  223. params["hw_mode"] = "a"
  224. params["channel"] = "36"
  225. hapd = hostapd.add_ap(apdev[0], params)
  226. logger.info("NFC connection handover")
  227. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  228. if "FAIL" in req:
  229. raise Exception("Failed to generate NFC connection handover request")
  230. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  231. if "FAIL" in sel:
  232. raise Exception("Failed to generate NFC connection handover select")
  233. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  234. if "FAIL" in res:
  235. raise Exception("Failed to report NFC connection handover to to hostapd")
  236. dev[0].dump_monitor()
  237. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  238. if "FAIL" in res:
  239. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  240. dev[0].wait_connected(timeout=30)
  241. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  242. finally:
  243. dev[0].request("DISCONNECT")
  244. if hapd:
  245. hapd.request("DISABLE")
  246. subprocess.call(['iw', 'reg', 'set', '00'])
  247. dev[0].flush_scan_cache()
  248. def test_nfc_wps_handover_chan14(dev, apdev):
  249. """Connect to WPS AP with NFC connection handover on channel 14"""
  250. try:
  251. ssid = "test-wps-nfc-handover"
  252. params = ap_wps_params(ssid)
  253. params["country_code"] = "JP"
  254. params["hw_mode"] = "b"
  255. params["channel"] = "14"
  256. hapd = hostapd.add_ap(apdev[0], params)
  257. logger.info("NFC connection handover")
  258. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  259. if "FAIL" in req:
  260. raise Exception("Failed to generate NFC connection handover request")
  261. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  262. if "FAIL" in sel:
  263. raise Exception("Failed to generate NFC connection handover select")
  264. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  265. if "FAIL" in res:
  266. raise Exception("Failed to report NFC connection handover to to hostapd")
  267. dev[0].dump_monitor()
  268. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  269. if "FAIL" in res:
  270. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  271. dev[0].wait_connected(timeout=30)
  272. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  273. finally:
  274. dev[0].request("DISCONNECT")
  275. if hapd:
  276. hapd.request("DISABLE")
  277. subprocess.call(['iw', 'reg', 'set', '00'])
  278. dev[0].flush_scan_cache()
  279. def test_nfc_wps_handover_with_pw_token_set(dev, apdev):
  280. """Connect to WPS AP with NFC connection handover (wps_nfc_* set)"""
  281. ssid = "test-wps-nfc-handover2"
  282. params = ap_wps_params(ssid)
  283. hapd = hostapd.add_ap(apdev[0], params)
  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. dev[0].wait_connected(timeout=15)
  306. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  307. def test_nfc_wps_handover_pk_hash_mismatch_sta(dev, apdev):
  308. """WPS NFC connection handover with invalid pkhash from station (negative)"""
  309. ssid = "wps-nfc-handover-pkhash-sta"
  310. if "FAIL" in dev[0].request("SET wps_corrupt_pkhash 1"):
  311. raise Exception("Could not enable wps_corrupt_pkhash")
  312. params = ap_wps_params(ssid)
  313. hapd = hostapd.add_ap(apdev[0], params)
  314. logger.info("NFC connection handover")
  315. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  316. if "FAIL" in req:
  317. raise Exception("Failed to generate NFC connection handover request")
  318. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  319. if "FAIL" in sel:
  320. raise Exception("Failed to generate NFC connection handover select")
  321. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  322. if "FAIL" in res:
  323. raise Exception("Failed to report NFC connection handover to to hostapd")
  324. dev[0].dump_monitor()
  325. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  326. if "FAIL" in res:
  327. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  328. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "WPS-FAIL"], timeout=15)
  329. if ev is None:
  330. raise Exception("Timed out")
  331. if "WPS-FAIL" not in ev:
  332. raise Exception("Public key hash mismatch not detected")
  333. def test_nfc_wps_handover_pk_hash_mismatch_ap(dev, apdev):
  334. """WPS NFC connection handover with invalid pkhash from AP (negative)"""
  335. ssid = "wps-nfc-handover-pkhash-ap"
  336. params = ap_wps_params(ssid)
  337. hapd = hostapd.add_ap(apdev[0], params)
  338. if "FAIL" in hapd.request("SET wps_corrupt_pkhash 1"):
  339. raise Exception("Could not enable wps_corrupt_pkhash")
  340. logger.info("NFC connection handover")
  341. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  342. if "FAIL" in req:
  343. raise Exception("Failed to generate NFC connection handover request")
  344. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  345. if "FAIL" in sel:
  346. raise Exception("Failed to generate NFC connection handover select")
  347. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  348. if "FAIL" in res:
  349. raise Exception("Failed to report NFC connection handover to to hostapd")
  350. dev[0].dump_monitor()
  351. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  352. if "FAIL" in res:
  353. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  354. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "WPS-FAIL"], timeout=15)
  355. if ev is None:
  356. raise Exception("Timed out")
  357. if "WPS-FAIL" not in ev:
  358. raise Exception("Public key hash mismatch not detected")
  359. def start_ap_er(er, ap, ssid):
  360. ap_pin = "12345670"
  361. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  362. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  363. "wpa_passphrase": "12345678", "wpa": "2",
  364. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  365. "device_name": "Wireless AP", "manufacturer": "Company",
  366. "model_name": "WAP", "model_number": "123",
  367. "serial_number": "12345", "device_type": "6-0050F204-1",
  368. "os_version": "01020300",
  369. "config_methods": "label push_button",
  370. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"}
  371. hapd = hostapd.add_ap(ap, params)
  372. logger.info("Learn AP configuration")
  373. er.dump_monitor()
  374. try:
  375. er.request("SET ignore_old_scan_res 1")
  376. er.wps_reg(ap['bssid'], ap_pin)
  377. finally:
  378. er.request("SET ignore_old_scan_res 0")
  379. logger.info("Start ER")
  380. er.request("WPS_ER_STOP")
  381. time.sleep(1)
  382. er.request("WPS_ER_START ifname=lo")
  383. ev = er.wait_event(["WPS-ER-AP-ADD"], timeout=15)
  384. if ev is None:
  385. raise Exception("AP discovery timed out")
  386. if ap_uuid not in ev:
  387. raise Exception("Expected AP UUID not found")
  388. logger.info("Use learned network configuration on ER")
  389. er.request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  390. return hapd
  391. def test_nfc_wps_er_pw_token(dev, apdev):
  392. """WPS NFC password token from Enrollee to ER"""
  393. try:
  394. _test_nfc_wps_er_pw_token(dev, apdev)
  395. finally:
  396. dev[0].request("WPS_ER_STOP")
  397. dev[1].request("SET ignore_old_scan_res 0")
  398. def _test_nfc_wps_er_pw_token(dev, apdev):
  399. ssid = "wps-nfc-er-pw-token"
  400. hapd = start_ap_er(dev[0], apdev[0], ssid)
  401. logger.info("WPS provisioning step using password token from station")
  402. dev[1].request("SET ignore_old_scan_res 1")
  403. pw = dev[1].request("WPS_NFC_TOKEN NDEF").rstrip()
  404. if "FAIL" in pw:
  405. raise Exception("Failed to generate password token")
  406. res = dev[0].request("WPS_NFC_TAG_READ " + pw)
  407. if "FAIL" in res:
  408. raise Exception("Failed to provide NFC tag contents to WPS ER")
  409. dev[0].dump_monitor()
  410. res = dev[1].request("WPS_NFC")
  411. if "FAIL" in res:
  412. raise Exception("Failed to start Enrollee using NFC password token")
  413. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  414. if ev is None:
  415. raise Exception("WPS ER did not report success")
  416. dev[1].wait_connected(timeout=15)
  417. check_wpa2_connection(dev[1], apdev[0], hapd, ssid)
  418. def test_nfc_wps_er_config_token(dev, apdev):
  419. """WPS NFC configuration token from ER to Enrollee"""
  420. try:
  421. _test_nfc_wps_er_config_token(dev, apdev)
  422. finally:
  423. dev[0].request("WPS_ER_STOP")
  424. dev[1].request("SET ignore_old_scan_res 0")
  425. def _test_nfc_wps_er_config_token(dev, apdev):
  426. ssid = "wps-nfc-er-config-token"
  427. hapd = start_ap_er(dev[0], apdev[0], ssid)
  428. logger.info("WPS provisioning step using configuration token from ER")
  429. wps = dev[0].request("WPS_ER_NFC_CONFIG_TOKEN WPS " + apdev[0]['bssid']).rstrip()
  430. if "FAIL" in wps:
  431. raise Exception("Failed to generate configuration token (WPS format)")
  432. conf = dev[0].request("WPS_ER_NFC_CONFIG_TOKEN NDEF " + apdev[0]['bssid']).rstrip()
  433. if "FAIL" in conf:
  434. raise Exception("Failed to generate configuration token")
  435. dev[1].request("SET ignore_old_scan_res 1")
  436. res = dev[1].request("WPS_NFC_TAG_READ " + conf)
  437. if "FAIL" in res:
  438. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  439. dev[1].wait_connected(timeout=15)
  440. check_wpa2_connection(dev[1], apdev[0], hapd, ssid)
  441. def test_nfc_wps_er_handover(dev, apdev):
  442. """WPS NFC connection handover between Enrollee and ER"""
  443. try:
  444. _test_nfc_wps_er_handover(dev, apdev)
  445. finally:
  446. dev[0].request("WPS_ER_STOP")
  447. def _test_nfc_wps_er_handover(dev, apdev):
  448. ssid = "wps-nfc-er-handover"
  449. hapd = start_ap_er(dev[0], apdev[0], ssid)
  450. logger.info("WPS provisioning step using connection handover")
  451. req = dev[1].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  452. if "FAIL" in req:
  453. raise Exception("Failed to generate NFC connection handover request")
  454. sel = dev[0].request("NFC_GET_HANDOVER_SEL NDEF WPS-CR " + apdev[0]['bssid']).rstrip()
  455. if "FAIL" in sel:
  456. raise Exception("Failed to generate NFC connection handover select")
  457. res = dev[0].request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  458. if "FAIL" in res:
  459. raise Exception("Failed to report NFC connection handover to to hostapd")
  460. dev[1].dump_monitor()
  461. res = dev[1].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  462. if "FAIL" in res:
  463. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  464. dev[1].wait_connected(timeout=15)
  465. check_wpa2_connection(dev[1], apdev[0], hapd, ssid)
  466. def test_nfc_wps_er_handover_pk_hash_mismatch_sta(dev, apdev):
  467. """WPS NFC connection handover with invalid pkhash from station to ER (negative)"""
  468. try:
  469. _test_nfc_wps_er_handover_pk_hash_mismatch_sta(dev, apdev)
  470. finally:
  471. dev[0].request("WPS_ER_STOP")
  472. dev[1].request("SET ignore_old_scan_res 0")
  473. def _test_nfc_wps_er_handover_pk_hash_mismatch_sta(dev, apdev):
  474. ssid = "wps-nfc-er-handover-pkhash-sta"
  475. hapd = start_ap_er(dev[0], apdev[0], ssid)
  476. logger.info("WPS provisioning step using connection handover")
  477. if "FAIL" in dev[1].request("SET wps_corrupt_pkhash 1"):
  478. raise Exception("Could not enable wps_corrupt_pkhash")
  479. dev[1].request("SET ignore_old_scan_res 1")
  480. req = dev[1].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  481. if "FAIL" in req:
  482. raise Exception("Failed to generate NFC connection handover request")
  483. sel = dev[0].request("NFC_GET_HANDOVER_SEL NDEF WPS-CR " + apdev[0]['bssid']).rstrip()
  484. if "FAIL" in sel:
  485. raise Exception("Failed to generate NFC connection handover select")
  486. res = dev[0].request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  487. if "FAIL" in res:
  488. raise Exception("Failed to report NFC connection handover to to hostapd")
  489. dev[1].dump_monitor()
  490. res = dev[1].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  491. if "FAIL" in res:
  492. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  493. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED", "WPS-FAIL"], timeout=15)
  494. if ev is None:
  495. raise Exception("Timed out")
  496. if "WPS-FAIL" not in ev:
  497. raise Exception("Public key hash mismatch not detected")
  498. def test_nfc_wps_er_handover_pk_hash_mismatch_er(dev, apdev):
  499. """WPS NFC connection handover with invalid pkhash from ER to station (negative)"""
  500. try:
  501. _test_nfc_wps_er_handover_pk_hash_mismatch_er(dev, apdev)
  502. finally:
  503. dev[0].request("WPS_ER_STOP")
  504. dev[1].request("SET ignore_old_scan_res 0")
  505. def _test_nfc_wps_er_handover_pk_hash_mismatch_er(dev, apdev):
  506. ssid = "wps-nfc-er-handover-pkhash-er"
  507. hapd = start_ap_er(dev[0], apdev[0], ssid)
  508. logger.info("WPS provisioning step using connection handover")
  509. if "FAIL" in dev[0].request("SET wps_corrupt_pkhash 1"):
  510. raise Exception("Could not enable wps_corrupt_pkhash")
  511. dev[1].request("SET ignore_old_scan_res 1")
  512. req = dev[1].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  513. if "FAIL" in req:
  514. raise Exception("Failed to generate NFC connection handover request")
  515. sel = dev[0].request("NFC_GET_HANDOVER_SEL NDEF WPS-CR " + apdev[0]['bssid']).rstrip()
  516. if "FAIL" in sel:
  517. raise Exception("Failed to generate NFC connection handover select")
  518. res = dev[0].request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  519. if "FAIL" in res:
  520. raise Exception("Failed to report NFC connection handover to to hostapd")
  521. dev[1].dump_monitor()
  522. res = dev[1].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  523. if "FAIL" in res:
  524. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  525. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED", "WPS-FAIL"], timeout=15)
  526. if ev is None:
  527. raise Exception("Timed out")
  528. if "WPS-FAIL" not in ev:
  529. raise Exception("Public key hash mismatch not detected")
  530. def test_nfc_invalid_ndef_record(dev, apdev):
  531. """Invalid NFC NDEF record handling"""
  532. tests = [ "11223344",
  533. "00112233",
  534. "0000112233445566",
  535. "0800112233445566",
  536. "080011223344",
  537. "18000000",
  538. "18010000",
  539. "90000050",
  540. "9000005000",
  541. "9001013344",
  542. "98010101334455",
  543. "0017ffffffe3",
  544. "0017ffffffe4",
  545. "0017ffffffe9",
  546. "0000fffffffa",
  547. "0017ffffffe46170706c69636174696f6e2f766e642e7766612e777363",
  548. "0017ffffffff6170706c69636174696f6e2f766e642e7766612e777363",
  549. "0017000000006170706c69636174696f6e2f766e642e7766612e7773ff",
  550. "080000000000" ]
  551. for test in tests:
  552. if "FAIL" not in dev[0].request("WPS_NFC_TAG_READ " + test):
  553. raise Exception("Invalid tag accepted: " + test)