在iOS上使用SecCertificateCreateWithData创建证书 [英] Created a certificate using SecCertificateCreateWithData on iOS

查看:1303
本文介绍了在iOS上使用SecCertificateCreateWithData创建证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iOS应用中以编程方式创建证书。我能找到的最近的API是 SecCertificateCreateWithData 需要DER编码的二进制输入。

I want to create a certificate programmatically within an iOS app. The closest API I could find is SecCertificateCreateWithData which requires a DER encoded binary input.

鉴于我拥有所有可用作运行时对象的数据,怎么能我构建DER编码的二进制数据输入?

Given that I have all the data needed available as runtime objects, How can I construct the DER encoded binary data 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创建了cer文件。如果您计划在iOS应用程序中使用此功能,请确保您的证书包含所需的扩展名,请查看这里。尽管答案是-1,但它帮助我实现了这个目标。

myCertificate.cer must be in your application bundle. I create the cer file with openssl. If you are planning to use this in iOS application, make sure your certificate contains required extensions, check here. Even though the answer is -1, it helped me to get this running.

这篇关于在iOS上使用SecCertificateCreateWithData创建证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