tags:

views:

98

answers:

3

I need to connect to a host, if it's off-line I get a TCP timeout (and it's ok), and if it's online with SSL available, I proceed to the logon phase.

The problem is, when the server is online, accepts connection on the configured TCP port, but then doesn't answer to SSL handshake, our application waits indefinitely for an answer.

I'm using IBM HACL (c++) wrapped by JNI to access the server.

How could I test (in Java or C++ under Windows XP) the availability of SSL on the server side? Can I start a handshake manually, get the first server response, and then close the TCP socket? Or do I need to complete the handshake (and how do I?)

Thanks.

+1  A: 

In SSL, the first message to be sent is always by the client: a ClientHello. The server should response with a ServerHello message, so that'll be when you know it supports SSL. You can close the connection (by sending a close_notify alert) at any point. You do not need to complete the handshake.

Iit is quite common for peers to terminate mid-handshake if the session parameters do not allow a trusted session be established.

In Java, the SSLEngine class gives you the sort of control you desire over the SSL handshake, but it is quite a complex state machine requiring in-depth knowledge of SSL.

David Grant
Good to know that there's a solution, at least in theory.I just need to find out how to implement that in Java.
G B
Create an SSLSocket and either call startHandshake() or send some data. If you don't do either of those things the handshake doesn't start. Set a socket timeout as well just to be sure.
EJP
@EJP: Good to see you on here. I enjoyed your networking book. :)
David Grant
Thanks David ;-)
EJP
+2  A: 
try {
    SSLContext ctx = SSLContext.getDefault();
    ctx.getClientSessionContext().setSessionTimeout(5); // in seconds
    SSLSocket socket = (SSLSocket) 
         ctx.getSocketFactory().createSocket("host.com", 443);

    socket.setSoTimeout(5000); // in millis
    socket.startHandshake();
} catch (IOException ex) {
    sslAvailable = false;
}

As per your comment the socket.setSoTimeout(5000) did the trick.

If that doesn't work, then:

URLConnection urlConn = // obtain connection
urlConn.setConnectTimeout(5000); // in millis
Bozho
Uhm, works, but it seems to do exactly the opposite of what I'm trying to do:- If the host doesn't answer at all, startHandshake() waits forever. I could wrap that in a thread and close the socket after a given timeout.- If the host supports SSL, I get a javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshakeIt could work, but I don't know if it's the right solution.
G B
@G B see my update about the timeout.
Bozho
Same problem. Session timeout is something different.
G B
Found the solution: it's not "setSessionTimeout" on the context, it's "setSoTimeout" on the socket.
G B
good. I'll include it in the answer above so that it's more visible to others with the same problem
Bozho
A: 

The problem is, when the server is online, accepts connection on the configured TCP port, but then doesn't answer to SSL handshake, our application waits indefinitely for an answer.

As general purpose solution why not put the connect code into a thread and waiting for the thread until a timeout is reached.

PeterMmm
Yes, we did: the connection hangs and can't be closed.The problem is the IBM Host Access Library.
G B