简体   繁体   English

在 Windows 上运行 NodeJS 的 Web 请求中包含带有证书的私钥

[英]Include Private Key With Certificate in web request running NodeJS on Windows

I need to authenticate my GET request to an API by providing a certificate with the web request.我需要通过为 Web 请求提供证书来验证我对 API 的 GET 请求。

I am using NodeJS running on a Windows client.我正在使用在 Windows 客户端上运行的 NodeJS。

The request is failing, apparently due to the need to include the private key in plain text, which I do not have available to me.请求失败,显然是因为需要以纯文本形式包含私钥,而我没有。

Am I missing a way to use an X509 with the key embedded (not plain text)?我是否缺少使用嵌入密钥(非纯文本)的 X509 的方法?

I can get the full certificate with the private key in a couple of ways, but I cannot get the private key in plain text.我可以通过几种方式获取带有私钥的完整证书,但我无法以纯文本形式获取私钥。

I have tried the WIN-CA NPM module, but it does not deal with private keys .我试过WIN-CA NPM 模块,但它不处理私钥

Any ideas, confirmation of the issue, and solutions are appreciated.任何想法,对问题的确认和解决方案表示赞赏。

Thanks!谢谢!

I was able to get this to work in rather a round-about way.我能够让它以一种迂回的方式工作。 Part of the issue is that our certificate authority is an internal provider.部分问题在于我们的证书颁发机构是内部提供商。

To solve the issue we:为了解决这个问题,我们:

  • We updated the old code to the Node.js https module.我们将旧代码更新为 Node.js https 模块。
  • We then added the win-ca module to inject the corporate certificate authorities.然后我们添加了win-ca模块来注入企业证书颁发机构。
  • We used win-cert module to obtain the certificate with private key.我们使用win-cert模块获取带有私钥的证书。

Node that We did not want to copy the certificate to the file system, so using NODE_EXTRA_CA_CERTS and similar was not preferred.我们不想将证书复制到文件系统的节点,因此不首选使用NODE_EXTRA_CA_CERTS 和类似的方法

Code extract:代码提取:

const https = require('https');
const winCert = require('win-cert');
require('win-ca/api')({store: ['root', 'ca'], inject: true});

const certOptions = {
    storeName: 'My',
    storeLocation: 'LocalMachine',
    thumbprint: '098d3.....'
};

const httpOptions = {
    method: 'GET',
    headers: {
        'Accept': 'application/json',
        'Accept-Charset': 'utf-8',
    }
};


const certAndKey = winCert.getCertificate(certOptions).then((resolve, reject) => {
    certObtained = true;
    httpOptions.cert = resolve.cert;
    httpOptions.key = resolve.key;
});

const dpmRequest = https.request(httpOptions, ...

Note that if the process is not running as admin, or if the certificate is not marked as exportable, the key will not be accessible.请注意,如果该进程未以管理员身份运行,或者证书未标记为可导出,则无法访问该密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM