Search in sources :

Example 1 with SslConfigurator

use of org.glassfish.jersey.SslConfigurator in project jersey by jersey.

the class MainTest method _testWithoutSSLAuthentication.

/**
     * Test to see that SSLHandshakeException is thrown when client don't have
     * trusted key.
     */
private void _testWithoutSSLAuthentication(ClientConfig clientConfig) {
    SslConfigurator sslConfig = SslConfigurator.newInstance().trustStoreFile(TRUSTORE_CLIENT_FILE).trustStorePassword(TRUSTSTORE_CLIENT_PWD);
    Client client = ClientBuilder.newBuilder().withConfig(clientConfig).sslContext(sslConfig.createSSLContext()).build();
    System.out.println("Client: GET " + Server.BASE_URI);
    WebTarget target = client.target(Server.BASE_URI);
    target.register(LoggingFeature.class);
    boolean caught = false;
    try {
        target.path("/").request().get(String.class);
    } catch (Exception e) {
        caught = true;
    }
    assertTrue(caught);
// solaris throws java.net.SocketException instead of SSLHandshakeException
// assertTrue(msg.contains("SSLHandshakeException"));
}
Also used : WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) SslConfigurator(org.glassfish.jersey.SslConfigurator)

Example 2 with SslConfigurator

use of org.glassfish.jersey.SslConfigurator in project jersey by jersey.

the class JdkHttpsServerTest method getClientSslContext.

private SSLContext getClientSslContext() throws IOException {
    final InputStream trustStore = JdkHttpsServerTest.class.getResourceAsStream(TRUSTSTORE_CLIENT_FILE);
    final InputStream keyStore = JdkHttpsServerTest.class.getResourceAsStream(KEYSTORE_CLIENT_FILE);
    final SslConfigurator sslConfigClient = SslConfigurator.newInstance().trustStoreBytes(ByteStreams.toByteArray(trustStore)).trustStorePassword(TRUSTSTORE_CLIENT_PWD).keyStoreBytes(ByteStreams.toByteArray(keyStore)).keyPassword(KEYSTORE_CLIENT_PWD);
    return sslConfigClient.createSSLContext();
}
Also used : InputStream(java.io.InputStream) SslConfigurator(org.glassfish.jersey.SslConfigurator)

Example 3 with SslConfigurator

use of org.glassfish.jersey.SslConfigurator in project jersey by jersey.

the class JdkHttpsServerTest method getServerSslContext.

private SSLContext getServerSslContext() throws IOException {
    final InputStream trustStore = JdkHttpsServerTest.class.getResourceAsStream(TRUSTSTORE_SERVER_FILE);
    final InputStream keyStore = JdkHttpsServerTest.class.getResourceAsStream(KEYSTORE_SERVER_FILE);
    final SslConfigurator sslConfigServer = SslConfigurator.newInstance().keyStoreBytes(ByteStreams.toByteArray(keyStore)).keyPassword(KEYSTORE_SERVER_PWD).trustStoreBytes(ByteStreams.toByteArray(trustStore)).trustStorePassword(TRUSTSTORE_SERVER_PWD);
    return sslConfigServer.createSSLContext();
}
Also used : InputStream(java.io.InputStream) SslConfigurator(org.glassfish.jersey.SslConfigurator)

Example 4 with SslConfigurator

use of org.glassfish.jersey.SslConfigurator in project jersey by jersey.

the class SslFilterTest method openClientSocket.

/**
     * Creates an SSL client. Returns when SSL handshake has been completed.
     *
     * @param completionLatch latch that will be triggered when the expected number of bytes has been received.
     * @param readBuffer      buffer where received message will be written. Must be the size of the expected message,
     *                        because when it is filled {@code completionLatch} will be triggered.
     * @throws Throwable any exception that occurs until SSL handshake has completed.
     */
