tls.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. .. _network_005ftls:
  2. TLS setup for network services
  3. ------------------------------
  4. Almost all network services in QEMU have the ability to use TLS for
  5. session data encryption, along with x509 certificates for simple client
  6. authentication. What follows is a description of how to generate
  7. certificates suitable for usage with QEMU, and applies to the VNC
  8. server, character devices with the TCP backend, NBD server and client,
  9. and migration server and client.
  10. At a high level, QEMU requires certificates and private keys to be
  11. provided in PEM format. Aside from the core fields, the certificates
  12. should include various extension data sets, including v3 basic
  13. constraints data, key purpose, key usage and subject alt name.
  14. The GnuTLS package includes a command called ``certtool`` which can be
  15. used to easily generate certificates and keys in the required format
  16. with expected data present. Alternatively a certificate management
  17. service may be used.
  18. At a minimum it is necessary to setup a certificate authority, and issue
  19. certificates to each server. If using x509 certificates for
  20. authentication, then each client will also need to be issued a
  21. certificate.
  22. Assuming that the QEMU network services will only ever be exposed to
  23. clients on a private intranet, there is no need to use a commercial
  24. certificate authority to create certificates. A self-signed CA is
  25. sufficient, and in fact likely to be more secure since it removes the
  26. ability of malicious 3rd parties to trick the CA into mis-issuing certs
  27. for impersonating your services. The only likely exception where a
  28. commercial CA might be desirable is if enabling the VNC websockets
  29. server and exposing it directly to remote browser clients. In such a
  30. case it might be useful to use a commercial CA to avoid needing to
  31. install custom CA certs in the web browsers.
  32. The recommendation is for the server to keep its certificates in either
  33. ``/etc/pki/qemu`` or for unprivileged users in ``$HOME/.pki/qemu``.
  34. .. _tls_005fgenerate_005fca:
  35. Setup the Certificate Authority
  36. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  37. This step only needs to be performed once per organization /
  38. organizational unit. First the CA needs a private key. This key must be
  39. kept VERY secret and secure. If this key is compromised the entire trust
  40. chain of the certificates issued with it is lost.
  41. ::
  42. # certtool --generate-privkey > ca-key.pem
  43. To generate a self-signed certificate requires one core piece of
  44. information, the name of the organization. A template file ``ca.info``
  45. should be populated with the desired data to avoid having to deal with
  46. interactive prompts from certtool::
  47. # cat > ca.info <<EOF
  48. cn = Name of your organization
  49. ca
  50. cert_signing_key
  51. EOF
  52. # certtool --generate-self-signed \
  53. --load-privkey ca-key.pem \
  54. --template ca.info \
  55. --outfile ca-cert.pem
  56. The ``ca`` keyword in the template sets the v3 basic constraints
  57. extension to indicate this certificate is for a CA, while
  58. ``cert_signing_key`` sets the key usage extension to indicate this will
  59. be used for signing other keys. The generated ``ca-cert.pem`` file
  60. should be copied to all servers and clients wishing to utilize TLS
  61. support in the VNC server. The ``ca-key.pem`` must not be
  62. disclosed/copied anywhere except the host responsible for issuing
  63. certificates.
  64. .. _tls_005fgenerate_005fserver:
  65. Issuing server certificates
  66. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  67. Each server (or host) needs to be issued with a key and certificate.
  68. When connecting the certificate is sent to the client which validates it
  69. against the CA certificate. The core pieces of information for a server
  70. certificate are the hostnames and/or IP addresses that will be used by
  71. clients when connecting. The hostname / IP address that the client
  72. specifies when connecting will be validated against the hostname(s) and
  73. IP address(es) recorded in the server certificate, and if no match is
  74. found the client will close the connection.
  75. Thus it is recommended that the server certificate include both the
  76. fully qualified and unqualified hostnames. If the server will have
  77. permanently assigned IP address(es), and clients are likely to use them
  78. when connecting, they may also be included in the certificate. Both IPv4
  79. and IPv6 addresses are supported. Historically certificates only
  80. included 1 hostname in the ``CN`` field, however, usage of this field
  81. for validation is now deprecated. Instead modern TLS clients will
  82. validate against the Subject Alt Name extension data, which allows for
  83. multiple entries. In the future usage of the ``CN`` field may be
  84. discontinued entirely, so providing SAN extension data is strongly
  85. recommended.
  86. On the host holding the CA, create template files containing the
  87. information for each server, and use it to issue server certificates.
  88. ::
  89. # cat > server-hostNNN.info <<EOF
  90. organization = Name of your organization
  91. cn = hostNNN.foo.example.com
  92. dns_name = hostNNN
  93. dns_name = hostNNN.foo.example.com
  94. ip_address = 10.0.1.87
  95. ip_address = 192.8.0.92
  96. ip_address = 2620:0:cafe::87
  97. ip_address = 2001:24::92
  98. tls_www_server
  99. encryption_key
  100. signing_key
  101. EOF
  102. # certtool --generate-privkey > server-hostNNN-key.pem
  103. # certtool --generate-certificate \
  104. --load-ca-certificate ca-cert.pem \
  105. --load-ca-privkey ca-key.pem \
  106. --load-privkey server-hostNNN-key.pem \
  107. --template server-hostNNN.info \
  108. --outfile server-hostNNN-cert.pem
  109. The ``dns_name`` and ``ip_address`` fields in the template are setting
  110. the subject alt name extension data. The ``tls_www_server`` keyword is
  111. the key purpose extension to indicate this certificate is intended for
  112. usage in a web server. Although QEMU network services are not in fact
  113. HTTP servers (except for VNC websockets), setting this key purpose is
  114. still recommended. The ``encryption_key`` and ``signing_key`` keyword is
  115. the key usage extension to indicate this certificate is intended for
  116. usage in the data session.
  117. The ``server-hostNNN-key.pem`` and ``server-hostNNN-cert.pem`` files
  118. should now be securely copied to the server for which they were
  119. generated, and renamed to ``server-key.pem`` and ``server-cert.pem``
  120. when added to the ``/etc/pki/qemu`` directory on the target host. The
  121. ``server-key.pem`` file is security sensitive and should be kept
  122. protected with file mode 0600 to prevent disclosure.
  123. .. _tls_005fgenerate_005fclient:
  124. Issuing client certificates
  125. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  126. The QEMU x509 TLS credential setup defaults to enabling client
  127. verification using certificates, providing a simple authentication
  128. mechanism. If this default is used, each client also needs to be issued
  129. a certificate. The client certificate contains enough metadata to
  130. uniquely identify the client with the scope of the certificate
  131. authority. The client certificate would typically include fields for
  132. organization, state, city, building, etc.
  133. Once again on the host holding the CA, create template files containing
  134. the information for each client, and use it to issue client
  135. certificates.
  136. ::
  137. # cat > client-hostNNN.info <<EOF
  138. country = GB
  139. state = London
  140. locality = City Of London
  141. organization = Name of your organization
  142. cn = hostNNN.foo.example.com
  143. tls_www_client
  144. encryption_key
  145. signing_key
  146. EOF
  147. # certtool --generate-privkey > client-hostNNN-key.pem
  148. # certtool --generate-certificate \
  149. --load-ca-certificate ca-cert.pem \
  150. --load-ca-privkey ca-key.pem \
  151. --load-privkey client-hostNNN-key.pem \
  152. --template client-hostNNN.info \
  153. --outfile client-hostNNN-cert.pem
  154. The subject alt name extension data is not required for clients, so
  155. the ``dns_name`` and ``ip_address`` fields are not included. The
  156. ``tls_www_client`` keyword is the key purpose extension to indicate this
  157. certificate is intended for usage in a web client. Although QEMU network
  158. clients are not in fact HTTP clients, setting this key purpose is still
  159. recommended. The ``encryption_key`` and ``signing_key`` keyword is the
  160. key usage extension to indicate this certificate is intended for usage
  161. in the data session.
  162. The ``client-hostNNN-key.pem`` and ``client-hostNNN-cert.pem`` files
  163. should now be securely copied to the client for which they were
  164. generated, and renamed to ``client-key.pem`` and ``client-cert.pem``
  165. when added to the ``/etc/pki/qemu`` directory on the target host. The
  166. ``client-key.pem`` file is security sensitive and should be kept
  167. protected with file mode 0600 to prevent disclosure.
  168. If a single host is going to be using TLS in both a client and server
  169. role, it is possible to create a single certificate to cover both roles.
  170. This would be quite common for the migration and NBD services, where a
  171. QEMU process will be started by accepting a TLS protected incoming
  172. migration, and later itself be migrated out to another host. To generate
  173. a single certificate, simply include the template data from both the
  174. client and server instructions in one.
  175. ::
  176. # cat > both-hostNNN.info <<EOF
  177. country = GB
  178. state = London
  179. locality = City Of London
  180. organization = Name of your organization
  181. cn = hostNNN.foo.example.com
  182. dns_name = hostNNN
  183. dns_name = hostNNN.foo.example.com
  184. ip_address = 10.0.1.87
  185. ip_address = 192.8.0.92
  186. ip_address = 2620:0:cafe::87
  187. ip_address = 2001:24::92
  188. tls_www_server
  189. tls_www_client
  190. encryption_key
  191. signing_key
  192. EOF
  193. # certtool --generate-privkey > both-hostNNN-key.pem
  194. # certtool --generate-certificate \
  195. --load-ca-certificate ca-cert.pem \
  196. --load-ca-privkey ca-key.pem \
  197. --load-privkey both-hostNNN-key.pem \
  198. --template both-hostNNN.info \
  199. --outfile both-hostNNN-cert.pem
  200. When copying the PEM files to the target host, save them twice, once as
  201. ``server-cert.pem`` and ``server-key.pem``, and again as
  202. ``client-cert.pem`` and ``client-key.pem``.
  203. .. _tls_005fcreds_005fsetup:
  204. TLS x509 credential configuration
  205. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  206. QEMU has a standard mechanism for loading x509 credentials that will be
  207. used for network services and clients. It requires specifying the
  208. ``tls-creds-x509`` class name to the ``--object`` command line argument
  209. for the system emulators. Each set of credentials loaded should be given
  210. a unique string identifier via the ``id`` parameter. A single set of TLS
  211. credentials can be used for multiple network backends, so VNC,
  212. migration, NBD, character devices can all share the same credentials.
  213. Note, however, that credentials for use in a client endpoint must be
  214. loaded separately from those used in a server endpoint.
  215. When specifying the object, the ``dir`` parameters specifies which
  216. directory contains the credential files. This directory is expected to
  217. contain files with the names mentioned previously, ``ca-cert.pem``,
  218. ``server-key.pem``, ``server-cert.pem``, ``client-key.pem`` and
  219. ``client-cert.pem`` as appropriate. It is also possible to include a set
  220. of pre-generated Diffie-Hellman (DH) parameters in a file
  221. ``dh-params.pem``, which can be created using the
  222. ``certtool --generate-dh-params`` command. If omitted, QEMU will
  223. dynamically generate DH parameters when loading the credentials.
  224. The ``endpoint`` parameter indicates whether the credentials will be
  225. used for a network client or server, and determines which PEM files are
  226. loaded.
  227. The ``verify`` parameter determines whether x509 certificate validation
  228. should be performed. This defaults to enabled, meaning clients will
  229. always validate the server hostname against the certificate subject alt
  230. name fields and/or CN field. It also means that servers will request
  231. that clients provide a certificate and validate them. Verification
  232. should never be turned off for client endpoints, however, it may be
  233. turned off for server endpoints if an alternative mechanism is used to
  234. authenticate clients. For example, the VNC server can use SASL to
  235. authenticate clients instead.
  236. To load server credentials with client certificate validation enabled
  237. .. parsed-literal::
  238. |qemu_system| -object tls-creds-x509,id=tls0,dir=/etc/pki/qemu,endpoint=server
  239. while to load client credentials use
  240. .. parsed-literal::
  241. |qemu_system| -object tls-creds-x509,id=tls0,dir=/etc/pki/qemu,endpoint=client
  242. Network services which support TLS will all have a ``tls-creds``
  243. parameter which expects the ID of the TLS credentials object. For
  244. example with VNC:
  245. .. parsed-literal::
  246. |qemu_system| -vnc 0.0.0.0:0,tls-creds=tls0
  247. .. _tls_005fpsk:
  248. TLS Pre-Shared Keys (PSK)
  249. ~~~~~~~~~~~~~~~~~~~~~~~~~
  250. Instead of using certificates, you may also use TLS Pre-Shared Keys
  251. (TLS-PSK). This can be simpler to set up than certificates but is less
  252. scalable.
  253. Use the GnuTLS ``psktool`` program to generate a ``keys.psk`` file
  254. containing one or more usernames and random keys::
  255. mkdir -m 0700 /tmp/keys
  256. psktool -u rich -p /tmp/keys/keys.psk
  257. TLS-enabled servers such as ``qemu-nbd`` can use this directory like so::
  258. qemu-nbd \
  259. -t -x / \
  260. --object tls-creds-psk,id=tls0,endpoint=server,dir=/tmp/keys \
  261. --tls-creds tls0 \
  262. image.qcow2
  263. When connecting from a qemu-based client you must specify the directory
  264. containing ``keys.psk`` and an optional username (defaults to "qemu")::
  265. qemu-img info \
  266. --object tls-creds-psk,id=tls0,dir=/tmp/keys,username=rich,endpoint=client \
  267. --image-opts \
  268. file.driver=nbd,file.host=localhost,file.port=10809,file.tls-creds=tls0,file.export=/