Skip to content

Commit 3f4fa6f

Browse files
committed
attestation: simplify cbor schema shape
1 parent 3a69b7e commit 3f4fa6f

2 files changed

Lines changed: 42 additions & 48 deletions

File tree

dstack-attest/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ anyhow.workspace = true
1414
cc-eventlog.workspace = true
1515
ciborium.workspace = true
1616
dcap-qvl.workspace = true
17-
dstack-attestation-types = { path = "../../../dstack-attestation-types" }
17+
dstack-attestation-types = { path = "../../../src/attestation-types" }
1818
dstack-types.workspace = true
1919
ez-hash.workspace = true
2020
fs-err.workspace = true

dstack-attest/src/attestation.rs

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use dcap_qvl::{
1919
verify::VerifiedReport as TdxVerifiedReport,
2020
};
2121
use dstack_attestation_types::{
22-
bytes_value, text_value, AttestationV1, Attribute as V1Attribute, Extension as V1Extension,
23-
PlatformEvidence,
22+
bytes_value, text_value, Attestation as SchemaAttestation, Attribute as V1Attribute,
23+
Extension as V1Extension,
2424
};
2525
#[cfg(feature = "quote")]
2626
use dstack_types::SysConfig;
@@ -36,8 +36,8 @@ use sha2::Digest as _;
3636
const NATIVE_PLATFORM_TDX: &str = "tdx";
3737
const NATIVE_PLATFORM_GCP_TDX: &str = "gcp-tdx";
3838
const NATIVE_PLATFORM_NITRO_ENCLAVE: &str = "nitro-enclave";
39-
const DSTACK_VARIANT_DSTACK: &str = "dstack";
40-
const DSTACK_VARIANT_DSTACK_K8S: &str = "dstack-k8s";
39+
const DSTACK_PROFILE_DSTACK: &str = "dstack";
40+
const DSTACK_PROFILE_DSTACK_K8S: &str = "dstack-k8s";
4141
const DSTACK_TDX: &str = "dstack-tdx";
4242
const DSTACK_GCP_TDX: &str = "dstack-gcp-tdx";
4343
const DSTACK_NITRO_ENCLAVE: &str = "dstack-nitro-enclave";
@@ -130,7 +130,7 @@ fn optional_decoded_attribute<T: DeserializeOwned>(
130130
}
131131
}
132132

