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
|
/*
* 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 android.net.wifi.WifiConfiguration;
import android.text.TextUtils;
import androidx.annotation.VisibleForTesting;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
/**
* Supports to parse 2 types of QR code
*
* 1. Standard Wi-Fi DPP bootstrapping information or
* 2. ZXing reader library's Wi-Fi Network config format described in
* https://github.com/zxing/zxing/wiki/Barcode-Contents#wi-fi-network-config-android-ios-11
*
* ZXing reader library's Wi-Fi Network config format example:
*
* WIFI:T:WPA;S:mynetwork;P:mypass;;
*
* parameter Example Description
* T WPA Authentication type; can be WEP or WPA, or 'nopass' for no password. Or,
* omit for no password.
* S mynetwork Network SSID. Required. Enclose in double quotes if it is an ASCII name,
* but could be interpreted as hex (i.e. "ABCD")
* P mypass Password, ignored if T is "nopass" (in which case it may be omitted).
* Enclose in double quotes if it is an ASCII name, but could be interpreted as
* hex (i.e. "ABCD")
* H true Optional. True if the network SSID is hidden.
*
*/
public class WifiQrCode {
static final String SCHEME_DPP = "DPP";
static final String SCHEME_ZXING_WIFI_NETWORK_CONFIG = "WIFI";
static final String PREFIX_DPP = "DPP:";
static final String PREFIX_ZXING_WIFI_NETWORK_CONFIG = "WIFI:";
static final String PREFIX_DPP_PUBLIC_KEY = "K:";
static final String PREFIX_DPP_INFORMATION = "I:";
static final String PREFIX_ZXING_SECURITY = "T:";
static final String PREFIX_ZXING_SSID = "S:";
static final String PREFIX_ZXING_PASSWORD = "P:";
static final String PREFIX_ZXING_HIDDEN_SSID = "H:";
static final String DELIMITER_QR_CODE = ";";
// Ignores password if security is SECURITY_NO_PASSWORD or absent
static final String SECURITY_NO_PASSWORD = "nopass"; //open network or OWE
static final String SECURITY_WEP = "WEP";
static final String SECURITY_WPA_PSK = "WPA";
static final String SECURITY_SAE = "SAE";
private String mQrCode;
/**
* SCHEME_DPP for standard Wi-Fi device provision protocol; SCHEME_ZXING_WIFI_NETWORK_CONFIG
* for ZXing reader library' Wi-Fi Network config format
*/
private String mScheme;
// Data from parsed Wi-Fi DPP QR code
private String mPublicKey;
private String mInformation;
// Data from parsed ZXing reader library's Wi-Fi Network config format
private WifiNetworkConfig mWifiNetworkConfig;
public WifiQrCode(String qrCode) throws IllegalArgumentException {
if (TextUtils.isEmpty(qrCode)) {
throw new IllegalArgumentException("Empty QR code");
}
mQrCode = qrCode;
if (qrCode.startsWith(PREFIX_DPP)) {
mScheme = SCHEME_DPP;
parseWifiDppQrCode(qrCode);
} else if (qrCode.startsWith(PREFIX_ZXING_WIFI_NETWORK_CONFIG)) {
mScheme = SCHEME_ZXING_WIFI_NETWORK_CONFIG;
parseZxingWifiQrCode(qrCode);
} else {
throw new IllegalArgumentException("Invalid scheme");
}
}
/** Parses Wi-Fi DPP QR code string */
private void parseWifiDppQrCode(String qrCode) throws IllegalArgumentException {
List<String> keyValueList = getKeyValueList(qrCode, PREFIX_DPP, DELIMITER_QR_CODE);
String publicKey = getValueOrNull(keyValueList, PREFIX_DPP_PUBLIC_KEY);
if (TextUtils.isEmpty(publicKey)) {
throw new IllegalArgumentException("Invalid format");
}
mPublicKey = publicKey;
mInformation = getValueOrNull(keyValueList, PREFIX_DPP_INFORMATION);
}
/** Parses ZXing reader library's Wi-Fi Network config format */
private void parseZxingWifiQrCode(String qrCode) throws IllegalArgumentException {
List<String> keyValueList = getKeyValueList(qrCode, PREFIX_ZXING_WIFI_NETWORK_CONFIG,
DELIMITER_QR_CODE);
String security = getValueOrNull(keyValueList, PREFIX_ZXING_SECURITY);
String ssid = getValueOrNull(keyValueList, PREFIX_ZXING_SSID);
String password = getValueOrNull(keyValueList, PREFIX_ZXING_PASSWORD);
String hiddenSsidString = getValueOrNull(keyValueList, PREFIX_ZXING_HIDDEN_SSID);
boolean hiddenSsid = "true".equalsIgnoreCase(hiddenSsidString);
//"\", ";", "," and ":" are escaped with a backslash "\", should remove at first
security = removeBackSlash(security);
ssid = removeBackSlash(ssid);
password = removeBackSlash(password);
mWifiNetworkConfig = WifiNetworkConfig.getValidConfigOrNull(security, ssid, password,
hiddenSsid, WifiConfiguration.INVALID_NETWORK_ID, /* isHotspot */ false);
if (mWifiNetworkConfig == null) {
throw new IllegalArgumentException("Invalid format");
}
}
/**
* Splits key/value pairs from qrCode
*
* @param qrCode the QR code raw string
* @param prefixQrCode the string before all key/value pairs in qrCode
* @param delimiter the string to split key/value pairs, can't contain a backslash
* @return a list contains string of key/value (e.g. K:key1)
*/
private List<String> getKeyValueList(String qrCode, String prefixQrCode,
String delimiter) {
String keyValueString = qrCode.substring(prefixQrCode.length());
// Should not treat \delimiter as a delimiter
String regex = "(?<!\\\\)" + Pattern.quote(delimiter);
return Arrays.asList(keyValueString.split(regex));
}
private String getValueOrNull(List<String> keyValueList, String prefix) {
for (String keyValue : keyValueList) {
if (keyValue.startsWith(prefix)) {
return keyValue.substring(prefix.length());
}
}
return null;
}
@VisibleForTesting
String removeBackSlash(String input) {
if (input == null) {
return null;
}
StringBuilder sb = new StringBuilder();
boolean backSlash = false;
for (char ch : input.toCharArray()) {
if (ch != '\\') {
sb.append(ch);
backSlash = false;
} else {
if (backSlash) {
sb.append(ch);
backSlash = false;
continue;
}
backSlash = true;
}
}
return sb.toString();
}
String getQrCode() {
return mQrCode;
}
/**
* Uses to check type of QR code
*
* SCHEME_DPP for standard Wi-Fi device provision protocol; SCHEME_ZXING_WIFI_NETWORK_CONFIG
* for ZXing reader library' Wi-Fi Network config format
*/
public String getScheme() {
return mScheme;
}
/** Available when {@code getScheme()} returns SCHEME_DPP */
@VisibleForTesting
String getPublicKey() {
return mPublicKey;
}
/** May be available when {@code getScheme()} returns SCHEME_DPP */
public String getInformation() {
return mInformation;
}
/** Available when {@code getScheme()} returns SCHEME_ZXING_WIFI_NETWORK_CONFIG */
WifiNetworkConfig getWifiNetworkConfig() {
if (mWifiNetworkConfig == null) {
return null;
}
return new WifiNetworkConfig(mWifiNetworkConfig);
}
static WifiQrCode getValidWifiDppQrCodeOrNull(String qrCode) {
WifiQrCode wifiQrCode;
try {
wifiQrCode = new WifiQrCode(qrCode);
} catch(IllegalArgumentException e) {
return null;
}
if (SCHEME_DPP.equals(wifiQrCode.getScheme())) {
return wifiQrCode;
}
return null;
}
}
|