SecTrustCreateWithCertificates在iPad上崩溃

5

我正在尝试使用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);

上面的代码在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'

你能发布证书吗? - rook
1个回答

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)

记得在适当的范围内CFRelease(array)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接