Microsoft Software KSP: NCryptOpenKey returns NTE_BAD_KEYSET after successful NCryptFinalizeKey with protected DACL

Agnel Vanjare 0 Reputation points
2026-11-24T17:43:12.7033333+00:00

I am investigating a Windows CNG key-access issue using the Microsoft Software Key Storage Provider on Windows 11.

Problem

A non-production ECDSA P-256 machine key is successfully created, configured, finalized, and read back using its original key handle. However, after closing that handle, NCryptOpenKey fails with 0x80090016 (NTE_BAD_KEYSET).

Configuration

  • Provider: Microsoft Software Key Storage Provider

Key algorithm: ECDSA P-256

Machine key: NCRYPT_MACHINE_KEY_FLAG

Export policy: 0 (non-exportable)

Usage: NCRYPT_ALLOW_SIGNING_FLAG

Reopen flags: NCRYPT_MACHINE_KEY_FLAG | NCRYPT_SILENT_FLAG

Caller: Same elevated Windows user before and after closing the handle

The key has a protected DACL with two allow ACEs:

BUILTIN\Administrators: 0x000E0000 (READ_CONTROL | WRITE_DAC | WRITE_OWNER)

Dedicated publisher account: 0x00020000 (READ_CONTROL)

No additional ACEs are present.

Observed results

NCryptCreatePersistedKey succeeds.

Key configuration and security descriptor assignment succeed.

NCryptFinalizeKey succeeds.

Before closing the original handle, NCRYPT_UNIQUE_NAME_PROPERTY, key properties, security descriptor, and public key blob are read successfully.

NCryptFreeObject succeeds.

NCryptOpenKey fails with NTE_BAD_KEYSET (0x80090016).

The provider-issued unique name identifies an existing 1,380-byte machine-key backing file. Its owner and protected DACL match the configured descriptor.

Windows Security auditing records successful key creation and key-file write operations (Events 5061 and 5058), followed by a failed Open Key operation (Event 5061).

Questions

What is the supported minimum access-rights model for opening and using a persisted machine key with Microsoft Software KSP?

During NCryptOpenKey, does the key-storage access check run under the caller's token, SYSTEM/LSA, or another security context?

Can a protected DACL containing only READ_CONTROL, WRITE_DAC, and WRITE_OWNER cause this reopen failure, even though key creation and finalization succeed?

How should a dedicated publisher be granted permission to open/sign while preventing unauthorized key deletion or security descriptor changes?

Is there a documented diagnostic method to identify the exact failed access check inside Microsoft Software KSP?

I am seeking a documented least-privilege solution, not an unrestricted Full Control workaround.

The affected test keys have been preserved without retries, ACL modifications, or cleanup.I am investigating a Windows CNG key-access issue using the Microsoft Software Key Storage Provider on Windows 11.

Problem

A non-production ECDSA P-256 machine key is successfully created, configured, finalized, and read back using its original key handle. However, after closing that handle, NCryptOpenKey fails with 0x80090016 (NTE_BAD_KEYSET).

Configuration

Provider: Microsoft Software Key Storage Provider

Key algorithm: ECDSA P-256

Machine key: NCRYPT_MACHINE_KEY_FLAG

Export policy: 0 (non-exportable)

Usage: NCRYPT_ALLOW_SIGNING_FLAG

Reopen flags: NCRYPT_MACHINE_KEY_FLAG | NCRYPT_SILENT_FLAG

Caller: Same elevated Windows user before and after closing the handle

The key has a protected DACL with two allow ACEs:

BUILTIN\Administrators: 0x000E0000 (READ_CONTROL | WRITE_DAC | WRITE_OWNER)

Dedicated publisher account: 0x00020000 (READ_CONTROL)

No additional ACEs are present.

Observed results

NCryptCreatePersistedKey succeeds.

Key configuration and security descriptor assignment succeed.

NCryptFinalizeKey succeeds.

Before closing the original handle, NCRYPT_UNIQUE_NAME_PROPERTY, key properties, security descriptor, and public key blob are read successfully.

NCryptFreeObject succeeds.

NCryptOpenKey fails with NTE_BAD_KEYSET (0x80090016).

