Skip to content

Commit cab3fda

Browse files
authored
Merge pull request #1000 from marci4/Issue997
2 parents 4232021 + 0670985 commit cab3fda

12 files changed

+239
-11
lines changed

keystore.jks

-2.2 KB
Binary file not shown.

src/main/example/SSLClientExample.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.FileInputStream;
2929
import java.io.InputStreamReader;
3030
import java.net.URI;
31+
import java.nio.file.Paths;
3132
import java.security.KeyStore;
3233

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

8485
// load up the key store
8586
String STORETYPE = "JKS";
86-
String KEYSTORE = "keystore.jks";
87+
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore.jks").toString();
8788
String STOREPASSWORD = "storepassword";
8889
String KEYPASSWORD = "keypassword";
8990

src/main/example/SSLServerCustomWebsocketFactoryExample.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import javax.net.ssl.TrustManagerFactory;
3333
import java.io.File;
3434
import java.io.FileInputStream;
35+
import java.nio.file.Paths;
3536
import java.security.KeyStore;
3637
import java.util.ArrayList;
3738
import java.util.Arrays;
@@ -52,7 +53,7 @@ public static void main(String[] args) throws Exception {
5253

5354
// load up the key store
5455
String STORETYPE = "JKS";
55-
String KEYSTORE = "keystore.jks";
56+
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore.jks").toString();
5657
String STOREPASSWORD = "storepassword";
5758
String KEYPASSWORD = "keypassword";
5859

src/main/example/SSLServerExample.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.io.File;
2929
import java.io.FileInputStream;
30+
import java.nio.file.Paths;
3031
import java.security.KeyStore;
3132

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

4950
// load up the key store
5051
String STORETYPE = "JKS";
51-
String KEYSTORE = "keystore.jks";
52+
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore.jks").toString();
5253
String STOREPASSWORD = "storepassword";
5354
String KEYPASSWORD = "keypassword";
5455

src/main/example/TwoWaySSLServerExample.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import javax.net.ssl.TrustManagerFactory;
3434
import java.io.File;
3535
import java.io.FileInputStream;
36+
import java.nio.file.Paths;
3637
import java.security.KeyStore;
3738

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

5253
// load up the key store
5354
String STORETYPE = "JKS";
54-
String KEYSTORE = "keystore.jks";
55+
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore.jks").toString();
5556
String STOREPASSWORD = "storepassword";
5657
String KEYPASSWORD = "keypassword";
5758

src/main/java/org/java_websocket/client/WebSocketClient.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,6 @@ public void run() {
449449
} else if( socket == null ) {
450450
socket = new Socket( proxy );
451451
isNewSocket = true;
452-
453452
} else if( socket.isClosed() ) {
454453
throw new IOException();
455454
}
@@ -464,13 +463,21 @@ public void run() {
464463

465464
// if the socket is set by others we don't apply any TLS wrapper
466465
if (isNewSocket && "wss".equals( uri.getScheme())) {
467-
468466
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
469467
sslContext.init(null, null, null);
470468
SSLSocketFactory factory = sslContext.getSocketFactory();
471469
socket = factory.createSocket(socket, uri.getHost(), getPort(), true);
472470
}
473471

472+
if (socket instanceof SSLSocket) {
473+
SSLSocket sslSocket = (SSLSocket)socket;
474+
SSLParameters sslParameters = sslSocket.getSSLParameters();
475+
// Make sure we perform hostname validation
476+
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
477+
onSetSSLParameters(sslParameters);
478+
sslSocket.setSSLParameters(sslParameters);
479+
}
480+
474481
istream = socket.getInputStream();
475482
ostream = socket.getOutputStream();
476483

@@ -511,6 +518,14 @@ public void run() {
511518
connectReadThread = null;
512519
}
513520

521+
/**
522+
* Apply specific SSLParameters
523+
*
524+
* @param sslParameters the SSLParameters which will be used for the SSLSocket
525+
*/
526+
protected void onSetSSLParameters(SSLParameters sslParameters) {
527+
}
528+
514529
/**
515530
* Extract the specified port
516531
* @return the specified port or the default port for the specific scheme

src/test/java/org/java_websocket/example/AutobahnSSLServerTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.net.InetSocketAddress;
4444
import java.net.UnknownHostException;
4545
import java.nio.ByteBuffer;
46+
import java.nio.file.Paths;
4647
import java.security.KeyStore;
4748
import java.security.spec.ECField;
4849
import java.util.Collections;
@@ -102,7 +103,7 @@ public static void main( String[] args ) throws UnknownHostException {
102103
try {
103104
// load up the key store
104105
String STORETYPE = "JKS";
105-
String KEYSTORE = "keystore.jks";
106+
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore.jks").toString();
106107
String STOREPASSWORD = "storepassword";
107108
String KEYPASSWORD = "keypassword";
108109

src/test/java/org/java_websocket/issues/Issue962Test.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public Socket createSocket(InetAddress ia, int i, InetAddress ia1, int i1) throw
8585

8686
}
8787

88-
@Test
88+
@Test(timeout = 2000)
8989
public void testIssue() throws IOException, URISyntaxException, InterruptedException {
9090
int port = SocketUtil.getAvailablePort();
9191
WebSocketClient client = new WebSocketClient(new URI("ws://127.0.0.1:" + port)) {
@@ -103,7 +103,7 @@ public void onClose(int code, String reason, boolean remote) {
103103

104104
@Override
105105
public void onError(Exception ex) {
106-
Assert.fail(ex.toString() + " sould not occur");
106+
Assert.fail(ex.toString() + " should not occur");
107107
}
108108
};
109109

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
package org.java_websocket.issues;
2+
3+
/*
4+
* Copyright (c) 2010-2020 Nathan Rajlich
5+
*
6+
* Permission is hereby granted, free of charge, to any person
7+
* obtaining a copy of this software and associated documentation
8+
* files (the "Software"), to deal in the Software without
9+
* restriction, including without limitation the rights to use,
10+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the
12+
* Software is furnished to do so, subject to the following
13+
* conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be
16+
* included in all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25+
* OTHER DEALINGS IN THE SOFTWARE.
26+
*
27+
*/
28+
29+
30+
import org.java_websocket.WebSocket;
31+
import org.java_websocket.client.WebSocketClient;
32+
import org.java_websocket.handshake.ClientHandshake;
33+
import org.java_websocket.handshake.ServerHandshake;
34+
import org.java_websocket.server.DefaultSSLWebSocketServerFactory;
35+
import org.java_websocket.server.WebSocketServer;
36+
import org.java_websocket.util.SSLContextUtil;
37+
import org.java_websocket.util.SocketUtil;
38+
import org.junit.Test;
39+
40+
import javax.net.ssl.SSLContext;
41+
import javax.net.ssl.SSLHandshakeException;
42+
import javax.net.ssl.SSLParameters;
43+
import java.io.IOException;
44+
import java.net.*;
45+
import java.security.KeyManagementException;
46+
import java.security.KeyStoreException;
47+
import java.security.NoSuchAlgorithmException;
48+
import java.security.UnrecoverableKeyException;
49+
import java.security.cert.CertificateException;
50+
import java.util.concurrent.CountDownLatch;
51+
import java.util.concurrent.TimeUnit;
52+
53+
import static org.junit.Assert.*;
54+
55+
public class Issue997Test {
56+
57+
@Test(timeout=2000)
58+
public void test_localServer_ServerLocalhost_Client127_CheckActive() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException {
59+
SSLWebSocketClient client = testIssueWithLocalServer("127.0.0.1", SocketUtil.getAvailablePort(), SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), "HTTPS");
60+
assertFalse(client.onOpen);
61+
assertTrue(client.onSSLError);
62+
}
63+
@Test(timeout=2000)
64+
public void test_localServer_ServerLocalhost_Client127_CheckInactive() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException {
65+
SSLWebSocketClient client = testIssueWithLocalServer("127.0.0.1", SocketUtil.getAvailablePort(), SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), "");
66+
assertTrue(client.onOpen);
67+
assertFalse(client.onSSLError);
68+
}
69+
70+
@Test(timeout=2000)
71+
public void test_localServer_ServerLocalhost_Client127_CheckDefault() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException {
72+
SSLWebSocketClient client = testIssueWithLocalServer("127.0.0.1", SocketUtil.getAvailablePort(), SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), null);
73+
assertFalse(client.onOpen);
74+
assertTrue(client.onSSLError);
75+
}
76+
77+
@Test(timeout=2000)
78+
public void test_localServer_ServerLocalhost_ClientLocalhost_CheckActive() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException {
79+
SSLWebSocketClient client = testIssueWithLocalServer("localhost", SocketUtil.getAvailablePort(), SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), "HTTPS");
80+
assertTrue(client.onOpen);
81+
assertFalse(client.onSSLError);
82+
}
83+
@Test(timeout=2000)
84+
public void test_localServer_ServerLocalhost_ClientLocalhost_CheckInactive() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException {
85+
SSLWebSocketClient client = testIssueWithLocalServer("localhost", SocketUtil.getAvailablePort(), SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), "");
86+
assertTrue(client.onOpen);
87+
assertFalse(client.onSSLError);
88+
}
89+
90+
@Test(timeout=2000)
91+
public void test_localServer_ServerLocalhost_ClientLocalhost_CheckDefault() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, URISyntaxException, InterruptedException {
92+
SSLWebSocketClient client = testIssueWithLocalServer("localhost", SocketUtil.getAvailablePort(), SSLContextUtil.getLocalhostOnlyContext(), SSLContextUtil.getLocalhostOnlyContext(), null);
93+
assertTrue(client.onOpen);
94+
assertFalse(client.onSSLError);
95+
}
96+
97+
98+
public SSLWebSocketClient testIssueWithLocalServer(String address, int port, SSLContext serverContext, SSLContext clientContext, String endpointIdentificationAlgorithm) throws IOException, URISyntaxException, InterruptedException {
99+
CountDownLatch countServerDownLatch = new CountDownLatch(1);
100+
SSLWebSocketClient client = new SSLWebSocketClient(address, port, endpointIdentificationAlgorithm);
101+
WebSocketServer server = new SSLWebSocketServer(port, countServerDownLatch);
102+
103+
server.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(serverContext));
104+
if (clientContext != null) {
105+
client.setSocketFactory(clientContext.getSocketFactory());
106+
}
107+
server.start();
108+
countServerDownLatch.await();
109+
client.connectBlocking(1, TimeUnit.SECONDS);
110+
return client;
111+
}
112+
113+
114+
private static class SSLWebSocketClient extends WebSocketClient {
115+
private final String endpointIdentificationAlgorithm;
116+
public boolean onSSLError = false;
117+
public boolean onOpen = false;
118+
119+
public SSLWebSocketClient(String address, int port, String endpointIdentificationAlgorithm) throws URISyntaxException {
120+
super(new URI("wss://"+ address + ':' +port));
121+
this.endpointIdentificationAlgorithm = endpointIdentificationAlgorithm;
122+
}
123+
124+
@Override
125+
public void onOpen(ServerHandshake handshakedata) {
126+
this.onOpen = true;
127+
}
128+
129+
@Override
130+
public void onMessage(String message) {
131+
}
132+
133+
@Override
134+
public void onClose(int code, String reason, boolean remote) {
135+
}
136+
137+
@Override
138+
public void onError(Exception ex) {
139+
if (ex instanceof SSLHandshakeException) {
140+
this.onSSLError = true;
141+
}
142+
}
143+
144+
@Override
145+
protected void onSetSSLParameters(SSLParameters sslParameters) {
146+
if (endpointIdentificationAlgorithm != null) {
147+
sslParameters.setEndpointIdentificationAlgorithm(endpointIdentificationAlgorithm);
148+
}
149+
}
150+
151+
};
152+
153+
154+
private static class SSLWebSocketServer extends WebSocketServer {
155+
private final CountDownLatch countServerDownLatch;
156+
157+
158+
public SSLWebSocketServer(int port, CountDownLatch countServerDownLatch) {
159+
super(new InetSocketAddress(port));
160+
this.countServerDownLatch = countServerDownLatch;
161+
}
162+
163+
@Override
164+
public void onOpen(WebSocket conn, ClientHandshake handshake) {
165+
}
166+
167+
@Override
168+
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
169+
}
170+
171+
@Override
172+
public void onMessage(WebSocket conn, String message) {
173+
174+
}
175+
176+
@Override
177+
public void onError(WebSocket conn, Exception ex) {
178+
ex.printStackTrace();
179+
}
180+
181+
@Override
182+
public void onStart() {
183+
countServerDownLatch.countDown();
184+
}
185+
}
186+
}
-194 Bytes
Binary file not shown.
Binary file not shown.

