Java Code Examples for javax.net.ssl.SSLParameters#setApplicationProtocols()

The following examples show how to use javax.net.ssl.SSLParameters#setApplicationProtocols() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Http2TestServer.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
final ServerSocket initSecure(int port) throws Exception {
    ServerSocketFactory fac;
    if (sslContext != null) {
        fac = sslContext.getServerSocketFactory();
    } else {
        fac = SSLServerSocketFactory.getDefault();
    }
    SSLServerSocket se = (SSLServerSocket) fac.createServerSocket(port);
    SSLParameters sslp = se.getSSLParameters();
    sslp.setApplicationProtocols(new String[]{"h2"});
    se.setSSLParameters(sslp);
    se.setEnabledCipherSuites(se.getSupportedCipherSuites());
    se.setEnabledProtocols(se.getSupportedProtocols());
    // other initialisation here
    return se;
}
 
Example 2
Source File: Https.java    From PacketProxy with Apache License 2.0 5 votes vote down vote up
public static ServerSocket createServerSSLSocket(int listen_port, String commonName, CA ca, String ApplicationProtocol) throws Exception {
	SSLServerSocket serverSocket = (SSLServerSocket)createServerSSLSocket(listen_port, commonName, ca);
	SSLParameters sslp = serverSocket.getSSLParameters();
	String[] serverAPs ={ ApplicationProtocol };
	sslp.setApplicationProtocols(serverAPs);
	serverSocket.setSSLParameters(sslp);
	return serverSocket;
}
 
Example 3
Source File: Https.java    From PacketProxy with Apache License 2.0 5 votes vote down vote up
public static SSLSocket convertToServerSSLSocket(Socket socket, String commonName, CA ca, InputStream is) throws Exception {
	SSLContext sslContext = createSSLContext(commonName, ca);
	SSLSocketFactory ssf = sslContext.getSocketFactory();
	SSLSocket ssl_socket  = (SSLSocket)ssf.createSocket(socket, is, true);
	ssl_socket.setUseClientMode(false);

	SSLParameters sslp = ssl_socket.getSSLParameters();
	String[] serverAPs ={ "h2", "http/1.1", "http/1.0" };
	sslp.setApplicationProtocols(serverAPs);
	ssl_socket.setSSLParameters(sslp);

	ssl_socket.startHandshake();
	return ssl_socket;
}
 
Example 4
Source File: Https.java    From PacketProxy with Apache License 2.0 5 votes vote down vote up
public static SSLSocket convertToClientSSLSocket(Socket socket, String alpn) throws Exception {
	SSLSocketFactory ssf = createSSLSocketFactory();
	SSLSocket sock = (SSLSocket) ssf.createSocket(socket, null, socket.getPort(), false);
	SSLParameters sslp = sock.getSSLParameters();
	String[] clientAPs;
	if (alpn != null && alpn.length() > 0) {
		clientAPs = new String[]{ alpn };
	} else {
		clientAPs = new String[]{ "h2", "http/1.1", "http/1.0" };
	}
	sslp.setApplicationProtocols(clientAPs);
	sock.setSSLParameters(sslp);
	sock.startHandshake();
	return sock;
}
 
Example 5
Source File: Https.java    From PacketProxy with Apache License 2.0 5 votes vote down vote up
public static SSLSocket createClientSSLSocket(InetSocketAddress addr, String alpn) throws Exception {
	SSLSocketFactory ssf = createSSLSocketFactory();
	SSLSocket sock = (SSLSocket) ssf.createSocket(addr.getAddress(), addr.getPort());
	SSLParameters sslp = sock.getSSLParameters();
	String[] clientAPs;
	if (alpn != null && alpn.length() > 0) {
		clientAPs = new String[]{ alpn };
	} else {
		clientAPs = new String[]{ "h2", "http/1.1", "http/1.0" };
	}
	sslp.setApplicationProtocols(clientAPs);
	sock.setSSLParameters(sslp);
	sock.startHandshake();
	return sock;
}
 
Example 6
Source File: Https.java    From PacketProxy with Apache License 2.0 5 votes vote down vote up
public static SSLSocket createClientSSLSocket(InetSocketAddress addr, String SNIServerName, String alpn) throws Exception {
	/* SNI */
	SNIHostName serverName = new SNIHostName(SNIServerName);
	/* Fetch Client Certificate from ClientKeyManager */
	Server server = Servers.getInstance().queryByAddress(addr);
	clientKeyManagers = ClientKeyManager.getKeyManagers(server);

	SSLSocketFactory ssf = createSSLSocketFactory();
	SSLSocket sock = (SSLSocket) ssf.createSocket(addr.getAddress(), addr.getPort());
	SSLParameters sslp = sock.getSSLParameters();
	String[] clientAPs;
	if (alpn != null && alpn.length() > 0) {
		clientAPs = new String[]{ alpn };
	} else {
		clientAPs = new String[]{ "h2", "http/1.1", "http/1.0" };
	}
	sslp.setApplicationProtocols(clientAPs);

	sock.setSSLParameters(sslp);
	List<SNIServerName> serverNames = new ArrayList<>();
	serverNames.add(serverName);
	SSLParameters params = sock.getSSLParameters();
	params.setServerNames(serverNames);
	sock.setSSLParameters(params);
	sock.startHandshake();
	return sock;
}
 
Example 7
Source File: SSLServerSocketImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the SSLParameters in effect for newly accepted connections.
 */
@Override
synchronized public SSLParameters getSSLParameters() {
    SSLParameters params = super.getSSLParameters();

    // the super implementation does not handle the following parameters
    params.setEndpointIdentificationAlgorithm(identificationProtocol);
    params.setAlgorithmConstraints(algorithmConstraints);
    params.setSNIMatchers(sniMatchers);
    params.setUseCipherSuitesOrder(preferLocalCipherSuites);
    params.setApplicationProtocols(applicationProtocols);

    return params;
}
 
Example 8
Source File: SSLConfiguration.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
SSLParameters getSSLParameters() {
    SSLParameters params = new SSLParameters();

    params.setAlgorithmConstraints(this.algorithmConstraints);
    params.setProtocols(ProtocolVersion.toStringArray(enabledProtocols));
    params.setCipherSuites(CipherSuite.namesOf(enabledCipherSuites));
    switch (this.clientAuthType) {
        case CLIENT_AUTH_REQUIRED:
            params.setNeedClientAuth(true);
            break;
        case CLIENT_AUTH_REQUESTED:
            params.setWantClientAuth(true);
            break;
        default:
            params.setWantClientAuth(false);
    }
    params.setEndpointIdentificationAlgorithm(this.identificationProtocol);

    if (serverNames.isEmpty() && !noSniExtension) {
        // 'null' indicates none has been set
        params.setServerNames(null);
    } else {
        params.setServerNames(this.serverNames);
    }

    if (sniMatchers.isEmpty() && !noSniMatcher) {
        // 'null' indicates none has been set
        params.setSNIMatchers(null);
    } else {
        params.setSNIMatchers(this.sniMatchers);
    }

    params.setApplicationProtocols(this.applicationProtocols);
    params.setUseCipherSuitesOrder(this.preferLocalCipherSuites);
    params.setEnableRetransmissions(this.enableRetransmissions);
    params.setMaximumPacketSize(this.maximumPacketSize);

    return params;
}
 
Example 9
Source File: SSLServerSocketImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the SSLParameters in effect for newly accepted connections.
 */
@Override
public synchronized SSLParameters getSSLParameters() {
    SSLParameters params = super.getSSLParameters();

    // the super implementation does not handle the following parameters
    params.setEndpointIdentificationAlgorithm(identificationProtocol);
    params.setAlgorithmConstraints(algorithmConstraints);
    params.setSNIMatchers(sniMatchers);
    params.setUseCipherSuitesOrder(preferLocalCipherSuites);
    params.setApplicationProtocols(applicationProtocols);

    return params;
}
 
