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