Skip to content
Merged
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
83 changes: 75 additions & 8 deletions attestation-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,33 @@ impl AttestationService {
&self,
verification_requests: Vec<VerificationRequest>,
policy_ids: Vec<String>,
) -> Result<String> {
self.evaluate_inner(verification_requests, policy_ids, None)
.await
}

/// Verify with attest-supplied DCAP collateral for the primary (first) request.
/// Like evaluate but the first request uses verifier.evaluate_with_collateral
/// (skip PCCS fetch); subsequent requests use the original evaluate.
#[cfg(feature = "tdx-dcap-rust")]
pub async fn evaluate_with_collateral(
&self,
verification_requests: Vec<VerificationRequest>,
policy_ids: Vec<String>,
collateral: &Value,
) -> Result<String> {
self.evaluate_inner(verification_requests, policy_ids, Some(collateral))
.await
}

/// Shared logic for evaluate / evaluate_with_collateral.
/// When first_request_collateral is Some, the primary (index 0) request uses
/// verifier.evaluate_with_collateral; otherwise all use verifier.evaluate.
async fn evaluate_inner(
&self,
verification_requests: Vec<VerificationRequest>,
policy_ids: Vec<String>,
first_request_collateral: Option<&Value>,
) -> Result<String> {
if verification_requests.is_empty() {
return Err(AttestationError::InvalidRequest {
Expand Down Expand Up @@ -437,14 +464,54 @@ impl AttestationService {
None => InitDataHash::NotProvided,
};

let (claims_from_tee_evidence, tee_class) = verifier
.evaluate(verification_request.evidence, &report_data, &init_data_hash)
.await
.map_err(|source| AttestationError::Verification {
request_index,
tee: verification_request.tee,
source,
})?;
let (claims_from_tee_evidence, tee_class) = if request_index == 0 {
#[cfg(feature = "tdx-dcap-rust")]
if let Some(collateral) = first_request_collateral {
verifier
.evaluate_with_collateral(
verification_request.evidence,
&report_data,
&init_data_hash,
collateral,
)
.await
.map_err(|source| AttestationError::Verification {
request_index,
tee: verification_request.tee,
source,
})?
} else {
verifier
.evaluate(verification_request.evidence, &report_data, &init_data_hash)
.await
.map_err(|source| AttestationError::Verification {
request_index,
tee: verification_request.tee,
source,
})?
}
#[cfg(not(feature = "tdx-dcap-rust"))]
{
let _ = first_request_collateral;
verifier
.evaluate(verification_request.evidence, &report_data, &init_data_hash)
.await
.map_err(|source| AttestationError::Verification {
request_index,
tee: verification_request.tee,
source,
})?
}
} else {
verifier
.evaluate(verification_request.evidence, &report_data, &init_data_hash)
.await
.map_err(|source| AttestationError::Verification {
request_index,
tee: verification_request.tee,
source,
})?
};
info!(
"{:?} Verifier/endorsement check passed.",
verification_request.tee
Expand Down
15 changes: 15 additions & 0 deletions deps/verifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,21 @@ pub trait Verifier {
expected_init_data_hash: &InitDataHash,
) -> Result<(TeeEvidenceParsedClaim, TeeClass)>;

/// Verify with attest-supplied DCAP collateral (skip PCCS fetch).
/// Default: ignore collateral, delegate to evaluate. TDX native overrides.
#[cfg(feature = "tdx-dcap-rust")]
async fn evaluate_with_collateral(
&self,
evidence: TeeEvidence,
expected_report_data: &ReportData,
expected_init_data_hash: &InitDataHash,
collateral: &serde_json::Value,
) -> Result<(TeeEvidenceParsedClaim, TeeClass)> {
let _ = collateral;
self.evaluate(evidence, expected_report_data, expected_init_data_hash)
.await
}

/// Generate the supplemental challenge
///
/// Some TEE like IBM SE need a `challenge` generated on verifier side
Expand Down
77 changes: 73 additions & 4 deletions deps/verifier/src/tdx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,41 @@ impl Verifier for Tdx {
}
})?;

let claims = verify_evidence(expected_report_data, expected_init_data_hash, tdx_evidence)
.await
.context("TDX verifier")?;
let claims = verify_evidence(
expected_report_data,
expected_init_data_hash,
tdx_evidence,
None,
)
.await
.context("TDX verifier")?;

Ok((claims, "cpu".to_string()))
}

/// Verify with attest-supplied DCAP collateral (skip PCCS fetch).
#[cfg(feature = "tdx-dcap-rust")]
async fn evaluate_with_collateral(
&self,
evidence: TeeEvidence,
expected_report_data: &ReportData,
expected_init_data_hash: &InitDataHash,
collateral: &serde_json::Value,
) -> Result<(TeeEvidenceParsedClaim, TeeClass)> {
let tdx_evidence = serde_json::from_value::<TdxEvidence>(evidence).map_err(|source| {
VerifierError::InvalidEvidenceFormat {
field: "evidence",
source: source.into(),
}
})?;
let claims = verify_evidence(
expected_report_data,
expected_init_data_hash,
tdx_evidence,
Some(collateral),
)
.await
.context("TDX verifier")?;
Ok((claims, "cpu".to_string()))
}
}
Expand All @@ -127,6 +158,7 @@ async fn verify_evidence(
expected_report_data: &ReportData<'_>,
expected_init_data_hash: &InitDataHash<'_>,
evidence: TdxEvidence,
collateral: Option<&serde_json::Value>,
) -> Result<TeeEvidenceParsedClaim> {
if evidence.quote.is_empty() {
return Err(VerifierError::InvalidEvidenceFormat {
Expand All @@ -147,7 +179,17 @@ async fn verify_evidence(
})?;

// Verify TD quote ECDSA signature.
let tcb_verification_result = ecdsa_quote_verification(quote_bin.as_slice()).await?;
let tcb_verification_result = {
#[cfg(feature = "tdx-dcap-rust")]
{
ecdsa_quote_verification(quote_bin.as_slice(), collateral).await?
}
#[cfg(feature = "tdx-dcap-ffi")]
{
let _ = collateral;
ecdsa_quote_verification(quote_bin.as_slice()).await?
}
};

info!(
"Quote DCAP check succeeded. TCB status: {}",
Expand Down Expand Up @@ -418,6 +460,33 @@ mod tests {
));
}

/// evaluate_with_collateral uses the caller-supplied DCAP collateral
/// (from test_data/tdx_collateral.json, fetched from PCCS for the
/// prerecorded tdx_quote_4.dat) instead of contacting a PCCS.
#[cfg(feature = "tdx-dcap-rust")]
#[tokio::test]
async fn evaluate_with_collateral_uses_supplied() {
let quote_bin = std::fs::read("./test_data/tdx_quote_4.dat").expect("read quote");
let quote_b64 = STANDARD.encode(&quote_bin);
let collateral_json =
std::fs::read("./test_data/tdx_collateral.json").expect("read collateral");
let collateral: serde_json::Value =
serde_json::from_slice(&collateral_json).expect("parse collateral");
let result = Tdx::default()
.evaluate_with_collateral(
json!({ "quote": quote_b64 }),
&ReportData::NotProvided,
&InitDataHash::NotProvided,
&collateral,
)
.await;
assert!(
result.is_ok(),
"evaluate_with_collateral failed: {:?}",
result.err()
);
}

#[test]
fn test_generate_parsed_claim() {
let ccel_bin = fs::read("./test_data/CCEL_data").unwrap();
Expand Down
6 changes: 6 additions & 0 deletions deps/verifier/src/tdx/verify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
//! Both backends expose the same entry point:
//!
//! ```ignore
//! // tdx-dcap-rust backend (with optional collateral):
//! pub async fn ecdsa_quote_verification(quote: &[u8], collateral: Option<&serde_json::Value>) -> Result<TcbVerificationResult>
//! // tdx-dcap-ffi backend (upstream, no collateral):
//! pub async fn ecdsa_quote_verification(quote: &[u8]) -> Result<TcbVerificationResult>
//! ```
//!
Expand Down Expand Up @@ -84,6 +87,9 @@ mod tests {
#[case("./test_data/tdx_quote_5.dat")]
async fn test_verify_tdx_quote(#[case] quote: &str) {
let quote_bin = fs::read(quote).unwrap();
#[cfg(feature = "tdx-dcap-rust")]
let res = ecdsa_quote_verification(quote_bin.as_slice(), None).await;
#[cfg(not(feature = "tdx-dcap-rust"))]
let res = ecdsa_quote_verification(quote_bin.as_slice()).await;
assert!(res.is_ok(), "{res:?}");
let tcb_result = res.unwrap();
Expand Down
27 changes: 20 additions & 7 deletions deps/verifier/src/tdx/verify/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,10 @@ fn pccs_http_client() -> Result<reqwest::Client> {
.map_err(|e| pccs_unavailable(anyhow!("failed to build HTTP client for PCCS: {e}")))
}

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

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

let cache_key = CollateralCacheKey {
pccs_base_urls: pccs_urls,
fmspc: fmspc.clone(),
ca: ca.to_string(),
// Use attest-supplied collateral if present (skip PCCS); else fetch via PCCS cache.
let collateral: QuoteCollateralV3 = match collateral {
Some(v) => {
debug!("Using attest-supplied collateral; skipping PCCS fetch.");
serde_json::from_value(v.clone())
.context("failed to deserialize attest-supplied collateral as QuoteCollateralV3")?
}
None => {
let cache_key = CollateralCacheKey {
pccs_base_urls: pccs_urls,
fmspc: fmspc.clone(),
ca: ca.to_string(),
};
let cache_policy = resolve_collateral_cache_policy();
get_collateral(cache_key, pck_chain, cache_policy).await?
}
};
let cache_policy = resolve_collateral_cache_policy();
let collateral = get_collateral(cache_key, pck_chain, cache_policy).await?;

let real_now = SystemTime::now()
.duration_since(UNIX_EPOCH)
Expand Down
Loading
Loading