Example usage for javax.net.ssl SSLContext getSocketFactory

List of usage examples for javax.net.ssl SSLContext getSocketFactory

Introduction

In this page you can find the example usage for javax.net.ssl SSLContext getSocketFactory.

Prototype

public final SSLSocketFactory getSocketFactory() 

Source Link

Document

Returns a SocketFactory object for this context.

Usage

From source file:org.neo4j.harness.InProcessBuilderTest.java

private void trustAllSSLCerts() throws NoSuchAlgorithmException, KeyManagementException {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override//w  w  w. ja  v  a  2 s  .  co  m
        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
        }

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

    // Install the all-trusting trust manager
    SSLContext sc = SSLContext.getInstance("TLS");
    sc.init(null, trustAllCerts, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}

From source file:com.jwrapper.maven.java.JavaDownloadMojo.java

protected void setupNonVerifingSSL() throws Exception {

    // Create a trust manager that does not validate certificate chains
    final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override//  www . j  av  a 2 s .  com
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkClientTrusted(final X509Certificate[] arg0, final String arg1)
                throws CertificateException {
        }

        @Override
        public void checkServerTrusted(final X509Certificate[] arg0, final String arg1)
                throws CertificateException {
        }
    } };

    // Install the all-trusting trust manager
    final SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

    // Create all-trusting host name verifier
    final HostnameVerifier allHostsValid = new HostnameVerifier() {
        @Override
        public boolean verify(final String hostname, final SSLSession session) {
            return true;
        }
    };

    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

}

From source file:org.fabric3.admin.interpreter.communication.DomainConnectionImpl.java

private void setSocketFactory(HttpsURLConnection connection) throws CommunicationException {
    try {/*from   ww w .j a v  a  2s  . c  om*/
        if (sslFactory == null) {
            // initialize the SSL context
            String keyStoreLocation = getKeystoreLocation();
            if (keyStoreLocation == null) {
                throw new CommunicationException(
                        "Keystore not configured. A keystore must be placed in /config when using SSL.");
            }
            System.setProperty(KEY_STORE, keyStoreLocation);
            System.setProperty(TRUST_STORE, keyStoreLocation);
            KeyStore keyStore = KeyStore.getInstance("JKS");
            InputStream stream = new FileInputStream(keyStoreLocation);
            keyStore.load(stream, null);

            TrustManagerFactory tmf = TrustManagerFactory
                    .getInstance(TrustManagerFactory.getDefaultAlgorithm());
            tmf.init(keyStore);
            SSLContext ctx = SSLContext.getInstance("TLS");
            ctx.init(null, tmf.getTrustManagers(), null);
            sslFactory = ctx.getSocketFactory();
        }
        connection.setSSLSocketFactory(sslFactory);
    } catch (NoSuchAlgorithmException | CertificateException | KeyManagementException | KeyStoreException
            | IOException e) {
        throw new CommunicationException(e);
    }
}

From source file:org.jembi.rhea.rapidsms.GenerateORU_R01Alert.java

public void sendRequest(String msg)
        throws IOException, TransformerFactoryConfigurationError, TransformerException, KeyStoreException,
        NoSuchAlgorithmException, CertificateException, KeyManagementException {

    //log.info("Sending to RapidSMS:\n" + msg);

    // Get the key store that includes self-signed cert as a "trusted"
    // entry.//  w  w w  .  j a v a2s .c  o m
    InputStream keyStoreStream = org.mule.util.IOUtils.getResourceAsStream("truststore.jks",
            GenerateORU_R01Alert.class);

    // Load the keyStore

    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    keyStore.load(keyStoreStream, "Jembi#123".toCharArray());
    //log.info("KeyStoreStream = " + IOUtils.toString(keyStoreStream));
    keyStoreStream.close();

    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(keyStore);

    SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.init(null, tmf.getTrustManagers(), null);

    // set SSL Factory to be used for all HTTPS connections
    sslFactory = ctx.getSocketFactory();

    callQueryFacility(msg);

}

From source file:org.apache.ambari.server.controller.internal.URLStreamProvider.java

protected HttpsURLConnection getSSLConnection(String spec) throws IOException {

    if (sslSocketFactory == null) {
        synchronized (this) {
            if (sslSocketFactory == null) {
                try {
                    FileInputStream in = new FileInputStream(new File(path));
                    KeyStore store = KeyStore.getInstance(type == null ? KeyStore.getDefaultType() : type);

                    store.load(in, password.toCharArray());
                    in.close();/*from   w  w  w .j ava2 s.c om*/

                    TrustManagerFactory tmf = TrustManagerFactory
                            .getInstance(TrustManagerFactory.getDefaultAlgorithm());

                    tmf.init(store);
                    SSLContext context = SSLContext.getInstance("TLS");
                    context.init(null, tmf.getTrustManagers(), null);

                    sslSocketFactory = context.getSocketFactory();
                } catch (Exception e) {
                    throw new IOException("Can't get connection.", e);
                }
            }
        }
    }
    HttpsURLConnection connection = (HttpsURLConnection) (new URL(spec).openConnection());

    connection.setSSLSocketFactory(sslSocketFactory);

    return connection;
}

From source file:com.example.mp_master.helper.UntrustedSSLSocketFactory.java

/**
 * Creates the default SSL socket factory.
 * This constructor is used exclusively to instantiate the factory for
 * {@link #getSocketFactory getSocketFactory}.
 * @throws NoSuchAlgorithmException //from www  . j a  v  a  2  s  .  com
 * @throws KeyManagementException 
 */
private UntrustedSSLSocketFactory() {
    super();
    this.nameResolver = null;
    TrustManager[] blindTrustMan = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public void checkClientTrusted(X509Certificate[] c, String a) throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] c, String a) throws CertificateException {
        }
    } };
    SSLContext sl = null;
    SSLSocketFactory sslf = null;
    try {
        sl = SSLContext.getInstance(TLS);
        sl.init(null, blindTrustMan, new java.security.SecureRandom());
        sslf = sl.getSocketFactory();
    } catch (Exception e) {
        e.printStackTrace();
        sslf = HttpsURLConnection.getDefaultSSLSocketFactory();
    }

    this.sslcontext = sl;
    this.socketfactory = sslf;

}

From source file:com.amazon.alexa.avs.auth.companionservice.CompanionServiceClient.java

/**
 * Loads the CA certificate into an in-memory keystore and creates an {@link SSLSocketFactory}.
 *
 * @return SSLSocketFactory//from ww  w  .  java  2 s.c  o  m
 */
public SSLSocketFactory getPinnedSSLSocketFactory() {
    InputStream caCertInputStream = null;
    InputStream clientKeyPair = null;
    try {
        // Load the CA certificate into memory
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        caCertInputStream = new FileInputStream(deviceConfig.getCompanionServiceInfo().getSslCaCert());
        Certificate caCert = cf.generateCertificate(caCertInputStream);

        // Load the CA certificate into the trusted KeyStore
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        trustStore.setCertificateEntry("myca", caCert);

        // Create a TrustManagerFactory with the trusted KeyStore
        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);

        // Load the client certificate and private key into another KeyStore
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        clientKeyPair = new FileInputStream(deviceConfig.getCompanionServiceInfo().getSslClientKeyStore());
        keyStore.load(clientKeyPair,
                deviceConfig.getCompanionServiceInfo().getSslClientKeyStorePassphrase().toCharArray());

        // Create a TrustManagerFactory with the client key pair KeyStore
        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore,
                deviceConfig.getCompanionServiceInfo().getSslClientKeyStorePassphrase().toCharArray());

        // Initialize the SSLContext and return an SSLSocketFactory;
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);

        return sc.getSocketFactory();
    } catch (CertificateException | KeyStoreException | UnrecoverableKeyException | NoSuchAlgorithmException
            | IOException | KeyManagementException e) {
        throw new RuntimeException("The KeyStore for contacting the Companion Service could not be loaded.", e);
    } finally {
        IOUtils.closeQuietly(caCertInputStream);
        IOUtils.closeQuietly(clientKeyPair);
    }
}

From source file:com.budrotech.jukebox.service.ssl.SSLSocketFactory.java

/**
 * @since 4.1/*from w  ww .  j  a  v  a 2s .  co  m*/
 */
public SSLSocketFactory(final SSLContext sslContext, final X509HostnameVerifier hostnameVerifier) {
    super();
    this.socketFactory = sslContext.getSocketFactory();
    this.hostnameVerifier = hostnameVerifier;
    this.nameResolver = null;
}

From source file:com.github.reverseproxy.ReverseProxyJettyHandler.java

private SSLSocketFactory getSSLSocketFactory() throws NoSuchAlgorithmException, KeyManagementException {
    // Create a trust manager that does not validate certificate chains
    final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public void checkClientTrusted(final java.security.cert.X509Certificate[] chain,
                final String authType) {
        }// w  w w. j  a va 2s .  c o m

        public void checkServerTrusted(final java.security.cert.X509Certificate[] chain,
                final String authType) {
        }

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    } };

    // Install the all-trusting trust manager
    final SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

    // Create an ssl socket factory with our all-trusting manager
    final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
    return sslSocketFactory;
}

From source file:org.eclipse.mylyn.internal.commons.net.PollingSslProtocolSocketFactory.java

public synchronized SSLSocketFactory getSocketFactory() throws IOException {
    if (socketFactory == null) {
        if (keyStoreFileName != null && keyStorePassword != null) {
            KeyManager[] keymanagers = null;
            try {
                if (keyStoreType == null) {
                    try {
                        keymanagers = openKeyStore(KeyStore.getDefaultType());
                    } catch (Exception e) {
                        keymanagers = openKeyStore("pkcs12"); //$NON-NLS-1$
                    }/*from   ww w.  jav  a2s.  com*/
                } else {
                    keymanagers = openKeyStore(keyStoreType);
                }
            } catch (Exception cause) {
                IOException e = new SslCertificateException();
                e.initCause(cause);
                throw e;
            }

            try {
                SSLContext sslContext = SSLContext.getInstance("SSL"); //$NON-NLS-1$
                sslContext.init(keymanagers, new TrustManager[] { new TrustAllTrustManager() }, null);
                this.socketFactory = sslContext.getSocketFactory();
            } catch (Exception cause) {
                IOException e = new SslCertificateException();
                e.initCause(cause);
                throw e;
            }
        }
        throw new IOException("Could not initialize SSL context"); //$NON-NLS-1$
    }
    return socketFactory;
}