Node.js – HTTPS PFX错误:无法加载BIO

我试图做一个HTTPS请求承诺。 我已经知道PFX是好的,这不是问题(我有一个类似的示例应用程序工作)。

我正在做以下事情:

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

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

我正在将我的选项传递给请求。

 request.post(options); 

然后我尝试构build请求我得到以下错误:

 _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) 

我有一个示例应用程序在相同的代码工作。 我试图转换到.p12没有成功。

有谁知道这个错误可能涉及到什么?

编辑:我正在使用lodash做一个具有dynamic属性和静态属性的2个对象的合并

 _.merge(options, _this.requestOptions); 

这是造成这个问题

看一下nodejs的源代码(具体是这个文件https://github.com/nodejs/node/blob/master/src/node_crypto.cc

该函数引发错误

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

在第964行

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

LoadBIO返回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; } 

也许缓冲区是不可读的? 此外,似乎该函数预计utf-8编码的string。

一些想法:

你确定文件的path是正确的吗?

也许编码问题? 你有没有尝试明确设置fs.readFileSync()编码?

尝试使用fs.readFile(<filename>, <encoding>, function(error, data){})来查看是否会引发错误?