我试图使用 .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 方法,但结果相同。我在网上搜索了解决方案,但一无所获。
据我所知,这是 PFX\P12 key 的问题,考虑到我从第三方收到 key ,这可能不是那么牵强。我唯一能想到的就是使用 openSSL 转换 key 格式并查看是否可行。任何建议或帮助将不胜感激。
最佳答案
所以答案在于https模块的API使用。 如记录in the Node.js https documentation ,在提供 pfx 文件时,需要将其作为字节流传递。
您需要读取文件并直接传递其内容:
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'
}
}
希望这对您有所帮助。
关于node.js - P12证书 "Not enough data"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37026880/