Skip to content

Commit a68a22e

Browse files
committed
feat(verifier): add evaluate_with_collateral for attest-supplied DCAP collateral
Add Verifier::evaluate_with_collateral default method (ignores collateral, calls evaluate) + AttestationService::evaluate_with_collateral, so TNG can pass attest-carried DCAP collateral to native ecdsa_quote_verification and skip PCCS fetch. TDX native verifier overrides to use collateral; SGX keeps default (upstream). No struct changes (TdxEvidence/SgxEvidence/VerificationRequest unchanged). Assisted-by: Claude:glm-5.2
1 parent a55ec72 commit a68a22e

6 files changed

Lines changed: 197 additions & 19 deletions

File tree

‎attestation-service/src/lib.rs‎

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,33 @@ impl AttestationService {
372372
&self,
373373
verification_requests: Vec<VerificationRequest>,
374374
policy_ids: Vec<String>,
375+
) -> Result<String> {
376+
self.evaluate_inner(verification_requests, policy_ids, None)
377+
.await
378+
}
379+
380+
/// Verify with attest-supplied DCAP collateral for the primary (first) request.
381+
/// Like evaluate but the first request uses verifier.evaluate_with_collateral
382+
/// (skip PCCS fetch); subsequent requests use the original evaluate.
383+
#[cfg(feature = "tdx-dcap-rust")]
384+
pub async fn evaluate_with_collateral(
385+
&self,
386+
verification_requests: Vec<VerificationRequest>,
387+
policy_ids: Vec<String>,
388+
collateral: &Value,
389+
) -> Result<String> {
390+
self.evaluate_inner(verification_requests, policy_ids, Some(collateral))
391+
.await
392+
}
393+
394+
/// Shared logic for evaluate / evaluate_with_collateral.
395+
/// When first_request_collateral is Some, the primary (index 0) request uses
396+
/// verifier.evaluate_with_collateral; otherwise all use verifier.evaluate.
397+
async fn evaluate_inner(
398+
&self,
399+
verification_requests: Vec<VerificationRequest>,
400+
policy_ids: Vec<String>,
401+
first_request_collateral: Option<&Value>,
375402
) -> Result<String> {
376403
if verification_requests.is_empty() {
377404
return Err(AttestationError::InvalidRequest {
@@ -437,14 +464,54 @@ impl AttestationService {
437464
None => InitDataHash::NotProvided,
438465
};
439466

440-
let (claims_from_tee_evidence, tee_class) = verifier
441-
.evaluate(verification_request.evidence, &report_data, &init_data_hash)
442-
.await
443-
.map_err(|source| AttestationError::Verification {
444-
request_index,
445-
tee: verification_request.tee,
446-
source,
447-
})?;
467+
let (claims_from_tee_evidence, tee_class) = if request_index == 0 {
468+
#[cfg(feature = "tdx-dcap-rust")]
469+
if let Some(collateral) = first_request_collateral {
470+
verifier
471+
.evaluate_with_collateral(
472+
verification_request.evidence,
473+
&report_data,
474+
&init_data_hash,
475+
collateral,
476+
)
477+
.await
478+
.map_err(|source| AttestationError::Verification {
479+
request_index,
480+
tee: verification_request.tee,
481+
source,
482+
})?
483+
} else {
484+
verifier
485+
.evaluate(verification_request.evidence, &report_data, &init_data_hash)
486+
.await
487+
.map_err(|source| AttestationError::Verification {
488+
request_index,
489+
tee: verification_request.tee,
490+
source,
491+
})?
492+
}
493+
#[cfg(not(feature = "tdx-dcap-rust"))]
494+
{
495+
let _ = first_request_collateral;
496+
verifier
497+
.evaluate(verification_request.evidence, &report_data, &init_data_hash)
498+
.await
499+
.map_err(|source| AttestationError::Verification {
500+
request_index,
501+
tee: verification_request.tee,
502+
source,
503+
})?
504+
}
505+
} else {
506+
verifier
507+
.evaluate(verification_request.evidence, &report_data, &init_data_hash)
508+
.await
509+
.map_err(|source| AttestationError::Verification {
510+
request_index,
511+
tee: verification_request.tee,
512+
source,
513+
})?
514+
};
448515
info!(
449516
"{:?} Verifier/endorsement check passed.",
450517
verification_request.tee

‎deps/verifier/src/lib.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,21 @@ pub trait Verifier {
238238
expected_init_data_hash: &InitDataHash,
239239
) -> Result<(TeeEvidenceParsedClaim, TeeClass)>;
240240

241+
/// Verify with attest-supplied DCAP collateral (skip PCCS fetch).
242+
/// Default: ignore collateral, delegate to evaluate. TDX native overrides.
243+
#[cfg(feature = "tdx-dcap-rust")]
244+
async fn evaluate_with_collateral(
245+
&self,
246+
evidence: TeeEvidence,
247+
expected_report_data: &ReportData,
248+
expected_init_data_hash: &InitDataHash,
249+
collateral: &serde_json::Value,
250+
) -> Result<(TeeEvidenceParsedClaim, TeeClass)> {
251+
let _ = collateral;
252+
self.evaluate(evidence, expected_report_data, expected_init_data_hash)
253+
.await
254+
}
255+
241256
/// Generate the supplemental challenge
242257
///
243258
/// Some TEE like IBM SE need a `challenge` generated on verifier side

‎deps/verifier/src/tdx/mod.rs‎

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,50 @@ impl Verifier for Tdx {
115115
}
116116
})?;
117117

118-
let claims = verify_evidence(expected_report_data, expected_init_data_hash, tdx_evidence)
119-
.await
120-
.context("TDX verifier")?;
118+
let claims = verify_evidence(
119+
expected_report_data,
120+
expected_init_data_hash,
121+
tdx_evidence,
122+
None,
123+
)
124+
.await
125+
.context("TDX verifier")?;
121126

122127
Ok((claims, "cpu".to_string()))
123128
}
129+
130+
/// Verify with attest-supplied DCAP collateral (skip PCCS fetch).
131+
#[cfg(feature = "tdx-dcap-rust")]
132+
async fn evaluate_with_collateral(
133+
&self,
134+
evidence: TeeEvidence,
135+
expected_report_data: &ReportData,
136+
expected_init_data_hash: &InitDataHash,
137+
collateral: &serde_json::Value,
138+
) -> Result<(TeeEvidenceParsedClaim, TeeClass)> {
139+
let tdx_evidence = serde_json::from_value::<TdxEvidence>(evidence).map_err(|source| {
140+
VerifierError::InvalidEvidenceFormat {
141+
field: "evidence",
142+
source: source.into(),
143+
}
144+
})?;
145+
let claims = verify_evidence(
146+
expected_report_data,
147+
expected_init_data_hash,
148+
tdx_evidence,
149+
Some(collateral),
150+
)
151+
.await
152+
.context("TDX verifier")?;
153+
Ok((claims, "cpu".to_string()))
154+
}
124155
}
125156

126157
async fn verify_evidence(
127158
expected_report_data: &ReportData<'_>,
128159
expected_init_data_hash: &InitDataHash<'_>,
129160
evidence: TdxEvidence,
161+
collateral: Option<&serde_json::Value>,
130162
) -> Result<TeeEvidenceParsedClaim> {
131163
if evidence.quote.is_empty() {
132164
return Err(VerifierError::InvalidEvidenceFormat {
@@ -147,7 +179,17 @@ async fn verify_evidence(
147179
})?;
148180

149181
// Verify TD quote ECDSA signature.
150-
let tcb_verification_result = ecdsa_quote_verification(quote_bin.as_slice()).await?;
182+
let tcb_verification_result = {
183+
#[cfg(feature = "tdx-dcap-rust")]
184+
{
185+
ecdsa_quote_verification(quote_bin.as_slice(), collateral).await?
186+
}
187+
#[cfg(feature = "tdx-dcap-ffi")]
188+
{
189+
let _ = collateral;
190+
ecdsa_quote_verification(quote_bin.as_slice()).await?
191+
}
192+
};
151193

152194
info!(
153195
"Quote DCAP check succeeded. TCB status: {}",
@@ -418,6 +460,29 @@ mod tests {
418460
));
419461
}
420462

463+
/// evaluate_with_collateral uses the caller-supplied DCAP collateral
464+
/// (from test_data/tdx_collateral.json, fetched from PCCS for the
465+
/// prerecorded tdx_quote_4.dat) instead of contacting a PCCS.
466+
#[cfg(feature = "tdx-dcap-rust")]
467+
#[tokio::test]
468+
async fn evaluate_with_collateral_uses_supplied() {
469+
let quote_bin = std::fs::read("./test_data/tdx_quote_4.dat").expect("read quote");
470+
let quote_b64 = STANDARD.encode(&quote_bin);
471+
let collateral_json =
472+
std::fs::read("./test_data/tdx_collateral.json").expect("read collateral");
473+
let collateral: serde_json::Value =
474+
serde_json::from_slice(&collateral_json).expect("parse collateral");
475+
let result = Tdx::default()
476+
.evaluate_with_collateral(
477+
json!({ "quote": quote_b64 }),
478+
&ReportData::NotProvided,
479+
&InitDataHash::NotProvided,
480+
&collateral,
481+
)
482+
.await;
483+
assert!(result.is_ok(), "evaluate_with_collateral failed: {:?}", result.err());
484+
}
485+
421486
#[test]
422487
fn test_generate_parsed_claim() {
423488
let ccel_bin = fs::read("./test_data/CCEL_data").unwrap();

‎deps/verifier/src/tdx/verify/mod.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
//! Both backends expose the same entry point:
1717
//!
1818
//! ```ignore
19+
//! // tdx-dcap-rust backend (with optional collateral):
20+
//! pub async fn ecdsa_quote_verification(quote: &[u8], collateral: Option<&serde_json::Value>) -> Result<TcbVerificationResult>
21+
//! // tdx-dcap-ffi backend (upstream, no collateral):
1922
//! pub async fn ecdsa_quote_verification(quote: &[u8]) -> Result<TcbVerificationResult>
2023
//! ```
2124
//!
@@ -84,6 +87,9 @@ mod tests {
8487
#[case("./test_data/tdx_quote_5.dat")]
8588
async fn test_verify_tdx_quote(#[case] quote: &str) {
8689
let quote_bin = fs::read(quote).unwrap();
90+
#[cfg(feature = "tdx-dcap-rust")]
91+
let res = ecdsa_quote_verification(quote_bin.as_slice(), None).await;
92+
#[cfg(not(feature = "tdx-dcap-rust"))]
8793
let res = ecdsa_quote_verification(quote_bin.as_slice()).await;
8894
assert!(res.is_ok(), "{res:?}");
8995
let tcb_result = res.unwrap();

‎deps/verifier/src/tdx/verify/native.rs‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,10 @@ fn pccs_http_client() -> Result<reqwest::Client> {
806806
.map_err(|e| pccs_unavailable(anyhow!("failed to build HTTP client for PCCS: {e}")))
807807
}
808808

809-
pub async fn ecdsa_quote_verification(quote: &[u8]) -> Result<TcbVerificationResult> {
809+
pub async fn ecdsa_quote_verification(
810+
quote: &[u8],
811+
collateral: Option<&serde_json::Value>,
812+
) -> Result<TcbVerificationResult> {
810813
let pccs_urls = resolve_pccs_urls();
811814

812815
// The PCK certificate chain is embedded in the quote's certification data
@@ -828,13 +831,23 @@ pub async fn ecdsa_quote_verification(quote: &[u8]) -> Result<TcbVerificationRes
828831
})?;
829832
debug!("dcap-qvl backend: fmspc={fmspc}, ca={ca}, pccs={pccs_urls:?}");
830833

831-
let cache_key = CollateralCacheKey {
832-
pccs_base_urls: pccs_urls,
833-
fmspc: fmspc.clone(),
834-
ca: ca.to_string(),
834+
// Use attest-supplied collateral if present (skip PCCS); else fetch via PCCS cache.
835+
let collateral: QuoteCollateralV3 = match collateral {
836+
Some(v) => {
837+
debug!("Using attest-supplied collateral; skipping PCCS fetch.");
838+
serde_json::from_value(v.clone())
839+
.context("failed to deserialize attest-supplied collateral as QuoteCollateralV3")?
840+
}
841+
None => {
842+
let cache_key = CollateralCacheKey {
843+
pccs_base_urls: pccs_urls,
844+
fmspc: fmspc.clone(),
845+
ca: ca.to_string(),
846+
};
847+
let cache_policy = resolve_collateral_cache_policy();
848+
get_collateral(cache_key, pck_chain, cache_policy).await?
849+
}
835850
};
836-
let cache_policy = resolve_collateral_cache_policy();
837-
let collateral = get_collateral(cache_key, pck_chain, cache_policy).await?;
838851

839852
let real_now = SystemTime::now()
840853
.duration_since(UNIX_EPOCH)

0 commit comments

Comments
 (0)