在iOS上使用SecCertificateCreateWithData创build证书

我想在iOS应用程序中以编程方式创build证书。 我能find的最接近的API是SecCertificateCreateWithData ,它需要一个DER编码的二进制input。

鉴于我有所需的所有数据作为运行时对象,我怎样才能构造DER编码的二进制数据input?

这是如何可以做到的:

NSString* certPath = [[NSBundle mainBundle] pathForResource:@"myCertificate" ofType:@"cer"]; NSData* certData = [NSData dataWithContentsOfFile:certPath]; SecCertificateRef cert; if( [certData length] ) { cert = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certData); if( cert != NULL ) { CFStringRef certSummary = SecCertificateCopySubjectSummary(cert); NSString* summaryString = [[NSString alloc] initWithString:(__bridge NSString*)certSummary]; NSLog(@"CERT SUMMARY: %@", summaryString); CFRelease(certSummary); } else { NSLog(@" *** ERROR *** trying to create the SSL certificate from data located at %@, but failed", certPath); } } // play with cert here 

myCertificate.cer必须在您的应用程序包中。 我用openssl创build了cer文件。 如果您打算在iOS应用程序中使用此function,请确保您的证书包含所需的扩展名,请点击此处 。 即使答案是-1,它帮助我得到这个运行。

看看SecKeyGeneratePair我认为这是你在找什么。