Skip to content

Commit 5c5619d

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

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-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: 27 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,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

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)