'How to convert .cer file to .pem in the browser
The cer file I'm reading in from a file input, I can then use the fileReader to get the contents. I was expecting the file to have start and end comments but it doesn't.
How can I convert the cer to pem in the browser, without uploading the file to a server?
What I really want is something like
openssl x509 -in cert.cer -out cert.pem
but in the browser
I've also tried a few x509 packages from npm, like...
import * as x509 from '@peculiar/x509';
...
const uploadCsr = async (fileArr: File[]): Promise<void> => {
const file = fileArr[0];
const reader = new FileReader();
reader.onload = (e: any): void => {
const cert = new x509.X509Certificate(e.target.result);
console.log('cert', cert);
};
reader.readAsBinaryString(file);
};
but the error I get is
TypeError: Unsupported format of 'raw' argument. Must be one of DER, PEM, HEX, Base64, or Base4Url
Solution 1:[1]
I had to use the readAsArrayBuffer with @peculiar/x509 and .toString('pem');
import * as x509 from '@peculiar/x509';
...
const checkCsr = async (fileArr: File[]): Promise<void> => {
setServerError('');
const file = fileArr[0];
const reader = new FileReader();
reader.onload = async (e: any): Promise<void> => {
const cert = new x509.X509Certificate(e.target.result);
const expires = Date.parse(`${cert.notAfter}`) / 1000;
const now = Math.floor(Date.now() / 1000);
if (now > expires) setServerError('Certificate has expired');
const issuerArr = cert.issuer.split('=');
if (!issuerArr[3].includes('Apple Inc'))
setServerError('Certificate not issued by Apple Inc');
const pem = cert.toString('pem');
if (!serverError) setCertificatePem(pem);
};
reader.readAsArrayBuffer(file);
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bill |
