1

I was working with openssl certificate. So what I want to achieve is I download the certificate from my API, it returns me bytes arrays. For example:

0�
g0�
'   *�H��
��
�
0�

And I try to write these into a file, and use some other function to read and convert to PEM file so I can access it with another API. (The initial format of this cert is PFX).

I've checked and if I download the certificate via Postman, via the 'Send and Download' button I get something like this:

0‚
g0‚
'   *†H†÷
 ‚
‚
0‚

It is slightly different than what I direct wrote into a file. How can I convert from that to this? I faced error from the next step is because this is not a valid PFX file. The error from the other API reads as below:

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.

So how can I store bytes arrays into local file correctly? Or is there any way to convert the byte arrays to what I have via Postman?

As of now I only write the bytearray directly to the file, below are the codes:

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}`);
    }
}

Thanks for reading and appreciates if anyone could help!

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Sivvie Lim
  • 784
  • 2
  • 14
  • 43
  • You can convert file content to HEX, then you can get md5 of file for comparing after, or you can create a zip file with this file(s). – dnaz Oct 29 '19 at 08:14
  • @dnaz Hmm not sure if the other API accepts hex file, I think it only accepts PFX file, do you mean that after I get the bytearray, I store it into the file as hex? – Sivvie Lim Oct 29 '19 at 08:32
  • Yes, if files dont have big size. – dnaz Oct 30 '19 at 07:09

1 Answers1

0
#!/usr/bin/env node
const fetch = require('node-fetch');
const fs = require('fs').promises;

async function main() {
    await fs.writeFile(
        '/tmp/so-58603015.pfx',
        Buffer.from(await (await fetch('…url…')).arrayBuffer())
    );
}

main();
daxim
  • 39,270
  • 4
  • 65
  • 132
  • Yes line 17 is the code that I will extract the cert and key from PFX file, but it probably is not a valid PFX file thus it throws error. I have my byte array in local so maybe no need fetch? And this is local console application also. Thanks! – Sivvie Lim Oct 29 '19 at 09:26
  • 1
    Consider providing some accompanying text that describes how your code solves the problem and how it works. – Wyck Oct 29 '19 at 13:27
  • 1
    Vague question, vague answer. – daxim Oct 29 '19 at 13:29
  • 1
    I've added codes and complete error to hope making it less vague then. – Sivvie Lim Oct 30 '19 at 00:05