richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
8d419f
From 9aecba3db5d381dd915e90931c63513141988d92 Mon Sep 17 00:00:00 2001
8d419f
From: Grigori Goronzy <greg@chown.ath.cx>
8d419f
Date: Fri, 18 Feb 2022 11:51:25 +0100
8d419f
Subject: [PATCH] cryptenroll: add support for TPM2 pin
8d419f
8d419f
Add support for PIN enrollment with TPM2. A new "tpm2-pin" field is
8d419f
introduced into metadata to signal that the policy needs to include a
8d419f
PIN.
8d419f
8d419f
v2: fix tpm2_make_luks2_json in sd-repart
8d419f
(cherry picked from commit 6c7a1681052c37ef354a000355c4c0d676113a1a)
8d419f
8d419f
Related: #2087652
8d419f
---
8d419f
 src/cryptenroll/cryptenroll-tpm2.c | 85 ++++++++++++++++++++++++++++--
8d419f
 src/cryptenroll/cryptenroll-tpm2.h |  4 +-
8d419f
 src/cryptenroll/cryptenroll.c      | 15 +++++-
8d419f
 src/partition/repart.c             |  2 +-
8d419f
 src/shared/tpm2-util.c             |  5 +-
8d419f
 src/shared/tpm2-util.h             |  6 ++-
8d419f
 6 files changed, 107 insertions(+), 10 deletions(-)
8d419f
8d419f
diff --git a/src/cryptenroll/cryptenroll-tpm2.c b/src/cryptenroll/cryptenroll-tpm2.c
8d419f
index f5f6b87d0f..e8c64dd753 100644
8d419f
--- a/src/cryptenroll/cryptenroll-tpm2.c
8d419f
+++ b/src/cryptenroll/cryptenroll-tpm2.c
8d419f
@@ -1,7 +1,9 @@
8d419f
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8d419f
 
8d419f
 #include "alloc-util.h"
8d419f
+#include "ask-password-api.h"
8d419f
 #include "cryptenroll-tpm2.h"
8d419f
+#include "env-util.h"
8d419f
 #include "hexdecoct.h"
8d419f
 #include "json.h"
8d419f
 #include "memory-util.h"
8d419f
@@ -58,11 +60,78 @@ static int search_policy_hash(
8d419f
         return -ENOENT; /* Not found */
8d419f
 }
8d419f
 
8d419f
+static int get_pin(char **ret_pin_str, TPM2Flags *ret_flags) {
8d419f
+        _cleanup_free_ char *pin_str = NULL;
8d419f
+        int r;
8d419f
+        TPM2Flags flags = 0;
8d419f
+
8d419f
+        assert(ret_pin_str);
8d419f
+        assert(ret_flags);
8d419f
+
8d419f
+        r = getenv_steal_erase("NEWPIN", &pin_str);
8d419f
+        if (r < 0)
8d419f
+                return log_error_errno(r, "Failed to acquire PIN from environment: %m");
8d419f
+        if (r > 0)
8d419f
+                flags |= TPM2_FLAGS_USE_PIN;
8d419f
+        else {
8d419f
+                for (size_t i = 5;; i--) {
8d419f
+                        _cleanup_strv_free_erase_ char **pin = NULL, **pin2 = NULL;
8d419f
+
8d419f
+                        if (i <= 0)
8d419f
+                                return log_error_errno(
8d419f
+                                                SYNTHETIC_ERRNO(ENOKEY), "Too many attempts, giving up.");
8d419f
+
8d419f
+                        pin = strv_free_erase(pin);
8d419f
+                        r = ask_password_auto(
8d419f
+                                        "Please enter TPM2 PIN:",
8d419f
+                                        "drive-harddisk",
8d419f
+                                        NULL,
8d419f
+                                        "tpm2-pin",
8d419f
+                                        "cryptenroll.tpm2-pin",
8d419f
+                                        USEC_INFINITY,
8d419f
+                                        0,
8d419f
+                                        &pin;;
8d419f
+                        if (r < 0)
8d419f
+                                return log_error_errno(r, "Failed to ask for user pin: %m");
8d419f
+                        assert(strv_length(pin) == 1);
8d419f
+
8d419f
+                        r = ask_password_auto(
8d419f
+                                        "Please enter TPM2 PIN (repeat):",
8d419f
+                                        "drive-harddisk",
8d419f
+                                        NULL,
8d419f
+                                        "tpm2-pin",
8d419f
+                                        "cryptenroll.tpm2-pin",
8d419f
+                                        USEC_INFINITY,
8d419f
+                                        0,
8d419f
+                                        &pin2);
8d419f
+                        if (r < 0)
8d419f
+                                return log_error_errno(r, "Failed to ask for user pin: %m");
8d419f
+                        assert(strv_length(pin) == 1);
8d419f
+
8d419f
+                        if (strv_equal(pin, pin2)) {
8d419f
+                                pin_str = strdup(*pin);
8d419f
+                                if (!pin_str)
8d419f
+                                        return log_oom();
8d419f
+                                flags |= TPM2_FLAGS_USE_PIN;
8d419f
+                                break;
8d419f
+                        }
8d419f
+
8d419f
+                        log_error("PINs didn't match, please try again!");
8d419f
+                }
8d419f
+        }
8d419f
+
8d419f
+        *ret_flags = flags;
8d419f
+        *ret_pin_str = TAKE_PTR(pin_str);
8d419f
+
8d419f
+        return 0;
8d419f
+}
8d419f
+
8d419f
 int enroll_tpm2(struct crypt_device *cd,
8d419f
                 const void *volume_key,
8d419f
                 size_t volume_key_size,
8d419f
                 const char *device,
8d419f
-                uint32_t pcr_mask) {
8d419f
+                uint32_t pcr_mask,
8d419f
+                bool use_pin) {
8d419f
 
8d419f
         _cleanup_(erase_and_freep) void *secret = NULL, *secret2 = NULL;
8d419f
         _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
8d419f
@@ -71,7 +140,9 @@ int enroll_tpm2(struct crypt_device *cd,
8d419f
         _cleanup_free_ void *blob = NULL, *hash = NULL;
8d419f
         uint16_t pcr_bank, primary_alg;
8d419f
         const char *node;
8d419f
+        _cleanup_(erase_and_freep) char *pin_str = NULL;
8d419f
         int r, keyslot;
8d419f
+        TPM2Flags flags = 0;
8d419f
 
8d419f
         assert(cd);
8d419f
         assert(volume_key);
8d419f
@@ -80,7 +151,13 @@ int enroll_tpm2(struct crypt_device *cd,
8d419f
 
8d419f
         assert_se(node = crypt_get_device_name(cd));
8d419f
 
8d419f
-        r = tpm2_seal(device, pcr_mask, NULL, &secret, &secret_size, &blob, &blob_size, &hash, &hash_size, &pcr_bank, &primary_alg);
8d419f
+        if (use_pin) {
8d419f
+                r = get_pin(&pin_str, &flags);
8d419f
+                if (r < 0)
8d419f
+                        return r;
8d419f
+        }
8d419f
+
8d419f
+        r = tpm2_seal(device, pcr_mask, pin_str, &secret, &secret_size, &blob, &blob_size, &hash, &hash_size, &pcr_bank, &primary_alg);
8d419f
         if (r < 0)
8d419f
                 return r;
8d419f
 
8d419f
@@ -97,7 +174,7 @@ int enroll_tpm2(struct crypt_device *cd,
8d419f
 
8d419f
         /* Quick verification that everything is in order, we are not in a hurry after all. */
8d419f
         log_debug("Unsealing for verification...");
8d419f
-        r = tpm2_unseal(device, pcr_mask, pcr_bank, primary_alg, blob, blob_size, hash, hash_size, NULL, &secret2, &secret2_size);
8d419f
+        r = tpm2_unseal(device, pcr_mask, pcr_bank, primary_alg, blob, blob_size, hash, hash_size, pin_str, &secret2, &secret2_size);
8d419f
         if (r < 0)
8d419f
                 return r;
8d419f
 
8d419f
@@ -123,7 +200,7 @@ int enroll_tpm2(struct crypt_device *cd,
8d419f
         if (keyslot < 0)
8d419f
                 return log_error_errno(keyslot, "Failed to add new TPM2 key to %s: %m", node);
8d419f
 
8d419f
-        r = tpm2_make_luks2_json(keyslot, pcr_mask, pcr_bank, primary_alg, blob, blob_size, hash, hash_size, &v);
8d419f
+        r = tpm2_make_luks2_json(keyslot, pcr_mask, pcr_bank, primary_alg, blob, blob_size, hash, hash_size, flags, &v);
8d419f
         if (r < 0)
8d419f
                 return log_error_errno(r, "Failed to prepare TPM2 JSON token object: %m");
8d419f
 
8d419f
diff --git a/src/cryptenroll/cryptenroll-tpm2.h b/src/cryptenroll/cryptenroll-tpm2.h
8d419f
index d5dd1b0003..742f49b8d5 100644
8d419f
--- a/src/cryptenroll/cryptenroll-tpm2.h
8d419f
+++ b/src/cryptenroll/cryptenroll-tpm2.h
8d419f
@@ -7,9 +7,9 @@
8d419f
 #include "log.h"
8d419f
 
8d419f
 #if HAVE_TPM2
8d419f
-int enroll_tpm2(struct crypt_device *cd, const void *volume_key, size_t volume_key_size, const char *device, uint32_t pcr_mask);
8d419f
+int enroll_tpm2(struct crypt_device *cd, const void *volume_key, size_t volume_key_size, const char *device, uint32_t pcr_mask, bool use_pin);
8d419f
 #else
8d419f
-static inline int enroll_tpm2(struct crypt_device *cd, const void *volume_key, size_t volume_key_size, const char *device, uint32_t pcr_mask) {
8d419f
+static inline int enroll_tpm2(struct crypt_device *cd, const void *volume_key, size_t volume_key_size, const char *device, uint32_t pcr_mask, bool use_pin) {
8d419f
         return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
8d419f
                                "TPM2 key enrollment not supported.");
8d419f
 }
8d419f
diff --git a/src/cryptenroll/cryptenroll.c b/src/cryptenroll/cryptenroll.c
8d419f
index 7f397f197f..ed19f3f8f4 100644
8d419f
--- a/src/cryptenroll/cryptenroll.c
8d419f
+++ b/src/cryptenroll/cryptenroll.c
8d419f
@@ -32,6 +32,7 @@ static char *arg_pkcs11_token_uri = NULL;
8d419f
 static char *arg_fido2_device = NULL;
8d419f
 static char *arg_tpm2_device = NULL;
8d419f
 static uint32_t arg_tpm2_pcr_mask = UINT32_MAX;
8d419f
+static bool arg_tpm2_pin = false;
8d419f
 static char *arg_node = NULL;
8d419f
 static int *arg_wipe_slots = NULL;
8d419f
 static size_t arg_n_wipe_slots = 0;
8d419f
@@ -100,6 +101,8 @@ static int help(void) {
8d419f
                "                       Enroll a TPM2 device\n"
8d419f
                "     --tpm2-pcrs=PCR1+PCR2+PCR3+…\n"
8d419f
                "                       Specify TPM2 PCRs to seal against\n"
8d419f
+               "     --tpm2-with-pin=BOOL\n"
8d419f
+               "                       Whether to require entering a PIN to unlock the volume\n"
8d419f
                "     --wipe-slot=SLOT1,SLOT2,…\n"
8d419f
                "                       Wipe specified slots\n"
8d419f
                "\nSee the %s for details.\n",
8d419f
@@ -121,6 +124,7 @@ static int parse_argv(int argc, char *argv[]) {
8d419f
                 ARG_FIDO2_DEVICE,
8d419f
                 ARG_TPM2_DEVICE,
8d419f
                 ARG_TPM2_PCRS,
8d419f
+                ARG_TPM2_PIN,
8d419f
                 ARG_WIPE_SLOT,
8d419f
                 ARG_FIDO2_WITH_PIN,
8d419f
                 ARG_FIDO2_WITH_UP,
8d419f
@@ -139,6 +143,7 @@ static int parse_argv(int argc, char *argv[]) {
8d419f
                 { "fido2-with-user-verification", required_argument, NULL, ARG_FIDO2_WITH_UV    },
8d419f
                 { "tpm2-device",                  required_argument, NULL, ARG_TPM2_DEVICE      },
8d419f
                 { "tpm2-pcrs",                    required_argument, NULL, ARG_TPM2_PCRS        },
8d419f
+                { "tpm2-with-pin",                required_argument, NULL, ARG_TPM2_PIN         },
8d419f
                 { "wipe-slot",                    required_argument, NULL, ARG_WIPE_SLOT        },
8d419f
                 {}
8d419f
         };
8d419f
@@ -301,6 +306,14 @@ static int parse_argv(int argc, char *argv[]) {
8d419f
                         break;
8d419f
                 }
8d419f
 
8d419f
+                case ARG_TPM2_PIN: {
8d419f
+                        r = parse_boolean_argument("--tpm2-with-pin=", optarg, &arg_tpm2_pin);
8d419f
+                        if (r < 0)
8d419f
+                                return r;
8d419f
+
8d419f
+                        break;
8d419f
+                }
8d419f
+
8d419f
                 case ARG_WIPE_SLOT: {
8d419f
                         const char *p = optarg;
8d419f
 
8d419f
@@ -563,7 +576,7 @@ static int run(int argc, char *argv[]) {
8d419f
                 break;
8d419f
 
8d419f
         case ENROLL_TPM2:
8d419f
-                slot = enroll_tpm2(cd, vk, vks, arg_tpm2_device, arg_tpm2_pcr_mask);
8d419f
+                slot = enroll_tpm2(cd, vk, vks, arg_tpm2_device, arg_tpm2_pcr_mask, arg_tpm2_pin);
8d419f
                 break;
8d419f
 
8d419f
         case _ENROLL_TYPE_INVALID:
8d419f
diff --git a/src/partition/repart.c b/src/partition/repart.c
8d419f
index adfec0b9f3..67e379be55 100644
8d419f
--- a/src/partition/repart.c
8d419f
+++ b/src/partition/repart.c
8d419f
@@ -2677,7 +2677,7 @@ static int partition_encrypt(
8d419f
                 if (keyslot < 0)
8d419f
                         return log_error_errno(keyslot, "Failed to add new TPM2 key to %s: %m", node);
8d419f
 
8d419f
-                r = tpm2_make_luks2_json(keyslot, arg_tpm2_pcr_mask, pcr_bank, primary_alg, blob, blob_size, hash, hash_size, &v);
8d419f
+                r = tpm2_make_luks2_json(keyslot, arg_tpm2_pcr_mask, pcr_bank, primary_alg, blob, blob_size, hash, hash_size, 0, &v);
8d419f
                 if (r < 0)
8d419f
                         return log_error_errno(r, "Failed to prepare TPM2 JSON token object: %m");
8d419f
 
8d419f
diff --git a/src/shared/tpm2-util.c b/src/shared/tpm2-util.c
8d419f
index aca7b69ab5..44fe899acd 100644
8d419f
--- a/src/shared/tpm2-util.c
8d419f
+++ b/src/shared/tpm2-util.c
8d419f
@@ -1291,6 +1291,7 @@ int tpm2_make_luks2_json(
8d419f
                 size_t blob_size,
8d419f
                 const void *policy_hash,
8d419f
                 size_t policy_hash_size,
8d419f
+                TPM2Flags flags,
8d419f
                 JsonVariant **ret) {
8d419f
 
8d419f
         _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *a = NULL;
8d419f
@@ -1331,7 +1332,9 @@ int tpm2_make_luks2_json(
8d419f
                                        JSON_BUILD_PAIR("tpm2-pcrs", JSON_BUILD_VARIANT(a)),
8d419f
                                        JSON_BUILD_PAIR_CONDITION(!!tpm2_pcr_bank_to_string(pcr_bank), "tpm2-pcr-bank", JSON_BUILD_STRING(tpm2_pcr_bank_to_string(pcr_bank))),
8d419f
                                        JSON_BUILD_PAIR_CONDITION(!!tpm2_primary_alg_to_string(primary_alg), "tpm2-primary-alg", JSON_BUILD_STRING(tpm2_primary_alg_to_string(primary_alg))),
8d419f
-                                       JSON_BUILD_PAIR("tpm2-policy-hash", JSON_BUILD_HEX(policy_hash, policy_hash_size))));
8d419f
+                                       JSON_BUILD_PAIR("tpm2-policy-hash", JSON_BUILD_HEX(policy_hash, policy_hash_size)),
8d419f
+                                       JSON_BUILD_PAIR("tpm2-pin", JSON_BUILD_BOOLEAN(flags & TPM2_FLAGS_USE_PIN)))
8d419f
+                        );
8d419f
         if (r < 0)
8d419f
                 return r;
8d419f
 
8d419f
diff --git a/src/shared/tpm2-util.h b/src/shared/tpm2-util.h
8d419f
index 784e9fd11e..5a9bcf8c24 100644
8d419f
--- a/src/shared/tpm2-util.h
8d419f
+++ b/src/shared/tpm2-util.h
8d419f
@@ -6,6 +6,10 @@
8d419f
 #include "json.h"
8d419f
 #include "macro.h"
8d419f
 
8d419f
+typedef enum TPM2Flags {
8d419f
+        TPM2_FLAGS_USE_PIN = 1 << 0,
8d419f
+} TPM2Flags;
8d419f
+
8d419f
 #if HAVE_TPM2
8d419f
 
8d419f
 #include <tss2/tss2_esys.h>
8d419f
@@ -49,7 +53,7 @@ int tpm2_find_device_auto(int log_level, char **ret);
8d419f
 
8d419f
 int tpm2_parse_pcrs(const char *s, uint32_t *ret);
8d419f
 
8d419f
-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, JsonVariant **ret);
8d419f
+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);
8d419f
 
8d419f
 #define TPM2_PCRS_MAX 24
8d419f