Commit 611b54ea authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Added support for TLS negotiation as a client. JM-395

git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@2858 b35dd754-fafc-0310-a699-88a17e54d16e
parent 60a3afc3
......@@ -26,7 +26,6 @@ import java.io.Writer;
import java.net.InetAddress;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
......@@ -43,7 +42,7 @@ public class SocketConnection implements Connection {
*/
public static final String CHARSET = "UTF-8";
private Map listeners = new HashMap();
private Map<ConnectionCloseListener, Object> listeners = new HashMap<ConnectionCloseListener, Object>();
private Socket socket;
......@@ -96,12 +95,13 @@ public class SocketConnection implements Connection {
/**
* Secures the plain connection by negotiating TLS with the client.
*
* @param clientMode boolean indicating if this entity is a client or a server.
* @throws IOException if an error occured while securing the connection.
*/
public void startTLS() throws IOException {
public void startTLS(boolean clientMode) throws IOException {
if (!secure) {
secure = true;
tlsStreamHandler = new TLSStreamHandler(socket);
tlsStreamHandler = new TLSStreamHandler(socket, clientMode);
writer = new BufferedWriter(new OutputStreamWriter(tlsStreamHandler.getOutputStream(), CHARSET));
xmlSerializer = new XMLSocketWriter(writer, socket);
}
......@@ -336,9 +336,7 @@ public class SocketConnection implements Connection {
*/
private void notifyCloseListeners() {
synchronized (listeners) {
Iterator itr = listeners.keySet().iterator();
while (itr.hasNext()) {
ConnectionCloseListener listener = (ConnectionCloseListener)itr.next();
for (ConnectionCloseListener listener : listeners.keySet()) {
listener.onConnectionClose(listeners.get(listener));
}
}
......
......@@ -77,11 +77,12 @@ public class TLSStreamHandler {
/**
* Creates a new TLSStreamHandler and secures the plain socket connection.
*
* @param clientMode boolean indicating if this entity is a client or a server.
* @param socket the plain socket connection to secure
* @throws IOException
*/
public TLSStreamHandler(Socket socket) throws IOException {
wrapper = new TLSWrapper();
public TLSStreamHandler(Socket socket, boolean clientMode) throws IOException {
wrapper = new TLSWrapper(clientMode);
tlsEngine = wrapper.getTlsEngine();
reader = new TLSStreamReader(wrapper, socket);
writer = new TLSStreamWriter(wrapper, socket);
......@@ -101,8 +102,12 @@ public class TLSStreamHandler {
appBB = ByteBuffer.allocate(appBBSize);
//socket.setSoTimeout(0);
//socket.setKeepAlive(true);
if (clientMode) {
socket.setSoTimeout(0);
socket.setKeepAlive(true);
initialHSStatus = HandshakeStatus.NEED_WRAP;
tlsEngine.beginHandshake();
}
while (!initialHSComplete) {
initialHSComplete = doHandshake(null);
......
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