cscの作法 その204

# 概要
cscの作法、調べてみた。
サイトの証明書、叩いてみた。

# サンプルコード

```
using System;
using System.Net;
using System.Net.Sockets;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.IO;
using System.Text;

public class Program {
	static void DisplaySecurityLevel(SslStream stream) {
		Console.WriteLine("Cipher: {0} strength {1}", stream.CipherAlgorithm, stream.CipherStrength);
		Console.WriteLine("Hash: {0} strength {1}", stream.HashAlgorithm, stream.HashStrength);
		Console.WriteLine("Key exchange: {0} strength {1}", stream.KeyExchangeAlgorithm, stream.KeyExchangeStrength);
		Console.WriteLine("Protocol: {0}", stream.SslProtocol);
	}
	static void DisplaySecurityServices(SslStream stream) {
		Console.WriteLine("Is authenticated: {0} as server? {1}", stream.IsAuthenticated, stream.IsServer);
		Console.WriteLine("IsSigned: {0}", stream.IsSigned);
		Console.WriteLine("Is Encrypted: {0}", stream.IsEncrypted);
	}
	static void DisplayStreamProperties(SslStream stream) {
		Console.WriteLine("Can read: {0}, write {1}", stream.CanRead, stream.CanWrite);
		Console.WriteLine("Can timeout: {0}", stream.CanTimeout);
	}
	static void DisplayCertificateInformation(SslStream stream) {
		Console.WriteLine("Certificate revocation list checked: {0}", stream.CheckCertRevocationStatus);
		X509Certificate localCertificate = stream.LocalCertificate;
		if (stream.LocalCertificate != null)
		{
			Console.WriteLine("1.Local cert was issued to {0} and is valid from {1} until {2}.", localCertificate.Subject, localCertificate.GetEffectiveDateString(), localCertificate.GetExpirationDateString());
		}
		else
		{
			Console.WriteLine("1.Local certificate is null.");
		}
		X509Certificate remoteCertificate = stream.RemoteCertificate;
		if (stream.RemoteCertificate != null)
		{
			Console.WriteLine("2.Remote cert was issued to {0} and is valid from {1} until {2}.", remoteCertificate.Subject, remoteCertificate.GetEffectiveDateString(), remoteCertificate.GetExpirationDateString());
		}
		else
		{
			Console.WriteLine("2.Remote certificate is null.");
		}
	}
	public static void Main() {
		String hostName = "google.com";
		Console.WriteLine(hostName);
		Int32 port = 443;
		using (TcpClient client = new TcpClient())
		{
			IPAddress[] ipAddresses = Dns.GetHostAddresses(hostName);
			Console.WriteLine(ipAddresses[1].ToString());
			client.Connect(new IPEndPoint(ipAddresses[1], port));
			using (SslStream sslStream = new SslStream(client.GetStream(), false))
			{
				sslStream.AuthenticateAsClient(hostName);
				Byte[] req = Encoding.ASCII.GetBytes(String.Format("GET / HTTP/1.0\r\nHost: {0}\r\n\r\n", hostName));
				sslStream.Write(req);
				sslStream.Flush();
				Byte[] res = new Byte[1024];
				Int32 n;
				while ((n = sslStream.Read(res, 0, res.Length)) > 0)
				{
					String s = Encoding.ASCII.GetString(res, 0, n);
					//Console.WriteLine(s);
				}
				DisplaySecurityLevel(sslStream);
				DisplaySecurityServices(sslStream);
				DisplayCertificateInformation(sslStream);
				DisplayStreamProperties(sslStream);
			}
		}
	}
}





```


# 実行結果

```
>ssl2
google.com
172.217.26.238
Cipher: Aes128 strength 128
Hash: Sha1 strength 160
Key exchange: 44550 strength 255
Protocol: Tls
Is authenticated: True as server? False
IsSigned: True
Is Encrypted: True
Certificate revocation list checked: False
1.Local certificate is null.
2.Remote cert was issued to CN=*.google.com and is valid from 2022/05/30 17:33:49 until 2022/08/22 17:33:48.
Can read: True, write True
Can timeout: True

```

以上。