簡體   English   中英

ios:公鑰 SSL 固定返回 Nil

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM