Node.js - HTTPS PFX Error: Unable to load BIO

I am trying to make an HTTPS request. I already know that PFX is good, and that’s not a problem (I have a similar example application running).

I do the following:

var request = require('request-promise'); 

...

 options.pfx = fs.readFileSync('myfile.pfx'); options.passphrase = 'passphrase'; 

I pass my options to the request.

 request.post(options); 

Then I try to create a request, I get the following error:

 _tls_common.js:130 c.context.loadPKCS12(pfx, passphrase); ^ Error: Unable to load BIO at Error (native) at Object.createSecureContext (_tls_common.js:130:17) at Object.exports.connect (_tls_wrap.js:955:21) at Agent.createConnection (https.js:73:22) at Agent.createSocket (_http_agent.js:174:16) at Agent.addRequest (_http_agent.js:143:23) at new ClientRequest (_http_client.js:133:16) at Object.exports.request (http.js:31:10) at Object.exports.request (https.js:163:15) at Request.start (/Users/filomeno/workspace/sla-crawler/node_modules/request/request.js:747:30) at Request.write (/Users/filomeno/workspace/sla-crawler/node_modules/request/request.js:1369:10) at end (/Users/filomeno/workspace/sla-crawler/node_modules/request/request.js:561:16) at Immediate._onImmediate (/Users/filomeno/workspace/sla-crawler/node_modules/request/request.js:589:7) at processImmediate [as _immediateCallback] (timers.js:374:17) 

I have an example application that runs the same code. I tried to convert to .p12 without success.

Does anyone have an idea what this error might relate to?

Edit: I use lodash to merge two objects with dynamic properties and static properties

 _.merge(options, _this.requestOptions); 

And it caused a problem

+6
source share
1 answer

Looking at the source code for nodejs (in particular, this file https://github.com/nodejs/node/blob/master/src/node_crypto.cc )

the error is caused by this function

 // Takes .pfx or .p12 and password in string or buffer format void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); ... 

At line 964

 in = LoadBIO(env, args[0]); if (in == nullptr) { return env->ThrowError("Unable to load BIO"); } 

If LoadBIO returns null

 // Takes a string or buffer and loads it into a BIO. // Caller responsible for BIO_free_all-ing the returned object. static BIO* LoadBIO(Environment* env, Local<Value> v) { HandleScope scope(env->isolate()); if (v->IsString()) { const node::Utf8Value s(env->isolate(), v); return NodeBIO::NewFixed(*s, s.length()); } if (Buffer::HasInstance(v)) { return NodeBIO::NewFixed(Buffer::Data(v), Buffer::Length(v)); } return nullptr; } 

Perhaps the buffer is somehow not readable? It also seems that the function is expecting a utf-8 encoded string.

Some ideas:

Are you sure the path to the file is correct?

Maybe there is a problem with the encoding? Have you tried explicitly setting the encoding fs.readFileSync() ?

Try using fs.readFile(<filename>, <encoding>, function(error, data){}) to see if this causes an error?

+6
source

All Articles