I think there are two parts to the question:
- How to get a server certificate
- How to get a certificate chain
To obtain a server certificate, you use SslStream, whose methods are similar to .NET. Sslstream
var serverName = "...; var client = new TcpClient(serverName, 443); // Create an SSL stream that will close the client stream. using (var sslStream = new SslStream(client.GetStream(),true)) { sslStream.AuthenticateAsClient(serverName); var serverCertificate = sslStream.RemoteCertificate; }
It seems that OpenSSL.Net cannot get the certificate chain. The -showcerts parameter uses the SSL_get_peer_cert_chain function, which is not implemented in OpenSSL.NET.
If you don't mind mixing OpenSSL.Net and the built-in .NET classes, you can convert the OpenSSL.Net certificate to a .NET certificate and retrieve the chain using .NET X509Chain.Build . You can convert .NET certificates back to OpenSSL.NET certificates using the .NET RawData certificate.
var managedCert = new System.Security.Cryptography.X509Certificates.X509Certificate2(serverCertificate.DER); var chain = new System.Security.Cryptography.X509Certificates.X509Chain(); chain.Build(managedCert); foreach (var element in chain.ChainElements) { var raw = element.Certificate.RawData; using (var bio = new BIO(raw)) { var oc = OpenSSL.X509.X509Certificate.FromDER(bio); } }
Perhaps you can use the .NET SslStream and X509Certificate2 objects to do what you want using the raw certificate data without using OpenSSL.Net.
Panagiotis kanavos
source share