grub-devel.gnu.org archive mirror
 help / color / mirror / Atom feed
From: Gary Lin via Grub-devel <grub-devel@gnu.org>
To: The development of GNU GRUB <grub-devel@gnu.org>
Cc: Gary Lin <glin@suse.com>,
	Hernan Gatta <hegatta@linux.microsoft.com>,
	Daniel Axtens <dja@axtens.net>,
	Daniel Kiper <daniel.kiper@oracle.com>,
	shkhisti@microsoft.com, jaskaran.khurana@microsoft.com,
	christopher.co@microsoft.com, daniel.mihai@microsoft.com,
	jaredz@redhat.com, development@efficientek.com,
	jejb@linux.ibm.com, mchang@suse.com, patrick.colp@oracle.com,
	Stefan Berger <stefanb@linux.ibm.com>
Subject: [PATCH v16 15/20] tpm2: Implement NV index
Date: Wed, 15 May 2024 13:07:07 +0800	[thread overview]
Message-ID: <20240515050712.21495-16-glin@suse.com> (raw)
In-Reply-To: <20240515050712.21495-1-glin@suse.com>

From: Patrick Colp <patrick.colp@oracle.com>

Currently with the TPM2 protector, only SRK mode is supported and
NV index support is just a stub. Implement the NV index option.

Note: This only extends support on the unseal path. grub2_protect
has not been updated. tpm2-tools can be used to insert a key into
the NV index.

An example of inserting a key using tpm2-tools:

  # Get random key.
  tpm2_getrandom 32 > key.dat

  # Create primary object.
  tpm2_createprimary -C o -g sha256 -G ecc -c primary.ctx

  # Create policy object. `pcrs.dat` contains the PCR values to seal against.
  tpm2_startauthsession -S session.dat
  tpm2_policypcr -S session.dat -l sha256:7,11 -f pcrs.dat -L policy.dat
  tpm2_flushcontext session.dat

  # Seal key into TPM.
  cat key.dat | tpm2_create -C primary.ctx -u key.pub -r key.priv -L policy.dat -i-
  tpm2_load -C primary.ctx -u key.pub -r key.priv -n sealing.name -c sealing.ctx
  tpm2_evictcontrol -C o -c sealing.ctx 0x81000000

Then to unseal the key in grub, add this to grub.cfg:

  tpm2_key_protector_init --mode=nv --nvindex=0x81000000 --pcrs=7,11
  cryptomount -u <UUID> --protector tpm2

Signed-off-by: Patrick Colp <patrick.colp@oracle.com>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
---
 grub-core/tpm2/module.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/grub-core/tpm2/module.c b/grub-core/tpm2/module.c
index e83b02865..b754b38df 100644
--- a/grub-core/tpm2/module.c
+++ b/grub-core/tpm2/module.c
@@ -1035,12 +1035,27 @@ static grub_err_t
 grub_tpm2_protector_nv_recover (const struct grub_tpm2_protector_context *ctx,
 				grub_uint8_t **key, grub_size_t *key_size)
 {
-  (void)ctx;
-  (void)key;
-  (void)key_size;
+  TPM_HANDLE sealed_handle = ctx->nv;
+  tpm2key_policy_t policy_seq = NULL;
+  grub_err_t err;
+
+  /* Create a basic policy sequence based on the given PCR selection */
+  err = grub_tpm2_protector_simple_policy_seq (ctx, &policy_seq);
+  if (err != GRUB_ERR_NONE)
+    goto exit;
+
+  err = grub_tpm2_protector_unseal (policy_seq, sealed_handle, key, key_size);
+
+  /* Pop error messages on success */
+  if (err == GRUB_ERR_NONE)
+    while (grub_error_pop ());
+
+exit:
+  TPM2_FlushContext (sealed_handle);
 
-  return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
-		     N_("NV Index mode is not implemented yet"));
+  grub_tpm2key_free_policy_seq (policy_seq);
+
+  return err;
 }
 
 static grub_err_t
