Example usage for javax.net.ssl SSLEngine setNeedClientAuth

List of usage examples for javax.net.ssl SSLEngine setNeedClientAuth

Introduction

In this page you can find the example usage for javax.net.ssl SSLEngine setNeedClientAuth.

Prototype

public abstract void setNeedClientAuth(boolean need);

Source Link

Document

Configures the engine to require client authentication.

Usage

From source file:org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayerTest.java

@Theory
public void clientRejectsServer(NetworkLayerFactory serverFactory, NetworkLayerFactory clientFactory)
        throws Exception {
    SSLEngine serverEngine = serverCtx.createSSLEngine();
    serverEngine.setUseClientMode(false);
    serverEngine.setNeedClientAuth(true);
    SSLEngine clientEngine = clientCtx.createSSLEngine();
    clientEngine.setUseClientMode(true);

    ProtocolStack<IOBufferMatcher> client = ProtocolStack
            .on(clientFactory.create(selector.hub(), serverToClient.source(), clientToServer.sink()))
            .filter(new SSLEngineFilterLayer(clientEngine, new SSLEngineFilterLayer.Listener() {
                @Override//from  w  ww  . j a va2  s.  co  m
                public void onHandshakeCompleted(SSLSession session) throws ConnectionRefusalException {
                    throw new ConnectionRefusalException("Bad server");
                }
            })).build(new IOBufferMatcherLayer());

    ProtocolStack<IOBufferMatcher> server = ProtocolStack
            .on(serverFactory.create(selector.hub(), clientToServer.source(), serverToClient.sink()))
            .filter(new SSLEngineFilterLayer(serverEngine, null)).build(new IOBufferMatcherLayer());

    IOBufferMatcher clientMatcher = client.get();
    IOBufferMatcher serverMatcher = server.get();

    clientMatcher.awaitClose();
    serverMatcher.awaitClose();
    assertThat(clientMatcher.getCloseCause(), instanceOf(ConnectionRefusalException.class));
    assertThat(serverMatcher.getCloseCause(), instanceOf(ClosedChannelException.class));
}

From source file:org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayerTest.java

@Theory
public void serverRejectsClient(NetworkLayerFactory serverFactory, NetworkLayerFactory clientFactory)
        throws Exception {
    Logger.getLogger(name.getMethodName()).log(Level.INFO, "Starting test with server {0} client {1}",
            new Object[] { serverFactory.getClass().getSimpleName(),
                    clientFactory.getClass().getSimpleName(), });
    SSLEngine serverEngine = serverCtx.createSSLEngine();
    serverEngine.setUseClientMode(false);
    serverEngine.setNeedClientAuth(true);
    SSLEngine clientEngine = clientCtx.createSSLEngine();
    clientEngine.setUseClientMode(true);

    ProtocolStack<IOBufferMatcher> client = ProtocolStack
            .on(clientFactory.create(selector.hub(), serverToClient.source(), clientToServer.sink()))
            .filter(new SSLEngineFilterLayer(clientEngine, null)).build(new IOBufferMatcherLayer());

    ProtocolStack<IOBufferMatcher> server = ProtocolStack
            .on(serverFactory.create(selector.hub(), clientToServer.source(), serverToClient.sink()))
            .filter(new SSLEngineFilterLayer(serverEngine, new SSLEngineFilterLayer.Listener() {
                @Override//from   w w  w.  j a  v  a  2 s  .c  o  m
                public void onHandshakeCompleted(SSLSession session) throws ConnectionRefusalException {
                    throw new ConnectionRefusalException("Bad client");
                }
            })).build(new IOBufferMatcherLayer());

    IOBufferMatcher clientMatcher = client.get();
    IOBufferMatcher serverMatcher = server.get();

    Logger.getLogger(name.getMethodName()).log(Level.INFO, "Waiting for client close");
    clientMatcher.awaitClose();
    Logger.getLogger(name.getMethodName()).log(Level.INFO, "Waiting for server close");
    serverMatcher.awaitClose();
    assertThat(clientMatcher.getCloseCause(), instanceOf(ClosedChannelException.class));
    assertThat(serverMatcher.getCloseCause(), instanceOf(ConnectionRefusalException.class));
    Logger.getLogger(name.getMethodName()).log(Level.INFO, "Done");
}

