Skip to content

Commit 72d5cbe

Browse files
committed
Introduce EifSector to represent coupled header and buffer
Signed-off-by: Maayan Keshet <[email protected]>
1 parent 646cd65 commit 72d5cbe

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

eif_extract/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ fn extract_ramdisks(eif_path: &str, output_dir: &str, prefix: &str) -> io::Resul
1919

2020
println!("Reading section data...");
2121
for section_result in sections {
22-
let (section, _, data) = section_result.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
23-
if section.section_type == EifSectionType::EifSectionRamdisk {
22+
let section = section_result.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
23+
if section.section_header.section_type == EifSectionType::EifSectionRamdisk {
2424
let output_file_path = format!("{}/{}{}.dat", output_dir, prefix, ramdisk_count);
2525
let mut output_file = File::create(&output_file_path)?;
26-
output_file.write_all(&data)?;
26+
output_file.write_all(&section.section_data)?;
2727
println!("Saved ramdisk to {}", output_file_path);
2828
ramdisk_count += 1;
2929
}

src/defs/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ impl EifSectionHeader {
222222
}
223223
}
224224

225+
#[derive(Clone, Debug)]
226+
pub struct EifSection {
227+
/// The header of the EIF section
228+
pub section_header: EifSectionHeader,
229+
/// Buffer containing the section data
230+
pub section_data: Vec<u8>,
231+
}
232+
225233
/// Array containing the signatures of at least one PCR.
226234
/// For now, it only contains the signature of PRC0.
227235
pub type EifSignature = Vec<PcrSignature>;

src/utils/eif_reader.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
use crate::defs::eif_hasher::EifHasher;
55
use crate::defs::{
6-
EifHeader, EifIdentityInfo, EifSectionHeader, EifSectionType, PcrInfo, PcrSignature,
6+
EifHeader, EifIdentityInfo, EifSectionType, EifSectionHeader, EifSection, PcrInfo,
7+
PcrSignature,
78
};
89
use aws_nitro_enclaves_cose::{crypto::Openssl, CoseSign1};
910
use crc::{Crc, CRC_32_ISO_HDLC};
@@ -75,33 +76,36 @@ impl Sections {
7576
}
7677

7778
impl Iterator for Sections {
78-
type Item = Result<(EifSectionHeader, Vec<u8>, Vec<u8>), String>; // Header, HeaderBuf, Buf
79+
type Item = Result<EifSection, String>; // Changed to EifSection
7980

8081
fn next(&mut self) -> Option<Self::Item> {
8182
let mut section_buf = vec![0u8; EifSectionHeader::size()];
8283
if self.eif_file.read_exact(&mut section_buf).is_err() {
8384
return None;
8485
}
8586

86-
let section = match EifSectionHeader::from_be_bytes(&section_buf) {
87+
let section_header = match EifSectionHeader::from_be_bytes(&section_buf) {
8788
Ok(sec) => sec,
8889
Err(e) => return Some(Err(format!("Error extracting EIF section header: {:?}", e))),
8990
};
9091

91-
let mut buf = vec![0u8; section.section_size as usize];
92+
let mut section_data = vec![0u8; section_header.section_size as usize];
9293
self.curr_seek += EifSectionHeader::size() as u64;
9394
if self.eif_file.seek(SeekFrom::Start(self.curr_seek)).is_err() {
9495
return Some(Err("Failed to seek after EIF header".to_string()));
9596
}
96-
if self.eif_file.read_exact(&mut buf).is_err() {
97+
if self.eif_file.read_exact(&mut section_data).is_err() {
9798
return Some(Err("Error while reading section from EIF".to_string()));
9899
}
99-
self.curr_seek += section.section_size as u64;
100+
self.curr_seek += section_header.section_size as u64;
100101
if self.eif_file.seek(SeekFrom::Start(self.curr_seek)).is_err() {
101102
return Some(Err("Failed to seek after EIF section".to_string()));
102103
}
103104

104-
Some(Ok((section, section_buf, buf)))
105+
Some(Ok(EifSection {
106+
section_header,
107+
section_data,
108+
}))
105109
}
106110
}
107111

@@ -165,41 +169,40 @@ impl EifReader {
165169

166170
// Read all sections and treat by type
167171
for section_result in sections {
168-
let (section, section_buf, buf) = section_result
169-
.map_err(|e| e.to_string())?;
170-
eif_crc.update(&section_buf);
172+
let section = section_result.map_err(|e| e.to_string())?;
173+
eif_crc.update(&section.section_header.to_be_bytes());
171174

172-
match section.section_type {
175+
match section.section_header.section_type {
173176
EifSectionType::EifSectionKernel | EifSectionType::EifSectionCmdline => {
174-
image_hasher.write_all(&buf).map_err(|e| {
177+
image_hasher.write_all(&section.section_data).map_err(|e| {
175178
format!("Failed to write EIF section to image_hasher: {:?}", e)
176179
})?;
177-
bootstrap_hasher.write_all(&buf).map_err(|e| {
180+
bootstrap_hasher.write_all(&section.section_data).map_err(|e| {
178181
format!("Failed to write EIF section to bootstrap_hasher: {:?}", e)
179182
})?;
180183
}
181184
EifSectionType::EifSectionRamdisk => {
182-
image_hasher.write_all(&buf).map_err(|e| {
185+
image_hasher.write_all(&section.section_data).map_err(|e| {
183186
format!("Failed to write ramdisk section to image_hasher: {:?}", e)
184187
})?;
185188
if ramdisk_idx == 0 {
186-
bootstrap_hasher.write_all(&buf).map_err(|e| {
189+
bootstrap_hasher.write_all(&section.section_data).map_err(|e| {
187190
format!(
188191
"Failed to write ramdisk section to bootstrap_hasher: {:?}",
189192
e
190193
)
191194
})?;
192195
} else {
193-
app_hasher.write_all(&buf).map_err(|e| {
196+
app_hasher.write_all(&section.section_data).map_err(|e| {
194197
format!("Failed to write ramdisk section to app_hasher: {:?}", e)
195198
})?;
196199
}
197200
ramdisk_idx += 1;
198201
}
199202
EifSectionType::EifSectionSignature => {
200-
signature_section = Some(buf.clone());
203+
signature_section = Some(section.section_data.clone());
201204
// Deserialize PCR0 signature structure and write it to the hasher
202-
let des_sign: Vec<PcrSignature> = from_slice(&buf[..])
205+
let des_sign: Vec<PcrSignature> = from_slice(&section.section_data[..])
203206
.map_err(|e| format!("Error deserializing certificate: {:?}", e))?;
204207

205208
let cert = openssl::x509::X509::from_pem(&des_sign[0].signing_certificate)
@@ -212,7 +215,7 @@ impl EifReader {
212215
})?;
213216
}
214217
EifSectionType::EifSectionMetadata => {
215-
metadata = serde_json::from_slice(&buf[..])
218+
metadata = serde_json::from_slice(&section.section_data[..])
216219
.map_err(|e| format!("Error deserializing metadata: {:?}", e))?;
217220
}
218221
EifSectionType::EifSectionInvalid => {

0 commit comments

Comments
 (0)