# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-honor-a-delegated-capability-attestation-across-a-tee-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-honor-a-delegated-capability-attestation-across-a-tee-boundary.warrant.json
# Markdown SHA-256:   7be7d8cc016cce21fcf025dbb0709e17a05f0670f1ab25b50f40e10104357536
# Code AST SHA-256:   6bf1f5a7c54c7b9e36a7899d84260b50e91bb30ad67f5e0f816986a9c69c95d5
# Verified Classes:   AttestationBindingError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class AttestationBindingError(RuntimeError):
    """Raised when a delegated VC's asserted runtime measurement does not
    match the fresh TEE report presented alongside it, or when the report's
    policy downgrades the VC's stated policy."""

    def __init__(self, vc_measurement: str, report_measurement: str,
                 vc_policy: str, report_policy: str, reason: str):
        self.vc_measurement = vc_measurement
        self.report_measurement = report_measurement
        self.vc_policy = vc_policy
        self.report_policy = report_policy
        self.reason = reason
        super().__init__(
            f"attestation binding failed ({reason}): "
            f"vc_measurement={vc_measurement} report_measurement={report_measurement} "
            f"vc_policy={vc_policy} report_policy={report_policy}"
        )


def verify_delegated_capability(vc: dict, report: dict,
                                max_nonce_age_s: int, now_s: int) -> None:
    """Enforce the two-field binding between a delegated VC and a fresh
    TEE-backed runtime report. Raises AttestationBindingError on any
    violation; returns None only when the capability may be honored."""
    subject = vc["credentialSubject"]
    vc_meas = subject["measurement"]
    vc_policy = subject.get("policy", "debug=false")

    rep_meas = report["measurement"]
    rep_policy = report.get("policy", "debug=false")

    # 1. Identity binding: the VC names a measurement; the report must
    #    actually be that measurement. This is the check that is routinely
    #    skipped when the two are assumed to be the same field.
    if vc_meas != rep_meas:
        raise AttestationBindingError(
            vc_meas, rep_meas, vc_policy, rep_policy,
            reason="vc_measurement != report_measurement",
        )

    # 2. Freshness: the report must answer *our* challenge, not a cached one.
    nonce_age = now_s - report["nonce_issued_at"]
    if nonce_age > max_nonce_age_s:
        raise AttestationBindingError(
            vc_meas, rep_meas, vc_policy, rep_policy,
            reason=f"stale_report_nonce age={nonce_age}s > {max_nonce_age_s}s",
        )

    # 3. Policy: on platforms where policy is not covered by the measurement,
    #    the VC's policy claim is unbacked unless the report agrees.
    if vc_policy != rep_policy:
        raise AttestationBindingError(
            vc_meas, rep_meas, vc_policy, rep_policy,
            reason="policy_downgrade_detected",
        )
