594167
From 98997b288819f4dac7b2ca19c199d71e733bfa92 Mon Sep 17 00:00:00 2001
594167
From: Grigori Goronzy <greg@chown.ath.cx>
594167
Date: Wed, 16 Feb 2022 22:13:42 +0100
594167
Subject: [PATCH] tpm2: support policies with PIN
594167
594167
Modify TPM2 authentication policy to optionally include an authValue, i.e.
594167
a password/PIN. We use the "PIN" terminology since it's used by other
594167
systems such as Windows, even though the PIN is not necessarily numeric.
594167
594167
The pin is hashed via SHA256 to allow for arbitrary length PINs.
594167
594167
v2: fix tpm2_seal in sd-repart
594167
v3: applied review feedback
594167
(cherry picked from commit 2f5a892aa0d70aa4f1f10c8dba495ad52bc02bc3)
594167
594167
Related: #2087652
594167
---
594167
 src/cryptenroll/cryptenroll-tpm2.c            |  4 +-
594167
 src/cryptsetup/cryptsetup-tokens/luks2-tpm2.c |  2 +-
594167
 src/cryptsetup/cryptsetup-tpm2.c              |  2 +-
594167
 src/partition/repart.c                        |  2 +-
594167
 src/shared/creds-util.c                       |  2 +
594167
 src/shared/tpm2-util.c                        | 76 ++++++++++++++++++-
594167
 src/shared/tpm2-util.h                        |  8 +-
594167
 7 files changed, 85 insertions(+), 11 deletions(-)
594167
594167
diff --git a/src/cryptenroll/cryptenroll-tpm2.c b/src/cryptenroll/cryptenroll-tpm2.c
594167
index 801014af11..f5f6b87d0f 100644
594167
--- a/src/cryptenroll/cryptenroll-tpm2.c
594167
+++ b/src/cryptenroll/cryptenroll-tpm2.c
594167
@@ -80,7 +80,7 @@ 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, &secret, &secret_size, &blob, &blob_size, &hash, &hash_size, &pcr_bank, &primary_alg);
594167
+        r = tpm2_seal(device, pcr_mask, NULL, &secret, &secret_size, &blob, &blob_size, &hash, &hash_size, &pcr_bank, &primary_alg);
594167
         if (r < 0)
594167
                 return r;
594167
 
594167
@@ -97,7 +97,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, &secret2, &secret2_size);
594167
+        r = tpm2_unseal(device, pcr_mask, pcr_bank, primary_alg, blob, blob_size, hash, hash_size, NULL, &secret2, &secret2_size);
594167
         if (r < 0)
594167
                 return r;
594167
 
594167
diff --git a/src/cryptsetup/cryptsetup-tokens/luks2-tpm2.c b/src/cryptsetup/cryptsetup-tokens/luks2-tpm2.c
594167
index 3d39dfa884..de189c7bed 100644
594167
--- a/src/cryptsetup/cryptsetup-tokens/luks2-tpm2.c
594167
+++ b/src/cryptsetup/cryptsetup-tokens/luks2-tpm2.c
594167
@@ -41,7 +41,7 @@ int acquire_luks2_key(
594167
                         pcr_mask, pcr_bank,
594167
                         primary_alg,
594167
                         key_data, key_data_size,
594167
-                        policy_hash, policy_hash_size,
594167
+                        policy_hash, policy_hash_size, NULL,
594167
                         ret_decrypted_key, ret_decrypted_key_size);
594167
 }
594167
 
594167
diff --git a/src/cryptsetup/cryptsetup-tpm2.c b/src/cryptsetup/cryptsetup-tpm2.c
594167
index cb139518a7..05d76a684d 100644
594167
--- a/src/cryptsetup/cryptsetup-tpm2.c
594167
+++ b/src/cryptsetup/cryptsetup-tpm2.c
594167
@@ -64,7 +64,7 @@ int acquire_tpm2_key(
594167
                 blob = loaded_blob;
594167
         }
594167
 
594167
-        return tpm2_unseal(device, pcr_mask, pcr_bank, primary_alg, blob, blob_size, policy_hash, policy_hash_size, ret_decrypted_key, ret_decrypted_key_size);
594167
+        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);
594167
 }
594167
 
