SecTrustEvaluate()是否在应用程序密钥链中查找根证书?

该文档说:“如果不是所有需要validation叶证书的证书都包含在信任pipe理对象中,那么SecTrustEvaluate会在钥匙串search列表(请参见SecTrustSetKeychains)和系统的锚证书存储区中search证书(请参阅SecTrustSetAnchorCertificates) “。

但是,由于SecTrustSetKeychains()在iOS上不可用,因此不清楚这个函数是否也会在应用程序的钥匙串中查找。

似乎已经过去了一段时间,所以我不知道你是否仍然需要答案。 如果你的用例是“我遇到了connection:didReceiveAuthenticationChallenge:我想确保正在评估确切的证书,那么你可以使用iOS内置的信任方法,或者做更多的工作基础API:(注意SecTrustEvaulate在这里没有被特别的调用,但是可以很容易地添加)

 #import <Security/Security.h> #import <CommonCrypto/CommonDigest.h> 

从那里,您可以迭代完整的证书数组,并将其与挑战的服务器信任引用的SHA1进行比较:

 // way #1 - iOS built-in ================================================ // SecTrustRef trust = challenge.protectionSpace.serverTrust; CFIndex cnt = SecTrustGetCertificateCount(trust); // way #2 - build it in yourself from a file ============================ // OSErr err; NSString *path = [[NSBundle mainBundle] pathForResource:@"my.cert" ofType:@"der"]; NSData *derData = [NSData dataWithContentsOfFile:path]; SecCertificateRef myCert = SecCertificateCreateWithData(NULL, (CFDataRef)derData); CFMutableArrayRef array = CFArrayCreateMutable(NULL, 1, NULL); CFArrayInsertValueAtIndex(array, 0, myCert); err = SecTrustSetAnchorCertificates(trust, array); if (err != errSecSuccess) { // do something smarter here, obviously, logging would be a start abort(); } CFArrayRef certs = NULL; err = SecTrustCopyCustomAnchorCertificates(trust, &certs); if (err != errSecSuccess) { // again, better choices needed abort(); } CFIndex cnt = CFArrayGetCount(certs); // loop and compare 'em for (int i = 0; i < cnt; i++) { SecCertificateRef cert = SecTrustGetCertificateAtIndex(trust, i); CFDataRef cdata = SecCertificateCopyData(cert); NSData *data = [[NSData alloc] initWithData:(NSData *)cdata]; unsigned char digest_result[CC_SHA1_DIGEST_LENGTH]; CC_SHA1(data.bytes, data.length, digest_result); // compare each byte with your in-code SHA1 bytes if (allBytesMatch) { NSURLCredential *cred = [NSURLCredential credentialForTrust:trust]; [challenge.sender useCredential:cred forAuthenticationChallenge:challenge]; } } // don't forget to release & CFRelease all the alloc'ed stuff from above 

现在写在文档中:

注意:虽然此函数search用户的钥匙串(或iOS中的应用程序钥匙串)以获取中间证书,但它不会search锚(根)证书的钥匙串。 要添加锚定证书,您必须调用SecTrustSetAnchorCertificates。

来源: SecTrustEvaluate文档

来自Apple Devforums的eskimo1回答如下:

  1. SecTrustEvaluate()是否在应用程序密钥链中查找根证书?

不是默认的。 但是,通过从您的钥匙串(或从任何地方)获取证书并使用SecTrustSetAnchorCertificates将它们应用于SecTrust对象,很容易。

SecTrustEvaluation /将/在您的钥匙串中find中间证书。