naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
594167
From 1b7d251ab836ba703913eda149f05bd03559c483 Mon Sep 17 00:00:00 2001
594167
From: Grigori Goronzy <greg@chown.ath.cx>
594167
Date: Fri, 18 Feb 2022 12:00:12 +0100
594167
Subject: [PATCH] cryptsetup: add libcryptsetup TPM2 PIN support
594167
594167
This is unfinished: we don't have any way to actually query for PINs
594167
interactively this way. It is similar to FIDO2 and PKCS#11 in this
594167
regard.
594167
594167
Nonetheless, this code is capable of validating and dumping tokens, so
594167
it is already useful as-is.
594167
594167
(cherry picked from commit 1f895adac287b5f1b6b854caa586093616ccc172)
594167
594167
Related: #2087652
594167
---
594167
 .../cryptsetup-token-systemd-tpm2.c           | 18 +++++++++--
594167
 src/cryptsetup/cryptsetup-tokens/luks2-tpm2.c | 32 +++++++++++++++++--
594167
 src/cryptsetup/cryptsetup-tokens/luks2-tpm2.h |  6 +++-
594167
 3 files changed, 51 insertions(+), 5 deletions(-)
594167
594167
diff --git a/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-systemd-tpm2.c b/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-systemd-tpm2.c
594167
index e2d28a5dda..23df974999 100644
594167
--- a/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-systemd-tpm2.c
594167
+++ b/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-systemd-tpm2.c
594167
@@ -6,8 +6,10 @@
594167
 #include "cryptsetup-token.h"
594167
 #include "cryptsetup-token-util.h"
594167
 #include "hexdecoct.h"
594167
+#include "json.h"
594167
 #include "luks2-tpm2.h"
594167
 #include "memory-util.h"
594167
+#include "strv.h"
594167
 #include "tpm2-util.h"
594167
 #include "version.h"
594167
 
