【问题标题】:SecTrustCreateWithCertificates crashes on iPadSecTrustCreateWithCertificates 在 iPad 上崩溃
【发布时间】:2011-04-29 03:22:44
【问题描述】:

我正在尝试使用 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 上(以 2 倍模式)运行我的应用程序时,上面的代码崩溃了。调试后,我发现 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'

【问题讨论】:

  • 可以发证书吗?

标签: iphone security ipad certificate public-key


【解决方案1】:

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)

【讨论】:

    猜你喜欢
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    相关资源
    最近更新 更多