P12证书“数据不足”错误

我试图使用.p12文件安全地连接到一个端点,但我不断收到以下错误。

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

生成错误的代码是这样的:

  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'); } }); 

我已经尝试使用https.request方法,但产生相同的结果。 我已经在网上search了一个解决scheme,但我空手而来。

从我所知道的情况来看,这是PFX \ P12键的问题,考虑到我从第三方那里得到了密钥,这可能不是那么牵强。 我能想到的唯一的事情就是使用openSSL来转换密钥格式,看看是否可行。 任何build议或帮助将不胜感激。

所以答案在于https模块的API使用。 正如Node.js https文档中所述 ,当提供pfx文件时,它需要作为字节stream进行传递。

您需要阅读文件并直接传递其内容:

 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' } } 

希望这可以帮助。