Skip to content
Snippets Groups Projects
Commit 79409e8a authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Implement certificate pinning for report requests

parent ee55cb34
No related branches found
No related tags found
3 merge requests!71Releasing v1.1.5 (214),!69Implemented filtering for banned/blocked users and reporting,!67v1.1.5 b(203)
...@@ -45,6 +45,36 @@ extension SendReport { ...@@ -45,6 +45,36 @@ extension SendReport {
) )
} }
private class SessionDelegate: NSObject, URLSessionDelegate { private final class SessionDelegate: NSObject, URLSessionDelegate {
// TODO: handle TLS func urlSession(
_ session: URLSession,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
) {
let authenticationMethod = challenge.protectionSpace.authenticationMethod
if authenticationMethod == NSURLAuthenticationMethodServerTrust,
let serverTrust = challenge.protectionSpace.serverTrust,
handleServerTrustChallenge(serverTrust) {
completionHandler(.useCredential, URLCredential(trust: serverTrust))
return
}
completionHandler(.cancelAuthenticationChallenge, nil)
}
}
private func handleServerTrustChallenge(_ serverTrust: SecTrust) -> Bool {
guard let serverCert = SecTrustGetCertificateAtIndex(serverTrust, 0) else {
return false
}
let serverCertCFData = SecCertificateCopyData(serverCert)
let serverCertNSData = NSData(
bytes: CFDataGetBytePtr(serverCertCFData),
length: CFDataGetLength(serverCertCFData)
)
let localCertPath = Bundle.module.path(forResource: "report_cert", ofType: "crt")!
let localCertNSData = NSData(contentsOfFile: localCertPath)!
return serverCertNSData.isEqual(to: localCertNSData as Data)
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment