| 1 |
from ..exceptions import InvalidTPMPubAreaStructure |
| 2 |
from .structs import ( |
| 3 |
TPM_ALG, |
| 4 |
TPM_ALG_MAP, |
| 5 |
TPMPubArea, |
| 6 |
TPMPubAreaObjectAttributes, |
| 7 |
TPMPubAreaParametersECC, |
| 8 |
TPMPubAreaParametersRSA, |
| 9 |
TPMPubAreaUnique, |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
def parse_pub_area(val: bytes) -> TPMPubArea: |
| 14 |
""" |
| 15 |
Turn `response.attestationObject.attStmt.pubArea` into structured data |
| 16 |
""" |
| 17 |
pointer = 0 |
| 18 |
|
| 19 |
type_bytes = val[pointer : pointer + 2] |
| 20 |
pointer += 2 |
| 21 |
mapped_type = TPM_ALG_MAP[type_bytes] |
| 22 |
|
| 23 |
name_alg_bytes = val[pointer : pointer + 2] |
| 24 |
pointer += 2 |
| 25 |
mapped_name_alg = TPM_ALG_MAP[name_alg_bytes] |
| 26 |
|
| 27 |
object_attributes_bytes = val[pointer : pointer + 4] |
| 28 |
pointer += 4 |
| 29 |
# Parse attributes from right to left by zero-index bit position |
| 30 |
object_attributes = TPMPubAreaObjectAttributes(object_attributes_bytes) |
| 31 |
|
| 32 |
auth_policy_length = int.from_bytes(val[pointer : pointer + 2], "big") |
| 33 |
pointer += 2 |
| 34 |
auth_policy_bytes = val[pointer : pointer + auth_policy_length] |
| 35 |
pointer += auth_policy_length |
| 36 |
|
| 37 |
# Decode the rest of the bytes to public key parameters |
| 38 |
if mapped_type == TPM_ALG.RSA: |
| 39 |
rsa_bytes = val[pointer : pointer + 10] |
| 40 |
pointer += 10 |
| 41 |
parameters = TPMPubAreaParametersRSA(rsa_bytes) |
| 42 |
elif mapped_type == TPM_ALG.ECC: |
| 43 |
ecc_bytes = val[pointer : pointer + 8] |
| 44 |
pointer += 8 |
| 45 |
# mypy will error here because of the incompatible "reassignment", but |
| 46 |
# `parameters` in `TPMPubArea` is a Union of either type so ignore the error |
| 47 |
parameters = TPMPubAreaParametersECC(ecc_bytes) # type: ignore |
| 48 |
else: |
| 49 |
raise InvalidTPMPubAreaStructure(f'Type "{mapped_type}" is unsupported') |
| 50 |
|
| 51 |
unique_length_bytes = val[pointer:] |
| 52 |
|
| 53 |
return TPMPubArea( |
| 54 |
type=mapped_type, |
| 55 |
name_alg=mapped_name_alg, |
| 56 |
object_attributes=object_attributes, |
| 57 |
auth_policy=auth_policy_bytes, |
| 58 |
parameters=parameters, |
| 59 |
unique=TPMPubAreaUnique(unique_length_bytes, mapped_type), |
| 60 |
) |