P12 PFX NodeJS请求

Kri*_*son 14 ssl certificate pfx node.js

我试图用p12文件或pfx发出请求,但我无法让它工作.如果我使用PEM和KEY,代码工作正常.但Azure Keyvault不支持PEM和KEY.有没有可以使用KEY/PEM证书的替代方案?

这就是我生成p12/pfx文件的方法,如果这是问题.

openssl pkcs12 -export -out certificate.pfx -inkey 1231181189.key -in 1231181189.pem -certfile CA.pem

这是一个示例代码,如果我注释掉cert和key系统不起作用,

错误:读取ECONNRESET

但是如果我注释掉pfx和passphrase并使用pem并键入连接工作.

var request = require('request');
var fs = require('fs');
var path = require('path');
var certFile = __dirname + '/certs/1231181189.pem';
var keyFile = __dirname + '/certs/1231181189.key';

var options = {
  method: 'POST',
  url: 'https://mss.cpc.getswish.net/swish-cpcapi/api/v1/paymentrequests',
  headers: { 'Content-Type': 'application/json' },
  agentOptions: {
    cert: fs.readFileSync(certFile),
    key: fs.readFileSync(keyFile),
    pfx: fs.readFileSync(__dirname + '/certs/certificate.pfx'),
    passphrase: 'swish'
  },
  body: {
    payeePaymentReference: '0123456789',
    callbackUrl: 'https://example.com/api/swishcb/paymentrequests',
    payerAlias: '4671234768',
    payeeAlias: '1231181189',
    amount: '100',
    currency: 'SEK',
    message: 'Kingston USB Flash Drive 8 GB'
  },
  json: true
};

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(response.headers);
  console.log(body);
});
Run Code Online (Sandbox Code Playgroud)

O. *_*nes 1

ECONNRESET意味着远端(在您的例子中是 swish.net 上的端点)毫不客气地与您的 Nodejs 程序中的 https 客户端断开连接。很难确切地知道它为什么这样做。这可能是由于某种安全故障造成的。强大的服务器并不能解释安全故障;毕竟为什么要帮助网络怪人呢?查看该服务器上的日志可能会告诉您更多信息。

同时,您用于包装节点的 https 代理功能的npmrequest可能不知道有关.pfx文件或密码的任何信息,因此尝试在没有任何客户端证书的情况下进行连接。

pemutils软件包 可能允许您从文件中提取所需的信息.pfx并使用它。像这样的东西可能会起作用(未调试)。

var request = require('request');
var pemutils = require('pemutils');
var fs = require('fs');
var path = require('path');
const pfxFile =  __dirname + '/certs/certificate.pfx';

pemutils.fromPfx({
    path: pfxFile,
    password: 'myPass'
}, function(err, pfxresults) {
    if(err) throw err;
    var options = {
      method: 'POST',
      url: 'https://mss.cpc.getswish.net/swish-cpcapi/api/v1/paymentrequests',
      headers: { 'Content-Type': 'application/json' },
      agentOptions: {
        cert: pfxresults.certificate,
        key:  pfxresults.key,
      },
      body: {
           ...
      },
      json: true
    };
    ...
Run Code Online (Sandbox Code Playgroud)

请注意该.fromPfx方法是异步的。