P12证书“数据不足”错误 [英] P12 certificate "Not enough data" error

查看:125
本文介绍了P12证书“数据不足”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was trying to connect to an endpoint securely using a .p12 file but I keep getting the following error.

_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方法,但结果相同。我在网上搜索了一个解决方案,但我空手而归。

I've tried using the https.request method but that yields the same result. I've searched the web for a solution but I came up empty handed.

据我所知,这是PFX \ P12键的一个问题,可能不是考虑到我从第三方那里收到了钥匙,我是如此牵强。我唯一能想到的是使用openSSL转换密钥格式并查看是否有效。任何建议或帮助将不胜感激。

From what I can tell, it's a problem with the PFX \ P12 key which might not be so far-fetched, considering I received the key from a third-party. The only thing I can think of is converting the key format using openSSL and seeing if that works. Any suggestions or help would be greatly appreciated.

推荐答案

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

So the answer lies in the API usage of the https module. As documented in the Node.js https documentation, when providing a pfx-file, it needs to be passed as a bytestream.

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

You need to read the file and directly pass its contents:

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

希望这会有所帮助。

这篇关于P12证书“数据不足”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