javax.net.ssl.HandshakeCompletedListener Java Examples

The following examples show how to use javax.net.ssl.HandshakeCompletedListener. 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: TransportContext.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    // Don't need to synchronize, as it only runs in one thread.
    for (Map.Entry<HandshakeCompletedListener,
            AccessControlContext> entry : targets) {
        final HandshakeCompletedListener listener = entry.getKey();
        AccessControlContext acc = entry.getValue();
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            @Override
            public Void run() {
                listener.handshakeCompleted(event);
                return null;
            }
        }, acc);
    }
}
 
Example #2
Source File: TransportContext.java    From openjsse with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void run() {
    // Don't need to synchronize, as it only runs in one thread.
    for (Map.Entry<HandshakeCompletedListener,
            AccessControlContext> entry : targets) {
        final HandshakeCompletedListener listener = entry.getKey();
        AccessControlContext acc = entry.getValue();
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            @Override
            public Void run() {
                listener.handshakeCompleted(event);
                return null;
            }
        }, acc);
    }
}
 
Example #3
Source File: SSLConfiguration.java    From openjsse with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings({"unchecked", "CloneDeclaresCloneNotSupported"})
public Object clone() {
    // Note that only references to the configurations are copied.
    try {
        SSLConfiguration config = (SSLConfiguration)super.clone();
        if (handshakeListeners != null) {
            config.handshakeListeners =
                (HashMap<HandshakeCompletedListener, AccessControlContext>)
                        handshakeListeners.clone();
        }

        return config;
    } catch (CloneNotSupportedException cnse) {
        // unlikely
    }

    return null;    // unlikely
}
 
Example #4
Source File: SSLConfiguration.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({"unchecked", "CloneDeclaresCloneNotSupported"})
public Object clone() {
    // Note that only references to the configurations are copied.
    try {
        SSLConfiguration config = (SSLConfiguration)super.clone();
        if (handshakeListeners != null) {
            config.handshakeListeners =
                (HashMap<HandshakeCompletedListener, AccessControlContext>)
                        handshakeListeners.clone();
        }

        return config;
    } catch (CloneNotSupportedException cnse) {
        // unlikely
    }

    return null;    // unlikely
}
 
Example #5
Source File: SSLSocketTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_SSLSocket_HandshakeCompletedListener_RuntimeException() throws Exception {
    final Thread self = Thread.currentThread();
    final UncaughtExceptionHandler original = self.getUncaughtExceptionHandler();

    final RuntimeException expectedException = new RuntimeException("expected");
    final TestUncaughtExceptionHandler test = new TestUncaughtExceptionHandler();
    self.setUncaughtExceptionHandler(test);

    final TestSSLContext c = TestSSLContext.create();
    final SSLSocket client = (SSLSocket)
            c.clientContext.getSocketFactory().createSocket(c.host, c.port);
    final SSLSocket server = (SSLSocket) c.serverSocket.accept();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<Void> future = executor.submit(new Callable<Void>() {
        @Override public Void call() throws Exception {
            server.startHandshake();
            return null;
        }
    });
    executor.shutdown();
    client.addHandshakeCompletedListener(new HandshakeCompletedListener() {
        public void handshakeCompleted(HandshakeCompletedEvent event) {
            throw expectedException;
        }
    });
    client.startHandshake();
    future.get();
    client.close();
    server.close();
    c.close();

    assertSame(expectedException, test.actualException);
    self.setUncaughtExceptionHandler(original);
}
 
Example #6
Source File: SSLSocketImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
public void removeHandshakeCompletedListener(
        HandshakeCompletedListener listener) {
    if (listener == null) {
        throw new IllegalArgumentException("listener is null");
    }

    socketLock.lock();
    try {
        conContext.sslConfig.removeHandshakeCompletedListener(listener);
    } finally {
        socketLock.unlock();
    }
}
 
Example #7
Source File: SSLSocketImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
public void addHandshakeCompletedListener(
        HandshakeCompletedListener listener) {
    if (listener == null) {
        throw new IllegalArgumentException("listener is null");
    }

    socketLock.lock();
    try {
        conContext.sslConfig.addHandshakeCompletedListener(listener);
    } finally {
        socketLock.unlock();
    }
}
 
Example #8
Source File: SSLConfiguration.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
void addHandshakeCompletedListener(
        HandshakeCompletedListener listener) {

    if (handshakeListeners == null) {
        handshakeListeners = new HashMap<>(4);
    }

    handshakeListeners.put(listener, AccessController.getContext());
}
 
Example #9
Source File: CustomSslSocketFactory.java    From keystore-explorer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Constructor
 *
 * @param sslSocketFactory The actual SSLSocketFactory (used by this class)
 * @param handshakeListener The class that handles "handshake completed" events
 */
public CustomSslSocketFactory(SSLSocketFactory sslSocketFactory, HandshakeCompletedListener handshakeListener,
		boolean sniEnabled) {
	this.sslSocketFactory = sslSocketFactory;
	this.handshakeListener = handshakeListener;
	this.sniEnabled = sniEnabled;
}
 