594167
 int find_tpm2_auto_data(
594167
diff --git a/src/partition/repart.c b/src/partition/repart.c
594167
index 2f70796e58..adfec0b9f3 100644
594167
--- a/src/partition/repart.c
594167
+++ b/src/partition/repart.c
594167
@@ -2655,7 +2655,7 @@ static int partition_encrypt(
594167
                 uint16_t pcr_bank, primary_alg;
594167
                 int keyslot;
594167
 
594167
-                r = tpm2_seal(arg_tpm2_device, arg_tpm2_pcr_mask, &secret, &secret_size, &blob, &blob_size, &hash, &hash_size, &pcr_bank, &primary_alg);
594167
+                r = tpm2_seal(arg_tpm2_device, arg_tpm2_pcr_mask, NULL, &secret, &secret_size, &blob, &blob_size, &hash, &hash_size, &pcr_bank, &primary_alg);
594167
                 if (r < 0)
594167
                         return log_error_errno(r, "Failed to seal to TPM2: %m");
594167
 
594167
diff --git a/src/shared/creds-util.c b/src/shared/creds-util.c
594167
index 4d0681bc10..c4dcc396ac 100644
594167
--- a/src/shared/creds-util.c
594167
+++ b/src/shared/creds-util.c
594167
@@ -534,6 +534,7 @@ int encrypt_credential_and_warn(
594167
 
594167
                 r = tpm2_seal(tpm2_device,
594167
                               tpm2_pcr_mask,
594167
+                              NULL,
594167
                               &tpm2_key,
594167
                               &tpm2_key_size,
594167
                               &tpm2_blob,
594167
@@ -803,6 +804,7 @@ int decrypt_credential_and_warn(
594167
                                 le32toh(t->blob_size),
594167
                                 t->policy_hash_and_blob + le32toh(t->blob_size),
594167
                                 le32toh(t->policy_hash_size),
594167
+                                NULL,
594167
                                 &tpm2_key,
594167
                                 &tpm2_key_size);
594167
                 if (r < 0)
594167
diff --git a/src/shared/tpm2-util.c b/src/shared/tpm2-util.c
594167
index 70a2929432..aca7b69ab5 100644
594167
--- a/src/shared/tpm2-util.c
594167
+++ b/src/shared/tpm2-util.c
594167
@@ -14,6 +14,7 @@
594167
 #include "hexdecoct.h"
594167
 #include "memory-util.h"
594167
 #include "random-util.h"
594167
+#include "sha256.h"
594167
 #include "time-util.h"
594167
 
594167
 static void *libtss2_esys_dl = NULL;
594167
@@ -30,10 +31,12 @@ TSS2_RC (*sym_Esys_GetRandom)(ESYS_CONTEXT *esysContext, ESYS_TR shandle1, ESYS_
594167
 TSS2_RC (*sym_Esys_Initialize)(ESYS_CONTEXT **esys_context,  TSS2_TCTI_CONTEXT *tcti, TSS2_ABI_VERSION *abiVersion) = NULL;
594167
 TSS2_RC (*sym_Esys_Load)(ESYS_CONTEXT *esysContext, ESYS_TR parentHandle, ESYS_TR shandle1, ESYS_TR shandle2, ESYS_TR shandle3, const TPM2B_PRIVATE *inPrivate, const TPM2B_PUBLIC *inPublic, ESYS_TR *objectHandle) = NULL;
594167
 TSS2_RC (*sym_Esys_PCR_Read)(ESYS_CONTEXT *esysContext, ESYS_TR shandle1,ESYS_TR shandle2, ESYS_TR shandle3, const TPML_PCR_SELECTION *pcrSelectionIn, UINT32 *pcrUpdateCounter, TPML_PCR_SELECTION **pcrSelectionOut, TPML_DIGEST **pcrValues);
594167
+TSS2_RC (*sym_Esys_PolicyAuthValue)(ESYS_CONTEXT *esysContext, ESYS_TR policySession, ESYS_TR shandle1, ESYS_TR shandle2, ESYS_TR shandle3) = NULL;
594167
 TSS2_RC (*sym_Esys_PolicyGetDigest)(ESYS_CONTEXT *esysContext, ESYS_TR policySession, ESYS_TR shandle1, ESYS_TR shandle2, ESYS_TR shandle3, TPM2B_DIGEST **policyDigest) = NULL;
594167
 TSS2_RC (*sym_Esys_PolicyPCR)(ESYS_CONTEXT *esysContext, ESYS_TR policySession, ESYS_TR shandle1, ESYS_TR shandle2, ESYS_TR shandle3, const TPM2B_DIGEST *pcrDigest, const TPML_PCR_SELECTION *pcrs) = NULL;
594167
 TSS2_RC (*sym_Esys_StartAuthSession)(ESYS_CONTEXT *esysContext, ESYS_TR tpmKey, ESYS_TR bind, ESYS_TR shandle1, ESYS_TR shandle2, ESYS_TR shandle3, const TPM2B_NONCE *nonceCaller, TPM2_SE sessionType, const TPMT_SYM_DEF *symmetric, TPMI_ALG_HASH authHash, ESYS_TR *sessionHandle) = NULL;
594167
 TSS2_RC (*sym_Esys_Startup)(ESYS_CONTEXT *esysContext, TPM2_SU startupType) = NULL;
594167
+TSS2_RC (*sym_Esys_TR_SetAuth)(ESYS_CONTEXT *esysContext, ESYS_TR handle, TPM2B_AUTH const *authValue) = NULL;
594167
 TSS2_RC (*sym_Esys_Unseal)(ESYS_CONTEXT *esysContext, ESYS_TR itemHandle, ESYS_TR shandle1, ESYS_TR shandle2, ESYS_TR shandle3, TPM2B_SENSITIVE_DATA **outData) = NULL;
594167
 
594167
 const char* (*sym_Tss2_RC_Decode)(TSS2_RC rc) = NULL;
594167
@@ -58,10 +61,12 @@ int dlopen_tpm2(void) {
594167
                         DLSYM_ARG(Esys_Initialize),
594167
                         DLSYM_ARG(Esys_Load),
594167
                         DLSYM_ARG(Esys_PCR_Read),
594167
+                        DLSYM_ARG(Esys_PolicyAuthValue),
594167
                         DLSYM_ARG(Esys_PolicyGetDigest),
594167
                         DLSYM_ARG(Esys_PolicyPCR),
594167
                         DLSYM_ARG(Esys_StartAuthSession),
594167
                         DLSYM_ARG(Esys_Startup),
594167
+                        DLSYM_ARG(Esys_TR_SetAuth),
594167
                         DLSYM_ARG(Esys_Unseal));
594167
         if (r < 0)
594167
                 return r;
594167
@@ -594,6 +599,7 @@ static int tpm2_make_pcr_session(
594167
                 ESYS_CONTEXT *c,
594167
                 uint32_t pcr_mask,
594167
                 uint16_t pcr_bank, /* If UINT16_MAX, pick best bank automatically, otherwise specify bank explicitly. */
594167
+                bool use_pin,
594167
                 ESYS_TR *ret_session,
594167
                 TPM2B_DIGEST **ret_policy_digest,
594167
                 TPMI_ALG_HASH *ret_pcr_bank) {
594167
@@ -669,6 +675,21 @@ static int tpm2_make_pcr_session(
594167
                 goto finish;
594167
         }
594167
 
594167
+        if (use_pin) {
594167
+                rc = sym_Esys_PolicyAuthValue(
594167
+                                c,
594167
+                                session,
594167
+                                ESYS_TR_NONE,
594167
+                                ESYS_TR_NONE,
594167
+                                ESYS_TR_NONE);
594167
+                if (rc != TSS2_RC_SUCCESS) {
594167
+                        r = log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
594167
+                                            "Failed to add authValue policy to TPM: %s",
594167
+                                            sym_Tss2_RC_Decode(rc));
594167
+                        goto finish;
594167
+                }
594167
+        }
594167
+
594167
         if (DEBUG_LOGGING || ret_policy_digest) {
594167
                 log_debug("Acquiring policy digest.");
594167
 
594167
@@ -717,9 +738,22 @@ finish:
594167
         return r;
594167
 }
594167
 
594167
+static void hash_pin(const char *pin, size_t len, uint8_t ret_digest[static SHA256_DIGEST_SIZE]) {
594167
+        struct sha256_ctx hash;
594167
+
594167
+        assert(pin);
594167
+
594167
+        sha256_init_ctx(&hash);
594167
+        sha256_process_bytes(pin, len, &hash);
594167
+        sha256_finish_ctx(&hash, ret_digest);
594167
+
594167
+        explicit_bzero_safe(&hash, sizeof(hash));
594167
+}
594167
+
594167
 int tpm2_seal(
594167
                 const char *device,
594167
                 uint32_t pcr_mask,
594167
+                const char *pin,
594167
                 void **ret_secret,
594167
                 size_t *ret_secret_size,
594167
                 void **ret_blob,
594167
@@ -782,7 +816,8 @@ int tpm2_seal(
594167
         if (r < 0)
594167
                 return r;
594167
 
594167
-        r = tpm2_make_pcr_session(c.esys_context, pcr_mask, UINT16_MAX, NULL, &policy_digest, &pcr_bank);
594167
+        r = tpm2_make_pcr_session(c.esys_context, pcr_mask, UINT16_MAX, !!pin, NULL, &policy_digest,
594167
+                                  &pcr_bank);
594167
         if (r < 0)
594167
                 goto finish;
594167
 
594167
@@ -813,6 +848,10 @@ int tpm2_seal(
594167
                 .size = sizeof(hmac_sensitive.sensitive),
594167
                 .sensitive.data.size = 32,
594167
         };
594167
+        if (pin) {
594167
+                hash_pin(pin, strlen(pin), hmac_sensitive.sensitive.userAuth.buffer);
594167
+                hmac_sensitive.sensitive.userAuth.size = SHA256_DIGEST_SIZE;
594167
+        }
594167
         assert(sizeof(hmac_sensitive.sensitive.data.buffer) >= hmac_sensitive.sensitive.data.size);
594167
 
594167
         (void) tpm2_credit_random(c.esys_context);
594167
@@ -910,6 +949,7 @@ int tpm2_seal(
594167
         r = 0;
594167
 
594167
 finish:
594167
+        explicit_bzero_safe(&hmac_sensitive, sizeof(hmac_sensitive));
594167
         primary = flush_context_verbose(c.esys_context, primary);
594167
         return r;
594167
 }
594167
@@ -923,6 +963,7 @@ int tpm2_unseal(
594167
                 size_t blob_size,
594167
                 const void *known_policy_hash,
594167
                 size_t known_policy_hash_size,
594167
+                const char *pin,
594167
                 void **ret_secret,
594167
                 size_t *ret_secret_size) {
594167
 
594167
@@ -978,7 +1019,7 @@ int tpm2_unseal(
594167
         if (r < 0)
594167
                 return r;
594167
 
594167
-        r = tpm2_make_pcr_session(c.esys_context, pcr_mask, pcr_bank, &session, &policy_digest, NULL);
594167
+        r = tpm2_make_pcr_session(c.esys_context, pcr_mask, pcr_bank, !!pin, &session, &policy_digest, NULL);
594167
         if (r < 0)
594167
                 goto finish;
594167
 
594167
@@ -1005,11 +1046,38 @@ int tpm2_unseal(
594167
                         &public,
594167
                         &hmac_key);
594167
         if (rc != TSS2_RC_SUCCESS) {
594167
-                r = log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
594167
-                                    "Failed to load HMAC key in TPM: %s", sym_Tss2_RC_Decode(rc));
594167
+                /* If we're in dictionary attack lockout mode, we should see a lockout error here, which we
594167
+                 * need to translate for the caller. */
594167
+                if (rc == TPM2_RC_LOCKOUT)
594167
+                        r = log_error_errno(
594167
+                                        SYNTHETIC_ERRNO(ENOLCK),
594167
+                                        "TPM2 device is in dictionary attack lockout mode.");
594167
+                else
594167
+                        r = log_error_errno(
594167
+                                        SYNTHETIC_ERRNO(ENOTRECOVERABLE),
594167
+                                        "Failed to load HMAC key in TPM: %s",
594167
+                                        sym_Tss2_RC_Decode(rc));
594167
                 goto finish;
594167
         }
594167
 
594167
+        if (pin) {
594167
+                TPM2B_AUTH auth = {
594167
+                        .size = SHA256_DIGEST_SIZE
594167
+                };
594167
+
594167
+                hash_pin(pin, strlen(pin), auth.buffer);
594167
+
594167
+                rc = sym_Esys_TR_SetAuth(c.esys_context, hmac_key, &auth);
594167
+                explicit_bzero_safe(&auth, sizeof(auth));
594167
+                if (rc != TSS2_RC_SUCCESS) {
594167
+                        r = log_error_errno(
594167
+                                        SYNTHETIC_ERRNO(ENOTRECOVERABLE),
594167
+                                        "Failed to load PIN in TPM: %s",
594167
+                                        sym_Tss2_RC_Decode(rc));
594167
+                        goto finish;
594167
+                }
594167
+        }
594167
+
594167
         log_debug("Unsealing HMAC key.");
594167
 
594167
         rc = sym_Esys_Unseal(
594167
diff --git a/src/shared/tpm2-util.h b/src/shared/tpm2-util.h
594167
index cb57a847e2..784e9fd11e 100644
594167
--- a/src/shared/tpm2-util.h
594167
+++ b/src/shared/tpm2-util.h
594167
@@ -1,6 +1,8 @@
594167
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
594167
 #pragma once
594167
 
594167
+#include <stdbool.h>
594167
+
594167
 #include "json.h"
594167
 #include "macro.h"
594167
 
594167
@@ -20,10 +22,12 @@ extern TSS2_RC (*sym_Esys_GetRandom)(ESYS_CONTEXT *esysContext, ESYS_TR shandle1
594167
 extern TSS2_RC (*sym_Esys_Initialize)(ESYS_CONTEXT **esys_context,  TSS2_TCTI_CONTEXT *tcti, TSS2_ABI_VERSION *abiVersion);
594167
 extern TSS2_RC (*sym_Esys_Load)(ESYS_CONTEXT *esysContext, ESYS_TR parentHandle, ESYS_TR shandle1, ESYS_TR shandle2, ESYS_TR shandle3, const TPM2B_PRIVATE *inPrivate, const TPM2B_PUBLIC *inPublic, ESYS_TR *objectHandle);
594167
 extern TSS2_RC (*sym_Esys_PCR_Read)(ESYS_CONTEXT *esysContext, ESYS_TR shandle1,ESYS_TR shandle2, ESYS_TR shandle3, const TPML_PCR_SELECTION *pcrSelectionIn, UINT32 *pcrUpdateCounter, TPML_PCR_SELECTION **pcrSelectionOut, TPML_DIGEST **pcrValues);
594167
+extern TSS2_RC (*sym_Esys_PolicyAuthValue)(ESYS_CONTEXT *esysContext, ESYS_TR policySession, ESYS_TR shandle1, ESYS_TR shandle2, ESYS_TR shandle3);
594167
 extern TSS2_RC (*sym_Esys_PolicyGetDigest)(ESYS_CONTEXT *esysContext, ESYS_TR policySession, ESYS_TR shandle1, ESYS_TR shandle2, ESYS_TR shandle3, TPM2B_DIGEST **policyDigest);
594167
 extern TSS2_RC (*sym_Esys_PolicyPCR)(ESYS_CONTEXT *esysContext, ESYS_TR policySession, ESYS_TR shandle1, ESYS_TR shandle2, ESYS_TR shandle3, const TPM2B_DIGEST *pcrDigest, const TPML_PCR_SELECTION *pcrs);
594167
 extern TSS2_RC (*sym_Esys_StartAuthSession)(ESYS_CONTEXT *esysContext, ESYS_TR tpmKey, ESYS_TR bind, ESYS_TR shandle1, ESYS_TR shandle2, ESYS_TR shandle3, const TPM2B_NONCE *nonceCaller, TPM2_SE sessionType, const TPMT_SYM_DEF *symmetric, TPMI_ALG_HASH authHash, ESYS_TR *sessionHandle);
594167
 extern TSS2_RC (*sym_Esys_Startup)(ESYS_CONTEXT *esysContext, TPM2_SU startupType);
594167
+extern TSS2_RC (*sym_Esys_TR_SetAuth)(ESYS_CONTEXT *esysContext, ESYS_TR handle, TPM2B_AUTH const *authValue);
594167
 extern TSS2_RC (*sym_Esys_Unseal)(ESYS_CONTEXT *esysContext, ESYS_TR itemHandle, ESYS_TR shandle1, ESYS_TR shandle2, ESYS_TR shandle3, TPM2B_SENSITIVE_DATA **outData);
594167
 
594167
 extern const char* (*sym_Tss2_RC_Decode)(TSS2_RC rc);
594167
@@ -35,8 +39,8 @@ extern TSS2_RC (*sym_Tss2_MU_TPM2B_PUBLIC_Unmarshal)(uint8_t const buffer[], siz
594167
 
594167
 int dlopen_tpm2(void);
594167
 
594167
-int tpm2_seal(const char *device, uint32_t pcr_mask, void **ret_secret, size_t *ret_secret_size, void **ret_blob, size_t *ret_blob_size, void **ret_pcr_hash, size_t *ret_pcr_hash_size, uint16_t *ret_pcr_bank, uint16_t *ret_primary_alg);
594167
-int tpm2_unseal(const char *device, uint32_t pcr_mask, uint16_t pcr_bank, uint16_t primary_alg, const void *blob, size_t blob_size, const void *pcr_hash, size_t pcr_hash_size, void **ret_secret, size_t *ret_secret_size);
594167
+int tpm2_seal(const char *device, uint32_t pcr_mask, const char *pin, void **ret_secret, size_t *ret_secret_size, void **ret_blob, size_t *ret_blob_size, void **ret_pcr_hash, size_t *ret_pcr_hash_size, uint16_t *ret_pcr_bank, uint16_t *ret_primary_alg);
594167
+int tpm2_unseal(const char *device, uint32_t pcr_mask, uint16_t pcr_bank, uint16_t primary_alg, const void *blob, size_t blob_size, const void *pcr_hash, size_t pcr_hash_size, const char *pin, void **ret_secret, size_t *ret_secret_size);
594167
 
594167
 #endif
594167