我在我的IOS应用程序中使用Alamofire,并实现了SSL Pinning。我已经将证书以.der格式保存在本地。问题是,当证书过期时,我必须发布新证书的应用程序版本。
使用的代码片段:
struct Certificates {
fileprivate static let sslCertificate: SecCertificate? = Certificates.certificate(filename: (Bundle.main.infoDictionary?["SSL_CERTIFICATE"] as? String))
private static func certificate(filename: String?) -> SecCertificate? {
guard let filePath = Bundle.main.path(forResource: filename, ofType: "der"), let data = try? Data(contentsOf: URL(fileURLWithPath: filePath)), let certificate = SecCertificateCreateWithData(nil, data as CFData) else{
return nil
}
return certificate
}
final class APIClient {
// MARK: - Host To Evaluate
private static var hostToEvaluate: String {
guard let urlString = Bundle.main.infoDictionary?["API_BASE_URL_ENDPOINT"] as? String, let hostURL = URL(string: urlString), let host = hostURL.host else{
return ""
}
return host
}
// MARK: - Evaluators
private static var evaluators: Dictionary<String,ServerTrustEvaluating> {
guard let sslCertificate = Certificates.sslCertificate else{
return [hostToEvaluate : DisabledTrustEvaluator()]
}
return [hostToEvaluate : PinnedCertificatesTrustEvaluator(certificates: [sslCertificate])]
}
// MARK: - Session
private static let session = Session(
serverTrustManager: ServerTrustManager(evaluators: evaluators)
)
}
我能保留两个证书并进行检查以评估现有证书,如果失败则使用新证书吗?
我已经查看了一些文档和论坛,但没有找到相关的内容。
谢谢,
Abin