<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Passing Curiosity: Posts tagged gpg</title>
    <link href="https://passingcuriosity.com/tags/gpg/gpg.xml" rel="self" />
    <link href="https://passingcuriosity.com" />
    <id>https://passingcuriosity.com/tags/gpg/gpg.xml</id>
    <author>
        <name>Thomas Sutton</name>
        
        <email>me@thomas-sutton.id.au</email>
        
    </author>
    <updated>2024-01-03T00:00:00Z</updated>
    <entry>
    <title>Setup Yubikey with OpenPGP for git</title>
    <link href="https://passingcuriosity.com/2024/yubikey-opengpg-setup/" />
    <id>https://passingcuriosity.com/2024/yubikey-opengpg-setup/</id>
    <published>2024-01-03T00:00:00Z</published>
    <updated>2024-01-03T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>This is a pretty sparse log of things to do to set up a new Yubikey with
OpenPGP for use with <code>git</code>. If anything below doesn’t make sense consult the
sources linked at the end of the post.</p>
<h2 id="install-things">Install things</h2>
<p>We’ll install a bunch of stuff with Homebrew:</p>
<ul>
<li><p><code>git</code> to get a newer version than what Apple ships</p></li>
<li><p><code>ykman</code> to view and tweak Yubikey configuration</p></li>
<li><p><code>pinentry</code> (to pop up a window to enter your Yubikey PIN when required)</p></li>
<li><p>GnuPG 2 to take care of doing all the cryptography</p></li>
</ul>
<pre><code>brew install \
    git \
    ykman \
    gnupg \
    pinentry \
    pinentry-mac </code></pre>
<h2 id="setup-yubikey">Setup Yubikey</h2>
<p>A modern Yubikey probably supports a <em>lot</em> of different interfaces: OTP, PIV,
OpenPGP, FIDO U2F, FIDO2, OATH. Many have one or more PIN that help to prevent
unauthorised usage. If you haven’t already, you should configure the various
codes for all interfaces configured on your Yubikey.</p>
<p>We’re focussed on setting up OpenPGP so let’s just take care of that.</p>
<p>You can use <code>ykman</code> to check the current policy for OpenPGP on your Yubikey:</p>
<pre><code>$ ykman openpgp info
OpenPGP version:            3.4
Application version:        5.4.3
PIN tries remaining:        3
Reset code tries remaining: 0
Admin PIN tries remaining:  3
Require PIN for signature:  Once
Touch policies:
  Signature key:      Off
  Encryption key:     Off
  Authentication key: Off
  Attestation key:    Off</code></pre>
<p>You can use <code>gpg --edit-card</code> to modify the various passwords. When started,
it’ll output the current card configuration and prompt for commands. Use <code>admin</code>
to enter administration mode, then <code>passwd</code> to control the card passwords.</p>
<p>If you have a brand new card will have the following details:</p>
<ul>
<li>PIN: 123456</li>
<li>Admin PIN: 12345678</li>
<li>Reset Code: NONE</li>
<li>Retries: 3</li>
</ul>
<pre><code>$ gpg --edit-card

...

gpg/card&gt; admin
Admin commands are allowed

gpg/card&gt; passwd
gpg: OpenPGP card no. D2760001240100000006196516380000 detected

