1 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
2 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"
3 [<!ENTITY mdash "—">]>
5 - Copyright (C) 2010, 2012, 2013 Internet Systems Consortium, Inc. ("ISC")
7 - Permission to use, copy, modify, and/or distribute this software for any
8 - purpose with or without fee is hereby granted, provided that the above
9 - copyright notice and this permission notice appear in all copies.
11 - THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 - REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 - AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 - INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 - LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 - OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 - PERFORMANCE OF THIS SOFTWARE.
20 <!-- $Id: pkcs11.xml,v 1.7 2012/01/16 22:50:12 each Exp $ -->
23 <title>PKCS #11 (Cryptoki) support</title>
24 <para>PKCS #11 (Public Key Cryptography Standard #11) defines a
25 platform- independent API for the control of hardware security
26 modules (HSMs) and other cryptographic support devices.</para>
27 <para>BIND 9 is known to work with two HSMs: The Sun SCA 6000
28 cryptographic acceleration board, tested under Solaris x86, and
29 the AEP Keyper network-attached key storage device, tested with
30 Debian Linux, Solaris x86 and Windows Server 2003.</para>
32 <title>Prerequisites</title>
33 <para>See the HSM vendor documentation for information about
34 installing, initializing, testing and troubleshooting the
36 <para>BIND 9 uses OpenSSL for cryptography, but stock OpenSSL
37 does not yet fully support PKCS #11. However, a PKCS #11 engine
38 for OpenSSL is available from the OpenSolaris project. It has
39 been modified by ISC to work with with BIND 9, and to provide
40 new features such as PIN management and key by
42 <para>The patched OpenSSL depends on a "PKCS #11 provider".
43 This is a shared library object, providing a low-level PKCS #11
44 interface to the HSM hardware. It is dynamically loaded by
45 OpenSSL at runtime. The PKCS #11 provider comes from the HSM
46 vendor, and is specific to the HSM to be controlled.</para>
47 <para>There are two "flavors" of PKCS #11 support provided by
48 the patched OpenSSL, one of which must be chosen at
49 configuration time. The correct choice depends on the HSM
53 <para>Use 'crypto-accelerator' with HSMs that have hardware
54 cryptographic acceleration features, such as the SCA 6000
55 board. This causes OpenSSL to run all supported
56 cryptographic operations in the HSM.</para>
59 <para>Use 'sign-only' with HSMs that are designed to
60 function primarily as secure key storage devices, but lack
61 hardware acceleration. These devices are highly secure, but
62 are not necessarily any faster at cryptography than the
63 system CPU — often, they are slower. It is therefore
64 most efficient to use them only for those cryptographic
65 functions that require access to the secured private key,
66 such as zone signing, and to use the system CPU for all
67 other computationally-intensive operations. The AEP Keyper
68 is an example of such a device.</para>
71 <para>The modified OpenSSL code is included in the BIND 9 release,
72 in the form of a context diff against the latest verions of
73 OpenSSL. OpenSSL 0.9.8, 1.0.0 and 1.0.1 are supported; there are
74 separate diffs for each version. In the examples to follow,
75 we use OpenSSL 0.9.8, but the same methods work with OpenSSL 1.0.0
79 The latest OpenSSL versions at the time of the BIND release
80 are 0.9.8y, 1.0.0k and 1.0.1e.
81 ISC will provide an updated patch as new versions of OpenSSL
82 are released. The version number in the following examples
83 is expected to change.</note>
85 Before building BIND 9 with PKCS #11 support, it will be
86 necessary to build OpenSSL with this patch in place and inform
87 it of the path to the HSM-specific PKCS #11 provider
89 <para>Obtain OpenSSL 0.9.8s:</para>
91 $ <userinput>wget <ulink>http://www.openssl.org/source/openssl-0.9.8s.tar.gz</ulink></userinput>
93 <para>Extract the tarball:</para>
95 $ <userinput>tar zxf openssl-0.9.8s.tar.gz</userinput>
97 <para>Apply the patch from the BIND 9 release:</para>
99 $ <userinput>patch -p1 -d openssl-0.9.8s \
100 < bind9/bin/pkcs11/openssl-0.9.8s-patch</userinput>
102 <note>(Note that the patch file may not be compatible with the
103 "patch" utility on all operating systems. You may need to
104 install GNU patch.)</note>
105 <para>When building OpenSSL, place it in a non-standard
106 location so that it does not interfere with OpenSSL libraries
107 elsewhere on the system. In the following examples, we choose
108 to install into "/opt/pkcs11/usr". We will use this location
109 when we configure BIND 9.</para>
112 <title>Building OpenSSL for the AEP Keyper on Linux</title>
113 <para>The AEP Keyper is a highly secure key storage device,
114 but does not provide hardware cryptographic acceleration. It
115 can carry out cryptographic operations, but it is probably
116 slower than your system's CPU. Therefore, we choose the
117 'sign-only' flavor when building OpenSSL.</para>
118 <para>The Keyper-specific PKCS #11 provider library is
119 delivered with the Keyper software. In this example, we place
120 it /opt/pkcs11/usr/lib:</para>
122 $ <userinput>cp pkcs11.GCC4.0.2.so.4.05 /opt/pkcs11/usr/lib/libpkcs11.so</userinput>
124 <para>This library is only available for Linux as a 32-bit
125 binary. If we are compiling on a 64-bit Linux system, it is
126 necessary to force a 32-bit build, by specifying -m32 in the
127 build options.</para>
128 <para>Finally, the Keyper library requires threads, so we
129 must specify -pthread.</para>
131 $ <userinput>cd openssl-0.9.8s</userinput>
132 $ <userinput>./Configure linux-generic32 -m32 -pthread \
133 --pk11-libname=/opt/pkcs11/usr/lib/libpkcs11.so \
134 --pk11-flavor=sign-only \
135 --prefix=/opt/pkcs11/usr</userinput>
137 <para>After configuring, run "<command>make</command>"
138 and "<command>make test</command>". If "<command>make
139 test</command>" fails with "pthread_atfork() not found", you forgot to
140 add the -pthread above.</para>
144 <title>Building OpenSSL for the SCA 6000 on Solaris</title>
145 <para>The SCA-6000 PKCS #11 provider is installed as a system
146 library, libpkcs11. It is a true crypto accelerator, up to 4
147 times faster than any CPU, so the flavor shall be
148 'crypto-accelerator'.</para>
149 <para>In this example, we are building on Solaris x86 on an
152 $ <userinput>cd openssl-0.9.8s</userinput>
153 $ <userinput>./Configure solaris64-x86_64-cc \
154 --pk11-libname=/usr/lib/64/libpkcs11.so \
155 --pk11-flavor=crypto-accelerator \
156 --prefix=/opt/pkcs11/usr</userinput>
158 <para>(For a 32-bit build, use "solaris-x86-cc" and
159 /usr/lib/libpkcs11.so.)</para>
160 <para>After configuring, run
161 <command>make</command> and
162 <command>make test</command>.</para>
166 <title>Building OpenSSL for SoftHSM</title>
167 <para>SoftHSM is a software library provided by the OpenDNSSEC
168 project (http://www.opendnssec.org) which provides a PKCS#11
169 interface to a virtual HSM, implemented in the form of encrypted
170 data on the local filesystem. It uses the Botan library for
171 encryption and SQLite3 for data storage. Though less secure
172 than a true HSM, it can provide more secure key storage than
173 traditional key files, and can allow you to experiment with
174 PKCS#11 when an HSM is not available.</para>
175 <para>The SoftHSM cryptographic store must be installed and
176 initialized before using it with OpenSSL, and the SOFTHSM_CONF
177 environment variable must always point to the SoftHSM configuration
180 $ <userinput> cd softhsm-1.3.0 </userinput>
181 $ <userinput> configure --prefix=/opt/pkcs11/usr </userinput>
182 $ <userinput> make </userinput>
183 $ <userinput> make install </userinput>
184 $ <userinput> export SOFTHSM_CONF=/opt/pkcs11/softhsm.conf </userinput>
185 $ <userinput> echo "0:/opt/pkcs11/softhsm.db" > $SOFTHSM_CONF </userinput>
186 $ <userinput> /opt/pkcs11/usr/bin/softhsm --init-token 0 --slot 0 --label softhsm </userinput>
188 <para>SoftHSM can perform all cryptographic operations, but
189 since it only uses your system CPU, there is no need to use it
190 for anything but signing. Therefore, we choose the 'sign-only'
191 flavor when building OpenSSL.</para>
193 $ <userinput>cd openssl-0.9.8s</userinput>
194 $ <userinput>./Configure linux-x86_64 -pthread \
195 --pk11-libname=/opt/pkcs11/usr/lib/libpkcs11.so \
196 --pk11-flavor=sign-only \
197 --prefix=/opt/pkcs11/usr</userinput>
199 <para>After configuring, run "<command>make</command>"
200 and "<command>make test</command>".</para>
202 <para>Once you have built OpenSSL, run
203 "<command>apps/openssl engine pkcs11</command>" to confirm
204 that PKCS #11 support was compiled in correctly. The output
205 should be one of the following lines, depending on the flavor
208 (pkcs11) PKCS #11 engine support (sign only)
212 (pkcs11) PKCS #11 engine support (crypto accelerator)
215 "<command>apps/openssl engine pkcs11 -t</command>". This will
216 attempt to initialize the PKCS #11 engine. If it is able to
217 do so successfully, it will report
218 <quote><literal>[ available ]</literal></quote>.</para>
219 <para>If the output is correct, run
220 "<command>make install</command>" which will install the
221 modified OpenSSL suite to
222 <filename>/opt/pkcs11/usr</filename>.</para>
225 <title>Building BIND 9 with PKCS#11</title>
226 <para>When building BIND 9, the location of the custom-built
227 OpenSSL library must be specified via configure.</para>
230 <title>Configuring BIND 9 for Linux with the AEP Keyper</title>
231 <para>To link with the PKCS #11 provider, threads must be
232 enabled in the BIND 9 build.</para>
233 <para>The PKCS #11 library for the AEP Keyper is currently
234 only available as a 32-bit binary. If we are building on a
235 64-bit host, we must force a 32-bit build by adding "-m32" to
236 the CC options on the "configure" command line.</para>
238 $ <userinput>cd ../bind9</userinput>
239 $ <userinput>./configure CC="gcc -m32" --enable-threads \
240 --with-openssl=/opt/pkcs11/usr \
241 --with-pkcs11=/opt/pkcs11/usr/lib/libpkcs11.so</userinput>
246 <title>Configuring BIND 9 for Solaris with the SCA 6000</title>
247 <para>To link with the PKCS #11 provider, threads must be
248 enabled in the BIND 9 build.</para>
250 $ <userinput>cd ../bind9</userinput>
251 $ <userinput>./configure CC="cc -xarch=amd64" --enable-threads \
252 --with-openssl=/opt/pkcs11/usr \
253 --with-pkcs11=/usr/lib/64/libpkcs11.so</userinput>
255 <para>(For a 32-bit build, omit CC="cc -xarch=amd64".)</para>
256 <para>If configure complains about OpenSSL not working, you
257 may have a 32/64-bit architecture mismatch. Or, you may have
258 incorrectly specified the path to OpenSSL (it should be the
259 same as the --prefix argument to the OpenSSL
264 <title>Configuring BIND 9 for SoftHSM</title>
266 $ <userinput>cd ../bind9</userinput>
267 $ <userinput>./configure --enable-threads \
268 --with-openssl=/opt/pkcs11/usr \
269 --with-pkcs11=/opt/pkcs11/usr/lib/libpkcs11.so</userinput>
272 <para>After configuring, run
273 "<command>make</command>",
274 "<command>make test</command>" and
275 "<command>make install</command>".</para>
276 <para>(Note: If "make test" fails in the "pkcs11" system test, you may
277 have forgotten to set the SOFTHSM_CONF environment variable.)</para>
280 <title>PKCS #11 Tools</title>
281 <para>BIND 9 includes a minimal set of tools to operate the
283 <command>pkcs11-keygen</command> to generate a new key pair
285 <command>pkcs11-list</command> to list objects currently
287 <command>pkcs11-destroy</command> to remove objects.</para>
288 <para>In UNIX/Linux builds, these tools are built only if BIND
289 9 is configured with the --with-pkcs11 option. (NOTE: If
290 --with-pkcs11 is set to "yes", rather than to the path of the
291 PKCS #11 provider, then the tools will be built but the
292 provider will be left undefined. Use the -m option or the
293 PKCS11_PROVIDER environment variable to specify the path to the
297 <title>Using the HSM</title>
298 <para>First, we must set up the runtime environment so the
299 OpenSSL and PKCS #11 libraries can be loaded:</para>
301 $ <userinput>export LD_LIBRARY_PATH=/opt/pkcs11/usr/lib:${LD_LIBRARY_PATH}</userinput>
303 <para>When operating an AEP Keyper, it is also necessary to
304 specify the location of the "machine" file, which stores
305 information about the Keyper for use by PKCS #11 provider
306 library. If the machine file is in
307 <filename>/opt/Keyper/PKCS11Provider/machine</filename>,
310 $ <userinput>export KEYPER_LIBRARY_PATH=/opt/Keyper/PKCS11Provider</userinput>
312 <!-- TODO: why not defined at compile time? -->
313 <para>These environment variables must be set whenever running
314 any tool that uses the HSM, including
315 <command>pkcs11-keygen</command>,
316 <command>pkcs11-list</command>,
317 <command>pkcs11-destroy</command>,
318 <command>dnssec-keyfromlabel</command>,
319 <command>dnssec-signzone</command>,
320 <command>dnssec-keygen</command>(which will use the HSM for
321 random number generation), and
322 <command>named</command>.</para>
323 <para>We can now create and use keys in the HSM. In this case,
324 we will create a 2048 bit key and give it the label
327 $ <userinput>pkcs11-keygen -b 2048 -l sample-ksk</userinput>
329 <para>To confirm that the key exists:</para>
331 $ <userinput>pkcs11-list</userinput>
333 object[0]: handle 2147483658 class 3 label[8] 'sample-ksk' id[0]
334 object[1]: handle 2147483657 class 2 label[8] 'sample-ksk' id[0]
336 <para>Before using this key to sign a zone, we must create a
337 pair of BIND 9 key files. The "dnssec-keyfromlabel" utility
338 does this. In this case, we will be using the HSM key
339 "sample-ksk" as the key-signing key for "example.net":</para>
341 $ <userinput>dnssec-keyfromlabel -l sample-ksk -f KSK example.net</userinput>
343 <para>The resulting K*.key and K*.private files can now be used
344 to sign the zone. Unlike normal K* files, which contain both
345 public and private key data, these files will contain only the
346 public key data, plus an identifier for the private key which
347 remains stored within the HSM. The HSM handles signing with the
349 <para>If you wish to generate a second key in the HSM for use
350 as a zone-signing key, follow the same procedure above, using a
351 different keylabel, a smaller key size, and omitting "-f KSK"
352 from the dnssec-keyfromlabel arguments:</para>
354 $ <userinput>pkcs11-keygen -b 1024 -l sample-zsk</userinput>
355 $ <userinput>dnssec-keyfromlabel -l sample-zsk example.net</userinput>
357 <para>Alternatively, you may prefer to generate a conventional
358 on-disk key, using dnssec-keygen:</para>
360 $ <userinput>dnssec-keygen example.net</userinput>
362 <para>This provides less security than an HSM key, but since
363 HSMs can be slow or cumbersome to use for security reasons, it
364 may be more efficient to reserve HSM keys for use in the less
365 frequent key-signing operation. The zone-signing key can be
366 rolled more frequently, if you wish, to compensate for a
367 reduction in key security.</para>
368 <para>Now you can sign the zone. (Note: If not using the -S
370 <command>dnssec-signzone</command>, it will be necessary to add
372 <filename>K*.key</filename> files to the zone master file before
375 $ <userinput>dnssec-signzone -S example.net</userinput>
377 Verifying the zone using the following algorithms:
379 Zone signing complete:
380 Algorithm: NSEC3RSASHA1: ZSKs: 1, KSKs: 1 active, 0 revoked, 0 stand-by
385 <title>Specifying the engine on the command line</title>
386 <para>The OpenSSL engine can be specified in
387 <command>named</command> and all of the BIND
388 <command>dnssec-*</command> tools by using the "-E
389 <engine>" command line option. If BIND 9 is built with
390 the --with-pkcs11 option, this option defaults to "pkcs11".
391 Specifying the engine will generally not be necessary unless
392 for some reason you wish to use a different OpenSSL
394 <para>If you wish to disable use of the "pkcs11" engine —
395 for troubleshooting purposes, or because the HSM is unavailable
396 — set the engine to the empty string. For example:</para>
398 $ <userinput>dnssec-signzone -E '' -S example.net</userinput>
401 <command>dnssec-signzone</command> to run as if it were compiled
402 without the --with-pkcs11 option.</para>
405 <title>Running named with automatic zone re-signing</title>
407 <command>named</command> to dynamically re-sign zones using HSM
408 keys, and/or to to sign new records inserted via nsupdate, then
409 named must have access to the HSM PIN. This can be accomplished
410 by placing the PIN into the openssl.cnf file (in the above
412 <filename>/opt/pkcs11/usr/ssl/openssl.cnf</filename>).</para>
413 <para>The location of the openssl.cnf file can be overridden by
414 setting the OPENSSL_CONF environment variable before running
416 <para>Sample openssl.cnf:</para>
418 openssl_conf = openssl_def
420 engines = engine_section
422 pkcs11 = pkcs11_section
424 PIN = <replaceable><PLACE PIN HERE></replaceable>
426 <para>This will also allow the dnssec-* tools to access the HSM
427 without PIN entry. (The pkcs11-* tools access the HSM directly,
428 not via OpenSSL, so a PIN will still be required to use
431 If the PIN is not known, I believe the first time named needs the
432 PIN to open a key, it'll ask you to type in the PIN, which will be
433 a problem because it probably won't be running on a terminal
436 <para>Placing the HSM's PIN in a text file in
437 this manner may reduce the security advantage of using an
438 HSM. Be sure this is what you want to do before configuring
439 OpenSSL in this way.</para>
442 <!-- TODO: what is alternative then for named dynamic re-signing? -->
443 <!-- TODO: what happens if PIN is not known? named will log about it? -->