Skip to contentSkip to conversation

KEYS: trusted: asymmetric keys

PATCH In review Jarkko Sakkinen 2 participants 23 messages Nov 28

Subsystems (12)
lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

v6 cover KEYS: trusted: asymmetric keys

OVERVIEW
========

Extend TPM 2.0 trusted keys to load ASN.1 TPMKey [1] asymmetric keys (OID_TPMLoadableKey, 2.23.133.10.1.3) for RSA (2048/3072/4096) and ECC (NIST P-256/P-384/P-521), and expose keyctl pkey-operations directly on key_type_trusted. In addition, extend TPMKey support to derive a transient TCG H-2 NIST P-256 primary key on the fly for 0x40 permanent hierarchy handles (such as TPM2_RH_OWNER).

This allows provisioning an asymmetric key whose private portion stays sealed to the TPM rather than sitting in plaintext on disk or in kernel memory, while letting privileged setup load the blob into a keyring and grant signing or decryption access via KEYCTL_PKEY_* without exposing /dev/tpm0, /dev/tpmrm0, or a userspace PKCS#11 stack. For example, a UEFI Secure Boot Machine Owner Key (MOK) used for local module signing can have its X.509 certificate enrolled into MokList while its private key is wrapped into an OID_TPMLoadableKey blob and shredded from disk; network daemons such as iwd can similarly authenticate with TPM-bound client keys.

Earlier iterations ([2], [3]) added a separate subtype under key_type_asymmetric. Instead, factor public-key keyctl dispatch behind CONFIG_KEYCTL_PKEY and wire optional asymmetric callbacks onto key_type_trusted, reusing trusted-key backend selection, authorization options, and TPM2 HMAC bus sessions without enabling software asymmetric private-key operations (CONFIG_ASYMMETRIC_KEY_PKEY). Prerequisite tpm2-sessions and tpm-buf cleanups from [4] are folded in.

[1] https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
[2] https://lore.kernel.org/linux-integrity/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/linux-integrity/[email protected]/

CHANGES
=======

v6:
- docs: Move asymmetric trusted-key documentation into modular kernel-doc sections and struct/callback comments in include/keys/trusted-type.h, include/keys/trusted_tpm.h, trusted_core.c, and trusted_tpm2.c, including a UEFI Secure Boot MOK provisioning and signing example, and include them from Documentation/security/keys/trusted-encrypted.rst.
- Describe motivation and add base-commit metadata.

