Skip to content

Commit 0d6a376

Browse files
Add test cases for TrustManager case
1 parent c8aca06 commit 0d6a376

File tree

14 files changed

+157
-0
lines changed

14 files changed

+157
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.example.app"
3+
android:installLocation="auto"
4+
android:versionCode="1"
5+
android:versionName="0.1" >
6+
7+
<application android:networkSecurityConfig="@xml/NetworkSecurityConfig">
8+
</application>
9+
10+
</manifest>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example;
2+
3+
class R {
4+
static final class raw {
5+
static final int cert = 0;
6+
}
7+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.example;
2+
3+
import java.net.URL;
4+
import java.net.URLConnection;
5+
import java.security.KeyStore;
6+
import javax.net.ssl.TrustManagerFactory;
7+
import javax.net.ssl.HttpsURLConnection;
8+
import javax.net.ssl.SSLContext;
9+
import android.content.res.Resources;
10+
11+
class Test{
12+
void test1(Resources resources) throws Exception {
13+
KeyStore keyStore = KeyStore.getInstance("BKS");
14+
keyStore.load(resources.openRawResource(R.raw.cert), null);
15+
16+
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
17+
tmf.init(keyStore);
18+
19+
SSLContext sslContext = SSLContext.getInstance("TLS");
20+
sslContext.init(null, tmf.getTrustManagers(), null);
21+
22+
URL url = new URL("http://www.example.com/");
23+
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
24+
25+
urlConnection.setSSLSocketFactory(sslContext.getSocketFactory());
26+
}
27+
28+
void test2() throws Exception {
29+
URL url = new URL("http://www.example.com/");
30+
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(); // $hasNoTrustedResult
31+
}
32+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// semmle-extractor-options: --javac-args -cp ${testdir}/../../../../../stubs/google-android-9.0.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<network-security-config>
3+
4+
</network-security-config>

java/ql/test/query-tests/security/CWE-295/AndroidMissingCertificatePinning/Test4/test.expected

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java
2+
import TestUtilities.InlineExpectationsTest
3+
import semmle.code.java.security.AndroidCertificatePinningQuery
4+
5+
class Test extends InlineExpectationsTest {
6+
Test() { this = "AndroidMissingCertificatePinningTest" }
7+
8+
override string getARelevantTag() { result = ["hasNoTrustedResult", "hasUntrustedResult"] }
9+
10+
override predicate hasActualResult(Location loc, string el, string tag, string value) {
11+
exists(DataFlow::Node node |
12+
missingPinning(node) and
13+
loc = node.getLocation() and
14+
el = node.toString() and
15+
value = "" and
16+
(
17+
if exists(string x | trustedDomain(x))
18+
then tag = "hasUntrustedResult"
19+
else tag = "hasNoTrustedResult"
20+
)
21+
)
22+
}
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.example.app"
3+
android:installLocation="auto"
4+
android:versionCode="1"
5+
android:versionName="0.1" >
6+
7+
<application android:networkSecurityConfig="@xml/NetworkSecurityConfig">
8+
</application>
9+
10+
</manifest>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example;
2+
3+
class R {
4+
static final class raw {
5+
static final int cert = 0;
6+
}
7+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.example;
2+
3+
import java.net.URL;
4+
import java.net.URLConnection;
5+
import java.io.InputStream;
6+
import java.security.KeyStore;
7+
import javax.net.ssl.TrustManagerFactory;
8+
import javax.net.ssl.HttpsURLConnection;
9+
import javax.net.ssl.SSLContext;
10+
import android.content.res.Resources;
11+
12+
class Test{
13+
void init(Resources resources) throws Exception {
14+
KeyStore keyStore = KeyStore.getInstance("BKS");
15+
keyStore.load(resources.openRawResource(R.raw.cert), null);
16+
17+
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
18+
tmf.init(keyStore);
19+
20+
SSLContext sslContext = SSLContext.getInstance("TLS");
21+
sslContext.init(null, tmf.getTrustManagers(), null);
22+
23+
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
24+
}
25+
26+
URLConnection test1() throws Exception {
27+
URL url = new URL("http://www.example.com/");
28+
return url.openConnection();
29+
}
30+
31+
InputStream test2() throws Exception {
32+
URL url = new URL("http://www.example.com/");
33+
return url.openStream();
34+
}
35+
}

0 commit comments

Comments
 (0)