Skip to contentSkip to conversation

KEYS: trusted: Use ASN.1 encoded OID

PATCH Superseded Jarkko Sakkinen 3 participants 7 messages May 23, 2024

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

PATCH v2 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 the encoded version, which has exactly the same size:

	67 81 05 0A 01 05

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

---
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 |  7 +-
 3 files changed, 4 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 +4 -3
index 8b7dd73d94c1..4a2b4ad5a913 100644
@@ -26,7 +26,8 @@ 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 };
+/* Encoded OID_TPMSealedData. */
+static 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 +52,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
maintainer
#1 lore raw
Quoted 9 lines · Jarkko Sakkinen

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

Replace it with the encoded version, which has exactly the same size:

	67 81 05 0A 01 05

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

This seems reasonable. We have a limited set of OIDs we can generate (currently 1). Better to store the BER-encoded form and copy that in rather than trying to turn a pretty-printed OID into the BER encoding unless we absolutely have to.

David

author maintainer
#2 lore raw
Quoted 9 lines · David Howells
Quoted 7 lines · Jarkko Sakkinen

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

Replace it with the encoded version, which has exactly the same size:

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

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

This seems reasonable. We have a limited set of OIDs we can generate (currently 1). Better to store the BER-encoded form and copy that in rather than trying to turn a pretty-printed OID into the BER encoding unless we absolutely have to.

Yup, I crafted a plan in response to James about possibility to generate all from a CSV file (oid_registry.gen.[sh] and oid_registry.h incldues oid_registry.gen.h for compat).

No bandwidth to work in it, but happy to review it.

Quoted 2 lines

David

BR, Jarkko

maintainer Reviewed-by on v2 1/1
#3 lore raw
In reply to 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 the encoded version, which has exactly the same size:

	67 81 05 0A 01 05

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

author maintainer
#4 lore raw
Quoted 7 lines · David Howells
Quoted 5 lines · Jarkko Sakkinen

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

Replace it with the encoded version, which has exactly the same size:

Quoted 8 lines
Quoted 6 lines
	67 81 05 0A 01 05

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

Thanks!

BR, Jarkko

#5 lore raw
In reply to Jarkko Sakkinen
Quoted 6 lines · Jarkko Sakkinen

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

Replace it with the encoded version, which has exactly the same size:

	67 81 05 0A 01 05

Is it the same size? It looks considerably smaller to me (6*4 bytes versus 8 bytes).

Quoted 2 lines

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

An "epilogue" occurs at the end, but it seems to be at the beginning here (that would be a "prologue").

security/keys/trusted-keys/trusted_tpm2.c static struct tpm2_hash tpm2_hash_map[] = { · line 30

Quoted 3 lines
security/keys/trusted-keys/trusted_tpm2.c +2 -1
-static u32 tpm2key_oid[] = { 2, 23, 133, 10, 1, 5 };
+/* Encoded OID_TPMSealedData. */
+static u8 OID_TPMSealedData_ASN1[] = {0x06, 0x06, 0x67, 0x81, 0x05, 0x0a, 0x01, 0x05};

I'd say that a comment of what it encodes would be good to have for context, but the source tree has OID_TPMSealedData in a header with the value in a comment there, so that seems good enough to me.

Quoted 1 line

as it never changes.

Should it, perhaps be const too?

--Ben

author maintainer
#6 lore raw
Quoted 10 lines · Ben Boeckel
Quoted 6 lines · Jarkko Sakkinen

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

Replace it with the encoded version, which has exactly the same size:

	67 81 05 0A 01 05

Is it the same size? It looks considerably smaller to me (6*4 bytes versus 8 bytes).

Not in that sense but in practice the old array stored byte values. Forgot for that reason that it was actually u32 array.

I can change it to "same number of elements".

Quoted 6 lines
Quoted 3 lines

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

An "epilogue" occurs at the end, but it seems to be at the beginning here (that would be a "prologue").

Yup, typo.

Quoted 7 lines

security/keys/trusted-keys/trusted_tpm2.c static struct tpm2_hash tpm2_hash_map[] = { · line 30

Quoted 3 lines
security/keys/trusted-keys/trusted_tpm2.c +2 -1
-static u32 tpm2key_oid[] = { 2, 23, 133, 10, 1, 5 };
+/* Encoded OID_TPMSealedData. */
+static u8 OID_TPMSealedData_ASN1[] = {0x06, 0x06, 0x67, 0x81, 0x05, 0x0a, 0x01, 0x05};

I'd say that a comment of what it encodes would be good to have for context, but the source tree has OID_TPMSealedData in a header with the value in a comment there, so that seems good enough to me.

OK. I named it this way to promote generation these from CSV file (see my other response to James).

Quoted 4 lines
Quoted 2 lines

as it never changes.

Should it, perhaps be const too?

Yup.

Quoted 2 lines

--Ben

Thanks for the remarks!

BR, Jarkko