Skip to contentSkip to conversation

crypto: fix TLS PSK credentials on Windows platforms

PATCH In review Daniel P. Berrangé 3 participants 7 messages Oct 3, 2022 Crypto API

lore
Share & tools
lore
#0 lore raw

cover crypto: fix TLS PSK credentials on Windows platforms

A mixup with DOS/UNIX line endings, combined with lack of error checking, resulted in us not initializing the PSK credentials on the client side.

Daniel P. Berrangé (2):
crypto: check for and report errors setting PSK credentials
tests: avoid DOS line endings in PSK file

 crypto/tlscredspsk.c                | 16 +++++++++++++---
 tests/unit/crypto-tls-psk-helpers.c | 11 +++++------
 2 files changed, 18 insertions(+), 9 deletions(-)
Signature · 1 line
2.37.3
#1 lore raw

PATCH 1/2 crypto: check for and report errors setting PSK credentials

If setting credentials fails, the handshake will later fail to complete with an obscure error message which is hard to diagnose.

---
 crypto/tlscredspsk.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
crypto/tlscredspsk.c +13 -3
index a4f9891274..546cad1c5a 100644
@@ -109,7 +109,12 @@ qcrypto_tls_creds_psk_load(QCryptoTLSCredsPSK *creds,
             goto cleanup;
         }
 
-        gnutls_psk_set_server_credentials_file(creds->data.server, pskfile);
+        ret = gnutls_psk_set_server_credentials_file(creds->data.server, pskfile);
+        if (ret < 0) {
+            error_setg(errp, "Cannot set PSK server credentials: %s",
+                       gnutls_strerror(ret));
+            goto cleanup;
+        }
         gnutls_psk_set_server_dh_params(creds->data.server,
                                         creds->parent_obj.dh_params);
     } else {
@@ -135,8 +140,13 @@ qcrypto_tls_creds_psk_load(QCryptoTLSCredsPSK *creds,
             goto cleanup;
         }
 
-        gnutls_psk_set_client_credentials(creds->data.client,
-                                          username, &key, GNUTLS_PSK_KEY_HEX);
+        ret = gnutls_psk_set_client_credentials(creds->data.client,
+                                                username, &key, GNUTLS_PSK_KEY_HEX);
+        if (ret < 0) {
+            error_setg(errp, "Cannot set PSK client credentials: %s",
+                       gnutls_strerror(ret));
+            goto cleanup;
+        }
     }
 
     rv = 0;
Signature · 1 line
2.37.3
Reviewed-by Tested-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] crypto: check for and report errors setting PSK credentials

Quoted 9 lines · Daniel P. Berrangé

If setting credentials fails, the handshake will later fail to complete with an obscure error message which is hard to diagnose.

---
 crypto/tlscredspsk.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
Reviewed-by Philippe Mathieu-Daudé on v1 1/2
#3 lore raw

Re: [PATCH 1/2] crypto: check for and report errors setting PSK credentials

In reply to Daniel P. Berrangé
Quoted 7 lines · Daniel P. Berrangé

If setting credentials fails, the handshake will later fail to complete with an obscure error message which is hard to diagnose.

---
  crypto/tlscredspsk.c | 16 +++++++++++++---
  1 file changed, 13 insertions(+), 3 deletions(-)
#4 lore raw

PATCH 2/2 tests: avoid DOS line endings in PSK file

In reply to Daniel P. Berrangé

Using FILE * APIs for writing the PSK file results in translation from UNIX to DOS line endings on Windows. When the crypto PSK code later loads the credentials the stray \r will result in failure to load the PSK credentials into GNUTLS.

Rather than switching the FILE* APIs to open in binary format, just switch to the more concise g_file_set_contents API.

---
 tests/unit/crypto-tls-psk-helpers.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
tests/unit/crypto-tls-psk-helpers.c +5 -6
index 511e08cc9c..c6cc740772 100644
@@ -27,15 +27,14 @@
 static void
 test_tls_psk_init_common(const char *pskfile, const char *user, const char *key)
 {
-    FILE *fp;
+    g_autoptr(GError) gerr = NULL;
+    g_autofree char *line = g_strdup_printf("%s:%s\n", user, key);
 
-    fp = fopen(pskfile, "w");
-    if (fp == NULL) {
-        g_critical("Failed to create pskfile %s: %s", pskfile, strerror(errno));
+    g_file_set_contents(pskfile, line, strlen(line), &gerr);
+    if (gerr != NULL) {
+        g_critical("Failed to create pskfile %s: %s", pskfile, gerr->message);
         abort();
     }
-    fprintf(fp, "%s:%s\n", user, key);
-    fclose(fp);
 }
 
 void test_tls_psk_init(const char *pskfile)
Signature · 1 line
2.37.3
Reviewed-by Tested-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] tests: avoid DOS line endings in PSK file

Quoted 6 lines · Daniel P. Berrangé

Using FILE * APIs for writing the PSK file results in translation from UNIX to DOS line endings on Windows. When the crypto PSK code later loads the credentials the stray \r will result in failure to load the PSK credentials into GNUTLS.

Quoted 8 lines

Rather than switching the FILE* APIs to open in binary format, just switch to the more concise g_file_set_contents API.

---
 tests/unit/crypto-tls-psk-helpers.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
Reviewed-by Philippe Mathieu-Daudé on v1 2/2
#6 lore raw

Re: [PATCH 2/2] tests: avoid DOS line endings in PSK file

In reply to Daniel P. Berrangé
Quoted 5 lines · Daniel P. Berrangé

Using FILE * APIs for writing the PSK file results in translation from UNIX to DOS line endings on Windows. When the crypto PSK code later loads the credentials the stray \r will result in failure to load the PSK credentials into GNUTLS.

Quoted 7 lines

Rather than switching the FILE* APIs to open in binary format, just switch to the more concise g_file_set_contents API.

---
  tests/unit/crypto-tls-psk-helpers.c | 11 +++++------
  1 file changed, 5 insertions(+), 6 deletions(-)