Example #10
Source File: SSLSocketTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * javax.net.ssl.SSLSocket#addHandshakeCompletedListener(HandshakeCompletedListener listener)
 */
// AndroidOnly("RI doesn't throw the specified IAE")
public void j2objcNotImplemented_test_addHandshakeCompletedListener() throws IOException {
    SSLSocket ssl = getSSLSocket();
    HandshakeCompletedListener ls = new HandshakeCL();
    try {
        ssl.addHandshakeCompletedListener(null);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    ssl.addHandshakeCompletedListener(ls);
    ssl.close();
}
 
Example #11
Source File: SSLConfiguration.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
void removeHandshakeCompletedListener(
        HandshakeCompletedListener listener) {

    if (handshakeListeners == null) {
        throw new IllegalArgumentException("no listeners");
    }

    if (handshakeListeners.remove(listener) == null) {
        throw new IllegalArgumentException("listener not registered");
    }

    if (handshakeListeners.isEmpty()) {
        handshakeListeners = null;
    }
}
 
Example #12
Source File: SSLConfiguration.java    From openjsse with GNU General Public License v2.0 5 votes vote down vote up
void removeHandshakeCompletedListener(
        HandshakeCompletedListener listener) {

    if (handshakeListeners == null) {
        throw new IllegalArgumentException("no listeners");
    }

    if (handshakeListeners.remove(listener) == null) {
        throw new IllegalArgumentException("listener not registered");
    }

    if (handshakeListeners.isEmpty()) {
        handshakeListeners = null;
    }
}
 
Example #13
Source File: SSLConfiguration.java    From openjsse with GNU General Public License v2.0 5 votes vote down vote up
void addHandshakeCompletedListener(
        HandshakeCompletedListener listener) {

    if (handshakeListeners == null) {
        handshakeListeners = new HashMap<>(4);
    }

    handshakeListeners.put(listener, AccessController.getContext());
}
 
Example #14
Source File: SocketFactory.java    From dacapobench with Apache License 2.0 5 votes vote down vote up
/**
 * Create an SSL client socket using the IOR-encoded
 * security characteristics.
 * Setting want/need client auth on a client socket has no effect so all we can do is use the right host, port, ciphers
 *
 * @param host     The target host name.
 * @param port     The target connection port.
 *
 * @return An appropriately configured client SSLSocket.
 * @exception IOException if ssl socket can't be obtained and configured.
 */
private Socket createSSLSocket(String host, int port, int requires, int supports) throws IOException {
    SSLSocketFactory factory = getSocketFactory();
    SSLSocket socket = (SSLSocket) factory.createSocket(host, port);

    socket.setSoTimeout(SOCKET_TIMEOUT_MS);

    // get a set of cipher suites appropriate for this connections requirements.
    // We request this for each connection, since the outgoing IOR's requirements may be different from
    // our server listener requirements.
    String[] iorSuites = SSLCipherSuiteDatabase.getCipherSuites(requires, supports, factory.getSupportedCipherSuites());
    socket.setEnabledCipherSuites(iorSuites);
    if (log.isDebugEnabled()) {
        log.debug("Created SSL socket to " + host + ":" + port);
        log.debug("    cipher suites:");

        for (int i = 0; i < iorSuites.length; i++) {
            log.debug("    " + iorSuites[i]);
        }
        socket.addHandshakeCompletedListener(new HandshakeCompletedListener() {

            public void handshakeCompleted(HandshakeCompletedEvent handshakeCompletedEvent) {
                Certificate[] certs = handshakeCompletedEvent.getLocalCertificates();
                if (certs != null) {
                    log.debug("handshake returned local certs count: " + certs.length);
                    for (int i = 0; i < certs.length; i++) {
                        Certificate cert = certs[i];
                        log.debug("cert: " + cert.toString());
                    }
                } else {
                    log.debug("handshake returned no local certs");
                }
            }
        });
    }
    return socket;
}
 
Example #15
Source File: SSLSocketImpl.java    From openjsse with GNU General Public License v2.0 5 votes vote down vote up
@Override
public synchronized void removeHandshakeCompletedListener(
        HandshakeCompletedListener listener) {
    if (listener == null) {
        throw new IllegalArgumentException("listener is null");
    }

    conContext.sslConfig.removeHandshakeCompletedListener(listener);
}
 
Example #16
Source File: SSLSocketImpl.java    From openjsse with GNU General Public License v2.0 5 votes vote down vote up
@Override
public synchronized void addHandshakeCompletedListener(
        HandshakeCompletedListener listener) {
    if (listener == null) {
        throw new IllegalArgumentException("listener is null");
    }

    conContext.sslConfig.addHandshakeCompletedListener(listener);
}
 
Example #17
Source File: SSLSocketWrapper.java    From Dashchan with Apache License 2.0 4 votes vote down vote up
@Override
public void removeHandshakeCompletedListener(HandshakeCompletedListener listener) {
	wrapped.removeHandshakeCompletedListener(listener);
}
 
Example #18
Source File: TestSsl.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testRenegotiateFail() throws Exception {

    // If RFC5746 is supported, renegotiation will always work (and will
    // always be secure)
    if (TesterSupport.RFC_5746_SUPPORTED) {
        return;
    }

    Tomcat tomcat = getTomcatInstance();

    File appDir = new File(getBuildDirectory(), "webapps/examples");
    // app dir is relative to server home
    tomcat.addWebapp(null, "/examples", appDir.getAbsolutePath());

    TesterSupport.initSsl(tomcat);

    // Default - MITM attack prevented

    tomcat.start();
    SSLContext sslCtx = SSLContext.getInstance("TLS");
    sslCtx.init(null, TesterSupport.getTrustManagers(), null);
    SSLSocketFactory socketFactory = sslCtx.getSocketFactory();
    SSLSocket socket = (SSLSocket) socketFactory.createSocket("localhost", getPort());

    socket.addHandshakeCompletedListener(new HandshakeCompletedListener() {
        @Override
        public void handshakeCompleted(HandshakeCompletedEvent event) {
            handshakeDone = true;
        }
    });

    OutputStream os = socket.getOutputStream();
    os.write("GET /examples/servlets/servlet/HelloWorldExample HTTP/1.0\n".getBytes());
    os.flush();


    InputStream is = socket.getInputStream();

    // Make sure the NIO connector has read the request before the handshake
    Thread.sleep(100);

    socket.startHandshake();

    os = socket.getOutputStream();

    try {
        os.write("Host: localhost\n\n".getBytes());
    } catch (IOException ex) {
        ex.printStackTrace();
        fail("Re-negotiation failed");
    }
    Reader r = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(r);
    String line = br.readLine();
    while (line != null) {
        // For testing System.out.println(line);
        line = br.readLine();
    }

    if (!handshakeDone) {
        // success - we timed-out without handshake
        return;
    }

    fail("Re-negotiation worked");
}
 
Example #19
Source File: SSLSocketWrapper.java    From Dashchan with Apache License 2.0 4 votes vote down vote up
@Override
public void addHandshakeCompletedListener(HandshakeCompletedListener listener) {
	wrapped.addHandshakeCompletedListener(listener);
}
 
Example #20
Source File: NoSSLv3SocketFactory.java    From Pix-Art-Messenger with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void removeHandshakeCompletedListener(HandshakeCompletedListener listener) {
    delegate.removeHandshakeCompletedListener(listener);
}
 
Example #21
Source File: NoSSLv3SocketFactory.java    From Pix-Art-Messenger with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void addHandshakeCompletedListener(HandshakeCompletedListener listener) {
    delegate.addHandshakeCompletedListener(listener);
}
 
Example #22
Source File: OpenSSLSocket.java    From wildfly-openssl with Apache License 2.0 4 votes vote down vote up
private void invokeHandshakeListeners() {
    final HandshakeCompletedEvent event = new HandshakeCompletedEvent(this, getSession());
    for (HandshakeCompletedListener listener : new ArrayList<>(handshakeCompletedListenerList)) {
        listener.handshakeCompleted(event);
    }
}
 
Example #23
Source File: OpenSSLSocket.java    From wildfly-openssl with Apache License 2.0 4 votes vote down vote up
@Override
public void removeHandshakeCompletedListener(HandshakeCompletedListener listener) {
    handshakeCompletedListenerList.remove(listener);
}
 
Example #24
Source File: OpenSSLSocket.java    From wildfly-openssl with Apache License 2.0 4 votes vote down vote up
@Override
public void addHandshakeCompletedListener(HandshakeCompletedListener listener) {
    handshakeCompletedListenerList.add(listener);
}
 
Example #25
Source File: DelegateSSLSocket.java    From ibm-cos-sdk-java with Apache License 2.0 4 votes vote down vote up
@Override
public void addHandshakeCompletedListener(
        HandshakeCompletedListener listener) {
    sock.addHandshakeCompletedListener(listener);
}
 
Example #26
Source File: StubSSLSocket.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Override
public void addHandshakeCompletedListener(HandshakeCompletedListener arg0) {
}
 
Example #27
Source File: StubSSLSocket.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Override
public void removeHandshakeCompletedListener(HandshakeCompletedListener arg0) {
}
 
Example #28
Source File: DroidSocket.java    From raccoon4 with Apache License 2.0 4 votes vote down vote up
@Override
public void addHandshakeCompletedListener(HandshakeCompletedListener listener) {
	throw new UnsupportedOperationException("Do we need this?");
}
 
Example #29
Source File: DroidSocket.java    From raccoon4 with Apache License 2.0 4 votes vote down vote up
@Override
public void removeHandshakeCompletedListener(
		HandshakeCompletedListener listener) {
	throw new UnsupportedOperationException("Do we need this?");
}
 
Example #30
Source File: IosSslSocket.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Override
public void addHandshakeCompletedListener(HandshakeCompletedListener listener) {}