Skip to content

Commit de66514

Browse files
jejbjarkkojs
authored andcommitted
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: 0fe5480 ("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>
1 parent 1c6476e commit de66514

3 files changed

Lines changed: 33 additions & 10 deletions

File tree

‎include/keys/trusted-type.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct trusted_key_options {
3030
uint16_t keytype;
3131
uint32_t keyhandle;
3232
unsigned char keyauth[TPM_DIGEST_SIZE];
33+
uint32_t blobauth_len;
3334
unsigned char blobauth[TPM_DIGEST_SIZE];
3435
uint32_t pcrinfo_len;
3536
unsigned char pcrinfo[MAX_PCRINFO_SIZE];

‎security/keys/trusted-keys/trusted_tpm1.c‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -791,13 +791,33 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
791791
return -EINVAL;
792792
break;
793793
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+
800819
break;
820+
801821
case Opt_migratable:
802822
if (*args[0].from == '0')
803823
pay->migratable = 0;

‎security/keys/trusted-keys/trusted_tpm2.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,12 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
9797
TPM_DIGEST_SIZE);
9898

9999
/* 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+
102+
tpm_buf_append_u16(&buf, options->blobauth_len);
103+
if (options->blobauth_len)
104+
tpm_buf_append(&buf, options->blobauth, options->blobauth_len);
101105

102-
tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE);
103-
tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE);
104106
tpm_buf_append_u16(&buf, payload->key_len + 1);
105107
tpm_buf_append(&buf, payload->key, payload->key_len);
106108
tpm_buf_append_u8(&buf, payload->migratable);
@@ -265,7 +267,7 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
265267
NULL /* nonce */, 0,
266268
TPM2_SA_CONTINUE_SESSION,
267269
options->blobauth /* hmac */,
268-
TPM_DIGEST_SIZE);
270+
options->blobauth_len);
269271

270272
rc = tpm_transmit_cmd(chip, &buf, 6, "unsealing");
271273
if (rc > 0)

0 commit comments

Comments
 (0)