Merge "Refactor enterprise config"

This commit is contained in:
Irfan Sheriff
2013-01-15 09:02:46 -08:00
committed by Android (Google) Code Review
4 changed files with 493 additions and 192 deletions

View File

@@ -24,7 +24,6 @@ import android.net.NetworkUtils;
import android.net.NetworkInfo.DetailedState; import android.net.NetworkInfo.DetailedState;
import android.net.ProxyProperties; import android.net.ProxyProperties;
import android.net.RouteInfo; import android.net.RouteInfo;
import android.net.wifi.WifiConfiguration.EnterpriseField;
import android.net.wifi.WifiConfiguration.IpAssignment; import android.net.wifi.WifiConfiguration.IpAssignment;
import android.net.wifi.WifiConfiguration.KeyMgmt; import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiConfiguration.ProxySettings; import android.net.wifi.WifiConfiguration.ProxySettings;
@@ -1090,31 +1089,17 @@ class WifiConfigStore {
break setVariables; break setVariables;
} }
for (WifiConfiguration.EnterpriseField field HashMap<String, String> enterpriseFields = config.enterpriseConfig.getFields();
: config.enterpriseFields) { for (String key : enterpriseFields.keySet()) {
String varName = field.varName(); String value = enterpriseFields.get(key);
String value = field.value();
if (value != null) {
if (field == config.engine) {
/*
* If the field is declared as an integer, it must not
* be null
*/
if (value.length() == 0) {
value = "0";
}
} else if (field != config.eap) {
value = (value.length() == 0) ? "NULL" : convertToQuotedString(value);
}
if (!mWifiNative.setNetworkVariable( if (!mWifiNative.setNetworkVariable(
netId, netId,
varName, key,
value)) { value)) {
loge(config.SSID + ": failed to set " + varName + loge(config.SSID + ": failed to set " + key +
": " + value); ": " + value);
break setVariables; break setVariables;
} }
}
} }
updateFailed = false; updateFailed = false;
} }
@@ -1413,78 +1398,26 @@ class WifiConfigStore {
} }
} }
for (WifiConfiguration.EnterpriseField field : HashMap<String, String> entepriseFields = config.enterpriseConfig.getFields();
config.enterpriseFields) { for (String key : entepriseFields.keySet()) {
value = mWifiNative.getNetworkVariable(netId, value = mWifiNative.getNetworkVariable(netId, key);
field.varName());
if (!TextUtils.isEmpty(value)) { if (!TextUtils.isEmpty(value)) {
if (field != config.eap && field != config.engine) { entepriseFields.put(key, removeDoubleQuotes(value));
value = removeDoubleQuotes(value);
}
field.setValue(value);
} }
} }
migrateOldEapTlsIfNecessary(config, netId); if (config.enterpriseConfig.migrateOldEapTlsNative(mWifiNative, netId)) {
} saveConfig();
/**
* Migration code for old EAP-TLS configurations. This should only be used
* when restoring an old wpa_supplicant.conf or upgrading from a previous
* platform version.
*
* @param config the configuration to be migrated
* @param netId the wpa_supplicant's net ID
* @param value the old private_key value
*/
private void migrateOldEapTlsIfNecessary(WifiConfiguration config, int netId) {
String value = mWifiNative.getNetworkVariable(netId,
WifiConfiguration.OLD_PRIVATE_KEY_NAME);
/*
* If the old configuration value is not present, then there is nothing
* to do.
*/
if (TextUtils.isEmpty(value)) {
return;
} else {
// Also ignore it if it's empty quotes.
value = removeDoubleQuotes(value);
if (TextUtils.isEmpty(value)) {
return;
}
} }
config.engine.setValue(WifiConfiguration.ENGINE_ENABLE);
config.engine_id.setValue(convertToQuotedString(WifiConfiguration.KEYSTORE_ENGINE_ID));
/*
* The old key started with the keystore:// URI prefix, but we don't
* need that anymore. Trim it off if it exists.
*/
final String keyName;
if (value.startsWith(WifiConfiguration.KEYSTORE_URI)) {
keyName = new String(value.substring(WifiConfiguration.KEYSTORE_URI.length()));
} else {
keyName = value;
}
config.key_id.setValue(convertToQuotedString(keyName));
// Now tell the wpa_supplicant the new configuration values.
final EnterpriseField needsUpdate[] = { config.engine, config.engine_id, config.key_id };
for (EnterpriseField field : needsUpdate) {
mWifiNative.setNetworkVariable(netId, field.varName(), field.value());
}
// Remove old private_key string so we don't run this again.
mWifiNative.setNetworkVariable(netId, WifiConfiguration.OLD_PRIVATE_KEY_NAME,
convertToQuotedString(""));
saveConfig();
} }
private String removeDoubleQuotes(String string) { private String removeDoubleQuotes(String string) {
if (string.length() <= 2) return ""; int length = string.length();
return string.substring(1, string.length() - 1); if ((length > 1) && (string.charAt(0) == '"')
&& (string.charAt(length - 1) == '"')) {
return string.substring(1, length - 1);
}
return string;
} }
private String convertToQuotedString(String string) { private String convertToQuotedString(String string) {

View File

@@ -24,44 +24,10 @@ import java.util.BitSet;
/** /**
* A class representing a configured Wi-Fi network, including the * A class representing a configured Wi-Fi network, including the
* security configuration. Android will not necessarily support * security configuration.
* all of these security schemes initially.
*/ */
public class WifiConfiguration implements Parcelable { public class WifiConfiguration implements Parcelable {
private static final String TAG = "WifiConfiguration";
/**
* In old configurations, the "private_key" field was used. However, newer
* configurations use the key_id field with the engine_id set to "keystore".
* If this field is found in the configuration, the migration code is
* triggered.
* @hide
*/
public static final String OLD_PRIVATE_KEY_NAME = "private_key";
/**
* String representing the keystore OpenSSL ENGINE's ID.
* @hide
*/
public static final String KEYSTORE_ENGINE_ID = "keystore";
/**
* String representing the keystore URI used for wpa_supplicant.
* @hide
*/
public static final String KEYSTORE_URI = "keystore://";
/**
* String to set the engine value to when it should be enabled.
* @hide
*/
public static final String ENGINE_ENABLE = "1";
/**
* String to set the engine value to when it should be disabled.
* @hide
*/
public static final String ENGINE_DISABLE = "0";
/** {@hide} */ /** {@hide} */
public static final String ssidVarName = "ssid"; public static final String ssidVarName = "ssid";
/** {@hide} */ /** {@hide} */
@@ -78,56 +44,6 @@ public class WifiConfiguration implements Parcelable {
public static final String hiddenSSIDVarName = "scan_ssid"; public static final String hiddenSSIDVarName = "scan_ssid";
/** {@hide} */ /** {@hide} */
public static final int INVALID_NETWORK_ID = -1; public static final int INVALID_NETWORK_ID = -1;
/** {@hide} */
public class EnterpriseField {
private String varName;
private String value;
private EnterpriseField(String varName) {
this.varName = varName;
this.value = null;
}
public void setValue(String value) {
this.value = value;
}
public String varName() {
return varName;
}
public String value() {
return value;
}
}
/** {@hide} */
public EnterpriseField eap = new EnterpriseField("eap");
/** {@hide} */
public EnterpriseField phase2 = new EnterpriseField("phase2");
/** {@hide} */
public EnterpriseField identity = new EnterpriseField("identity");
/** {@hide} */
public EnterpriseField anonymous_identity = new EnterpriseField("anonymous_identity");
/** {@hide} */
public EnterpriseField password = new EnterpriseField("password");
/** {@hide} */
public EnterpriseField client_cert = new EnterpriseField("client_cert");
/** {@hide} */
public EnterpriseField engine = new EnterpriseField("engine");
/** {@hide} */
public EnterpriseField engine_id = new EnterpriseField("engine_id");
/** {@hide} */
public EnterpriseField key_id = new EnterpriseField("key_id");
/** {@hide} */
public EnterpriseField ca_cert = new EnterpriseField("ca_cert");
/** {@hide} */
public EnterpriseField[] enterpriseFields = {
eap, phase2, identity, anonymous_identity, password, client_cert,
engine, engine_id, key_id, ca_cert };
/** /**
* Recognized key management schemes. * Recognized key management schemes.
*/ */
@@ -357,6 +273,11 @@ public class WifiConfiguration implements Parcelable {
* Defaults to CCMP TKIP WEP104 WEP40. * Defaults to CCMP TKIP WEP104 WEP40.
*/ */
public BitSet allowedGroupCiphers; public BitSet allowedGroupCiphers;
/**
* The enterprise configuration details
* @hide
*/
public WifiEnterpriseConfig enterpriseConfig;
/** /**
* @hide * @hide
@@ -412,11 +333,10 @@ public class WifiConfiguration implements Parcelable {
allowedPairwiseCiphers = new BitSet(); allowedPairwiseCiphers = new BitSet();
allowedGroupCiphers = new BitSet(); allowedGroupCiphers = new BitSet();
wepKeys = new String[4]; wepKeys = new String[4];
for (int i = 0; i < wepKeys.length; i++) for (int i = 0; i < wepKeys.length; i++) {
wepKeys[i] = null; wepKeys[i] = null;
for (EnterpriseField field : enterpriseFields) {
field.setValue(null);
} }
enterpriseConfig = new WifiEnterpriseConfig();
ipAssignment = IpAssignment.UNASSIGNED; ipAssignment = IpAssignment.UNASSIGNED;
proxySettings = ProxySettings.UNASSIGNED; proxySettings = ProxySettings.UNASSIGNED;
linkProperties = new LinkProperties(); linkProperties = new LinkProperties();
@@ -496,12 +416,9 @@ public class WifiConfiguration implements Parcelable {
sbuf.append('*'); sbuf.append('*');
} }
for (EnterpriseField field : enterpriseFields) { sbuf.append(enterpriseConfig);
sbuf.append('\n').append(" " + field.varName() + ": ");
String value = field.value();
if (value != null) sbuf.append(value);
}
sbuf.append('\n'); sbuf.append('\n');
sbuf.append("IP assignment: " + ipAssignment.toString()); sbuf.append("IP assignment: " + ipAssignment.toString());
sbuf.append("\n"); sbuf.append("\n");
sbuf.append("Proxy settings: " + proxySettings.toString()); sbuf.append("Proxy settings: " + proxySettings.toString());
@@ -549,8 +466,9 @@ public class WifiConfiguration implements Parcelable {
int cardinality = src.readInt(); int cardinality = src.readInt();
BitSet set = new BitSet(); BitSet set = new BitSet();
for (int i = 0; i < cardinality; i++) for (int i = 0; i < cardinality; i++) {
set.set(src.readInt()); set.set(src.readInt());
}
return set; return set;
} }
@@ -560,8 +478,9 @@ public class WifiConfiguration implements Parcelable {
dest.writeInt(set.cardinality()); dest.writeInt(set.cardinality());
while ((nextSetBit = set.nextSetBit(nextSetBit + 1)) != -1) while ((nextSetBit = set.nextSetBit(nextSetBit + 1)) != -1) {
dest.writeInt(nextSetBit); dest.writeInt(nextSetBit);
}
} }
/** @hide */ /** @hide */
@@ -594,8 +513,9 @@ public class WifiConfiguration implements Parcelable {
preSharedKey = source.preSharedKey; preSharedKey = source.preSharedKey;
wepKeys = new String[4]; wepKeys = new String[4];
for (int i = 0; i < wepKeys.length; i++) for (int i = 0; i < wepKeys.length; i++) {
wepKeys[i] = source.wepKeys[i]; wepKeys[i] = source.wepKeys[i];
}
wepTxKeyIndex = source.wepTxKeyIndex; wepTxKeyIndex = source.wepTxKeyIndex;
priority = source.priority; priority = source.priority;
@@ -606,9 +526,8 @@ public class WifiConfiguration implements Parcelable {
allowedPairwiseCiphers = (BitSet) source.allowedPairwiseCiphers.clone(); allowedPairwiseCiphers = (BitSet) source.allowedPairwiseCiphers.clone();
allowedGroupCiphers = (BitSet) source.allowedGroupCiphers.clone(); allowedGroupCiphers = (BitSet) source.allowedGroupCiphers.clone();
for (int i = 0; i < source.enterpriseFields.length; i++) { enterpriseConfig = new WifiEnterpriseConfig(source.enterpriseConfig);
enterpriseFields[i].setValue(source.enterpriseFields[i].value());
}
ipAssignment = source.ipAssignment; ipAssignment = source.ipAssignment;
proxySettings = source.proxySettings; proxySettings = source.proxySettings;
linkProperties = new LinkProperties(source.linkProperties); linkProperties = new LinkProperties(source.linkProperties);
@@ -623,8 +542,9 @@ public class WifiConfiguration implements Parcelable {
dest.writeString(SSID); dest.writeString(SSID);
dest.writeString(BSSID); dest.writeString(BSSID);
dest.writeString(preSharedKey); dest.writeString(preSharedKey);
for (String wepKey : wepKeys) for (String wepKey : wepKeys) {
dest.writeString(wepKey); dest.writeString(wepKey);
}
dest.writeInt(wepTxKeyIndex); dest.writeInt(wepTxKeyIndex);
dest.writeInt(priority); dest.writeInt(priority);
dest.writeInt(hiddenSSID ? 1 : 0); dest.writeInt(hiddenSSID ? 1 : 0);
@@ -635,9 +555,8 @@ public class WifiConfiguration implements Parcelable {
writeBitSet(dest, allowedPairwiseCiphers); writeBitSet(dest, allowedPairwiseCiphers);
writeBitSet(dest, allowedGroupCiphers); writeBitSet(dest, allowedGroupCiphers);
for (EnterpriseField field : enterpriseFields) { dest.writeParcelable(enterpriseConfig, flags);
dest.writeString(field.value());
}
dest.writeString(ipAssignment.name()); dest.writeString(ipAssignment.name());
dest.writeString(proxySettings.name()); dest.writeString(proxySettings.name());
dest.writeParcelable(linkProperties, flags); dest.writeParcelable(linkProperties, flags);
@@ -654,8 +573,9 @@ public class WifiConfiguration implements Parcelable {
config.SSID = in.readString(); config.SSID = in.readString();
config.BSSID = in.readString(); config.BSSID = in.readString();
config.preSharedKey = in.readString(); config.preSharedKey = in.readString();
for (int i = 0; i < config.wepKeys.length; i++) for (int i = 0; i < config.wepKeys.length; i++) {
config.wepKeys[i] = in.readString(); config.wepKeys[i] = in.readString();
}
config.wepTxKeyIndex = in.readInt(); config.wepTxKeyIndex = in.readInt();
config.priority = in.readInt(); config.priority = in.readInt();
config.hiddenSSID = in.readInt() != 0; config.hiddenSSID = in.readInt() != 0;
@@ -665,13 +585,12 @@ public class WifiConfiguration implements Parcelable {
config.allowedPairwiseCiphers = readBitSet(in); config.allowedPairwiseCiphers = readBitSet(in);
config.allowedGroupCiphers = readBitSet(in); config.allowedGroupCiphers = readBitSet(in);
for (EnterpriseField field : config.enterpriseFields) { config.enterpriseConfig = in.readParcelable(null);
field.setValue(in.readString());
}
config.ipAssignment = IpAssignment.valueOf(in.readString()); config.ipAssignment = IpAssignment.valueOf(in.readString());
config.proxySettings = ProxySettings.valueOf(in.readString()); config.proxySettings = ProxySettings.valueOf(in.readString());
config.linkProperties = in.readParcelable(null); config.linkProperties = in.readParcelable(null);
return config; return config;
} }

View File

@@ -0,0 +1,19 @@
/**
* Copyright (c) 2013, 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 android.net.wifi;
parcelable WifiEnterpriseConfig;

View File

@@ -0,0 +1,430 @@
/*
* Copyright (C) 2013 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 android.net.wifi;
import android.os.Parcel;
import android.os.Parcelable;
import android.security.Credentials;
import android.text.TextUtils;
import java.util.HashMap;
import java.util.Map;
/** Enterprise configuration details for Wi-Fi @hide */
public class WifiEnterpriseConfig implements Parcelable {
private static final String TAG = "WifiEnterpriseConfig";
/**
* In old configurations, the "private_key" field was used. However, newer
* configurations use the key_id field with the engine_id set to "keystore".
* If this field is found in the configuration, the migration code is
* triggered.
*/
private static final String OLD_PRIVATE_KEY_NAME = "private_key";
/**
* String representing the keystore OpenSSL ENGINE's ID.
*/
private static final String ENGINE_ID_KEYSTORE = "keystore";
/**
* String representing the keystore URI used for wpa_supplicant.
*/
private static final String KEYSTORE_URI = "keystore://";
/**
* String to set the engine value to when it should be enabled.
*/
private static final String ENGINE_ENABLE = "1";
/**
* String to set the engine value to when it should be disabled.
*/
private static final String ENGINE_DISABLE = "0";
private static final String CA_CERT_PREFIX = KEYSTORE_URI + Credentials.CA_CERTIFICATE;
private static final String CLIENT_CERT_PREFIX = KEYSTORE_URI + Credentials.USER_CERTIFICATE;
private static final String EAP_KEY = "eap";
private static final String PHASE2_KEY = "phase2";
private static final String IDENTITY_KEY = "identity";
private static final String ANON_IDENTITY_KEY = "anonymous_identity";
private static final String PASSWORD_KEY = "password";
private static final String CLIENT_CERT_KEY = "client_cert";
private static final String CA_CERT_KEY = "ca_cert";
private static final String SUBJECT_MATCH_KEY = "subject_match";
private static final String ENGINE_KEY = "engine";
private static final String ENGINE_ID_KEY = "engine_id";
private static final String PRIVATE_KEY_ID_KEY = "key_id";
private HashMap<String, String> mFields = new HashMap<String, String>();
/** This represents an empty value of an enterprise field.
* NULL is used at wpa_supplicant to indicate an empty value
*/
private static final String EMPTY_VALUE = "NULL";
public WifiEnterpriseConfig() {
// Set the required defaults
mFields.put(EAP_KEY, Eap.strings[Eap.PEAP]);
mFields.put(ENGINE_KEY, ENGINE_DISABLE);
for (String key : new String[] {PHASE2_KEY, IDENTITY_KEY, ANON_IDENTITY_KEY,
PASSWORD_KEY, CLIENT_CERT_KEY, ENGINE_ID_KEY, PRIVATE_KEY_ID_KEY,
CA_CERT_KEY, SUBJECT_MATCH_KEY}) {
mFields.put(key, EMPTY_VALUE);
}
}
/** Copy constructor */
public WifiEnterpriseConfig(WifiEnterpriseConfig source) {
for (String key : source.mFields.keySet()) {
mFields.put(key, source.mFields.get(key));
}
}
@Override
public int describeContents() {
return 0;
}
/** @Override */
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mFields.size());
for (Map.Entry<String, String> entry : mFields.entrySet()) {
dest.writeString(entry.getKey());
dest.writeString(entry.getValue());
}
}
/** @Override */
public static final Creator<WifiEnterpriseConfig> CREATOR =
new Creator<WifiEnterpriseConfig>() {
public WifiEnterpriseConfig createFromParcel(Parcel in) {
WifiEnterpriseConfig enterpriseConfig = new WifiEnterpriseConfig();
int count = in.readInt();
for (int i = 0; i < count; i++) {
String key = in.readString();
String value = in.readString();
enterpriseConfig.mFields.put(key, value);
}
return enterpriseConfig;
}
public WifiEnterpriseConfig[] newArray(int size) {
return new WifiEnterpriseConfig[size];
}
};
public static final class Eap {
public static final int PEAP = 0;
public static final int TLS = 1;
public static final int TTLS = 2;
public static final int PWD = 3;
/** @hide */
public static final String[] strings = { "PEAP", "TLS", "TTLS", "PWD" };
}
public static final class Phase2 {
public static final int NONE = 0;
public static final int PAP = 1;
public static final int MSCHAP = 2;
public static final int MSCHAPV2 = 3;
public static final int GTC = 4;
private static final String PREFIX = "auth=";
/** @hide */
public static final String[] strings = {EMPTY_VALUE, "PAP", "MSCHAP", "MSCHAPV2", "GTC" };
}
/** Internal use only @hide */
public HashMap<String, String> getFields() {
return mFields;
}
/**
* Set the EAP authentication method.
* @param eapMethod is one {@link Eap#PEAP}, {@link Eap#TLS}, {@link Eap#TTLS} or
* {@link Eap#PWD}
*/
public void setEapMethod(int eapMethod) {
switch (eapMethod) {
/** Valid methods */
case Eap.PEAP:
case Eap.PWD:
case Eap.TLS:
case Eap.TTLS:
mFields.put(EAP_KEY, Eap.strings[eapMethod]);
break;
default:
throw new IllegalArgumentException("Unknown EAP method");
}
}
/**
* Get the eap method.
* @return eap method configured
*/
public int getEapMethod() {
String eapMethod = mFields.get(EAP_KEY);
return getStringIndex(Eap.strings, eapMethod, Eap.PEAP);
}
/**
* Set Phase 2 authentication method. Sets the inner authentication method to be used in
* phase 2 after setting up a secure channel
* @param phase2Method is the inner authentication method and can be one of {@link Phase2#NONE},
* {@link Phase2#PAP}, {@link Phase2#MSCHAP}, {@link Phase2#MSCHAPV2},
* {@link Phase2#GTC}
*
*/
public void setPhase2Method(int phase2Method) {
switch (phase2Method) {
case Phase2.NONE:
mFields.put(PHASE2_KEY, EMPTY_VALUE);
break;
/** Valid methods */
case Phase2.PAP:
case Phase2.MSCHAP:
case Phase2.MSCHAPV2:
case Phase2.GTC:
mFields.put(PHASE2_KEY, convertToQuotedString(
Phase2.PREFIX + Phase2.strings[phase2Method]));
break;
default:
throw new IllegalArgumentException("Unknown Phase 2 method");
}
}
/**
* Get the phase 2 authentication method.
* @return a phase 2 method defined at {@link Phase2}
* */
public int getPhase2Method() {
String phase2Method = mFields.get(PHASE2_KEY);
return getStringIndex(Phase2.strings, phase2Method, Phase2.NONE);
}
/**
* Set the identity
* @param identity
*/
public void setIdentity(String identity) {
setFieldValue(IDENTITY_KEY, identity, "");
}
/**
* Get the identity
* @return the identity
*/
public String getIdentity() {
return getFieldValue(IDENTITY_KEY, "");
}
/**
* Set anonymous identity. This is used as the unencrypted identity with
* certain EAP types
* @param anonymousIdentity the anonymous identity
*/
public void setAnonymousIdentity(String anonymousIdentity) {
setFieldValue(ANON_IDENTITY_KEY, anonymousIdentity, "");
}
/** Get the anonymous identity
* @return anonymous identity
*/
public String getAnonymousIdentity() {
return getFieldValue(ANON_IDENTITY_KEY, "");
}
/**
* Set the password.
* @param password the password
*/
public void setPassword(String password) {
setFieldValue(PASSWORD_KEY, password, "");
}
/**
* Set CA certificate alias.
*
* <p> See the {@link android.security.KeyChain} for details on installing or choosing
* a certificate
* </p>
* @param alias identifies the certificate
*/
public void setCaCertificate(String alias) {
setFieldValue(CA_CERT_KEY, alias, CA_CERT_PREFIX);
}
/**
* Get CA certificate alias
* @return alias to the CA certificate
*/
public String getCaCertificate() {
return getFieldValue(CA_CERT_KEY, CA_CERT_PREFIX);
}
/**
* Set Client certificate alias.
*
* <p> See the {@link android.security.KeyChain} for details on installing or choosing
* a certificate
* </p>
* @param alias identifies the certificate
*/
public void setClientCertificate(String alias) {
setFieldValue(CLIENT_CERT_KEY, alias, CLIENT_CERT_PREFIX);
setFieldValue(PRIVATE_KEY_ID_KEY, alias, Credentials.USER_PRIVATE_KEY);
// Also, set engine parameters
if (TextUtils.isEmpty(alias)) {
mFields.put(ENGINE_KEY, ENGINE_DISABLE);
mFields.put(ENGINE_ID_KEY, EMPTY_VALUE);
} else {
mFields.put(ENGINE_KEY, ENGINE_ENABLE);
mFields.put(ENGINE_ID_KEY, convertToQuotedString(ENGINE_ID_KEYSTORE));
}
}
/**
* Get client certificate alias
* @return alias to the client certificate
*/
public String getClientCertificate() {
return getFieldValue(CLIENT_CERT_KEY, CLIENT_CERT_PREFIX);
}
/**
* Set subject match. This is the substring to be matched against the subject of the
* authentication server certificate.
* @param subjectMatch substring to be matched
*/
public void setSubjectMatch(String subjectMatch) {
setFieldValue(SUBJECT_MATCH_KEY, subjectMatch, "");
}
/**
* Get subject match
* @return the subject match string
*/
public String getSubjectMatch() {
return getFieldValue(SUBJECT_MATCH_KEY, "");
}
/** Migrates the old style TLS config to the new config style. This should only be used
* when restoring an old wpa_supplicant.conf or upgrading from a previous
* platform version.
* @return true if the config was updated
* @hide
*/
public boolean migrateOldEapTlsNative(WifiNative wifiNative, int netId) {
String oldPrivateKey = wifiNative.getNetworkVariable(netId, OLD_PRIVATE_KEY_NAME);
/*
* If the old configuration value is not present, then there is nothing
* to do.
*/
if (TextUtils.isEmpty(oldPrivateKey)) {
return false;
} else {
// Also ignore it if it's empty quotes.
oldPrivateKey = removeDoubleQuotes(oldPrivateKey);
if (TextUtils.isEmpty(oldPrivateKey)) {
return false;
}
}
mFields.put(ENGINE_KEY, ENGINE_ENABLE);
mFields.put(ENGINE_ID_KEY, convertToQuotedString(ENGINE_ID_KEYSTORE));
/*
* The old key started with the keystore:// URI prefix, but we don't
* need that anymore. Trim it off if it exists.
*/
final String keyName;
if (oldPrivateKey.startsWith(KEYSTORE_URI)) {
keyName = new String(oldPrivateKey.substring(KEYSTORE_URI.length()));
} else {
keyName = oldPrivateKey;
}
mFields.put(PRIVATE_KEY_ID_KEY, convertToQuotedString(keyName));
wifiNative.setNetworkVariable(netId, ENGINE_KEY, mFields.get(ENGINE_KEY));
wifiNative.setNetworkVariable(netId, ENGINE_ID_KEY, mFields.get(ENGINE_ID_KEY));
wifiNative.setNetworkVariable(netId, PRIVATE_KEY_ID_KEY, mFields.get(PRIVATE_KEY_ID_KEY));
// Remove old private_key string so we don't run this again.
wifiNative.setNetworkVariable(netId, OLD_PRIVATE_KEY_NAME, EMPTY_VALUE);
return true;
}
private String removeDoubleQuotes(String string) {
int length = string.length();
if ((length > 1) && (string.charAt(0) == '"')
&& (string.charAt(length - 1) == '"')) {
return string.substring(1, length - 1);
}
return string;
}
private String convertToQuotedString(String string) {
return "\"" + string + "\"";
}
/** Returns the index at which the toBeFound string is found in the array.
* @param arr array of strings
* @param toBeFound string to be found
* @param defaultIndex default index to be returned when string is not found
* @return the index into array
*/
private int getStringIndex(String arr[], String toBeFound, int defaultIndex) {
for (int i = 0; i < arr.length; i++) {
// toBeFound can be formatted with a prefix. For example, phase2
// string has "auth=" as the prefix.
if (toBeFound.contains(arr[i])) return i;
}
return defaultIndex;
}
/** Returns the field value for the key.
* @param key into the hash
* @param prefix is the prefix that the value may have
* @return value
*/
private String getFieldValue(String key, String prefix) {
String value = mFields.get(key);
if (EMPTY_VALUE.equals(value)) return "";
return removeDoubleQuotes(value).substring(prefix.length());
}
/** Set a value with an optional prefix at key
* @param key into the hash
* @param value to be set
* @param prefix an optional value to be prefixed to actual value
*/
private void setFieldValue(String key, String value, String prefix) {
if (TextUtils.isEmpty(value)) {
mFields.put(key, EMPTY_VALUE);
} else {
mFields.put(key, convertToQuotedString(prefix + value));
}
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
for (String key : mFields.keySet()) {
sb.append(key).append(" ").append(mFields.get(key)).append("\n");
}
return sb.toString();
}
}