Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

security: keys: trusted: fix TPM2 authorizations

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=f572d396fae9206628714fb2ce00f72e94f2258fkeyhandle=81000001" @u

after we will accept both the old hex sha1 form as well as a new
directly supplied password:

keyctl add trusted kmk "new 32 blobauth=hello keyhandle=81000001" @u

Since a sha1 hex code must be exactly 40 bytes long and a direct
password must be 20 or less, we use the length as the discriminator
for which form is input.

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.

Fixes: 0fe5480303a1 ("keys, trusted: seal/unseal with TPM 2.0 chips")
Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Tested-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>

authored by

James Bottomley and committed by
Jarkko Sakkinen
de66514d 1c6476e9

+33 -10
+1
include/keys/trusted-type.h
··· 30 30 uint16_t keytype; 31 31 uint32_t keyhandle; 32 32 unsigned char keyauth[TPM_DIGEST_SIZE]; 33 + uint32_t blobauth_len; 33 34 unsigned char blobauth[TPM_DIGEST_SIZE]; 34 35 uint32_t pcrinfo_len; 35 36 unsigned char pcrinfo[MAX_PCRINFO_SIZE];
+26 -6
security/keys/trusted-keys/trusted_tpm1.c
··· 791 791 return -EINVAL; 792 792 break; 793 793 case Opt_blobauth: 794 - if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE) 795 - return -EINVAL; 796 - res = hex2bin(opt->blobauth, args[0].from, 797 - SHA1_DIGEST_SIZE); 798 - if (res < 0) 799 - return -EINVAL; 794 + /* 795 + * TPM 1.2 authorizations are sha1 hashes passed in as 796 + * hex strings. TPM 2.0 authorizations are simple 797 + * passwords (although it can take a hash as well) 798 + */ 799 + opt->blobauth_len = strlen(args[0].from); 800 + 801 + if (opt->blobauth_len == 2 * TPM_DIGEST_SIZE) { 802 + res = hex2bin(opt->blobauth, args[0].from, 803 + TPM_DIGEST_SIZE); 804 + if (res < 0) 805 + return -EINVAL; 806 + 807 + opt->blobauth_len = TPM_DIGEST_SIZE; 808 + break; 809 + } 810 + 811 + if (tpm2 && opt->blobauth_len <= sizeof(opt->blobauth)) { 812 + memcpy(opt->blobauth, args[0].from, 813 + opt->blobauth_len); 814 + break; 815 + } 816 + 817 + return -EINVAL; 818 + 800 819 break; 820 + 801 821 case Opt_migratable: 802 822 if (*args[0].from == '0') 803 823 pay->migratable = 0;
+6 -4
security/keys/trusted-keys/trusted_tpm2.c
··· 97 97 TPM_DIGEST_SIZE); 98 98 99 99 /* sensitive */ 100 - tpm_buf_append_u16(&buf, 4 + TPM_DIGEST_SIZE + payload->key_len + 1); 100 + tpm_buf_append_u16(&buf, 4 + options->blobauth_len + payload->key_len + 1); 101 101 102 - tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE); 103 - tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE); 102 + tpm_buf_append_u16(&buf, options->blobauth_len); 103 + if (options->blobauth_len) 104 + tpm_buf_append(&buf, options->blobauth, options->blobauth_len); 105 + 104 106 tpm_buf_append_u16(&buf, payload->key_len + 1); 105 107 tpm_buf_append(&buf, payload->key, payload->key_len); 106 108 tpm_buf_append_u8(&buf, payload->migratable); ··· 267 265 NULL /* nonce */, 0, 268 266 TPM2_SA_CONTINUE_SESSION, 269 267 options->blobauth /* hmac */, 270 - TPM_DIGEST_SIZE); 268 + options->blobauth_len); 271 269 272 270 rc = tpm_transmit_cmd(chip, &buf, 6, "unsealing"); 273 271 if (rc > 0)