|
|
8d419f |
From 2418e9c1409ba04ddff516a83d83b2daa3417832 Mon Sep 17 00:00:00 2001
|
|
|
8d419f |
From: Grigori Goronzy <greg@chown.ath.cx>
|
|
|
8d419f |
Date: Fri, 18 Feb 2022 11:56:02 +0100
|
|
|
8d419f |
Subject: [PATCH] cryptsetup: add support for TPM2 pin
|
|
|
8d419f |
|
|
|
8d419f |
Extend cryptsetup for TPM2 pin entry, similar to FIDO2.
|
|
|
8d419f |
|
|
|
8d419f |
(cherry picked from commit bea344a1a426e615ba87b66b6d3ff4b265c57a95)
|
|
|
8d419f |
|
|
|
8d419f |
Related: #2087652
|
|
|
8d419f |
---
|
|
|
8d419f |
src/cryptsetup/cryptsetup-tpm2.c | 108 ++++++++++++++++++++++++++++++-
|
|
|
8d419f |
src/cryptsetup/cryptsetup-tpm2.h | 16 ++++-
|
|
|
8d419f |
src/cryptsetup/cryptsetup.c | 16 ++++-
|
|
|
8d419f |
3 files changed, 135 insertions(+), 5 deletions(-)
|
|
|
8d419f |
|
|
|
8d419f |
diff --git a/src/cryptsetup/cryptsetup-tpm2.c b/src/cryptsetup/cryptsetup-tpm2.c
|
|
|
8d419f |
index 05d76a684d..b84d64def8 100644
|
|
|
8d419f |
--- a/src/cryptsetup/cryptsetup-tpm2.c
|
|
|
8d419f |
+++ b/src/cryptsetup/cryptsetup-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 "cryptsetup-tpm2.h"
|
|
|
8d419f |
+#include "env-util.h"
|
|
|
8d419f |
#include "fileio.h"
|
|
|
8d419f |
#include "hexdecoct.h"
|
|
|
8d419f |
#include "json.h"
|
|
|
8d419f |
@@ -9,6 +11,47 @@
|
|
|
8d419f |
#include "random-util.h"
|
|
|
8d419f |
#include "tpm2-util.h"
|
|
|
8d419f |
|
|
|
8d419f |
+static int get_pin(usec_t until, AskPasswordFlags ask_password_flags, bool headless, char **ret_pin_str) {
|
|
|
8d419f |
+ _cleanup_free_ char *pin_str = NULL;
|
|
|
8d419f |
+ _cleanup_strv_free_erase_ char **pin = NULL;
|
|
|
8d419f |
+ int r;
|
|
|
8d419f |
+
|
|
|
8d419f |
+ assert(ret_pin_str);
|
|
|
8d419f |
+
|
|
|
8d419f |
+ r = getenv_steal_erase("PIN", &pin_str);
|
|
|
8d419f |
+ if (r < 0)
|
|
|
8d419f |
+ return log_error_errno(r, "Failed to acquire PIN from environment: %m");
|
|
|
8d419f |
+ if (!r) {
|
|
|
8d419f |
+ if (headless)
|
|
|
8d419f |
+ return log_error_errno(
|
|
|
8d419f |
+ SYNTHETIC_ERRNO(ENOPKG),
|
|
|
8d419f |
+ "PIN querying disabled via 'headless' option. "
|
|
|
8d419f |
+ "Use the '$PIN' environment variable.");
|
|
|
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 |
+ "cryptsetup.tpm2-pin",
|
|
|
8d419f |
+ until,
|
|
|
8d419f |
+ ask_password_flags,
|
|
|
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 |
+ pin_str = strdup(pin[0]);
|
|
|
8d419f |
+ if (!pin_str)
|
|
|
8d419f |
+ return log_oom();
|
|
|
8d419f |
+ }
|
|
|
8d419f |
+
|
|
|
8d419f |
+ *ret_pin_str = TAKE_PTR(pin_str);
|
|
|
8d419f |
+
|
|
|
8d419f |
+ return r;
|
|
|
8d419f |
+}
|
|
|
8d419f |
+
|
|
|
8d419f |
int acquire_tpm2_key(
|
|
|
8d419f |
const char *volume_name,
|
|
|
8d419f |
const char *device,
|
|
|
8d419f |
@@ -22,6 +65,10 @@ int acquire_tpm2_key(
|
|
|
8d419f |
size_t key_data_size,
|
|
|
8d419f |
const void *policy_hash,
|
|
|
8d419f |
size_t policy_hash_size,
|
|
|
8d419f |
+ TPM2Flags flags,
|
|
|
8d419f |
+ usec_t until,
|
|
|
8d419f |
+ bool headless,
|
|
|
8d419f |
+ AskPasswordFlags ask_password_flags,
|
|
|
8d419f |
void **ret_decrypted_key,
|
|
|
8d419f |
size_t *ret_decrypted_key_size) {
|
|
|
8d419f |
|
|
|
8d419f |
@@ -64,7 +111,51 @@ int acquire_tpm2_key(
|
|
|
8d419f |
blob = loaded_blob;
|
|
|
8d419f |
}
|
|
|
8d419f |
|
|
|
8d419f |
- return tpm2_unseal(device, pcr_mask, pcr_bank, primary_alg, blob, blob_size, policy_hash, policy_hash_size, NULL, ret_decrypted_key, ret_decrypted_key_size);
|
|
|
8d419f |
+ if (!(flags & TPM2_FLAGS_USE_PIN))
|
|
|
8d419f |
+ return tpm2_unseal(
|
|
|
8d419f |
+ device,
|
|
|
8d419f |
+ pcr_mask,
|
|
|
8d419f |
+ pcr_bank,
|
|
|
8d419f |
+ primary_alg,
|
|
|
8d419f |
+ blob,
|
|
|
8d419f |
+ blob_size,
|
|
|
8d419f |
+ policy_hash,
|
|
|
8d419f |
+ policy_hash_size,
|
|
|
8d419f |
+ NULL,
|
|
|
8d419f |
+ ret_decrypted_key,
|
|
|
8d419f |
+ ret_decrypted_key_size);
|
|
|
8d419f |
+
|
|
|
8d419f |
+ for (int i = 5;; i--) {
|
|
|
8d419f |
+ _cleanup_(erase_and_freep) char *pin_str = NULL;
|
|
|
8d419f |
+
|
|
|
8d419f |
+ if (i <= 0)
|
|
|
8d419f |
+ return -EACCES;
|
|
|
8d419f |
+
|
|
|
8d419f |
+ r = get_pin(until, ask_password_flags, headless, &pin_str);
|
|
|
8d419f |
+ if (r < 0)
|
|
|
8d419f |
+ return r;
|
|
|
8d419f |
+
|
|
|
8d419f |
+ r = tpm2_unseal(
|
|
|
8d419f |
+ device,
|
|
|
8d419f |
+ pcr_mask,
|
|
|
8d419f |
+ pcr_bank,
|
|
|
8d419f |
+ primary_alg,
|
|
|
8d419f |
+ blob,
|
|
|
8d419f |
+ blob_size,
|
|
|
8d419f |
+ policy_hash,
|
|
|
8d419f |
+ policy_hash_size,
|
|
|
8d419f |
+ pin_str,
|
|
|
8d419f |
+ ret_decrypted_key,
|
|
|
8d419f |
+ ret_decrypted_key_size);
|
|
|
8d419f |
+ /* We get this error in case there is an authentication policy mismatch. This should
|
|
|
8d419f |
+ * not happen, but this avoids confusing behavior, just in case. */
|
|
|
8d419f |
+ if (IN_SET(r, -EPERM, -ENOLCK))
|
|
|
8d419f |
+ return r;
|
|
|
8d419f |
+ if (r < 0)
|
|
|
8d419f |
+ continue;
|
|
|
8d419f |
+
|
|
|
8d419f |
+ return r;
|
|
|
8d419f |
+ }
|
|
|
8d419f |
}
|
|
|
8d419f |
|
|
|
8d419f |
int find_tpm2_auto_data(
|
|
|
8d419f |
@@ -79,11 +170,13 @@ int find_tpm2_auto_data(
|
|
|
8d419f |
void **ret_policy_hash,
|
|
|
8d419f |
size_t *ret_policy_hash_size,
|
|
|
8d419f |
int *ret_keyslot,
|
|
|
8d419f |
- int *ret_token) {
|
|
|
8d419f |
+ int *ret_token,
|
|
|
8d419f |
+ TPM2Flags *ret_flags) {
|
|
|
8d419f |
|
|
|
8d419f |
_cleanup_free_ void *blob = NULL, *policy_hash = NULL;
|
|
|
8d419f |
size_t blob_size = 0, policy_hash_size = 0;
|
|
|
8d419f |
int r, keyslot = -1, token = -1;
|
|
|
8d419f |
+ TPM2Flags flags = 0;
|
|
|
8d419f |
uint32_t pcr_mask = 0;
|
|
|
8d419f |
uint16_t pcr_bank = UINT16_MAX; /* default: pick automatically */
|
|
|
8d419f |
uint16_t primary_alg = TPM2_ALG_ECC; /* ECC was the only supported algorithm in systemd < 250, use that as implied default, for compatibility */
|
|
|
8d419f |
@@ -196,6 +289,16 @@ int find_tpm2_auto_data(
|
|
|
8d419f |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
8d419f |
"Invalid base64 data in 'tpm2-policy-hash' field.");
|
|
|
8d419f |
|
|
|
8d419f |
+ w = json_variant_by_key(v, "tpm2-pin");
|
|
|
8d419f |
+ if (w) {
|
|
|
8d419f |
+ if (!json_variant_is_boolean(w))
|
|
|
8d419f |
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
8d419f |
+ "TPM2 PIN policy is not a boolean.");
|
|
|
8d419f |
+
|
|
|
8d419f |
+ if (json_variant_boolean(w))
|
|
|
8d419f |
+ flags |= TPM2_FLAGS_USE_PIN;
|
|
|
8d419f |
+ }
|
|
|
8d419f |
+
|
|
|
8d419f |
break;
|
|
|
8d419f |
}
|
|
|
8d419f |
|
|
|
8d419f |
@@ -215,6 +318,7 @@ int find_tpm2_auto_data(
|
|
|
8d419f |
*ret_token = token;
|
|
|
8d419f |
*ret_pcr_bank = pcr_bank;
|
|
|
8d419f |
*ret_primary_alg = primary_alg;
|
|
|
8d419f |
+ *ret_flags = flags;
|
|
|
8d419f |
|
|
|
8d419f |
return 0;
|
|
|
8d419f |
}
|
|
|
8d419f |
diff --git a/src/cryptsetup/cryptsetup-tpm2.h b/src/cryptsetup/cryptsetup-tpm2.h
|
|
|
8d419f |
index bd04620462..ab16d0a18f 100644
|
|
|
8d419f |
--- a/src/cryptsetup/cryptsetup-tpm2.h
|
|
|
8d419f |
+++ b/src/cryptsetup/cryptsetup-tpm2.h
|
|
|
8d419f |
@@ -3,9 +3,11 @@
|
|
|
8d419f |
|
|
|
8d419f |
#include <sys/types.h>
|
|
|
8d419f |
|
|
|
8d419f |
+#include "ask-password-api.h"
|
|
|
8d419f |
#include "cryptsetup-util.h"
|
|
|
8d419f |
#include "log.h"
|
|
|
8d419f |
#include "time-util.h"
|
|
|
8d419f |
+#include "tpm2-util.h"
|
|
|
8d419f |
|
|
|
8d419f |
#if HAVE_TPM2
|
|
|
8d419f |
|
|
|
8d419f |
@@ -22,6 +24,10 @@ int acquire_tpm2_key(
|
|
|
8d419f |
size_t key_data_size,
|
|
|
8d419f |
const void *policy_hash,
|
|
|
8d419f |
size_t policy_hash_size,
|
|
|
8d419f |
+ TPM2Flags flags,
|
|
|
8d419f |
+ usec_t until,
|
|
|
8d419f |
+ bool headless,
|
|
|
8d419f |
+ AskPasswordFlags ask_password_flags,
|
|
|
8d419f |
void **ret_decrypted_key,
|
|
|
8d419f |
size_t *ret_decrypted_key_size);
|
|
|
8d419f |
|
|
|
8d419f |
@@ -37,7 +43,8 @@ int find_tpm2_auto_data(
|
|
|
8d419f |
void **ret_policy_hash,
|
|
|
8d419f |
size_t *ret_policy_hash_size,
|
|
|
8d419f |
int *ret_keyslot,
|
|
|
8d419f |
- int *ret_token);
|
|
|
8d419f |
+ int *ret_token,
|
|
|
8d419f |
+ TPM2Flags *ret_flags);
|
|
|
8d419f |
|
|
|
8d419f |
#else
|
|
|
8d419f |
|
|
|
8d419f |
@@ -54,6 +61,10 @@ static inline int acquire_tpm2_key(
|
|
|
8d419f |
size_t key_data_size,
|
|
|
8d419f |
const void *policy_hash,
|
|
|
8d419f |
size_t policy_hash_size,
|
|
|
8d419f |
+ TPM2Flags flags,
|
|
|
8d419f |
+ usec_t until,
|
|
|
8d419f |
+ bool headless,
|
|
|
8d419f |
+ AskPasswordFlags ask_password_flags,
|
|
|
8d419f |
void **ret_decrypted_key,
|
|
|
8d419f |
size_t *ret_decrypted_key_size) {
|
|
|
8d419f |
|
|
|
8d419f |
@@ -73,7 +84,8 @@ static inline int find_tpm2_auto_data(
|
|
|
8d419f |
void **ret_policy_hash,
|
|
|
8d419f |
size_t *ret_policy_hash_size,
|
|
|
8d419f |
int *ret_keyslot,
|
|
|
8d419f |
- int *ret_token) {
|
|
|
8d419f |
+ int *ret_token,
|
|
|
8d419f |
+ TPM2Flags *ret_flags) {
|
|
|
8d419f |
|
|
|
8d419f |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
|
|
|
8d419f |
"TPM2 support not available.");
|
|
|
8d419f |
diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c
|
|
|
8d419f |
index dddd976dc8..ede0f7ed0b 100644
|
|
|
8d419f |
--- a/src/cryptsetup/cryptsetup.c
|
|
|
8d419f |
+++ b/src/cryptsetup/cryptsetup.c
|
|
|
8d419f |
@@ -1301,9 +1301,15 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
|
|
|
8d419f |
key_file, arg_keyfile_size, arg_keyfile_offset,
|
|
|
8d419f |
key_data, key_data_size,
|
|
|
8d419f |
NULL, 0, /* we don't know the policy hash */
|
|
|
8d419f |
+ 0, /* PIN is currently unhandled in this case */
|
|
|
8d419f |
+ until,
|
|
|
8d419f |
+ arg_headless,
|
|
|
8d419f |
+ arg_ask_password_flags,
|
|
|
8d419f |
&decrypted_key, &decrypted_key_size);
|
|
|
8d419f |
if (r >= 0)
|
|
|
8d419f |
break;
|
|
|
8d419f |
+ if (IN_SET(r, -EACCES, -ENOLCK))
|
|
|
8d419f |
+ return log_error_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 PIN unlock failed, falling back to traditional unlocking.");
|
|
|
8d419f |
if (ERRNO_IS_NOT_SUPPORTED(r)) /* TPM2 support not compiled in? */
|
|
|
8d419f |
return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 support not available, falling back to traditional unlocking.");
|
|
|
8d419f |
if (r != -EAGAIN) /* EAGAIN means: no tpm2 chip found */
|
|
|
8d419f |
@@ -1335,6 +1341,7 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
|
|
|
8d419f |
for (;;) {
|
|
|
8d419f |
uint32_t pcr_mask;
|
|
|
8d419f |
uint16_t pcr_bank, primary_alg;
|
|
|
8d419f |
+ TPM2Flags tpm2_flags;
|
|
|
8d419f |
|
|
|
8d419f |
r = find_tpm2_auto_data(
|
|
|
8d419f |
cd,
|
|
|
8d419f |
@@ -1346,7 +1353,8 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
|
|
|
8d419f |
&blob, &blob_size,
|
|
|
8d419f |
&policy_hash, &policy_hash_size,
|
|
|
8d419f |
&keyslot,
|
|
|
8d419f |
- &token);
|
|
|
8d419f |
+ &token,
|
|
|
8d419f |
+ &tpm2_flags);
|
|
|
8d419f |
if (r == -ENXIO)
|
|
|
8d419f |
/* No further TPM2 tokens found in the LUKS2 header. */
|
|
|
8d419f |
return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
|
|
|
8d419f |
@@ -1369,7 +1377,13 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
|
|
|
8d419f |
NULL, 0, 0, /* no key file */
|
|
|
8d419f |
blob, blob_size,
|
|
|
8d419f |
policy_hash, policy_hash_size,
|
|
|
8d419f |
+ tpm2_flags,
|
|
|
8d419f |
+ until,
|
|
|
8d419f |
+ arg_headless,
|
|
|
8d419f |
+ arg_ask_password_flags,
|
|
|
8d419f |
&decrypted_key, &decrypted_key_size);
|
|
|
8d419f |
+ if (IN_SET(r, -EACCES, -ENOLCK))
|
|
|
8d419f |
+ return log_error_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 PIN unlock failed, falling back to traditional unlocking.");
|
|
|
8d419f |
if (r != -EPERM)
|
|
|
8d419f |
break;
|
|
|
8d419f |
|