]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/bind9/doc/arm/pkcs11.xml
Update BIND to 9.9.6-P1
[FreeBSD/stable/9.git] / contrib / bind9 / doc / arm / pkcs11.xml
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 "&#8212;">]>
4 <!--
5  - Copyright (C) 2010, 2012-2014  Internet Systems Consortium, Inc. ("ISC")
6  -
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.
10  -
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.
18 -->
19
20 <!-- $Id: pkcs11.xml,v 1.7 2012/01/16 22:50:12 each Exp $ -->
21
22 <sect1 id="pkcs11">
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>
31   <sect2>
32     <title>Prerequisites</title>
33     <para>See the HSM vendor documentation for information about
34     installing, initializing, testing and troubleshooting the
35     HSM.</para>
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
41     reference.</para>
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
50     hardware:</para>
51     <itemizedlist>
52       <listitem>
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>
57       </listitem>
58       <listitem>
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 &mdash; 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>
69       </listitem>
70     </itemizedlist>
71     <para>
72       The modified OpenSSL code is included in the BIND 9 release,
73       in the form of a context diff against the latest versions of
74       OpenSSL.  OpenSSL 0.9.8, 1.0.0, and 1.0.1 are supported; there are
75       separate diffs for each version.  In the examples to follow,
76       we use OpenSSL 0.9.8, but the same methods work with OpenSSL
77       1.0.0 and 1.0.1.
78     </para>
79     <note>
80       The latest OpenSSL versions at the time of the BIND release
81       are 0.9.8y, 1.0.0k and 1.0.1e.
82       ISC will provide an updated patch as new versions of OpenSSL
83       are released. The version number in the following examples
84       is expected to change.</note>
85     <para>
86     Before building BIND 9 with PKCS #11 support, it will be
87     necessary to build OpenSSL with this patch in place and inform
88     it of the path to the HSM-specific PKCS #11 provider
89     library.</para>
90     <para>Obtain OpenSSL 0.9.8s:</para>
91     <screen>
92 $ <userinput>wget <ulink>http://www.openssl.org/source/openssl-0.9.8s.tar.gz</ulink></userinput>
93 </screen>
94     <para>Extract the tarball:</para>
95     <screen>
96 $ <userinput>tar zxf openssl-0.9.8s.tar.gz</userinput>
97 </screen>
98     <para>Apply the patch from the BIND 9 release:</para>
99     <screen>
100 $ <userinput>patch -p1 -d openssl-0.9.8s \
101             &lt; bind9/bin/pkcs11/openssl-0.9.8s-patch</userinput>
102 </screen>
103     <note>(Note that the patch file may not be compatible with the
104     "patch" utility on all operating systems. You may need to
105     install GNU patch.)</note>
106     <para>When building OpenSSL, place it in a non-standard
107     location so that it does not interfere with OpenSSL libraries
108     elsewhere on the system. In the following examples, we choose
109     to install into "/opt/pkcs11/usr". We will use this location
110     when we configure BIND 9.</para>
111     <sect3>
112       <!-- Example 1 -->
113       <title>Building OpenSSL for the AEP Keyper on Linux</title>
114       <para>The AEP Keyper is a highly secure key storage device,
115       but does not provide hardware cryptographic acceleration. It
116       can carry out cryptographic operations, but it is probably
117       slower than your system's CPU. Therefore, we choose the
118       'sign-only' flavor when building OpenSSL.</para>
119       <para>The Keyper-specific PKCS #11 provider library is
120       delivered with the Keyper software. In this example, we place
121       it /opt/pkcs11/usr/lib:</para>
122       <screen>
123 $ <userinput>cp pkcs11.GCC4.0.2.so.4.05 /opt/pkcs11/usr/lib/libpkcs11.so</userinput>
124 </screen>
125       <para>This library is only available for Linux as a 32-bit
126       binary. If we are compiling on a 64-bit Linux system, it is
127       necessary to force a 32-bit build, by specifying -m32 in the
128       build options.</para>
129       <para>Finally, the Keyper library requires threads, so we
130       must specify -pthread.</para>
131       <screen>
132 $ <userinput>cd openssl-0.9.8s</userinput>
133 $ <userinput>./Configure linux-generic32 -m32 -pthread \
134             --pk11-libname=/opt/pkcs11/usr/lib/libpkcs11.so \
135             --pk11-flavor=sign-only \
136             --prefix=/opt/pkcs11/usr</userinput>
137 </screen>
138       <para>After configuring, run "<command>make</command>"
139       and "<command>make test</command>". If "<command>make
140       test</command>" fails with "pthread_atfork() not found", you forgot to
141       add the -pthread above.</para>
142     </sect3>
143     <sect3>
144       <!-- Example 2 -->
145       <title>Building OpenSSL for the SCA 6000 on Solaris</title>
146       <para>The SCA-6000 PKCS #11 provider is installed as a system
147       library, libpkcs11. It is a true crypto accelerator, up to 4
148       times faster than any CPU, so the flavor shall be
149       'crypto-accelerator'.</para>
150       <para>In this example, we are building on Solaris x86 on an
151       AMD64 system.</para>
152       <screen>
153 $ <userinput>cd openssl-0.9.8s</userinput>
154 $ <userinput>./Configure solaris64-x86_64-cc \
155             --pk11-libname=/usr/lib/64/libpkcs11.so \
156             --pk11-flavor=crypto-accelerator \
157             --prefix=/opt/pkcs11/usr</userinput>
158 </screen>
159       <para>(For a 32-bit build, use "solaris-x86-cc" and
160       /usr/lib/libpkcs11.so.)</para>
161       <para>After configuring, run 
162       <command>make</command> and 
163       <command>make test</command>.</para>
164     </sect3>
165     <sect3>
166       <!-- Example 3 -->
167       <title>Building OpenSSL for SoftHSM</title>
168       <para>SoftHSM is a software library provided by the OpenDNSSEC
169       project (http://www.opendnssec.org) which provides a PKCS#11
170       interface to a virtual HSM, implemented in the form of encrypted
171       data on the local filesystem.  It uses the Botan library for
172       encryption and SQLite3 for data storage.  Though less secure
173       than a true HSM, it can provide more secure key storage than
174       traditional key files, and can allow you to experiment with
175       PKCS#11 when an HSM is not available.</para>
176       <para>The SoftHSM cryptographic store must be installed and
177       initialized before using it with OpenSSL, and the SOFTHSM_CONF
178       environment variable must always point to the SoftHSM configuration
179       file:</para>
180       <screen>
181 $ <userinput> cd softhsm-1.3.0 </userinput>
182 $ <userinput> configure --prefix=/opt/pkcs11/usr </userinput>
183 $ <userinput> make </userinput>
184 $ <userinput> make install </userinput>
185 $ <userinput> export SOFTHSM_CONF=/opt/pkcs11/softhsm.conf </userinput>
186 $ <userinput> echo "0:/opt/pkcs11/softhsm.db" > $SOFTHSM_CONF </userinput>
187 $ <userinput> /opt/pkcs11/usr/bin/softhsm --init-token 0 --slot 0 --label softhsm </userinput>
188 </screen>
189       <para>SoftHSM can perform all cryptographic operations, but
190       since it only uses your system CPU, there is no need to use it
191       for anything but signing.  Therefore, we choose the 'sign-only'
192       flavor when building OpenSSL.</para>
193       <screen>
194 $ <userinput>cd openssl-0.9.8s</userinput>
195 $ <userinput>./Configure linux-x86_64 -pthread \
196             --pk11-libname=/opt/pkcs11/usr/lib/libpkcs11.so \
197             --pk11-flavor=sign-only \
198             --prefix=/opt/pkcs11/usr</userinput>
199 </screen>
200       <para>After configuring, run "<command>make</command>"
201       and "<command>make test</command>".</para>
202     </sect3>
203     <para>Once you have built OpenSSL, run
204     "<command>apps/openssl engine pkcs11</command>" to confirm
205     that PKCS #11 support was compiled in correctly. The output
206     should be one of the following lines, depending on the flavor
207     selected:</para>
208     <screen>
209         (pkcs11) PKCS #11 engine support (sign only)
210 </screen>
211     <para>Or:</para>
212     <screen>
213         (pkcs11) PKCS #11 engine support (crypto accelerator)
214 </screen>
215     <para>Next, run
216     "<command>apps/openssl engine pkcs11 -t</command>". This will
217     attempt to initialize the PKCS #11 engine. If it is able to
218     do so successfully, it will report
219     <quote><literal>[ available ]</literal></quote>.</para>
220     <para>If the output is correct, run
221     "<command>make install</command>" which will install the
222     modified OpenSSL suite to 
223     <filename>/opt/pkcs11/usr</filename>.</para>
224   </sect2>
225   <sect2>
226     <title>Building BIND 9 with PKCS#11</title>
227     <para>When building BIND 9, the location of the custom-built
228     OpenSSL library must be specified via configure.</para>
229     <sect3>
230       <!-- Example 4 -->
231       <title>Configuring BIND 9 for Linux with the AEP Keyper</title>
232       <para>To link with the PKCS #11 provider, threads must be
233       enabled in the BIND 9 build.</para>
234       <para>The PKCS #11 library for the AEP Keyper is currently
235       only available as a 32-bit binary. If we are building on a
236       64-bit host, we must force a 32-bit build by adding "-m32" to
237       the CC options on the "configure" command line.</para>
238       <screen>
239 $ <userinput>cd ../bind9</userinput>
240 $ <userinput>./configure CC="gcc -m32" --enable-threads \
241            --with-openssl=/opt/pkcs11/usr \
242            --with-pkcs11=/opt/pkcs11/usr/lib/libpkcs11.so</userinput>
243 </screen>
244     </sect3>
245     <sect3>
246       <!-- Example 5 -->
247       <title>Configuring BIND 9 for Solaris with the SCA 6000</title>
248       <para>To link with the PKCS #11 provider, threads must be
249       enabled in the BIND 9 build.</para>
250       <screen>
251 $ <userinput>cd ../bind9</userinput>
252 $ <userinput>./configure CC="cc -xarch=amd64" --enable-threads \
253             --with-openssl=/opt/pkcs11/usr \
254             --with-pkcs11=/usr/lib/64/libpkcs11.so</userinput>
255 </screen>
256       <para>(For a 32-bit build, omit CC="cc -xarch=amd64".)</para>
257       <para>If configure complains about OpenSSL not working, you
258       may have a 32/64-bit architecture mismatch. Or, you may have
259       incorrectly specified the path to OpenSSL (it should be the
260       same as the --prefix argument to the OpenSSL
261       Configure).</para>
262     </sect3>
263     <sect3>
264       <!-- Example 6 -->
265       <title>Configuring BIND 9 for SoftHSM</title>
266       <screen>
267 $ <userinput>cd ../bind9</userinput>
268 $ <userinput>./configure --enable-threads \
269            --with-openssl=/opt/pkcs11/usr \
270            --with-pkcs11=/opt/pkcs11/usr/lib/libpkcs11.so</userinput>
271 </screen>
272     </sect3>
273     <para>After configuring, run
274     "<command>make</command>",
275     "<command>make test</command>" and
276     "<command>make install</command>".</para>
277     <para>(Note: If "make test" fails in the "pkcs11" system test, you may
278     have forgotten to set the SOFTHSM_CONF environment variable.)</para>
279   </sect2>
280   <sect2>
281     <title>PKCS #11 Tools</title>
282     <para>BIND 9 includes a minimal set of tools to operate the
283     HSM, including 
284     <command>pkcs11-keygen</command> to generate a new key pair
285     within the HSM, 
286     <command>pkcs11-list</command> to list objects currently
287     available, and 
288     <command>pkcs11-destroy</command> to remove objects.</para>
289     <para>In UNIX/Linux builds, these tools are built only if BIND
290     9 is configured with the --with-pkcs11 option. (NOTE: If
291     --with-pkcs11 is set to "yes", rather than to the path of the
292     PKCS #11 provider, then the tools will be built but the
293     provider will be left undefined. Use the -m option or the
294     PKCS11_PROVIDER environment variable to specify the path to the
295     provider.)</para>
296   </sect2>
297   <sect2>
298     <title>Using the HSM</title>
299     <para>First, we must set up the runtime environment so the
300     OpenSSL and PKCS #11 libraries can be loaded:</para>
301     <screen>
302 $ <userinput>export LD_LIBRARY_PATH=/opt/pkcs11/usr/lib:${LD_LIBRARY_PATH}</userinput>
303 </screen>
304     <para>When operating an AEP Keyper, it is also necessary to
305     specify the location of the "machine" file, which stores
306     information about the Keyper for use by PKCS #11 provider
307     library. If the machine file is in 
308     <filename>/opt/Keyper/PKCS11Provider/machine</filename>,
309     use:</para>
310     <screen>
311 $ <userinput>export KEYPER_LIBRARY_PATH=/opt/Keyper/PKCS11Provider</userinput>
312 </screen>
313     <!-- TODO: why not defined at compile time? -->
314     <para>These environment variables must be set whenever running
315     any tool that uses the HSM, including 
316     <command>pkcs11-keygen</command>, 
317     <command>pkcs11-list</command>, 
318     <command>pkcs11-destroy</command>, 
319     <command>dnssec-keyfromlabel</command>, 
320     <command>dnssec-signzone</command>, 
321     <command>dnssec-keygen</command>(which will use the HSM for
322     random number generation), and 
323     <command>named</command>.</para>
324     <para>We can now create and use keys in the HSM. In this case,
325     we will create a 2048 bit key and give it the label
326     "sample-ksk":</para>
327     <screen>
328 $ <userinput>pkcs11-keygen -b 2048 -l sample-ksk</userinput>
329 </screen>
330     <para>To confirm that the key exists:</para>
331     <screen>
332 $ <userinput>pkcs11-list</userinput>
333 Enter PIN:
334 object[0]: handle 2147483658 class 3 label[8] 'sample-ksk' id[0]
335 object[1]: handle 2147483657 class 2 label[8] 'sample-ksk' id[0]
336 </screen>
337     <para>Before using this key to sign a zone, we must create a
338     pair of BIND 9 key files. The "dnssec-keyfromlabel" utility
339     does this. In this case, we will be using the HSM key
340     "sample-ksk" as the key-signing key for "example.net":</para>
341     <screen>
342 $ <userinput>dnssec-keyfromlabel -l sample-ksk -f KSK example.net</userinput>
343 </screen>
344     <para>The resulting K*.key and K*.private files can now be used
345     to sign the zone. Unlike normal K* files, which contain both
346     public and private key data, these files will contain only the
347     public key data, plus an identifier for the private key which
348     remains stored within the HSM. The HSM handles signing with the
349     private key.</para>
350     <para>If you wish to generate a second key in the HSM for use
351     as a zone-signing key, follow the same procedure above, using a
352     different keylabel, a smaller key size, and omitting "-f KSK"
353     from the dnssec-keyfromlabel arguments:</para>
354     <screen>
355 $ <userinput>pkcs11-keygen -b 1024 -l sample-zsk</userinput>
356 $ <userinput>dnssec-keyfromlabel -l sample-zsk example.net</userinput>
357 </screen>
358     <para>Alternatively, you may prefer to generate a conventional
359     on-disk key, using dnssec-keygen:</para>
360     <screen>
361 $ <userinput>dnssec-keygen example.net</userinput>
362 </screen>
363     <para>This provides less security than an HSM key, but since
364     HSMs can be slow or cumbersome to use for security reasons, it
365     may be more efficient to reserve HSM keys for use in the less
366     frequent key-signing operation. The zone-signing key can be
367     rolled more frequently, if you wish, to compensate for a
368     reduction in key security.</para>
369     <para>Now you can sign the zone. (Note: If not using the -S
370     option to 
371     <command>dnssec-signzone</command>, it will be necessary to add
372     the contents of both 
373     <filename>K*.key</filename> files to the zone master file before
374     signing it.)</para>
375     <screen>
376 $ <userinput>dnssec-signzone -S example.net</userinput>
377 Enter PIN:
378 Verifying the zone using the following algorithms:
379 NSEC3RSASHA1.
380 Zone signing complete:
381 Algorithm: NSEC3RSASHA1: ZSKs: 1, KSKs: 1 active, 0 revoked, 0 stand-by
382 example.net.signed
383 </screen>
384   </sect2>
385   <sect2>
386     <title>Specifying the engine on the command line</title>
387     <para>The OpenSSL engine can be specified in 
388     <command>named</command> and all of the BIND 
389     <command>dnssec-*</command> tools by using the "-E
390     &lt;engine&gt;" command line option. If BIND 9 is built with
391     the --with-pkcs11 option, this option defaults to "pkcs11".
392     Specifying the engine will generally not be necessary unless
393     for some reason you wish to use a different OpenSSL
394     engine.</para>
395     <para>If you wish to disable use of the "pkcs11" engine &mdash;
396     for troubleshooting purposes, or because the HSM is unavailable
397     &mdash; set the engine to the empty string. For example:</para>
398     <screen>
399 $ <userinput>dnssec-signzone -E '' -S example.net</userinput>
400 </screen>
401     <para>This causes 
402     <command>dnssec-signzone</command> to run as if it were compiled
403     without the --with-pkcs11 option.</para>
404   </sect2>
405   <sect2>
406     <title>Running named with automatic zone re-signing</title>
407     <para>If you want 
408     <command>named</command> to dynamically re-sign zones using HSM
409     keys, and/or to to sign new records inserted via nsupdate, then
410     named must have access to the HSM PIN. This can be accomplished
411     by placing the PIN into the openssl.cnf file (in the above
412     examples, 
413     <filename>/opt/pkcs11/usr/ssl/openssl.cnf</filename>).</para>
414     <para>The location of the openssl.cnf file can be overridden by
415     setting the OPENSSL_CONF environment variable before running
416     named.</para>
417     <para>Sample openssl.cnf:</para>
418     <programlisting>
419         openssl_conf = openssl_def
420         [ openssl_def ]
421         engines = engine_section
422         [ engine_section ]
423         pkcs11 = pkcs11_section
424         [ pkcs11_section ]
425         PIN = <replaceable>&lt;PLACE PIN HERE&gt;</replaceable>
426 </programlisting>
427     <para>This will also allow the dnssec-* tools to access the HSM
428     without PIN entry. (The pkcs11-* tools access the HSM directly,
429     not via OpenSSL, so a PIN will still be required to use
430     them.)</para>
431 <!-- 
432 If the PIN is not known, I believe the first time named needs the
433 PIN to open a key, it'll ask you to type in the PIN, which will be
434 a problem because it probably won't be running on a terminal
435 -->
436     <warning>
437       <para>Placing the HSM's PIN in a text file in
438       this manner may reduce the security advantage of using an
439       HSM. Be sure this is what you want to do before configuring
440       OpenSSL in this way.</para>
441     </warning>
442   </sect2>
443   <!-- TODO: what is alternative then for named dynamic re-signing? -->
444   <!-- TODO: what happens if PIN is not known? named will log about it? -->
445 </sect1>