Skip to content

Instantly share code, notes, and snippets.

@jborean93
Last active February 28, 2024 09:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jborean93/52a56d28cb658000b64d82d900b5e882 to your computer and use it in GitHub Desktop.
Save jborean93/52a56d28cb658000b64d82d900b5e882 to your computer and use it in GitHub Desktop.
Tests the TLS connection by doing a client hello with the hostname specified
# Copyright: (c) 2024, Jordan Borean (@jborean93) <jborean93@gmail.com>
# MIT License (see LICENSE or https://opensource.org/licenses/MIT)
Function Test-Tls {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]
$HostName,
[Parameter()]
[int]
$Port = 443,
[Parameter()]
[System.Security.Authentication.SslProtocols]
$TlsVersion = 'None',
[Parameter()]
[string]
$SNIName
)
$tcp = [System.Net.Sockets.TcpClient]::new()
$ssl = $null
try {
$tcp.Connect($HostName, $port)
$validationState = @{}
$ssl = [System.Net.Security.SslStream]::new($tcp.GetStream(), $false, {
param($SslSender, $Certificate, $Chain, $SslPolicyErrors)
$validationState.PolicyErrors = $SslPolicyErrors
$true
})
$sslHost = $HostName
if ($SNIName) {
$sslHost = $SNIName
}
$ssl.AuthenticateAsClient($sslHost, $null, $TlsVersion, $true)
$cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($ssl.RemoteCertificate)
[PSCustomObject]@{
SslProtocol = $ssl.SslProtocol
NegotiatedCipherSuite = $ssl.NegotiatedCipherSuite # Only works with pwsh 7+
Certificate = $cert
ValidationErrors = $validationState.PolicyErrors
}
}
finally {
if ($ssl) { $ssl.Dispose() }
$tcp.Dispose()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment