Commit 75b4b46f authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Fix TLS timing problem. JM-669

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@3834 b35dd754-fafc-0310-a699-88a17e54d16e
parent 2e083e3b
...@@ -65,7 +65,8 @@ public class SocketConnection implements Connection { ...@@ -65,7 +65,8 @@ public class SocketConnection implements Connection {
*/ */
private long idleTimeout = -1; private long idleTimeout = -1;
private Map<ConnectionCloseListener, Object> listeners = new HashMap<ConnectionCloseListener, Object>(); final private Map<ConnectionCloseListener, Object> listeners =
new HashMap<ConnectionCloseListener, Object>();
private Socket socket; private Socket socket;
private SocketReader socketReader; private SocketReader socketReader;
...@@ -149,7 +150,15 @@ public class SocketConnection implements Connection { ...@@ -149,7 +150,15 @@ public class SocketConnection implements Connection {
public void startTLS(boolean clientMode, String remoteServer) throws IOException { public void startTLS(boolean clientMode, String remoteServer) throws IOException {
if (!secure) { if (!secure) {
secure = true; secure = true;
// Prepare for TLS
tlsStreamHandler = new TLSStreamHandler(socket, clientMode, remoteServer, session instanceof IncomingServerSession); tlsStreamHandler = new TLSStreamHandler(socket, clientMode, remoteServer, session instanceof IncomingServerSession);
if (!clientMode) {
// Indicate the client that the server is ready to negotiate TLS
deliverRawText("<proceed xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>");
}
// Start handshake
tlsStreamHandler.start();
// Use new wrapped writers
writer = new BufferedWriter(new OutputStreamWriter(tlsStreamHandler.getOutputStream(), CHARSET)); writer = new BufferedWriter(new OutputStreamWriter(tlsStreamHandler.getOutputStream(), CHARSET));
xmlSerializer = new XMLSocketWriter(writer, this); xmlSerializer = new XMLSocketWriter(writer, this);
} }
...@@ -229,10 +238,30 @@ public class SocketConnection implements Connection { ...@@ -229,10 +238,30 @@ public class SocketConnection implements Connection {
return socket.getInetAddress(); return socket.getInetAddress();
} }
/**
* Returns the port that the connection uses.
*
* @return the port that the connection uses.
*/
public int getPort() { public int getPort() {
return socket.getPort(); return socket.getPort();
} }
/**
* Returns the Writer used to send data to the connection. The writer should be
* used with caution. In the majority of cases, the {@link #deliver(Packet)}
* method should be used to send data instead of using the writer directly.
* You must synchronize on the writer before writing data to it to ensure
* data consistency:
*
* <pre>
* Writer writer = connection.getWriter();
* synchronized(writer) {
* // write data....
* }</pre>
*
* @return the Writer for this connection.
*/
public Writer getWriter() { public Writer getWriter() {
return writer; return writer;
} }
...@@ -256,6 +285,15 @@ public class SocketConnection implements Connection { ...@@ -256,6 +285,15 @@ public class SocketConnection implements Connection {
return tlsPolicy; return tlsPolicy;
} }
/**
* Sets whether TLS is mandatory, optional or is disabled. When TLS is mandatory clients
* are required to secure their connections or otherwise their connections will be closed.
* On the other hand, when TLS is disabled clients are not allowed to secure their connections
* using TLS. Their connections will be closed if they try to secure the connection. in this
* last case.
*
* @param tlsPolicy whether TLS is mandatory, optional or is disabled.
*/
public void setTlsPolicy(TLSPolicy tlsPolicy) { public void setTlsPolicy(TLSPolicy tlsPolicy) {
this.tlsPolicy = tlsPolicy; this.tlsPolicy = tlsPolicy;
} }
...@@ -264,6 +302,11 @@ public class SocketConnection implements Connection { ...@@ -264,6 +302,11 @@ public class SocketConnection implements Connection {
return compressionPolicy; return compressionPolicy;
} }
/**
* Sets whether compression is enabled or is disabled.
*
* @param compressionPolicy whether Compression is enabled or is disabled.
*/
public void setCompressionPolicy(CompressionPolicy compressionPolicy) { public void setCompressionPolicy(CompressionPolicy compressionPolicy) {
this.compressionPolicy = compressionPolicy; this.compressionPolicy = compressionPolicy;
} }
...@@ -272,6 +315,14 @@ public class SocketConnection implements Connection { ...@@ -272,6 +315,14 @@ public class SocketConnection implements Connection {
return idleTimeout; return idleTimeout;
} }
/**
* Sets the number of milliseconds a connection has to be idle to be closed. Sending
* stanzas to the client is not considered as activity. We are only considering the
* connection active when the client sends some data or hearbeats (i.e. whitespaces)
* to the server.
*
* @param timeout the number of milliseconds a connection has to be idle to be closed.
*/
public void setIdleTimeout(long timeout) { public void setIdleTimeout(long timeout) {
this.idleTimeout = timeout; this.idleTimeout = timeout;
} }
...@@ -353,7 +404,9 @@ public class SocketConnection implements Connection { ...@@ -353,7 +404,9 @@ public class SocketConnection implements Connection {
} }
writer.flush(); writer.flush();
} }
catch (IOException e) {} catch (IOException e) {
// Do nothing
}
finally { finally {
// Register that we finished sending data on the connection // Register that we finished sending data on the connection
writeFinished(); writeFinished();
......
...@@ -672,9 +672,7 @@ public abstract class SocketReader implements Runnable { ...@@ -672,9 +672,7 @@ public abstract class SocketReader implements Runnable {
"Closing connection : " + connection); "Closing connection : " + connection);
return false; return false;
} }
// Client requested to secure the connection using TLS // Client requested to secure the connection using TLS. Negotiate TLS.
connection.deliverRawText("<proceed xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>");
// Negotiate TLS.
try { try {
connection.startTLS(false, null); connection.startTLS(false, null);
} }
......
...@@ -201,10 +201,6 @@ public class TLSStreamHandler { ...@@ -201,10 +201,6 @@ public class TLSStreamHandler {
else if (needClientAuth) { else if (needClientAuth) {
tlsEngine.setNeedClientAuth(true); tlsEngine.setNeedClientAuth(true);
} }
while (!initialHSComplete) {
initialHSComplete = doHandshake(null);
}
} }
public InputStream getInputStream(){ public InputStream getInputStream(){
...@@ -215,6 +211,12 @@ public class TLSStreamHandler { ...@@ -215,6 +211,12 @@ public class TLSStreamHandler {
return writer.getOutputStream(); return writer.getOutputStream();
} }
void start() throws IOException {
while (!initialHSComplete) {
initialHSComplete = doHandshake(null);
}
}
private boolean doHandshake(SelectionKey sk) throws IOException { private boolean doHandshake(SelectionKey sk) throws IOException {
SSLEngineResult result; SSLEngineResult result;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment