【问题标题】:Using pem certificate and key for authentication in powershell https request在 powershell https 请求中使用 pem 证书和密钥进行身份验证
【发布时间】:2017-04-18 07:20:56
【问题描述】:

我有 pem 格式的证书。总共 2 个文件,RSA 公钥和 RSA 私钥。 我必须使用这些在 powershell 脚本中向服务器发出 https 请求。

我尝试使用 X509Certificates 证书存储添加证书。但我不确定如何添加客户端密钥证书( RSA 私钥)。 我尝试只使用证书,但出现此错误:

Exception Message: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure c

渠道。

如何使用 powershell 同时请求客户端证书和密钥?

我写的powershell脚本:

$method = "GET"
# Create a dictionary object that allows header storage in Rest call
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("X-Qlik-Xrfkey",$xrfKey)
$headers.Add("X-Qlik-User", "***")


#Get a selection object for all inactive users
$path = "/qrs/app?xrfkey=$xrfKey"
$theCommand = $senseServerHostName + "/qrs" + $path

$ns = "System.Security.Cryptography.X509Certificates"
$store = New-Object "$ns.X509Store"("My","CurrentUser")
#$store = New-Object System.Security.Cryptography.X509Certificates.X509Store("My", "CurrentUser")
$store.Open("ReadOnly")
#$certs = $store.Certificates.Find("FindByExtension", $certExtension, $false)
#"$store.Certificates"
    ForEach($cert in $store.Certificates)
    {

        $certToUse = $cert
    }
    "$certToUse"
$response = Invoke-RestMethod $theCommand -Headers $headers -Method $method -Certificate $certToUse

虽然我能够使用 node.js 发出请求 节点代码:

var https = require('https');    
var fs = require('fs');    
var options = {    
rejectUnauthorized: false,    
hostname: '****',
method: 'GET',
path: '/qrs/app?xrfkey=****',
headers: {
//'Accept': 'application/json',
'x-qlik-xrfkey' : '****',
'X-Qlik-User' : '****'
},
key: fs.readFileSync("C:\\client_key.pem"),
cert: fs.readFileSync("C:\\client.pem")

};
https.get(options, function(res) {
console.log("Got response: " + res.statusCode);
res.on("data", function(chunk) {
console.log("BODY: " + chunk);
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});

谢谢。

【问题讨论】:

  • 您是否将整个 PEM 导入商店? certmgr.msc 是否将证书显示为具有关联的私钥?我认为您需要将这些文件转换为 PKCS#12 格式,然后才能作为带有主键的证书导入。 IIRC Windows 在运行其 SSL 引擎时不允许将密钥存储为文件。
  • 是的,您必须将公共证书和关联的私钥文件合并到单个 PKCS#12 容器中,然后将其安装到 Windows 证书存储中。
  • 我刚刚将公钥导入到证书存储中。我将尝试将公共和私有合并到 PKCS12 并尝试安装在证书存储中。然后尝试访问服务器。谢谢。
  • @Vesper :我认为我的系统中有很多证书。如何知道使用哪一个?
  • 我认为您应该按指纹过滤,这是最快和可靠的方法。您有一个循环存储,选择与您的 PEM 中证书的指纹匹配的那个,然后存储在您的 $certToUse 中。在脚本中存储指纹是可以接受的,只需记录在证书更新的情况下您需要更新脚本。

标签: node.js powershell https x509certificate pem


【解决方案1】:

将 PEM 文件转换为 PKCS12 对我有用。

【讨论】:

    猜你喜欢
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    • 2017-08-27
    • 2016-02-08
    相关资源
    最近更新 更多