I've found several answer about how to download the certificate for a website in PowerShell using TcpClient.
function Get-RemoteCertificate {
[CmdletBinding()]
[OutputType([System.Security.Cryptography.X509Certificates.X509Certificate])]
param (
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateNotNull()]
[Uri]$Uri
)
process {
try {# connecting
$TcpClient = [System.Net.Sockets.TcpClient]::new($Uri.Host, $Uri.Port)
try {# getting SSL
$SslStream = [System.Net.Security.SslStream]::new($TcpClient.GetStream())
$SslStream.AuthenticateAsClient($Uri.Host)
$SslStream.RemoteCertificate
} finally {
$SslStream.Dispose()
}# end SSL
} finally {
$TcpClient.Dispose()
}# end connect
}
}
But as the TLS handshake will fail when the certificate isn't trusted, I can't download self signed certificates and I will get the error
Exception calling ".ctor" with "2" argument(s): "A connection attempt failed because the connected party did not
properly respond after a period of time, or established connection failed because connected host has failed to
respond
Is there a way to download self signed certificates without using OpenSSL?