Skip to contentSkip to conversation

KEYS: trusted: Use ASN.1 encoded OID

PATCH In review Jarkko Sakkinen 1 participant 3 messages May 23, 2024

Subsystems (5)
lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

PATCH v3 1/1 KEYS: trusted: Use ASN.1 encoded OID

There's no reason to encode OID_TPMSealedData at run-time, as it never changes.

Replace it with an encoded u8-array, which has the same number of elements:

	67 81 05 0A 01 05

Include OBJECT IDENTIFIER (0x06) tag and length as the prologue so that the OID can be simply copied to the blob leading to:

	06 06 67 81 05 0A 01 05

Since this in stationary place in the buffer it is guaranteed to always fit and not further checks are required.

---
v3:
* Addressed Ben's suggestions:
https://lore.kernel.org/linux-integrity/Zk9Hb-whVYvJrfLY@farprobe/
* Added David's R-b, as the only change in code was to add const to the declaration.
v2:
* Not my day I guess. This one has print_hex_dump() taken away. Apologies for spamming. The patch is however tested properly with run-tests.sh in https://gitlab.com/jarkkojs/linux-tpmdd-test.

---
 include/linux/asn1_encoder.h              |  4 -
 lib/asn1_encoder.c                        | 91 -----------------------
 security/keys/trusted-keys/trusted_tpm2.c |  6 +-
 3 files changed, 3 insertions(+), 98 deletions(-)
include/linux/asn1_encoder.h +0 -4
index 08cd0c2ad34f..afeefdfe2525 100644
@@ -8,14 +8,10 @@
 #include <linux/asn1_ber_bytecode.h>
 #include <linux/bug.h>
 
-#define asn1_oid_len(oid) (sizeof(oid)/sizeof(u32))
 unsigned char *
 asn1_encode_integer(unsigned char *data, const unsigned char *end_data,
 		    s64 integer);
 unsigned char *
-asn1_encode_oid(unsigned char *data, const unsigned char *end_data,
-		u32 oid[], int oid_len);
-unsigned char *
 asn1_encode_tag(unsigned char *data, const unsigned char *end_data,
 		u32 tag, const unsigned char *string, int len);
 unsigned char *
lib/asn1_encoder.c +0 -91

97 lines not on this page · raw message

security/keys/trusted-keys/trusted_tpm2.c +3 -3
index 8b7dd73d94c1..d478096cb5c1 100644
@@ -26,7 +26,7 @@ static struct tpm2_hash tpm2_hash_map[] = {
 	{HASH_ALGO_SM3_256, TPM_ALG_SM3_256},
 };
 
-static u32 tpm2key_oid[] = { 2, 23, 133, 10, 1, 5 };
+static const u8 OID_TPMSealedData_ASN1[] = {0x06, 0x06, 0x67, 0x81, 0x05, 0x0a, 0x01, 0x05};
 
 static int tpm2_key_encode(struct trusted_key_payload *payload,
 			   struct trusted_key_options *options,
@@ -51,8 +51,8 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
 	if (!scratch)
 		return -ENOMEM;
 
-	work = asn1_encode_oid(work, end_work, tpm2key_oid,
-			       asn1_oid_len(tpm2key_oid));
+	work = memcpy(work, OID_TPMSealedData_ASN1, sizeof(OID_TPMSealedData_ASN1));
+	work += sizeof(OID_TPMSealedData_ASN1);
 
 	if (options->blobauth_len == 0) {
 		unsigned char bool[3], *w = bool;
Signature · 1 line
2.45.1
author maintainer
#1 lore raw
Quoted 11 lines · Jarkko Sakkinen

There's no reason to encode OID_TPMSealedData at run-time, as it never changes.

Replace it with an encoded u8-array, which has the same number of elements:

	67 81 05 0A 01 05

Include OBJECT IDENTIFIER (0x06) tag and length as the prologue so that the OID can be simply copied to the blob leading to:

Quoted 7 lines
	06 06 67 81 05 0A 01 05

Since this in stationary place in the buffer it is guaranteed to always fit and not further checks are required.

Does not really substitute distribution kernel testing, which is IMHO essential for something like TPM2 boot in systemd but for simple patches like this, the following does a trivial smoke test:

export LINUX_OVERRIDE_SRCDIR=<path to a kernel tree with a trusted keys patch> git clone https://gitlab.com/jarkkojs/linux-tpmdd-test.git
cd linux-tpmdd-test
cmake -Bbuild && make -Cbuild buildroot-prepare
make -Cbuild/buildroot/build build/buildroot/build/images/run-tests.sh

I'm planning to migrate at some point to systemd and make it appear more like distribution tho..

For recompiling just kernel only thing needed is:

rm -rf build/buildroot/build/build/linux-custom
make -Cbuild/buildroot/build

I've put this also to the MAINTAINERS entry of TPM driver although I use it also for keyrings etc. Also it is open for contributions via Gitlab merge requests (not requesting them per se but I'm open to such possibility).

BR, Jarkko

author maintainer
#2 lore raw
Quoted 37 lines · Jarkko Sakkinen
Quoted 11 lines · Jarkko Sakkinen

There's no reason to encode OID_TPMSealedData at run-time, as it never changes.

Replace it with an encoded u8-array, which has the same number of elements:

	67 81 05 0A 01 05

Include OBJECT IDENTIFIER (0x06) tag and length as the prologue so that the OID can be simply copied to the blob leading to:

Quoted 7 lines
	06 06 67 81 05 0A 01 05

Since this in stationary place in the buffer it is guaranteed to always fit and not further checks are required.

Does not really substitute distribution kernel testing, which is IMHO essential for something like TPM2 boot in systemd but for simple patches like this, the following does a trivial smoke test:

export LINUX_OVERRIDE_SRCDIR=<path to a kernel tree with a trusted keys patch> git clone https://gitlab.com/jarkkojs/linux-tpmdd-test.git
cd linux-tpmdd-test
cmake -Bbuild && make -Cbuild buildroot-prepare
make -Cbuild/buildroot/build build/buildroot/build/images/run-tests.sh

I'm planning to migrate at some point to systemd and make it appear more like distribution tho..

For recompiling just kernel only thing needed is:

Quoted 7 lines

rm -rf build/buildroot/build/build/linux-custom
make -Cbuild/buildroot/build

I've put this also to the MAINTAINERS entry of TPM driver although I use it also for keyrings etc. Also it is open for contributions via Gitlab merge requests (not requesting them per se but I'm open to such possibility).

Right and this is fully CI compatible with both Github and Gitlab with only a single tweak: BR2_PACKAGE_HOST_QEMU needs to be added to the qemu config. It is CI agnostic test environment to put short.

I've had this also deployed to the CI in the past. It can run both x86 and aarch64 based runners and even emulates hardware TPM in three different modes (TPM 1.2, TPM 2.0 FIFO, TPM 2.0 CRB).

I've been just wondering why we need kernel tree changes for Gitlab CI when you can pretty easily just bootstrap toolchain and qemu and call it a day (and with trimmed builds it is quite fast too).

BR, Jarkko