Hi! I'm implementing SSL certificate pinning with ...
# ktor
s
Hi! I'm implementing SSL certificate pinning with soft fallback in a KMP app (Ktor 3.4.0, Darwin engine on iOS). I'm wrapping Ktor's built-in ``CertificatePinner`` in a custom ``handleChallenge`` to catch ``TlsPeerUnverifiedException`` on pin mismatch and fall back to normal TLS (instead of hard-failing):
Copy code
val pinner = CertificatePinner.Builder()
    .add("api.example.com", "sha256/leafHash=", "sha256/intermediateHash=")
    .build()

HttpClient(Darwin) {
    engine {
        handleChallenge { session, task, challenge, completionHandler ->
            try {
                pinner.invoke(session, task, challenge, completionHandler)
            } catch (e: Exception) {
                // Pin mismatch — fall back to default TLS
                completionHandler(
                    NSURLSessionAuthChallengePerformDefaultHandling, null
                )
            }
        }
    }
}
Is there a better pattern for soft-fail pinning with the Darwin engine, or is wrapping with try/catch the right approach?
a
Note that your try-catch works, but catching Exception is too broad, and you should check the exception message to distinguish pin mismatches from other failures. More importantly, if a pin mismatch silently falls back to default TLS, what does the pinner actually protect you against?