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

Commit 5116ac0d authored by Chad Brubaker's avatar Chad Brubaker
Browse files

Add implemention for isSameTrustConfiguration

Bug: 27672565
Change-Id: Id5291a4a5f2ca75fdf84db4d51363735f1a76845
parent 390a8130
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));
    }
}