On 9 December 2019 18:03:11 GMT, James Bottomley <James.Bottomley@xxxxxxxxxxxxxxxxxxxxx> wrote: >On Mon, 2019-12-09 at 10:18 +0000, David Woodhouse wrote: >> On Sat, 2019-12-07 at 21:12 -0800, James Bottomley wrote: >> > This 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 >> > pcrinfo=030000016768033e216468247bd031a0a2d9876d79818f8f" @u >> >> OK... but I've love to see a more formal definition of this binary >> format, as part of the "standard" we allegedly have for the overall >> ASN.1 representation. > >It's actually defined in the TPM2 command manual ... it's basically the >policy commands you send to the TPM ordered so they can be directly >hashed. > >However, I agree a standards definition would be good. This format >doesn't support TPM2_PolicyOr directly (and the command manual is >silent on how it should be supported), so that's going to have to be >defined in the standard anyway. > >[...] >> > +int tpm2_encode_policy(struct tpm2_policies *pols, u8 **data, u32 >> > *len) >> > +{ >> > + u8 *buf = kmalloc(2 * PAGE_SIZE, GFP_KERNEL); >> > + u8 *work = buf + PAGE_SIZE, *ptr; >> > + int i; >> > + >> > + if (!buf) >> > + return -ENOMEM; >> > + >> > + for (i = 0; i < pols->count; i++) { >> > + u8 *seq_len, *tag_len; >> > + u32 cmd = pols->code[i]; >> > + int l; >> > + >> > + /* >> > + * cheat a bit here: we know a policy is < 128 >> > bytes, >> > + * so the sequence and cons tags will only be two >> > + * bytes long >> > + */ >> > + *work++ = _tag(UNIV, CONS, SEQ); >> > + seq_len = work++; >> > + *work++ = _tagn(CONT, CONS, 0); >> > + tag_len = work++; >> > + asn1_encode_integer(&work, cmd); >> > + *tag_len = work - tag_len - 1; >> > + *work++ = _tagn(CONT, CONS, 1); >> > + tag_len = work++; >> > + asn1_encode_octet_string(&work, pols->policies[i], >> > + pols->len[i]); >> > + *tag_len = work - tag_len - 1; >> > + l = work - seq_len - 1; >> > + /* our assumption about policy length failed */ >> > + if (WARN(l > 127, >> > + "policy is too long: %d but must be < >> > 128", l)) { >> > + kfree(buf); >> > + return -EINVAL; >> > + } >> > + *seq_len = l; >> >> >> >> You're not even using your own sequence encoding here, because it >> only works when you know the length in advance. How about setting >> *seq_len to 0x80 to start with, for an indeterminate length. > >I already did that in the asn.1 patch, so I've updated this one to use >it. > >> Then in the happy case where it is <128, just go back and fill it in >> as you currently do. Otherwise append 0x00 0x00 as the end marker. > >That doesn't work ... the format of these octet strings is likely to >have two zeros together, so they *have* to be definite length encoded. The octet-strings sure, but we know the length of those. It was the sequence you have that <127 check and bail out for... wasn't it? >> None of this has to be DER, does it? > >None of what? The policy? the DER format is already in use so we >can't change it. What we *output* doesn't need to be DER (mandatory definite length) and can be BER though, right? >> <Insert more whining about PAGE_SIZE assumptions and buffer >> overflows> > >OK, OK, I fixed that too. > >James -- Sent from my Android device with K-9 Mail. Please excuse my brevity.