-- 
2.35.3


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

  parent reply	other threads:[~2024-05-15  5:08 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-15  5:06 [PATCH v16 00/20] Automatic Disk Unlock with TPM2 Gary Lin via Grub-devel
2024-05-15  5:06 ` [PATCH v16 01/20] posix_wrap: tweaks in preparation for libtasn1 Gary Lin via Grub-devel
2024-06-05 14:45   ` Daniel Kiper via Grub-devel
2024-06-07  5:46     ` Gary Lin via Grub-devel
2024-05-15  5:06 ` [PATCH v16 02/20] libtasn1: import libtasn1-4.19.0 Gary Lin via Grub-devel
2024-06-05 15:04   ` Daniel Kiper via Grub-devel
2024-06-07  5:57     ` Gary Lin via Grub-devel
2024-05-15  5:06 ` [PATCH v16 03/20] libtasn1: disable code not needed in grub Gary Lin via Grub-devel
2024-06-05 15:18   ` Daniel Kiper via Grub-devel
2024-06-07  3:07     ` Gary Lin via Grub-devel
2024-06-07 14:14       ` Daniel Kiper via Grub-devel
2024-06-11  7:10         ` Gary Lin via Grub-devel
2024-05-15  5:06 ` [PATCH v16 04/20] libtasn1: changes for grub compatibility Gary Lin via Grub-devel
2024-05-15  5:06 ` [PATCH v16 05/20] libtasn1: fix the potential buffer overrun Gary Lin via Grub-devel
2024-05-15  5:06 ` [PATCH v16 06/20] libtasn1: compile into asn1 module Gary Lin via Grub-devel
2024-05-15  5:06 ` [PATCH v16 07/20] asn1_test: test module for libtasn1 Gary Lin via Grub-devel
2024-05-15  5:07 ` [PATCH v16 08/20] libtasn1: Add the documentation Gary Lin via Grub-devel
2024-05-15  5:07 ` [PATCH v16 09/20] key_protector: Add key protectors framework Gary Lin via Grub-devel
2024-05-15  5:07 ` [PATCH v16 10/20] tpm2: Add TPM Software Stack (TSS) Gary Lin via Grub-devel
2024-05-15  5:07 ` [PATCH v16 11/20] key_protector: Add TPM2 Key Protector Gary Lin via Grub-devel
2024-05-15  5:07 ` [PATCH v16 12/20] cryptodisk: Support key protectors Gary Lin via Grub-devel
2024-05-15  5:07 ` [PATCH v16 13/20] util/grub-protect: Add new tool Gary Lin via Grub-devel
2024-05-15  5:07 ` [PATCH v16 14/20] tpm2: Support authorized policy Gary Lin via Grub-devel
2024-05-15  5:07 ` Gary Lin via Grub-devel [this message]
2024-05-15  5:07 ` [PATCH v16 16/20] cryptodisk: Fallback to passphrase Gary Lin via Grub-devel
2024-05-15  5:07 ` [PATCH v16 17/20] cryptodisk: wipe out the cached keys from protectors Gary Lin via Grub-devel
2024-05-15  5:07 ` [PATCH v16 18/20] diskfilter: look up cryptodisk devices first Gary Lin via Grub-devel
2024-05-15  5:07 ` [PATCH v16 19/20] tpm2: Enable tpm2 module for grub-emu Gary Lin via Grub-devel
2024-05-15  5:07 ` [PATCH v16 20/20] tests: Add tpm2_test Gary Lin via Grub-devel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240515050712.21495-16-glin@suse.com \
    --to=grub-devel@gnu.org \
    --cc=christopher.co@microsoft.com \
    --cc=daniel.kiper@oracle.com \
    --cc=daniel.mihai@microsoft.com \
    --cc=development@efficientek.com \
    --cc=dja@axtens.net \
    --cc=glin@suse.com \
    --cc=hegatta@linux.microsoft.com \
    --cc=jaredz@redhat.com \
    --cc=jaskaran.khurana@microsoft.com \
    --cc=jejb@linux.ibm.com \
    --cc=mchang@suse.com \
    --cc=patrick.colp@oracle.com \
    --cc=shkhisti@microsoft.com \
    --cc=stefanb@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).