| 1 |
from ..exceptions import InvalidTPMCertInfoStructure |
| 2 |
from .structs import ( |
| 3 |
TPM_ST, |
| 4 |
TPM_ST_MAP, |
| 5 |
TPMCertInfo, |
| 6 |
TPMCertInfoAttested, |
| 7 |
TPMCertInfoClockInfo, |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
def parse_cert_info(val: bytes) -> TPMCertInfo: |
| 12 |
""" |
| 13 |
Turn `response.attestationObject.attStmt.certInfo` into structured data |
| 14 |
""" |
| 15 |
pointer = 0 |
| 16 |
|
| 17 |
# The constant "TPM_GENERATED_VALUE" indicating a structure generated by TPM |
| 18 |
magic_bytes = val[pointer : pointer + 4] |
| 19 |
pointer += 4 |
| 20 |
|
| 21 |
# Type of the cert info structure |
| 22 |
type_bytes = val[pointer : pointer + 2] |
| 23 |
pointer += 2 |
| 24 |
mapped_type = TPM_ST_MAP[type_bytes] |
| 25 |
|
| 26 |
# Name of parent entity |
| 27 |
qualified_signer_length = int.from_bytes(val[pointer : pointer + 2], "big") |
| 28 |
pointer += 2 |
| 29 |
qualified_signer = val[pointer : pointer + qualified_signer_length] |
| 30 |
pointer += qualified_signer_length |
| 31 |
|
| 32 |
# Expected hash value of `attsToBeSigned` |
| 33 |
extra_data_length = int.from_bytes(val[pointer : pointer + 2], "big") |
| 34 |
pointer += 2 |
| 35 |
extra_data_bytes = val[pointer : pointer + extra_data_length] |
| 36 |
pointer += extra_data_length |
| 37 |
|
| 38 |
# Info about the TPM's internal clock |
| 39 |
clock_info_bytes = val[pointer : pointer + 17] |
| 40 |
pointer += 17 |
| 41 |
|
| 42 |
# Device firmware version |
| 43 |
firmware_version_bytes = val[pointer : pointer + 8] |
| 44 |
pointer += 8 |
| 45 |
|
| 46 |
# Verify that type is set to TPM_ST_ATTEST_CERTIFY. |
| 47 |
if mapped_type != TPM_ST.ATTEST_CERTIFY: |
| 48 |
raise InvalidTPMCertInfoStructure( |
| 49 |
f'Cert Info type "{mapped_type}" was not "{TPM_ST.ATTEST_CERTIFY}"' |
| 50 |
) |
| 51 |
|
| 52 |
# Attested name |
| 53 |
attested_name_length = int.from_bytes(val[pointer : pointer + 2], "big") |
| 54 |
pointer += 2 |
| 55 |
attested_name_bytes = val[pointer : pointer + attested_name_length] |
| 56 |
pointer += attested_name_length |
| 57 |
qualified_name_length = int.from_bytes(val[pointer : pointer + 2], "big") |
| 58 |
pointer += 2 |
| 59 |
qualified_name_bytes = val[pointer : pointer + qualified_name_length] |
| 60 |
pointer += qualified_name_length |
| 61 |
|
| 62 |
return TPMCertInfo( |
| 63 |
magic=magic_bytes, |
| 64 |
type=mapped_type, |
| 65 |
extra_data=extra_data_bytes, |
| 66 |
attested=TPMCertInfoAttested(attested_name_bytes, qualified_name_bytes), |
| 67 |
# Note that the remaining fields in the "Standard Attestation Structure" |
| 68 |
# [TPMv2-Part1] section 31.2, i.e., qualifiedSigner, clockInfo and |
| 69 |
# firmwareVersion are ignored. These fields MAY be used as an input to risk |
| 70 |
# engines. |
| 71 |
qualified_signer=qualified_signer, |
| 72 |
clock_info=TPMCertInfoClockInfo(clock_info_bytes), |
| 73 |
firmware_version=firmware_version_bytes, |
| 74 |
) |