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,27 @@ 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
+
109
+ if ( ret != 0 )
110
+ {
111
+ throw new UserCancelledException ( "bar" ) ;
112
+ }
113
+
114
+ return true ;
115
+ }
116
+
117
+ private HttpWebRequest CreateWebRequest ( Uri endpointUrl , bool isPost , string contentType )
104
118
{
105
119
ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 ;
106
120
107
121
HttpWebRequest webRequest = ( HttpWebRequest ) HttpWebRequest . Create ( endpointUrl ) ;
108
122
webRequest . UserAgent = "git/1.0 (libgit2 custom transport)" ;
109
123
webRequest . ServicePoint . Expect100Continue = false ;
110
124
webRequest . AllowAutoRedirect = false ;
125
+ webRequest . ServerCertificateValidationCallback += CertificateValidationProxy ;
111
126
112
127
if ( isPost )
113
128
{
@@ -147,7 +162,18 @@ private HttpWebResponse GetResponseWithRedirects()
147
162
}
148
163
catch ( WebException ex )
149
164
{
150
- response = ( HttpWebResponse ) ex . Response ;
165
+ if ( ex . Response != null )
166
+ {
167
+ response = ( HttpWebResponse ) ex . Response ;
168
+ }
169
+ else if ( ex . InnerException != null )
170
+ {
171
+ throw ex . InnerException ;
172
+ }
173
+ else
174
+ {
175
+ throw new Exception ( "unknown network failure" ) ;
176
+ }
151
177
}
152
178
153
179
if ( response . StatusCode == HttpStatusCode . OK )
@@ -171,7 +197,7 @@ private HttpWebResponse GetResponseWithRedirects()
171
197
}
172
198
else if ( response . StatusCode == HttpStatusCode . Moved || response . StatusCode == HttpStatusCode . Redirect )
173
199
{
174
- request = CreateWebRequest ( response . Headers [ "Location" ] , IsPost , ContentType ) ;
200
+ request = CreateWebRequest ( new Uri ( response . Headers [ "Location" ] ) , IsPost , ContentType ) ;
175
201
continue ;
176
202
}
177
203
0 commit comments