private Filter<ByteBuffer, ByteBuffer, ByteBuffer, ByteBuffer> openClientSocket(String host, final ByteBuffer readBuffer, final CountDownLatch completionLatch, HostnameVerifier customHostnameVerifier) throws Throwable {
    SslConfigurator sslConfig = SslConfigurator.newInstance().trustStoreFile(this.getClass().getResource("/truststore_client").getPath()).trustStorePassword("asdfgh").keyStoreFile(this.getClass().getResource("/keystore_client").getPath()).keyStorePassword("asdfgh");
    TransportFilter transportFilter = new TransportFilter(17_000, ThreadPoolConfig.defaultConfig(), 100_000);
    final SslFilter sslFilter = new SslFilter(transportFilter, sslConfig.createSSLContext(), host, customHostnameVerifier);
    // exceptions errors that occur before SSL handshake has finished are thrown from this method
    final AtomicReference<Throwable> exception = new AtomicReference<>();
    final CountDownLatch connectLatch = new CountDownLatch(1);
    final CountDownLatch startSslLatch = new CountDownLatch(1);
    Filter<ByteBuffer, ByteBuffer, ByteBuffer, ByteBuffer> clientSocket = new Filter<ByteBuffer, ByteBuffer, ByteBuffer, ByteBuffer>(sslFilter) {

        @Override
        void processConnect() {
            connectLatch.countDown();
        }

        @Override
        boolean processRead(ByteBuffer data) {
            readBuffer.put(data);
            if (!readBuffer.hasRemaining()) {
                completionLatch.countDown();
            }
            return false;
        }

        @Override
        void startSsl() {
            if (startSslLatch.getCount() == 1) {
                downstreamFilter.startSsl();
                try {
                    startSslLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                sslFilter.rehandshake();
            }
        }

        @Override
        void processSslHandshakeCompleted() {
            startSslLatch.countDown();
        }

        @Override
        void processError(Throwable t) {
            if (connectLatch.getCount() == 1 || startSslLatch.getCount() == 1) {
                exception.set(t);
                connectLatch.countDown();
                startSslLatch.countDown();
            }
        }

        @Override
        void write(ByteBuffer data, CompletionHandler<ByteBuffer> completionHandler) {
            downstreamFilter.write(data, completionHandler);
        }

        @Override
        void processConnectionClosed() {
            downstreamFilter.close();
        }

        @Override
        void close() {
            downstreamFilter.close();
        }
    };
    clientSocket.connect(new InetSocketAddress(host, PORT), null);
    try {
        connectLatch.await();
    } catch (InterruptedException ex) {
        ex.printStackTrace();
    }
    clientSocket.startSsl();
    if (exception.get() != null) {
        clientSocket.close();
        throw exception.get();
    }
    return clientSocket;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) SslConfigurator(org.glassfish.jersey.SslConfigurator)

Example 5 with SslConfigurator

use of org.glassfish.jersey.SslConfigurator in project jersey by jersey.

the class MainTest method _testWithoutBasicAuth.

/**
     * Test to see that HTTP 401 is returned when client tries to GET without
     * proper credentials.
     */
private void _testWithoutBasicAuth(ClientConfig clientConfig) {
    SslConfigurator sslConfig = SslConfigurator.newInstance().trustStoreFile(TRUSTORE_CLIENT_FILE).trustStorePassword(TRUSTSTORE_CLIENT_PWD).keyStoreFile(KEYSTORE_CLIENT_FILE).keyPassword(KEYSTORE_CLIENT_PWD);
    Client client = ClientBuilder.newBuilder().withConfig(clientConfig).sslContext(sslConfig.createSSLContext()).build();
    System.out.println("Client: GET " + Server.BASE_URI);
    WebTarget target = client.target(Server.BASE_URI);
    target.register(LoggingFeature.class);
    Response response;
    try {
        response = target.path("/").request().get(Response.class);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    assertEquals(401, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) SslConfigurator(org.glassfish.jersey.SslConfigurator)

Aggregations

SslConfigurator (org.glassfish.jersey.SslConfigurator)7 Client (javax.ws.rs.client.Client)3 WebTarget (javax.ws.rs.client.WebTarget)3 InputStream (java.io.InputStream)2 Response (javax.ws.rs.core.Response)2 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 SSLContext (javax.net.ssl.SSLContext)1