1# External password storage
2# Copyright (c) 2014, 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
7from remotehost import remote_compatible
8import logging
9logger = logging.getLogger()
10import os
11import tempfile
12
13import hostapd
14from utils import *
15from wpasupplicant import WpaSupplicant
16from test_ap_hs20 import hs20_ap_params
17from test_ap_hs20 import interworking_select
18from test_ap_hs20 import interworking_connect
19
20@remote_compatible
21def test_ext_password_psk(dev, apdev):
22    """External password storage for PSK"""
23    params = hostapd.wpa2_params(ssid="ext-pw-psk", passphrase="12345678")
24    hostapd.add_ap(apdev[0], params)
25    dev[0].request("SET ext_password_backend test:psk1=12345678")
26    dev[0].connect("ext-pw-psk", raw_psk="ext:psk1", scan_freq="2412")
27
28def test_ext_password_psk_not_found(dev, apdev):
29    """External password storage for PSK and PSK not found"""
30    params = hostapd.wpa2_params(ssid="ext-pw-psk", passphrase="12345678")
31    hostapd.add_ap(apdev[0], params)
32    dev[0].request("SET ext_password_backend test:psk1=12345678")
33    dev[0].connect("ext-pw-psk", raw_psk="ext:psk2", scan_freq="2412",
34                   wait_connect=False)
35    dev[1].request("SET ext_password_backend test:psk1=1234567")
36    dev[1].connect("ext-pw-psk", raw_psk="ext:psk1", scan_freq="2412",
37                   wait_connect=False)
38    dev[2].request("SET ext_password_backend test:psk1=1234567890123456789012345678901234567890123456789012345678901234567890")
39    dev[2].connect("ext-pw-psk", raw_psk="ext:psk1", scan_freq="2412",
40                   wait_connect=False)
41    wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
42    wpas.interface_add("wlan5")
43    wpas.request("SET ext_password_backend test:psk1=123456789012345678901234567890123456789012345678901234567890123q")
44    wpas.connect("ext-pw-psk", raw_psk="ext:psk1", scan_freq="2412",
45                 wait_connect=False)
46
47    ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
48    if ev is not None:
49        raise Exception("Unexpected association")
50    ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
51    if ev is not None:
52        raise Exception("Unexpected association")
53    ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
54    if ev is not None:
55        raise Exception("Unexpected association")
56    ev = wpas.wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
57    if ev is not None:
58        raise Exception("Unexpected association")
59
60def test_ext_password_eap(dev, apdev):
61    """External password storage for EAP password"""
62    params = hostapd.wpa2_eap_params(ssid="ext-pw-eap")
63    hostapd.add_ap(apdev[0], params)
64    dev[0].request("SET ext_password_backend test:pw0=hello|pw1=password|pw2=secret")
65    dev[0].connect("ext-pw-eap", key_mgmt="WPA-EAP", eap="PEAP",
66                   identity="user", password_hex="ext:pw1",
67                   ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
68                   scan_freq="2412")
69
70def test_ext_password_interworking(dev, apdev):
71    """External password storage for Interworking network selection"""
72    skip_with_fips(dev[0])
73    bssid = apdev[0]['bssid']
74    params = hs20_ap_params()
75    hostapd.add_ap(apdev[0], params)
76
77    dev[0].hs20_enable()
78    dev[0].request("SET ext_password_backend test:pw1=password")
79    id = dev[0].add_cred_values({'realm': "example.com",
80                                 'username': "hs20-test"})
81    dev[0].set_cred(id, "password", "ext:pw1")
82    interworking_select(dev[0], bssid, freq="2412")
83    interworking_connect(dev[0], bssid, "TTLS")
84
85def test_ext_password_file_psk(dev, apdev):
86    """External password (file) storage for PSK"""
87    params = hostapd.wpa2_params(ssid="ext-pw-psk", passphrase="12345678")
88    hostapd.add_ap(apdev[0], params)
89    fd, fn = tempfile.mkstemp()
90    with open(fn, "w") as f:
91        f.write("psk1=12345678\n")
92    os.close(fd)
93    dev[0].request("SET ext_password_backend file:%s" % fn)
94    dev[0].connect("ext-pw-psk", raw_psk="ext:psk1", scan_freq="2412")
95    for i in range(2):
96        dev[0].request("REMOVE_NETWORK all")
97        if i == 0:
98            dev[0].wait_disconnected()
99            dev[0].connect("ext-pw-psk", raw_psk="ext:psk2", scan_freq="2412",
100                           wait_connect=False)
101        else:
102            dev[0].connect("ext-pw-psk", raw_psk="ext:psk1", scan_freq="2412",
103                           wait_connect=False)
104        ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED",
105                                "EXT PW: No PSK found from external storage"],
106                               timeout=10)
107        if i == 0:
108            os.unlink(fn)
109        if ev is None:
110            raise Exception("No connection result reported")
111        if "CTRL-EVENT-CONNECTED" in ev:
112            raise Exception("Unexpected connection")
113
114def test_ext_password_file_psk_with_hash(dev, apdev):
115    """External password (file) storage for PSK with hash"""
116    params = hostapd.wpa2_params(ssid="ext-pw-psk", passphrase="12345678#foo")
117    hostapd.add_ap(apdev[0], params)
118    fd, fn = tempfile.mkstemp()
119    with open(fn, "w") as f:
120        f.write("# comment\n\n")
121        f.write("psk1=12345678#foo\n")
122    os.close(fd)
123    dev[0].request("SET ext_password_backend file:%s" % fn)
124    dev[0].connect("ext-pw-psk", raw_psk="ext:psk1", scan_freq="2412")
125
126def test_ext_password_file_with_partially_matching_keys(dev, apdev):
127    """External password (file) storage with partially matching keys"""
128    params = hostapd.wpa2_params(ssid="ext-pw-psk", passphrase="12345678")
129    hostapd.add_ap(apdev[0], params)
130    fd, fn = tempfile.mkstemp()
131    with open(fn, "w") as f:
132        f.write("psk=password\n")
133        f.write("psk1=12345678\n")
134    os.close(fd)
135    dev[0].set("ext_password_backend", "file:%s" % fn)
136    dev[0].connect("ext-pw-psk", raw_psk="ext:psk1", scan_freq="2412")
137    for i in range(2):
138        dev[0].request("REMOVE_NETWORK all")
139        if i == 0:
140            dev[0].wait_disconnected()
141            dev[0].connect("ext-pw-psk", raw_psk="ext:psk12", scan_freq="2412",
142                           wait_connect=False)
143        else:
144            dev[0].connect("ext-pw-psk", raw_psk="ext:ps", scan_freq="2412",
145                           wait_connect=False)
146        ev = dev[0].wait_event(["EXT PW: No PSK found from external storage"],
147                               timeout=10)
148        if ev is None:
149            raise Exception("No connection result reported")
150    dev[0].request("REMOVE_NETWORK all")
151    os.unlink(fn)
152
153@remote_compatible
154def test_ext_password_sae(dev, apdev):
155    """External password storage for SAE"""
156    check_sae_capab(dev[0])
157    params = hostapd.wpa2_params(ssid="ext-pw-sae", passphrase="12345678")
158    params['wpa_key_mgmt'] = 'SAE'
159    hapd = hostapd.add_ap(apdev[0], params)
160
161    dev[0].set("ext_password_backend", "test:sae1=12345678")
162    dev[0].set("sae_groups", "")
163    dev[0].connect("ext-pw-sae", raw_psk="ext:sae1", key_mgmt="SAE",
164                   scan_freq="2412")
165