pkex_test.py (12176B)
1 #!/usr/bin/python3 2 3 import unittest 4 import sys 5 6 sys.path.append('../util') 7 from iwd import IWD, SharedCodeAgent, DeviceState 8 from iwd import DeviceProvisioning 9 from wpas import Wpas 10 from hostapd import HostapdCLI 11 from hwsim import Hwsim 12 from config import ctx 13 from time import time 14 import os 15 16 class Test(unittest.TestCase): 17 def start_wpas_pkex(self, code, curve=None, **kwargs): 18 self.wpas.dpp_bootstrap_gen(type='pkex', curve=curve) 19 self.wpas.dpp_pkex_add(code=code, **kwargs) 20 if kwargs.get('role', 'configurator') == 'configurator': 21 self.wpas.dpp_configurator_create() 22 self.wpas.dpp_listen(2437) 23 24 def stop_wpas_pkex(self): 25 self.wpas.dpp_pkex_remove() 26 self.wpas.dpp_stop_listen() 27 self.wpas.dpp_configurator_remove() 28 29 def start_iwd_pkex_configurator(self, device, agent=False, profile='ssidCCMP.psk'): 30 self.hapd.reload() 31 self.hapd.wait_for_event('AP-ENABLED') 32 33 IWD.copy_to_storage(profile) 34 device.autoconnect = True 35 36 condition = 'obj.state == DeviceState.connected' 37 self.wd.wait_for_object_condition(device, condition) 38 39 if agent: 40 self.agent = SharedCodeAgent(codes = {"test": "secret123"}) 41 42 device.dpp_pkex_start_configurator(self.agent.path) 43 else: 44 device.dpp_pkex_configure_enrollee('secret123', identifier="test") 45 46 # 47 # WPA Supplicant has awful handling of retransmissions and no-ACK 48 # conditions. It only handles retransmitting the exchange request when 49 # there is no-ACK, which makes zero sense since its a broadcast... 50 # 51 # So, really, testing against wpa_supplicant is fragile and dependent on 52 # how the scheduling works out. If IWD doesn't ACK due to being on the 53 # next frequency or in between offchannel requests wpa_supplicant gets 54 # into a state where it thinks a PKEX session has been started (having 55 # received the exchange request) but will only accept commit-reveal 56 # frames. IWD is unaware because it never got a response so it keeps 57 # sending exchange requests which are ignored. 58 # 59 # Nevertheless we should still test against wpa_supplicant for 60 # compatibility so attempt to detect this case and restart the 61 # wpa_supplicant configurator. 62 # 63 def restart_wpas_if_needed(self): 64 i = 0 65 66 while i < 10: 67 data = self.wpas.wait_for_event("DPP-RX") 68 self.assertIn("type=7", data) 69 70 data = self.wpas.wait_for_event("DPP-TX") 71 self.assertIn("type=8", data) 72 73 data = self.wpas.wait_for_event("DPP-TX-STATUS") 74 if "result=no-ACK" in data: 75 self.stop_wpas_pkex() 76 self.start_wpas_pkex('secret123', identifier="test") 77 else: 78 return 79 80 i += 1 81 82 raise Exception("wpa_supplicant could not complete PKEX after 10 retries") 83 84 def test_pkex_iwd_as_enrollee(self): 85 self.start_wpas_pkex('secret123', identifier="test") 86 87 self.device[0].dpp_pkex_enroll('secret123', identifier="test") 88 89 self.restart_wpas_if_needed() 90 91 self.wpas.wait_for_event("DPP-AUTH-SUCCESS") 92 93 def test_pkex_iwd_as_enrollee_retransmit(self): 94 self.rule_reveal_req.enabled = True 95 96 self.start_wpas_pkex('secret123', identifier="test") 97 98 self.device[0].dpp_pkex_enroll('secret123', identifier="test") 99 100 self.restart_wpas_if_needed() 101 102 self.wpas.wait_for_event("DPP-AUTH-SUCCESS") 103 104 def test_pkex_unsupported_version(self): 105 self.start_wpas_pkex('secret123', identifier="test", version=2) 106 107 now = time() 108 self.device[0].dpp_pkex_enroll('secret123', identifier="test") 109 110 condition = "obj.started == False" 111 self.wd.wait_for_object_condition(self.device[0]._sc_device_provisioning, 112 condition, max_wait=125) 113 114 # Check the enrollee stopped after 2 minutes 115 elapsed = time() - now 116 self.assertLess(elapsed, 125) 117 118 def test_pkex_configurator_timeout(self): 119 self.start_iwd_pkex_configurator(self.device[0]) 120 121 now = time() 122 123 condition = "obj.started == False" 124 self.wd.wait_for_object_condition(self.device[0]._sc_device_provisioning, 125 condition, max_wait=125) 126 127 # Check the enrollee stopped after 2 minutes 128 elapsed = time() - now 129 self.assertLess(elapsed, 125) 130 131 def test_pkex_iwd_as_configurator(self): 132 self.start_iwd_pkex_configurator(self.device[0]) 133 134 self.start_wpas_pkex('secret123', identifier="test", initiator=True, 135 role='enrollee') 136 137 self.wpas.wait_for_event("DPP-AUTH-SUCCESS") 138 self.wpas.wait_for_event("DPP-CONF-RECEIVED") 139 140 def test_pkex_iwd_as_configurator_retransmit(self): 141 self.rule_xchg_resp.enabled = True 142 self.rule_reveal_resp.enabled = True 143 144 self.start_iwd_pkex_configurator(self.device[0]) 145 146 self.start_wpas_pkex('secret123', identifier="test", initiator=True, 147 role='enrollee') 148 149 self.wpas.wait_for_event("DPP-AUTH-SUCCESS") 150 self.wpas.wait_for_event("DPP-CONF-RECEIVED") 151 152 def test_pkex_iwd_as_configurator_bad_group(self): 153 self.start_iwd_pkex_configurator(self.device[0]) 154 155 self.start_wpas_pkex('secret123', identifier="test", initiator=True, 156 role='enrollee', curve='P-384') 157 158 self.wpas.wait_for_event(f"DPP-RX src={self.device[0].address} freq=2437 type=8") 159 self.wpas.wait_for_event("DPP-FAIL") 160 161 def test_pkex_iwd_to_iwd(self): 162 self.start_iwd_pkex_configurator(self.device[0]) 163 self.device[1].autoconnect = True 164 self.device[1].dpp_pkex_enroll('secret123', identifier="test") 165 166 condition = 'obj.state == DeviceState.connected' 167 self.wd.wait_for_object_condition(self.device[1], condition) 168 169 # Check additional settings were carried over 170 with open('/tmp/ns0/ssidCCMP.psk', 'r') as f: 171 settings = f.read() 172 173 self.assertIn("SendHostname=true", settings) 174 175 def test_pkex_configurator_with_agent(self): 176 self.start_iwd_pkex_configurator(self.device[0], agent=True) 177 self.device[1].autoconnect = True 178 self.device[1].dpp_pkex_enroll('secret123', identifier="test") 179 180 condition = 'obj.state == DeviceState.connected' 181 self.wd.wait_for_object_condition(self.device[1], condition) 182 183 self.agent = None 184 185 def test_existing_network(self): 186 self.hapd.reload() 187 self.hapd.wait_for_event('AP-ENABLED') 188 IWD.copy_to_storage("existingProfile.psk", "/tmp/ns0/", "ssidCCMP.psk") 189 190 # Scan first so a network object exists, and its a known network 191 self.device[1].scan() 192 self.wd.wait_for_object_condition(self.device[1], 'obj.scanning == True') 193 self.wd.wait_for_object_condition(self.device[1], 'obj.scanning == False') 194 195 self.start_iwd_pkex_configurator(self.device[0]) 196 197 self.device[1].autoconnect = False 198 self.device[1].dpp_pkex_enroll('secret123', identifier="test") 199 200 condition = 'obj.state == DeviceState.connected' 201 self.wd.wait_for_object_condition(self.device[1], condition) 202 203 # Check additional settings were carried over 204 with open('/tmp/ns0/ssidCCMP.psk', 'r') as f: 205 settings = f.read() 206 207 self.assertIn("SendHostname=true", settings) 208 209 def test_existing_incorrect_profile(self): 210 self.hapd.reload() 211 self.hapd.wait_for_event('AP-ENABLED') 212 IWD.copy_to_storage("existingProfile.psk", "/tmp/ns0/", "ssidCCMP.psk") 213 214 # Start connecting 215 self.device[1].autoconnect = True 216 self.wd.wait_for_object_condition(self.device[1], 'obj.state == DeviceState.connecting') 217 218 # We should be able to start DPP despite the connecting state 219 self.device[1].dpp_pkex_enroll('secret123', identifier="test") 220 221 self.start_iwd_pkex_configurator(self.device[0]) 222 self.assertEqual(self.device[1].state, DeviceState.disconnected) 223 224 condition = 'obj.state == DeviceState.connected' 225 self.wd.wait_for_object_condition(self.device[1], condition) 226 227 def test_existing_hidden_network(self): 228 self.hapd_hidden.reload() 229 self.hapd_hidden.wait_for_event('AP-ENABLED') 230 IWD.copy_to_storage("existingProfile.psk", "/tmp/ns0/", "ssidHidden.psk") 231 232 # Scan first so a network object exists, and its a known network 233 self.device[1].scan() 234 self.wd.wait_for_object_condition(self.device[1], 'obj.scanning == True') 235 self.wd.wait_for_object_condition(self.device[1], 'obj.scanning == False') 236 237 self.start_iwd_pkex_configurator(self.device[0], profile='ssidHidden.psk') 238 239 self.device[1].autoconnect = False 240 self.device[1].dpp_pkex_enroll('secret123', identifier="test") 241 242 243 condition = 'obj.state == DeviceState.connected' 244 self.wd.wait_for_object_condition(self.device[1], condition) 245 246 # Check additional settings were carried over 247 with open('/tmp/ns0/ssidHidden.psk', 'r') as f: 248 settings = f.read() 249 250 self.assertIn("Hidden=true", settings) 251 252 def test_hidden_network(self): 253 self.hapd_hidden.reload() 254 self.hapd_hidden.wait_for_event('AP-ENABLED') 255 self.start_iwd_pkex_configurator(self.device[0], profile='ssidHidden.psk') 256 257 self.device[1].autoconnect = False 258 self.device[1].dpp_pkex_enroll('secret123', identifier="test") 259 260 condition = 'obj.state == DeviceState.connected' 261 self.wd.wait_for_object_condition(self.device[1], condition) 262 263 # Check additional settings were carried over 264 with open('/tmp/ns0/ssidHidden.psk', 'r') as f: 265 settings = f.read() 266 267 self.assertIn("Hidden=true", settings) 268 269 def setUp(self): 270 ns0 = ctx.get_namespace('ns0') 271 self.wpas = Wpas('wpas.conf') 272 273 self.wd = IWD(True) 274 self.wd_ns0 = IWD(True, iwd_storage_dir='/tmp/ns0', namespace=ns0) 275 self.device = [] 276 self.device.append(self.wd.list_devices(1)[0]) 277 self.device.append(self.wd_ns0.list_devices(1)[0]) 278 self.hapd = HostapdCLI('hostapd.conf') 279 self.hapd.disable() 280 self.hapd_hidden = HostapdCLI('ssidHidden.conf') 281 self.hapd_hidden.disable() 282 self.hwsim = Hwsim() 283 284 self.rule_xchg_resp = self.hwsim.rules.create() 285 self.rule_xchg_resp.prefix = 'd0' 286 self.rule_xchg_resp.match_offset = 24 287 self.rule_xchg_resp.match = '04 09 50 6f 9a 1a 01 08' 288 self.rule_xchg_resp.match_times = 1 289 self.rule_xchg_resp.drop = True 290 291 self.rule_reveal_resp = self.hwsim.rules.create() 292 self.rule_reveal_resp.prefix = 'd0' 293 self.rule_reveal_resp.match_offset = 24 294 self.rule_reveal_resp.match = '04 09 50 6f 9a 1a 01 0a' 295 self.rule_reveal_resp.match_times = 1 296 self.rule_reveal_resp.drop = True 297 298 self.rule_reveal_req = self.hwsim.rules.create() 299 self.rule_reveal_req.prefix = 'd0' 300 self.rule_reveal_req.match_offset = 24 301 self.rule_reveal_req.match = '04 09 50 6f 9a 1a 01 09' 302 self.rule_reveal_req.match_times = 1 303 self.rule_reveal_req.drop = True 304 305 def tearDown(self): 306 # Tests end in various states, don't fail when tearing down. 307 try: 308 self.device[0].disconnect() 309 self.device[0].dpp_pkex_stop() 310 self.device[1].disconnect() 311 self.device[1].dpp_pkex_stop() 312 except: 313 pass 314 315 self.wpas.dpp_configurator_remove() 316 self.wpas.clean_up() 317 318 self.wd = None 319 self.wd_ns0 = None 320 self.device = None 321 self.wpas = None 322 self.hapd = None 323 self.rule_xchg_resp = None 324 IWD.clear_storage() 325 IWD.clear_storage('/tmp/ns0') 326 327 @classmethod 328 def setUpClass(cls): 329 pass 330 331 @classmethod 332 def tearDownClass(cls): 333 pass 334 335 if __name__ == '__main__': 336 unittest.main(exit=True)