TPM 2.0 trusted keys with attached policy
Subsystems (7)
James Bottomley
maintainerv4 cover TPM 2.0 trusted keys with attached policy
This is basically a respin to update the ASN.1 interface to pass pointers in and out instead of updating in place. The remainder of the patches haven't changed in substance, but have changed to support the new ASN.1 encoder API.
General Cover letter description:
I've changed the output format to use the standardised ASN.1 coding for TPM2 keys, meaning they should interoperate with userspace TPM2 key implementations. Apart from interoperability, another advantage of the existing key format is that it carries all parameters like parent and hash with it and it is capable of carrying policy directives in a way that mean they're tied permanently to the key (no having to try to remember what the policy was and reconstruct it from userspace). This actually allows us to support the TPM 1.2 commands like pcrinfo easily in 2.0.
Using the TPM2_PolicyPassword trick, this series now combines authorization with policy in a flexible way that would allow us to move to HMAC based authorizations later for TPM security. In getting passwords to work, I fixed the tpm2 password format in a separate patch. TPM 1.2 only allows fixed length authorizations, but TPM 2.0 allows for variable length passphrases, so we should support that in the keys.
James
---
James Bottomley (9):
lib: add asn.1 encoder
oid_registry: Add TCG defined OIDS for TPM keys
security: keys: trusted fix tpm2 authorizations
security: keys: trusted: use ASN.1 tpm2 key format for the blobs
security: keys: trusted: Make sealed key properly interoperable
security: keys: trusted: add PCR policy to TPM2 keys
security: keys: trusted: add ability to specify arbitrary policy
security: keys: trusted: implement counter/timer policy
security: keys: trusted: add password based authorizations to policy
keys
Documentation/security/keys/trusted-encrypted.rst | 64 +++- include/keys/trusted-type.h | 7 +- include/linux/asn1_encoder.h | 32 ++ include/linux/oid_registry.h | 5 + include/linux/tpm.h | 8 + lib/Makefile | 2 +- lib/asn1_encoder.c | 391 +++++++++++++++++++ security/keys/Kconfig | 2 + security/keys/trusted-keys/Makefile | 2 +- security/keys/trusted-keys/tpm2-policy.c | 433 ++++++++++++++++++++++ security/keys/trusted-keys/tpm2-policy.h | 31 ++ security/keys/trusted-keys/tpm2key.asn1 | 23 ++ security/keys/trusted-keys/trusted_tpm1.c | 46 ++- security/keys/trusted-keys/trusted_tpm2.c | 360 ++++++++++++++++-- 14 files changed, 1370 insertions(+), 36 deletions(-) create mode 100644 include/linux/asn1_encoder.h create mode 100644 lib/asn1_encoder.c create mode 100644 security/keys/trusted-keys/tpm2-policy.c create mode 100644 security/keys/trusted-keys/tpm2-policy.h create mode 100644 security/keys/trusted-keys/tpm2key.asn1
Signature · 1 line
2.16.4
James Bottomley
maintainerPATCH v4 1/9 lib: add asn.1 encoder
We have a need in the TPM trusted keys to return the ASN.1 form of the TPM key blob so it can be operated on by tools outside of the kernel. To do that, we have to be able to read and write the key format. The current ASN.1 decoder does fine for reading, but we need pieces of an ASN.1 encoder to return the key blob.
The current implementation only encodes the ASN.1 bits we actually need.
---
v2: updated API to use indefinite length, and made symbol exports gpl
v3: add data length error handling
v4: use end_data instead of data_len pointer
--- include/linux/asn1_encoder.h | 32 ++++ lib/Makefile | 2 +- lib/asn1_encoder.c | 391 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 424 insertions(+), 1 deletion(-) create mode 100644 include/linux/asn1_encoder.h create mode 100644 lib/asn1_encoder.c
include/linux/asn1_encoder.h new file +32 -0
@@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef _LINUX_ASN1_ENCODER_H +#define _LINUX_ASN1_ENCODER_H + +#include <linux/types.h> +#include <linux/asn1.h> +#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 * +asn1_encode_octet_string (unsigned char *data, +const unsigned char *end_data, +const unsigned char *string,u32 len); +unsigned char * +asn1_encode_sequence (unsigned char *data,const unsigned char *end_data, +const unsigned char *seq,int len); +unsigned char * +asn1_encode_boolean (unsigned char *data,const unsigned char *end_data, +bool val); + +#endif
lib/Makefile +1 -1
@@ -233,7 +233,7 @@ obj-$(CONFIG_INTERVAL_TREE_TEST) += interval_tree_test.o obj-$(CONFIG_PERCPU_TEST) += percpu_test.o -obj-$(CONFIG_ASN1) += asn1_decoder.o +obj-$(CONFIG_ASN1) += asn1_decoder.o asn1_encoder.o obj-$(CONFIG_FONT_SUPPORT) += fonts/
lib/asn1_encoder.c new file +391 -0
391 lines not on this page · raw message
Signature · 1 line
2.16.4
Re: [PATCH v4 1/9] lib: add asn.1 encoder
Quoted 9 lines · James Bottomley
We have a need in the TPM trusted keys to return the ASN.1 form of the TPM key blob so it can be operated on by tools outside of the kernel. To do that, we have to be able to read and write the key format. The current ASN.1 decoder does fine for reading, but we need pieces of an ASN.1 encoder to return the key blob.
The current implementation only encodes the ASN.1 bits we actually need.
Signed-off-by James Bottomley <[email protected]>
Please be explicit with the external tools. You must have specific tools in mind that you use. The abstraction level is unacceptable.
Quoted 70 lines
---
v2: updated API to use indefinite length, and made symbol exports gpl
v3: add data length error handling
v4: useend_datainstead ofdata_lenpointer--- include/linux/asn1_encoder.h | 32 ++++ lib/Makefile | 2 +- lib/asn1_encoder.c | 391 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 424 insertions(+), 1 deletion(-) create mode 100644 include/linux/asn1_encoder.h create mode 100644 lib/asn1_encoder.cinclude/linux/asn1_encoder.h new file +32
-0@@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef _LINUX_ASN1_ENCODER_H +#define _LINUX_ASN1_ENCODER_H + +#include <linux/types.h> +#include <linux/asn1.h> +#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 * +asn1_encode_octet_string (unsigned char *data, +const unsigned char *end_data, +const unsigned char *string,u32 len); +unsigned char * +asn1_encode_sequence (unsigned char *data,const unsigned char *end_data, +const unsigned char *seq,int len); +unsigned char * +asn1_encode_boolean (unsigned char *data,const unsigned char *end_data, +bool val); + +#endif lib/Makefile +1
-1@@ -233,7 +233,7 @@ obj-$(CONFIG_INTERVAL_TREE_TEST) += interval_tree_test.o obj-$(CONFIG_PERCPU_TEST) += percpu_test.o -obj-$(CONFIG_ASN1) += asn1_decoder.o +obj-$(CONFIG_ASN1) += asn1_decoder.o asn1_encoder.o obj-$(CONFIG_FONT_SUPPORT) += fonts/lib/asn1_encoder.c new file +0
-0
lib/asn1_encoder.c line 5
Quoted 5 lines
lib/asn1_encoder.c new file +5
-0@@ -0,0 +1,391 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Simple encoder primitives for ASN.1 BER/DER/CER +* +* Copyright (C) 2019 [email protected]
How much explicit copyright statements really matter for new code? This is something that bothers me when reviewing patches as GIT log itself should be able to acknowledge the copyright implicitly.
lib/asn1_encoder.c line 14
Quoted 9 lines
lib/asn1_encoder.c new file +9
-0+*/ + +#include <linux/asn1_encoder.h> +#include <linux/bug.h> +#include <linux/string.h> +#include <linux/module.h> + +/** +* asn1_encode_integer - encode positive integer to ASN.1
Parentheses missing [1].
lib/asn1_encoder.c line 17
Quoted 3 lines
lib/asn1_encoder.c new file +3
-0+* @data: pointer to the pointer to the data +* @end_data: end of data pointer, points one beyond last usable byte in @data +* @integer: integer to be encoded
Please align [1].
Quoted 6 lines
lib/asn1_encoder.c new file +6
-0+* +* This is a simplified encoder: it only currently does +* positive integers, but it should be simple enough to add the +* negative case if a use comes along. +*/ +unsigned char *
lib/asn1_encoder.c line 33
Quoted 10 lines
lib/asn1_encoder.c new file +10
-0+asn1_encode_integer(unsigned char *data, const unsigned char *end_data, + s64 integer) +{ + unsigned char *d = &data[2]; +int i; +bool found = false; +int data_len = end_data - data; + +if (WARN (integer <0 , +"BUG: integer encode only supports positive integers" ))
Please replace with WARN_ON(). Maintaining custom log messages here is senseless as this could only emit from a programming error.
lib/asn1_encoder.c line 42
Quoted 9 lines
lib/asn1_encoder.c new file +9
-0+return ERR_PTR (-EINVAL ); + +if (IS_ERR (data)) +return data; + +if (data_len <3 ) +return ERR_PTR (-EINVAL ); + + data_len -=2 ;
What is point of this (please add a comment to the source code since it was not obvious)? Also, why not have this substracted in the initialization.
lib/asn1_encoder.c line 44
Quoted 2 lines
lib/asn1_encoder.c new file +2
-0+ + data[0 ] =_tag (UNIV ,PRIM ,INT );
Empty line.
lib/asn1_encoder.c line 48
Quoted 4 lines
lib/asn1_encoder.c new file +4
-0+if (integer =0 ) { + *d++ =0 ; + goto out; + }
Empty line.
lib/asn1_encoder.c line 53
Quoted 5 lines
lib/asn1_encoder.c new file +5
-0+for (i =sizeof (integer); i >0 ; i--) { +int byte = integer >> (8 *(i-1 )); + +if (!found && byte =0 ) +continue ;
Empty line.
Quoted 4 lines
lib/asn1_encoder.c new file +4
-0+/* +* for a positive number the first byte must have bit +* 7 clear in two's complement (otherwise it's a +* negative number) so prepend a leading zero if
lib/asn1_encoder.c line 67
Quoted 10 lines
lib/asn1_encoder.c new file +10
-0+* that's not the case +*/ +if (!found && (byte &0x80 )) { +/* +* no check needed here, we already know we +* have len >= 1 +*/ + *d++ =0 ; + data_len--; + }
Empty line.
lib/asn1_encoder.c line 70
Quoted 3 lines
lib/asn1_encoder.c new file +3
-0+ found = true; +if (data_len =0 ) +return ERR_PTR (-EINVAL );
Why not:
int data_len = end_data - data - 2;
lib/asn1_encoder.c line 73
Quoted 3 lines
lib/asn1_encoder.c new file +3
-0+ *d++ = byte; + data_len--; + }
Empty line before the label.
Quoted 300 lines
lib/asn1_encoder.c new file +300
-0+ out: + data[1 ] = d - data -2 ; + +return d; +} +EXPORT_SYMBOL_GPL (asn1_encode_integer); + +/* calculate the base 128 digit values setting the top bit of the first octet */ +static int asn1_encode_oid_digit (unsigned char **_data,int *data_len,u32 oid) +{ +int start =7 +7 +7 +7 ; +unsigned char *data = *_data; +int ret =0 ; + +if (*data_len <1 ) +return -EINVAL ; + +/* quick case */ +if (oid =0 ) { + *data++ =0x80 ; + (*data_len)--; + goto out; + } + +while (oid >> start =0 ) + start -=7 ; + +while (start >0 && *data_len >0 ) { +u8 byte; + + byte = oid >> start; + oid = oid - (byte << start); + start -=7 ; + byte |=0x80 ; + *data++ = byte; + (*data_len)--; + } +if (*data_len >0 ) { + *data++ = oid; + (*data_len)--; + }else { + ret = -EINVAL ; + } + + out: + *_data = data; +return ret; +} + +/** +* asn1_encode_oid - encode an oid to ASN.1 +* @data: position to begin encoding at +* @end_data: end of data pointer, points one beyond last usable byte in @data +* @oid: array of oids +* @oid_len: length of oid array +* +* this encodes an OID up to ASN.1 when presented as an array of OID values +*/ +unsigned char * +asn1_encode_oid (unsigned char *data,const unsigned char *end_data, +u32 oid[],int oid_len) +{ +unsigned char *d = data +2 ; +int i, ret; +int data_len = end_data - data; + +if (WARN (oid_len <2 ,"OID must have at least two elements" )) +return ERR_PTR (-EINVAL ); +if (WARN (oid_len >32 ,"OID is too large" )) +return ERR_PTR (-EINVAL ); +if (IS_ERR (data)) +return data; +if (data_len <2 ) +return ERR_PTR (-EINVAL ); + + data[0 ] =_tag (UNIV ,PRIM ,OID ); + *d++ = oid[0 ] *40 + oid[1 ]; + data_len -=2 ; + ret =0 ; +for (i =2 ; i < oid_len; i++) { + ret =asn1_encode_oid_digit (&d, &data_len, oid[i]); +if (ret <0 ) +return ERR_PTR (ret); + } + data[1 ] = d - data -2 ; +return d; +} +EXPORT_SYMBOL_GPL (asn1_encode_oid); + +static int asn1_encode_length (unsigned char **data,int *data_len,int len) +{ +if (*data_len <1 ) +return -EINVAL ; +if (len <0 ) { + *((*data)++) =ASN1_INDEFINITE_LENGTH ; + (*data_len)--; +return 0 ; + } +if (len <=0x7f ) { + *((*data)++) = len; + (*data_len)--; +return 0 ; + } + +if (*data_len <2 ) +return -EINVAL ; +if (len <=0xff ) { + *((*data)++) =0x81 ; + *((*data)++) = len &0xff ; + *data_len -=2 ; +return 0 ; + } + +if (*data_len <3 ) +return -EINVAL ; +if (len <=0xffff ) { + *((*data)++) =0x82 ; + *((*data)++) = (len >>8 ) &0xff ; + *((*data)++) = len &0xff ; + *data_len -=3 ; +return 0 ; + } + +if (WARN (len >0xffffff ,"ASN.1 length can't be > 0xffffff" )) +return -EINVAL ; + +if (*data_len <4 ) +return -EINVAL ; + *((*data)++) =0x83 ; + *((*data)++) = (len >>16 ) &0xff ; + *((*data)++) = (len >>8 ) &0xff ; + *((*data)++) = len &0xff ; + *data_len -=4 ; + +return 0 ; +} + +/** +* asn1_encode_tag - add a tag for optional or explicit value +* @data: pointer to place tag at +* @end_data: end of data pointer, points one beyond last usable byte in @data +* @tag: tag to be placed +* @string: the data to be tagged +* @len: the length of the data to be tagged +* +* Note this currently only handles short form tags < 31. To encode +* in place pass a NULL @string and -1 for @len; all this will do is +* add an indefinite length tag and update the data pointer to the +* place where the tag contents should be placed. After the data is +* placed, repeat the prior statement but now with the known length. +* In order to avoid having to keep both before and after pointers, +* the repeat expects to be called with @data pointing to where the +* first encode placed it. +*/ +unsigned char * +asn1_encode_tag (unsigned char *data,const unsigned char *end_data, +u32 tag,const unsigned char *string,int len) +{ +int ret; +int data_len = end_data - data; + +if (WARN (tag >30 ,"ASN.1 tag can't be > 30" )) +return ERR_PTR (-EINVAL ); + +if (!string &&WARN (len >127 , +"BUG: recode tag is too big (>127)" )) +return ERR_PTR (-EINVAL ); +if (IS_ERR (data)) +return data; + +if (!string && len >0 ) { +/* +* we're recoding, so move back to the start of the +* tag and install a dummy length because the real +* data_len should be NULL +*/ + data -=2 ; + data_len =2 ; + } + +if (data_len <2 ) +return ERR_PTR (-EINVAL ); + + *(data++) =_tagn (CONT ,CONS , tag); + data_len--; + ret =asn1_encode_length (&data, &data_len, len); +if (ret <0 ) +return ERR_PTR (ret); +if (!string) +return data; +if (data_len < len) +return ERR_PTR (-EINVAL ); +memcpy (data, string, len); + data += len; + +return data; +} +EXPORT_SYMBOL_GPL (asn1_encode_tag); + +/** +* asn1_encode_octet_string - encode an ASN.1 OCTET STRING +* @data: pointer to encode at +* @end_data: end of data pointer, points one beyond last usable byte in @data +* @string: string to be encoded +* @len: length of string +* +* Note ASN.1 octet strings may contain zeros, so the length is obligatory. +*/ +unsigned char * +asn1_encode_octet_string (unsigned char *data, +const unsigned char *end_data, +const unsigned char *string,u32 len) +{ +int ret; +int data_len = end_data - data; + +if (IS_ERR (data)) +return data; + +if (data_len <2 ) +return ERR_PTR (-EINVAL ); + + *(data++) =_tag (UNIV ,PRIM ,OTS ); + data_len--; + ret =asn1_encode_length (&data, &data_len, len); +if (ret) +return ERR_PTR (ret); + +if (data_len < len) +return ERR_PTR (-EINVAL ); + +memcpy (data, string, len); + data += len; + +return data; +} +EXPORT_SYMBOL_GPL (asn1_encode_octet_string); + +/** +* asn1_encode_sequence - wrap a byte stream in an ASN.1 SEQUENCE +* @data: pointer to encode at +* @end_data: end of data pointer, points one beyond last usable byte in @data +* @seq: data to be encoded as a sequence +* @len: length of the data to be encoded as a sequence +* +* Fill in a sequence. To encode in place, pass NULL for @seq and -1 +* for @len; then call again once the length is known (still with NULL +* for @seq). In order to avoid having to keep both before and after +* pointers, the repeat expects to be called with @data pointing to +* where the first encode placed it. +*/ +unsigned char * +asn1_encode_sequence (unsigned char *data,const unsigned char *end_data, +const unsigned char *seq,int len) +{ +int ret; +int data_len = end_data - data; + +if (!seq &&WARN (len >127 , +"BUG: recode sequence is too big (>127)" )) +return ERR_PTR (-EINVAL ); +if (IS_ERR (data)) +return data; +if (!seq && len >=0 ) { +/* +* we're recoding, so move back to the start of the +* sequence and install a dummy length because the +* real length should be NULL +*/ + data -=2 ; + data_len =2 ; + } + +if (data_len <2 ) +return ERR_PTR (-EINVAL ); + + *(data++) =_tag (UNIV ,CONS ,SEQ ); + data_len--; + ret =asn1_encode_length (&data, &data_len, len); +if (ret) +return ERR_PTR (ret); +if (!seq) +return data; + +if (data_len < len) +return ERR_PTR (-EINVAL ); + +memcpy (data, seq, len); + data += len; + +return data; +} +EXPORT_SYMBOL_GPL (asn1_encode_sequence); + +/** +* asn1_encode_boolean - encode a boolean value to ASN.1 +* @data: pointer to encode at +* @end_data: end of data pointer, points one beyond last usable byte in @data +* @val: the boolean true/false value +*/
lib/asn1_encoder.c line 383
Quoted 10 lines
lib/asn1_encoder.c new file +10
-0+unsigned char * +asn1_encode_boolean (unsigned char *data,const unsigned char *end_data, +bool val) +{ +int data_len = end_data - data; + +if (IS_ERR (data)) +return data; +if (data_len <3 ) +return ERR_PTR (-EINVAL );
This patch is full of overly packed code. Please just make it more spacy and readable.
lib/asn1_encoder.c line 387
Quoted 4 lines
lib/asn1_encoder.c new file +4
-0+ *(data++) =_tag (UNIV ,PRIM ,BOOL ); + data_len--; +asn1_encode_length (&data, &data_len,1 ); + *(data++) = val ?1 :0 ;
Please do not use ternary operator but instead:
if (*data)
*(data++) = 1; else *(data++) = 0;
lib/asn1_encoder.c line 391
Quoted 4 lines
lib/asn1_encoder.c new file +4
-0+ +return data; +} +EXPORT_SYMBOL_GPL (asn1_encode_boolean);
[1] https://www.kernel.org/doc/Documentation/kernel-doc-nano-HOWTO.txt
/Jarkko
James Bottomley
maintainerRe: [PATCH v4 1/9] lib: add asn.1 encoder
Quoted 7 lines · Jarkko Sakkinen
Quoted 6 lines · James Bottomley
We have a need in the TPM trusted keys to return the ASN.1 form of the TPM key blob so it can be operated on by tools outside of the kernel. To do that, we have to be able to read and write the key format. The current ASN.1 decoder does fine for reading, but we need pieces of an ASN.1 encoder to return the key blob.
Quoted 8 lines
Quoted 5 lines
The current implementation only encodes the ASN.1 bits we actually need.
Signed-off-by James Bottomley <[email protected]om>
Please be explicit with the external tools. You must have specific tools in mind that you use. The abstraction level is unacceptable.
There are three current tools that use the ASN.1 format: the openssl_tpm2_engine:
https://git.kernel.org/pub/scm/linux/kernel/git/jejb/openssl_tpm2_engine.git/
Openconnect:
http://git.infradead.org/users/dwmw2/openconnect.git
And the Intel TSS implementation of the openssl engine:
https://github.com/tpm2-software/tpm2-tss-engine
[...]
Quoted 4 lines
Quoted 4 lines
lib/asn1_encoder.c new file +1
-0@@ -0,0 +1,391 @@ +// SPDX-License-Identifier: GPL-2.0-only
Quoted 8 lines
lib/asn1_encoder.c line 5
Quoted 4 lines
lib/asn1_encoder.c new file +4
-0+/* +* Simple encoder primitives for ASN.1 BER/DER/CER +* +* Copyright (C) 2019 [email protected] How much explicit copyright statements really matter for new code? This is something that bothers me when reviewing patches as GIT log itself should be able to acknowledge the copyright implicitly.
This is actually the recommended statutory form of the US Copyright Act:
https://www.copyright.gov/title17/92chap4.html
The Berne convention subsequently mandated that copyright should subsist without the notice being required (and this became the law in the US in 1989 meaning that for works after this date in the US, lack of copyright notice can't be used as evidence of no copyright in the file) but this is unevenly adhered to in the rest of the world, so the US Copyright office recommends the notice should still be present.
The McHardy cases showed us the difficulty of convincing courts to believe technology like git over simple statements in files, so it's still best practice for all files in the kernel to have a copyright notice just in case.
Quoted 12 lines
lib/asn1_encoder.c line 14
Quoted 9 lines
lib/asn1_encoder.c new file +9
-0+*/ + +#include <linux/asn1_encoder.h> +#include <linux/bug.h> +#include <linux/string.h> +#include <linux/module.h> + +/** +* asn1_encode_integer - encode positive integer to ASN.1 Parentheses missing [1].
Quoted 6 lines
Quoted 4 lines
lib/asn1_encoder.c new file +2
-0+* @data: pointer to the pointer to the data +* @end_data: end of data pointer, points one beyond last usable byte in @data
+ * @integer: integer to be encodedPlease align [1].
Will fix both.
Quoted 13 lines
Quoted 13 lines
+ *
+ * This is a simplified encoder: it only currently does
+ * positive integers, but it should be simple enough to add the
+ * negative case if a use comes along.
+ */lib/asn1_encoder.c new file +2
-0+unsigned char * +asn1_encode_integer(unsigned char *data, const unsigned char
*end_data,lib/asn1_encoder.c new file +5
-0+ s64 integer) +{ +unsigned char *d = &data[2 ]; +int i; +bool found = false;
Quoted 8 lines
Quoted 5 lines
lib/asn1_encoder.c new file +4
-0+int data_len = end_data - data; + + if (WARN(integer < 0, + "BUG: integer encode only supports positiveintegers"))
Please replace with
WARN_ON(). Maintaining custom log messages here is senseless as this could only emit from a programming error.
I've got to say, having tripped a few of these, that I do like the explicit message telling me why the warn on triggered and what I need to do about it. But I do admit it's only a minor inconvenience to trace the WARN_ON back throught the file.
Quoted 5 lines
Quoted 5 lines
lib/asn1_encoder.c new file +5
-0+return ERR_PTR (-EINVAL ); + +if (IS_ERR (data)) +return data; +
Quoted 8 lines
lib/asn1_encoder.c line 42
Quoted 4 lines
lib/asn1_encoder.c new file +4
-0+if (data_len <3 ) +return ERR_PTR (-EINVAL ); + + data_len -=2 ;What is point of this (please add a comment to the source code since it was not obvious)? Also, why not have this substracted in the initialization.
All ASN.1 elements begin with a tag and a length. An integer must also have a value, so it must be at least 3 bytes. The reason for subtracting 2 is that we insert the tag, save the length and begin coding the integer in the for loop.
If I start data_len = end_data - data - 2 I then have to explain that the if (data_len < 1) in fact means we need three bytes available instead of its being obvious.
Quoted 5 lines
lib/asn1_encoder.c
Quoted 3 lines
lib/asn1_encoder.c new file +2
-0+ + data[0 ] =_tag (UNIV ,PRIM ,INT );Empty line.
Yes, I'll try to do more spacing.
[...]
Quoted 10 lines
This patch is full of overly packed code. Please just make it more spacy and readable.
lib/asn1_encoder.c line 387
Quoted 4 lines
lib/asn1_encoder.c new file +4
-0+ *(data++) =_tag (UNIV ,PRIM ,BOOL ); + data_len--; +asn1_encode_length (&data, &data_len,1 ); + *(data++) = val ?1 :0 ;Please do not use ternary operator but instead:
Quoted 4 lines
if (*data)
*(data++) = 1; else *(data++) = 0;
If (val) but yes, I can do that.
James
James Bottomley
maintainerPATCH v4 2/9 oid_registry: Add TCG defined OIDS for TPM keys
In reply to James BottomleyThe TCG has defined an OID prefix "2.23.133.10.1" for the various TPM key uses. We've defined three of the available numbers:
2.23.133.10.1.3 TPM Loadable key. This is an asymmetric key (Usually
RSA2048 or Elliptic Curve) which can be imported by a TPM2_Load() operation.
2.23.133.10.1.4 TPM Importable Key. This is an asymmetric key (Usually
RSA2048 or Elliptic Curve) which can be imported by a TPM2_Import() operation.
Both loadable and importable keys are specific to a given TPM, the difference is that a loadable key is wrapped with the symmetric secret, so must have been created by the TPM itself. An importable key is wrapped with a DH shared secret, and may be created without access to the TPM provided you know the public part of the parent key.
2.23.133.10.1.5 TPM Sealed Data. This is a set of data (up to 128
bytes) which is sealed by the TPM. It usually represents a symmetric key and must be unsealed before use.
---
v3: correct OID_TPMImportableKey name
--- include/linux/oid_registry.h | 5 +++++ 1 file changed, 5 insertions(+)
include/linux/oid_registry.h +5 -0
@@ -107,6 +107,11 @@ enum OID { OID_gostTC26Sign512B,/* 1.2.643.7.1.2.1.2.2 */ OID_gostTC26Sign512C,/* 1.2.643.7.1.2.1.2.3 */ +/* TCG defined OIDS for TPM based keys */ + OID_TPMLoadableKey,/* 2.23.133.10.1.3 */ + OID_TPMImportableKey,/* 2.23.133.10.1.4 */ + OID_TPMSealedData,/* 2.23.133.10.1.5 */ + OID__NR };
Signature · 1 line
2.16.4
James Bottomley
maintainerPATCH v4 3/9 security: keys: trusted fix tpm2 authorizations
In reply to James BottomleyIn TPM 1.2 an authorization was a 20 byte number. The spec actually recommended you to hash variable length passwords and use the sha1 hash as the authorization. Because the spec doesn't require this hashing, the current authorization for trusted keys is a 40 digit hex number. For TPM 2.0 the spec allows the passing in of variable length passwords and passphrases directly, so we should allow that in trusted keys for ease of use. Update the 'blobauth' parameter to take this into account, so we can now use plain text passwords for the keys.
so before
keyctl add trusted kmk "new 32 blobauthõ72d396fae9206628714fb2ce00f72e94f2258f"
after:
keyctl add trusted kmk "new 32 blobauth=hello keyhandle�000001"
Note this is both and enhancement and a potential bug fix. The TPM 2.0 spec requires us to strip leading zeros, meaning empyty authorization is a zero length HMAC whereas we're currently passing in 20 bytes of zeros. A lot of TPMs simply accept this as OK, but the Microsoft TPM emulator rejects it with TPM_RC_BAD_AUTH, so this patch makes the Microsoft TPM emulator work with trusted keys.
--- include/keys/trusted-type.h | 1 + security/keys/trusted-keys/trusted_tpm1.c | 24 +++++++++++++++++++----- security/keys/trusted-keys/trusted_tpm2.c | 9 +++++---- 3 files changed, 25 insertions(+), 9 deletions(-)
include/keys/trusted-type.h +1 -0
@@ -30,6 +30,7 @@ struct trusted_key_options {uint16_t keytype;uint32_t keyhandle;unsigned char keyauth[TPM_DIGEST_SIZE ]; +uint32_t blobauth_len;unsigned char blobauth[TPM_DIGEST_SIZE ];uint32_t pcrinfo_len;unsigned char pcrinfo[MAX_PCRINFO_SIZE ];
security/keys/trusted-keys/trusted_tpm1.c +19 -5
@@ -781,12 +781,26 @@ static int getoptions(char *c, struct trusted_key_payload *pay,return -EINVAL ;break ;case Opt_blobauth: -if (strlen (args[0 ].from) !=2 *SHA1_DIGEST_SIZE ) -return -EINVAL ; - res =hex2bin (opt->blobauth, args[0 ].from, -SHA1_DIGEST_SIZE ); -if (res <0 ) +/* +* TPM 1.2 authorizations are sha1 hashes +* passed in as hex strings. TPM 2.0 +* authorizations are simple passwords +* (although it can take a hash as well) +*/ + opt->blobauth_len =strlen (args[0 ].from); +if (opt->blobauth_len =2 *TPM_DIGEST_SIZE ) { + res =hex2bin (opt->blobauth, args[0 ].from, +TPM_DIGEST_SIZE ); +if (res <0 ) +return -EINVAL ; + opt->blobauth_len =TPM_DIGEST_SIZE ; + }else if (tpm2 && + opt->blobauth_len <=sizeof (opt->blobauth)) { +memcpy (opt->blobauth, args[0 ].from, + opt->blobauth_len); + }else {return -EINVAL ; + }break ;case Opt_migratable:if (*args[0 ].from ='0' )
security/keys/trusted-keys/trusted_tpm2.c +5 -4
@@ -91,10 +91,11 @@ int tpm2_seal_trusted(struct tpm_chip *chip, TPM_DIGEST_SIZE);/* sensitive */ -tpm_buf_append_u16 (&buf,4 +TPM_DIGEST_SIZE + payload->key_len +1 ); +tpm_buf_append_u16 (&buf,4 + options->blobauth_len + payload->key_len +1 ); -tpm_buf_append_u16 (&buf,TPM_DIGEST_SIZE ); -tpm_buf_append (&buf, options->blobauth,TPM_DIGEST_SIZE ); +tpm_buf_append_u16 (&buf, options->blobauth_len); +if (options->blobauth_len) +tpm_buf_append (&buf, options->blobauth, options->blobauth_len);tpm_buf_append_u16 (&buf, payload->key_len +1 );tpm_buf_append (&buf, payload->key, payload->key_len);tpm_buf_append_u8 (&buf, payload->migratable);
@@ -258,7 +259,7 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,NULL /* nonce */ ,0 ,TPM2_SA_CONTINUE_SESSION , options->blobauth/* hmac */ , - TPM_DIGEST_SIZE); + options->blobauth_len); rc =tpm_send (chip, buf.data,tpm_buf_length (&buf));if (rc >0 )
Signature · 1 line
2.16.4
Re: [PATCH v4 3/9] security: keys: trusted fix tpm2 authorizations
Quoted 15 lines · James Bottomley
In TPM 1.2 an authorization was a 20 byte number. The spec actually recommended you to hash variable length passwords and use the sha1 hash as the authorization. Because the spec doesn't require this hashing, the current authorization for trusted keys is a 40 digit hex number. For TPM 2.0 the spec allows the passing in of variable length passwords and passphrases directly, so we should allow that in trusted keys for ease of use. Update the 'blobauth' parameter to take this into account, so we can now use plain text passwords for the keys.
so before
keyctl add trusted kmk "new 32 blobauthõ72d396fae9206628714fb2ce00f72e94f2258f"
after:
Quoted 8 lines
keyctl add trusted kmk "new 32 blobauth=hello keyhandle�000001"
Note this is both and enhancement and a potential bug fix. The TPM 2.0 spec requires us to strip leading zeros, meaning empyty authorization is a zero length HMAC whereas we're currently passing in 20 bytes of zeros. A lot of TPMs simply accept this as OK, but the Microsoft TPM emulator rejects it with
TPM_RC_BAD_AUTH, so this patch makes the Microsoft TPM emulator work with trusted keys.
Even if for good reasons, you should be explicit when you make an API change that is not backwards compatible.
Quoted 43 lines
Signed-off-by James Bottomley <[email protected]>--- include/keys/trusted-type.h | 1 + security/keys/trusted-keys/trusted_tpm1.c | 24 +++++++++++++++++++----- security/keys/trusted-keys/trusted_tpm2.c | 9 +++++---- 3 files changed, 25 insertions(+), 9 deletions(-)include/keys/trusted-type.h +1
-0@@ -30,6 +30,7 @@ struct trusted_key_options {uint16_t keytype;uint32_t keyhandle;unsigned char keyauth[TPM_DIGEST_SIZE ]; +uint32_t blobauth_len;unsigned char blobauth[TPM_DIGEST_SIZE ];uint32_t pcrinfo_len;unsigned char pcrinfo[MAX_PCRINFO_SIZE ];security/keys/trusted-keys/trusted_tpm1.c +10
-5@@ -781,12 +781,26 @@ static int getoptions(char *c, struct trusted_key_payload *pay,return -EINVAL ;break ;case Opt_blobauth: -if (strlen (args[0 ].from) !=2 *SHA1_DIGEST_SIZE ) -return -EINVAL ; - res =hex2bin (opt->blobauth, args[0 ].from, -SHA1_DIGEST_SIZE ); -if (res <0 ) +/* +* TPM 1.2 authorizations are sha1 hashes +* passed in as hex strings. TPM 2.0 +* authorizations are simple passwords +* (although it can take a hash as well) +*/ + opt->blobauth_len =strlen (args[0 ].from); +if (opt->blobauth_len =2 *TPM_DIGEST_SIZE ) { + res =hex2bin (opt->blobauth, args[0 ].from, +TPM_DIGEST_SIZE );
security/keys/trusted-keys/trusted_tpm1.c static int getoptions(char *c, struct trusted_key_payload *pay, · line 803
Quoted 10 lines
security/keys/trusted-keys/trusted_tpm1.c +9
-0+if (res <0 ) +return -EINVAL ; + opt->blobauth_len =TPM_DIGEST_SIZE ; + }else if (tpm2 && + opt->blobauth_len <=sizeof (opt->blobauth)) { +memcpy (opt->blobauth, args[0 ].from, + opt->blobauth_len); + }else {return -EINVAL ; + }
I don't see any reason of packing conditional statements like this even if it has been done in the past. Makes the code only exhausting to read.
I'd consider moving the whole code block to a static function of its own. Please also document to that function how the passphrase is handled in TPM 1.2 and TPM 2.0 (you can derive it from you commit message description) just to remind us how it is being handled when reviewing future patches.
Quoted 8 lines
security/keys/trusted-keys/trusted_tpm1.c +0
-0break ;case Opt_migratable:if (*args[0 ].from ='0' )security/keys/trusted-keys/trusted_tpm2.c +0
-0
security/keys/trusted-keys/trusted_tpm2.c int tpm2_seal_trusted(struct tpm_chip *chip, · line 96
Quoted 9 lines
security/keys/trusted-keys/trusted_tpm2.c +2
-3@@ -91,10 +91,11 @@ int tpm2_seal_trusted(struct tpm_chip *chip, TPM_DIGEST_SIZE);/* sensitive */ -tpm_buf_append_u16 (&buf,4 +TPM_DIGEST_SIZE + payload->key_len +1 ); +tpm_buf_append_u16 (&buf,4 + options->blobauth_len + payload->key_len +1 ); -tpm_buf_append_u16 (&buf,TPM_DIGEST_SIZE ); -tpm_buf_append (&buf, options->blobauth,TPM_DIGEST_SIZE ); +tpm_buf_append_u16 (&buf, options->blobauth_len);
Empty line.
security/keys/trusted-keys/trusted_tpm2.c int tpm2_seal_trusted(struct tpm_chip *chip, · line 98
Quoted 2 lines
security/keys/trusted-keys/trusted_tpm2.c +2
-0+if (options->blobauth_len) +tpm_buf_append (&buf, options->blobauth, options->blobauth_len);
Empty line.
security/keys/trusted-keys/trusted_tpm2.c static int tpm2_unseal_cmd(struct tpm_chip *chip, · line 265
Quoted 12 lines
security/keys/trusted-keys/trusted_tpm2.c +1
-1tpm_buf_append_u16 (&buf, payload->key_len +1 );tpm_buf_append (&buf, payload->key, payload->key_len);tpm_buf_append_u8 (&buf, payload->migratable);@@ -258,7 +259,7 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,NULL /* nonce */ ,0 ,TPM2_SA_CONTINUE_SESSION , options->blobauth/* hmac */ , - TPM_DIGEST_SIZE); + options->blobauth_len); rc =tpm_send (chip, buf.data,tpm_buf_length (&buf));if (rc >0 )
/Jarkko
Re: [PATCH v4 3/9] security: keys: trusted fix tpm2 authorizations
Quoted 25 lines · Jarkko Sakkinen
Quoted 15 lines · James Bottomley
In TPM 1.2 an authorization was a 20 byte number. The spec actually recommended you to hash variable length passwords and use the sha1 hash as the authorization. Because the spec doesn't require this hashing, the current authorization for trusted keys is a 40 digit hex number. For TPM 2.0 the spec allows the passing in of variable length passwords and passphrases directly, so we should allow that in trusted keys for ease of use. Update the 'blobauth' parameter to take this into account, so we can now use plain text passwords for the keys.
so before
keyctl add trusted kmk "new 32 blobauthõ72d396fae9206628714fb2ce00f72e94f2258f"
after:
Quoted 8 lines
keyctl add trusted kmk "new 32 blobauth=hello keyhandle�000001"
Note this is both and enhancement and a potential bug fix. The TPM 2.0 spec requires us to strip leading zeros, meaning empyty authorization is a zero length HMAC whereas we're currently passing in 20 bytes of zeros. A lot of TPMs simply accept this as OK, but the Microsoft TPM emulator rejects it with
TPM_RC_BAD_AUTH, so this patch makes the Microsoft TPM emulator work with trusted keys.
Quoted 2 lines
Even if for good reasons, you should be explicit when you make an API change that is not backwards compatible.
Also you have illformed abbrevation in your short summary. Should be TPM2, not tpm2.
/Jarkko
James Bottomley
maintainerRe: [PATCH v4 3/9] security: keys: trusted fix tpm2 authorizations
In reply to Jarkko SakkinenQuoted 28 lines · Jarkko Sakkinen
Quoted 18 lines · James Bottomley
In TPM 1.2 an authorization was a 20 byte number. The spec actually
recommended you to hash variable length passwords and use the sha1 hash as the authorization. Because the spec doesn't require this hashing, the current authorization for trusted keys is a 40 digit hex number. For TPM 2.0 the spec allows the passing in of variable length passwords and passphrases directly, so we should allow that in trusted keys for ease of use. Update the 'blobauth' parameter to take this into account, so we can now use plain text passwords for the keys.so before
keyctl add trusted kmk "new 32
blobauthõ72d396fae9206628714fb2ce00f72e94f2258f"after:
Quoted 8 lines
keyctl add trusted kmk "new 32 blobauth=hello keyhandle�000001"
Note this is both and enhancement and a potential bug fix. The TPM 2.0 spec requires us to strip leading zeros, meaning empyty authorization is a zero length HMAC whereas we're currently passing in 20 bytes of zeros. A lot of TPMs simply accept this as OK, but the Microsoft TPM emulator rejects it with
TPM_RC_BAD_AUTH, so this patch makes the Microsoft TPM emulator work with trusted keys.
Quoted 2 lines
Even if for good reasons, you should be explicit when you make an API change that is not backwards compatible.
This change should be backwards compatible. I've got a set of TPMs, one of which works both before and after and another which doesn't work before but does after, so all it does is increase the set of TPMs that work with the authorizations i.e. if the TPM worked before, it continues to work after.
I think what happens in the TPMs that work before is that they explicily remove trailing zeros and ones that don't work before don't.
Actually, the before form (20 hex bytes) still works in the after case ... I'll make that clear in the commit message.
James
Re: [PATCH v4 3/9] security: keys: trusted fix tpm2 authorizations
Quoted 9 lines · James Bottomley
Quoted 2 lines
Even if for good reasons, you should be explicit when you make an API change that is not backwards compatible.
This change should be backwards compatible. I've got a set of TPMs, one of which works both before and after and another which doesn't work before but does after, so all it does is increase the set of TPMs that work with the authorizations i.e. if the TPM worked before, it continues to work after.
Quoted 5 lines
I think what happens in the TPMs that work before is that they explicily remove trailing zeros and ones that don't work before don't.
Actually, the before form (20 hex bytes) still works in the after case ... I'll make that clear in the commit message.
OK, got it, thanks! Yeah, obviously would not hurt to be bit more explicit.
/Jarkko
James Bottomley
maintainerPATCH v4 4/9 security: keys: trusted: use ASN.1 tpm2 key format for the blobs
In reply to James BottomleyModify the tpm2 key format blob output to export and import in the ASN.1 form for tpm2 sealed object keys. For compatibility with prior trusted keys, the importer will also accept two tpm2b quantities representing the public and private parts of the key. However, the export via keyctl pipe will only output the ASN.1 format.
The benefit of the ASN.1 format is that it's a standard and thus the exported key can be used by userspace tools. The format includes policy specifications, thus it gets us out of having to construct policy handles in userspace and the format includes the parent meaning you don't have to keep passing it in each time.
This patch only implements basic handling for the ASN.1 format, so keys with passwords but no policy.
v2: Updated encode API, added length checks
--- security/keys/trusted-keys/Makefile | 2 +- security/keys/trusted-keys/tpm2key.asn1 | 23 ++++ security/keys/trusted-keys/trusted_tpm1.c | 2 +- security/keys/trusted-keys/trusted_tpm2.c | 187 +++++++++++++++++++++++++++++- 4 files changed, 207 insertions(+), 7 deletions(-) create mode 100644 security/keys/trusted-keys/tpm2key.asn1
security/keys/trusted-keys/Makefile +1 -1
@@ -5,4 +5,4 @@ obj-$(CONFIG_TRUSTED_KEYS) += trusted.o trusted-y += trusted_tpm1.o -trusted-y += trusted_tpm2.o +trusted-y += trusted_tpm2.o tpm2key.asn1.o
security/keys/trusted-keys/tpm2key.asn1 new file +23 -0
@@ -0,0 +1,23 @@ +--- +--- Note: This isn't quite the definition in the standard +--- However, the Linux asn.1 parser doesn't understand +--- [2] EXPLICIT SEQUENCE OF OPTIONAL +--- So there's an extra intermediate TPMPolicySequence +--- definition to work around this + +TPMKey ::= SEQUENCE { + type OBJECT IDENTIFIER ({tpmkey_type}), + emptyAuth [0] EXPLICIT BOOLEAN OPTIONAL, + policy [1] EXPLICIT TPMPolicySequence OPTIONAL, + secret [2] EXPLICIT OCTET STRING OPTIONAL, + parent INTEGER ({tpmkey_parent}), + pubkey OCTET STRING ({tpmkey_pub}), + privkey OCTET STRING ({tpmkey_priv}) + } + +TPMPolicySequence ::= SEQUENCE OF TPMPolicy + +TPMPolicy ::= SEQUENCE { + commandCode [0] EXPLICIT INTEGER, + commandPolicy [1] EXPLICIT OCTET STRING + }
security/keys/trusted-keys/trusted_tpm1.c +1 -1
@@ -1005,7 +1005,7 @@ static int trusted_instantiate(struct key *key, goto out; } -if (!options->keyhandle) { +if (!options->keyhandle && !tpm2) { ret = -EINVAL ; goto out; }
security/keys/trusted-keys/trusted_tpm2.c +181 -5
245 lines not on this page · raw message
Signature · 1 line
2.16.4
Re: [PATCH v4 4/9] security: keys: trusted: use ASN.1 tpm2 key format for the blobs
On Mon, 2019-12-30 at 09:37 -0800, James Bottomley wrote:
Again bad formatting in the short summary.
Quoted 5 lines
Modify the tpm2 key format blob output to export and import in the ASN.1 form for tpm2 sealed object keys. For compatibility with prior trusted keys, the importer will also accept two tpm2b quantities representing the public and private parts of the key. However, the export via keyctl pipe will only output the ASN.1 format.
I think this is fair enough when it comes to backwards compatibility.
What you should do with the earlier patch is to document in the commit message this framework for backwards compatibility. It is redundancy but makes the commit log easier to follow. E.g. tell in that commit message that ASN.1 loader will handle loading old blobs.
Quoted 5 lines
The benefit of the ASN.1 format is that it's a standard and thus the exported key can be used by userspace tools. The format includes policy specifications, thus it gets us out of having to construct policy handles in userspace and the format includes the parent meaning you don't have to keep passing it in each time.
Please be explicit with the tools you are referring. Point out what you are using if there is variable number of choices.
In high-level the code change itself looks good.
/Jarkko
James Bottomley
maintainerPATCH v4 5/9 security: keys: trusted: Make sealed key properly interoperable
In reply to James BottomleyThe current implementation appends a migratable flag to the end of a key, meaning the format isn't exactly interoperable because the using party needs to know to strip this extra byte. However, all other consumers of TPM sealed blobs expect the unseal to return exactly the key. Since TPM2 keys have a key property flag that corresponds to migratable, use that flag instead and make the actual key the only sealed quantity. This is secure because the key properties are bound to a hash in the private part, so if they're altered the key won't load.
Backwards compatibility is implemented by detecting whether we're loading a new format key or not and correctly setting migratable from the last byte of old format keys.
---
v2: added length checks to untrusted payload
--- include/keys/trusted-type.h | 1 + include/linux/tpm.h | 2 + security/keys/trusted-keys/trusted_tpm2.c | 69 ++++++++++++++++++++++--------- 3 files changed, 53 insertions(+), 19 deletions(-)
include/keys/trusted-type.h +1 -0
@@ -22,6 +22,7 @@ struct trusted_key_payload {unsigned int key_len;unsigned int blob_len;unsigned char migratable; +unsigned char old_format;unsigned char key[MAX_KEY_SIZE +1 ];unsigned char blob[MAX_BLOB_SIZE ]; };
include/linux/tpm.h +2 -0
@@ -297,6 +297,8 @@ struct tpm_buf { };enum tpm2_object_attributes { +TPM2_OA_FIXED_TPM =BIT (1 ), +TPM2_OA_FIXED_PARENT =BIT (4 ),TPM2_OA_USER_WITH_AUTH =BIT (6 ), };
security/keys/trusted-keys/trusted_tpm2.c +50 -19
125 lines not on this page · raw message
Signature · 1 line
2.16.4
Re: [PATCH v4 5/9] security: keys: trusted: Make sealed key properly interoperable
Quoted 10 lines · James Bottomley
The current implementation appends a migratable flag to the end of a key, meaning the format isn't exactly interoperable because the using party needs to know to strip this extra byte. However, all other consumers of TPM sealed blobs expect the unseal to return exactly the key. Since TPM2 keys have a key property flag that corresponds to migratable, use that flag instead and make the actual key the only sealed quantity. This is secure because the key properties are bound to a hash in the private part, so if they're altered the key won't load.
Quoted 5 lines
Backwards compatibility is implemented by detecting whether we're loading a new format key or not and correctly setting migratable from the last byte of old format keys.
Signed-off-by James Bottomley <[email protected]>
I'll stop review here as I'm now quite confident that in high-level this going to right direction.
For remaining patches:
- Be more explict with the tools. That will also give a framework to easily test the patches.
- Same remarks for the code formatting as I gave to earlier.
/Jarkko
James Bottomley
maintainerPATCH v4 6/9 security: keys: trusted: add PCR policy to TPM2 keys
In reply to James BottomleyThis commit adds the ability to specify a PCR lock policy to TPM2 keys. There is a complexity in that the creator of the key must chose either to use a PCR lock policy or to use authentication. At the current time they can't use both due to a complexity with the way authentication works when policy registers are in use. The way to construct a pcrinfo statement for a key is simply to use the TPMS_PCR_SELECT structure to specify the PCRs and follow this by a hash of all their values in order of ascending PCR number.
For simplicity, we require the policy name hash and the hash used for the PCRs to be the same. Thus to construct a policy around the value of the resettable PCR 16 using the sha1 bank, first reset the pcr to zero giving a hash of all zeros as:
6768033e216468247bd031a0a2d9876d79818f8f
Then the TPMS_PCR_SELECT value for PCR 16 is
03000001
So create a new 32 byte key with a policy policy locking the key to this value of PCR 16 with a parent key of 81000001 would be:
keyctl new 32 keyhandle=0x81000001 hash=sha1 pcrinfo0000016768033e216468247bd 031a0a2d9876d79818f8f" @u
---
v2: fix for new ASN.1 API eliminating hack in place and check lengths
--- Documentation/security/keys/trusted-encrypted.rst | 19 +- include/keys/trusted-type.h | 5 +- include/linux/tpm.h | 5 + security/keys/Kconfig | 2 + security/keys/trusted-keys/Makefile | 2 +- security/keys/trusted-keys/tpm2-policy.c | 354 ++++++++++++++++++++++ security/keys/trusted-keys/tpm2-policy.h | 30 ++ security/keys/trusted-keys/tpm2key.asn1 | 4 +- security/keys/trusted-keys/trusted_tpm1.c | 7 +- security/keys/trusted-keys/trusted_tpm2.c | 89 +++++- 10 files changed, 501 insertions(+), 16 deletions(-) create mode 100644 security/keys/trusted-keys/tpm2-policy.c create mode 100644 security/keys/trusted-keys/tpm2-policy.h
Documentation/security/keys/trusted-encrypted.rst +18 -1
@@ -60,7 +60,10 @@ Usage:: (40 ascii zeros) blobauth= ascii hex auth for sealed data default 0x00... (40 ascii zeros) - pcrinfo= ascii hex of PCR_INFO or PCR_INFO_LONG (no default) + pcrinfo= ascii hex of PCR_INFO or PCR_INFO_LONG (no + default) on TPM 1.2 and a TPMS_PCR_SELECTION + coupled with a hash of all the selected PCRs on + TPM 2.0 using the selected hash. pcrlock= pcr number to be extended to "lock" blob migratable= 0|1 indicating permission to reseal to new PCR values, default 1 (resealing allowed)
@@ -151,6 +154,20 @@ Load a trusted key from the saved blob:: f1f8fff03ad0acb083725535636addb08d73dedb9832da198081e5deae84bfaf0409c22b e4a8aea2b607ec96931e6f4d4fe563ba +Create a trusted key on TPM 2.0 using an all zero value of PCR16 and +using the NV storage root 81000001 as the parent:: + + $ keyctl add trusted kmk "new 32 keyhandle=0x81000001 hash=sha1 pcrinfo0000016768033e216468247bd031a0a2d9876d79818f8f" @u + +Note the TPMS_PCR_SELECT value for PCR 16 is 03000001 because all +current TPMs have 24 PCRs, so the initial 03 says there are three +following bytes of selection and then because the bytes are big +endian, 16 is bit zero of byte 2. the hash is the sha1 sum of all +zeros (the value of PCR 16):: + + $ dd if=/dev/zero bs=1 count 2>/dev/null|sha1sum + 6768033e216468247bd031a0a2d9876d79818f8f + Reseal a trusted key under new pcr values:: $ keyctl update 268728824 "update pcrinfo=`cat pcr.blob`"
include/keys/trusted-type.h +4 -1
@@ -14,9 +14,11 @@#define MIN_KEY_SIZE 32#define MAX_KEY_SIZE 128#define MAX_BLOB_SIZE 512 -#define MAX_PCRINFO_SIZE 64 +#define MAX_PCRINFO_SIZE 128#define MAX_DIGEST_SIZE 64 +#define TPM2_MAX_POLICIES 16 +struct trusted_key_payload {struct rcu_head rcu;unsigned int key_len;
@@ -25,6 +27,7 @@ struct trusted_key_payload {unsigned char old_format;unsigned char key[MAX_KEY_SIZE +1 ];unsigned char blob[MAX_BLOB_SIZE ]; +struct tpm2_policies *policies; };struct trusted_key_options {
include/linux/tpm.h +5 -0
@@ -222,10 +222,14 @@ enum tpm2_command_codes {TPM2_CC_CONTEXT_LOAD =0x0161 ,TPM2_CC_CONTEXT_SAVE =0x0162 ,TPM2_CC_FLUSH_CONTEXT =0x0165 , +TPM2_CC_POLICY_AUTHVALUE =0x016B , +TPM2_CC_POLICY_COUNTER_TIMER =0x016D , +TPM2_CC_START_AUTH_SESS =0x0176 ,TPM2_CC_VERIFY_SIGNATURE =0x0177 ,TPM2_CC_GET_CAPABILITY =0x017A ,TPM2_CC_GET_RANDOM =0x017B ,TPM2_CC_PCR_READ =0x017E , +TPM2_CC_POLICY_PCR =0x017F ,TPM2_CC_PCR_EXTEND =0x0182 ,TPM2_CC_EVENT_SEQUENCE_COMPLETE =0x0185 , TPM2_CC_HASH_SEQUENCE_START = 0x0186,
@@ -234,6 +238,7 @@ enum tpm2_command_codes { };enum tpm2_permanent_handles { +TPM2_RH_NULL =0x40000007 ,TPM2_RS_PW =0x40000009 , };
security/keys/Kconfig +2 -0
@@ -80,6 +80,8 @@ config TRUSTED_KEYS select CRYPTO select CRYPTO_HMAC select CRYPTO_SHA1 + select CRYPTO_SHA256 + select CRYPTO_SHA512 select CRYPTO_HASH_INFO help This option provides support for creating, sealing, and unsealing
security/keys/trusted-keys/Makefile +1 -1
@@ -5,4 +5,4 @@ obj-$(CONFIG_TRUSTED_KEYS) += trusted.o trusted-y += trusted_tpm1.o -trusted-y += trusted_tpm2.o tpm2key.asn1.o +trusted-y += trusted_tpm2.o tpm2key.asn1.o tpm2-policy.o
security/keys/trusted-keys/tpm2-policy.c new file +354 -0
354 lines not on this page · raw message
security/keys/trusted-keys/tpm2-policy.h new file +30 -0
@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +struct tpm2key_context { +u32 parent; +const u8 *pub; +u32 pub_len; +const u8 *priv; +u32 priv_len; +const u8 *policies[TPM2_MAX_POLICIES ]; +u32 policy_code[TPM2_MAX_POLICIES ]; +u16 policy_len[TPM2_MAX_POLICIES ]; +u8 policy_count; +}; + +struct tpm2_policies { +u32 code[TPM2_MAX_POLICIES ]; +u8 *policies[TPM2_MAX_POLICIES ]; +u16 len[TPM2_MAX_POLICIES ]; +u8 count; +u16 hash; +u16 hash_size; +}; + +int tpmkey_policy_process (struct tpm2key_context *ctx, +struct trusted_key_payload *payload); +int tpm2_generate_policy_digest (struct tpm2_policies *pols,u32 hash, +u8 *policydigest,u32 *plen); +int tpm2_encode_policy (struct tpm2_policies *pols,u8 **data,u32 *len); +int tpm2_get_policy_session (struct tpm_chip *chip,struct tpm2_policies *pols, +u32 *handle);
security/keys/trusted-keys/tpm2key.asn1 +2 -2
@@ -18,6 +18,6 @@ TPMKey ::= SEQUENCE { TPMPolicySequence ::= SEQUENCE OF TPMPolicy TPMPolicy ::= SEQUENCE { - commandCode [0] EXPLICIT INTEGER, - commandPolicy [1] EXPLICIT OCTET STRING + commandCode [0] EXPLICIT INTEGER ({tpmkey_code}), + commandPolicy [1] EXPLICIT OCTET STRING ({tpmkey_policy}) }
security/keys/trusted-keys/trusted_tpm1.c +6 -1
@@ -1059,6 +1059,7 @@ static void trusted_rcu_free(struct rcu_head *rcu)struct trusted_key_payload *p; p = container_of(rcu, struct trusted_key_payload, rcu); +kzfree (p->policies);kzfree (p); }
@@ -1178,7 +1179,11 @@ static long trusted_read(const struct key *key, char __user *buffer,*/ static void trusted_destroy (struct key *key) { -kzfree (key->payload.data[0 ]); +struct trusted_key_payload *p; + + p = key->payload.data[0 ]; +kzfree (p->policies); +kzfree (p); }struct key_type key_type_trusted = {
security/keys/trusted-keys/trusted_tpm2.c +79 -10
144 lines not on this page · raw message
Signature · 1 line
2.16.4
James Bottomley
maintainerPATCH v4 7/9 security: keys: trusted: add ability to specify arbitrary policy
In reply to James BottomleyThis patch adds a policy= argument to key creation. The policy is the standard tss policymaker format and each separate policy line must have a newline after it.
Thus to construct a policy requiring authorized value and pcr 16 locking using a sha256 hash, the policy (policy.txt) file would be two lines:
0000017F00000001000B03000001303095B49BE85E381E5B20E557E46363EF55B0F43B132C2D8E3DE9AC436656F2 0000016b
This can be inserted into the key with
keyctl add trusted kmk "new 32 policy=cat policy.txt keyhandle=0x81000001 hash=sha256" @u
Note that although a few policies work like this, most require special handling which must be added to the kernel policy construction routine.
--- Documentation/security/keys/trusted-encrypted.rst | 16 ++++++++ security/keys/trusted-keys/tpm2-policy.c | 46 +++++++++++++++++++++++ security/keys/trusted-keys/tpm2-policy.h | 1 + security/keys/trusted-keys/trusted_tpm1.c | 13 +++++++ 4 files changed, 76 insertions(+)
Documentation/security/keys/trusted-encrypted.rst +16 -0
@@ -76,6 +76,9 @@ Usage:: policyhandle= handle to an authorization policy session that defines the same policy and with the same hash algorithm as was used to seal the key. + policy= specify an arbitrary set of policies. These must + be in policymaker format with each separate + policy line newline terminated. "keyctl print" returns an ascii hex copy of the sealed key, which is in standard TPM_STORED_DATA format. The key length for new keys are always in bytes.
@@ -168,6 +171,19 @@ zeros (the value of PCR 16):: $ dd if=/dev/zero bs=1 count 2>/dev/null|sha1sum 6768033e216468247bd031a0a2d9876d79818f8f +You can also specify arbitrary policy in policymaker format, so a two +value policy (the pcr example above and authvalue) would look like +this in policymaker format:: + + 0000017F000000010004030000016768033e216468247bd031a0a2d9876d79818f8f + 0000016b + +This can be placed in a file (say policy.txt) and then added to the key as:: + + $ keyctl add trusted kmk "new 32 keyhandle=0x81000001 hash=sha1 policy=`cat policy.txt`" @u + +The newlines in the file policy.txt will be automatically processed. + Reseal a trusted key under new pcr values:: $ keyctl update 268728824 "update pcrinfo=`cat pcr.blob`"
security/keys/trusted-keys/tpm2-policy.c +46 -0
49 lines not on this page · raw message
security/keys/trusted-keys/tpm2-policy.h +1 -0
@@ -28,3 +28,4 @@ int tpm2_generate_policy_digest(struct tpm2_policies *pols, u32 hash,int tpm2_encode_policy (struct tpm2_policies *pols,u8 **data,u32 *len);int tpm2_get_policy_session (struct tpm_chip *chip,struct tpm2_policies *pols,u32 *handle); +int tpm2_parse_policies (struct tpm2_policies **ppols,char *str);
security/keys/trusted-keys/trusted_tpm1.c +13 -0
@@ -29,6 +29,8 @@#include <keys/trusted_tpm.h> +#include "tpm2-policy.h" +static const char hmac_alg[] ="hmac(sha1)" ;static const char hash_alg[] ="sha1" ;static struct tpm_chip *chip;
@@ -709,6 +711,7 @@ enum { Opt_hash, Opt_policydigest, Opt_policyhandle, + Opt_policy, }; static const match_table_t key_tokens = {
@@ -724,6 +727,7 @@ static const match_table_t key_tokens = { {Opt_hash, "hash=%s"}, {Opt_policydigest, "policydigest=%s"}, {Opt_policyhandle, "policyhandle=%s"}, + {Opt_policy, "policy=%s"}, {Opt_err, NULL} };
@@ -848,6 +852,15 @@ static int getoptions(char *c, struct trusted_key_payload *pay,return -EINVAL ; opt->policyhandle = handle;break ; +case Opt_policy: +if (pay->policies) +return -EINVAL ; +if (!tpm2) +return -EINVAL ; + res =tpm2_parse_policies (&pay->policies, args[0 ].from); +if (res) +return res; +break ;default :return -EINVAL ; }
Signature · 1 line
2.16.4
James Bottomley
maintainerPATCH v4 8/9 security: keys: trusted: implement counter/timer policy
In reply to James BottomleyThis is actually a generic policy allowing a range of comparisons against any value set in the TPM Clock, which includes things like the reset count, a monotonic millisecond count and the restart count. The most useful comparison is against the millisecond count for expiring keys. However, you have to remember that currently Linux doesn't try to sync the epoch timer with the TPM, so the expiration is actually measured in how long the TPM itself has been powered on ... the TPM timer doesn't count while the system is powered down. The millisecond counter is a u64 quantity found at offset 8 in the timer structure, and the <= comparision operand is 9, so a policy set to expire after the TPM has been up for 100 seconds would look like
0000016d00000000000f424000080009
Where 0x16d is the counter timer policy code and 0xf4240 is 100 000 in hex.
--- Documentation/security/keys/trusted-encrypted.rst | 29 +++++++++++++++++++++++ security/keys/trusted-keys/tpm2-policy.c | 19 +++++++++++++++ 2 files changed, 48 insertions(+)
Documentation/security/keys/trusted-encrypted.rst +29 -0
@@ -241,3 +241,32 @@ about the usage can be found in the file Another new format 'enc32' has been defined in order to support encrypted keys with payload size of 32 bytes. This will initially be used for nvdimm security but may expand to other usages that require 32 bytes payload. + +Appendix +-------- + +TPM 2.0 Policies +---------------- + +The current TPM supports PCR lock policies as documented above and +CounterTimer policies which can be used to create expiring keys. One +caveat with expiring keys is that the TPM millisecond counter does not +update while a system is powered off and Linux does not sync the TPM +millisecond count with its internal clock, so the best you can expire +in is in terms of how long any given TPM has been powered on. (FIXME: +Linux should simply update the millisecond clock to the current number +of seconds past the epoch on boot). + +A CounterTimer policy is expressed in terms of length and offset +against the TPM clock structure (TPMS_TIME_INFO), which looks like the +packed structure:: + + struct tpms_time_info { + u64 uptime; /* time in ms since last start or reset */ + u64 clock; /* cumulative uptime in ms */ + u32 resetcount; /* numer of times the TPM has been reset */ + u32 restartcount; /* number of times the TPM has been restarted */ + u8 safe /* time was safely loaded from NVRam */ + }; + +The usual comparison for expiring keys is against clock, at offset 8.
security/keys/trusted-keys/tpm2-policy.c +19 -0
@@ -336,6 +336,25 @@ int tpm2_get_policy_session(struct tpm_chip *chip, struct tpm2_policies *pols,tpm_buf_append (&buf, pols->policies[i], pols->len[i] - pols->hash_size);break ; +case TPM2_CC_POLICY_COUNTER_TIMER : { +/* +* the format of this is the last two u16 +* quantities are the offset and operation +* respectively. The rest is operandB which +* must be zero padded in a hash digest +*/ +u16 opb_len = pols->len[i] -4 ; + +if (opb_len > pols->hash_size) +return -EINVAL ; + +tpm_buf_append_u16 (&buf, opb_len); +tpm_buf_append (&buf, pols->policies[i], opb_len); +/* offset and operand*/ +tpm_buf_append (&buf, pols->policies[i] + opb_len,4 ); + failure ="Counter Timer" ; +break ; + }default : failure ="unknown policy" ;break ;
Signature · 1 line
2.16.4
James Bottomley
maintainerPATCH v4 9/9 security: keys: trusted: add password based authorizations to policy keys
In reply to James BottomleyTPM 2.0 has a trick where you can turn off the usual HMAC password session requirement using TPM2_PolicyPassword, so everywhere we see a TPM2_PolicyAuthValue (which does require HMAC password), we replace with the TPM2_PolicyPassword command instead. This allows us to use passwords with TPM 2.0 trusted keys that also have a policy.
--- include/linux/tpm.h | 1 + security/keys/trusted-keys/tpm2-policy.c | 16 ++++++++++++++- security/keys/trusted-keys/trusted_tpm2.c | 34 ++++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 2 deletions(-)
include/linux/tpm.h +1 -0
@@ -233,6 +233,7 @@ enum tpm2_command_codes {TPM2_CC_PCR_EXTEND =0x0182 ,TPM2_CC_EVENT_SEQUENCE_COMPLETE =0x0185 ,TPM2_CC_HASH_SEQUENCE_START =0x0186 , +TPM2_CC_POLICY_PASSWORD =0x018c ,TPM2_CC_CREATE_LOADED =0x0191 , TPM2_CC_LAST = 0x0193, /* Spec 1.36 */ };
security/keys/trusted-keys/tpm2-policy.c +15 -1
@@ -193,7 +193,8 @@ int tpm2_generate_policy_digest(struct tpm2_policies *pols, policy = digest; len = *plen; } -crypto_shash_update (sdesc, policy, len); +if (len) +crypto_shash_update (sdesc, policy, len);/* now output the intermediate to the policydigest */ crypto_shash_final (sdesc, policydigest);
@@ -316,6 +317,16 @@ int tpm2_get_policy_session(struct tpm_chip *chip, struct tpm2_policies *pols,u32 cmd = pols->code[i];struct tpm_buf buf; +if (cmd =TPM2_CC_POLICY_AUTHVALUE ) +/* +* both PolicyAuthValue and PolicyPassword +* hash to the same thing, but one triggers +* HMAC authentication and the other simple +* authentication. Since we have no HMAC +* code, we're choosing the simple +*/ + cmd =TPM2_CC_POLICY_PASSWORD ; + rc =tpm_buf_init (&buf,TPM2_ST_NO_SESSIONS , cmd);if (rc)return rc;
@@ -357,6 +368,9 @@ int tpm2_get_policy_session(struct tpm_chip *chip, struct tpm2_policies *pols, }default : failure ="unknown policy" ; +if (pols->len[i]) +tpm_buf_append (&buf, pols->policies[i], + pols->len[i]);break ; } rc =tpm_send (chip, buf.data,tpm_buf_length (&buf));
security/keys/trusted-keys/trusted_tpm2.c +33 -1
@@ -244,6 +244,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,u32 flags;int i;int rc; +static const int POLICY_SIZE =2 *PAGE_SIZE ;for (i =0 ; i <ARRAY_SIZE (tpm2_hash_map); i++) {if (options->hash = tpm2_hash_map[i].crypto_id) {
@@ -264,7 +265,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,/* 4 array len, 2 hash alg */ const int len =4 +2 + options->pcrinfo_len; - pols =kmalloc (sizeof (*pols) + len,GFP_KERNEL ); + pols =kmalloc (POLICY_SIZE ,GFP_KERNEL );if (!pols)return -ENOMEM ;
@@ -285,6 +286,37 @@ int tpm2_seal_trusted(struct tpm_chip *chip,return -EINVAL ; } +/* +* if we already have a policy, we have to add authorization +* to it. If we don't, we can simply follow the usual +* non-policy route. +*/ +if (options->blobauth_len !=0 && payload->policies) { +struct tpm2_policies *pols; +static u8 *scratch; +int i; +bool found = false; + + pols = payload->policies; + +/* make sure it's not already in policy */ +for (i =0 ; i < pols->count; i++) { +if (pols->code[i] =TPM2_CC_POLICY_AUTHVALUE ) { + found = true; +break ; + } + } + +if (!found) { + i = pols->count++; + scratch = pols->policies[i -1 ] + pols->len[i -1 ]; +/* the TPM2_PolicyPassword command has no payload */ + pols->policies[i] = scratch; + pols->len[i] =0 ; + pols->code[i] =TPM2_CC_POLICY_AUTHVALUE ; + } + } +if (payload->policies) { rc = tpm2_generate_policy_digest(payload->policies, options->hash,
Signature · 1 line
2.16.4
Quoted 25 lines · James Bottomley
This is basically a respin to update the ASN.1 interface to pass pointers in and out instead of updating in place. The remainder of the patches haven't changed in substance, but have changed to support the new ASN.1 encoder API.
General Cover letter description:
I've changed the output format to use the standardised ASN.1 coding for TPM2 keys, meaning they should interoperate with userspace TPM2 key implementations. Apart from interoperability, another advantage of the existing key format is that it carries all parameters like parent and hash with it and it is capable of carrying policy directives in a way that mean they're tied permanently to the key (no having to try to remember what the policy was and reconstruct it from userspace). This actually allows us to support the TPM 1.2 commands like pcrinfo easily in 2.0.
Using the
TPM2_PolicyPasswordtrick, this series now combines authorization with policy in a flexible way that would allow us to move to HMAC based authorizations later for TPM security. In getting passwords to work, I fixed the tpm2 password format in a separate patch. TPM 1.2 only allows fixed length authorizations, but TPM 2.0 allows for variable length passphrases, so we should support that in the keys.
Quoted 1 line
James
I'll finally go deep with this as soon as we land the fixes for
https://bugzilla.kernel.org/show_bug.cgi?id 5935.
I'm sorry for ignorance but there's been multiple factors that have delayed the review (the bug mentioned, kind of sudden steep ramp up to keyring maintenance as David had to focus on other things, SGX upstreaming and generally time seems to dissappear somewhere towards the end of the year).
This week is a bit catching up but I'm sure that next week I have space to give the focus the patch set requires (and deserves).
/Jarkko