Skip to content

Commit

Permalink
[FIX] App crashes when entering server after applying certificate (An…
Browse files Browse the repository at this point in the history
…droid) (#3579)
  • Loading branch information
AlexAlexandre authored Jan 13, 2022
1 parent f29f360 commit 9d9553b
Showing 1 changed file with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@
import com.facebook.react.bridge.Promise;

import java.net.Socket;
import java.security.KeyStore;
import java.security.Principal;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509ExtendedKeyManager;
import java.security.PrivateKey;
import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import okhttp3.OkHttpClient;
import java.lang.InterruptedException;
import android.app.Activity;
import javax.net.ssl.KeyManager;
import android.security.KeyChain;
import android.security.KeyChainAliasCallback;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.RNFetchBlob.RNFetchBlob;
Expand All @@ -52,8 +56,9 @@ public class CustomClient implements CustomClientBuilder {
public void apply(OkHttpClient.Builder builder) {
if (alias != null) {
SSLSocketFactory sslSocketFactory = getSSLFactory(alias);
X509TrustManager trustManager = getTrustManagerFactory();
if (sslSocketFactory != null) {
builder.sslSocketFactory(sslSocketFactory);
builder.sslSocketFactory(sslSocketFactory, trustManager);
}
}
}
Expand All @@ -68,8 +73,9 @@ protected OkHttpClient getOkHttpClient() {

if (alias != null) {
SSLSocketFactory sslSocketFactory = getSSLFactory(alias);
X509TrustManager trustManager = getTrustManagerFactory();
if (sslSocketFactory != null) {
builder.sslSocketFactory(sslSocketFactory);
builder.sslSocketFactory(sslSocketFactory, trustManager);
}
}

Expand Down Expand Up @@ -162,25 +168,9 @@ public PrivateKey getPrivateKey(String s) {
}
};

final TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}

@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}

@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return certChain;
}
}
};

final X509TrustManager trustManager = getTrustManagerFactory();
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(new KeyManager[]{keyManager}, trustAllCerts, new java.security.SecureRandom());
sslContext.init(new KeyManager[]{keyManager}, new TrustManager[]{trustManager}, new java.security.SecureRandom());
SSLContext.setDefault(sslContext);

final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
Expand All @@ -190,4 +180,19 @@ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
}

public static X509TrustManager getTrustManagerFactory() {
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
}
final X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
return trustManager;
} catch (Exception e) {
return null;
}
}
}

0 comments on commit 9d9553b

Please sign in to comment.