Search code examples
iosswiftxmlx509certificatexml-signature

How to get X509Certificate field in Swift


I’m trying to Sign a XML document in iOS. I already created a digestValue and a SignatureValue using a pfx file, but I cannot generate the X509Certificate field inside KeyInfo field. I know I only need the pfx file to generate this, but how could I do it in iOS?

Thank you.


Solution

  • I got it.

    I had to import OpenSSL lib to my project as a pod:

    pod 'OpenSSL-Universal', '~> 1.0'
    

    Then I created an Objective-C file to use the C functions of OpenSSL, and use the following functions to get the PEM value:

    • d2i_PKCS12: read certificate
    • PKCS12_parse: parse the certificate to X509 object.
    • PEM_write_bio_X509: parse the X509 data to a BIO object.
    • BIO_get_mem_ptr: parse the BIO data to a MEM data

    Then I allocated a NSString using the mem->data, and there is the Certificate PEM value.

    For more details about the functions, I followed this documentation: https://www.openssl.org/docs/man1.0.2/crypto/