|
1 | | -use anyhow::Context; |
2 | | -use eventlog::TdxEventLog; |
3 | | -pub use tdx_attest_sys as sys; |
| 1 | +#[cfg(all(target_os = "linux", target_arch = "x86_64", target_env = "gnu"))] |
| 2 | +pub use linux::*; |
| 3 | +#[cfg(all(target_os = "linux", target_arch = "x86_64", target_env = "gnu"))] |
| 4 | +mod linux; |
4 | 5 |
|
5 | | -use std::io::Write; |
6 | | -use std::ptr; |
7 | | -use std::slice; |
| 6 | +#[cfg(not(all(target_os = "linux", target_arch = "x86_64", target_env = "gnu")))] |
| 7 | +pub use dummy::*; |
8 | 8 |
|
9 | | -use sys::*; |
10 | | - |
11 | | -use fs_err as fs; |
12 | | -use num_enum::FromPrimitive; |
13 | | -use thiserror::Error; |
| 9 | +#[cfg(not(all(target_os = "linux", target_arch = "x86_64", target_env = "gnu")))] |
| 10 | +mod dummy; |
14 | 11 |
|
15 | 12 | pub use cc_eventlog as eventlog; |
16 | 13 |
|
17 | 14 | pub type Result<T> = std::result::Result<T, TdxAttestError>; |
18 | 15 |
|
19 | 16 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
20 | | -pub struct TdxUuid(pub [u8; TDX_UUID_SIZE as usize]); |
| 17 | +pub struct TdxUuid(pub [u8; 16]); |
21 | 18 |
|
22 | | -pub type TdxReportData = [u8; TDX_REPORT_DATA_SIZE as usize]; |
| 19 | +pub type TdxReportData = [u8; 64]; |
23 | 20 |
|
24 | 21 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
25 | | -pub struct TdxReport(pub [u8; TDX_REPORT_SIZE as usize]); |
26 | | - |
27 | | -#[repr(u32)] |
28 | | -#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, Error)] |
29 | | -pub enum TdxAttestError { |
30 | | - #[error("unexpected")] |
31 | | - Unexpected = _tdx_attest_error_t::TDX_ATTEST_ERROR_UNEXPECTED, |
32 | | - #[error("invalid parameter")] |
33 | | - InvalidParameter = _tdx_attest_error_t::TDX_ATTEST_ERROR_INVALID_PARAMETER, |
34 | | - #[error("out of memory")] |
35 | | - OutOfMemory = _tdx_attest_error_t::TDX_ATTEST_ERROR_OUT_OF_MEMORY, |
36 | | - #[error("vsock failure")] |
37 | | - VsockFailure = _tdx_attest_error_t::TDX_ATTEST_ERROR_VSOCK_FAILURE, |
38 | | - #[error("report failure")] |
39 | | - ReportFailure = _tdx_attest_error_t::TDX_ATTEST_ERROR_REPORT_FAILURE, |
40 | | - #[error("extend failure")] |
41 | | - ExtendFailure = _tdx_attest_error_t::TDX_ATTEST_ERROR_EXTEND_FAILURE, |
42 | | - #[error("not supported")] |
43 | | - NotSupported = _tdx_attest_error_t::TDX_ATTEST_ERROR_NOT_SUPPORTED, |
44 | | - #[error("quote failure")] |
45 | | - QuoteFailure = _tdx_attest_error_t::TDX_ATTEST_ERROR_QUOTE_FAILURE, |
46 | | - #[error("busy")] |
47 | | - Busy = _tdx_attest_error_t::TDX_ATTEST_ERROR_BUSY, |
48 | | - #[error("device failure")] |
49 | | - DeviceFailure = _tdx_attest_error_t::TDX_ATTEST_ERROR_DEVICE_FAILURE, |
50 | | - #[error("invalid rtmr index")] |
51 | | - InvalidRtmrIndex = _tdx_attest_error_t::TDX_ATTEST_ERROR_INVALID_RTMR_INDEX, |
52 | | - #[error("unsupported att key id")] |
53 | | - UnsupportedAttKeyId = _tdx_attest_error_t::TDX_ATTEST_ERROR_UNSUPPORTED_ATT_KEY_ID, |
54 | | - #[num_enum(catch_all)] |
55 | | - #[error("unknown error ({0})")] |
56 | | - UnknownError(u32), |
57 | | -} |
58 | | - |
59 | | -pub fn get_quote( |
60 | | - report_data: &TdxReportData, |
61 | | - att_key_id_list: Option<&[TdxUuid]>, |
62 | | -) -> Result<(TdxUuid, Vec<u8>)> { |
63 | | - let mut att_key_id = TdxUuid([0; TDX_UUID_SIZE as usize]); |
64 | | - let mut quote_ptr = ptr::null_mut(); |
65 | | - let mut quote_size = 0; |
66 | | - |
67 | | - let error = unsafe { |
68 | | - let key_id_list_ptr = att_key_id_list |
69 | | - .map(|list| list.as_ptr() as *const tdx_uuid_t) |
70 | | - .unwrap_or(ptr::null()); |
71 | | - tdx_att_get_quote( |
72 | | - report_data as *const TdxReportData as *const tdx_report_data_t, |
73 | | - key_id_list_ptr, |
74 | | - att_key_id_list.map_or(0, |list| list.len() as u32), |
75 | | - &mut att_key_id as *mut TdxUuid as *mut tdx_uuid_t, |
76 | | - &mut quote_ptr, |
77 | | - &mut quote_size, |
78 | | - 0, |
79 | | - ) |
80 | | - }; |
81 | | - |
82 | | - if error != _tdx_attest_error_t::TDX_ATTEST_SUCCESS { |
83 | | - return Err(error.into()); |
84 | | - } |
85 | | - |
86 | | - let quote = unsafe { slice::from_raw_parts(quote_ptr, quote_size as usize).to_vec() }; |
87 | | - |
88 | | - unsafe { |
89 | | - tdx_att_free_quote(quote_ptr); |
90 | | - } |
91 | | - |
92 | | - Ok((att_key_id, quote)) |
93 | | -} |
94 | | - |
95 | | -pub fn get_report(report_data: &TdxReportData) -> Result<TdxReport> { |
96 | | - let mut report = TdxReport([0; TDX_REPORT_SIZE as usize]); |
97 | | - |
98 | | - let error = unsafe { |
99 | | - tdx_att_get_report( |
100 | | - report_data as *const TdxReportData as *const tdx_report_data_t, |
101 | | - &mut report as *mut TdxReport as *mut tdx_report_t, |
102 | | - ) |
103 | | - }; |
104 | | - |
105 | | - if error != _tdx_attest_error_t::TDX_ATTEST_SUCCESS { |
106 | | - return Err(error.into()); |
107 | | - } |
108 | | - |
109 | | - Ok(report) |
110 | | -} |
111 | | - |
112 | | -pub fn log_rtmr_event(log: &TdxEventLog) -> anyhow::Result<()> { |
113 | | - // Append to event log |
114 | | - let logline = serde_json::to_string(&log).context("Failed to serialize event log")?; |
115 | | - |
116 | | - let logfile_path = std::path::Path::new(eventlog::RUNTIME_EVENT_LOG_FILE); |
117 | | - let logfile_dir = logfile_path |
118 | | - .parent() |
119 | | - .context("Failed to get event log directory")?; |
120 | | - fs::create_dir_all(logfile_dir).context("Failed to create event log directory")?; |
121 | | - |
122 | | - let mut logfile = fs::OpenOptions::new() |
123 | | - .append(true) |
124 | | - .create(true) |
125 | | - .open(logfile_path) |
126 | | - .context("Failed to open event log file")?; |
127 | | - logfile |
128 | | - .write_all(logline.as_bytes()) |
129 | | - .context("Failed to write to event log file")?; |
130 | | - logfile |
131 | | - .write_all(b"\n") |
132 | | - .context("Failed to write to event log file")?; |
133 | | - Ok(()) |
134 | | -} |
135 | | - |
136 | | -pub fn extend_rtmr(index: u32, event_type: u32, digest: [u8; 48]) -> Result<()> { |
137 | | - let event = tdx_rtmr_event_t { |
138 | | - version: 1, |
139 | | - rtmr_index: index as u64, |
140 | | - extend_data: digest, |
141 | | - event_type, |
142 | | - event_data_size: 0, |
143 | | - event_data: Default::default(), |
144 | | - }; |
145 | | - let error = unsafe { tdx_att_extend(&event) }; |
146 | | - if error != _tdx_attest_error_t::TDX_ATTEST_SUCCESS { |
147 | | - return Err(error.into()); |
148 | | - } |
149 | | - Ok(()) |
150 | | -} |
151 | | - |
152 | | -pub fn get_supported_att_key_ids() -> Result<Vec<TdxUuid>> { |
153 | | - let mut list_size = 0; |
154 | | - let error = unsafe { tdx_att_get_supported_att_key_ids(ptr::null_mut(), &mut list_size) }; |
155 | | - |
156 | | - if error != _tdx_attest_error_t::TDX_ATTEST_SUCCESS { |
157 | | - return Err(error.into()); |
158 | | - } |
159 | | - |
160 | | - let mut att_key_id_list = vec![TdxUuid([0; TDX_UUID_SIZE as usize]); list_size as usize]; |
161 | | - |
162 | | - let error = unsafe { |
163 | | - tdx_att_get_supported_att_key_ids( |
164 | | - att_key_id_list.as_mut_ptr() as *mut tdx_uuid_t, |
165 | | - &mut list_size, |
166 | | - ) |
167 | | - }; |
168 | | - |
169 | | - if error != _tdx_attest_error_t::TDX_ATTEST_SUCCESS { |
170 | | - return Err(error.into()); |
171 | | - } |
172 | | - |
173 | | - Ok(att_key_id_list) |
174 | | -} |
| 22 | +pub struct TdxReport(pub [u8; 1024]); |
0 commit comments