133-
fn report_data_payload_v1(attestation: &AttestationV1) -> Option<&str> {
133+
fn report_data_payload_v1(attestation: &SchemaAttestation) -> Option<&str> {
134134
match find_extension(&attestation.extensions, EXT_REPORT_DATA_PAYLOAD) {
135135
Some(CborValue::Text(text)) => Some(text.as_str()),
136136
_ => None,
@@ -143,9 +143,9 @@ fn is_cbor_map_prefix(byte: u8) -> bool {
143143

144144
fn legacy_into_v1(
145145
attestation: Attestation,
146-
variant: &str,
146+
profile: &str,
147147
extensions: Vec<V1Extension>,
148-
) -> AttestationV1 {
148+
) -> SchemaAttestation {
149149
let Attestation {
150150
quote,
151151
runtime_events,
@@ -159,21 +159,19 @@ fn legacy_into_v1(
159159
AttestationQuote::DstackGcpTdx => NATIVE_PLATFORM_GCP_TDX,
160160
AttestationQuote::DstackNitroEnclave => NATIVE_PLATFORM_NITRO_ENCLAVE,
161161
};
162-
let mut platform = PlatformEvidence::new(native_platform, variant);
162+
let mut v1 = SchemaAttestation::new(native_platform, profile);
163163
match quote {
164164
AttestationQuote::DstackTdx(TdxQuote { quote, event_log }) => {
165-
platform
166-
.basic_attributes
165+
v1.basic_attributes
167166
.push(V1Attribute::new(ATTR_TDX_QUOTE, bytes_value(quote)));
168-
platform.basic_attributes.push(V1Attribute::new(
167+
v1.basic_attributes.push(V1Attribute::new(
169168
ATTR_TDX_EVENT_LOG,
170169
to_cbor_value(&event_log).or_panic("TDX event log should encode to CBOR"),
171170
));
172171
}
173172
AttestationQuote::DstackGcpTdx | AttestationQuote::DstackNitroEnclave => {}
174173
}
175174

176-
let mut v1 = AttestationV1::new(platform);
177175
v1.basic_attributes.push(V1Attribute::new(
178176
ATTR_REPORT_DATA,
179177
bytes_value(report_data.to_vec()),
@@ -188,7 +186,7 @@ fn legacy_into_v1(
188186
v1
189187
}
190188

191-
fn v1_into_legacy(attestation: AttestationV1) -> Result<Attestation> {
189+
fn v1_into_legacy(attestation: SchemaAttestation) -> Result<Attestation> {
192190
let report_data_bytes =
193191
required_bytes_attribute(&attestation.basic_attributes, ATTR_REPORT_DATA)?;
194192
let report_data: [u8; 64] = report_data_bytes
@@ -200,20 +198,17 @@ fn v1_into_legacy(attestation: AttestationV1) -> Result<Attestation> {
200198
.unwrap_or_default();
201199
let config = optional_text_attribute(&attestation.optional_attributes, ATTR_CONFIG)?;
202200

203-
let quote = match attestation.platform.platform.as_str() {
201+
let quote = match attestation.platform.as_str() {
204202
NATIVE_PLATFORM_TDX => {
205-
let quote =
206-
required_bytes_attribute(&attestation.platform.basic_attributes, ATTR_TDX_QUOTE)?;
207-
let event_log: Vec<TdxEvent> = optional_decoded_attribute(
208-
&attestation.platform.basic_attributes,
209-
ATTR_TDX_EVENT_LOG,
210-
)?
211-
.unwrap_or_default();
203+
let quote = required_bytes_attribute(&attestation.basic_attributes, ATTR_TDX_QUOTE)?;
204+
let event_log: Vec<TdxEvent> =
205+
optional_decoded_attribute(&attestation.basic_attributes, ATTR_TDX_EVENT_LOG)?
206+
.unwrap_or_default();
212207
AttestationQuote::DstackTdx(TdxQuote { quote, event_log })
213208
}
214209
NATIVE_PLATFORM_GCP_TDX => AttestationQuote::DstackGcpTdx,
215210
NATIVE_PLATFORM_NITRO_ENCLAVE => AttestationQuote::DstackNitroEnclave,
216-
other => bail!("Unsupported platform in AttestationV1: {other}"),
211+
other => bail!("Unsupported platform in attestation schema: {other}"),
217212
};
218213

219214
Ok(Attestation {
@@ -225,11 +220,10 @@ fn v1_into_legacy(attestation: AttestationV1) -> Result<Attestation> {
225220
})
226221
}
227222

228-
fn strip_v1_attestation(mut attestation: AttestationV1) -> AttestationV1 {
229-
if let Some(event_log_value) = find_attribute_mut(
230-
&mut attestation.platform.basic_attributes,
231-
ATTR_TDX_EVENT_LOG,
232-
) {
223+
fn strip_v1_attestation(mut attestation: SchemaAttestation) -> SchemaAttestation {
224+
if let Some(event_log_value) =
225+
find_attribute_mut(&mut attestation.basic_attributes, ATTR_TDX_EVENT_LOG)
226+
{
233227
let event_log: Vec<TdxEvent> =
234228
from_cbor_value(event_log_value).or_panic("TDX event log should decode from CBOR");
235229
let stripped: Vec<_> = event_log
@@ -460,7 +454,7 @@ pub enum VersionedAttestation {
460454
/// Extensible CBOR-encoded attestation.
461455
V1 {
462456
/// The version 1 attestation envelope.
463-
attestation: AttestationV1,
457+
attestation: SchemaAttestation,
464458
},
465459
}
466460

@@ -506,11 +500,11 @@ impl VersionedAttestation {
506500
};
507501
}
508502
if is_cbor_map_prefix(first) {
509-
let attestation = AttestationV1::from_cbor(bytes)?;
503+
let attestation = SchemaAttestation::from_cbor(bytes)?;
510504
return Ok(Self::V1 { attestation });
511505
}
512506
if first == 0x01 && bytes.get(1).is_some_and(|byte| is_cbor_map_prefix(*byte)) {
513-
let attestation = AttestationV1::from_cbor(&bytes[1..])?;
507+
let attestation = SchemaAttestation::from_cbor(&bytes[1..])?;
514508
return Ok(Self::V1 { attestation });
515509
}
516510
bail!("Unknown attestation wire format");
@@ -525,7 +519,7 @@ impl VersionedAttestation {
525519
.encode(),
526520
Self::V1 { attestation } => attestation
527521
.to_cbor()
528-
.or_panic("AttestationV1 should encode as CBOR"),
522+
.or_panic("attestation schema should encode as CBOR"),
529523
}
530524
}
531525

@@ -601,7 +595,7 @@ impl VersionedAttestation {
601595
VersionedAttestation::V1 { attestation } => {
602596
let report_data_payload = report_data_payload_v1(&attestation).map(str::to_owned);
603597
let mut legacy = v1_into_legacy(attestation)
604-
.or_panic("AttestationV1 should project into legacy Attestation");
598+
.or_panic("attestation schema should project into legacy Attestation");
605599
patch_legacy_report_data(&mut legacy, report_data);
606600
match report_data_payload {
607601
Some(payload) => legacy.into_versioned_with_report_data_payload(payload),
@@ -970,20 +964,20 @@ impl Attestation {
970964
}
971965

972966
impl Attestation {
973-
pub fn into_v1(self) -> AttestationV1 {
974-
legacy_into_v1(self, DSTACK_VARIANT_DSTACK, Vec::new())
967+
pub fn into_v1(self) -> SchemaAttestation {
968+
legacy_into_v1(self, DSTACK_PROFILE_DSTACK, Vec::new())
975969
}
976970

977-
pub fn into_v1_with_extensions(self, extensions: Vec<V1Extension>) -> AttestationV1 {
978-
legacy_into_v1(self, DSTACK_VARIANT_DSTACK, extensions)
971+
pub fn into_v1_with_extensions(self, extensions: Vec<V1Extension>) -> SchemaAttestation {
972+
legacy_into_v1(self, DSTACK_PROFILE_DSTACK, extensions)
979973
}
980974

981-
pub fn into_v1_with_variant_and_extensions(
975+
pub fn into_v1_with_profile_and_extensions(
982976
self,
983-
variant: &str,
977+
profile: &str,
984978
extensions: Vec<V1Extension>,
985-
) -> AttestationV1 {
986-
legacy_into_v1(self, variant, extensions)
979+
) -> SchemaAttestation {
980+
legacy_into_v1(self, profile, extensions)
987981
}
988982

989983
/// Verify the quote with optional custom time (testing hook)
@@ -1023,8 +1017,8 @@ impl Attestation {
10231017
report_data_payload: String,
10241018
) -> VersionedAttestation {
10251019
VersionedAttestation::V1 {
1026-
attestation: self.into_v1_with_variant_and_extensions(
1027-
DSTACK_VARIANT_DSTACK_K8S,
1020+
attestation: self.into_v1_with_profile_and_extensions(
1021+
DSTACK_PROFILE_DSTACK_K8S,
10281022
vec![V1Extension::new(
10291023
EXT_REPORT_DATA_PAYLOAD,
10301024
true,
@@ -1224,8 +1218,8 @@ mod tests {
12241218
let VersionedAttestation::V1 { attestation } = decoded else {
12251219
panic!("expected V1 attestation");
12261220
};
1227-
assert_eq!(attestation.platform.platform, NATIVE_PLATFORM_TDX);
1228-
assert_eq!(attestation.platform.variant, DSTACK_VARIANT_DSTACK_K8S);
1221+
assert_eq!(attestation.platform, NATIVE_PLATFORM_TDX);
1222+
assert_eq!(attestation.profile, DSTACK_PROFILE_DSTACK_K8S);
12291223
}
12301224

12311225
#[test]
@@ -1238,9 +1232,9 @@ mod tests {
12381232
}
12391233

12401234
#[test]
1241-
fn legacy_v0_upgrade_uses_dstack_variant() {
1235+
fn legacy_v0_upgrade_uses_dstack_profile() {
12421236
let upgraded = dummy_tdx_attestation([3u8; 64]).into_v1();
1243-
assert_eq!(upgraded.platform.platform, NATIVE_PLATFORM_TDX);
1244-
assert_eq!(upgraded.platform.variant, DSTACK_VARIANT_DSTACK);
1237+
assert_eq!(upgraded.platform, NATIVE_PLATFORM_TDX);
1238+
assert_eq!(upgraded.profile, DSTACK_PROFILE_DSTACK);
12451239
}
12461240
}

0 commit comments

Comments
 (0)