Extract TLS handling code from VNC server
Subsystems (2)
v5 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
v4: https://lists.gnu.org/archive/html/qemu-devel/2015-08/msg02655.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 v5:
- Introduce use of -Wl,--whole-archive with libqemuutil.a to ensure QOM objects are not discarded by linker
- Remove nasty back dummy functions used to prevent QOM object discard by linker
- Extend QAPI enum generator to allow enum name prefix spec to override heuristics
- Switch to use QAPI to generate QCryptoTLSCredsEndpoint enum definition
- Fix misc bugs in error message strings
- Use alternate definition for DPRINTF
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 (9):
qapi: allow override of default enum prefix naming
make: ensure all members of libqemuutil.a are linked
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
Makefile.target | 7 +- configure | 53 +- crypto/Makefile.objs | 4 + crypto/tlscreds.c | 262 +++++++++ crypto/tlscredsanon.c | 236 ++++++++ crypto/tlscredspriv.h | 41 ++ crypto/tlscredsx509.c | 820 ++++++++++++++++++++++++++++ crypto/tlssession.c | 583 ++++++++++++++++++++ include/crypto/tlscreds.h | 68 +++ include/crypto/tlscredsanon.h | 112 ++++ include/crypto/tlscredsx509.h | 113 ++++ include/crypto/tlssession.h | 322 +++++++++++ qapi-schema.json | 3 + qapi/crypto.json | 20 + qemu-options.hx | 75 ++- scripts/qapi-types.py | 14 +- scripts/qapi.py | 9 +- 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 +- 33 files changed, 6053 insertions(+), 826 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 qapi/crypto.json 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
PATCH v5 1/9 qapi: allow override of default enum prefix naming
The camel_to_upper() method applies some heuristics to turn a mixed case type name into an all-uppercase name. This is used for example, to generate enum constant name prefixes.
The heuristics don't also generate a satisfactory name though. eg
{ 'enum': 'QCryptoTLSCredsEndpoint',
'data': ['client', 'server']}
Results in Q_CRYPTOTLS_CREDS_ENDPOINT_CLIENT. This has an undesirable _ after the initial Q and is missing an _ betweeen the CRYPTO & TLS strings.
Rather than try to add more and more heuristics to try to cope with this, simply allow the QAPI schema to specify the desired enum constant prefix explicitly.
eg
{ 'enum': 'QCryptoTLSCredsEndpoint',
'prefix': 'QCRYPTO_TLS_CREDS_ENDPOINT',
'data': ['client', 'server']}Now gives the QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT name.
--- scripts/qapi-types.py | 14 +++++++------- scripts/qapi.py | 9 ++++++--- 2 files changed, 13 insertions(+), 10 deletions(-)
scripts/qapi-types.py +7 -7
@@ -103,20 +103,20 @@ struct %(name)s return ret -def generate_enum_lookup(name, values): +def generate_enum_lookup(name, values, prefix=None): ret = mcgen(''' const char * const %(name)s_lookup[] = { ''', name=c_name(name)) i = 0 for value in values: - index = c_enum_const(name, value) + index = c_enum_const(name, value, prefix) ret += mcgen(''' [%(index)s] = "%(value)s", ''', index = index, value = value) - max_index = c_enum_const(name, 'MAX') + max_index = c_enum_const(name, 'MAX', prefix) ret += mcgen(''' [%(max_index)s] = NULL, };
@@ -125,7 +125,7 @@ const char * const %(name)s_lookup[] = { max_index=max_index) return ret -def generate_enum(name, values): +def generate_enum(name, values, prefix=None): name = c_name(name) lookup_decl = mcgen(''' extern const char * const %(name)s_lookup[];
@@ -143,7 +143,7 @@ typedef enum %(name)s i = 0 for value in enum_values: - enum_full_value = c_enum_const(name, value) + enum_full_value = c_enum_const(name, value, prefix) enum_decl += mcgen(''' %(enum_full_value)s = %(i)d, ''',
@@ -336,9 +336,9 @@ for expr in exprs: if expr.has_key('struct'): ret += generate_fwd_struct(expr['struct']) elif expr.has_key('enum'): - ret += generate_enum(expr['enum'], expr['data']) + "\n" + ret += generate_enum(expr['enum'], expr['data'], expr.get('prefix')) + "\n" ret += generate_fwd_enum_struct(expr['enum']) - fdef.write(generate_enum_lookup(expr['enum'], expr['data'])) + fdef.write(generate_enum_lookup(expr['enum'], expr['data'], expr.get('prefix'))) elif expr.has_key('union'): ret += generate_fwd_struct(expr['union']) + "\n" enum_define = discriminator_find_enum_define(expr)
scripts/qapi.py +6 -3
@@ -698,7 +698,7 @@ def check_exprs(exprs): expr = expr_elem['expr'] info = expr_elem['info'] if expr.has_key('enum'): - check_keys(expr_elem, 'enum', ['data']) + check_keys(expr_elem, 'enum', ['data'], ['prefix']) add_enum(expr['enum'], info, expr['data']) elif expr.has_key('union'): check_keys(expr_elem, 'union', ['data'],
@@ -818,8 +818,11 @@ def camel_to_upper(value): new_name += c return new_name.lstrip('_').upper() -def c_enum_const(type_name, const_name): - return camel_to_upper(type_name + '_' + const_name) +def c_enum_const(type_name, const_name, prefix=None): + if prefix is not None: + return prefix + '_' + camel_to_upper(const_name) + else: + return camel_to_upper(type_name + '_' + const_name) c_name_trans = string.maketrans('.-', '__')
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH v5 1/9] qapi: allow override of default enum prefix naming
Quoted 7 lines · Daniel P. Berrange
The
camel_to_upper()method applies some heuristics to turn a mixed case type name into an all-uppercase name. This is used for example, to generate enum constant name prefixes.The heuristics don't also generate a satisfactory name though. eg
Quoted 6 lines
{ 'enum': 'QCryptoTLSCredsEndpoint',
'data': ['client', 'server']}Results in
Q_CRYPTOTLS_CREDS_ENDPOINT_CLIENT. This has an undesirable _ after the initial Q and is missing an _ betweeen the CRYPTO & TLS strings.
s/betweeen/between/
Quoted 5 lines
Rather than try to add more and more heuristics to try to cope with this, simply allow the QAPI schema to specify the desired enum constant prefix explicitly.
Quoted 7 lines
eg
{ 'enum': 'QCryptoTLSCredsEndpoint',
'prefix': 'QCRYPTO_TLS_CREDS_ENDPOINT', 'data': ['client', 'server']}Now gives the
QCRYPTO_TLS_CREDS_ENDPOINT_CLIENTname.
Idea seems reasonable.
Quoted 6 lines
Signed-off-by Daniel P. Berrange <[email protected]>--- scripts/qapi-types.py | 14 +++++++------- scripts/qapi.py | 9 ++++++--- 2 files changed, 13 insertions(+), 10 deletions(-)
Missing documentation (docs/qapi-code-gen.txt) and a testsuite addition.
I suggest using 'prefix' on one of the existing enums intests/qapi-schema/qapi-schema-test.json, then fixing any fallout from 'make check-unit check-qapi-schema' to ensure it still passes - probably done correctly if this also touches tests/qapi-schema/qapi-schema-test.out and tests/test-qmp-*visitor.c.
Doesn't affect QMP wire ABI, so the new key shouldn't impact the output produced by Markus' pending series on introspection, although we may have some code collisions with the refactoring work going on there.
Quoted 7 lines
scripts/qapi-types.py +0
-0
scripts/qapi-types.py for expr in exprs: · line 341
Quoted 8 lines
scripts/qapi-types.py +2
-2@@ -336,9 +336,9 @@ for expr in exprs: if expr.has_key('struct'): ret += generate_fwd_struct(expr['struct']) elif expr.has_key('enum'): - ret += generate_enum(expr['enum'], expr['data']) + "\n" + ret += generate_enum(expr['enum'], expr['data'], expr.get('prefix')) + "\n" ret += generate_fwd_enum_struct(expr['enum']) - fdef.write(generate_enum_lookup(expr['enum'], expr['data'])) + fdef.write(generate_enum_lookup(expr['enum'], expr['data'], expr.get('prefix')))
Long line; I'd wrap after the last ','
scripts/qapi-types.py def check_exprs(exprs): · line 701
Quoted 7 lines
scripts/qapi-types.py +2
-1+++ b/scripts/qapi.py@@ -698,7 +698,7 @@ def check_exprs(exprs): expr = expr_elem['expr'] info = expr_elem['info'] if expr.has_key('enum'): - check_keys(expr_elem, 'enum', ['data']) + check_keys(expr_elem, 'enum', ['data'], ['prefix'])
I'd also amend check_enum() to ensure that the supplied prefix is a string (and not some other data structure); if you add a new error message that explicitly filters out an invalid prefix, then that is a further testsuite addition of a new negative test (tests/Makefile.am to add the the new tests/qapi-schema/*.json file, plus the corresponding .{out,exit,err} files to match expected results).
scripts/qapi-types.py def check_exprs(exprs):
Quoted 7 lines
scripts/qapi-types.py +5
-2-def c_enum_const(type_name, const_name): - return camel_to_upper(type_name + '_' + const_name) +def c_enum_const(type_name, const_name, prefix=None): + if prefix is not None: + return prefix + '_' + camel_to_upper(const_name) + else: + return camel_to_upper(type_name + '_' + const_name)
Would it be any easier to read as:
def c_enum_const(type_name, const_name, prefix=None):
if not prefix:
prefix = camel_to_upper(type_name)
return prefix + '_' + camel_to_upper(const_name)
But I'm not sure if that would introduce any subtle changes to existing enums.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v5 1/9] qapi: allow override of default enum prefix naming
Quoted 39 lines · Eric Blake
Quoted 7 lines · Daniel P. Berrange
The
camel_to_upper()method applies some heuristics to turn a mixed case type name into an all-uppercase name. This is used for example, to generate enum constant name prefixes.The heuristics don't also generate a satisfactory name though. eg
Quoted 6 lines
{ 'enum': 'QCryptoTLSCredsEndpoint',
'data': ['client', 'server']}Results in
Q_CRYPTOTLS_CREDS_ENDPOINT_CLIENT. This has an undesirable _ after the initial Q and is missing an _ betweeen the CRYPTO & TLS strings.s/betweeen/between/
Quoted 5 lines
Rather than try to add more and more heuristics to try to cope with this, simply allow the QAPI schema to specify the desired enum constant prefix explicitly.
Quoted 7 lines
eg
{ 'enum': 'QCryptoTLSCredsEndpoint',
'prefix': 'QCRYPTO_TLS_CREDS_ENDPOINT', 'data': ['client', 'server']}Now gives the
QCRYPTO_TLS_CREDS_ENDPOINT_CLIENTname.Idea seems reasonable.
Quoted 6 lines
Signed-off-by Daniel P. Berrange <[email protected]>--- scripts/qapi-types.py | 14 +++++++------- scripts/qapi.py | 9 ++++++--- 2 files changed, 13 insertions(+), 10 deletions(-)
Quoted 6 lines
Missing documentation (
docs/qapi-code-gen.txt) and a testsuite addition.
I suggest using 'prefix' on one of the existing enums intests/qapi-schema/qapi-schema-test.json, then fixing any fallout from 'make check-unit check-qapi-schema' to ensure it still passes - probably done correctly if this also touches tests/qapi-schema/qapi-schema-test.out andtests/test-qmp-*visitor.c.
test-qmp-*visitor is not affected because we're not changing the data types / structure in any way - just the naming of constants and that is not checked by any test-qmp-*vistor test.
I've updated qapi-schema-test.out though, and added a test for a bad prefix type.
Quoted 6 lines
Quoted 6 lines
+++
b/scripts/qapi.pyscripts/qapi.py +0
-1@@ -698,7 +698,7 @@ def check_exprs(exprs): expr = expr_elem['expr'] info = expr_elem['info'] if expr.has_key('enum'): - check_keys(expr_elem, 'enum', ['data'])
Quoted 8 lines
scripts/qapi.py def check_exprs(exprs): · line 701
Quoted 1 line
scripts/qapi.py +1
-0+ check_keys(expr_elem, 'enum', ['data'], ['prefix'])I'd also amend
check_enum()to ensure that the supplied prefix is a string (and not some other data structure); if you add a new error message that explicitly filters out an invalid prefix, then that is a further testsuite addition of a new negative test (tests/Makefile.am to add the the new tests/qapi-schema/*.json file, plus the corresponding .{out,exit,err} files to match expected results).
Yep, done that now.
Quoted 10 lines
scripts/qapi.py def camel_to_upper(value): · line 825
Quoted 7 lines
scripts/qapi.py +5
-2-def c_enum_const(type_name, const_name): - return camel_to_upper(type_name + '_' + const_name) +def c_enum_const(type_name, const_name, prefix=None): + if prefix is not None: + return prefix + '_' + camel_to_upper(const_name) + else: + return camel_to_upper(type_name + '_' + const_name)Would it be any easier to read as:
Quoted 7 lines
def
c_enum_const(type_name, const_name, prefix=None):
if not prefix:
prefix =camel_to_upper(type_name)
return prefix + '_' +camel_to_upper(const_name)But I'm not sure if that would introduce any subtle changes to existing enums.
That doesn't quite work because if the const_name starts with an '_', camel_to_upper would previously collapse the repeated '_'. I can tweak it a bit to be more readable and avoid this problem though.
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 :|
PATCH v5 2/9 make: ensure all members of libqemuutil.a are linked
In reply to Daniel P. BerrangeThe libqemuutil.a archive may contain QOM objects which are only indirectly referenced via __attribute__((constructor)) annotations. Despite the constructor annotation the linker will think these objects are unused by the executable and so drop them when linking, to the system emulator. As a result the objects in question will be missing from QOM.
Using the -Wl,--whole-archive flag instructs the linker to pull in everything in libqemuutil.a regardless of whether it thinks it is used or not.
--- Makefile.target | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
Makefile.target +6 -1
@@ -180,8 +180,13 @@ all-obj-$(CONFIG_SOFTMMU) += $(block-obj-y) $(QEMU_PROG_BUILD): config-devices.mak # build either PROG or PROGW +# We must use --whole-archive with libqemuutil.a otherwise the +# linker will drop any objects from the archive which are only +# indirectly referenced via __attribute__(constructor) annotations $(QEMU_PROG_BUILD): $(all-obj-y) ../libqemuutil.a ../libqemustub.a - $(call LINK, $(filter-out %.mak, $^)) + $(call LINK, $(filter-out %.mak, $(sort $(all-obj-y)))) \ + -Wl,--whole-archive ../libqemuutil.a -Wl,--no-whole-archive \ + ../libqemustub.a ifdef CONFIG_DARWIN $(call quiet-command,Rez -append $(SRC_PATH)/pc-bios/qemu.rsrc -o $@," REZ $(TARGET_DIR)$@") $(call quiet-command,SetFile -a C $@," SETFILE $(TARGET_DIR)$@")
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH v5 2/9] make: ensure all members of libqemuutil.a are linked
Quoted 21 lines · Daniel P. Berrange
The libqemuutil.a archive may contain QOM objects which are only indirectly referenced via __attribute__((constructor)) annotations. Despite the constructor annotation the linker will think these objects are unused by the executable and so drop them when linking, to the system emulator. As a result the objects in question will be missing from QOM.
Using the -Wl,--whole-archive flag instructs the linker to pull in everything in libqemuutil.a regardless of whether it thinks it is used or not.
Signed-off-by Daniel P. Berrange <[email protected]>--- Makefile.target | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)Makefile.target +0
-0
Makefile.target all-obj-$(CONFIG_SOFTMMU) += $(block-obj-y) · line 187
Quoted 9 lines
Makefile.target +4
-1@@ -180,8 +180,13 @@ all-obj-$(CONFIG_SOFTMMU) += $(block-obj-y) $(QEMU_PROG_BUILD): config-devices.mak # build either PROG or PROGW +# We must use --whole-archive with libqemuutil.a otherwise the +# linker will drop any objects from the archive which are only +# indirectly referenced via __attribute__(constructor) annotations $(QEMU_PROG_BUILD): $(all-obj-y) ../libqemuutil.a ../libqemustub.a - $(call LINK, $(filter-out %.mak, $^)) + $(call LINK, $(filter-out %.mak, $(sort $(all-obj-y)))) \
Why the added $(sort)? Should that be a separate patch?
Makefile.target all-obj-$(CONFIG_SOFTMMU) += $(block-obj-y) · line 189
Quoted 2 lines
Makefile.target +2
-0+ -Wl,--whole-archive ../libqemuutil.a -Wl,--no-whole-archive \ + ../libqemustub.a
Otherwise makes sense to me.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v5 2/9] make: ensure all members of libqemuutil.a are linked
Quoted 24 lines · Eric Blake
Quoted 23 lines · Daniel P. Berrange
The libqemuutil.a archive may contain QOM objects which are only indirectly referenced via __attribute__((constructor)) annotations. Despite the constructor annotation the linker will think these objects are unused by the executable and so drop them when linking, to the system emulator. As a result the objects in question will be missing from QOM.
Using the -Wl,--whole-archive flag instructs the linker to pull in everything in libqemuutil.a regardless of whether it thinks it is used or not.
Signed-off-by Daniel P. Berrange <[email protected]>--- Makefile.target | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)Makefile.target +0
-0@@ -180,8 +180,13 @@ all-obj-$(CONFIG_SOFTMMU) += $(block-obj-y) $(QEMU_PROG_BUILD): config-devices.mak
Quoted 9 lines
Makefile.target all-obj-$(CONFIG_SOFTMMU) += $(block-obj-y) · line 187
Quoted 7 lines
Makefile.target +4
-1# build either PROG or PROGW +# We must use --whole-archive with libqemuutil.a otherwise the +# linker will drop any objects from the archive which are only +# indirectly referenced via __attribute__(constructor) annotations $(QEMU_PROG_BUILD): $(all-obj-y) ../libqemuutil.a ../libqemustub.a - $(call LINK, $(filter-out %.mak, $^)) + $(call LINK, $(filter-out %.mak, $(sort $(all-obj-y)))) \Why the added $(sort)? Should that be a separate patch?
$(all-obj-y) has some .o's listed multiple times. $^ removes duplicates, avoiding this problem, but if we use $(all-obj-y) we must now remove duplicates ourselves. $(sort) removes duplicates and AFAICT, there's no built-in make function that can remove duplicates without sorting.
Quoted 6 lines
Makefile.target all-obj-$(CONFIG_SOFTMMU) += $(block-obj-y) · line 189
Quoted 3 lines
Makefile.target +2
-0+ -Wl,--whole-archive ../libqemuutil.a -Wl,--no-whole-archive \ + ../libqemustub.aOtherwise makes sense to me.
Reviewed-by Eric Blake <[email protected]>
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 :|
PATCH v5 3/9 crypto: introduce new base module for TLS credentials
In reply to Daniel P. BerrangeIntroduce 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 manage TLS credentials independently 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/tlscreds.c | 262 ++++++++++++++++++++++++++++++++++++++++++++++ crypto/tlscredspriv.h | 41 ++++++++ include/crypto/tlscreds.h | 68 ++++++++++++ qapi-schema.json | 3 + qapi/crypto.json | 20 ++++ tests/Makefile | 4 +- 7 files changed, 397 insertions(+), 2 deletions(-) create mode 100644 crypto/tlscreds.c create mode 100644 crypto/tlscredspriv.h create mode 100644 include/crypto/tlscreds.h create mode 100644 qapi/crypto.json
crypto/Makefile.objs +1 -0
@@ -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/tlscreds.c new file +262 -0
262 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 +68 -0
68 lines not on this page · raw message
qapi-schema.json +3 -0
@@ -5,6 +5,9 @@ # QAPI common definitions { 'include': 'qapi/common.json' } +# QAPI crypto definitions +{ 'include': 'qapi/crypto.json' } + # QAPI block definitions { 'include': 'qapi/block.json' }
qapi/crypto.json new file +20 -0
@@ -0,0 +1,20 @@ +# -*- Mode: Python -*- +# +# QAPI crypto definitions + +## +# QCryptoTLSCredsEndpoint: +# +# The type of network endpoint that will be using the credentials. +# Most types of credential require different setup / structures +# depending on whether they will be used in a server vs a client. +# +# @client: the network endpoint is acting as the client +# +# @server: the network endpoint is acting as the server +# +# Since: 2.5 +## +{ 'enum': 'QCryptoTLSCredsEndpoint', + 'prefix': 'QCRYPTO_TLS_CREDS_ENDPOINT', + 'data': ['client', 'server']}
tests/Makefile +2 -2
@@ -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
Re: [Qemu-devel] [PATCH v5 3/9] crypto: introduce new base module for TLS credentials
Quoted 31 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 manage TLS credentials independently 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.
Signed-off-by Daniel P. Berrange <[email protected]>---
+++
b/qapi/crypto.json
qapi/crypto.json line 10
Quoted 10 lines
qapi/crypto.json new file +10
-0@@ -0,0 +1,20 @@ +# -*- Mode: Python -*- +# +# QAPI crypto definitions + +## +# QCryptoTLSCredsEndpoint: +# +# The type of network endpoint that will be using the credentials. +# Most types of credential require different setup / structures +# depending on whether they will be used in a server vs a client.
I'm not sure if 'vs.' is more common than 'vs' when abbreviating 'versus'; or you could skip the debate by s/vs/or/
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH v5 4/9 crypto: introduce new module for TLS anonymous credentials
In reply to Daniel P. BerrangeIntroduce 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 example, 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/tlscredsanon.c | 236 ++++++++++++++++++++++++++++++++++++++++++ include/crypto/tlscredsanon.h | 112 ++++++++++++++++++++ qemu-options.hx | 20 ++++ 4 files changed, 369 insertions(+) create mode 100644 crypto/tlscredsanon.c create mode 100644 include/crypto/tlscredsanon.h
crypto/Makefile.objs +1 -0
@@ -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/tlscredsanon.c new file +236 -0
236 lines not on this page · raw message
include/crypto/tlscredsanon.h new file +112 -0
112 lines not on this page · raw message
qemu-options.hx +20 -0
@@ -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
Re: [Qemu-devel] [PATCH v5 4/9] crypto: introduce new module for TLS anonymous credentials
Quoted 19 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
Example using pre-created diffie-hellman parameters
$QEMU -object tls-creds-anon,id=tls0,endpoint=server,\
dir=/path/to/creds/dirThe 'id' value in the -object args will be used to associate the credentials with the network services. For example, when the VNC server is later converted it would use
Quoted 5 lines
$QEMU -object tls-creds-anon,id=tls0,.... \ -vnc 127.0.0.1:1,tls-creds=tls0Signed-off-by Daniel P. Berrange <[email protected]>---
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH v5 5/9 crypto: introduce new module for TLS x509 credentials
In reply to Daniel P. BerrangeIntroduce 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/tlscredsx509.c | 276 ++++++++++++++++++++++++++++++++++++++++++ include/crypto/tlscredsx509.h | 112 +++++++++++++++++ qemu-options.hx | 27 +++++ 4 files changed, 416 insertions(+) create mode 100644 crypto/tlscredsx509.c create mode 100644 include/crypto/tlscredsx509.h
crypto/Makefile.objs +1 -0
@@ -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/tlscredsx509.c new file +276 -0
276 lines not on this page · raw message
include/crypto/tlscredsx509.h new file +112 -0
112 lines not on this page · raw message
qemu-options.hx +27 -0
@@ -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
Re: [Qemu-devel] [PATCH v5 5/9] 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=yesThe '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 7 lines
server is later converted it would use
$QEMU -object tls-creds-x509,id=tls0,.... \ -vnc 127.0.0.1:1,tls-creds=tls0Signed-off-by Daniel P. Berrange <[email protected]>---
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH v5 6/9 crypto: add sanity checking of TLS x509 credentials
In reply to Daniel P. BerrangeIf 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
@@ -415,6 +415,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`
@@ -2215,6 +2218,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
@@ -4513,6 +4529,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"
@@ -4881,6 +4898,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
@@ -5204,6 +5224,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
@@ -100,6 +100,7 @@ struct QCryptoTLSCredsX509 {#ifdef CONFIG_GNUTLS gnutls_certificate_credentials_t data;#endif +bool sanityCheck; };
tests/.gitignore +3 -0
@@ -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
@@ -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.
Re: [Qemu-devel] [PATCH v5 6/9] crypto: add sanity checking of TLS x509 credentials
Quoted 47 lines · 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.cfrom 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.Signed-off-by Daniel P. Berrange <[email protected]>---
+++
b/crypto/tlscredsx509.ccrypto/tlscredsx509.c +12
-0@@ -38,6 +38,514 @@ +static int +qcrypto_tls_creds_check_cert_pair (gnutls_x509_crt_t cert, +const char *certFile, +gnutls_x509_crt_t *cacerts, +size_t ncacerts, +const char *cacertFile, +bool isServer, +Error **errp) +{ +if (status !=0 ) { +const char *reason ="Invalid certificate" ; +
crypto/tlscredsx509.c line 62
Quoted 10 lines
crypto/tlscredsx509.c +10
-0+if (status &GNUTLS_CERT_INVALID ) { + reason ="The certificate is not trusted." ; + } + +if (status &GNUTLS_CERT_SIGNER_NOT_FOUND ) { + reason ="The certificate hasn't got a known issuer." ; + } + +if (status &GNUTLS_CERT_REVOKED ) { + reason ="The certificate has been revoked." ;
The trailing dots seem unusual here, since most of your code doesn't have them.
tests/crypto-tls-x509-helpers.c line 175
Quoted 10 lines
tests/crypto-tls-x509-helpers.c new file +8
-0+++ b/tests/crypto-tls-x509-helpers.c +void +test_tls_generate_cert (QCryptoTLSTestCertReq *req, +gnutls_x509_crt_t ca) +{ +gnutls_x509_crt_t crt; +int err; +static char buffer[1024 *1024 ];
Space around operator '*'
tests/crypto-tls-x509-helpers.c line 180
Quoted 5 lines
tests/crypto-tls-x509-helpers.c new file +5
-0+size_t size =sizeof (buffer); +char serial[5 ] = {1 ,2 ,3 ,4 ,0 }; +gnutls_datum_t der; +time_t start =time (NULL ) + (60 *60 *req->start_offset); + time_t expire = time(NULL) + (60*60*(req->expire_offset
and again
tests/pkix_asn1_tab.c line 3
Quoted 5 lines
tests/pkix_asn1_tab.c new file +4
-0+++ b/tests/pkix_asn1_tab.c@@ -0,0 +1,1103 @@ +/* +* This file is taken from gnutls 1.6.3 under the GPLv2+ +*/
Is this missing a copyright statement? Even if gnutls 1.6.3 didn't mention copyright per-file, it might be nice to mention the copyright owner of the overall release of that old tarball.
Findings are minor, and overall seems nice to have. And adding to the testsuite is always a nice proof of validity.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v5 6/9] crypto: add sanity checking of TLS x509 credentials
Quoted 84 lines · Eric Blake
Quoted 47 lines · 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.cfrom 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.Signed-off-by Daniel P. Berrange <[email protected]>---
+++
b/crypto/tlscredsx509.ccrypto/tlscredsx509.c +12
-0@@ -38,6 +38,514 @@ +static int +qcrypto_tls_creds_check_cert_pair (gnutls_x509_crt_t cert, +const char *certFile, +gnutls_x509_crt_t *cacerts, +size_t ncacerts, +const char *cacertFile, +bool isServer, +Error **errp) +{ +if (status !=0 ) { +const char *reason ="Invalid certificate" ; +crypto/tlscredsx509.c line 62
Quoted 10 lines
crypto/tlscredsx509.c +10
-0+if (status &GNUTLS_CERT_INVALID ) { + reason ="The certificate is not trusted." ; + } + +if (status &GNUTLS_CERT_SIGNER_NOT_FOUND ) { + reason ="The certificate hasn't got a known issuer." ; + } + +if (status &GNUTLS_CERT_REVOKED ) { + reason ="The certificate has been revoked." ;The trailing dots seem unusual here, since most of your code doesn't have them.
tests/crypto-tls-x509-helpers.c line 175
Quoted 10 lines
tests/crypto-tls-x509-helpers.c new file +8
-0+++ b/tests/crypto-tls-x509-helpers.c +void +test_tls_generate_cert (QCryptoTLSTestCertReq *req, +gnutls_x509_crt_t ca) +{ +gnutls_x509_crt_t crt; +int err; +static char buffer[1024 *1024 ];Space around operator '*'
tests/crypto-tls-x509-helpers.c line 180
Quoted 5 lines
tests/crypto-tls-x509-helpers.c new file +5
-0+size_t size =sizeof (buffer); +char serial[5 ] = {1 ,2 ,3 ,4 ,0 }; +gnutls_datum_t der; +time_t start =time (NULL ) + (60 *60 *req->start_offset); + time_t expire = time(NULL) + (60*60*(req->expire_offsetand again
Quoted 9 lines
tests/pkix_asn1_tab.c line 3
Quoted 5 lines
tests/pkix_asn1_tab.c new file +4
-0+++ b/tests/pkix_asn1_tab.c@@ -0,0 +1,1103 @@ +/* +* This file is taken from gnutls 1.6.3 under the GPLv2+ +*/ Is this missing a copyright statement? Even if gnutls 1.6.3 didn't mention copyright per-file, it might be nice to mention the copyright owner of the overall release of that old tarball.
The original gnutls file didn't have any header at all, so I added the mention that it was GPLv2+ per the global gnutls license file. I didn't put any Copyright as it was not clear who exactly it add. I guess the answer might lie in gnutls git history somewhere if you think that's important.
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 :|
PATCH v5 7/9 crypto: introduce new module for handling TLS sessions
In reply to Daniel P. BerrangeIntroduce 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 | 583 +++++++++++++++++++++++++++++++++++++++++ include/crypto/tlssession.h | 322 +++++++++++++++++++++++ tests/.gitignore | 4 + tests/Makefile | 3 + tests/test-crypto-tlssession.c | 534 +++++++++++++++++++++++++++++++++++++ 6 files changed, 1447 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
@@ -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 +583 -0
583 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
@@ -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
@@ -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
Re: [Qemu-devel] [PATCH v5 7/9] crypto: introduce new module for handling TLS sessions
Quoted 29 lines · 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.
Signed-off-by Daniel P. Berrange <[email protected]>--- crypto/Makefile.objs | 1 + crypto/tlssession.c | 583 +++++++++++++++++++++++++++++++++++++++++ include/crypto/tlssession.h | 322 +++++++++++++++++++++++ tests/.gitignore | 4 + tests/Makefile | 3 + tests/test-crypto-tlssession.c | 534 +++++++++++++++++++++++++++++++++++++ 6 files changed, 1447 insertions(+) create mode 100644 crypto/tlssession.c create mode 100644 include/crypto/tlssession.h create mode 100644 tests/test-crypto-tlssession.c+++
b/crypto/tlssession.c
Review
Quoted 2 lines
(quoted diff) +2
-0+ +struct _QCryptoTLSSession {
Why the leading underscore before a capital? This collides with the namespace reserved to the compiler/library toolchain.
crypto/tlssession.c line 60
Quoted 3 lines
crypto/tlssession.c new file +3
-0+ +void +qcrypto_tls_session_free (QCryptoTLSSession *session)
qemu coding style generally puts the return type and function name on the same line; but if checkpatch.pl isn't complaining, I won't insist. (I actually like the return type on a separate line, as emacs handles it nicer)
crypto/tlssession.c line 261
Quoted 9 lines
crypto/tlssession.c new file +8
-0+ +static int +qcrypto_tls_session_check_certificate (QCryptoTLSSession *session, +Error **errp) +{ +int ret; + +for (i =0 ; i < nCerts ; i++) {
No space before ';' (twice)
Quoted 5 lines
crypto/tlssession.c new file +5
-0+gnutls_x509_crt_t cert; + +if (gnutls_x509_crt_init (&cert) <0 ) { +return -1 ; + }
crypto/tlssession.c line 276
Quoted 10 lines
crypto/tlssession.c new file +10
-0+ +if (gnutls_x509_crt_import (cert, &certs[i],GNUTLS_X509_FMT_DER ) <0 ) { +gnutls_x509_crt_deinit (cert); +return -1 ; + } + +if (gnutls_x509_crt_get_expiration_time (cert) < now) { +error_setg (errp,"The certificate has expired" ); +gnutls_x509_crt_deinit (cert); +return -1 ;
Lots of common cleanup; worth consolidating it into an out: label?
crypto/tlssession.c line 299
Quoted 9 lines
crypto/tlssession.c new file +9
-0+if (i ==0 ) { +size_t dnameSize =1024 ; + session->peername =g_malloc (dnameSize); + requery: + ret =gnutls_x509_crt_get_dn (cert, session->peername, &dnameSize); +if (ret <0 ) { +if (ret ==GNUTLS_E_SHORT_MEMORY_BUFFER ) { + session->peername =g_realloc (session->peername, + dnameSize);
Indentation is off.
Quoted 10 lines
+++
b/include/crypto/tlssession.h+ * sess = qcrypto_tls_session_new(creds,
+ * "vnc.example.com", + * NULL, + * QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT, + * errp);+ * if (sess == NULL) {
+ * return -1;
+ * }
Indentation is off
Quoted 5 lines
+ *
+ * qcrypto_tls_session_set_callbacks(sess,+ * mysock_send, + * mysock_recv + * GINT_TO_POINTER(fd));
Quoted 10 lines
+ *
+ * while (1) {
+ * if (qcrypto_tls_session_handshake(sess, errp)< 0) {
+ *qcrypto_tls_session_free(sess);
+ * return -1;
+ * }
+ *
+ * switch(qcrypto_tls_session_get_handshake_status(sess)) {
+ * caseQCRYPTO_TLS_HANDSHAKE_COMPLETE:
+ * if (qcrypto_tls_session_check_credentials(sess, errp)< )) {
Unusual indentation
include/crypto/tlssession.h line 108
Quoted 2 lines
include/crypto/tlssession.h new file +2
-0+ +typedef struct _QCryptoTLSSession QCryptoTLSSession ;
Naming the struct without a leading _ should be fine.
include/crypto/tlssession.h line 130
Quoted 7 lines
include/crypto/tlssession.h new file +6
-0+ + +/** +* qcrypto_tls_session_new: +* The @aclname parameter (optionally) specifies the name +* of an access controll list that will be used to validate
s/controll/control/
Quoted 6 lines
+/**
+ *qcrypto_tls_session_set_callbacks:+ *
+ * The @readFunc callback will be passed a pointer to fill
+ * with encrypted data received fro mthe remote peer
s/fro mthe/from the/
Quoted 7 lines
+/**
+ *qcrypto_tls_session_read:
+ * @sess: the TLS session object
+ * @buf: to fill with plain text received
+ * @len: the length of @buf
+ *
+ * Receive upto @len bytes of data from the remote peer
s/upto/up to/
tests/test-crypto-tlssession.c line 103
Quoted 10 lines
+++
b/tests/test-crypto-tlssession.ctests/test-crypto-tlssession.c new file +8
-0+ +/* +* This tests validation checking of peer certificates +* +* This is replicating the checks that are done for an +* active TLS session after handshake completes. To +* simulate that we create our TLS contexts, skipping +* sanity checks. When then get a socketpair, and
s/When/We/
tests/test-crypto-tlssession.c line 123
Quoted 7 lines
+ * initiate a TLS session across them. Finally do
+ * do actual cert validation tests
+ */tests/test-crypto-tlssession.c new file +3
-0+static void test_crypto_tls_session(const void *opaque) + /* We'll use this for our fake client-server connection */ + g_assert(socketpair(AF_UNIX, SOCK_STREAM, 0, channel) == 0);
Evil to stick side-effects in a g_assert() (not as evil as doing it in assert(), but still something you should hoist out separately).
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v5 7/9] crypto: introduce new module for handling TLS sessions
Quoted 28 lines · Eric Blake
Quoted 26 lines · 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.
Signed-off-by Daniel P. Berrange <[email protected]>--- crypto/Makefile.objs | 1 + crypto/tlssession.c | 583 +++++++++++++++++++++++++++++++++++++++++ include/crypto/tlssession.h | 322 +++++++++++++++++++++++ tests/.gitignore | 4 + tests/Makefile | 3 + tests/test-crypto-tlssession.c | 534 +++++++++++++++++++++++++++++++++++++ 6 files changed, 1447 insertions(+) create mode 100644 crypto/tlssession.c create mode 100644 include/crypto/tlssession.h create mode 100644 tests/test-crypto-tlssession.c
Quoted 7 lines
Review
Quoted 4 lines
+++
b/crypto/tlssession.c(quoted diff) +2
-0+ +struct _QCryptoTLSSession {Why the leading underscore before a capital? This collides with the namespace reserved to the compiler/library toolchain.
Just left over from my conversion from libvirt code, so will remove that.
Quoted 8 lines
crypto/tlssession.c line 60
Quoted 3 lines
crypto/tlssession.c new file +3
-0+ +void +qcrypto_tls_session_free (QCryptoTLSSession *session)qemu coding style generally puts the return type and function name on the same line; but if checkpatch.pl isn't complaining, I won't insist. (I actually like the return type on a separate line, as emacs handles it nicer)
I ended up putting the return type on a separate line because in several places it helped keep under the 80 character limit.
Quoted 29 lines
Quoted 10 lines
+++
b/include/crypto/tlssession.h+ * sess = qcrypto_tls_session_new(creds,
+ * "vnc.example.com", + * NULL, + * QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT, + * errp);+ * if (sess == NULL) {
+ * return -1;
+ * }Indentation is off
Quoted 5 lines
+ *
+ * qcrypto_tls_session_set_callbacks(sess,+ * mysock_send, + * mysock_recv + * GINT_TO_POINTER(fd));Quoted 10 lines
+ *
+ * while (1) {
+ * if (qcrypto_tls_session_handshake(sess, errp)< 0) {
+ *qcrypto_tls_session_free(sess);
+ * return -1;
+ * }
+ *
+ * switch(qcrypto_tls_session_get_handshake_status(sess)) {
+ * caseQCRYPTO_TLS_HANDSHAKE_COMPLETE:
+ * if (qcrypto_tls_session_check_credentials(sess, errp)< )) {
Quoted 1 line
Unusual indentation
Hehe, reviewing code examples in the comments is nice :-)
Quoted 5 lines
tests/test-crypto-tlssession.c line 123
Quoted 2 lines
tests/test-crypto-tlssession.c new file +2
-0+/* We'll use this for our fake client-server connection */ +g_assert (socketpair (AF_UNIX ,SOCK_STREAM ,0 , channel) ==0 );Evil to stick side-effects in a
g_assert()(not as evil as doing it inassert(), but still something you should hoist out separately).
yep, will separate.
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 :|
PATCH v5 8/9 ui: fix return type for VNC I/O functions to be ssize_t
In reply to Daniel P. BerrangeVarious 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
@@ -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
@@ -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
Re: [Qemu-devel] [PATCH v5 8/9] ui: fix return type for VNC I/O functions to be ssize_t
Quoted 6 lines · 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_tto match the signature of read/write APIs and thus avoid potential for integer truncation / wraparound.
Quoted 6 lines
Signed-off-by Daniel P. Berrange <[email protected]>--- ui/vnc.c | 36 ++++++++++++++++++------------------ ui/vnc.h | 6 +++--- 2 files changed, 21 insertions(+), 21 deletions(-)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH v5 9/9 ui: convert VNC server to use QCryptoTLSSession
In reply to Daniel P. BerrangeSwitch 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
@@ -242,7 +242,6 @@ vnc="yes" sparse="no" uuid="" vde="" -vnc_tls="" vnc_sasl="" vnc_jpeg="" vnc_png=""
@@ -882,10 +881,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"
@@ -2375,28 +2370,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
@@ -4540,7 +4513,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"
@@ -4746,9 +4718,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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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 VncDisplaybool 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 VncStateint 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
Re: [Qemu-devel] [PATCH v5 9/9] ui: convert VNC server to use QCryptoTLSSession
Quoted 11 lines · 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_TLSconditionals 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.
We don't want to disable the old way right away; does it issue a warning on the command line, or does it silently just act as sugar for the correct new way? ...
Quoted 7 lines
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.
Quoted 5 lines
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.
...Ah, I should have just read further :)
qemu-options.hx By definition the Websocket port is 5700+@var{display}. If @var{host} is · line 1218
Quoted 13 lines
Signed-off-by Daniel P. Berrange <[email protected]>---
+++ b/qemu-options.hx
qemu-options.hx +2
-2@@ -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
s/uncrypted/unencrypted/
qemu-options.hx uses anonymous TLS credentials so is susceptible to a man-in-the-middle · line 1262
Quoted 6 lines
qemu-options.hx +2
-0@@ -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.
I don't know if US or UK spellings have precedence in the user-facing documentation.
Quoted 19 lines
ui/vnc-auth-sasl.c +5
-5+++ b/ui/vnc-auth-sasl.c@@ -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);
ui/vnc-auth-sasl.c void start_auth_sasl(VncState *vs) · line 545
Quoted 10 lines
ui/vnc-auth-sasl.c +5
-1+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 */
Worth using the standard CHAR_BITS here rather than a magic number?
ui/vnc-auth-sasl.c void start_auth_sasl(VncState *vs) · line 557
Quoted 12 lines
ui/vnc-auth-sasl.c +2
-2err =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 ; + }
Not needed for this patch, but a followup patch that converts wantSSF to bool might be nice.
Quoted 9 lines
ui/vnc-auth-vencrypt.c +1
-4+++ b/ui/vnc-auth-vencrypt.c@@ -67,37 +67,41 @@ static void vnc_tls_handshake_io(void *opaque);static int vnc_start_vencrypt_handshake (VncState *vs) { -int ret; - -if ((ret =gnutls_handshake (vs->tls.session)) <0 ) { -if (!gnutls_error_is_fatal (ret)) {
ui/vnc-auth-vencrypt.c static void vnc_tls_handshake_io(void *opaque); · line 83
Quoted 10 lines
ui/vnc-auth-vencrypt.c +0
-10-VNC_DEBUG ("Handshake interrupted (blocking)\n" ); -if (!gnutls_record_get_direction (vs->tls.session)) -qemu_set_fd_handler (vs->csock, vnc_tls_handshake_io,NULL , vs); -else -qemu_set_fd_handler (vs->csock,NULL , vnc_tls_handshake_io, vs); -return 0 ; - } -VNC_DEBUG ("Handshake failed %s\n" ,gnutls_strerror (ret)); -vnc_client_error (vs); -return -1 ;
Old code returns -1 on failure...
Quoted 4 lines
ui/vnc-auth-vencrypt.c +4
-0+case QCRYPTO_TLS_HANDSHAKE_SENDING : +VNC_DEBUG ("Handshake interrupted (blocking write)\n" ); +qemu_set_fd_handler (vs->csock,NULL , vnc_tls_handshake_io, vs); +break ;
ui/vnc-auth-vencrypt.c static void vnc_tls_handshake_io(void *opaque); · line 105
Quoted 10 lines
ui/vnc-auth-vencrypt.c +6
-1+ } -start_auth_vencrypt_subauth (vs); +return 0 ; + error: +VNC_DEBUG ("Handshake failed %s\n" ,error_get_pretty (err)); +error_free (err); +vnc_client_error (vs);return 0 ;
...new code does not. Oops.
Quoted 15 lines
ui/vnc-ws.c +5
-4+++ b/ui/vnc-ws.cstatic int vncws_start_tls_handshake (VncState *vs) { - } -VNC_DEBUG ("Handshake failed %s\n" ,gnutls_strerror (ret)); -vnc_client_error (vs); -return -1 ; +case QCRYPTO_TLS_HANDSHAKE_SENDING : +VNC_DEBUG ("Handshake interrupted (blocking write)\n" ); +qemu_set_fd_handler (vs->csock,NULL , vncws_tls_handshake_io, vs); +break ; }
ui/vnc-ws.c line 60
Quoted 10 lines
ui/vnc-ws.c +5
-2-VNC_DEBUG ("Handshake done, switching to TLS data mode\n" ); -qemu_set_fd_handler (vs->csock, vncws_handshake_read,NULL , vs); +return 0 ; + error: +VNC_DEBUG ("Handshake failed %s\n" ,error_get_pretty (err)); +error_free (err); +vnc_client_error (vs);return 0 ;
And again.
Quoted 7 lines
ui/vnc.c +1
-1+++ b/ui/vnc.c@@ -3301,13 +3303,12 @@ static QemuOptsList qemu_vnc_opts = { }; -static void
ui/vnc.c static QemuOptsList qemu_vnc_opts = { · line 3312
Quoted 10 lines
ui/vnc.c +3
-3+static int vnc_display_setup_auth (VncDisplay *vs,bool password,bool sasl, - bool tls, - bool x509, - bool websocket) +bool websocket, +Error **errp) {
Adding a return value and an errp pointer? Can't callers just check whether errp was set, and then you can leave this as returning void?
Quoted 26 lines
ui/vnc.c +26
-0+/* +* Handle back compat with old CLI syntax by creating some +* suitable QCryptoTLSCreds objects +*/ +static QCryptoTLSCreds * +vnc_display_create_creds (bool x509, +bool x509verify, +const char *dir, +const char *id, +Error **errp) +{ +gchar *credsid =g_strdup_printf ("tlsvnc%s" , id); +Object *parent =object_get_objects_root (); +Object *creds; + +if (x509) { + creds =object_new_with_props (TYPE_QCRYPTO_TLS_CREDS_X509 , + parent, + credsid, + errp, +"endpoint" ,"server" , +"dir" , dir, +"verify-peer" , x509verify ?"yes" :"no" , +NULL ); + }else { + creds = object_new_with_props(TYPE_QCRYPTO_TLS_CREDS_ANON,
ui/vnc.c vnc_display_setup_auth(VncDisplay *vs, · line 3490
Quoted 10 lines
ui/vnc.c +10
-0+ parent, + credsid, + errp, +"endpoint" ,"server" , + NULL); + } + +g_free (credsid); + +if (*errp) {
Won't work. Caller might pass in NULL to ignore the error, in which case you will segfault. You need a local error object coupled with error_propagate() if you are going to make control flow decisions on whether an error occurs in object_new_with_props().
Overall, looks like it is mostly a sane upgrade to use the new framework, and good validation that the earlier patches in the series provide a sane framework.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v5 9/9] ui: convert VNC server to use QCryptoTLSSession
Quoted 8 lines · Eric Blake
Quoted 6 lines · Daniel P. Berrange
+++
b/ui/vnc.c(quoted diff) +0
-0@@ -3301,13 +3303,12 @@ static QemuOptsList qemu_vnc_opts = { };
Quoted 14 lines
ui/vnc.c static QemuOptsList qemu_vnc_opts = { · line 3312
Quoted 11 lines
ui/vnc.c +3
-4-static void +static int vnc_display_setup_auth (VncDisplay *vs,bool password,bool sasl, -bool tls, -bool x509, -bool websocket) +bool websocket, +Error **errp) {Adding a return value and an errp pointer? Can't callers just check whether errp was set, and then you can leave this as returning void?
If we leave this function void, then the caller is forced to use a local Error and error_propagate(), since it is forbidden to de-reference the Error ** it already has. So this means the caller can just do a plain < 0 check for failure which is shorter & simpler.
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 :|