Skip to content

Commit 4508945

Browse files
committed
Fix assumption regarding when an SSLSocket does the TLS handhsake
1 parent e842acf commit 4508945

File tree

3 files changed

+26
-63
lines changed

3 files changed

+26
-63
lines changed

java/ql/lib/semmle/code/java/frameworks/Networking.qll

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ class SocketGetInputStreamMethod extends Method {
4747
}
4848
}
4949

50+
class SocketGetOutputStreamMethod extends Method {
51+
SocketGetOutputStreamMethod() {
52+
this.getDeclaringType() instanceof TypeSocket and
53+
this.hasName("getOutputStream") and
54+
this.hasNoParameters()
55+
}
56+
}
57+
5058
/** A method or constructor call that returns a new `URI`. */
5159
class UriCreation extends Call {
5260
UriCreation() {
@@ -152,7 +160,7 @@ class UrlOpenConnectionMethod extends Method {
152160
class CreateSocketMethod extends Method {
153161
CreateSocketMethod() {
154162
this.hasName("createSocket") and
155-
this.getDeclaringType() instanceof TypeSocketFactory
163+
this.getDeclaringType().getASupertype*() instanceof TypeSocketFactory
156164
}
157165
}
158166

java/ql/lib/semmle/code/java/security/UnsafeCertTrust.qll

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@ private import semmle.code.java.dataflow.DataFlow2
1313
*/
1414
class SslConnectionInit extends DataFlow::Node {
1515
SslConnectionInit() {
16-
this.asExpr().(MethodAccess).getMethod() instanceof CreateSslEngineMethod or
17-
this.asExpr().(MethodAccess).getMethod() instanceof CreateSocketMethod
16+
exists(MethodAccess ma, Method m |
17+
this.asExpr() = ma and
18+
ma.getMethod() = m
19+
|
20+
m instanceof CreateSslEngineMethod
21+
or
22+
m instanceof CreateSocketMethod and isSslSocket(ma)
23+
)
1824
}
1925
}
2026

@@ -29,21 +35,11 @@ class SslConnectionCreation extends DataFlow::Node {
2935
m instanceof BeginHandshakeMethod or
3036
m instanceof SslWrapMethod or
3137
m instanceof SslUnwrapMethod or
32-
m instanceof SocketConnectMethod
38+
m instanceof SocketGetOutputStreamMethod
3339
|
3440
ma.getMethod() = m and
3541
this.asExpr() = ma.getQualifier()
3642
)
37-
or
38-
// calls to SocketFactory.createSocket with parameters immediately create the connection
39-
exists(MethodAccess ma, Method m |
40-
ma.getMethod() = m and
41-
m instanceof CreateSocketMethod and
42-
m.getNumberOfParameters() > 0 and
43-
isSslSocket(ma)
44-
|
45-
this.asExpr() = ma
46-
)
4743
}
4844
}
4945

java/ql/test/query-tests/security/CWE-273/UnsafeCertTrustTest.java

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import java.net.InetSocketAddress;
21
import java.net.Socket;
32
import java.nio.ByteBuffer;
43
import javax.net.SocketFactory;
@@ -25,9 +24,6 @@ public void testSSLEngineEndpointIdSetNull() throws Exception {
2524
sslEngine.unwrap(null, null, 0, 0); // $hasUnsafeCertTrust
2625
}
2726

28-
/**
29-
* Test the endpoint identification of SSL engine is set to null
30-
*/
3127
public void testSSLEngineEndpointIdSetEmpty() throws Exception {
3228
SSLContext sslContext = SSLContext.getInstance("TLS");
3329
SSLEngine sslEngine = sslContext.createSSLEngine();
@@ -39,9 +35,6 @@ public void testSSLEngineEndpointIdSetEmpty() throws Exception {
3935
sslEngine.unwrap(null, null, 0, 0); // $hasUnsafeCertTrust
4036
}
4137

42-
/**
43-
* Test the endpoint identification of SSL engine is set to HTTPS
44-
*/
4538
public void testSSLEngineEndpointIdSafe() throws Exception {
4639
SSLContext sslContext = SSLContext.getInstance("TLS");
4740
SSLEngine sslEngine = sslContext.createSSLEngine();
@@ -53,9 +46,6 @@ public void testSSLEngineEndpointIdSafe() throws Exception {
5346
sslEngine.unwrap(null, null, 0, 0); // Safe
5447
}
5548

56-
/**
57-
* Test the endpoint identification of SSL engine is set to HTTPS
58-
*/
5949
public void testSSLEngineInServerMode() throws Exception {
6050
SSLContext sslContext = SSLContext.getInstance("TLS");
6151
SSLEngine sslEngine = sslContext.createSSLEngine();
@@ -65,95 +55,64 @@ public void testSSLEngineInServerMode() throws Exception {
6555
sslEngine.unwrap(null, null, 0, 0); // Safe
6656
}
6757

68-
/**
69-
* Test the endpoint identification of SSL socket is not set
70-
*/
71-
public void testSSLSocketImmediatelyConnects() throws Exception {
72-
SSLContext sslContext = SSLContext.getInstance("TLS");
73-
SocketFactory socketFactory = sslContext.getSocketFactory();
74-
SSLSocket socket = (SSLSocket) socketFactory.createSocket("www.example.com", 443); // $hasUnsafeCertTrust
75-
}
76-
77-
/**
78-
* Test the endpoint identification of SSL socket is not set
79-
*/
8058
public void testSSLSocketEndpointIdNotSet() throws Exception {
8159
SSLContext sslContext = SSLContext.getInstance("TLS");
8260
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
8361
SSLSocket socket = (SSLSocket) socketFactory.createSocket();
84-
socket.connect(new InetSocketAddress("www.example.com", 443)); // $hasUnsafeCertTrust
62+
socket.getOutputStream(); // $hasUnsafeCertTrust
8563
}
8664

87-
/**
88-
* Test the endpoint identification of SSL socket is set to null
89-
*/
9065
public void testSSLSocketEndpointIdSetNull() throws Exception {
9166
SSLContext sslContext = SSLContext.getInstance("TLS");
9267
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
9368
SSLSocket socket = (SSLSocket) socketFactory.createSocket();
9469
SSLParameters sslParameters = socket.getSSLParameters();
9570
sslParameters.setEndpointIdentificationAlgorithm(null);
9671
socket.setSSLParameters(sslParameters);
97-
socket.connect(new InetSocketAddress("www.example.com", 443)); // $hasUnsafeCertTrust
72+
socket.getOutputStream(); // $hasUnsafeCertTrust
9873
}
9974

100-
/**
101-
* Test the endpoint identification of SSL socket is set to empty
102-
*/
10375
public void testSSLSocketEndpointIdSetEmpty() throws Exception {
10476
SSLContext sslContext = SSLContext.getInstance("TLS");
10577
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
10678
SSLSocket socket = (SSLSocket) socketFactory.createSocket();
10779
SSLParameters sslParameters = socket.getSSLParameters();
10880
sslParameters.setEndpointIdentificationAlgorithm("");
10981
socket.setSSLParameters(sslParameters);
110-
socket.connect(new InetSocketAddress("www.example.com", 443)); // $hasUnsafeCertTrust
82+
socket.getOutputStream(); // $hasUnsafeCertTrust
11183
}
11284

113-
/**
114-
* Test the endpoint identification of SSL socket is not set
115-
*/
11685
public void testSSLSocketEndpointIdAfterConnecting() throws Exception {
11786
SSLContext sslContext = SSLContext.getInstance("TLS");
11887
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
119-
SSLSocket socket = (SSLSocket) socketFactory.createSocket("www.example.com", 443); // $hasUnsafeCertTrust
88+
SSLSocket socket = (SSLSocket) socketFactory.createSocket();
89+
socket.getOutputStream(); // $hasUnsafeCertTrust
12090
SSLParameters sslParameters = socket.getSSLParameters();
12191
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
12292
socket.setSSLParameters(sslParameters);
12393
}
12494

125-
/**
126-
* Test the endpoint identification of SSL socket is not set
127-
*/
12895
public void testSSLSocketEndpointIdSafe() throws Exception {
12996
SSLContext sslContext = SSLContext.getInstance("TLS");
13097
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
13198
SSLSocket socket = (SSLSocket) socketFactory.createSocket();
13299
SSLParameters sslParameters = socket.getSSLParameters();
133100
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
134101
socket.setSSLParameters(sslParameters);
135-
socket.connect(new InetSocketAddress("www.example.com", 443)); // Safe
102+
socket.getOutputStream(); // Safe
136103
}
137104

138-
/**
139-
* Test the endpoint identification of regular socket is not set
140-
*/
141105
public void testSocketEndpointIdNotSet() throws Exception {
142106
SocketFactory socketFactory = SocketFactory.getDefault();
143-
Socket socket = socketFactory.createSocket("www.example.com", 80); // Safe
107+
Socket socket = socketFactory.createSocket("www.example.com", 80);
108+
socket.getOutputStream(); // Safe
144109
}
145110

146-
/**
147-
* Test the enableHostnameVerification of RabbitMQConnectionFactory is not set
148-
*/
149111
public void testRabbitMQFactoryEnableHostnameVerificationNotSet() throws Exception {
150112
ConnectionFactory connectionFactory = new ConnectionFactory();
151113
connectionFactory.useSslProtocol(); // $hasUnsafeCertTrust
152114
}
153115

154-
/**
155-
* Test the enableHostnameVerification of RabbitMQConnectionFactory is not set
156-
*/
157116
public void testRabbitMQFactorySafe() throws Exception {
158117
ConnectionFactory connectionFactory = new ConnectionFactory();
159118
connectionFactory.useSslProtocol(); // Safe

0 commit comments

Comments
 (0)