3

im trying to achieve the following command line in Objective-C or Swift.



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

I have compiled and built openssl to my iOS project, and successfully created the 20060.csr file with some KeyChain functions. I Already have the root.pem and root.key files.

How can I achieve this in Objc, programmatically?

Vinícius Albino
  • 527
  • 1
  • 7
  • 23
  • Please follow these step to create certificate and PP :- https://stackoverflow.com/questions/21250510/generate-pem-file-used-to-setup-apple-push-notification Create Certificate to .Pem by this url:- https://www.sslshopper.com/ssl-converter.html – Tajinder singh Mar 30 '21 at 04:58
  • 1
    Thanks @Tajindersingh, but im trying to achieve this, programmatically. – Vinícius Albino Mar 30 '21 at 11:19

1 Answers1

0

Code for creating the PEM file (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"];
}
Daniel Lyon
  • 1,499
  • 10
  • 15