简体   繁体   English

Equalivent for openssl s_client-连接 <hostname> : <port> 在C#中

[英]Equalivent for openssl s_client -connect <hostname>:<port> in C#

When I execute a openssl command to connect a particular server [myadda.tie.fire.glass.... dummy server name] , it gives me some output. 当我执行openssl命令以连接特定服务器[myadda.tie.fire.glass ....哑服务器名称]时,它会提供一些输出。

openssl s_client -connect myadda.tie.fire.glass:443

It gives me some output which contains information like 它给了我一些包含如下信息的输出

  • server certificate 服务器证书
  • issuer information 发行人信息

And another command which required the certificate from above command to provide me details info about the certificate. 另一个命令需要上述命令中的证书来向我提供有关证书的详细信息。

openssl x509 -in <Certificate_FileName.crt> -text -nout

It gives me output as information about the certificate 它给我输出有关证书的信息

  • issued for server 为服务器发行
  • Validity 合法性

I want similar kind of output using some C# classes. 我想要使​​用某些C#类进行类似的输出。 I am not sure how to solve this query. 我不确定如何解决此查询。 Can anyone help me out? 谁能帮我吗?

Well below code help me to retrieve the required information. 下面的代码可以帮助我检索所需的信息。

            X509Certificate2 cert = null;
            var client = new TcpClient(host, 443);
            var certValidation = new RemoteCertificateValidationCallback(delegate (object snd, X509Certificate certificate, X509Chain chainLocal, SslPolicyErrors sslPolicyErrors)
            {
                //Accept every certificate, even if it's invalid
                return true;
            });

            // Create an SSL stream and takeover client's stream
            using (var sslStream = new SslStream(client.GetStream(), true, certValidation))
            {
                sslStream.AuthenticateAsClient(host);

                var serverCertificate = sslStream.RemoteCertificate;
                cert = new X509Certificate2(serverCertificate);

                //Convert Raw Data to Base64String
                var certBytes = cert.Export(X509ContentType.Cert);

                var certAsString = Convert.ToBase64String(certBytes, Base64FormattingOptions.None);

            }

Here vertAsString gives me the certificate whereas cert gives me the other required information. 这里vertAsString给我证书,而cert给我其他必需的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM