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