ssl - 如何在 Powershell 中使用客户端证书创建 TCP 客户端连接

标签 ssl powershell sslstream system.net.sockets

使用以下代码我可以建立 SSL 连接:

   $cert = dir cert:\CurrentUser\My | where {$_.Subject -like "*Alice*"}

   $computerName = "google.com"
   $port = "443"

   $socket = New-Object Net.Sockets.TcpClient($computerName, $port)
   $stream = $socket.GetStream()

   $sslStream = New-Object System.Net.Security.SslStream $stream,$false
   $sslStream.AuthenticateAsClient($computerName)


    $sslStream

这很好用。但现在我不想添加用于身份验证的客户端证书。 我想我只需要替换

$sslStream.AuthenticateAsClient($computerName)

$sslStream.BeginAuthenticateAsClient($computerName,$cert,"SslProtocols",$false,"Foo" ,"Bar")

但我没能把论点弄对。 Sombody 可以解决 Assync Calls 吗 ;-) 也许我为此需要一些 C# 代码?!?

参数是:

System.IAsyncResult BeginAuthenticateAsClient(

string targetHost, 
System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, 
System.Security.Authentication.SslProtocols enabledSslProtocols,
bool checkCertificateRevocation, 
System.AsyncCallback asyncCallback, 
System.Object asyncState)

我最终想要实现的是列出并稍后指定客户端连接到的 CipherSuites。 (我可以使用我知道的 Wireshark ;-))

最佳答案

终于让它工作了,不是参数 4,而是 $Cert,它不是集合。

   $cert = dir cert:\CurrentUser\My | where {$_.Subject -like "*alice*"}

   $computerName = "google.com"
   $port = "443"

    [System.Security.Authentication.SslProtocols]$protocol = "ssl3"

   $certcol = New-object System.Security.Cryptography.X509Certificates.X509CertificateCollection
   $certcol.Add($cert)




   $socket = New-Object Net.Sockets.TcpClient($computerName, $port)
   $stream = $socket.GetStream()

   $sslStream = New-Object System.Net.Security.SslStream $stream,$false
   #$sslStream.AuthenticateAsClient($computerName)
   $sslStream.AuthenticateAsClient($computerName,$certcol,$protocol,$false) 


    $sslStream

关于ssl - 如何在 Powershell 中使用客户端证书创建 TCP 客户端连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9018338/

相关文章:

php - 使用 SSL 证书验证 Web 浏览器

powershell - 将相对路径转换为绝对字符串路径

windows - 删除报表服务器(2014 native 模式)加密 key 和数据 [PowerShell]

C# SSL 安全套接字

c# - 如何让 NUnit 测试等待线程完成?

java - 如何使用 wireshark 解密服务以服务 SSL 流量?

javax.net.ssl.SSLHandshakeException : Received fatal alert: handshake_failure

java - 如何在 Java 中接收 SSL 套接字数据?

.net - 使用 powershell 在 hyper-v(给定的 VMname)上获取 VM 的主机名

c# - SslStream.WriteAsync "The BeginWrite method cannot be called when another write operation is pending"