tests: Allow multiple Enrollee events in ap_wps_er_add_enrollee_pbc
[mech_eap.git] / tests / hwsim / test_ap_wps.py
1 #!/usr/bin/python
2 #
3 # WPS tests
4 # Copyright (c) 2013-2014, 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
9 import time
10 import subprocess
11 import logging
12 logger = logging.getLogger()
13
14 import hwsim_utils
15 import hostapd
16
17 def test_ap_wps_init(dev, apdev):
18     """Initial AP configuration with first WPS Enrollee"""
19     ssid = "test-wps"
20     hostapd.add_ap(apdev[0]['ifname'],
21                    { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
22     hapd = hostapd.Hostapd(apdev[0]['ifname'])
23     logger.info("WPS provisioning step")
24     hapd.request("WPS_PBC")
25     if "PBC Status: Active" not in hapd.request("WPS_GET_STATUS"):
26         raise Exception("PBC status not shown correctly")
27     dev[0].dump_monitor()
28     dev[0].request("WPS_PBC")
29     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
30     if ev is None:
31         raise Exception("Association with the AP timed out")
32     status = dev[0].get_status()
33     if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
34         raise Exception("Not fully connected")
35     if status['ssid'] != ssid:
36         raise Exception("Unexpected SSID")
37     if status['pairwise_cipher'] != 'CCMP':
38         raise Exception("Unexpected encryption configuration")
39     if status['key_mgmt'] != 'WPA2-PSK':
40         raise Exception("Unexpected key_mgmt")
41
42     status = hapd.request("WPS_GET_STATUS")
43     if "PBC Status: Disabled" not in status:
44         raise Exception("PBC status not shown correctly")
45     if "Last WPS result: Success" not in status:
46         raise Exception("Last WPS result not shown correctly")
47     if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
48         raise Exception("Peer address not shown correctly")
49     conf = hapd.request("GET_CONFIG")
50     if "wps_state=configured" not in conf:
51         raise Exception("AP not in WPS configured state")
52     if "rsn_pairwise_cipher=CCMP TKIP" not in conf:
53         raise Exception("Unexpected rsn_pairwise_cipher")
54     if "wpa_pairwise_cipher=CCMP TKIP" not in conf:
55         raise Exception("Unexpected wpa_pairwise_cipher")
56     if "group_cipher=TKIP" not in conf:
57         raise Exception("Unexpected group_cipher")
58
59 def test_ap_wps_init_2ap_pbc(dev, apdev):
60     """Initial two-radio AP configuration with first WPS PBC Enrollee"""
61     ssid = "test-wps"
62     params = { "ssid": ssid, "eap_server": "1", "wps_state": "1" }
63     hostapd.add_ap(apdev[0]['ifname'], params)
64     hostapd.add_ap(apdev[1]['ifname'], params)
65     hapd = hostapd.Hostapd(apdev[0]['ifname'])
66     logger.info("WPS provisioning step")
67     hapd.request("WPS_PBC")
68     dev[0].scan(freq="2412")
69     bss = dev[0].get_bss(apdev[0]['bssid'])
70     if "[WPS-PBC]" not in bss['flags']:
71         raise Exception("WPS-PBC flag missing from AP1")
72     bss = dev[0].get_bss(apdev[1]['bssid'])
73     if "[WPS-PBC]" not in bss['flags']:
74         raise Exception("WPS-PBC flag missing from AP2")
75     dev[0].dump_monitor()
76     dev[0].request("WPS_PBC")
77     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
78     if ev is None:
79         raise Exception("Association with the AP timed out")
80
81     dev[1].scan(freq="2412")
82     bss = dev[1].get_bss(apdev[0]['bssid'])
83     if "[WPS-PBC]" in bss['flags']:
84         raise Exception("WPS-PBC flag not cleared from AP1")
85     bss = dev[1].get_bss(apdev[1]['bssid'])
86     if "[WPS-PBC]" in bss['flags']:
87         raise Exception("WPS-PBC flag bit ckeared from AP2")
88
89 def test_ap_wps_init_2ap_pin(dev, apdev):
90     """Initial two-radio AP configuration with first WPS PIN Enrollee"""
91     ssid = "test-wps"
92     params = { "ssid": ssid, "eap_server": "1", "wps_state": "1" }
93     hostapd.add_ap(apdev[0]['ifname'], params)
94     hostapd.add_ap(apdev[1]['ifname'], params)
95     hapd = hostapd.Hostapd(apdev[0]['ifname'])
96     logger.info("WPS provisioning step")
97     pin = dev[0].wps_read_pin()
98     hapd.request("WPS_PIN any " + pin)
99     dev[0].scan(freq="2412")
100     bss = dev[0].get_bss(apdev[0]['bssid'])
101     if "[WPS-AUTH]" not in bss['flags']:
102         raise Exception("WPS-AUTH flag missing from AP1")
103     bss = dev[0].get_bss(apdev[1]['bssid'])
104     if "[WPS-AUTH]" not in bss['flags']:
105         raise Exception("WPS-AUTH flag missing from AP2")
106     dev[0].dump_monitor()
107     dev[0].request("WPS_PIN any " + pin)
108     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
109     if ev is None:
110         raise Exception("Association with the AP timed out")
111
112     dev[1].scan(freq="2412")
113     bss = dev[1].get_bss(apdev[0]['bssid'])
114     if "[WPS-AUTH]" in bss['flags']:
115         raise Exception("WPS-AUTH flag not cleared from AP1")
116     bss = dev[1].get_bss(apdev[1]['bssid'])
117     if "[WPS-AUTH]" in bss['flags']:
118         raise Exception("WPS-AUTH flag bit ckeared from AP2")
119
120 def test_ap_wps_init_through_wps_config(dev, apdev):
121     """Initial AP configuration using wps_config command"""
122     ssid = "test-wps-init-config"
123     hostapd.add_ap(apdev[0]['ifname'],
124                    { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
125     hapd = hostapd.Hostapd(apdev[0]['ifname'])
126     if "FAIL" in hapd.request("WPS_CONFIG " + ssid.encode("hex") + " WPA2PSK CCMP " + "12345678".encode("hex")):
127         raise Exception("WPS_CONFIG command failed")
128     dev[0].connect(ssid, psk="12345678", scan_freq="2412", proto="WPA2",
129                    pairwise="CCMP", group="CCMP")
130
131 def test_ap_wps_conf(dev, apdev):
132     """WPS PBC provisioning with configured AP"""
133     ssid = "test-wps-conf"
134     hostapd.add_ap(apdev[0]['ifname'],
135                    { "ssid": ssid, "eap_server": "1", "wps_state": "2",
136                      "wpa_passphrase": "12345678", "wpa": "2",
137                      "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
138     hapd = hostapd.Hostapd(apdev[0]['ifname'])
139     logger.info("WPS provisioning step")
140     hapd.request("WPS_PBC")
141     dev[0].dump_monitor()
142     dev[0].request("WPS_PBC")
143     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
144     if ev is None:
145         raise Exception("Association with the AP timed out")
146     status = dev[0].get_status()
147     if status['wpa_state'] != 'COMPLETED':
148         raise Exception("Not fully connected")
149     if status['bssid'] != apdev[0]['bssid']:
150         raise Exception("Unexpected BSSID")
151     if status['ssid'] != ssid:
152         raise Exception("Unexpected SSID")
153     if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
154         raise Exception("Unexpected encryption configuration")
155     if status['key_mgmt'] != 'WPA2-PSK':
156         raise Exception("Unexpected key_mgmt")
157
158     sta = hapd.get_sta(dev[0].p2p_interface_addr())
159     if 'wpsDeviceName' not in sta or sta['wpsDeviceName'] != "Device A":
160         raise Exception("Device name not available in STA command")
161
162 def test_ap_wps_twice(dev, apdev):
163     """WPS provisioning with twice to change passphrase"""
164     ssid = "test-wps-twice"
165     params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
166                "wpa_passphrase": "12345678", "wpa": "2",
167                "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" }
168     hostapd.add_ap(apdev[0]['ifname'], params)
169     hapd = hostapd.Hostapd(apdev[0]['ifname'])
170     logger.info("WPS provisioning step")
171     hapd.request("WPS_PBC")
172     dev[0].dump_monitor()
173     dev[0].request("WPS_PBC")
174     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
175     if ev is None:
176         raise Exception("Association with the AP timed out")
177     dev[0].request("DISCONNECT")
178
179     logger.info("Restart AP with different passphrase and re-run WPS")
180     hapd_global = hostapd.HostapdGlobal()
181     hapd_global.remove(apdev[0]['ifname'])
182     params['wpa_passphrase'] = 'another passphrase'
183     hostapd.add_ap(apdev[0]['ifname'], params)
184     hapd = hostapd.Hostapd(apdev[0]['ifname'])
185     logger.info("WPS provisioning step")
186     hapd.request("WPS_PBC")
187     dev[0].dump_monitor()
188     dev[0].request("WPS_PBC")
189     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
190     if ev is None:
191         raise Exception("Association with the AP timed out")
192     networks = dev[0].list_networks()
193     if len(networks) > 1:
194         raise Exception("Unexpected duplicated network block present")
195
196 def test_ap_wps_incorrect_pin(dev, apdev):
197     """WPS PIN provisioning with incorrect PIN"""
198     ssid = "test-wps-incorrect-pin"
199     hostapd.add_ap(apdev[0]['ifname'],
200                    { "ssid": ssid, "eap_server": "1", "wps_state": "2",
201                      "wpa_passphrase": "12345678", "wpa": "2",
202                      "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
203     hapd = hostapd.Hostapd(apdev[0]['ifname'])
204
205     logger.info("WPS provisioning attempt 1")
206     hapd.request("WPS_PIN any 12345670")
207     dev[0].dump_monitor()
208     dev[0].request("WPS_PIN any 55554444")
209     ev = dev[0].wait_event(["WPS-FAIL"], timeout=30)
210     if ev is None:
211         raise Exception("WPS operation timed out")
212     if "config_error=18" not in ev:
213         raise Exception("Incorrect config_error reported")
214     if "msg=8" not in ev:
215         raise Exception("PIN error detected on incorrect message")
216     ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
217     if ev is None:
218         raise Exception("Timeout on disconnection event")
219     dev[0].request("WPS_CANCEL")
220     # if a scan was in progress, wait for it to complete before trying WPS again
221     ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
222
223     status = hapd.request("WPS_GET_STATUS")
224     if "Last WPS result: Failed" not in status:
225         raise Exception("WPS failure result not shown correctly")
226
227     logger.info("WPS provisioning attempt 2")
228     hapd.request("WPS_PIN any 12345670")
229     dev[0].dump_monitor()
230     dev[0].request("WPS_PIN any 12344444")
231     ev = dev[0].wait_event(["WPS-FAIL"], timeout=30)
232     if ev is None:
233         raise Exception("WPS operation timed out")
234     if "config_error=18" not in ev:
235         raise Exception("Incorrect config_error reported")
236     if "msg=10" not in ev:
237         raise Exception("PIN error detected on incorrect message")
238     ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
239     if ev is None:
240         raise Exception("Timeout on disconnection event")
241
242 def test_ap_wps_conf_pin(dev, apdev):
243     """WPS PIN provisioning with configured AP"""
244     ssid = "test-wps-conf-pin"
245     hostapd.add_ap(apdev[0]['ifname'],
246                    { "ssid": ssid, "eap_server": "1", "wps_state": "2",
247                      "wpa_passphrase": "12345678", "wpa": "2",
248                      "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
249     hapd = hostapd.Hostapd(apdev[0]['ifname'])
250     logger.info("WPS provisioning step")
251     pin = dev[0].wps_read_pin()
252     hapd.request("WPS_PIN any " + pin)
253     dev[0].dump_monitor()
254     dev[0].request("WPS_PIN any " + pin)
255     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
256     if ev is None:
257         raise Exception("Association with the AP timed out")
258     status = dev[0].get_status()
259     if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
260         raise Exception("Not fully connected")
261     if status['ssid'] != ssid:
262         raise Exception("Unexpected SSID")
263     if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
264         raise Exception("Unexpected encryption configuration")
265     if status['key_mgmt'] != 'WPA2-PSK':
266         raise Exception("Unexpected key_mgmt")
267
268     dev[1].scan(freq="2412")
269     bss = dev[1].get_bss(apdev[0]['bssid'])
270     if "[WPS-AUTH]" in bss['flags']:
271         raise Exception("WPS-AUTH flag not cleared")
272     logger.info("Try to connect from another station using the same PIN")
273     dev[1].request("WPS_PIN any " + pin)
274     ev = dev[1].wait_event(["WPS-M2D","CTRL-EVENT-CONNECTED"], timeout=30)
275     if ev is None:
276         raise Exception("Operation timed out")
277     if "WPS-M2D" not in ev:
278         raise Exception("Unexpected WPS operation started")
279
280 def test_ap_wps_conf_pin_2sta(dev, apdev):
281     """Two stations trying to use WPS PIN at the same time"""
282     ssid = "test-wps-conf-pin2"
283     hostapd.add_ap(apdev[0]['ifname'],
284                    { "ssid": ssid, "eap_server": "1", "wps_state": "2",
285                      "wpa_passphrase": "12345678", "wpa": "2",
286                      "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
287     hapd = hostapd.Hostapd(apdev[0]['ifname'])
288     logger.info("WPS provisioning step")
289     pin = "12345670"
290     pin2 = "55554444"
291     hapd.request("WPS_PIN " + dev[0].get_status_field("uuid") + " " + pin)
292     hapd.request("WPS_PIN " + dev[1].get_status_field("uuid") + " " + pin)
293     dev[0].dump_monitor()
294     dev[1].dump_monitor()
295     dev[0].request("WPS_PIN any " + pin)
296     dev[1].request("WPS_PIN any " + pin)
297     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
298     if ev is None:
299         raise Exception("Association with the AP timed out")
300     ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
301     if ev is None:
302         raise Exception("Association with the AP timed out")
303
304 def test_ap_wps_reg_connect(dev, apdev):
305     """WPS registrar using AP PIN to connect"""
306     ssid = "test-wps-reg-ap-pin"
307     appin = "12345670"
308     hostapd.add_ap(apdev[0]['ifname'],
309                    { "ssid": ssid, "eap_server": "1", "wps_state": "2",
310                      "wpa_passphrase": "12345678", "wpa": "2",
311                      "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
312                      "ap_pin": appin})
313     logger.info("WPS provisioning step")
314     dev[0].dump_monitor()
315     dev[0].wps_reg(apdev[0]['bssid'], appin)
316     status = dev[0].get_status()
317     if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
318         raise Exception("Not fully connected")
319     if status['ssid'] != ssid:
320         raise Exception("Unexpected SSID")
321     if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
322         raise Exception("Unexpected encryption configuration")
323     if status['key_mgmt'] != 'WPA2-PSK':
324         raise Exception("Unexpected key_mgmt")
325
326 def check_wps_reg_failure(dev, ap, appin):
327     dev.request("WPS_REG " + ap['bssid'] + " " + appin)
328     ev = dev.wait_event(["WPS-SUCCESS", "WPS-FAIL"], timeout=15)
329     if ev is None:
330         raise Exception("WPS operation timed out")
331     if "WPS-SUCCESS" in ev:
332         raise Exception("WPS operation succeeded unexpectedly")
333     if "config_error=15" not in ev:
334         raise Exception("WPS setup locked state was not reported correctly")
335
336 def test_ap_wps_random_ap_pin(dev, apdev):
337     """WPS registrar using random AP PIN"""
338     ssid = "test-wps-reg-random-ap-pin"
339     ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
340     hostapd.add_ap(apdev[0]['ifname'],
341                    { "ssid": ssid, "eap_server": "1", "wps_state": "2",
342                      "wpa_passphrase": "12345678", "wpa": "2",
343                      "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
344                      "device_name": "Wireless AP", "manufacturer": "Company",
345                      "model_name": "WAP", "model_number": "123",
346                      "serial_number": "12345", "device_type": "6-0050F204-1",
347                      "os_version": "01020300",
348                      "config_methods": "label push_button",
349                      "uuid": ap_uuid, "upnp_iface": "lo" })
350     hapd = hostapd.Hostapd(apdev[0]['ifname'])
351     appin = hapd.request("WPS_AP_PIN random")
352     if "FAIL" in appin:
353         raise Exception("Could not generate random AP PIN")
354     if appin not in hapd.request("WPS_AP_PIN get"):
355         raise Exception("Could not fetch current AP PIN")
356     logger.info("WPS provisioning step")
357     dev[0].wps_reg(apdev[0]['bssid'], appin)
358
359     hapd.request("WPS_AP_PIN disable")
360     logger.info("WPS provisioning step with AP PIN disabled")
361     check_wps_reg_failure(dev[1], apdev[0], appin)
362
363     logger.info("WPS provisioning step with AP PIN reset")
364     appin = "12345670"
365     hapd.request("WPS_AP_PIN set " + appin)
366     dev[1].wps_reg(apdev[0]['bssid'], appin)
367     dev[0].request("REMOVE_NETWORK all")
368     dev[1].request("REMOVE_NETWORK all")
369     dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
370     dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
371
372     logger.info("WPS provisioning step after AP PIN timeout")
373     hapd.request("WPS_AP_PIN disable")
374     appin = hapd.request("WPS_AP_PIN random 1")
375     time.sleep(1.1)
376     if "FAIL" not in hapd.request("WPS_AP_PIN get"):
377         raise Exception("AP PIN unexpectedly still enabled")
378     check_wps_reg_failure(dev[0], apdev[0], appin)
379
380     logger.info("WPS provisioning step after AP PIN timeout(2)")
381     hapd.request("WPS_AP_PIN disable")
382     appin = "12345670"
383     hapd.request("WPS_AP_PIN set " + appin + " 1")
384     time.sleep(1.1)
385     if "FAIL" not in hapd.request("WPS_AP_PIN get"):
386         raise Exception("AP PIN unexpectedly still enabled")
387     check_wps_reg_failure(dev[1], apdev[0], appin)
388
389 def test_ap_wps_reg_config(dev, apdev):
390     """WPS registrar configuring and AP using AP PIN"""
391     ssid = "test-wps-init-ap-pin"
392     appin = "12345670"
393     hostapd.add_ap(apdev[0]['ifname'],
394                    { "ssid": ssid, "eap_server": "1", "wps_state": "2",
395                      "ap_pin": appin})
396     logger.info("WPS configuration step")
397     dev[0].dump_monitor()
398     new_ssid = "wps-new-ssid"
399     new_passphrase = "1234567890"
400     dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPA2PSK", "CCMP",
401                    new_passphrase)
402     status = dev[0].get_status()
403     if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
404         raise Exception("Not fully connected")
405     if status['ssid'] != new_ssid:
406         raise Exception("Unexpected SSID")
407     if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
408         raise Exception("Unexpected encryption configuration")
409     if status['key_mgmt'] != 'WPA2-PSK':
410         raise Exception("Unexpected key_mgmt")
411
412 def test_ap_wps_reg_config_tkip(dev, apdev):
413     """WPS registrar configuring AP to use TKIP and AP upgrading to TKIP+CCMP"""
414     ssid = "test-wps-init-ap"
415     appin = "12345670"
416     hostapd.add_ap(apdev[0]['ifname'],
417                    { "ssid": ssid, "eap_server": "1", "wps_state": "1",
418                      "ap_pin": appin})
419     logger.info("WPS configuration step")
420     dev[0].request("SET wps_version_number 0x10")
421     dev[0].dump_monitor()
422     new_ssid = "wps-new-ssid-with-tkip"
423     new_passphrase = "1234567890"
424     dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPAPSK", "TKIP",
425                    new_passphrase)
426     logger.info("Re-connect to verify WPA2 mixed mode")
427     dev[0].request("DISCONNECT")
428     id = 0
429     dev[0].set_network(id, "pairwise", "CCMP")
430     dev[0].set_network(id, "proto", "RSN")
431     dev[0].connect_network(id)
432     status = dev[0].get_status()
433     if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
434         raise Exception("Not fully connected")
435     if status['ssid'] != new_ssid:
436         raise Exception("Unexpected SSID")
437     if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
438         raise Exception("Unexpected encryption configuration")
439     if status['key_mgmt'] != 'WPA2-PSK':
440         raise Exception("Unexpected key_mgmt")
441
442 def test_ap_wps_setup_locked(dev, apdev):
443     """WPS registrar locking up AP setup on AP PIN failures"""
444     ssid = "test-wps-incorrect-ap-pin"
445     appin = "12345670"
446     hostapd.add_ap(apdev[0]['ifname'],
447                    { "ssid": ssid, "eap_server": "1", "wps_state": "2",
448                      "wpa_passphrase": "12345678", "wpa": "2",
449                      "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
450                      "ap_pin": appin})
451     new_ssid = "wps-new-ssid-test"
452     new_passphrase = "1234567890"
453
454     ap_setup_locked=False
455     for pin in ["55554444", "1234", "12345678", "00000000", "11111111"]:
456         dev[0].dump_monitor()
457         logger.info("Try incorrect AP PIN - attempt " + pin)
458         dev[0].wps_reg(apdev[0]['bssid'], pin, new_ssid, "WPA2PSK",
459                        "CCMP", new_passphrase, no_wait=True)
460         ev = dev[0].wait_event(["WPS-FAIL", "CTRL-EVENT-CONNECTED"])
461         if ev is None:
462             raise Exception("Timeout on receiving WPS operation failure event")
463         if "CTRL-EVENT-CONNECTED" in ev:
464             raise Exception("Unexpected connection")
465         if "config_error=15" in ev:
466             logger.info("AP Setup Locked")
467             ap_setup_locked=True
468         elif "config_error=18" not in ev:
469             raise Exception("config_error=18 not reported")
470         ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
471         if ev is None:
472             raise Exception("Timeout on disconnection event")
473         time.sleep(0.1)
474     if not ap_setup_locked:
475         raise Exception("AP setup was not locked")
476
477     hapd = hostapd.Hostapd(apdev[0]['ifname'])
478     status = hapd.request("WPS_GET_STATUS")
479     if "Last WPS result: Failed" not in status:
480         raise Exception("WPS failure result not shown correctly")
481     if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
482         raise Exception("Peer address not shown correctly")
483
484     time.sleep(0.5)
485     dev[0].dump_monitor()
486     logger.info("WPS provisioning step")
487     pin = dev[0].wps_read_pin()
488     hapd = hostapd.Hostapd(apdev[0]['ifname'])
489     hapd.request("WPS_PIN any " + pin)
490     dev[0].request("WPS_PIN any " + pin)
491     ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=30)
492     if ev is None:
493         raise Exception("WPS success was not reported")
494     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
495     if ev is None:
496         raise Exception("Association with the AP timed out")
497
498 def test_ap_wps_pbc_overlap_2ap(dev, apdev):
499     """WPS PBC session overlap with two active APs"""
500     hostapd.add_ap(apdev[0]['ifname'],
501                    { "ssid": "wps1", "eap_server": "1", "wps_state": "2",
502                      "wpa_passphrase": "12345678", "wpa": "2",
503                      "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
504                      "wps_independent": "1"})
505     hostapd.add_ap(apdev[1]['ifname'],
506                    { "ssid": "wps2", "eap_server": "1", "wps_state": "2",
507                      "wpa_passphrase": "123456789", "wpa": "2",
508                      "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
509                      "wps_independent": "1"})
510     hapd = hostapd.Hostapd(apdev[0]['ifname'])
511     hapd.request("WPS_PBC")
512     hapd2 = hostapd.Hostapd(apdev[1]['ifname'])
513     hapd2.request("WPS_PBC")
514     logger.info("WPS provisioning step")
515     dev[0].dump_monitor()
516     dev[0].request("WPS_PBC")
517     ev = dev[0].wait_event(["WPS-OVERLAP-DETECTED"], timeout=15)
518     if ev is None:
519         raise Exception("PBC session overlap not detected")
520
521 def test_ap_wps_pbc_overlap_2sta(dev, apdev):
522     """WPS PBC session overlap with two active STAs"""
523     ssid = "test-wps-pbc-overlap"
524     hostapd.add_ap(apdev[0]['ifname'],
525                    { "ssid": ssid, "eap_server": "1", "wps_state": "2",
526                      "wpa_passphrase": "12345678", "wpa": "2",
527                      "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
528     hapd = hostapd.Hostapd(apdev[0]['ifname'])
529     logger.info("WPS provisioning step")
530     hapd.request("WPS_PBC")
531     dev[0].dump_monitor()
532     dev[1].dump_monitor()
533     dev[0].request("WPS_PBC")
534     dev[1].request("WPS_PBC")
535     ev = dev[0].wait_event(["WPS-M2D"], timeout=15)
536     if ev is None:
537         raise Exception("PBC session overlap not detected (dev0)")
538     if "config_error=12" not in ev:
539         raise Exception("PBC session overlap not correctly reported (dev0)")
540     ev = dev[1].wait_event(["WPS-M2D"], timeout=15)
541     if ev is None:
542         raise Exception("PBC session overlap not detected (dev1)")
543     if "config_error=12" not in ev:
544         raise Exception("PBC session overlap not correctly reported (dev1)")
545
546 def test_ap_wps_cancel(dev, apdev):
547     """WPS AP cancelling enabled config method"""
548     ssid = "test-wps-ap-cancel"
549     hostapd.add_ap(apdev[0]['ifname'],
550                    { "ssid": ssid, "eap_server": "1", "wps_state": "2",
551                      "wpa_passphrase": "12345678", "wpa": "2",
552                      "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
553     bssid = apdev[0]['bssid']
554     hapd = hostapd.Hostapd(apdev[0]['ifname'])
555
556     logger.info("Verify PBC enable/cancel")
557     hapd.request("WPS_PBC")
558     dev[0].scan(freq="2412")
559     bss = dev[0].get_bss(apdev[0]['bssid'])
560     if "[WPS-PBC]" not in bss['flags']:
561         raise Exception("WPS-PBC flag missing")
562     if "FAIL" in hapd.request("WPS_CANCEL"):
563         raise Exception("WPS_CANCEL failed")
564     dev[0].scan(freq="2412")
565     bss = dev[0].get_bss(apdev[0]['bssid'])
566     if "[WPS-PBC]" in bss['flags']:
567         raise Exception("WPS-PBC flag not cleared")
568
569     logger.info("Verify PIN enable/cancel")
570     hapd.request("WPS_PIN any 12345670")
571     dev[0].scan(freq="2412")
572     bss = dev[0].get_bss(apdev[0]['bssid'])
573     if "[WPS-AUTH]" not in bss['flags']:
574         raise Exception("WPS-AUTH flag missing")
575     if "FAIL" in hapd.request("WPS_CANCEL"):
576         raise Exception("WPS_CANCEL failed")
577     dev[0].scan(freq="2412")
578     bss = dev[0].get_bss(apdev[0]['bssid'])
579     if "[WPS-AUTH]" in bss['flags']:
580         raise Exception("WPS-AUTH flag not cleared")
581
582 def test_ap_wps_er_add_enrollee(dev, apdev):
583     """WPS ER configuring AP and adding a new enrollee using PIN"""
584     ssid = "wps-er-add-enrollee"
585     ap_pin = "12345670"
586     ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
587     hostapd.add_ap(apdev[0]['ifname'],
588                    { "ssid": ssid, "eap_server": "1", "wps_state": "1",
589                      "device_name": "Wireless AP", "manufacturer": "Company",
590                      "model_name": "WAP", "model_number": "123",
591                      "serial_number": "12345", "device_type": "6-0050F204-1",
592                      "os_version": "01020300",
593                      "config_methods": "label push_button",
594                      "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
595     logger.info("WPS configuration step")
596     new_passphrase = "1234567890"
597     dev[0].dump_monitor()
598     dev[0].wps_reg(apdev[0]['bssid'], ap_pin, ssid, "WPA2PSK", "CCMP",
599                    new_passphrase)
600     status = dev[0].get_status()
601     if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
602         raise Exception("Not fully connected")
603     if status['ssid'] != ssid:
604         raise Exception("Unexpected SSID")
605     if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
606         raise Exception("Unexpected encryption configuration")
607     if status['key_mgmt'] != 'WPA2-PSK':
608         raise Exception("Unexpected key_mgmt")
609
610     logger.info("Start ER")
611     dev[0].request("WPS_ER_START ifname=lo")
612     ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
613     if ev is None:
614         raise Exception("AP discovery timed out")
615     if ap_uuid not in ev:
616         raise Exception("Expected AP UUID not found")
617
618     logger.info("Learn AP configuration through UPnP")
619     dev[0].dump_monitor()
620     dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
621     ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
622     if ev is None:
623         raise Exception("AP learn timed out")
624     if ap_uuid not in ev:
625         raise Exception("Expected AP UUID not in settings")
626     if "ssid=" + ssid not in ev:
627         raise Exception("Expected SSID not in settings")
628     if "key=" + new_passphrase not in ev:
629         raise Exception("Expected passphrase not in settings")
630
631     logger.info("Add Enrollee using ER")
632     pin = dev[1].wps_read_pin()
633     dev[0].dump_monitor()
634     dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
635     dev[1].dump_monitor()
636     dev[1].request("WPS_PIN any " + pin)
637     ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=30)
638     if ev is None:
639         raise Exception("Enrollee did not report success")
640     ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
641     if ev is None:
642         raise Exception("Association with the AP timed out")
643     ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
644     if ev is None:
645         raise Exception("WPS ER did not report success")
646     hwsim_utils.test_connectivity_sta(dev[0], dev[1])
647
648     logger.info("Verify registrar selection behavior")
649     dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
650     dev[1].request("DISCONNECT")
651     dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
652     dev[1].scan(freq="2412")
653     bss = dev[1].get_bss(apdev[0]['bssid'])
654     if "[WPS-AUTH]" not in bss['flags']:
655         raise Exception("WPS-AUTH flag missing")
656
657     logger.info("Stop ER")
658     dev[0].dump_monitor()
659     dev[0].request("WPS_ER_STOP")
660     ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"])
661     if ev is None:
662         raise Exception("WPS ER unsubscription timed out")
663
664     dev[1].scan(freq="2412")
665     bss = dev[1].get_bss(apdev[0]['bssid'])
666     if "[WPS-AUTH]" in bss['flags']:
667         raise Exception("WPS-AUTH flag not removed")
668
669 def test_ap_wps_er_add_enrollee_pbc(dev, apdev):
670     """WPS ER connected to AP and adding a new enrollee using PBC"""
671     ssid = "wps-er-add-enrollee-pbc"
672     ap_pin = "12345670"
673     ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
674     hostapd.add_ap(apdev[0]['ifname'],
675                    { "ssid": ssid, "eap_server": "1", "wps_state": "2",
676                      "wpa_passphrase": "12345678", "wpa": "2",
677                      "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
678                      "device_name": "Wireless AP", "manufacturer": "Company",
679                      "model_name": "WAP", "model_number": "123",
680                      "serial_number": "12345", "device_type": "6-0050F204-1",
681                      "os_version": "01020300",
682                      "config_methods": "label push_button",
683                      "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
684     logger.info("Learn AP configuration")
685     dev[0].dump_monitor()
686     dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
687     status = dev[0].get_status()
688     if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
689         raise Exception("Not fully connected")
690
691     logger.info("Start ER")
692     dev[0].request("WPS_ER_START ifname=lo")
693     ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
694     if ev is None:
695         raise Exception("AP discovery timed out")
696     if ap_uuid not in ev:
697         raise Exception("Expected AP UUID not found")
698
699     logger.info("Use learned network configuration on ER")
700     dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
701
702     logger.info("Add Enrollee using ER and PBC")
703     dev[0].dump_monitor()
704     enrollee = dev[1].p2p_interface_addr()
705     dev[1].dump_monitor()
706     dev[1].request("WPS_PBC")
707
708     for i in range(0, 2):
709         ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=15)
710         if ev is None:
711             raise Exception("Enrollee discovery timed out")
712         if enrollee in ev:
713             break
714         if i == 1:
715             raise Exception("Expected Enrollee not found")
716     dev[0].request("WPS_ER_PBC " + enrollee)
717
718     ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=15)
719     if ev is None:
720         raise Exception("Enrollee did not report success")
721     ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
722     if ev is None:
723         raise Exception("Association with the AP timed out")
724     ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
725     if ev is None:
726         raise Exception("WPS ER did not report success")
727     hwsim_utils.test_connectivity_sta(dev[0], dev[1])
728
729     # verify BSSID selection of the AP instead of UUID
730     if "FAIL" in dev[0].request("WPS_ER_SET_CONFIG " + apdev[0]['bssid'] + " 0"):
731         raise Exception("Could not select AP based on BSSID")
732
733 def test_ap_wps_er_config_ap(dev, apdev):
734     """WPS ER configuring AP over UPnP"""
735     ssid = "wps-er-ap-config"
736     ap_pin = "12345670"
737     ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
738     hostapd.add_ap(apdev[0]['ifname'],
739                    { "ssid": ssid, "eap_server": "1", "wps_state": "2",
740                      "wpa_passphrase": "12345678", "wpa": "2",
741                      "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
742                      "device_name": "Wireless AP", "manufacturer": "Company",
743                      "model_name": "WAP", "model_number": "123",
744                      "serial_number": "12345", "device_type": "6-0050F204-1",
745                      "os_version": "01020300",
746                      "config_methods": "label push_button",
747                      "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
748
749     logger.info("Connect ER to the AP")
750     dev[0].connect(ssid, psk="12345678", scan_freq="2412")
751
752     logger.info("WPS configuration step")
753     dev[0].request("WPS_ER_START ifname=lo")
754     ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
755     if ev is None:
756         raise Exception("AP discovery timed out")
757     if ap_uuid not in ev:
758         raise Exception("Expected AP UUID not found")
759     new_passphrase = "1234567890"
760     dev[0].request("WPS_ER_CONFIG " + apdev[0]['bssid'] + " " + ap_pin + " " +
761                    ssid.encode("hex") + " WPA2PSK CCMP " +
762                    new_passphrase.encode("hex"))
763     ev = dev[0].wait_event(["WPS-SUCCESS"])
764     if ev is None:
765         raise Exception("WPS ER configuration operation timed out")
766     dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
767     dev[0].connect(ssid, psk="1234567890", scan_freq="2412")
768
769 def test_ap_wps_fragmentation(dev, apdev):
770     """WPS with fragmentation in EAP-WSC and mixed mode WPA+WPA2"""
771     ssid = "test-wps-fragmentation"
772     hostapd.add_ap(apdev[0]['ifname'],
773                    { "ssid": ssid, "eap_server": "1", "wps_state": "2",
774                      "wpa_passphrase": "12345678", "wpa": "3",
775                      "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
776                      "wpa_pairwise": "TKIP",
777                      "fragment_size": "50" })
778     hapd = hostapd.Hostapd(apdev[0]['ifname'])
779     logger.info("WPS provisioning step")
780     hapd.request("WPS_PBC")
781     dev[0].dump_monitor()
782     dev[0].request("SET wps_fragment_size 50")
783     dev[0].request("WPS_PBC")
784     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
785     if ev is None:
786         raise Exception("Association with the AP timed out")
787     status = dev[0].get_status()
788     if status['wpa_state'] != 'COMPLETED':
789         raise Exception("Not fully connected")
790     if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
791         raise Exception("Unexpected encryption configuration")
792     if status['key_mgmt'] != 'WPA2-PSK':
793         raise Exception("Unexpected key_mgmt")
794
795 def test_ap_wps_new_version_sta(dev, apdev):
796     """WPS compatibility with new version number on the station"""
797     ssid = "test-wps-ver"
798     hostapd.add_ap(apdev[0]['ifname'],
799                    { "ssid": ssid, "eap_server": "1", "wps_state": "2",
800                      "wpa_passphrase": "12345678", "wpa": "2",
801                      "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
802     hapd = hostapd.Hostapd(apdev[0]['ifname'])
803     logger.info("WPS provisioning step")
804     hapd.request("WPS_PBC")
805     dev[0].dump_monitor()
806     dev[0].request("SET wps_version_number 0x43")
807     dev[0].request("WPS_PBC")
808     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
809     if ev is None:
810         raise Exception("Association with the AP timed out")
811
812 def test_ap_wps_new_version_ap(dev, apdev):
813     """WPS compatibility with new version number on the AP"""
814     ssid = "test-wps-ver"
815     hostapd.add_ap(apdev[0]['ifname'],
816                    { "ssid": ssid, "eap_server": "1", "wps_state": "2",
817                      "wpa_passphrase": "12345678", "wpa": "2",
818                      "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
819     hapd = hostapd.Hostapd(apdev[0]['ifname'])
820     logger.info("WPS provisioning step")
821     if "FAIL" in hapd.request("SET wps_version_number 0x43"):
822         raise Exception("Failed to enable test functionality")
823     hapd.request("WPS_PBC")
824     dev[0].dump_monitor()
825     dev[0].request("WPS_PBC")
826     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
827     hapd.request("SET wps_version_number 0x20")
828     if ev is None:
829         raise Exception("Association with the AP timed out")
830
831 def test_ap_wps_check_pin(dev, apdev):
832     """Verify PIN checking through control interface"""
833     hostapd.add_ap(apdev[0]['ifname'],
834                    { "ssid": "wps", "eap_server": "1", "wps_state": "2",
835                      "wpa_passphrase": "12345678", "wpa": "2",
836                      "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
837     hapd = hostapd.Hostapd(apdev[0]['ifname'])
838     for t in [ ("12345670", "12345670"),
839                ("12345678", "FAIL-CHECKSUM"),
840                ("1234-5670", "12345670"),
841                ("1234 5670", "12345670"),
842                ("1-2.3:4 5670", "12345670") ]:
843         res = hapd.request("WPS_CHECK_PIN " + t[0]).rstrip('\n')
844         res2 = dev[0].request("WPS_CHECK_PIN " + t[0]).rstrip('\n')
845         if res != res2:
846             raise Exception("Unexpected difference in WPS_CHECK_PIN responses")
847         if res != t[1]:
848             raise Exception("Incorrect WPS_CHECK_PIN response {} (expected {})".format(res, t[1]))