| 1 |
from dataclasses import dataclass |
| 2 |
from enum import Enum |
| 3 |
from typing import Mapping, Union |
| 4 |
|
| 5 |
from ..cose import COSECRV, COSEAlgorithmIdentifier |
| 6 |
from ..exceptions import InvalidTPMPubAreaStructure |
| 7 |
|
| 8 |
################ |
| 9 |
# |
| 10 |
# A whole lotta domain knowledge is captured here, with hazy connections to source |
| 11 |
# documents. Good places to start searching for more info on these values are the |
| 12 |
# following Trusted Computing Group TPM Library docs linked in the WebAuthn API: |
| 13 |
# |
| 14 |
# - https://www.trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-1-Architecture-01.38.pdf |
| 15 |
# - https://www.trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-2-Structures-01.38.pdf |
| 16 |
# - https://www.trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-3-Commands-01.38.pdf |
| 17 |
# |
| 18 |
################ |
| 19 |
|
| 20 |
|
| 21 |
class TPM_ST(str, Enum): |
| 22 |
RSP_COMMAND = "TPM_ST_RSP_COMMAND" |
| 23 |
NULL = "TPM_ST_NULL" |
| 24 |
NO_SESSIONS = "TPM_ST_NO_SESSIONS" |
| 25 |
SESSIONS = "TPM_ST_SESSIONS" |
| 26 |
ATTEST_NV = "TPM_ST_ATTEST_NV" |
| 27 |
ATTEST_COMMAND_AUDIT = "TPM_ST_ATTEST_COMMAND_AUDIT" |
| 28 |
ATTEST_SESSION_AUDIT = "TPM_ST_ATTEST_SESSION_AUDIT" |
| 29 |
ATTEST_CERTIFY = "TPM_ST_ATTEST_CERTIFY" |
| 30 |
ATTEST_QUOTE = "TPM_ST_ATTEST_QUOTE" |
| 31 |
ATTEST_TIME = "TPM_ST_ATTEST_TIME" |
| 32 |
ATTEST_CREATION = "TPM_ST_ATTEST_CREATION" |
| 33 |
CREATION = "TPM_ST_CREATION" |
| 34 |
VERIFIED = "TPM_ST_VERIFIED" |
| 35 |
AUTH_SECRET = "TPM_ST_AUTH_SECRET" |
| 36 |
HASHCHECK = "TPM_ST_HASHCHECK" |
| 37 |
AUTH_SIGNED = "TPM_ST_AUTH_SIGNED" |
| 38 |
FU_MANIFEST = "TPM_ST_FU_MANIFEST" |
| 39 |
|
| 40 |
|
| 41 |
class TPM_ALG(str, Enum): |
| 42 |
ERROR = "TPM_ALG_ERROR" |
| 43 |
RSA = "TPM_ALG_RSA" |
| 44 |
SHA1 = "TPM_ALG_SHA1" |
| 45 |
HMAC = "TPM_ALG_HMAC" |
| 46 |
AES = "TPM_ALG_AES" |
| 47 |
MGF1 = "TPM_ALG_MGF1" |
| 48 |
KEYEDHASH = "TPM_ALG_KEYEDHASH" |
| 49 |
XOR = "TPM_ALG_XOR" |
| 50 |
SHA256 = "TPM_ALG_SHA256" |
| 51 |
SHA384 = "TPM_ALG_SHA384" |
| 52 |
SHA512 = "TPM_ALG_SHA512" |
| 53 |
NULL = "TPM_ALG_NULL" |
| 54 |
SM3_256 = "TPM_ALG_SM3_256" |
| 55 |
SM4 = "TPM_ALG_SM4" |
| 56 |
RSASSA = "TPM_ALG_RSASSA" |
| 57 |
RSAES = "TPM_ALG_RSAES" |
| 58 |
RSAPSS = "TPM_ALG_RSAPSS" |
| 59 |
OAEP = "TPM_ALG_OAEP" |
| 60 |
ECDSA = "TPM_ALG_ECDSA" |
| 61 |
ECDH = "TPM_ALG_ECDH" |
| 62 |
ECDAA = "TPM_ALG_ECDAA" |
| 63 |
SM2 = "TPM_ALG_SM2" |
| 64 |
ECSCHNORR = "TPM_ALG_ECSCHNORR" |
| 65 |
ECMQV = "TPM_ALG_ECMQV" |
| 66 |
KDF1_SP800_56A = "TPM_ALG_KDF1_SP800_56A" |
| 67 |
KDF2 = "TPM_ALG_KDF2" |
| 68 |
KDF1_SP800_108 = "TPM_ALG_KDF1_SP800_108" |
| 69 |
ECC = "TPM_ALG_ECC" |
| 70 |
SYMCIPHER = "TPM_ALG_SYMCIPHER" |
| 71 |
CAMELLIA = "TPM_ALG_CAMELLIA" |
| 72 |
CTR = "TPM_ALG_CTR" |
| 73 |
OFB = "TPM_ALG_OFB" |
| 74 |
CBC = "TPM_ALG_CBC" |
| 75 |
CFB = "TPM_ALG_CFB" |
| 76 |
ECB = "TPM_ALG_ECB" |
| 77 |
|
| 78 |
|
| 79 |
class TPM_ECC_CURVE(str, Enum): |
| 80 |
NONE = "NONE" |
| 81 |
NIST_P192 = "NIST_P192" |
| 82 |
NIST_P224 = "NIST_P224" |
| 83 |
NIST_P256 = "NIST_P256" |
| 84 |
NIST_P384 = "NIST_P384" |
| 85 |
NIST_P521 = "NIST_P521" |
| 86 |
BN_P256 = "BN_P256" |
| 87 |
BN_P638 = "BN_P638" |
| 88 |
SM2_P256 = "SM2_P256" |
| 89 |
|
| 90 |
|
| 91 |
class TPMCertInfoClockInfo: |
| 92 |
""" |
| 93 |
10.11.1 TPMS_CLOCK_INFO |
| 94 |
""" |
| 95 |
|
| 96 |
clock: bytes |
| 97 |
reset_count: int |
| 98 |
restart_count: int |
| 99 |
safe: bool |
| 100 |
|
| 101 |
def __init__(self, clock_info: bytes): |
| 102 |
self.clock = clock_info[0:8] |
| 103 |
self.reset_count = int.from_bytes(clock_info[8:12], "big") |
| 104 |
self.restart_count = int.from_bytes(clock_info[12:16], "big") |
| 105 |
self.safe = bool(clock_info[16]) |
| 106 |
|
| 107 |
|
| 108 |
class TPMCertInfoAttested: |
| 109 |
""" |
| 110 |
10.12.3 TPMS_CERTIFY_INFO |
| 111 |
""" |
| 112 |
|
| 113 |
name_alg: TPM_ALG |
| 114 |
name_alg_bytes: bytes |
| 115 |
name: bytes |
| 116 |
qualified_name: bytes |
| 117 |
|
| 118 |
def __init__(self, attested_name: bytes, qualified_name: bytes): |
| 119 |
self.name_alg = TPM_ALG_MAP[attested_name[0:2]] |
| 120 |
self.name_alg_bytes = attested_name[0:2] |
| 121 |
self.name = attested_name |
| 122 |
self.qualified_name = qualified_name |
| 123 |
|
| 124 |
|
| 125 |
@dataclass |
| 126 |
class TPMCertInfo: |
| 127 |
""" |
| 128 |
10.12.8 TPMS_ATTEST |
| 129 |
""" |
| 130 |
|
| 131 |
magic: bytes |
| 132 |
type: TPM_ST |
| 133 |
qualified_signer: bytes |
| 134 |
extra_data: bytes |
| 135 |
clock_info: TPMCertInfoClockInfo |
| 136 |
firmware_version: bytes |
| 137 |
attested: TPMCertInfoAttested |
| 138 |
|
| 139 |
|
| 140 |
class TPMPubAreaParametersRSA: |
| 141 |
""" |
| 142 |
12.2.3.5 TPMS_RSA_PARMS |
| 143 |
""" |
| 144 |
|
| 145 |
symmetric: TPM_ALG |
| 146 |
scheme: TPM_ALG |
| 147 |
key_bits: bytes |
| 148 |
exponent: bytes |
| 149 |
|
| 150 |
def __init__(self, params: bytes): |
| 151 |
self.symmetric = TPM_ALG_MAP[params[0:2]] |
| 152 |
self.scheme = TPM_ALG_MAP[params[2:4]] |
| 153 |
self.key_bits = params[4:6] |
| 154 |
self.exponent = params[6:10] |
| 155 |
|
| 156 |
|
| 157 |
class TPMPubAreaParametersECC: |
| 158 |
""" |
| 159 |
12.2.3.6 TPMS_ECC_PARMS |
| 160 |
""" |
| 161 |
|
| 162 |
symmetric: TPM_ALG |
| 163 |
scheme: TPM_ALG |
| 164 |
curve_id: TPM_ECC_CURVE |
| 165 |
kdf: TPM_ALG |
| 166 |
|
| 167 |
def __init__(self, params: bytes): |
| 168 |
self.symmetric = TPM_ALG_MAP[params[0:2]] |
| 169 |
self.scheme = TPM_ALG_MAP[params[2:4]] |
| 170 |
self.curve_id = TPM_ECC_CURVE_MAP[params[4:6]] |
| 171 |
self.kdf = TPM_ALG_MAP[params[6:8]] |
| 172 |
|
| 173 |
|
| 174 |
class TPMPubAreaObjectAttributes: |
| 175 |
""" |
| 176 |
8.3 TPMA_OBJECT (Object Attributes) |
| 177 |
""" |
| 178 |
|
| 179 |
fixed_tpm: bool |
| 180 |
st_clear: bool |
| 181 |
fixed_parent: bool |
| 182 |
sensitive_data_origin: bool |
| 183 |
user_with_auth: bool |
| 184 |
admin_with_policy: bool |
| 185 |
no_da: bool |
| 186 |
encrypted_duplication: bool |
| 187 |
restricted: bool |
| 188 |
decrypt: bool |
| 189 |
sign_or_encrypt: bool |
| 190 |
|
| 191 |
def __init__(self, object_attributes: bytes): |
| 192 |
attrs = int.from_bytes(object_attributes[0:4], "big") |
| 193 |
self.fixed_tpm = attrs & (1 << 1) != 0 |
| 194 |
self.st_clear = attrs & (1 << 2) != 0 |
| 195 |
self.fixed_parent = attrs & (1 << 4) != 0 |
| 196 |
self.sensitive_data_origin = attrs & (1 << 5) != 0 |
| 197 |
self.user_with_auth = attrs & (1 << 6) != 0 |
| 198 |
self.admin_with_policy = attrs & (1 << 7) != 0 |
| 199 |
self.no_da = attrs & (1 << 10) != 0 |
| 200 |
self.encrypted_duplication = attrs & (1 << 11) != 0 |
| 201 |
self.restricted = attrs & (1 << 16) != 0 |
| 202 |
self.decrypt = attrs & (1 << 17) != 0 |
| 203 |
self.sign_or_encrypt = attrs & (1 << 18) != 0 |
| 204 |
|
| 205 |
|
| 206 |
class TPMPubAreaUnique: |
| 207 |
""" |
| 208 |
12.2.3.2 TPMU_PUBLIC_ID |
| 209 |
""" |
| 210 |
|
| 211 |
value: bytes |
| 212 |
|
| 213 |
def __init__(self, unique: bytes, alg_type: TPM_ALG): |
| 214 |
if alg_type == TPM_ALG.RSA: |
| 215 |
""" |
| 216 |
As per 11.2.4.5 TPM2B_PUBLIC_KEY_RSA, extract `unique` of dynamic length |
| 217 |
""" |
| 218 |
unique_length = int.from_bytes(unique[0:2], "big") |
| 219 |
rsa_unique = unique[2 : 2 + unique_length] |
| 220 |
self.value = rsa_unique |
| 221 |
elif alg_type == TPM_ALG.ECC: |
| 222 |
""" |
| 223 |
As per 12.2.3.2 TPMU_PUBLIC_ID, `unique` is `TPMS_ECC_POINT` when `type` |
| 224 |
indicates ECC. |
| 225 |
|
| 226 |
As per 11.2.5.2 TPMS_ECC_POINT, `x` and `y` within are |
| 227 |
`TPM2B_ECC_PARAMETER`, which is a uint16 `size`, followed by bytes of |
| 228 |
the indicated length. |
| 229 |
|
| 230 |
`unique`, then, is structured thusly: |
| 231 |
[ |
| 232 |
[x_len, ...x_bytes], |
| 233 |
[y_len, ...y_bytes] |
| 234 |
] |
| 235 |
|
| 236 |
Get `unique` to this structure for easier comparison with the output from |
| 237 |
`decode_credential_public_key`, which will return bytes for its `x` and `y` |
| 238 |
and to which this value will be compared: |
| 239 |
|
| 240 |
[ |
| 241 |
x_bytes, |
| 242 |
y_bytes, |
| 243 |
] |
| 244 |
|
| 245 |
""" |
| 246 |
pointer = 0 |
| 247 |
unique_x_len = int.from_bytes(unique[0:2], "big") |
| 248 |
pointer += 2 |
| 249 |
unique_x = unique[pointer : pointer + unique_x_len] |
| 250 |
pointer += unique_x_len |
| 251 |
unique_y_len = int.from_bytes(unique[pointer : pointer + 2], "big") |
| 252 |
pointer += 2 |
| 253 |
unique_y = unique[pointer : pointer + unique_y_len] |
| 254 |
|
| 255 |
self.value = b"".join([unique_x, unique_y]) |
| 256 |
else: |
| 257 |
raise InvalidTPMPubAreaStructure( |
| 258 |
f'Pub Area alg type "{alg_type}" was not "{TPM_ALG.RSA}" or "{TPM_ALG.ECC}"' |
| 259 |
) |
| 260 |
|
| 261 |
|
| 262 |
@dataclass |
| 263 |
class TPMPubArea: |
| 264 |
""" |
| 265 |
12.2.4 TPMT_PUBLIC |
| 266 |
""" |
| 267 |
|
| 268 |
type: TPM_ALG |
| 269 |
name_alg: TPM_ALG |
| 270 |
object_attributes: TPMPubAreaObjectAttributes |
| 271 |
auth_policy: bytes |
| 272 |
parameters: Union[TPMPubAreaParametersRSA, TPMPubAreaParametersECC] |
| 273 |
unique: TPMPubAreaUnique |
| 274 |
|
| 275 |
|
| 276 |
@dataclass |
| 277 |
class TPMManufacturerInfo: |
| 278 |
name: str |
| 279 |
id: str |
| 280 |
|
| 281 |
|
| 282 |
""" |
| 283 |
6.9 TPM_ST (Structure Tags) |
| 284 |
""" |
| 285 |
TPM_ST_MAP: Mapping[bytes, TPM_ST] = { |
| 286 |
b"\x00\xc4": TPM_ST.RSP_COMMAND, |
| 287 |
b"\x80\x00": TPM_ST.NULL, |
| 288 |
b"\x80\x01": TPM_ST.NO_SESSIONS, |
| 289 |
b"\x80\x02": TPM_ST.SESSIONS, |
| 290 |
b"\x80\x14": TPM_ST.ATTEST_NV, |
| 291 |
b"\x80\x15": TPM_ST.ATTEST_COMMAND_AUDIT, |
| 292 |
b"\x80\x16": TPM_ST.ATTEST_SESSION_AUDIT, |
| 293 |
b"\x80\x17": TPM_ST.ATTEST_CERTIFY, |
| 294 |
b"\x80\x18": TPM_ST.ATTEST_QUOTE, |
| 295 |
b"\x80\x19": TPM_ST.ATTEST_TIME, |
| 296 |
b"\x80\x1a": TPM_ST.ATTEST_CREATION, |
| 297 |
b"\x80\x21": TPM_ST.CREATION, |
| 298 |
b"\x80\x22": TPM_ST.VERIFIED, |
| 299 |
b"\x80\x23": TPM_ST.AUTH_SECRET, |
| 300 |
b"\x80\x24": TPM_ST.HASHCHECK, |
| 301 |
b"\x80\x25": TPM_ST.AUTH_SIGNED, |
| 302 |
b"\x80\x29": TPM_ST.FU_MANIFEST, |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
""" |
| 307 |
6.3 TPM_ALG_ID |
| 308 |
""" |
| 309 |
TPM_ALG_MAP: Mapping[bytes, TPM_ALG] = { |
| 310 |
b"\x00\x00": TPM_ALG.ERROR, |
| 311 |
b"\x00\x01": TPM_ALG.RSA, |
| 312 |
b"\x00\x04": TPM_ALG.SHA1, |
| 313 |
b"\x00\x05": TPM_ALG.HMAC, |
| 314 |
b"\x00\x06": TPM_ALG.AES, |
| 315 |
b"\x00\x07": TPM_ALG.MGF1, |
| 316 |
b"\x00\x08": TPM_ALG.KEYEDHASH, |
| 317 |
b"\x00\x0a": TPM_ALG.XOR, |
| 318 |
b"\x00\x0b": TPM_ALG.SHA256, |
| 319 |
b"\x00\x0c": TPM_ALG.SHA384, |
| 320 |
b"\x00\x0d": TPM_ALG.SHA512, |
| 321 |
b"\x00\x10": TPM_ALG.NULL, |
| 322 |
b"\x00\x12": TPM_ALG.SM3_256, |
| 323 |
b"\x00\x13": TPM_ALG.SM4, |
| 324 |
b"\x00\x14": TPM_ALG.RSASSA, |
| 325 |
b"\x00\x15": TPM_ALG.RSAES, |
| 326 |
b"\x00\x16": TPM_ALG.RSAPSS, |
| 327 |
b"\x00\x17": TPM_ALG.OAEP, |
| 328 |
b"\x00\x18": TPM_ALG.ECDSA, |
| 329 |
b"\x00\x19": TPM_ALG.ECDH, |
| 330 |
b"\x00\x1a": TPM_ALG.ECDAA, |
| 331 |
b"\x00\x1b": TPM_ALG.SM2, |
| 332 |
b"\x00\x1c": TPM_ALG.ECSCHNORR, |
| 333 |
b"\x00\x1d": TPM_ALG.ECMQV, |
| 334 |
b"\x00\x20": TPM_ALG.KDF1_SP800_56A, |
| 335 |
b"\x00\x21": TPM_ALG.KDF2, |
| 336 |
b"\x00\x22": TPM_ALG.KDF1_SP800_108, |
| 337 |
b"\x00\x23": TPM_ALG.ECC, |
| 338 |
b"\x00\x25": TPM_ALG.SYMCIPHER, |
| 339 |
b"\x00\x26": TPM_ALG.CAMELLIA, |
| 340 |
b"\x00\x40": TPM_ALG.CTR, |
| 341 |
b"\x00\x41": TPM_ALG.OFB, |
| 342 |
b"\x00\x42": TPM_ALG.CBC, |
| 343 |
b"\x00\x43": TPM_ALG.CFB, |
| 344 |
b"\x00\x44": TPM_ALG.ECB, |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
""" |
| 349 |
6.4 TPM_ECC_CURVE |
| 350 |
""" |
| 351 |
TPM_ECC_CURVE_MAP: Mapping[bytes, TPM_ECC_CURVE] = { |
| 352 |
b"\x00\x00": TPM_ECC_CURVE.NONE, |
| 353 |
b"\x00\x01": TPM_ECC_CURVE.NIST_P192, |
| 354 |
b"\x00\x02": TPM_ECC_CURVE.NIST_P224, |
| 355 |
b"\x00\x03": TPM_ECC_CURVE.NIST_P256, |
| 356 |
b"\x00\x04": TPM_ECC_CURVE.NIST_P384, |
| 357 |
b"\x00\x05": TPM_ECC_CURVE.NIST_P521, |
| 358 |
b"\x00\x10": TPM_ECC_CURVE.BN_P256, |
| 359 |
b"\x00\x11": TPM_ECC_CURVE.BN_P638, |
| 360 |
b"\x00\x20": TPM_ECC_CURVE.SM2_P256, |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
# Intentionally omit curves we can't map so a KeyError gets thrown |
| 365 |
TPM_ECC_CURVE_COSE_CRV_MAP: Mapping[TPM_ECC_CURVE, COSECRV] = { |
| 366 |
TPM_ECC_CURVE.NIST_P256: COSECRV.P256, |
| 367 |
TPM_ECC_CURVE.NIST_P384: COSECRV.P384, |
| 368 |
TPM_ECC_CURVE.NIST_P521: COSECRV.P521, |
| 369 |
TPM_ECC_CURVE.BN_P256: COSECRV.P256, |
| 370 |
TPM_ECC_CURVE.SM2_P256: COSECRV.P256, |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
# Intentionally omit algs we can't map so a KeyError gets thrown |
| 375 |
TPM_ALG_COSE_ALG_MAP: Mapping[TPM_ALG, COSEAlgorithmIdentifier] = { |
| 376 |
TPM_ALG.SHA256: COSEAlgorithmIdentifier.RSASSA_PSS_SHA_256, |
| 377 |
TPM_ALG.SHA384: COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_384, |
| 378 |
TPM_ALG.SHA512: COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_512, |
| 379 |
TPM_ALG.SHA1: COSEAlgorithmIdentifier.RSASSA_PKCS1_v1_5_SHA_1, |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
# Sourced from https://trustedcomputinggroup.org/resource/vendor-id-registry/ |
| 384 |
# Latest version: https://trustedcomputinggroup.org/wp-content/uploads/TCG-TPM-Vendor-ID-Registry-Version-1.02-Revision-1.00.pdf |
| 385 |
TPM_MANUFACTURERS: Mapping[str, TPMManufacturerInfo] = { |
| 386 |
"id:414D4400": TPMManufacturerInfo(name="AMD", id="AMD"), |
| 387 |
"id:41544D4C": TPMManufacturerInfo(name="Atmel", id="ATML"), |
| 388 |
"id:4252434D": TPMManufacturerInfo(name="Broadcom", id="BRCM"), |
| 389 |
"id:4353434F": TPMManufacturerInfo(name="Cisco", id="CSCO"), |
| 390 |
"id:464C5953": TPMManufacturerInfo(name="Flyslice Technologies", id="FLYS"), |
| 391 |
"id:48504500": TPMManufacturerInfo(name="HPE", id="HPE"), |
| 392 |
"id:49424d00": TPMManufacturerInfo(name="IBM", id="IBM"), |
| 393 |
"id:49465800": TPMManufacturerInfo(name="Infineon", id="IFX"), |
| 394 |
"id:494E5443": TPMManufacturerInfo(name="Intel", id="INTC"), |
| 395 |
"id:4C454E00": TPMManufacturerInfo(name="Lenovo", id="LEN"), |
| 396 |
"id:4D534654": TPMManufacturerInfo(name="Microsoft", id="MSFT"), |
| 397 |
"id:4E534D20": TPMManufacturerInfo(name="National Semiconductor", id="NSM"), |
| 398 |
"id:4E545A00": TPMManufacturerInfo(name="Nationz", id="NTZ"), |
| 399 |
"id:4E544300": TPMManufacturerInfo(name="Nuvoton Technology", id="NTC"), |
| 400 |
"id:51434F4D": TPMManufacturerInfo(name="Qualcomm", id="QCOM"), |
| 401 |
"id:534D5343": TPMManufacturerInfo(name="SMSC", id="SMSC"), |
| 402 |
"id:53544D20": TPMManufacturerInfo(name="ST Microelectronics", id="STM"), |
| 403 |
"id:534D534E": TPMManufacturerInfo(name="Samsung", id="SMSN"), |
| 404 |
"id:534E5300": TPMManufacturerInfo(name="Sinosun", id="SNS"), |
| 405 |
"id:54584E00": TPMManufacturerInfo(name="Texas Instruments", id="TXN"), |
| 406 |
"id:57454300": TPMManufacturerInfo(name="Winbond", id="WEC"), |
| 407 |
"id:524F4343": TPMManufacturerInfo(name="Fuzhou Rockchip", id="ROCC"), |
| 408 |
"id:474F4F47": TPMManufacturerInfo(name="Google", id="GOOG"), |
| 409 |
} |