Skip to content

Commit

Permalink
Support X509ExtendedTrustManager for sdk >= 24
Browse files Browse the repository at this point in the history
  • Loading branch information
kiber.io committed Mar 19, 2024
1 parent 861c5fa commit 3a523b8
Showing 1 changed file with 55 additions and 4 deletions.
59 changes: 55 additions & 4 deletions app/src/main/java/just/trust/me/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package just.trust.me;

import android.annotation.TargetApi;
import android.content.Context;
import android.net.http.SslError;
import android.net.http.X509TrustManagerExtensions;
import android.os.Build;
import android.util.Log;
import android.util.Pair;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;

Expand Down Expand Up @@ -35,9 +38,11 @@
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509ExtendedTrustManager;
import javax.net.ssl.X509TrustManager;

import de.robv.android.xposed.IXposedHookLoadPackage;
Expand Down Expand Up @@ -138,7 +143,7 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
keymanagers = (KeyManager[]) callStaticMethod(SSLSocketFactory.class, "createKeyManagers", keystore, keystorePassword);
}

trustmanagers = new TrustManager[]{new ImSureItsLegitTrustManager()};
trustmanagers = new TrustManager[]{getTrustManager()};

setObjectField(param.thisObject, "sslcontext", SSLContext.getInstance(algorithm));
callMethod(getObjectField(param.thisObject, "sslcontext"), "init", keymanagers, trustmanagers, random);
Expand Down Expand Up @@ -183,7 +188,7 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
return;
}

param.setResult(new TrustManager[]{new ImSureItsLegitTrustManager()});
param.setResult(new TrustManager[]{getTrustManager()});
}
});

Expand Down Expand Up @@ -232,7 +237,7 @@ protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {

param.args[0] = null;
param.args[1] = new TrustManager[]{new ImSureItsLegitTrustManager()};
param.args[1] = new TrustManager[]{getTrustManager()};
param.args[2] = null;

}
Expand Down Expand Up @@ -345,7 +350,7 @@ public boolean hasDefaultHTTPClient() {
private javax.net.ssl.SSLSocketFactory getEmptySSLFactory() {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{new ImSureItsLegitTrustManager()}, null);
sslContext.init(null, new TrustManager[]{getTrustManager()}, null);
return sslContext.getSocketFactory();
} catch (NoSuchAlgorithmException | KeyManagementException e) {
return null;
Expand Down Expand Up @@ -566,6 +571,44 @@ void processHttpClientAndroidLib(ClassLoader classLoader) {
}
}

@TargetApi(Build.VERSION_CODES.N)
private class ImSureItsLegitExtendedTrustManager extends X509ExtendedTrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException {

}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException {

}

@Override
public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException {

}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException {

}

@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

}

@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}

private class ImSureItsLegitTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
Expand All @@ -586,6 +629,14 @@ public X509Certificate[] getAcceptedIssuers() {
}
}

private X509TrustManager getTrustManager() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return new ImSureItsLegitExtendedTrustManager();
} else {
return new ImSureItsLegitTrustManager();
}
}

private class ImSureItsLegitHostnameVerifier implements HostnameVerifier {

@Override
Expand Down

0 comments on commit 3a523b8

Please sign in to comment.