I have implemented a websockets server in C++ with libwebsockets. It works when I make calls from any web browser using or not TLS. When I call it from an android app using TooTallNate/Java-Websockets library it works if the server doesn't use a digital certificate, but if it does (which is needed if I want to use TLS) the sever is blocked (and does not respond to new messages) and the app gets a 1006 error on onClose(int code,String reason,boolean remote). It happens wether or not I use SSL on the client. The problem is on line 2040 of libwebsockets.c:
eff_buf.token_len = SSL_read(wsi->ssl, buf, sizeof buf);
It blocks on the server until I kill the android app.
This is the relevant code on the App:
class WebSocketChatClient extends WebSocketClient {
public WebSocketChatClient( URI serverUri ) {
super( serverUri );
}
public WebSocketChatClient( URI serverUri , Draft protocolDraft , Map<String,String> httpHeaders )
{
super(serverUri,protocolDraft,httpHeaders);
}
@Override
public void onOpen( ServerHandshake handshakedata ) {
System.out.println( "Connected" );
send("prueba");
}
@Override
public void onMessage( String message ) {
System.out.println( "got: " + message );
}
@Override
public void onClose( int code, String reason, boolean remote ) {
System.out.println( "Disconnected" );
//System.exit( 0 );
}
@Override
public void onError( Exception ex ) {
ex.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
URI uri;
try
{
uri=new URI("wss://192.168.1.128:8080");
Map<String,String> headers=new HashMap<String,String>();
headers.put("Sec-WebSocket-Protocol", "dumb-increment-protocol");
headers.put("User-Agent", "My Android App");
wsClient = new WebSocketChatClient(uri,new Draft_17(),headers);
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance( "TLS" );
sslContext.init( null, null, null ); // will use java's default key and trust store which is sufficient unless you deal with self-signed certificates
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
wsClient.setWebSocketFactory( new DefaultSSLWebSocketClientFactory( sslContext ) );
wsClient.connect();
}
catch(URISyntaxException e)
{
e.printStackTrace();
}
}
This is the relevant code on the server:
int port = 8080;
const char *intrface = NULL;
struct libwebsocket_context *context;
// we're not using ssl
const char *cert_path = "c:\\mydir\\1921681128.crt";
const char *key_path = "c:\\mydir\\1921681128.key";
// no special options
int opts = 0;
// create libwebsocket context representing this server
context = libwebsocket_create_context(port, intrface, protocols,
libwebsocket_internal_extensions,
cert_path, key_path, NULL,-1, -1, opts);
if (context == NULL) {
fprintf(stderr, "libwebsocket init failed\n");
return -1;
}
printf("starting server...\n");
// infinite loop, to end this server send SIGTERM. (CTRL+C)
int k=0;
while (1) {
int res=libwebsocket_service(context, 50);
// libwebsocket_service will process all waiting events with their
// callback functions and then wait 50 ms.
// (this is a single threaded webserver and this will keep our server
// from generating load while there are not requests to process)
}
I would like to know if there is any bug on my side or if it is a bug on libwebsockets, openssl or the java websockets client library . The client works if I call this server: ws://echo.websocket.org using or not TLS. Even if there is a bug on the java client, I would also like to know if it's right behaviour that ssl_read doesn't return and gets blocked even when the client already detects a problem and returns an error. (The ssl_read on openssl code is a bit obfuscated for me to understand it)
Regards, Juanjo
UPDATE: The libwebsockets.c code is here: https://github.com/davidgaleano/libwebsockets/blob/master/lib/libwebsockets.c and it seems I have a previous version. Now the line is 2037. Given that there isn't any release and it seems a fork from an old version of the original warmcat/libwebsockets, I suppose the main problem is in that library.
The solution was to use the original libwebsockets library (not a fork) and to implement a TrustManager wrapper to pass to sslContext.init. This wrapper validates the server certificate independently.