Search in sources :

Example 6 with SNIServerName

use of javax.net.ssl.SNIServerName in project jetty.project by eclipse.

the class SslConnectionFactoryTest method getResponse.

private String getResponse(String sniHost, String reqHost, String cn) throws Exception {
    SslContextFactory clientContextFactory = new SslContextFactory(true);
    clientContextFactory.start();
    SSLSocketFactory factory = clientContextFactory.getSslContext().getSocketFactory();
    SSLSocket sslSocket = (SSLSocket) factory.createSocket("127.0.0.1", _port);
    if (cn != null) {
        SNIHostName serverName = new SNIHostName(sniHost);
        List<SNIServerName> serverNames = new ArrayList<>();
        serverNames.add(serverName);
        SSLParameters params = sslSocket.getSSLParameters();
        params.setServerNames(serverNames);
        sslSocket.setSSLParameters(params);
    }
    sslSocket.startHandshake();
    if (cn != null) {
        X509Certificate cert = ((X509Certificate) sslSocket.getSession().getPeerCertificates()[0]);
        Assert.assertThat(cert.getSubjectX500Principal().getName("CANONICAL"), Matchers.startsWith("cn=" + cn));
    }
    sslSocket.getOutputStream().write(("GET /ctx/path HTTP/1.0\r\nHost: " + reqHost + ":" + _port + "\r\n\r\n").getBytes(StandardCharsets.ISO_8859_1));
    String response = IO.toString(sslSocket.getInputStream());
    sslSocket.close();
    clientContextFactory.stop();
    return response;
}
Also used : SNIServerName(javax.net.ssl.SNIServerName) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) SSLParameters(javax.net.ssl.SSLParameters) SNIHostName(javax.net.ssl.SNIHostName) SSLSocket(javax.net.ssl.SSLSocket) ArrayList(java.util.ArrayList) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) X509Certificate(java.security.cert.X509Certificate)

Example 7 with SNIServerName

use of javax.net.ssl.SNIServerName in project mongo-java-driver by mongodb.

the class Java8SniSslHelper method enableSni.

@Override
public void enableSni(final ServerAddress address, final SSLParameters sslParameters) {
    try {
        SNIServerName sniHostName = new SNIHostName(address.getHost());
        sslParameters.setServerNames(singletonList(sniHostName));
    } catch (IllegalArgumentException e) {
    // ignore because SNIHostName will throw this for some legit host names for connecting to MongoDB, e.g an IPV6 literal
    }
}
Also used : SNIServerName(javax.net.ssl.SNIServerName) SNIHostName(javax.net.ssl.SNIHostName)

Example 8 with SNIServerName

use of javax.net.ssl.SNIServerName in project jdk8u_jdk by JetBrains.

the class SSLEchoServer method init.

static SSLClient init(String host, int port, String cipherSuiteFilter, String sniHostName) throws NoSuchAlgorithmException, IOException {
    SSLContext sslContext = SSLContext.getDefault();
    SSLSocketFactory ssf = (SSLSocketFactory) sslContext.getSocketFactory();
    SSLSocket socket = (SSLSocket) ssf.createSocket(host, port);
    SSLParameters params = new SSLParameters();
    if (cipherSuiteFilter != null) {
        String[] cipherSuites = UnboundSSLUtils.filterStringArray(ssf.getSupportedCipherSuites(), cipherSuiteFilter);
        System.out.println("Client: enabled cipher suites: " + Arrays.toString(cipherSuites));
        params.setCipherSuites(cipherSuites);
    }
    if (sniHostName != null) {
        System.out.println("Client: set SNI hostname: " + sniHostName);
        SNIHostName serverName = new SNIHostName(sniHostName);
        List<SNIServerName> serverNames = new ArrayList<>();
        serverNames.add(serverName);
        params.setServerNames(serverNames);
    }
    socket.setSSLParameters(params);
    return new SSLClient(socket);
}
Also used : SNIServerName(javax.net.ssl.SNIServerName) SSLParameters(javax.net.ssl.SSLParameters) SNIHostName(javax.net.ssl.SNIHostName) SSLSocket(javax.net.ssl.SSLSocket) ArrayList(java.util.ArrayList) SSLContext(javax.net.ssl.SSLContext) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Aggregations

SNIServerName (javax.net.ssl.SNIServerName)8 SNIHostName (javax.net.ssl.SNIHostName)6 ArrayList (java.util.ArrayList)5 SSLParameters (javax.net.ssl.SSLParameters)3 SSLSocket (javax.net.ssl.SSLSocket)3 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)3 X509Certificate (java.security.cert.X509Certificate)2 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)2 SNIMatcher (javax.net.ssl.SNIMatcher)1 SSLContext (javax.net.ssl.SSLContext)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1