Example usage for javax.net.ssl SSLServerSocket getSupportedProtocols

List of usage examples for javax.net.ssl SSLServerSocket getSupportedProtocols

Introduction

In this page you can find the example usage for javax.net.ssl SSLServerSocket getSupportedProtocols.

Prototype

public abstract String[] getSupportedProtocols();

Source Link

Document

Returns the names of the protocols which could be enabled for use.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SSLServerSocketFactory factory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    SSLServerSocket serverSocket = (SSLServerSocket) factory.createServerSocket(8080);
    String[] suites = serverSocket.getSupportedCipherSuites();
    for (int i = 0; i < suites.length; i++) {
        System.out.println(suites[i]);
    }/*from  w  ww. ja  va2  s.  c o  m*/
    serverSocket.setEnabledCipherSuites(suites);
    String[] protocols = serverSocket.getSupportedProtocols();
    for (int i = 0; i < protocols.length; i++) {
        System.out.println(protocols[i]);
    }
    SSLSocket socket = (SSLSocket) serverSocket.accept();
    socket.startHandshake();
    System.out.println(socket.getRemoteSocketAddress());
}

From source file:MainClass.java

public static void main(String[] args) {
    int port = Integer.parseInt(args[0]);

    try {/* ww w  . j  av a 2 s  . c om*/
        System.out.println("Locating server socket factory for SSL...");
        SSLServerSocketFactory factory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();

        System.out.println("Creating a server socket on port " + port);
        SSLServerSocket serverSocket = (SSLServerSocket) factory.createServerSocket(port);

        String[] suites = serverSocket.getSupportedCipherSuites();
        System.out.println("Support cipher suites are:");
        for (int i = 0; i < suites.length; i++) {
            System.out.println(suites[i]);
        }
        serverSocket.setEnabledCipherSuites(suites);

        System.out.println("Support protocols are:");
        String[] protocols = serverSocket.getSupportedProtocols();
        for (int i = 0; i < protocols.length; i++) {
            System.out.println(protocols[i]);
        }

        System.out.println("Waiting for client...");
        SSLSocket socket = (SSLSocket) serverSocket.accept();

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

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

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 w  w w . j a  v  a  2s .  co m
 */
public static void setProtocolsAndCiphers(SSLServerSocket socket) {
    String[] p = selectProtocols(socket.getEnabledProtocols(), socket.getSupportedProtocols());
    for (int i = 0; i < p.length; i++) {
        // if we left SSLv3 in there, we don't support TLS,
        // so we should't remove the SSL ciphers
        if (p[i].equals("SSLv3"))
            return;
    }
    socket.setEnabledProtocols(p);
    socket.setEnabledCipherSuites(
            selectCipherSuites(socket.getEnabledCipherSuites(), socket.getSupportedCipherSuites()));
}

From source file:coyote.commons.network.http.SSLServerSocketFactoryTest.java

@Test
public void createPassesTheProtocolsToServerSocket() throws IOException {
    // first find the supported protocols
    SecureServerSocketFactory secureServerSocketFactory = new SecureServerSocketFactory(
            HTTPD.makeSSLSocketFactory("/keystore.jks", "password".toCharArray()), null);
    SSLServerSocket socket = (SSLServerSocket) secureServerSocketFactory.create();
    String[] protocols = socket.getSupportedProtocols();

    // remove one element from supported protocols
    if (protocols.length > 0) {
        protocols = Arrays.copyOfRange(protocols, 0, protocols.length - 1);
    }//from w  w  w. j  a  v a 2  s .c om

    // test
    secureServerSocketFactory = new SecureServerSocketFactory(
            HTTPD.makeSSLSocketFactory("/keystore.jks", "password".toCharArray()), protocols);
    socket = (SSLServerSocket) secureServerSocketFactory.create();
    Assert.assertArrayEquals("Enabled protocols specified in the factory were not set to the socket.",
            protocols, socket.getEnabledProtocols());
}

From source file:com.adito.server.jetty.CustomJsseListener.java

protected ServerSocket newServerSocket(InetAddrPort p_address, int p_acceptQueueSize) throws IOException {
    SSLServerSocket serverSocket = (SSLServerSocket) super.newServerSocket(p_address, p_acceptQueueSize);
    if (serverSocket.getNeedClientAuth()) {

        serverSocket.setNeedClientAuth(require);
        setNeedClientAuth(require);//from  w w w .ja  v a 2 s  .  c o  m
        if (!require)
            serverSocket.setWantClientAuth(true);
    }

    String[] ciphers = serverSocket.getSupportedCipherSuites();
    String[] protocols = serverSocket.getSupportedProtocols();

    if (log.isInfoEnabled()) {
        log.info("The following protocols are supported:");
        for (int i = 0; i < protocols.length; i++) {
            log.info("     " + protocols[i]);
        }
    }

    if (createAvailableCipherSuitesList) {
        File f = new File(ContextHolder.getContext().getTempDirectory(), "availableCipherSuites.txt");
        BufferedWriter writer = null;

        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f)));
            if (log.isInfoEnabled())
                log.info("The following cipher suites are supported:");
            for (int i = 0; i < ciphers.length; i++) {
                if (log.isInfoEnabled())
                    log.info("     " + ciphers[i]);
                writer.write(ciphers[i]);
                writer.newLine();
            }
        } catch (Throwable e) {
            log.error("Could not create cipher list!", e);
            configureContext = false;
        } finally {
            if (writer != null)
                writer.close();
        }
        createAvailableCipherSuitesList = false;
    }

    if (configureContext) {

        PropertyList list = ContextHolder.getContext().getConfig()
                .retrievePropertyList(new ContextKey("ssl.supportedProtocols"));

        if (!list.isEmpty()) {
            serverSocket.setEnabledProtocols(list.asArray());
        }

        list = ContextHolder.getContext().getConfig()
                .retrievePropertyList(new ContextKey("ssl.supportedCiphers"));

        if (!list.isEmpty()) {
            serverSocket.setEnabledCipherSuites(list.asArray());
        }
    }

    protocols = serverSocket.getEnabledProtocols();

    if (log.isInfoEnabled()) {
        log.info("The following protocols are enabled:");
        for (int i = 0; i < protocols.length; i++) {
            log.info("     " + protocols[i]);
        }
    }

    ciphers = serverSocket.getEnabledCipherSuites();
    if (log.isInfoEnabled()) {
        log.info("The following cipher suites are enabled:");
        for (int i = 0; i < ciphers.length; i++) {
            log.info("     " + ciphers[i]);
        }
    }

    return serverSocket;
}

