]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tpm2-util: split out helpers which format a PCR mask as a JSON array
authorLennart Poettering <lennart@poettering.net>
Fri, 19 Aug 2022 14:09:51 +0000 (16:09 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 19 Aug 2022 14:29:38 +0000 (16:29 +0200)
This makes the code easier to read, and is something we can reuse later
on.

src/shared/tpm2-util.c
src/shared/tpm2-util.h

index f88272db7b052c9eea89f1cd4825f78a32ee962c..dde6ce8422521d103c825353190f3b95b693e7da 100644 (file)
@@ -1380,6 +1380,36 @@ int tpm2_parse_pcrs(const char *s, uint32_t *ret) {
         return 0;
 }
 
+int tpm2_make_pcr_json_array(uint32_t pcr_mask, JsonVariant **ret) {
+        _cleanup_(json_variant_unrefp) JsonVariant *a = NULL;
+        JsonVariant* pcr_array[TPM2_PCRS_MAX];
+        unsigned n_pcrs = 0;
+        int r;
+
+        for (size_t i = 0; i < ELEMENTSOF(pcr_array); i++) {
+                if ((pcr_mask & (UINT32_C(1) << i)) == 0)
+                        continue;
+
+                r = json_variant_new_integer(pcr_array + n_pcrs, i);
+                if (r < 0)
+                        goto finish;
+
+                n_pcrs++;
+        }
+
+        r = json_variant_new_array(&a, pcr_array, n_pcrs);
+        if (r < 0)
+                goto finish;
+
+        if (ret)
+                *ret = TAKE_PTR(a);
+        r = 0;
+
+finish:
+        json_variant_unref_many(pcr_array, n_pcrs);
+        return r;
+}
+
 int tpm2_make_luks2_json(
                 int keyslot,
                 uint32_t pcr_mask,
@@ -1394,8 +1424,6 @@ int tpm2_make_luks2_json(
 
         _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *a = NULL;
         _cleanup_free_ char *keyslot_as_string = NULL;
-        JsonVariant* pcr_array[TPM2_PCRS_MAX];
-        unsigned n_pcrs = 0;
         int r;
 
         assert(blob || blob_size == 0);
@@ -1404,23 +1432,9 @@ int tpm2_make_luks2_json(
         if (asprintf(&keyslot_as_string, "%i", keyslot) < 0)
                 return -ENOMEM;
 
-        for (unsigned i = 0; i < ELEMENTSOF(pcr_array); i++) {
-                if ((pcr_mask & (UINT32_C(1) << i)) == 0)
-                        continue;
-
-                r = json_variant_new_integer(pcr_array + n_pcrs, i);
-                if (r < 0) {
-                        json_variant_unref_many(pcr_array, n_pcrs);
-                        return -ENOMEM;
-                }
-
-                n_pcrs++;
-        }
-
-        r = json_variant_new_array(&a, pcr_array, n_pcrs);
-        json_variant_unref_many(pcr_array, n_pcrs);
+        r = tpm2_make_pcr_json_array(pcr_mask, &a);
         if (r < 0)
-                return -ENOMEM;
+                return r;
 
         r = json_build(&v,
                        JSON_BUILD_OBJECT(
index ef19bed4f6cbb157e12475f89bdadfa2b00ae006..440d92e37906ca462f71c4c7769760af013eaca6 100644 (file)
@@ -54,6 +54,7 @@ int tpm2_find_device_auto(int log_level, char **ret);
 
 int tpm2_parse_pcrs(const char *s, uint32_t *ret);
 
+int tpm2_make_pcr_json_array(uint32_t pcr_mask, JsonVariant **ret);
 int tpm2_make_luks2_json(int keyslot, uint32_t pcr_mask, uint16_t pcr_bank, uint16_t primary_alg, const void *blob, size_t blob_size, const void *policy_hash, size_t policy_hash_size, TPM2Flags flags, JsonVariant **ret);
 
 #define TPM2_PCRS_MAX 24