Skip to content

Commit e06837a

Browse files
committed
ManagedHttpSmartSubtransport: provide certificate callbacks
Provide certificate callback functionality when using the managed HTTP smart subtransport.
1 parent ffe518c commit e06837a

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

LibGit2Sharp/CertificateX509.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace LibGit2Sharp
1010
/// </summary>
1111
public class CertificateX509 : Certificate
1212
{
13-
1413
/// <summary>
1514
/// For mocking purposes
1615
/// </summary>
@@ -30,6 +29,11 @@ internal unsafe CertificateX509(git_certificate_x509* cert)
3029
Certificate = new X509Certificate(data);
3130
}
3231

32+
internal CertificateX509(X509Certificate cert)
33+
{
34+
Certificate = cert;
35+
}
36+
3337
internal unsafe IntPtr ToPointers(out IntPtr dataPtr)
3438
{
3539
var certData = Certificate.Export(X509ContentType.Cert);

LibGit2Sharp/Core/ManagedHttpSmartSubtransport.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.IO;
33
using System.Net;
4+
using System.Net.Security;
5+
using System.Security.Cryptography.X509Certificates;
46

57
namespace 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,27 @@ 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+
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)
104118
{
105119
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
106120

107121
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(endpointUrl);
108122
webRequest.UserAgent = "git/1.0 (libgit2 custom transport)";
109123
webRequest.ServicePoint.Expect100Continue = false;
110124
webRequest.AllowAutoRedirect = false;
125+
webRequest.ServerCertificateValidationCallback += CertificateValidationProxy;
111126

112127
if (isPost)
113128
{
@@ -147,7 +162,18 @@ private HttpWebResponse GetResponseWithRedirects()
147162
}
148163
catch (WebException ex)
149164
{
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+
}
151177
}
152178

153179
if (response.StatusCode == HttpStatusCode.OK)
@@ -171,7 +197,7 @@ private HttpWebResponse GetResponseWithRedirects()
171197
}
172198
else if (response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.Redirect)
173199
{
174-
request = CreateWebRequest(response.Headers["Location"], IsPost, ContentType);
200+
request = CreateWebRequest(new Uri(response.Headers["Location"]), IsPost, ContentType);
175201
continue;
176202
}
177203

LibGit2Sharp/SmartSubtransportStream.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ private unsafe static int Read(
102102
UIntPtr buf_size,
103103
out UIntPtr bytes_read)
104104
{
105+
GitErrorCode errorCode = GitErrorCode.Error;
105106
bytes_read = UIntPtr.Zero;
106107

107108
SmartSubtransportStream transportStream =
@@ -124,14 +125,19 @@ private unsafe static int Read(
124125

125126
return toReturn;
126127
}
128+
catch (NativeException ex)
129+
{
130+
errorCode = ex.ErrorCode;
131+
Proxy.giterr_set_str(GitErrorCategory.Net, ex);
132+
}
127133
catch (Exception ex)
128134
{
129135
Proxy.giterr_set_str(GitErrorCategory.Net, ex);
130136
}
131137
}
132138
}
133139

134-
return (int)GitErrorCode.Error;
140+
return (int)errorCode;
135141
}
136142

137143
private static unsafe int Write(IntPtr stream, IntPtr buffer, UIntPtr len)

0 commit comments

Comments
 (0)