Skip to content

Dispose the certificate chain elements with the chain #62531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,35 @@ private async Task<AuthenticateResult> ValidateCertificateAsync(X509Certificate2
}

var chainPolicy = BuildChainPolicy(clientCertificate, isCertificateSelfSigned);
using var chain = new X509Chain
var chain = new X509Chain
{
ChainPolicy = chainPolicy
};

var certificateIsValid = chain.Build(clientCertificate);
if (!certificateIsValid)
try
{
var certificateIsValid = chain.Build(clientCertificate);
if (!certificateIsValid)
{
var chainErrors = new List<string>(chain.ChainStatus.Length);
foreach (var validationFailure in chain.ChainStatus)
{
chainErrors.Add($"{validationFailure.Status} {validationFailure.StatusInformation}");
}
Logger.CertificateFailedValidation(clientCertificate.Subject, chainErrors);
return AuthenticateResults.InvalidClientCertificate;
}
}
finally
{
var chainErrors = new List<string>(chain.ChainStatus.Length);
foreach (var validationFailure in chain.ChainStatus)
// Disposing the chain does not dispose the elements we potentially built.
// Do the full walk manually to dispose.
for (int chainElementIndex = 0; chainElementIndex < chain.ChainElements.Count; ++chainElementIndex)
{
chainErrors.Add($"{validationFailure.Status} {validationFailure.StatusInformation}");
chain.ChainElements[chainElementIndex].Certificate.Dispose();
}
Logger.CertificateFailedValidation(clientCertificate.Subject, chainErrors);
return AuthenticateResults.InvalidClientCertificate;

chain.Dispose();
}

var certificateValidatedContext = new CertificateValidatedContext(Context, Scheme, Options)
Expand Down
32 changes: 23 additions & 9 deletions src/Shared/CertificateGeneration/UnixCertificateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,32 @@ public override TrustLevel GetTrustLevel(X509Certificate2 certificate)
// Building the chain will check whether dotnet trusts the cert. We could, instead,
// enumerate the Root store and/or look for the file in the OpenSSL directory, but
// this tests the real-world behavior.
using var chain = new X509Chain();
// This is just a heuristic for whether or not we should prompt the user to re-run with `--trust`
// so we don't need to check revocation (which doesn't really make sense for dev certs anyway)
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
if (chain.Build(certificate))
var chain = new X509Chain();
try
{
sawTrustSuccess = true;
// This is just a heuristic for whether or not we should prompt the user to re-run with `--trust`
// so we don't need to check revocation (which doesn't really make sense for dev certs anyway)
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
if (chain.Build(certificate))
{
sawTrustSuccess = true;
}
else
{
sawTrustFailure = true;
Log.UnixNotTrustedByDotnet();
}
}
else
finally
{
sawTrustFailure = true;
Log.UnixNotTrustedByDotnet();
// Disposing the chain does not dispose the elements we potentially built.
// Do the full walk manually to dispose.
for (int chainElementIndex = 0; chainElementIndex < chain.ChainElements.Count; ++chainElementIndex)
{
chain.ChainElements[chainElementIndex].Certificate.Dispose();
}

chain.Dispose();
}

// Will become the name of the file on disk and the nickname in the NSS DBs
Expand Down
Loading