Skip to content

Instantly share code, notes, and snippets.

@CosmosKey
Created September 9, 2017 14:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CosmosKey/357997c3c5ea8b318bd889b47aa04962 to your computer and use it in GitHub Desktop.
Save CosmosKey/357997c3c5ea8b318bd889b47aa04962 to your computer and use it in GitHub Desktop.
Get-CertificateChain.ps1
Function Get-CertificateChain {
param(
[string]$server=$(throw "Mandatory parameter -Server is missing."),
[int]$port=$(throw "Mandatory parameter -Port is missing."),
[switch]$ToBase64
)
$code=@"
using System;
using System.Collections;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.IO;
using System.Threading;
namespace CosmosKey.Powershell
{
public class SslUtility
{
private static byte[] CertChain;
private static object Lock = new object();
private static Hashtable certificateErrors = new Hashtable();
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
byte[] data = certificate.Export(X509ContentType.Cert);
lock (Lock)
{
CertChain = data;
Monitor.Pulse(Lock);
}
return true;
}
public static byte[] GetCertificate(string serverName, int port)
{
TcpClient client = new TcpClient(serverName,port);
SslStream sslStream = new SslStream(
client.GetStream(),
false,
new RemoteCertificateValidationCallback (ValidateServerCertificate),
null
);
try
{
lock (Lock)
{
sslStream.BeginAuthenticateAsClient(serverName,null,null);
bool didTimeout = Monitor.Wait(Lock);
}
}
finally
{
client.Close();
}
return CertChain;
}
}
}
"@
Add-Type $code
[byte[]]$certData = [CosmosKey.Powershell.SslUtility]::GetCertificate($server,$port)
if($ToBase64){
[convert]::ToBase64String($certData)
} else {
$cert = new-object System.Security.Cryptography.X509Certificates.X509Certificate2
$cert.import($certData)
$cert
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment