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

ssl == null when set bks to webSocketServer in Android #1327

Open
Zain-yh opened this issue Apr 26, 2023 · 0 comments
Open

ssl == null when set bks to webSocketServer in Android #1327

Zain-yh opened this issue Apr 26, 2023 · 0 comments
Labels

Comments

@Zain-yh
Copy link

Zain-yh commented Apr 26, 2023

Describe the bug
I use websokcetServer in android, but when i set bks, client cant connect to server by ssl

onError: java.lang.NullPointerException: ssl == null

The code is as follows :
`private void startServer() {
port = Integer.parseInt(et_port.getText().toString());
server = new WebSocketServer(new InetSocketAddress(port)) {
@OverRide
public void onOpen(WebSocket conn, ClientHandshake handshake) {
Log.e(TAG, "onOpen: " + handshake.getResourceDescriptor());
}

        @Override
        public void onClose(WebSocket conn, int code, String reason, boolean remote) {
            Log.e(TAG, "onClose: " + reason);
        }

        @Override
        public void onMessage(WebSocket conn, String message) {
            Log.e(TAG, "onMessage: ");
        }

        @Override
        public void onError(WebSocket conn, Exception ex) {
            Log.e(TAG, "onError: " + ex.toString());

        }

        @Override
        public void onStart() {
            Log.e(TAG, "onStart: ");
        }
    };

    server.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(getSSL()));

    server.start();
}

private SSLContext getSSL() {
    try {
        String pass = "abcdefg";
        KeyStore ks = KeyStore.getInstance("BKS");
        InputStream keystoreStream = getResources().openRawResource(R.raw.websocket_bycert);
        ks.load(keystoreStream, pass.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
        kmf.init(ks, pass.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
        tmf.init(ks);
        SSLContext ssl = SSLContext.getInstance("TLS");
        ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
        return ssl;
    } catch (KeyStoreException | UnrecoverableKeyException | CertificateException | IOException | NoSuchAlgorithmException | KeyManagementException e) {
        e.printStackTrace();
    }
    return null;
}`
@marci4 marci4 added the Android label Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants