PeculiarVentures / PKI.js

PKI.js is a pure JavaScript library implementing the formats that are used in PKI applications (signing, encryption, certificate requests, OCSP and TSP requests/responses). It is built on WebCrypto (Web Cryptography API) and requires no plug-ins.
http://pkijs.org
Other
1.25k stars 204 forks source link

Is it possible to convert an x509 certificate object back to a file. #282

Closed jegan019 closed 3 years ago

jegan019 commented 3 years ago

I am trying to convert an x509 certificate object back into a PEM string that can be written to a file. The certificate object was generated by using PKI.js to parse a DER encoded certificate retrieved through Firefox's API (WebRequest.CertificateInfo). Is there a way to do this through PKI.js?

rmhrisk commented 3 years ago

I think you are asking how you can convert DER to PEM?

jegan019 commented 3 years ago

I think you are asking how you can convert DER to PEM?

Not really. So WebRequest.CertificateInfo contains an attribute called rawDER which is an array of numbers and represents the DER encoding for the certificate. I used PKI.js to parse this using the following code: const asn1 = asn1js.fromBER(certificate); let x509 = new Certificate({ schema: asn1.result }); I had to play around with the rawDER before I parsed it. But now I have a x509 certificate object. Now I am trying to write the certificate to a file. Either convert the x509 to PEM and then write it or any other way. All the examples I have seen are to convert PEM, DER to the x509 object but no examples to do the opposite.

rmhrisk commented 3 years ago

You can look at the “complex certificate example” for how to encode to PEM.

YuryStrozhevsky commented 3 years ago

@jegan019 Please aware that “examples “ folder exists only in development version of PKIJS. I mean on GitHub repo only. You will not have it after “npm I pkijs”.

jegan019 commented 3 years ago

You can look at the “complex certificate example” for how to encode to PEM.

Thank you very much. I think I have found it.

jegan019 commented 3 years ago

@jegan019 Please aware that “examples “ folder exists only in development version of PKIJS. I mean on GitHub repo only. You will not have it after “npm I pkijs”.

Yes I am aware of that. My usage of pkijs is through npm but I did download the repo to took through the code.

themighty1 commented 3 years ago

Here's what it takes to convert a Certificate back to ArrayBuffer

cert.toSchema(true).toBER(false)