Skip to content

Commit ac418e0

Browse files
committed
refactor(simulator): localize report data patching
1 parent aa48b04 commit ac418e0

4 files changed

Lines changed: 69 additions & 36 deletions

File tree

dstack-attest/src/attestation.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -272,25 +272,6 @@ impl VersionedAttestation {
272272
}
273273
}
274274

275-
/// Set the report_data field in the attestation and in the raw TDX quote bytes (offset 568..632).
276-
/// This is used by the simulator to patch a canned attestation with the correct report_data
277-
/// that binds to the actual TLS public key.
278-
pub fn set_report_data(&mut self, report_data: [u8; 64]) {
279-
let VersionedAttestation::V0 { attestation } = self;
280-
attestation.report_data = report_data;
281-
if let Some(tdx_quote) = attestation.tdx_quote_mut() {
282-
if tdx_quote.quote.len() >= TDX_QUOTE_REPORT_DATA_RANGE.end {
283-
tdx_quote.quote[TDX_QUOTE_REPORT_DATA_RANGE].copy_from_slice(&report_data);
284-
} else {
285-
tracing::warn!(
286-
"TDX quote too short to patch report_data ({} < {})",
287-
tdx_quote.quote.len(),
288-
TDX_QUOTE_REPORT_DATA_RANGE.end
289-
);
290-
}
291-
}
292-
}
293-
294275
/// Strip data for certificate embedding (e.g. keep RTMR3 event logs only).
295276
pub fn into_stripped(mut self) -> Self {
296277
let VersionedAttestation::V0 { attestation } = &mut self;

guest-agent-simulator/src/main.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,11 @@ impl PlatformBackend for SimulatorPlatform {
7171
simulator::simulated_quote_response(&self.attestation, report_data, vm_config)
7272
}
7373

74-
fn attest_response(&self, _report_data: [u8; 64]) -> Result<AttestResponse> {
75-
Ok(simulator::simulated_attest_response(&self.attestation))
74+
fn attest_response(&self, report_data: [u8; 64]) -> Result<AttestResponse> {
75+
Ok(simulator::simulated_attest_response(
76+
&self.attestation,
77+
report_data,
78+
))
7679
}
7780

7881
fn emit_event(&self, event: &str, _payload: &[u8]) -> Result<()> {
@@ -134,4 +137,14 @@ mod tests {
134137
assert!(cert_attestation.decode_app_info(false).is_ok());
135138
let _ = platform.attestation_for_info().unwrap();
136139
}
140+
141+
#[test]
142+
fn simulator_attest_response_uses_supplied_report_data() {
143+
let platform = load_fixture_platform();
144+
let report_data = [0x5a; 64];
145+
let response = platform.attest_response(report_data).unwrap();
146+
let patched = VersionedAttestation::from_scale(&response.attestation).unwrap();
147+
let VersionedAttestation::V0 { attestation } = patched;
148+
assert_eq!(attestation.report_data, report_data);
149+
}
137150
}

guest-agent-simulator/src/simulator.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ pub fn simulated_quote_response(
2727
report_data: [u8; 64],
2828
vm_config: &str,
2929
) -> Result<GetQuoteResponse> {
30-
let VersionedAttestation::V0 { attestation } = attestation.clone();
30+
let VersionedAttestation::V0 { attestation } = patch_report_data(attestation, report_data);
3131
let mut attestation = attestation;
3232
let Some(quote) = attestation.tdx_quote_mut() else {
3333
return Err(anyhow::anyhow!("Quote not found"));
3434
};
3535

36-
quote.quote[TDX_QUOTE_REPORT_DATA_RANGE].copy_from_slice(&report_data);
3736
Ok(GetQuoteResponse {
3837
quote: quote.quote.to_vec(),
3938
event_log: serde_json::to_string(&quote.event_log)
@@ -43,9 +42,12 @@ pub fn simulated_quote_response(
4342
})
4443
}
4544

46-
pub fn simulated_attest_response(attestation: &VersionedAttestation) -> AttestResponse {
45+
pub fn simulated_attest_response(
46+
attestation: &VersionedAttestation,
47+
report_data: [u8; 64],
48+
) -> AttestResponse {
4749
AttestResponse {
48-
attestation: attestation.to_scale(),
50+
attestation: patch_report_data(attestation, report_data).to_scale(),
4951
}
5052
}
5153

@@ -57,8 +59,27 @@ pub fn simulated_certificate_attestation(
5759
attestation: &VersionedAttestation,
5860
pubkey: &[u8],
5961
) -> VersionedAttestation {
60-
let mut attestation = attestation.clone();
6162
let report_data = QuoteContentType::RaTlsCert.to_report_data(pubkey);
62-
attestation.set_report_data(report_data);
63-
attestation
63+
patch_report_data(attestation, report_data)
64+
}
65+
66+
fn patch_report_data(
67+
attestation: &VersionedAttestation,
68+
report_data: [u8; 64],
69+
) -> VersionedAttestation {
70+
let VersionedAttestation::V0 { attestation } = attestation.clone();
71+
let mut attestation = attestation;
72+
attestation.report_data = report_data;
73+
if let Some(tdx_quote) = attestation.tdx_quote_mut() {
74+
if tdx_quote.quote.len() >= TDX_QUOTE_REPORT_DATA_RANGE.end {
75+
tdx_quote.quote[TDX_QUOTE_REPORT_DATA_RANGE].copy_from_slice(&report_data);
76+
} else {
77+
tracing::warn!(
78+
"TDX quote too short to patch report_data ({} < {})",
79+
tdx_quote.quote.len(),
80+
TDX_QUOTE_REPORT_DATA_RANGE.end
81+
);
82+
}
83+
}
84+
VersionedAttestation::V0 { attestation }
6485
}

guest-agent/src/rpc_service.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -807,17 +807,37 @@ pNs85uhOZE8z2jr8Pg==
807807
attestation: VersionedAttestation,
808808
}
809809

810+
fn patch_report_data(
811+
attestation: &VersionedAttestation,
812+
report_data: [u8; 64],
813+
) -> VersionedAttestation {
814+
let ra_tls::attestation::VersionedAttestation::V0 { attestation } = attestation.clone();
815+
let mut attestation = attestation;
816+
attestation.report_data = report_data;
817+
if let Some(tdx_quote) = attestation.tdx_quote_mut() {
818+
if tdx_quote.quote.len() >= ra_tls::attestation::TDX_QUOTE_REPORT_DATA_RANGE.end {
819+
tdx_quote.quote[ra_tls::attestation::TDX_QUOTE_REPORT_DATA_RANGE]
820+
.copy_from_slice(&report_data);
821+
} else {
822+
tracing::warn!(
823+
"TDX quote too short to patch report_data ({} < {})",
824+
tdx_quote.quote.len(),
825+
ra_tls::attestation::TDX_QUOTE_REPORT_DATA_RANGE.end
826+
);
827+
}
828+
}
829+
ra_tls::attestation::VersionedAttestation::V0 { attestation }
830+
}
831+
810832
impl PlatformBackend for TestSimulatorPlatform {
811833
fn attestation_for_info(&self) -> Result<ra_rpc::Attestation> {
812834
Ok(self.attestation.clone().into_inner())
813835
}
814836

815837
fn certificate_attestation(&self, pubkey: &[u8]) -> Result<VersionedAttestation> {
816-
let mut attestation = self.attestation.clone();
817838
let report_data =
818839
ra_tls::attestation::QuoteContentType::RaTlsCert.to_report_data(pubkey);
819-
attestation.set_report_data(report_data);
820-
Ok(attestation)
840+
Ok(patch_report_data(&self.attestation, report_data))
821841
}
822842

823843
fn quote_response(
@@ -826,13 +846,11 @@ pNs85uhOZE8z2jr8Pg==
826846
vm_config: &str,
827847
) -> Result<GetQuoteResponse> {
828848
let ra_tls::attestation::VersionedAttestation::V0 { attestation } =
829-
self.attestation.clone();
849+
patch_report_data(&self.attestation, report_data);
830850
let mut attestation = attestation;
831851
let Some(quote) = attestation.tdx_quote_mut() else {
832852
return Err(anyhow::anyhow!("Quote not found"));
833853
};
834-
quote.quote[ra_tls::attestation::TDX_QUOTE_REPORT_DATA_RANGE]
835-
.copy_from_slice(&report_data);
836854
Ok(GetQuoteResponse {
837855
quote: quote.quote.to_vec(),
838856
event_log: serde_json::to_string(&quote.event_log)
@@ -842,9 +860,9 @@ pNs85uhOZE8z2jr8Pg==
842860
})
843861
}
844862

845-
fn attest_response(&self, _report_data: [u8; 64]) -> Result<AttestResponse> {
863+
fn attest_response(&self, report_data: [u8; 64]) -> Result<AttestResponse> {
846864
Ok(AttestResponse {
847-
attestation: self.attestation.to_scale(),
865+
attestation: patch_report_data(&self.attestation, report_data).to_scale(),
848866
})
849867
}
850868

0 commit comments

Comments
 (0)