From source file:org.nectarframework.base.service.nanohttp.NanoHttpService.java

/**
 * Creates an SSLSocketFactory for HTTPS. Pass a KeyStore resource with your
 * certificate and passphrase/*from ww w  . j ava  2 s  .  co  m*/
 */
public ServerSocket makeSSLServerSocket(String keyAndTrustStoreClasspathPath, char[] passphrase)
        throws IOException {
    try {
        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        InputStream keystoreStream = new FileInputStream(new File(keyAndTrustStoreClasspathPath));

        keystore.load(keystoreStream, passphrase);
        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keystore, passphrase);

        SSLServerSocketFactory res = null;
        try {
            TrustManagerFactory trustManagerFactory = TrustManagerFactory
                    .getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init(keystore);
            SSLContext ctx = SSLContext.getInstance("TLS");
            ctx.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
            res = ctx.getServerSocketFactory();

        } catch (Exception e) {
            throw new IOException(e.getMessage());
        }

        SSLServerSocket ss = null;
        ss = (SSLServerSocket) res.createServerSocket();
        ss.setEnabledProtocols(ss.getSupportedProtocols());
        ss.setUseClientMode(false);
        ss.setWantClientAuth(false);
        ss.setNeedClientAuth(false);

        return ss;

    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:org.glite.security.trustmanager.tomcat.TMSSLServerSocketFactory.java

/**
 * DOCUMENT ME!/*from   w  w w .  j  a v  a  2  s  . c  o  m*/
 *
 * @param socket DOCUMENT ME!
 * @param requestedProtocols DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 */
protected String[] getEnabledProtocols(SSLServerSocket socket, String requestedProtocols) {
    LOGGER.debug("TMSSLServerSocketFactory.getEnabledProtocols:");

    String[] supportedProtocols = socket.getSupportedProtocols();

    String[] enabledProtocols = null;

    if (requestedProtocols != null) {
        Vector vec = null;
        String protocol = requestedProtocols;
        int index = requestedProtocols.indexOf(',');

        if (index != -1) {
            int fromIndex = 0;

            while (index != -1) {
                protocol = requestedProtocols.substring(fromIndex, index).trim();

                if (protocol.length() > 0) {
                    /*
                     * Check to see if the requested protocol is among the
                     * supported protocols, i.e., may be enabled
                     */
                    for (int i = 0; (supportedProtocols != null) && (i < supportedProtocols.length); i++) {
                        if (supportedProtocols[i].equals(protocol)) {
                            if (vec == null) {
                                vec = new Vector();
                            }

                            vec.addElement(protocol);

                            break;
                        }
                    }
                }

                fromIndex = index + 1;
                index = requestedProtocols.indexOf(',', fromIndex);
            }
            // while

            protocol = requestedProtocols.substring(fromIndex);
        }

        if (protocol != null) {
            protocol = protocol.trim();

            if (protocol.length() > 0) {
                /*
                 * Check to see if the requested protocol is among the
                 * supported protocols, i.e., may be enabled
                 */
                for (int i = 0; (supportedProtocols != null) && (i < supportedProtocols.length); i++) {
                    if (supportedProtocols[i].equals(protocol)) {
                        if (vec == null) {
                            vec = new Vector();
                        }

                        vec.addElement(protocol);

                        break;
                    }
                }
            }
        }

        if (vec != null) {
            enabledProtocols = new String[vec.size()];
            vec.copyInto(enabledProtocols);
        }
    }

    return enabledProtocols;
}

From source file:org.jsslutils.extra.apachetomcat5.JSSLutilsJSSESocketFactory.java

/**
 * Determines the SSL protocol variants to be enabled.
 * //from   w w  w  .  ja va2s. c om
 * @param socket
 *            The socket to get supported list from.
 * @param requestedProtocols
 *            Comma-separated list of requested SSL protocol variants
 * 
 * @return Array of SSL protocol variants to be enabled, or null if none of
 *         the requested protocol variants are supported
 */
protected String[] getEnabledProtocols(SSLServerSocket socket, String requestedProtocols) {
    String[] supportedProtocols = socket.getSupportedProtocols();

    String[] enabledProtocols = null;

    if (requestedProtocols != null) {
        Vector<String> vec = null;
        String protocol = requestedProtocols;
        int index = requestedProtocols.indexOf(',');
        if (index != -1) {
            int fromIndex = 0;
            while (index != -1) {
                protocol = requestedProtocols.substring(fromIndex, index).trim();
                if (protocol.length() > 0) {
                    /*
                     * Check to see if the requested protocol is among the
                     * supported protocols, i.e., may be enabled
                     */
                    for (int i = 0; supportedProtocols != null && i < supportedProtocols.length; i++) {
                        if (supportedProtocols[i].equals(protocol)) {
                            if (vec == null) {
                                vec = new Vector<String>();
                            }
                            vec.addElement(protocol);
                            break;
                        }
                    }
                }
                fromIndex = index + 1;
                index = requestedProtocols.indexOf(',', fromIndex);
            } // while
            protocol = requestedProtocols.substring(fromIndex);
        }

        if (protocol != null) {
            protocol = protocol.trim();
            if (protocol.length() > 0) {
                /*
                 * Check to see if the requested protocol is among the
                 * supported protocols, i.e., may be enabled
                 */
                for (int i = 0; supportedProtocols != null && i < supportedProtocols.length; i++) {
                    if (supportedProtocols[i].equals(protocol)) {
                        if (vec == null) {
                            vec = new Vector<String>();
                        }
                        vec.addElement(protocol);
                        break;
                    }
                }
            }
        }

        if (vec != null) {
            enabledProtocols = new String[vec.size()];
            vec.copyInto(enabledProtocols);
        }
    }

    return enabledProtocols;
}