Skip to content

Commit 75ae928

Browse files
committed
Switch the pinned target to a genuine good URL
It's useful if that target is actually valid, because it means that intercepted requests in HTTP Toolkit will succeed, once they're captured. It can be a bit confusing otherwise!
1 parent a9f1cc8 commit 75ae928

File tree

4 files changed

+18
-55
lines changed

4 files changed

+18
-55
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import java.security.cert.CertificateFactory
2828
import java.security.cert.X509Certificate
2929
import javax.net.ssl.*
3030

31-
const val BADSSL_UNTRUSTED_ROOT_SHA256 = "sr2tjak7H6QRi8o0fyIXGWdPiU32rDsczcIEAqA+s4g="
31+
const val DIGICERT_ROOT_SHA256 = "5kJvNEMw0KjrCAu7eXY5HZdvyCS13BbA0VJG1RSP91w="
3232

3333
class MainActivity : AppCompatActivity() {
3434
override fun onCreate(savedInstanceState: Bundle?) {
@@ -86,7 +86,7 @@ class MainActivity : AppCompatActivity() {
8686
GlobalScope.launch(Dispatchers.IO) {
8787
onStart(R.id.unpinned)
8888
try {
89-
val mURL = URL("https://badssl.com")
89+
val mURL = URL("https://example.com")
9090
with(mURL.openConnection() as HttpsURLConnection) {
9191
println("URL: ${this.url}")
9292
println("Response Code: ${this.responseCode}")
@@ -104,8 +104,8 @@ class MainActivity : AppCompatActivity() {
104104
GlobalScope.launch(Dispatchers.IO) {
105105
onStart(R.id.config_pinned)
106106
try {
107-
// Untrusted in system store, trusted & pinned in network config:
108-
val mURL = URL("https://untrusted-root.badssl.com")
107+
// Pinned by hash in network config:
108+
val mURL = URL("https://sha512.badssl.com")
109109
with(mURL.openConnection() as HttpsURLConnection) {
110110
println("URL: ${this.url}")
111111
println("Response Code: ${this.responseCode}")
@@ -124,16 +124,16 @@ class MainActivity : AppCompatActivity() {
124124
onStart(R.id.okhttp_pinned)
125125

126126
try {
127-
val hostname = "badssl.com"
127+
val hostname = "sha512.badssl.com"
128128
val certificatePinner = CertificatePinner.Builder()
129-
.add(hostname, "sha256/${BADSSL_UNTRUSTED_ROOT_SHA256}")
129+
.add(hostname, "sha256/${DIGICERT_ROOT_SHA256}")
130130
.build()
131131

132132
val client = OkHttpClient.Builder()
133133
.certificatePinner(certificatePinner)
134134
.build()
135135
val request = Request.Builder()
136-
.url("https://untrusted-root.badssl.com")
136+
.url("https://sha512.badssl.com")
137137
.build();
138138

139139
client.newCall(request).execute().use { response ->
@@ -155,7 +155,7 @@ class MainActivity : AppCompatActivity() {
155155
try {
156156
// Create an HTTP client that only trusts our specific certificate:
157157
val cf = CertificateFactory.getInstance("X.509")
158-
val caStream = BufferedInputStream(resources.openRawResource(R.raw.example_com_digicert_ca))
158+
val caStream = BufferedInputStream(resources.openRawResource(R.raw.digicert_ca))
159159
val ca = cf.generateCertificate(caStream)
160160
caStream.close()
161161

@@ -179,7 +179,7 @@ class MainActivity : AppCompatActivity() {
179179
// Make a request using that client:
180180
val stringRequest = StringRequest(
181181
com.android.volley.Request.Method.GET,
182-
"https://example.com",
182+
"https://sha512.badssl.com",
183183
{ _ ->
184184
println("Volley success")
185185
this@MainActivity.onSuccess(R.id.volley_pinned)
@@ -201,10 +201,10 @@ class MainActivity : AppCompatActivity() {
201201
GlobalScope.launch(Dispatchers.IO) {
202202
onStart(R.id.trustkit_pinned)
203203
try {
204-
val mURL = URL("https://untrusted-root.badssl.com")
204+
val mURL = URL("https://sha512.badssl.com")
205205
with(mURL.openConnection() as HttpsURLConnection) {
206206
this.sslSocketFactory = TrustKit.getInstance().getSSLSocketFactory(
207-
"untrusted-root.badssl.com"
207+
"sha512.badssl.com"
208208
)
209209
println("URL: ${this.url}")
210210
println("Response Code: ${this.responseCode}")
@@ -235,19 +235,19 @@ class MainActivity : AppCompatActivity() {
235235
val context = SSLContext.getInstance("TLS")
236236
context.init(null, trustManager, null)
237237

238-
val socket = context.socketFactory.createSocket("untrusted-root.badssl.com", 443) as SSLSocket
238+
val socket = context.socketFactory.createSocket("sha512.badssl.com", 443) as SSLSocket
239239

240240
val certs = socket.session.peerCertificates
241241

242-
if (!certs.any { cert -> doesCertMatchPin(BADSSL_UNTRUSTED_ROOT_SHA256, cert) }) {
242+
if (!certs.any { cert -> doesCertMatchPin(DIGICERT_ROOT_SHA256, cert) }) {
243243
socket.close() // Close the socket immediately without sending a request
244244
throw Error("Unrecognized cert hash.")
245245
}
246246

247247
// Send a real request, just to make it clear that we trust the connection:
248248
val pw = PrintWriter(socket.outputStream)
249249
pw.println("GET / HTTP/1.1")
250-
pw.println("Host: untrusted-root.badssl.com")
250+
pw.println("Host: sha512.badssl.com")
251251
pw.println("")
252252
pw.flush()
253253

app/src/main/res/raw/badssl_untrusted_root.pem

Lines changed: 0 additions & 37 deletions
This file was deleted.

app/src/main/res/raw/example_com_digicert_ca.pem renamed to app/src/main/res/raw/digicert_ca.pem

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg
1919
PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls
2020
YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk
2121
CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=
22-
-----END CERTIFICATE-----
22+
-----END CERTIFICATE-----
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<network-security-config>
33
<domain-config>
4-
<domain includeSubdomains="false">untrusted-root.badssl.com</domain>
4+
<domain includeSubdomains="false">sha512.badssl.com</domain>
55
<pin-set>
6-
<pin digest="SHA-256">sr2tjak7H6QRi8o0fyIXGWdPiU32rDsczcIEAqA+s4g=</pin>
6+
<pin digest="SHA-256">5kJvNEMw0KjrCAu7eXY5HZdvyCS13BbA0VJG1RSP91w=</pin>
77
</pin-set>
88
<trust-anchors>
9-
<certificates src="@raw/badssl_untrusted_root" />
9+
<certificates src="@raw/digicert_ca" />
1010
</trust-anchors>
1111
</domain-config>
1212
</network-security-config>

0 commit comments

Comments
 (0)