cscの作法 その208

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

# サンプルコード

```

using System.Net;
using System.IO;
using System.Text;
using System.Reflection;
using System.Collections;
using System;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

public class Test {
	private static void PrintCertificate(X509Certificate certificate) {
		Console.WriteLine("===========================================");
		Console.WriteLine("Subject: {0}", certificate.Subject);
		Console.WriteLine("Issuer: {0}", certificate.Issuer);
		Console.WriteLine("Format: {0}", certificate.GetFormat());
		Console.WriteLine("ExpirationDate: {0}", certificate.GetExpirationDateString());
		Console.WriteLine("EffectiveDate: {0}", certificate.GetEffectiveDateString());
		Console.WriteLine("KeyAlgorithm: {0}", certificate.GetKeyAlgorithm());
		Console.WriteLine("PublicKey: {0}", certificate.GetPublicKeyString());
		Console.WriteLine("SerialNumber: {0}", certificate.GetSerialNumberString());
		Console.WriteLine("===========================================");
	}
	public static bool ValidateServerCertificate (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) {
		PrintCertificate(certificate);
		Console.WriteLine("Chain: " + chain.ChainStatus.Length);
		Console.WriteLine("Certificate error: " + sslPolicyErrors);
		Console.WriteLine(string.Format("Chain revocation flag: {0}", chain.ChainPolicy.RevocationFlag));
		Console.WriteLine(string.Format("Chain revocation mode: {0}", chain.ChainPolicy.RevocationMode));
		Console.WriteLine(string.Format("Chain verification flag: {0}", chain.ChainPolicy.VerificationFlags));
		Console.WriteLine(string.Format("Chain verification time: {0}", chain.ChainPolicy.VerificationTime));
		Console.WriteLine(string.Format("Chain status length: {0}", chain.ChainStatus.Length));
		Console.WriteLine(string.Format("Chain application policy count: {0}", chain.ChainPolicy.ApplicationPolicy.Count));
		Console.WriteLine(string.Format("Chain certificate policy count: {0} {1}", chain.ChainPolicy.CertificatePolicy.Count, Environment.NewLine));
		for (int i = 0; i < chain.ChainStatus.Length; i++) 
		{
			Console.WriteLine("Chain status: " + chain.ChainStatus[i].Status);
			foreach (X509ChainElement element in chain.ChainElements)
			{
				Console.WriteLine(string.Format("Element issuer name: {0}", element.Certificate.Issuer));
				Console.WriteLine(string.Format("Element certificate valid until: {0}", element.Certificate.NotAfter));
				Console.WriteLine(string.Format("Element certificate is valid: {0}", element.Certificate.Verify ()));
				Console.WriteLine(string.Format("Element error status length: {0}", element.ChainElementStatus.Length));
				Console.WriteLine(string.Format("Element information: {0}", element.Information));
				Console.WriteLine(string.Format("Number of element extensions: {0}{1}", element.Certificate.Extensions.Count, Environment.NewLine));
				if (chain.ChainStatus.Length > 1)
				{
					for (int index = 0; index < element.ChainElementStatus.Length; index++)
					{
						Console.WriteLine("Status:" + element.ChainElementStatus[index].Status);
						Console.WriteLine("Status:" + element.ChainElementStatus[index].StatusInformation);
					}
				}
			}
		}
		return true;
	}
	static string URL = "https://google.com";
	public static void Main() {
		try
		{
			ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
			HttpWebRequest request = (HttpWebRequest) WebRequest.Create(URL);
			HttpWebResponse response = (HttpWebResponse) request.GetResponse();
			Stream stream = response.GetResponseStream();
			string webContext = new StreamReader(stream).ReadToEnd();
			//Console.WriteLine(webContext);
		}
		catch (WebException webException)
		{
			var ex = webException as Exception;
			while (ex != null)
			{
				Console.WriteLine(ex.ToString());
				ex = ex.InnerException;
			}
		}
	}
}


```

# 実行結果

```
===========================================
Subject: CN=www.google.com
Issuer: CN=GTS CA 1C3, O=Google Trust Services LLC, C=US
Format: X509
ExpirationDate: 2022/08/29 18:39:59
EffectiveDate: 2022/06/06 18:40:00
KeyAlgorithm: 1.2.840.10045.2.1
PublicKey: 043F0EEDC5D648F42187F1A286A262FA076DA52D2F02DD8678344705A55E07CC79743B71DEEB364C65B658919F0BB55E4951C25F5B835E48C020E4DAB8C5230AED
SerialNumber: 009F074B11745F16FC122375FA587993F0
===========================================
Chain: 0
Certificate error: None
Chain revocation flag: ExcludeRoot
Chain revocation mode: NoCheck
Chain verification flag: NoFlag
Chain verification time: 2022/06/22 5:58:16
Chain status length: 0
Chain application policy count: 1
Chain certificate policy count: 0
```

以上。