11using System ;
22using System . IO ;
33using System . Net ;
4+ using System . Net . Security ;
5+ using System . Security . Cryptography . X509Certificates ;
46
57namespace LibGit2Sharp . Core
68{
@@ -50,12 +52,12 @@ private class ManagedHttpSmartSubtransportStream : SmartSubtransportStream
5052 public ManagedHttpSmartSubtransportStream ( ManagedHttpSmartSubtransport parent , string endpointUrl , bool isPost , string contentType )
5153 : base ( parent )
5254 {
53- EndpointUrl = endpointUrl ;
55+ EndpointUrl = new Uri ( endpointUrl ) ;
5456 IsPost = isPost ;
5557 ContentType = contentType ;
5658 }
5759
58- private string EndpointUrl
60+ private Uri EndpointUrl
5961 {
6062 get ;
6163 set ;
@@ -100,14 +102,23 @@ public override int Write(Stream dataStream, long length)
100102 return 0 ;
101103 }
102104
103- private static HttpWebRequest CreateWebRequest ( string endpointUrl , bool isPost , string contentType )
105+ private bool CertificateValidationProxy ( object sender , X509Certificate cert , X509Chain chain , SslPolicyErrors errors )
106+ {
107+ int ret = SmartTransport . CertificateCheck ( new CertificateX509 ( cert ) , ( errors == SslPolicyErrors . None ) , EndpointUrl . Host ) ;
108+ Ensure . ZeroResult ( ret ) ;
109+
110+ return true ;
111+ }
112+
113+ private HttpWebRequest CreateWebRequest ( Uri endpointUrl , bool isPost , string contentType )
104114 {
105115 ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 ;
106116
107117 HttpWebRequest webRequest = ( HttpWebRequest ) HttpWebRequest . Create ( endpointUrl ) ;
108118 webRequest . UserAgent = "git/1.0 (libgit2 custom transport)" ;
109119 webRequest . ServicePoint . Expect100Continue = false ;
110120 webRequest . AllowAutoRedirect = false ;
121+ webRequest . ServerCertificateValidationCallback += CertificateValidationProxy ;
111122
112123 if ( isPost )
113124 {
@@ -147,7 +158,18 @@ private HttpWebResponse GetResponseWithRedirects()
147158 }
148159 catch ( WebException ex )
149160 {
150- response = ( HttpWebResponse ) ex . Response ;
161+ if ( ex . Response != null )
162+ {
163+ response = ( HttpWebResponse ) ex . Response ;
164+ }
165+ else if ( ex . InnerException != null )
166+ {
167+ throw ex . InnerException ;
168+ }
169+ else
170+ {
171+ throw new Exception ( "unknown network failure" ) ;
172+ }
151173 }
152174
153175 if ( response . StatusCode == HttpStatusCode . OK )
@@ -171,7 +193,7 @@ private HttpWebResponse GetResponseWithRedirects()
171193 }
172194 else if ( response . StatusCode == HttpStatusCode . Moved || response . StatusCode == HttpStatusCode . Redirect )
173195 {
174- request = CreateWebRequest ( response . Headers [ "Location" ] , IsPost , ContentType ) ;
196+ request = CreateWebRequest ( new Uri ( response . Headers [ "Location" ] ) , IsPost , ContentType ) ;
175197 continue ;
176198 }
177199
0 commit comments