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

Provide SSLSession in WebSocketClient #1143

Merged
merged 1 commit into from Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/main/java/org/java_websocket/client/WebSocketClient.java
Expand Up @@ -955,12 +955,16 @@ public String getResourceDescriptor() {

@Override
public boolean hasSSLSupport() {
return engine.hasSSLSupport();
return socket instanceof SSLSocket;
}

@Override
public SSLSession getSSLSession() {
return engine.getSSLSession();
if (!hasSSLSupport()) {
throw new IllegalArgumentException(
"This websocket uses ws instead of wss. No SSLSession available.");
}
return ((SSLSocket)socket).getSession();
}

@Override
Expand Down
138 changes: 138 additions & 0 deletions src/test/java/org/java_websocket/issues/Issue1142Test.java
@@ -0,0 +1,138 @@
package org.java_websocket.issues;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.concurrent.CountDownLatch;
import javax.net.ssl.SSLContext;
import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.handshake.ServerHandshake;
import org.java_websocket.server.DefaultSSLWebSocketServerFactory;
import org.java_websocket.server.WebSocketServer;
import org.java_websocket.util.SSLContextUtil;
import org.java_websocket.util.SocketUtil;
import org.junit.Test;

public class Issue1142Test {



@Test(timeout = 4000)
public void testWithoutSSLSession()
throws IOException, URISyntaxException, InterruptedException {
int port = SocketUtil.getAvailablePort();
final CountDownLatch countServerDownLatch = new CountDownLatch(1);
final WebSocketClient webSocket = new WebSocketClient(new URI("ws://localhost:" + port)) {
@Override
public void onOpen(ServerHandshake handshakedata) {
countServerDownLatch.countDown();
}

@Override
public void onMessage(String message) {
}

@Override
public void onClose(int code, String reason, boolean remote) {
}

@Override
public void onError(Exception ex) {
}
};
WebSocketServer server = new MyWebSocketServer(port, countServerDownLatch);
server.start();
countServerDownLatch.await();
webSocket.connectBlocking();
assertFalse(webSocket.hasSSLSupport());
try {
webSocket.getSSLSession();
assertFalse(false);
} catch (IllegalArgumentException e) {
// Fine
}
server.stop();
}

@Test(timeout = 4000)
public void testWithSSLSession()
throws IOException, URISyntaxException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException, UnrecoverableKeyException, CertificateException, InterruptedException {
int port = SocketUtil.getAvailablePort();
final CountDownLatch countServerDownLatch = new CountDownLatch(1);
final WebSocketClient webSocket = new WebSocketClient(new URI("wss://localhost:" + port)) {
@Override
public void onOpen(ServerHandshake handshakedata) {
countServerDownLatch.countDown();
}

@Override
public void onMessage(String message) {
}

@Override
public void onClose(int code, String reason, boolean remote) {
}

@Override
public void onError(Exception ex) {
}
};
WebSocketServer server = new MyWebSocketServer(port, countServerDownLatch);
SSLContext sslContext = SSLContextUtil.getContext();

server.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(sslContext));
webSocket.setSocketFactory(sslContext.getSocketFactory());
server.start();
countServerDownLatch.await();
webSocket.connectBlocking();
assertTrue(webSocket.hasSSLSupport());
assertNotNull(webSocket.getSSLSession());
server.stop();
}

private static class MyWebSocketServer extends WebSocketServer {

private final CountDownLatch countServerLatch;


public MyWebSocketServer(int port, CountDownLatch serverDownLatch) {
super(new InetSocketAddress(port));
this.countServerLatch = serverDownLatch;
}

@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
}

@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
}

@Override
public void onMessage(WebSocket conn, String message) {

}

@Override
public void onError(WebSocket conn, Exception ex) {
ex.printStackTrace();
}

@Override
public void onStart() {
countServerLatch.countDown();
}
}
}