Skip to content

Commit

Permalink
SLVS-1674 Improve CertificateChainValidator logging
Browse files Browse the repository at this point in the history
  • Loading branch information
georgii-borovinskikh-sonarsource committed Dec 2, 2024
1 parent 51f1284 commit f0c585e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ public CertificateChainValidator(ILogger logger)
{
this.logger = logger;
}

[ExcludeFromCodeCoverage] // can't easily unit test X509Chain
public bool ValidateChain(X509Certificate2 primaryCertificate, IEnumerable<X509Certificate2> additionalCertificates)
{
logger.LogVerbose($"[{nameof(CertificateChainValidator)}] Validating certificate: " + primaryCertificate);
logger.LogVerbose("[CertificateChainValidator] Validating certificate: " + primaryCertificate);
using var x509Chain = new X509Chain();

foreach (var additionalCertificate in additionalCertificates)
{
logger.LogVerbose($"[{nameof(CertificateChainValidator)}] Using chain certificate: " + primaryCertificate);
logger.LogVerbose("[CertificateChainValidator] Using chain certificate: " + primaryCertificate);

x509Chain.ChainPolicy.ExtraStore.Add(additionalCertificate);
}
Expand All @@ -59,12 +59,13 @@ public bool ValidateChain(X509Certificate2 primaryCertificate, IEnumerable<X509C

if (!validationResult)
{
logger.WriteLine(SLCoreStrings.CertificateValidator_Failed);
foreach (var x509ChainChainStatus in x509Chain.ChainStatus)
{
logger.LogVerbose($"{x509ChainChainStatus.Status}: {x509ChainChainStatus.StatusInformation}");
logger.WriteLine(SLCoreStrings.CertificateValidator_FailureReasonTemplate, x509ChainChainStatus.Status, x509ChainChainStatus.StatusInformation);
}
}

return validationResult;
}
}
19 changes: 18 additions & 1 deletion src/SLCore/SLCoreStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/SLCore/SLCoreStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,10 @@
<data name="UnexpectedServerConnectionType" xml:space="preserve">
<value>Unexpected server connection type</value>
</data>
<data name="CertificateValidator_Failed" xml:space="preserve">
<value>[CertificateChainValidator] Certificate validation failed for the following reason(s):</value>
</data>
<data name="CertificateValidator_FailureReasonTemplate" xml:space="preserve">
<value>[CertificateChainValidator] {0}: {1}</value>
</data>
</root>

0 comments on commit f0c585e

Please sign in to comment.