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:sample.tomcat.SslApplicationTests.java

private SSLSocketFactory secureSocketFactory() throws Exception {
    KeyStore truststore = KeyStore.getInstance(KeyStore.getDefaultType());
    truststore.load(getKeyStoreFile(), "password".toCharArray());
    // setup ssl context
    SSLContext ctx = SSLContexts.custom().loadTrustMaterial(truststore)
            .loadKeyMaterial(truststore, "password".toCharArray()).build();
    return ctx.getSocketFactory();
}

From source file:org.gluu.oxtrust.ldap.service.LinktrackService.java

public String newLink(@NotEmpty String login, @NotEmpty String password, @NotEmpty String link) {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }//  w  w  w.ja  va2 s.  c o m

        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }

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

    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
    }
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(String.format(CREATE_LINK_URL_PATTERN, login, password, link));
    HttpResponse response;
    try {
        response = httpclient.execute(httpget);
    } catch (Exception e) {
        log.error(String.format("Exception happened during linktrack link "
                + "creation with username: %s, password: %s," + " link: %s.", login, password, link), e);
        return null;
    }

    String trackedLink = null;
    if (response.getStatusLine().getStatusCode() == 201) {
        try {
            trackedLink = IOUtils.toString(response.getEntity().getContent());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return trackedLink;
}

From source file:at.gv.egiz.bku.spring.SSLSocketFactoryBean.java

@Override
public Object getObject() throws Exception {
    PKITrustManager pkiTrustManager = new PKITrustManager();
    pkiTrustManager.setConfiguration(configurationFacade.configuration);
    pkiTrustManager.setPkiProfile(pkiProfile);

    SSLContext sslContext = SSLContext.getInstance(configurationFacade.getSslProtocol());
    sslContext.init(null, new TrustManager[] { pkiTrustManager }, null);

    return sslContext.getSocketFactory();
}

From source file:edu.internet2.middleware.subject.provider.LdapPEMSocketFactory.java

protected void initSocketFactory() {
    try {/*from   w  ww  .  j a va 2 s. c  om*/
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(keyManagers, trustManagers, new java.security.SecureRandom());
        socketFactory = sc.getSocketFactory();
    } catch (Exception e) {
        log.error("ldap source initSF error: " + e);
    }
}

From source file:org.apache.juddi.v3.client.cryptor.TransportSecurityHelper.java

public static boolean applyTransportSecurity(BindingProvider webServicePort) {
    try {/*from   www .  j  ava  2  s.c o  m*/
        File currentdir = new File(".");
        String s = System.getProperty("javax.net.ssl.keyStore");
        String st = System.getProperty("javax.net.ssl.trustStore");
        log.info("Attempting to initialize keystore and truststore from " + s + " " + st);
        if (s == null) {
            log.warn("keystore isn't defined! " + s);
            return false;
        } else if (st == null) {
            log.warn("truststore isn't defined! " + s);
            return false;
        } else {
            File keystore = new File(s);
            if (keystore == null || !keystore.exists()) {
                log.warn("keystore doesn't exist! input was " + s + " working dir is "
                        + currentdir.getAbsolutePath());
                return false;
            }
            //File truststore =new File(System.getProperty("javax.net.ssl.trustStore"));
            String pwd = System.getProperty("javax.net.ssl.keyStorePassword");
            if (pwd == null) {
                log.warn("keystore password isn't defined!");
                return false;
            }

            File truststore = new File(st);
            if (truststore == null || !truststore.exists()) {
                log.warn("truststore doesn't exist! input was " + s + " working dir is "
                        + currentdir.getAbsolutePath());
                return false;
            }
            //File truststore =new File(System.getProperty("javax.net.ssl.trustStore"));
            String pwdt = System.getProperty("javax.net.ssl.trustStorePassword");
            if (pwdt == null) {
                log.warn("truststore password isn't defined!");
                return false;
            }

            if (keystore.exists()) {
                try {
                    log.info("Using keystore from " + keystore.getAbsolutePath() + " current dir is "
                            + currentdir.getAbsolutePath());

                    log.info("Using truststore from " + truststore.getAbsolutePath() + " current dir is "
                            + currentdir.getAbsolutePath());
                    //log.info("Using truststure from " + truststore.getAbsolutePath() + " current dir is " + currentdir.getAbsolutePath());
                    SSLContext sc = SSLContext.getInstance("SSLv3");

                    KeyManagerFactory kmf = KeyManagerFactory
                            .getInstance(KeyManagerFactory.getDefaultAlgorithm());

                    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
                    ks.load(new FileInputStream(keystore), pwd.toCharArray());

                    kmf.init(ks, pwd.toCharArray());

                    String alg = TrustManagerFactory.getDefaultAlgorithm();
                    TrustManagerFactory tmFact = TrustManagerFactory.getInstance(alg);

                    FileInputStream fis = new FileInputStream(st);
                    KeyStore kst = KeyStore.getInstance("jks");
                    kst.load(fis, pwdt.toCharArray());
                    fis.close();

                    tmFact.init(kst);

                    TrustManager[] tms = tmFact.getTrustManagers();

                    sc.init(kmf.getKeyManagers(), null, null);
                    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                    ((BindingProvider) webServicePort).getRequestContext().put(
                            "com.sun.xml.internal.ws.transport.https.client.SSLSocketFactory",
                            sc.getSocketFactory());
                    ((BindingProvider) webServicePort).getRequestContext().put(
                            "com.sun.xml.ws.transport.https.client.SSLSocketFactory", sc.getSocketFactory());
                    return true;
                } catch (Exception ex) {
                    log.warn("unable to establish ssl settings", ex);
                }
            }
        }
        return false;
    } catch (Exception x) {
        log.error("unexpected error", x);
    }
    return false;
}

From source file:org.kuali.mobility.push.factory.iOSFeedbackConnectionFactory.java

@Override
public SSLSocket makeObject() throws Exception {
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    keyStore.load(certPath.getInputStream(), certPassword.toCharArray());
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("sunx509");
    keyManagerFactory.init(keyStore, certPassword.toCharArray());
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("sunx509");
    trustManagerFactory.init(keyStore);//from w ww. j a v  a  2 s  .c o  m
    SSLContext sslCtx = SSLContext.getInstance("TLS");
    sslCtx.init(keyManagerFactory.getKeyManagers(), null, null);
    SSLSocketFactory sslSocketFactory = sslCtx.getSocketFactory();
    SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket(host, port);
    socket.startHandshake();
    return socket;
}

From source file:org.jivesoftware.community.util.ssl.DummySSLSocketFactory.java

public DummySSLSocketFactory() {
    try {//from  w ww  .j  av  a 2 s  . co  m
        SSLContext sslcontent = SSLContext.getInstance("SSL");
        sslcontent.init(null, new TrustManager[] { new DummyTrustManager() }, new SecureRandom());
        factory = sslcontent.getSocketFactory();
    } catch (NoSuchAlgorithmException e) {
        Log.error(e.toString());
    } catch (KeyManagementException e) {
        Log.error(e.toString());
    }
}

From source file:org.kuali.mobility.push.factory.iOSConnectionFactory.java

@Override
public SSLSocket makeObject() throws Exception {
    SSLSocket socket = null;/*from  w  w  w  . jav  a 2 s.  co m*/
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    keyStore.load(certPath.getInputStream(), certPassword.toCharArray());
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("sunx509");
    keyManagerFactory.init(keyStore, certPassword.toCharArray());
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("sunx509");
    trustManagerFactory.init(keyStore);
    SSLContext sslCtx = SSLContext.getInstance("TLS");
    sslCtx.init(keyManagerFactory.getKeyManagers(), null, null);
    SSLSocketFactory sslSocketFactory = sslCtx.getSocketFactory();
    socket = (SSLSocket) sslSocketFactory.createSocket(host, port);
    socket.startHandshake();
    return socket;
}

From source file:org.ksoap2.transport.ServiceConnectionSE.java

public ServiceConnectionSE(String url) throws IOException {
    // /*from   ww w  .  ja v a2 s  .c  om*/
    try {
        SSLContext sContext = SSLContext.getInstance("SSL");
        sContext.init(null, trustAllCerts, new java.security.SecureRandom());

        HttpsURLConnection.setDefaultSSLSocketFactory(sContext.getSocketFactory());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }

    connection = (HttpsURLConnection) new URL(url).openConnection();
    ((HttpsURLConnection) connection).setHostnameVerifier(new AllowAllHostnameVerifier());
    connection.setUseCaches(false);
    connection.setDoOutput(true);
    connection.setDoInput(true);
}

From source file:org.apache.ftpserver.ssl.Ssl.java

/**
 * Create a secure socket.//from  w w  w.j  a v  a 2  s .com
 */
public Socket createSocket(String protocol, InetAddress addr, int port, boolean clientMode) throws Exception {

    // get socket factory
    SSLContext ctx = getSSLContext(protocol);
    SSLSocketFactory socFactory = ctx.getSocketFactory();

    // create socket
    SSLSocket ssoc = (SSLSocket) socFactory.createSocket(addr, port);
    ssoc.setUseClientMode(clientMode);

    // initialize socket
    String cipherSuites[] = ssoc.getSupportedCipherSuites();
    ssoc.setEnabledCipherSuites(cipherSuites);
    return ssoc;
}