crypto: Implement TLS Pre-Shared Keys (PSK).
PATCH 1/1 crypto: Implement TLS Pre-Shared Keys (PSK).
TLS-PSK (Pre-Shared Keys) lets us set up TLS connections much more easily, especially for NBD.
This is a "version 0" of the patch for now, mainly to solicit comments. It needs documentation at least.
Rich.
PATCH 1/1 crypto: Implement TLS Pre-Shared Keys (PSK).
Pre-Shared Keys (PSK) is a simpler mechanism for enabling TLS connections than using certificates. It requires only a simple secret key:
$ mkdir -m 0700 /tmp/keys $ psktool -u rjones -p /tmp/keys/keys.psk $ cat /tmp/keys/keys.psk rjones:d543770c15ad93d76443fb56f501a31969235f47e999720ae8d2336f6a13fcbc
The key can be secretly shared between clients and servers. Clients must specify the directory containing the "keys.psk" file and a username (defaults to "qemu"). Servers must specify only the directory.
Example NBD client:
$ qemu-img info \
--object tls-creds-psk,id=tls0,dir=/tmp/keys,username=rjones,endpoint=client \
--image-opts \
file.driver=nbd,file.host=localhost,file.port=10809,file.tls-creds=tls0,file.export=/Example NBD server using qemu-nbd:
$ qemu-nbd -t -x / \
--object tls-creds-psk,id=tls0,endpoint=server,dir=/tmp/keys \
--tls-creds tls0 \
image.qcow2Example NBD server using nbdkit:
$ nbdkit -n -e / -fv \
--tls=on --tls-psk=/tmp/keys/keys.psk \
file file=disk.img--- crypto/Makefile.objs | 1 + crypto/tlscredspsk.c | 300 +++++++++++++++++++++++++++++++++++++++++++ crypto/tlssession.c | 38 ++++++ crypto/trace-events | 3 + include/crypto/tlscredspsk.h | 106 +++++++++++++++ 5 files changed, 448 insertions(+)
crypto/Makefile.objs +1 -0
@@ -15,6 +15,7 @@ crypto-obj-$(CONFIG_AF_ALG) += cipher-afalg.o crypto-obj-$(CONFIG_AF_ALG) += hash-afalg.o crypto-obj-y += tlscreds.o crypto-obj-y += tlscredsanon.o +crypto-obj-y += tlscredspsk.o crypto-obj-y += tlscredsx509.o crypto-obj-y += tlssession.o crypto-obj-y += secret.o
crypto/tlscredspsk.c new file +300 -0
300 lines not on this page · raw message
crypto/tlssession.c +38 -0
@@ -21,6 +21,7 @@#include "qemu/osdep.h" #include "crypto/tlssession.h" #include "crypto/tlscredsanon.h" +#include "crypto/tlscredspsk.h" #include "crypto/tlscredsx509.h" #include "qapi/error.h" #include "qemu/acl.h"
@@ -162,6 +163,39 @@ qcrypto_tls_session_new(QCryptoTLSCreds *creds, gnutls_strerror(ret)); goto error; } + }else if (object_dynamic_cast (OBJECT (creds), +TYPE_QCRYPTO_TLS_CREDS_PSK )) { +QCryptoTLSCredsPSK *pcreds =QCRYPTO_TLS_CREDS_PSK (creds); +char *prio; + +if (creds->priority !=NULL ) { + prio =g_strdup_printf ("%s:+PSK:+DHE-PSK" , creds->priority); + }else { + prio =g_strdup (CONFIG_TLS_PRIORITY ":+PSK:+DHE-PSK" ); + } + + ret =gnutls_priority_set_direct (session->handle, prio,NULL ); +if (ret <0 ) { +error_setg (errp,"Unable to set TLS session priority %s: %s" , + prio,gnutls_strerror (ret)); +g_free (prio); + goto error; + } +g_free (prio); +if (creds->endpoint ==QCRYPTO_TLS_CREDS_ENDPOINT_SERVER ) { + ret =gnutls_credentials_set (session->handle, +GNUTLS_CRD_PSK , + pcreds->data.server); + }else { + ret =gnutls_credentials_set (session->handle, +GNUTLS_CRD_PSK , + pcreds->data.client); + } +if (ret <0 ) { +error_setg (errp,"Cannot set session credentials: %s" , +gnutls_strerror (ret)); + goto error; + } }else if (object_dynamic_cast (OBJECT (creds),TYPE_QCRYPTO_TLS_CREDS_X509 )) {QCryptoTLSCredsX509 *tcreds =QCRYPTO_TLS_CREDS_X509 (creds);
@@ -353,6 +387,10 @@ qcrypto_tls_session_check_credentials(QCryptoTLSSession *session, TYPE_QCRYPTO_TLS_CREDS_ANON)) {trace_qcrypto_tls_session_check_creds (session,"nop" );return 0 ; + } else if (object_dynamic_cast(OBJECT(session->creds), + TYPE_QCRYPTO_TLS_CREDS_PSK)) { +trace_qcrypto_tls_session_check_creds (session,"nop" ); +return 0 ; } else if (object_dynamic_cast(OBJECT(session->creds), TYPE_QCRYPTO_TLS_CREDS_X509)) {if (session->creds->verifyPeer) {
crypto/trace-events +3 -0
@@ -7,6 +7,9 @@ qcrypto_tls_creds_get_path(void *creds, const char *filename, const char *path) # crypto/tlscredsanon.c qcrypto_tls_creds_anon_load(void *creds, const char *dir) "TLS creds anon load creds=%p dir=%s" +# crypto/tlscredspsk.c +qcrypto_tls_creds_psk_load(void *creds, const char *dir) "TLS creds psk load creds=%p dir=%s" + # crypto/tlscredsx509.c qcrypto_tls_creds_x509_load(void *creds, const char *dir) "TLS creds x509 load creds=%p dir=%s" qcrypto_tls_creds_x509_check_basic_constraints(void *creds, const char *file, int status) "TLS creds x509 check basic constraints creds=%p file=%s status=%d"
include/crypto/tlscredspsk.h new file +106 -0
106 lines not on this page · raw message
Signature · 1 line
2.16.2
Quoted 34 lines · Richard W.M. Jones
Pre-Shared Keys (PSK) is a simpler mechanism for enabling TLS connections than using certificates. It requires only a simple secret key:
$ mkdir -m 0700 /tmp/keys $ psktool -u rjones -p /tmp/keys/keys.psk $ cat /tmp/keys/keys.psk rjones:d543770c15ad93d76443fb56f501a31969235f47e999720ae8d2336f6a13fcbcThe key can be secretly shared between clients and servers. Clients must specify the directory containing the "keys.psk" file and a username (defaults to "qemu"). Servers must specify only the directory.
Example NBD client:
$ qemu-img info \ --object tls-creds-psk,id=tls0,dir=/tmp/keys,username=rjones,endpoint=client \ --image-opts \ file.driver=nbd,file.host=localhost,file.port=10809,file.tls-creds=tls0,file.export=/Example NBD server using qemu-nbd:
$ qemu-nbd -t -x / \ --object tls-creds-psk,id=tls0,endpoint=server,dir=/tmp/keys \ --tls-creds tls0 \ image.qcow2Example NBD server using nbdkit:
$ nbdkit -n -e / -fv \ --tls=on --tls-psk=/tmp/keys/keys.psk \ file file=disk.img
Quoted 8 lines
Signed-off-by Richard W.M. Jones <[email protected]>--- crypto/Makefile.objs | 1 + crypto/tlscredspsk.c | 300 +++++++++++++++++++++++++++++++++++++++++++ crypto/tlssession.c | 38 ++++++ crypto/trace-events | 3 + include/crypto/tlscredspsk.h | 106 +++++++++++++++ 5 files changed, 448 insertions(+)
Not done a proper code review yet but just a docs point...
Can you describe the new object type in qemu-options.hx - look for the existing entry related to tls-creds-x509
Also in qemu-doc.texi at approx
"@section TLS setup for network services"
we'll need something added related to PSK to describe how to set it up. I guess we'll want to put what's there already under an "x509" section, and add a PSK section
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|