@@ -17,7 +17,11 @@ public class GitSsl
1717 public GitSsl ( )
1818 {
1919 this . certificatePathOrSubjectCommonName = null ;
20+
2021 this . isCertificatePasswordProtected = false ;
22+
23+ // True by default, both to have good default security settings and to match git behavior.
24+ // https://git-scm.com/docs/git-config#git-config-httpsslVerify
2125 this . ShouldVerify = true ;
2226 }
2327
@@ -27,32 +31,26 @@ public GitSsl(IDictionary<string, GitConfigSetting> configSettings) : this()
2731 {
2832 if ( configSettings . TryGetValue ( GitConfigSetting . HttpSslCert , out GitConfigSetting sslCerts ) )
2933 {
30- this . certificatePathOrSubjectCommonName = sslCerts . Values . Single ( ) ;
34+ this . certificatePathOrSubjectCommonName = sslCerts . Values . Last ( ) ;
3135 }
3236
3337 if ( configSettings . TryGetValue ( GitConfigSetting . HttpSslCertPasswordProtected , out GitConfigSetting isSslCertPasswordProtected ) )
3438 {
35- this . isCertificatePasswordProtected = isSslCertPasswordProtected . Values . Select ( bool . Parse ) . Single ( ) ;
39+ this . isCertificatePasswordProtected = isSslCertPasswordProtected . Values . Select ( bool . Parse ) . Last ( ) ;
3640 }
3741
3842 if ( configSettings . TryGetValue ( GitConfigSetting . HttpSslVerify , out GitConfigSetting sslVerify ) )
3943 {
40- this . ShouldVerify = sslVerify . Values . Select ( bool . Parse ) . Single ( ) ;
44+ this . ShouldVerify = sslVerify . Values . Select ( bool . Parse ) . Last ( ) ;
4145 }
4246 }
4347 }
44-
45- public bool ShouldVerify { get ; }
46-
47- public string GetCertificatePassword ( ITracer tracer , GitProcess git )
48- {
49- if ( git . TryGetCertificatePassword ( tracer , this . certificatePathOrSubjectCommonName , out string password , out string error ) )
50- {
51- return password ;
52- }
5348
54- return null ;
55- }
49+ /// <summary>
50+ /// Gets a value indicating whether SSL certificates being loaded should be verified. Also used to determine, whether client should verify server SSL certificate. True by default.
51+ /// </summary>
52+ /// <value><c>true</c> if should verify SSL certificates; otherwise, <c>false</c>.</value>
53+ public bool ShouldVerify { get ; }
5654
5755 public X509Certificate2 GetCertificate ( ITracer tracer , GitProcess gitProcess )
5856 {
@@ -74,33 +72,28 @@ public X509Certificate2 GetCertificate(ITracer tracer, GitProcess gitProcess)
7472
7573 if ( result == null )
7674 {
77- tracer . RelatedError ( "Certificate {0 } not found" , this . certificatePathOrSubjectCommonName ) ;
75+ tracer . RelatedError ( metadata , $ "Certificate { this . certificatePathOrSubjectCommonName } not found") ;
7876 }
7977
8078 return result ;
8179 }
8280
8381 private static void LogWithAppropriateLevel ( ITracer tracer , EventMetadata metadata , IEnumerable < X509Certificate2 > certificates , string logMessage )
8482 {
85- Action < EventMetadata , string > loggingFunction ;
8683 int numberOfCertificates = certificates . Count ( ) ;
8784
8885 switch ( numberOfCertificates )
8986 {
9087 case 0 :
91- loggingFunction = tracer . RelatedError ;
88+ tracer . RelatedError ( metadata , logMessage ) ;
9289 break ;
9390 case 1 :
94- loggingFunction = tracer . RelatedInfo ;
91+ tracer . RelatedInfo ( metadata , logMessage ) ;
9592 break ;
9693 default :
97- loggingFunction = tracer . RelatedWarning ;
94+ tracer . RelatedWarning ( metadata , logMessage ) ;
9895 break ;
9996 }
100-
101- loggingFunction (
102- metadata ,
103- logMessage ) ;
10497 }
10598
10699 private static string GetSubjectNameLineForLogging ( IEnumerable < X509Certificate2 > certificates )
@@ -110,6 +103,16 @@ private static string GetSubjectNameLineForLogging(IEnumerable<X509Certificate2>
110103 certificates . Select ( x => x . Subject ) ) ;
111104 }
112105
106+ private string GetCertificatePassword ( ITracer tracer , GitProcess git )
107+ {
108+ if ( git . TryGetCertificatePassword ( tracer , this . certificatePathOrSubjectCommonName , out string password , out string error ) )
109+ {
110+ return password ;
111+ }
112+
113+ return null ;
114+ }
115+
113116 private X509Certificate2 GetCertificateFromFile ( ITracer tracer , EventMetadata metadata , GitProcess gitProcess )
114117 {
115118 string certificatePassword = null ;
@@ -120,10 +123,7 @@ private X509Certificate2 GetCertificateFromFile(ITracer tracer, EventMetadata me
120123 if ( string . IsNullOrEmpty ( certificatePassword ) )
121124 {
122125 tracer . RelatedWarning (
123- new EventMetadata
124- {
125- { "SslCertificate" , this . certificatePathOrSubjectCommonName }
126- } ,
126+ metadata ,
127127 "Git config indicates, that certificate is password protected, but retrieved password was null or empty!" ) ;
128128 }
129129
@@ -145,7 +145,7 @@ private X509Certificate2 GetCertificateFromFile(ITracer tracer, EventMetadata me
145145 }
146146 catch ( CryptographicException cryptEx )
147147 {
148- metadata . Add ( "Exception" , cryptEx ) ;
148+ metadata . Add ( "Exception" , cryptEx . ToString ( ) ) ;
149149 tracer . RelatedError ( metadata , "Error, while loading certificate from disk" ) ;
150150 return null ;
151151 }
0 commit comments