test_nfc_wps.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. hapd = None
  220. try:
  221. ssid = "test-wps-nfc-handover"
  222. params = ap_wps_params(ssid)
  223. params["country_code"] = "FI"
  224. params["hw_mode"] = "a"
  225. params["channel"] = "36"
  226. hapd = hostapd.add_ap(apdev[0], params)
  227. logger.info("NFC connection handover")
  228. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  229. if "FAIL" in req:
  230. raise Exception("Failed to generate NFC connection handover request")
  231. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  232. if "FAIL" in sel:
  233. raise Exception("Failed to generate NFC connection handover select")
  234. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  235. if "FAIL" in res:
  236. raise Exception("Failed to report NFC connection handover to to hostapd")
  237. dev[0].dump_monitor()
  238. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  239. if "FAIL" in res:
  240. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  241. dev[0].wait_connected(timeout=30)
  242. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  243. finally:
  244. dev[0].request("DISCONNECT")
  245. if hapd:
  246. hapd.request("DISABLE")
  247. subprocess.call(['iw', 'reg', 'set', '00'])
  248. dev[0].flush_scan_cache()
  249. def test_nfc_wps_handover_chan14(dev, apdev):
  250. """Connect to WPS AP with NFC connection handover on channel 14"""
  251. hapd = None
  252. try:
  253. ssid = "test-wps-nfc-handover"
  254. params = ap_wps_params(ssid)
  255. params["country_code"] = "JP"
  256. params["hw_mode"] = "b"
  257. params["channel"] = "14"
  258. hapd = hostapd.add_ap(apdev[0], params)
  259. logger.info("NFC connection handover")
  260. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  261. if "FAIL" in req:
  262. raise Exception("Failed to generate NFC connection handover request")
  263. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  264. if "FAIL" in sel:
  265. raise Exception("Failed to generate NFC connection handover select")
  266. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  267. if "FAIL" in res:
  268. raise Exception("Failed to report NFC connection handover to to hostapd")
  269. dev[0].dump_monitor()
  270. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  271. if "FAIL" in res:
  272. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  273. dev[0].wait_connected(timeout=30)
  274. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  275. finally:
  276. dev[0].request("DISCONNECT")
  277. if hapd:
  278. hapd.request("DISABLE")
  279. subprocess.call(['iw', 'reg', 'set', '00'])
  280. dev[0].flush_scan_cache()
  281. def test_nfc_wps_handover_with_pw_token_set(dev, apdev):
  282. """Connect to WPS AP with NFC connection handover (wps_nfc_* set)"""
  283. ssid = "test-wps-nfc-handover2"
  284. params = ap_wps_params(ssid)
  285. hapd = hostapd.add_ap(apdev[0], params)
  286. # enable a password token (which won't be used in this test case)
  287. pw = hapd.request("WPS_NFC_TOKEN NDEF").rstrip()
  288. if "FAIL" in pw:
  289. raise Exception("Failed to generate password token")
  290. res = hapd.request("WPS_NFC_TOKEN enable")
  291. if "FAIL" in pw:
  292. raise Exception("Failed to enable AP password token")
  293. logger.info("NFC connection handover")
  294. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  295. if "FAIL" in req:
  296. raise Exception("Failed to generate NFC connection handover request")
  297. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  298. if "FAIL" in sel:
  299. raise Exception("Failed to generate NFC connection handover select")
  300. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  301. if "FAIL" in res:
  302. raise Exception("Failed to report NFC connection handover to to hostapd")
  303. dev[0].dump_monitor()
  304. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  305. if "FAIL" in res:
  306. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  307. dev[0].wait_connected(timeout=15)
  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. hapd = hostapd.add_ap(apdev[0], params)
  316. logger.info("NFC connection handover")
  317. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  318. if "FAIL" in req:
  319. raise Exception("Failed to generate NFC connection handover request")
  320. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  321. if "FAIL" in sel:
  322. raise Exception("Failed to generate NFC connection handover select")
  323. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  324. if "FAIL" in res:
  325. raise Exception("Failed to report NFC connection handover to to hostapd")
  326. dev[0].dump_monitor()
  327. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  328. if "FAIL" in res:
  329. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  330. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "WPS-FAIL"], timeout=15)
  331. if ev is None:
  332. raise Exception("Timed out")
  333. if "WPS-FAIL" not in ev:
  334. raise Exception("Public key hash mismatch not detected")
  335. def test_nfc_wps_handover_pk_hash_mismatch_ap(dev, apdev):
  336. """WPS NFC connection handover with invalid pkhash from AP (negative)"""
  337. ssid = "wps-nfc-handover-pkhash-ap"
  338. params = ap_wps_params(ssid)
  339. hapd = hostapd.add_ap(apdev[0], params)
  340. if "FAIL" in hapd.request("SET wps_corrupt_pkhash 1"):
  341. raise Exception("Could not enable wps_corrupt_pkhash")
  342. logger.info("NFC connection handover")
  343. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  344. if "FAIL" in req:
  345. raise Exception("Failed to generate NFC connection handover request")
  346. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  347. if "FAIL" in sel:
  348. raise Exception("Failed to generate NFC connection handover select")
  349. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  350. if "FAIL" in res:
  351. raise Exception("Failed to report NFC connection handover to to hostapd")
  352. dev[0].dump_monitor()
  353. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  354. if "FAIL" in res:
  355. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  356. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "WPS-FAIL"], timeout=15)
  357. if ev is None:
  358. raise Exception("Timed out")
  359. if "WPS-FAIL" not in ev:
  360. raise Exception("Public key hash mismatch not detected")
  361. def start_ap_er(er, ap, ssid):
  362. ap_pin = "12345670"
  363. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  364. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  365. "wpa_passphrase": "12345678", "wpa": "2",
  366. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  367. "device_name": "Wireless AP", "manufacturer": "Company",
  368. "model_name": "WAP", "model_number": "123",
  369. "serial_number": "12345", "device_type": "6-0050F204-1",
  370. "os_version": "01020300",
  371. "config_methods": "label push_button",
  372. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"}
  373. hapd = hostapd.add_ap(ap, params)
  374. logger.info("Learn AP configuration")
  375. er.dump_monitor()
  376. try:
  377. er.request("SET ignore_old_scan_res 1")
  378. er.wps_reg(ap['bssid'], ap_pin)
  379. finally:
  380. er.request("SET ignore_old_scan_res 0")
  381. logger.info("Start ER")
  382. er.request("WPS_ER_STOP")
  383. time.sleep(1)
  384. er.request("WPS_ER_START ifname=lo")
  385. ev = er.wait_event(["WPS-ER-AP-ADD"], timeout=15)
  386. if ev is None:
  387. raise Exception("AP discovery timed out")
  388. if ap_uuid not in ev:
  389. raise Exception("Expected AP UUID not found")
  390. logger.info("Use learned network configuration on ER")
  391. er.request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  392. return hapd
  393. def test_nfc_wps_er_pw_token(dev, apdev):
  394. """WPS NFC password token from Enrollee to ER"""
  395. try:
  396. _test_nfc_wps_er_pw_token(dev, apdev)
  397. finally:
  398. dev[0].request("WPS_ER_STOP")
  399. dev[1].request("SET ignore_old_scan_res 0")
  400. def _test_nfc_wps_er_pw_token(dev, apdev):
  401. ssid = "wps-nfc-er-pw-token"
  402. hapd = start_ap_er(dev[0], apdev[0], ssid)
  403. logger.info("WPS provisioning step using password token from station")
  404. dev[1].request("SET ignore_old_scan_res 1")
  405. pw = dev[1].request("WPS_NFC_TOKEN NDEF").rstrip()
  406. if "FAIL" in pw:
  407. raise Exception("Failed to generate password token")
  408. res = dev[0].request("WPS_NFC_TAG_READ " + pw)
  409. if "FAIL" in res:
  410. raise Exception("Failed to provide NFC tag contents to WPS ER")
  411. dev[0].dump_monitor()
  412. res = dev[1].request("WPS_NFC")
  413. if "FAIL" in res:
  414. raise Exception("Failed to start Enrollee using NFC password token")
  415. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  416. if ev is None:
  417. raise Exception("WPS ER did not report success")
  418. dev[1].wait_connected(timeout=15)
  419. check_wpa2_connection(dev[1], apdev[0], hapd, ssid)
  420. def test_nfc_wps_er_config_token(dev, apdev):
  421. """WPS NFC configuration token from ER to Enrollee"""
  422. try:
  423. _test_nfc_wps_er_config_token(dev, apdev)
  424. finally:
  425. dev[0].request("WPS_ER_STOP")
  426. dev[1].request("SET ignore_old_scan_res 0")
  427. def _test_nfc_wps_er_config_token(dev, apdev):
  428. ssid = "wps-nfc-er-config-token"
  429. hapd = start_ap_er(dev[0], apdev[0], ssid)
  430. logger.info("WPS provisioning step using configuration token from ER")
  431. wps = dev[0].request("WPS_ER_NFC_CONFIG_TOKEN WPS " + apdev[0]['bssid']).rstrip()
  432. if "FAIL" in wps:
  433. raise Exception("Failed to generate configuration token (WPS format)")
  434. conf = dev[0].request("WPS_ER_NFC_CONFIG_TOKEN NDEF " + apdev[0]['bssid']).rstrip()
  435. if "FAIL" in conf:
  436. raise Exception("Failed to generate configuration token")
  437. dev[1].request("SET ignore_old_scan_res 1")
  438. res = dev[1].request("WPS_NFC_TAG_READ " + conf)
  439. if "FAIL" in res:
  440. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  441. dev[1].wait_connected(timeout=15)
  442. check_wpa2_connection(dev[1], apdev[0], hapd, ssid)
  443. def test_nfc_wps_er_handover(dev, apdev):
  444. """WPS NFC connection handover between Enrollee and ER"""
  445. try:
  446. _test_nfc_wps_er_handover(dev, apdev)
  447. finally:
  448. dev[0].request("WPS_ER_STOP")
  449. def _test_nfc_wps_er_handover(dev, apdev):
  450. ssid = "wps-nfc-er-handover"
  451. hapd = start_ap_er(dev[0], apdev[0], ssid)
  452. logger.info("WPS provisioning step using connection handover")
  453. req = dev[1].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  454. if "FAIL" in req:
  455. raise Exception("Failed to generate NFC connection handover request")
  456. sel = dev[0].request("NFC_GET_HANDOVER_SEL NDEF WPS-CR " + apdev[0]['bssid']).rstrip()
  457. if "FAIL" in sel:
  458. raise Exception("Failed to generate NFC connection handover select")
  459. res = dev[0].request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  460. if "FAIL" in res:
  461. raise Exception("Failed to report NFC connection handover to to hostapd")
  462. dev[1].dump_monitor()
  463. res = dev[1].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  464. if "FAIL" in res:
  465. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  466. dev[1].wait_connected(timeout=15)
  467. check_wpa2_connection(dev[1], apdev[0], hapd, ssid)
  468. def test_nfc_wps_er_handover_pk_hash_mismatch_sta(dev, apdev):
  469. """WPS NFC connection handover with invalid pkhash from station to ER (negative)"""
  470. try:
  471. _test_nfc_wps_er_handover_pk_hash_mismatch_sta(dev, apdev)
  472. finally:
  473. dev[0].request("WPS_ER_STOP")
  474. dev[1].request("SET ignore_old_scan_res 0")
  475. def _test_nfc_wps_er_handover_pk_hash_mismatch_sta(dev, apdev):
  476. ssid = "wps-nfc-er-handover-pkhash-sta"
  477. hapd = start_ap_er(dev[0], apdev[0], ssid)
  478. logger.info("WPS provisioning step using connection handover")
  479. if "FAIL" in dev[1].request("SET wps_corrupt_pkhash 1"):
  480. raise Exception("Could not enable wps_corrupt_pkhash")
  481. dev[1].request("SET ignore_old_scan_res 1")
  482. req = dev[1].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  483. if "FAIL" in req:
  484. raise Exception("Failed to generate NFC connection handover request")
  485. sel = dev[0].request("NFC_GET_HANDOVER_SEL NDEF WPS-CR " + apdev[0]['bssid']).rstrip()
  486. if "FAIL" in sel:
  487. raise Exception("Failed to generate NFC connection handover select")
  488. res = dev[0].request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  489. if "FAIL" in res:
  490. raise Exception("Failed to report NFC connection handover to to hostapd")
  491. dev[1].dump_monitor()
  492. res = dev[1].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  493. if "FAIL" in res:
  494. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  495. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED", "WPS-FAIL"], timeout=15)
  496. if ev is None:
  497. raise Exception("Timed out")
  498. if "WPS-FAIL" not in ev:
  499. raise Exception("Public key hash mismatch not detected")
  500. def test_nfc_wps_er_handover_pk_hash_mismatch_er(dev, apdev):
  501. """WPS NFC connection handover with invalid pkhash from ER to station (negative)"""
  502. try:
  503. _test_nfc_wps_er_handover_pk_hash_mismatch_er(dev, apdev)
  504. finally:
  505. dev[0].request("WPS_ER_STOP")
  506. dev[1].request("SET ignore_old_scan_res 0")
  507. def _test_nfc_wps_er_handover_pk_hash_mismatch_er(dev, apdev):
  508. ssid = "wps-nfc-er-handover-pkhash-er"
  509. hapd = start_ap_er(dev[0], apdev[0], ssid)
  510. logger.info("WPS provisioning step using connection handover")
  511. if "FAIL" in dev[0].request("SET wps_corrupt_pkhash 1"):
  512. raise Exception("Could not enable wps_corrupt_pkhash")
  513. dev[1].request("SET ignore_old_scan_res 1")
  514. req = dev[1].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  515. if "FAIL" in req:
  516. raise Exception("Failed to generate NFC connection handover request")
  517. sel = dev[0].request("NFC_GET_HANDOVER_SEL NDEF WPS-CR " + apdev[0]['bssid']).rstrip()
  518. if "FAIL" in sel:
  519. raise Exception("Failed to generate NFC connection handover select")
  520. res = dev[0].request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  521. if "FAIL" in res:
  522. raise Exception("Failed to report NFC connection handover to to hostapd")
  523. dev[1].dump_monitor()
  524. res = dev[1].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  525. if "FAIL" in res:
  526. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  527. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED", "WPS-FAIL"], timeout=15)
  528. if ev is None:
  529. raise Exception("Timed out")
  530. if "WPS-FAIL" not in ev:
  531. raise Exception("Public key hash mismatch not detected")
  532. def test_nfc_invalid_ndef_record(dev, apdev):
  533. """Invalid NFC NDEF record handling"""
  534. tests = [ "11223344",
  535. "00112233",
  536. "0000112233445566",
  537. "0800112233445566",
  538. "080011223344",
  539. "18000000",
  540. "18010000",
  541. "90000050",
  542. "9000005000",
  543. "9001013344",
  544. "98010101334455",
  545. "0017ffffffe3",
  546. "0017ffffffe4",
  547. "0017ffffffe9",
  548. "0000fffffffa",
  549. "0017ffffffe46170706c69636174696f6e2f766e642e7766612e777363",
  550. "0017ffffffff6170706c69636174696f6e2f766e642e7766612e777363",
  551. "0017000000006170706c69636174696f6e2f766e642e7766612e7773ff",
  552. "080000000000" ]
  553. for test in tests:
  554. if "FAIL" not in dev[0].request("WPS_NFC_TAG_READ " + test):
  555. raise Exception("Invalid tag accepted: " + test)