Skip to content

Instantly share code, notes, and snippets.

@aldegoeij
Last active December 15, 2021 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aldegoeij/ac76fe00655c833364ffee2cfde180ac to your computer and use it in GitHub Desktop.
Save aldegoeij/ac76fe00655c833364ffee2cfde180ac to your computer and use it in GitHub Desktop.
YubiKey GPG for git SSH and commit signing

So you want to be super secure and awesome with a GitHub verified badge to your name? Read on!

The below has been tested and verified with a new 'Yubico YubiKey 5c NFC', note that FIPS versions don't seem to support SSH usage, and e.g. the BIO is missing support for several standards).

Goal

The idea is you end up with a YubiKey 5 with:

  • a signing key for commit signing
  • an authorization key for SSH authentication
  • protected by PIN cached for X amount of time (if someone steals it)
  • protected by touch for each operation (to prevent unattended use)
  • custom PINs setup so it can't be reset

Steps

  • Create or export a GPG secret key (you can e.g. use your ProtonMail exported secret key 😎)
  • Create yubikey-only signing and authentication keys
  • Set custom PINs and settings
  • Test it :party:

OK, let's do it

Prerequisites

Assuming you are on macOS, have brew installed and are running zsh:

# Use brew-managed openssh to be on latest featureset
brew install openssh gpg-suite ykman

# Setup gpg-agent and shell integration properly
echo 'enable-ssh-support' >> ~/.gnupg/gpg-agent.conf
echo 'export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)' >> ~/.zshrc
echo 'gpgconf --launch gpg-agent' >> ~/.zshrc
echo 'gpg-connect-agent /bye' >> ~/.zshrc
echo 'export GPG_TTY=$(tty)' >> ~/.zshrc

Setup the keys

Make sure you have your secret key either created using gpg --expert --full-generate-key or downloaded securely from e.g. ProtonMail.

# Find your SOME_KEY_ID
gpg --list-secret-keys --with-keygrip

gpg --expert --edit-key SOME_KEY_ID

# add the existing secret key
keytocard 

# add the existing encryption key
key 1
keytocard

# add a device only signing key
addcardkey
1

# add a device only authentication key
addcardkey
3

# SAVE it!
save
quit

Test it works

echo "test" | gpg -u YOUR_KEY_USER_ID --clearsign
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment