node.js - P12证书 "Not enough data"错误

标签 node.js https

我试图使用 .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/

相关文章:

node.js - Mongoose 模式忽略值

javascript - 通过 CPU 交易 RAM(性能问题)

python - 在 tox 环境中为 pip install 命令设置 https 代理

java - 如何发出 HTTPS GET 请求来与其他方竞争并尝试成为最快的?

https - Dart HTTP Server 并导入 SSL 证书

node.js - Node Sequelize Postgres - BulkCreate 忽略自定义字段的重复值

node.js - 如何在也具有关系属性的 Sequelize Js 中实现多对多关系?

node.js - PhpStorm 无法识别 node.js 语法

apache - 在 WebFlow 中强制使用 https

ruby-on-rails - Rails、Windows 和 HTTPS