tests: Add support for wlantest for remote hwsim tests
[mech_eap.git] / tests / hwsim / test_ap_ciphers.py
1 # Cipher suite tests
2 # Copyright (c) 2013-2015, Jouni Malinen <j@w1.fi>
3 #
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
6
7 import time
8 import logging
9 logger = logging.getLogger()
10 import os.path
11
12 import hwsim_utils
13 import hostapd
14 from utils import HwsimSkip, skip_with_fips
15 from wlantest import Wlantest
16
17 def check_cipher(dev, ap, cipher):
18     if cipher not in dev.get_capability("pairwise"):
19         raise HwsimSkip("Cipher %s not supported" % cipher)
20     params = { "ssid": "test-wpa2-psk",
21                "wpa_passphrase": "12345678",
22                "wpa": "2",
23                "wpa_key_mgmt": "WPA-PSK",
24                "rsn_pairwise": cipher }
25     hapd = hostapd.add_ap(ap, params)
26     dev.connect("test-wpa2-psk", psk="12345678",
27                 pairwise=cipher, group=cipher, scan_freq="2412")
28     hwsim_utils.test_connectivity(dev, hapd)
29
30 def check_group_mgmt_cipher(dev, ap, cipher):
31     if cipher not in dev.get_capability("group_mgmt"):
32         raise HwsimSkip("Cipher %s not supported" % cipher)
33     params = { "ssid": "test-wpa2-psk-pmf",
34                "wpa_passphrase": "12345678",
35                "wpa": "2",
36                "ieee80211w": "2",
37                "wpa_key_mgmt": "WPA-PSK-SHA256",
38                "rsn_pairwise": "CCMP",
39                "group_mgmt_cipher": cipher }
40     hapd = hostapd.add_ap(ap, params)
41
42     Wlantest.setup(hapd)
43     wt = Wlantest()
44     wt.flush()
45     wt.add_passphrase("12345678")
46
47     dev.connect("test-wpa2-psk-pmf", psk="12345678", ieee80211w="2",
48                 key_mgmt="WPA-PSK-SHA256",
49                 pairwise="CCMP", group="CCMP", scan_freq="2412")
50     hwsim_utils.test_connectivity(dev, hapd)
51     hapd.request("DEAUTHENTICATE ff:ff:ff:ff:ff:ff")
52     dev.wait_disconnected()
53     if wt.get_bss_counter('valid_bip_mmie', ap['bssid']) < 1:
54         raise Exception("No valid BIP MMIE seen")
55     if wt.get_bss_counter('bip_deauth', ap['bssid']) < 1:
56         raise Exception("No valid BIP deauth seen")
57
58     if cipher == "AES-128-CMAC":
59         group_mgmt = "BIP"
60     else:
61         group_mgmt = cipher
62     res =  wt.info_bss('group_mgmt', ap['bssid']).strip()
63     if res != group_mgmt:
64         raise Exception("Unexpected group mgmt cipher: " + res)
65
66 def test_ap_cipher_tkip(dev, apdev):
67     """WPA2-PSK/TKIP connection"""
68     skip_with_fips(dev[0])
69     check_cipher(dev[0], apdev[0], "TKIP")
70
71 def test_ap_cipher_tkip_countermeasures_ap(dev, apdev):
72     """WPA-PSK/TKIP countermeasures (detected by AP)"""
73     skip_with_fips(dev[0])
74     testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (dev[0].get_driver_status_field("phyname"), dev[0].ifname)
75     if not os.path.exists(testfile):
76         raise HwsimSkip("tkip_mic_test not supported in mac80211")
77
78     params = { "ssid": "tkip-countermeasures",
79                "wpa_passphrase": "12345678",
80                "wpa": "1",
81                "wpa_key_mgmt": "WPA-PSK",
82                "wpa_pairwise": "TKIP" }
83     hapd = hostapd.add_ap(apdev[0], params)
84
85     dev[0].connect("tkip-countermeasures", psk="12345678",
86                    pairwise="TKIP", group="TKIP", scan_freq="2412")
87
88     dev[0].dump_monitor()
89     with open(testfile, "w") as f:
90         f.write(apdev[0]['bssid'])
91     ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
92     if ev is not None:
93         raise Exception("Unexpected disconnection on first Michael MIC failure")
94
95     with open(testfile, "w") as f:
96         f.write("ff:ff:ff:ff:ff:ff")
97     ev = dev[0].wait_disconnected(timeout=10,
98                                   error="No disconnection after two Michael MIC failures")
99     if "reason=14" not in ev:
100         raise Exception("Unexpected disconnection reason: " + ev)
101     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
102     if ev is not None:
103         raise Exception("Unexpected connection during TKIP countermeasures")
104
105 def test_ap_cipher_tkip_countermeasures_sta(dev, apdev):
106     """WPA-PSK/TKIP countermeasures (detected by STA)"""
107     skip_with_fips(dev[0])
108     params = { "ssid": "tkip-countermeasures",
109                "wpa_passphrase": "12345678",
110                "wpa": "1",
111                "wpa_key_mgmt": "WPA-PSK",
112                "wpa_pairwise": "TKIP" }
113     hapd = hostapd.add_ap(apdev[0], params)
114
115     testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (hapd.get_driver_status_field("phyname"), apdev[0]['ifname'])
116     if not os.path.exists(testfile):
117         raise HwsimSkip("tkip_mic_test not supported in mac80211")
118
119     dev[0].connect("tkip-countermeasures", psk="12345678",
120                    pairwise="TKIP", group="TKIP", scan_freq="2412")
121
122     dev[0].dump_monitor()
123     with open(testfile, "w") as f:
124         f.write(dev[0].own_addr())
125     ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
126     if ev is not None:
127         raise Exception("Unexpected disconnection on first Michael MIC failure")
128
129     with open(testfile, "w") as f:
130         f.write("ff:ff:ff:ff:ff:ff")
131     ev = dev[0].wait_disconnected(timeout=10,
132                                   error="No disconnection after two Michael MIC failures")
133     if "reason=14 locally_generated=1" not in ev:
134         raise Exception("Unexpected disconnection reason: " + ev)
135     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
136     if ev is not None:
137         raise Exception("Unexpected connection during TKIP countermeasures")
138
139 def test_ap_cipher_ccmp(dev, apdev):
140     """WPA2-PSK/CCMP connection"""
141     check_cipher(dev[0], apdev[0], "CCMP")
142
143 def test_ap_cipher_gcmp(dev, apdev):
144     """WPA2-PSK/GCMP connection"""
145     check_cipher(dev[0], apdev[0], "GCMP")
146
147 def test_ap_cipher_ccmp_256(dev, apdev):
148     """WPA2-PSK/CCMP-256 connection"""
149     check_cipher(dev[0], apdev[0], "CCMP-256")
150
151 def test_ap_cipher_gcmp_256(dev, apdev):
152     """WPA2-PSK/GCMP-256 connection"""
153     check_cipher(dev[0], apdev[0], "GCMP-256")
154
155 def test_ap_cipher_mixed_wpa_wpa2(dev, apdev):
156     """WPA2-PSK/CCMP/ and WPA-PSK/TKIP mixed configuration"""
157     skip_with_fips(dev[0])
158     ssid = "test-wpa-wpa2-psk"
159     passphrase = "12345678"
160     params = { "ssid": ssid,
161                "wpa_passphrase": passphrase,
162                "wpa": "3",
163                "wpa_key_mgmt": "WPA-PSK",
164                "rsn_pairwise": "CCMP",
165                "wpa_pairwise": "TKIP" }
166     hapd = hostapd.add_ap(apdev[0], params)
167     dev[0].connect(ssid, psk=passphrase, proto="WPA2",
168                    pairwise="CCMP", group="TKIP", scan_freq="2412")
169     status = dev[0].get_status()
170     if status['key_mgmt'] != 'WPA2-PSK':
171         raise Exception("Incorrect key_mgmt reported")
172     if status['pairwise_cipher'] != 'CCMP':
173         raise Exception("Incorrect pairwise_cipher reported")
174     if status['group_cipher'] != 'TKIP':
175         raise Exception("Incorrect group_cipher reported")
176     bss = dev[0].get_bss(apdev[0]['bssid'])
177     if bss['ssid'] != ssid:
178         raise Exception("Unexpected SSID in the BSS entry")
179     if "[WPA-PSK-TKIP]" not in bss['flags']:
180         raise Exception("Missing BSS flag WPA-PSK-TKIP")
181     if "[WPA2-PSK-CCMP]" not in bss['flags']:
182         raise Exception("Missing BSS flag WPA2-PSK-CCMP")
183     hwsim_utils.test_connectivity(dev[0], hapd)
184
185     dev[1].connect(ssid, psk=passphrase, proto="WPA",
186                    pairwise="TKIP", group="TKIP", scan_freq="2412")
187     status = dev[1].get_status()
188     if status['key_mgmt'] != 'WPA-PSK':
189         raise Exception("Incorrect key_mgmt reported")
190     if status['pairwise_cipher'] != 'TKIP':
191         raise Exception("Incorrect pairwise_cipher reported")
192     if status['group_cipher'] != 'TKIP':
193         raise Exception("Incorrect group_cipher reported")
194     hwsim_utils.test_connectivity(dev[1], hapd)
195     hwsim_utils.test_connectivity(dev[0], dev[1])
196
197 def test_ap_cipher_bip(dev, apdev):
198     """WPA2-PSK with BIP"""
199     check_group_mgmt_cipher(dev[0], apdev[0], "AES-128-CMAC")
200
201 def test_ap_cipher_bip_gmac_128(dev, apdev):
202     """WPA2-PSK with BIP-GMAC-128"""
203     check_group_mgmt_cipher(dev[0], apdev[0], "BIP-GMAC-128")
204
205 def test_ap_cipher_bip_gmac_256(dev, apdev):
206     """WPA2-PSK with BIP-GMAC-256"""
207     check_group_mgmt_cipher(dev[0], apdev[0], "BIP-GMAC-256")
208
209 def test_ap_cipher_bip_cmac_256(dev, apdev):
210     """WPA2-PSK with BIP-CMAC-256"""
211     check_group_mgmt_cipher(dev[0], apdev[0], "BIP-CMAC-256")