[英]ios: Public Key SSL Pinning Returns Nil
我已将我的 SSL 证书的公钥作为 .crt 保存到我的 xcode 项目中,并尝试使用 Alamofire 通过 https 连接到我的网站。 不断失败的部分是使用 SecCertificateCreateWithData,它返回 nil,如下所示:
func configureAlamoFireSSLPinningWithCertificateData() {
let cert = "nameOfCert"
let pathToCert = NSBundle.mainBundle().pathForResource(cert, ofType: "crt")
let certificateData:NSData = NSData(contentsOfFile: pathToCert!)!
let localCertificate = SecCertificateCreateWithData(nil, certificateData)! //RETURNS NIL
self.serverTrustPolicy = ServerTrustPolicy.PinCertificates(
certificates: [localCertificate],
// Choose to validate the complete certificate chain, not only the certificate itself
validateCertificateChain: true,
// Check that the certificate mathches the host who provided it
validateHost: true
)
self.serverTrustPolicies = [
"nameOfTrustedServer": self.serverTrustPolicy!
]
self.afManager = Manager(
configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
}
我查看了项目中的证书,一切似乎都很好,我还可以打印 certificateData 并获得格式正确的结果。
问题是我只保存了公钥而不是整个证书? 出于明显的安全原因,我想避免这样做,但我似乎无法弄清楚为什么它在尝试创建安全证书时返回 nil。
提前致谢!
终于想通了,问题出在我的证书中,即使它看起来是正确的。 我通过 openssl 将它重新保存为 .cer 来修复它,这修复了它。 奇怪的是,当我尝试在 sublime 或记事本中将其重新保存为 .cer 时,即使 Xcode 显示它们相同,它也不起作用,直到我从每个打印 NSData 并显示它们不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.