tests: Verify PMKSA caching
[mech_eap.git] / tests / hwsim / wpasupplicant.py
1 #!/usr/bin/python
2 #
3 # Python class for controlling wpa_supplicant
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 os
10 import time
11 import logging
12 import re
13 import subprocess
14 import wpaspy
15
16 logger = logging.getLogger()
17 wpas_ctrl = '/var/run/wpa_supplicant'
18
19 class WpaSupplicant:
20     def __init__(self, ifname, global_iface=None):
21         self.ifname = ifname
22         self.group_ifname = None
23         self.ctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
24         self.mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
25         self.mon.attach()
26
27         self.global_iface = global_iface
28         if global_iface:
29             self.global_ctrl = wpaspy.Ctrl(global_iface)
30             self.global_mon = wpaspy.Ctrl(global_iface)
31             self.global_mon.attach()
32
33     def request(self, cmd):
34         logger.debug(self.ifname + ": CTRL: " + cmd)
35         return self.ctrl.request(cmd)
36
37     def global_request(self, cmd):
38         if self.global_iface is None:
39             self.request(cmd)
40         else:
41             logger.debug(self.ifname + ": CTRL: " + cmd)
42             return self.global_ctrl.request(cmd)
43
44     def group_request(self, cmd):
45         if self.group_ifname and self.group_ifname != self.ifname:
46             logger.debug(self.group_ifname + ": CTRL: " + cmd)
47             gctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, self.group_ifname))
48             return gctrl.request(cmd)
49         return self.request(cmd)
50
51     def ping(self):
52         return "PONG" in self.request("PING")
53
54     def reset(self):
55         res = self.request("FLUSH")
56         if not "OK" in res:
57             logger.info("FLUSH to " + self.ifname + " failed: " + res)
58         self.request("SET ignore_old_scan_res 0")
59         self.request("SET external_sim 0")
60         self.request("SET hessid 00:00:00:00:00:00")
61         self.request("SET access_network_type 15")
62         self.request("SET p2p_add_cli_chan 0")
63         self.request("SET p2p_no_go_freq ")
64         self.request("SET p2p_pref_chan ")
65         self.request("SET disallow_aps ")
66         self.request("SET p2p_no_group_iface 1")
67         self.request("P2P_SET per_sta_psk 0")
68         self.request("P2P_SET disabled 0")
69         self.request("P2P_SERVICE_FLUSH")
70         self.group_ifname = None
71         self.dump_monitor()
72
73         iter = 0
74         while iter < 60:
75             state = self.get_driver_status_field("scan_state")
76             if "SCAN_STARTED" in state or "SCAN_REQUESTED" in state:
77                 logger.info(self.ifname + ": Waiting for scan operation to complete before continuing")
78                 time.sleep(1)
79             else:
80                 break
81             iter = iter + 1
82         if iter == 60:
83             logger.error(self.ifname + ": Driver scan state did not clear")
84             print "Trying to clear cfg80211/mac80211 scan state"
85             try:
86                 cmd = ["sudo", "ifconfig", self.ifname, "down"]
87                 subprocess.call(cmd)
88             except subprocess.CalledProcessError, e:
89                 logger.info("ifconfig failed: " + str(e.returncode))
90                 logger.info(e.output)
91             try:
92                 cmd = ["sudo", "ifconfig", self.ifname, "up"]
93                 subprocess.call(cmd)
94             except subprocess.CalledProcessError, e:
95                 logger.info("ifconfig failed: " + str(e.returncode))
96                 logger.info(e.output)
97
98         if not self.ping():
99             logger.info("No PING response from " + self.ifname + " after reset")
100
101     def add_network(self):
102         id = self.request("ADD_NETWORK")
103         if "FAIL" in id:
104             raise Exception("ADD_NETWORK failed")
105         return int(id)
106
107     def remove_network(self, id):
108         id = self.request("REMOVE_NETWORK " + str(id))
109         if "FAIL" in id:
110             raise Exception("REMOVE_NETWORK failed")
111         return None
112
113     def set_network(self, id, field, value):
114         res = self.request("SET_NETWORK " + str(id) + " " + field + " " + value)
115         if "FAIL" in res:
116             raise Exception("SET_NETWORK failed")
117         return None
118
119     def set_network_quoted(self, id, field, value):
120         res = self.request("SET_NETWORK " + str(id) + " " + field + ' "' + value + '"')
121         if "FAIL" in res:
122             raise Exception("SET_NETWORK failed")
123         return None
124
125     def list_networks(self):
126         res = self.request("LIST_NETWORKS")
127         lines = res.splitlines()
128         networks = []
129         for l in lines:
130             if "network id" in l:
131                 continue
132             [id,ssid,bssid,flags] = l.split('\t')
133             network = {}
134             network['id'] = id
135             network['ssid'] = ssid
136             network['bssid'] = bssid
137             network['flags'] = flags
138             networks.append(network)
139         return networks
140
141     def hs20_enable(self):
142         self.request("SET interworking 1")
143         self.request("SET hs20 1")
144
145     def add_cred(self):
146         id = self.request("ADD_CRED")
147         if "FAIL" in id:
148             raise Exception("ADD_CRED failed")
149         return int(id)
150
151     def remove_cred(self, id):
152         id = self.request("REMOVE_CRED " + str(id))
153         if "FAIL" in id:
154             raise Exception("REMOVE_CRED failed")
155         return None
156
157     def set_cred(self, id, field, value):
158         res = self.request("SET_CRED " + str(id) + " " + field + " " + value)
159         if "FAIL" in res:
160             raise Exception("SET_CRED failed")
161         return None
162
163     def set_cred_quoted(self, id, field, value):
164         res = self.request("SET_CRED " + str(id) + " " + field + ' "' + value + '"')
165         if "FAIL" in res:
166             raise Exception("SET_CRED failed")
167         return None
168
169     def add_cred_values(self, params):
170         id = self.add_cred()
171
172         quoted = [ "realm", "username", "password", "domain", "imsi",
173                    "excluded_ssid", "milenage", "ca_cert", "client_cert",
174                    "private_key" ]
175         for field in quoted:
176             if field in params:
177                 self.set_cred_quoted(id, field, params[field])
178
179         not_quoted = [ "eap", "roaming_consortium",
180                        "required_roaming_consortium" ]
181         for field in not_quoted:
182             if field in params:
183                 self.set_cred(id, field, params[field])
184
185         return id;
186
187     def select_network(self, id):
188         id = self.request("SELECT_NETWORK " + str(id))
189         if "FAIL" in id:
190             raise Exception("SELECT_NETWORK failed")
191         return None
192
193     def connect_network(self, id):
194         self.dump_monitor()
195         self.select_network(id)
196         ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
197         if ev is None:
198             raise Exception("Association with the AP timed out")
199         self.dump_monitor()
200
201     def get_status(self):
202         res = self.request("STATUS")
203         lines = res.splitlines()
204         vals = dict()
205         for l in lines:
206             [name,value] = l.split('=', 1)
207             vals[name] = value
208         return vals
209
210     def get_status_field(self, field):
211         vals = self.get_status()
212         if field in vals:
213             return vals[field]
214         return None
215
216     def get_group_status(self):
217         res = self.group_request("STATUS")
218         lines = res.splitlines()
219         vals = dict()
220         for l in lines:
221             [name,value] = l.split('=', 1)
222             vals[name] = value
223         return vals
224
225     def get_group_status_field(self, field):
226         vals = self.get_group_status()
227         if field in vals:
228             return vals[field]
229         return None
230
231     def get_driver_status(self):
232         res = self.request("STATUS-DRIVER")
233         lines = res.splitlines()
234         vals = dict()
235         for l in lines:
236             [name,value] = l.split('=', 1)
237             vals[name] = value
238         return vals
239
240     def get_driver_status_field(self, field):
241         vals = self.get_driver_status()
242         if field in vals:
243             return vals[field]
244         return None
245
246     def p2p_dev_addr(self):
247         return self.get_status_field("p2p_device_address")
248
249     def p2p_interface_addr(self):
250         return self.get_group_status_field("address")
251
252     def p2p_listen(self):
253         return self.global_request("P2P_LISTEN")
254
255     def p2p_find(self, social=False):
256         if social:
257             return self.global_request("P2P_FIND type=social")
258         return self.global_request("P2P_FIND")
259
260     def p2p_stop_find(self):
261         return self.global_request("P2P_STOP_FIND")
262
263     def wps_read_pin(self):
264         #TODO: make this random
265         self.pin = "12345670"
266         return self.pin
267
268     def peer_known(self, peer, full=True):
269         res = self.global_request("P2P_PEER " + peer)
270         if peer.lower() not in res.lower():
271             return False
272         if not full:
273             return True
274         return "[PROBE_REQ_ONLY]" not in res
275
276     def discover_peer(self, peer, full=True, timeout=15, social=True):
277         logger.info(self.ifname + ": Trying to discover peer " + peer)
278         if self.peer_known(peer, full):
279             return True
280         self.p2p_find(social)
281         count = 0
282         while count < timeout:
283             time.sleep(1)
284             count = count + 1
285             if self.peer_known(peer, full):
286                 return True
287         return False
288
289     def get_peer(self, peer):
290         res = self.global_request("P2P_PEER " + peer)
291         if peer.lower() not in res.lower():
292             raise Exception("Peer information not available")
293         lines = res.splitlines()
294         vals = dict()
295         for l in lines:
296             if '=' in l:
297                 [name,value] = l.split('=', 1)
298                 vals[name] = value
299         return vals
300
301     def group_form_result(self, ev, expect_failure=False, go_neg_res=None):
302         if expect_failure:
303             if "P2P-GROUP-STARTED" in ev:
304                 raise Exception("Group formation succeeded when expecting failure")
305             exp = r'<.>(P2P-GO-NEG-FAILURE) status=([0-9]*)'
306             s = re.split(exp, ev)
307             if len(s) < 3:
308                 return None
309             res = {}
310             res['result'] = 'go-neg-failed'
311             res['status'] = int(s[2])
312             return res
313
314         if "P2P-GROUP-STARTED" not in ev:
315             raise Exception("No P2P-GROUP-STARTED event seen")
316
317         exp = r'<.>(P2P-GROUP-STARTED) ([^ ]*) ([^ ]*) ssid="(.*)" freq=([0-9]*) ((?:psk=.*)|(?:passphrase=".*")) go_dev_addr=([0-9a-f:]*)'
318         s = re.split(exp, ev)
319         if len(s) < 8:
320             raise Exception("Could not parse P2P-GROUP-STARTED")
321         res = {}
322         res['result'] = 'success'
323         res['ifname'] = s[2]
324         self.group_ifname = s[2]
325         res['role'] = s[3]
326         res['ssid'] = s[4]
327         res['freq'] = s[5]
328         if "[PERSISTENT]" in ev:
329             res['persistent'] = True
330         else:
331             res['persistent'] = False
332         p = re.match(r'psk=([0-9a-f]*)', s[6])
333         if p:
334             res['psk'] = p.group(1)
335         p = re.match(r'passphrase="(.*)"', s[6])
336         if p:
337             res['passphrase'] = p.group(1)
338         res['go_dev_addr'] = s[7]
339
340         if go_neg_res:
341             exp = r'<.>(P2P-GO-NEG-SUCCESS) role=(GO|client) freq=([0-9]*)'
342             s = re.split(exp, go_neg_res)
343             if len(s) < 4:
344                 raise Exception("Could not parse P2P-GO-NEG-SUCCESS")
345             res['go_neg_role'] = s[2]
346             res['go_neg_freq'] = s[3]
347
348         return res
349
350     def p2p_go_neg_auth(self, peer, pin, method, go_intent=None, persistent=False, freq=None):
351         if not self.discover_peer(peer):
352             raise Exception("Peer " + peer + " not found")
353         self.dump_monitor()
354         cmd = "P2P_CONNECT " + peer + " " + pin + " " + method + " auth"
355         if go_intent:
356             cmd = cmd + ' go_intent=' + str(go_intent)
357         if freq:
358             cmd = cmd + ' freq=' + str(freq)
359         if persistent:
360             cmd = cmd + " persistent"
361         if "OK" in self.global_request(cmd):
362             return None
363         raise Exception("P2P_CONNECT (auth) failed")
364
365     def p2p_go_neg_auth_result(self, timeout=1, expect_failure=False):
366         go_neg_res = None
367         ev = self.wait_global_event(["P2P-GO-NEG-SUCCESS",
368                                      "P2P-GO-NEG-FAILURE"], timeout);
369         if ev is None:
370             if expect_failure:
371                 return None
372             raise Exception("Group formation timed out")
373         if "P2P-GO-NEG-SUCCESS" in ev:
374             go_neg_res = ev
375             ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout);
376             if ev is None:
377                 if expect_failure:
378                     return None
379                 raise Exception("Group formation timed out")
380         self.dump_monitor()
381         return self.group_form_result(ev, expect_failure, go_neg_res)
382
383     def p2p_go_neg_init(self, peer, pin, method, timeout=0, go_intent=None, expect_failure=False, persistent=False, freq=None):
384         if not self.discover_peer(peer):
385             raise Exception("Peer " + peer + " not found")
386         self.dump_monitor()
387         if pin:
388             cmd = "P2P_CONNECT " + peer + " " + pin + " " + method
389         else:
390             cmd = "P2P_CONNECT " + peer + " " + method
391         if go_intent:
392             cmd = cmd + ' go_intent=' + str(go_intent)
393         if freq:
394             cmd = cmd + ' freq=' + str(freq)
395         if persistent:
396             cmd = cmd + " persistent"
397         if "OK" in self.global_request(cmd):
398             if timeout == 0:
399                 self.dump_monitor()
400                 return None
401             go_neg_res = None
402             ev = self.wait_global_event(["P2P-GO-NEG-SUCCESS",
403                                          "P2P-GO-NEG-FAILURE"], timeout)
404             if ev is None:
405                 if expect_failure:
406                     return None
407                 raise Exception("Group formation timed out")
408             if "P2P-GO-NEG-SUCCESS" in ev:
409                 go_neg_res = ev
410                 ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout)
411                 if ev is None:
412                     if expect_failure:
413                         return None
414                     raise Exception("Group formation timed out")
415             self.dump_monitor()
416             return self.group_form_result(ev, expect_failure, go_neg_res)
417         raise Exception("P2P_CONNECT failed")
418
419     def wait_event(self, events, timeout=10):
420         count = 0
421         while count < timeout * 10:
422             count = count + 1
423             time.sleep(0.1)
424             while self.mon.pending():
425                 ev = self.mon.recv()
426                 logger.debug(self.ifname + ": " + ev)
427                 for event in events:
428                     if event in ev:
429                         return ev
430         return None
431
432     def wait_global_event(self, events, timeout):
433         if self.global_iface is None:
434             self.wait_event(events, timeout)
435         else:
436             count = 0
437             while count < timeout * 10:
438                 count = count + 1
439                 time.sleep(0.1)
440                 while self.global_mon.pending():
441                     ev = self.global_mon.recv()
442                     logger.debug(self.ifname + "(global): " + ev)
443                     for event in events:
444                         if event in ev:
445                             return ev
446         return None
447
448     def wait_go_ending_session(self):
449         ev = self.wait_event(["P2P-GROUP-REMOVED"], timeout=3)
450         if ev is None:
451             raise Exception("Group removal event timed out")
452         if "reason=GO_ENDING_SESSION" not in ev:
453             raise Exception("Unexpected group removal reason")
454
455     def dump_monitor(self):
456         while self.mon.pending():
457             ev = self.mon.recv()
458             logger.debug(self.ifname + ": " + ev)
459         while self.global_mon.pending():
460             ev = self.global_mon.recv()
461             logger.debug(self.ifname + "(global): " + ev)
462
463     def remove_group(self, ifname=None):
464         if ifname is None:
465             ifname = self.group_ifname if self.group_ifname else self.ifname
466         if "OK" not in self.global_request("P2P_GROUP_REMOVE " + ifname):
467             raise Exception("Group could not be removed")
468         self.group_ifname = None
469
470     def p2p_start_go(self, persistent=None, freq=None):
471         self.dump_monitor()
472         cmd = "P2P_GROUP_ADD"
473         if persistent is None:
474             pass
475         elif persistent is True:
476             cmd = cmd + " persistent"
477         else:
478             cmd = cmd + " persistent=" + str(persistent)
479         if freq:
480             cmd = cmd + " freq=" + str(freq)
481         if "OK" in self.global_request(cmd):
482             ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout=5)
483             if ev is None:
484                 raise Exception("GO start up timed out")
485             self.dump_monitor()
486             return self.group_form_result(ev)
487         raise Exception("P2P_GROUP_ADD failed")
488
489     def p2p_go_authorize_client(self, pin):
490         cmd = "WPS_PIN any " + pin
491         if "FAIL" in self.group_request(cmd):
492             raise Exception("Failed to authorize client connection on GO")
493         return None
494
495     def p2p_go_authorize_client_pbc(self):
496         cmd = "WPS_PBC"
497         if "FAIL" in self.group_request(cmd):
498             raise Exception("Failed to authorize client connection on GO")
499         return None
500
501     def p2p_connect_group(self, go_addr, pin, timeout=0, social=False):
502         self.dump_monitor()
503         if not self.discover_peer(go_addr, social=social):
504             raise Exception("GO " + go_addr + " not found")
505         self.dump_monitor()
506         cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
507         if "OK" in self.global_request(cmd):
508             if timeout == 0:
509                 self.dump_monitor()
510                 return None
511             ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout)
512             if ev is None:
513                 raise Exception("Joining the group timed out")
514             self.dump_monitor()
515             return self.group_form_result(ev)
516         raise Exception("P2P_CONNECT(join) failed")
517
518     def tdls_setup(self, peer):
519         cmd = "TDLS_SETUP " + peer
520         if "FAIL" in self.group_request(cmd):
521             raise Exception("Failed to request TDLS setup")
522         return None
523
524     def tdls_teardown(self, peer):
525         cmd = "TDLS_TEARDOWN " + peer
526         if "FAIL" in self.group_request(cmd):
527             raise Exception("Failed to request TDLS teardown")
528         return None
529
530     def connect(self, ssid, psk=None, proto=None, key_mgmt=None, wep_key0=None,
531                 ieee80211w=None, pairwise=None, group=None, scan_freq=None,
532                 eap=None, identity=None, anonymous_identity=None,
533                 password=None, phase1=None, phase2=None, ca_cert=None,
534                 domain_suffix_match=None, password_hex=None,
535                 client_cert=None, private_key=None, peerkey=False, okc=False,
536                 wait_connect=True, only_add_network=False):
537         logger.info("Connect STA " + self.ifname + " to AP")
538         id = self.add_network()
539         self.set_network_quoted(id, "ssid", ssid)
540         if psk:
541             self.set_network_quoted(id, "psk", psk)
542         if proto:
543             self.set_network(id, "proto", proto)
544         if key_mgmt:
545             self.set_network(id, "key_mgmt", key_mgmt)
546         if ieee80211w:
547             self.set_network(id, "ieee80211w", ieee80211w)
548         if pairwise:
549             self.set_network(id, "pairwise", pairwise)
550         if group:
551             self.set_network(id, "group", group)
552         if wep_key0:
553             self.set_network(id, "wep_key0", wep_key0)
554         if scan_freq:
555             self.set_network(id, "scan_freq", scan_freq)
556         if eap:
557             self.set_network(id, "eap", eap)
558         if identity:
559             self.set_network_quoted(id, "identity", identity)
560         if anonymous_identity:
561             self.set_network_quoted(id, "anonymous_identity",
562                                     anonymous_identity)
563         if password:
564             self.set_network_quoted(id, "password", password)
565         if password_hex:
566             self.set_network(id, "password", password_hex)
567         if ca_cert:
568             self.set_network_quoted(id, "ca_cert", ca_cert)
569         if client_cert:
570             self.set_network_quoted(id, "client_cert", client_cert)
571         if private_key:
572             self.set_network_quoted(id, "private_key", private_key)
573         if phase1:
574             self.set_network_quoted(id, "phase1", phase1)
575         if phase2:
576             self.set_network_quoted(id, "phase2", phase2)
577         if domain_suffix_match:
578             self.set_network_quoted(id, "domain_suffix_match",
579                                     domain_suffix_match)
580         if peerkey:
581             self.set_network(id, "peerkey", "1")
582         if okc:
583             self.set_network(id, "proactive_key_caching", "1")
584         if only_add_network:
585             return id
586         if wait_connect:
587             self.connect_network(id)
588         else:
589             self.dump_monitor()
590             self.select_network(id)
591         return id
592
593     def scan(self, type=None, freq=None):
594         if type:
595             cmd = "SCAN TYPE=" + type
596         else:
597             cmd = "SCAN"
598         if freq:
599             cmd = cmd + " freq=" + freq
600         self.dump_monitor()
601         if not "OK" in self.request(cmd):
602             raise Exception("Failed to trigger scan")
603         ev = self.wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
604         if ev is None:
605             raise Exception("Scan timed out")
606
607     def roam(self, bssid):
608         self.dump_monitor()
609         self.request("ROAM " + bssid)
610         ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
611         if ev is None:
612             raise Exception("Roaming with the AP timed out")
613         self.dump_monitor()
614
615     def roam_over_ds(self, bssid):
616         self.dump_monitor()
617         self.request("FT_DS " + bssid)
618         ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
619         if ev is None:
620             raise Exception("Roaming with the AP timed out")
621         self.dump_monitor()
622
623     def wps_reg(self, bssid, pin, new_ssid=None, key_mgmt=None, cipher=None,
624                 new_passphrase=None, no_wait=False):
625         self.dump_monitor()
626         if new_ssid:
627             self.request("WPS_REG " + bssid + " " + pin + " " +
628                          new_ssid.encode("hex") + " " + key_mgmt + " " +
629                          cipher + " " + new_passphrase.encode("hex"))
630             if no_wait:
631                 return
632             ev = self.wait_event(["WPS-SUCCESS"], timeout=15)
633         else:
634             self.request("WPS_REG " + bssid + " " + pin)
635             if no_wait:
636                 return
637             ev = self.wait_event(["WPS-CRED-RECEIVED"], timeout=15)
638             if ev is None:
639                 raise Exception("WPS cred timed out")
640             ev = self.wait_event(["WPS-FAIL"], timeout=15)
641         if ev is None:
642             raise Exception("WPS timed out")
643         ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
644         if ev is None:
645             raise Exception("Association with the AP timed out")
646
647     def relog(self):
648         self.request("RELOG")
649
650     def wait_completed(self, timeout=10):
651         for i in range(0, timeout * 2):
652             if self.get_status_field("wpa_state") == "COMPLETED":
653                 return
654             time.sleep(0.5)
655         raise Exception("Timeout while waiting for COMPLETED state")
656
657     def get_capability(self, field):
658         res = self.request("GET_CAPABILITY " + field)
659         if "FAIL" in res:
660             return None
661         return res.split(' ')
662
663     def get_bss(self, bssid):
664         res = self.request("BSS " + bssid)
665         lines = res.splitlines()
666         vals = dict()
667         for l in lines:
668             [name,value] = l.split('=', 1)
669             vals[name] = value
670         return vals
671
672     def get_pmksa(self, bssid):
673         res = self.request("PMKSA")
674         lines = res.splitlines()
675         for l in lines:
676             if bssid not in l:
677                 continue
678             vals = dict()
679             [index,aa,pmkid,expiration,opportunistic] = l.split(' ')
680             vals['index'] = index
681             vals['pmkid'] = pmkid
682             vals['expiration'] = expiration
683             vals['opportunistic'] = opportunistic
684             return vals
685         return None