1 - change PIN
2 - unblock PIN
3 - change Admin PIN
4 - set the Reset Code
Q - quit</code></pre>
<p>You can change the number of attempts allowed for the PIN, reset code, and admin
PIN:</p>
<pre><code>$ ykman openpgp access set-retries 10 10 10</code></pre>
<h2 id="create-new-keys">Create new keys</h2>
<p>The general process looks something like:</p>
<pre><code>$ gpg --gen-key
$ gpg --expert --edit-key KEY_ID
gpg&gt; addkey
# Select whichever &quot;set your own capabilities&quot; option you like; it's probably a
# good idea to use the same key type as your main key.
#
# Enable Authenticate, disable the other actions (Sign and Encrypt)
#
# Set the key expiry to the same as the first key. In my case it was 3y</code></pre>
<p>Export your key so that you can keep a backup off-line somewhere. Make sure that
it is safe, secure, and <em>offline</em>. Print it out or write it on a CD or something
and keep it with your important papers.</p>
<p>Then you can move the new keys to your Yubikey:</p>
<pre><code>$ gpg --edit-key KEY_ID
# Switch to viewing private keys:
gpg&gt; toggle
# First, move the primary key to the Yubikey. The key list will show usage of SC
# denoting a signing key, so move it to the &quot;Signature key&quot; slot.
gpg&gt; keytocard
# Then select the first sub-key.
gpg&gt; key 1
# The key list will show usage of E denoting encryption, so move it to the
# &quot;Encryption key&quot; slot in the Yubikey.
gpg&gt; keytocard
# Finally, deselect the first sub-key and select the second sub-key:
gpg&gt; key 1
gpg&gt; key 2
# This sub-key will have usage &quot;A&quot;, so move it to the &quot;Authentication key&quot; slot.
gpg&gt; keytocard</code></pre>
<p>At various points in this process, GnuPG will ask for the private key passphrase
(if you set one when generating the key) and the Yubikey Admin PIN.</p>
<h2 id="setup-gnupg">Setup GnuPG</h2>
<pre><code>echo &quot;pinentry-program $(which pinentry-mac)&quot; &gt;&gt; ~/.gnupg/gpg-agent.conf
killall gpg-agent</code></pre>
<h2 id="test-signing">Test signing</h2>
<p>You can verify that GnuPG is configured and working correctly by signing a
sample message:</p>
<pre><code>$ echo &quot;Hello world&quot; | gpg --clearsign
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Hello world
-----BEGIN PGP SIGNATURE-----
...
-----END PGP SIGNATURE-----</code></pre>
<p>Depending on the settings for your key, you may need to enter your Yubikey PIN
(usually only the first time you’ve used it this session) and/or touch the key
(see the <code>ykman</code> settings above to control this).</p>
<h2 id="configure-git">Configure <code>git</code></h2>
<p>You need to tell <code>git</code> which key to use when signing things and might like to
set a few other options which control when things are signed and when signatures
are displayed:</p>
<pre><code>$ git config --global user.signingkey KEY_ID</code></pre>
<p>Some settings which may help <code>git</code> to run GnuPG correctly:</p>
<pre><code>$ git config --global gpg.program gpg</code></pre>
<p>Some settings that control when <code>git</code> will sign things and display signatures:</p>
<pre><code>$ git config --global log.showSignature true
$ git config --global commit.gpgSign true
$ git config --global tag.gpgSign true</code></pre>
<h2 id="github-configuration">GitHub configuration</h2>
<p>Export your <em>public</em> key (note the <code>PUBLIC KEY</code> in the output below) and copy
the output.</p>
<pre><code>$ gpg --export --armor KEY_ID

-----BEGIN PGP PUBLIC KEY BLOCK-----
....
-----END PGP PUBLIC KEY BLOCK-----</code></pre>
<p>Go to the “SSH and GPG keys” page of your GitHub settings and click add <a href="https://github.com/settings/gpg/new">New GPG
key</a>. Paste the key into the text area, add
a useful comment to help identify the key, and click the save button.</p>
<p>When you use the GitHub.com web-site to commit changes to your code, GitHub
signs your commits with an internal key. To validate these commits, you’ll need
to import and sign the key:</p>
<pre><code>$ curl https://github.com/web-flow.gpg | gpg --import
$ gpg --lsign-key noreply@github.com</code></pre>
<h2 id="references">References</h2>
<ul>
<li><p>Yubikey PGP <a href="https://developers.yubico.com/PGP/Card_edit.html">Card edit</a></p></li>
<li><p>Yubikey PGP <a href="https://developers.yubico.com/PGP/Importing_keys.html">Importing keys</a></p></li>
<li><p>Yubikey PGP <a href="https://developers.yubico.com/PGP/Git_signing.html">Git signing</a></p></li>
<li><p><a href="https://docs.yubico.com/software/yubikey/tools/ykman/OpenPGP_Commands.html">ykman openpgp manual</a></p></li>
<li><p><a href="https://stackoverflow.com/a/60482908">Trusting GitHub’s key</a></p></li>
<li><p><a href="https://github.com/drduh/YubiKey-Guide">YubiKey Guide</a></p></li>
<li><p><code>git-commit(1)</code> man page</p></li>
<li><p><code>git-tag(1)</code> man page</p></li>
<li><p><code>git-log(1)</code> man page</p></li>
</ul>]]></summary>
</entry>

</feed>
