Skip to content

Commit 06aafe4

Browse files
authored
Merge pull request #61 from flashbots/peg/test-dcap-verification
Improve logic for test DCAP verification
2 parents 4939bd4 + 6aff240 commit 06aafe4

File tree

2 files changed

+44
-36
lines changed

2 files changed

+44
-36
lines changed

src/attestation/dcap.rs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,44 +24,45 @@ pub async fn verify_dcap_attestation(
2424
expected_input_data: [u8; 64],
2525
pccs_url: Option<String>,
2626
) -> Result<MultiMeasurements, DcapVerificationError> {
27-
let measurements = if cfg!(not(test)) {
28-
let now = std::time::SystemTime::now()
29-
.duration_since(std::time::UNIX_EPOCH)?
30-
.as_secs();
31-
let quote = Quote::parse(&input)?;
32-
tracing::info!("Verifying DCAP attestation: {quote:?}");
27+
let now = std::time::SystemTime::now()
28+
.duration_since(std::time::UNIX_EPOCH)?
29+
.as_secs();
30+
let quote = Quote::parse(&input)?;
31+
tracing::info!("Verifying DCAP attestation: {quote:?}");
3332

34-
let ca = quote.ca()?;
35-
let fmspc = hex::encode_upper(quote.fmspc()?);
36-
let collateral = get_collateral_for_fmspc(
37-
&pccs_url.clone().unwrap_or(PCS_URL.to_string()),
38-
fmspc,
39-
ca,
40-
false, // Indicates not SGX
41-
)
42-
.await?;
43-
44-
let _verified_report = dcap_qvl::verify::verify(&input, &collateral, now)?;
33+
let ca = quote.ca()?;
34+
let fmspc = hex::encode_upper(quote.fmspc()?);
35+
let collateral = get_collateral_for_fmspc(
36+
&pccs_url.clone().unwrap_or(PCS_URL.to_string()),
37+
fmspc,
38+
ca,
39+
false, // Indicates not SGX
40+
)
41+
.await?;
4542

46-
let measurements = MultiMeasurements::from_dcap_qvl_quote(&quote)?;
43+
let _verified_report = dcap_qvl::verify::verify(&input, &collateral, now)?;
4744

48-
if get_quote_input_data(quote.report) != expected_input_data {
49-
return Err(DcapVerificationError::InputMismatch);
50-
}
51-
measurements
52-
} else {
53-
// In tests we use mock quotes which will fail to verify
54-
let quote = tdx_quote::Quote::from_bytes(&input)?;
55-
if quote.report_input_data() != expected_input_data {
56-
return Err(DcapVerificationError::InputMismatch);
57-
}
45+
let measurements = MultiMeasurements::from_dcap_qvl_quote(&quote)?;
5846

59-
MultiMeasurements::from_tdx_quote(&quote)
60-
};
47+
if get_quote_input_data(quote.report) != expected_input_data {
48+
return Err(DcapVerificationError::InputMismatch);
49+
}
6150

6251
Ok(measurements)
6352
}
6453

54+
pub fn mock_verify_dcap(
55+
input: Vec<u8>,
56+
expected_input_data: [u8; 64],
57+
) -> Result<MultiMeasurements, DcapVerificationError> {
58+
// In tests we use mock quotes which will fail to verify
59+
let quote = tdx_quote::Quote::from_bytes(&input)?;
60+
if quote.report_input_data() != expected_input_data {
61+
return Err(DcapVerificationError::InputMismatch);
62+
}
63+
Ok(MultiMeasurements::from_tdx_quote(&quote))
64+
}
65+
6566
/// Create a mock quote for testing on non-confidential hardware
6667
#[cfg(test)]
6768
fn generate_quote(input: [u8; 64]) -> Result<Vec<u8>, QuoteGenerationError> {

src/attestation/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,19 @@ impl AttestationVerifier {
285285
.await?
286286
}
287287
_ => {
288-
dcap::verify_dcap_attestation(
289-
attestation_exchange_message.attestation,
290-
expected_input_data,
291-
self.pccs_url.clone(),
292-
)
293-
.await?
288+
if cfg!(test) {
289+
dcap::mock_verify_dcap(
290+
attestation_exchange_message.attestation,
291+
expected_input_data,
292+
)?
293+
} else {
294+
dcap::verify_dcap_attestation(
295+
attestation_exchange_message.attestation,
296+
expected_input_data,
297+
self.pccs_url.clone(),
298+
)
299+
.await?
300+
}
294301
}
295302
};
296303

0 commit comments

Comments
 (0)