有人要求我根据 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/