Example usage for org.apache.http.conn.ssl SSLConnectionSocketFactory TLS

List of usage examples for org.apache.http.conn.ssl SSLConnectionSocketFactory TLS

Introduction

In this page you can find the example usage for org.apache.http.conn.ssl SSLConnectionSocketFactory TLS.

Prototype

String TLS

To view the source code for org.apache.http.conn.ssl SSLConnectionSocketFactory TLS.

Click Source Link

Usage

From source file:io.fabric8.maven.docker.access.hc.http.HttpClientBuilder.java

private static Registry<ConnectionSocketFactory> getSslFactoryRegistry(String certPath) throws IOException {
    try {//from w  ww.j  a va2s.co  m
        KeyStore keyStore = KeyStoreUtil.createDockerKeyStore(certPath);

        SSLContext sslContext = SSLContexts.custom().useProtocol(SSLConnectionSocketFactory.TLS)
                .loadKeyMaterial(keyStore, "docker".toCharArray()).loadTrustMaterial(keyStore, null).build();
        String tlsVerify = System.getenv("DOCKER_TLS_VERIFY");
        SSLConnectionSocketFactory sslsf = tlsVerify != null && !tlsVerify.equals("0")
                && !tlsVerify.equals("false") ? new SSLConnectionSocketFactory(sslContext)
                        : new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);

        return RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslsf).build();
    } catch (GeneralSecurityException e) {
        // this isn't ideal but the net effect is the same
        throw new IOException(e);
    }
}

From source file:com.sonatype.nexus.ssl.plugin.internal.TrustStoreImpl.java

@Override
public SSLContext getSSLContext() {
    SSLContext _sslcontext = this.sslcontext; // local variable allows concurrent removeTrustCertificate
    if (_sslcontext == null) {
        try {/*from w w w  .  ja  va 2s. co  m*/
            _sslcontext = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
            _sslcontext.init(keyManagers, trustManagers, DEFAULT_RANDOM);
            this.sslcontext = _sslcontext;
        } catch (Exception e) {
            log.debug("Could not create SSL context", e);
            Throwables.throwIfUnchecked(e);
            throw new RuntimeException(e);
        }
    }
    return _sslcontext;
}

From source file:org.commonjava.util.jhttpc.HttpFactory.java

