Download verify_tee.py from decisionmodels/tee-attestation-prototype: direct link, hf CLI and curl.
- Browser
- Download file 6.69 kB
-
https://huggingface.co/datasets/decisionmodels/tee-attestation-prototype/resolve/main/verify_tee.py
- Command line
-
hf download hf://datasets/decisionmodels/tee-attestation-prototype/verify_tee.py
-
curl -L -o verify_tee.py https://huggingface.co/datasets/decisionmodels/tee-attestation-prototype/resolve/main/verify_tee.py
6.69 kB
| #!/usr/bin/env python3 | |
| """Independent verifier for the decisionmodels.io TEE prototype endpoint. | |
| Trusts only AMD (KDS root chain fetched live) and NVIDIA (NRAS JWKS fetched live). Usage: | |
| python3 verify_tee.py https://HOST | |
| Checks: (1) the TLS key the server actually presents == key in the bundle; (2) AMD SEV-SNP report signed by a VCEK | |
| that chains to AMD's ARK/ASK fetched from kdsintf.amd.com; (3) report_data == SHA-512(SPKI); debug disabled; | |
| (4) a FRESH report for a random nonce (report_data == SHA-512(SPKI || nonce)); (5) NVIDIA NRAS JWT (ES384, NVIDIA JWKS) | |
| says overall attestation true and eat_nonce == SHA-256(SPKI). | |
| """ | |
| import base64, hashlib, json, os, re, socket, ssl, struct, sys, urllib.request | |
| from urllib.parse import urlparse | |
| from cryptography import x509 | |
| from cryptography.hazmat.primitives import hashes, serialization | |
| from cryptography.hazmat.primitives.asymmetric import ec, padding | |
| from cryptography.hazmat.primitives.asymmetric.utils import encode_dss_signature | |
| import jwt | |
| def get(url, ctx=None): | |
| with urllib.request.urlopen(url, context=ctx, timeout=30) as r: | |
| return r.read() | |
| def spki_of(cert): | |
| return cert.public_key().public_bytes(serialization.Encoding.DER, serialization.PublicFormat.SubjectPublicKeyInfo) | |
| def check(name, ok, detail=""): | |
| print(("PASS " if ok else "FAIL ") + name + (f" ({detail})" if detail else "")) | |
| if not ok: | |
| check.failed = True | |
| check.failed = False | |
| def verify_snp(report, vcek, ask, ark, expect_rd): | |
| # SEV-SNP ATTESTATION_REPORT layout (AMD SEV-SNP ABI spec, Table 22) | |
| policy = struct.unpack_from("<Q", report, 0x08)[0] | |
| rd, meas = report[0x50:0x90], report[0x90:0xC0] | |
| r = int.from_bytes(report[0x2A0:0x2A0 + 72], "little"); s = int.from_bytes(report[0x2E8:0x2E8 + 72], "little") | |
| try: | |
| vcek.public_key().verify(encode_dss_signature(r, s), report[:0x2A0], ec.ECDSA(hashes.SHA384())) | |
| sig_ok = True | |
| except Exception as e: | |
| sig_ok = False | |
| check("SNP report signature by VCEK", sig_ok) | |
| check("SNP report_data binds the TLS key", rd == expect_rd) | |
| check("SNP guest policy: debug disabled", not (policy >> 19) & 1, f"policy=0x{policy:x}") | |
| return meas.hex() | |
| def pss_verify(issuer, cert): | |
| issuer.public_key().verify(cert.signature, cert.tbs_certificate_bytes, | |
| padding.PSS(mgf=padding.MGF1(hashes.SHA384()), salt_length=48), hashes.SHA384()) | |
| def main(base): | |
| offline = os.path.isfile(base) | |
| noverify = ssl.create_default_context(); noverify.check_hostname = False; noverify.verify_mode = ssl.CERT_NONE | |
| if offline: # saved bundle: no live TLS / freshness checks possible | |
| bundle = json.load(open(base)) | |
| spki = spki_of(x509.load_pem_x509_certificate(bundle["binding"]["tls_cert_pem"].encode())) | |
| print("OFFLINE mode: verifying a saved bundle (signatures and bindings only)") | |
| else: | |
| host = urlparse(base).hostname; port = urlparse(base).port or 443 | |
| # (1) the key the server really uses on this connection | |
| with socket.create_connection((host, port), timeout=20) as sock: | |
| with noverify.wrap_socket(sock, server_hostname=host) as tls: | |
| live = x509.load_der_x509_certificate(tls.getpeercert(binary_form=True)) | |
| bundle = json.loads(get(base + "/attestation", noverify)) | |
| bcert = x509.load_pem_x509_certificate(bundle["binding"]["tls_cert_pem"].encode()) | |
| spki = spki_of(live) | |
| check("live TLS key == bundle TLS key", spki == spki_of(bcert)) | |
| # (2) AMD chain: ARK/ASK fetched from AMD, VCEK from bundle must chain to them | |
| product = os.environ.get("AMD_PRODUCT", "Turin") | |
| pem = get(f"https://kdsintf.amd.com/vcek/v1/{product}/cert_chain").decode() | |
| chain = [x509.load_pem_x509_certificate(b.encode()) for b in | |
| re.findall(r"-----BEGIN CERTIFICATE-----.+?-----END CERTIFICATE-----", pem, re.S)] | |
| ask, ark = chain[0], chain[1] | |
| vcek = x509.load_pem_x509_certificate(bundle["cpu_tee"]["vcek_pem"].encode()) | |
| try: | |
| pss_verify(ark, ark); pss_verify(ark, ask); pss_verify(ask, vcek); chain_ok = True | |
| except Exception: | |
| chain_ok = False | |
| check(f"VCEK chains to AMD ARK/ASK from kdsintf.amd.com ({product})", chain_ok) | |
| report = base64.b64decode(bundle["cpu_tee"]["report_b64"]) | |
| meas = verify_snp(report, vcek, ask, ark, hashlib.sha512(spki).digest()) | |
| print(" launch measurement:", meas) | |
| # (4) freshness (live endpoint only) | |
| if not offline: | |
| nonce = os.urandom(32) | |
| fresh = json.loads(get(base + "/attestation/fresh?nonce=" + nonce.hex(), noverify)) | |
| rep2 = base64.b64decode(fresh["report_b64"]) | |
| print("-- fresh report for a random nonce:") | |
| meas2 = verify_snp(rep2, vcek, ask, ark, hashlib.sha512(spki + nonce).digest()) | |
| check("fresh report has the same launch measurement", meas2 == meas) | |
| # (5) NVIDIA NRAS token | |
| claims = bundle["gpu_tee"]["nras_token"][1]["REMOTE_GPU_CLAIMS"] | |
| gpu_jwt = claims[0][1] | |
| jwks = jwt.PyJWKClient("https://nras.attestation.nvidia.com/.well-known/jwks.json") | |
| try: | |
| key = jwks.get_signing_key_from_jwt(gpu_jwt) | |
| p = jwt.decode(gpu_jwt, key.key, algorithms=["ES384"], options={"verify_exp": False, "verify_aud": False}) | |
| nv_ok = True | |
| except Exception as e: | |
| p, nv_ok = {}, False | |
| check("NVIDIA NRAS token signature (NVIDIA JWKS)", nv_ok) | |
| check("NVIDIA overall attestation result true", p.get("x-nvidia-overall-att-result") is True) | |
| check("NVIDIA eat_nonce binds the TLS key", p.get("eat_nonce") == hashlib.sha256(spki).hexdigest()) | |
| for gid, sub in (claims[1] or {}).items(): | |
| try: | |
| k2 = jwks.get_signing_key_from_jwt(sub) | |
| sp = jwt.decode(sub, k2.key, algorithms=["ES384"], options={"verify_exp": False, "verify_aud": False}) | |
| print(f" {gid}: measres={sp.get('measres')} cc_mode={sp.get('x-nvidia-cc-mode') or sp.get('x-nvidia-gpu-cc-mode')} " | |
| f"driver={sp.get('x-nvidia-gpu-driver-version')} vbios={sp.get('x-nvidia-gpu-vbios-version')} " | |
| f"secboot={sp.get('secboot')} dbgstat={sp.get('dbgstat')}") | |
| except Exception as e: | |
| print(f" {gid}: could not decode detailed claims ({type(e).__name__})") | |
| print("RESULT:", "FAIL" if check.failed else "ALL CHECKS PASSED") | |
| print("NOTE: proves a genuine AMD SEV-SNP VM (debug off) + NVIDIA CC GPU hold this TLS key; it does not yet prove which " | |
| "software runs inside (prototype: no measured root filesystem; operator has SSH).") | |
| return 1 if check.failed else 0 | |
| if __name__ == "__main__": | |
| sys.exit(main(sys.argv[1].rstrip("/"))) | |