Sub topic 2
How Git signs: OpenPGP, SSH, and X.509 backends
This chapter assumes you know basic Git (commit, tag, log, git config), can use a shell, and have a general idea of public and private keys. You will learn how Git hands signing work to other programs and how one setting, gpg.format, selects the program.
Git does not sign anything itself
Git contains no cryptography for signatures. When you sign a commit, Git builds the commit data, passes it to an external program, and stores the signature that comes back in the commit object. When you verify, Git passes the data and signature to a program again and reads the result.
This design means you choose the signing technology by choosing the program. It also means most signing errors come from that external program, not from Git.
The three backends
The gpg.format setting picks the signature format. Git then runs the program configured for that format.
| `gpg.format` value | Format | Default program | Key type |
|---|---|---|---|
| `openpgp` (default) | OpenPGP | `gpg` | GPG key pair |
| `ssh` | SSH signature | `ssh-keygen` | SSH key pair |
| `x509` | X.509 / S/MIME | `gpgsm` | Certificate from a certificate authority |
The setting name says gpg even for SSH and X.509. It is a historical name from when OpenPGP was the only option.
OpenPGP
OpenPGP is the original and default backend. Git runs gpg, which signs with a key from your GnuPG keyring. GnuPG has a trust model, key expiry, subkeys, and revocation certificates. These features suit teams that already use GPG, but they bring more concepts to learn.
SSH
Git 2.34 added SSH signing. Git runs ssh-keygen -Y sign, so you can reuse the kind of key you already use to log in over SSH, or create a separate one for signing. There is no keyring or trust database. To verify, you list trusted keys in an allowed signers file, covered in a later chapter. SSH signing is usually the quickest to set up.
X.509
X.509 signs with a certificate issued by a certificate authority, through gpgsm. It fits organizations that already issue certificates to their staff. Most individual developers do not use it, and this tutorial does not set it up.
Choosing the backend and the program
Setting the format takes one command:
git config --global gpg.format ssh
git config --global gpg.formatThe second command prints the value back so you can confirm it. If you never set gpg.format, Git uses openpgp.
You can also change the program for a format. Each format has its own program setting, named gpg.<format>.program:
git config --global gpg.openpgp.program gpg2
git config --global gpg.ssh.program /usr/bin/ssh-keygen
git config --global gpg.x509.program gpgsmYou rarely need these. They matter when the program is not on your PATH or when your system has several versions installed.
What gets signed
For a commit, the signature covers the commit object: the tree hash, the parent hashes, the author and committer lines, and the message. Git stores the signature in a gpgsig header inside the commit. Changing any of those fields changes the commit hash and invalidates the signature. For a tag, the signature is appended to the tag message.
You can see the header yourself:
git cat-file -p HEADOn a signed commit, the output includes a gpgsig block. The block starts with -----BEGIN PGP SIGNATURE----- for OpenPGP or -----BEGIN SSH SIGNATURE----- for SSH.
Common mistakes
- Setting
user.signingkeywithout matchinggpg.format. A GPG key ID withgpg.format sshfails, and so does an SSH key path with the default format. - Assuming
gpg.format sshneeds GnuPG installed. It does not. It needs OpenSSH 8.0 or later with signing support. - Expecting Git to verify SSH signatures with no extra setup. Verification needs an allowed signers file.
Exercise
A teammate sets gpg.format ssh and user.signingkey 3AA5C34371567BD2, and signing fails. What is wrong, and which program does Git run for the signature?
Show answer
The value 3AA5C34371567BD2 is a GPG key ID, but the ssh format expects a path to an SSH public key file or a literal public key. Git runs ssh-keygen -Y sign for this format. The fix is to point user.signingkey at an SSH public key, or to set gpg.format openpgp if they meant to use the GPG key.