Skip to content

Instantly share code, notes, and snippets.

@Leaking
Created December 3, 2017 12:38
Show Gist options
  • Save Leaking/473887d1e95e2b8058bb35f4a0446445 to your computer and use it in GitHub Desktop.
Save Leaking/473887d1e95e2b8058bb35f4a0446445 to your computer and use it in GitHub Desktop.
find TrustManager From SSLContext
private X509TrustManager findTrustManagerFromSocketFactory(SSLContext mCtx) {
try {
//SSLContext --> contextSpi(OpenSSLContextImpl) --> sslParameters(SSLParametersImpl) --> x509TrustManager(X509TrustManager)
// find OpenSSLContextImpl
Field contextSpiField = mCtx.getClass().getDeclaredField("contextSpi");
contextSpiField.setAccessible(true);
Object openSSLContextImplObj = contextSpiField.get(mCtx);
// find SSLParametersImpl
Field sslParametersField = openSSLContextImplObj.getClass().getSuperclass().getDeclaredField("sslParameters");
sslParametersField.setAccessible(true);
Object sslParametersImplObj = sslParametersField.get(openSSLContextImplObj);
// find X509TrustManager
Field x509TrustManagerField = sslParametersImplObj.getClass().getDeclaredField("x509TrustManager");
x509TrustManagerField.setAccessible(true);
Object x509TrustManagerObj = x509TrustManagerField.get(sslParametersImplObj);
Log.i(TAG, "findTrustManagerFromSocketFactory object " + x509TrustManagerObj.getClass() + " " + (x509TrustManagerObj instanceof X509TrustManager));
if(x509TrustManagerObj instanceof X509TrustManager) {
return (X509TrustManager)x509TrustManagerObj;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@ehka
Copy link

ehka commented Jun 20, 2019

I get java.lang.NoSuchFieldException: sslParameters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment