test_nfc_wps.py 27 KB

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