v5:
- tpm / trusted: Fold in the tpm2-sessions and fallible tpm_buf_read*() patches [4] (adding Jonathan McDowell's Reviewed-by to "tpm2-sessions:
Drop TPM2_NAME_SIZE"), resolve handle names with tpm2_read_public(), and replace the custom tpm2_read_*() / tpm2_skip_bytes() helpers with tpm_buf_read_u{16,32}() in tpm2_asym_parse_*_public().
- trusted: Split "KEYS: trusted: Add asymmetric key support for TPM2" into a framework patch ("KEYS: trusted: Add asymmetric key operations", adding optional asym_* callbacks on struct trusted_key_ops and HAVE_TRUSTED_KEYS_ASYMMETRIC) and the TPM2 backend patch.
- trusted: Move "KEYS: trusted: Validate public_len before reading key attributes" to patch 01/18 and add the Fixes tag for e5fb5d2c5a03.

v4:
- KEYS: Turn CONFIG_KEYCTL_PKEY into a hidden helper symbol selected per
subsystem: add off-by-default CONFIG_ASYMMETRIC_KEY_PKEY for software asymmetric keys, and switch TURRIS_SIGNING_KEY, TURRIS_OMNIA_MCU_KEYCTL, and TURRIS_MOX_RWTM_KEYCTL from CONFIG_ASYMMETRIC_KEY_TYPE to CONFIG_KEYCTL_PKEY (remaining default y).
- trusted: Default CONFIG_TRUSTED_KEYS_ASYMMETRIC to y and select CONFIG_KEYCTL_PKEY.
- trusted: Split "KEYS: trusted: Factor out TPM2 blob loading" into separate patches for public_len bounds checking, the private backend pointer in trusted_key_payload, and TPM2 blob loading.

v3:
- KEYS: Move CONFIG_KEYCTL_PKEY factoring to the beginning of the series and make it an off-by-default prompt instead of selecting it from CONFIG_ASYMMETRIC_KEY_TYPE.
- trusted: Make CONFIG_TRUSTED_KEYS_ASYMMETRIC depend on CONFIG_KEYCTL_PKEY instead of selecting it.
- trusted: Use unsigned int instead of u16 for data_len and min_len in tpm2_unseal_cmd().
- asn1: The reason for not supporting implicit tags was that it follows what other functions do in asn1_encoder.c, and is that way coherent.

v2:
- lib/asn1_encoder: Remove unreachable !bytes_len branch in asn1_encode_integer_bytes().
- tpm: Bounds-check ECC coordinates, defer state updates until after validation, and verify subsequent TPM2_RH_NULL primaries against chip->null_key_name in tpm2_parse_create_primary().
- trusted: Reject trailing or truncated TPM2B bytes in tpm2_key_decode() so tpm2_load_cmd() and tpm2_asym_load() pass the same blob length.
- trusted: Avoid 32-bit param_size wraparound in tpm2_asym_rsa_decrypt() and tpm2_asym_parse_signature().

VERSIONS
========

v5: https://lore.kernel.org/keyrings/[email protected]/
v4: https://lore.kernel.org/keyrings/[email protected]/
v3: https://lore.kernel.org/keyrings/[email protected]/
v2: https://lore.kernel.org/keyrings/[email protected]/
v1: https://lore.kernel.org/keyrings/[email protected]/

Cc Ahmad Fatoum <[email protected]>
Cc Alasdair Kergon <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Benjamin Marzinski <[email protected]>
Cc David Gstir <[email protected]>
Cc David Howells <[email protected]>
Cc David Laight <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Eric Biggers <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Ignat Korchagin <[email protected]>
Cc James Bottomley <[email protected]>
Cc James Morris <[email protected]>
Cc James Prestwood <[email protected]>
Cc Jarkko Sakkinen <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Jonathan McDowell <[email protected]>
Cc Lukas Wunner <[email protected]>
Cc Marek Behún <[email protected]>
Cc Mike Snitzer <[email protected]>
Cc Mikulas Patocka <[email protected]>
Cc Mimi Zohar <[email protected]>
Cc Paul Moore <[email protected]>
Cc Peter Huewe <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Sumit Garg <[email protected]>

Jarkko Sakkinen (18):
KEYS: trusted: Validate public_len before reading key attributes
KEYS: Factor out KEYCTL_PKEY
tpm2-sessions: Drop TPM2_NAME_SIZE
KEYS: trusted: Open code tpm2_buf_append_auth()
KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
tpm2-sessions: Re-orchestrate tpm2_read_public() use
tpm-buf: Make tpm_buf_read*() fallible
KEYS: trusted: Allow ASN.1 TPM2 sealed data shorter than MIN_KEY_SIZE lib/asn1_encoder: Add asn1_encode_integer_bytes()
tpm: Export tpm2_create_primary()
tpm: Add persistent hierarchies to tpm2_create_primary()
KEYS: trusted: Extend TPMKey support
KEYS: trusted: Reject zero-length keys in symmetric consumers
KEYS: trusted: Add private backend data to trusted_key_payload
KEYS: trusted: Factor out TPM2 blob loading
KEYS: trusted: Add asymmetric key operations
KEYS: trusted: Add asymmetric key support for TPM2
docs: trusted-encrypted: Document asymmetric trusted keys

 .../security/keys/trusted-encrypted.rst       |   77 +-
 crypto/af_alg.c                               |    2 +
 crypto/asymmetric_keys/Kconfig                |   11 +
 crypto/asymmetric_keys/asymmetric_keys.h      |    2 +
 crypto/asymmetric_keys/asymmetric_type.c      |    4 +
 drivers/char/tpm/Kconfig                      |    2 +-
 drivers/char/tpm/tpm-buf.c                    |   56 +-
 drivers/char/tpm/tpm-sysfs.c                  |    2 +-
 drivers/char/tpm/tpm2-cmd.c                   |    9 +-
 drivers/char/tpm/tpm2-sessions.c              |  523 +++---
 drivers/firmware/Kconfig                      |    1 -
 drivers/md/dm-crypt.c                         |    2 +-
 drivers/platform/cznic/Kconfig                |    3 +-
 include/keys/trusted-type.h                   |   67 +-
 include/keys/trusted_tpm.h                    |   45 +-
 include/linux/asn1_encoder.h                  |    3 +
 include/linux/tpm.h                           |   20 +-
 include/linux/tpm_buf.h                       |    6 +-
 include/linux/tpm_command.h                   |   13 +
 lib/asn1_encoder.c                            |   56 +
 security/keys/Kconfig                         |    3 +
 security/keys/Makefile                        |    2 +-
 .../keys/encrypted-keys/masterkey_trusted.c   |    5 +
 security/keys/internal.h                      |    2 +-
 security/keys/keyctl.c                        |    2 +-
 security/keys/trusted-keys/Kconfig            |   15 +
 security/keys/trusted-keys/tpm2key.asn1       |   19 +
 security/keys/trusted-keys/trusted_core.c     |  141 +-
 security/keys/trusted-keys/trusted_tpm1.c     |   24 +
 security/keys/trusted-keys/trusted_tpm2.c     | 1467 +++++++++++++++--
 30 files changed, 2159 insertions(+), 425 deletions(-)

base-commit: 822ba3a89c7896b26e4beb70e2ebffb881924dfe
prerequisite-patch-id: 6b9f0c26cb5c2290d5a2a78c18ccf225b09a6bbb
prerequisite-patch-id: 0f71199bbbbaf9fe1fa82afb1926b452c493532e
prerequisite-patch-id: 97be79eb77addef7282d5cb9966c4208cf0e965e
prerequisite-patch-id: 4517a290b914c1a79717aece49041479eca4b399
prerequisite-patch-id: 0b49963321abe30437c8e82ffd64fa235c9746bd --
2.47.3

author maintainer
#1 lore raw

PATCH v6 1/18 KEYS: trusted: Validate public_len before reading key attributes

tpm2_load_cmd() reads the 32-bit objectAttributes field at pub + 4 without checking that public_len is at least 8 bytes. Reject blobs with public_len < 8 before dereferencing pub + 4.

Fixes e5fb5d2c5a03 ("security: keys: trusted: Make sealed key properly interoperable")
---
 security/keys/trusted-keys/trusted_tpm2.c | 3 +++
 1 file changed, 3 insertions(+)
security/keys/trusted-keys/trusted_tpm2.c +3 -0
index 30188a0ec676..621f8ffba17e 100644
@@ -425,6 +425,9 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	if (private_len + 2 + public_len + 2 > blob_len)
 		return -E2BIG;
 
+	if (public_len < 8)
+		return -EINVAL;
+
 	pub = blob + 2 + private_len + 2;
 	/* key attributes are always at offset 4 */
 	attrs = get_unaligned_be32(pub + 4);
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH v6 2/18 KEYS: Factor out KEYCTL_PKEY

In reply to Jarkko Sakkinen

Factor out CONFIG_KEYCTL_PKEY from CONFIG_ASYMMETRIC_KEY_TYPE as a hidden helper symbol and make keyctl public-key support a per-subsystem choice:

- Add off-by-default CONFIG_ASYMMETRIC_KEY_PKEY to gate keyctl public-key operations on software asymmetric keys so systems that only need CONFIG_ASYMMETRIC_KEY_TYPE for in-kernel signature verification do not expose KEYCTL_PKEY_* operations.
- Switch TURRIS_SIGNING_KEY, TURRIS_OMNIA_MCU_KEYCTL, and TURRIS_MOX_RWTM_KEYCTL from CONFIG_ASYMMETRIC_KEY_TYPE to CONFIG_KEYCTL_PKEY while keeping them enabled by default.

---
 crypto/asymmetric_keys/Kconfig           | 11 +++++++++++
 crypto/asymmetric_keys/asymmetric_keys.h |  2 ++
 crypto/asymmetric_keys/asymmetric_type.c |  4 ++++
 drivers/firmware/Kconfig                 |  1 -
 drivers/platform/cznic/Kconfig           |  3 +--
 security/keys/Kconfig                    |  3 +++
 security/keys/Makefile                   |  2 +-
 security/keys/internal.h                 |  2 +-
 security/keys/keyctl.c                   |  2 +-
 9 files changed, 24 insertions(+), 6 deletions(-)
crypto/asymmetric_keys/Kconfig +11 -0
index c73979b2e98e..a1fa64a38f24 100644
@@ -10,6 +10,17 @@ menuconfig ASYMMETRIC_KEY_TYPE
 
 if ASYMMETRIC_KEY_TYPE
 
+config ASYMMETRIC_KEY_PKEY
+	bool "Asymmetric key keyctl public-key operations"
+	select KEYCTL_PKEY
+	help
+	  This option exposes keyctl public-key operations (query, encrypt,
+	  decrypt, sign, and verify) on asymmetric keys to userspace.
+	  In-kernel signature verification (such as module and kexec
+	  verification) does not require this option.
+
+	  If you are unsure as to whether this is required, answer N.
+
 config ASYMMETRIC_PUBLIC_KEY_SUBTYPE
 	tristate "Asymmetric public-key crypto algorithm subtype"
 	select MPILIB
crypto/asymmetric_keys/asymmetric_keys.h +2 -0
index dc854cb737fb..7b669f41f297 100644
@@ -13,5 +13,7 @@ extern int __asymmetric_key_hex_to_key_id(const char *id,
 					  struct asymmetric_key_id *match_id,
 					  size_t hexlen);
 
+#ifdef CONFIG_ASYMMETRIC_KEY_PKEY
 extern int asymmetric_key_eds_op(struct kernel_pkey_params *params,
 				 const void *in, void *out);
+#endif
crypto/asymmetric_keys/asymmetric_type.c +4 -0
index 16a7ae16593c..8b8a75b70527 100644
@@ -565,6 +565,7 @@ static struct key_restriction *asymmetric_lookup_restriction(
 	return ret;
 }
 
+#ifdef CONFIG_ASYMMETRIC_KEY_PKEY
 int asymmetric_key_eds_op(struct kernel_pkey_params *params,
 			  const void *in, void *out)
 {
@@ -603,6 +604,7 @@ static int asymmetric_key_verify_signature(struct kernel_pkey_params *params,
 
 	return verify_signature(params->key, &sig);
 }
+#endif
 
 struct key_type key_type_asymmetric = {
 	.name			= "asymmetric",
@@ -614,9 +616,11 @@ struct key_type key_type_asymmetric = {
 	.destroy		= asymmetric_key_destroy,
 	.describe		= asymmetric_key_describe,
 	.lookup_restriction	= asymmetric_lookup_restriction,
+#ifdef CONFIG_ASYMMETRIC_KEY_PKEY
 	.asym_query		= query_asymmetric_key,
 	.asym_eds_op		= asymmetric_key_eds_op,
 	.asym_verify_signature	= asymmetric_key_verify_signature,
+#endif
 };
 EXPORT_SYMBOL_GPL(key_type_asymmetric);
 
drivers/firmware/Kconfig +0 -1
index 43e2e2a64594..9f382ba10fc9 100644
@@ -288,7 +288,6 @@ config TURRIS_MOX_RWTM_KEYCTL
 	bool "Turris Mox rWTM ECDSA message signing"
 	default y
 	depends on KEYS
-	depends on ASYMMETRIC_KEY_TYPE
 	select CZNIC_PLATFORMS
 	select TURRIS_SIGNING_KEY
 	help
drivers/platform/cznic/Kconfig +1 -2
index 61cff5f7e02e..850319fb8c67 100644
@@ -80,7 +80,6 @@ config TURRIS_OMNIA_MCU_KEYCTL
 	bool "Turris Omnia MCU ECDSA message signing"
 	default y
 	depends on KEYS
-	depends on ASYMMETRIC_KEY_TYPE
 	depends on TURRIS_OMNIA_MCU_GPIO
 	select TURRIS_SIGNING_KEY
 	help
@@ -93,6 +92,6 @@ endif # TURRIS_OMNIA_MCU
 config TURRIS_SIGNING_KEY
 	tristate
 	depends on KEYS
-	depends on ASYMMETRIC_KEY_TYPE
+	select KEYCTL_PKEY
 
 endif # CZNIC_PLATFORMS
security/keys/Kconfig +3 -0
index f4510d8cb485..e092a91abba3 100644
@@ -23,6 +23,9 @@ menuconfig KEYS
 
 if KEYS
 
+config KEYCTL_PKEY
+	bool
+
 config KEYS_REQUEST_CACHE
 	bool "Enable temporary caching of the last request_key() result"
 	help
security/keys/Makefile +1 -1
index 5f40807f05b3..aafd28f7ee2f 100644
@@ -22,7 +22,7 @@ obj-$(CONFIG_PROC_FS) += proc.o
 obj-$(CONFIG_SYSCTL) += sysctl.o
 obj-$(CONFIG_PERSISTENT_KEYRINGS) += persistent.o
 obj-$(CONFIG_KEY_DH_OPERATIONS) += dh.o
-obj-$(CONFIG_ASYMMETRIC_KEY_TYPE) += keyctl_pkey.o
+obj-$(CONFIG_KEYCTL_PKEY) += keyctl_pkey.o
 
 #
 # Key types
security/keys/internal.h +1 -1
index 73d71de274c2..5230c1239d03 100644
@@ -310,7 +310,7 @@ static inline long compat_keyctl_dh_compute(
 #endif
 #endif
 
-#ifdef CONFIG_ASYMMETRIC_KEY_TYPE
+#ifdef CONFIG_KEYCTL_PKEY
 extern long keyctl_pkey_query(key_serial_t,
 			      const char __user *,
 			      struct keyctl_pkey_query __user *);
security/keys/keyctl.c +1 -1
index b11716f88ded..95a89a21f77f 100644
@@ -28,7 +28,7 @@ static const unsigned char keyrings_capabilities[2] = {
 	[0] = (KEYCTL_CAPS0_CAPABILITIES |
 	       (IS_ENABLED(CONFIG_PERSISTENT_KEYRINGS)	? KEYCTL_CAPS0_PERSISTENT_KEYRINGS : 0) |
 	       (IS_ENABLED(CONFIG_KEY_DH_OPERATIONS)	? KEYCTL_CAPS0_DIFFIE_HELLMAN : 0) |
-	       (IS_ENABLED(CONFIG_ASYMMETRIC_KEY_TYPE)	? KEYCTL_CAPS0_PUBLIC_KEY : 0) |
+	       (IS_ENABLED(CONFIG_KEYCTL_PKEY)		? KEYCTL_CAPS0_PUBLIC_KEY : 0) |
 	       (IS_ENABLED(CONFIG_BIG_KEYS)		? KEYCTL_CAPS0_BIG_KEY : 0) |
 	       KEYCTL_CAPS0_INVALIDATE |
 	       KEYCTL_CAPS0_RESTRICT_KEYRING |
Signature · 1 line
2.47.3
author maintainer
#3 lore raw

PATCH v6 3/18 tpm2-sessions: Drop TPM2_NAME_SIZE

In reply to Jarkko Sakkinen

TPM2_NAME_SIZE was left to include/linux/tpm.h when the base definitions were consolidated. Switch struct tpm2_auth to TPM2_MAX_NAME_SIZE and convert TPM2_NAME_SIZE users to TPM2_NULL_NAME_SIZE, removing the redundant TPM2_NAME_SIZE definition.

Fixes 88c5a62c3af7 ("tpm: Move TPM common base definitions to the command header")
---
 drivers/char/tpm/tpm-sysfs.c     | 2 +-
 drivers/char/tpm/tpm2-sessions.c | 2 +-
 include/linux/tpm.h              | 8 +-------
 3 files changed, 3 insertions(+), 9 deletions(-)
drivers/char/tpm/tpm-sysfs.c +1 -1
index d915307ae833..0f726cf65189 100644
@@ -318,7 +318,7 @@ static ssize_t null_name_show(struct device *dev, struct device_attribute *attr,
 			      char *buf)
 {
 	struct tpm_chip *chip = to_tpm_chip(dev);
-	int size = TPM2_NAME_SIZE;
+	int size = TPM2_NULL_NAME_SIZE;
 
 	bin2hex(buf, chip->null_key_name, size);
 	size *= 2;
drivers/char/tpm/tpm2-sessions.c +1 -1
index 2aaaad69760d..7afcfaaf84d9 100644
@@ -137,7 +137,7 @@ struct tpm2_auth {
 	 * we must compute and remember
 	 */
 	u32 name_h[AUTH_MAX_NAMES];
-	u8 name[AUTH_MAX_NAMES][2 + SHA512_DIGEST_SIZE];
+	u8 name[AUTH_MAX_NAMES][TPM2_MAX_NAME_SIZE];
 };
 
 #ifdef CONFIG_TCG_TPM2_HMAC
include/linux/tpm.h +1 -7
index 0db277af45c3..84c8fe226235 100644
@@ -94,12 +94,6 @@ struct tpm_chip_seqops {
 /* Fixed define for the curve we use which is NIST_P256 */
 #define EC_PT_SZ	32
 
-/*
- * fixed define for the size of a name.  This is actually HASHALG size
- * plus 2, so 32 for SHA256
- */
-#define TPM2_NAME_SIZE	34
-
 /*
  * The maximum size for an object context
  */
@@ -166,7 +160,7 @@ struct tpm_chip {
 	/* saved context for NULL seed */
 	u8 null_key_context[TPM2_MAX_CONTEXT_SIZE];
 	 /* name of NULL seed */
-	u8 null_key_name[TPM2_NAME_SIZE];
+	u8 null_key_name[TPM2_NULL_NAME_SIZE];
 	u8 null_ec_key_x[EC_PT_SZ];
 	u8 null_ec_key_y[EC_PT_SZ];
 	struct tpm2_auth *auth;
Signature · 1 line
2.47.3
author maintainer
#4 lore raw

PATCH v6 4/18 KEYS: trusted: Open code tpm2_buf_append_auth()

In reply to Jarkko Sakkinen

tpm2_buf_append_auth() has a single call site and most of its parameters are redundant. Open code it to the call site so that less cross-referencing is required while browsing the source code.

---
 security/keys/trusted-keys/trusted_tpm2.c | 40 ++++-------------------
 1 file changed, 7 insertions(+), 33 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +7 -33
index 621f8ffba17e..2f7c8e571522 100644
@@ -197,36 +197,6 @@ int tpm2_key_priv(void *context, size_t hdrlen,
 	return 0;
 }
 
-/**
- * tpm2_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
- *
- * @buf: an allocated tpm_buf instance
- * @session_handle: session handle
- * @nonce: the session nonce, may be NULL if not used
- * @nonce_len: the session nonce length, may be 0 if not used
- * @attributes: the session attributes
- * @hmac: the session HMAC or password, may be NULL if not used
- * @hmac_len: the session HMAC or password length, maybe 0 if not used
- */
-static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,
-				 const u8 *nonce, u16 nonce_len,
-				 u8 attributes,
-				 const u8 *hmac, u16 hmac_len)
-{
-	tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len);
-	tpm_buf_append_u32(buf, session_handle);
-	tpm_buf_append_u16(buf, nonce_len);
-
-	if (nonce && nonce_len)
-		tpm_buf_append(buf, nonce, nonce_len);
-
-	tpm_buf_append_u8(buf, attributes);
-	tpm_buf_append_u16(buf, hmac_len);
-
-	if (hmac && hmac_len)
-		tpm_buf_append(buf, hmac, hmac_len);
-}
-
 /**
  * tpm2_seal_trusted() - seal the payload of a trusted key
  *
@@ -540,9 +510,13 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 		 * could repeat our actions with the exfiltrated
 		 * password.
 		 */
-		tpm2_buf_append_auth(buf, private->policyhandle,
-				     NULL /* nonce */, 0, 0,
-				     private->blobauth, private->blobauth_len);
+		tpm_buf_append_u32(buf, 9 + private->blobauth_len);
+		tpm_buf_append_u32(buf, private->policyhandle);
+		tpm_buf_append_u16(buf, 0);
+		tpm_buf_append_u8(buf, 0);
+		tpm_buf_append_u16(buf, private->blobauth_len);
+		tpm_buf_append(buf, private->blobauth, private->blobauth_len);
+
 		if (tpm2_chip_auth(chip)) {
 			tpm_buf_append_hmac_session(chip, buf, TPM2_SA_ENCRYPT,
 						    NULL, 0);
Signature · 1 line
2.47.3
author maintainer
#5 lore raw

PATCH v6 5/18 KEYS: trusted: Remove dead branch from tpm2_unseal_cmd

In reply to Jarkko Sakkinen

TPM2_Unseal requires TPM2_ST_SESSIONS, and tpm2_unseal_cmd() always does set up either password or HMAC session.

Remove the branch in tpm2_unseal_cmd() conditionally setting TPM2_ST_NO_SESSIONS. It is faulty but luckily it is never exercised at run-time, and thus does not cause regressions.

---
 security/keys/trusted-keys/trusted_tpm2.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +1 -9
index 2f7c8e571522..32d90a1f8357 100644
@@ -470,10 +470,8 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 			   u32 blob_handle)
 {
 	struct trusted_key_tpm *private = options->private;
-	struct tpm_header *head;
 	struct tpm_buf *buf __free(kfree) = NULL;
 	u16 data_len;
-	int offset;
 	u8 *data;
 	int rc;
 
@@ -517,15 +515,9 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 		tpm_buf_append_u16(buf, private->blobauth_len);
 		tpm_buf_append(buf, private->blobauth, private->blobauth_len);
 
-		if (tpm2_chip_auth(chip)) {
+		if (tpm2_chip_auth(chip))
 			tpm_buf_append_hmac_session(chip, buf, TPM2_SA_ENCRYPT,
 						    NULL, 0);
-		} else  {
-			offset = buf->handles * 4 + TPM_HEADER_SIZE;
-			head = (struct tpm_header *)buf->data;
-			if (tpm_buf_length(buf) == offset)
-				head->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
-		}
 	}
 
 	rc = tpm_buf_fill_hmac_session(chip, buf);
Signature · 1 line
2.47.3
author maintainer
#6 lore raw

PATCH v6 6/18 tpm2-sessions: Re-orchestrate tpm2_read_public() use

In reply to Jarkko Sakkinen

Move TPM handle name resolution out of tpm_buf_append_name(), so that callers resolve object names explicitly before constructing HMAC-protected commands:

1. Export tpm2_read_public(), so that trusted_tpm2 can resolve handle names, and store non-object handles in big-endian byte order matching the TPM 2.0 handle Name definition.
2. Call tpm2_read_public() explicitly in tpm2_seal_trusted(), tpm2_load_cmd(), and tpm2_unseal_cmd() before starting the auth session, and pass the pre-resolved name and size to tpm_buf_append_name().
3. Rework tpm_buf_append_name() and tpm_buf_fill_hmac_session() to use the caller-supplied name and size directly.

This reduces fallible exits from tpm2_buf_append_name().

---
 drivers/char/tpm/tpm2-cmd.c               |   9 +-
 drivers/char/tpm/tpm2-sessions.c          | 122 +++++++---------------
 include/linux/tpm.h                       |  10 +-
 security/keys/trusted-keys/trusted_tpm2.c |  33 +++++-
 4 files changed, 84 insertions(+), 90 deletions(-)
drivers/char/tpm/tpm2-cmd.c +5 -4
index 4b2bb2ddc321..9c7e8b9aff4d 100644
@@ -194,11 +194,12 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
 	tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_EXTEND);
 
 	if (!disable_pcr_integrity) {
-		rc = tpm_buf_append_name(chip, buf, pcr_idx, NULL);
-		if (rc) {
-			tpm2_end_auth_session(chip);
+		__be32 pcr_idx_be = cpu_to_be32(pcr_idx);
+
+		rc = tpm_buf_append_name(chip, buf, pcr_idx,
+					 (u8 *)&pcr_idx_be, sizeof(pcr_idx_be));
+		if (rc)
 			return rc;
-		}
 		tpm_buf_append_hmac_session(chip, buf, 0, NULL, 0);
 	} else {
 		tpm_buf_append_handle(buf, pcr_idx);
drivers/char/tpm/tpm2-sessions.c +40 -82

198 lines not on this page · raw message

include/linux/tpm.h +9 -1
index 84c8fe226235..a9fd53363dc4 100644
@@ -300,7 +300,7 @@ static inline struct tpm2_auth *tpm2_chip_auth(struct tpm_chip *chip)
 }
 
 int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
-			u32 handle, u8 *name);
+			u32 handle, u8 *name, u16 name_size);
 void tpm_buf_append_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf,
 				 u8 attributes, u8 *passphrase,
 				 int passphraselen);
@@ -314,6 +314,7 @@ int tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf);
 int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf,
 				int rc);
 void tpm2_end_auth_session(struct tpm_chip *chip);
+int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name);
 #else
 #include <linux/unaligned.h>
 
