2016-03-01 101 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