All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hubspot.horizon.ning.internal.NingSSLContext Maven / Gradle / Ivy

package com.hubspot.horizon.ning.internal;

import com.hubspot.horizon.HttpConfig;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

public final class NingSSLContext {

  private NingSSLContext() {
    throw new AssertionError();
  }

  public static SSLContext forConfig(HttpConfig config) {
    return config.isAcceptAllSSL() ? acceptAllSSLContext() : standardSSLContext();
  }

  private static SSLContext standardSSLContext() {
    try {
      SSLContext sslContext = SSLContext.getInstance("TLS");
      sslContext.init(null, null, null);

      return sslContext;
    } catch (GeneralSecurityException e) {
      throw new RuntimeException(e);
    }
  }

  private static SSLContext acceptAllSSLContext() {
    try {
      SSLContext sslcontext = SSLContext.getInstance("SSL");
      sslcontext.init(null, new TrustManager[]{ new AcceptAllTrustManager() }, new SecureRandom());

      return sslcontext;
    } catch (GeneralSecurityException e) {
      throw new RuntimeException(e);
    }
  }

  private static class AcceptAllTrustManager implements X509TrustManager {

    @Override
    public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
      // do nothing
    }

    @Override
    public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
      // do nothing
    }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy