test_nfc_wps.py 28 KB

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