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

3 answers

Sort by: Most helpful
  1. 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

  2. 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

  3. 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.