Recently, I encountered an issue while working with an openssl certificate. Specifically, when I tried to download the certificate from my API, it returned byte arrays that I needed to convert to a PEM file in order to access them through another API.
The raw byte array format looked something like this:
0
g0
' *H
0
However, when I downloaded the certificate via Postman, the byte array appeared slightly different:
0‚
g0‚
' *†H†÷
‚
‚
0‚
This discrepancy led to errors during conversion to a PFX file format, as the other API threw the following error message:
DecodeToAsn:
ASN.1 length cannot be more than 4 bytes in definite long-form.
This error typically occurs when trying to decode data that is not ASN.1
A common cause is when decrypting ASN.1 data with an invalid password,
which results in garbage data. An attempt is made to decode the garbage bytes
as ASN.1, and this error occurs...
--DecodeToAsn
Failed to decode PFX ASN.1 for integrity verification.
Now, the challenge lies in correctly storing the byte arrays into a local file or finding a way to convert them to match the format obtained through Postman. Currently, my code writes the bytearray directly to a file using the following function:
async function downloadCertificate() {
try {
let pfx = new chilkat.Pfx();
let downloadedPfxByteArray = await Api.downloadCertificate(id);
let pfxFileLocation = `${process.cwd()}\\media\\CERTFILE.pfx`;
fs.writeFileSync(pfxFileLocation, downloadedPfxByteArray);
pfx.LoadPfxFile(pfxFileLocation, 'password');
let strPem = pfx.ToPem();
console.log(strPem);
return pemValue;
} catch (error) {
console.log(`READ PFX FAIL ! ${error}`);
}
}
If anyone has insights or solutions on how to resolve this issue, your help would be greatly appreciated. Thank you for taking the time to read!