2 # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
10 logger = logging.getLogger()
16 def check_cipher(dev, ap, cipher):
17 if cipher not in dev.get_capability("pairwise"):
19 params = { "ssid": "test-wpa2-psk",
20 "wpa_passphrase": "12345678",
22 "wpa_key_mgmt": "WPA-PSK",
23 "rsn_pairwise": cipher }
24 hapd = hostapd.add_ap(ap['ifname'], params)
25 dev.connect("test-wpa2-psk", psk="12345678",
26 pairwise=cipher, group=cipher, scan_freq="2412")
27 hwsim_utils.test_connectivity(dev, hapd)
29 def test_ap_cipher_tkip(dev, apdev):
30 """WPA2-PSK/TKIP connection"""
31 return check_cipher(dev[0], apdev[0], "TKIP")
33 def test_ap_cipher_tkip_countermeasures_ap(dev, apdev):
34 """WPA-PSK/TKIP countermeasures (detected by AP)"""
35 testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (dev[0].get_driver_status_field("phyname"), dev[0].ifname)
36 if not os.path.exists(testfile):
39 params = { "ssid": "tkip-countermeasures",
40 "wpa_passphrase": "12345678",
42 "wpa_key_mgmt": "WPA-PSK",
43 "wpa_pairwise": "TKIP" }
44 hapd = hostapd.add_ap(apdev[0]['ifname'], params)
46 dev[0].connect("tkip-countermeasures", psk="12345678",
47 pairwise="TKIP", group="TKIP", scan_freq="2412")
50 cmd = subprocess.Popen(["sudo", "tee", testfile],
51 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
52 cmd.stdin.write(apdev[0]['bssid'])
56 ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
58 raise Exception("Unexpected disconnection on first Michael MIC failure")
60 cmd = subprocess.Popen(["sudo", "tee", testfile],
61 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
62 cmd.stdin.write("ff:ff:ff:ff:ff:ff")
66 ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=10)
68 raise Exception("No disconnection after two Michael MIC failures")
69 if "reason=14" not in ev:
70 raise Exception("Unexpected disconnection reason: " + ev)
71 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
73 raise Exception("Unexpected connection during TKIP countermeasures")
75 def test_ap_cipher_tkip_countermeasures_sta(dev, apdev):
76 """WPA-PSK/TKIP countermeasures (detected by STA)"""
77 params = { "ssid": "tkip-countermeasures",
78 "wpa_passphrase": "12345678",
80 "wpa_key_mgmt": "WPA-PSK",
81 "wpa_pairwise": "TKIP" }
82 hapd = hostapd.add_ap(apdev[0]['ifname'], params)
84 testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (hapd.get_driver_status_field("phyname"), apdev[0]['ifname'])
85 if not os.path.exists(testfile):
88 dev[0].connect("tkip-countermeasures", psk="12345678",
89 pairwise="TKIP", group="TKIP", scan_freq="2412")
92 cmd = subprocess.Popen(["sudo", "tee", testfile],
93 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
94 cmd.stdin.write(dev[0].p2p_dev_addr())
98 ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
100 raise Exception("Unexpected disconnection on first Michael MIC failure")
102 cmd = subprocess.Popen(["sudo", "tee", testfile],
103 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
104 cmd.stdin.write("ff:ff:ff:ff:ff:ff")
108 ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=10)
110 raise Exception("No disconnection after two Michael MIC failures")
111 if "reason=14 locally_generated=1" not in ev:
112 raise Exception("Unexpected disconnection reason: " + ev)
113 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
115 raise Exception("Unexpected connection during TKIP countermeasures")
117 def test_ap_cipher_ccmp(dev, apdev):
118 """WPA2-PSK/CCMP connection"""
119 return check_cipher(dev[0], apdev[0], "CCMP")
121 def test_ap_cipher_gcmp(dev, apdev):
122 """WPA2-PSK/GCMP connection"""
123 return check_cipher(dev[0], apdev[0], "GCMP")
125 def test_ap_cipher_ccmp_256(dev, apdev):
126 """WPA2-PSK/CCMP-256 connection"""
127 return check_cipher(dev[0], apdev[0], "CCMP-256")
129 def test_ap_cipher_gcmp_256(dev, apdev):
130 """WPA2-PSK/GCMP-256 connection"""
131 return check_cipher(dev[0], apdev[0], "GCMP-256")
133 def test_ap_cipher_mixed_wpa_wpa2(dev, apdev):
134 """WPA2-PSK/CCMP/ and WPA-PSK/TKIP mixed configuration"""
135 ssid = "test-wpa-wpa2-psk"
136 passphrase = "12345678"
137 params = { "ssid": ssid,
138 "wpa_passphrase": passphrase,
140 "wpa_key_mgmt": "WPA-PSK",
141 "rsn_pairwise": "CCMP",
142 "wpa_pairwise": "TKIP" }
143 hapd = hostapd.add_ap(apdev[0]['ifname'], params)
144 dev[0].connect(ssid, psk=passphrase, proto="WPA2",
145 pairwise="CCMP", group="TKIP", scan_freq="2412")
146 status = dev[0].get_status()
147 if status['key_mgmt'] != 'WPA2-PSK':
148 raise Exception("Incorrect key_mgmt reported")
149 if status['pairwise_cipher'] != 'CCMP':
150 raise Exception("Incorrect pairwise_cipher reported")
151 if status['group_cipher'] != 'TKIP':
152 raise Exception("Incorrect group_cipher reported")
153 bss = dev[0].get_bss(apdev[0]['bssid'])
154 if bss['ssid'] != ssid:
155 raise Exception("Unexpected SSID in the BSS entry")
156 if "[WPA-PSK-TKIP]" not in bss['flags']:
157 raise Exception("Missing BSS flag WPA-PSK-TKIP")
158 if "[WPA2-PSK-CCMP]" not in bss['flags']:
159 raise Exception("Missing BSS flag WPA2-PSK-CCMP")
160 hwsim_utils.test_connectivity(dev[0], hapd)
162 dev[1].connect(ssid, psk=passphrase, proto="WPA",
163 pairwise="TKIP", group="TKIP", scan_freq="2412")
164 status = dev[1].get_status()
165 if status['key_mgmt'] != 'WPA-PSK':
166 raise Exception("Incorrect key_mgmt reported")
167 if status['pairwise_cipher'] != 'TKIP':
168 raise Exception("Incorrect pairwise_cipher reported")
169 if status['group_cipher'] != 'TKIP':
170 raise Exception("Incorrect group_cipher reported")
171 hwsim_utils.test_connectivity(dev[1], hapd)
172 hwsim_utils.test_connectivity(dev[0], dev[1])