richardphibel / rpms / systemd

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