Skip to content

Unify/Simplify PFX Private Key lifetime on Windows #48789

Closed

Description

@bartonjs

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

  1. added this to the Future milestone on Feb 26, 2021
  2. ghost added
    untriaged
    on Feb 26, 2021
  3. removed
    untriaged
    on Feb 26, 2021
  4. vcsjones commented on Sep 13, 2024

    @vcsjones
    Member

    Per offline discussion with @bartonjs, this was done as part of #102167.

  5. locked and limited conversation to collaborators on Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions