Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to verify hostname after handshake #452

Closed
biaxident opened this issue Apr 11, 2017 · 5 comments
Closed

Unable to verify hostname after handshake #452

biaxident opened this issue Apr 11, 2017 · 5 comments

Comments

@biaxident
Copy link

biaxident commented Apr 11, 2017

Hi! WebSocket lib is greate, but it's impossible to verify hostname after handshake for secure connection to do check as proposed here https://developer.android.com/training/articles/security-ssl.html#CommonHostnameProbs
As a result it's a lack in security and library cant be used in production code.
How this problem could be solved?

@marci4
Copy link
Collaborator

marci4 commented Apr 12, 2017

Hello @biaxident,

as I understand your example correctly, you just need a SSLSocket to verify the hostname.

Gonna possible add a getter for this in the new version.

Greetings
marci4

@marci4 marci4 self-assigned this Apr 12, 2017
@biaxident
Copy link
Author

Hello, @marci4 . Thank you for so fast reply! Look forward for such feature!

@marci4
Copy link
Collaborator

marci4 commented Apr 13, 2017

Hello @biaxident,
I merged a change to this repository just now.

Could you please check if this is sufficient for your needs?

Greetings
marci4

@marci4 marci4 added this to the Release 1.3.3 milestone Apr 15, 2017
@marci4
Copy link
Collaborator

marci4 commented Apr 19, 2017

Example code for echo.websocket.org

package com.example.marci4.websockettest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import org.java_websocket.WebSocketImpl;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebSocketClient client = null;
        try {
            client = new WebSocketClient(new URI("wss://echo.websocket.org")) {
                @Override
                public void onOpen(ServerHandshake handshakedata) {
                    Log.i("Client", "Open");
                }

                @Override
                public void onMessage(String message) {
                    Log.i("Client", "Message: " + message);
                }

                @Override
                public void onClose(int code, String reason, boolean remote) {
                    Log.i("Client", "Close: " + reason + " Code: " + code + " Remote: " + remote);
                }

                @Override
                public void onError(Exception ex) {
                    Log.e("Client", "Error: " + ex.getMessage());
                }
            };
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        try {
            //Get SSLContext
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, null, null);
            client.setSocket(sslContext.getSocketFactory().createSocket());
            //Connect to server
            client.connectBlocking();
            //Verify 
            HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
            SSLSocket socket = (SSLSocket) client.getSocket();
            SSLSession s = socket.getSession();
            if (!hv.verify("echo.websocket.org", s)) {
                Log.e("Client", "Expected echo.websocket.org, found " + s.getPeerPrincipal());
                throw new SSLHandshakeException("Expected echo.websocket.org, found " + s.getPeerPrincipal());
            } else {
                Log.i("Client", "Success");
            }
        } catch (SSLHandshakeException e) {
            client.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

@marci4 marci4 closed this as completed Apr 19, 2017
@marci4
Copy link
Collaborator

marci4 commented Apr 19, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants