Unable to extract trust manager on IBM JDK
What kind of issue is this?
-
[ ] Question. This issue tracker is not the place for questions. If you want to ask how to do something, or to understand why something isn't working the way you expect it to, use Stack Overflow. https://stackoverflow.com/questions/tagged/okhttp
-
[x] Bug report. If you’ve found a bug, spend the time to write a failing test. Bugs with tests get fixed. Here’s an example: https://gist.github.com/swankjesse/981fcae102f513eb13ed
Note: This particular issue is really tricky to write a unit test since it's very coupled to the platform, which is retrieved via internal implementation details in OkHttp's
Platformclass (which isn't injectable into the OkHttpClient). -
[ ] Feature Request. Start by telling us what problem you’re trying to solve. Often a solution already exists! Don’t send pull requests to implement new features without first getting our support. Sometimes we leave features out on purpose to keep the project small.
_Issue:_
java.lang.IllegalStateException: Unable to extract the trust manager on okhttp3.internal.Platform@e85a0ce8, sslSocketFactory is class com.ibm.jsse2.SSLSocketFactoryImpl
at okhttp3.OkHttpClient.<init>(OkHttpClient.java:187)
at okhttp3.OkHttpClient.<init>(OkHttpClient.java:151)
at retrofit2.Retrofit$Builder.build(Retrofit.java:551)
This seems closely related to #2323, except that I am running this on an IBM JDK (don't have a choice). In looking into the source, I noticed that the Platform is using reflection to pull the X509TrustManager from the SSLSocketFactory using known (via open source) implementation details from various open JDK implementations. However, I'm assuming there is a good reason why OkHttp can't just get a X509TrustManager using the JDK's API via something like the following?
private X509TrustManager resolveDefaultTrustManager() {
X509TrustManager x509TrustManager = null;
try {
TrustManagerFactory trustManagerFactory =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore)null);
for (TrustManager trustManager: trustManagerFactory.getTrustManagers()) {
if (trustManager instanceof X509TrustManager) {
x509TrustManager = (X509TrustManager)trustManager;
break;
}
}
} catch (NoSuchAlgorithmException | KeyStoreException e) {
// move along...
}
return x509TrustManager;
}
If there are good reasons why this approach simply isn't preferred, could it still be used as a fallback?
Note to self, the code causing this issue was implemented here in order to (based on comments in that commit) resolve #1669, though the commit admits that this solution is not ideal and should evolve into something better.
#1669 also mentions a related issue on StackOverflow. It seems that the accepted answer to this question is, more or less, my proposed solution above...
I also debugged through and inspected com.ibm.jsse2.SSLSocketFactoryImpl. It's obfuscated to the point where dirty reflection will never work....hence the current implementation is completely crippled on IBM JREs.
This assumes that the trust manager is the same one as in the system-wide default SSLSocketFactory. Unfortunately for us, we allow you to provide a custom SSLSocketFactory, which could have hidden inside a different trust manager. When the two trust managers disagree, we have a security problem.
I think the fix we’re going to ultimately need is closer to this change from @artem-zinnatullin: https://github.com/square/okhttp/pull/2405
I’m not sure that’s the exact API we want, but the spirit of it is right. Avoid magic if we can avoid it.
Unfortunately for us, we allow you to provide a custom SSLSocketFactory
but if a consumer doesn't provide a custom SSLSocketFactory and they do just lean on the system default, then wouldn't it make sense for OkHttp to leverage that, and the associated TrustManager(s)?
@sparty02 yep, definitely.
I face a similar problem. Can you pls guide me on how to solve this?
I did some debugging of okHttp in the last couple of days and found an approach to get the TrustManager out of a SSLSocketFactory using reflection. Just wanted to share it here. I think it is too ugly to actually ship. The whole IBM JDK is completely obfuscated, so you need to be very generic. Also be aware that this works with the IBM JDK 8.0.6.16, but it does not work when running on WebSphere Application Server!
private X509TrustManager getIBMTrustManager(SSLSocketFactory socketFactory) {
try {
Field[] declaredFields = socketFactory.getClass().getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
Object sslSocketFactoryImpl = field.get(socketFactory);
if (sslSocketFactoryImpl == null) {
continue;
}
Method[] implMethods = sslSocketFactoryImpl.getClass().getDeclaredMethods();
for (Method implMethod : implMethods) {
if (implMethod.getReturnType().equals(TrustManager[].class)) {
implMethod.setAccessible(true);
Object invoke = implMethod.invoke(sslSocketFactoryImpl);
TrustManager[] tms = (TrustManager[]) invoke;
if (tms.length != 1 || !(tms[0] instanceof X509TrustManager)) {
throw new IllegalStateException(
"Unexpected default trust managers:" + Arrays.toString(tms));
}
return (X509TrustManager) tms[0];
}
}
}
} catch (Exception e) {
...
}
return null;
}
To get the SSLSocketFactory you can use HttpsURLConnection.getDefaultSSLSocketFactory() or SSLContext.getInstance("SSL"). Your results might differ. SSLContext.getInstance("TLS") also works, but somehow has only very few ciphers enabled.
Yeah, it's a mess...