The Data Protection key ring is encrypted at rest with a certificate, or with DPAPI on Windows #275

Merged
claude merged 5 commits from feature/42-keyring-at-rest into develop 2026-11-05 22:09:15 -05:00
Owner

The Data Protection key ring, which unlocks every stored secret, is now encrypted at rest (owner decisions Q-REL-12, R2-KEYRING).

  • New DataProtection options: CertificatePath, CertificatePassword, PreviousCertificatePath, PreviousCertificatePassword (ENVIRONMENT.md: 342 → 346 variables).
  • A configured certificate protects new keys on every platform, Windows included. The current and previous certificates both unprotect, so a certificate change keeps old keys readable.
  • With no certificate on Windows: DPAPI, scoped to the service account (machine scope would let any local account decrypt the ring). With no certificate elsewhere: unchanged (owner-only folder).
  • Existing plaintext keys stay readable; new keys are written protected.
  • A bad certificate setting (missing or unreadable file, wrong password, no private key, non-RSA key, previous without current) stops start-up with exit 78 and a message naming the setting. It never falls back to plaintext and never prints the password.
  • A restore first checks, in a throwaway in-memory ring, that this server can read the backup's key files. If not, it returns 400 naming the cause and the setting, and nothing is restored.
  • README Security and backup sections, CHANGELOG Unreleased.

Verified on Linux: Web.Tests 2065, Backup.Tests 60, Identity.Tests 77; build 0 warnings; red/green (6 tests fail with protection disabled). The verifier's low findings are fixed (unreadable PFX now exits 78; an untested README claim about expired certificates removed).

Windows acceptance passed on Lofnheim on 2026-11-05 (results in the PR comment): DPAPI under NT SERVICE\Ninlil, a certificate added after DPAPI, certificate-protected new keys, the exit-78 refusal from a console, and a cross-machine restore to Linux. Under the Windows service, a refusal is not reported to the event log or the SCM; that applies to every startup refusal and is tracked in #336.

Closes #42

🤖 Generated with Claude Code

https://claude.ai/code/session_01T33E3pmQvqkTriyQUMNUTK

The Data Protection key ring, which unlocks every stored secret, is now encrypted at rest (owner decisions Q-REL-12, R2-KEYRING). - New `DataProtection` options: `CertificatePath`, `CertificatePassword`, `PreviousCertificatePath`, `PreviousCertificatePassword` (ENVIRONMENT.md: 342 → 346 variables). - A configured certificate protects new keys on every platform, Windows included. The current and previous certificates both unprotect, so a certificate change keeps old keys readable. - With no certificate on Windows: DPAPI, scoped to the service account (machine scope would let any local account decrypt the ring). With no certificate elsewhere: unchanged (owner-only folder). - Existing plaintext keys stay readable; new keys are written protected. - A bad certificate setting (missing or unreadable file, wrong password, no private key, non-RSA key, previous without current) stops start-up with exit 78 and a message naming the setting. It never falls back to plaintext and never prints the password. - A restore first checks, in a throwaway in-memory ring, that this server can read the backup's key files. If not, it returns 400 naming the cause and the setting, and nothing is restored. - README Security and backup sections, CHANGELOG Unreleased. Verified on Linux: Web.Tests 2065, Backup.Tests 60, Identity.Tests 77; build 0 warnings; red/green (6 tests fail with protection disabled). The verifier's low findings are fixed (unreadable PFX now exits 78; an untested README claim about expired certificates removed). Windows acceptance passed on Lofnheim on 2026-11-05 (results in the PR comment): DPAPI under `NT SERVICE\Ninlil`, a certificate added after DPAPI, certificate-protected new keys, the exit-78 refusal from a console, and a cross-machine restore to Linux. Under the Windows service, a refusal is not reported to the event log or the SCM; that applies to every startup refusal and is tracked in #336. Closes #42 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01T33E3pmQvqkTriyQUMNUTK
Refs #42

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T33E3pmQvqkTriyQUMNUTK
Refs #42

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T33E3pmQvqkTriyQUMNUTK
A key-ring certificate the service account cannot read stops the start with a message naming the setting
Some checks failed
Build / Pack and Push NuGet Packages (dhcp) (pull_request) Successful in 1m32s
Build / Build Binary (linux-arm64) (pull_request) Successful in 33s
Build / Build Binary (macos-amd64) (pull_request) Successful in 37s
Build / Build Binary (linux-amd64) (pull_request) Successful in 39s
Build / Build Binary (windows-amd64) (pull_request) Successful in 14s
Build / Build Binary (macos-arm64) (pull_request) Successful in 3m57s
Build / Build and Push Image (pull_request) Successful in -2m32s
Build / Smoke Test the Windows Binary (pull_request) Successful in 17s
Build / Build Arch Linux Packages (pull_request) Successful in -2m22s
Build / Test .NET (push) Failing after 10m18s
Build / Pack and Push NuGet Packages (dns-core) (push) Has been skipped
Build / Pack and Push NuGet Packages (common) (push) Has been skipped
Build / Build Binary (linux-arm64) (push) Has been skipped
Build / Stage Draft Release (push) Has been skipped
Build / Pack and Push NuGet Packages (dhcp) (push) Has been skipped
Build / Pack and Push NuGet Packages (dns-server) (push) Has been skipped
Build / Build Binary (linux-amd64) (push) Has been skipped
Build / Build Binary (windows-amd64) (push) Has been skipped
Build / Build Binary (macos-amd64) (push) Has been skipped
Build / Build Binary (macos-arm64) (push) Has been skipped
Build / Build Arch Linux Packages (push) Has been skipped
Build / Build and Push Image (push) Has been skipped
Build / Build and Test Deb and RPM Packages (push) Has been skipped
Build / Publish Release (push) Has been skipped
Build / Build and Test Deb and RPM Packages (pull_request) Successful in -24s
Build / Pack and Push NuGet Packages (dns-core) (pull_request) Successful in 7m7s
Build / Publish Release (pull_request) Successful in 6s
Build / Pipeline (pull_request) Successful in 0s
Build / Smoke Test the Windows Binary (push) Has been skipped
Build / Pipeline (push) Failing after 0s
c4e6e5572d
Refs #42

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T33E3pmQvqkTriyQUMNUTK
Author
Owner

