Skip to content

Commit 3a69b7e

Browse files
committed
attestation: rename wire format helpers
1 parent 6710bb8 commit 3a69b7e

10 files changed

Lines changed: 36 additions & 26 deletions

File tree

dstack-attest/src/attestation.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,11 @@ pub enum VersionedAttestation {
466466

467467
impl Encode for VersionedAttestation {
468468
fn size_hint(&self) -> usize {
469-
self.to_scale().len()
469+
self.to_bytes().len()
470470
}
471471

472472
fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
473-
dest.write(&self.to_scale());
473+
dest.write(&self.to_bytes());
474474
}
475475
}
476476

@@ -483,7 +483,7 @@ impl Decode for VersionedAttestation {
483483
};
484484
let mut bytes = vec![0u8; remaining_len];
485485
input.read(&mut bytes)?;
486-
Self::from_scale(&bytes).map_err(|err| {
486+
Self::from_bytes(&bytes).map_err(|err| {
487487
ScaleError::from(std::io::Error::new(
488488
std::io::ErrorKind::InvalidData,
489489
err.to_string(),
@@ -494,30 +494,30 @@ impl Decode for VersionedAttestation {
494494

495495
impl VersionedAttestation {
496496
/// Decode versioned attestation bytes.
497-
pub fn from_scale(scale: &[u8]) -> Result<Self> {
498-
let Some(first) = scale.first().copied() else {
497+
pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
498+
let Some(first) = bytes.first().copied() else {
499499
bail!("Empty attestation bytes");
500500
};
501501
if first == 0x00 {
502-
let legacy = LegacyVersionedAttestation::decode(&mut &scale[..])
502+
let legacy = LegacyVersionedAttestation::decode(&mut &bytes[..])
503503
.context("Failed to decode legacy VersionedAttestation")?;
504504
return match legacy {
505505
LegacyVersionedAttestation::V0 { attestation } => Ok(Self::V0 { attestation }),
506506
};
507507
}
508508
if is_cbor_map_prefix(first) {
509-
let attestation = AttestationV1::from_cbor(scale)?;
509+
let attestation = AttestationV1::from_cbor(bytes)?;
510510
return Ok(Self::V1 { attestation });
511511
}
512-
if first == 0x01 && scale.get(1).is_some_and(|byte| is_cbor_map_prefix(*byte)) {
513-
let attestation = AttestationV1::from_cbor(&scale[1..])?;
512+
if first == 0x01 && bytes.get(1).is_some_and(|byte| is_cbor_map_prefix(*byte)) {
513+
let attestation = AttestationV1::from_cbor(&bytes[1..])?;
514514
return Ok(Self::V1 { attestation });
515515
}
516516
bail!("Unknown attestation wire format");
517517
}
518518

519519
/// Encode versioned attestation bytes.
520-
pub fn to_scale(&self) -> Vec<u8> {
520+
pub fn to_bytes(&self) -> Vec<u8> {
521521
match self {
522522
Self::V0 { attestation } => LegacyVersionedAttestation::V0 {
523523
attestation: attestation.clone(),
@@ -529,6 +529,16 @@ impl VersionedAttestation {
529529
}
530530
}
531531

532+
#[doc(hidden)]
533+
pub fn from_scale(bytes: &[u8]) -> Result<Self> {
534+
Self::from_bytes(bytes)
535+
}
536+
537+
#[doc(hidden)]
538+
pub fn to_scale(&self) -> Vec<u8> {
539+
self.to_bytes()
540+
}
541+
532542
/// Try to project any version into the legacy attestation structure.
533543
pub fn try_into_inner(self) -> Result<Attestation> {
534544
match self {
@@ -1206,9 +1216,9 @@ mod tests {
12061216
let payload = r#"{"pod_uid":"abc","workload_id":"default/app"}"#.to_string();
12071217
let encoded = dummy_tdx_attestation(report_data)
12081218
.into_versioned_with_report_data_payload(payload.clone())
1209-
.to_scale();
1219+
.to_bytes();
12101220
assert!(matches!(encoded.first(), Some(0xa0..=0xbf)));
1211-
let decoded = VersionedAttestation::from_scale(&encoded).expect("decode attestation");
1221+
let decoded = VersionedAttestation::from_bytes(&encoded).expect("decode attestation");
12121222
assert_eq!(decoded.report_data_payload(), Some(payload.as_str()));
12131223
assert_eq!(decoded.clone().into_inner().report_data, report_data);
12141224
let VersionedAttestation::V1 { attestation } = decoded else {

guest-agent-simulator/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ mod tests {
172172
let platform = load_fixture_platform();
173173
let report_data = [0x5a; 64];
174174
let response = platform.attest_response(report_data).unwrap();
175-
let patched = VersionedAttestation::from_scale(&response.attestation).unwrap();
175+
let patched = VersionedAttestation::from_bytes(&response.attestation).unwrap();
176176
let attestation = patched.into_inner();
177177
assert_eq!(attestation.report_data, report_data);
178178
}
@@ -188,7 +188,7 @@ mod tests {
188188
let platform = SimulatorPlatform::new(fixture, false);
189189
let report_data = [0x5a; 64];
190190
let response = platform.attest_response(report_data).unwrap();
191-
let patched = VersionedAttestation::from_scale(&response.attestation).unwrap();
191+
let patched = VersionedAttestation::from_bytes(&response.attestation).unwrap();
192192
let attestation = patched.into_inner();
193193
assert_eq!(attestation.report_data, original);
194194
assert_ne!(attestation.report_data, report_data);

guest-agent-simulator/src/simulator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn load_versioned_attestation(path: impl AsRef<Path>) -> Result<VersionedAtt
1919
path.display()
2020
)
2121
})?;
22-
VersionedAttestation::from_scale(&attestation_bytes)
22+
VersionedAttestation::from_bytes(&attestation_bytes)
2323
.context("Failed to decode simulator attestation")
2424
}
2525

@@ -51,7 +51,7 @@ pub fn simulated_attest_response(
5151
) -> AttestResponse {
5252
AttestResponse {
5353
attestation: maybe_patch_report_data(attestation, report_data, patch_report_data, "attest")
54-
.to_scale(),
54+
.to_bytes(),
5555
}
5656
}
5757

guest-agent/src/backend.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl PlatformBackend for RealPlatform {
4646
fn attest_response(&self, report_data: [u8; 64]) -> Result<AttestResponse> {
4747
let attestation = Attestation::quote(&report_data).context("Failed to get attestation")?;
4848
Ok(AttestResponse {
49-
attestation: attestation.into_versioned().to_scale(),
49+
attestation: attestation.into_versioned().to_bytes(),
5050
})
5151
}
5252

guest-agent/src/rpc_service.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ pNs85uhOZE8z2jr8Pg==
849849

850850
fn attest_response(&self, report_data: [u8; 64]) -> Result<AttestResponse> {
851851
Ok(AttestResponse {
852-
attestation: patch_report_data(&self.attestation, report_data).to_scale(),
852+
attestation: patch_report_data(&self.attestation, report_data).to_bytes(),
853853
})
854854
}
855855

@@ -865,7 +865,7 @@ pNs85uhOZE8z2jr8Pg==
865865
cert_client: dummy_cert_client,
866866
demo_cert: RwLock::new(String::new()),
867867
platform: Arc::new(TestSimulatorPlatform {
868-
attestation: VersionedAttestation::from_scale(
868+
attestation: VersionedAttestation::from_bytes(
869869
&std::fs::read(temp_attestation_file.path()).unwrap(),
870870
)
871871
.unwrap(),

kms/src/main_service/upgrade_authority.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub(crate) async fn local_kms_boot_info(pccs_url: Option<&str>) -> Result<BootIn
7676
let response = app_attest(pad64([0u8; 32]))
7777
.await
7878
.context("Failed to get local KMS attestation")?;
79-
let attestation = VersionedAttestation::from_scale(&response.attestation)
79+
let attestation = VersionedAttestation::from_bytes(&response.attestation)
8080
.context("Failed to decode local KMS attestation")?
8181
.into_inner();
8282
let verified = attestation

kms/src/onboard_service.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl OnboardRpc for OnboardHandler {
111111
.context("Failed to get attestation")?;
112112

113113
// Decode and verify the attestation to get real device ID
114-
let attestation = VersionedAttestation::from_scale(&response.attestation)
114+
let attestation = VersionedAttestation::from_bytes(&response.attestation)
115115
.context("Failed to decode attestation")?
116116
.into_inner();
117117
let attestation_mode = serde_json::to_value(attestation.quote.mode())
@@ -195,7 +195,7 @@ impl Keys {
195195
let response = app_attest(report_data.to_vec())
196196
.await
197197
.context("Failed to get quote")?;
198-
let attestation = VersionedAttestation::from_scale(&response.attestation)
198+
let attestation = VersionedAttestation::from_bytes(&response.attestation)
199199
.context("Invalid attestation")?;
200200

201201
// Sign WWW server cert with KMS cert
@@ -376,7 +376,7 @@ async fn gen_ra_cert(ca_cert_pem: String, ca_key_pem: String) -> Result<(String,
376376
.await
377377
.context("Failed to get quote")?;
378378
let attestation =
379-
VersionedAttestation::from_scale(&response.attestation).context("Invalid attestation")?;
379+
VersionedAttestation::from_bytes(&response.attestation).context("Invalid attestation")?;
380380
let req = CertRequest::builder()
381381
.subject("RA-TLS TEMP Cert")
382382
.attestation(&attestation)

ra-tls/src/attestation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn from_ext_getter(
2727
) -> Result<Option<Attestation>> {
2828
// Try to detect attestation mode from certificate extension
2929
if let Some(attestation_bytes) = get_ext(oids::PHALA_RATLS_ATTESTATION)? {
30-
let attestation = VersionedAttestation::from_scale(&attestation_bytes)
30+
let attestation = VersionedAttestation::from_bytes(&attestation_bytes)
3131
.context("Failed to decode attestation from cert extension")?
3232
.into_inner();
3333
return Ok(Some(attestation));

ra-tls/src/cert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ impl<Key> CertRequest<'_, Key> {
388388
add_ext(&mut params, PHALA_RATLS_CERT_USAGE, usage);
389389
}
390390
if let Some(ver_att) = self.attestation {
391-
let attestation_bytes = ver_att.clone().into_stripped().to_scale();
391+
let attestation_bytes = ver_att.clone().into_stripped().to_bytes();
392392
add_ext(&mut params, PHALA_RATLS_ATTESTATION, &attestation_bytes);
393393
}
394394
if let Some(ca_level) = self.ca_level {

verifier/src/verification.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ impl CvmVerifier {
389389

390390
pub async fn verify(&self, request: VerificationRequest) -> Result<VerificationResponse> {
391391
let attestation = if let Some(attestation) = &request.attestation {
392-
VersionedAttestation::from_scale(attestation).context("Failed to decode attestaion")?
392+
VersionedAttestation::from_bytes(attestation).context("Failed to decode attestaion")?
393393
} else if let Some(tdx_quote) = request.quote {
394394
let event_log = request
395395
.event_log

0 commit comments

Comments
 (0)