@@ -337,6 +338,13 @@ static inline int tpm_buf_check_hmac_response(struct tpm_chip *chip,
 {
 	return rc;
 }
+
+static inline int tpm2_read_public(struct tpm_chip *chip, u32 handle,
+				   void *name)
+{
+	put_unaligned_be32(handle, name);
+	return sizeof(u32);
+}
 #endif	/* CONFIG_TCG_TPM2_HMAC */
 
 #endif
security/keys/trusted-keys/trusted_tpm2.c +30 -3
index 32d90a1f8357..575b488a4f78 100644
@@ -211,9 +211,11 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 		      struct trusted_key_options *options)
 {
 	struct trusted_key_tpm *private = options->private;
+	u8 parent_name[TPM2_MAX_NAME_SIZE];
 	off_t offset = TPM_HEADER_SIZE;
 	struct tpm_buf *buf __free(kfree) = NULL;
 	struct tpm_buf *sized __free(kfree) = NULL;
+	u16 parent_name_size;
 	int blob_len = 0;
 	int hash;
 	u32 flags;
@@ -230,6 +232,12 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	if (rc)
 		return rc;
 
+	rc = tpm2_read_public(chip, private->keyhandle, parent_name);
+	if (rc < 0)
+		goto out_put;
+
+	parent_name_size = rc;
+
 	rc = tpm2_start_auth_session(chip);
 	if (rc)
 		goto out_put;
@@ -253,7 +261,8 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 
 	tpm_buf_init_sized(sized, TPM_BUFSIZE);
 
-	rc = tpm_buf_append_name(chip, buf, private->keyhandle, NULL);
+	rc = tpm_buf_append_name(chip, buf, private->keyhandle, parent_name,
+				 parent_name_size);
 	if (rc)
 		goto out;
 
@@ -358,7 +367,9 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 {
 	u8 *blob_ref __free(kfree) = NULL;
 	struct trusted_key_tpm *private = options->private;
+	u8 parent_name[TPM2_MAX_NAME_SIZE];
 	struct tpm_buf *buf __free(kfree) = NULL;
+	u16 parent_name_size;
 	unsigned int private_len;
 	unsigned int public_len;
 	unsigned int blob_len;
@@ -412,6 +423,12 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	if (blob_len > payload->blob_len)
 		return -E2BIG;
 
+	rc = tpm2_read_public(chip, private->keyhandle, parent_name);
+	if (rc < 0)
+		return rc;
+
+	parent_name_size = rc;
+
 	rc = tpm2_start_auth_session(chip);
 	if (rc)
 		return rc;
@@ -425,7 +442,8 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	tpm_buf_init(buf, TPM_BUFSIZE);
 	tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
 
-	rc = tpm_buf_append_name(chip, buf, private->keyhandle, NULL);
+	rc = tpm_buf_append_name(chip, buf, private->keyhandle, parent_name,
+				 parent_name_size);
 	if (rc)
 		return rc;
 
@@ -470,11 +488,19 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 			   u32 blob_handle)
 {
 	struct trusted_key_tpm *private = options->private;
+	u8 blob_name[TPM2_MAX_NAME_SIZE];
 	struct tpm_buf *buf __free(kfree) = NULL;
+	u16 blob_name_size;
 	u16 data_len;
 	u8 *data;
 	int rc;
 
+	rc = tpm2_read_public(chip, blob_handle, blob_name);
+	if (rc < 0)
+		return rc;
+
+	blob_name_size = rc;
+
 	rc = tpm2_start_auth_session(chip);
 	if (rc)
 		return rc;
@@ -488,7 +514,8 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 	tpm_buf_init(buf, TPM_BUFSIZE);
 	tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
 
-	rc = tpm_buf_append_name(chip, buf, blob_handle, NULL);
+	rc = tpm_buf_append_name(chip, buf, blob_handle, blob_name,
+				 blob_name_size);
 	if (rc)
 		return rc;
 
Signature · 1 line
2.47.3
author maintainer
#7 lore raw

PATCH v6 7/18 tpm-buf: Make tpm_buf_read*() fallible

In reply to Jarkko Sakkinen

Make tpm_buf_read_u8(), tpm_buf_read_u16() and tpm_buf_read_u32() flows unambiguous, and fallible by writing the updated offset to @offset.

---
 drivers/char/tpm/tpm-buf.c                |  56 ++++---
 drivers/char/tpm/tpm2-sessions.c          | 189 ++++++++++++++++------
 include/linux/tpm_buf.h                   |   6 +-
 security/keys/trusted-keys/trusted_tpm2.c |  10 +-
 4 files changed, 180 insertions(+), 81 deletions(-)
drivers/char/tpm/tpm-buf.c +30 -26
index 233e81d3f149..a2d79b3a9385 100644
@@ -204,39 +204,35 @@ void tpm_buf_append_handle(struct tpm_buf *buf, u32 handle)
  * @offset:	offset within the buffer
  * @count:	the number of bytes to read
  * @output:	the output buffer
+ *
+ * Return: true on success, and @offset is advanced by @count bytes, or false
+ * if the read would cross the end of the buffer.
  */
-static void tpm_buf_read(struct tpm_buf *buf, off_t *offset, size_t count, void *output)
+static bool tpm_buf_read(struct tpm_buf *buf, off_t *offset, size_t count, void *output)
 {
 	off_t next_offset;
 
-	if (buf->flags & TPM_BUF_INVALID)
-		return;
-
 	next_offset = *offset + count;
-	if (next_offset > buf->length) {
-		WARN(1, "tpm_buf: read out of boundary\n");
-		buf->flags |= TPM_BUF_INVALID;
-		return;
-	}
+	if (next_offset > buf->length)
+		return false;
 
 	memcpy(output, &buf->data[*offset], count);
 	*offset = next_offset;
+	return true;
 }
 
 /**
  * tpm_buf_read_u8() - Read 8-bit word from a TPM buffer
  * @buf:	&tpm_buf instance
  * @offset:	offset within the buffer
+ * @value:	returns the 8-bit word
  *
- * Return: next 8-bit word
+ * Return: true on success, and @offset is advanced, or false if the read
+ * would cross the end of the buffer.
  */
-u8 tpm_buf_read_u8(struct tpm_buf *buf, off_t *offset)
+bool tpm_buf_read_u8(struct tpm_buf *buf, off_t *offset, u8 *value)
 {
-	u8 value = 0;
-
-	tpm_buf_read(buf, offset, sizeof(value), &value);
-
-	return value;
+	return tpm_buf_read(buf, offset, sizeof(*value), value);
 }
 EXPORT_SYMBOL_GPL(tpm_buf_read_u8);
 
@@ -244,16 +240,20 @@ EXPORT_SYMBOL_GPL(tpm_buf_read_u8);
  * tpm_buf_read_u16() - Read 16-bit word from a TPM buffer
  * @buf:	&tpm_buf instance
  * @offset:	offset within the buffer
+ * @value:	returns the 16-bit word
  *
- * Return: next 16-bit word
+ * Return: true on success, and @offset is advanced, or false if the read
+ * would cross the end of the buffer.
  */
-u16 tpm_buf_read_u16(struct tpm_buf *buf, off_t *offset)
+bool tpm_buf_read_u16(struct tpm_buf *buf, off_t *offset, u16 *value)
 {
-	u16 value = 0;
+	__be16 value_be;
 
-	tpm_buf_read(buf, offset, sizeof(value), &value);
+	if (!tpm_buf_read(buf, offset, sizeof(value_be), &value_be))
+		return false;
 
-	return be16_to_cpu(value);
+	*value = be16_to_cpu(value_be);
+	return true;
 }
 EXPORT_SYMBOL_GPL(tpm_buf_read_u16);
 
@@ -261,16 +261,20 @@ EXPORT_SYMBOL_GPL(tpm_buf_read_u16);
  * tpm_buf_read_u32() - Read 32-bit word from a TPM buffer
  * @buf:	&tpm_buf instance
  * @offset:	offset within the buffer
+ * @value:	returns the 32-bit word
  *
- * Return: next 32-bit word
+ * Return: true on success, and @offset is advanced, or false if the read
+ * would cross the end of the buffer.
  */
-u32 tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset)
+bool tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset, u32 *value)
 {
-	u32 value = 0;
+	__be32 value_be;
 
-	tpm_buf_read(buf, offset, sizeof(value), &value);
+	if (!tpm_buf_read(buf, offset, sizeof(value_be), &value_be))
+		return false;
 
-	return be32_to_cpu(value);
+	*value = be32_to_cpu(value_be);
+	return true;
 }
 EXPORT_SYMBOL_GPL(tpm_buf_read_u32);
 
drivers/char/tpm/tpm2-sessions.c +142 -47

380 lines not on this page · raw message

include/linux/tpm_buf.h +3 -3
index f8c105d8b8bf..d0f4c1c3ee2d 100644
@@ -52,9 +52,9 @@ void tpm_buf_append(struct tpm_buf *buf, const u8 *new_data, u16 new_length);
 void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value);
 void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value);
 void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value);
-u8 tpm_buf_read_u8(struct tpm_buf *buf, off_t *offset);
-u16 tpm_buf_read_u16(struct tpm_buf *buf, off_t *offset);
-u32 tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset);
+bool tpm_buf_read_u8(struct tpm_buf *buf, off_t *offset, u8 *value);
+bool tpm_buf_read_u16(struct tpm_buf *buf, off_t *offset, u16 *value);
+bool tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset, u32 *value);
 void tpm_buf_append_handle(struct tpm_buf *buf, u32 handle);
 
 #endif
security/keys/trusted-keys/trusted_tpm2.c +5 -5
index 575b488a4f78..e20e54203b83 100644
@@ -217,6 +217,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	struct tpm_buf *sized __free(kfree) = NULL;
 	u16 parent_name_size;
 	int blob_len = 0;
+	u32 size;
 	int hash;
 	u32 flags;
 	int rc;
@@ -323,18 +324,17 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	if (rc)
 		goto out;
 
