Skip to content

Commit

Permalink
http: add support for client certificates
Browse files Browse the repository at this point in the history
Add support for automatically sending client TLS certificates using the
Git configuration setting 'http.sslAutoClientCert'.

This setting is currently only present in Git for Windows, and there is
only respected when the SSL backend is "schannel".
  • Loading branch information
mjcheetham committed Mar 14, 2023
1 parent c12409f commit 19220f0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/shared/Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public static class Http
public const string SslBackend = "sslBackend";
public const string SslVerify = "sslVerify";
public const string SslCaInfo = "sslCAInfo";
public const string SslAutoClientCert = "sslAutoClientCert";
}

public static class Remote
Expand Down
14 changes: 14 additions & 0 deletions src/shared/Core/HttpClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ public HttpClient CreateClient()
handler = new HttpClientHandler();
}

// Trace Git's chosen SSL/TLS backend
_trace.WriteLine($"Git's SSL/TLS backend is: {_settings.TlsBackend}");

// Mirror Git for Windows and only send client TLS certificates automatically if we're using
// the schannel backend _and_ the user has opted in to sending them.
if (_settings.TlsBackend == TlsBackend.Schannel &&
_settings.AutomaticallyUseClientCertificates)
{
_trace.WriteLine("Configured to automatically send TLS client certificates.");
handler.ClientCertificateOptions = ClientCertificateOption.Automatic;
}

// Configure server certificate verification and warn if we're bypassing validation

// IsCertificateVerificationEnabled takes precedence over custom TLS cert verification
if (!_settings.IsCertificateVerificationEnabled)
{
Expand Down
8 changes: 8 additions & 0 deletions src/shared/Core/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ public interface ISettings : IDisposable
/// </summary>
bool IsCertificateVerificationEnabled { get; }

/// <summary>
/// Automatically send client TLS certificates.
/// </summary>
bool AutomaticallyUseClientCertificates { get; }

/// <summary>
/// Get the proxy setting if configured, or null otherwise.
/// </summary>
Expand Down Expand Up @@ -563,6 +568,9 @@ public bool IsCertificateVerificationEnabled
}
}

public bool AutomaticallyUseClientCertificates =>
!TryGetSetting(null, KnownGitCfg.Credential.SectionName, KnownGitCfg.Http.SslAutoClientCert, out string value) && value.ToBooleanyOrDefault(false);

public string CustomCertificateBundlePath =>
TryGetPathSetting(KnownEnvars.GitSslCaInfo, KnownGitCfg.Http.SectionName, KnownGitCfg.Http.SslCaInfo, out string value) ? value : null;

Expand Down
2 changes: 2 additions & 0 deletions src/shared/TestInfrastructure/Objects/TestSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class TestSettings : ISettings

public bool IsCertificateVerificationEnabled { get; set; } = true;

public bool AutomaticallyUseClientCertificates { get; set; }

public ProxyConfiguration ProxyConfiguration { get; set; }

public string ParentWindowId { get; set; }
Expand Down

0 comments on commit 19220f0

Please sign in to comment.