You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unify/Simplify PFX Private Key lifetime on Windows #48789
Currently new X509Certificate2(pfx) and X509Certificate2Collection.Import(pfx) use two different lifetime trackers for the private key on a perphemeral import.
Single cert ctor:
The managed object tracks the lifetime
The native object has no awareness that the key is temporary
Copies made from new X509Certificate2(cert.Handle) are unaware the key is temporary
The key is thus erased when the original managed object is disposed (or finalized)
Collection import:
The native object has a property saying that the CLR wants to delete it
The managed object only tracks that it saw that property
Copies made from new X509Certificate2(cert.Handle) are aware the key is temporary
The key is thus erased when ANY object using the same native cert handle is disposed (or finalized)
The fact that they're different makes things complicated. Especially since a lot of copies get made "from cert.Handle" without it being obvious that it has happened (X509Certificate2Collection.Find, X509Chain.Build, etc).
We probably want to dump the collection behavior, and make it be that during collection import each individual created cert tracks the key cleanup. It should reduce premature key erasure, and at least give a predictable model.
An alternative would be to change the boolean property to a refcount property and change the single cert ctor to use it. That should mean that as long as the native cert is alive the key is alive. But we may end up with misalignments, resulting in keys not getting erased.
Activity
vcsjones commentedon Sep 13, 2024
Per offline discussion with @bartonjs, this was done as part of #102167.