카테고리:

1 분 소요

RemoteCertificateValidationCallback

RemoteCertificateValidationCallback은 HTTPS 인증서를 확인할 수 있게 해준다.

단, UNITYTLS_X509VERIFY_FLAG_NOT_TRUSTED’라는 메시지가 나오면서 Unity상에서는 위와 같은 방법을 사용할 수 없다. 왜냐하면 현재와 같이 루트 인증서와 비교하여 검증하기 위해 OpenSSL/MbedTLS를 사용하는 대신 시스템별 TLS API를 통해 검증을 수행함으로써 이 문제를 해결할 수 있을지 모르지만, 이 해결방법은 서로 다른 운영체제에서 작동되지 않을 가능성이 크기 때문에 Unity상에서 구현을 하지 않았다고 한다.

https://docs.microsoft.com/ko-kr/dotnet/api/system.net.security.remotecertificatevalidationcallback?view=netcore-3.1
https://issuetracker.unity3d.com/issues/error-of-tlsexception-when-system-dot-net-dot-http-dot-dll-is-used-for-http-in-the-editor

소스 코드

static void Main(string[] args) {
	try {
		ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
		HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://원하는_도메인_주소");
		request.Method = "GET";
		HttpWebResponse response = (HttpWebResponse)request.GetResponse();
		Stream stream = response.GetResponseStream();
		StreamReader reader = new StreamReader(stream, Encoding.UTF8);
		string text = reader.ReadToEnd();
		stream.Close();
		response.Close();
		reader.Close();
	} catch (Exception ex) {
		Console.WriteLine(ex.Message);
	}
}

static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
	try {
		if (sslPolicyErrors != SslPolicyErrors.None) {//SSL이 오류인 경우
			Console.WriteLine(sslPolicyErrors);
			return false;
		}

		Console.WriteLine("Subject : " + certificate.Subject);
		Console.WriteLine("Issuer : " + certificate.Issuer);
		Console.WriteLine("Hash : " + certificate.GetCertHashString());

		X509ChainElementCollection x509ChainElement = chain.ChainElements;
		X509Certificate rootCA = x509ChainElement[x509ChainElement.Count - 1].Certificate;
        
		Console.WriteLine("Root CA Subject : " + rootCA.Subject);
		Console.WriteLine("Root CA Issuer : " + rootCA.Issuer);
		Console.WriteLine("Root CA Hash : " + rootCA.GetCertHashString());

		return true;
	} catch (Exception) {
		return false;
	}
}

구 블로그 주소인 https://syudal.tistory.com을 테스트한 결과 화면

위의 그림에서는 본 블로그의 주소인 https://syudal.tistory.com을 입력하여 테스트하였다.

태그: HttpWebRequest, RemoteCertificateValidationCallback, RootCA인증서, ServerCertificateValidationCallback, SslPolicyErrors, UNITYTLS_X509VERIFY_FLAG_NOT_TRUSTED, ValidateServerCertificate, WebRequest, x509certificate, X509Chain, 도메인, 인증서

업데이트: