SecTrustCreateWithCertificates在iPad上崩溃

loh*_*das 5 security iphone certificate ipad public-key

我正在尝试使用iOS安全框架来安全地与我的服务器通信.我有一个证书文件,我可以从中获得公钥引用.这就是我在做的事情.

 NSString *certPath    = [[NSBundle mainBundle] pathForResource:@"supportwarriors.com" ofType:@"cer"];
 SecCertificateRef myCertificate = nil;

 NSData *certificateData   = [[NSData alloc] initWithContentsOfFile:certPath]; 
 myCertificate     = SecCertificateCreateWithData(kCFAllocatorDefault, (CFDataRef)certificateData);

 //got certificate ref..Now get public key secKeyRef reference from certificate..
 SecPolicyRef myPolicy   = SecPolicyCreateBasicX509();
 SecTrustRef myTrust;
 OSStatus status     = SecTrustCreateWithCertificates(myCertificate,myPolicy,&myTrust);  

    SecTrustResultType trustResult;
    if (status == noErr) {
        status = SecTrustEvaluate(myTrust, &trustResult);  
    }
 publicKey      = SecTrustCopyPublicKey(myTrust);
Run Code Online (Sandbox Code Playgroud)

上面的代码完全适用于iPhone,我测试过.我能够安全地与我的服务器通信.但是当我尝试在iPad上运行我的应用程序时(在2x模式下)上面的代码崩溃了.调试之后,我发现secTrustCreateWithCertificate崩溃并且崩溃日志在下面给出.我使用的证书对于iPad和iPhone都是相同的...上面的函数secCertificateCreateWithData返回证书引用并且不是nil ...所以这是不是崩溃的原因.我做错了什么.

*** -[NSCFType count]: unrecognized selector sent to instance 0x14af24
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '***      -[NSCFType count]: unrecognized selector sent to instance 0x14af24'
Run Code Online (Sandbox Code Playgroud)

Chr*_*nak 4

文档声称SecTrustCreateWithCertificates您可以传递单个证书或数组。您收到的异常表明-[NSCFType count]: unrecognized selector sent to instance. iOS 3.2 中发生的情况是SecTrustCreateWithCertificates将输入值视为 CFArray,而不首先检查它是否是单数SecCertificateRef

为了解决这个问题,您可以执行类似于以下代码的操作:

    SecCertificateRef certs[1] = { certificate };
    CFArrayRef array = CFArrayCreate(NULL, (const void **) certs, 1, NULL);
    if(SecTrustCreateWithCertificates(array, x509Policy, &trustChain) == errSecSuccess)
Run Code Online (Sandbox Code Playgroud)

只要记住CFRelease(array)在适当的范围内即可。