Artifact 4acc9ee0cc6ced7ee827a86a61d744033cb88e34f1fdd5019c941161aa5ef4d2:
- File
test/pic/keysource.sh
— part of check-in
[bcb82b941e]
at
2026-11-23 11:55:36
on branch keysource
— Keep the credential store's key in the Keychain or a key file, not behind a passphrase
A new store gets a random 32-byte master key kept in the login Keychain (macOS, /usr/bin/security run as a child with the command on stdin) or the keyring (secret-tool), else ~/.pic/master.key at 0600. The source is recorded in picu_credential_key.source; a passphrase store keeps working as before. pic auth rekey keychain|file|passphrase reseals every sealed row in one transaction, and a typed passphrase is followed once by the offer to stop asking. --passphrase-fd and PIC_PASSPHRASE_COMMAND (setting passphraseCommand) give a passphrase with no terminal. pic remote opens a keychain or file store with no prompt. Every OS service stays in platform.c behind secret_key, secret_keep and secret_drop. (user: dmestas, size: 12216) [annotate] [blame] [check-ins using]
#!/bin/sh # Where the credential store's master key comes from, end to end with a private HOME and a fake # keystore (fake_keychain.sh as PIC_TEST_KEYCHAIN), so the real login Keychain is never touched: # a new store keeps a random key in the keychain and asks for nothing; a keychain that refuses # (locked, over ssh) falls back to ~/.pic/master.key at 0600; a store under a HOME that is not the # account's own never reaches the real keystore; the key file must stay private; a copied database # alone opens nothing; pic auth rekey moves the key keychain -> file -> passphrase -> keychain with # every key still opening and the old source dropped; --passphrase-fd and PIC_PASSPHRASE_COMMAND # give a passphrase with no terminal; and on a terminal a typed passphrase is followed once by the # offer to stop asking, a no remembered and a yes moving the key. No key or passphrase is in any # argv the keystore saw. Both builds run it whole: nothing here needs PIC_TEST_PASSPHRASE. set -eu PIC=${PIC:?set PIC to the pic binary} SERVER=${SERVER:?set SERVER to the fake_server binary} DRIVE=${DRIVE:?set DRIVE to the pty_drive binary} SQLITE3=${SQLITE3:-sqlite3} S=$(cd "$(dirname "$0")/streams" && pwd) FAKE_SRC=$(cd "$(dirname "$0")" && pwd)/fake_keychain.sh T=$(mktemp -d "${TMPDIR:-/tmp}/pic-keysrc.XXXXXX") T=$(cd "$T" && pwd -P) spid= cleanup() { [ -n "$spid" ] && kill "$spid" 2>/dev/null || :; rm -rf -- "$T"; } trap cleanup EXIT HUP INT TERM fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; } mkdir "$T/ws" "$T/log" "$T/out" "$T/bin" for v in $(env | sed -nE 's/^([A-Za-z0-9_]*_(API_KEY|TOKEN))=.*/\1/p'); do unset "$v"; done unset FAKE_API_KEY PIC_TEST_PASSPHRASE PIC_PASSPHRASE_COMMAND PIC_TEST_KEYCHAIN PIC_KEY_FD export PIC_MODEL_CATALOG=offline PIC_PI_EXPORT=0 PIC_MODELS_FILE="$T/models.json" PIC_NO_NATIVE_CLIPBOARD=1 cp "$FAKE_SRC" "$T/bin/keychain" chmod 755 "$T/bin/keychain" FAKE="$T/bin/keychain" KEY="pic-keysrc-sentinel-$$-7c2d" PASS="pic-keysrc-passphrase-$$" "$SERVER" "$T/port" "$T/log" 120 "sse:$S/text.sse" & spid=$! i=0 while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server did not start"; sleep 0.05; done printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$FAKE_API_KEY","models":[{"id":"m","contextWindow":200000}]}}}' \ "$(cat "$T/port")" > "$T/models.json" notty() { perl -MPOSIX=setsid -e 'setsid() or die "setsid"; exec @ARGV or die "exec"' "$@"; } pic() { # NAME ARGS...: pic with no terminal, in the current HOME; output in $T/out/NAME.{out,err} name=$1 shift notty "$PIC" --db "$T/j" --workspace "$T/ws" --model fake/m "$@" > "$T/out/$name.out" 2> "$T/out/$name.err" } drive() { # NAME ARGS...: pic on a pseudo-terminal, the script on stdin name=$1 shift "$DRIVE" "$T/out/$name.tx" 100 24 "$PIC" --db "$T/j" --workspace "$T/ws" --model fake/m "$@" } screen() { perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$1" | grep -v '^\s*$' | tail -8; } requests() { ls "$T/log" | wc -l | tr -d ' '; } sent_key() { sed -n 's/^x-api-key: //p' "$T/log/$1.req" | tr -d '\r'; } source_of() { "$SQLITE3" "$HOME/.pic/pic.sqlite" "SELECT source || ' ' || kdf FROM picu_credential_key"; } mode_of() { if stat -f %Lp "$1" > /dev/null 2>&1; then stat -f %Lp "$1"; else stat -c %a "$1"; fi; } home() { # NAME: a fresh private HOME, and a fresh fake keychain beside it export HOME="$T/$1" FAKE_KEYCHAIN_DIR="$T/$1-keychain" mkdir -p "$HOME" "$FAKE_KEYCHAIN_DIR" } ask() { # NAME TEXT: a request that must send $KEY before=$(requests) pic "$1" -p "$2" < /dev/null || fail "$1: $(cat "$T/out/$1.err")" [ "$(requests)" = $((before + 1)) ] || fail "$1: $(requests) requests" [ "$(sent_key "$(requests)")" = "$KEY" ] || fail "$1: the server got another key" } noask() { # NAME PATTERN ARGS...: a request that must fail with PATTERN and reach no server name=$1 pat=$2 shift 2 before=$(requests) pic "$name" -p 'no' "$@" < /dev/null && fail "$name: the request ran" grep -q -- "$pat" "$T/out/$name.err" || fail "$name: $(cat "$T/out/$name.err")" [ "$(requests)" = "$before" ] || fail "$name: a request reached the server" } # A HOME that is not the account's own, and no stand-in: the key file, never the real keystore. home plain printf '%s\n' "$KEY" | notty "$PIC" login fake > "$T/out/plain.out" 2> "$T/out/plain.err" || fail "plain: $(cat "$T/out/plain.err")" ! grep -qi passphrase "$T/out/plain.err" || fail "plain: a passphrase was asked: $(cat "$T/out/plain.err")" [ "$(source_of)" = "file none" ] || fail "plain: the source is $(source_of)" [ -f "$HOME/.pic/master.key" ] || fail "plain: no key file" [ "$(mode_of "$HOME/.pic/master.key")" = 600 ] || fail "plain: the key file is $(mode_of "$HOME/.pic/master.key")" ! ls "$HOME/.pic" | grep -q 'master.key.*tmp' || fail "plain: a temporary was left: $(ls "$HOME/.pic")" ask plain1 'from the key file' printf 'PASS: under a HOME that is not your own, a new store keeps its key in ~/.pic/master.key at 0600, asking nothing\n' # A copied database on its own opens nothing. mkdir -p "$T/copy/.pic" cp "$HOME/.pic/pic.sqlite" "$T/copy/.pic/" (HOME="$T/copy" && export HOME && noask copy 'master.key') printf 'PASS: a copied database without its key opens nothing\n' # The key file must stay private. chmod 644 "$HOME/.pic/master.key" noask loose 'can be read by others: chmod 600 it' chmod 600 "$HOME/.pic/master.key" ask plain2 'private again' printf 'PASS: a key file others can read is refused\n' # The keychain: a new store's key goes there, through stdin, and nothing else is written. home kc export PIC_TEST_KEYCHAIN="$FAKE" printf '%s\n' "$KEY" | notty "$PIC" login fake > "$T/out/kc.out" 2> "$T/out/kc.err" || fail "kc: $(cat "$T/out/kc.err")" [ "$(source_of)" = "keychain none" ] || fail "kc: the source is $(source_of)" [ ! -e "$HOME/.pic/master.key" ] || fail "kc: a key file was written too" [ "$(ls "$FAKE_KEYCHAIN_DIR/items" | wc -l | tr -d ' ')" = 1 ] || fail "kc: $(ls "$FAKE_KEYCHAIN_DIR/items")" item=$(cat "$FAKE_KEYCHAIN_DIR"/items/*) [ "${#item}" = 64 ] || fail "kc: the item is not a 32-byte key in hex" ! grep -qF -- "$item" "$FAKE_KEYCHAIN_DIR/argv.log" || fail "kc: the key was in an argv" grep -q "^-i$" "$FAKE_KEYCHAIN_DIR/argv.log" || grep -q "^store " "$FAKE_KEYCHAIN_DIR/argv.log" || fail "kc: $(cat "$FAKE_KEYCHAIN_DIR/argv.log")" ask kc1 'from the keychain' grep -q "^find-generic-password -s pic credential store -a $HOME/.pic/pic.sqlite -w$" "$FAKE_KEYCHAIN_DIR/argv.log" || grep -q "^lookup service pic credential store account $HOME/.pic/pic.sqlite$" "$FAKE_KEYCHAIN_DIR/argv.log" || fail "kc: $(cat "$FAKE_KEYCHAIN_DIR/argv.log")" printf 'PASS: a new store keeps a random key in the keychain, never in argv, and opens with no prompt\n' # A locked keychain: refused at unlock, cleanly; a new store falls back to the key file. touch "$FAKE_KEYCHAIN_DIR/locked" noask kclocked 'keychain' rm "$FAKE_KEYCHAIN_DIR/locked" home kcfall touch "$FAKE_KEYCHAIN_DIR/locked" printf '%s\n' "$KEY" | notty "$PIC" login fake > "$T/out/fall.out" 2> "$T/out/fall.err" || fail "fall: $(cat "$T/out/fall.err")" [ "$(source_of)" = "file none" ] || fail "fall: the source is $(source_of)" [ "$(mode_of "$HOME/.pic/master.key")" = 600 ] || fail "fall: the key file is $(mode_of "$HOME/.pic/master.key")" ask fall1 'from the fallback file' rm "$FAKE_KEYCHAIN_DIR/locked" printf 'PASS: a locked keychain is refused cleanly, and a new store then uses the key file\n' # Rekey: file -> keychain -> passphrase (from a descriptor) -> file -> keychain, every key opening. pic rk1 auth rekey keychain < /dev/null || fail "rk1: $(cat "$T/out/rk1.err")" grep -q 'now in your \(Keychain\|keyring\)' "$T/out/rk1.out" || fail "rk1: $(cat "$T/out/rk1.out")" [ "$(source_of)" = "keychain none" ] || fail "rk1: the source is $(source_of)" [ ! -e "$HOME/.pic/master.key" ] || fail "rk1: the old key file was kept" ask rk1ask 'after the move to the keychain' pic rk1again auth rekey keychain < /dev/null || fail "rk1again: $(cat "$T/out/rk1again.err")" grep -q 'already in your' "$T/out/rk1again.out" || fail "rk1again: $(cat "$T/out/rk1again.out")" printf '%s\n' "$PASS" > "$T/pass" pic rk2 --passphrase-fd 3 auth rekey passphrase 3< "$T/pass" < /dev/null || fail "rk2: $(cat "$T/out/rk2.err")" [ "$(source_of)" = "passphrase pbkdf2-sha256" ] || fail "rk2: the source is $(source_of)" [ "$(ls "$FAKE_KEYCHAIN_DIR/items" | wc -l | tr -d ' ')" = 0 ] || fail "rk2: the keychain item was kept" noask rk2none 'no terminal' before=$(requests) pic rk2fd -p 'by descriptor' --passphrase-fd 3 3< "$T/pass" < /dev/null || fail "rk2fd: $(cat "$T/out/rk2fd.err")" [ "$(sent_key "$(requests)")" = "$KEY" ] && [ "$(requests)" = $((before + 1)) ] || fail "rk2fd: no request with the key" printf 'not-%s\n' "$PASS" > "$T/wrong" noask rk2wrong 'wrong passphrase' --passphrase-fd 3 3< "$T/wrong" (PIC_PASSPHRASE_COMMAND="cat '$T/pass'" && export PIC_PASSPHRASE_COMMAND && ask rk2cmd 'by command') (PIC_PASSPHRASE_COMMAND="exit 3" && export PIC_PASSPHRASE_COMMAND && noask rk2cmdfail 'passphrase command failed') pic rk3 --passphrase-fd 3 auth rekey file 3< "$T/pass" < /dev/null || fail "rk3: $(cat "$T/out/rk3.err")" [ "$(source_of)" = "file none" ] || fail "rk3: the source is $(source_of)" [ "$(mode_of "$HOME/.pic/master.key")" = 600 ] || fail "rk3: the key file is $(mode_of "$HOME/.pic/master.key")" ask rk3ask 'after the move to the file' pic rk4 auth rekey keychain < /dev/null || fail "rk4: $(cat "$T/out/rk4.err")" [ ! -e "$HOME/.pic/master.key" ] || fail "rk4: the old key file was kept" ask rk4ask 'back in the keychain' for f in "$FAKE_KEYCHAIN_DIR/argv.log" "$HOME/.pic/pic.sqlite"*; do ! grep -aqF -- "$KEY" "$f" || fail "leak: the key is in $f" ! grep -aqF -- "$PASS" "$f" || fail "leak: the passphrase is in $f" done printf 'PASS: rekey moves the key between keychain, file and passphrase, every key still opening\n' printf 'PASS: --passphrase-fd and PIC_PASSPHRASE_COMMAND give the passphrase with no terminal\n' # On a terminal: a new passphrase store (rekey on a new store makes it), the key, then a typed # passphrase is followed once by the offer to stop asking; a no is remembered. home offer drive mkpass auth rekey passphrase <<EOF || fail "mkpass: $(screen "$T/out/mkpass.tx")" expect New passphrase for pic's credential store: send $PASS\r expect Same passphrase again: send $PASS\r expect now comes from a passphrase EOF [ "$(source_of)" = "passphrase pbkdf2-sha256" ] || fail "mkpass: the source is $(source_of)" printf '%s\n' "$KEY" | pic offerlogin --passphrase-fd 3 login fake 3< "$T/pass" || fail "offerlogin: $(cat "$T/out/offerlogin.err")" ! grep -q 'Stop asking' "$T/out/offerlogin.err" || fail "offerlogin: offered for a passphrase that was not typed" drive offer1 -p 'offered' <<EOF || fail "offer1: $(screen "$T/out/offer1.tx")" expect Passphrase for pic's credential store: send $PASS\r expect Stop asking for this passphrase? pic can keep the key in your send n\r expect Hello, "world" EOF drive offer2 -p 'not again' <<EOF || fail "offer2: $(screen "$T/out/offer2.tx")" expect Passphrase for pic's credential store: send $PASS\r expect Hello, "world" EOF ! grep -aq 'Stop asking' "$T/out/offer2.tx" || fail "offer2: offered again after a no" "$SQLITE3" "$HOME/.pic/pic.sqlite" "DELETE FROM picu_setting WHERE key = 'cred.offer'" drive offer3 -p 'yes please' <<EOF || fail "offer3: $(screen "$T/out/offer3.tx")" expect Passphrase for pic's credential store: send $PASS\r expect Stop asking for this passphrase? send y\r expect Hello, "world" EOF grep -aq 'now in your' "$T/out/offer3.tx" || fail "offer3: $(screen "$T/out/offer3.tx")" [ "$(source_of)" = "keychain none" ] || fail "offer3: the source is $(source_of)" ask offer4 'no prompt now' printf 'PASS: a typed passphrase is followed once by the offer; a no is remembered, a yes moves the key\n' # Remote control opens a keychain store with no one at a terminal. pic devs remote devices < /dev/null || fail "devs: $(cat "$T/out/devs.err")" grep -q 'No phone is paired' "$T/out/devs.out" || fail "devs: $(cat "$T/out/devs.out")" printf 'PASS: pic remote opens a store whose key is in the keychain with no prompt\n'