All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Paolo Bonzini" <pbonzini@redhat.com>,
	"Daniel P. Berrangé" <berrange@redhat.com>,
	"Thanos Makatos" <thanos.makatos@nutanix.com>,
	qemu-block@nongnu.org, "Hanna Reitz" <hreitz@redhat.com>,
	"Kevin Wolf" <kwolf@redhat.com>,
	"Pierrick Bouvier" <pierrick.bouvier@oss.qualcomm.com>,
	"Cédric Le Goater" <clg@redhat.com>,
	"John Snow" <jsnow@redhat.com>,
	"John Levon" <john.levon@nutanix.com>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Cleber Rosa" <crosa@redhat.com>,
	"Maciej S. Szmigiero" <mail@maciej.szmigiero.name>
Subject: [PULL 02/29] crypto: fix client side anonymous TLS credentials
Date: Wed, 20 May 2026 10:13:24 +0100	[thread overview]
Message-ID: <20260520091351.1160442-3-berrange@redhat.com> (raw)
In-Reply-To: <20260520091351.1160442-1-berrange@redhat.com>

The previous refactoring of credential creation failed to allocate
storage fo the anonymous TLS credentials on the client endpoint.

Fixes: 70f9fd8dbf7233bee497055a9b7825e3729ce853
Reported-by: Maciej S. Szmigiero <mail@maciej.szmigiero.name>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 crypto/tlscredsanon.c               |   2 +
 tests/unit/test-crypto-tlssession.c | 120 +++++++++++++++++++++++++++-
 2 files changed, 121 insertions(+), 1 deletion(-)

diff --git a/crypto/tlscredsanon.c b/crypto/tlscredsanon.c
index 1551382e1f..190c9833a7 100644
--- a/crypto/tlscredsanon.c
+++ b/crypto/tlscredsanon.c
@@ -73,6 +73,8 @@ qcrypto_tls_creds_anon_load(QCryptoTLSCredsAnon *creds,
                                              box->dh_params);
         }
     } else {
+        box = qcrypto_tls_creds_box_new_client(GNUTLS_CRD_ANON);
+
         ret = gnutls_anon_allocate_client_credentials(&box->data.anonclient);
         if (ret < 0) {
             error_setg(errp, "Cannot allocate credentials: %s",
diff --git a/tests/unit/test-crypto-tlssession.c b/tests/unit/test-crypto-tlssession.c
index 0d06a6892e..dc7a01bb06 100644
--- a/tests/unit/test-crypto-tlssession.c
+++ b/tests/unit/test-crypto-tlssession.c
@@ -24,6 +24,7 @@
 #include "crypto-tls-psk-helpers.h"
 #include "crypto/tlscredsx509.h"
 #include "crypto/tlscredspsk.h"
+#include "crypto/tlscredsanon.h"
 #include "crypto/tlssession.h"
 #include "qom/object_interfaces.h"
 #include "qapi/error.h"
@@ -190,6 +191,121 @@ static void test_crypto_tls_session_psk(void)
 }
 
 
+static QCryptoTLSCreds *test_tls_creds_anon_create(
+    QCryptoTLSCredsEndpoint endpoint)
+{
+    Object *parent = object_get_objects_root();
+    Object *creds = object_new_with_props(
+        TYPE_QCRYPTO_TLS_CREDS_ANON,
+        parent,
+        (endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER ?
+         "testtlscredsserver" : "testtlscredsclient"),
+        &error_abort,
+        "endpoint", (endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER ?
+                     "server" : "client"),
+        "priority", "NORMAL",
+        NULL
+        );
+    return QCRYPTO_TLS_CREDS(creds);
+}
+
+
+static void test_crypto_tls_session_anon(void)
+{
+    QCryptoTLSCreds *clientCreds;
+    QCryptoTLSCreds *serverCreds;
+    QCryptoTLSSession *clientSess = NULL;
+    QCryptoTLSSession *serverSess = NULL;
+    int channel[2];
+    bool clientShake = false;
+    bool serverShake = false;
+    int ret;
+
+    /* We'll use this for our fake client-server connection */
+    ret = qemu_socketpair(AF_UNIX, SOCK_STREAM, 0, channel);
+    g_assert(ret == 0);
+
+    /*
+     * We have an evil loop to do the handshake in a single
+     * thread, so we need these non-blocking to avoid deadlock
+     * of ourselves
+     */
+    qemu_set_blocking(channel[0], false, &error_abort);
+    qemu_set_blocking(channel[1], false, &error_abort);
+
+    clientCreds = test_tls_creds_anon_create(
+        QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT);
+    g_assert(clientCreds != NULL);
+
+    serverCreds = test_tls_creds_anon_create(
+        QCRYPTO_TLS_CREDS_ENDPOINT_SERVER);
+    g_assert(serverCreds != NULL);
+
+    /* Now the real part of the test, setup the sessions */
+    clientSess = qcrypto_tls_session_new(
+        clientCreds, NULL, NULL,
+        QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT, &error_abort);
+    g_assert(clientSess != NULL);
+
+    serverSess = qcrypto_tls_session_new(
+        serverCreds, NULL, NULL,
+        QCRYPTO_TLS_CREDS_ENDPOINT_SERVER, &error_abort);
+    g_assert(serverSess != NULL);
+
+    /* For handshake to work, we need to set the I/O callbacks
+     * to read/write over the socketpair
+     */
+    qcrypto_tls_session_set_callbacks(serverSess,
+                                      testWrite, testRead,
+                                      &channel[0]);
+    qcrypto_tls_session_set_callbacks(clientSess,
+                                      testWrite, testRead,
+                                      &channel[1]);
+
+    /*
+     * Finally we loop around & around doing handshake on each
+     * session until we get an error, or the handshake completes.
+     * This relies on the socketpair being nonblocking to avoid
+     * deadlocking ourselves upon handshake
+     */
+    do {
+        int rv;
+        if (!serverShake) {
+            rv = qcrypto_tls_session_handshake(serverSess,
+                                               &error_abort);
+            g_assert(rv >= 0);
+            if (rv == QCRYPTO_TLS_HANDSHAKE_COMPLETE) {
+                serverShake = true;
+            }
+        }
+        if (!clientShake) {
+            rv = qcrypto_tls_session_handshake(clientSess,
+                                               &error_abort);
+            g_assert(rv >= 0);
+            if (rv == QCRYPTO_TLS_HANDSHAKE_COMPLETE) {
+                clientShake = true;
+            }
+        }
+    } while (!clientShake || !serverShake);
+
+
+    /* Finally make sure the server & client validation is successful. */
+    g_assert(qcrypto_tls_session_check_credentials(serverSess,
+                                                   &error_abort) == 0);
+    g_assert(qcrypto_tls_session_check_credentials(clientSess,
+                                                   &error_abort) == 0);
+
+    object_unparent(OBJECT(serverCreds));
+    object_unparent(OBJECT(clientCreds));
+
+    qcrypto_tls_session_free(serverSess);
+    qcrypto_tls_session_free(clientSess);
+
+    close(channel[0]);
+    close(channel[1]);
+}
+
+
 struct QCryptoTLSSessionTestData {
     const char *servercacrt;
     const char *clientcacrt;
@@ -421,9 +537,11 @@ int main(int argc, char **argv)
     test_tls_init(KEYFILE);
     test_tls_psk_init(PSKFILE);
 
-    /* Simple initial test using Pre-Shared Keys. */
+    /* Simple initial tests using Pre-Shared Keys & anon creds */
     g_test_add_func("/qcrypto/tlssession/psk",
                     test_crypto_tls_session_psk);
+    g_test_add_func("/qcrypto/tlssession/anon",
+                    test_crypto_tls_session_anon);
 
     /* More complex tests using X.509 certificates. */
 # define TEST_SESS_REG(name, caCrt,                                     \
-- 
2.54.0



  parent reply	other threads:[~2026-05-20  9:19 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-20  9:13 [PULL 00/29] Misc next patches Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 01/29] io: invert the return semantics of qio_channel_flush Daniel P. Berrangé
2026-05-20  9:13 ` Daniel P. Berrangé [this message]
2026-05-20  9:13 ` [PULL 03/29] python: bump qemu.qmp to v0.0.6 Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 04/29] gitlab: ensure all meson jobs capture build/meson-logs by default Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 05/29] iotests: print reason when I/O test is skipped in TAP mode Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 06/29] iotests: remove redundant meson suite for iotests Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 07/29] iotests: ensure all qcow2 I/O tests are able to be run via make Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 08/29] scripts/mtest2make: ensure output has stable sorting Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 09/29] scripts/mtest2make: support optional tests grouping Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 10/29] iotests: add a meson suite / make target per block I/O tests format Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 11/29] docs/devel/testing: expand documentation for 'make check-block' Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 12/29] iotests: add nbd and luks to the I/O test suites Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 13/29] iotests: use 'driver' as collective term for either format or protocol Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 14/29] iotests: validate dmsetup result in test 128 Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 15/29] iotests: fix check for sudo access in LUKS I/O test Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 16/29] iotests: mark 185 as a flaky test Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 17/29] gitlab: add jobs for thorough block tests Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 18/29] gitlab: remove I/O tests from build-tcg-disabled job Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 19/29] qom: add trace events for object/property lifecycle Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 20/29] hw/vfio-user: use a valid object ID for iothread Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 21/29] qom: validate ID format when creating objects Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 22/29] qom: make errp last param in methods taking va_list Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 23/29] qom: shorten name of object_set_properties_from_keyval Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 24/29] qom: have object_set_props_keyval return bool Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 25/29] qom: move object_set_prop_keyval into object.c Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 26/29] qom: add object_new_with_props_from_qdict Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 27/29] qom: fix ability to create objects without a parent Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 28/29] qom: allow object_new_with_prop* to trigger module loading Daniel P. Berrangé
2026-05-20  9:13 ` [PULL 29/29] qom: drop user_creatable_add_type method Daniel P. Berrangé
2026-05-20 20:51 ` [PULL 00/29] Misc next patches Stefan Hajnoczi
2026-05-21  8:56   ` Daniel P. Berrangé
  -- strict thread matches above, loose matches on Subject: below --
2026-05-21 13:49 [PULL v3 00/29] Misc patches for iotests, qom, crypt & io Daniel P. Berrangé
2026-05-21 13:49 ` [PULL 02/29] crypto: fix client side anonymous TLS credentials Daniel P. Berrangé

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260520091351.1160442-3-berrange@redhat.com \
    --to=berrange@redhat.com \
    --cc=alex.bennee@linaro.org \
    --cc=clg@redhat.com \
    --cc=crosa@redhat.com \
    --cc=hreitz@redhat.com \
    --cc=john.levon@nutanix.com \
    --cc=jsnow@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mail@maciej.szmigiero.name \
    --cc=pbonzini@redhat.com \
    --cc=pierrick.bouvier@oss.qualcomm.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=thanos.makatos@nutanix.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.