Windows acceptance on Lofnheim (Windows 10.0.26200), 2026-11-05, with the exe built from this branch after taking in develop (9c1d2b7b, bd3fd307):

Check Result
No certificate, service under NT SERVICE\Ninlil: new key file DPAPI (DpapiXmlDecryptor), no plaintext <masterKey>
Restart, then a TSIG-signed query using a stored TSIG secret (dig -y from Linux) the server signs with the decrypted secret (TSIG NOERROR); a wrong secret gets BADSIG
Certificate added to that DPAPI instance starts Healthy, the DPAPI key still decrypts
Fresh instance under NT SERVICE\NinlilB with a certificate new key is certificate-protected (EncryptedXmlDecryptor), not DPAPI
Wrong certificate password, console run exit 78, message names DataProtection:CertificatePath and CertificatePassword, no password printed
Wrong password or missing file, as a service the start stops, but the SCM shows exit 0 and nothing reaches the event log. That applies to every startup refusal in Program.cs, so it is filed as #336
Certificate backup from NinlilB restored on Linux with the same certificate restored; after a restart the restored key ring decrypts NinlilB's TSIG secret
DPAPI backup restored on Linux / under another Windows account 400 naming DPAPI and DataProtection:CertificatePath, nothing restored
KeyRing*/DataProtection tests natively on Windows 30/30 after fixing KeyRing_OnWindowsWithDpapi_..., which treated DPAPI's own <value> element as plaintext

Ninlil.Dns.Server.Backup.Tests on Windows: 23 of 64 fail at cleanup (test.sqlite3 in use). Develop already fails 20 of 61 the same way, so this belongs to the Windows test epic #8, not here.

Linux after the merge: build 0 warnings, Web.Tests 2303/2303, Backup.Tests 64/64, Identity.Tests 77/77, csharpier clean.

**Windows acceptance on Lofnheim (Windows 10.0.26200), 2026-11-05**, with the exe built from this branch after taking in develop (`9c1d2b7b`, `bd3fd307`): | Check | Result | |---|---| | No certificate, service under `NT SERVICE\Ninlil`: new key file | DPAPI (`DpapiXmlDecryptor`), no plaintext `<masterKey>` | | Restart, then a TSIG-signed query using a stored TSIG secret (`dig -y` from Linux) | the server signs with the decrypted secret (TSIG NOERROR); a wrong secret gets BADSIG | | Certificate added to that DPAPI instance | starts Healthy, the DPAPI key still decrypts | | Fresh instance under `NT SERVICE\NinlilB` with a certificate | new key is certificate-protected (`EncryptedXmlDecryptor`), not DPAPI | | Wrong certificate password, console run | exit 78, message names `DataProtection:CertificatePath` and `CertificatePassword`, no password printed | | Wrong password or missing file, as a service | the start stops, but the SCM shows exit 0 and nothing reaches the event log. That applies to every startup refusal in `Program.cs`, so it is filed as #336 | | Certificate backup from NinlilB restored on Linux with the same certificate | restored; after a restart the restored key ring decrypts NinlilB's TSIG secret | | DPAPI backup restored on Linux / under another Windows account | 400 naming DPAPI and `DataProtection:CertificatePath`, nothing restored | | `KeyRing*`/`DataProtection` tests natively on Windows | 30/30 after fixing `KeyRing_OnWindowsWithDpapi_...`, which treated DPAPI's own `<value>` element as plaintext | `Ninlil.Dns.Server.Backup.Tests` on Windows: 23 of 64 fail at cleanup (`test.sqlite3` in use). Develop already fails 20 of 61 the same way, so this belongs to the Windows test epic #8, not here. Linux after the merge: build 0 warnings, Web.Tests 2303/2303, Backup.Tests 64/64, Identity.Tests 77/77, csharpier clean.
Refs #42

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T33E3pmQvqkTriyQUMNUTK
The Windows key-ring test treats only a bare master key as plaintext, since DPAPI keeps its ciphertext in a value element
Some checks failed
Build / Pack and Push NuGet Packages (common) (push) Has been skipped
Build / Build Binary (macos-arm64) (push) Has been skipped
Build / Build Binary (macos-amd64) (push) Has been skipped
Build / Pack and Push NuGet Packages (dns-server) (push) Has been skipped
Build / Build Binary (linux-arm64) (push) Has been skipped
Build / Smoke Test the Windows Binary (push) Has been skipped
Build / Build Binary (linux-amd64) (push) Has been skipped
Build / Build Binary (windows-amd64) (push) Has been skipped
Build / Build and Test Deb and RPM Packages (push) Has been skipped
Build / Build Arch Linux Packages (push) Has been skipped
Build / Build and Push Image (push) Has been skipped
Build / Publish Release (push) Has been skipped
Build / Pipeline (push) Failing after 0s
Build / Test .NET (pull_request) Failing after 8m22s
Build / Pack and Push NuGet Packages (dns-core) (pull_request) Has been skipped
Build / Stage Draft Release (pull_request) Has been skipped
Build / Pack and Push NuGet Packages (common) (pull_request) Has been skipped
Build / Pack and Push NuGet Packages (dns-server) (pull_request) Has been skipped
Build / Pack and Push NuGet Packages (dhcp) (pull_request) Has been skipped
Build / Build Binary (windows-amd64) (pull_request) Has been skipped
Build / Build Binary (macos-amd64) (pull_request) Has been skipped
Build / Build Binary (macos-arm64) (pull_request) Has been skipped
Build / Build Binary (linux-arm64) (pull_request) Has been skipped
Build / Build Binary (linux-amd64) (pull_request) Has been skipped
Build / Build Arch Linux Packages (pull_request) Has been skipped
Build / Build and Push Image (pull_request) Has been skipped
Build / Smoke Test the Windows Binary (pull_request) Has been skipped
Build / Publish Release (pull_request) Has been skipped
Build / Pipeline (pull_request) Failing after 0s
Build / Build and Test Deb and RPM Packages (pull_request) Has been skipped
bd3fd307b1
Refs #42

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T33E3pmQvqkTriyQUMNUTK
claude changed title from WIP: The Data Protection key ring is encrypted at rest with a certificate, or with DPAPI on Windows to The Data Protection key ring is encrypted at rest with a certificate, or with DPAPI on Windows 2026-11-05 22:09:06 -05:00
claude merged commit 09e874d044 into develop 2026-11-05 22:09:15 -05:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
lantea-one/ninlil!275
No description provided.