Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(spaces-macos): install-local.sh says a locked Keychain is locked, not new (CUA-2213) (#5883)

cua-agents[bot]fd93182fcfee
diff --git a/apps/cua-spaces-macos/scripts/install-local.sh b/apps/cua-spaces-macos/scripts/install-local.sh
index 8b58bdaf79..72e6ef8564 100755
--- a/apps/cua-spaces-macos/scripts/install-local.sh
+++ b/apps/cua-spaces-macos/scripts/install-local.sh
@@ -460,6 +460,38 @@ identity_trusted() {
security find-identity -v -p codesigning "$2" 2>/dev/null | grep -qF "$1"
}
+# Why the stable local certificate cannot sign, when the script can tell a
+# locked Keychain from a new one (a new one only needs the one-time setup; a
+# locked one needs unlocking first, and the setup's commands would fail on it):
+# locked codesign needed the Keychain's password. An SSH session, or
+# a VM that logged in on its own, has the Keychain locked and
+# cannot be asked for it (errSecInternalComponent).
+# no-certificate the certificate could not be created in the Keychain, which
+# a locked Keychain refuses.
+# (empty) a new Keychain: the certificate is there but not trusted.
+SIGNING_PROBLEM=""
+# What codesign said on the last signing_probe.
+SIGNING_PROBE_ERROR=""
+
+# Whether the last signing_probe failed because the Keychain is locked.
+signing_probe_locked() {
+ case "$SIGNING_PROBE_ERROR" in
+ *errSecInternalComponent*|*"User interaction is not allowed"*) return 0 ;;
+ esac
+ return 1
+}
+
+# Whether the Keychain exists but holds no stable local certificate although the
+# script just tried to create one (the shared helper swallows why it could not).
+signing_certificate_missing() {
+ local kc
+ [ -z "${CUA_DRIVER_LOCAL_SIGNING_IDENTITY:-}" ] || return 1
+ command -v openssl >/dev/null 2>&1 || return 1
+ kc="$(local_signing_keychain)"
+ [ -f "$kc" ] || return 1
+ ! security find-certificate -c "$CUA_LOCAL_SIGN_CN" "$kc" >/dev/null 2>&1
+}
+
# The one-time setup, as the commands to type, for a Mac where the script
# could not (or was not allowed to) do it.
SIGNING_HELP_SHOWN=0
@@ -469,10 +501,30 @@ print_signing_bootstrap() {
[ "$SIGNING_HELP_SHOWN" = 0 ] || return 0
SIGNING_HELP_SHOWN=1
kc="$(local_signing_keychain)"
- echo "Cua Spaces Local is signed with the stable local certificate \"$CUA_LOCAL_SIGN_CN\"" >&2
- echo "(the one Cua Driver's local install uses), so its Screen Recording and" >&2
- echo "Accessibility permissions survive rebuilds. On a new Keychain that needs a" >&2
- echo "one-time setup, in Terminal on this Mac:" >&2
+ case "$SIGNING_PROBLEM" in
+ no-certificate)
+ echo "The stable local certificate \"$CUA_LOCAL_SIGN_CN\" is not in the Keychain $kc:" >&2
+ echo "it could not be created there. A locked Keychain refuses it, as in an SSH session" >&2
+ echo "or a VM that logged in on its own. Unlock it (it asks for the Keychain's password)" >&2
+ echo "and run this script again:" >&2
+ echo " security unlock-keychain \"$kc\"" >&2
+ return 0
+ ;;
+ locked)
+ echo "The Keychain $kc is locked, so codesign cannot use the stable local certificate's key." >&2
+ echo "That is usual in an SSH session or a VM that logged in on its own. Unlock it" >&2
+ echo "(it asks for the Keychain's password) and run this script again:" >&2
+ echo " security unlock-keychain \"$kc\"" >&2
+ echo "If codesign still cannot sign after that, the certificate also needs the" >&2
+ echo "one-time setup, in Terminal on this Mac:" >&2
+ ;;
+ *)
+ echo "Cua Spaces Local is signed with the stable local certificate \"$CUA_LOCAL_SIGN_CN\"" >&2
+ echo "(the one Cua Driver's local install uses), so its Screen Recording and" >&2
+ echo "Accessibility permissions survive rebuilds. On a new Keychain that needs a" >&2
+ echo "one-time setup, in Terminal on this Mac:" >&2
+ ;;
+ esac
echo " security find-certificate -c \"$CUA_LOCAL_SIGN_CN\" -p \"$kc\" > $pem" >&2
echo " security add-trusted-cert -r trustRoot -p codeSign -k \"$kc\" $pem" >&2
echo " (macOS asks for your password once. In an SSH session or a VM without a" >&2
@@ -491,10 +543,11 @@ print_signing_bootstrap() {
# certificate-backed is checked by sign_staged_app, as before.)
signing_probe() {
local identity="$1" macho="$2" dir probe status=0
+ SIGNING_PROBE_ERROR=""
dir="$(mktemp -d "${TMPDIR:-/tmp}/cua-local-sign-probe.XXXXXX")" || return 1
probe="$dir/probe"
if cp "$macho" "$probe"; then
- codesign_bounded 20 --force --sign "$identity" "$probe" >/dev/null 2>&1 || status=$?
+ SIGNING_PROBE_ERROR="$(codesign_bounded 20 --force --sign "$identity" "$probe" 2>&1 >/dev/null)" || status=$?
else
status=1
fi
@@ -536,6 +589,27 @@ make_signing_identity_usable() {
return 1
fi
kc="$(local_signing_keychain)"
+ if signing_probe_locked; then
+ SIGNING_PROBLEM=locked
+ warn "codesign cannot use the stable local certificate: its Keychain ($kc) is locked. That is usual in an SSH session or a VM that logged in on its own, where nothing can ask for the password."
+ if [ -t 0 ] && [ -t 1 ]; then
+ printf 'Unlock it now? It asks for the Keychain password. [Y/n] '
+ read -r answer || answer=n
+ case "$answer" in ""|y|Y|yes|YES|Yes) answer=y ;; *) answer=n ;; esac
+ if [ "$answer" = y ] && security unlock-keychain "$kc"; then
+ if signing_probe "$identity" "$macho"; then
+ ok "codesign can use the stable local certificate"
+ return 0
+ fi
+ fi
+ fi
+ if signing_probe_locked; then
+ print_signing_bootstrap
+ return 1
+ fi
+ # Unlocked now; what is left is a new Keychain's setup.
+ SIGNING_PROBLEM=""
+ fi
warn "codesign cannot use the stable local certificate yet (a new Keychain: it is not trusted for code signing, or codesign may not use its key)."
answer=n
@@ -617,6 +691,9 @@ sign_staged_app() {
sign_id="$(ensure_local_signing_identity)"
if [ "$sign_id" != "-" ]; then
make_signing_identity_usable "$sign_id" "$app_stage" || true
+ elif signing_certificate_missing; then
+ SIGNING_PROBLEM=no-certificate
+ warn "could not create the stable local certificate in $(local_signing_keychain): the Keychain is probably locked."
fi
if [ "$sign_id" != "-" ]; then
diff --git a/apps/cua-spaces-macos/scripts/tests/install-local-test.sh b/apps/cua-spaces-macos/scripts/tests/install-local-test.sh
index 4945cc4492..7e27de2901 100755
--- a/apps/cua-spaces-macos/scripts/tests/install-local-test.sh
+++ b/apps/cua-spaces-macos/scripts/tests/install-local-test.sh
@@ -20,6 +20,8 @@
# checked first, in one message;
# - on a fresh Keychain (a certificate codesign cannot use yet) it says so and
# sets it up only when asked (--setup-signing), or prints the exact steps;
+# - on a locked Keychain (SSH session, VM) it says the Keychain is locked and
+# how to unlock it, not that it is new, and never sets up a locked one;
# - uninstall-local.sh removes the Local pieces and only those, and first
# takes this Mac off the relay when Local is signed in, as Local and with
# nothing of the caller's Stable environment;
@@ -285,12 +287,16 @@ if [ "${SHIM_FRESH_KEYCHAIN:-0}" = 1 ]; then
add-trusted-cert)
if [ "${SHIM_TRUST_FAILS:-0}" = 1 ]; then echo "SecTrustSettingsSetTrustSettings: no user interaction was possible" >&2; exit 1; fi
: > "$SHIM_STATE/trusted"; exit 0 ;;
+ unlock-keychain)
+ : > "$SHIM_STATE/unlocked"; exit 0 ;;
set-key-partition-list)
# SHIM_PARTITION_FAILS=1: no key matched the filter (the tool says so and changes nothing).
if [ "${SHIM_PARTITION_FAILS:-0}" = 1 ]; then echo "security: SecItemCopyMatching: The specified item could not be found in the keychain." >&2; exit 1; fi
: > "$SHIM_STATE/partition"; exit 0 ;;
esac
fi
+# No certificate in the Keychain (what the real tool says: not found).
+if [ "$1" = find-certificate ]; then exit 44; fi
exit 0'
# codesign: the real one, except that it can be made to fail verifying one
# path, to stall (exit like a killed-by-alarm process, no output) on its first
@@ -305,6 +311,11 @@ write_shim codesign 'if [ "${SHIM_FRESH_KEYCHAIN:-0}" = 1 ]; then
prev="$a"
done
if [ "$fake" = 1 ]; then
+ # A locked Keychain (SHIM_LOCKED_KEYCHAIN=1) in a session that cannot ask
+ # for its password, until `security unlock-keychain` ran.
+ if [ "${SHIM_LOCKED_KEYCHAIN:-0}" = 1 ] && [ ! -e "$SHIM_STATE/unlocked" ]; then
+ echo "probe: errSecInternalComponent" >&2; exit 1
+ fi
if [ ! -e "$SHIM_STATE/trusted" ] || { [ "${SHIM_NEED_PARTITION:-0}" = 1 ] && [ ! -e "$SHIM_STATE/partition" ]; }; then
echo "no identity found" >&2; exit 1
fi
@@ -1113,6 +1124,49 @@ fresh_fixture
tty_install
check "with the key already usable after the trust it asks for no password and calls no set-key-partition-list" '! grep >/dev/null "set-key-partition-list" "$SHIM_LOG" && ! printf "%s" "$OUT" | grep -F >/dev/null "Password of the Keychain"'
+# A locked Keychain (an SSH session, or a VM that logged in on its own): codesign
+# fails with errSecInternalComponent, because nothing can ask for the password.
+# The message says the Keychain is locked and how to unlock it, and not that it
+# is new; the new-Keychain setup is not attempted on a locked one.
+echo "== a locked Keychain =="
+tty_unlock_install() { run env PTY_ANSWER=y PTY_PROMPT="Unlock it now" /usr/bin/python3 -I "$WORK/pty-run.py" env SHIM_FRESH_KEYCHAIN=1 "$@" "$FAKE_SCRIPTS/install-local.sh" --no-launch --setup-signing; }
+UNLOCK_CMD='security unlock-keychain "$FAKE_KEYCHAIN"'
+
+fresh_fixture
+FRESH_ARGS="--setup-signing"
+fresh_install SHIM_LOCKED_KEYCHAIN=1
+check "it says the Keychain is locked and prints the unlock command" \
+ 'printf "%s" "$OUT" | grep -F >/dev/null "its Keychain ($FAKE_KEYCHAIN) is locked" && printf "%s" "$OUT" | grep -F >/dev/null "security unlock-keychain \"$FAKE_KEYCHAIN\""'
+check "it does not call a locked Keychain new, and prints the one-time setup once, as the step after unlocking" \
+ '! printf "%s" "$OUT" | grep -F >/dev/null "a new Keychain: it is not trusted" && printf "%s" "$OUT" | grep -F >/dev/null "If codesign still cannot sign after that" && [ "$(printf "%s\n" "$OUT" | grep -c "one-time setup")" = 1 ]'
+check "--setup-signing without a terminal does not set up a locked Keychain (no trust, no search list, no key access)" \
+ '! security_calls | grep >/dev/null -e "add-trusted-cert" -e "list-keychains .*-s " -e "set-key-partition-list"'
+check "the install is ad hoc, reported, as before (status $RC)" '[ "$RC" = 0 ] && printf "%s" "$OUT" | grep -F >/dev/null "signed ad hoc"'
+
+fresh_fixture
+FRESH_ARGS="--require-stable-signing"
+fresh_install SHIM_LOCKED_KEYCHAIN=1
+check "--require-stable-signing on a locked Keychain stops, installs nothing, and says it is locked" \
+ '[ "$RC" != 0 ] && [ ! -e "$LOCAL_APP" ] && printf "%s" "$OUT" | grep -F >/dev/null "is locked" && printf "%s" "$OUT" | grep -F >/dev/null "stable signing is required"'
+
+fresh_fixture
+tty_unlock_install SHIM_LOCKED_KEYCHAIN=1
+check "at a terminal it offers to unlock, runs security unlock-keychain, and goes on to the setup that a new Keychain still needs" \
+ 'grep -F >/dev/null "security unlock-keychain $FAKE_KEYCHAIN" "$SHIM_LOG" && grep -n -e "^security unlock-keychain" -e "^security add-trusted-cert" "$SHIM_LOG" | sed -n 1p | grep >/dev/null unlock-keychain'
+check "and then signs with the certificate (status $RC)" '[ "$RC" = 0 ] && printf "%s" "$OUT" | grep -F >/dev/null "codesign can use the stable local certificate"'
+
+# The certificate could not be created at all: the Keychain exists, holds none,
+# and (the usual reason) refused the import.
+fresh_fixture
+install --require-stable-signing
+check "no certificate in the Keychain: it says it could not create one and that the Keychain is probably locked, with the unlock command" \
+ '[ "$RC" != 0 ] && [ ! -e "$LOCAL_APP" ] && printf "%s" "$OUT" | grep -F >/dev/null "could not create the stable local certificate in $FAKE_KEYCHAIN: the Keychain is probably locked" && printf "%s" "$OUT" | grep -F >/dev/null "security unlock-keychain \"$FAKE_KEYCHAIN\""'
+check "and it does not print steps that need the certificate (find-certificate, add-trusted-cert)" '! printf "%s" "$OUT" | grep -F >/dev/null -e "security find-certificate" -e "add-trusted-cert"'
+fresh_fixture
+rm -f "$FAKE_KEYCHAIN"
+install --require-stable-signing
+check "no Keychain at all is not called locked" '! printf "%s" "$OUT" | grep -F >/dev/null -e "locked" -e "could not create the stable local certificate"'
+
# The real `security`, on a throwaway Keychain (not in the search list, no trust
# change): the function the script runs matches the Local certificate's key and
# no other signing key, and changes nothing when there is no such key.