Skip to content

Commit 565f9f2

Browse files
committed
Update Wifi to use new keystore function
The old wpa_supplicant.conf format used a special URI prefix "keystore://" for the private_key value to indicate when to load things from keystore. The new format uses an OpenSSL ENGINE to perform operations with the private key, so we don't need the special URI prefix. This changes enables that usage and also supports migrating the old style configuration to the new style. Change-Id: Ibdf2322743eaa129bd2aa5e874f197b573714b57
1 parent 5423e68 commit 565f9f2

File tree

2 files changed

+94
-4
lines changed

2 files changed

+94
-4
lines changed

wifi/java/android/net/wifi/WifiConfigStore.java

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import android.net.NetworkInfo.DetailedState;
2626
import android.net.ProxyProperties;
2727
import android.net.RouteInfo;
28+
import android.net.wifi.WifiConfiguration.EnterpriseField;
2829
import android.net.wifi.WifiConfiguration.IpAssignment;
2930
import android.net.wifi.WifiConfiguration.KeyMgmt;
3031
import android.net.wifi.WifiConfiguration.ProxySettings;
@@ -1092,7 +1093,7 @@ private NetworkUpdateResult addOrUpdateNetworkNative(WifiConfiguration config) {
10921093
String varName = field.varName();
10931094
String value = field.value();
10941095
if (value != null) {
1095-
if (field != config.eap) {
1096+
if (field != config.eap && field != config.engine) {
10961097
value = (value.length() == 0) ? "NULL" : convertToQuotedString(value);
10971098
}
10981099
if (!mWifiNative.setNetworkVariable(
@@ -1399,10 +1400,68 @@ private void readNetworkVariables(WifiConfiguration config) {
13991400
value = mWifiNative.getNetworkVariable(netId,
14001401
field.varName());
14011402
if (!TextUtils.isEmpty(value)) {
1402-
if (field != config.eap) value = removeDoubleQuotes(value);
1403+
if (field != config.eap && field != config.engine) {
1404+
value = removeDoubleQuotes(value);
1405+
}
14031406
field.setValue(value);
14041407
}
14051408
}
1409+
1410+
migrateOldEapTlsIfNecessary(config, netId);
1411+
}
1412+
1413+
/**
1414+
* Migration code for old EAP-TLS configurations. This should only be used
1415+
* when restoring an old wpa_supplicant.conf or upgrading from a previous
1416+
* platform version.
1417+
*
1418+
* @param config the configuration to be migrated
1419+
* @param netId the wpa_supplicant's net ID
1420+
* @param value the old private_key value
1421+
*/
1422+
private void migrateOldEapTlsIfNecessary(WifiConfiguration config, int netId) {
1423+
String value = mWifiNative.getNetworkVariable(netId,
1424+
WifiConfiguration.OLD_PRIVATE_KEY_NAME);
1425+
/*
1426+
* If the old configuration value is not present, then there is nothing
1427+
* to do.
1428+
*/
1429+
if (TextUtils.isEmpty(value)) {
1430+
return;
1431+
} else {
1432+
// Also ignore it if it's empty quotes.
1433+
value = removeDoubleQuotes(value);
1434+
if (TextUtils.isEmpty(value)) {
1435+
return;
1436+
}
1437+
}
1438+
1439+
config.engine.setValue(WifiConfiguration.ENGINE_ENABLE);
1440+
config.engine_id.setValue(convertToQuotedString(WifiConfiguration.KEYSTORE_ENGINE_ID));
1441+
1442+
/*
1443+
* The old key started with the keystore:// URI prefix, but we don't
1444+
* need that anymore. Trim it off if it exists.
1445+
*/
1446+
final String keyName;
1447+
if (value.startsWith(WifiConfiguration.KEYSTORE_URI)) {
1448+
keyName = new String(value.substring(WifiConfiguration.KEYSTORE_URI.length()));
1449+
} else {
1450+
keyName = value;
1451+
}
1452+
config.key_id.setValue(convertToQuotedString(keyName));
1453+
1454+
// Now tell the wpa_supplicant the new configuration values.
1455+
final EnterpriseField needsUpdate[] = { config.engine, config.engine_id, config.key_id };
1456+
for (EnterpriseField field : needsUpdate) {
1457+
mWifiNative.setNetworkVariable(netId, field.varName(), field.value());
1458+
}
1459+
1460+
// Remove old private_key string so we don't run this again.
1461+
mWifiNative.setNetworkVariable(netId, WifiConfiguration.OLD_PRIVATE_KEY_NAME,
1462+
convertToQuotedString(""));
1463+
1464+
saveConfig();
14061465
}
14071466

14081467
private String removeDoubleQuotes(String string) {

wifi/java/android/net/wifi/WifiConfiguration.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,33 @@
2929
*/
3030
public class WifiConfiguration implements Parcelable {
3131

32+
/**
33+
* In old configurations, the "private_key" field was used. However, newer
34+
* configurations use the key_id field with the engine_id set to "keystore".
35+
* If this field is found in the configuration, the migration code is
36+
* triggered.
37+
* @hide
38+
*/
39+
public static final String OLD_PRIVATE_KEY_NAME = "private_key";
40+
41+
/**
42+
* String representing the keystore OpenSSL ENGINE's ID.
43+
* @hide
44+
*/
45+
public static final String KEYSTORE_ENGINE_ID = "keystore";
46+
47+
/**
48+
* String representing the keystore URI used for wpa_supplicant.
49+
* @hide
50+
*/
51+
public static final String KEYSTORE_URI = "keystore://";
52+
53+
/**
54+
* String to set the engine value to when it should be enabled.
55+
* @hide
56+
*/
57+
public static final String ENGINE_ENABLE = "1";
58+
3259
/** {@hide} */
3360
public static final String ssidVarName = "ssid";
3461
/** {@hide} */
@@ -82,14 +109,18 @@ public String value() {
82109
/** {@hide} */
83110
public EnterpriseField client_cert = new EnterpriseField("client_cert");
84111
/** {@hide} */
85-
public EnterpriseField private_key = new EnterpriseField("private_key");
112+
public EnterpriseField engine = new EnterpriseField("engine");
113+
/** {@hide} */
114+
public EnterpriseField engine_id = new EnterpriseField("engine_id");
115+
/** {@hide} */
116+
public EnterpriseField key_id = new EnterpriseField("key_id");
86117
/** {@hide} */
87118
public EnterpriseField ca_cert = new EnterpriseField("ca_cert");
88119

89120
/** {@hide} */
90121
public EnterpriseField[] enterpriseFields = {
91122
eap, phase2, identity, anonymous_identity, password, client_cert,
92-
private_key, ca_cert };
123+
engine, engine_id, key_id, ca_cert };
93124

94125
/**
95126
* Recognized key management schemes.

0 commit comments

Comments
 (0)