test_peerkey.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # PeerKey tests
  2. # Copyright (c) 2013-2016, 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 logging
  8. logger = logging.getLogger()
  9. import os
  10. import time
  11. import hwsim_utils
  12. import hostapd
  13. from utils import skip_with_fips
  14. from wlantest import Wlantest
  15. from tshark import run_tshark
  16. @remote_compatible
  17. def test_peerkey(dev, apdev):
  18. """RSN AP and PeerKey between two STAs"""
  19. ssid = "test-peerkey"
  20. passphrase = "12345678"
  21. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  22. params['peerkey'] = "1"
  23. hostapd.add_ap(apdev[0], params)
  24. dev[0].connect(ssid, psk=passphrase, scan_freq="2412", peerkey=True)
  25. dev[1].connect(ssid, psk=passphrase, scan_freq="2412", peerkey=True)
  26. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  27. dev[0].request("STKSTART " + dev[1].p2p_interface_addr())
  28. time.sleep(0.5)
  29. # NOTE: Actual use of the direct link (DLS) is not supported in
  30. # mac80211_hwsim, so this operation fails at setting the keys after
  31. # successfully completed 4-way handshake. This test case does allow the
  32. # key negotiation part to be tested for coverage, though.
  33. def test_peerkey_sniffer_check(dev, apdev, params):
  34. """RSN AP and PeerKey between two STAs with sniffer check"""
  35. ssid = "test-peerkey"
  36. passphrase = "12345678"
  37. hparams = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  38. hparams['peerkey'] = "1"
  39. hapd = hostapd.add_ap(apdev[0], hparams)
  40. Wlantest.setup(hapd)
  41. wt = Wlantest()
  42. wt.flush()
  43. wt.add_passphrase("12345678")
  44. dev[0].connect(ssid, psk=passphrase, scan_freq="2412", peerkey=True)
  45. dev[1].connect(ssid, psk=passphrase, scan_freq="2412", peerkey=True)
  46. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  47. dev[0].request("STKSTART " + dev[1].p2p_interface_addr())
  48. time.sleep(1)
  49. # NOTE: Actual use of the direct link (DLS) is not supported in
  50. # mac80211_hwsim, so this operation fails at setting the keys after
  51. # successfully completed 4-way handshake. This test case does allow the
  52. # key negotiation part to be tested for coverage, though. Use sniffer to
  53. # verify that all the SMK and STK handshake messages were transmitted.
  54. bssid = hapd.own_addr()
  55. addr0 = dev[0].own_addr()
  56. addr1 = dev[1].own_addr()
  57. # Wireshark renamed the EAPOL-Key key_info field, so need to try both the
  58. # new and the old name to work with both versions.
  59. try_other = False
  60. try:
  61. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  62. "eapol.type == 3",
  63. display=["wlan.sa", "wlan.da",
  64. "wlan_rsna_eapol.keydes.key_info"])
  65. except Exception, e:
  66. if "Unknown tshark field" in str(e):
  67. try_other = True
  68. pass
  69. else:
  70. raise
  71. if not try_other:
  72. found = False
  73. for pkt in out.splitlines():
  74. sa, da, key_info = pkt.split('\t')
  75. if key_info != '':
  76. found = True
  77. break
  78. if not found:
  79. try_other = True
  80. if try_other:
  81. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  82. "eapol.type == 3",
  83. display=["wlan.sa", "wlan.da",
  84. "eapol.keydes.key_info"],
  85. wait=False)
  86. smk = [ False, False, False, False, False ]
  87. stk = [ False, False, False, False ]
  88. for pkt in out.splitlines():
  89. sa, da, key_info = pkt.split('\t')
  90. key_info = int(key_info, 16)
  91. if sa == addr0 and da == bssid and key_info == 0x2b02:
  92. # Initiator -> AP: MIC+Secure+Request+SMK = SMK 1
  93. smk[0] = True
  94. elif sa == bssid and da == addr1 and key_info == 0x2382:
  95. # AP -> Responder: ACK+MIC+Secure+SMK = SMK 2
  96. smk[1] = True
  97. elif sa == addr1 and da == bssid and key_info == 0x2302:
  98. # Responder -> AP: MIC+Secure+SMK = SMK 3
  99. smk[2] = True
  100. elif sa == bssid and da == addr1 and key_info == 0x3342:
  101. # AP -> Responder: Install+MIC+Secure+EncrKeyData+SMK = SMK 4
  102. smk[3] = True
  103. elif sa == bssid and da == addr0 and key_info == 0x3302:
  104. # AP -> Initiator: MIC+Secure+EncrKeyData+SMK = SMK 5
  105. smk[4] = True
  106. elif sa == addr0 and da == addr1 and key_info == 0x008a:
  107. # Initiator -> Responder: Pairwise+ACK = STK 1
  108. stk[0] = True
  109. elif sa == addr1 and da == addr0 and key_info == 0x010a:
  110. # Responder -> Initiator: Pairwise+MIC = STK 2
  111. stk[1] = True
  112. elif sa == addr0 and da == addr1 and key_info == 0x038a:
  113. # Initiator -> Responder: Pairwise+ACK+MIC+Secure = STK 3
  114. stk[2] = True
  115. elif sa == addr1 and da == addr0 and key_info == 0x030a:
  116. # Responder -> Initiator: Pairwise+MIC+Secure = STK 4
  117. stk[3] = True
  118. logger.info("Seen SMK messages: " + str(smk))
  119. logger.info("Seen STK messages: " + str(stk))
  120. if False in smk:
  121. raise Exception("Missing SMK message: " + str(smk))
  122. if False in stk:
  123. raise Exception("Missing STK message: " + str(stk))
  124. def test_peerkey_unknown_peer(dev, apdev):
  125. """RSN AP and PeerKey attempt with unknown peer"""
  126. ssid = "test-peerkey"
  127. passphrase = "12345678"
  128. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  129. params['peerkey'] = "1"
  130. hostapd.add_ap(apdev[0], params)
  131. dev[0].connect(ssid, psk=passphrase, scan_freq="2412", peerkey=True)
  132. dev[1].connect(ssid, psk=passphrase, scan_freq="2412", peerkey=True)
  133. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  134. dev[0].request("STKSTART " + dev[2].p2p_interface_addr())
  135. time.sleep(0.5)
  136. @remote_compatible
  137. def test_peerkey_pairwise_mismatch(dev, apdev):
  138. """RSN TKIP+CCMP AP and PeerKey between two STAs using different ciphers"""
  139. skip_with_fips(dev[0])
  140. ssid = "test-peerkey"
  141. passphrase = "12345678"
  142. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  143. params['peerkey'] = "1"
  144. params['rsn_pairwise'] = "TKIP CCMP"
  145. hapd = hostapd.add_ap(apdev[0], params)
  146. Wlantest.setup(hapd)
  147. wt = Wlantest()
  148. wt.flush()
  149. wt.add_passphrase("12345678")
  150. dev[0].connect(ssid, psk=passphrase, scan_freq="2412", peerkey=True,
  151. pairwise="CCMP")
  152. dev[1].connect(ssid, psk=passphrase, scan_freq="2412", peerkey=True,
  153. pairwise="TKIP")
  154. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  155. dev[0].request("STKSTART " + dev[1].p2p_interface_addr())
  156. time.sleep(0.5)
  157. dev[1].request("STKSTART " + dev[0].p2p_interface_addr())
  158. time.sleep(0.5)
  159. def test_peerkey_deinit_during_neg(dev, apdev):
  160. """RSN AP deinit during PeerKey negotiation"""
  161. ssid = "test-peerkey"
  162. passphrase = "12345678"
  163. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  164. params['peerkey'] = "1"
  165. hapd = hostapd.add_ap(apdev[0], params)
  166. dev[0].connect(ssid, psk=passphrase, scan_freq="2412", peerkey=True)
  167. dev[1].connect(ssid, psk=passphrase, scan_freq="2412", peerkey=True)
  168. dev[1].request("SET ext_eapol_frame_io 1")
  169. dev[0].request("STKSTART " + dev[1].own_addr())
  170. ev = dev[1].wait_event(["EAPOL-TX"], timeout=5)
  171. if ev is None:
  172. raise Exception("No PeerKey response from dev1")
  173. hapd.request("DISABLE")
  174. dev[0].request("REMOVE_NETWORK all")
  175. dev[1].request("REMOVE_NETWORK all")