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

Commit d92e5c31 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add implemention for isSameTrustConfiguration" into nyc-dev

parents 20f2b38e 5116ac0d
Loading
Loading
Loading
Loading
+25 −1
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ public class X509TrustManagerExtensions {
    private final X509TrustManager mTrustManager;
    private final Method mCheckServerTrusted;
    private final Method mIsUserAddedCertificate;
    private final Method mIsSameTrustConfiguration;

    /**
     * Constructs a new X509TrustManagerExtensions wrapper.
@@ -57,6 +58,7 @@ public class X509TrustManagerExtensions {
            mTrustManager = null;
            mCheckServerTrusted = null;
            mIsUserAddedCertificate = null;
            mIsSameTrustConfiguration = null;
            return;
        }
        // Use duck typing if possible.
@@ -80,6 +82,15 @@ public class X509TrustManagerExtensions {
            throw new IllegalArgumentException(
                    "Required method isUserAddedCertificate(X509Certificate) missing");
        }
        // Get the option isSameTrustConfiguration method.
        Method isSameTrustConfiguration = null;
        try {
            isSameTrustConfiguration = tm.getClass().getMethod("isSameTrustConfiguration",
                    String.class,
                    String.class);
        } catch (ReflectiveOperationException ignored) {
        }
        mIsSameTrustConfiguration = isSameTrustConfiguration;
    }

    /**
@@ -150,6 +161,19 @@ public class X509TrustManagerExtensions {
     */
    @SystemApi
    public boolean isSameTrustConfiguration(String hostname1, String hostname2) {
        if (mIsSameTrustConfiguration == null) {
            return true;
        }
        try {
            return (Boolean) mIsSameTrustConfiguration.invoke(mTrustManager, hostname1, hostname2);
        } catch (IllegalAccessException e) {
            throw new RuntimeException("Failed to call isSameTrustConfiguration", e);
        } catch (InvocationTargetException e) {
            if (e.getCause() instanceof RuntimeException) {
                throw (RuntimeException) e.getCause();
            } else {
                throw new RuntimeException("isSameTrustConfiguration failed", e.getCause());
            }
        }
    }
}
+11 −0
Original line number Diff line number Diff line
@@ -148,4 +148,15 @@ public class RootTrustManager extends X509ExtendedTrustManager {
        NetworkSecurityConfig config = mConfig.getConfigForHostname("");
        return config.getTrustManager().getAcceptedIssuers();
    }

    /**
     * Returns {@code true} if this trust manager uses the same trust configuration for the provided
     * hostnames.
     *
     * <p>This is required by android.net.http.X509TrustManagerExtensions.
     */
    public boolean isSameTrustConfiguration(String hostname1, String hostname2) {
        return mConfig.getConfigForHostname(hostname1)
                .equals(mConfig.getConfigForHostname(hostname2));
    }
}