Example 10
Source File: SSLServerSocketImpl.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the SSLParameters in effect for newly accepted connections.
 */
@Override
synchronized public SSLParameters getSSLParameters() {
    SSLParameters params = super.getSSLParameters();

    // the super implementation does not handle the following parameters
    params.setEndpointIdentificationAlgorithm(identificationProtocol);
    params.setAlgorithmConstraints(algorithmConstraints);
    params.setSNIMatchers(sniMatchers);
    params.setUseCipherSuitesOrder(preferLocalCipherSuites);
    params.setApplicationProtocols(applicationProtocols);

    return params;
}
 
Example 11
Source File: Timeout.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void test(boolean async) throws Exception {
    System.setProperty("javax.net.ssl.keyStore", KEYSTORE);
    System.setProperty("javax.net.ssl.keyStorePassword", PASSWORD);
    System.setProperty("javax.net.ssl.trustStore", KEYSTORE);
    System.setProperty("javax.net.ssl.trustStorePassword", PASSWORD);

    SSLServerSocketFactory factory =
            (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();

    try (SSLServerSocket ssocket =
            (SSLServerSocket) factory.createServerSocket(RANDOM_PORT)) {

        // start server
        Thread server = new Thread(() -> {
            while (true) {
                System.out.println("server: ready");
                SSLParameters params = ssocket.getSSLParameters();
                params.setApplicationProtocols(new String[]{"h2"});
                ssocket.setSSLParameters(params);
                ready = true;
                try (SSLSocket socket = (SSLSocket) ssocket.accept()) {

                    // just read forever
                    System.out.println("server: accepted");
                    while (true) {
                        socket.getInputStream().read();
                    }
                } catch (IOException e) {
                    // ignore exceptions on server side
                    System.out.println("server: exception: " + e);
                }
            }
        });
        server.setDaemon(true);
        server.start();

        // wait for server is ready
        do {
            Thread.sleep(1000);
        } while (!ready);

        String uri = "https://localhost:" + ssocket.getLocalPort();
        if (async) {
            System.out.println(uri + ": Trying to connect asynchronously");
            connectAsync(uri);
        } else {
            System.out.println(uri + ": Trying to connect synchronously");
            connect(uri);
        }
    }
}
 
Example 12
Source File: ErrorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test() throws Exception {
    SSLContext sslContext = (new SimpleSSLContext()).get();
    ExecutorService exec = Executors.newCachedThreadPool();
    HttpClient client = HttpClient.newBuilder()
                                  .executor(exec)
                                  .sslContext(sslContext)
                                  .sslParameters(new SSLParameters(CIPHER_SUITES))
                                  .version(HTTP_2)
                                  .build();

    Http2TestServer httpsServer = null;
    try {
        SSLContext serverContext = (new SimpleSSLContext()).get();
        SSLParameters p = serverContext.getSupportedSSLParameters();
        p.setApplicationProtocols(new String[]{"h2"});
        httpsServer = new Http2TestServer(true,
                                          0,
                                          exec,
                                          serverContext);
        httpsServer.addHandler(new Http2EchoHandler(), "/");
        int httpsPort = httpsServer.getAddress().getPort();
        String httpsURIString = "https://127.0.0.1:" + httpsPort + "/bar/";

        httpsServer.start();
        URI uri = URI.create(httpsURIString);
        System.err.println("Request to " + uri);

        HttpRequest req = HttpRequest.newBuilder(uri)
                                .POST(fromString(SIMPLE_STRING))
                                .build();
        HttpResponse response;
        try {
            response = client.send(req, discard(null));
            throw new RuntimeException("Unexpected response: " + response);
        } catch (IOException e) {
            System.err.println("Caught Expected IOException: " + e);
        }
        System.err.println("DONE");
    } finally {
        if (httpsServer != null )  { httpsServer.stop(); }
        exec.shutdownNow();
    }
}