文档说:“如果不是所有的证书来验证叶子证书所需的证书,则包含在信托管理对象中,然后在钥匙扣搜索列表中搜索证书(请参阅SectrustSetKeyChains)和系统的锚固证书存储(请参阅SectRustSetSetanchOrcertifites) 。”

但是,由于iOS上没有SectRustSetKeyChains()SectRustSetKeyChains(),因此尚不清楚此功能是否还会在应用程序的键链中查看。

有帮助吗?

解决方案 3

Apple Devforums的Eskimo1回答了这一点:

  1. Sectrustevaluate()是否在应用程序键链中寻找根证书?

默认情况下不是。但是,通过将证书从钥匙扣(或任何地方)中获取,并使用sectrustSetnanchorCertificates将证书施加到Sectrust对象中,从而使其很容易做到这一点。

SectrustEvaluation / Will / Will / Wind键链中的中间证书。

其他提示

似乎已经有一段时间以来已经发布了,所以我不确定您是否仍然需要答案。如果您的用例是“我被击中 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

它写在文档中:

注意:尽管此功能会搜索用户的键链(或iOS中的应用程序键链)中的中间证书,但它不会搜索这些钥匙扣以获取锚定(root)证书。要添加锚固证书,您必须调用sectrustSetanchorCertificates。

资源: SectrustEvaluate文档

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top