I was trying to connect to an endpoint securely using a .p12
file but I keep getting the following error.
_tls_common.js:136
c.context.loadPKCS12(pfx);
^
Error: not enough data
at Error (native)
at Object.createSecureContext (_tls_common.js:136:17)
at Object.TLSSocket._init.ssl.onclienthello.ssl.oncertcb.exports.connect (_tls_wrap.js:1003:48)
at Agent.createConnection (https.js:80:22)
at Agent.createSocket (_http_agent.js:179:26)
at Agent.addRequest (_http_agent.js:141:10)
at new ClientRequest (_http_client.js:147:16)
at Object.exports.request (http.js:31:10)
at Object.exports.request (https.js:197:15)
at Request.start (D:\path_to_project\node_modules\request\request.js:747:30)
The code that generates the error is this:
request({
method: 'POST',
url: config.secureEndpoint.hostname + config.secureEndpoint.path,
body: XMLAPIResponse.body,
rejectUnauthorized: false,
strictSSL: false,
agentOptions: {
//pfx: pfx,
pfx: 'string_path_to_the_p12_key_file.p12',
passphrase: 'redacted_password'
}
}, function (error, response, body) {
console.log(response);
if (response.satusCode == 200) {
model.updateStatus(ID, 'done');
} else {
model.updateStatus(ID, 'error');
}
});
I've tried using the https.request method but that yields the same result. I've searched the web for a solution but I came up empty handed.
From what I can tell, it's a problem with the PFX \ P12 key which might not be so far-fetched, considering I received the key from a third-party. The only thing I can think of is converting the key format using openSSL and seeing if that works. Any suggestions or help would be greatly appreciated.
So the answer lies in the API usage of the https module. As documented in the Node.js https documentation, when providing a pfx-file, it needs to be passed as a bytestream.
You need to read the file and directly pass its contents:
request({
method: 'POST',
url: config.secureEndpoint.hostname + config.secureEndpoint.path,
body: XMLAPIResponse.body,
rejectUnauthorized: false,
strictSSL: false,
agentOptions: {
//pfx: pfx,
pfx: require('fs').readFileSync('string_path_to_the_p12_key_file.p12'),
passphrase: 'redacted_password'
}
}
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With