Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

P12 certificate "Not enough data" error

Tags:

node.js

https

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.

like image 456
Tudor Merlas Avatar asked May 04 '16 11:05

Tudor Merlas


1 Answers

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.

like image 188
krassdanke Avatar answered Oct 05 '22 22:10

krassdanke