Skip to content

Commit

Permalink
Rework after review
Browse files Browse the repository at this point in the history
  • Loading branch information
marci4 committed Apr 21, 2020
1 parent 2dbe2d3 commit 0670985
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/main/example/SSLClientExample.java
Expand Up @@ -28,6 +28,7 @@
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.file.Paths;
import java.security.KeyStore;

import javax.net.ssl.KeyManagerFactory;
Expand Down Expand Up @@ -83,7 +84,7 @@ public static void main( String[] args ) throws Exception {

// load up the key store
String STORETYPE = "JKS";
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator);
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore.jks").toString();
String STOREPASSWORD = "storepassword";
String KEYPASSWORD = "keypassword";

Expand Down
3 changes: 2 additions & 1 deletion src/main/example/SSLServerCustomWebsocketFactoryExample.java
Expand Up @@ -32,6 +32,7 @@
import javax.net.ssl.TrustManagerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -52,7 +53,7 @@ public static void main(String[] args) throws Exception {

// load up the key store
String STORETYPE = "JKS";
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator);
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore.jks").toString();
String STOREPASSWORD = "storepassword";
String KEYPASSWORD = "keypassword";

Expand Down
3 changes: 2 additions & 1 deletion src/main/example/SSLServerExample.java
Expand Up @@ -27,6 +27,7 @@

import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Paths;
import java.security.KeyStore;

import javax.net.ssl.KeyManagerFactory;
Expand All @@ -48,7 +49,7 @@ public static void main( String[] args ) throws Exception {

// load up the key store
String STORETYPE = "JKS";
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator);
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore.jks").toString();
String STOREPASSWORD = "storepassword";
String KEYPASSWORD = "keypassword";

Expand Down
3 changes: 2 additions & 1 deletion src/main/example/TwoWaySSLServerExample.java
Expand Up @@ -33,6 +33,7 @@
import javax.net.ssl.TrustManagerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Paths;
import java.security.KeyStore;

/**
Expand All @@ -51,7 +52,7 @@ public static void main( String[] args ) throws Exception {

// load up the key store
String STORETYPE = "JKS";
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator);
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore.jks").toString();
String STOREPASSWORD = "storepassword";
String KEYPASSWORD = "keypassword";

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/java_websocket/client/WebSocketClient.java
Expand Up @@ -472,6 +472,8 @@ public void run() {
if (socket instanceof SSLSocket) {
SSLSocket sslSocket = (SSLSocket)socket;
SSLParameters sslParameters = sslSocket.getSSLParameters();
// Make sure we perform hostname validation
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
onSetSSLParameters(sslParameters);
sslSocket.setSSLParameters(sslParameters);
}
Expand Down Expand Up @@ -517,12 +519,11 @@ public void run() {
}

/**
* Apply specific
* Apply specific SSLParameters
*
* @param sslParameters the SSLParameters which will be used for the SSLSocket
*/
protected void onSetSSLParameters(SSLParameters sslParameters) {
// Make sure we perform hostname validation
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
}

/**
Expand Down
Expand Up @@ -43,6 +43,7 @@
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.security.spec.ECField;
import java.util.Collections;
Expand Down Expand Up @@ -102,7 +103,7 @@ public static void main( String[] args ) throws UnknownHostException {
try {
// load up the key store
String STORETYPE = "JKS";
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator);
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore.jks").toString();
String STOREPASSWORD = "storepassword";
String KEYPASSWORD = "keypassword";

Expand Down
18 changes: 15 additions & 3 deletions src/test/java/org/java_websocket/issues/Issue997Test.java
Expand Up @@ -67,6 +67,13 @@ public void test_localServer_ServerLocalhost_Client127_CheckInactive() throws Ce
assertFalse(client.onSSLError);
}

@Test(timeout=2000)
public void test_localServer_ServerLocalhost_Client127_CheckDefault() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException {
SSLWebSocketClient client = testIssueWithLocalServer("127.0.0.1", SocketUtil.getAvailablePort(), SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), null);
assertFalse(client.onOpen);
assertTrue(client.onSSLError);
}

@Test(timeout=2000)
public void test_localServer_ServerLocalhost_ClientLocalhost_CheckActive() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException {
SSLWebSocketClient client = testIssueWithLocalServer("localhost", SocketUtil.getAvailablePort(), SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), "HTTPS");
Expand All @@ -80,6 +87,13 @@ public void test_localServer_ServerLocalhost_ClientLocalhost_CheckInactive() thr
assertFalse(client.onSSLError);
}

@Test(timeout=2000)
public void test_localServer_ServerLocalhost_ClientLocalhost_CheckDefault() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException {
SSLWebSocketClient client = testIssueWithLocalServer("localhost", SocketUtil.getAvailablePort(), SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), null);
assertTrue(client.onOpen);
assertFalse(client.onSSLError);
}


public SSLWebSocketClient testIssueWithLocalServer(String address, int port, SSLContext serverContext, SSLContext clientContext, String endpointIdentificationAlgorithm) throws IOException, URISyntaxException, InterruptedException {
CountDownLatch countServerDownLatch = new CountDownLatch(1);
Expand Down Expand Up @@ -129,9 +143,7 @@ public void onError(Exception ex) {

@Override
protected void onSetSSLParameters(SSLParameters sslParameters) {
if (endpointIdentificationAlgorithm == null) {
super.onSetSSLParameters(sslParameters);
} else {
if (endpointIdentificationAlgorithm != null) {
sslParameters.setEndpointIdentificationAlgorithm(endpointIdentificationAlgorithm);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/org/java_websocket/util/SSLContextUtil.java
Expand Up @@ -32,6 +32,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Paths;
import java.security.*;
import java.security.cert.CertificateException;

Expand All @@ -40,7 +41,7 @@ public class SSLContextUtil {
public static SSLContext getContext() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException {
// load up the key store
String STORETYPE = "JKS";
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator);
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore.jks").toString();
String STOREPASSWORD = "storepassword";
String KEYPASSWORD = "keypassword";

Expand All @@ -62,7 +63,7 @@ public static SSLContext getContext() throws NoSuchAlgorithmException, KeyManage
public static SSLContext getLocalhostOnlyContext() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException {
// load up the key store
String STORETYPE = "JKS";
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore_localhost_only.jks", File.separator);
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore_localhost_only.jks").toString();
String STOREPASSWORD = "storepassword";
String KEYPASSWORD = "keypassword";

Expand Down

0 comments on commit 0670985

Please sign in to comment.