diff --git a/android/src/main/kotlin/diefferson/http_certificate_pinning/HttpCertificatePinningPlugin.kt b/android/src/main/kotlin/diefferson/http_certificate_pinning/HttpCertificatePinningPlugin.kt index 55a280d..3126708 100644 --- a/android/src/main/kotlin/diefferson/http_certificate_pinning/HttpCertificatePinningPlugin.kt +++ b/android/src/main/kotlin/diefferson/http_certificate_pinning/HttpCertificatePinningPlugin.kt @@ -95,23 +95,28 @@ public class HttpCertificatePinningPlugin : FlutterPlugin, MethodCallHandler { private fun checkConnexion(serverURL: String, allowedFingerprints: List, httpHeaderArgs: Map, 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, type: String): String { + val fingerprints: Set = 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, type: String): Set { + 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) =