From 1d8fd98bd5667678fc4682057e5488b339e6bae2 Mon Sep 17 00:00:00 2001 From: seb Date: Fri, 14 Oct 2011 08:56:28 +0000 Subject: [PATCH] Fixes ICE failed case where Gmail user calls to Jitsi ones with SSLTCP only (when connecting to remote SSLTCP relay, the SSL handshake was not done). --- src/org/ice4j/ice/Component.java | 22 ++++++++++- .../GoogleTurnSSLCandidateHarvester.java | 39 +++++++++++++++---- src/org/ice4j/socket/MultiplexingSocket.java | 24 ++++++++++++ 3 files changed, 76 insertions(+), 9 deletions(-) diff --git a/src/org/ice4j/ice/Component.java b/src/org/ice4j/ice/Component.java index d11120e..8508a80 100644 --- a/src/org/ice4j/ice/Component.java +++ b/src/org/ice4j/ice/Component.java @@ -13,6 +13,7 @@ import java.util.*; import java.util.logging.*; import org.ice4j.*; +import org.ice4j.ice.harvest.*; import org.ice4j.socket.*; /** @@ -251,7 +252,7 @@ public class Component continue; } - Socket sock = new MultiplexingSocket(); + MultiplexingSocket sock = new MultiplexingSocket(); try { // if we use proxy (socks5, ...), the connect() timeout @@ -272,6 +273,25 @@ public class Component continue; } + if(candidate.getTransportAddress().getPort() == 443) + { + //SSLTCP handshake + OutputStream outputStream = + sock.getOriginalOutputStream(); + InputStream inputStream = sock.getOriginalInputStream(); + + if(!GoogleTurnSSLCandidateHarvester.sslHandshake( + inputStream, outputStream)) + { + logger.info("Failed to connect to SSLTCP relay"); + outputStream = null; + inputStream = null; + continue; + } + outputStream = null; + inputStream = null; + } + LocalCandidate tmp = new HostCandidate(new IceTcpSocketWrapper(sock), this, Transport.TCP); diff --git a/src/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java b/src/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java index cda3022..fcc26bc 100644 --- a/src/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java @@ -127,16 +127,9 @@ public class GoogleTurnSSLCandidateHarvester stunServer.getPort()); OutputStream outputStream = sock.getOutputStream(); - outputStream.write(SSL_CLIENT_HANDSHAKE); - InputStream inputStream = sock.getInputStream(); - byte data[] = new byte[SSL_SERVER_HANDSHAKE.length]; - inputStream.read(data); - outputStream = null; - inputStream = null; - - if(Arrays.equals(data, SSL_SERVER_HANDSHAKE)) + if(sslHandshake(inputStream, outputStream)) { cand = new HostCandidate(new IceTcpSocketWrapper( new MultiplexingSocket(sock)), @@ -155,4 +148,34 @@ public class GoogleTurnSSLCandidateHarvester return cand; } + + /** + * Do the SSL handshake (send client certificate and wait for receive server + * certificate). We explicitely need InputStream and + * OutputStream because some Socket may redefine + * getInputStream()/getOutputStream() and we need the original stream. + * + * @param inputStream InputStream of the socket + * @param outputStream OuputStream of the socket + * @return true if the SSL handshake is done + * @throws IOException if something goes wrong + */ + public static boolean sslHandshake(InputStream inputStream, OutputStream + outputStream) throws IOException + { + byte data[] = new byte[SSL_SERVER_HANDSHAKE.length]; + + outputStream.write(SSL_CLIENT_HANDSHAKE); + inputStream.read(data); + + outputStream = null; + inputStream = null; + + if(Arrays.equals(data, SSL_SERVER_HANDSHAKE)) + { + return true; + } + + return false; + } } diff --git a/src/org/ice4j/socket/MultiplexingSocket.java b/src/org/ice4j/socket/MultiplexingSocket.java index c12034f..4ca9db9 100644 --- a/src/org/ice4j/socket/MultiplexingSocket.java +++ b/src/org/ice4j/socket/MultiplexingSocket.java @@ -531,4 +531,28 @@ public class MultiplexingSocket return outputStream; } + + /** + * Get original InputStream. + * + * @return original InputStream + * @throws IOException if something goes wrong + */ + public InputStream getOriginalInputStream() + throws IOException + { + return super.getInputStream(); + } + + /** + * Get original OutputStream. + * + * @return original OutputStream + * @throws IOException if something goes wrong + */ + public OutputStream getOriginalOutputStream() + throws IOException + { + return super.getOutputStream(); + } }