Search code examples
iosobjective-cnode.jssecuritycsr

Convert NSData to PEM to Sign Certificate in Node.js


I am creating a CSR using this library on iOS and then encoding it as Base 64. https://github.com/ateska/ios-csr

The library creates the CSR as NS Data on iOS.

I am able to send this data to my Node.JS server. I want to convert this to a PEM so that I can sign this CSR using the server's private key. Does anyone know how to do this?

Thanks


Solution

  • With SCCCSR from ios-csr library you will have a certificate request in PKCS#10 format, encoded in binary

    let certificateRequest = sccsr.build(publicKey, privateKey: privateKey)
    

    A PEM format requires a conversion to Base64 and around with the -----BEGIN CERTIFICATE REQUEST----- and -----END CERTIFICATE REQUEST----- headers

    let certificateRequestB64 = certificateRequest.base64EncodedStringWithOptions(NSDataBase64EncodingOptions())
    let certificateRequestPEM =
            "-----BEGIN CERTIFICATE REQUEST-----\\n" + certificateRequestB64 + "\\n-----END CERTIFICATE REQUEST-----\\n"