Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,28 @@ public class HttpCertificatePinningPlugin : FlutterPlugin, MethodCallHandler {


private fun checkConnexion(serverURL: String, allowedFingerprints: List<String>, httpHeaderArgs: Map<String, String>, timeout: Int, type: String): Boolean {
val sha: String = this.getFingerprint(serverURL, timeout, httpHeaderArgs, type)
return allowedFingerprints.map { fp -> fp.toUpperCase().replace("\\s".toRegex(), "") }.contains(sha)
}

@Throws(IOException::class, NoSuchAlgorithmException::class, CertificateException::class, CertificateEncodingException::class, SocketTimeoutException::class)
private fun getFingerprint(httpsURL: String, connectTimeout: Int, httpHeaderArgs: Map<String, String>, type: String): String {
val fingerprints: Set<String> = this.getFingerprints(serverURL, timeout, httpHeaderArgs, type)
val normalizedAllowedFingerprints = allowedFingerprints.map { fp -> fp.toUpperCase().replace("\\s".toRegex(), "") }

val url = URL(httpsURL)
val httpClient: HttpsURLConnection = url.openConnection() as HttpsURLConnection
if (connectTimeout > 0)
httpClient.connectTimeout = connectTimeout * 1000
httpHeaderArgs.forEach { (key, value) -> httpClient.setRequestProperty(key, value) }
// Check if at least one fingerprint matches the allowed list
return fingerprints.any { fingerprint -> normalizedAllowedFingerprints.contains(fingerprint) }
}

httpClient.connect()

val cert: Certificate = httpClient.serverCertificates[0] as Certificate
return this.hashString(type, cert.encoded)
@Throws(IOException::class, NoSuchAlgorithmException::class, CertificateException::class, CertificateEncodingException::class, SocketTimeoutException::class)
private fun getFingerprints(httpsURL: String, connectTimeout: Int, httpHeaderArgs: Map<String, String>, type: String): Set<String> {
val url = URL(httpsURL)
val httpClient: HttpsURLConnection = url.openConnection() as HttpsURLConnection
if (connectTimeout > 0)
httpClient.connectTimeout = connectTimeout * 1000
httpHeaderArgs.forEach { (key, value) -> httpClient.setRequestProperty(key, value) }

httpClient.connect()

val fingerprints = httpClient.serverCertificates.map { cert ->
this.hashString(type, (cert as Certificate).encoded)
}.toSet() // Convert the list to a set to remove duplicates
return fingerprints
}

private fun hashString(type: String, input: ByteArray) =
Expand Down