1
0
mirror of https://github.com/systemd/systemd.git synced 2025-02-04 21:47:31 +03:00

tpm2-util: split out helpers which format a PCR mask as a JSON array

This makes the code easier to read, and is something we can reuse later
on.
This commit is contained in:
Lennart Poettering 2022-08-19 16:09:51 +02:00
parent 2ab1fb77e2
commit 4436081e9d
2 changed files with 33 additions and 18 deletions

View 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(

View 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