Skip to content
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

Question: do I need to dispose the X509Certificate2 elements in an X509Chain myself? #112987

Closed
drauch opened this issue Feb 27, 2025 · 6 comments · Fixed by dotnet/dotnet-api-docs#11042
Assignees
Labels
area-System.Security documentation Documentation bug or enhancement, does not impact product or test code
Milestone

Comments

@drauch
Copy link

drauch commented Feb 27, 2025

I've experienced that the following code works just fine:

var x509Chain = new X509Chain();
x509Chain.Build(someCert);

var certs = x509Chain.ChainElements.Select(ce => ce.Certificate).ToList();
x509Chain.Dispose();

// make use of certs, even calling methods like GetRSAPublicKey() does not throw

It looks like the X509Certificate2 objects inside a chain are not disposed with the chain. Is that so? Do I need to dispose them myself when I dispose the chain to do proper cleanup?

Best regards,
D.R.

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Feb 27, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.

@vcsjones
Copy link
Member

vcsjones commented Feb 27, 2025

It looks like the X509Certificate2 objects inside a chain are not disposed with the chain. Is that so?

Correct.

You can see that in places we use X509Chain ourselves, we dispose of the chain elements, like here:

finally
{
for (int i = 0; i < chain.ChainElements.Count; i++)
{
chain.ChainElements[i].Certificate.Dispose();
}
chain.Dispose();
}

Do I need to dispose them myself when I dispose the chain to do proper cleanup?

Calling Dispose on them will make the freeing of native resources deterministic, as well as keep down the amount of work the .NET Finalizer does during garbage collection. If you can dispose of them, you should.

Some possible follow up questions, answered.

Is it a memory leak if I don't dispose of the chain elements?

No, they will be cleaned up by a finalizer for the underlying certificate PAL.

Why not dispose of the chain elements when X509Chain is disposed?

This is how .NET Framework does it, so it has been this way for a long time. Changing it now would be a breaking change (because if we did dispose of the chain elements, then they would become unusable after the chain is disposed, which some people may, and likely, have a dependency on).

@vcsjones vcsjones added question Answer questions and provide assistance, not an issue with source code or documentation. and removed untriaged New issue has not been triaged by the area owner labels Feb 27, 2025
@vcsjones vcsjones added this to the Future milestone Feb 27, 2025
@drauch
Copy link
Author

drauch commented Feb 28, 2025

Thank you for your detailed answer!

As a small suggestion: this should probably be documented in the X509Chain class docs.

@Clockwork-Muse
Copy link
Contributor

(sorry, fat fingered something on my phone)

Changing it now would be a breaking change (because if we did dispose of the chain elements, then they would become unusable after the chain is disposed, which some people may, and likely, have a dependency on).

In particular, with a certificate chain it's quite likely that multiple chains will be built with the same root certificate, and if disposing of the chain disposed of all certificates it would force reloading of the root (and intermediate) certificates.

@vcsjones
Copy link
Member

As a small suggestion: this should probably be documented in the X509Chain class docs.

That's probably reasonable. Where would you generally expect to see this documented? There are a couple of places that are reasonable

  • In "Remarks" on X509Chain class
  • In "Remarks" on X509Chain.ChainElements property
  • In "Remarks" on X509Chain.Dispose method

@drauch
Copy link
Author

drauch commented Feb 28, 2025

@vcsjones I looked at all those places, so it wouldn't have mattered much. Foremost I guess on the X509Chain class itself.

@vcsjones vcsjones added documentation Documentation bug or enhancement, does not impact product or test code and removed question Answer questions and provide assistance, not an issue with source code or documentation. labels Mar 1, 2025
@vcsjones vcsjones self-assigned this Mar 1, 2025
@vcsjones vcsjones modified the milestones: Future, 10.0.0 Mar 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Security documentation Documentation bug or enhancement, does not impact product or test code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants