Skip to contentSkip to conversation

Extract TLS handling code from VNC server

PATCH Superseded Daniel P. Berrange 2 participants 13 messages Aug 24, 2015 – Aug 26, 2015 Crypto API

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover Extract TLS handling code from VNC server

This small patch series is a formal submission of another part of my previous series

v1: https://lists.gnu.org/archive/html/qemu-devel/2015-04/msg02038.html
v2: https://lists.gnu.org/archive/html/qemu-devel/2015-08/msg01267.html
v3: https://lists.gnu.org/archive/html/qemu-devel/2015-08/msg01386.html

Now we have the basic crypto module defined for hash/cipher APIs, we extend it to also cover TLS credential and TLS session handling APIs. These new TLS related APIs obsolete the vast majority of the TLS handling code in the current VNC server. As a result the VNC server no longer has to worry about conditional compilation for GNUTLS. It also gives us code reuse for future patches which intend to add TLS support to chardevs, migration, nbd, etc.

This series deprecates the existing way of configuring TLS for VNC on the command line, but maintains support for back-compat reasons.

Since the TLS code is now totally isolated from the VNC server it is also practical to provide significant unit test coverage of what is security critical code.

Aside from the new CLI syntax for configuring TLS with VNC, the only other functional change is to allow diffie-hellman params to be loaded from a file, instead of being generated at startup.

Changes in v4:

- Fix build when GNUTLS is disabled
- Add missed return type conversion in vnc.h

Changes in v3:

- Switched "tls-creds" object to be just an abstract base class
- Created "tls-creds-anon" object subclass in new file
- Created "tls-creds-x509" object subclass in new file

Daniel P. Berrange (7):
crypto: introduce new base module for TLS credentials
crypto: introduce new module for TLS anonymous credentials
crypto: introduce new module for TLS x509 credentials
crypto: add sanity checking of TLS x509 credentials
crypto: introduce new module for handling TLS sessions
ui: fix return type for VNC I/O functions to be ssize_t
ui: convert VNC server to use QCryptoTLSSession

 configure                        |   53 +-
 crypto/Makefile.objs             |    4 +
 crypto/init.c                    |   17 +
 crypto/tlscreds.c                |  270 ++++++++++
 crypto/tlscredsanon.c            |  235 ++++++++
 crypto/tlscredspriv.h            |   41 ++
 crypto/tlscredsx509.c            |  821 ++++++++++++++++++++++++++++
 crypto/tlssession.c              |  578 ++++++++++++++++++++
 include/crypto/tlscreds.h        |   77 +++
 include/crypto/tlscredsanon.h    |  113 ++++
 include/crypto/tlscredsx509.h    |  115 ++++
 include/crypto/tlssession.h      |  322 +++++++++++
 qemu-options.hx                  |   75 ++-
 tests/.gitignore                 |    7 +
 tests/Makefile                   |   14 +-
 tests/crypto-tls-x509-helpers.c  |  486 +++++++++++++++++
 tests/crypto-tls-x509-helpers.h  |  133 +++++
 tests/pkix_asn1_tab.c            | 1103 ++++++++++++++++++++++++++++++++++++++
 tests/test-crypto-tlscredsx509.c |  734 +++++++++++++++++++++++++
 tests/test-crypto-tlssession.c   |  534 ++++++++++++++++++
 ui/Makefile.objs                 |    2 +-
 ui/vnc-auth-sasl.c               |   36 +-
 ui/vnc-auth-vencrypt.c           |   80 +--
 ui/vnc-tls.c                     |  474 ----------------
 ui/vnc-tls.h                     |   69 ---
 ui/vnc-ws.c                      |   82 +--
 ui/vnc-ws.h                      |    2 -
 ui/vnc.c                         |  360 ++++++++-----
 ui/vnc.h                         |   21 +-
 29 files changed, 6043 insertions(+), 815 deletions(-)
 create mode 100644 crypto/tlscreds.c
 create mode 100644 crypto/tlscredsanon.c
 create mode 100644 crypto/tlscredspriv.h
 create mode 100644 crypto/tlscredsx509.c
 create mode 100644 crypto/tlssession.c
 create mode 100644 include/crypto/tlscreds.h
 create mode 100644 include/crypto/tlscredsanon.h
 create mode 100644 include/crypto/tlscredsx509.h
 create mode 100644 include/crypto/tlssession.h
 create mode 100644 tests/crypto-tls-x509-helpers.c
 create mode 100644 tests/crypto-tls-x509-helpers.h
 create mode 100644 tests/pkix_asn1_tab.c
 create mode 100644 tests/test-crypto-tlscredsx509.c
 create mode 100644 tests/test-crypto-tlssession.c
 delete mode 100644 ui/vnc-tls.c
 delete mode 100644 ui/vnc-tls.h
Signature · 1 line
2.4.3
#1 lore raw

PATCH v4 1/7 crypto: introduce new base module for TLS credentials

Introduce a QCryptoTLSCreds class to act as the base class for storing TLS credentials. This will be later subclassed to provide handling of anonymous and x509 credential types. The subclasses will be user creatable objects, so instances can be created & deleted via 'object-add' and 'object-del' QMP commands respectively, or via the -object command line arg.

If the credentials cannot be initialized an error will be reported as a QMP reply, or on stderr respectively.

The idea is to make it possible to represent and manager TLS credentials independantly of the network service that is using them. This will enable multiple services to use the same set of credentials and minimize code duplication. A later patch will convert the current VNC server TLS code over to use this object.

The representation of credentials will be functionally equivalent to that currently implemented in the VNC server with one exception. The new code has the ability to (optionally) load a pre-generated set of diffie-hellman parameters, if the file dh-params.pem exists, whereas the current VNC server will always generate them on startup. This is beneficial for admins who wish to avoid the (small) time sink of generating DH parameters at startup and/or avoid depleting entropy.

---
 crypto/Makefile.objs      |   1 +
 crypto/init.c             |  11 ++
 crypto/tlscreds.c         | 270 ++++++++++++++++++++++++++++++++++++++++++++++
 crypto/tlscredspriv.h     |  41 +++++++
 include/crypto/tlscreds.h |  77 +++++++++++++
 tests/Makefile            |   4 +-
 6 files changed, 402 insertions(+), 2 deletions(-)
 create mode 100644 crypto/tlscreds.c
 create mode 100644 crypto/tlscredspriv.h
 create mode 100644 include/crypto/tlscreds.h
crypto/Makefile.objs +1 -0
index b050138..cf62d51 100644
@@ -3,3 +3,4 @@ util-obj-y += hash.o
 util-obj-y += aes.o
 util-obj-y += desrfb.o
 util-obj-y += cipher.o
+util-obj-y += tlscreds.o
crypto/init.c +11 -0
index 7447882..fe8d4ef 100644
@@ -19,6 +19,7 @@
  */
 
 #include "crypto/init.h"
+#include "crypto/tlscreds.h"
 #include "qemu/thread.h"
 
 #ifdef CONFIG_GNUTLS
@@ -137,6 +138,13 @@ int qcrypto_init(Error **errp)
     gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
 #endif
 
+    /* XXX hack - if we don't reference any function in tlscreds*.c
+     * then the linker drops tlscred*.o from libqemutil.a when it
+     * links the emulators as it thinks it is unused. It isn't
+     * clever enough to see the constructor :-(
+     */
+    qcrypto_tls_creds_dummy();
+
     return 0;
 }
 
@@ -144,6 +152,9 @@ int qcrypto_init(Error **errp)
 
 int qcrypto_init(Error **errp G_GNUC_UNUSED)
 {
+    /* XXX hack - see above */
+    qcrypto_tls_creds_dummy();
+
     return 0;
 }
 
crypto/tlscreds.c new file +270 -0

270 lines not on this page · raw message

crypto/tlscredspriv.h new file +41 -0

41 lines not on this page · raw message

include/crypto/tlscreds.h new file +77 -0

77 lines not on this page · raw message

tests/Makefile +2 -2
index 5271123..fcbc86f 100644
@@ -345,8 +345,8 @@ tests/test-opts-visitor$(EXESUF): tests/test-opts-visitor.o $(test-qapi-obj-y) l
 
 tests/test-mul64$(EXESUF): tests/test-mul64.o libqemuutil.a
 tests/test-bitops$(EXESUF): tests/test-bitops.o libqemuutil.a
-tests/test-crypto-hash$(EXESUF): tests/test-crypto-hash.o libqemuutil.a libqemustub.a
-tests/test-crypto-cipher$(EXESUF): tests/test-crypto-cipher.o libqemuutil.a libqemustub.a
+tests/test-crypto-hash$(EXESUF): tests/test-crypto-hash.o $(qom-core-obj) libqemuutil.a libqemustub.a
+tests/test-crypto-cipher$(EXESUF): tests/test-crypto-cipher.o $(qom-core-obj) libqemuutil.a libqemustub.a
 
 libqos-obj-y = tests/libqos/pci.o tests/libqos/fw_cfg.o tests/libqos/malloc.o
 libqos-obj-y += tests/libqos/i2c.o tests/libqos/libqos.o
Signature · 1 line
2.4.3
#2 lore raw

Re: [Qemu-devel] [PATCH v4 1/7] crypto: introduce new base module for TLS credentials

Quoted 11 lines · Daniel P. Berrange

Introduce a QCryptoTLSCreds class to act as the base class for storing TLS credentials. This will be later subclassed to provide handling of anonymous and x509 credential types. The subclasses will be user creatable objects, so instances can be created & deleted via 'object-add' and 'object-del' QMP commands respectively, or via the -object command line arg.

If the credentials cannot be initialized an error will be reported as a QMP reply, or on stderr respectively.

The idea is to make it possible to represent and manager TLS

s/manager/manage/

Quoted 1 line

credentials independantly of the network service that is using

s/independantly/independently/

Quoted 29 lines

them. This will enable multiple services to use the same set of credentials and minimize code duplication. A later patch will convert the current VNC server TLS code over to use this object.

The representation of credentials will be functionally equivalent to that currently implemented in the VNC server with one exception. The new code has the ability to (optionally) load a pre-generated set of diffie-hellman parameters, if the file dh-params.pem exists, whereas the current VNC server will always generate them on startup. This is beneficial for admins who wish to avoid the (small) time sink of generating DH parameters at startup and/or avoid depleting entropy.

---
 crypto/Makefile.objs      |   1 +
 crypto/init.c             |  11 ++
 crypto/tlscreds.c         | 270 ++++++++++++++++++++++++++++++++++++++++++++++
 crypto/tlscredspriv.h     |  41 +++++++
 include/crypto/tlscreds.h |  77 +++++++++++++
 tests/Makefile            |   4 +-
 6 files changed, 402 insertions(+), 2 deletions(-)
 create mode 100644 crypto/tlscreds.c
 create mode 100644 crypto/tlscredspriv.h
 create mode 100644 include/crypto/tlscreds.h

+++ b/crypto/tlscreds.c

crypto/tlscreds.c line 29

Quoted 8 lines
crypto/tlscreds.c new file +7 -0
@@ -0,0 +1,270 @@
 
+/* #define QCRYPTO_DEBUG */
+
+#ifdef QCRYPTO_DEBUG
+#define DPRINTF(fmt, ...) do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
+#else
+#define DPRINTF(fmt, ...) do { } while (0)
+#endif

Please rework this to:

#ifdef QCRYPTO_DEBUG

# define QCRYPT_DEBUG_PRINT 1
#else
# define QCRYPT_DEBUG_PRINT 0
#endif
#define DPRINTF(fmt, ...) \
    do { \
        if (QCRYPT_DEBUG_PRINT) { \
            fprintf(stderr, fmt, ## __VA_ARGS__); \
        } \
    } while (0)

so that we don't bit-rot the printf arguments when debugging is disabled.

crypto/tlscreds.c line 38

Quoted 9 lines
crypto/tlscreds.c new file +9 -0
+
+
+#define DH_BITS 2048
+
+static const char * const endpoint_map[QCRYPTO_TLS_CREDS_ENDPOINT_LAST + 1] = {
+    [QCRYPTO_TLS_CREDS_ENDPOINT_SERVER] = "server",
+    [QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT] = "client",
+    [QCRYPTO_TLS_CREDS_ENDPOINT_LAST] = NULL,
+};

Is it worth an entry in a .json file to get qapi to generate this mapping automatically?

Quoted 6 lines
crypto/tlscreds.c new file +6 -0
+
+
+#ifdef CONFIG_GNUTLS
+int
+qcrypto_tls_creds_get_dh_params_file(const char *filename,
+                                     gnutls_dh_params_t *dh_params,

crypto/tlscreds.c line 54

Quoted 10 lines
crypto/tlscreds.c new file +10 -0
+                                     Error **errp)
+{
+    int ret;
+
+    DPRINTF("Loading DH params %s\n", filename ? filename : "<generated>");
+    if (filename == NULL) {
+        ret = gnutls_dh_params_init(dh_params);
+        if (ret < 0) {
+            error_setg(errp, "Unable to initialize DH parameters %s",
+                       gnutls_strerror(ret));

Maybe s/parameters %s/parameters: %s/ ?

crypto/tlscreds.c line 62

Quoted 8 lines
crypto/tlscreds.c new file +8 -0
+            return -1;
+        }
+        ret = gnutls_dh_params_generate2(*dh_params, DH_BITS);
+        if (ret < 0) {
+            gnutls_dh_params_deinit(*dh_params);
+            *dh_params = NULL;
+            error_setg(errp, "Unable to generate DH parameters %s",
+                       gnutls_strerror(ret));

and again? (Recurring theme, so I'll quit pointing it out)

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

Re: [Qemu-devel] [PATCH v4 1/7] crypto: introduce new base module for TLS credentials

Quoted 11 lines · Eric Blake

crypto/tlscreds.c line 29

Quoted 7 lines
crypto/tlscreds.c new file +7 -0
+/* #define QCRYPTO_DEBUG */
+
+#ifdef QCRYPTO_DEBUG
+#define DPRINTF(fmt, ...) do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
+#else
+#define DPRINTF(fmt, ...) do { } while (0)
+#endif

Please rework this to:

#ifdef QCRYPTO_DEBUG

Quoted 10 lines
# define QCRYPT_DEBUG_PRINT 1
#else
# define QCRYPT_DEBUG_PRINT 0
#endif
#define DPRINTF(fmt, ...) \
    do { \
        if (QCRYPT_DEBUG_PRINT) { \
            fprintf(stderr, fmt, ## __VA_ARGS__); \
        } \
    } while (0)

Ah that's a good idea.

One day it would nice if QEMU had a standardized debug logging macro in qemu-common.h, so we could just turn on/off debugging per file using

   #define ENABLE_DEBUG 1
   #include "qemu-common.h"
Quoted 10 lines

crypto/tlscreds.c line 38

Quoted 7 lines
crypto/tlscreds.c new file +7 -0
+#define DH_BITS 2048
+
+static const char * const endpoint_map[QCRYPTO_TLS_CREDS_ENDPOINT_LAST + 1] = {
+    [QCRYPTO_TLS_CREDS_ENDPOINT_SERVER] = "server",
+    [QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT] = "client",
+    [QCRYPTO_TLS_CREDS_ENDPOINT_LAST] = NULL,
+};

Is it worth an entry in a .json file to get qapi to generate this mapping automatically?

I guess adding the enum definition itself to QAPI would mean we would get better introspection support when we solve QOM class introspection of properties.

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#4 lore raw

PATCH v4 2/7 crypto: introduce new module for TLS anonymous credentials

In reply to Daniel P. Berrange

Introduce a QCryptoTLSCredsAnon class which is used to manage anonymous TLS credentials. Use of this class is generally discouraged since it does not offer strong security, but it is required for backwards compatibility with the current VNC server implementation.

Simple example CLI configuration:

$QEMU -object tls-creds-anon,id=tls0,endpoint=server

Example using pre-created diffie-hellman parameters

$QEMU -object tls-creds-anon,id=tls0,endpoint=server,\
dir=/path/to/creds/dir

The 'id' value in the -object args will be used to associate the credentials with the network services. For eample, when the VNC server is later converted it would use

 $QEMU -object tls-creds-anon,id=tls0,.... \
       -vnc 127.0.0.1:1,tls-creds=tls0
---
 crypto/Makefile.objs          |   1 +
 crypto/init.c                 |   3 +
 crypto/tlscredsanon.c         | 235 ++++++++++++++++++++++++++++++++++++++++++
 include/crypto/tlscredsanon.h | 113 ++++++++++++++++++++
 qemu-options.hx               |  20 ++++
 5 files changed, 372 insertions(+)
 create mode 100644 crypto/tlscredsanon.c
 create mode 100644 include/crypto/tlscredsanon.h
crypto/Makefile.objs +1 -0
index cf62d51..cf0199e 100644
@@ -4,3 +4,4 @@ util-obj-y += aes.o
 util-obj-y += desrfb.o
 util-obj-y += cipher.o
 util-obj-y += tlscreds.o
+util-obj-y += tlscredsanon.o
crypto/init.c +3 -0
index fe8d4ef..c08d3ce 100644
@@ -20,6 +20,7 @@
 
 #include "crypto/init.h"
 #include "crypto/tlscreds.h"
+#include "crypto/tlscredsanon.h"
 #include "qemu/thread.h"
 
 #ifdef CONFIG_GNUTLS
@@ -144,6 +145,7 @@ int qcrypto_init(Error **errp)
      * clever enough to see the constructor :-(
      */
     qcrypto_tls_creds_dummy();
+    qcrypto_tls_creds_anon_dummy();
 
     return 0;
 }
@@ -154,6 +156,7 @@ int qcrypto_init(Error **errp G_GNUC_UNUSED)
 {
     /* XXX hack - see above */
     qcrypto_tls_creds_dummy();
+    qcrypto_tls_creds_anon_dummy();
 
     return 0;
 }
crypto/tlscredsanon.c new file +235 -0

235 lines not on this page · raw message

include/crypto/tlscredsanon.h new file +113 -0

113 lines not on this page · raw message

qemu-options.hx +20 -0
index 77f5853..057801a 100644
@@ -3568,6 +3568,26 @@ the @option{virtio-rng} device. The @option{chardev} parameter is
 the unique ID of a character device backend that provides the connection
 to the RNG daemon.
 
+@item -object tls-creds-anon,id=@var{id},endpoint=@var{endpoint},dir=@var{/path/to/cred/dir},verify-peer=@var{on|off}
+
+Creates a TLS anonymous credentials object, which can be used to provide
+TLS support on network backends. The @option{id} parameter is a unique
+ID which network backends will use to access the credentials. The
+@option{endpoint} is either @option{server} or @option{client} depending
+on whether the QEMU network backend that uses the credentials will be
+acting as a client or as a server. If @option{verify-peer} is enabled
+(the default) then once the handshake is completed, the peer credentials
+will be verified, though this is a no-op for anonymous credentials.
+
+The @var{dir} parameter tells QEMU where to find the credential
+files. For server endpoints, this directory may contain a file
+@var{dh-params.pem} providing diffie-hellman parameters to use
+for the TLS server. If the file is missing, QEMU will generate
+a set of DH parameters at startup. This is a computationally
+expensive operation that consumes random pool entropy, so it is
+recommended that a persistent set of parameters be generated
+upfront and saved.
+
 @end table
 
 ETEXI
Signature · 1 line
2.4.3
#5 lore raw

Re: [Qemu-devel] [PATCH v4 2/7] crypto: introduce new module for TLS anonymous credentials

Quoted 10 lines · Daniel P. Berrange

Introduce a QCryptoTLSCredsAnon class which is used to manage anonymous TLS credentials. Use of this class is generally discouraged since it does not offer strong security, but it is required for backwards compatibility with the current VNC server implementation.

Simple example CLI configuration:

$QEMU -object tls-creds-anon,id=tls0,endpoint=server

Quoted 7 lines

Example using pre-created diffie-hellman parameters

$QEMU -object tls-creds-anon,id=tls0,endpoint=server,\
dir=/path/to/creds/dir

The 'id' value in the -object args will be used to associate the credentials with the network services. For eample, when the VNC

s/eample/example/

Quoted 18 lines

server is later converted it would use

 $QEMU -object tls-creds-anon,id=tls0,.... \
       -vnc 127.0.0.1:1,tls-creds=tls0

---

+++ b/crypto/init.c

crypto/init.c +1 -0
@@ -20,6 +20,7 @@
 
 #include "crypto/init.h"
 #include "crypto/tlscreds.h"
+#include "crypto/tlscredsanon.h"
 #include "qemu/thread.h"
 
 #ifdef CONFIG_GNUTLS

crypto/init.c int qcrypto_init(Error **errp) · line 148

Quoted 4 lines
crypto/init.c +1 -0
@@ -144,6 +145,7 @@ int qcrypto_init(Error **errp)
      * clever enough to see the constructor :-(
      */
     qcrypto_tls_creds_dummy();
+    qcrypto_tls_creds_anon_dummy();

Are there any gcc hacks such as adding __attribute__((used)) that might help?

crypto/tlscredsanon.c line 31

Quoted 10 lines
crypto/tlscredsanon.c new file +8 -0
+++ b/crypto/tlscredsanon.c
@@ -0,0 +1,235 @@
 
+/* #define QCRYPTO_DEBUG */
+
+#ifdef QCRYPTO_DEBUG
+#define DPRINTF(fmt, ...) do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
+#else
+#define DPRINTF(fmt, ...) do { } while (0)
+#endif

Again, please rework this to avoid bitrot when debug is off (I'll quit pointing it out, but suspect it to be a common problem in this series)

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#6 lore raw

Re: [Qemu-devel] [PATCH v4 2/7] crypto: introduce new module for TLS anonymous credentials

Quoted 37 lines · Eric Blake
Quoted 10 lines · Daniel P. Berrange

Introduce a QCryptoTLSCredsAnon class which is used to manage anonymous TLS credentials. Use of this class is generally discouraged since it does not offer strong security, but it is required for backwards compatibility with the current VNC server implementation.

Simple example CLI configuration:

$QEMU -object tls-creds-anon,id=tls0,endpoint=server

Quoted 7 lines

Example using pre-created diffie-hellman parameters

$QEMU -object tls-creds-anon,id=tls0,endpoint=server,\
dir=/path/to/creds/dir

The 'id' value in the -object args will be used to associate the credentials with the network services. For eample, when the VNC

s/eample/example/

Quoted 16 lines

server is later converted it would use

 $QEMU -object tls-creds-anon,id=tls0,.... \
       -vnc 127.0.0.1:1,tls-creds=tls0

---

+++ b/crypto/init.c

crypto/init.c +1 -0
@@ -20,6 +20,7 @@
 
 #include "crypto/init.h"
 #include "crypto/tlscreds.h"
+#include "crypto/tlscredsanon.h"
 #include "qemu/thread.h"
 
Quoted 9 lines

crypto/init.c int qcrypto_init(Error **errp) · line 148

Quoted 6 lines
crypto/init.c +1 -0
 #ifdef CONFIG_GNUTLS
@@ -144,6 +145,7 @@ int qcrypto_init(Error **errp)
      * clever enough to see the constructor :-(
      */
     qcrypto_tls_creds_dummy();
+    qcrypto_tls_creds_anon_dummy();

Are there any gcc hacks such as adding __attribute__((used)) that might help?

I finally figured out that we can use -Wl,--whole-archive when linking to libqemuutil.a to fix this properly.

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#7 lore raw

PATCH v4 3/7 crypto: introduce new module for TLS x509 credentials

In reply to Daniel P. Berrange

Introduce a QCryptoTLSCredsX509 class which is used to manage x509 certificate TLS credentials. This will be the preferred credential type offering strong security characteristics

Example CLI configuration:

$QEMU -object tls-creds-x509,id=tls0,endpoint=server,\
dir=/path/to/creds/dir,verify-peer=yes

The 'id' value in the -object args will be used to associate the credentials with the network services. For eample, when the VNC server is later converted it would use

 $QEMU -object tls-creds-x509,id=tls0,.... \
       -vnc 127.0.0.1:1,tls-creds=tls0
---
 crypto/Makefile.objs          |   1 +
 crypto/init.c                 |   3 +
 crypto/tlscredsx509.c         | 277 ++++++++++++++++++++++++++++++++++++++++++
 include/crypto/tlscredsx509.h | 114 +++++++++++++++++
 qemu-options.hx               |  27 ++++
 5 files changed, 422 insertions(+)
 create mode 100644 crypto/tlscredsx509.c
 create mode 100644 include/crypto/tlscredsx509.h
crypto/Makefile.objs +1 -0
index cf0199e..914bb68 100644
@@ -5,3 +5,4 @@ util-obj-y += desrfb.o
 util-obj-y += cipher.o
 util-obj-y += tlscreds.o
 util-obj-y += tlscredsanon.o
+util-obj-y += tlscredsx509.o
crypto/init.c +3 -0
index c08d3ce..8abe7cb 100644
@@ -21,6 +21,7 @@
 #include "crypto/init.h"
 #include "crypto/tlscreds.h"
 #include "crypto/tlscredsanon.h"
+#include "crypto/tlscredsx509.h"
 #include "qemu/thread.h"
 
 #ifdef CONFIG_GNUTLS
@@ -146,6 +147,7 @@ int qcrypto_init(Error **errp)
      */
     qcrypto_tls_creds_dummy();
     qcrypto_tls_creds_anon_dummy();
+    qcrypto_tls_creds_x509_dummy();
 
     return 0;
 }
@@ -157,6 +159,7 @@ int qcrypto_init(Error **errp G_GNUC_UNUSED)
     /* XXX hack - see above */
     qcrypto_tls_creds_dummy();
     qcrypto_tls_creds_anon_dummy();
+    qcrypto_tls_creds_x509_dummy();
 
     return 0;
 }
crypto/tlscredsx509.c new file +277 -0

277 lines not on this page · raw message

include/crypto/tlscredsx509.h new file +114 -0

114 lines not on this page · raw message

qemu-options.hx +27 -0
index 057801a..23c91d5 100644
@@ -3588,6 +3588,33 @@ expensive operation that consumes random pool entropy, so it is
 recommended that a persistent set of parameters be generated
 upfront and saved.
 
+@item -object tls-creds-x509,id=@var{id},endpoint=@var{endpoint},dir=@var{/path/to/cred/dir},verify-peer=@var{on|off}
+
+Creates a TLS anonymous credentials object, which can be used to provide
+TLS support on network backends. The @option{id} parameter is a unique
+ID which network backends will use to access the credentials. The
+@option{endpoint} is either @option{server} or @option{client} depending
+on whether the QEMU network backend that uses the credentials will be
+acting as a client or as a server. If @option{verify-peer} is enabled
+(the default) then once the handshake is completed, the peer credentials
+will be verified. With x509 certificates, this implies that the clients
+must be provided with valid client certificates too.
+
+The @var{dir} parameter tells QEMU where to find the credential
+files. For server endpoints, this directory may contain a file
+@var{dh-params.pem} providing diffie-hellman parameters to use
+for the TLS server. If the file is missing, QEMU will generate
+a set of DH parameters at startup. This is a computationally
+expensive operation that consumes random pool entropy, so it is
+recommended that a persistent set of parameters be generated
+upfront and saved.
+
+For x509 certificate credentials the directory will contain further files
+providing the x509 certificates. The certificates must be stored
+in PEM format, in filenames @var{ca-cert.pem}, @var{ca-crl.pem} (optional),
+@var{server-cert.pem} (only servers), @var{server-key.pem} (only servers),
+@var{client-cert.pem} (only clients), and @var{client-key.pem} (only clients).
+
 @end table
 
 ETEXI
Signature · 1 line
2.4.3
#8 lore raw

Re: [Qemu-devel] [PATCH v4 3/7] crypto: introduce new module for TLS x509 credentials

Quoted 5 lines · Daniel P. Berrange

Introduce a QCryptoTLSCredsX509 class which is used to manage x509 certificate TLS credentials. This will be the preferred credential type offering strong security characteristics

Quoted 7 lines

Example CLI configuration:

$QEMU -object tls-creds-x509,id=tls0,endpoint=server,\
dir=/path/to/creds/dir,verify-peer=yes

The 'id' value in the -object args will be used to associate the credentials with the network services. For eample, when the VNC

s/eample/example/

Quoted 8 lines

server is later converted it would use

 $QEMU -object tls-creds-x509,id=tls0,.... \
       -vnc 127.0.0.1:1,tls-creds=tls0

---

crypto/tlscredsx509.c line 31

Quoted 7 lines
crypto/tlscredsx509.c new file +7 -0
+/* #define QCRYPTO_DEBUG */
+
+#ifdef QCRYPTO_DEBUG
+#define DPRINTF(fmt, ...) do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
+#else
+#define DPRINTF(fmt, ...) do { } while (0)
+#endif

/me I said I wouldn't point it out further... Must Resist...

Otherwise looks okay.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#9 lore raw

PATCH v4 4/7 crypto: add sanity checking of TLS x509 credentials

In reply to Daniel P. Berrange

If the administrator incorrectly sets up their x509 certificates, the errors seen at runtime during connection attempts are very obscure and difficult to diagnose. This has been a particular problem for people using openssl to generate their certificates instead of the gnutls certtool, because the openssl tools don't turn on the various x509 extensions that gnutls expects to be present by default.

This change thus adds support in the TLS credentials object to sanity check the certificates when QEMU first loads them. This gives the administrator immediate feedback for the majority of common configuration mistakes, reducing the pain involved in setting up TLS. The code is derived from equivalent code that has been part of libvirt's TLS support and has been seen to be valuable in assisting admins.

It is possible to disable the sanity checking, however, via the new 'sanity-check' property on the tls-creds object type, with a value of 'no'.

Unit tests are included in this change to verify the correctness of the sanity checking code in all the key scenarios it is intended to cope with. As part of the test suite, the pkix_asn1_tab.c from gnutls is imported. This file is intentionally copied from the (long since obsolete) gnutls 1.6.3 source tree, since that version was still under GPLv2+, rather than the GPLv3+ of gnutls >= 2.0.

---
 configure                        |   22 +
 crypto/tlscredsx509.c            |  544 +++++++++++++++++++
 include/crypto/tlscredsx509.h    |    1 +
 tests/.gitignore                 |    3 +
 tests/Makefile                   |    7 +-
 tests/crypto-tls-x509-helpers.c  |  486 +++++++++++++++++
 tests/crypto-tls-x509-helpers.h  |  133 +++++
 tests/pkix_asn1_tab.c            | 1103 ++++++++++++++++++++++++++++++++++++++
 tests/test-crypto-tlscredsx509.c |  734 +++++++++++++++++++++++++
 9 files changed, 3032 insertions(+), 1 deletion(-)
 create mode 100644 tests/crypto-tls-x509-helpers.c
 create mode 100644 tests/crypto-tls-x509-helpers.h
 create mode 100644 tests/pkix_asn1_tab.c
 create mode 100644 tests/test-crypto-tlscredsx509.c
configure +22 -0
index cc6ced9..2335cba 100755
@@ -416,6 +416,9 @@ if test "$debug_info" = "yes"; then
     LDFLAGS="-g $LDFLAGS"
 fi
 
+test_cflags=""
+test_libs=""
+
 # make source path absolute
 source_path=`cd "$source_path"; pwd`
 
@@ -2221,6 +2224,19 @@ if test "$gnutls_nettle" != "no"; then
     fi
 fi
 
+##########################################
+# libtasn1 - only for the TLS creds/session test suite
+
+tasn1=yes
+if $pkg_config --exists "libtasn1"; then
+    tasn1_cflags=`$pkg_config --cflags libtasn1`
+    tasn1_libs=`$pkg_config --libs libtasn1`
+    test_cflags="$test_cflags $tasn1_cflags"
+    test_libs="$test_libs $tasn1_libs"
+else
+    tasn1=no
+fi
+
 
 ##########################################
 # VTE probe
@@ -4519,6 +4535,7 @@ echo "GNUTLS support    $gnutls"
 echo "GNUTLS hash       $gnutls_hash"
 echo "GNUTLS gcrypt     $gnutls_gcrypt"
 echo "GNUTLS nettle     $gnutls_nettle ${gnutls_nettle+($nettle_version)}"
+echo "libtasn1          $tasn1"
 echo "VTE support       $vte"
 echo "curses support    $curses"
 echo "curl support      $curl"
@@ -4888,6 +4905,9 @@ if test "$gnutls_nettle" = "yes" ; then
   echo "CONFIG_GNUTLS_NETTLE=y" >> $config_host_mak
   echo "CONFIG_NETTLE_VERSION_MAJOR=${nettle_version%%.*}" >> $config_host_mak
 fi
+if test "$tasn1" = "yes" ; then
+  echo "CONFIG_TASN1=y" >> $config_host_mak
+fi
 if test "$vte" = "yes" ; then
   echo "CONFIG_VTE=y" >> $config_host_mak
   echo "VTE_CFLAGS=$vte_cflags" >> $config_host_mak
@@ -5211,6 +5231,8 @@ echo "EXESUF=$EXESUF" >> $config_host_mak
 echo "DSOSUF=$DSOSUF" >> $config_host_mak
 echo "LDFLAGS_SHARED=$LDFLAGS_SHARED" >> $config_host_mak
 echo "LIBS_QGA+=$libs_qga" >> $config_host_mak
+echo "TEST_LIBS=$test_libs" >> $config_host_mak
+echo "TEST_CFLAGS=$test_cflags" >> $config_host_mak
 echo "POD2MAN=$POD2MAN" >> $config_host_mak
 echo "TRANSLATE_OPT_CFLAGS=$TRANSLATE_OPT_CFLAGS" >> $config_host_mak
 if test "$gcov" = "yes" ; then
crypto/tlscredsx509.c +544 -0

572 lines not on this page · raw message

include/crypto/tlscredsx509.h +1 -0
index bdb7b25..02f6a0d 100644
@@ -100,6 +100,7 @@ struct QCryptoTLSCredsX509 {
 #ifdef CONFIG_GNUTLS
     gnutls_certificate_credentials_t data;
 #endif
+    bool sanityCheck;
 };
 
 
tests/.gitignore +3 -0
index ccc92e4..7b4ee23 100644
@@ -12,6 +12,9 @@ test-bitops
 test-coroutine
 test-crypto-cipher
 test-crypto-hash
+test-crypto-tlscredsx509
+test-crypto-tlscredsx509-work/
+test-crypto-tlscredsx509-certs/
 test-cutils
 test-hbitmap
 test-int128
tests/Makefile +6 -1
index fcbc86f..84aabaf 100644
@@ -76,6 +76,7 @@ check-unit-y += tests/test-write-threshold$(EXESUF)
 gcov-files-test-write-threshold-y = block/write-threshold.c
 check-unit-$(CONFIG_GNUTLS_HASH) += tests/test-crypto-hash$(EXESUF)
 check-unit-y += tests/test-crypto-cipher$(EXESUF)
+check-unit-$(CONFIG_GNUTLS) += tests/test-crypto-tlscredsx509$(EXESUF)
 
 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 
@@ -347,6 +348,8 @@ tests/test-mul64$(EXESUF): tests/test-mul64.o libqemuutil.a
 tests/test-bitops$(EXESUF): tests/test-bitops.o libqemuutil.a
 tests/test-crypto-hash$(EXESUF): tests/test-crypto-hash.o $(qom-core-obj) libqemuutil.a libqemustub.a
 tests/test-crypto-cipher$(EXESUF): tests/test-crypto-cipher.o $(qom-core-obj) libqemuutil.a libqemustub.a
+tests/test-crypto-tlscredsx509$(EXESUF): tests/test-crypto-tlscredsx509.o tests/crypto-tls-x509-helpers.o tests/pkix_asn1_tab.o \
+        crypto/tlscredsx509.o $(qom-core-obj) libqemuutil.a libqemustub.a
 
 libqos-obj-y = tests/libqos/pci.o tests/libqos/fw_cfg.o tests/libqos/malloc.o
 libqos-obj-y += tests/libqos/i2c.o tests/libqos/libqos.o
@@ -405,7 +408,7 @@ tests/usb-hcd-uhci-test$(EXESUF): tests/usb-hcd-uhci-test.o $(libqos-usb-obj-y)
 tests/usb-hcd-ehci-test$(EXESUF): tests/usb-hcd-ehci-test.o $(libqos-usb-obj-y)
 tests/usb-hcd-xhci-test$(EXESUF): tests/usb-hcd-xhci-test.o $(libqos-usb-obj-y)
 tests/pc-cpu-test$(EXESUF): tests/pc-cpu-test.o
-tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o qemu-char.o qemu-timer.o $(qtest-obj-y)
+tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o qemu-char.o qemu-timer.o $(qtest-obj-y) $(qom-core-obj) libqemuutil.a libqemustub.a
 tests/qemu-iotests/socket_scm_helper$(EXESUF): tests/qemu-iotests/socket_scm_helper.o
 tests/test-qemu-opts$(EXESUF): tests/test-qemu-opts.o libqemuutil.a libqemustub.a
 tests/test-write-threshold$(EXESUF): tests/test-write-threshold.o $(block-obj-y) libqemuutil.a libqemustub.a
@@ -413,6 +416,8 @@ tests/test-write-threshold$(EXESUF): tests/test-write-threshold.o $(block-obj-y)
 ifeq ($(CONFIG_POSIX),y)
 LIBS += -lutil
 endif
+LIBS += $(TEST_LIBS)
+CFLAGS += $(TEST_CFLAGS)
 
 # QTest rules
 
tests/crypto-tls-x509-helpers.c new file +486 -0

486 lines not on this page · raw message

tests/crypto-tls-x509-helpers.h new file +133 -0

133 lines not on this page · raw message

tests/pkix_asn1_tab.c new file +1103 -0

1,103 lines not on this page · raw message

tests/test-crypto-tlscredsx509.c new file +734 -0

607 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.4.3

Part of the diff is not shown here. View the raw message.

#10 lore raw

PATCH v4 5/7 crypto: introduce new module for handling TLS sessions

In reply to Daniel P. Berrange

Introduce a QCryptoTLSSession object that will encapsulate all the code for setting up and using a client/sever TLS session. This isolates the code which depends on the gnutls library, avoiding #ifdefs in the rest of the codebase, as well as facilitating any possible future port to other TLS libraries, if desired. It makes use of the previously defined QCryptoTLSCreds object to access credentials to use with the session. It also includes further unit tests to validate the correctness of the TLS session handshake and certificate validation. This is functionally equivalent to the current TLS session handling code embedded in the VNC server, and will obsolete it.

---
 crypto/Makefile.objs           |   1 +
 crypto/tlssession.c            | 578 +++++++++++++++++++++++++++++++++++++++++
 include/crypto/tlssession.h    | 322 +++++++++++++++++++++++
 tests/.gitignore               |   4 +
 tests/Makefile                 |   3 +
 tests/test-crypto-tlssession.c | 534 +++++++++++++++++++++++++++++++++++++
 6 files changed, 1442 insertions(+)
 create mode 100644 crypto/tlssession.c
 create mode 100644 include/crypto/tlssession.h
 create mode 100644 tests/test-crypto-tlssession.c
crypto/Makefile.objs +1 -0
index 914bb68..52a192d 100644
@@ -6,3 +6,4 @@ util-obj-y += cipher.o
 util-obj-y += tlscreds.o
 util-obj-y += tlscredsanon.o
 util-obj-y += tlscredsx509.o
+util-obj-y += tlssession.o
crypto/tlssession.c new file +578 -0

578 lines not on this page · raw message

include/crypto/tlssession.h new file +322 -0

322 lines not on this page · raw message

tests/.gitignore +4 -0
index 7b4ee23..2c5e2c3 100644
@@ -15,6 +15,10 @@ test-crypto-hash
 test-crypto-tlscredsx509
 test-crypto-tlscredsx509-work/
 test-crypto-tlscredsx509-certs/
+test-crypto-tlssession
+test-crypto-tlssession-work/
+test-crypto-tlssession-client/
+test-crypto-tlssession-server/
 test-cutils
 test-hbitmap
 test-int128
tests/Makefile +3 -0
index 84aabaf..98dd408 100644
@@ -77,6 +77,7 @@ gcov-files-test-write-threshold-y = block/write-threshold.c
 check-unit-$(CONFIG_GNUTLS_HASH) += tests/test-crypto-hash$(EXESUF)
 check-unit-y += tests/test-crypto-cipher$(EXESUF)
 check-unit-$(CONFIG_GNUTLS) += tests/test-crypto-tlscredsx509$(EXESUF)
+check-unit-$(CONFIG_GNUTLS) += tests/test-crypto-tlssession$(EXESUF)
 
 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 
@@ -350,6 +351,8 @@ tests/test-crypto-hash$(EXESUF): tests/test-crypto-hash.o $(qom-core-obj) libqem
 tests/test-crypto-cipher$(EXESUF): tests/test-crypto-cipher.o $(qom-core-obj) libqemuutil.a libqemustub.a
 tests/test-crypto-tlscredsx509$(EXESUF): tests/test-crypto-tlscredsx509.o tests/crypto-tls-x509-helpers.o tests/pkix_asn1_tab.o \
         crypto/tlscredsx509.o $(qom-core-obj) libqemuutil.a libqemustub.a
+tests/test-crypto-tlssession$(EXESUF): tests/test-crypto-tlssession.o tests/crypto-tls-x509-helpers.o tests/pkix_asn1_tab.o \
+        crypto/tlscreds.o crypto/tlssession.o $(qom-core-obj) libqemuutil.a libqemustub.a
 
 libqos-obj-y = tests/libqos/pci.o tests/libqos/fw_cfg.o tests/libqos/malloc.o
 libqos-obj-y += tests/libqos/i2c.o tests/libqos/libqos.o
tests/test-crypto-tlssession.c new file +534 -0

534 lines not on this page · raw message

Signature · 1 line
2.4.3
#11 lore raw

PATCH v4 6/7 ui: fix return type for VNC I/O functions to be ssize_t

In reply to Daniel P. Berrange

Various VNC server I/O functions return 'long' and then also pass this to a method accepting 'int'. All these should be ssize_t to match the signature of read/write APIs and thus avoid potential for integer truncation / wraparound.

---
 ui/vnc.c | 36 ++++++++++++++++++------------------
 ui/vnc.h |  6 +++---
 2 files changed, 21 insertions(+), 21 deletions(-)
ui/vnc.c +18 -18
index e26973a..37133a4 100644
@@ -1268,7 +1268,7 @@ void vnc_disconnect_finish(VncState *vs)
     g_free(vs);
 }
 
-int vnc_client_io_error(VncState *vs, int ret, int last_errno)
+ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, int last_errno)
 {
     if (ret == 0 || ret == -1) {
         if (ret == -1) {
@@ -1284,7 +1284,7 @@ int vnc_client_io_error(VncState *vs, int ret, int last_errno)
             }
         }
 
-        VNC_DEBUG("Closing down client sock: ret %d, errno %d\n",
+        VNC_DEBUG("Closing down client sock: ret %zd, errno %d\n",
                   ret, ret < 0 ? last_errno : 0);
         vnc_disconnect_start(vs);
 
@@ -1301,11 +1301,11 @@ void vnc_client_error(VncState *vs)
 }
 
 #ifdef CONFIG_VNC_TLS
-static long vnc_client_write_tls(gnutls_session_t *session,
-                                 const uint8_t *data,
-                                 size_t datalen)
+static ssize_t vnc_client_write_tls(gnutls_session_t *session,
+                                    const uint8_t *data,
+                                    size_t datalen)
 {
-    long ret = gnutls_write(*session, data, datalen);
+    ssize_t ret = gnutls_write(*session, data, datalen);
     if (ret < 0) {
         if (ret == GNUTLS_E_AGAIN) {
             errno = EAGAIN;
@@ -1333,9 +1333,9 @@ static long vnc_client_write_tls(gnutls_session_t *session,
  * the requested 'datalen' if the socket would block. Returns
  * -1 on error, and disconnects the client socket.
  */
-long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
+ssize_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
 {
-    long ret;
+    ssize_t ret;
 #ifdef CONFIG_VNC_TLS
     if (vs->tls.session) {
         ret = vnc_client_write_tls(&vs->tls.session, data, datalen);
@@ -1360,9 +1360,9 @@ long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
  * the buffered output data if the socket would block. Returns
  * -1 on error, and disconnects the client socket.
  */
-static long vnc_client_write_plain(VncState *vs)
+static ssize_t vnc_client_write_plain(VncState *vs)
 {
-    long ret;
+    ssize_t ret;
 
 #ifdef CONFIG_VNC_SASL
     VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n",
@@ -1436,10 +1436,10 @@ void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
 }
 
 #ifdef CONFIG_VNC_TLS
-static long vnc_client_read_tls(gnutls_session_t *session, uint8_t *data,
-                                size_t datalen)
+static ssize_t vnc_client_read_tls(gnutls_session_t *session, uint8_t *data,
+                                   size_t datalen)
 {
-    long ret = gnutls_read(*session, data, datalen);
+    ssize_t ret = gnutls_read(*session, data, datalen);
     if (ret < 0) {
         if (ret == GNUTLS_E_AGAIN) {
             errno = EAGAIN;
@@ -1467,9 +1467,9 @@ static long vnc_client_read_tls(gnutls_session_t *session, uint8_t *data,
  * the requested 'datalen' if the socket would block. Returns
  * -1 on error, and disconnects the client socket.
  */
-long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
+ssize_t vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
 {
-    long ret;
+    ssize_t ret;
 #ifdef CONFIG_VNC_TLS
     if (vs->tls.session) {
         ret = vnc_client_read_tls(&vs->tls.session, data, datalen);
@@ -1492,9 +1492,9 @@ long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
  * Returns the number of bytes read. Returns -1 on error, and
  * disconnects the client socket.
  */
-static long vnc_client_read_plain(VncState *vs)
+static ssize_t vnc_client_read_plain(VncState *vs)
 {
-    int ret;
+    ssize_t ret;
     VNC_DEBUG("Read plain %p size %zd offset %zd\n",
               vs->input.buffer, vs->input.capacity, vs->input.offset);
     buffer_reserve(&vs->input, 4096);
@@ -1520,7 +1520,7 @@ static void vnc_jobs_bh(void *opaque)
 void vnc_client_read(void *opaque)
 {
     VncState *vs = opaque;
-    long ret;
+    ssize_t ret;
 
 #ifdef CONFIG_VNC_SASL
     if (vs->sasl.conn && vs->sasl.runSSF)
ui/vnc.h +3 -3
index 814d720..194ccf7 100644
@@ -513,8 +513,8 @@ enum {
 void vnc_client_read(void *opaque);
 void vnc_client_write(void *opaque);
 
-long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen);
-long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen);
+ssize_t vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen);
+ssize_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen);
 
 /* Protocol I/O functions */
 void vnc_write(VncState *vs, const void *data, size_t len);
@@ -533,7 +533,7 @@ uint32_t read_u32(uint8_t *data, size_t offset);
 
 /* Protocol stage functions */
 void vnc_client_error(VncState *vs);
-int vnc_client_io_error(VncState *vs, int ret, int last_errno);
+ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, int last_errno);
 
 void start_client_init(VncState *vs);
 void start_auth_vnc(VncState *vs);
Signature · 1 line
2.4.3
#12 lore raw

PATCH v4 7/7 ui: convert VNC server to use QCryptoTLSSession

In reply to Daniel P. Berrange

Switch VNC server over to using the QCryptoTLSSession object for the TLS session. This removes the direct use of gnutls from the VNC server code. It also removes most knowledge about TLS certificate handling from the VNC server code. This has the nice effect that all the CONFIG_VNC_TLS conditionals go away and the user gets an actual error message when requesting TLS instead of it being silently ignored.

With this change, the existing configuration options for enabling TLS with -vnc are deprecated.

Old syntax for anon-DH credentials:

-vnc hostname:0,tls

New syntax:

  -object tls-creds-anon,id=tls0,endpoint=server \
  -vnc hostname:0,tls-creds=tls0

Old syntax for x509 credentials, no client certs:

-vnc hostname:0,tls,x509=/path/to/certs

New syntax:

  -object tls-creds-x509,id=tls0,dir=/path/to/certs,endpoint=server,verify-peer=no \
  -vnc hostname:0,tls-creds=tls0

Old syntax for x509 credentials, requiring client certs:

-vnc hostname:0,tls,x509verify=/path/to/certs

New syntax:

  -object tls-creds-x509,id=tls0,dir=/path/to/certs,endpoint=server,verify-peer=yes \
  -vnc hostname:0,tls-creds=tls0

This aligns VNC with the way TLS credentials are to be configured in the future for chardev, nbd and migration backends. It also has the benefit that the same TLS credentials can be shared across multiple VNC server instances, if desired.

If someone uses the deprecated syntax, it will internally result in the creation of a 'tls-creds' object with an ID based on the VNC server ID. This allows backwards compat with the CLI syntax, while still deleting all the original TLS code from the VNC server.

---
 configure              |  31 ----
 qemu-options.hx        |  28 ++-
 ui/Makefile.objs       |   2 +-
 ui/vnc-auth-sasl.c     |  36 ++--
 ui/vnc-auth-vencrypt.c |  80 +++++----
 ui/vnc-tls.c           | 474 -------------------------------------------------
 ui/vnc-tls.h           |  69 -------
 ui/vnc-ws.c            |  82 +++++----
 ui/vnc-ws.h            |   2 -
 ui/vnc.c               | 338 ++++++++++++++++++++++-------------
 ui/vnc.h               |  15 +-
 11 files changed, 359 insertions(+), 798 deletions(-)
 delete mode 100644 ui/vnc-tls.c
 delete mode 100644 ui/vnc-tls.h
configure +0 -31
index 2335cba..24a5007 100755
@@ -242,7 +242,6 @@ vnc="yes"
 sparse="no"
 uuid=""
 vde=""
-vnc_tls=""
 vnc_sasl=""
 vnc_jpeg=""
 vnc_png=""
@@ -883,10 +882,6 @@ for opt do
   ;;
   --disable-strip) strip_opt="no"
   ;;
-  --disable-vnc-tls) vnc_tls="no"
-  ;;
-  --enable-vnc-tls) vnc_tls="yes"
-  ;;
   --disable-vnc-sasl) vnc_sasl="no"
   ;;
   --enable-vnc-sasl) vnc_sasl="yes"
@@ -2381,28 +2376,6 @@ EOF
   fi
 fi
 
-##########################################
-# VNC TLS/WS detection
-if test "$vnc" = "yes" -a "$vnc_tls" != "no" ; then
-  cat > $TMPC <<EOF
-#include <gnutls/gnutls.h>
-int main(void) { gnutls_session_t s; gnutls_init(&s, GNUTLS_SERVER); return 0; }
-EOF
-  vnc_tls_cflags=`$pkg_config --cflags gnutls 2> /dev/null`
-  vnc_tls_libs=`$pkg_config --libs gnutls 2> /dev/null`
-  if compile_prog "$vnc_tls_cflags" "$vnc_tls_libs" ; then
-    if test "$vnc_tls" != "no" ; then
-      vnc_tls=yes
-    fi
-    libs_softmmu="$vnc_tls_libs $libs_softmmu"
-    QEMU_CFLAGS="$QEMU_CFLAGS $vnc_tls_cflags"
-  else
-    if test "$vnc_tls" = "yes" ; then
-      feature_not_found "vnc-tls" "Install gnutls devel"
-    fi
-    vnc_tls=no
-  fi
-fi
 
 ##########################################
 # VNC SASL detection
@@ -4546,7 +4519,6 @@ echo "Block whitelist (ro) $block_drv_ro_whitelist"
 echo "VirtFS support    $virtfs"
 echo "VNC support       $vnc"
 if test "$vnc" = "yes" ; then
-    echo "VNC TLS support   $vnc_tls"
     echo "VNC SASL support  $vnc_sasl"
     echo "VNC JPEG support  $vnc_jpeg"
     echo "VNC PNG support   $vnc_png"
@@ -4753,9 +4725,6 @@ echo "CONFIG_BDRV_RO_WHITELIST=$block_drv_ro_whitelist" >> $config_host_mak
 if test "$vnc" = "yes" ; then
   echo "CONFIG_VNC=y" >> $config_host_mak
 fi
-if test "$vnc_tls" = "yes" ; then
-  echo "CONFIG_VNC_TLS=y" >> $config_host_mak
-fi
 if test "$vnc_sasl" = "yes" ; then
   echo "CONFIG_VNC_SASL=y" >> $config_host_mak
 fi
qemu-options.hx +26 -2
index 23c91d5..4256483 100644
@@ -1214,8 +1214,9 @@ By definition the Websocket port is 5700+@var{display}. If @var{host} is
 specified connections will only be allowed from this host.
 As an alternative the Websocket port could be specified by using
 @code{websocket}=@var{port}.
-TLS encryption for the Websocket connection is supported if the required
-certificates are specified with the VNC option @option{x509}.
+If no TLS credentials are provided, the websocket connection runs in
+uncrypted mode. If TLS credentials are provided, the websocket connection
+requires encrypted client connections.
 
 @item password
 
@@ -1236,6 +1237,20 @@ date and time).
 You can also use keywords "now" or "never" for the expiration time to
 allow <protocol> password to expire immediately or never expire.
 
+@item tls-creds=@var{ID}
+
+Provides the ID of a set of TLS credentials to use to secure the
+VNC server. They will apply to both the normal VNC server socket
+and the websocket socket (if enabled). Setting TLS credentials
+will cause the VNC server socket to enable the VeNCrypt auth
+mechanism.  The credentials should have been previously created
+using the @option{-object tls-creds} argument.
+
+The @option{tls-creds} parameter obsoletes the @option{tls},
+@option{x509}, and @option{x509verify} options, and as such
+it is not permitted to set both new and old type options at
+the same time.
+
 @item tls
 
 Require that client use TLS when communicating with the VNC server. This
@@ -1243,6 +1258,9 @@ uses anonymous TLS credentials so is susceptible to a man-in-the-middle
 attack. It is recommended that this option be combined with either the
 @option{x509} or @option{x509verify} options.
 
+This option is now deprecated in favour of using the @option{tls-creds}
+argument.
+
 @item x509=@var{/path/to/certificate/dir}
 
 Valid if @option{tls} is specified. Require that x509 credentials are used
@@ -1252,6 +1270,9 @@ to provide authentication of the client when this is used. The path following
 this option specifies where the x509 certificates are to be loaded from.
 See the @ref{vnc_security} section for details on generating certificates.
 
+This option is now deprecated in favour of using the @option{tls-creds}
+argument.
+
 @item x509verify=@var{/path/to/certificate/dir}
 
 Valid if @option{tls} is specified. Require that x509 credentials are used
@@ -1265,6 +1286,9 @@ path following this option specifies where the x509 certificates are to
 be loaded from. See the @ref{vnc_security} section for details on generating
 certificates.
 
+This option is now deprecated in favour of using the @option{tls-creds}
+argument.
+
 @item sasl
 
 Require that the client use SASL to authenticate with the VNC server.
ui/Makefile.objs +1 -1
index c62d4d9..0034fbb 100644
@@ -2,7 +2,7 @@ vnc-obj-y += vnc.o
 vnc-obj-y += vnc-enc-zlib.o vnc-enc-hextile.o
 vnc-obj-y += vnc-enc-tight.o vnc-palette.o
 vnc-obj-y += vnc-enc-zrle.o
-vnc-obj-$(CONFIG_VNC_TLS) += vnc-tls.o vnc-auth-vencrypt.o
+vnc-obj-y += vnc-auth-vencrypt.o
 vnc-obj-$(CONFIG_VNC_SASL) += vnc-auth-sasl.o
 vnc-obj-y += vnc-ws.o
 vnc-obj-y += vnc-jobs.o
ui/vnc-auth-sasl.c +19 -17
index 62a5fc4..a69d819 100644
@@ -525,21 +525,24 @@ void start_auth_sasl(VncState *vs)
         goto authabort;
     }
 
-#ifdef CONFIG_VNC_TLS
     /* Inform SASL that we've got an external SSF layer from TLS/x509 */
     if (vs->auth == VNC_AUTH_VENCRYPT &&
         vs->subauth == VNC_AUTH_VENCRYPT_X509SASL) {
-        gnutls_cipher_algorithm_t cipher;
+        Error *local_err = NULL;
+        int keysize;
         sasl_ssf_t ssf;
 
-        cipher = gnutls_cipher_get(vs->tls.session);
-        if (!(ssf = (sasl_ssf_t)gnutls_cipher_get_key_size(cipher))) {
-            VNC_DEBUG("%s", "cannot TLS get cipher size\n");
+        keysize = qcrypto_tls_session_get_key_size(vs->tls,
+                                                   &local_err);
+        if (keysize < 0) {
+            VNC_DEBUG("cannot TLS get cipher size: %s\n",
+                      error_get_pretty(local_err));
+            error_free(local_err);
             sasl_dispose(&vs->sasl.conn);
             vs->sasl.conn = NULL;
             goto authabort;
         }
-        ssf *= 8; /* tls key size is bytes, sasl wants bits */
+        ssf = keysize * 8; /* tls key size is bytes, sasl wants bits */
 
         err = sasl_setprop(vs->sasl.conn, SASL_SSF_EXTERNAL, &ssf);
         if (err != SASL_OK) {
@@ -549,20 +552,19 @@ void start_auth_sasl(VncState *vs)
             vs->sasl.conn = NULL;
             goto authabort;
         }
-    } else
-#endif /* CONFIG_VNC_TLS */
+    } else {
         vs->sasl.wantSSF = 1;
+    }
 
     memset (&secprops, 0, sizeof secprops);
-    /* Inform SASL that we've got an external SSF layer from TLS */
-    if (vs->vd->is_unix
-#ifdef CONFIG_VNC_TLS
-        /* Disable SSF, if using TLS+x509+SASL only. TLS without x509
-           is not sufficiently strong */
-        || (vs->auth == VNC_AUTH_VENCRYPT &&
-            vs->subauth == VNC_AUTH_VENCRYPT_X509SASL)
-#endif /* CONFIG_VNC_TLS */
-        ) {
+    /* Inform SASL that we've got an external SSF layer from TLS.
+     *
+     * Disable SSF, if using TLS+x509+SASL only. TLS without x509
+     * is not sufficiently strong
+     */
+    if (vs->vd->is_unix ||
+        (vs->auth == VNC_AUTH_VENCRYPT &&
+         vs->subauth == VNC_AUTH_VENCRYPT_X509SASL)) {
         /* If we've got TLS or UNIX domain sock, we don't care about SSF */
         secprops.min_ssf = 0;
         secprops.max_ssf = 0;
ui/vnc-auth-vencrypt.c +45 -35

111 lines not on this page · raw message

ui/vnc-tls.c deleted +0 -474

474 lines not on this page · raw message

ui/vnc-tls.h deleted +0 -69

69 lines not on this page · raw message

ui/vnc-ws.c +46 -36

106 lines not on this page · raw message

ui/vnc-ws.h +0 -2
index 9494225..4ab0a8c 100644
@@ -72,9 +72,7 @@ enum {
     WS_OPCODE_PONG = 0xA
 };
 
-#ifdef CONFIG_VNC_TLS
 void vncws_tls_handshake_io(void *opaque);
-#endif /* CONFIG_VNC_TLS */
 void vncws_handshake_read(void *opaque);
 long vnc_client_write_ws(VncState *vs);
 long vnc_client_read_ws(VncState *vs);
ui/vnc.c +216 -122

561 lines not on this page · raw message

ui/vnc.h +6 -9
index 194ccf7..4dd769c 100644
@@ -33,6 +33,7 @@
 #include "ui/console.h"
 #include "audio/audio.h"
 #include "qemu/bitmap.h"
+#include "crypto/tlssession.h"
 #include <zlib.h>
 #include <stdbool.h>
 
@@ -101,10 +102,7 @@ typedef void VncSendHextileTile(VncState *vs,
 
 typedef struct VncDisplay VncDisplay;
 
-#ifdef CONFIG_VNC_TLS
-#include "vnc-tls.h"
 #include "vnc-auth-vencrypt.h"
-#endif
 #ifdef CONFIG_VNC_SASL
 #include "vnc-auth-sasl.h"
 #endif
@@ -181,9 +179,8 @@ struct VncDisplay
     bool ws_tls; /* Used by websockets */
     bool lossy;
     bool non_adaptive;
-#ifdef CONFIG_VNC_TLS
-    VncDisplayTLS tls;
-#endif
+    QCryptoTLSCreds *tlscreds;
+    char *tlsaclname;
 #ifdef CONFIG_VNC_SASL
     VncDisplaySASL sasl;
 #endif
@@ -284,9 +281,7 @@ struct VncState
     int auth;
     int subauth; /* Used by VeNCrypt */
     char challenge[VNC_AUTH_CHALLENGE_SIZE];
-#ifdef CONFIG_VNC_TLS
-    VncStateTLS tls;
-#endif
+    QCryptoTLSSession *tls;
 #ifdef CONFIG_VNC_SASL
     VncStateSASL sasl;
 #endif
@@ -515,6 +510,8 @@ void vnc_client_write(void *opaque);
 
 ssize_t vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen);
 ssize_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen);
+ssize_t vnc_tls_pull(char *buf, size_t len, void *opaque);
+ssize_t vnc_tls_push(const char *buf, size_t len, void *opaque);
 
 /* Protocol I/O functions */
 void vnc_write(VncState *vs, const void *data, size_t len);
Signature · 1 line
2.4.3