src/test/java/org/java_websocket/util/SSLContextUtil.java

+24-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
import javax.net.ssl.TrustManagerFactory;
3232
import java.io.File;
3333
import java.io.FileInputStream;
34-
import java.io.FileNotFoundException;
3534
import java.io.IOException;
35+
import java.nio.file.Paths;
3636
import java.security.*;
3737
import java.security.cert.CertificateException;
3838

@@ -41,7 +41,29 @@ public class SSLContextUtil {
4141
public static SSLContext getContext() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException {
4242
// load up the key store
4343
String STORETYPE = "JKS";
44-
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator);
44+
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore.jks").toString();
45+
String STOREPASSWORD = "storepassword";
46+
String KEYPASSWORD = "keypassword";
47+
48+
KeyStore ks = KeyStore.getInstance(STORETYPE);
49+
File kf = new File(KEYSTORE);
50+
ks.load(new FileInputStream(kf), STOREPASSWORD.toCharArray());
51+
52+
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
53+
kmf.init(ks, KEYPASSWORD.toCharArray());
54+
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
55+
tmf.init(ks);
56+
57+
SSLContext sslContext = null;
58+
sslContext = SSLContext.getInstance("TLS");
59+
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
60+
return sslContext;
61+
}
62+
63+
public static SSLContext getLocalhostOnlyContext() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException {
64+
// load up the key store
65+
String STORETYPE = "JKS";
66+
String KEYSTORE = Paths.get("src", "test", "java", "org", "java_websocket", "keystore_localhost_only.jks").toString();
4567
String STOREPASSWORD = "storepassword";
4668
String KEYPASSWORD = "keypassword";
4769

0 commit comments

Comments
 (0)