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

Commit a8d51aac authored by Yan Yan's avatar Yan Yan Committed by Gerrit Code Review
Browse files

Merge changes from topic "ike-params"

* changes:
  Support converting VcnControlPlaneIkeConfig to/from PersistableBundle
  Support converting IKE Options to/from PersistableBundle
  Support converting IkeConfigRequest to/from PersistableBundle
  Support converting IkeAuthEapConfig to/from PersistableBundle
  Support converting IkeAuthDigitalSignConfig to/from PersistableBundle
  Support converting IkeAuthConfig to/from PersistableBundle
  Support converting IkeSessionParams to/from PersistableBundle
parents dd010bff f8bce7ed
Loading
Loading
Loading
Loading
+25 −17
Original line number Diff line number Diff line
@@ -19,11 +19,13 @@ package android.net.vcn;
import static android.net.vcn.VcnControlPlaneConfig.CONFIG_TYPE_IKE;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.net.ipsec.ike.IkeSessionParams;
import android.net.ipsec.ike.TunnelModeChildSessionParams;
import android.net.vcn.persistablebundleutils.IkeSessionParamsUtils;
import android.net.vcn.persistablebundleutils.TunnelModeChildSessionParamsUtils;
import android.os.PersistableBundle;
import android.util.ArraySet;
import android.util.Log;

import java.util.Objects;

@@ -38,14 +40,11 @@ import java.util.Objects;
public final class VcnControlPlaneIkeConfig extends VcnControlPlaneConfig {
    private static final String TAG = VcnControlPlaneIkeConfig.class.getSimpleName();

    // STOPSHIP: b/163604823 Make mIkeParams and mChildParams @NonNull when it is supported to
    // construct mIkeParams and mChildParams from PersistableBundles.

    private static final String IKE_PARAMS_KEY = "mIkeParams";
    @Nullable private final IkeSessionParams mIkeParams;
    @NonNull private final IkeSessionParams mIkeParams;

    private static final String CHILD_PARAMS_KEY = "mChildParams";
    @Nullable private final TunnelModeChildSessionParams mChildParams;
    @NonNull private final TunnelModeChildSessionParams mChildParams;

    private static final ArraySet<String> BUNDLE_KEY_SET = new ArraySet<>();

@@ -80,11 +79,19 @@ public final class VcnControlPlaneIkeConfig extends VcnControlPlaneConfig {
        final PersistableBundle ikeParamsBundle = in.getPersistableBundle(IKE_PARAMS_KEY);
        final PersistableBundle childParamsBundle = in.getPersistableBundle(CHILD_PARAMS_KEY);

        // STOPSHIP: b/163604823 Support constructing mIkeParams and mChildParams from
        // PersistableBundles.
        Objects.requireNonNull(ikeParamsBundle, "IKE Session Params was null");
        Objects.requireNonNull(childParamsBundle, "Child Session Params was null");

        mIkeParams = IkeSessionParamsUtils.fromPersistableBundle(ikeParamsBundle);
        mChildParams = TunnelModeChildSessionParamsUtils.fromPersistableBundle(childParamsBundle);

        mIkeParams = null;
        mChildParams = null;
        for (String key : in.keySet()) {
            if (!BUNDLE_KEY_SET.contains(key)) {
                Log.w(TAG, "Found an unexpected key in the PersistableBundle: " + key);
            }
        }

        validate();
    }

    private void validate() {
@@ -101,9 +108,11 @@ public final class VcnControlPlaneIkeConfig extends VcnControlPlaneConfig {
    @NonNull
    public PersistableBundle toPersistableBundle() {
        final PersistableBundle result = super.toPersistableBundle();

        // STOPSHIP: b/163604823 Support converting mIkeParams and mChildParams to
        // PersistableBundles.
        result.putPersistableBundle(
                IKE_PARAMS_KEY, IkeSessionParamsUtils.toPersistableBundle(mIkeParams));
        result.putPersistableBundle(
                CHILD_PARAMS_KEY,
                TunnelModeChildSessionParamsUtils.toPersistableBundle(mChildParams));
        return result;
    }

@@ -134,10 +143,9 @@ public final class VcnControlPlaneIkeConfig extends VcnControlPlaneConfig {

        VcnControlPlaneIkeConfig other = (VcnControlPlaneIkeConfig) o;

        // STOPSHIP: b/163604823 Also check mIkeParams and mChildParams when it is supported to
        // construct mIkeParams and mChildParams from PersistableBundles. They are not checked
        // now so that VcnGatewayConnectionConfigTest and VcnConfigTest can pass.
        return super.equals(o);
        return super.equals(o)
                && Objects.equals(mIkeParams, other.mIkeParams)
                && Objects.equals(mChildParams, other.mChildParams);
    }

    /** @hide */
+21 −1
Original line number Diff line number Diff line
@@ -18,18 +18,24 @@ package android.net.vcn.persistablebundleutils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Objects;

/**
 * CertUtils provides utility methods for constructing Certificate.
 * CertUtils provides utility methods for constructing Certificate and PrivateKey.
 *
 * @hide
 */
public class CertUtils {
    private static final String CERT_TYPE_X509 = "X.509";
    private static final String PRIVATE_KEY_TYPE_RSA = "RSA";

    /** Decodes an ASN.1 DER encoded Certificate */
    public static X509Certificate certificateFromByteArray(byte[] derEncoded) {
@@ -43,4 +49,18 @@ public class CertUtils {
            throw new IllegalArgumentException("Fail to decode certificate", e);
        }
    }

    /** Decodes a PKCS#8 encoded RSA private key */
    public static RSAPrivateKey privateKeyFromByteArray(byte[] pkcs8Encoded) {
        Objects.requireNonNull(pkcs8Encoded, "pkcs8Encoded was null");
        PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(pkcs8Encoded);

        try {
            KeyFactory keyFactory = KeyFactory.getInstance(PRIVATE_KEY_TYPE_RSA);

            return (RSAPrivateKey) keyFactory.generatePrivate(privateKeySpec);
        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            throw new IllegalArgumentException("Fail to decode PrivateKey", e);
        }
    }
}
Loading