Merge "Add implemention for isSameTrustConfiguration" into nyc-dev

This commit is contained in:
TreeHugger Robot
2016-04-12 21:28:51 +00:00
committed by Android (Google) Code Review
2 changed files with 36 additions and 1 deletions

View File

@@ -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());
}
}
}
}

View File

@@ -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));
}
}