1
1
using System ;
2
2
using System . IO ;
3
3
using System . Net ;
4
+ using System . Net . Security ;
5
+ using System . Security . Cryptography . X509Certificates ;
4
6
5
7
namespace LibGit2Sharp . Core
6
8
{
@@ -50,12 +52,12 @@ private class ManagedHttpSmartSubtransportStream : SmartSubtransportStream
50
52
public ManagedHttpSmartSubtransportStream ( ManagedHttpSmartSubtransport parent , string endpointUrl , bool isPost , string contentType )
51
53
: base ( parent )
52
54
{
53
- EndpointUrl = endpointUrl ;
55
+ EndpointUrl = new Uri ( endpointUrl ) ;
54
56
IsPost = isPost ;
55
57
ContentType = contentType ;
56
58
}
57
59
58
- private string EndpointUrl
60
+ private Uri EndpointUrl
59
61
{
60
62
get ;
61
63
set ;
@@ -100,14 +102,23 @@ public override int Write(Stream dataStream, long length)
100
102
return 0 ;
101
103
}
102
104
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 )
104
114
{
105
115
ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 ;
106
116
107
117
HttpWebRequest webRequest = ( HttpWebRequest ) HttpWebRequest . Create ( endpointUrl ) ;
108
118
webRequest . UserAgent = "git/1.0 (libgit2 custom transport)" ;
109
119
webRequest . ServicePoint . Expect100Continue = false ;
110
120
webRequest . AllowAutoRedirect = false ;
121
+ webRequest . ServerCertificateValidationCallback += CertificateValidationProxy ;
111
122
112
123
if ( isPost )
113
124
{
@@ -147,7 +158,18 @@ private HttpWebResponse GetResponseWithRedirects()
147
158
}
148
159
catch ( WebException ex )
149
160
{
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
+ }
151
173
}
152
174
153
175
if ( response . StatusCode == HttpStatusCode . OK )
@@ -171,7 +193,7 @@ private HttpWebResponse GetResponseWithRedirects()
171
193
}
172
194
else if ( response . StatusCode == HttpStatusCode . Moved || response . StatusCode == HttpStatusCode . Redirect )
173
195
{
174
- request = CreateWebRequest ( response . Headers [ "Location" ] , IsPost , ContentType ) ;
196
+ request = CreateWebRequest ( new Uri ( response . Headers [ "Location" ] ) , IsPost , ContentType ) ;
175
197
continue ;
176
198
}
177
199
0 commit comments