1use bitfield_struct::bitfield;
9use open_enum::open_enum;
10use zerocopy::FromBytes;
11use zerocopy::Immutable;
12use zerocopy::IntoBytes;
13use zerocopy::KnownLayout;
14
15const ATTESTATION_VERSION: u32 = 2;
16const ATTESTATION_SIGNATURE: u32 = 0x414c4348; const ATTESTATION_REPORT_SIZE_MAX: usize = SNP_VM_REPORT_SIZE;
20
21pub const VBS_VM_REPORT_SIZE: usize = hvdef::vbs::VBS_REPORT_SIZE;
22pub const SNP_VM_REPORT_SIZE: usize = x86defs::snp::SNP_REPORT_SIZE;
23pub const TDX_VM_REPORT_SIZE: usize = x86defs::tdx::TDX_REPORT_SIZE;
24pub const TVM_REPORT_SIZE: usize = 0;
26
27const PAGE_SIZE: usize = 4096;
28
29pub const WRAPPED_KEY_RESPONSE_BUFFER_SIZE: usize = 16 * PAGE_SIZE;
32pub const KEY_RELEASE_RESPONSE_BUFFER_SIZE: usize = 16 * PAGE_SIZE;
35pub const AK_CERT_RESPONSE_BUFFER_SIZE: usize = PAGE_SIZE;
38
39pub const IGVM_ATTEST_RESPONSE_CURRENT_VERSION: IgvmAttestResponseVersion =
41 IgvmAttestResponseVersion::VERSION_2;
42
43open_enum! {
44 #[derive(Default, IntoBytes, Immutable, KnownLayout, FromBytes)]
46 pub enum IgvmAttestResponseVersion: u32 {
47 VERSION_1 = 1,
49 VERSION_2 = 2,
51 }
52}
53
54pub const IGVM_ATTEST_REQUEST_CURRENT_VERSION: IgvmAttestRequestVersion =
56 IgvmAttestRequestVersion::VERSION_2;
57
58open_enum! {
59 #[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
61 pub enum IgvmAttestRequestVersion: u32 {
62 VERSION_1 = 1,
64 VERSION_2 = 2,
66 }
67}
68
69#[repr(C)]
73#[derive(Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
74pub struct IgvmAttestRequestBase {
75 pub header: IgvmAttestRequestHeader,
77 pub attestation_report: [u8; ATTESTATION_REPORT_SIZE_MAX],
79 pub request_data: IgvmAttestRequestData,
81 }
87
88open_enum! {
89 #[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
91 pub enum IgvmAttestReportType: u32 {
92 INVALID_REPORT = 0,
94 VBS_VM_REPORT = 1,
96 SNP_VM_REPORT = 2,
98 TVM_REPORT = 3,
100 TDX_VM_REPORT = 4,
102 CCA_VM_REPORT = 5,
104 }
105}
106
107open_enum! {
108 #[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
110 pub enum IgvmAttestRequestType: u32 {
111 INVALID_REQUEST = 0,
113 KEY_RELEASE_REQUEST = 1,
115 AK_CERT_REQUEST = 2,
117 WRAPPED_KEY_REQUEST = 3,
119 }
120}
121
122open_enum! {
123 #[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
125 pub enum IgvmAttestHashType: u32 {
126 INVALID_HASH = 0,
128 SHA_256 = 1,
130 SHA_384 = 2,
132 SHA_512 = 3,
134 }
135}
136
137#[repr(C)]
139#[derive(Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
140pub struct IgvmAttestRequestHeader {
141 pub signature: u32,
143 pub version: u32,
145 pub report_size: u32,
147 pub request_type: IgvmAttestRequestType,
149 pub status: u32,
151 pub reserved: [u32; 3],
153}
154
155impl IgvmAttestRequestHeader {
156 pub fn new(report_size: u32, request_type: IgvmAttestRequestType, status: u32) -> Self {
158 Self {
159 signature: ATTESTATION_SIGNATURE,
160 version: ATTESTATION_VERSION,
161 report_size,
162 request_type,
163 status,
164 reserved: [0u32; 3],
165 }
166 }
167}
168
169#[bitfield(u32)]
174#[derive(IntoBytes, FromBytes, Immutable, KnownLayout)]
175pub struct IgvmCapabilityBitMap {
176 pub error_code: bool,
177 pub retry: bool,
178 pub skip_hw_unsealing: bool,
179 #[bits(29)]
180 _reserved: u32,
181}
182
183#[repr(C)]
185#[derive(Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
186pub struct IgvmAttestRequestData {
187 pub data_size: u32,
189 pub version: IgvmAttestRequestVersion,
191 pub report_type: IgvmAttestReportType,
193 pub report_data_hash_type: IgvmAttestHashType,
195 pub variable_data_size: u32,
197}
198
199impl IgvmAttestRequestData {
200 pub fn new(
202 version: IgvmAttestRequestVersion,
203 data_size: u32,
204 report_type: IgvmAttestReportType,
205 report_data_hash_type: IgvmAttestHashType,
206 variable_data_size: u32,
207 ) -> Self {
208 Self {
209 data_size,
210 version,
211 report_type,
212 report_data_hash_type,
213 variable_data_size,
214 }
215 }
216}
217
218#[repr(C)]
221#[derive(Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
222pub struct IgvmAttestRequestDataExt {
223 pub capability_bitmap: IgvmCapabilityBitMap,
225}
226
227impl IgvmAttestRequestDataExt {
228 pub fn new(capability_bitmap: IgvmCapabilityBitMap) -> Self {
230 Self { capability_bitmap }
231 }
232}
233
234#[bitfield(u32)]
238#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
239pub struct IgvmSignal {
240 pub retry: bool,
241 pub skip_hw_unsealing: bool,
242 #[bits(30)]
243 _reserved: u32,
244}
245
246#[repr(C)]
248#[derive(Default, Debug, IntoBytes, FromBytes)]
249pub struct IgvmAttestCommonResponseHeader {
250 pub data_size: u32,
252 pub version: IgvmAttestResponseVersion,
254}
255
256#[repr(C)]
258#[derive(Default, Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
259pub struct IgvmErrorInfo {
260 pub error_code: u32,
262 pub http_status_code: u32,
264 pub igvm_signal: IgvmSignal,
266 pub reserved: [u32; 3],
268}
269
270#[repr(C)]
272#[derive(Default, Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
273pub struct IgvmAttestKeyReleaseResponseHeader {
274 pub data_size: u32,
276 pub version: IgvmAttestResponseVersion,
278 pub error_info: IgvmErrorInfo,
280}
281
282#[repr(C)]
285#[derive(Default, Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
286pub struct IgvmAttestWrappedKeyResponseHeader {
287 pub data_size: u32,
289 pub version: IgvmAttestResponseVersion,
291 pub error_info: IgvmErrorInfo,
293}
294
295#[repr(C)]
297#[derive(Default, Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
298pub struct IgvmAttestAkCertResponseHeader {
299 pub data_size: u32,
301 pub version: IgvmAttestResponseVersion,
303 pub error_info: IgvmErrorInfo,
305}
306
307pub mod runtime_claims {
310 use base64_serde::base64_serde_type;
311 use mesh::MeshPayload;
312 use serde::Deserialize;
313 use serde::Serialize;
314
315 base64_serde_type!(Base64Url, base64::engine::general_purpose::URL_SAFE_NO_PAD);
316
317 #[derive(Debug, Deserialize, Serialize)]
321 #[serde(rename_all = "kebab-case")]
322 pub struct RuntimeClaims {
323 pub keys: Vec<RsaJwk>,
325 pub vm_configuration: AttestationVmConfig,
327 #[serde(default, skip_serializing_if = "String::is_empty")]
329 pub user_data: String,
330 }
331
332 impl RuntimeClaims {
333 pub fn key_release_request_runtime_claims(
335 exponent: &[u8],
336 modulus: &[u8],
337 attestation_vm_config: &AttestationVmConfig,
338 ) -> Self {
339 let transfer_key_jwks = RsaJwk::get_transfer_key_jwks(exponent, modulus);
340 Self {
341 keys: transfer_key_jwks,
342 vm_configuration: attestation_vm_config.clone(),
343 user_data: "".to_string(),
344 }
345 }
346
347 pub fn ak_cert_runtime_claims(
349 ak_pub_exponent: &[u8],
350 ak_pub_modulus: &[u8],
351 ek_pub_exponent: &[u8],
352 ek_pub_modulus: &[u8],
353 attestation_vm_config: &AttestationVmConfig,
354 user_data: &[u8],
355 ) -> Self {
356 let tpm_jwks = RsaJwk::get_tpm_jwks(
357 ak_pub_exponent,
358 ak_pub_modulus,
359 ek_pub_exponent,
360 ek_pub_modulus,
361 );
362 Self {
363 keys: tpm_jwks,
364 vm_configuration: attestation_vm_config.clone(),
365 user_data: hex::encode(user_data),
366 }
367 }
368 }
369
370 #[derive(Debug, Deserialize, Serialize)]
372 pub struct RsaJwk {
373 pub kid: String,
375 pub key_ops: Vec<String>,
377 pub kty: String,
379 #[serde(with = "Base64Url")]
381 pub e: Vec<u8>,
382 #[serde(with = "Base64Url")]
384 pub n: Vec<u8>,
385 }
386
387 impl RsaJwk {
388 pub fn get_transfer_key_jwks(exponent: &[u8], modulus: &[u8]) -> Vec<RsaJwk> {
390 let jwk = RsaJwk {
391 kid: "HCLTransferKey".to_string(),
392 key_ops: vec!["encrypt".to_string()],
393 kty: "RSA".to_string(),
394 e: exponent.to_vec(),
395 n: modulus.to_vec(),
396 };
397
398 vec![jwk]
399 }
400
401 pub fn get_tpm_jwks(
403 ak_pub_exponent: &[u8],
404 ak_pub_modulus: &[u8],
405 ek_pub_exponent: &[u8],
406 ek_pub_modulus: &[u8],
407 ) -> Vec<RsaJwk> {
408 let ak_pub = RsaJwk {
409 kid: "HCLAkPub".to_string(),
410 key_ops: vec!["sign".to_string()],
411 kty: "RSA".to_string(),
412 e: ak_pub_exponent.to_vec(),
413 n: ak_pub_modulus.to_vec(),
414 };
415 let ek_pub = RsaJwk {
416 kid: "HCLEkPub".to_string(),
417 key_ops: vec!["encrypt".to_string()],
418 kty: "RSA".to_string(),
419 e: ek_pub_exponent.to_vec(),
420 n: ek_pub_modulus.to_vec(),
421 };
422
423 vec![ak_pub, ek_pub]
424 }
425 }
426
427 #[derive(Clone, Debug, Deserialize, Serialize, MeshPayload)]
429 #[serde(rename_all = "kebab-case")]
430 pub struct AttestationVmConfig {
431 #[serde(skip_serializing_if = "Option::is_none")]
433 pub current_time: Option<i64>,
434 pub root_cert_thumbprint: String,
436 pub console_enabled: bool,
438 pub interactive_console_enabled: bool,
440 pub secure_boot: bool,
442 pub tpm_enabled: bool,
444 pub tpm_persisted: bool,
446 pub filtered_vpci_devices_allowed: bool,
448 #[serde(rename = "vmUniqueId")]
450 pub vm_unique_id: String,
451 }
452}