# Where the credential store's key comes from

Status: built 2026-11-23 (owner's choice: options A and C, with E's sources that need no terminal).

Tier: core (`cred.c`: the recorded source, `pic_cred_quiet`, `pic_cred_rekey`, the offer) over the
platform seam (`platform.h`: `secret_key`, `secret_keep`, `secret_drop`); the host's sources live in
`platform.c` alone. Clock: os (the `security` and `secret-tool` command lines, `tmutil`). Record:
`picu_credential_key.source` (`keychain`, `file` or `passphrase`; a raw key's `kdf` is `none`), the
setting `cred.offer`. Default: a new store's random key in the login Keychain (or keyring), else
`~/.pic/master.key`; a passphrase only when chosen.

## The threat model

A copied `pic.sqlite`, a backup or an exported journal on its own reveals nothing. Malware or a
person acting in the user's unlocked session can read the key, as with the keys other tools keep.
So a passphrase stops being the default: it guarded against the second case at the cost of a prompt
in every process, and against the first case no better than a key kept apart from the database.

## Decisions

- The sealing (ChaCha20-Poly1305 under a 32-byte master key, the verifier) is unchanged; only where
  the master key comes from changes, with no stretching for a raw key.
- The keystore runs as a child process (`/usr/bin/security -i`, the command on its stdin;
  `secret-tool store`, the secret on its stdin), so the key is never in argv and pic links no
  framework. A keep is read back before it counts, since a locked Keychain can seem to take one.
- The keystore is used only for the store under the account's own home directory; any other `HOME`
  gets the key file. Tests run under a private `HOME`, so a test that forgets the stand-in
  (`PIC_TEST_KEYCHAIN`) still cannot reach the real Keychain.
- A rekey reseals every sealed row (credentials, remote control's keys and devices) in one
  transaction; the old source is cleared only after the commit, and a new source that the commit
  did not take is cleared again. A row that did not open under the old key is left as it was.
- Every target stays fully featured. `cred.c` treats every source but `passphrase` as an opaque
  name the platform gave, so a platform adds a key source in its own `secret_key`, `secret_keep`
  and `secret_drop` without touching `cred.c`; `secret_keep(NULL)` is its choice for a new store
  and `secret_keep("")` its choice of a source that keeps a key (the offer's yes). On a host the
  key file is the portable source, working on any POSIX system; the Keychain or Secret Service is
  an improvement taken when present.
- A platform with no filesystem keeps the "file" key as a blob in a SQLite key database of its
  own beside `pic.sqlite`, through its VFS, attached only to read the key, so a copied
  `pic.sqlite` or backup still reveals nothing. Only a platform that has a single database and
  nothing else may put the key in `pic.sqlite` itself, and then a copied database opens: that
  platform loses the copied-database protection, and its documentation must say so.
- An OS-free build that holds a fixed boot key has `secret_keep` name that source and keep
  nothing; `cred.c` then fetches it.
- `pic remote` opens a keychain or file store with no prompt, which `pic remote install` (a login
  agent) will rely on.
