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