【问题标题】:Created a certificate using SecCertificateCreateWithData on iOS在 iOS 上使用 SecCertificateCreateWithData 创建证书
【发布时间】:2012-03-14 10:57:15
【问题描述】:

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

鉴于我拥有作为运行时对象所需的所有数据,我该如何构造 DER 编码的二进制数据输入?

【问题讨论】:

    标签: ios security pki


    【解决方案1】:

    这是怎么做的:

    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 应用程序中使用它,请确保您的证书包含所需的扩展,检查 here。尽管答案是 -1,但它帮助我完成了这项工作。

    【讨论】:

    • 嗨@lawick,您提出的解决方案是复制现有证书“myCertificate.cer”。我的问题是关于以编程方式创建证书而不是复制现有证书。我有所有需要的详细信息(例如密钥、用户详细信息等)。
    • 对不起,我误解了你的问题。 DER 数据已经是一个证书,因此 SecCertificateCreateWithData 方法将无法帮助您。请问为什么要用iOS自己创建证书?
    • 我需要每个用户的证书。您认为在应用程序中使用 OpenSSL 会更容易吗?
    • 不,老实说,我认为您在滥用 ssl。 ssl 证书用于代表 Web 服务器,而不是用户。除非你真的知道自己在做什么,否则我认为你应该看看here
    • 这不是一个完全无效的用例。他可能正在生成客户端证书来识别用户。证书并不严格归入服务器。但是,这样做确实违背了用户证书的主要好处:由受信任的机构颁发。如果您在设备上为每个用户生成它们并自签名,则您无法证明证书是由受信任的机构颁发的。
    【解决方案2】:

    看看 SecKeyGeneratePair 我想这就是你要找的。​​p>

    【讨论】:

    • 这不是问题的答案,应该是评论。
    猜你喜欢
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多