基本上我想将命令curl翻译为与当前在Linux服务器中相同的参数,但在Powershell中,以便上传文件:
curl -v -T $file -u user:password http://myurl --cacert /opt/keystores/ca_cert.pem
我找到了执行此任务的等效命令:PowerShell 3.0+ 的“Invoke-WebRequest”,但问题是我不知道如何使用 CA 证书文件 (.pem) 调用它,而且我还没有找到互联网上的任何样本。
Thanks!
当你做一个TLS
.NET 中的连接,对等证书根据RemoteCertificateValidationCallback功能 https://msdn.microsoft.com/en-us/library/system.net.security.remotecertificatevalidationcallback(v=vs.110).aspx,由ServicePointManager
对于有问题的 AppDomain。
大多数有关如何在 PowerShell 中覆盖默认验证的示例都会告诉您只需执行以下操作:
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
不要那样做!- 它将完全绕过验证。
你什么can要做的就是实现适当的回调函数并手动调用链验证。在执行此操作之前,您可以将计算机或用户证书存储中没有的证书添加到您可以验证的链列表中:
$callback = {
param(
$sender,
[System.Security.Cryptography.X509Certificates.X509Certificate]$certificate,
[System.Security.Cryptography.X509Certificates.X509Chain]$chain,
[System.Net.Security.SslPolicyErrors]$sslPolicyErrors
)
# No need to retype this long type name
$CertificateType = [System.Security.Cryptography.X509Certificates.X509Certificate2]
# Read the CA cert from file
$CACert = $CertificateType::CreateFromCertFile("C:\path\to\ca.crt") -as $CertificateType
# Add the CA cert from the file to the ExtraStore on the Chain object
$null = $chain.ChainPolicy.ExtraStore.Add($CACert)
# return the result of chain validation
return $chain.Build($certificate)
}
# Assign your delegate to the ServicePointManager callback
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = $callback
# Do your Invoke-WebRequest or WebClient call here
我不知道如何将 PEM 文件中的多个证书读取到证书集合中,因此您必须一一添加每个 ca 证书,抱歉
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)