diff --git a/gpg-switch-yubikey/README.md b/gpg-switch-yubikey/README.md deleted file mode 100644 index fa9561d..0000000 --- a/gpg-switch-yubikey/README.md +++ /dev/null @@ -1,26 +0,0 @@ -# gpg-switch-yubikey - -Switch GPG between two YubiKeys that hold identical key material. - -GPG binds secret key stubs to a specific card serial number. When you swap YubiKeys, GPG prompts you to insert the other one. This script deletes the stubs and re-learns whichever card is currently inserted. - -## Install - -```bash -cp gpg-switch-yubikey ~/.local/bin/ -chmod +x ~/.local/bin/gpg-switch-yubikey -``` - -Make sure `~/.local/bin` is in your `PATH`. - -## Usage - -After physically swapping to a different YubiKey: - -```bash -gpg-switch-yubikey -``` - -## Configuration - -The script contains a hardcoded GPG key fingerprint. Edit the `FINGERPRINT` variable in the script to match your own key. diff --git a/yubikey-setup/README.md b/yubikey-setup/README.md new file mode 100644 index 0000000..def2534 --- /dev/null +++ b/yubikey-setup/README.md @@ -0,0 +1,85 @@ +# yubikey-setup + +Set up the YubiKey on a fresh machine — GPG commit signing, SSH auth, and git +identity — without hunting for the public key. + +The YubiKey's OpenPGP app holds the private keys (signature, auth, encryption). +gpg needs the matching **public key** in its keyring to sign, but a fresh +machine doesn't have it and the card alone doesn't hand it over in a usable +form. This subfolder bundles `pubkey.asc` (the public key block) so `setup.sh` +can import it directly — clone the repo on a new machine, insert the YubiKey, +run the script, done. + +## What it configures + +- **GPG**: imports the bundled public key, binds the YubiKey's secret stubs, + sets ultimate trust (so signatures verify as `Good` not `unknown`). +- **SSH**: enables the YubiKey's auth key as an SSH key via gpg-agent + (`enable-ssh-support`), so `ssh` and `git push` authenticate with the YubiKey. +- **Git**: sets `user.name`/`user.email`, `user.signingkey`, `gpg.format + openpgp`, and `commit.gpgsign`/`tag.gpgsign true`. +- **Verification**: a local `allowed_signers` file plus a test GPG signature + with the YubiKey to confirm everything works end to end. + +## Requirements + +- `gpg` (gnupg), `ssh-keygen` (openssh), `git` +- A YubiKey with an OpenPGP app inserted +- Run as your normal user + +## Usage + +```bash +./setup.sh +``` + +Tap the YubiKey when it blinks (it signs a test blob and, for SSH, exposes the +auth key through the agent). It's idempotent — re-running on a machine that's +already set up just re-confirms each step. + +After it runs, for the current shell, enable the YubiKey for SSH: + +```bash +export SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)" +``` + +New logins pick this up automatically via gpg-agent. Add the YubiKey's SSH +public key (`ssh-add -L`) to Gitea/GitHub under **Settings → SSH/GPG Keys** so +pushes authenticate and commits verify. + +## Files + +- `setup.sh` — the installer (run this on a fresh machine). +- `pubkey.asc` — the public key block (primary `63C6571F1095CC5C` + S/A/E + subkeys). Safe to ship in a repo: it's a public key, no secret material. +- `gpg-switch-yubikey` — re-learn the currently inserted YubiKey after + physically swapping between two YubiKeys that hold identical key material. + +## Switching between two YubiKeys + +GPG binds secret key stubs to a specific card serial number. When you swap to a +different YubiKey holding identical key material, GPG prompts you to insert the +other one. `gpg-switch-yubikey` deletes the stubs and re-learns whichever card +is currently inserted: + +```bash +# one-time: put the script on your PATH +cp gpg-switch-yubikey ~/.local/bin/ +chmod +x ~/.local/bin/gpg-switch-yubikey + +# after physically swapping YubiKeys: +gpg-switch-yubikey +``` + +It contains a hardcoded GPG key fingerprint — edit the `FINGERPRINT` variable to +match your own key. + +## Notes + +- **No private keys** are stored or generated by this setup. The YubiKey remains + the sole holder of the private material; only the public key is bundled here. +- The script resolves machine-specific values (UID, the gpg-agent SSH socket + path) at runtime, so it's portable across UIDs. +- `FINGERPRINT`, `KEYID`, `GIT_NAME`, and `GIT_EMAIL` near the top of + `setup.sh` pin this to a specific key/identity. Change them if you adopt this + for a different key. diff --git a/gpg-switch-yubikey/gpg-switch-yubikey b/yubikey-setup/gpg-switch-yubikey similarity index 100% rename from gpg-switch-yubikey/gpg-switch-yubikey rename to yubikey-setup/gpg-switch-yubikey diff --git a/yubikey-setup/pubkey.asc b/yubikey-setup/pubkey.asc new file mode 100644 index 0000000..23f7dda --- /dev/null +++ b/yubikey-setup/pubkey.asc @@ -0,0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hdnb4VJsMVqNIjhgpg +95mTOoRtdcO2P7Ocaoh4BBgWCgAgFiEESiJ93vp6NwSGCVSYY8ZXHxCVzFwFAmmZ +e6ECGyAACgkQY8ZXHxCVzFzEPAEAnLd2QZMKQApr/AV6GRwvHcWSzeUviSe2KsZM +WgXJwGsBAJel+CFTCNtPYM3r/D3mvap8Ej4ZXWEfZiVgotrciOUDuDgEaZl7oRIK +KwYBBAGXVQEFAQEHQNK3aAJPmg4hIDBZYT43kl4DhBcGHBcsNYjerAPTHoUsAwEI +B4h4BBgWCgAgFiEESiJ93vp6NwSGCVSYY8ZXHxCVzFwFAmmZe6ECGwwACgkQY8ZX +HxCVzFyI0AD7B1rdicvHPoGD4vwznfmZ1VHH33DeX/ZhorQJSOL00cMA/11iRRxg +qJzlNl/bpcq23MHQLSqLmns7uXz2epyB+v8N +=Ivzv +-----END PGP PUBLIC KEY BLOCK----- diff --git a/yubikey-setup/setup.sh b/yubikey-setup/setup.sh new file mode 100755 index 0000000..309e72f --- /dev/null +++ b/yubikey-setup/setup.sh @@ -0,0 +1,146 @@ +#!/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 ` 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" </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 '')" +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)."