The provider-issued unique name identifies an existing 1,380-byte machine-key backing file. Its owner and protected DACL match the configured descriptor.

Windows Security auditing records successful key creation and key-file write operations (Events 5061 and 5058), followed by a failed Open Key operation (Event 5061).

Questions

What is the supported minimum access-rights model for opening and using a persisted machine key with Microsoft Software KSP?

During NCryptOpenKey, does the key-storage access check run under the caller's token, SYSTEM/LSA, or another security context?

Can a protected DACL containing only READ_CONTROL, WRITE_DAC, and WRITE_OWNER cause this reopen failure, even though key creation and finalization succeed?

How should a dedicated publisher be granted permission to open/sign while preventing unauthorized key deletion or security descriptor changes?

Is there a documented diagnostic method to identify the exact failed access check inside Microsoft Software KSP?

I am seeking a documented least-privilege solution, not an unrestricted Full Control workaround.

The affected test keys have been preserved without retries, ACL modifications, or cleanup.

Windows development | Windows API - Win32
0 comments No comments

4 answers

Sort by: Most helpful
  1. Gatlin Le (WICLOUD CORPORATION) 1,495 Reputation points Microsoft External Staff Moderator
    2026-11-25T03:53:32.3533333+00:00

    Hi @Agnel Vanjare ,

    Thank you for preserving the keys and for the detailed write-up. I reproduced this on a test machine, and the DACL is the cause: when NCryptOpenKey reopens the key, LSASS opens the backing file requesting 0x120089 (FILE_GENERIC_READ) under the caller's identity, and your DACL grants only READ_CONTROL from that mask. Granting FR to the publisher let it open and sign, while changing the descriptor and deleting the key were still denied.

    Creation and finalization succeed because, as the NCryptOpenKey remarks explain, Microsoft software-based KSPs cache private key material in LSA for as long as a handle to the key is open. After NCryptFreeObject, the reopen has to read the file again, and on my build the denied read was reported as NTE_BAD_KEYSET.

    I tested on Windows 11 Pro build 26300.9457 with an ECDSA P-256 machine key set up like yours (Export Policy 0, signing usage, protected DACL owned by Administrators), reopening from a new process with NCRYPT_MACHINE_KEY_FLAG | NCRYPT_SILENT_FLAG and using a standard local user as the publisher. These are the five cases I ran:

    1. Your DACL, O:BAD:P(A;;0xE0000;;;BA)(A;;0x20000;;;<publisher-SID>): reopen returned 0x80090016 for both the admin and the publisher.
    2. Your DACL plus FR for Administrators, no SYSTEM ACE: the admin reopened and signed successfully.
    3. FR for SYSTEM only, Administrators keeping 0xE0000: the admin still got 0x80090016.
    4. O:BAD:P(A;;FA;;;SY)(A;;FA;;;BA)(A;;FR;;;<publisher-SID>): the publisher opened and signed, got NTE_PERM (0x80090010) from NCryptSetProperty with NCRYPT_SECURITY_DESCR_PROPERTY, and E_ACCESSDENIED (0x80070005) from NCryptDeleteKey, with the backing file still present afterwards.
    5. The same layout without the SYSTEM ACE: identical results to case 4.

    On the security context, cases 2 and 3 show that the caller needs read access and that read access for SYSTEM alone is not enough. Each failed reopen also produced a failed Event 4656 with Process Name lsass.exe, the calling account as Subject (the admin in one run, the publisher in the other), Access Mask 0x120089, and Access Reasons showing READ_CONTROL and ReadAttributes granted but ReadData, ReadEA and SYNCHRONIZE not granted. So for this file open path, the access check runs under the caller's security context through LSASS. I would not extend that to every internal check the KSP performs.

    As for the access model, the public CNG documentation does not define a separate sign-only right for Microsoft Software KSP keys. What I can show is that FR on the key is sufficient for open and sign, and that with Administrators as owner and no WRITE_DAC, WRITE_OWNER or DELETE for the publisher, the descriptor change and the delete were both refused. I did not try every bit combination, so FR is a working value rather than a proven minimum. The KSP also reported FR and FA back as 0x80120089 and 0xD01F01FF, while the file DACL showed plain FR and FA, so it is worth checking the descriptor and the file separately when you verify. Local administrators remain inside the trust boundary, since the same NCryptOpenKey remarks note that they can access the LSA-cached copy of the key. For your preserved keys, Administrators already hold WRITE_DAC in the current DACL, so adding FR for the publisher on one of them should let you confirm the same result on your machine while keeping the other untouched as evidence.

    For diagnosis, the 4656 events only appeared on my build once Handle Manipulation was enabled together with File System failure auditing, which matches Advanced Audit Policy Configuration listing 4656 under Handle Manipulation. I used System Integrity for 5061 and Other System Events for 5058, and placed the SACL on a fresh test key with your DACL. To audit one of your preserved keys without touching its descriptor, a global file SACL through auditpol resourceSACL is stored in policy instead, although I did not run that variant myself.

    One more observation: the failed reopen did not produce a 5058 "Read persisted key from file" record. I only saw that operation on successful opens, so a missing 5058 does not rule out a denied read.

    Everything above is based on the public documentation and on what I could reproduce from the details in your question, on one machine and one build, so it describes observed behavior rather than a documented contract. This is as far as I can take it from the forum side. If you need Microsoft to confirm the access model for a production design, Windows Developer Support is the right channel, and the 4656 record with mask 0x120089 would be the most useful item to bring.

    I hope the test results and the documentation help you move forward with the publisher design. If this answer resolves your question, please consider this guidance when convenient, as it can help others with a similar question find it more easily.

    Thank you.

    Was this answer helpful?

    0 comments No comments

  2. Agnel Vanjare 0 Reputation points
    2026-11-24T17:49:51.33+00:00

    Thank you. This is an AI-generated response and does not resolve the provider-specific access requirements.

    We already have Security Events 5058/5061, successful key persistence evidence, and the exact failed NCryptOpenKey status.

    Could a human Microsoft Windows CNG / Software KSP specialist review this case and provide a supported least-privilege ACL model or an official escalation path?

    We need the actual access-context and required-rights contract, not another generic troubleshooting response.

    Was this answer helpful?

    0 comments No comments

  3. Agnel Vanjare 0 Reputation points
    2026-11-24T17:48:12.6333333+00:00

    Thank you for the preliminary analysis.

    We have already collected Windows Security Events 5058 and 5061, including successful persisted-key creation and a failed NCryptOpenKey with 0x80090016.

    The backing file exists and its protected DACL matches the descriptor applied during key creation.

    We are preserving both failed test keys without further mutation.

    Could a human Windows CNG / Microsoft Software KSP specialist please review this case?

    We need clarification on two specific points:

    What supported access-rights and security-context requirements allow a persisted machine key to be reopened and used for signing?

    If Microsoft does not expose a supported sign-only ACL model, what Microsoft-supported least-privilege design should be used to isolate signing authority from key deletion and security administration?

    We are specifically requesting an engineering-supported answer or authoritative reference, rather than additional AI-generated speculation or broad-permission workarounds.

    Was this answer helpful?

    0 comments No comments

  4. AI answer

    2026-11-24T17:44:46.7666667+00:00

    Microsoft does not publish a supported minimum ACL mask for “open and sign” with a Microsoft Software KSP key, so the preserved keys should remain unchanged and the exact provider-side access denial should be investigated through CNG auditing and Microsoft support rather than by guessing additional rights.

    What the current DACL grants

    The masks shown contain only standard security-administration rights:

    • READ_CONTROL (0x00020000): read the owner, group, and DACL.
    • WRITE_DAC (0x00040000): modify the DACL.
    • WRITE_OWNER (0x00080000): change the owner.

    They contain no generic rights and no object-specific rights in bits 0–15. Standard rights govern security-descriptor operations; they do not inherently grant normal access to object contents or operations ^1^ ^2^.

    Consequently, your DACL is consistent with this failure: the publisher can inspect security information, while Administrators can inspect and alter ownership or the DACL, but neither ACE expressly grants an object-specific or generic use right. However, Microsoft does not document the Microsoft Software KSP’s generic-right mapping or the exact access mask requested by NCryptOpenKey. It therefore cannot be stated from the public contract that a particular missing bit definitively caused this instance.

    Successful creation and finalization do not disprove a reopen-access problem. The original handle was already established, and Microsoft software KSPs cache private-key material in LSA while a handle remains open. NCryptOpenKey starts a new retrieval after that handle is released ^3^.

    Security context used during open

    The documented architecture is:

    1. The application calls the key-storage router in Ncrypt.dll.
    2. The router uses LRPC to reach the key-isolation process.
    3. The Microsoft KSP runs in the LSA process.
    4. Private-key access passes through this architecture and is audited ^4^.

    That establishes where provider processing occurs, not which token is used for this provider’s backing-object access check. The public CNG documentation does not say whether this specific check uses:

    • the caller’s primary token,
    • an impersonation token representing the caller,
    • LSA/SYSTEM’s token, or
    • multiple checks under different contexts.

    General Windows RPC impersonation rules say that access checks use the client’s impersonation token while a server is impersonating, but that does not document whether or when Microsoft Software KSP performs impersonation ^5^. Therefore, describing the check simply as “SYSTEM access” or “caller access” would be unsupported.

    Least privilege and deletion

    The public CNG contract documents a security descriptor for persisted keys and permits setting its DACL through NCRYPT_SECURITY_DESCR_PROPERTY ^6^ ^7^. It does not publish:

    • a KEY_SIGN or equivalent CNG ACL right;
    • an “open/use but not delete” access mask;
    • the Microsoft Software KSP’s generic-right mapping; or
    • separate documented ACL authorization requirements for NCryptSignHash.

    This creates an important limitation: a documented DACL recipe guaranteeing open/sign while preventing NCryptDeleteKey cannot be derived from the public API. NCryptDeleteKey accepts a key handle obtained through NCryptOpenKey, and its documentation does not identify a separate required access right ^8^.

    Do not grant the publisher:

    • WRITE_DAC,
    • WRITE_OWNER, or
    • DELETE

    if the account must not modify ownership/security or receive an explicit standard delete right. But omitting those rights alone is not a documented guarantee that a caller able to open and use the key cannot delete it through the KSP.

    For deployment, separate control should therefore be added outside the undocumented KSP ACL mapping—for example, run signing behind a narrowly scoped signing service and do not give the publisher process direct possession of an open KSP handle. The exact service design and authorization policy are application-level controls, not CNG access-mask guarantees.

    Diagnostics without changing the preserved keys

    Enable the documented Microsoft KSP audit category from an elevated command prompt:

    auditpol /set /subcategory:"other system events" /success:enable /failure:enable
    auditpol /get /subcategory:"other system events"
    

    Reproduce only NCryptOpenKey, then preserve:

    • the Security log;
    • the failing 5061 event and its caller/process fields;
    • provider name, key name, unique name, flags, and status;
    • the security descriptor in SDDL and binary form;
    • the key-file metadata and ACL;
    • Windows edition, build, and patch level.

    Microsoft documents that persistent-key reads/writes and relevant cryptographic failures are audited, and that Microsoft KSP audit records are generated in user mode in LSA ^9^.

    ETW can supplement those records, but the public ETW documentation describes the tracing framework rather than a Microsoft Software KSP event that exposes the exact desired access mask and token ^10^. There is no documented public tool that decodes this failure into “SID X was denied mask Y by ACE Z” inside the Microsoft Software KSP.

    Because the original keys are preserved and the missing rights/provider mapping are undocumented, submit the artifacts through Microsoft support and request analysis of the CNG/LSA provider trace. Test candidate ACLs only on newly generated disposable keys; changing the preserved DACL would destroy the evidence needed to distinguish an access-check failure from another provider retrieval failure. NTE_BAD_KEYSET alone means the key could not be found/opened under the documented NCryptOpenKey contract and does not identify the failed internal check ^3^.


    References

    1. ACCESS_MASK
    2. Standard Access Rights - Win32 apps | Microsoft Learn
    3. NCryptOpenKey function (ncrypt.h)
    4. Key Storage and Retrieval
    5. Impersonation Tokens
    6. Key Storage Property Identifiers
    7. NCryptSetProperty function (ncrypt.h)
    8. NCryptDeleteKey function (ncrypt.h)
    9. CNG Features
    10. About Event Tracing
    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.