From: Lennart Poettering Date: Wed, 17 Aug 2022 15:00:27 +0000 (+0200) Subject: tpm2-util: introduce tpm2_parse_pcr_argument() helper X-Git-Tag: v252-rc1~402 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=222a951fa4a625e2d7d9c2799d9873377029d1bf;p=thirdparty%2Fsystemd.git tpm2-util: introduce tpm2_parse_pcr_argument() helper Add a new tpm2_parse_pcr_argument() helper that unifies how we merge PCR masks in a single function, we can use all over the place. Previously we had basically the same code for this at 4 places. --- diff --git a/src/creds/creds.c b/src/creds/creds.c index e9c7c96fc56..c067c886db9 100644 --- a/src/creds/creds.c +++ b/src/creds/creds.c @@ -832,25 +832,12 @@ static int parse_argv(int argc, char *argv[]) { arg_tpm2_device = streq(optarg, "auto") ? NULL : optarg; break; - case ARG_TPM2_PCRS: { - uint32_t mask; - - if (isempty(optarg)) { - arg_tpm2_pcr_mask = 0; - break; - } - - r = tpm2_parse_pcrs(optarg, &mask); + case ARG_TPM2_PCRS: + r = tpm2_parse_pcr_argument(optarg, &arg_tpm2_pcr_mask); if (r < 0) return r; - if (arg_tpm2_pcr_mask == UINT32_MAX) - arg_tpm2_pcr_mask = mask; - else - arg_tpm2_pcr_mask |= mask; - break; - } case ARG_NAME: if (isempty(optarg)) { diff --git a/src/cryptenroll/cryptenroll.c b/src/cryptenroll/cryptenroll.c index f7fc4963a83..3c2b914a435 100644 --- a/src/cryptenroll/cryptenroll.c +++ b/src/cryptenroll/cryptenroll.c @@ -315,25 +315,12 @@ static int parse_argv(int argc, char *argv[]) { break; } - case ARG_TPM2_PCRS: { - uint32_t mask; - - if (isempty(optarg)) { - arg_tpm2_pcr_mask = 0; - break; - } - - r = tpm2_parse_pcrs(optarg, &mask); + case ARG_TPM2_PCRS: + r = tpm2_parse_pcr_argument(optarg, &arg_tpm2_pcr_mask); if (r < 0) return r; - if (arg_tpm2_pcr_mask == UINT32_MAX) - arg_tpm2_pcr_mask = mask; - else - arg_tpm2_pcr_mask |= mask; - break; - } case ARG_TPM2_PIN: r = parse_boolean_argument("--tpm2-with-pin=", optarg, &arg_tpm2_pin); diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c index f0130eb2388..c9a55a314b2 100644 --- a/src/cryptsetup/cryptsetup.c +++ b/src/cryptsetup/cryptsetup.c @@ -394,20 +394,9 @@ static int parse_one_option(const char *option) { } else if ((val = startswith(option, "tpm2-pcrs="))) { - if (isempty(val)) - arg_tpm2_pcr_mask = 0; - else { - uint32_t mask; - - r = tpm2_parse_pcrs(val, &mask); - if (r < 0) - return r; - - if (arg_tpm2_pcr_mask == UINT32_MAX) - arg_tpm2_pcr_mask = mask; - else - arg_tpm2_pcr_mask |= mask; - } + r = tpm2_parse_pcr_argument(val, &arg_tpm2_pcr_mask); + if (r < 0) + return r; } else if ((val = startswith(option, "tpm2-pin="))) { diff --git a/src/partition/repart.c b/src/partition/repart.c index 0c0c0de794c..f44e239b37d 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -4359,25 +4359,12 @@ static int parse_argv(int argc, char *argv[]) { break; } - case ARG_TPM2_PCRS: { - uint32_t mask; - - if (isempty(optarg)) { - arg_tpm2_pcr_mask = 0; - break; - } - - r = tpm2_parse_pcrs(optarg, &mask); + case ARG_TPM2_PCRS: + r = tpm2_parse_pcr_argument(optarg, &arg_tpm2_pcr_mask); if (r < 0) return r; - if (arg_tpm2_pcr_mask == UINT32_MAX) - arg_tpm2_pcr_mask = mask; - else - arg_tpm2_pcr_mask |= mask; - break; - } case '?': return -EINVAL; diff --git a/src/shared/tpm2-util.c b/src/shared/tpm2-util.c index e1a51908936..db8d42b5019 100644 --- a/src/shared/tpm2-util.c +++ b/src/shared/tpm2-util.c @@ -1508,3 +1508,28 @@ Tpm2Support tpm2_support(void) { return support; } + +int tpm2_parse_pcr_argument(const char *arg, uint32_t *mask) { + uint32_t m; + int r; + + assert(mask); + + /* For use in getopt_long() command line parsers: merges masks specified on the command line */ + + if (isempty(arg)) { + *mask = 0; + return 0; + } + + r = tpm2_parse_pcrs(arg, &m); + if (r < 0) + return r; + + if (*mask == UINT32_MAX) + *mask = m; + else + *mask |= m; + + return 0; +} diff --git a/src/shared/tpm2-util.h b/src/shared/tpm2-util.h index 220eb341ecf..c62a0843bad 100644 --- a/src/shared/tpm2-util.h +++ b/src/shared/tpm2-util.h @@ -113,3 +113,5 @@ typedef enum Tpm2Support { } Tpm2Support; Tpm2Support tpm2_support(void); + +int tpm2_parse_pcr_argument(const char *arg, uint32_t *mask);