Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 565f9f21 authored by Kenny Root's avatar Kenny Root
Browse files

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
parent 5423e68d
Loading
Loading
Loading
Loading
+61 −2
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.net.NetworkUtils;
import android.net.NetworkInfo.DetailedState;
import android.net.ProxyProperties;
import android.net.RouteInfo;
import android.net.wifi.WifiConfiguration.EnterpriseField;
import android.net.wifi.WifiConfiguration.IpAssignment;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiConfiguration.ProxySettings;
@@ -1092,7 +1093,7 @@ class WifiConfigStore {
                String varName = field.varName();
                String value = field.value();
                if (value != null) {
                    if (field != config.eap) {
                    if (field != config.eap && field != config.engine) {
                        value = (value.length() == 0) ? "NULL" : convertToQuotedString(value);
                    }
                    if (!mWifiNative.setNetworkVariable(
@@ -1399,10 +1400,68 @@ class WifiConfigStore {
            value = mWifiNative.getNetworkVariable(netId,
                    field.varName());
            if (!TextUtils.isEmpty(value)) {
                if (field != config.eap) value = removeDoubleQuotes(value);
                if (field != config.eap && field != config.engine) {
                    value = removeDoubleQuotes(value);
                }
                field.setValue(value);
            }
        }

        migrateOldEapTlsIfNecessary(config, netId);
    }

    /**
     * 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) {
+33 −2
Original line number Diff line number Diff line
@@ -29,6 +29,33 @@ import java.util.BitSet;
 */
public class WifiConfiguration implements Parcelable {

    /**
     * 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";

    /** {@hide} */
    public static final String ssidVarName = "ssid";
    /** {@hide} */
@@ -82,14 +109,18 @@ public class WifiConfiguration implements Parcelable {
    /** {@hide} */
    public EnterpriseField client_cert = new EnterpriseField("client_cert");
    /** {@hide} */
    public EnterpriseField private_key = new EnterpriseField("private_key");
    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,
            private_key, ca_cert };
            engine, engine_id, key_id, ca_cert };

    /**
     * Recognized key management schemes.