检查canAuthenticateAgainstProtectionSpace中的公钥

Rob*_*ert 3 iphone cryptography public-key ios

我被要求检查公钥中的已知值canAuthenticateAgainstProtectionSpace(代表回调NSURLConnection)

这是我到目前为止:

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

        NSLog(@"%@",SecTrustCopyPublicKey([protectionSpace serverTrust])); 
        return YES;
}
Run Code Online (Sandbox Code Playgroud)

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

NSLog产生:<SecKeyRef: 0x687c000>不是有用的.

Rob*_*ert 5

如果有人关心,解决方案是使用保存在捆绑包上的证书来检查证书字节的字节.

- (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;
}
Run Code Online (Sandbox Code Playgroud)