test_pmksa_cache.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/usr/bin/python
  2. #
  3. # WPA2-Enterprise PMKSA caching 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 logging
  9. logger = logging.getLogger()
  10. import hostapd
  11. def test_pmksa_cache_on_roam_back(dev, apdev):
  12. """PMKSA cache to skip EAP on reassociation back to same AP"""
  13. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  14. hostapd.add_ap(apdev[0]['ifname'], params)
  15. bssid = apdev[0]['bssid']
  16. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  17. eap="GPSK", identity="gpsk user",
  18. password="abcdefghijklmnop0123456789abcdef",
  19. scan_freq="2412")
  20. pmksa = dev[0].get_pmksa(bssid)
  21. if pmksa is None:
  22. raise Exception("No PMKSA cache entry created")
  23. if pmksa['opportunistic'] != '0':
  24. raise Exception("Unexpected opportunistic PMKSA cache entry")
  25. hostapd.add_ap(apdev[1]['ifname'], params)
  26. bssid2 = apdev[1]['bssid']
  27. dev[0].dump_monitor()
  28. logger.info("Roam to AP2")
  29. dev[0].scan(freq="2412")
  30. dev[0].request("ROAM " + bssid2)
  31. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
  32. if ev is None:
  33. raise Exception("EAP success timed out")
  34. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
  35. if ev is None:
  36. raise Exception("Roaming with the AP timed out")
  37. pmksa2 = dev[0].get_pmksa(bssid2)
  38. if pmksa2 is None:
  39. raise Exception("No PMKSA cache entry found")
  40. if pmksa2['opportunistic'] != '0':
  41. raise Exception("Unexpected opportunistic PMKSA cache entry")
  42. dev[0].dump_monitor()
  43. logger.info("Roam back to AP1")
  44. dev[0].scan(freq="2412")
  45. dev[0].request("ROAM " + bssid)
  46. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  47. "CTRL-EVENT-CONNECTED"], timeout=10)
  48. if ev is None:
  49. raise Exception("Roaming with the AP timed out")
  50. if "CTRL-EVENT-EAP-STARTED" in ev:
  51. raise Exception("Unexpected EAP exchange")
  52. pmksa1b = dev[0].get_pmksa(bssid)
  53. if pmksa1b is None:
  54. raise Exception("No PMKSA cache entry found")
  55. if pmksa['pmkid'] != pmksa1b['pmkid']:
  56. raise Exception("Unexpected PMKID change for AP1")
  57. def test_pmksa_cache_opportunistic_only_on_sta(dev, apdev):
  58. """Opportunistic PMKSA caching enabled only on station"""
  59. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  60. hostapd.add_ap(apdev[0]['ifname'], params)
  61. bssid = apdev[0]['bssid']
  62. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  63. eap="GPSK", identity="gpsk user",
  64. password="abcdefghijklmnop0123456789abcdef", okc=True,
  65. scan_freq="2412")
  66. pmksa = dev[0].get_pmksa(bssid)
  67. if pmksa is None:
  68. raise Exception("No PMKSA cache entry created")
  69. if pmksa['opportunistic'] != '0':
  70. raise Exception("Unexpected opportunistic PMKSA cache entry")
  71. hostapd.add_ap(apdev[1]['ifname'], params)
  72. bssid2 = apdev[1]['bssid']
  73. dev[0].dump_monitor()
  74. logger.info("Roam to AP2")
  75. dev[0].scan(freq="2412")
  76. dev[0].request("ROAM " + bssid2)
  77. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
  78. if ev is None:
  79. raise Exception("EAP success timed out")
  80. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
  81. if ev is None:
  82. raise Exception("Roaming with the AP timed out")
  83. pmksa2 = dev[0].get_pmksa(bssid2)
  84. if pmksa2 is None:
  85. raise Exception("No PMKSA cache entry found")
  86. if pmksa2['opportunistic'] != '0':
  87. raise Exception("Unexpected opportunistic PMKSA cache entry")
  88. dev[0].dump_monitor()
  89. logger.info("Roam back to AP1")
  90. dev[0].scan(freq="2412")
  91. dev[0].request("ROAM " + bssid)
  92. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  93. "CTRL-EVENT-CONNECTED"], timeout=10)
  94. if ev is None:
  95. raise Exception("Roaming with the AP timed out")
  96. if "CTRL-EVENT-EAP-STARTED" in ev:
  97. raise Exception("Unexpected EAP exchange")
  98. pmksa1b = dev[0].get_pmksa(bssid)
  99. if pmksa1b is None:
  100. raise Exception("No PMKSA cache entry found")
  101. if pmksa['pmkid'] != pmksa1b['pmkid']:
  102. raise Exception("Unexpected PMKID change for AP1")
  103. def test_pmksa_cache_opportunistic(dev, apdev):
  104. """Opportunistic PMKSA caching"""
  105. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  106. params['okc'] = "1"
  107. hostapd.add_ap(apdev[0]['ifname'], params)
  108. bssid = apdev[0]['bssid']
  109. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  110. eap="GPSK", identity="gpsk user",
  111. password="abcdefghijklmnop0123456789abcdef", okc=True,
  112. scan_freq="2412")
  113. pmksa = dev[0].get_pmksa(bssid)
  114. if pmksa is None:
  115. raise Exception("No PMKSA cache entry created")
  116. if pmksa['opportunistic'] != '0':
  117. raise Exception("Unexpected opportunistic PMKSA cache entry")
  118. hostapd.add_ap(apdev[1]['ifname'], params)
  119. bssid2 = apdev[1]['bssid']
  120. dev[0].dump_monitor()
  121. logger.info("Roam to AP2")
  122. dev[0].scan(freq="2412")
  123. dev[0].request("ROAM " + bssid2)
  124. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  125. "CTRL-EVENT-CONNECTED"], timeout=10)
  126. if ev is None:
  127. raise Exception("Roaming with the AP timed out")
  128. if "CTRL-EVENT-EAP-STARTED" in ev:
  129. raise Exception("Unexpected EAP exchange")
  130. pmksa2 = dev[0].get_pmksa(bssid2)
  131. if pmksa2 is None:
  132. raise Exception("No PMKSA cache entry created")
  133. dev[0].dump_monitor()
  134. logger.info("Roam back to AP1")
  135. dev[0].scan(freq="2412")
  136. dev[0].request("ROAM " + bssid)
  137. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  138. "CTRL-EVENT-CONNECTED"], timeout=10)
  139. if ev is None:
  140. raise Exception("Roaming with the AP timed out")
  141. if "CTRL-EVENT-EAP-STARTED" in ev:
  142. raise Exception("Unexpected EAP exchange")
  143. pmksa1b = dev[0].get_pmksa(bssid)
  144. if pmksa1b is None:
  145. raise Exception("No PMKSA cache entry found")
  146. if pmksa['pmkid'] != pmksa1b['pmkid']:
  147. raise Exception("Unexpected PMKID change for AP1")
  148. def test_pmksa_cache_expiration(dev, apdev):
  149. """PMKSA cache entry expiration"""
  150. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  151. hostapd.add_ap(apdev[0]['ifname'], params)
  152. bssid = apdev[0]['bssid']
  153. dev[0].request("SET dot11RSNAConfigPMKLifetime 10")
  154. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  155. eap="GPSK", identity="gpsk user",
  156. password="abcdefghijklmnop0123456789abcdef",
  157. scan_freq="2412")
  158. pmksa = dev[0].get_pmksa(bssid)
  159. if pmksa is None:
  160. raise Exception("No PMKSA cache entry created")
  161. logger.info("Wait for PMKSA cache entry to expire")
  162. ev = dev[0].wait_event(["WPA: Key negotiation completed",
  163. "CTRL-EVENT-DISCONNECTED"], timeout=15)
  164. if ev is None:
  165. raise Exception("No EAP reauthentication seen")
  166. if "CTRL-EVENT-DISCONNECTED" in ev:
  167. raise Exception("Unexpected disconnection")
  168. pmksa2 = dev[0].get_pmksa(bssid)
  169. if pmksa['pmkid'] == pmksa2['pmkid']:
  170. raise Exception("PMKID did not change")