ios - 使用 objective-c 和 OpenSSL 创建 .PEM 文件

标签 ios swift objective-c security openssl

我正在尝试在 Objective-C 或 Swift 中实现以下命令行。

openssl x509 -req -in 20060.csr -CA root.pem -CAkey root.key -CAcreateserial -out 20060.pem -days 825 -sha256

我已经为我的 iOS 项目编译和构建了 openssl,并成功创建了带有一些 KeyChain 函数的 20060.csr 文件。我已经有了 root.pem 和 root.key 文件。

我如何在 Objc 中以编程方式实现这一点?

最佳答案

创建 PEM 文件 (CertificateSigningRequest.pem) 的代码。

- (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);
    //close the file
    fclose(pemFile);
}

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

关于ios - 使用 objective-c 和 OpenSSL 创建 .PEM 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66861914/

相关文章:

swift - 尝试修改表情符号序列时出现问题

Objective-C 与 C 速度

ios - 动画调整 UIView 和 subview 大小的更好方法?

ios - RMStore 恢复应用内购买

ios - iBeacon 箭头旋转

ios - 在 viewDidLoad 中异步加载图像,并带有事件指示器

swift - 应该如何根据 Swift API 设计指南调整协议(protocol)/实现对?

ios - 错误: "The sandbox is not in sync with the Podfile.lock..." after installing RestKit with cocoapods

objective-c - 想在 iOS 应用中使用 sqlite3.h

iOS - 字符串后跟数字时的大写