Update Red Hat Copyright Notices
[nbdkit.git] / docs / nbdkit-tls.pod
blob70311be1d300093b3800f99f67609b468794ad38
1 =head1 NAME
3 nbdkit-tls - authentication and encryption of NBD connections
4 (sometimes called "SSL")
6 =head1 SYNOPSIS
8  nbdkit [--tls=off|on|require]
9         [--tls-certificates=/path/to/certificates]
10         [--tls-psk=/path/to/pskfile]
11         [--tls-verify-peer]
12         PLUGIN [...]
14 =head1 DESCRIPTION
16 TLS (authentication and encryption, sometimes incorrectly called
17 "SSL") is supported if nbdkit was compiled with GnuTLS.  This allows
18 the server to verify that the client is allowed access, and to encrypt
19 the contents of the protocol in transit over the network.
21 TLS can be disabled or enabled by specifying either I<--tls=off> or
22 I<--tls=on>.  With I<--tls=off>, if a client tries to use TLS to
23 connect, it will be rejected by the server (in other words, as if the
24 server doesn't support TLS).
26 I<--tls=on> means that the client may choose to connect either with or
27 without TLS.
29 I<--tls=require> enables TLS B<and> rejects all non-TLS connection
30 attempts.  This prevents downgrade attacks where a malicious proxy
31 pretends not to support TLS in order to force either the client or
32 server to communicate in plaintext.
34 =head2 Example
36 If certificates have been set up correctly then you should be able to
37 start a TLS server by doing:
39  nbdkit --tls=require memory 1G
41 and connect to it by doing:
43  nbdinfo nbds://localhost
45 If certificates are in a non-standard directory and you have
46 S<libnbd E<ge> 1.10>:
48  nbdkit --tls=require --tls-certificates=/certs memory 1G
49  nbdinfo nbds://localhost?tls-certificates=/certs
51 =head2 TLS with X.509 certificates
53 When nbdkit starts up, it loads TLS certificates from some built-in
54 paths, or from the directory specified by the I<--tls-certificates>
55 option.
57 In this directory, nbdkit expects to find several files:
59 =over 4
61 =item F<ca-cert.pem>
63 The Certificate Authority certificate.
65 =item F<server-cert.pem>
67 The server certificate.
69 =item F<server-key.pem>
71 The server private key.
73 =item F<ca-crl.pem>
75 (Optional) The certificate revocation list.
77 =back
79 =head3 Setting up the Certificate Authority
81 This step only needs to be done once per organization.  It may be that
82 your organization already has a CA.
84  $ certtool --generate-privkey > ca-key.pem
85  $ chmod 0600 ca-key.pem
87 The F<ca-key.pem> file is the CA private key and is I<extremely>
88 sensitive data.  With possession of this key, anyone can create
89 certificates pretending to be your organization!
91 To create the CA certificate file:
93  $ cat > ca.info <<EOF
94  cn = Name of your organization
95  ca
96  cert_signing_key
97  EOF
98  $ certtool --generate-self-signed \
99             --load-privkey ca-key.pem \
100             --template ca.info \
101             --outfile ca-cert.pem
103 =head3 Issuing a server certificate for the nbdkit server
105 Each nbdkit server (or host) needs a secret key and certificate.
107  $ certtool --generate-privkey > server-key.pem
108  $ chmod 0600 server-key.pem
110 The server key file is sensitive.  Setting the mode to C<0600> helps
111 to prevent other users on the same machine from reading it.
113 The common name (C<cn> below) field must be the fully qualified
114 hostname that the client connects to.  However most clients and
115 servers including nbdkit support the Subject Alternative Name
116 extension (S<RFC 2818>) which uses the C<dns_name> and C<ip_address>
117 fields and deprecates C<cn>.
119  $ cat > server.info <<EOF
120  organization = Name of your organization
121  cn = nbd-server.example.com
122  dns_name = nbd-server
123  dns_name = nbd-server.example.com
124  ip_address = 10.0.1.2
125  ip_address = 2001:24::92
126  tls_www_server
127  encryption_key
128  signing_key
129  EOF
130  $ certtool --generate-certificate \
131             --load-ca-certificate ca-cert.pem \
132             --load-ca-privkey ca-key.pem \
133             --load-privkey server-key.pem \
134             --template server.info \
135             --outfile server-cert.pem
137 =head3 Issuing and checking client certificates
139 B<Note:> You don't need to create client certificates unless you want
140 to check and limit which clients can connect to nbdkit.  B<nbdkit does
141 not check client certificates> unless you specify the
142 I<--tls-verify-peer> option on the command line.  There are other
143 methods for limiting access to nbdkit including
144 L<nbdkit-ip-filter(1)>.
146 For each client you should generate a private key and a client
147 certificate:
149  $ certtool --generate-privkey > client-key.pem
150  $ chmod 0600 client-key.pem
152 The client key file is sensitive.
154 The client DNS name (C<cn> below) is the client's name that nbdkit
155 sees and checks.
157  $ cat > client.info <<EOF
158  country = US
159  state = New York
160  locality = New York
161  organization = Name of your organization
162  cn = client.example.com
163  tls_www_client
164  encryption_key
165  signing_key
166  EOF
167  $ certtool --generate-certificate \
168             --load-ca-certificate ca-cert.pem \
169             --load-ca-privkey ca-key.pem \
170             --load-privkey client-key.pem \
171             --template client.info \
172             --outfile client-cert.pem
174 Client certificates do I<not> need to be present anywhere on the
175 nbdkit host.  You don't need to copy them into nbdkit's TLS
176 certificates directory.  The security comes from the fact that the
177 client must present a client certificate signed by the Certificate
178 Authority, and nbdkit can check this because it has the F<ca-cert.pem>
179 file.
181 To enable checking of client certificates, specify the
182 I<--tls-verify-peer> option on the command line.  Clients which don't
183 present a valid certificate (eg. not signed, incorrect signature) are
184 denied.  Also denied are clients which present a valid certificate
185 signed by another CA.  Also denied are clients with certificates added
186 to the certificate revocation list (F<ca-crl.pem>).
188 =head2 Connecting nbd-client to nbdkit with TLS certificates
190 With the TLS certificates files generated above in the current
191 directory (C<.>) you can use:
193  nbdkit --tls=require --tls-certificates=. --tls-verify-peer memory 1G
195  nbd-client /dev/nbd0 \
196            -cacertfile ca-cert.pem \
197            -certfile client-cert.pem \
198            -keyfile client-key.pem
200 I<--tls-verify-peer> is only required if you want to check the client
201 certificate.  If you want to allow any client to connect then you can
202 omit it.
204 =head2 TLS with Pre-Shared Keys (PSK)
206 As a simpler alternative to TLS certificates, you may used pre-shared
207 keys to authenticate clients.
209 Create a PSK file containing one or more C<username:key> pairs.  It is
210 easiest to use L<psktool(1)> for this:
212  mkdir -m 0700 /tmp/keys
213  psktool -u alice -p /tmp/keys/keys.psk
215 The PSK file contains the hex-encoded random keys in plaintext.  Any
216 client which can read this file will be able to connect to the server.
218 Use the nbdkit I<--tls-psk> option to start the server:
220  nbdkit --tls=require --tls-psk=/tmp/keys/keys.psk file disk.img
222 This option overrides X.509 certificate authentication.
224 Clients must supply one of the usernames in the PSK file and the
225 corresponding key in order to connect.
227 An example of connecting using L<nbdinfo(1)> using an NBD URI is:
229  nbdinfo 'nbds://alice@localhost?tls-psk-file=/tmp/keys/keys.psk'
231 An example of connecting using L<qemu-img(1)> is:
233  qemu-img info \
234    --object tls-creds-psk,id=tls0,dir=/tmp/keys,username=alice,endpoint=client \
235    --image-opts \
236    file.driver=nbd,file.host=localhost,file.port=10809,file.tls-creds=tls0,file.export=/
238 =head2 Default TLS behaviour
240 If nbdkit was compiled without GnuTLS support, then TLS is disabled
241 and TLS connections will be rejected (as if I<--tls=off> was specified
242 on the command line).  Also it is impossible to turn on TLS in this
243 scenario.  You can tell if nbdkit was compiled without GnuTLS support
244 because C<nbdkit --dump-config> will contain C<tls=no>.
246 If TLS certificates cannot be loaded either from the built-in path or
247 from the directory specified by I<--tls-certificates>, then TLS
248 defaults to disabled.  Turning TLS on will give a warning
249 (I<--tls=on>) or error (I<--tls=require>) about the missing
250 certificates.
252 If TLS certificates can be loaded from the built-in path or from the
253 I<--tls-certificates> directory, then TLS will by default be enabled
254 (like I<--tls=on>), but it is not required.  Clients can choose
255 whether or not to use TLS and whether or not to present certificates.
257 TLS client certificates are I<not> checked by default unless you
258 specify I<--tls-verify-peer>.
260 If the I<--tls-psk> option is used then TLS is enabled (but I<not>
261 required).  To ensure that all clients are authorized you must use
262 I<--tls=require>.
264 Each of these defaults is insecure to some extent (including
265 I<--tls=on> which could be subject to a downgrade attack), so if you
266 expect TLS then it is best to specify the I<--tls> option that you
267 require, and if you want to check client certificates, specify the
268 I<--tls-verify-peer> option.
270 =head2 Controlling TLS fallback to plaintext
272 When I<--tls=on> is used, the connection can fall back to plaintext.
273 You can use L<nbdkit-tls-fallback-filter(1)> to provide safe fallback
274 content to plaintext connections.  With this filter the underlying
275 plugin content is only served on secure connections.
277 Alternatively a plugin may wish to serve different content depending
278 on whether the client is using TLS.  The function C<nbdkit_is_tls()>
279 can be used during the C<.open> callback for that purpose.
281 =head2 NBD URIs for TLS
283 Tools such L<nbdcopy(1)>, L<nbdinfo(1)> and L<nbdsh(1)> (from
284 L<libnbd(3)>) allow you to use C<nbds://> or C<nbds+unix://> URIs to
285 connect to nbdkit servers using TLS.
287 The syntax is fully documented in the NBD URI specification:
288 L<https://github.com/NetworkBlockDevice/nbd/blob/master/doc/uri.md>.
289 This section contains an outline.  You can also find further examples
290 in L<nbd_connect_uri(3)>.
292 =over 4
294 =item B<nbds://>example.com
296 Connect over TCP with TLS, to C<example.com> port 10809.  If the
297 server does not support TLS then this will fail.
299 =item B<nbds+unix:///?socket=>SOCKET
301 As above, but connect over a Unix domain socket called F<SOCKET>.
303 =item B<nbds+unix:///?socket=>SOCKETB<&tls-certificates=>DIR
305 As above, but specify the directory F<DIR> containing TLS certificates
306 (used by the client to verify the server, and to present client
307 authentication to the server).  Note this requires S<libnbd E<ge> 1.10>.
309 =item B<nbds+unix:///?socket=>SOCKETB<&tls-psk-file=>FILENAME
311 As above, but use TLS with Pre-Shared Keys (PSK), stored in the
312 secrets file F<FILENAME>.
314 =item B<nbds+unix://>aliceB<@/?socket=>SOCKETB<&tls-psk-file=>FILENAME
316 As above, but use C<alice> as the username.
318 =back
320 =head2 Default location of certificates
322 Without I<--tls-certificates> nbdkit and libnbd look in several
323 locations for certificates.
325 If nbdkit is started as a non-root user (note this does not include
326 use of the I<-u> or I<-g> options), nbdkit looks in each of these
327 paths in turn:
329  $HOME/.pki/nbdkit/
330  $HOME/.config/pki/nbdkit/
332 If nbdkit is started as root:
334  $sysconfdir/pki/nbdkit/
336 where $sysconfdir is set when nbdkit is compiled, usually F</etc>.
337 (Use C<nbdkit --dump-config> and look at the
338 C<root_tls_certificates_dir> setting to get the actual directory built
339 into the binary.)
341 In libnbd the paths are different.  For non-root:
343  $HOME/.pki/libnbd/
344  $HOME/.config/pki/libnbd/
346 For root:
348  $sysconfdir/pki/libnbd/
350 In nbdkit you can override these directories by using
351 I<--tls-certificates=/path/to/certificates>.
353 In libnbd you can use L<nbd_set_tls_certificates(3)>.
354 In S<libnbd E<ge> 1.10> you can append
355 C<&tls-certificates=/path/to/certificates> to URIs.
357 =head2 Choice of TLS algorithms
359 TLS has a bewildering choice of algorithms that can be used.  To
360 enable you to choose a default set of algorithms, there is a configure
361 setting I<--with-tls-priority>.  This defaults to C<NORMAL> which, to
362 quote the GnuTLS documentation:
364 =over 4
366 "C<NORMAL> means all C<secure> ciphersuites.  The 256-bit ciphers are
367 included as a fallback only.  The ciphers are sorted by security
368 margin."
370 =back
372 You could also set the TLS priority so that it can be configured from
373 a file at runtime:
375  ./configure --with-tls-priority=@SYSTEM
377 means use the policy from F</etc/crypto-policies/config>.
379  ./configure --with-tls-priority=@NBDKIT,SYSTEM
381 means use the policy from
382 F</etc/crypto-policies/local.d/nbdkit.config> and fall back to
383 F</etc/crypto-policies/config> if the first file does not exist.
385 More information can be found in L<gnutls_priority_init(3)>.
387 =head1 SEE ALSO
389 L<nbdkit(1)>,
390 L<nbdkit-luks-filter(1)>,
391 L<nbdkit-tls-fallback-filter(1)>,
392 L<nbdcopy(1)>,
393 L<nbdfuse(1)>,
394 L<nbdinfo(1)>,
395 L<nbdsh(1)>,
396 L<nbd_connect_uri(3)>,
397 L<nbd_set_tls(3)>,
398 L<nbd_set_tls_certificates(3)>,
399 L<gnutls_priority_init(3)>,
400 L<psktool(1)>,
401 L<https://github.com/NetworkBlockDevice/nbd/blob/master/doc/proto.md>,
402 L<https://github.com/NetworkBlockDevice/nbd/blob/master/doc/uri.md>,
403 L<https://nbd.sourceforge.io/>.
405 =head1 AUTHORS
407 Eric Blake
409 Richard W.M. Jones
411 Pino Toscano
413 =head1 COPYRIGHT
415 Copyright Red Hat