test_nfc_wps.py 29 KB

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