-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathv1.rs
More file actions
252 lines (228 loc) · 7.32 KB
/
Copy pathv1.rs
File metadata and controls
252 lines (228 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// SPDX-FileCopyrightText: © 2024-2025 Phala Network <dstack@phala.network>
//
// SPDX-License-Identifier: Apache-2.0
use anyhow::{anyhow, bail, Context, Result};
use cc_eventlog::{RuntimeEvent, TdxEvent};
use serde::{Deserialize, Serialize};
pub const ATTESTATION_VERSION: u64 = 1;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", content = "data")]
pub enum PlatformEvidence {
#[serde(rename = "tdx")]
Tdx {
quote: Vec<u8>,
event_log: Vec<TdxEvent>,
},
#[serde(rename = "gcp-tdx")]
GcpTdx,
#[serde(rename = "nitro-enclave")]
NitroEnclave,
}
impl PlatformEvidence {
pub fn tdx_quote(&self) -> Option<&[u8]> {
match self {
Self::Tdx { quote, .. } => Some(quote.as_slice()),
_ => None,
}
}
pub fn tdx_event_log(&self) -> Option<&[TdxEvent]> {
match self {
Self::Tdx { event_log, .. } => Some(event_log.as_slice()),
_ => None,
}
}
pub fn into_stripped(self) -> Self {
match self {
Self::Tdx { quote, event_log } => Self::Tdx {
quote,
event_log: event_log
.into_iter()
.filter(|event| event.imr == 3)
.map(|event| event.stripped())
.collect(),
},
other => other,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", content = "data")]
pub enum StackEvidence {
#[serde(rename = "dstack")]
Dstack {
report_data: Vec<u8>,
runtime_events: Vec<RuntimeEvent>,
config: String,
},
#[serde(rename = "dstack-pod")]
DstackPod {
report_data: Vec<u8>,
runtime_events: Vec<RuntimeEvent>,
config: String,
report_data_payload: String,
},
}
fn decode_report_data(report_data: &[u8]) -> Result<[u8; 64]> {
report_data
.try_into()
.map_err(|_| anyhow!("stack.report_data must be 64 bytes"))
}
impl StackEvidence {
pub fn report_data(&self) -> Result<[u8; 64]> {
match self {
Self::Dstack { report_data, .. } | Self::DstackPod { report_data, .. } => {
decode_report_data(report_data)
}
}
}
pub fn runtime_events(&self) -> &[RuntimeEvent] {
match self {
Self::Dstack { runtime_events, .. } | Self::DstackPod { runtime_events, .. } => {
runtime_events.as_slice()
}
}
}
pub fn config(&self) -> &str {
match self {
Self::Dstack { config, .. } | Self::DstackPod { config, .. } => config,
}
}
pub fn report_data_payload(&self) -> Option<&str> {
match self {
Self::Dstack { .. } => None,
Self::DstackPod {
report_data_payload,
..
} => Some(report_data_payload.as_str()),
}
}
pub fn into_dstack_pod(self, report_data_payload: String) -> Self {
match self {
Self::Dstack {
report_data,
runtime_events,
config,
}
| Self::DstackPod {
report_data,
runtime_events,
config,
..
} => Self::DstackPod {
report_data,
runtime_events,
config,
report_data_payload,
},
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Attestation {
pub version: u64,
pub platform: PlatformEvidence,
pub stack: StackEvidence,
}
impl Attestation {
pub fn new(platform: PlatformEvidence, stack: StackEvidence) -> Self {
Self {
version: ATTESTATION_VERSION,
platform,
stack,
}
}
pub fn to_msgpack(&self) -> Result<Vec<u8>> {
let mut normalized = self.clone();
normalized.version = ATTESTATION_VERSION;
rmp_serde::to_vec_named(&normalized).context("failed to encode attestation as msgpack")
}
pub fn from_msgpack(bytes: &[u8]) -> Result<Self> {
let value: Self =
rmp_serde::from_slice(bytes).context("failed to decode attestation from msgpack")?;
if value.version != ATTESTATION_VERSION {
bail!(
"unsupported attestation version: expected {}, got {}",
ATTESTATION_VERSION,
value.version
);
}
Ok(value)
}
pub fn report_data(&self) -> Result<[u8; 64]> {
self.stack.report_data()
}
pub fn report_data_payload(&self) -> Option<&str> {
self.stack.report_data_payload()
}
pub fn into_stripped(self) -> Self {
Self {
version: self.version,
platform: self.platform.into_stripped(),
stack: self.stack,
}
}
pub fn into_dstack_pod(self, report_data_payload: String) -> Self {
Self {
version: self.version,
platform: self.platform,
stack: self.stack.into_dstack_pod(report_data_payload),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn msgpack_roundtrip_preserves_attestation() {
let attestation = Attestation::new(
PlatformEvidence::Tdx {
quote: vec![1u8, 2, 3],
event_log: vec![TdxEvent {
imr: 3,
event_type: 0x08000001,
digest: vec![0xaa, 0xbb, 0xcc],
event: "pod".into(),
event_payload: vec![0xde, 0xad, 0xbe, 0xef],
}],
},
StackEvidence::DstackPod {
report_data: vec![7u8; 64],
runtime_events: vec![RuntimeEvent {
event: "pod".into(),
payload: vec![0xca, 0xfe, 0xba, 0xbe],
}],
config: "{}".into(),
report_data_payload: "{\"hello\":\"world\"}".into(),
},
);
let encoded = attestation.to_msgpack().expect("encode msgpack");
assert!(matches!(encoded.first(), Some(0x80..=0x8f)));
let decoded = Attestation::from_msgpack(&encoded).expect("decode msgpack");
assert_eq!(decoded.version, ATTESTATION_VERSION);
match decoded.platform {
PlatformEvidence::Tdx { quote, event_log } => {
assert_eq!(quote, vec![1u8, 2, 3]);
assert_eq!(event_log.len(), 1);
assert_eq!(event_log[0].event, "pod");
assert_eq!(event_log[0].event_payload, vec![0xde, 0xad, 0xbe, 0xef]);
}
_ => panic!("expected tdx platform evidence"),
}
match decoded.stack {
StackEvidence::DstackPod {
report_data,
runtime_events,
config,
report_data_payload,
} => {
assert_eq!(report_data, vec![7u8; 64]);
assert_eq!(runtime_events.len(), 1);
assert_eq!(runtime_events[0].event, "pod");
assert_eq!(runtime_events[0].payload, vec![0xca, 0xfe, 0xba, 0xbe]);
assert_eq!(config, "{}");
assert_eq!(report_data_payload, "{\"hello\":\"world\"}");
}
_ => panic!("expected dstack-pod stack evidence"),
}
}
}