我已经将我的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并获得正确格式化的结果。
问题是我只保存了公钥,而没有保存整个证书吗?出于明显的安全原因,我想避免这样做,但我似乎不明白为什么在尝试创建Sec证书时它返回nil。
提前感谢!
发布于 2016-04-08 12:51:23
最后弄清楚了,问题出在我的证书上,尽管它看起来是正确的。我修复了它,通过openssl将它重新保存为.cer,这就修复了它。奇怪的是,当我试图在sublime或notepad中将其重新保存为.cer时,即使Xcode显示它们是相同的,它也无法工作,直到我打印了它们的NSData,它显示它们是不同的。
https://stackoverflow.com/questions/36482311
复制相似问题