iphone - 在 canAuthenticateAgainstProtectionSpace 中检查公钥

标签 iphone ios cryptography public-key

有人要求我根据 canAuthenticateAgainstProtectionSpace 中的已知值检查公钥( NSURLConnection 的委托(delegate)回调)

这是我目前所拥有的:

- (BOOL)connection:(NSURLConnection *)connection 
        canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 
    {
        SecKeyRef publicKey = SecTrustCopyPublicKey([protectionSpace serverTrust]);

        NSLog(@"%@",SecTrustCopyPublicKey([protectionSpace serverTrust])); 
        return YES;
}

如何将公钥与已知值进行比较?

NSLog 产生:<SecKeyRef: 0x687c000>这没什么用。

最佳答案

如果有人关心,解决方案是使用保存在 bundle 中的证书逐字节检查证书。

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 
{
    SecTrustRef trust = [protectionSpace serverTrust];

    SecCertificateRef certificate = SecTrustGetCertificateAtIndex(trust, 0);

    NSData* ServerCertificateData = (NSData*) SecCertificateCopyData(certificate);

    // Check if the certificate returned from the server is identical to the saved certificate in
    // the main bundle
    BOOL areCertificatesEqual = ([ServerCertificateData 
                                  isEqualToData:[MyClass getCertificate]]);

    [ServerCertificateData release];

    if (!areCertificatesEqual) 
    {    
        NSLog(@"Bad Certificate, canceling request");
        [connection cancel];
    }

    // If the certificates are not equal we should not talk to the server;
    return areCertificatesEqual;
}

关于iphone - 在 canAuthenticateAgainstProtectionSpace 中检查公钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5675808/

相关文章:

ios - 从同一部 iPhone 获取两个不同的设备 ID

iphone - xcode 地理位置 : Getting coordinates before view is loaded

ios - 错误 : index out of range when retriveing data from firebase snapshot

c - 是否可以通过分配内存来恢复 secret 数据(例如用于解密的空闲内存中的 RSA 私钥)?

c# - 为什么 .Net Framework 的加密如此复杂?

iphone - MFMailComposeViewController 不从 View 中关闭

javascript - iOS 8.3 Mobile Safari 禁用表情符号渲染

ios - 边界在带有内容插入的 UIScrollView 上自动更改

ios - 将对象发送给 RxSwift Action 的订阅者

c++ - 如何生成伪随机 32 字节字符串以用作加密哈希函数中的盐?