Skip to content

Add keygen --accessibility after-first-unlock - #27

Open
piersdd wants to merge 1 commit into
remko:mainfrom
piersdd:after-first-unlock
Open

piersdd wants to merge 1 commit into
remko:mainfrom
piersdd:after-first-unlock

Conversation

@piersdd

@piersdd piersdd commented Nov 23, 2026 •

Copy link
Copy Markdown

Add keygen --accessibility after-first-unlock, so a key with access control none keeps working while the screen is locked. The default stays when-unlocked, and existing keys and identity files do not change.

Summary

 generateKey(accessControl, recipientType, now, pq)
+generateKey(accessControl, accessibility = .whenUnlocked, recipientType, now, pq)
   flags from accessControl
-  SecAccessControlCreateWithFlags(kSecAttrAccessibleWhenUnlockedThisDeviceOnly, flags)
+  protection = accessibility == .afterFirstUnlock
+    ? kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
+    : kSecAttrAccessibleWhenUnlockedThisDeviceOnly
+  SecAccessControlCreateWithFlags(protection, flags)
   identity file header
     # access control: none
+    # accessibility: after first unlock      (only when not the default)
  • CLI: new --accessibility when-unlocked|after-first-unlock, parsed like --access-control. The help text and README are updated.
  • The class is fixed when the key is made, so --accessibility is a keygen option only.
  • Tests: CLITests parses the new value and refuses an unknown one. PluginTests.testGenerate_AfterFirstUnlock checks the header line. The existing header tests are unchanged, which shows the default output keeps its exact shape.

Why: a background job (launchd agent, sops decrypt at service start) on an unattended Mac cannot use an enclave key today while the screen is locked, even with --access-control none.

Evidence

  • Before (v0.2.1, macOS 26, Apple Silicon, --access-control none, screen locked with the user logged in; a decrypt every 5 s through age -d and through sops decrypt):

    Screen age -d sops decrypt
    unlocked 31 ok 31 ok
    locked, first ~10 s 2 ok 2 ok
    locked, after that 22 refused 22 refused

    The refusal: age: se plugin: The operation couldn’t be completed. (OSStatus error -25308.) (errSecInteractionNotAllowed). keygen is also refused while locked, with the same error.

  • After:

    • swift test on macOS 26 (Swift 6.4.2): 110 tests, 0 failures. That includes testGenerate_AfterFirstUnlock, testParse_Keygen_AfterFirstUnlock and testParse_KeyGen_InvalidAccessibility.

    • On an unlocked Apple Silicon Mac, keygen --access-control none --accessibility after-first-unlock makes a key, and an age encrypt and decrypt round trip with it succeeds. The Secure Enclave accepts the class.

    • Locked-screen probe with that key (a second Apple Silicon Mac, macOS 26, same probe, same versions plus this branch):

      Screen age -d sops decrypt
      unlocked 9 ok 9 ok
      locked, 12:57:01 to 12:58:58 24 ok, 0 refused 24 ok, 0 refused

      The locked samples run for about 2 minutes, well past the ~10 s grace window seen with the default class.

    • Not yet run: a reboot without an unlock, to check that the key is still refused before the first unlock (the after-first-unlock contract).

Merge Danger

Door: two-way

A new opt-in flag with the old default. Reverting it removes the flag. Keys made with it stay usable while locked until the user deletes them, because the class lives in the key.

Blast Radius: opt-in

Default keygen output and every existing key are unchanged. A user who opts in trades screen-lock protection for unattended use, so a process that runs as that user can use the key while the screen is locked. The help text names that trade-off.

🤖 Generated with Claude Code

Keys are created with kSecAttrAccessibleWhenUnlockedThisDeviceOnly, so
even a key with --access-control none is refused while the screen is
locked (OSStatus -25308). That blocks unattended jobs, such as a launchd
agent that decrypts at start.

Add --accessibility when-unlocked|after-first-unlock. The default keeps
the current class and the current identity file output. With
after-first-unlock the key uses
kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, and the identity file
gets an "# accessibility: after first unlock" header line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@piersdd
piersdd marked this pull request as ready for review November 23, 2026 01:59

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant