2016-03-01 104 views
6

我试图做出HTTPS请求 - 承诺。我已经知道PFX是好的,这不是问题(我有一个类似的示例应用程序工作)。Node.js - HTTPS PFX错误:无法加载BIO

我做了以下内容:

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

...

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

我通过我的选项为请求。

request.post(options); 

然后我尝试建立我得到以下错误请求:

_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做2个对象与DINAMIC属性和静态属性

_.merge(options, _this.requestOptions); 

这的合并是造成问题

+0

你有什么发现?我得到了完全相同的错误 –

+0

此代码与“相同代码有效”的示例应用程序有什么不同? _I.e._是由不同用户运行的两个应用程序(在'myfile.pfx'文件中建议文件系统权限)?有问题的PFX文件是否使用相同的密码来保护容器**和**私钥或不同的密码? – Castaglia

+0

我的问题是lodash我正在做'_.merge(options,_this.requestOptions); '这与我的编码混乱。 – fasantos

回答

6

望着源的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编码的字符串。

一些想法:

您确定该文件的路径是否正确?

也许编码问题?您是否尝试明确设置fs.readFileSync()编码?

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

+2

这是编码,我正在与lodash合并,与我的对象混淆。 – fasantos