@@ -10,15 +10,16 @@ import androidx.appcompat.app.AppCompatActivity
10
10
import androidx.core.content.ContextCompat
11
11
import com.android.volley.RequestQueue
12
12
import com.android.volley.toolbox.*
13
+ import com.datatheorem.android.trustkit.TrustKit
13
14
import kotlinx.coroutines.*
14
15
import okhttp3.CertificatePinner
15
16
import okhttp3.OkHttpClient
16
17
import okhttp3.Request
17
18
import java.io.BufferedInputStream
18
- import java.net.HttpURLConnection
19
19
import java.net.URL
20
20
import java.security.KeyStore
21
21
import java.security.cert.CertificateFactory
22
+ import javax.net.ssl.HttpsURLConnection
22
23
import javax.net.ssl.SSLContext
23
24
import javax.net.ssl.TrustManagerFactory
24
25
@@ -27,6 +28,8 @@ class MainActivity : AppCompatActivity() {
27
28
override fun onCreate (savedInstanceState : Bundle ? ) {
28
29
super .onCreate(savedInstanceState)
29
30
setContentView(R .layout.activity_main)
31
+
32
+ TrustKit .initializeWithNetworkSecurityConfiguration(this @MainActivity)
30
33
}
31
34
32
35
private fun onStart (@IdRes id : Int ) {
@@ -78,7 +81,7 @@ class MainActivity : AppCompatActivity() {
78
81
onStart(R .id.unpinned)
79
82
try {
80
83
val mURL = URL (" https://badssl.com" )
81
- with (mURL.openConnection() as HttpURLConnection ) {
84
+ with (mURL.openConnection() as HttpsURLConnection ) {
82
85
println (" URL: ${this .url} " )
83
86
println (" Response Code: ${this .responseCode} " )
84
87
}
@@ -97,7 +100,7 @@ class MainActivity : AppCompatActivity() {
97
100
try {
98
101
// Untrusted in system store, trusted & pinned in network config:
99
102
val mURL = URL (" https://untrusted-root.badssl.com" )
100
- with (mURL.openConnection() as HttpURLConnection ) {
103
+ with (mURL.openConnection() as HttpsURLConnection ) {
101
104
println (" URL: ${this .url} " )
102
105
println (" Response Code: ${this .responseCode} " )
103
106
}
@@ -188,4 +191,25 @@ class MainActivity : AppCompatActivity() {
188
191
onError(R .id.volley_pinned, e.toString())
189
192
}
190
193
}
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
+ }
191
215
}
0 commit comments