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