1*324b2298SPaolo Bonzini.. _network_005ftls: 2*324b2298SPaolo Bonzini 3*324b2298SPaolo BonziniTLS setup for network services 4*324b2298SPaolo Bonzini------------------------------ 5*324b2298SPaolo Bonzini 6*324b2298SPaolo BonziniAlmost all network services in QEMU have the ability to use TLS for 7*324b2298SPaolo Bonzinisession data encryption, along with x509 certificates for simple client 8*324b2298SPaolo Bonziniauthentication. What follows is a description of how to generate 9*324b2298SPaolo Bonzinicertificates suitable for usage with QEMU, and applies to the VNC 10*324b2298SPaolo Bonziniserver, character devices with the TCP backend, NBD server and client, 11*324b2298SPaolo Bonziniand migration server and client. 12*324b2298SPaolo Bonzini 13*324b2298SPaolo BonziniAt a high level, QEMU requires certificates and private keys to be 14*324b2298SPaolo Bonziniprovided in PEM format. Aside from the core fields, the certificates 15*324b2298SPaolo Bonzinishould include various extension data sets, including v3 basic 16*324b2298SPaolo Bonziniconstraints data, key purpose, key usage and subject alt name. 17*324b2298SPaolo Bonzini 18*324b2298SPaolo BonziniThe GnuTLS package includes a command called ``certtool`` which can be 19*324b2298SPaolo Bonziniused to easily generate certificates and keys in the required format 20*324b2298SPaolo Bonziniwith expected data present. Alternatively a certificate management 21*324b2298SPaolo Bonziniservice may be used. 22*324b2298SPaolo Bonzini 23*324b2298SPaolo BonziniAt a minimum it is necessary to setup a certificate authority, and issue 24*324b2298SPaolo Bonzinicertificates to each server. If using x509 certificates for 25*324b2298SPaolo Bonziniauthentication, then each client will also need to be issued a 26*324b2298SPaolo Bonzinicertificate. 27*324b2298SPaolo Bonzini 28*324b2298SPaolo BonziniAssuming that the QEMU network services will only ever be exposed to 29*324b2298SPaolo Bonziniclients on a private intranet, there is no need to use a commercial 30*324b2298SPaolo Bonzinicertificate authority to create certificates. A self-signed CA is 31*324b2298SPaolo Bonzinisufficient, and in fact likely to be more secure since it removes the 32*324b2298SPaolo Bonziniability of malicious 3rd parties to trick the CA into mis-issuing certs 33*324b2298SPaolo Bonzinifor impersonating your services. The only likely exception where a 34*324b2298SPaolo Bonzinicommercial CA might be desirable is if enabling the VNC websockets 35*324b2298SPaolo Bonziniserver and exposing it directly to remote browser clients. In such a 36*324b2298SPaolo Bonzinicase it might be useful to use a commercial CA to avoid needing to 37*324b2298SPaolo Bonziniinstall custom CA certs in the web browsers. 38*324b2298SPaolo Bonzini 39*324b2298SPaolo BonziniThe recommendation is for the server to keep its certificates in either 40*324b2298SPaolo Bonzini``/etc/pki/qemu`` or for unprivileged users in ``$HOME/.pki/qemu``. 41*324b2298SPaolo Bonzini 42*324b2298SPaolo Bonzini.. _tls_005fgenerate_005fca: 43*324b2298SPaolo Bonzini 44*324b2298SPaolo BonziniSetup the Certificate Authority 45*324b2298SPaolo Bonzini~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 46*324b2298SPaolo Bonzini 47*324b2298SPaolo BonziniThis step only needs to be performed once per organization / 48*324b2298SPaolo Bonziniorganizational unit. First the CA needs a private key. This key must be 49*324b2298SPaolo Bonzinikept VERY secret and secure. If this key is compromised the entire trust 50*324b2298SPaolo Bonzinichain of the certificates issued with it is lost. 51*324b2298SPaolo Bonzini 52*324b2298SPaolo Bonzini:: 53*324b2298SPaolo Bonzini 54*324b2298SPaolo Bonzini # certtool --generate-privkey > ca-key.pem 55*324b2298SPaolo Bonzini 56*324b2298SPaolo BonziniTo generate a self-signed certificate requires one core piece of 57*324b2298SPaolo Bonziniinformation, the name of the organization. A template file ``ca.info`` 58*324b2298SPaolo Bonzinishould be populated with the desired data to avoid having to deal with 59*324b2298SPaolo Bonziniinteractive prompts from certtool:: 60*324b2298SPaolo Bonzini 61*324b2298SPaolo Bonzini # cat > ca.info <<EOF 62*324b2298SPaolo Bonzini cn = Name of your organization 63*324b2298SPaolo Bonzini ca 64*324b2298SPaolo Bonzini cert_signing_key 65*324b2298SPaolo Bonzini EOF 66*324b2298SPaolo Bonzini # certtool --generate-self-signed \ 67*324b2298SPaolo Bonzini --load-privkey ca-key.pem 68*324b2298SPaolo Bonzini --template ca.info \ 69*324b2298SPaolo Bonzini --outfile ca-cert.pem 70*324b2298SPaolo Bonzini 71*324b2298SPaolo BonziniThe ``ca`` keyword in the template sets the v3 basic constraints 72*324b2298SPaolo Bonziniextension to indicate this certificate is for a CA, while 73*324b2298SPaolo Bonzini``cert_signing_key`` sets the key usage extension to indicate this will 74*324b2298SPaolo Bonzinibe used for signing other keys. The generated ``ca-cert.pem`` file 75*324b2298SPaolo Bonzinishould be copied to all servers and clients wishing to utilize TLS 76*324b2298SPaolo Bonzinisupport in the VNC server. The ``ca-key.pem`` must not be 77*324b2298SPaolo Bonzinidisclosed/copied anywhere except the host responsible for issuing 78*324b2298SPaolo Bonzinicertificates. 79*324b2298SPaolo Bonzini 80*324b2298SPaolo Bonzini.. _tls_005fgenerate_005fserver: 81*324b2298SPaolo Bonzini 82*324b2298SPaolo BonziniIssuing server certificates 83*324b2298SPaolo Bonzini~~~~~~~~~~~~~~~~~~~~~~~~~~~ 84*324b2298SPaolo Bonzini 85*324b2298SPaolo BonziniEach server (or host) needs to be issued with a key and certificate. 86*324b2298SPaolo BonziniWhen connecting the certificate is sent to the client which validates it 87*324b2298SPaolo Bonziniagainst the CA certificate. The core pieces of information for a server 88*324b2298SPaolo Bonzinicertificate are the hostnames and/or IP addresses that will be used by 89*324b2298SPaolo Bonziniclients when connecting. The hostname / IP address that the client 90*324b2298SPaolo Bonzinispecifies when connecting will be validated against the hostname(s) and 91*324b2298SPaolo BonziniIP address(es) recorded in the server certificate, and if no match is 92*324b2298SPaolo Bonzinifound the client will close the connection. 93*324b2298SPaolo Bonzini 94*324b2298SPaolo BonziniThus it is recommended that the server certificate include both the 95*324b2298SPaolo Bonzinifully qualified and unqualified hostnames. If the server will have 96*324b2298SPaolo Bonzinipermanently assigned IP address(es), and clients are likely to use them 97*324b2298SPaolo Bonziniwhen connecting, they may also be included in the certificate. Both IPv4 98*324b2298SPaolo Bonziniand IPv6 addresses are supported. Historically certificates only 99*324b2298SPaolo Bonziniincluded 1 hostname in the ``CN`` field, however, usage of this field 100*324b2298SPaolo Bonzinifor validation is now deprecated. Instead modern TLS clients will 101*324b2298SPaolo Bonzinivalidate against the Subject Alt Name extension data, which allows for 102*324b2298SPaolo Bonzinimultiple entries. In the future usage of the ``CN`` field may be 103*324b2298SPaolo Bonzinidiscontinued entirely, so providing SAN extension data is strongly 104*324b2298SPaolo Bonzinirecommended. 105*324b2298SPaolo Bonzini 106*324b2298SPaolo BonziniOn the host holding the CA, create template files containing the 107*324b2298SPaolo Bonziniinformation for each server, and use it to issue server certificates. 108*324b2298SPaolo Bonzini 109*324b2298SPaolo Bonzini:: 110*324b2298SPaolo Bonzini 111*324b2298SPaolo Bonzini # cat > server-hostNNN.info <<EOF 112*324b2298SPaolo Bonzini organization = Name of your organization 113*324b2298SPaolo Bonzini cn = hostNNN.foo.example.com 114*324b2298SPaolo Bonzini dns_name = hostNNN 115*324b2298SPaolo Bonzini dns_name = hostNNN.foo.example.com 116*324b2298SPaolo Bonzini ip_address = 10.0.1.87 117*324b2298SPaolo Bonzini ip_address = 192.8.0.92 118*324b2298SPaolo Bonzini ip_address = 2620:0:cafe::87 119*324b2298SPaolo Bonzini ip_address = 2001:24::92 120*324b2298SPaolo Bonzini tls_www_server 121*324b2298SPaolo Bonzini encryption_key 122*324b2298SPaolo Bonzini signing_key 123*324b2298SPaolo Bonzini EOF 124*324b2298SPaolo Bonzini # certtool --generate-privkey > server-hostNNN-key.pem 125*324b2298SPaolo Bonzini # certtool --generate-certificate \ 126*324b2298SPaolo Bonzini --load-ca-certificate ca-cert.pem \ 127*324b2298SPaolo Bonzini --load-ca-privkey ca-key.pem \ 128*324b2298SPaolo Bonzini --load-privkey server-hostNNN-key.pem \ 129*324b2298SPaolo Bonzini --template server-hostNNN.info \ 130*324b2298SPaolo Bonzini --outfile server-hostNNN-cert.pem 131*324b2298SPaolo Bonzini 132*324b2298SPaolo BonziniThe ``dns_name`` and ``ip_address`` fields in the template are setting 133*324b2298SPaolo Bonzinithe subject alt name extension data. The ``tls_www_server`` keyword is 134*324b2298SPaolo Bonzinithe key purpose extension to indicate this certificate is intended for 135*324b2298SPaolo Bonziniusage in a web server. Although QEMU network services are not in fact 136*324b2298SPaolo BonziniHTTP servers (except for VNC websockets), setting this key purpose is 137*324b2298SPaolo Bonzinistill recommended. The ``encryption_key`` and ``signing_key`` keyword is 138*324b2298SPaolo Bonzinithe key usage extension to indicate this certificate is intended for 139*324b2298SPaolo Bonziniusage in the data session. 140*324b2298SPaolo Bonzini 141*324b2298SPaolo BonziniThe ``server-hostNNN-key.pem`` and ``server-hostNNN-cert.pem`` files 142*324b2298SPaolo Bonzinishould now be securely copied to the server for which they were 143*324b2298SPaolo Bonzinigenerated, and renamed to ``server-key.pem`` and ``server-cert.pem`` 144*324b2298SPaolo Bonziniwhen added to the ``/etc/pki/qemu`` directory on the target host. The 145*324b2298SPaolo Bonzini``server-key.pem`` file is security sensitive and should be kept 146*324b2298SPaolo Bonziniprotected with file mode 0600 to prevent disclosure. 147*324b2298SPaolo Bonzini 148*324b2298SPaolo Bonzini.. _tls_005fgenerate_005fclient: 149*324b2298SPaolo Bonzini 150*324b2298SPaolo BonziniIssuing client certificates 151*324b2298SPaolo Bonzini~~~~~~~~~~~~~~~~~~~~~~~~~~~ 152*324b2298SPaolo Bonzini 153*324b2298SPaolo BonziniThe QEMU x509 TLS credential setup defaults to enabling client 154*324b2298SPaolo Bonziniverification using certificates, providing a simple authentication 155*324b2298SPaolo Bonzinimechanism. If this default is used, each client also needs to be issued 156*324b2298SPaolo Bonzinia certificate. The client certificate contains enough metadata to 157*324b2298SPaolo Bonziniuniquely identify the client with the scope of the certificate 158*324b2298SPaolo Bonziniauthority. The client certificate would typically include fields for 159*324b2298SPaolo Bonziniorganization, state, city, building, etc. 160*324b2298SPaolo Bonzini 161*324b2298SPaolo BonziniOnce again on the host holding the CA, create template files containing 162*324b2298SPaolo Bonzinithe information for each client, and use it to issue client 163*324b2298SPaolo Bonzinicertificates. 164*324b2298SPaolo Bonzini 165*324b2298SPaolo Bonzini:: 166*324b2298SPaolo Bonzini 167*324b2298SPaolo Bonzini # cat > client-hostNNN.info <<EOF 168*324b2298SPaolo Bonzini country = GB 169*324b2298SPaolo Bonzini state = London 170*324b2298SPaolo Bonzini locality = City Of London 171*324b2298SPaolo Bonzini organization = Name of your organization 172*324b2298SPaolo Bonzini cn = hostNNN.foo.example.com 173*324b2298SPaolo Bonzini tls_www_client 174*324b2298SPaolo Bonzini encryption_key 175*324b2298SPaolo Bonzini signing_key 176*324b2298SPaolo Bonzini EOF 177*324b2298SPaolo Bonzini # certtool --generate-privkey > client-hostNNN-key.pem 178*324b2298SPaolo Bonzini # certtool --generate-certificate \ 179*324b2298SPaolo Bonzini --load-ca-certificate ca-cert.pem \ 180*324b2298SPaolo Bonzini --load-ca-privkey ca-key.pem \ 181*324b2298SPaolo Bonzini --load-privkey client-hostNNN-key.pem \ 182*324b2298SPaolo Bonzini --template client-hostNNN.info \ 183*324b2298SPaolo Bonzini --outfile client-hostNNN-cert.pem 184*324b2298SPaolo Bonzini 185*324b2298SPaolo BonziniThe subject alt name extension data is not required for clients, so the 186*324b2298SPaolo Bonzinithe ``dns_name`` and ``ip_address`` fields are not included. The 187*324b2298SPaolo Bonzini``tls_www_client`` keyword is the key purpose extension to indicate this 188*324b2298SPaolo Bonzinicertificate is intended for usage in a web client. Although QEMU network 189*324b2298SPaolo Bonziniclients are not in fact HTTP clients, setting this key purpose is still 190*324b2298SPaolo Bonzinirecommended. The ``encryption_key`` and ``signing_key`` keyword is the 191*324b2298SPaolo Bonzinikey usage extension to indicate this certificate is intended for usage 192*324b2298SPaolo Bonziniin the data session. 193*324b2298SPaolo Bonzini 194*324b2298SPaolo BonziniThe ``client-hostNNN-key.pem`` and ``client-hostNNN-cert.pem`` files 195*324b2298SPaolo Bonzinishould now be securely copied to the client for which they were 196*324b2298SPaolo Bonzinigenerated, and renamed to ``client-key.pem`` and ``client-cert.pem`` 197*324b2298SPaolo Bonziniwhen added to the ``/etc/pki/qemu`` directory on the target host. The 198*324b2298SPaolo Bonzini``client-key.pem`` file is security sensitive and should be kept 199*324b2298SPaolo Bonziniprotected with file mode 0600 to prevent disclosure. 200*324b2298SPaolo Bonzini 201*324b2298SPaolo BonziniIf a single host is going to be using TLS in both a client and server 202*324b2298SPaolo Bonzinirole, it is possible to create a single certificate to cover both roles. 203*324b2298SPaolo BonziniThis would be quite common for the migration and NBD services, where a 204*324b2298SPaolo BonziniQEMU process will be started by accepting a TLS protected incoming 205*324b2298SPaolo Bonzinimigration, and later itself be migrated out to another host. To generate 206*324b2298SPaolo Bonzinia single certificate, simply include the template data from both the 207*324b2298SPaolo Bonziniclient and server instructions in one. 208*324b2298SPaolo Bonzini 209*324b2298SPaolo Bonzini:: 210*324b2298SPaolo Bonzini 211*324b2298SPaolo Bonzini # cat > both-hostNNN.info <<EOF 212*324b2298SPaolo Bonzini country = GB 213*324b2298SPaolo Bonzini state = London 214*324b2298SPaolo Bonzini locality = City Of London 215*324b2298SPaolo Bonzini organization = Name of your organization 216*324b2298SPaolo Bonzini cn = hostNNN.foo.example.com 217*324b2298SPaolo Bonzini dns_name = hostNNN 218*324b2298SPaolo Bonzini dns_name = hostNNN.foo.example.com 219*324b2298SPaolo Bonzini ip_address = 10.0.1.87 220*324b2298SPaolo Bonzini ip_address = 192.8.0.92 221*324b2298SPaolo Bonzini ip_address = 2620:0:cafe::87 222*324b2298SPaolo Bonzini ip_address = 2001:24::92 223*324b2298SPaolo Bonzini tls_www_server 224*324b2298SPaolo Bonzini tls_www_client 225*324b2298SPaolo Bonzini encryption_key 226*324b2298SPaolo Bonzini signing_key 227*324b2298SPaolo Bonzini EOF 228*324b2298SPaolo Bonzini # certtool --generate-privkey > both-hostNNN-key.pem 229*324b2298SPaolo Bonzini # certtool --generate-certificate \ 230*324b2298SPaolo Bonzini --load-ca-certificate ca-cert.pem \ 231*324b2298SPaolo Bonzini --load-ca-privkey ca-key.pem \ 232*324b2298SPaolo Bonzini --load-privkey both-hostNNN-key.pem \ 233*324b2298SPaolo Bonzini --template both-hostNNN.info \ 234*324b2298SPaolo Bonzini --outfile both-hostNNN-cert.pem 235*324b2298SPaolo Bonzini 236*324b2298SPaolo BonziniWhen copying the PEM files to the target host, save them twice, once as 237*324b2298SPaolo Bonzini``server-cert.pem`` and ``server-key.pem``, and again as 238*324b2298SPaolo Bonzini``client-cert.pem`` and ``client-key.pem``. 239*324b2298SPaolo Bonzini 240*324b2298SPaolo Bonzini.. _tls_005fcreds_005fsetup: 241*324b2298SPaolo Bonzini 242*324b2298SPaolo BonziniTLS x509 credential configuration 243*324b2298SPaolo Bonzini~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 244*324b2298SPaolo Bonzini 245*324b2298SPaolo BonziniQEMU has a standard mechanism for loading x509 credentials that will be 246*324b2298SPaolo Bonziniused for network services and clients. It requires specifying the 247*324b2298SPaolo Bonzini``tls-creds-x509`` class name to the ``--object`` command line argument 248*324b2298SPaolo Bonzinifor the system emulators. Each set of credentials loaded should be given 249*324b2298SPaolo Bonzinia unique string identifier via the ``id`` parameter. A single set of TLS 250*324b2298SPaolo Bonzinicredentials can be used for multiple network backends, so VNC, 251*324b2298SPaolo Bonzinimigration, NBD, character devices can all share the same credentials. 252*324b2298SPaolo BonziniNote, however, that credentials for use in a client endpoint must be 253*324b2298SPaolo Bonziniloaded separately from those used in a server endpoint. 254*324b2298SPaolo Bonzini 255*324b2298SPaolo BonziniWhen specifying the object, the ``dir`` parameters specifies which 256*324b2298SPaolo Bonzinidirectory contains the credential files. This directory is expected to 257*324b2298SPaolo Bonzinicontain files with the names mentioned previously, ``ca-cert.pem``, 258*324b2298SPaolo Bonzini``server-key.pem``, ``server-cert.pem``, ``client-key.pem`` and 259*324b2298SPaolo Bonzini``client-cert.pem`` as appropriate. It is also possible to include a set 260*324b2298SPaolo Bonziniof pre-generated Diffie-Hellman (DH) parameters in a file 261*324b2298SPaolo Bonzini``dh-params.pem``, which can be created using the 262*324b2298SPaolo Bonzini``certtool --generate-dh-params`` command. If omitted, QEMU will 263*324b2298SPaolo Bonzinidynamically generate DH parameters when loading the credentials. 264*324b2298SPaolo Bonzini 265*324b2298SPaolo BonziniThe ``endpoint`` parameter indicates whether the credentials will be 266*324b2298SPaolo Bonziniused for a network client or server, and determines which PEM files are 267*324b2298SPaolo Bonziniloaded. 268*324b2298SPaolo Bonzini 269*324b2298SPaolo BonziniThe ``verify`` parameter determines whether x509 certificate validation 270*324b2298SPaolo Bonzinishould be performed. This defaults to enabled, meaning clients will 271*324b2298SPaolo Bonzinialways validate the server hostname against the certificate subject alt 272*324b2298SPaolo Bonzininame fields and/or CN field. It also means that servers will request 273*324b2298SPaolo Bonzinithat clients provide a certificate and validate them. Verification 274*324b2298SPaolo Bonzinishould never be turned off for client endpoints, however, it may be 275*324b2298SPaolo Bonziniturned off for server endpoints if an alternative mechanism is used to 276*324b2298SPaolo Bonziniauthenticate clients. For example, the VNC server can use SASL to 277*324b2298SPaolo Bonziniauthenticate clients instead. 278*324b2298SPaolo Bonzini 279*324b2298SPaolo BonziniTo load server credentials with client certificate validation enabled 280*324b2298SPaolo Bonzini 281*324b2298SPaolo Bonzini.. parsed-literal:: 282*324b2298SPaolo Bonzini 283*324b2298SPaolo Bonzini |qemu_system| -object tls-creds-x509,id=tls0,dir=/etc/pki/qemu,endpoint=server 284*324b2298SPaolo Bonzini 285*324b2298SPaolo Bonziniwhile to load client credentials use 286*324b2298SPaolo Bonzini 287*324b2298SPaolo Bonzini.. parsed-literal:: 288*324b2298SPaolo Bonzini 289*324b2298SPaolo Bonzini |qemu_system| -object tls-creds-x509,id=tls0,dir=/etc/pki/qemu,endpoint=client 290*324b2298SPaolo Bonzini 291*324b2298SPaolo BonziniNetwork services which support TLS will all have a ``tls-creds`` 292*324b2298SPaolo Bonziniparameter which expects the ID of the TLS credentials object. For 293*324b2298SPaolo Bonziniexample with VNC: 294*324b2298SPaolo Bonzini 295*324b2298SPaolo Bonzini.. parsed-literal:: 296*324b2298SPaolo Bonzini 297*324b2298SPaolo Bonzini |qemu_system| -vnc 0.0.0.0:0,tls-creds=tls0 298*324b2298SPaolo Bonzini 299*324b2298SPaolo Bonzini.. _tls_005fpsk: 300*324b2298SPaolo Bonzini 301*324b2298SPaolo BonziniTLS Pre-Shared Keys (PSK) 302*324b2298SPaolo Bonzini~~~~~~~~~~~~~~~~~~~~~~~~~ 303*324b2298SPaolo Bonzini 304*324b2298SPaolo BonziniInstead of using certificates, you may also use TLS Pre-Shared Keys 305*324b2298SPaolo Bonzini(TLS-PSK). This can be simpler to set up than certificates but is less 306*324b2298SPaolo Bonziniscalable. 307*324b2298SPaolo Bonzini 308*324b2298SPaolo BonziniUse the GnuTLS ``psktool`` program to generate a ``keys.psk`` file 309*324b2298SPaolo Bonzinicontaining one or more usernames and random keys:: 310*324b2298SPaolo Bonzini 311*324b2298SPaolo Bonzini mkdir -m 0700 /tmp/keys 312*324b2298SPaolo Bonzini psktool -u rich -p /tmp/keys/keys.psk 313*324b2298SPaolo Bonzini 314*324b2298SPaolo BonziniTLS-enabled servers such as qemu-nbd can use this directory like so:: 315*324b2298SPaolo Bonzini 316*324b2298SPaolo Bonzini qemu-nbd \ 317*324b2298SPaolo Bonzini -t -x / \ 318*324b2298SPaolo Bonzini --object tls-creds-psk,id=tls0,endpoint=server,dir=/tmp/keys \ 319*324b2298SPaolo Bonzini --tls-creds tls0 \ 320*324b2298SPaolo Bonzini image.qcow2 321*324b2298SPaolo Bonzini 322*324b2298SPaolo BonziniWhen connecting from a qemu-based client you must specify the directory 323*324b2298SPaolo Bonzinicontaining ``keys.psk`` and an optional username (defaults to "qemu"):: 324*324b2298SPaolo Bonzini 325*324b2298SPaolo Bonzini qemu-img info \ 326*324b2298SPaolo Bonzini --object tls-creds-psk,id=tls0,dir=/tmp/keys,username=rich,endpoint=client \ 327*324b2298SPaolo Bonzini --image-opts \ 328*324b2298SPaolo Bonzini file.driver=nbd,file.host=localhost,file.port=10809,file.tls-creds=tls0,file.export=/ 329