final void notifyHandshakeCompletedListeners() { if (listeners != null && !listeners.isEmpty()) { // notify the listeners HandshakeCompletedEvent event = new HandshakeCompletedEvent(this, getActiveSession()); for (HandshakeCompletedListener listener : listeners) { try { listener.handshakeCompleted(event); } catch (RuntimeException e) { // The RI runs the handlers in a separate thread, // which we do not. But we try to preserve their // behavior of logging a problem and not killing // the handshaking thread just because a listener // has a problem. Thread thread = Thread.currentThread(); thread.getUncaughtExceptionHandler().uncaughtException(thread, e); } } } }
/** * Creates a new HTTP or HTTPS connection on the specified port. * @param hostname the name of the host to connect to * @param port the port on the host to connect to * @param secure whether to use a secure connection * @param connectionTimeout the connection timeout * @param timeout the socket read timeout * * @throws IllegalArgumentException if either connectionTimeout or * timeout less than zero. */ public HTTPConnection(String hostname, int port, boolean secure, int connectionTimeout, int timeout) { if (connectionTimeout < 0 || timeout < 0) throw new IllegalArgumentException(); this.hostname = hostname; this.port = port; this.secure = secure; this.connectionTimeout = connectionTimeout; this.timeout = timeout; majorVersion = minorVersion = 1; handshakeCompletedListeners = new ArrayList<HandshakeCompletedListener>(2); }
@Override public void connect(SocketAddress endpoint, int timeout) throws IOException { Date start = new Date(); socket.connect(endpoint, timeout); Date end = new Date(); RequestInfo.get().connect(start, end); handshakeStart = new Date(); startHandshake(); this.addHandshakeCompletedListener(new HandshakeCompletedListener() { @Override public void handshakeCompleted(HandshakeCompletedEvent handshakeCompletedEvent) { if (handshakeStart != null) { RequestInfo.get().ssl(handshakeStart, new Date()); } } }); }
/** * This method works according to the specification of implemented class. * @see javax.net.ssl.SSLSocket#removeHandshakeCompletedListener(HandshakeCompletedListener) * method documentation for more information */ @Override public void removeHandshakeCompletedListener( HandshakeCompletedListener listener) { if (listener == null) { throw new IllegalArgumentException("Provided listener is null"); } if (listeners == null) { throw new IllegalArgumentException( "Provided listener is not registered"); } if (!listeners.remove(listener)) { throw new IllegalArgumentException( "Provided listener is not registered"); } }
private void notifyHandshakeCompletedListeners() { if (listeners != null && !listeners.isEmpty()) { // notify the listeners HandshakeCompletedEvent event = new HandshakeCompletedEvent(this, sslSession); for (HandshakeCompletedListener listener : listeners) { try { listener.handshakeCompleted(event); } catch (RuntimeException e) { // The RI runs the handlers in a separate thread, // which we do not. But we try to preserve their // behavior of logging a problem and not killing // the handshaking thread just because a listener // has a problem. Thread thread = Thread.currentThread(); thread.getUncaughtExceptionHandler().uncaughtException(thread, e); } } } }
/** * The method removes a registered listener. * @throws IllegalArgumentException if listener is null or not registered */ @Override public void removeHandshakeCompletedListener( HandshakeCompletedListener listener) { if (listener == null) { throw new IllegalArgumentException("Provided listener is null"); } if (listeners == null) { throw new IllegalArgumentException( "Provided listener is not registered"); } if (!listeners.remove(listener)) { throw new IllegalArgumentException( "Provided listener is not registered"); } }
/** * @param socket Socket to configure * @param protocols Enabled SSL protocol versions */ protected void configure(final Socket socket, final String[] protocols) throws IOException { if(socket instanceof SSLSocket) { try { if(log.isDebugEnabled()) { log.debug(String.format("Configure SSL parameters with protocols %s", Arrays.toString(protocols))); } ((SSLSocket) socket).setEnabledProtocols(protocols); final List<String> ciphers = Arrays.asList(((SSLSocket) socket).getEnabledCipherSuites()); final List<String> blacklist = preferences.getList("connection.ssl.cipher.blacklist"); if(!blacklist.isEmpty()) { ciphers.removeIf(blacklist::contains); } ((SSLSocket) socket).setEnabledCipherSuites(ciphers.toArray(new String[ciphers.size()])); if(log.isInfoEnabled()) { log.info(String.format("Enabled cipher suites %s", Arrays.toString(((SSLSocket) socket).getEnabledCipherSuites()))); ((SSLSocket) socket).addHandshakeCompletedListener(new HandshakeCompletedListener() { @Override public void handshakeCompleted(final HandshakeCompletedEvent event) { log.info(String.format("Completed handshake with %s and negotiated cipher suite %s", event.getSession().getProtocol(), event.getCipherSuite())); ((SSLSocket) socket).removeHandshakeCompletedListener(this); } }); } } catch(Exception e) { log.warn(String.format("Failed to configure SSL parameters %s", e.getMessage())); } } }
/** * 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; }
@Override public void removeHandshakeCompletedListener(HandshakeCompletedListener listener) { checkArgument(listener != null, "Provided listener is null"); if (!listeners.remove(listener)) { throw new IllegalArgumentException("Provided listener is not registered"); } }
@Test 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(); Future<Void> future = runAsync(new Callable<Void>() { @Override public Void call() throws Exception { server.startHandshake(); return null; } }); client.addHandshakeCompletedListener(new HandshakeCompletedListener() { @Override public void handshakeCompleted(HandshakeCompletedEvent event) { throw expectedException; } }); client.startHandshake(); future.get(); client.close(); server.close(); c.close(); assertSame(expectedException, test.actualException); self.setUncaughtExceptionHandler(original); }
void addHandshakeCompletedListener(HandshakeCompletedListener l) { synchronized (handshakeCompletedListeners) { handshakeCompletedListeners.add(l); } }
void removeHandshakeCompletedListener(HandshakeCompletedListener l) { synchronized (handshakeCompletedListeners) { handshakeCompletedListeners.remove(l); } }
public SSLSocketImpl(SSLContextImpl contextImpl, String host, int port, Socket underlyingSocket, boolean autoClose) { engine = new SSLEngineImpl(contextImpl, host, port); engine.setUseClientMode(true); // default to client mode listeners = new HashSet<HandshakeCompletedListener>(); this.underlyingSocket = underlyingSocket; this.autoClose = autoClose; }
/** * 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; }
public void handshakeCompleted() { if(listenerList.isEmpty()) { return; } final HandshakeCompletedEvent event = new HandshakeCompletedEvent(this, engine.getSession()); for(HandshakeCompletedListener listener: listenerList) { listener.handshakeCompleted(event); } }
/** * This method works according to the specification of implemented class. * @see javax.net.ssl.SSLSocket#addHandshakeCompletedListener(HandshakeCompletedListener) * method documentation for more information */ @Override public void addHandshakeCompletedListener( HandshakeCompletedListener listener) { if (listener == null) { throw new IllegalArgumentException("Provided listener is null"); } if (listeners == null) { listeners = new ArrayList<HandshakeCompletedListener>(); } listeners.add(listener); }
/** * Registers a listener to be notified that a SSL handshake * was successfully completed on this connection. * @throws <code>IllegalArgumentException</code> if listener is null. */ @Override public void addHandshakeCompletedListener( HandshakeCompletedListener listener) { if (listener == null) { throw new IllegalArgumentException("Provided listener is null"); } if (listeners == null) { listeners = new ArrayList(); } listeners.add(listener); }
@Override public void startHandshake() throws IOException { Log.d(TAG, "SSLSocket.startHandshake"); EidTlsClient client = new EidTlsClient(eidService, host, port); tlsClientProtocol.connect(client); this.session = new EidSSLSession(client); for (HandshakeCompletedListener listener : listeners) { listener.handshakeCompleted(new HandshakeCompletedEvent(this, this.session)); } }
protected MockSSLSocket(Socket socket, String host, int port) throws UnknownHostException { this.port = port; this.localHost = socket.getLocalAddress().getHostAddress(); this.localPort = socket.getLocalPort(); address = InetAddress.getByName(host); listeners = new LinkedList<HandshakeCompletedListener>(); }
@Override public void startHandshake() throws IOException { for (HandshakeCompletedListener listener : listeners) { if (this.listeners != null) { listener.handshakeCompleted(null); } else { throw new Error( "should not be here, at MockHandshake.startHandshake()"); } } }