GPG
Creating GPG keys, setting a default key and signing Git commits and Maven artifacts.
4 min read
References
- Associating an email with your GPG key
- Telling Git about your signing key
- How to enable SSH access using a GPG key for authentication
- GPG Keys Management
Creating GPG secret key
Ensure GPG is Installed
gpg --versionbrew install gpgGenerate or Import a GPG Key
gpg --full-gen-keygpg --import /path/to/your/private-key.ascSet the Default GPG Key
gpg --list-secret-keys --keyid-format LONG <EMAIL>sec rsa4096/30F2B65B9246B6CA 2017-08-18 [SC] D5E4F29F3275DC0CDA8FFC8730F2B65B9246B6CAuid [ultimate] Mr. Robot <your_email>ssb rsa4096/B7ABC0813E4028C0 2017-08-18 [E]Find the key you want to use and note its ID. Set it as the default by adding it to your Maven configuration in the settings.xml file or your pom.xml.
<properties> <gpg.keyname>YOUR_KEY_ID</gpg.keyname></properties>Alternatively, you can set it globally for GPG by running:
gpg --default-key <YOUR_KEY_ID>To show the associated public key, run this command, replacing
gpg --armor --export <YOUR_KEY_ID>Change identity for a GPG key
to list the long form of the GPG keys for which you have both a public and private key. A private key is required for signing commits or tags.
$ gpg --list-secret-keys --keyid-format=long/Users/hubot/.gnupg/secring.gpg------------------------------------sec 4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]uid Hubot <hubot@example.com>ssb 4096R/4BB6D45482678BE3 2016-03-10In this example, the GPG key ID is 3AA5C34371567BD2:
$ gpg --edit-key 3AA5C34371567BD2to add the user ID details.
$ gpg> adduid Real Name: OCTOCAT Email address: "octocat@github.com" Comment: GITHUB-KEY Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit?Enter O to confirm your selections.
Enter your key’s passphrase.
To save the changes.
$ gpg> saveTo print the GPG key in ASCII armor format
$ gpg --armor --export 3AA5C34371567BD2Copy your GPG key, beginning with -----BEGIN PGP PUBLIC KEY BLOCK----- and ending with -----END PGP PUBLIC KEY BLOCK-----.
Telling Git about your signing key
Telling Git about your GPG key
If you have previously configured Git to use a different key format when signing with --gpg-sign, unset this configuration so the default format of openpgp will be used.
$ git config --global --unset gpg.formatTo set your primary GPG signing key in Git
$ git config --global user.signingkey 3AA5C34371567BD2Telling Git about your SSH key
Configure Git to use SSH to sign commits and tags:
$ git config --global gpg.format sshTo set your SSH signing key in Git
$ git config --global user.signingkey ~/.ssh/id_ed25519.pubor copy the SSH public key to your clipboard.
$ pbcopy < ~/.ssh/id_ed25519.pub# Copies the contents of the id_ed25519.pub file to your clipboardTo set your SSH signing key in Git
$ git config --global user.signingkey 'ssh-ed25519 AAAAC3(...) user@example.com'Enable signing in Git
git commit -S -m "My signed commit"To avoid needing -S every time
git config --global commit.gpgSign trueNow you can just run
git commit -m "My commit"And it will be signed automatically.
Bitbucket Pipelines Setup
The private-key.gpg.enc file is an openssl-encrypted, GPG key file, that contains the key required for signing all files. The unencrypted key file can exported with gpg:
gpg -a --export-secret-key KEYID > private-key.gpgBefore checking it into the bitbucket repository, it was encrypted with openssl:
openssl aes-256-cbc -md sha512 -pbkdf2 -iter 100000 -salt -pass pass:$OPENSSL_PWD -p -in private-key.gpg -out private-key.gpg.encThe password value for the openssl encryption and decryption is stored in the OPENSSL_PWD environment variable configured in Bitbucket and used in the bitbucket-pipelines.yml script.
The settings.xml is configured to access environment variables for the authentication to OSSRH as well the GPG signing:
OSSRH_USER_TOKENandOSSRH-PWD_TOKEN: The username and password tokens for your OSSRH account as an alternative to your actual username and password. You can retrieve the token values by logging into OSSRH.GPG_KEY: the name of the GPG key file to use for signing e.g.F784FAB8GPG_PWD: the password to access the GPG key
Copying a GPG key from one account to another
- Export Keys from the Source Account
List Keys to find your Key ID:
gpg --list-secret-keysExport Public Key:
gpg --armor --export YOUR_KEY_ID > public.ascExport Private Key: This step will require your GPG passphrase.
gpg --armor --export-secret-keys YOUR_KEY_ID > private.ascExport Trust Data (Optional): This preserves your trust settings for the keys.
gpg --export-ownertrust > trust.txt-
Transfer the Files Since these files contain your private key, use a secure method like scp
-
Import Keys to the Destination Account
Import Keys:
gpg --import public.ascgpg --import private.ascImport Trust Data:
gpg --import-ownertrust trust.txt- Verify and Set Ultimate Trust If you did not import a trust database, GPG may show “trust undefined.” You should manually set Ultimate Trust for your own key:
a. Run gpg —edit-key YOUR_KEY_ID.
b. Type trust and press Enter.
c. Enter 5 for “ultimate trust” and confirm with y.
d. Type quit to exit.
- Important Considerations
- Permissions: Ensure the .gnupg directory in the new account has correct permissions (700), often requiring chmod 700 ~/.gnupg.
- Git Configuration: Update your Git config on the new account to use the imported key, or you can copy ~/.gitconfig from the old user to the new user.
git config --global user.signingkey your_key_id- Windows/Git Bash: If transferring to Windows, ensure files are not encoded with BOM (Byte Order Mark) to avoid import errors.
- Alternatively, you can copy the entire ~/.gnupg directory from the old user to the new user, ensuring ownership is updated with chown -R.