From source file:org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayerTest.java

@Theory
public void untrustingClientDoesNotConnect(NetworkLayerFactory serverFactory, NetworkLayerFactory clientFactory)
        throws Exception {
    SSLEngine serverEngine = serverCtx.createSSLEngine();
    serverEngine.setUseClientMode(false);
    serverEngine.setNeedClientAuth(true);
    SSLEngine clientEngine = untrustingClientCtx.createSSLEngine();
    clientEngine.setUseClientMode(true);

    ProtocolStack<IOBufferMatcher> client = ProtocolStack
            .on(clientFactory.create(selector.hub(), serverToClient.source(), clientToServer.sink()))
            .filter(new SSLEngineFilterLayer(clientEngine, null)).build(new IOBufferMatcherLayer());

    ProtocolStack<IOBufferMatcher> server = ProtocolStack
            .on(serverFactory.create(selector.hub(), clientToServer.source(), serverToClient.sink()))
            .filter(new SSLEngineFilterLayer(serverEngine, null)).build(new IOBufferMatcherLayer());

    IOBufferMatcher clientMatcher = client.get();
    IOBufferMatcher serverMatcher = server.get();

    clientMatcher.awaitClose();//from  www . j a v  a  2 s.com
    serverMatcher.awaitClose();
    assertThat(clientMatcher.getCloseCause(), instanceOf(SSLHandshakeException.class));
    assertThat(serverMatcher.getCloseCause(), instanceOf(ClosedChannelException.class));
}

From source file:org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayerTest.java

@Theory
public void expiredClientDoesNotConnect(NetworkLayerFactory serverFactory, NetworkLayerFactory clientFactory)
        throws Exception {
    SSLEngine serverEngine = serverCtx.createSSLEngine();
    serverEngine.setUseClientMode(false);
    serverEngine.setNeedClientAuth(true);
    SSLEngine clientEngine = expiredClientCtx.createSSLEngine();
    clientEngine.setUseClientMode(true);

    ProtocolStack<IOBufferMatcher> client = ProtocolStack
            .on(clientFactory.create(selector.hub(), serverToClient.source(), clientToServer.sink()))
            .filter(new SSLEngineFilterLayer(clientEngine, null)).build(new IOBufferMatcherLayer());

    ProtocolStack<IOBufferMatcher> server = ProtocolStack
            .on(serverFactory.create(selector.hub(), clientToServer.source(), serverToClient.sink()))
            .filter(new SSLEngineFilterLayer(serverEngine, null)).build(new IOBufferMatcherLayer());

    IOBufferMatcher clientMatcher = client.get();
    IOBufferMatcher serverMatcher = server.get();

    clientMatcher.awaitClose();/*from w w w . j ava2  s  .c  o  m*/
    serverMatcher.awaitClose();
    assertThat(clientMatcher.getCloseCause(), instanceOf(ClosedChannelException.class));
    assertThat(serverMatcher.getCloseCause(), instanceOf(SSLHandshakeException.class));
}

From source file:org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayerTest.java

@Theory
public void clientDoesNotConnectToNotYetValidServer(NetworkLayerFactory serverFactory,
        NetworkLayerFactory clientFactory) throws Exception {
    SSLEngine serverEngine = notYetValidServerCtx.createSSLEngine();
    serverEngine.setUseClientMode(false);
    serverEngine.setNeedClientAuth(true);
    SSLEngine clientEngine = expiredClientCtx.createSSLEngine();
    clientEngine.setUseClientMode(true);

    ProtocolStack<IOBufferMatcher> client = ProtocolStack
            .on(clientFactory.create(selector.hub(), serverToClient.source(), clientToServer.sink()))
            .filter(new SSLEngineFilterLayer(clientEngine, null)).build(new IOBufferMatcherLayer());

    ProtocolStack<IOBufferMatcher> server = ProtocolStack
            .on(serverFactory.create(selector.hub(), clientToServer.source(), serverToClient.sink()))
            .filter(new SSLEngineFilterLayer(serverEngine, null)).build(new IOBufferMatcherLayer());

    IOBufferMatcher clientMatcher = client.get();
    IOBufferMatcher serverMatcher = server.get();

    clientMatcher.awaitClose();/* ww w. j  a va2  s . co  m*/
    serverMatcher.awaitClose();
    assertThat(clientMatcher.getCloseCause(), instanceOf(SSLHandshakeException.class));
    assertThat(serverMatcher.getCloseCause(), instanceOf(ClosedChannelException.class));
}

From source file:org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayerTest.java

private void concurrentStress(NetworkLayerFactory serverFactory, NetworkLayerFactory clientFactory,
        int serverLimit, int clientLimit) throws java.io.IOException, InterruptedException,
        java.util.concurrent.ExecutionException, TimeoutException {
    Logger.getLogger(name.getMethodName()).log(Level.INFO,
            "Starting test with server {0} client {1} serverLimit {2} clientLimit {3}",
            new Object[] { serverFactory.getClass().getSimpleName(), clientFactory.getClass().getSimpleName(),
                    serverLimit, clientLimit });
    SSLEngine serverEngine = serverCtx.createSSLEngine();
    serverEngine.setUseClientMode(false);
    serverEngine.setNeedClientAuth(true);
    SSLEngine clientEngine = clientCtx.createSSLEngine();
    clientEngine.setUseClientMode(true);

    ProtocolStack<IOBufferMatcher> clientStack = ProtocolStack
            .on(clientFactory.create(selector.hub(), serverToClient.source(), clientToServer.sink()))
            .filter(new SSLEngineFilterLayer(clientEngine, null)).build(new IOBufferMatcherLayer());

    ProtocolStack<IOBufferMatcher> serverStack = ProtocolStack
            .on(serverFactory.create(selector.hub(), clientToServer.source(), serverToClient.sink()))
            .filter(new SSLEngineFilterLayer(serverEngine, null)).build(new IOBufferMatcherLayer());

    final IOBufferMatcher client = clientStack.get();
    final IOBufferMatcher server = serverStack.get();
    Future<Void> clientWork = selector.executorService().submit(new SequentialSender(client, clientLimit, 11));
    Future<Void> serverWork = selector.executorService().submit(new SequentialSender(server, serverLimit, 13));

    clientWork.get();//from   www  . j  a  v a 2  s . c o m
    serverWork.get();

    client.awaitByteContent(SequentialSender.matcher(serverLimit));
    server.awaitByteContent(SequentialSender.matcher(clientLimit));

    client.close();
    server.close();

    client.awaitClose();
    server.awaitClose();

    assertThat(client.asByteArray(), SequentialSender.matcher(serverLimit));
    assertThat(server.asByteArray(), SequentialSender.matcher(clientLimit));
}

From source file:org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayerTest.java

