/ConfigurableRestSource/configurable-rest-source/src/main/java/com/davis/ddf/crs/client/TrustSpecifiedOkHttpClient.java
https://gitlab.com/samuel-davis/DDF-TestSource · Java · 301 lines · 163 code · 20 blank · 118 comment · 10 complexity · e1513957c0e8ba2339518ee214a70a07 MD5 · raw file
- package com.davis.ddf.crs.client;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.SequenceInputStream;
- import java.nio.charset.Charset;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.security.GeneralSecurityException;
- import java.security.KeyStore;
- import java.security.cert.Certificate;
- import java.security.cert.CertificateFactory;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.concurrent.TimeUnit;
- import javax.net.ssl.KeyManagerFactory;
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.SSLSocketFactory;
- import javax.net.ssl.TrustManager;
- import javax.net.ssl.TrustManagerFactory;
- import javax.net.ssl.X509TrustManager;
- import okhttp3.CertificatePinner;
- import okhttp3.OkHttpClient;
- import okio.Buffer;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- /**
- * Class to create a custom Okhttp client that only trusts the specified certificates.
- *
- * **/
- public final class TrustSpecifiedOkHttpClient {
- private static final String ROOT_FILE_PATH="certs/";
- private static final Logger logger = LoggerFactory.getLogger(TrustSpecifiedOkHttpClient.class.getName());
- private OkHttpClient client;
- /**
- * Public constructor, this constructor will created a TrustSpecified client that only trusts the
- * certificates it is given.
- * The certificates must be located within the Jar's resources directory
- * @param pathToJarResources a list of absolute paths leading to jar resources
- * These certificates must be in PEM
- * **/
- public TrustSpecifiedOkHttpClient(String[] pathToJarResources,int connectTimeout, int readTimeout) {
- X509TrustManager trustManager;
- SSLSocketFactory sslSocketFactory;
- InputStream inputStream = createInputStreamFromFiles(pathToJarResources);
- try {
- trustManager = trustManagerForCertificates(inputStream);
- SSLContext sslContext = SSLContext.getInstance("TLS");
- sslContext.init(null,
- new TrustManager[] {trustManager},
- null
- );
- sslSocketFactory = sslContext.getSocketFactory();
- } catch (GeneralSecurityException e) {
- throw new RuntimeException(e);
- }
- OkHttpClient.Builder builder = new OkHttpClient.Builder();
- builder.connectTimeout(connectTimeout, TimeUnit.SECONDS);
- builder.readTimeout(readTimeout, TimeUnit.SECONDS);
- builder.sslSocketFactory(sslSocketFactory, trustManager);
- client = builder.build();
- }
- /**
- * Creates a custom okhttp client where all certificates within the jar resources folder cert
- * are trusted. This is a convenience method so you dont have to pass a array of file paths.
- * **/
- public TrustSpecifiedOkHttpClient(boolean useCertFolder,int connectTimeout, int readTimeout) {
- if(!useCertFolder){
- return;
- }
- X509TrustManager trustManager;
- SSLSocketFactory sslSocketFactory;
- ArrayList<String> certArray = getCertDirectoryFileNames();
- String[] certNames = new String[certArray.size()];
- certNames = certArray.toArray(certNames);
- InputStream inputStream = createInputStreamFromFiles(certNames);
- try {
- trustManager = trustManagerForCertificates(inputStream);
- SSLContext sslContext = SSLContext.getInstance("TLS");
- sslContext.init(null,
- new TrustManager[] {trustManager},
- null
- );
- sslSocketFactory = sslContext.getSocketFactory();
- } catch (GeneralSecurityException e) {
- throw new RuntimeException(e);
- }
- OkHttpClient.Builder builder = new OkHttpClient.Builder();
- builder.connectTimeout(connectTimeout, TimeUnit.SECONDS);
- builder.readTimeout(readTimeout, TimeUnit.SECONDS);
- builder.sslSocketFactory(sslSocketFactory, trustManager);
- client = builder.build();
- }
- private InputStream createInputStreamFromFiles(String[] pathToJarResources){
- ArrayList<String> certArray = new ArrayList<>();
- for(String cert : pathToJarResources){
- String crtFromFile =readFile(cert,StandardCharsets.UTF_8);
- certArray.add(crtFromFile);
- logger.debug(crtFromFile);
- }
- return trustedCertificatesInputStream(certArray);
- }
- public OkHttpClient getClient() {
- return client;
- }
- private ArrayList<String> getCertDirectoryFileNames(){
- File f=new File(ROOT_FILE_PATH);
- File[] allSubFiles=f.listFiles();
- ArrayList<String> certNames = new ArrayList<String>();
- for(File file : allSubFiles) {
- certNames.add(file.getName());
- }
- return certNames;
- }
- /**Reads a
- * @param path the file to be read
- * into a string with the
- * @param encoding the specified encoding
- * @return the encoded string.
- **/
- private static String readFile(String path, Charset encoding)
- {
- String result = null;
- byte[] encoded = new byte[0];
- try {
- encoded = TrustSpecifiedOkHttpClient.class.getClassLoader().getResource(path).getFile().getBytes();
- } catch (Exception e) {
- e.printStackTrace();
- }
- if(encoded.length < 1){
- result ="";
- }else{
- result = new String(encoded,encoding);
- }
- return result;
- }
- /**
- * Reads a array list of cert strings into a single input stream.
- * @param certificates the cert strings to read
- * @return a inputstream containing them all
- * **/
- private InputStream trustedCertificatesInputStream(ArrayList<String> certificates) {
- Buffer buffer = new Buffer();
- for(String certString : certificates){
- buffer.writeUtf8(certString);
- }
- return buffer.inputStream();
- }
- /**
- * Returns a trust manager that trusts {@code certificates} and none other. HTTPS services whose
- * certificates have not been signed by these certificates will fail with a {@code
- * SSLHandshakeException}.
- *
- * <p>This can be used to replace the host platform's built-in trusted certificates with a custom
- * set. This is useful in development where certificate authority-trusted certificates aren't
- * available. Or in production, to avoid reliance on third-party certificate authorities.
- *
- * <p>See also {@link CertificatePinner}, which can limit trusted certificates while still using
- * the host platform's built-in trust store.
- *
- * <h3>Warning: Customizing Trusted Certificates is Dangerous!</h3>
- *
- * <p>Relying on your own trusted certificates limits your server team's ability to update their
- * TLS certificates. By installing a specific set of trusted certificates, you take on additional
- * operational complexity and limit your ability to migrate between certificate authorities. Do
- * not use custom trusted certificates in production without the blessing of your server's TLS
- * administrator.
- */
- private X509TrustManager trustManagerForCertificates(InputStream in)
- throws GeneralSecurityException {
- CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
- Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(in);
- if (certificates.isEmpty()) {
- throw new IllegalArgumentException("expected non-empty set of trusted certificates");
- }
- // Put the certificates a key store.
- char[] password = "password".toCharArray(); // Any password will work.
- KeyStore keyStore = newEmptyKeyStore(password);
- int index = 0;
- for (Certificate certificate : certificates) {
- String certificateAlias = Integer.toString(index++);
- keyStore.setCertificateEntry(certificateAlias, certificate);
- }
- // Use it to build an X509 trust manager.
- KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(
- KeyManagerFactory.getDefaultAlgorithm());
- keyManagerFactory.init(keyStore, password);
- TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
- TrustManagerFactory.getDefaultAlgorithm());
- trustManagerFactory.init(keyStore);
- TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
- if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
- throw new IllegalStateException("Unexpected default trust managers:"
- + Arrays.toString(trustManagers));
- }
- return (X509TrustManager) trustManagers[0];
- }
- private KeyStore newEmptyKeyStore(char[] password) throws GeneralSecurityException {
- try {
- KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
- InputStream in = null; // By convention, 'null' creates an empty key store.
- keyStore.load(in, password);
- return keyStore;
- } catch (IOException e) {
- throw new AssertionError(e);
- }
- }
- /*Keeping these for convenience purposes
- *
- * // PEM files for root certificates of Comodo and Entrust. These two CAs are sufficient to view
- // https://publicobject.com (Comodo) and https://squareup.com (Entrust). But they aren't
- // sufficient to connect to most HTTPS sites including https://godaddy.com and https://visa.com.
- // Typically developers will need to get a PEM file from their organization's TLS administrator.
- *
- * String comodoRsaCertificationAuthority = ""
- + "-----BEGIN CERTIFICATE-----\n"
- + "MIIF2DCCA8CgAwIBAgIQTKr5yttjb+Af907YWwOGnTANBgkqhkiG9w0BAQwFADCB\n"
- + "hTELMAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4G\n"
- + "A1UEBxMHU2FsZm9yZDEaMBgGA1UEChMRQ09NT0RPIENBIExpbWl0ZWQxKzApBgNV\n"
- + "BAMTIkNPTU9ETyBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTAwMTE5\n"
- + "M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n"
- + "FgQUu69+Aj36pvE8hI6t7jiY7NkyMtQwDgYDVR0PAQH/BAQDAgEGMA8GA1UdEwEB\n"
- + "/wQFMAMBAf8wDQYJKoZIhvcNAQEMBQADggIBAArx1UaEt65Ru2yyTUEUAJNMnMvl\n"
- + "wFTPoCWOAvn9sKIN9SCYPBMtrFaisNZ+EZLpLrqeLppysb0ZRGxhNaKatBYSaVqM\n"
- + "4dc+pBroLwP0rmEdEBsqpIt6xf4FpuHA1sj+nq6PK7o9mfjYcwlYRm6mnPTXJ9OV\n"
- + "2jeDchzTc+CiR5kDOF3VSXkAKRzH7JsgHAckaVd4sjn8OoSgtZx8jb8uk2Intzna\n"
- + "FxiuvTwJaP+EmzzV1gsD41eeFPfR60/IvYcjt7ZJQ3mFXLrrkguhxuhoqEwWsRqZ\n"
- + "CuhTLJK7oQkYdQxlqHvLI7cawiiFwxv/0Cti76R7CZGYZ4wUAc1oBmpjIXUDgIiK\n"
- + "boHGhfKppC3n9KUkEEeDys30jXlYsQab5xoq2Z0B15R97QNKyvDb6KkBPvVWmcke\n"
- + "jkk9u+UJueBPSZI9FoJAzMxZxuY67RIuaTxslbH9qh17f4a+Hg4yRvv7E491f0yL\n"
- + "S0Zj/gA0QHDBw7mh3aZw4gSzQbzpgJHqZJx64SIDqZxubw5lT2yHh17zbqD5daWb\n"
- + "QOhTsiedSrnAdyGN/4fy3ryM7xfft0kL0fJuMAsaDk527RH89elWsn2/x20Kk4yl\n"
- + "0MC2Hb46TpSi125sC8KKfPog88Tk5c0NqMuRkrF8hey1FGlmDoLnzc7ILaZRfyHB\n"
- + "NVOFBkpdn627G190\n"
- + "-----END CERTIFICATE-----\n";
- String entrustRootCertificateAuthority = ""
- + "-----BEGIN CERTIFICATE-----\n"
- + "MIIEkTCCA3mgAwIBAgIERWtQVDANBgkqhkiG9w0BAQUFADCBsDELMAkGA1UEBhMC\n"
- + "VVMxFjAUBgNVBAoTDUVudHJ1c3QsIEluYy4xOTA3BgNVBAsTMHd3dy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