594167
@@ -78,7 +80,8 @@ _public_ int cryptsetup_token_open(
594167
         if (usrptr)
594167
                 params = *(systemd_tpm2_plugin_params *)usrptr;
594167
 
594167
-        r = parse_luks2_tpm2_data(json, params.search_pcr_mask, &pcr_mask, &pcr_bank, &primary_alg, &base64_blob, &hex_policy_hash);
594167
+        TPM2Flags flags = 0;
594167
+        r = parse_luks2_tpm2_data(json, params.search_pcr_mask, &pcr_mask, &pcr_bank, &primary_alg, &base64_blob, &hex_policy_hash, &flags);
594167
         if (r < 0)
594167
                 return log_debug_open_error(cd, r);
594167
 
594167
@@ -101,6 +104,7 @@ _public_ int cryptsetup_token_open(
594167
                         blob_size,
594167
                         policy_hash,
594167
                         policy_hash_size,
594167
+                        flags,
594167
                         &decrypted_key,
594167
                         &decrypted_key_size);
594167
         if (r < 0)
594167
@@ -135,6 +139,7 @@ _public_ void cryptsetup_token_dump(
594167
                 const char *json /* validated 'systemd-tpm2' token if cryptsetup_token_validate is defined */) {
594167
 
594167
         int r;
594167
+        TPM2Flags flags = 0;
594167
         uint32_t pcr_mask;
594167
         uint16_t pcr_bank, primary_alg;
594167
         size_t decoded_blob_size;
594167
@@ -144,7 +149,7 @@ _public_ void cryptsetup_token_dump(
594167
 
594167
         assert(json);
594167
 
594167
-        r = parse_luks2_tpm2_data(json, UINT32_MAX, &pcr_mask, &pcr_bank, &primary_alg, &base64_blob, &hex_policy_hash);
594167
+        r = parse_luks2_tpm2_data(json, UINT32_MAX, &pcr_mask, &pcr_bank, &primary_alg, &base64_blob, &hex_policy_hash, &flags);
594167
         if (r < 0)
594167
                 return (void) crypt_log_debug_errno(cd, r, "Failed to parse " TOKEN_NAME " metadata: %m.");
594167
 
594167
@@ -171,6 +176,7 @@ _public_ void cryptsetup_token_dump(
594167
         crypt_log(cd, "\ttpm2-primary-alg:  %s\n", strna(tpm2_primary_alg_to_string(primary_alg)));
594167
         crypt_log(cd, "\ttpm2-blob:  %s\n", blob_str);
594167
         crypt_log(cd, "\ttpm2-policy-hash:" CRYPT_DUMP_LINE_SEP "%s\n", policy_hash_str);
594167
+        crypt_log(cd, "\ttpm2-pin: %s\n", true_false(flags & TPM2_FLAGS_USE_PIN));
594167
 }
594167
 
594167
 /*
594167
@@ -268,5 +274,13 @@ _public_ int cryptsetup_token_validate(
594167
         if (r < 0)
594167
                 return crypt_log_debug_errno(cd, r, "Invalid base64 data in 'tpm2-policy-hash' field: %m");
594167
 
594167
+        w = json_variant_by_key(v, "tpm2-pin");
594167
+        if (w) {
594167
+                if (!json_variant_is_boolean(w)) {
594167
+                        crypt_log_debug(cd, "TPM2 PIN policy is not a boolean.");
594167
+                        return 1;
594167
+                }
594167
+        }
594167
+
594167
         return 0;
594167
 }
594167
diff --git a/src/cryptsetup/cryptsetup-tokens/luks2-tpm2.c b/src/cryptsetup/cryptsetup-tokens/luks2-tpm2.c
594167
index de189c7bed..0d6e4bc0f8 100644
594167
--- a/src/cryptsetup/cryptsetup-tokens/luks2-tpm2.c
594167
+++ b/src/cryptsetup/cryptsetup-tokens/luks2-tpm2.c
594167
@@ -1,11 +1,15 @@
594167
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
594167
 
594167
 #include "alloc-util.h"
594167
+#include "ask-password-api.h"
594167
+#include "env-util.h"
594167
 #include "hexdecoct.h"
594167
 #include "json.h"
594167
+#include "log.h"
594167
 #include "luks2-tpm2.h"
594167
 #include "parse-util.h"
594167
 #include "random-util.h"
594167
+#include "strv.h"
594167
 #include "tpm2-util.h"
594167
 
594167
 int acquire_luks2_key(
594167
@@ -17,10 +21,12 @@ int acquire_luks2_key(
594167
                 size_t key_data_size,
594167
                 const void *policy_hash,
594167
                 size_t policy_hash_size,
594167
+                TPM2Flags flags,
594167
                 void **ret_decrypted_key,
594167
                 size_t *ret_decrypted_key_size) {
594167
 
594167
         _cleanup_free_ char *auto_device = NULL;
594167
+        _cleanup_(erase_and_freep) char *pin_str = NULL;
594167
         int r;
594167
 
594167
         assert(ret_decrypted_key);
594167
@@ -36,12 +42,22 @@ int acquire_luks2_key(
594167
                 device = auto_device;
594167
         }
594167
 
594167
+        r = getenv_steal_erase("PIN", &pin_str);
594167
+        if (r < 0)
594167
+                return log_error_errno(r, "Failed to acquire PIN from environment: %m");
594167
+        if (!r) {
594167
+                /* PIN entry is not supported by plugin, let it fallback, possibly to sd-cryptsetup's
594167
+                 * internal handling. */
594167
+                if (flags & TPM2_FLAGS_USE_PIN)
594167
+                        return -EOPNOTSUPP;
594167
+        }
594167
+
594167
         return tpm2_unseal(
594167
                         device,
594167
                         pcr_mask, pcr_bank,
594167
                         primary_alg,
594167
                         key_data, key_data_size,
594167
-                        policy_hash, policy_hash_size, NULL,
594167
+                        policy_hash, policy_hash_size, pin_str,
594167
                         ret_decrypted_key, ret_decrypted_key_size);
594167
 }
594167
 
594167
@@ -53,7 +69,8 @@ int parse_luks2_tpm2_data(
594167
                 uint16_t *ret_pcr_bank,
594167
                 uint16_t *ret_primary_alg,
594167
                 char **ret_base64_blob,
594167
-                char **ret_hex_policy_hash) {
594167
+                char **ret_hex_policy_hash,
594167
+                TPM2Flags *ret_flags) {
594167
 
594167
         int r;
594167
         JsonVariant *w, *e;
594167
@@ -61,6 +78,7 @@ int parse_luks2_tpm2_data(
594167
         uint16_t pcr_bank = UINT16_MAX, primary_alg = TPM2_ALG_ECC;
594167
         _cleanup_free_ char *base64_blob = NULL, *hex_policy_hash = NULL;
594167
         _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
594167
+        TPM2Flags flags = 0;
594167
 
594167
         assert(json);
594167
         assert(ret_pcr_mask);
594167
@@ -138,11 +156,21 @@ int parse_luks2_tpm2_data(
594167
         if (!hex_policy_hash)
594167
                 return -ENOMEM;
594167
 
594167
+        w = json_variant_by_key(v, "tpm2-pin");
594167
+        if (w) {
594167
+                if (!json_variant_is_boolean(w))
594167
+                        return -EINVAL;
594167
+
594167
+                if (json_variant_boolean(w))
594167
+                        flags |= TPM2_FLAGS_USE_PIN;
594167
+        }
594167
+
594167
         *ret_pcr_mask = pcr_mask;
594167
         *ret_pcr_bank = pcr_bank;
594167
         *ret_primary_alg = primary_alg;
594167
         *ret_base64_blob = TAKE_PTR(base64_blob);
594167
         *ret_hex_policy_hash = TAKE_PTR(hex_policy_hash);
594167
+        *ret_flags = flags;
594167
 
594167
         return 0;
594167
 }
594167
diff --git a/src/cryptsetup/cryptsetup-tokens/luks2-tpm2.h b/src/cryptsetup/cryptsetup-tokens/luks2-tpm2.h
594167
index 0c93ea82cc..34c93216ee 100644
594167
--- a/src/cryptsetup/cryptsetup-tokens/luks2-tpm2.h
594167
+++ b/src/cryptsetup/cryptsetup-tokens/luks2-tpm2.h
594167
@@ -2,6 +2,8 @@
594167
 
594167
 #pragma once
594167
 
594167
+#include "tpm2-util.h"
594167
+
594167
 struct crypt_device;
594167
 
594167
 int acquire_luks2_key(
594167
@@ -13,6 +15,7 @@ int acquire_luks2_key(
594167
                 size_t key_data_size,
594167
                 const void *policy_hash,
594167
                 size_t policy_hash_size,
594167
+                TPM2Flags flags,
594167
                 void **ret_decrypted_key,
594167
                 size_t *ret_decrypted_key_size);
594167
 
594167
@@ -23,4 +26,5 @@ int parse_luks2_tpm2_data(
594167
                 uint16_t *ret_pcr_bank,
594167
                 uint16_t *ret_primary_alg,
594167
                 char **ret_base64_blob,
594167
-                char **ret_hex_policy_hash);
594167
+                char **ret_hex_policy_hash,
594167
+                TPM2Flags *ret_flags);