<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title></title>
    <description>Writing about stuffs</description>
    <link>https://thoenen.kim//</link>
    <atom:link href="https://thoenen.kim//feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Tue, 30 Jan 2018 06:51:50 -0600</pubDate>
    <lastBuildDate>Tue, 30 Jan 2018 06:51:50 -0600</lastBuildDate>
    <generator>Jekyll v3.6.2</generator>
    
      <item>
        <title>GPG with Agent on OS X - Sign your commits!</title>
        <description>&lt;p&gt;With the recent accouncement of the new &lt;a href=&quot;https://github.com/blog/2144-gpg-signature-verification&quot;&gt;GPG signature verification&lt;/a&gt; on GitHub, GPG signing moved back into the spotlight. The more people adopting it the better, however there are not too many helpful guides out there. I personally struggled while trying to set it up on my machine a few weeks ago and that’s why I want to share my learnings here.&lt;/p&gt;

&lt;p&gt;First thing to do is to install &lt;code&gt;gpg&lt;/code&gt; + &lt;code&gt;gpg2&lt;/code&gt; and &lt;code&gt;gpg-agent&lt;/code&gt; + &lt;code&gt;pinentry-mac&lt;/code&gt;. The first two are for the main GPG actions (we need both for compatibility reasons) and the last two will be used for storing the GPG key passphrase in OS X’s keychain.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;$ brew install gpg gpg2 gpg-agent pinentry-mac
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Second, we have to generate a new GPG key if you don’t already have one. You can start the wizard to generate a new key with the following command:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;$ gpg2 --gen-key
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The wizard is going to ask you a few questions, here’s what I’d recommend:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;kind: RSA and RSA&lt;/li&gt;
  &lt;li&gt;keysize: 4096 bits&lt;/li&gt;
  &lt;li&gt;expiry: 1y&lt;/li&gt;
  &lt;li&gt;real name: use your real name&lt;/li&gt;
  &lt;li&gt;email adress: same here&lt;/li&gt;
  &lt;li&gt;comment: some extra information like your website URL or Twitter handle&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Afterwards, it will open a new window (this is the pinentry-mac app we installed earlier) where it prompts you for a passphrase - &lt;a href=&quot;https://xkcd.com/936/&quot;&gt;use a strong password&lt;/a&gt; and store it somewhere safe.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/gpg-github-create-pinentry.png&quot; alt=&quot;Verification&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Next, check that everything worked as expected by listing your keys:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;$ gpg2 -k
/Users/kim/.gnupg/pubring.gpg
-----------------------------
pub   4096R/77CBA19D 2016-04-06 [expires: 2017-04-06]
uid       [ultimate] Kim Thoenen (chive.ch) &amp;lt;kim@smuzey.ch&amp;gt;
sub   4096R/5B5DDEE7 2016-04-06 [expires: 2017-04-06]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now to configuring &lt;code&gt;gpg-agent&lt;/code&gt;. This will prevents us from having to enter our key every time we want to encrypt, decrypt or sign something.&lt;/p&gt;

&lt;p&gt;Add to following to your &lt;code&gt;~/.bash_profile&lt;/code&gt; file (or any other location that will be executed during shell startup)&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;[ -f ~/.gnupg/gpg-agent.env ] &amp;amp;&amp;amp; source ~/.gnupg/gpg-agent.env
if [ -S &quot;${GPG_AGENT_INFO%%:*}&quot; ]; then
  export GPG_AGENT_INFO
else
  eval $(gpg-agent --daemon --log-file /tmp/gpg.log --write-env-file ~/.gnupg/gpg-agent.env --pinentry-program /usr/local/bin/pinentry-mac)
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will ensure that the &lt;code&gt;gpg-agent&lt;/code&gt; is running when you open a shell, and will also configure your current shell to find it.&lt;/p&gt;

&lt;p&gt;Next, open the file &lt;code&gt;~/.gnupg/gpg.conf&lt;/code&gt; and uncomment the line where it says &lt;code&gt;use-agent&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, everything on the GPG side is setup and we can test it with the following command. It will encrypt “hello world” for &lt;code&gt;&amp;lt;recipient&amp;gt;&lt;/code&gt; and then immediately decrypt it again.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;$ echo hello world | gpg2 -e -r &amp;lt;identifier&amp;gt; | gpg2 -d
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Replace &lt;code&gt;&amp;lt;identifier&amp;gt;&lt;/code&gt; with yourself - it will fuzzy match it against the name and email you have specified earlier when we’ve created the key. This will also open the pinentry program again: Enter your key, mark the checkbox &lt;code&gt;Save in Keychain&lt;/code&gt; and confirm with &lt;code&gt;OK&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/gpg-github-unlock-pinentry.png&quot; alt=&quot;Verification&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If everything was successful, you should see the “hello world” text again.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/gpg-github-message.png&quot; alt=&quot;Verification&quot; /&gt;&lt;/p&gt;

&lt;p&gt;And now, finally, signing commits: all you need to do is add a -S to your git commit command.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;$ git commit -a -S -m 'signed commit'

You need a passphrase to unlock the secret key for
user: &quot;Kim Thoenen (chive.ch) &amp;lt;kim@smuzey.ch&amp;gt;&quot;
4096-bit RSA key, ID 3AD2F563, created 2015-08-29

[master b670131] signed commit
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To see and verify these signatures, there is also a –show-signature option to git log.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;$ git log --show-signature -1
commit b670131bec80d4273e785aa75b5e42dc2cc51017
gpg: Signature made Wed Apr  6 21:34:10 2016 CEST using RSA key ID 3AD2F563
gpg: Good signature from &quot;Kim Thoenen (chive.ch) &amp;lt;kim@smuzey.ch&amp;gt;&quot;
Author: Kim Thoenen &amp;lt;kim@smuzey.ch&amp;gt;
Date:   Wed Apr 6 21:34:10 2016 +0200

    signed commit
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Even nicer is to configure git to automatically sign all commits:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;$ git config --global commit.gpgsign true
&lt;/code&gt;&lt;/pre&gt;

&lt;blockquote&gt;
  &lt;p&gt;Note: If you have &lt;strong&gt;multiple GPG keys&lt;/strong&gt; you can specify one to be used as default by running: &lt;code&gt;$ git config --global user.signingkey '&amp;lt;identifier&amp;gt;'&lt;/code&gt;. Thanks to &lt;a href=&quot;https://twitter.com/schovi&quot;&gt;@schovi&lt;/a&gt; for that hint!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The last thing to do now is to let GitHub know about your key. Go to &lt;a href=&quot;https://github.com/settings/keys&quot;&gt;GitHub key settings&lt;/a&gt; and click on “New GPG key”. Paste the content of the following command and hit “Add GPG key”&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;$ gpg --armor --export &amp;lt;identifier&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;After you’ve pushed your test commit to GitHub it should be verified and look something like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/gpg-github-verification.png&quot; alt=&quot;Verification&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If you want to read more about GPG and signing, here’s a few links I’d recommend:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/blog/2144-gpg-signature-verification&quot;&gt;GitHub’s blog post on GPG signature verification&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://git-scm.com/book/en/v2/Git-Tools-Signing-Your-Work&quot;&gt;Git’s guide on signing&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
        <pubDate>Wed, 06 Apr 2016 13:30:00 -0500</pubDate>
        <link>https://thoenen.kim//security/2016/04/06/gpg-on-os-x.html</link>
        <guid isPermaLink="true">https://thoenen.kim//security/2016/04/06/gpg-on-os-x.html</guid>
        
        
        <category>Security</category>
        
      </item>
    
  </channel>
</rss>