private SSLConnectionSocketFactory createSSLSocketFactory(final SiteConfig location) throws JHttpCException {
    SSLConnectionSocketFactory fac = (SSLConnectionSocketFactory) location.getAttribute(SSL_FACTORY_ATTRIB);
    if (fac != null) {
        return fac;
    }//from  w  ww  .j ava2  s.  c  o m

    KeyStore ks = null;
    KeyStore ts = null;

    final String kcPem = location.getKeyCertPem();

    final String kcPass = passwords.lookup(new PasswordKey(location, PasswordType.KEY));
    if (kcPem != null) {
        logger.debug("Adding client key/certificate from: {}", location);
        if (kcPass == null || kcPass.length() < 1) {
            logger.error("Invalid configuration. Location: {} cannot have an empty key password!",
                    location.getUri());
            throw new JHttpCException(
                    "Location: " + location.getUri() + " is misconfigured! Key password cannot be empty.");
        }

        try {
            logger.trace("Reading Client SSL key from:\n\n{}\n\n", kcPem);
            ks = SSLUtils.readKeyAndCert(kcPem, kcPass);

            logger.trace("Keystore contains the following certificates: {}", new CertEnumerator(ks, kcPass));
        } catch (final CertificateException e) {
            logger.error(String.format(
                    "Invalid configuration. Location: %s has an invalid client certificate! Error: %s",
                    location.getUri(), e.getMessage()), e);
            throw new JHttpCException(
                    "Failed to initialize SSL connection for repository: " + location.getUri());
        } catch (final KeyStoreException e) {
            logger.error(String.format(
                    "Invalid configuration. Cannot initialize keystore for repository: %s. Error: %s",
                    location.getUri(), e.getMessage()), e);
            throw new JHttpCException(
                    "Failed to initialize SSL connection for repository: " + location.getUri());
        } catch (final NoSuchAlgorithmException e) {
            logger.error(String.format(
                    "Invalid configuration. Cannot initialize keystore for repository: %s. Error: %s",
                    location.getUri(), e.getMessage()), e);
            throw new JHttpCException(
                    "Failed to initialize SSL connection for repository: " + location.getUri());
        } catch (final InvalidKeySpecException e) {
            logger.error(
                    String.format("Invalid configuration. Invalid client key for repository: %s. Error: %s",
                            location.getUri(), e.getMessage()),
                    e);
            throw new JHttpCException(
                    "Failed to initialize SSL connection for repository: " + location.getUri());
        } catch (IOException e) {
            throw new JHttpCException("Failed to read client SSL key/certificate from: %s. Reason: %s", e,
                    location, e.getMessage());
        } catch (JHttpCException e) {
            throw new JHttpCException("Failed to read client SSL key/certificate from: %s. Reason: %s", e,
                    location, e.getMessage());
        }
    } else {
        logger.debug("No client key/certificate found");
    }

    final String sPem = location.getServerCertPem();

    //        logger.debug( "Server certificate PEM:\n{}", sPem );
    if (sPem != null) {
        logger.debug("Loading TrustStore (server SSL) information from: {}", location);
        try {
            logger.trace("Reading Server SSL cert from:\n\n{}\n\n", sPem);
            ts = SSLUtils.decodePEMTrustStore(sPem, location.getHost());

            logger.trace("Trust store contains the following certificates:\n{}", new CertEnumerator(ts, null));
        } catch (final CertificateException e) {
            logger.error(String.format(
                    "Invalid configuration. Location: %s has an invalid server certificate! Error: %s",
                    location.getUri(), e.getMessage()), e);
            throw new JHttpCException(
                    "Failed to initialize SSL connection for repository: " + location.getUri());
        } catch (final KeyStoreException e) {
            logger.error(String.format(
                    "Invalid configuration. Cannot initialize keystore for repository: %s. Error: %s",
                    location.getUri(), e.getMessage()), e);
            throw new JHttpCException(
                    "Failed to initialize SSL connection for repository: " + location.getUri());
        } catch (final NoSuchAlgorithmException e) {
            logger.error(String.format(
                    "Invalid configuration. Cannot initialize keystore for repository: %s. Error: %s",
                    location.getUri(), e.getMessage()), e);
            throw new JHttpCException(
                    "Failed to initialize SSL connection for repository: " + location.getUri());
        } catch (IOException e) {
            throw new JHttpCException(
                    "Failed to read server SSL certificate(s) (or couldn't parse server hostname) from: %s. Reason: %s",
                    e, location, e.getMessage());
        }
    } else {
        logger.debug("No server certificates found");
    }

    if (ks != null || ts != null) {
        logger.debug("Setting up SSL context.");
        try {
            SSLContextBuilder sslBuilder = SSLContexts.custom().useProtocol(SSLConnectionSocketFactory.TLS);
            if (ks != null) {
                logger.trace("Loading key material for SSL context...");
                PrivateKeyStrategy pkStrategy = new MonolithicKeyStrategy();
                sslBuilder.loadKeyMaterial(ks, kcPass.toCharArray(), pkStrategy);
            }

            if (ts != null) {
                logger.trace("Loading trust material for SSL context...");

                SiteTrustType trustType = location.getTrustType();
                if (trustType == null) {
                    trustType = SiteTrustType.DEFAULT;
                }

                sslBuilder.loadTrustMaterial(ts, trustType.getTrustStrategy());
            }

            SSLContext ctx = sslBuilder.build();

            fac = new SSLConnectionSocketFactory(ctx, new DefaultHostnameVerifier());
            location.setAttribute(SSL_FACTORY_ATTRIB, fac);
            return fac;
        } catch (final KeyManagementException e) {
            logger.error(
                    "Invalid configuration. Cannot initialize SSL socket factory for repository: {}. Error: {}",
                    e, location.getUri(), e.getMessage());
            throw new JHttpCException(
                    "Failed to initialize SSL connection for repository: " + location.getUri());
        } catch (final UnrecoverableKeyException e) {
            logger.error(
                    "Invalid configuration. Cannot initialize SSL socket factory for repository: {}. Error: {}",
                    e, location.getUri(), e.getMessage());
            throw new JHttpCException(
                    "Failed to initialize SSL connection for repository: " + location.getUri());
        } catch (final NoSuchAlgorithmException e) {
            logger.error(
                    "Invalid configuration. Cannot initialize SSL socket factory for repository: {}. Error: {}",
                    e, location.getUri(), e.getMessage());
            throw new JHttpCException(
                    "Failed to initialize SSL connection for repository: " + location.getUri());
        } catch (final KeyStoreException e) {
            logger.error(
                    "Invalid configuration. Cannot initialize SSL socket factory for repository: {}. Error: {}",
                    e, location.getUri(), e.getMessage());
            throw new JHttpCException(
                    "Failed to initialize SSL connection for repository: " + location.getUri());
        }
    } else {
        logger.debug("No SSL configuration present; no SSL context created.");
    }

    return null;
}

From source file:com.github.parisoft.resty.client.Client.java

private HttpClient newHttpClient() throws IOException {
    final SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(timeout).build();

    final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout)
            .setConnectTimeout(timeout).setSocketTimeout(timeout).setCookieSpec(CookieSpecs.DEFAULT).build();

    final SSLContext sslContext;
    final HostnameVerifier hostnameVerifier;

    if (bypassSSL) {
        hostnameVerifier = NoopHostnameVerifier.INSTANCE;

        try {/*  ww  w . j  av  a 2  s  .com*/
            sslContext = SSLContexts.custom().loadTrustMaterial(new BypassTrustStrategy())
                    .useProtocol(SSLConnectionSocketFactory.TLS).build();
        } catch (Exception e) {
            throw new IOException("Cannot create bypassed SSL context", e);
        }
    } else {
        sslContext = SSLContexts.createSystemDefault();
        hostnameVerifier = null;
    }

    final HttpRequestRetryHandler retryHandler = new RequestRetryHandler(retries);
    final HttpClientConnectionManager connectionManager = getConnectionManager();

    return HttpClientBuilder.create().setConnectionManager(connectionManager).setConnectionManagerShared(true)
            .setRetryHandler(retryHandler).setDefaultSocketConfig(socketConfig)
            .setDefaultRequestConfig(requestConfig).setSSLContext(sslContext)
            .setSSLHostnameVerifier(hostnameVerifier).build();
}