]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tpm2-util: introduce tpm2_parse_pcr_argument() helper
authorLennart Poettering <lennart@poettering.net>
Wed, 17 Aug 2022 15:00:27 +0000 (17:00 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 19 Aug 2022 19:26:26 +0000 (21:26 +0200)
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.

src/creds/creds.c
src/cryptenroll/cryptenroll.c
src/cryptsetup/cryptsetup.c
src/partition/repart.c
src/shared/tpm2-util.c
src/shared/tpm2-util.h

index e9c7c96fc561065e58093d16afb02a64394a5b92..c067c886db9e0ef34df0e99c140e6fa5df751397 100644 (file)
@@ -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)) {
index f7fc4963a83341ecf798fa6c4bf8a46287dc7077..3c2b914a435eea85954c8ed3b835e70c6c88715f 100644 (file)
@@ -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);
index f0130eb238889bd027f9e921f140e7191a7d5d7a..c9a55a314b2be37774669f069d42acf6d1df69b3 100644 (file)
@@ -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="))) {
 
index 0c0c0de794ca7d9c96dc0f936422f0f5e4e5d6c5..f44e239b37d6e622d806eb8fcde0196d525f0402 100644 (file)
@@ -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;
index e1a519089366f79790f0daa2d02608780faf5408..db8d42b5019cef183268cc971a6b145556c92197 100644 (file)
@@ -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;
+}
index 220eb341ecf49fca99c60faafc60d84bb47cf90d..c62a0843bad8c3f9f641c4cd7c0f50acd4a7fafd 100644 (file)
@@ -113,3 +113,5 @@ typedef enum Tpm2Support {
 } Tpm2Support;
 
 Tpm2Support tpm2_support(void);
+
+int tpm2_parse_pcr_argument(const char *arg, uint32_t *mask);