test_nfc_wps.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/python
  2. #
  3. # WPS+NFC tests
  4. # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
  5. #
  6. # This software may be distributed under the terms of the BSD license.
  7. # See README for more details.
  8. import time
  9. import subprocess
  10. import logging
  11. logger = logging.getLogger()
  12. import hwsim_utils
  13. import hostapd
  14. def check_wpa2_connection(sta, ap, 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.ifname, ap['ifname'])
  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. def test_nfc_wps_password_token_sta(dev, apdev):
  34. """NFC tag with password token on the station/Enrollee"""
  35. dev[0].request("SET ignore_old_scan_res 1")
  36. ssid = "test-wps-nfc-pw-token-conf"
  37. params = ap_wps_params(ssid)
  38. hostapd.add_ap(apdev[0]['ifname'], params)
  39. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  40. logger.info("WPS provisioning step using password token from station")
  41. pw = dev[0].request("WPS_NFC_TOKEN NDEF").rstrip()
  42. if "FAIL" in pw:
  43. raise Exception("Failed to generate password token")
  44. res = hapd.request("WPS_NFC_TAG_READ " + pw)
  45. if "FAIL" in res:
  46. raise Exception("Failed to provide NFC tag contents to hostapd")
  47. dev[0].dump_monitor()
  48. res = dev[0].request("WPS_NFC")
  49. if "FAIL" in res:
  50. raise Exception("Failed to start Enrollee using NFC password token")
  51. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  52. if ev is None:
  53. raise Exception("Association with the AP timed out")
  54. check_wpa2_connection(dev[0], apdev[0], ssid)
  55. def test_nfc_wps_config_token(dev, apdev):
  56. """NFC tag with configuration token from AP"""
  57. ssid = "test-wps-nfc-conf-token"
  58. params = ap_wps_params(ssid)
  59. hostapd.add_ap(apdev[0]['ifname'], params)
  60. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  61. logger.info("NFC configuration token from AP to station")
  62. conf = hapd.request("WPS_NFC_CONFIG_TOKEN NDEF").rstrip()
  63. if "FAIL" in conf:
  64. raise Exception("Failed to generate configuration token")
  65. dev[0].dump_monitor()
  66. res = dev[0].request("WPS_NFC_TAG_READ " + conf)
  67. if "FAIL" in res:
  68. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  69. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  70. if ev is None:
  71. raise Exception("Association with the AP timed out")
  72. check_wpa2_connection(dev[0], apdev[0], ssid)
  73. def test_nfc_wps_config_token_init(dev, apdev):
  74. """NFC tag with configuration token from AP with auto configuration"""
  75. dev[0].request("SET ignore_old_scan_res 1")
  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. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  89. if ev is None:
  90. raise Exception("Association with the AP timed out")
  91. check_wpa2_connection(dev[0], apdev[0], ssid, mixed=True)
  92. def test_nfc_wps_password_token_sta_init(dev, apdev):
  93. """Initial AP configuration with first WPS NFC Enrollee"""
  94. dev[0].request("SET ignore_old_scan_res 1")
  95. ssid = "test-wps-nfc-pw-token-init"
  96. hostapd.add_ap(apdev[0]['ifname'],
  97. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  98. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  99. logger.info("WPS provisioning step using password token from station")
  100. pw = dev[0].request("WPS_NFC_TOKEN NDEF").rstrip()
  101. if "FAIL" in pw:
  102. raise Exception("Failed to generate password token")
  103. res = hapd.request("WPS_NFC_TAG_READ " + pw)
  104. if "FAIL" in res:
  105. raise Exception("Failed to provide NFC tag contents to hostapd")
  106. dev[0].dump_monitor()
  107. res = dev[0].request("WPS_NFC")
  108. if "FAIL" in res:
  109. raise Exception("Failed to start Enrollee using NFC password token")
  110. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  111. if ev is None:
  112. raise Exception("Association with the AP timed out")
  113. check_wpa2_connection(dev[0], apdev[0], ssid, mixed=True)
  114. def test_nfc_wps_password_token_ap(dev, apdev):
  115. """WPS registrar configuring an AP using AP password token"""
  116. dev[0].request("SET ignore_old_scan_res 1")
  117. ssid = "test-wps-nfc-pw-token-init"
  118. hostapd.add_ap(apdev[0]['ifname'],
  119. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  120. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  121. logger.info("WPS configuration step")
  122. pw = hapd.request("WPS_NFC_TOKEN NDEF").rstrip()
  123. if "FAIL" in pw:
  124. raise Exception("Failed to generate password token")
  125. res = hapd.request("WPS_NFC_TOKEN enable")
  126. if "FAIL" in pw:
  127. raise Exception("Failed to enable AP password token")
  128. res = dev[0].request("WPS_NFC_TAG_READ " + pw)
  129. if "FAIL" in res:
  130. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  131. dev[0].dump_monitor()
  132. new_ssid = "test-wps-nfc-pw-token-new-ssid"
  133. new_passphrase = "1234567890"
  134. res = dev[0].request("WPS_REG " + apdev[0]['bssid'] + " nfc-pw " + new_ssid.encode("hex") + " WPA2PSK CCMP " + new_passphrase.encode("hex"))
  135. if "FAIL" in res:
  136. raise Exception("Failed to start Registrar using NFC password token")
  137. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  138. if ev is None:
  139. raise Exception("Association with the AP timed out")
  140. check_wpa2_connection(dev[0], apdev[0], new_ssid, mixed=True)
  141. def test_nfc_wps_handover(dev, apdev):
  142. """Connect to WPS AP with NFC connection handover"""
  143. ssid = "test-wps-nfc-handover"
  144. params = ap_wps_params(ssid)
  145. hostapd.add_ap(apdev[0]['ifname'], params)
  146. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  147. logger.info("NFC connection handover")
  148. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  149. if "FAIL" in req:
  150. raise Exception("Failed to generate NFC connection handover request")
  151. sel = hapd.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  152. if "FAIL" in sel:
  153. raise Exception("Failed to generate NFC connection handover select")
  154. res = hapd.request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  155. if "FAIL" in res:
  156. raise Exception("Failed to report NFC connection handover to to hostapd")
  157. dev[0].dump_monitor()
  158. res = dev[0].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  159. if "FAIL" in res:
  160. raise Exception("Failed to report NFC connection handover to to wpa_supplicant")
  161. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  162. if ev is None:
  163. raise Exception("Association with the AP timed out")
  164. check_wpa2_connection(dev[0], apdev[0], ssid)