Skip to content

Commit f1c0a0f

Browse files
committed
Add a TrustKit test case too
Worked before regardless, just worth testing explicitly as it appears to be a common recommended pinning library.
1 parent 4451790 commit f1c0a0f

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ dependencies {
4343

4444
implementation 'com.squareup.okhttp3:okhttp:4.5.0'
4545
implementation 'com.android.volley:volley:1.2.0'
46+
implementation 'com.datatheorem.android.trustkit:trustkit:1.1.3'
47+
implementation 'androidx.preference:preference-ktx:1.1.1'
4648
}

app/src/main/java/tech/httptoolkit/pinning_demo/MainActivity.kt

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ import androidx.appcompat.app.AppCompatActivity
1010
import androidx.core.content.ContextCompat
1111
import com.android.volley.RequestQueue
1212
import com.android.volley.toolbox.*
13+
import com.datatheorem.android.trustkit.TrustKit
1314
import kotlinx.coroutines.*
1415
import okhttp3.CertificatePinner
1516
import okhttp3.OkHttpClient
1617
import okhttp3.Request
1718
import java.io.BufferedInputStream
18-
import java.net.HttpURLConnection
1919
import java.net.URL
2020
import java.security.KeyStore
2121
import java.security.cert.CertificateFactory
22+
import javax.net.ssl.HttpsURLConnection
2223
import javax.net.ssl.SSLContext
2324
import javax.net.ssl.TrustManagerFactory
2425

@@ -27,6 +28,8 @@ class MainActivity : AppCompatActivity() {
2728
override fun onCreate(savedInstanceState: Bundle?) {
2829
super.onCreate(savedInstanceState)
2930
setContentView(R.layout.activity_main)
31+
32+
TrustKit.initializeWithNetworkSecurityConfiguration(this@MainActivity)
3033
}
3134

3235
private fun onStart(@IdRes id: Int) {
@@ -78,7 +81,7 @@ class MainActivity : AppCompatActivity() {
7881
onStart(R.id.unpinned)
7982
try {
8083
val mURL = URL("https://badssl.com")
81-
with(mURL.openConnection() as HttpURLConnection) {
84+
with(mURL.openConnection() as HttpsURLConnection) {
8285
println("URL: ${this.url}")
8386
println("Response Code: ${this.responseCode}")
8487
}
@@ -97,7 +100,7 @@ class MainActivity : AppCompatActivity() {
97100
try {
98101
// Untrusted in system store, trusted & pinned in network config:
99102
val mURL = URL("https://untrusted-root.badssl.com")
100-
with(mURL.openConnection() as HttpURLConnection) {
103+
with(mURL.openConnection() as HttpsURLConnection) {
101104
println("URL: ${this.url}")
102105
println("Response Code: ${this.responseCode}")
103106
}
@@ -188,4 +191,25 @@ class MainActivity : AppCompatActivity() {
188191
onError(R.id.volley_pinned, e.toString())
189192
}
190193
}
194+
195+
fun sendTrustKitPinned(view: View) {
196+
GlobalScope.launch(Dispatchers.IO) {
197+
onStart(R.id.trustkit_pinned)
198+
try {
199+
val mURL = URL("https://untrusted-root.badssl.com")
200+
with(mURL.openConnection() as HttpsURLConnection) {
201+
this.sslSocketFactory = TrustKit.getInstance().getSSLSocketFactory(
202+
"untrusted-root.badssl.com"
203+
)
204+
println("URL: ${this.url}")
205+
println("Response Code: ${this.responseCode}")
206+
}
207+
208+
onSuccess(R.id.trustkit_pinned)
209+
} catch (e: Throwable) {
210+
println(e)
211+
onError(R.id.trustkit_pinned, e.toString())
212+
}
213+
}
214+
}
191215
}

app/src/main/res/layout/activity_main.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@
4444
android:onClick="sendVolleyPinned"
4545
android:text="Volley pinned request" />
4646

47+
<Button
48+
android:id="@+id/trustkit_pinned"
49+
android:layout_width="match_parent"
50+
android:layout_height="wrap_content"
51+
android:onClick="sendTrustKitPinned"
52+
android:text="TrustKit pinned request" />
53+
4754
</LinearLayout>
4855

4956
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)