test_nfc_wps.py 26 KB

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