Files
Helper-Scripts/yubikey-setup/setup.sh
T
hendrik-hog 0868ce16aa Add YubiKey new-machine setup script
Self-contained setup.sh that imports the bundled public key, binds the YubiKey
card stubs, enables SSH via the auth key, and configures git signing — so a
fresh machine is provisioned without hunting for the public key. Idempotent and
verified end to end.
2026-07-24 19:28:15 +02:00

147 lines
6.7 KiB
Bash
Executable File

#!/bin/bash
#
# setup.sh — set up the YubiKey on a fresh machine (Arch / KDE / Plasma Wayland).
#
# Self-contained: the matching public key is bundled as pubkey.asc in this
# folder, so you don't have to hunt for it. The YubiKey holds the private keys
# (the OpenPGP app); this script imports the public key, binds the card's
# secret stubs, enables SSH via the YubiKey's auth key, and configures git to
# sign commits with the YubiKey — then verifies it all works.
#
# Idempotent: safe to re-run on a machine that's already set up.
#
# Requirements: gpg, ssh-keygen, git, and a YubiKey with an OpenPGP app
# inserted. Run as your normal user.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PUBKEY="${SCRIPT_DIR}/pubkey.asc"
# --- The key this setup is for. Change FINGERPRINT / NAME / EMAIL if you adopt
# this for a different key/identity. -------------------------------------
FINGERPRINT="4A227DDEFA7A37048609549863C6571F1095CC5C"
KEYID="63C6571F1095CC5C"
GIT_NAME="Hendrik Hogertz"
GIT_EMAIL="hendrik@hogertz.eu"
# --- gpg-agent SSH socket (machine-specific; resolved at runtime) -----------
SSH_SOCKET="$(gpgconf --list-dirs agent-ssh-socket 2>/dev/null || true)"
note() { printf '\033[1m→\033[0m %s\n' "$*"; }
ok() { printf ' \033[32m✓\033[0m %s\n' "$*"; }
fail() { printf ' \033[31m✗\033[0m %s\n' "$*" >&2; }
die() { fail "$*"; exit 1; }
# --- 0. Pre-flight -----------------------------------------------------------
note "Checking prerequisites…"
command -v gpg >/dev/null || die "gpg not found (install gnupg)"
command -v ssh-keygen >/dev/null || die "ssh-keygen not found (install openssh)"
command -v git >/dev/null || die "git not found"
[ -f "$PUBKEY" ] || die "pubkey.asc not found next to setup.sh ($PUBKEY)"
if ! gpg --card-status >/dev/null 2>&1; then
die "No OpenPGP YubiKey detected. Insert the YubiKey and re-run."
fi
ok "YubiKey detected"
# --- 1. Import the public key ------------------------------------------------
# The YubiKey holds the private keys; gpg needs the matching public key in the
# keyring to sign (without it, `gpg -u <key>` fails with "no secret key" even
# with the card present). Bundling pubkey.asc means you never have to find it.
note "Importing public key ($KEYID)…"
if gpg --import "$PUBKEY" 2>&1 | grep -q "importiert\|imported\|not changed\|ungeändert"; then
ok "Public key imported/present"
else
gpg --import "$PUBKEY" >/dev/null 2>&1 && ok "Public key imported" || fail "import reported an issue (continuing)"
fi
# --- 2. Bind the card's secret stubs ----------------------------------------
# Touching the card makes gpg-agent learn it and create the shadowed secret
# stubs that point at OPENPGP.1/2/3 on the card.
note "Binding YubiKey secret stubs…"
gpg --card-status >/dev/null 2>&1 || die "card became unreadable mid-setup"
gpg-connect-agent "scd learn --force" /bye >/dev/null 2>&1 || true
ok "Card learned"
# --- 3. Ultimate trust (so signatures verify as [G], not [U]) ---------------
note "Setting ultimate trust on $FINGERPRINT…"
echo "$FINGERPRINT:6:" | gpg --import-ownertrust >/dev/null 2>&1 || true
ok "Ownertrust set"
# --- 4. gpg-agent with SSH support ------------------------------------------
# Enables the YubiKey's auth key (OPENPGP.3) as an SSH key via the gpg-agent
# SSH socket. This is what makes `ssh`/`git push` work with the YubiKey.
note "Configuring gpg-agent for SSH…"
mkdir -p "$HOME/.gnupg"
cat > "$HOME/.gnupg/gpg-agent.conf" <<EOF
enable-ssh-support
default-cache-ttl 3600
max-cache-ttl 86400
EOF
gpgconf --kill gpg-agent 2>/dev/null || true
gpgconf --launch gpg-agent 2>/dev/null || true
ok "gpg-agent configured (enable-ssh-support)"
# --- 5. git identity + GPG commit signing ----------------------------------
note "Configuring git identity and signing…"
git config --global user.name "$GIT_NAME"
git config --global user.email "$GIT_EMAIL"
git config --global user.signingkey "$FINGERPRINT"
git config --global gpg.format openpgp
git config --global commit.gpgsign true
git config --global tag.gpgsign true
ok "git configured: $GIT_NAME <$GIT_EMAIL>, signing with $KEYID"
# --- 6. Allowed-signers for local SSH-signature verification ----------------
# Lets `git log --show-signature` verify SSH signatures locally. We add the
# YubiKey's auth key (read from the agent) so any SSH-signed commit verifies.
note "Setting up allowed-signers…"
if [ -n "$SSH_SOCKET" ] && SSH_AUTH_SOCK="$SSH_SOCKET" ssh-add -L >/dev/null 2>&1; then
mkdir -p "$HOME/.config/git"
AUTHKEY="$(SSH_AUTH_SOCK="$SSH_SOCKET" ssh-add -L 2>/dev/null | grep -iE 'ed25519|cardno' | head -1)"
if [ -n "$AUTHKEY" ]; then
echo "$GIT_EMAIL $AUTHKEY" > "$HOME/.config/git/allowed_signers"
git config --global gpg.ssh.allowedSignersFile "$HOME/.config/git/allowed_signers"
ok "allowed_signers written for $GIT_EMAIL"
else
fail "could not read auth key from agent (tap the YubiKey and re-run this step)"
fi
else
fail "gpg-agent SSH socket not available yet ($SSH_SOCKET); the YubiKey may need a tap"
fi
# --- 7. Verify: sign a test blob with the YubiKey ---------------------------
# The card's signing PIN is cached ("nicht zwingend"), so signing doesn't need
# a PIN prompt — but plain `gpg --detach-sign` still invokes pinentry, which
# fails with "cannot open /dev/tty" in a session without a controlling TTY.
# --batch --yes skips the pinentry prompt, so the verify step works whether or
# not there's a TTY. (If the card requires a PIN/tap, gpg will surface it.)
note "Verifying: signing a test blob with the YubiKey…"
TESTSIG="$(mktemp)"
if echo "yubikey setup verification" | gpg --batch --yes --detach-sign -u "$FINGERPRINT" -o "$TESTSIG" >/dev/null 2>&1; then
ok "GPG signing works with the YubiKey"; rm -f "$TESTSIG"
else
rm -f "$TESTSIG"
fail "GPG signing test failed — tap the YubiKey (if it requires touch) or run from a terminal"
note " retest: echo test | gpg --detach-sign -u $FINGERPRINT"
fi
# --- 8. Verify: SSH agent sees the YubiKey auth key ------------------------
if [ -n "$SSH_SOCKET" ]; then
if SSH_AUTH_SOCK="$SSH_SOCKET" ssh-add -l >/dev/null 2>&1; then
ok "SSH agent sees the YubiKey auth key"
else
note "SSH agent didn't list the key yet — run in a new shell with:"
note " export SSH_AUTH_SOCK=$SSH_SOCKET"
fi
fi
echo
note "Done. Next steps:"
echo " • Add the YubiKey's SSH public key to Gitea (Settings → SSH/GPG Keys)."
echo " Public key: $(SSH_AUTH_SOCK="$SSH_SOCKET" ssh-add -L 2>/dev/null | head -1 || echo '<run: ssh-add -L>')"
echo " • For this shell, export SSH_AUTH_SOCK so ssh/git push use the YubiKey:"
echo " export SSH_AUTH_SOCK=$SSH_SOCKET"
echo " • New logins pick it up automatically via gpg-agent (enable-ssh-support)."