diff --git a/core/java/android/net/http/X509TrustManagerExtensions.java b/core/java/android/net/http/X509TrustManagerExtensions.java index 67293475a9e7d..87a0b709d9f80 100644 --- a/core/java/android/net/http/X509TrustManagerExtensions.java +++ b/core/java/android/net/http/X509TrustManagerExtensions.java @@ -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) { - return true; + 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()); + } + } } } diff --git a/core/java/android/security/net/config/RootTrustManager.java b/core/java/android/security/net/config/RootTrustManager.java index 19f688787abe8..859e022952db9 100644 --- a/core/java/android/security/net/config/RootTrustManager.java +++ b/core/java/android/security/net/config/RootTrustManager.java @@ -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. + * + *
This is required by android.net.http.X509TrustManagerExtensions. + */ + public boolean isSameTrustConfiguration(String hostname1, String hostname2) { + return mConfig.getConfigForHostname(hostname1) + .equals(mConfig.getConfigForHostname(hostname2)); + } }