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. hostapd.add_ap(apdev[0]['ifname'], params)
  37. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  38. logger.info("WPS provisioning step using password token from station")
  39. wps = dev[0].request("WPS_NFC_TOKEN WPS").rstrip()
  40. if "FAIL" in wps:
  41. raise Exception("Failed to generate password token (WPS only)")
  42. pw = dev[0].request("WPS_NFC_TOKEN NDEF").rstrip()
  43. if "FAIL" in pw:
  44. raise Exception("Failed to generate password token")
  45. res = hapd.request("WPS_NFC_TAG_READ " + pw)
  46. if "FAIL" in res:
  47. raise Exception("Failed to provide NFC tag contents to hostapd")
  48. dev[0].dump_monitor()
  49. res = dev[0].request("WPS_NFC")
  50. if "FAIL" in res:
  51. raise Exception("Failed to start Enrollee using NFC password token")
  52. dev[0].wait_connected(timeout=30)
  53. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  54. def test_nfc_wps_config_token(dev, apdev):
  55. """NFC tag with configuration token from AP"""
  56. ssid = "test-wps-nfc-conf-token"
  57. params = ap_wps_params(ssid)
  58. hostapd.add_ap(apdev[0]['ifname'], params)
  59. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  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. hostapd.add_ap(apdev[0]['ifname'],
  78. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  79. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  80. logger.info("NFC configuration token from AP to station")
  81. conf = hapd.request("WPS_NFC_CONFIG_TOKEN NDEF").rstrip()
  82. if "FAIL" in conf:
  83. raise Exception("Failed to generate configuration token")
  84. dev[0].dump_monitor()
  85. res = dev[0].request("WPS_NFC_TAG_READ " + conf)
  86. if "FAIL" in res:
  87. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  88. dev[0].wait_connected(timeout=15)
  89. check_wpa2_connection(dev[0], apdev[0], hapd, ssid, mixed=True)
  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. hostapd.add_ap(apdev[0]['ifname'],
  94. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  95. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  96. logger.info("WPS provisioning step using password token from station")
  97. pw = dev[0].request("WPS_NFC_TOKEN NDEF").rstrip()
  98. if "FAIL" in pw:
  99. raise Exception("Failed to generate password token")
  100. res = hapd.request("WPS_NFC_TAG_READ " + pw)
  101. if "FAIL" in res:
  102. raise Exception("Failed to provide NFC tag contents to hostapd")
  103. dev[0].dump_monitor()
  104. res = dev[0].request("WPS_NFC")
  105. if "FAIL" in res:
  106. raise Exception("Failed to start Enrollee using NFC password token")
  107. dev[0].wait_connected(timeout=30)
  108. check_wpa2_connection(dev[0], apdev[0], hapd, ssid, mixed=True)
  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. hostapd.add_ap(apdev[0]['ifname'],
  113. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  114. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  115. logger.info("WPS configuration step")
  116. pw = hapd.request("WPS_NFC_TOKEN NDEF").rstrip()
  117. if "FAIL" in pw:
  118. raise Exception("Failed to generate password token")
  119. res = hapd.request("WPS_NFC_TOKEN enable")
  120. if "FAIL" in pw:
  121. raise Exception("Failed to enable AP password token")
  122. res = dev[0].request("WPS_NFC_TAG_READ " + pw)
  123. if "FAIL" in res:
  124. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  125. dev[0].dump_monitor()
  126. new_ssid = "test-wps-nfc-pw-token-new-ssid"
  127. new_passphrase = "1234567890"
  128. res = dev[0].request("WPS_REG " + apdev[0]['bssid'] + " nfc-pw " + new_ssid.encode("hex") + " WPA2PSK CCMP " + new_passphrase.encode("hex"))
  129. if "FAIL" in res:
  130. raise Exception("Failed to start Registrar using NFC password token")
  131. dev[0].wait_connected(timeout=30)
  132. check_wpa2_connection(dev[0], apdev[0], hapd, new_ssid, mixed=True)
  133. if "FAIL" in hapd.request("WPS_NFC_TOKEN disable"):
  134. raise Exception("Failed to disable AP password token")
  135. if "FAIL" in hapd.request("WPS_NFC_TOKEN WPS"):
  136. raise Exception("Unexpected WPS_NFC_TOKEN WPS failure")
  137. with fail_test(hapd, 1, "os_get_random;wps_nfc_token_gen"):
  138. if "FAIL" not in hapd.request("WPS_NFC_TOKEN WPS"):
  139. raise Exception("Unexpected WPS_NFC_TOKEN success")
  140. with fail_test(hapd, 2, "os_get_random;wps_nfc_token_gen"):
  141. if "FAIL" not in hapd.request("WPS_NFC_TOKEN WPS"):
  142. raise Exception("Unexpected WPS_NFC_TOKEN success")
  143. def test_nfc_wps_handover_init(dev, apdev):
  144. """Connect to WPS AP with NFC connection handover and move to configured state"""
  145. dev[0].request("SET ignore_old_scan_res 1")
  146. ssid = "test-wps-nfc-handover-init"
  147. hostapd.add_ap(apdev[0]['ifname'],
  148. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  149. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  150. logger.info("NFC connection handover")
  151. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  152. if "FAIL" in req:
  153. raise Exception("Failed to generate NFC connection handover request")
  154. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  155. if "FAIL" in sel:
  156. raise Exception("Failed to generate NFC connection handover select")
  157. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  158. if "FAIL" in res:
  159. raise Exception("Failed to report NFC connection handover to to hostapd")
  160. dev[0].dump_monitor()
  161. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  162. if "FAIL" in res:
  163. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  164. dev[0].wait_connected(timeout=15)
  165. check_wpa2_connection(dev[0], apdev[0], hapd, ssid, mixed=True)
  166. with alloc_fail(hapd, 1, "wps_build_nfc_handover_sel"):
  167. if "FAIL" not in hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR"):
  168. raise Exception("Unexpected NFC_GET_HANDOVER_SEL success during OOM")
  169. def test_nfc_wps_handover_errors(dev, apdev):
  170. """WPS AP NFC handover report error cases"""
  171. ssid = "test-wps-nfc-handover"
  172. hostapd.add_ap(apdev[0]['ifname'],
  173. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  174. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  175. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  176. if "FAIL" in sel:
  177. raise Exception("Failed to generate NFC connection handover select")
  178. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER "):
  179. raise Exception("Unexpected handover report success")
  180. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER RESP"):
  181. raise Exception("Unexpected handover report success")
  182. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER RESP WPS"):
  183. raise Exception("Unexpected handover report success")
  184. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER RESP WPS 001122"):
  185. raise Exception("Unexpected handover report success")
  186. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER RESP WPS 001122 00"):
  187. raise Exception("Unexpected handover report success")
  188. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER RESP WPS 0 00"):
  189. raise Exception("Unexpected handover report success")
  190. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER RESP WPS 001122 0"):
  191. raise Exception("Unexpected handover report success")
  192. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER RESP WPS 00q122 001122"):
  193. raise Exception("Unexpected handover report success")
  194. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER RESP WPS 001122 001q22"):
  195. raise Exception("Unexpected handover report success")
  196. if "FAIL" not in hapd.request("NFC_REPORT_HANDOVER RESP FOO 001122 00"):
  197. raise Exception("Unexpected handover report success")
  198. def test_nfc_wps_handover(dev, apdev):
  199. """Connect to WPS AP with NFC connection handover"""
  200. ssid = "test-wps-nfc-handover"
  201. params = ap_wps_params(ssid)
  202. hostapd.add_ap(apdev[0]['ifname'], params)
  203. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  204. logger.info("NFC connection handover")
  205. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  206. if "FAIL" in req:
  207. raise Exception("Failed to generate NFC connection handover request")
  208. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  209. if "FAIL" in sel:
  210. raise Exception("Failed to generate NFC connection handover select")
  211. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  212. if "FAIL" in res:
  213. raise Exception("Failed to report NFC connection handover to to hostapd")
  214. dev[0].dump_monitor()
  215. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  216. if "FAIL" in res:
  217. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  218. dev[0].wait_connected(timeout=30)
  219. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  220. def test_nfc_wps_handover_5ghz(dev, apdev):
  221. """Connect to WPS AP with NFC connection handover on 5 GHz band"""
  222. try:
  223. ssid = "test-wps-nfc-handover"
  224. params = ap_wps_params(ssid)
  225. params["country_code"] = "FI"
  226. params["hw_mode"] = "a"
  227. params["channel"] = "36"
  228. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  229. logger.info("NFC connection handover")
  230. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  231. if "FAIL" in req:
  232. raise Exception("Failed to generate NFC connection handover request")
  233. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  234. if "FAIL" in sel:
  235. raise Exception("Failed to generate NFC connection handover select")
  236. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  237. if "FAIL" in res:
  238. raise Exception("Failed to report NFC connection handover to to hostapd")
  239. dev[0].dump_monitor()
  240. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  241. if "FAIL" in res:
  242. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  243. dev[0].wait_connected(timeout=30)
  244. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  245. finally:
  246. dev[0].request("DISCONNECT")
  247. if hapd:
  248. hapd.request("DISABLE")
  249. subprocess.call(['iw', 'reg', 'set', '00'])
  250. dev[0].flush_scan_cache()
  251. def test_nfc_wps_handover_chan14(dev, apdev):
  252. """Connect to WPS AP with NFC connection handover on channel 14"""
  253. try:
  254. ssid = "test-wps-nfc-handover"
  255. params = ap_wps_params(ssid)
  256. params["country_code"] = "JP"
  257. params["hw_mode"] = "b"
  258. params["channel"] = "14"
  259. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  260. logger.info("NFC connection handover")
  261. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  262. if "FAIL" in req:
  263. raise Exception("Failed to generate NFC connection handover request")
  264. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  265. if "FAIL" in sel:
  266. raise Exception("Failed to generate NFC connection handover select")
  267. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  268. if "FAIL" in res:
  269. raise Exception("Failed to report NFC connection handover to to hostapd")
  270. dev[0].dump_monitor()
  271. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  272. if "FAIL" in res:
  273. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  274. dev[0].wait_connected(timeout=30)
  275. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  276. finally:
  277. dev[0].request("DISCONNECT")
  278. if hapd:
  279. hapd.request("DISABLE")
  280. subprocess.call(['iw', 'reg', 'set', '00'])
  281. dev[0].flush_scan_cache()
  282. def test_nfc_wps_handover_with_pw_token_set(dev, apdev):
  283. """Connect to WPS AP with NFC connection handover (wps_nfc_* set)"""
  284. ssid = "test-wps-nfc-handover2"
  285. params = ap_wps_params(ssid)
  286. hostapd.add_ap(apdev[0]['ifname'], params)
  287. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  288. # enable a password token (which won't be used in this test case)
  289. pw = hapd.request("WPS_NFC_TOKEN NDEF").rstrip()
  290. if "FAIL" in pw:
  291. raise Exception("Failed to generate password token")
  292. res = hapd.request("WPS_NFC_TOKEN enable")
  293. if "FAIL" in pw:
  294. raise Exception("Failed to enable AP password token")
  295. logger.info("NFC connection handover")
  296. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  297. if "FAIL" in req:
  298. raise Exception("Failed to generate NFC connection handover request")
  299. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  300. if "FAIL" in sel:
  301. raise Exception("Failed to generate NFC connection handover select")
  302. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  303. if "FAIL" in res:
  304. raise Exception("Failed to report NFC connection handover to to hostapd")
  305. dev[0].dump_monitor()
  306. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  307. if "FAIL" in res:
  308. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  309. dev[0].wait_connected(timeout=15)
  310. check_wpa2_connection(dev[0], apdev[0], hapd, ssid)
  311. def test_nfc_wps_handover_pk_hash_mismatch_sta(dev, apdev):
  312. """WPS NFC connection handover with invalid pkhash from station (negative)"""
  313. ssid = "wps-nfc-handover-pkhash-sta"
  314. if "FAIL" in dev[0].request("SET wps_corrupt_pkhash 1"):
  315. raise Exception("Could not enable wps_corrupt_pkhash")
  316. params = ap_wps_params(ssid)
  317. hostapd.add_ap(apdev[0]['ifname'], params)
  318. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  319. logger.info("NFC connection handover")
  320. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  321. if "FAIL" in req:
  322. raise Exception("Failed to generate NFC connection handover request")
  323. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  324. if "FAIL" in sel:
  325. raise Exception("Failed to generate NFC connection handover select")
  326. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  327. if "FAIL" in res:
  328. raise Exception("Failed to report NFC connection handover to to hostapd")
  329. dev[0].dump_monitor()
  330. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  331. if "FAIL" in res:
  332. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  333. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "WPS-FAIL"], timeout=15)
  334. if ev is None:
  335. raise Exception("Timed out")
  336. if "WPS-FAIL" not in ev:
  337. raise Exception("Public key hash mismatch not detected")
  338. def test_nfc_wps_handover_pk_hash_mismatch_ap(dev, apdev):
  339. """WPS NFC connection handover with invalid pkhash from AP (negative)"""
  340. ssid = "wps-nfc-handover-pkhash-ap"
  341. params = ap_wps_params(ssid)
  342. hostapd.add_ap(apdev[0]['ifname'], params)
  343. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  344. if "FAIL" in hapd.request("SET wps_corrupt_pkhash 1"):
  345. raise Exception("Could not enable wps_corrupt_pkhash")
  346. logger.info("NFC connection handover")
  347. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  348. if "FAIL" in req:
  349. raise Exception("Failed to generate NFC connection handover request")
  350. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  351. if "FAIL" in sel:
  352. raise Exception("Failed to generate NFC connection handover select")
  353. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  354. if "FAIL" in res:
  355. raise Exception("Failed to report NFC connection handover to to hostapd")
  356. dev[0].dump_monitor()
  357. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  358. if "FAIL" in res:
  359. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  360. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "WPS-FAIL"], timeout=15)
  361. if ev is None:
  362. raise Exception("Timed out")
  363. if "WPS-FAIL" not in ev:
  364. raise Exception("Public key hash mismatch not detected")
  365. def start_ap_er(er, ap, ssid):
  366. ap_pin = "12345670"
  367. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  368. hostapd.add_ap(ap['ifname'],
  369. { "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. logger.info("Learn AP configuration")
  379. er.dump_monitor()
  380. er.request("SET ignore_old_scan_res 1")
  381. er.wps_reg(ap['bssid'], ap_pin)
  382. logger.info("Start ER")
  383. er.request("WPS_ER_STOP")
  384. time.sleep(1)
  385. er.request("WPS_ER_START ifname=lo")
  386. ev = er.wait_event(["WPS-ER-AP-ADD"], timeout=15)
  387. if ev is None:
  388. raise Exception("AP discovery timed out")
  389. if ap_uuid not in ev:
  390. raise Exception("Expected AP UUID not found")
  391. logger.info("Use learned network configuration on ER")
  392. er.request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  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. def _test_nfc_wps_er_pw_token(dev, apdev):
  400. ssid = "wps-nfc-er-pw-token"
  401. start_ap_er(dev[0], apdev[0], ssid)
  402. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  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. def _test_nfc_wps_er_config_token(dev, apdev):
  427. ssid = "wps-nfc-er-config-token"
  428. start_ap_er(dev[0], apdev[0], ssid)
  429. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  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. start_ap_er(dev[0], apdev[0], ssid)
  452. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  453. logger.info("WPS provisioning step using connection handover")
  454. req = dev[1].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  455. if "FAIL" in req:
  456. raise Exception("Failed to generate NFC connection handover request")
  457. sel = dev[0].request("NFC_GET_HANDOVER_SEL NDEF WPS-CR " + apdev[0]['bssid']).rstrip()
  458. if "FAIL" in sel:
  459. raise Exception("Failed to generate NFC connection handover select")
  460. res = dev[0].request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  461. if "FAIL" in res:
  462. raise Exception("Failed to report NFC connection handover to to hostapd")
  463. dev[1].dump_monitor()
  464. res = dev[1].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  465. if "FAIL" in res:
  466. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  467. dev[1].wait_connected(timeout=15)
  468. check_wpa2_connection(dev[1], apdev[0], hapd, ssid)
  469. def test_nfc_wps_er_handover_pk_hash_mismatch_sta(dev, apdev):
  470. """WPS NFC connection handover with invalid pkhash from station to ER (negative)"""
  471. try:
  472. _test_nfc_wps_er_handover_pk_hash_mismatch_sta(dev, apdev)
  473. finally:
  474. dev[0].request("WPS_ER_STOP")
  475. def _test_nfc_wps_er_handover_pk_hash_mismatch_sta(dev, apdev):
  476. ssid = "wps-nfc-er-handover-pkhash-sta"
  477. start_ap_er(dev[0], apdev[0], ssid)
  478. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  479. logger.info("WPS provisioning step using connection handover")
  480. if "FAIL" in dev[1].request("SET wps_corrupt_pkhash 1"):
  481. raise Exception("Could not enable wps_corrupt_pkhash")
  482. dev[1].request("SET ignore_old_scan_res 1")
  483. req = dev[1].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  484. if "FAIL" in req:
  485. raise Exception("Failed to generate NFC connection handover request")
  486. sel = dev[0].request("NFC_GET_HANDOVER_SEL NDEF WPS-CR " + apdev[0]['bssid']).rstrip()
  487. if "FAIL" in sel:
  488. raise Exception("Failed to generate NFC connection handover select")
  489. res = dev[0].request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  490. if "FAIL" in res:
  491. raise Exception("Failed to report NFC connection handover to to hostapd")
  492. dev[1].dump_monitor()
  493. res = dev[1].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  494. if "FAIL" in res:
  495. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  496. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED", "WPS-FAIL"], timeout=15)
  497. if ev is None:
  498. raise Exception("Timed out")
  499. if "WPS-FAIL" not in ev:
  500. raise Exception("Public key hash mismatch not detected")
  501. def test_nfc_wps_er_handover_pk_hash_mismatch_er(dev, apdev):
  502. """WPS NFC connection handover with invalid pkhash from ER to station (negative)"""
  503. try:
  504. _test_nfc_wps_er_handover_pk_hash_mismatch_er(dev, apdev)
  505. finally:
  506. dev[0].request("WPS_ER_STOP")
  507. def _test_nfc_wps_er_handover_pk_hash_mismatch_er(dev, apdev):
  508. ssid = "wps-nfc-er-handover-pkhash-er"
  509. start_ap_er(dev[0], apdev[0], ssid)
  510. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  511. logger.info("WPS provisioning step using connection handover")
  512. if "FAIL" in dev[0].request("SET wps_corrupt_pkhash 1"):
  513. raise Exception("Could not enable wps_corrupt_pkhash")
  514. dev[1].request("SET ignore_old_scan_res 1")
  515. req = dev[1].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  516. if "FAIL" in req:
  517. raise Exception("Failed to generate NFC connection handover request")
  518. sel = dev[0].request("NFC_GET_HANDOVER_SEL NDEF WPS-CR " + apdev[0]['bssid']).rstrip()
  519. if "FAIL" in sel:
  520. raise Exception("Failed to generate NFC connection handover select")
  521. res = dev[0].request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  522. if "FAIL" in res:
  523. raise Exception("Failed to report NFC connection handover to to hostapd")
  524. dev[1].dump_monitor()
  525. res = dev[1].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  526. if "FAIL" in res:
  527. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  528. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED", "WPS-FAIL"], timeout=15)
  529. if ev is None:
  530. raise Exception("Timed out")
  531. if "WPS-FAIL" not in ev:
  532. raise Exception("Public key hash mismatch not detected")
  533. def test_nfc_invalid_ndef_record(dev, apdev):
  534. """Invalid NFC NDEF record handling"""
  535. tests = [ "11223344",
  536. "00112233",
  537. "0000112233445566",
  538. "0800112233445566",
  539. "080011223344",
  540. "18000000",
  541. "18010000",
  542. "90000050",
  543. "9000005000",
  544. "9001013344",
  545. "98010101334455",
  546. "0017ffffffe3",
  547. "0017ffffffe4",
  548. "0017ffffffe9",
  549. "0000fffffffa",
  550. "0017ffffffe46170706c69636174696f6e2f766e642e7766612e777363",
  551. "0017ffffffff6170706c69636174696f6e2f766e642e7766612e777363",
  552. "0017000000006170706c69636174696f6e2f766e642e7766612e7773ff",
  553. "080000000000" ]
  554. for test in tests:
  555. if "FAIL" not in dev[0].request("WPS_NFC_TAG_READ " + test):
  556. raise Exception("Invalid tag accepted: " + test)