Files
firestuff/2016-03-26-nitrokey-hsm-ec-setup.html

106 lines
5.8 KiB
HTML
Raw Permalink Normal View History

2019-04-14 23:49:51 +00:00
<!--# set var="title" value="Nitrokey HSM EC setup" -->
2019-04-25 02:45:09 +00:00
<!--# set var="date" value="2016-03-26" -->
2019-04-14 23:49:51 +00:00
<!--# include file="include/top.html" -->
<p>Following up from my previous writeup on <a href="2016-03-21-elliptic-curve-certificate-authority.html">creating an EC CA</a>, lets talk about key security.</p>
2019-04-14 23:49:51 +00:00
<p><a href="https://en.wikipedia.org/wiki/Hardware_security_module">Hardware security modules</a> are physical devices that manage keys. Generally, the rule is that they let you use the keys for operations (e.g. signing) given correct authentication, but dont let you extract the raw key material. This means that if youre holding the HSM, you know that no one else is currently abusing your key (though they may have done so in the past).</p>
<p>Searching for reasonably-priced options, I found the <a href="https://shop.nitrokey.com/shop/product/nitrokey-hsm-7">Nitrokey HSM</a>. Open source hardware, firmware, and software. Supports EC keys. Supports lots of keys. Supports Linux. Awesome.</p>
<p><rant>Open source gets a bad rap sometimes, for the same reason PHP does: it has a low barrier to entry (thats a simplification in pretty much all directions, but roll with it). That means theres a lot of crap out there, because its so easy to start things without a significant audience and not hold to rigorous standards. Open source hardware, however, doesnt have this excuse. Youre selling it. The fact that you publish the designs and the firmware is secondary; youre still making money from it, and theres no excuse for shipping shit that you havent actually finished turning into a product.</rant></p>
<p>Below are the steps to get the Nitrokey HSM to a working state where it can generate an EC key pair, and (self-)sign a cert with it. Hopefully many of these go away in the future, as support percolates into release versions and distribution packages.</p>
<h2>Hardware &amp; setup</h2>
2019-04-14 23:49:51 +00:00
<p>These instructions were developed and tested on a Raspberry Pi. Base setup instructions are here. Youll also need a Nitrokey HSM, obviously.</p>
<h2>Install prerequisites</h2>
2019-04-14 23:49:51 +00:00
<pre><code>sudo apt-get install pcscd libpcsclite-dev libssl-dev libreadline-dev autoconf automake build-essential docbook-xsl xsltproc libtool pkg-config git
</code></pre>
<h2>libccid</h2>
2019-04-14 23:49:51 +00:00
<p>Youll need a <a href="https://www.nitrokey.com/documentation/frequently-asked-questions#which-gnupg,-opensc-and-libccid-versions-are-required">newer version of libccid</a> than currently exists in Raspbian Jessie (1.4.22 > 1.4.18). You can download it for your platform here, or use the commands below for an RPi.</p>
<pre><code>wget http://http.us.debian.org/debian/pool/main/c/ccid/libccid_1.4.22-1_armhf.deb
sudo dpkg -i libccid_1.4.22-1_armhf.deb
</code></pre>
<h2>Install libp11</h2>
2019-04-14 23:49:51 +00:00
<p>engine_pkcs11 requires >= 0.3.1. Raspbian Jessie has 0.2.8. Debian sid <a href="https://packages.debian.org/sid/libp11-2">has a package</a>, but you need the dev package as well, so you might as well build it.</p>
<pre><code>git clone https://github.com/OpenSC/libp11.git
cd libp11
./bootstrap
./configure
make
sudo make install
cd ..
</code></pre>
<h2>Install engine_pkcs11</h2>
2019-04-14 23:49:51 +00:00
<p>EC <a href="https://www.nitrokey.com/forum/viewtopic.php?t=1549">requires engine_pkcs11 >= 0.2.0</a>. Raspbian Jessie has 0.1.8. Debian <a href="https://packages.debian.org/sid/libengine-pkcs11-openssl">sid also has a package</a> that I havent tested.</p>
<pre><code>git clone https://github.com/OpenSC/engine_pkcs11.git
cd engine_pkcs11
./bootstrap
./configure
make
sudo make install
cd ..
</code></pre>
<h2>Install OpenSC</h2>
2019-04-14 23:49:51 +00:00
<p>As of writing (2016/Mar/26), working support for the Nitrokey HSM <a href="https://www.nitrokey.com/documentation/frequently-asked-questions#which-gnupg,-opensc-and-libccid-versions-are-required">requires a build of OpenSC</a> that hasnt made it into a package yet (0.16.0). Theyve also screwed up their repository branching, so master is behind the release branch and wont work.</p>
<pre><code>git clone — branch opensc-0.16.0 https://github.com/OpenSC/OpenSC.git
cd OpenSC
./bootstrap
./configure
make
sudo make install
cd ..
</code></pre>
<h2>Misc</h2>
2019-04-14 23:49:51 +00:00
<pre><code>sudo ldconfig
</code></pre>
<h2>Initialize the device</h2>
2019-04-14 23:49:51 +00:00
<pre><code>/usr/local/bin/sc-hsm-tool --initialize
</code></pre>
<p>If this tells you that it cant find the device, you probably forgot to update libccid, and need to start over. Youll need to set an SO PIN and PIN the first time. The SO PIN should be 16 characters, and the PIN 6. Both should be all digits. They can technically be hex, but some apps get confused if they see letters.</p>
<h2>Generate a test EC key pair</h2>
2019-04-14 23:49:51 +00:00
<pre><code>/usr/local/bin/pkcs11-tool --module /usr/local/lib/opensc-pkcs11.so---login --keypairgen --key-type EC:prime256v1 --label test
</code></pre>
<h2>Generate a self-signed cert</h2>
2019-04-14 23:49:51 +00:00
<pre><code>openssl
OpenSSL&gt; engine -t -pre SO_PATH:/usr/lib/arm-linux-gnueabihf/openssl-1.0.0/engines/libpkcs11.so -pre ID:pkcs11 -pre LIST_ADD:1 -pre LOAD -pre MODULE_PATH:/usr/local/lib/pkcs11/opensc-pkcs11.so dynamic
...
OpenSSL&gt; req -engine pkcs11 -new -keyform engine -out cert.pem -text -x509 -days 3640 -key label_test -subj '/CN=test'
</code></pre>
<p>If you now have a cert.pem file, youre golden. If you see “error in req”, you probably have a <a href="https://www.nitrokey.com/forum/viewtopic.php?t=1549">bad version of OpenSC</a>.</p>
<p>Now, delete the file, re-initialize the device, and youre good to go.</p>
<p>More instructions on various Nitrokey HSM operations can be found <a href="https://github.com/OpenSC/OpenSC/wiki/SmartCardHSM#init">here</a>.</p>
<p>Instructions for running a complete certificate authority (CA) with your Nitrokey are <a href="2016-03-27-ec-ca-redux-now-with-more-nitrokey.html">here</a>.</p>
2019-04-14 23:49:51 +00:00
<!--# include file="include/bottom.html" -->