@Theory
public void sendingBiggerAndBiggerBatches(NetworkLayerFactory serverFactory, NetworkLayerFactory clientFactory,
        BatchSendBufferingFilterLayer batch)
        throws java.io.IOException, InterruptedException, java.util.concurrent.ExecutionException {
    Logger.getLogger(name.getMethodName()).log(Level.INFO, "Starting test with server {0} client {1} batch {2}",
            new Object[] { serverFactory.getClass().getSimpleName(), clientFactory.getClass().getSimpleName(),
                    batch });//  w  w  w .j  a v a  2  s.c o m
    SSLEngine serverEngine = serverCtx.createSSLEngine();
    serverEngine.setUseClientMode(false);
    serverEngine.setNeedClientAuth(true);
    SSLEngine clientEngine = clientCtx.createSSLEngine();
    clientEngine.setUseClientMode(true);

    ProtocolStack<IOBufferMatcher> clientStack = ProtocolStack
            .on(clientFactory.create(selector.hub(), serverToClient.source(), clientToServer.sink()))
            .filter(new SSLEngineFilterLayer(clientEngine, null)).build(new IOBufferMatcherLayer());

    ProtocolStack<IOBufferMatcher> serverStack = ProtocolStack
            .on(serverFactory.create(selector.hub(), clientToServer.source(), serverToClient.sink()))
            .filter(new SSLEngineFilterLayer(serverEngine, null)).filter(batch)
            .build(new IOBufferMatcherLayer());

    final IOBufferMatcher client = clientStack.get();
    final IOBufferMatcher server = serverStack.get();
    int amount = fullTests ? 65536 * 4 : 16384;
    Future<Void> serverWork = selector.executorService().submit(new SequentialSender(server, amount, 13));

    serverWork.get();
    batch.flush();

    client.awaitByteContent(SequentialSender.matcher(amount));

    client.close();
    server.close();

    client.awaitClose();
    server.awaitClose();

    assertThat(client.asByteArray(), SequentialSender.matcher(amount));
}

From source file:org.jenkinsci.remoting.protocol.impl.SSLEngineFilterLayerTest.java

@Theory
public void bidiSendingBiggerAndBiggerBatches(NetworkLayerFactory serverFactory,
        NetworkLayerFactory clientFactory, BatchSendBufferingFilterLayer batch)
        throws java.io.IOException, InterruptedException, java.util.concurrent.ExecutionException {
    Logger.getLogger(name.getMethodName()).log(Level.INFO, "Starting test with server {0} client {1} batch {2}",
            new Object[] { serverFactory.getClass().getSimpleName(), clientFactory.getClass().getSimpleName(),
                    batch });//from w ww  .j  av  a 2 s  .c o m
    SSLEngine serverEngine = serverCtx.createSSLEngine();
    serverEngine.setUseClientMode(false);
    serverEngine.setNeedClientAuth(true);
    SSLEngine clientEngine = clientCtx.createSSLEngine();
    clientEngine.setUseClientMode(true);

    BatchSendBufferingFilterLayer clientBatch = batch.clone();
    ProtocolStack<IOBufferMatcher> clientStack = ProtocolStack
            .on(clientFactory.create(selector.hub(), serverToClient.source(), clientToServer.sink()))
            .filter(new NoOpFilterLayer()).filter(new SSLEngineFilterLayer(clientEngine, null))
            .filter(clientBatch).filter(new NoOpFilterLayer()).build(new IOBufferMatcherLayer());

    ProtocolStack<IOBufferMatcher> serverStack = ProtocolStack
            .on(serverFactory.create(selector.hub(), clientToServer.source(), serverToClient.sink()))
            .filter(new NoOpFilterLayer()).filter(new SSLEngineFilterLayer(serverEngine, null)).filter(batch)
            .filter(new NoOpFilterLayer()).build(new IOBufferMatcherLayer());

    final IOBufferMatcher client = clientStack.get();
    final IOBufferMatcher server = serverStack.get();
    int clientAmount = fullTests ? 65536 * 4 : 16384;
    Future<Void> clientWork = selector.executorService().submit(new SequentialSender(client, clientAmount, 11));
    int serverAmount = fullTests ? 65536 * 4 : 16384;
    Future<Void> serverWork = selector.executorService().submit(new SequentialSender(server, serverAmount, 13));

    clientWork.get();
    serverWork.get();
    clientBatch.flush();
    batch.flush();

    client.awaitByteContent(SequentialSender.matcher(clientAmount));
    server.awaitByteContent(SequentialSender.matcher(serverAmount));
}