Example usage for javax.net.ssl SSLSocket setEnabledCipherSuites

List of usage examples for javax.net.ssl SSLSocket setEnabledCipherSuites

Introduction

In this page you can find the example usage for javax.net.ssl SSLSocket setEnabledCipherSuites.

Prototype

public abstract void setEnabledCipherSuites(String suites[]);

Source Link

Document

Sets the cipher suites enabled for use on this connection.

Usage

From source file:MyHandshakeListener.java

public static void main(String[] args) throws Exception {
    SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket socket = (SSLSocket) factory.createSocket("127.0.0.1", 8080);
    String[] suites = socket.getSupportedCipherSuites();
    socket.setEnabledCipherSuites(suites);
    socket.addHandshakeCompletedListener(new MyHandshakeListener());
    socket.startHandshake();// ww w . ja  v  a 2 s . c o m
    System.out.println("Just connected to " + socket.getRemoteSocketAddress());
}

From source file:MainClass.java

public static void main(String[] args) {
    String host = args[0];//from   w  w w.j a  v a2s  .  co m
    int port = Integer.parseInt(args[1]);

    try {
        System.out.println("Locating socket factory for SSL...");
        SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();

        System.out.println("Creating secure socket to " + host + ":" + port);
        SSLSocket socket = (SSLSocket) factory.createSocket(host, port);

        System.out.println("Enabling all available cipher suites...");
        String[] suites = socket.getSupportedCipherSuites();
        socket.setEnabledCipherSuites(suites);

        System.out.println("Registering a handshake listener...");
        socket.addHandshakeCompletedListener(new MyHandshakeListener());

        System.out.println("Starting handshaking...");
        socket.startHandshake();

        System.out.println("Just connected to " + socket.getRemoteSocketAddress());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.tc.simple.apn.quicktests.Test.java

/**
 * @param args/*from   w  ww  .  j  ava 2  s  . c  o  m*/
 */

public static void main(String[] args) {
    SSLSocket socket = null;

    try {
        String host = "gateway.sandbox.push.apple.com";
        int port = 2195;

        String token = "de7f197546e41a76684f8e2d89f397ed165298d7772f4bd9b0f39c674b185b0f";
        System.out.println(token.toCharArray().length);

        //String token = "8cebc7c08f79fa62f0994eb4298387ff930857ff8d14a50de431559cf476b223";

        KeyStore keyStore = KeyStore.getInstance("PKCS12");

        keyStore.load(Test.class.getResourceAsStream("egram-dev-apn.p12"), "xxxxxxxxx".toCharArray());
        KeyManagerFactory keyMgrFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyMgrFactory.init(keyStore, "xxxxxxxxx".toCharArray());

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyMgrFactory.getKeyManagers(), null, null);
        SSLSocketFactory socketFactory = sslContext.getSocketFactory();

        socket = (SSLSocket) socketFactory.createSocket(host, port);
        String[] cipherSuites = socket.getSupportedCipherSuites();
        socket.setEnabledCipherSuites(cipherSuites);
        socket.startHandshake();

        char[] t = token.toCharArray();
        byte[] b = Hex.decodeHex(t);

        OutputStream outputstream = socket.getOutputStream();

        String payload = "{\"aps\":{\"alert\":\"yabadabadooo\"}}";

        int expiry = (int) ((System.currentTimeMillis() / 1000L) + 7200);

        ByteArrayOutputStream bout = new ByteArrayOutputStream();

        DataOutputStream dos = new DataOutputStream(bout);

        //command
        dos.writeByte(1);

        //id
        dos.writeInt(900);

        //expiry
        dos.writeInt(expiry);

        //token length.
        dos.writeShort(b.length);

        //token
        dos.write(b);

        //payload length
        dos.writeShort(payload.length());

        //payload.
        dos.write(payload.getBytes());

        byte[] byteMe = bout.toByteArray();

        socket.getOutputStream().write(byteMe);

        socket.setSoTimeout(900);
        InputStream in = socket.getInputStream();

        System.out.println(APNErrors.getError(in.read()));

        in.close();

        outputstream.close();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

From source file:proxy.ElementalHttpGet.java

private static void request(HttpProcessor httpproc, HttpRequestExecutor httpexecutor,
        HttpCoreContext coreContext, HttpHost host, InetAddress localinetAddress)
        throws NoSuchAlgorithmException, IOException, HttpException {
    DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(8 * 1024);
    ConnectionReuseStrategy connStrategy = DefaultConnectionReuseStrategy.INSTANCE;
    try {/* w w  w  .j a  v a 2  s  .com*/

        String[] targets = { "/2/users/show.json?access_token=2.00SlDQsDdcZIJC94e5308f67sRL13D&uid=3550148352",
                "/account/rate_limit_status.json?access_token=2.00SlDQsDdcZIJC94e5308f67sRL13D" };

        for (int i = 0; i < targets.length; i++) {
            if (!conn.isOpen()) {
                SSLContext sslcontext = SSLContext.getInstance("Default");
                //               sslcontext.init(null, null, null);
                SocketFactory sf = sslcontext.getSocketFactory();
                SSLSocket socket = (SSLSocket) sf.createSocket(host.getHostName(), host.getPort(),
                        localinetAddress, 0);
                socket.setEnabledCipherSuites(new String[] { "TLS_RSA_WITH_AES_256_CBC_SHA",
                        "TLS_DHE_RSA_WITH_AES_256_CBC_SHA", "TLS_DHE_DSS_WITH_AES_256_CBC_SHA" });
                conn.bind(socket);
                //               Socket socket = new Socket(host.getHostName(), host.getPort());
                //               conn.bind(socket);
            }
            BasicHttpRequest request = new BasicHttpRequest("GET", targets[i]);
            System.out.println(">> Request URI: " + request.getRequestLine().getUri());

            httpexecutor.preProcess(request, httpproc, coreContext);
            HttpResponse response = httpexecutor.execute(request, conn, coreContext);
            httpexecutor.postProcess(response, httpproc, coreContext);

            System.out.println("<< Response: " + response.getStatusLine());
            System.out.println(EntityUtils.toString(response.getEntity()));
            System.out.println("==============");
            if (!connStrategy.keepAlive(response, coreContext)) {
                conn.close();
            } else {
                System.out.println("Connection kept alive...");
            }
        }
    } finally {
        conn.close();
    }
}

From source file:net.i2p.util.I2PSSLSocketFactory.java

/**
 * Select protocols and cipher suites to be used
 * based on configured inclusion and exclusion lists
 * as well as enabled and supported protocols and cipher suites.
 *
 * Adapted from Jetty SslContextFactory.java
 *
 * @since 0.9.16/*from   ww  w .ja  va 2 s.com*/
 */
public static void setProtocolsAndCiphers(SSLSocket socket) {
    socket.setEnabledProtocols(selectProtocols(socket.getEnabledProtocols(), socket.getSupportedProtocols()));
    socket.setEnabledCipherSuites(
            selectCipherSuites(socket.getEnabledCipherSuites(), socket.getSupportedCipherSuites()));
}

From source file:edu.cmu.cylab.starslinger.exchange.CheckedSSLSocketFactory.java

public static SSLSocket limitEnabledCipherSuites(SSLSocket sslEngine) {
    String[] supportedCipherSuites = sslEngine.getSupportedCipherSuites();
    List<String> favoredCipherSuites = new ArrayList<String>();

    /*//ww  w  . j a v a  2 s  .c  om
     * We don't want anonymous Diffie Hellman and no DES or 40 or 56 bit
     * keys and no null-md5 or null-sha.
     */
    String[] unwantedCipherSuites = new String[] { "_dh_anon", "_des", "_40", "_56", "_null_md5", "_null_sha" };
    for (String cs : supportedCipherSuites) {
        boolean isCSok = true;
        for (String ucs : unwantedCipherSuites)
            if (cs.toLowerCase(Locale.US).contains(ucs))
                isCSok = false;

        if (isCSok)
            favoredCipherSuites.add(cs);
    }

    sslEngine.setEnabledCipherSuites(favoredCipherSuites.toArray(new String[favoredCipherSuites.size()]));
    return sslEngine;
}

From source file:com.eviware.soapui.impl.wsdl.support.http.SoapUISSLSocketFactory.java

private static SSLSocket enableSocket(SSLSocket socket) {
    String invalidateSession = System.getProperty("soapui.https.session.invalidate");
    String protocols = System.getProperty("soapui.https.protocols");
    String ciphers = System.getProperty("soapui.https.ciphers");

    if (StringUtils.hasContent(invalidateSession)) {
        socket.getSession().invalidate();
    }/*from w w w.j av a  2  s  .  c o m*/

    if (StringUtils.hasContent(protocols)) {
        socket.setEnabledProtocols(protocols.split(","));
    }
    //      else if( socket.getSupportedProtocols() != null )
    //      {
    //         socket.setEnabledProtocols( socket.getSupportedProtocols() );
    //      }

    if (StringUtils.hasContent(ciphers)) {
        socket.setEnabledCipherSuites(ciphers.split(","));
    }
    //      else if( socket.getSupportedCipherSuites() != null )
    //      {
    //         socket.setEnabledCipherSuites(  socket.getSupportedCipherSuites()  );
    //      }

    return socket;
}

From source file:SocketFetcher.java

/**
 * Configure the SSL options for the socket (if it's an SSL socket), based on
 * the mail.<protocol>.ssl.protocols and mail.<protocol>.ssl.ciphersuites
 * properties.//from  www . jav a 2s  .com
 */
private static void configureSSLSocket(Socket socket, Properties props, String prefix) {
    if (!(socket instanceof SSLSocket))
        return;
    SSLSocket sslsocket = (SSLSocket) socket;

    String protocols = props.getProperty(prefix + ".ssl.protocols", null);
    // if (protocols != null)
    // sslsocket.setEnabledProtocols(stringArray(protocols));
    // else {
    // /*
    // * At least the UW IMAP server insists on only the TLSv1
    // * protocol for STARTTLS, and won't accept the old SSLv2
    // * or SSLv3 protocols. Here we enable only the TLSv1
    // * protocol. XXX - this should probably be parameterized.
    // */
    // sslsocket.setEnabledProtocols(new String[] {"TLSv1"});
    // }
    String ciphers = props.getProperty(prefix + ".ssl.ciphersuites", null);
    if (ciphers != null)
        sslsocket.setEnabledCipherSuites(stringArray(ciphers));
    /*
     * System.out.println("SSL protocols after " +
     * Arrays.asList(sslsocket.getEnabledProtocols())); System.out.println("SSL
     * ciphers after " + Arrays.asList(sslsocket.getEnabledCipherSuites()));
     */
}

From source file:SocketFetcher.java

/**
 * Configure the SSL options for the socket (if it's an SSL socket),
 * based on the mail.<protocol>.ssl.protocols and
 * mail.<protocol>.ssl.ciphersuites properties.
 *///from   ww  w .j a va 2 s.co m
private static void configureSSLSocket(Socket socket, Properties props, String prefix) {
    if (!(socket instanceof SSLSocket))
        return;
    SSLSocket sslsocket = (SSLSocket) socket;

    String protocols = props.getProperty(prefix + ".ssl.protocols", null);
    if (protocols != null)
        sslsocket.setEnabledProtocols(stringArray(protocols));
    else {
        /*
         * At least the UW IMAP server insists on only the TLSv1
         * protocol for STARTTLS, and won't accept the old SSLv2
         * or SSLv3 protocols.  Here we enable only the TLSv1
         * protocol.  XXX - this should probably be parameterized.
         */
        sslsocket.setEnabledProtocols(new String[] { "TLSv1" });
    }
    String ciphers = props.getProperty(prefix + ".ssl.ciphersuites", null);
    if (ciphers != null)
        sslsocket.setEnabledCipherSuites(stringArray(ciphers));
    /*
    System.out.println("SSL protocols after " +
        Arrays.asList(sslsocket.getEnabledProtocols()));
    System.out.println("SSL ciphers after " +
        Arrays.asList(sslsocket.getEnabledCipherSuites()));
    */
}

From source file:eu.prestoprime.p4gui.connection.P4HttpClient.java

public P4HttpClient(String userID) {
    HttpParams params = new BasicHttpParams();

    // setup SSL//from w  ww .  ja  v a 2s . c o  m
    try {
        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, new TrustManager[] { easyTrustManager }, null);

        SSLSocketFactory sf = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000L);
        SSLSocket socket = (SSLSocket) sf.createSocket(params);
        socket.setEnabledCipherSuites(new String[] { "SSL_RSA_WITH_RC4_128_MD5" });

        Scheme sch = new Scheme("https", 443, sf);
        this.getConnectionManager().getSchemeRegistry().register(sch);
    } catch (IOException | KeyManagementException | NoSuchAlgorithmException e) {
        logger.error("Unable to create SSL handler for HttpClient...");
        e.printStackTrace();
    }

    // save userID
    this.userID = userID;
}