I have the following files available:
- .cer file (DER encoded)
- .pvk file
- .pfx file (password protected file containing both the .cer and .pvk files together)
- passphrase
I've found solutions here and here suggesting I can use my .pfx file directly. The examples use .p12 files, while mine is .pfx - I believe they are the same thing.
const fs = require('fs');
const axios = require('axios');
const https = require('https');
const response = await axios.request({
url: *URL_HERE*,
method: 'post',
headers: {
"content-type": "application/json",
},
data: JSON.stringify({
*YOUR_DATA_HERE*
}),
httpsAgent: new https.Agent({
passphrase: *PASSPHRASE_HERE*,
pfx: fs.readFileSync(__dirname + '\\..\\certificates\\certificate.p12'),
})
});
But this gives me the following error:
Error: header too long at Object.createSecureContext
I think this error usually means an incorrect file format - expecting headers of a different length because it's the wrong format.
NodeJS documentation on tls.createSecureContext suggests I need to be converting my .cer file from DER to PEM format?
Can I convert from DER to PEM using NodeJS crypto?
UPDATE
I tried this solution, which got me close:
const cer_der = ***my binary certificate file***;
const prefix = '-----BEGIN CERTIFICATE-----\n';
const postfix = '-----END CERTIFICATE-----';
const cer_pem = prefix + Buffer.from(cer_der, 'binary').toString('base64').match(/.{0,64}/g).join('\n') + postfix;
But the text string is not base64. Maybe I don't need to use Buffer.from