-	blob_len = tpm_buf_read_u32(buf, &offset);
-	if (buf->flags & TPM_BUF_INVALID) {
-		rc = -E2BIG;
+	if (!tpm_buf_read_u32(buf, &offset, &size)) {
+		rc = -EIO;
 		goto out;
 	}
 
-	if (buf->length - offset < blob_len) {
+	if (buf->length - offset < size) {
 		rc = -EFAULT;
 		goto out;
 	}
 
-	blob_len = tpm2_key_encode(payload, options, &buf->data[offset], blob_len);
+	blob_len = tpm2_key_encode(payload, options, &buf->data[offset], size);
 	if (blob_len < 0)
 		rc = blob_len;
 
Signature · 1 line
2.47.3
author maintainer
#8 lore raw

PATCH v6 8/18 KEYS: trusted: Allow ASN.1 TPM2 sealed data shorter than MIN_KEY_SIZE

In reply to Jarkko Sakkinen

tpm2_unseal_cmd() rejects unsealed payloads shorter than MIN_KEY_SIZE (32 bytes) with -EFAULT even when loading an ASN.1 TPMKey (OID_TPMSealedData, 2.23.133.10.1.5) blob.

While kernel-generated trusted keys ("new <keylen>") and legacy old_format blobs are at least 32 bytes long, external TPM2 tools allow sealing any non-empty payload up to 128 bytes.

Keep MIN_KEY_SIZE for legacy old_format blobs and accept 1..MAX_KEY_SIZE bytes for ASN.1 TPMKey sealed blobs.

---
 security/keys/trusted-keys/trusted_tpm2.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +4 -2
index e20e54203b83..6b4cd07176cb 100644
@@ -490,8 +490,8 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 	struct trusted_key_tpm *private = options->private;
 	u8 blob_name[TPM2_MAX_NAME_SIZE];
 	struct tpm_buf *buf __free(kfree) = NULL;
+	unsigned int data_len;
 	u16 blob_name_size;
-	u16 data_len;
 	u8 *data;
 	int rc;
 
@@ -555,9 +555,11 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 	rc = tpm_buf_check_hmac_response(chip, buf, rc);
 
 	if (!rc) {
+		unsigned int min_len = payload->old_format ? MIN_KEY_SIZE : 1;
+
 		data_len = be16_to_cpup(
 			(__be16 *)&buf->data[TPM_HEADER_SIZE + 4]);
-		if (data_len < MIN_KEY_SIZE ||  data_len > MAX_KEY_SIZE)
+		if (data_len < min_len || data_len > MAX_KEY_SIZE)
 			return -EFAULT;
 
 		if (tpm_buf_length(buf) < TPM_HEADER_SIZE + 6 + data_len)
Signature · 1 line
2.47.3
author maintainer
#9 lore raw

PATCH v6 9/18 lib/asn1_encoder: Add asn1_encode_integer_bytes()

In reply to Jarkko Sakkinen

Add a helper encoding a positive integer from a byte array in big-endian format.

---
 include/linux/asn1_encoder.h |  3 ++
 lib/asn1_encoder.c           | 56 ++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+)
include/linux/asn1_encoder.h +3 -0
index d17484dffb74..e206bd425854 100644
@@ -12,6 +12,9 @@ unsigned char *
 asn1_encode_integer(unsigned char *data, const unsigned char *end_data,
 		    s64 integer);
 unsigned char *
+asn1_encode_integer_bytes(unsigned char *data, const unsigned char *end_data,
+			  const unsigned char *integer, u32 integer_len);
+unsigned char *
 asn1_encode_oid(unsigned char *data, const unsigned char *end_data,
 		u32 oid[], int oid_len);
 unsigned char *
lib/asn1_encoder.c +56 -0

68 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#10 lore raw

PATCH v6 10/18 tpm: Export tpm2_create_primary()

In reply to Jarkko Sakkinen

Move tpm2_create_primary() outside CONFIG_TCG_TPM2_HMAC and export it for callers such as trusted keys.

---
 drivers/char/tpm/Kconfig         |   2 +-
 drivers/char/tpm/tpm2-sessions.c | 103 ++++++++++++++++---------------
 include/linux/tpm.h              |   2 +
 3 files changed, 56 insertions(+), 51 deletions(-)
drivers/char/tpm/Kconfig +1 -1
index 5f672f2c01b0..c753cf230e96 100644
@@ -9,6 +9,7 @@ menuconfig TCG_TPM
 	imply SECURITYFS
 	select CRYPTO
 	select CRYPTO_HASH_INFO
+	select CRYPTO_LIB_SHA256
 	help
 	  If you have a TPM security chip in your system, which
 	  implements the Trusted Computing Group's specification,
@@ -32,7 +33,6 @@ config TCG_TPM2_HMAC
 	default n
 	select CRYPTO_ECDH
 	select CRYPTO_LIB_AESCFB
-	select CRYPTO_LIB_SHA256
 	select CRYPTO_LIB_UTILS
 	help
 	  Setting this causes us to deploy a scheme which uses request
drivers/char/tpm/tpm2-sessions.c +53 -50

149 lines not on this page · raw message

include/linux/tpm.h +2 -0
index a9fd53363dc4..2230ff848561 100644
@@ -248,6 +248,8 @@ extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max);
 extern struct tpm_chip *tpm_default_chip(void);
 void tpm2_flush_context(struct tpm_chip *chip, u32 handle);
 int tpm2_find_hash_alg(unsigned int crypto_id);
+int tpm2_create_primary(struct tpm_chip *chip, u32 hierarchy, u32 *handle,
+			u8 *name);
 
 static inline void tpm_buf_append_empty_auth(struct tpm_buf *buf, u32 handle)
 {
Signature · 1 line
2.47.3
author maintainer
#11 lore raw

PATCH v6 11/18 tpm: Add persistent hierarchies to tpm2_create_primary()

In reply to Jarkko Sakkinen

TPMKey files can specify a 0x40 MSO permanent handle as parent, requiring TPM2_CreatePrimary with the TCG H-2 NIST P-256 template [1].

Populate chip->null_ec_key_{x,y} in tpm2_parse_create_primary() only for TPM2_RH_NULL.

Verify the returned key name against the locally computed TPMT_PUBLIC digest, and flush the transient handle on validation failure.

[1] https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html#section-3.1.8

---
 drivers/char/tpm/tpm2-sessions.c | 189 ++++++++++++++++---------------
 1 file changed, 97 insertions(+), 92 deletions(-)
drivers/char/tpm/tpm2-sessions.c +97 -92
index 4577df7033a2..871343fbe6c4 100644
@@ -1123,10 +1123,11 @@ int tpm2_sessions_init(struct tpm_chip *chip)
 	return rc;
 }
 #endif /* CONFIG_TCG_TPM2_HMAC */
+
 /*
- * A mask containing the object attributes for the kernel held null primary key
- * used in HMAC encryption. For more information on specific attributes look up
- * to "8.3 TPMA_OBJECT (Object Attributes)".
+ * A mask containing the object attributes for the TCG mandated H-2 ECC P-256
+ * primary storage key template. For more information on specific attributes
+ * look up "8.3 TPMA_OBJECT (Object Attributes)".
  */
 #define TPM2_OA_NULL_KEY ( \
 	TPM2_OA_NO_DA | \
@@ -1156,21 +1157,23 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 {
 	struct tpm_header *head = (struct tpm_header *)buf->data;
 	off_t offset_r = TPM_HEADER_SIZE, offset_t;
-	u16 len = TPM_HEADER_SIZE;
+	u8 resp_name[TPM2_NULL_NAME_SIZE];
 	u32 total_len = be32_to_cpu(head->length);
 	u32 val32, param_len, keyhandle;
-	u16 val;
+#ifdef CONFIG_TCG_TPM2_HMAC
+	const u8 *ec_x, *ec_y;
+#endif
+	int rc = -EIO;
+	u16 val, len;
 
 	if (!tpm_buf_read_u32(buf, &offset_r, &keyhandle))
 		return -EIO;
 
-	if (handle)
-		*handle = keyhandle;
-	else
-		tpm2_flush_context(chip, keyhandle);
-
 	if (!tpm_buf_read_u32(buf, &offset_r, &param_len))
-		return -EIO;
+		goto err;
+	if (total_len < TPM_HEADER_SIZE + 8 ||
+	    param_len > total_len - TPM_HEADER_SIZE - 8)
+		goto err_inval;
 
 	/*
 	 * param_len doesn't include the header, but all the other
@@ -1179,183 +1182,185 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 	 */
 	param_len += TPM_HEADER_SIZE;
 
-	if (param_len + 8 > total_len)
-		return -EINVAL;
-
 	if (!tpm_buf_read_u16(buf, &offset_r, &len))
-		return -EIO;
-
+		goto err;
 	offset_t = offset_r;
-	if (name) {
-		/*
-		 * now we have the public area, compute the name of
-		 * the object
-		 */
-		put_unaligned_be16(TPM_ALG_SHA256, name);
-		sha256(&buf->data[offset_r], len, name + 2);
-	}
+	offset_r += len;
+	if (offset_r > param_len || offset_r > tpm_buf_length(buf))
+		goto err_inval;
+
+	/*
+	 * Now we have the public area, compute the name of the object.
+	 */
+	put_unaligned_be16(TPM_ALG_SHA256, resp_name);
+	sha256(&buf->data[offset_t], len, resp_name + 2);
 
 	/* validate the public key */
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
+		goto err;
 
 	/* key type (must be what we asked for) */
 	if (val != TPM_ALG_ECC)
-		return -EINVAL;
+		goto err_inval;
 
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
+		goto err;
 
 	/* name algorithm */
 	if (val != TPM_ALG_SHA256)
-		return -EINVAL;
+		goto err_inval;
 
 	if (!tpm_buf_read_u32(buf, &offset_t, &val32))
-		return -EIO;
+		goto err;
 
 	/* object properties */
 	if (val32 != TPM2_OA_NULL_KEY)
-		return -EINVAL;
+		goto err_inval;
 
 	/* auth policy (empty) */
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
-
+		goto err;
 	if (val != 0)
-		return -EINVAL;
+		goto err_inval;
 
 	/* symmetric key parameters */
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
-
+		goto err;
 	if (val != TPM_ALG_AES)
-		return -EINVAL;
+		goto err_inval;
 
 	/* symmetric key length */
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
-
+		goto err;
 	if (val != AES_KEY_BITS)
-		return -EINVAL;
+		goto err_inval;
 
 	/* symmetric encryption scheme */
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
-
+		goto err;
 	if (val != TPM_ALG_CFB)
-		return -EINVAL;
+		goto err_inval;
 
 	/* signing scheme */
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
-
+		goto err;
 	if (val != TPM_ALG_NULL)
-		return -EINVAL;
+		goto err_inval;
 
 	/* ECC Curve */
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
-
+		goto err;
 	if (val != TPM2_ECC_NIST_P256)
-		return -EINVAL;
+		goto err_inval;
 
 	/* KDF Scheme */
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
-
+		goto err;
 	if (val != TPM_ALG_NULL)
-		return -EINVAL;
+		goto err_inval;
 
 	/* extract public key (x and y points) */
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
-
-	if (val != EC_PT_SZ)
-		return -EINVAL;
+		goto err;
+	if (val != EC_PT_SZ || offset_t + val > offset_r)
+		goto err_inval;
 #ifdef CONFIG_TCG_TPM2_HMAC
-	memcpy(chip->null_ec_key_x, &buf->data[offset_t], val);
+	ec_x = &buf->data[offset_t];
 #endif
 	offset_t += val;
 
 	if (!tpm_buf_read_u16(buf, &offset_t, &val))
-		return -EIO;
-
-	if (val != EC_PT_SZ)
-		return -EINVAL;
+		goto err;
+	if (val != EC_PT_SZ || offset_t + val > offset_r)
+		goto err_inval;
 #ifdef CONFIG_TCG_TPM2_HMAC
-	memcpy(chip->null_ec_key_y, &buf->data[offset_t], val);
+	ec_y = &buf->data[offset_t];
 #endif
 	offset_t += val;
 
-	/* original length of the whole TPM2B */
-	offset_r += len;
-
 	/* should have exactly consumed the TPM2B public structure */
 	if (offset_t != offset_r)
-		return -EINVAL;
-	if (offset_r > param_len)
-		return -EINVAL;
+		goto err_inval;
 
 	/* creation data (skip) */
 	if (!tpm_buf_read_u16(buf, &offset_r, &len))
-		return -EIO;
-
+		goto err;
 	offset_r += len;
 	if (offset_r > param_len)
-		return -EINVAL;
+		goto err_inval;
 
 	/* creation digest (must be sha256) */
 	if (!tpm_buf_read_u16(buf, &offset_r, &len))
-		return -EIO;
-
+		goto err;
 	offset_r += len;
 	if (len != SHA256_DIGEST_SIZE || offset_r > param_len)
-		return -EINVAL;
+		goto err_inval;
 
 	/* TPMT_TK_CREATION follows */
 	/* tag, must be TPM_ST_CREATION (0x8021) */
 	if (!tpm_buf_read_u16(buf, &offset_r, &val))
-		return -EIO;
-
+		goto err;
 	if (val != TPM2_ST_CREATION || offset_r > param_len)
-		return -EINVAL;
+		goto err_inval;
 
 	/* hierarchy */
 	if (!tpm_buf_read_u32(buf, &offset_r, &val32))
-		return -EIO;
-
+		goto err;
 	if (val32 != hierarchy || offset_r > param_len)
-		return -EINVAL;
+		goto err_inval;
 
 	/* the ticket digest HMAC (might not be sha256) */
 	if (!tpm_buf_read_u16(buf, &offset_r, &len))
-		return -EIO;
-
+		goto err;
 	offset_r += len;
 	if (offset_r > param_len)
-		return -EINVAL;
+		goto err_inval;
 
 	/*
 	 * finally we have the name, which is a sha256 digest plus a 2
 	 * byte algorithm type
 	 */
 	if (!tpm_buf_read_u16(buf, &offset_r, &len))
-		return -EIO;
+		goto err;
+	if (offset_r + len != param_len + 8 || len != sizeof(resp_name))
+		goto err_inval;
+	if (offset_r + len > tpm_buf_length(buf))
+		goto err;
 
-	if (offset_r + len != param_len + 8)
-		return -EINVAL;
-	if (len != SHA256_DIGEST_SIZE + 2)
-		return -EINVAL;
+	if (memcmp(resp_name, &buf->data[offset_r], sizeof(resp_name)) != 0) {
+		dev_err(&chip->dev, "primary key name comparison failed\n");
+		goto err_inval;
+	}
 
 #ifdef CONFIG_TCG_TPM2_HMAC
-	if (memcmp(chip->null_key_name, &buf->data[offset_r],
-		   SHA256_DIGEST_SIZE + 2) != 0) {
-		dev_err(&chip->dev, "NULL Seed name comparison failed\n");
-		return -EINVAL;
+	if (hierarchy == TPM2_RH_NULL) {
+		if (name == chip->null_key_name) {
+			memcpy(chip->null_ec_key_x, ec_x, EC_PT_SZ);
+			memcpy(chip->null_ec_key_y, ec_y, EC_PT_SZ);
+		} else if (memcmp(chip->null_key_name, resp_name,
+				  sizeof(resp_name)) != 0) {
+			dev_err(&chip->dev,
+				"NULL Seed name comparison failed\n");
+			goto err_inval;
+		}
 	}
 #endif
 
+	if (name)
+		memcpy(name, resp_name, sizeof(resp_name));
+
+	if (handle)
+		*handle = keyhandle;
+	else
+		tpm2_flush_context(chip, keyhandle);
+
 	return 0;
+
+err_inval:
+	rc = -EINVAL;
+err:
+	tpm2_flush_context(chip, keyhandle);
+	return rc;
 }
 
 /**
@@ -1467,7 +1472,7 @@ int tpm2_create_primary(struct tpm_chip *chip, u32 hierarchy, u32 *handle,
 	tpm_buf_append_u32(buf, 0);
 
 	rc = tpm_transmit_cmd(chip, buf, 0,
-			      "attempting to create NULL primary");
+			      "attempting to create primary key");
 
 	if (rc == TPM2_RC_SUCCESS)
 		rc = tpm2_parse_create_primary(chip, buf, handle, hierarchy,
Signature · 1 line
2.47.3
author maintainer
#12 lore raw

PATCH v6 12/18 KEYS: trusted: Extend TPMKey support

In reply to Jarkko Sakkinen

Extend tpm2key.asn1 with the optional [1] policy, [2] secret, [3] authPolicy, [4] description, and [5] rsaParent fields.

Create a temporary TCG H-2 NIST P-256 primary key in tpm2_seal_trusted() and tpm2_load_cmd() when the parent handle is TPM2_RH_OWNER, TPM2_RH_NULL, or TPM2_RH_PLATFORM.

---
 .../security/keys/trusted-encrypted.rst       |  18 ++-
 include/keys/trusted_tpm.h                    |   1 -
 include/linux/tpm_command.h                   |   2 +
 security/keys/trusted-keys/tpm2key.asn1       |  19 +++
 security/keys/trusted-keys/trusted_tpm2.c     | 150 +++++++++++++++---
 5 files changed, 161 insertions(+), 29 deletions(-)
Documentation/security/keys/trusted-encrypted.rst +12 -6
index ddff7c7c2582..c6a507ef228a 100644
@@ -561,6 +561,11 @@ policy::
     TPMKey ::= SEQUENCE {
         type		OBJECT IDENTIFIER
         emptyAuth	[0] EXPLICIT BOOLEAN OPTIONAL
+        policy		[1] EXPLICIT SEQUENCE OF TPMPolicy OPTIONAL
+        secret		[2] EXPLICIT OCTET STRING OPTIONAL
+        authPolicy	[3] EXPLICIT SEQUENCE OF TPMAuthPolicy OPTIONAL
+        description	[4] EXPLICIT UTF8String OPTIONAL
+        rsaParent	[5] EXPLICIT BOOLEAN OPTIONAL
         parent		INTEGER
         pubkey		OCTET STRING
         privkey		OCTET STRING
@@ -592,12 +597,13 @@ phrase.  This is used by most user space consumers to decide whether
 to prompt for a password.
 
 parent represents the parent key handle, either in the 0x81 MSO space,
-like 0x81000001 for the RSA primary storage key.  Userspace programmes
-also support specifying the primary handle in the 0x40 MSO space.  If
-this happens the Elliptic Curve variant of the primary key using the
-TCG defined template will be generated on the fly into a volatile
-object and used as the parent.  The current kernel code only supports
-the 0x81 MSO form.
+like 0x81000001 for a persistent primary storage key, or a hierarchy
+handle in the 0x40 MSO permanent handle space (TPM_RH_OWNER 0x40000001,
+TPM_RH_NULL 0x40000007, or TPM_RH_PLATFORM 0x4000000c).  When a 0x40
+MSO hierarchy handle is used without rsaParent, the Elliptic Curve
+variant of the primary key using the TCG defined H-2 NIST P-256 template
+is generated on the fly into a volatile object, used as the parent, and
+flushed afterwards.
 
 pubkey is the binary representation of TPM2B_PRIVATE excluding the
 initial TPM2B header, which can be reconstructed from the ASN.1 octet
include/keys/trusted_tpm.h +0 -1
index 9a83269821e5..b8b1f5c8fbc3 100644
@@ -26,5 +26,4 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 int tpm2_unseal_trusted(struct tpm_chip *chip,
 			struct trusted_key_payload *payload,
 			struct trusted_key_options *options);
-
 #endif
include/linux/tpm_command.h +2 -0
index fc446a1282e2..6ff456f6ec58 100644
@@ -265,8 +265,10 @@ enum tpm2_cc_attrs {
 };
 
 enum tpm2_permanent_handles {
+	TPM2_RH_OWNER		= 0x40000001,
 	TPM2_RH_NULL		= 0x40000007,
 	TPM2_RS_PW		= 0x40000009,
+	TPM2_RH_PLATFORM	= 0x4000000C,
 };
 
 /* Most Significant Octet for key types  */
security/keys/trusted-keys/tpm2key.asn1 +19 -0
index f57f869ad600..d15dfcf593a5 100644
@@ -5,7 +5,26 @@
 TPMKey ::= SEQUENCE {
 	type		OBJECT IDENTIFIER ({tpm2_key_type}),
 	emptyAuth	[0] EXPLICIT BOOLEAN OPTIONAL,
+	policy		[1] EXPLICIT TPMPolicySequence OPTIONAL,
+	secret		[2] EXPLICIT OCTET STRING OPTIONAL ({tpm2_key_secret}),
+	authPolicy	[3] EXPLICIT TPMAuthPolicySequence OPTIONAL,
+	description	[4] EXPLICIT UTF8String OPTIONAL,
+	rsaParent	[5] EXPLICIT BOOLEAN OPTIONAL ({tpm2_key_rsa_parent}),
 	parent		INTEGER ({tpm2_key_parent}),
 	pubkey		OCTET STRING ({tpm2_key_pub}),
 	privkey		OCTET STRING ({tpm2_key_priv})
 	}
+
+TPMPolicySequence ::= SEQUENCE OF TPMPolicy ({tpm2_key_policy})
+
+TPMPolicy ::= SEQUENCE {
+	commandCode	[0] EXPLICIT INTEGER,
+	commandPolicy	[1] EXPLICIT OCTET STRING
+	}
+
+TPMAuthPolicySequence ::= SEQUENCE OF TPMAuthPolicy ({tpm2_key_auth_policy})
+
+TPMAuthPolicy ::= SEQUENCE {
+	name		[0] EXPLICIT UTF8String OPTIONAL,
+	policy		[1] EXPLICIT TPMPolicySequence
+	}
security/keys/trusted-keys/trusted_tpm2.c +128 -22

289 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#13 lore raw

PATCH v6 13/18 KEYS: trusted: Reject zero-length keys in symmetric consumers

In reply to Jarkko Sakkinen

Asymmetric trusted keys will not carry a raw symmetric key payload (key_len == 0). Reject zero-length trusted keys in encrypted-keys, af_alg, dm-crypt, and trusted_update() resealing.

---
 crypto/af_alg.c                                  | 2 ++
 drivers/md/dm-crypt.c                            | 2 +-
 security/keys/encrypted-keys/masterkey_trusted.c | 5 +++++
 security/keys/trusted-keys/trusted_core.c        | 2 +-
 4 files changed, 9 insertions(+), 2 deletions(-)
crypto/af_alg.c +2 -0
index ab84c4488a15..133cb88e3c12 100644
@@ -323,6 +323,8 @@ static const u8 *key_data_ptr_trusted(const struct key *key,
 	if (IS_ERR_OR_NULL(tkp))
 		return ERR_PTR(-EKEYREVOKED);
 
+	if (!tkp->key_len)
+		return ERR_PTR(-EINVAL);
 	*datalen = tkp->key_len;
 
 	return tkp->key;
drivers/md/dm-crypt.c +1 -1
index 02038a9cd21d..1e034c747d39 100644
@@ -2492,7 +2492,7 @@ static int set_key_trusted(struct crypt_config *cc, struct key *key)
 	if (!tkp)
 		return -EKEYREVOKED;
 
-	if (cc->key_size != tkp->key_len)
+	if (!tkp->key_len || cc->key_size != tkp->key_len)
 		return -EINVAL;
 
 	memcpy(cc->key, tkp->key, cc->key_size);
security/keys/encrypted-keys/masterkey_trusted.c +5 -0
index e6d22ce77e98..8c6e6bdd8ef7 100644
@@ -36,6 +36,11 @@ struct key *request_trusted_key(const char *trusted_desc,
 
 	down_read(&tkey->sem);
 	tpayload = tkey->payload.data[0];
+	if (!tpayload->key_len) {
+		up_read(&tkey->sem);
+		key_put(tkey);
+		return ERR_PTR(-EINVAL);
+	}
 	*master_key = tpayload->key;
 	*master_keylen = tpayload->key_len;
 error:
security/keys/trusted-keys/trusted_core.c +1 -1
index 27756fc906b9..665b2c37734b 100644
@@ -249,7 +249,7 @@ static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
 	if (key_is_negative(key))
 		return -ENOKEY;
 	p = key->payload.data[0];
-	if (!p->migratable)
+	if (!p->migratable || !p->key_len)
 		return -EPERM;
 	if (datalen == 0 || datalen > 32767 || !prep->data)
 		return -EINVAL;
Signature · 1 line
2.47.3
author maintainer
#14 lore raw

PATCH v6 14/18 KEYS: trusted: Add private backend data to trusted_key_payload

In reply to Jarkko Sakkinen

Add a backend-private pointer to struct trusted_key_payload and free it sensitively via trusted_payload_free() on teardown and error paths. This prepares for asymmetric TPM2 keys, which retain backend key metadata across runtime pkey operations instead of unsealing a raw key at load time.

---
 include/keys/trusted-type.h               | 14 ++++++++++++++
 security/keys/trusted-keys/trusted_core.c | 18 +++++++++++++-----
 2 files changed, 27 insertions(+), 5 deletions(-)
include/keys/trusted-type.h +14 -0
index 3db61b57cf73..6b31b66c0d35 100644
@@ -27,12 +27,26 @@
 #define MAX_PCRINFO_SIZE		64
 #define MAX_DIGEST_SIZE			64
 
+/**
+ * struct trusted_key_payload - In-kernel representation of a trusted key
+ * @rcu:	RCU head for deferred payload reclamation on key update.
+ * @key_len:	Byte length of the unsealed symmetric @key, or %0 when the
+ *		payload represents a hardware-bound asymmetric key.
+ * @blob_len:	Byte length of the trust-source-wrapped @blob.
+ * @migratable:	Non-zero if the key may be resealed with %KEYCTL_UPDATE.
+ * @old_format:	Non-zero if @blob uses the legacy non-ASN.1 TPM 2.0 format.
+ * @private:	Optional backend-owned runtime context freed sensitively with
+ *		the payload.
+ * @key:	Unsealed symmetric key material for in-kernel consumers.
+ * @blob:	Wrapped key blob exported in ASCII hex by %KEYCTL_READ.
+ */
 struct trusted_key_payload {
 	struct rcu_head rcu;
 	unsigned int key_len;
 	unsigned int blob_len;
 	unsigned char migratable;
 	unsigned char old_format;
+	void *private;
 	unsigned char key[MAX_KEY_SIZE + 1];
 	unsigned char blob[MAX_BLOB_SIZE];
 };
security/keys/trusted-keys/trusted_core.c +13 -5
index 665b2c37734b..1db6497ba297 100644
@@ -149,6 +149,14 @@ static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
 	return p;
 }
 
+static void trusted_payload_free(struct trusted_key_payload *p)
+{
+	if (!p)
+		return;
+	kfree_sensitive(p->private);
+	kfree_sensitive(p);
+}
+
 /*
  * trusted_instantiate - create a new trusted key
  *
@@ -223,7 +231,7 @@ static int trusted_instantiate(struct key *key,
 	if (!ret)
 		rcu_assign_keypointer(key, payload);
 	else
-		kfree_sensitive(payload);
+		trusted_payload_free(payload);
 	return ret;
 }
 
@@ -232,7 +240,7 @@ static void trusted_rcu_free(struct rcu_head *rcu)
 	struct trusted_key_payload *p;
 
 	p = container_of(rcu, struct trusted_key_payload, rcu);
-	kfree_sensitive(p);
+	trusted_payload_free(p);
 }
 
 /*
@@ -269,7 +277,7 @@ static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
 	ret = datablob_parse(&datablob, new_p);
 	if (ret != Opt_update) {
 		ret = -EINVAL;
-		kfree_sensitive(new_p);
+		trusted_payload_free(new_p);
 		goto out;
 	}
 
@@ -283,7 +291,7 @@ static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
 	ret = static_call(trusted_key_seal)(new_p, datablob);
 	if (ret < 0) {
 		pr_info("key_seal failed (%d)\n", ret);
-		kfree_sensitive(new_p);
+		trusted_payload_free(new_p);
 		goto out;
 	}
 
@@ -322,7 +330,7 @@ static long trusted_read(const struct key *key, char *buffer,
  */
 static void trusted_destroy(struct key *key)
 {
-	kfree_sensitive(key->payload.data[0]);
+	trusted_payload_free(key->payload.data[0]);
 }
 
 struct key_type key_type_trusted = {
Signature · 1 line
2.47.3
author maintainer
#15 lore raw

PATCH v6 15/18 KEYS: trusted: Factor out TPM2 blob loading

In reply to Jarkko Sakkinen

Prepare for asymmetric TPM2 key operations, which reload the TPM key blob and append key authorization on each pkey call, by factoring out common helpers:

1. Decouple tpm2_key_decode() from struct trusted_key_options.
2. Factor out tpm2_load_blob() and tpm2_buf_append_key_auth() from tpm2_load_cmd() and tpm2_unseal_cmd().

---
 security/keys/trusted-keys/trusted_tpm2.c | 202 ++++++++++++----------
 1 file changed, 112 insertions(+), 90 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +112 -90
index a19ab2a62f65..467e8570b106 100644
@@ -97,6 +97,7 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
 
 struct tpm2_key_context {
 	u32 parent;
+	enum OID oid;
 	const u8 *pub;
 	u32 pub_len;
 	const u8 *priv;
@@ -104,11 +105,11 @@ struct tpm2_key_context {
 	bool has_policy;
 };
 
-static int tpm2_key_decode(struct trusted_key_payload *payload,
-			   struct trusted_key_options *options,
-			   u8 **buf, unsigned int *blob_len)
+static int tpm2_key_decode(const struct trusted_key_payload *payload,
+			   const struct trusted_key_tpm *private,
+			   u32 *parent, u8 **buf, unsigned int *blob_len,
+			   enum OID *oid)
 {
-	struct trusted_key_tpm *private = options->private;
 	int ret;
 	struct tpm2_key_context ctx;
 	u8 *blob;
@@ -132,8 +133,10 @@ static int tpm2_key_decode(struct trusted_key_payload *payload,
 
 	*buf = blob;
 	*blob_len = ctx.priv_len + ctx.pub_len;
-	if (!private->keyhandle)
-		private->keyhandle = ctx.parent;
+	if (oid)
+		*oid = ctx.oid;
+	if (parent)
+		*parent = ctx.parent;
 
 	memcpy(blob, ctx.priv, ctx.priv_len);
 	blob += ctx.priv_len;
@@ -209,18 +212,22 @@ int tpm2_key_type(void *context, size_t hdrlen,
 		unsigned char tag,
 		const void *value, size_t vlen)
 {
+	struct tpm2_key_context *ctx = context;
 	enum OID oid = look_up_OID(value, vlen);
 
-	if (oid != OID_TPMSealedData) {
+	if (oid == OID_TPMSealedData) {
+		ctx->oid = oid;
+		return 0;
+	}
+
+	if (IS_ENABLED(CONFIG_DEBUG_KERNEL)) {
 		char buffer[50];
 
 		sprint_oid(value, vlen, buffer, sizeof(buffer));
-		pr_debug("OID is \"%s\" which is not TPMSealedData\n",
-			 buffer);
-		return -EINVAL;
+		pr_debug("OID \"%s\" is not supported\n", buffer);
 	}
 
-	return 0;
+	return -EINVAL;
 }
 int tpm2_key_pub(void *context, size_t hdrlen,
 	       unsigned char tag,
@@ -441,6 +448,69 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	return tpm_ret_to_err(rc);
 }
 
+static int tpm2_load_blob(struct tpm_chip *chip,
+			  struct trusted_key_tpm *private,
+			  const u8 *blob, unsigned int blob_len,
+			  u32 *blob_handle)
+{
+	u8 parent_name[TPM2_MAX_NAME_SIZE];
+	struct tpm_buf *buf __free(kfree) = NULL;
+	bool created_parent = false;
+	u16 parent_name_size;
+	u32 parent_handle;
+	int rc;
+
+	rc = tpm2_load_parent(chip, private->keyhandle, &parent_handle,
+			      parent_name, &parent_name_size, &created_parent);
+	if (rc)
+		return rc;
+
+	rc = tpm2_start_auth_session(chip);
+	if (rc)
+		goto out;
+
+	buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
+	if (!buf) {
+		tpm2_end_auth_session(chip);
+		rc = -ENOMEM;
+		goto out;
+	}
+
+	tpm_buf_init(buf, TPM_BUFSIZE);
+	tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
+
+	rc = tpm_buf_append_name(chip, buf, parent_handle, parent_name,
+				 parent_name_size);
+	if (rc)
+		goto out;
+
+	tpm_buf_append_hmac_session(chip, buf, 0, private->keyauth,
+				    TPM_DIGEST_SIZE);
+
+	tpm_buf_append(buf, blob, blob_len);
+
+	if (buf->flags & TPM_BUF_INVALID) {
+		tpm2_end_auth_session(chip);
+		rc = -E2BIG;
+		goto out;
+	}
+
+	rc = tpm_buf_fill_hmac_session(chip, buf);
+	if (rc)
+		goto out;
+
+	rc = tpm_transmit_cmd(chip, buf, 4, "loading blob");
+	rc = tpm_buf_check_hmac_response(chip, buf, rc);
+	if (!rc)
+		*blob_handle = get_unaligned_be32(&buf->data[TPM_HEADER_SIZE]);
+
+out:
+	if (created_parent)
+		tpm2_flush_context(chip, parent_handle);
+
+	return tpm_ret_to_err(rc);
+}
+
 /**
  * tpm2_load_cmd() - execute a TPM2_Load command
  *
@@ -461,19 +531,15 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 {
 	u8 *blob_ref __free(kfree) = NULL;
 	struct trusted_key_tpm *private = options->private;
-	u8 parent_name[TPM2_MAX_NAME_SIZE];
-	struct tpm_buf *buf __free(kfree) = NULL;
-	bool created_parent = false;
-	u16 parent_name_size;
 	unsigned int private_len;
 	unsigned int public_len;
 	unsigned int blob_len;
-	u32 parent_handle;
+	u32 parent = 0;
 	u8 *blob, *pub;
 	int rc;
 	u32 attrs;
 
-	rc = tpm2_key_decode(payload, options, &blob, &blob_len);
+	rc = tpm2_key_decode(payload, private, &parent, &blob, &blob_len, NULL);
 	if (rc) {
 		if (rc != -EBADMSG ||
 		    (payload->blob_len &&
@@ -487,6 +553,8 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	} else {
 		/* Bind for cleanup: */
 		blob_ref = blob;
+		if (!private->keyhandle)
+			private->keyhandle = parent;
 	}
 
 	/* new format carries keyhandle but old format doesn't */
@@ -524,56 +592,36 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	if (blob_len > payload->blob_len)
 		return -E2BIG;
 
-	rc = tpm2_load_parent(chip, private->keyhandle, &parent_handle,
-			      parent_name, &parent_name_size, &created_parent);
-	if (rc)
-		return rc;
-
-	rc = tpm2_start_auth_session(chip);
-	if (rc)
-		goto out;
-
-	buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
-	if (!buf) {
-		tpm2_end_auth_session(chip);
-		rc = -ENOMEM;
-		goto out;
-	}
-
-	tpm_buf_init(buf, TPM_BUFSIZE);
-	tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
-
-	rc = tpm_buf_append_name(chip, buf, parent_handle, parent_name,
-				 parent_name_size);
-	if (rc)
-		goto out;
-
-	tpm_buf_append_hmac_session(chip, buf, 0, private->keyauth,
-				    TPM_DIGEST_SIZE);
-
-	tpm_buf_append(buf, blob, blob_len);
+	return tpm2_load_blob(chip, private, blob, blob_len, blob_handle);
+}
 
-	if (buf->flags & TPM_BUF_INVALID) {
-		tpm2_end_auth_session(chip);
-		rc = -E2BIG;
-		goto out;
+static void tpm2_buf_append_key_auth(struct tpm_chip *chip,
+				     struct tpm_buf *buf,
+				     struct trusted_key_tpm *private,
+				     u8 hmac_attrs)
+{
+	if (!private->policyhandle) {
+		tpm_buf_append_hmac_session(chip, buf, hmac_attrs,
+					    private->blobauth,
+					    private->blobauth_len);
+		return;
 	}
 
-	rc = tpm_buf_fill_hmac_session(chip, buf);
-	if (rc)
-		goto out;
-
-	rc = tpm_transmit_cmd(chip, buf, 4, "loading blob");
-	rc = tpm_buf_check_hmac_response(chip, buf, rc);
-	if (!rc)
-		*blob_handle = be32_to_cpup(
-			(__be32 *)&buf->data[TPM_HEADER_SIZE]);
-
-out:
-	if (created_parent)
-		tpm2_flush_context(chip, parent_handle);
+	/*
+	 * FIXME: The policy session was generated outside the kernel so we
+	 * don't know the nonce and thus can't calculate a HMAC on it.
+	 * Therefore, the user can only really use TPM2_PolicyPassword and we
+	 * must send down the plain text password, which could be intercepted.
+	 */
+	tpm_buf_append_u32(buf, 9 + private->blobauth_len);
+	tpm_buf_append_u32(buf, private->policyhandle);
+	tpm_buf_append_u16(buf, 0);
+	tpm_buf_append_u8(buf, 0);
+	tpm_buf_append_u16(buf, private->blobauth_len);
+	tpm_buf_append(buf, private->blobauth, private->blobauth_len);
 
-	return tpm_ret_to_err(rc);
+	if (tpm2_chip_auth(chip))
+		tpm_buf_append_hmac_session(chip, buf, hmac_attrs, NULL, 0);
 }
 
 /**
@@ -625,33 +673,7 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 	if (rc)
 		return rc;
 
-	if (!private->policyhandle) {
-		tpm_buf_append_hmac_session(chip, buf, TPM2_SA_ENCRYPT,
-					    private->blobauth,
-					    private->blobauth_len);
-	} else {
-		/*
-		 * FIXME: The policy session was generated outside the
-		 * kernel so we don't known the nonce and thus can't
-		 * calculate a HMAC on it.  Therefore, the user can
-		 * only really use TPM2_PolicyPassword and we must
-		 * send down the plain text password, which could be
-		 * intercepted.  We can still encrypt the returned
-		 * key, but that's small comfort since the interposer
-		 * could repeat our actions with the exfiltrated
-		 * password.
-		 */
-		tpm_buf_append_u32(buf, 9 + private->blobauth_len);
-		tpm_buf_append_u32(buf, private->policyhandle);
-		tpm_buf_append_u16(buf, 0);
-		tpm_buf_append_u8(buf, 0);
-		tpm_buf_append_u16(buf, private->blobauth_len);
-		tpm_buf_append(buf, private->blobauth, private->blobauth_len);
-
-		if (tpm2_chip_auth(chip))
-			tpm_buf_append_hmac_session(chip, buf, TPM2_SA_ENCRYPT,
-						    NULL, 0);
-	}
+	tpm2_buf_append_key_auth(chip, buf, private, TPM2_SA_ENCRYPT);
 
 	rc = tpm_buf_fill_hmac_session(chip, buf);
 	if (rc)
Signature · 1 line
2.47.3
author maintainer
#16 lore raw

PATCH v6 16/18 KEYS: trusted: Add asymmetric key operations

In reply to Jarkko Sakkinen

Allow trust source backends to expose hardware-protected asymmetric keys through KEYCTL_PKEY_* without exposing private key material in plaintext or requiring direct userspace access to the underlying hardware device.

Add optional asymmetric key callbacks (asym_query, asym_eds_op, and asym_verify_signature) to struct trusted_key_ops and wire them to key_type_trusted in trusted_core via static calls when a backend provides them, reusing trusted-key backend selection, authorization options, and wrapped blob handling.

Gate the support with CONFIG_TRUSTED_KEYS_ASYMMETRIC, exposed when a backend selects HAVE_TRUSTED_KEYS_ASYMMETRIC.

---
 include/keys/trusted-type.h               |  49 ++++++---
 security/keys/trusted-keys/Kconfig        |  14 +++
 security/keys/trusted-keys/trusted_core.c | 121 ++++++++++++++++++++++
 3 files changed, 170 insertions(+), 14 deletions(-)
include/keys/trusted-type.h +35 -14
index 6b31b66c0d35..93e00b6947f0 100644
@@ -27,6 +27,9 @@
 #define MAX_PCRINFO_SIZE		64
 #define MAX_DIGEST_SIZE			64
 
+struct kernel_pkey_params;
+struct kernel_pkey_query;
+
 /**
  * struct trusted_key_payload - In-kernel representation of a trusted key
  * @rcu:	RCU head for deferred payload reclamation on key update.
@@ -56,26 +59,44 @@ struct trusted_key_options {
 	void *private;
 };
 
+/**
+ * struct trusted_key_ops - Trust source backend operations
+ * @migratable:			Default migratability flag for newly sealed
+ *				symmetric keys (%1 if resealing is supported).
+ * @init:			Initialize the trust source backend.
+ * @seal:			Seal @p->key into @p->blob for ``"new"`` and
+ *				``"update"`` commands.
+ * @unseal:			Unseal a symmetric @p->blob into @p->key, or
+ *				validate and populate @p->private for an
+ *				asymmetric key blob (``p->key_len == 0``).
+ * @get_random:			Optional callback to draw @key_len random bytes
+ *				from the trust source RNG.
+ * @asym_query:			Optional callback to query asymmetric key size,
+ *				buffer bounds, and supported operations.
+ * @asym_eds_op:		Optional callback to encrypt, decrypt, or sign
+ *				with an asymmetric trusted key.
+ * @asym_verify_signature:	Optional callback to verify a signature with an
+ *				asymmetric trusted key.
+ * @exit:			Tear down the trust source backend.
+ *
+ * A backend that supports asymmetric keys provides @asym_query, @asym_eds_op,
+ * and @asym_verify_signature, and selects %CONFIG_HAVE_TRUSTED_KEYS_ASYMMETRIC.
+ */
 struct trusted_key_ops {
-	/*
-	 * flag to indicate if trusted key implementation supports migration
-	 * or not.
-	 */
 	unsigned char migratable;
-
-	/* Initialize key interface. */
 	int (*init)(void);
-
-	/* Seal a key. */
 	int (*seal)(struct trusted_key_payload *p, char *datablob);
-
-	/* Unseal a key. */
 	int (*unseal)(struct trusted_key_payload *p, char *datablob);
-
-	/* Optional: Get a randomized key. */
 	int (*get_random)(unsigned char *key, size_t key_len);
-
-	/* Exit key interface. */
+	int (*asym_query)(const struct trusted_key_payload *p,
+			  const struct kernel_pkey_params *params,
+			  struct kernel_pkey_query *info);
+	int (*asym_eds_op)(const struct trusted_key_payload *p,
+			   struct kernel_pkey_params *params,
+			   const void *in, void *out);
+	int (*asym_verify_signature)(const struct trusted_key_payload *p,
+				     struct kernel_pkey_params *params,
+				     const void *in, const void *in2);
 	void (*exit)(void);
 };
 
security/keys/trusted-keys/Kconfig +14 -0
index e5a4a53aeab2..30b49460c3d9 100644
@@ -4,6 +4,20 @@ config HAVE_TRUSTED_KEYS
 config HAVE_TRUSTED_KEYS_DEBUG
 	bool
 
+config HAVE_TRUSTED_KEYS_ASYMMETRIC
+	bool
+
+config TRUSTED_KEYS_ASYMMETRIC
+	bool "Asymmetric trusted keys"
+	depends on HAVE_TRUSTED_KEYS_ASYMMETRIC
+	default y
+	select KEYCTL_PKEY
+	help
+	  Allow trusted key backends that support asymmetric keys to load
+	  public/private key blobs and expose them through the keyctl
+	  public-key operations (query, encrypt, decrypt, sign, and verify)
+	  without changing support for sealed symmetric keys.
+
 config TRUSTED_KEYS_DEBUG
 	bool "Debug trusted keys"
 	depends on HAVE_TRUSTED_KEYS_DEBUG
security/keys/trusted-keys/trusted_core.c +121 -0

157 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#17 lore raw

PATCH v6 17/18 KEYS: trusted: Add asymmetric key support for TPM2

In reply to Jarkko Sakkinen

Extend the TPM 2.0 trusted key backend to load ASN.1 TPMKey asymmetric keys (OID_TPMLoadableKey, 2.23.133.10.1.3) for RSA (2048/3072/4096) and ECC (NIST P-256/P-384/P-521), wiring query, sign, verify, and RSA encrypt/decrypt operations to trusted_key_tpm_ops.

Child keys may be wrapped under either a persistent primary storage key (0x81 MSO) or a permanent hierarchy handle (TPM2_RH_OWNER, TPM2_RH_NULL, or TPM2_RH_PLATFORM), for which a transient TCG H-2 NIST P-256 primary key is derived and flushed on the fly. Private-key operations (TPM2_Sign and TPM2_RSA_Decrypt) use HMAC authorization and parameter encryption sessions on the TPM bus.

Select HAVE_TRUSTED_KEYS_ASYMMETRIC when CONFIG_TRUSTED_KEYS_TPM is enabled.

---
 include/keys/trusted-type.h               |    4 +-
 include/keys/trusted_tpm.h                |   44 +
 include/linux/tpm_command.h               |   11 +
 security/keys/trusted-keys/Kconfig        |    1 +
 security/keys/trusted-keys/trusted_tpm1.c |   24 +
 security/keys/trusted-keys/trusted_tpm2.c | 1123 ++++++++++++++++++++-
 6 files changed, 1203 insertions(+), 4 deletions(-)
include/keys/trusted-type.h +3 -1
index 93e00b6947f0..ea39ed2a334c 100644
@@ -19,7 +19,9 @@
 
 #define MIN_KEY_SIZE			32
 #define MAX_KEY_SIZE			128
-#if IS_ENABLED(CONFIG_TRUSTED_KEYS_PKWM)
+#if IS_ENABLED(CONFIG_TRUSTED_KEYS_TPM)
+#define MAX_BLOB_SIZE			2048
+#elif IS_ENABLED(CONFIG_TRUSTED_KEYS_PKWM)
 #define MAX_BLOB_SIZE			1152
 #else
 #define MAX_BLOB_SIZE                   512
include/keys/trusted_tpm.h +44 -0

60 lines not on this page · raw message

include/linux/tpm_command.h +11 -0
index 6ff456f6ec58..ce800124c4e5 100644
@@ -190,6 +190,7 @@ enum tpm2_structures {
 	TPM2_ST_NO_SESSIONS	= 0x8001,
 	TPM2_ST_SESSIONS	= 0x8002,
 	TPM2_ST_CREATION	= 0x8021,
+	TPM2_ST_HASHCHECK	= 0x8024,
 };
 
 /* Indicates from what layer of the software stack the error comes from */
@@ -224,12 +225,15 @@ enum tpm2_command_codes {
 	TPM2_CC_NV_READ			= 0x014E,
 	TPM2_CC_CREATE			= 0x0153,
 	TPM2_CC_LOAD			= 0x0157,
+	TPM2_CC_RSA_DECRYPT		= 0x0159,
 	TPM2_CC_SEQUENCE_UPDATE		= 0x015C,
+	TPM2_CC_SIGN			= 0x015D,
 	TPM2_CC_UNSEAL			= 0x015E,
 	TPM2_CC_CONTEXT_LOAD		= 0x0161,
 	TPM2_CC_CONTEXT_SAVE		= 0x0162,
 	TPM2_CC_FLUSH_CONTEXT		= 0x0165,
 	TPM2_CC_READ_PUBLIC		= 0x0173,
+	TPM2_CC_RSA_ENCRYPT		= 0x0174,
 	TPM2_CC_START_AUTH_SESS		= 0x0176,
 	TPM2_CC_VERIFY_SIGNATURE	= 0x0177,
 	TPM2_CC_GET_CAPABILITY		= 0x017A,
@@ -284,6 +288,8 @@ enum tpm2_mso_type {
 enum tpm2_curves {
 	TPM2_ECC_NONE		= 0x0000,
 	TPM2_ECC_NIST_P256	= 0x0003,
+	TPM2_ECC_NIST_P384	= 0x0004,
+	TPM2_ECC_NIST_P521	= 0x0005,
 };
 
 enum tpm2_object_attributes {
@@ -463,6 +469,7 @@ struct tpm2_context {
 /* If you add a new hash to this, increment TPM_MAX_HASHES below */
 enum tpm_algorithms {
 	TPM_ALG_ERROR		= 0x0000,
+	TPM_ALG_RSA		= 0x0001,
 	TPM_ALG_SHA1		= 0x0004,
 	TPM_ALG_AES		= 0x0006,
 	TPM_ALG_KEYEDHASH	= 0x0008,
@@ -471,6 +478,10 @@ enum tpm_algorithms {
 	TPM_ALG_SHA512		= 0x000D,
 	TPM_ALG_NULL		= 0x0010,
 	TPM_ALG_SM3_256		= 0x0012,
+	TPM_ALG_RSASSA		= 0x0014,
+	TPM_ALG_RSAES		= 0x0015,
+	TPM_ALG_OAEP		= 0x0017,
+	TPM_ALG_ECDSA		= 0x0018,
 	TPM_ALG_ECC		= 0x0023,
 	TPM_ALG_CFB		= 0x0043,
 };
security/keys/trusted-keys/Kconfig +1 -0
index 30b49460c3d9..fce92c8c4d0b 100644
@@ -38,6 +38,7 @@ config TRUSTED_KEYS_TPM
 	depends on TCG_TPM >= TRUSTED_KEYS
 	default y
 	select HAVE_TRUSTED_KEYS_DEBUG
+	select HAVE_TRUSTED_KEYS_ASYMMETRIC
 	select CRYPTO_HASH_INFO
 	select CRYPTO_LIB_SHA1
 	select CRYPTO_LIB_UTILS
security/keys/trusted-keys/trusted_tpm1.c +24 -0
index 3b7af11d30c4..7ff5110027d0 100644
@@ -943,6 +943,9 @@ static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob)
 		}
 	}
 
+	if (!p->key_len)
+		p->private = no_free_ptr(private);
+
 	return ret;
 }
 
@@ -951,6 +954,22 @@ static int trusted_tpm_get_random(unsigned char *key, size_t key_len)
 	return tpm_get_random(chip, key, key_len);
 }
 
+#ifdef CONFIG_TRUSTED_KEYS_ASYMMETRIC
+static int trusted_tpm_asym_eds_op(const struct trusted_key_payload *p,
+				   struct kernel_pkey_params *params,
+				   const void *in, void *out)
+{
+	return tpm2_asym_eds_op(chip, p, params, in, out);
+}
+
+static int trusted_tpm_asym_verify_signature(const struct trusted_key_payload *p,
+					     struct kernel_pkey_params *params,
+					     const void *in, const void *in2)
+{
+	return tpm2_asym_verify_signature(chip, p, params, in, in2);
+}
+#endif
+
 static int __init init_digests(void)
 {
 	int i;
@@ -1002,5 +1021,10 @@ struct trusted_key_ops trusted_key_tpm_ops = {
 	.seal = trusted_tpm_seal,
 	.unseal = trusted_tpm_unseal,
 	.get_random = trusted_tpm_get_random,
+#ifdef CONFIG_TRUSTED_KEYS_ASYMMETRIC
+	.asym_query = tpm2_asym_query,
+	.asym_eds_op = trusted_tpm_asym_eds_op,
+	.asym_verify_signature = trusted_tpm_asym_verify_signature,
+#endif
 	.exit = trusted_tpm_exit,
 };
security/keys/trusted-keys/trusted_tpm2.c +1120 -3

1,179 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#18 lore raw

PATCH v6 18/18 docs: trusted-encrypted: Document asymmetric trusted keys

In reply to Jarkko Sakkinen

Document asymmetric trusted keys and the TPM 2.0 asymmetric backend in trusted-encrypted.rst by modularly including the kernel-doc sections, data structures, and backend entry points from:

- include/keys/trusted-type.h
- include/keys/trusted_tpm.h
- security/keys/trusted-keys/trusted_core.c
- security/keys/trusted-keys/trusted_tpm2.c

---
 .../security/keys/trusted-encrypted.rst       | 59 +++++++++++++++++--
 1 file changed, 55 insertions(+), 4 deletions(-)
Documentation/security/keys/trusted-encrypted.rst +55 -4
index c6a507ef228a..b3aefaf4f0c2 100644
@@ -2,9 +2,10 @@
 Trusted and Encrypted Keys
 ==========================
 
-Trusted and Encrypted Keys are two new key types added to the existing kernel
-key ring service.  Both of these new types are variable length symmetric keys,
-and in both cases all keys are created in the kernel, and user space sees,
+Trusted and Encrypted Keys are two key types in the kernel keyring service.
+Both types support variable-length symmetric keys generated in the kernel, and
+Trusted Keys also support loading hardware-bound asymmetric keys when
+``CONFIG_TRUSTED_KEYS_ASYMMETRIC`` is enabled.  In all cases user space sees,
 stores, and loads only encrypted blobs.  Trusted Keys require the availability
 of a Trust Source for greater security, while Encrypted Keys can be used on any
 system. All user level blobs, are displayed and loaded in hex ASCII for
@@ -589,7 +590,9 @@ available are::
                     represents a symmetric key and must be unsealed before
                     use.
 
-The trusted key code only uses the TPM Sealed Data OID.
+The trusted key code uses TPM Sealed Data for symmetric keys and, when
+``CONFIG_TRUSTED_KEYS_ASYMMETRIC`` is enabled, TPM Loadable Key for
+asymmetric keys.
 
 emptyAuth is true if the key has well known authorization "".  If it
 is false or not present, the key requires an explicit authorization
@@ -621,3 +624,51 @@ DCP Blob Format
 
 .. kernel-doc:: security/keys/trusted-keys/trusted_dcp.c
    :identifiers: struct dcp_blob_fmt
+
+Asymmetric Trusted Keys
+=======================
+
+Overview
+--------
+
+.. kernel-doc:: security/keys/trusted-keys/trusted_core.c
+   :doc: Asymmetric Trusted Keys
+
+Backend Interface
+-----------------
+
+.. kernel-doc:: include/keys/trusted-type.h
+   :identifiers: trusted_key_payload trusted_key_ops
+
+TPM 2.0 Backend
+---------------
+
+.. kernel-doc:: security/keys/trusted-keys/trusted_tpm2.c
+   :doc: TPM 2.0 Asymmetric Keys
+
+TPM 2.0 Parent Key Resolution
+-----------------------------
+
+.. kernel-doc:: security/keys/trusted-keys/trusted_tpm2.c
+   :doc: TPM 2.0 Parent Key Resolution
+
+TPM 2.0 Algorithms and Encodings
+--------------------------------
+
+.. kernel-doc:: security/keys/trusted-keys/trusted_tpm2.c
+   :doc: TPM 2.0 Asymmetric Algorithms and Encodings
+
+TPM 2.0 Machine Owner Key Example
+---------------------------------
+
+.. kernel-doc:: security/keys/trusted-keys/trusted_tpm2.c
+   :doc: TPM 2.0 Machine Owner Key Example
+
+TPM 2.0 Driver Reference
+------------------------
+
+.. kernel-doc:: include/keys/trusted_tpm.h
+   :identifiers: trusted_key_tpm
+
+.. kernel-doc:: security/keys/trusted-keys/trusted_tpm2.c
+   :identifiers: tpm2_asym_query tpm2_asym_eds_op tpm2_asym_verify_signature
Signature · 1 line
2.47.3
author maintainer
#19 lore raw
In reply to Jarkko Sakkinen
Quoted 194 lines · Jarkko Sakkinen

OVERVIEW
========

Extend TPM 2.0 trusted keys to load ASN.1 TPMKey [1] asymmetric keys (OID_TPMLoadableKey, 2.23.133.10.1.3) for RSA (2048/3072/4096) and ECC (NIST P-256/P-384/P-521), and expose keyctl pkey-operations directly on key_type_trusted. In addition, extend TPMKey support to derive a transient TCG H-2 NIST P-256 primary key on the fly for 0x40 permanent hierarchy handles (such as TPM2_RH_OWNER).

This allows provisioning an asymmetric key whose private portion stays sealed to the TPM rather than sitting in plaintext on disk or in kernel memory, while letting privileged setup load the blob into a keyring and grant signing or decryption access via KEYCTL_PKEY_* without exposing /dev/tpm0, /dev/tpmrm0, or a userspace PKCS#11 stack. For example, a UEFI Secure Boot Machine Owner Key (MOK) used for local module signing can have its X.509 certificate enrolled into MokList while its private key is wrapped into an OID_TPMLoadableKey blob and shredded from disk; network daemons such as iwd can similarly authenticate with TPM-bound client keys.

Earlier iterations ([2], [3]) added a separate subtype under key_type_asymmetric. Instead, factor public-key keyctl dispatch behind CONFIG_KEYCTL_PKEY and wire optional asymmetric callbacks onto key_type_trusted, reusing trusted-key backend selection, authorization options, and TPM2 HMAC bus sessions without enabling software asymmetric private-key operations (CONFIG_ASYMMETRIC_KEY_PKEY). Prerequisite tpm2-sessions and tpm-buf cleanups from [4] are folded in.

[1] https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
[2] https://lore.kernel.org/linux-integrity/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/linux-integrity/[email protected]/

CHANGES
=======

v6:
- docs: Move asymmetric trusted-key documentation into modular kernel-doc sections and struct/callback comments in include/keys/trusted-type.h, include/keys/trusted_tpm.h, trusted_core.c, and trusted_tpm2.c, including a UEFI Secure Boot MOK provisioning and signing example, and include them from Documentation/security/keys/trusted-encrypted.rst.
- Describe motivation and add base-commit metadata.

v5:
- tpm / trusted: Fold in the tpm2-sessions and fallible tpm_buf_read*() patches [4] (adding Jonathan McDowell's Reviewed-by to "tpm2-sessions:
Drop TPM2_NAME_SIZE"), resolve handle names with tpm2_read_public(), and replace the custom tpm2_read_*() / tpm2_skip_bytes() helpers with tpm_buf_read_u{16,32}() in tpm2_asym_parse_*_public().
- trusted: Split "KEYS: trusted: Add asymmetric key support for TPM2" into a framework patch ("KEYS: trusted: Add asymmetric key operations", adding optional asym_* callbacks on struct trusted_key_ops and HAVE_TRUSTED_KEYS_ASYMMETRIC) and the TPM2 backend patch.
- trusted: Move "KEYS: trusted: Validate public_len before reading key attributes" to patch 01/18 and add the Fixes tag for e5fb5d2c5a03.

v4:
- KEYS: Turn CONFIG_KEYCTL_PKEY into a hidden helper symbol selected per
subsystem: add off-by-default CONFIG_ASYMMETRIC_KEY_PKEY for software asymmetric keys, and switch TURRIS_SIGNING_KEY, TURRIS_OMNIA_MCU_KEYCTL, and TURRIS_MOX_RWTM_KEYCTL from CONFIG_ASYMMETRIC_KEY_TYPE to CONFIG_KEYCTL_PKEY (remaining default y).
- trusted: Default CONFIG_TRUSTED_KEYS_ASYMMETRIC to y and select CONFIG_KEYCTL_PKEY.
- trusted: Split "KEYS: trusted: Factor out TPM2 blob loading" into separate patches for public_len bounds checking, the private backend pointer in trusted_key_payload, and TPM2 blob loading.

v3:
- KEYS: Move CONFIG_KEYCTL_PKEY factoring to the beginning of the series and make it an off-by-default prompt instead of selecting it from CONFIG_ASYMMETRIC_KEY_TYPE.
- trusted: Make CONFIG_TRUSTED_KEYS_ASYMMETRIC depend on CONFIG_KEYCTL_PKEY instead of selecting it.
- trusted: Use unsigned int instead of u16 for data_len and min_len in tpm2_unseal_cmd().
- asn1: The reason for not supporting implicit tags was that it follows what other functions do in asn1_encoder.c, and is that way coherent.

v2:
- lib/asn1_encoder: Remove unreachable !bytes_len branch in asn1_encode_integer_bytes().
- tpm: Bounds-check ECC coordinates, defer state updates until after validation, and verify subsequent TPM2_RH_NULL primaries against chip->null_key_name in tpm2_parse_create_primary().
- trusted: Reject trailing or truncated TPM2B bytes in tpm2_key_decode() so tpm2_load_cmd() and tpm2_asym_load() pass the same blob length.
- trusted: Avoid 32-bit param_size wraparound in tpm2_asym_rsa_decrypt() and tpm2_asym_parse_signature().

VERSIONS
========

v5: https://lore.kernel.org/keyrings/[email protected]/
v4: https://lore.kernel.org/keyrings/[email protected]/
v3: https://lore.kernel.org/keyrings/[email protected]/
v2: https://lore.kernel.org/keyrings/[email protected]/
v1: https://lore.kernel.org/keyrings/[email protected]/

Cc Ahmad Fatoum <[email protected]>
Cc Alasdair Kergon <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Benjamin Marzinski <[email protected]>
Cc David Gstir <[email protected]>
Cc David Howells <[email protected]>
Cc David Laight <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Eric Biggers <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Ignat Korchagin <[email protected]>
Cc James Bottomley <[email protected]>
Cc James Morris <[email protected]>
Cc James Prestwood <[email protected]>
Cc Jarkko Sakkinen <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Jonathan McDowell <[email protected]>
Cc Lukas Wunner <[email protected]>
Cc Marek Behún <[email protected]>
Cc Mike Snitzer <[email protected]>
Cc Mikulas Patocka <[email protected]>
Cc Mimi Zohar <[email protected]>
Cc Paul Moore <[email protected]>
Cc Peter Huewe <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Sumit Garg <[email protected]>

Jarkko Sakkinen (18):
KEYS: trusted: Validate public_len before reading key attributes
KEYS: Factor out KEYCTL_PKEY
tpm2-sessions: Drop TPM2_NAME_SIZE
KEYS: trusted: Open code tpm2_buf_append_auth()
KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
tpm2-sessions: Re-orchestrate tpm2_read_public() use
tpm-buf: Make tpm_buf_read*() fallible
KEYS: trusted: Allow ASN.1 TPM2 sealed data shorter than MIN_KEY_SIZE lib/asn1_encoder: Add asn1_encode_integer_bytes()
tpm: Export tpm2_create_primary()
tpm: Add persistent hierarchies to tpm2_create_primary()
KEYS: trusted: Extend TPMKey support
KEYS: trusted: Reject zero-length keys in symmetric consumers
KEYS: trusted: Add private backend data to trusted_key_payload
KEYS: trusted: Factor out TPM2 blob loading
KEYS: trusted: Add asymmetric key operations
KEYS: trusted: Add asymmetric key support for TPM2
docs: trusted-encrypted: Document asymmetric trusted keys

 .../security/keys/trusted-encrypted.rst       |   77 +-
 crypto/af_alg.c                               |    2 +
 crypto/asymmetric_keys/Kconfig                |   11 +
 crypto/asymmetric_keys/asymmetric_keys.h      |    2 +
 crypto/asymmetric_keys/asymmetric_type.c      |    4 +
 drivers/char/tpm/Kconfig                      |    2 +-
 drivers/char/tpm/tpm-buf.c                    |   56 +-
 drivers/char/tpm/tpm-sysfs.c                  |    2 +-
 drivers/char/tpm/tpm2-cmd.c                   |    9 +-
 drivers/char/tpm/tpm2-sessions.c              |  523 +++---
 drivers/firmware/Kconfig                      |    1 -
 drivers/md/dm-crypt.c                         |    2 +-
 drivers/platform/cznic/Kconfig                |    3 +-
 include/keys/trusted-type.h                   |   67 +-
 include/keys/trusted_tpm.h                    |   45 +-
 include/linux/asn1_encoder.h                  |    3 +
 include/linux/tpm.h                           |   20 +-
 include/linux/tpm_buf.h                       |    6 +-
 include/linux/tpm_command.h                   |   13 +
 lib/asn1_encoder.c                            |   56 +
 security/keys/Kconfig                         |    3 +
 security/keys/Makefile                        |    2 +-
 .../keys/encrypted-keys/masterkey_trusted.c   |    5 +
 security/keys/internal.h                      |    2 +-
 security/keys/keyctl.c                        |    2 +-
 security/keys/trusted-keys/Kconfig            |   15 +
 security/keys/trusted-keys/tpm2key.asn1       |   19 +
 security/keys/trusted-keys/trusted_core.c     |  141 +-
 security/keys/trusted-keys/trusted_tpm1.c     |   24 +
 security/keys/trusted-keys/trusted_tpm2.c     | 1467 +++++++++++++++--
 30 files changed, 2159 insertions(+), 425 deletions(-)
Quoted 9 lines

base-commit: 822ba3a89c7896b26e4beb70e2ebffb881924dfe
prerequisite-patch-id: 6b9f0c26cb5c2290d5a2a78c18ccf225b09a6bbb
prerequisite-patch-id: 0f71199bbbbaf9fe1fa82afb1926b452c493532e
prerequisite-patch-id: 97be79eb77addef7282d5cb9966c4208cf0e965e
prerequisite-patch-id: 4517a290b914c1a79717aece49041479eca4b399
prerequisite-patch-id: 0b49963321abe30437c8e82ffd64fa235c9746bd --
2.47.3

As feature this is larger than userland containers :-) That's about 2000 LOC. Both span history from around 2020 work by others than me (credits due, credits given).

Br, Jarkko

author maintainer
#20 lore raw
Quoted 205 lines · Jarkko Sakkinen
Quoted 194 lines · Jarkko Sakkinen

OVERVIEW
========

Extend TPM 2.0 trusted keys to load ASN.1 TPMKey [1] asymmetric keys (OID_TPMLoadableKey, 2.23.133.10.1.3) for RSA (2048/3072/4096) and ECC (NIST P-256/P-384/P-521), and expose keyctl pkey-operations directly on key_type_trusted. In addition, extend TPMKey support to derive a transient TCG H-2 NIST P-256 primary key on the fly for 0x40 permanent hierarchy handles (such as TPM2_RH_OWNER).

This allows provisioning an asymmetric key whose private portion stays sealed to the TPM rather than sitting in plaintext on disk or in kernel memory, while letting privileged setup load the blob into a keyring and grant signing or decryption access via KEYCTL_PKEY_* without exposing /dev/tpm0, /dev/tpmrm0, or a userspace PKCS#11 stack. For example, a UEFI Secure Boot Machine Owner Key (MOK) used for local module signing can have its X.509 certificate enrolled into MokList while its private key is wrapped into an OID_TPMLoadableKey blob and shredded from disk; network daemons such as iwd can similarly authenticate with TPM-bound client keys.

Earlier iterations ([2], [3]) added a separate subtype under key_type_asymmetric. Instead, factor public-key keyctl dispatch behind CONFIG_KEYCTL_PKEY and wire optional asymmetric callbacks onto key_type_trusted, reusing trusted-key backend selection, authorization options, and TPM2 HMAC bus sessions without enabling software asymmetric private-key operations (CONFIG_ASYMMETRIC_KEY_PKEY). Prerequisite tpm2-sessions and tpm-buf cleanups from [4] are folded in.

[1] https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
[2] https://lore.kernel.org/linux-integrity/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/linux-integrity/[email protected]/

CHANGES
=======

v6:
- docs: Move asymmetric trusted-key documentation into modular kernel-doc sections and struct/callback comments in include/keys/trusted-type.h, include/keys/trusted_tpm.h, trusted_core.c, and trusted_tpm2.c, including a UEFI Secure Boot MOK provisioning and signing example, and include them from Documentation/security/keys/trusted-encrypted.rst.
- Describe motivation and add base-commit metadata.

v5:
- tpm / trusted: Fold in the tpm2-sessions and fallible tpm_buf_read*() patches [4] (adding Jonathan McDowell's Reviewed-by to "tpm2-sessions:
Drop TPM2_NAME_SIZE"), resolve handle names with tpm2_read_public(), and replace the custom tpm2_read_*() / tpm2_skip_bytes() helpers with tpm_buf_read_u{16,32}() in tpm2_asym_parse_*_public().
- trusted: Split "KEYS: trusted: Add asymmetric key support for TPM2" into a framework patch ("KEYS: trusted: Add asymmetric key operations", adding optional asym_* callbacks on struct trusted_key_ops and HAVE_TRUSTED_KEYS_ASYMMETRIC) and the TPM2 backend patch.
- trusted: Move "KEYS: trusted: Validate public_len before reading key attributes" to patch 01/18 and add the Fixes tag for e5fb5d2c5a03.

v4:
- KEYS: Turn CONFIG_KEYCTL_PKEY into a hidden helper symbol selected per
subsystem: add off-by-default CONFIG_ASYMMETRIC_KEY_PKEY for software asymmetric keys, and switch TURRIS_SIGNING_KEY, TURRIS_OMNIA_MCU_KEYCTL, and TURRIS_MOX_RWTM_KEYCTL from CONFIG_ASYMMETRIC_KEY_TYPE to CONFIG_KEYCTL_PKEY (remaining default y).
- trusted: Default CONFIG_TRUSTED_KEYS_ASYMMETRIC to y and select CONFIG_KEYCTL_PKEY.
- trusted: Split "KEYS: trusted: Factor out TPM2 blob loading" into separate patches for public_len bounds checking, the private backend pointer in trusted_key_payload, and TPM2 blob loading.

v3:
- KEYS: Move CONFIG_KEYCTL_PKEY factoring to the beginning of the series and make it an off-by-default prompt instead of selecting it from CONFIG_ASYMMETRIC_KEY_TYPE.
- trusted: Make CONFIG_TRUSTED_KEYS_ASYMMETRIC depend on CONFIG_KEYCTL_PKEY instead of selecting it.
- trusted: Use unsigned int instead of u16 for data_len and min_len in tpm2_unseal_cmd().
- asn1: The reason for not supporting implicit tags was that it follows what other functions do in asn1_encoder.c, and is that way coherent.

v2:
- lib/asn1_encoder: Remove unreachable !bytes_len branch in asn1_encode_integer_bytes().
- tpm: Bounds-check ECC coordinates, defer state updates until after validation, and verify subsequent TPM2_RH_NULL primaries against chip->null_key_name in tpm2_parse_create_primary().
- trusted: Reject trailing or truncated TPM2B bytes in tpm2_key_decode() so tpm2_load_cmd() and tpm2_asym_load() pass the same blob length.
- trusted: Avoid 32-bit param_size wraparound in tpm2_asym_rsa_decrypt() and tpm2_asym_parse_signature().

VERSIONS
========

v5: https://lore.kernel.org/keyrings/[email protected]/
v4: https://lore.kernel.org/keyrings/[email protected]/
v3: https://lore.kernel.org/keyrings/[email protected]/
v2: https://lore.kernel.org/keyrings/[email protected]/
v1: https://lore.kernel.org/keyrings/[email protected]/

Cc Ahmad Fatoum <[email protected]>
Cc Alasdair Kergon <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Benjamin Marzinski <[email protected]>
Cc David Gstir <[email protected]>
Cc David Howells <[email protected]>
Cc David Laight <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Eric Biggers <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Ignat Korchagin <[email protected]>
Cc James Bottomley <[email protected]>
Cc James Morris <[email protected]>
Cc James Prestwood <[email protected]>
Cc Jarkko Sakkinen <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Jonathan McDowell <[email protected]>
Cc Lukas Wunner <[email protected]>
Cc Marek Behún <[email protected]>
Cc Mike Snitzer <[email protected]>
Cc Mikulas Patocka <[email protected]>
Cc Mimi Zohar <[email protected]>
Cc Paul Moore <[email protected]>
Cc Peter Huewe <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Sumit Garg <[email protected]>

Jarkko Sakkinen (18):
KEYS: trusted: Validate public_len before reading key attributes
KEYS: Factor out KEYCTL_PKEY
tpm2-sessions: Drop TPM2_NAME_SIZE
KEYS: trusted: Open code tpm2_buf_append_auth()
KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
tpm2-sessions: Re-orchestrate tpm2_read_public() use
tpm-buf: Make tpm_buf_read*() fallible
KEYS: trusted: Allow ASN.1 TPM2 sealed data shorter than MIN_KEY_SIZE lib/asn1_encoder: Add asn1_encode_integer_bytes()
tpm: Export tpm2_create_primary()
tpm: Add persistent hierarchies to tpm2_create_primary()
KEYS: trusted: Extend TPMKey support
KEYS: trusted: Reject zero-length keys in symmetric consumers
KEYS: trusted: Add private backend data to trusted_key_payload
KEYS: trusted: Factor out TPM2 blob loading
KEYS: trusted: Add asymmetric key operations
KEYS: trusted: Add asymmetric key support for TPM2
docs: trusted-encrypted: Document asymmetric trusted keys

 .../security/keys/trusted-encrypted.rst       |   77 +-
 crypto/af_alg.c                               |    2 +
 crypto/asymmetric_keys/Kconfig                |   11 +
 crypto/asymmetric_keys/asymmetric_keys.h      |    2 +
 crypto/asymmetric_keys/asymmetric_type.c      |    4 +
 drivers/char/tpm/Kconfig                      |    2 +-
 drivers/char/tpm/tpm-buf.c                    |   56 +-
 drivers/char/tpm/tpm-sysfs.c                  |    2 +-
 drivers/char/tpm/tpm2-cmd.c                   |    9 +-
 drivers/char/tpm/tpm2-sessions.c              |  523 +++---
 drivers/firmware/Kconfig                      |    1 -
 drivers/md/dm-crypt.c                         |    2 +-
 drivers/platform/cznic/Kconfig                |    3 +-
 include/keys/trusted-type.h                   |   67 +-
 include/keys/trusted_tpm.h                    |   45 +-
 include/linux/asn1_encoder.h                  |    3 +
 include/linux/tpm.h                           |   20 +-
 include/linux/tpm_buf.h                       |    6 +-
 include/linux/tpm_command.h                   |   13 +
 lib/asn1_encoder.c                            |   56 +
 security/keys/Kconfig                         |    3 +
 security/keys/Makefile                        |    2 +-
 .../keys/encrypted-keys/masterkey_trusted.c   |    5 +
 security/keys/internal.h                      |    2 +-
 security/keys/keyctl.c                        |    2 +-
 security/keys/trusted-keys/Kconfig            |   15 +
 security/keys/trusted-keys/tpm2key.asn1       |   19 +
 security/keys/trusted-keys/trusted_core.c     |  141 +-
 security/keys/trusted-keys/trusted_tpm1.c     |   24 +
 security/keys/trusted-keys/trusted_tpm2.c     | 1467 +++++++++++++++--
 30 files changed, 2159 insertions(+), 425 deletions(-)
Quoted 9 lines

base-commit: 822ba3a89c7896b26e4beb70e2ebffb881924dfe
prerequisite-patch-id: 6b9f0c26cb5c2290d5a2a78c18ccf225b09a6bbb
prerequisite-patch-id: 0f71199bbbbaf9fe1fa82afb1926b452c493532e
prerequisite-patch-id: 97be79eb77addef7282d5cb9966c4208cf0e965e
prerequisite-patch-id: 4517a290b914c1a79717aece49041479eca4b399
prerequisite-patch-id: 0b49963321abe30437c8e82ffd64fa235c9746bd --
2.47.3

Quoted 3 lines

As feature this is larger than userland containers :-) That's about 2000 LOC. Both span history from around 2020 work by others than me (credits due, credits given).

And you would not have to fetch far from this (mainly policy commands) to allow deviceless TPM usage in some of the use cases of Linux.

Br, Jarkko

author maintainer
#21 lore raw
Quoted 206 lines · Jarkko Sakkinen
Quoted 205 lines · Jarkko Sakkinen
Quoted 203 lines · Jarkko Sakkinen

OVERVIEW
========

Extend TPM 2.0 trusted keys to load ASN.1 TPMKey [1] asymmetric keys (OID_TPMLoadableKey, 2.23.133.10.1.3) for RSA (2048/3072/4096) and ECC (NIST P-256/P-384/P-521), and expose keyctl pkey-operations directly on key_type_trusted. In addition, extend TPMKey support to derive a transient TCG H-2 NIST P-256 primary key on the fly for 0x40 permanent hierarchy handles (such as TPM2_RH_OWNER).

This allows provisioning an asymmetric key whose private portion stays sealed to the TPM rather than sitting in plaintext on disk or in kernel memory, while letting privileged setup load the blob into a keyring and grant signing or decryption access via KEYCTL_PKEY_* without exposing /dev/tpm0, /dev/tpmrm0, or a userspace PKCS#11 stack. For example, a UEFI Secure Boot Machine Owner Key (MOK) used for local module signing can have its X.509 certificate enrolled into MokList while its private key is wrapped into an OID_TPMLoadableKey blob and shredded from disk; network daemons such as iwd can similarly authenticate with TPM-bound client keys.

Earlier iterations ([2], [3]) added a separate subtype under key_type_asymmetric. Instead, factor public-key keyctl dispatch behind CONFIG_KEYCTL_PKEY and wire optional asymmetric callbacks onto key_type_trusted, reusing trusted-key backend selection, authorization options, and TPM2 HMAC bus sessions without enabling software asymmetric private-key operations (CONFIG_ASYMMETRIC_KEY_PKEY). Prerequisite tpm2-sessions and tpm-buf cleanups from [4] are folded in.

[1] https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
[2] https://lore.kernel.org/linux-integrity/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/linux-integrity/[email protected]/

CHANGES
=======

v6:
- docs: Move asymmetric trusted-key documentation into modular kernel-doc sections and struct/callback comments in include/keys/trusted-type.h, include/keys/trusted_tpm.h, trusted_core.c, and trusted_tpm2.c, including a UEFI Secure Boot MOK provisioning and signing example, and include them from Documentation/security/keys/trusted-encrypted.rst.
- Describe motivation and add base-commit metadata.

v5:
- tpm / trusted: Fold in the tpm2-sessions and fallible tpm_buf_read*() patches [4] (adding Jonathan McDowell's Reviewed-by to "tpm2-sessions:
Drop TPM2_NAME_SIZE"), resolve handle names with tpm2_read_public(), and replace the custom tpm2_read_*() / tpm2_skip_bytes() helpers with tpm_buf_read_u{16,32}() in tpm2_asym_parse_*_public().
- trusted: Split "KEYS: trusted: Add asymmetric key support for TPM2" into a framework patch ("KEYS: trusted: Add asymmetric key operations", adding optional asym_* callbacks on struct trusted_key_ops and HAVE_TRUSTED_KEYS_ASYMMETRIC) and the TPM2 backend patch.
- trusted: Move "KEYS: trusted: Validate public_len before reading key attributes" to patch 01/18 and add the Fixes tag for e5fb5d2c5a03.

v4:
- KEYS: Turn CONFIG_KEYCTL_PKEY into a hidden helper symbol selected per
subsystem: add off-by-default CONFIG_ASYMMETRIC_KEY_PKEY for software asymmetric keys, and switch TURRIS_SIGNING_KEY, TURRIS_OMNIA_MCU_KEYCTL, and TURRIS_MOX_RWTM_KEYCTL from CONFIG_ASYMMETRIC_KEY_TYPE to CONFIG_KEYCTL_PKEY (remaining default y).
- trusted: Default CONFIG_TRUSTED_KEYS_ASYMMETRIC to y and select CONFIG_KEYCTL_PKEY.
- trusted: Split "KEYS: trusted: Factor out TPM2 blob loading" into separate patches for public_len bounds checking, the private backend pointer in trusted_key_payload, and TPM2 blob loading.

v3:
- KEYS: Move CONFIG_KEYCTL_PKEY factoring to the beginning of the series and make it an off-by-default prompt instead of selecting it from CONFIG_ASYMMETRIC_KEY_TYPE.
- trusted: Make CONFIG_TRUSTED_KEYS_ASYMMETRIC depend on CONFIG_KEYCTL_PKEY instead of selecting it.
- trusted: Use unsigned int instead of u16 for data_len and min_len in tpm2_unseal_cmd().
- asn1: The reason for not supporting implicit tags was that it follows what other functions do in asn1_encoder.c, and is that way coherent.

v2:
- lib/asn1_encoder: Remove unreachable !bytes_len branch in asn1_encode_integer_bytes().
- tpm: Bounds-check ECC coordinates, defer state updates until after validation, and verify subsequent TPM2_RH_NULL primaries against chip->null_key_name in tpm2_parse_create_primary().
- trusted: Reject trailing or truncated TPM2B bytes in tpm2_key_decode() so tpm2_load_cmd() and tpm2_asym_load() pass the same blob length.
- trusted: Avoid 32-bit param_size wraparound in tpm2_asym_rsa_decrypt() and tpm2_asym_parse_signature().

VERSIONS
========

v5: https://lore.kernel.org/keyrings/[email protected]/
v4: https://lore.kernel.org/keyrings/[email protected]/
v3: https://lore.kernel.org/keyrings/[email protected]/
v2: https://lore.kernel.org/keyrings/[email protected]/
v1: https://lore.kernel.org/keyrings/[email protected]/

Cc Ahmad Fatoum <[email protected]>
Cc Alasdair Kergon <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Benjamin Marzinski <[email protected]>
Cc David Gstir <[email protected]>
Cc David Howells <[email protected]>
Cc David Laight <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Eric Biggers <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Ignat Korchagin <[email protected]>
Cc James Bottomley <[email protected]>
Cc James Morris <[email protected]>
Cc James Prestwood <[email protected]>
Cc Jarkko Sakkinen <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Jonathan McDowell <[email protected]>
Cc Lukas Wunner <[email protected]>
Cc Marek Behún <[email protected]>
Cc Mike Snitzer <[email protected]>
Cc Mikulas Patocka <[email protected]>
Cc Mimi Zohar <[email protected]>
Cc Paul Moore <[email protected]>
Cc Peter Huewe <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Sumit Garg <[email protected]>

Jarkko Sakkinen (18):
KEYS: trusted: Validate public_len before reading key attributes
KEYS: Factor out KEYCTL_PKEY
tpm2-sessions: Drop TPM2_NAME_SIZE
KEYS: trusted: Open code tpm2_buf_append_auth()
KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
tpm2-sessions: Re-orchestrate tpm2_read_public() use
tpm-buf: Make tpm_buf_read*() fallible
KEYS: trusted: Allow ASN.1 TPM2 sealed data shorter than MIN_KEY_SIZE lib/asn1_encoder: Add asn1_encode_integer_bytes()
tpm: Export tpm2_create_primary()
tpm: Add persistent hierarchies to tpm2_create_primary()
KEYS: trusted: Extend TPMKey support
KEYS: trusted: Reject zero-length keys in symmetric consumers
KEYS: trusted: Add private backend data to trusted_key_payload
KEYS: trusted: Factor out TPM2 blob loading
KEYS: trusted: Add asymmetric key operations
KEYS: trusted: Add asymmetric key support for TPM2
docs: trusted-encrypted: Document asymmetric trusted keys

 .../security/keys/trusted-encrypted.rst       |   77 +-
 crypto/af_alg.c                               |    2 +
 crypto/asymmetric_keys/Kconfig                |   11 +
 crypto/asymmetric_keys/asymmetric_keys.h      |    2 +
 crypto/asymmetric_keys/asymmetric_type.c      |    4 +
 drivers/char/tpm/Kconfig                      |    2 +-
 drivers/char/tpm/tpm-buf.c                    |   56 +-
 drivers/char/tpm/tpm-sysfs.c                  |    2 +-
 drivers/char/tpm/tpm2-cmd.c                   |    9 +-
 drivers/char/tpm/tpm2-sessions.c              |  523 +++---
 drivers/firmware/Kconfig                      |    1 -
 drivers/md/dm-crypt.c                         |    2 +-
 drivers/platform/cznic/Kconfig                |    3 +-
 include/keys/trusted-type.h                   |   67 +-
 include/keys/trusted_tpm.h                    |   45 +-
 include/linux/asn1_encoder.h                  |    3 +
 include/linux/tpm.h                           |   20 +-
 include/linux/tpm_buf.h                       |    6 +-
 include/linux/tpm_command.h                   |   13 +
 lib/asn1_encoder.c                            |   56 +
 security/keys/Kconfig                         |    3 +
 security/keys/Makefile                        |    2 +-
 .../keys/encrypted-keys/masterkey_trusted.c   |    5 +
 security/keys/internal.h                      |    2 +-
 security/keys/keyctl.c                        |    2 +-
 security/keys/trusted-keys/Kconfig            |   15 +
 security/keys/trusted-keys/tpm2key.asn1       |   19 +
 security/keys/trusted-keys/trusted_core.c     |  141 +-
 security/keys/trusted-keys/trusted_tpm1.c     |   24 +
 security/keys/trusted-keys/trusted_tpm2.c     | 1467 +++++++++++++++--
 30 files changed, 2159 insertions(+), 425 deletions(-)

base-commit: 822ba3a89c7896b26e4beb70e2ebffb881924dfe
prerequisite-patch-id: 6b9f0c26cb5c2290d5a2a78c18ccf225b09a6bbb
prerequisite-patch-id: 0f71199bbbbaf9fe1fa82afb1926b452c493532e
prerequisite-patch-id: 97be79eb77addef7282d5cb9966c4208cf0e965e
prerequisite-patch-id: 4517a290b914c1a79717aece49041479eca4b399
prerequisite-patch-id: 0b49963321abe30437c8e82ffd64fa235c9746bd --
2.47.3

Quoted 6 lines
Quoted 3 lines

As feature this is larger than userland containers :-) That's about 2000 LOC. Both span history from around 2020 work by others than me (credits due, credits given).

And you would not have to fetch far from this (mainly policy commands) to allow deviceless TPM usage in some of the use cases of Linux.

Fully passed (again) Sashiko review:

https://sashiko.dev/#/patchset/20261128215621.2253928-1-jarkko%40kernel.org

Br, Jarkko