Last active
October 23, 2018 15:24
-
-
Save qichuan/a4880e5b9e6cf62b894cdbe239c89883 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Read the pinned certificate from local (i.e., assets folder) | |
val inputStream = context.assets.open("google.crt") | |
val pinnedCertificate = CertificateFactory.getInstance("X.509") | |
.generateCertificate(inputStream) | |
// Create a request to www.google.com | |
val url = URL("https://www.google.com") | |
val httpsUrlConnection = url.openConnection() as HttpsURLConnection | |
// Establish the connection | |
httpsUrlConnection.connect() | |
// Check the certificates and see if one of the server certificates | |
// matches the pinned certificate | |
if (httpsUrlConnection.serverCertificates.contains(pinnedCertificate)) { | |
// Open stream | |
httpsUrlConnection.inputStream | |
Log.d("Pinning", "Server certificates validation successful") | |
} else { | |
Log.d("Pinning", "Server certificates validation failed") | |
throw SSLException("Server certificates validation failed for google.com") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment