|
| 1 | +package com.asterinet.react.tcpsocket; |
| 2 | + |
| 3 | +import android.annotation.SuppressLint; |
| 4 | +import android.content.Context; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.net.URI; |
| 9 | +import java.security.GeneralSecurityException; |
| 10 | +import java.security.KeyStore; |
| 11 | +import java.security.SecureRandom; |
| 12 | +import java.security.cert.Certificate; |
| 13 | +import java.security.cert.CertificateFactory; |
| 14 | +import java.security.cert.X509Certificate; |
| 15 | + |
| 16 | +import javax.net.ssl.SSLContext; |
| 17 | +import javax.net.ssl.SSLSocketFactory; |
| 18 | +import javax.net.ssl.TrustManager; |
| 19 | +import javax.net.ssl.TrustManagerFactory; |
| 20 | +import javax.net.ssl.X509TrustManager; |
| 21 | + |
| 22 | +import androidx.annotation.NonNull; |
| 23 | +import androidx.annotation.RawRes; |
| 24 | + |
| 25 | +final class SSLCertificateHelper { |
| 26 | + /** |
| 27 | + * Creates an SSLSocketFactory instance for use with all CAs provided. |
| 28 | + * |
| 29 | + * @return An SSLSocketFactory which trusts all CAs when provided to network clients |
| 30 | + */ |
| 31 | + static SSLSocketFactory createBlindSocketFactory() throws GeneralSecurityException { |
| 32 | + SSLContext ctx = SSLContext.getInstance("TLS"); |
| 33 | + ctx.init(null, new TrustManager[]{new BlindTrustManager()}, null); |
| 34 | + return ctx.getSocketFactory(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Creates an SSLSocketFactory instance for use with the CA provided in the resource file. |
| 39 | + * |
| 40 | + * @param context Context used to open up the CA file |
| 41 | + * @param rawResourceUri Raw resource file to the CA (in .crt or .cer format, for instance) |
| 42 | + * @return An SSLSocketFactory which trusts the provided CA when provided to network clients |
| 43 | + */ |
| 44 | + static SSLSocketFactory createCustomTrustedSocketFactory(@NonNull final Context context, @NonNull final String rawResourceUri) throws IOException, GeneralSecurityException { |
| 45 | + InputStream caInput = getRawResourceStream(context, rawResourceUri); |
| 46 | + // Generate the CA Certificate from the raw resource file |
| 47 | + Certificate ca = CertificateFactory.getInstance("X.509").generateCertificate(caInput); |
| 48 | + caInput.close(); |
| 49 | + // Load the key store using the CA |
| 50 | + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 51 | + keyStore.load(null, null); |
| 52 | + keyStore.setCertificateEntry("ca", ca); |
| 53 | + |
| 54 | + // Initialize the TrustManager with this CA |
| 55 | + TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 56 | + tmf.init(keyStore); |
| 57 | + |
| 58 | + // Create an SSL context that uses the created trust manager |
| 59 | + SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 60 | + sslContext.init(null, tmf.getTrustManagers(), new SecureRandom()); |
| 61 | + return sslContext.getSocketFactory(); |
| 62 | + } |
| 63 | + |
| 64 | + private static InputStream getRawResourceStream(@NonNull final Context context, @NonNull final String resourceUri) throws IOException { |
| 65 | + final int resId = getResourceId(context, resourceUri); |
| 66 | + if (resId == 0) |
| 67 | + return URI.create(resourceUri).toURL().openStream(); // From metro on development |
| 68 | + else return context.getResources().openRawResource(resId); // From bundle in production |
| 69 | + } |
| 70 | + |
| 71 | + @RawRes |
| 72 | + private static int getResourceId(@NonNull final Context context, @NonNull final String resourceUri) { |
| 73 | + String name = resourceUri.toLowerCase().replace("-", "_"); |
| 74 | + try { |
| 75 | + return Integer.parseInt(name); |
| 76 | + } catch (NumberFormatException ex) { |
| 77 | + return context.getResources().getIdentifier(name, "raw", context.getPackageName()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static class BlindTrustManager implements X509TrustManager { |
| 82 | + public X509Certificate[] getAcceptedIssuers() { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + @SuppressLint("TrustAllX509TrustManager") |
| 87 | + public void checkClientTrusted(X509Certificate[] chain, String authType) { |
| 88 | + } |
| 89 | + |
| 90 | + @SuppressLint("TrustAllX509TrustManager") |
| 91 | + public void checkServerTrusted(X509Certificate[] chain, String authType) { |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments