d76c62
From fe42b8bb2e4a456a5b2297313f3859221013fdfc Mon Sep 17 00:00:00 2001
d76c62
Message-Id: <fe42b8bb2e4a456a5b2297313f3859221013fdfc@dist-git>
d76c62
From: Peter Krempa <pkrempa@redhat.com>
d76c62
Date: Mon, 16 Mar 2020 22:11:46 +0100
d76c62
Subject: [PATCH] qemuDomainSecretAESSetup: Allocate and return 'secinfo' here
d76c62
MIME-Version: 1.0
d76c62
Content-Type: text/plain; charset=UTF-8
d76c62
Content-Transfer-Encoding: 8bit
d76c62
d76c62
Rather than passing in an empty qemuDomainSecretInfoPtr allocate it
d76c62
in this function and return it. This is done by absorbing the check from
d76c62
qemuDomainSecretInfoNew and removing the internals of
d76c62
qemuDomainSecretInfoNew.
d76c62
d76c62
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
d76c62
Reviewed-by: Ján Tomko <jtomko@redhat.com>
d76c62
(cherry picked from commit bad8637892ae8fc310b252651876738ca4fdee0d)
d76c62
https://bugzilla.redhat.com/show_bug.cgi?id=1804750
d76c62
Message-Id: <94071336dbc97ed64a1a5dcbb82da32e5199f117.1584391726.git.pkrempa@redhat.com>
d76c62
Reviewed-by: Ján Tomko <jtomko@redhat.com>
d76c62
---
d76c62
 src/qemu/qemu_domain.c | 53 ++++++++++++++++++------------------------
d76c62
 1 file changed, 22 insertions(+), 31 deletions(-)
d76c62
d76c62
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
d76c62
index b26187659e..37e361b1f4 100644
d76c62
--- a/src/qemu/qemu_domain.c
d76c62
+++ b/src/qemu/qemu_domain.c
d76c62
@@ -1529,21 +1529,20 @@ qemuDomainSecretPlainSetup(qemuDomainSecretInfoPtr secinfo,
d76c62
  * @seclookupdef: Pointer to seclookupdef data
d76c62
  * @isLuks: True/False for is for luks (alias generation)
d76c62
  *
d76c62
- * Taking a secinfo, fill in the AES specific information using the
d76c62
+ * Encrypts a secret looked up via @seclookupdef for use with qemu.
d76c62
  *
d76c62
- * Returns 0 on success, -1 on failure with error message
d76c62
+ * Returns qemuDomainSecretInfoPtr filled with the necessary information.
d76c62
  */
d76c62
-static int
d76c62
+static qemuDomainSecretInfoPtr
d76c62
 qemuDomainSecretAESSetup(qemuDomainObjPrivatePtr priv,
d76c62
-                         qemuDomainSecretInfoPtr secinfo,
d76c62
                          const char *srcalias,
d76c62
                          virSecretUsageType usageType,
d76c62
                          const char *username,
d76c62
                          virSecretLookupTypeDefPtr seclookupdef,
d76c62
                          bool isLuks)
d76c62
 {
d76c62
+    g_autoptr(qemuDomainSecretInfo) secinfo = NULL;
d76c62
     g_autoptr(virConnect) conn = virGetConnectSecret();
d76c62
-    int ret = -1;
d76c62
     g_autofree uint8_t *raw_iv = NULL;
d76c62
     size_t ivlen = QEMU_DOMAIN_AES_IV_LEN;
d76c62
     uint8_t *secret = NULL;
d76c62
@@ -1552,19 +1551,27 @@ qemuDomainSecretAESSetup(qemuDomainObjPrivatePtr priv,
d76c62
     size_t ciphertextlen = 0;
d76c62
 
d76c62
     if (!conn)
d76c62
-        return -1;
d76c62
+        return NULL;
d76c62
+
d76c62
+    if (!qemuDomainSupportsEncryptedSecret(priv)) {
d76c62
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
d76c62
+                       _("encrypted secrets are not supported"));
d76c62
+        return NULL;
d76c62
+    }
d76c62
+
d76c62
+    secinfo = g_new0(qemuDomainSecretInfo, 1);
d76c62
 
d76c62
     secinfo->type = VIR_DOMAIN_SECRET_INFO_TYPE_AES;
d76c62
     secinfo->s.aes.username = g_strdup(username);
d76c62
 
d76c62
     if (!(secinfo->s.aes.alias = qemuDomainGetSecretAESAlias(srcalias, isLuks)))
d76c62
-        return -1;
d76c62
+        return NULL;
d76c62
 
d76c62
     raw_iv = g_new0(uint8_t, ivlen);
d76c62
 
d76c62
     /* Create a random initialization vector */
d76c62
     if (virRandomBytes(raw_iv, ivlen) < 0)
d76c62
-        return -1;
d76c62
+        return NULL;
d76c62
 
d76c62
     /* Encode the IV and save that since qemu will need it */
d76c62
     secinfo->s.aes.iv = g_base64_encode(raw_iv, ivlen);
d76c62
@@ -1572,13 +1579,13 @@ qemuDomainSecretAESSetup(qemuDomainObjPrivatePtr priv,
d76c62
     /* Grab the unencoded secret */
d76c62
     if (virSecretGetSecretString(conn, seclookupdef, usageType,
d76c62
                                  &secret, &secretlen) < 0)
d76c62
-        goto cleanup;
d76c62
+        goto error;
d76c62
 
d76c62
     if (virCryptoEncryptData(VIR_CRYPTO_CIPHER_AES256CBC,
d76c62
                              priv->masterKey, QEMU_DOMAIN_MASTER_KEY_LEN,
d76c62
                              raw_iv, ivlen, secret, secretlen,
d76c62
                              &ciphertext, &ciphertextlen) < 0)
d76c62
-        goto cleanup;
d76c62
+        goto error;
d76c62
 
d76c62
     /* Clear out the secret */
d76c62
     memset(secret, 0, secretlen);
d76c62
@@ -1587,11 +1594,11 @@ qemuDomainSecretAESSetup(qemuDomainObjPrivatePtr priv,
d76c62
     secinfo->s.aes.ciphertext = g_base64_encode(ciphertext,
d76c62
                                                 ciphertextlen);
d76c62
 
d76c62
-    ret = 0;
d76c62
+    return g_steal_pointer(&secinfo);
d76c62
 
d76c62
- cleanup:
d76c62
+ error:
d76c62
     VIR_DISPOSE_N(secret, secretlen);
d76c62
-    return ret;
d76c62
+    return NULL;
d76c62
 }
d76c62
 
d76c62
 
d76c62
@@ -1663,24 +1670,8 @@ qemuDomainSecretInfoNew(qemuDomainObjPrivatePtr priv,
d76c62
                         virSecretLookupTypeDefPtr lookupDef,
d76c62
                         bool isLuks)
d76c62
 {
d76c62
-    qemuDomainSecretInfoPtr secinfo = NULL;
d76c62
-
d76c62
-    if (!qemuDomainSupportsEncryptedSecret(priv)) {
d76c62
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
d76c62
-                       _("encrypted secrets are not supported"));
d76c62
-        return NULL;
d76c62
-    }
d76c62
-
d76c62
-    if (VIR_ALLOC(secinfo) < 0)
d76c62
-        return NULL;
d76c62
-
d76c62
-    if (qemuDomainSecretAESSetup(priv, secinfo, srcAlias, usageType, username,
d76c62
-                                 lookupDef, isLuks) < 0) {
d76c62
-        g_clear_pointer(&secinfo, qemuDomainSecretInfoFree);
d76c62
-        return NULL;
d76c62
-    }
d76c62
-
d76c62
-    return secinfo;
d76c62
+    return qemuDomainSecretAESSetup(priv, srcAlias, usageType, username,
d76c62
+                                    lookupDef, isLuks);
d76c62
 }
d76c62
 
d76c62
 
d76c62
-- 
d76c62
2.25.1
d76c62