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