1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.wifi.dpp;
import static com.android.settings.wifi.dpp.WifiQrCode.SECURITY_NO_PASSWORD;
import static com.android.settings.wifi.dpp.WifiQrCode.SECURITY_SAE;
import static com.android.settings.wifi.dpp.WifiQrCode.SECURITY_WEP;
import static com.android.settings.wifi.dpp.WifiQrCode.SECURITY_WPA_PSK;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiManager;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
import java.util.ArrayList;
import java.util.List;
/**
* Wraps the parameters of ZXing reader library's Wi-Fi Network config format.
* Please check {@code WifiQrCode} for detail of the format.
*
* Checks below members of {@code WifiDppUtils} for more information.
* EXTRA_WIFI_SECURITY / EXTRA_WIFI_SSID / EXTRA_WIFI_PRE_SHARED_KEY / EXTRA_WIFI_HIDDEN_SSID /
* EXTRA_QR_CODE
*/
public class WifiNetworkConfig {
static final String FAKE_SSID = "fake network";
static final String FAKE_PASSWORD = "password";
private static final String TAG = "WifiNetworkConfig";
private String mSecurity;
private String mSsid;
private String mPreSharedKey;
private boolean mHiddenSsid;
private int mNetworkId;
private boolean mIsHotspot;
@VisibleForTesting
WifiNetworkConfig(String security, String ssid, String preSharedKey,
boolean hiddenSsid, int networkId, boolean isHotspot) {
mSecurity = security;
mSsid = ssid;
mPreSharedKey = preSharedKey;
mHiddenSsid = hiddenSsid;
mNetworkId = networkId;
mIsHotspot = isHotspot;
}
public WifiNetworkConfig(WifiNetworkConfig config) {
mSecurity = config.mSecurity;
mSsid = config.mSsid;
mPreSharedKey = config.mPreSharedKey;
mHiddenSsid = config.mHiddenSsid;
mNetworkId = config.mNetworkId;
mIsHotspot = config.mIsHotspot;
}
/**
* Wi-Fi DPP activities should implement this interface for fragments to retrieve the
* WifiNetworkConfig for configuration
*/
public interface Retriever {
WifiNetworkConfig getWifiNetworkConfig();
}
/**
* Retrieve WifiNetworkConfig from below 2 intents
*
* android.settings.WIFI_DPP_CONFIGURATOR_QR_CODE_GENERATOR
* android.settings.WIFI_DPP_CONFIGURATOR_QR_CODE_SCANNER
*/
static WifiNetworkConfig getValidConfigOrNull(Intent intent) {
final String security = intent.getStringExtra(WifiDppUtils.EXTRA_WIFI_SECURITY);
final String ssid = intent.getStringExtra(WifiDppUtils.EXTRA_WIFI_SSID);
final String preSharedKey = intent.getStringExtra(WifiDppUtils.EXTRA_WIFI_PRE_SHARED_KEY);
final boolean hiddenSsid = intent.getBooleanExtra(WifiDppUtils.EXTRA_WIFI_HIDDEN_SSID,
false);
final int networkId = intent.getIntExtra(WifiDppUtils.EXTRA_WIFI_NETWORK_ID,
WifiConfiguration.INVALID_NETWORK_ID);
final boolean isHotspot = intent.getBooleanExtra(WifiDppUtils.EXTRA_IS_HOTSPOT, false);
return getValidConfigOrNull(security, ssid, preSharedKey, hiddenSsid, networkId, isHotspot);
}
static WifiNetworkConfig getValidConfigOrNull(String security, String ssid,
String preSharedKey, boolean hiddenSsid, int networkId, boolean isHotspot) {
if (!isValidConfig(security, ssid, preSharedKey, hiddenSsid)) {
return null;
}
return new WifiNetworkConfig(security, ssid, preSharedKey, hiddenSsid, networkId,
isHotspot);
}
static boolean isValidConfig(WifiNetworkConfig config) {
if (config == null) {
return false;
} else {
return isValidConfig(config.mSecurity, config.mSsid, config.mPreSharedKey,
config.mHiddenSsid);
}
}
static boolean isValidConfig(String security, String ssid, String preSharedKey,
boolean hiddenSsid) {
if (!TextUtils.isEmpty(security) && !SECURITY_NO_PASSWORD.equals(security)) {
if (TextUtils.isEmpty(preSharedKey)) {
return false;
}
}
if (!hiddenSsid && TextUtils.isEmpty(ssid)) {
return false;
}
return true;
}
/**
* Escaped special characters "\", ";", ":", "," with a backslash
* See https://github.com/zxing/zxing/wiki/Barcode-Contents
*/
private String escapeSpecialCharacters(String str) {
if (TextUtils.isEmpty(str)) {
return str;
}
StringBuilder buf = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch =='\\' || ch == ',' || ch == ';' || ch == ':') {
buf.append('\\');
}
buf.append(ch);
}
return buf.toString();
}
/**
* Construct a barcode string for WiFi network login.
* See https://en.wikipedia.org/wiki/QR_code#WiFi_network_login
*/
String getQrCode() {
final String empty = "";
return new StringBuilder("WIFI:")
.append("S:")
.append(escapeSpecialCharacters(mSsid))
.append(";")
.append("T:")
.append(TextUtils.isEmpty(mSecurity) ? empty : mSecurity)
.append(";")
.append("P:")
.append(TextUtils.isEmpty(mPreSharedKey) ? empty
: escapeSpecialCharacters(mPreSharedKey))
.append(";")
.append("H:")
.append(mHiddenSsid)
.append(";;")
.toString();
}
public String getSecurity() {
return mSecurity;
}
public String getSsid() {
return mSsid;
}
public String getPreSharedKey() {
return mPreSharedKey;
}
public boolean getHiddenSsid() {
return mHiddenSsid;
}
public int getNetworkId() {
return mNetworkId;
}
public boolean isHotspot() {
return mIsHotspot;
}
public boolean isSupportWifiDpp(Context context) {
if (!WifiDppUtils.isWifiDppEnabled(context)) {
return false;
}
if (TextUtils.isEmpty(mSecurity)) {
return false;
}
// DPP 1.0 only supports SAE and PSK.
final WifiManager wifiManager = context.getSystemService(WifiManager.class);
switch (mSecurity) {
case SECURITY_SAE:
if (wifiManager.isWpa3SaeSupported()) {
return true;
}
break;
case SECURITY_WPA_PSK:
return true;
default:
}
return false;
}
/**
* This is a simplified method from {@code WifiConfigController.getConfig()}
*
* @return When it's a open network, returns 2 WifiConfiguration in the List, the 1st is
* open network and the 2nd is enhanced open network. Returns 1 WifiConfiguration in the
* List for all other supported Wi-Fi securities.
*/
List<WifiConfiguration> getWifiConfigurations() {
final List<WifiConfiguration> wifiConfigurations = new ArrayList<>();
if (!isValidConfig(this)) {
return wifiConfigurations;
}
if (TextUtils.isEmpty(mSecurity) || SECURITY_NO_PASSWORD.equals(mSecurity)) {
// TODO (b/129835824): we add both open network and enhanced open network to WifiManager
// for android Q, should improve it in the future.
final WifiConfiguration openNetworkWifiConfiguration = getBasicWifiConfiguration();
openNetworkWifiConfiguration.allowedKeyManagement.set(KeyMgmt.NONE);
wifiConfigurations.add(openNetworkWifiConfiguration);
final WifiConfiguration enhancedOpenNetworkWifiConfiguration =
getBasicWifiConfiguration();
enhancedOpenNetworkWifiConfiguration
.setSecurityParams(WifiConfiguration.SECURITY_TYPE_OWE);
wifiConfigurations.add(enhancedOpenNetworkWifiConfiguration);
return wifiConfigurations;
}
final WifiConfiguration wifiConfiguration = getBasicWifiConfiguration();
if (mSecurity.startsWith(SECURITY_WEP)) {
wifiConfiguration.setSecurityParams(WifiConfiguration.SECURITY_TYPE_WEP);
// WEP-40, WEP-104, and 256-bit WEP (WEP-232?)
final int length = mPreSharedKey.length();
if ((length == 10 || length == 26 || length == 58)
&& mPreSharedKey.matches("[0-9A-Fa-f]*")) {
wifiConfiguration.wepKeys[0] = mPreSharedKey;
} else {
wifiConfiguration.wepKeys[0] = addQuotationIfNeeded(mPreSharedKey);
}
} else if (mSecurity.startsWith(SECURITY_WPA_PSK)) {
wifiConfiguration.setSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK);
if (mPreSharedKey.matches("[0-9A-Fa-f]{64}")) {
wifiConfiguration.preSharedKey = mPreSharedKey;
} else {
wifiConfiguration.preSharedKey = addQuotationIfNeeded(mPreSharedKey);
}
} else if (mSecurity.startsWith(SECURITY_SAE)) {
wifiConfiguration.setSecurityParams(WifiConfiguration.SECURITY_TYPE_SAE);
if (mPreSharedKey.length() != 0) {
wifiConfiguration.preSharedKey = addQuotationIfNeeded(mPreSharedKey);
}
} else {
Log.w(TAG, "Unsupported security");
return wifiConfigurations;
}
wifiConfigurations.add(wifiConfiguration);
return wifiConfigurations;
}
private WifiConfiguration getBasicWifiConfiguration() {
final WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = addQuotationIfNeeded(mSsid);
wifiConfiguration.hiddenSSID = mHiddenSsid;
wifiConfiguration.networkId = mNetworkId;
return wifiConfiguration;
}
private String addQuotationIfNeeded(String input) {
if (TextUtils.isEmpty(input)) {
return "";
}
if (input.length() >= 2 && input.startsWith("\"") && input.endsWith("\"")) {
return input;
}
StringBuilder sb = new StringBuilder();
sb.append("\"").append(input).append("\"");
return sb.toString();
}
}
|