Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/cmcp_verify/azure_cvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def _extract_extra_data(quote_msg: bytes) -> bytes:
attest = quote_msg[2 : 2 + size]
else:
raise ValueError("TPMS_ATTEST magic not found")
if len(attest) < 6 or struct.unpack_from(">H", attest, 4)[0] != 0x8018:
raise ValueError("TPMS_ATTEST type must be TPM_ST_ATTEST_QUOTE")
offset = 6 # magic(4) + type(2)
if offset + 2 > len(attest):
raise ValueError("TPMS_ATTEST qualifiedSigner truncated")
Expand Down Expand Up @@ -156,7 +158,7 @@ def _ak_from_runtime_data(runtime_data: bytes) -> rsa.RSAPublicKey:
ak = matches[0]
if _PRIVATE_JWK_PARAMS.intersection(ak):
raise ValueError("HCLAkPub must not contain private or symmetric JWK parameters")
if "kty" in ak and ak["kty"] != "RSA":
if ak.get("kty") != "RSA":
raise ValueError("HCLAkPub kty must be RSA")

def _b64u(v: Any) -> int:
Expand Down
65 changes: 65 additions & 0 deletions tests/unit/test_azure_quote_type_and_required_kty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""Independent signed controls for the two outstanding #805 review requests."""
import base64
import hashlib
import json
import struct

import pytest
from cryptography.hazmat.primitives.asymmetric import rsa

from cmcp_verify.azure_cvm import verify_azure_cvm_measurement
from tests.unit.test_azure_cvm_verify import (
_build_evidence,
_signed_snp,
_synthetic_chain,
_tpmt_signature,
)


@pytest.mark.parametrize("wrapped", [False, True])
def test_signed_non_quote_attestation_is_refused_with_quote_twin(wrapped):
nonce = bytes(range(64))
key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
raw, measurement, root = _build_evidence(nonce, ak_key=key)
envelope = json.loads(raw)
body = base64.b64decode(envelope["quote_msg"])[2:]
for kind, expected in [(0x8018, True), (0x8017, False)]:
attest = bytearray(body)
struct.pack_into(">H", attest, 4, kind)
message = (struct.pack(">H", len(attest)) if wrapped else b"") + bytes(attest)
envelope["quote_msg"] = base64.b64encode(message).decode()
envelope["quote_sig"] = base64.b64encode(_tpmt_signature(key, message)).decode()
result = verify_azure_cvm_measurement(
measurement, json.dumps(envelope).encode(), nonce.hex(), root
)
assert result.verified is expected
if not expected:
assert result.failure_reason == "quote_parse_error"
assert "quote_nonce_binding" not in result.verified_fields


def test_hardware_bound_hcl_key_requires_kty_with_signed_valid_twin():
nonce = bytes(range(64))
raw, _, _ = _build_evidence(nonce)
original = json.loads(raw)
chain, root, signer = _synthetic_chain()
runtime = json.loads(base64.b64decode(original["runtime_data"]))
for missing, expected in [(False, True), (True, False)]:
candidate = json.loads(json.dumps(runtime))
if missing:
candidate["keys"][0].pop("kty")
runtime_bytes = json.dumps(candidate).encode()
report, measurement = _signed_snp(signer, runtime_bytes)
assert hashlib.sha256(runtime_bytes).digest() in report
envelope = dict(
original,
runtime_data=base64.b64encode(runtime_bytes).decode(),
snp_report=base64.b64encode(report).decode(),
vcek_chain_pem=base64.b64encode(chain).decode(),
)
result = verify_azure_cvm_measurement(
measurement, json.dumps(envelope).encode(), nonce.hex(), root
)
assert result.verified is expected
if not expected:
assert result.failure_reason == "ak_parse_error"
Loading