【问题标题】:Creating .pem file programmatically in Objective-C?在 Objective-C 中以编程方式创建 .pem 文件?
【发布时间】:2013-02-07 00:26:44
【问题描述】:

我正在尝试使用 Objective-C 和 iPhone 应用程序中的 OpenSSL 库从证书签名请求以编程方式创建 PEM 文件。我按照 Adria Navarro 对这个问题的回答生成了 CSR(类型为 X509_REQ *):

Generating an OpenSSL Certificate Signing Request in iOS with Keychain stored keys

我已通过将 CSR 打印到控制台来确认它是有效的。

以下是我创建 PEM 文件 (CertificateSigningRequest.pem) 的代码。它最终创建了一个空白文件(0 字节,没有文本)。我是不是做错了什么,以至于它无法通过 PEM_write_X509_REQ 写入文件? (请注意,我通过 Organizer 下载 app 文件夹来检查文件。)

提前感谢您提供的任何帮助,如果我应该提供其他信息,请告诉我。

- (void)createPemFileWithCertificateSigningRequest:(X509_REQ *)certSigningRequest
{
    //delete existing PEM file if there is one
    [self deletePemFile];

    //create empty PEM file
    NSString *pemFilePath = [self pemFilePath];
    if (![[NSFileManager defaultManager] createFileAtPath:pemFilePath contents:nil attributes:nil])
    {
        NSLog(@"Error creating file for PEM");
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error creating file for PEM" message:[NSString stringWithFormat:@"Could not create file at the following location:\n\n%@", pemFilePath] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        return;
    }

    //get a FILE struct for the PEM file
    NSFileHandle *outputFileHandle = [NSFileHandle fileHandleForWritingAtPath:pemFilePath];
    FILE *pemFile = fdopen([outputFileHandle fileDescriptor], "w");

    //write the CSR to the PEM file
    PEM_write_X509_REQ(pemFile, certSigningRequest);
}

- (NSString *)pemFilePath
{
    NSString *documentsFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    return [documentsFolder stringByAppendingPathComponent:@"CertificateSigningRequest.pem"];
}

【问题讨论】:

    标签: iphone objective-c openssl pem csr


    【解决方案1】:

    原来我的问题是我在写入文件后没有关闭它。将最后一行添加到此方法中就可以了。

    - (void)createPemFileWithCertificateSigningRequest:(X509_REQ *)certSigningRequest
    {
        //...
    
        //write the CSR to the PEM file
        PEM_write_X509_REQ(pemFile, certSigningRequest);
    
        //close the file
        fclose(pemFile); //THIS MAKES EVERYTHING WORK =)
    }
    

    【讨论】:

    • 我找到了所有使用 RSA 的 CSR 解决方案,那么 EC 呢,你能帮我找到使用 OpenSSL 的 EC 的 CSR 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 2014-01-06
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    相关资源
    最近更新 更多