我有PEM格式的证书。完全2个文件,RSA Public和RSA私钥。我必须使用这些来以PowerShell脚本向服务器提出HTTPS请求。
我尝试使用 x509certificates 证书存储添加证书。但是我不确定如何添加客户端密钥证书(RSA私钥(。我仅尝试使用证书,但是我得到了这个错误:
Exception Message: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure c
Hannel。
如何使用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文件转换为PKCS12为我工作。