From a630a592dc839f84f8d621f6e1c3fbb306130b86 Mon Sep 17 00:00:00 2001 From: Lorenzo Delgado Date: Tue, 18 Aug 2026 00:39:28 +0200 Subject: [PATCH] feat(nx-object): read an NCA and the PFS0 archives it carries The crate could build both formats but not read either, so a consumer verifying what it packed had to re-derive the layouts itself. These readers close that gap while keeping the crate's no-crypto contract: an NCA arrives already decrypted, and the magic check is what catches a caller who skipped that step. - Add `read::pfs0`, proving the entry table, the string table, and every file's bounds at construction, so a file's bytes come back as a plain slice rather than a `Result` - Add `read::nca`, validating section extents, hash and encryption types, and each superblock's data region, and exposing a section's byte range and AES-CTR counter so a caller can decrypt in place - Model a section's verification structure as a `Superblock` enum rather than two optional fields, since the hash type is what decides which of the two the same `0x138` bytes are - Recover the single key generation the header splits across `crypto_type` and `crypto_type2`, inverting how the builder writes it Signed-off-by: Lorenzo Delgado --- src/lib.rs | 7 +- src/read.rs | 12 +- src/read/nca.rs | 690 +++++++++++++++++++++++++++++++++++++++++++++++ src/read/pfs0.rs | 449 ++++++++++++++++++++++++++++++ 4 files changed, 1152 insertions(+), 6 deletions(-) create mode 100644 src/read/nca.rs create mode 100644 src/read/pfs0.rs diff --git a/src/lib.rs b/src/lib.rs index 5bd7e76..d697ca0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,9 +49,9 @@ //! | NACP | How a title is presented and what it may do | ✓ | ✓ | ✓ | //! | NPDM | The permissions a program is granted | ✓ | ✓ | ✓ | //! | RomFS | The read-only filesystem a title ships | ✓ | ✓ | ✓ | -//! | PFS0 | The flat archive an NSP is built from | ✓ | | ✓ | +//! | PFS0 | The flat archive an NSP is built from | ✓ | ✓ | ✓ | //! | MOD0 | The runtime header embedded in an executable | ✓ | ✓ | | -//! | NCA | The container a title's contents ship in | ✓ | | ✓ | +//! | NCA | The container a title's contents ship in | ✓ | ✓ | ✓ | //! | CNMT | The content meta naming every NCA of a title | ✓ | | ✓ | //! //! # What this crate does not do @@ -63,7 +63,8 @@ //! //! NCA is where that line is most visible, because an NCA on disk is encrypted throughout. //! [`write::NcaBuilder`] produces the plaintext container and every hash covering it, then names -//! what is still owed; the caller supplies the keyset and the ciphers. The split is deliberate: a +//! what is still owed; the caller supplies the keyset and the ciphers. Reading runs the same way +//! in reverse: [`read::nca::Nca`] takes a buffer the caller has already decrypted. The split is deliberate: a //! keyset is secret material with its own provenance and handling rules, and a crate whose contract //! is bytes in, bytes out, no I/O is the wrong place to acquire one. Hashing stays here because a //! hash is part of the layout — it is what makes the recorded offsets checkable — while encryption diff --git a/src/read.rs b/src/read.rs index 0122eff..349ba53 100644 --- a/src/read.rs +++ b/src/read.rs @@ -11,19 +11,25 @@ //! at all: those readers check lengths and layout, so success means the buffer is shaped like the //! format, never that it is one. //! -//! Nothing here decompresses or verifies a hash. An NSO segment comes back as stored, and checking -//! it against the header's digest is the caller's, because a borrowing reader has nowhere to put -//! the expanded bytes. +//! Nothing here decompresses, decrypts, or verifies a hash. An NSO segment comes back as stored, +//! and checking it against the header's digest is the caller's, because a borrowing reader has +//! nowhere to put the expanded bytes. The same line puts NCA decryption outside: [`nca::Nca`] takes +//! a buffer the caller has already decrypted, and an image still in ciphertext fails its magic +//! check rather than parsing into nonsense. #[cfg(feature = "mod0")] pub mod mod0; #[cfg(feature = "nacp")] pub mod nacp; +#[cfg(feature = "nca")] +pub mod nca; #[cfg(feature = "npdm")] pub mod npdm; #[cfg(feature = "nro")] pub mod nro; #[cfg(feature = "nso")] pub mod nso; +#[cfg(feature = "pfs0")] +pub mod pfs0; #[cfg(feature = "romfs")] pub mod romfs; diff --git a/src/read/nca.rs b/src/read/nca.rs new file mode 100644 index 0000000..be8bdfe --- /dev/null +++ b/src/read/nca.rs @@ -0,0 +1,690 @@ +//! Validated reader over a decrypted NCA and the sections it carries. +//! +//! **The buffer this reader takes is plaintext.** An NCA on disk has its header wrapped in AES-XTS +//! and its sections usually in AES-CTR, and neither is undone here — this crate does no crypto, so +//! the caller decrypts the image and hands over the result. A ciphertext buffer fails at the magic +//! check rather than parsing into nonsense. +//! +//! [`Nca::try_from_bytes`] proves every present section lies inside the buffer, and proves the data +//! region each section's superblock points at lies inside that section. That is why the accessors +//! below return slices without re-checking. +//! +//! A section entry and the FS header describing it are matched by index and by nothing else, so this +//! reader pairs them the same way and treats a zeroed entry as an absent section rather than as a +//! section of length zero. + +use zerocopy::FromBytes as _; + +use crate::raw::nca::{ + MEDIA_UNIT_SIZE, NCA_HEADER_SIZE, NCA_SECTION_COUNT, NCA3_MAGIC, NcaContentType, NcaCryptType, + NcaFsHeader, NcaFsType, NcaHashType, NcaHeader, NcaSectionEntry, Pfs0Superblock, + RomFsSuperblock, +}; + +/// A borrowed view of a decrypted NCA whose every section has already been bounds-checked. +/// +/// Construction is where every check happens, so an `Nca` that exists is one whose sections lie +/// inside the buffer and whose content, hash, and encryption types are ones this crate knows. +pub struct Nca<'a> { + bytes: &'a [u8], + header: &'a NcaHeader, + content_type: NcaContentType, +} + +impl<'a> Nca<'a> { + /// Validate `bytes` as a decrypted NCA and borrow it, proving every section lies inside it. + /// + /// # Errors + /// + /// Returns an error if the buffer cannot hold the `0xC00`-byte header, if the magic is not + /// `NCA3` — which is what a still-encrypted image looks like here — if the content type is not + /// one the format defines, or if any present section has inverted bounds, runs past the end of + /// the buffer, declares a hash or encryption type this crate does not know, or points its data + /// region outside itself. + pub fn try_from_bytes(bytes: &'a [u8]) -> Result { + let header = NcaHeader::ref_from_prefix(bytes) + .map_err(|_| FromBytesError::BufferTooSmall { + required: NCA_HEADER_SIZE as usize, + available: bytes.len(), + })? + .0; + + if header.magic.get() != NCA3_MAGIC { + return Err(FromBytesError::InvalidMagic { + found: header.magic.get(), + }); + } + + let content_type = content_type(header.content_type)?; + + for index in 0..NCA_SECTION_COUNT { + // Indexing is bounded by the loop, and both arrays are `NCA_SECTION_COUNT` long. + let entry = &header.section_entries[index]; + let fs_header = &header.fs_headers[index]; + + let Some(range) = section_range(index, entry, bytes.len())? else { + continue; + }; + + crypt_type(index, fs_header.crypt_type)?; + let hash_type = hash_type(index, fs_header.hash_type)?; + data_range(index, fs_header, hash_type, range.end - range.start)?; + } + + Ok(Self { + bytes, + header, + content_type, + }) + } + + /// The header describing the archive's identity, key area, and sections. + pub fn header(&self) -> &NcaHeader { + self.header + } + + /// What the archive holds, which is what the console decides how to mount it by. + pub fn content_type(&self) -> NcaContentType { + self.content_type + } + + /// The title this archive belongs to. + pub fn title_id(&self) -> u64 { + self.header.title_id.get() + } + + /// Length of the whole archive in bytes, as the header records it. + pub fn nca_size(&self) -> u64 { + self.header.nca_size.get() + } + + /// The keyset generation the key area is wrapped with, as a keyset file names it. + /// + /// The header splits the generation across two fields with a gap in the numbering: the low one + /// saturates at 2 and the high one carries anything beyond. Taking the larger and flooring at 1 + /// recovers the single number both halves stand for. + pub fn key_generation(&self) -> u8 { + self.header.crypto_type.max(self.header.crypto_type2).max(1) + } + + /// The section at `index`, or `None` when the archive leaves that slot empty. + /// + /// # Panics + /// + /// Panics if `index` is not one of the four slots an NCA holds. + pub fn section(&self, index: usize) -> Option> { + assert!( + index < NCA_SECTION_COUNT, + "an NCA holds {NCA_SECTION_COUNT} sections" + ); + + let entry = &self.header.section_entries[index]; + let fs_header = &self.header.fs_headers[index]; + + // Every value reached below was proven by `try_from_bytes`: the range lies inside the + // buffer, the two type fields are known, and the data region lies inside the section. + let range = section_range(index, entry, self.bytes.len()).ok()??; + let bytes = &self.bytes[range.clone()]; + + let hash_type = hash_type(index, fs_header.hash_type).ok()?; + let data = data_range(index, fs_header, hash_type, bytes.len()).ok()?; + + Some(NcaSection { + index, + range, + fs_header, + bytes, + data: &bytes[data], + }) + } + + /// Every section the archive carries, skipping the slots it leaves empty. + pub fn sections(&self) -> impl Iterator> + '_ { + (0..NCA_SECTION_COUNT).filter_map(|index| self.section(index)) + } +} + +/// One section: where it sits, what filesystem it holds, and how it is verified and encrypted. +pub struct NcaSection<'a> { + index: usize, + range: core::ops::Range, + fs_header: &'a NcaFsHeader, + bytes: &'a [u8], + data: &'a [u8], +} + +impl<'a> NcaSection<'a> { + /// Which of the four slots this section occupies. + pub fn index(&self) -> usize { + self.index + } + + /// Where the section sits in the image, as a byte range. + /// + /// This is what a caller decrypting the image in place indexes with, which is why it is offered + /// alongside the borrowed bytes rather than left to be recomputed from the header. + pub fn range(&self) -> core::ops::Range { + self.range.clone() + } + + /// The FS header describing this section. + pub fn fs_header(&self) -> &'a NcaFsHeader { + self.fs_header + } + + /// The whole section, verification structures included. + pub fn bytes(&self) -> &'a [u8] { + self.bytes + } + + /// The filesystem image alone, with the hash table or tree that covers it stripped. + /// + /// For a PFS0 section this is the archive; for a RomFS section it is the bottom level of the + /// IVFC tree, which is the RomFS image itself. + pub fn data(&self) -> &'a [u8] { + self.data + } + + /// What kind of filesystem the section holds. + /// + /// A section whose `fs_type` is neither of the two the format defines reads as a RomFS, which + /// is what the field's zero value means; the hash type is the field that decides how the + /// superblock is read, and it was proven at construction. + pub fn fs_type(&self) -> NcaFsType { + match self.fs_header.fs_type { + value if value == NcaFsType::Pfs0 as u8 => NcaFsType::Pfs0, + _ => NcaFsType::RomFs, + } + } + + /// How the section's bytes are encrypted on disk. + pub fn encryption(&self) -> NcaCryptType { + // Proven at construction, so the fallback is unreachable rather than a default. + crypt_type(self.index, self.fs_header.crypt_type).unwrap_or(NcaCryptType::None) + } + + /// The verification structure covering the section, read according to its hash type. + pub fn superblock(&self) -> Superblock<'a> { + // Proven at construction: the hash type is one of the two, and the superblock span is a + // fixed `0x138` bytes that both structures exactly fill. + match hash_type(self.index, self.fs_header.hash_type) { + Ok(NcaHashType::Pfs0) => Pfs0Superblock::ref_from_bytes(&self.fs_header.superblock) + .map_or(Superblock::Unreadable, Superblock::Pfs0), + _ => RomFsSuperblock::ref_from_bytes(&self.fs_header.superblock) + .map_or(Superblock::Unreadable, Superblock::RomFs), + } + } + + /// The AES-CTR counter for the first byte of the section. + /// + /// The counter is tied to where the section sits in the file, so the same bytes at a different + /// offset decrypt differently. A section that is not CTR-encrypted has no counter, so this + /// returns `None` for it. + pub fn counter(&self) -> Option<[u8; 0x10]> { + if self.encryption() != NcaCryptType::Ctr { + return None; + } + + let offset = self.range.start as u64; + + let mut counter = [0u8; 0x10]; + for (index, byte) in self.fs_header.section_ctr.iter().rev().enumerate() { + counter[index] = *byte; + } + counter[0x8..].copy_from_slice(&(offset / 0x10).to_be_bytes()); + Some(counter) + } +} + +/// The verification structure a section's superblock holds. +/// +/// Which of the two it is comes from the FS header's hash type, and nothing in the layout +/// distinguishes them — the same `0x138` bytes read as either. +pub enum Superblock<'a> { + /// The single-level hash table covering a PFS0 section. + Pfs0(&'a Pfs0Superblock), + /// The IVFC hash tree covering a RomFS section. + RomFs(&'a RomFsSuperblock), + /// The superblock span could not be read as either structure. + /// + /// Both structures are exactly the `0x138` bytes the span reserves, so this is unreachable for + /// a section [`Nca::try_from_bytes`] accepted; it exists so that reading a superblock stays + /// infallible rather than returning a `Result` no caller can act on. + Unreadable, +} + +/// The content type `value` names, or an error naming what was found instead. +fn content_type(value: u8) -> Result { + match value { + value if value == NcaContentType::Program as u8 => Ok(NcaContentType::Program), + value if value == NcaContentType::Meta as u8 => Ok(NcaContentType::Meta), + value if value == NcaContentType::Control as u8 => Ok(NcaContentType::Control), + value if value == NcaContentType::Manual as u8 => Ok(NcaContentType::Manual), + value if value == NcaContentType::Data as u8 => Ok(NcaContentType::Data), + value if value == NcaContentType::PublicData as u8 => Ok(NcaContentType::PublicData), + found => Err(FromBytesError::UnknownContentType { found }), + } +} + +/// The hash type `value` names, or an error naming the section that carried it. +fn hash_type(index: usize, value: u8) -> Result { + match value { + value if value == NcaHashType::Pfs0 as u8 => Ok(NcaHashType::Pfs0), + value if value == NcaHashType::RomFs as u8 => Ok(NcaHashType::RomFs), + found => Err(FromBytesError::UnknownHashType { + section_index: index, + found, + }), + } +} + +/// The encryption type `value` names, or an error naming the section that carried it. +fn crypt_type(index: usize, value: u8) -> Result { + match value { + value if value == NcaCryptType::None as u8 => Ok(NcaCryptType::None), + value if value == NcaCryptType::Xts as u8 => Ok(NcaCryptType::Xts), + value if value == NcaCryptType::Ctr as u8 => Ok(NcaCryptType::Ctr), + value if value == NcaCryptType::Bktr as u8 => Ok(NcaCryptType::Bktr), + found => Err(FromBytesError::UnknownCryptType { + section_index: index, + found, + }), + } +} + +/// Where one section sits in the image, or `None` when the entry is empty. +/// +/// An entry that ends at media unit zero is an unused slot rather than a section of length zero, +/// which is the distinction the format leaves to the reader. +fn section_range( + index: usize, + entry: &NcaSectionEntry, + available: usize, +) -> Result>, FromBytesError> { + let start = u64::from(entry.media_start_offset.get()) * MEDIA_UNIT_SIZE; + let end = u64::from(entry.media_end_offset.get()) * MEDIA_UNIT_SIZE; + + if end == 0 { + return Ok(None); + } + + if end < start || start < NCA_HEADER_SIZE { + return Err(FromBytesError::SectionBoundsInvalid { + section_index: index, + start, + end, + }); + } + + if end > available as u64 { + return Err(FromBytesError::SectionOutOfBounds { + section_index: index, + start, + end, + available, + }); + } + + // Both bounds were just proven to fit an in-memory buffer, so the casts cannot truncate. + Ok(Some(start as usize..end as usize)) +} + +/// Where the filesystem image sits within a section of `section_size` bytes. +fn data_range( + index: usize, + fs_header: &NcaFsHeader, + hash_type: NcaHashType, + section_size: usize, +) -> Result, FromBytesError> { + let (offset, size) = match hash_type { + NcaHashType::Pfs0 => { + let superblock = + Pfs0Superblock::ref_from_bytes(&fs_header.superblock).map_err(|_| { + FromBytesError::SuperblockUnreadable { + section_index: index, + } + })?; + (superblock.pfs0_offset.get(), superblock.pfs0_size.get()) + } + NcaHashType::RomFs => { + let superblock = + RomFsSuperblock::ref_from_bytes(&fs_header.superblock).map_err(|_| { + FromBytesError::SuperblockUnreadable { + section_index: index, + } + })?; + + // `level_count` counts the master hash as well as the stored levels, so the bottom + // level — the RomFS image itself — is two below it. + let ivfc = &superblock.ivfc_header; + let last = (ivfc.level_count.get() as usize).checked_sub(2).ok_or( + FromBytesError::IvfcLevelCountInvalid { + section_index: index, + level_count: ivfc.level_count.get(), + }, + )?; + let level = + ivfc.level_headers + .get(last) + .ok_or(FromBytesError::IvfcLevelCountInvalid { + section_index: index, + level_count: ivfc.level_count.get(), + })?; + (level.logical_offset.get(), level.hash_data_size.get()) + } + }; + + let end = offset + .checked_add(size) + .ok_or(FromBytesError::DataRegionOutOfBounds { + section_index: index, + offset, + size, + available: section_size, + })?; + + if end > section_size as u64 { + return Err(FromBytesError::DataRegionOutOfBounds { + section_index: index, + offset, + size, + available: section_size, + }); + } + + // Both bounds were just proven to fit the section, which is itself in memory. + Ok(offset as usize..end as usize) +} + +/// Errors that can occur when parsing a decrypted NCA from bytes +#[derive(Debug, thiserror::Error)] +pub enum FromBytesError { + /// Buffer is too small to contain the NCA header + #[error("buffer too small: need {required} bytes, have {available}")] + BufferTooSmall { + /// Number of bytes required + required: usize, + /// Number of bytes available + available: usize, + }, + /// Magic number does not match NCA3 (0x3341434e) + /// + /// An image whose header is still encrypted fails here, because ciphertext does not carry the + /// magic at the offset the format puts it. + #[error("invalid NCA magic: found {found:#010x}; is the header still encrypted?")] + InvalidMagic { + /// The magic that was found in place of `NCA3` + found: u32, + }, + /// The content type is not one the format defines + #[error("unknown content type {found}")] + UnknownContentType { + /// The value the header carried + found: u8, + }, + /// A section's hash type is neither of the two the format defines + #[error("section {section_index} declares unknown hash type {found}")] + UnknownHashType { + /// Index of the section in the header + section_index: usize, + /// The value the FS header carried + found: u8, + }, + /// A section's encryption type is not one the format defines + #[error("section {section_index} declares unknown encryption type {found}")] + UnknownCryptType { + /// Index of the section in the header + section_index: usize, + /// The value the FS header carried + found: u8, + }, + /// A section ends before it starts, or starts inside the header + #[error("section {section_index} spans {start}..{end}, which is not a valid extent")] + SectionBoundsInvalid { + /// Index of the section in the header + section_index: usize, + /// First byte of the section + start: u64, + /// One past the last byte of the section + end: u64, + }, + /// A section extends past the end of the image + #[error("section {section_index} ends at {end}, past the {available} available")] + SectionOutOfBounds { + /// Index of the section in the header + section_index: usize, + /// First byte of the section + start: u64, + /// One past the last byte of the section + end: u64, + /// Number of bytes the image holds + available: usize, + }, + /// A section's superblock span could not be read as the structure its hash type names + #[error("section {section_index} has an unreadable superblock")] + SuperblockUnreadable { + /// Index of the section in the header + section_index: usize, + }, + /// An IVFC header declares a level count that names no bottom level + #[error("section {section_index} declares {level_count} IVFC levels")] + IvfcLevelCountInvalid { + /// Index of the section in the header + section_index: usize, + /// The count the IVFC header carried + level_count: u32, + }, + /// A section's filesystem image falls outside the section + #[error( + "section {section_index} places its data at {offset} of size {size}, \ + past the {available} the section holds" + )] + DataRegionOutOfBounds { + /// Index of the section in the header + section_index: usize, + /// Offset of the data region from the start of the section + offset: u64, + /// Length of the data region + size: u64, + /// Number of bytes the section holds + available: usize, + }, +} + +#[cfg(all(test, feature = "alloc"))] +mod tests { + use alloc::vec; + + use super::{FromBytesError, Nca, Superblock}; + use crate::{ + raw::nca::{NcaContentType, NcaCryptType}, + write::{ + NcaBuilder, + nca::{KeyGeneration, Section, SectionData, SectionEncryption}, + }, + }; + + /// Build a plaintext NCA holding one partition section of `size` bytes. + fn plain_nca(size: usize) -> alloc::vec::Vec { + NcaBuilder::new(NcaContentType::Program, 0x0100_0000_0000_1000) + .section( + 0, + Section { + data: SectionData::Partition { + archive: vec![0xAB; size], + hash_block_size: 0x1000, + }, + encryption: SectionEncryption::None, + }, + ) + .expect("placing a section at index 0 should succeed") + .build() + .expect("a small archive should build") + .to_bytes() + } + + #[test] + fn try_from_bytes_with_a_plaintext_archive_succeeds() { + //* Given + let image = plain_nca(0x400); + + //* When + let nca = Nca::try_from_bytes(&image).expect("a built archive should parse"); + + //* Then + assert_eq!(nca.content_type(), NcaContentType::Program); + assert_eq!(nca.title_id(), 0x0100_0000_0000_1000); + } + + #[test] + fn try_from_bytes_with_an_encrypted_header_fails() { + //* Given + // Ciphertext does not carry the magic where the format puts it, which is the one signal + // this reader has that the caller skipped decryption. + let mut image = plain_nca(0x400); + image[0x200..0x204].copy_from_slice(&[0xDE, 0xAD, 0xBE, 0xEF]); + + //* When + let result = Nca::try_from_bytes(&image); + + //* Then + assert!(matches!(result, Err(FromBytesError::InvalidMagic { .. }))); + } + + #[test] + fn try_from_bytes_with_a_truncated_image_fails() { + //* Given + let image = plain_nca(0x400); + let truncated = &image[..0x800]; + + //* When + let result = Nca::try_from_bytes(truncated); + + //* Then + assert!(matches!(result, Err(FromBytesError::BufferTooSmall { .. }))); + } + + #[test] + fn try_from_bytes_with_a_section_past_the_end_fails() { + //* Given + // The section's end is pushed past what the buffer holds. + let mut image = plain_nca(0x400); + let end_at = 0x200 + 0x40 + 0x4; + image[end_at..end_at + 4].copy_from_slice(&0xFFFFu32.to_le_bytes()); + + //* When + let result = Nca::try_from_bytes(&image); + + //* Then + assert!(matches!( + result, + Err(FromBytesError::SectionOutOfBounds { .. }) + )); + } + + #[test] + fn try_from_bytes_with_an_unknown_content_type_fails() { + //* Given + let mut image = plain_nca(0x400); + image[0x205] = 0x63; + + //* When + let result = Nca::try_from_bytes(&image); + + //* Then + assert!(matches!( + result, + Err(FromBytesError::UnknownContentType { found: 0x63 }) + )); + } + + #[test] + fn section_with_a_partition_section_returns_the_archive_alone() { + //* Given + let image = plain_nca(0x400); + let nca = Nca::try_from_bytes(&image).expect("a built archive should parse"); + + //* When + let section = nca.section(0).expect("index 0 holds a section"); + + //* Then + assert_eq!( + section.data(), + &vec![0xABu8; 0x400][..], + "the hash table should be stripped from the data region" + ); + assert!(matches!(section.superblock(), Superblock::Pfs0(_))); + assert_eq!(section.encryption(), NcaCryptType::None); + } + + #[test] + fn section_with_an_empty_slot_returns_none() { + //* Given + let image = plain_nca(0x400); + let nca = Nca::try_from_bytes(&image).expect("a built archive should parse"); + + //* When + let section = nca.section(1); + + //* Then + assert!(section.is_none()); + } + + #[test] + fn sections_with_one_section_placed_yields_only_it() { + //* Given + let image = plain_nca(0x400); + let nca = Nca::try_from_bytes(&image).expect("a built archive should parse"); + + //* When + let indices: alloc::vec::Vec = nca.sections().map(|s| s.index()).collect(); + + //* Then + assert_eq!(indices, vec![0]); + } + + #[test] + fn counter_with_an_unencrypted_section_returns_none() { + //* Given + let image = plain_nca(0x400); + let nca = Nca::try_from_bytes(&image).expect("a built archive should parse"); + let section = nca.section(0).expect("index 0 holds a section"); + + //* When + let counter = section.counter(); + + //* Then + assert!(counter.is_none()); + } + + #[test] + fn key_generation_with_a_first_generation_archive_returns_one() { + //* Given + let image = NcaBuilder::new(NcaContentType::Program, 0x0100_0000_0000_1000) + .key_generation(KeyGeneration::FIRST) + .section( + 0, + Section { + data: SectionData::Partition { + archive: vec![0xAB; 0x200], + hash_block_size: 0x1000, + }, + encryption: SectionEncryption::None, + }, + ) + .expect("placing a section at index 0 should succeed") + .build() + .expect("a small archive should build") + .to_bytes(); + let nca = Nca::try_from_bytes(&image).expect("a built archive should parse"); + + //* When + let generation = nca.key_generation(); + + //* Then + assert_eq!( + generation, 1, + "both header fields are zero for the first generation" + ); + } +} diff --git a/src/read/pfs0.rs b/src/read/pfs0.rs new file mode 100644 index 0000000..47c7370 --- /dev/null +++ b/src/read/pfs0.rs @@ -0,0 +1,449 @@ +//! Validated reader over a PFS0 archive and the files it holds. +//! +//! [`Pfs0::try_from_bytes`] checks the magic and proves the entry table, the string table, and every +//! file's bounds lie inside the buffer before borrowing it, so the accessors below return slices +//! without re-checking and cannot panic on a malformed archive. +//! +//! Proving every entry up front is affordable here in a way it is not for a RomFS: the entry table +//! is a flat array whose length the header states, so the whole of it is walked in one pass. That is +//! why a file's bytes come back as a plain slice rather than a `Result`. +//! +//! Neither offset in an entry is absolute — the file offset is measured from the start of the data +//! region and the name offset from the start of the string table — so both are resolved here +//! against regions this reader has already located. + +use zerocopy::FromBytes as _; + +use crate::raw::pfs0::{PFS0_MAGIC, Pfs0FileEntry, Pfs0Header}; + +/// A borrowed view of a PFS0 archive whose every file has already been bounds-checked. +/// +/// Construction is where every check happens, so a `Pfs0` that exists is one whose files lie inside +/// the buffer and whose names are terminated inside the string table. +pub struct Pfs0<'a> { + header: &'a Pfs0Header, + entries: &'a [Pfs0FileEntry], + string_table: &'a [u8], + data: &'a [u8], +} + +impl<'a> Pfs0<'a> { + /// Validate `bytes` as a PFS0 and borrow it, proving every file lies inside the buffer. + /// + /// # Errors + /// + /// Returns an error if the buffer cannot hold the header, the entry table, or the string table, + /// if the magic does not match, if a file's offset and size overflow or run past the end of the + /// data region, or if a name starts outside the string table or is never terminated inside it. + pub fn try_from_bytes(bytes: &'a [u8]) -> Result { + let (header, rest) = + Pfs0Header::ref_from_prefix(bytes).map_err(|_| FromBytesError::BufferTooSmall { + required: size_of::(), + available: bytes.len(), + })?; + + if header.magic.get() != PFS0_MAGIC { + return Err(FromBytesError::InvalidMagic { + found: header.magic.get(), + }); + } + + let file_count = header.file_count.get() as usize; + let string_table_size = header.string_table_size.get() as usize; + + let (entries, rest) = <[Pfs0FileEntry]>::ref_from_prefix_with_elems(rest, file_count) + .map_err(|_| FromBytesError::EntryTableOutOfBounds { + file_count, + available: bytes.len(), + })?; + + let string_table = + rest.get(..string_table_size) + .ok_or(FromBytesError::StringTableOutOfBounds { + string_table_size, + available: rest.len(), + })?; + + // Indexing past the string table is what the check above just proved. + let data = &rest[string_table_size..]; + + for (index, entry) in entries.iter().enumerate() { + check_file_bounds(index, entry, data.len())?; + check_name_bounds(index, entry, string_table)?; + } + + Ok(Self { + header, + entries, + string_table, + data, + }) + } + + /// The header stating how many files the archive holds and how long its string table is. + pub fn header(&self) -> &Pfs0Header { + self.header + } + + /// Number of files in the archive. + pub fn file_count(&self) -> usize { + self.entries.len() + } + + /// The file at `index`, or `None` when the archive holds fewer files than that. + pub fn file(&self, index: usize) -> Option> { + let entry = self.entries.get(index)?; + Some(self.resolve(entry)) + } + + /// Every file in the archive, in the order the entry table lists them. + pub fn files(&self) -> impl Iterator> + '_ { + self.entries.iter().map(|entry| self.resolve(entry)) + } + + /// The file stored under `name`, or `None` when the archive holds no such entry. + /// + /// A name that is not valid UTF-8 reads as empty, so a malformed entry cannot match a lookup. + pub fn file_by_name(&self, name: &str) -> Option> { + self.files().find(|file| file.name() == name) + } + + // Every bound reached here was proven by `try_from_bytes`, which is what lets this return a + // file rather than a `Result`. + fn resolve(&self, entry: &'a Pfs0FileEntry) -> Pfs0File<'a> { + let offset = entry.offset.get() as usize; + let size = entry.size.get() as usize; + let name_offset = entry.string_table_offset.get() as usize; + + let name_bytes = &self.string_table[name_offset..]; + let name_end = name_bytes + .iter() + .position(|byte| *byte == 0) + .unwrap_or(name_bytes.len()); + + Pfs0File { + name: core::str::from_utf8(&name_bytes[..name_end]).unwrap_or(""), + data: &self.data[offset..offset + size], + } + } +} + +/// Prove one entry's bytes lie inside the data region. +fn check_file_bounds( + index: usize, + entry: &Pfs0FileEntry, + available: usize, +) -> Result<(), FromBytesError> { + let offset = entry.offset.get() as usize; + let size = entry.size.get() as usize; + + let end = offset + .checked_add(size) + .ok_or(FromBytesError::FileBoundsOverflow { + file_index: index, + offset, + size, + })?; + + if end > available { + return Err(FromBytesError::FileOutOfBounds { + file_index: index, + offset, + size, + available, + }); + } + + Ok(()) +} + +/// Prove one entry's name starts inside the string table and is terminated within it. +fn check_name_bounds( + index: usize, + entry: &Pfs0FileEntry, + string_table: &[u8], +) -> Result<(), FromBytesError> { + let offset = entry.string_table_offset.get() as usize; + + let name_bytes = string_table + .get(offset..) + .ok_or(FromBytesError::NameOutOfBounds { + file_index: index, + offset, + string_table_size: string_table.len(), + })?; + + if !name_bytes.contains(&0) { + return Err(FromBytesError::NameUnterminated { + file_index: index, + offset, + }); + } + + Ok(()) +} + +/// Errors that can occur when parsing a PFS0 from bytes +#[derive(Debug, thiserror::Error)] +pub enum FromBytesError { + /// Buffer is too small to contain the header + #[error("buffer too small: need {required} bytes, have {available}")] + BufferTooSmall { + /// Number of bytes required + required: usize, + /// Number of bytes available + available: usize, + }, + /// Magic number does not match PFS0 (0x30534650) + #[error("invalid PFS0 magic: found {found:#010x}")] + InvalidMagic { + /// The magic that was found in place of `PFS0` + found: u32, + }, + /// The entry table the header declares does not fit in the buffer + #[error("entry table of {file_count} files does not fit in {available} bytes")] + EntryTableOutOfBounds { + /// Number of files the header declares + file_count: usize, + /// Number of bytes the whole archive occupies + available: usize, + }, + /// The string table the header declares does not fit in the buffer + #[error("string table of {string_table_size} bytes does not fit in {available} bytes")] + StringTableOutOfBounds { + /// Length the header declares for the string table + string_table_size: usize, + /// Number of bytes left after the entry table + available: usize, + }, + /// A file's offset and size overflow when added + #[error("file {file_index} has offset {offset} and size {size}, which overflow")] + FileBoundsOverflow { + /// Index of the file in the entry table + file_index: usize, + /// Offset the entry records, from the start of the data region + offset: usize, + /// Size the entry records + size: usize, + }, + /// A file extends past the end of the data region + #[error("file {file_index} at {offset} of size {size} runs past the {available} available")] + FileOutOfBounds { + /// Index of the file in the entry table + file_index: usize, + /// Offset the entry records, from the start of the data region + offset: usize, + /// Size the entry records + size: usize, + /// Number of bytes the data region holds + available: usize, + }, + /// A file's name starts past the end of the string table + #[error( + "file {file_index} names an offset {offset} outside the {string_table_size}-byte table" + )] + NameOutOfBounds { + /// Index of the file in the entry table + file_index: usize, + /// Offset the entry records, from the start of the string table + offset: usize, + /// Length of the string table + string_table_size: usize, + }, + /// A file's name has no terminator before the end of the string table + #[error("file {file_index} has an unterminated name at offset {offset}")] + NameUnterminated { + /// Index of the file in the entry table + file_index: usize, + /// Offset the entry records, from the start of the string table + offset: usize, + }, +} + +/// One file in the archive, with its name and its bytes already resolved. +pub struct Pfs0File<'a> { + name: &'a str, + data: &'a [u8], +} + +impl<'a> Pfs0File<'a> { + /// The name the file is stored under. + /// + /// A name that is not valid UTF-8 reads as empty rather than failing, so a malformed entry + /// cannot match a lookup. + pub fn name(&self) -> &'a str { + self.name + } + + /// The file's bytes, borrowed from the archive. + pub fn data(&self) -> &'a [u8] { + self.data + } +} + +#[cfg(all(test, feature = "alloc"))] +mod tests { + use alloc::vec::Vec; + + use super::{FromBytesError, Pfs0}; + + /// Build a PFS0 holding `files`, laid out the way the format requires. + fn archive(files: &[(&str, &[u8])]) -> Vec { + let mut string_table = Vec::new(); + let mut name_offsets = Vec::new(); + for (name, _) in files { + name_offsets.push(string_table.len() as u32); + string_table.extend_from_slice(name.as_bytes()); + string_table.push(0); + } + while string_table.len() % 0x20 != 0 { + string_table.push(0); + } + + let mut entries = Vec::new(); + let mut offset = 0u64; + for ((_, data), name_offset) in files.iter().zip(name_offsets) { + entries.extend_from_slice(&offset.to_le_bytes()); + entries.extend_from_slice(&(data.len() as u64).to_le_bytes()); + entries.extend_from_slice(&name_offset.to_le_bytes()); + entries.extend_from_slice(&0u32.to_le_bytes()); + offset += data.len() as u64; + } + + let mut image = Vec::new(); + image.extend_from_slice(&0x3053_4650u32.to_le_bytes()); + image.extend_from_slice(&(files.len() as u32).to_le_bytes()); + image.extend_from_slice(&(string_table.len() as u32).to_le_bytes()); + image.extend_from_slice(&0u32.to_le_bytes()); + image.extend_from_slice(&entries); + image.extend_from_slice(&string_table); + for (_, data) in files { + image.extend_from_slice(data); + } + image + } + + #[test] + fn try_from_bytes_with_two_files_resolves_both() { + //* Given + let image = archive(&[("main", b"code"), ("main.npdm", b"descriptor")]); + + //* When + let pfs0 = Pfs0::try_from_bytes(&image).expect("a well-formed archive should parse"); + + //* Then + assert_eq!(pfs0.file_count(), 2); + let main = pfs0.file(0).expect("the first entry should resolve"); + assert_eq!(main.name(), "main"); + assert_eq!(main.data(), b"code"); + let npdm = pfs0.file(1).expect("the second entry should resolve"); + assert_eq!(npdm.name(), "main.npdm"); + assert_eq!(npdm.data(), b"descriptor"); + } + + #[test] + fn try_from_bytes_with_no_files_succeeds() { + //* Given + let image = archive(&[]); + + //* When + let pfs0 = Pfs0::try_from_bytes(&image).expect("an empty archive should parse"); + + //* Then + assert_eq!(pfs0.file_count(), 0); + } + + #[test] + fn try_from_bytes_with_wrong_magic_fails() { + //* Given + let mut image = archive(&[("main", b"code")]); + image[0] = b'X'; + + //* When + let result = Pfs0::try_from_bytes(&image); + + //* Then + assert!(matches!(result, Err(FromBytesError::InvalidMagic { .. }))); + } + + #[test] + fn try_from_bytes_with_a_file_past_the_data_region_fails() { + //* Given + // The entry's size is widened past what the archive actually carries. + let mut image = archive(&[("main", b"code")]); + let size_at = 0x10 + 0x8; + image[size_at..size_at + 8].copy_from_slice(&0x1000u64.to_le_bytes()); + + //* When + let result = Pfs0::try_from_bytes(&image); + + //* Then + assert!(matches!( + result, + Err(FromBytesError::FileOutOfBounds { .. }) + )); + } + + #[test] + fn try_from_bytes_with_a_name_outside_the_string_table_fails() { + //* Given + let mut image = archive(&[("main", b"code")]); + let name_offset_at = 0x10 + 0x10; + image[name_offset_at..name_offset_at + 4].copy_from_slice(&0xFFFF_FFFFu32.to_le_bytes()); + + //* When + let result = Pfs0::try_from_bytes(&image); + + //* Then + assert!(matches!( + result, + Err(FromBytesError::NameOutOfBounds { .. }) + )); + } + + #[test] + fn try_from_bytes_with_a_truncated_entry_table_fails() { + //* Given + // The header claims a file the buffer has no entry for. + let mut image = archive(&[]); + image[0x4..0x8].copy_from_slice(&1u32.to_le_bytes()); + + //* When + let result = Pfs0::try_from_bytes(&image); + + //* Then + assert!(matches!( + result, + Err(FromBytesError::EntryTableOutOfBounds { .. }) + )); + } + + #[test] + fn file_by_name_with_a_name_the_archive_holds_returns_it() { + //* Given + let image = archive(&[("main", b"code"), ("main.npdm", b"descriptor")]); + let pfs0 = Pfs0::try_from_bytes(&image).expect("a well-formed archive should parse"); + + //* When + let file = pfs0.file_by_name("main.npdm"); + + //* Then + assert_eq!( + file.expect("the archive holds this name").data(), + b"descriptor" + ); + } + + #[test] + fn file_by_name_with_a_name_the_archive_lacks_returns_none() { + //* Given + let image = archive(&[("main", b"code")]); + let pfs0 = Pfs0::try_from_bytes(&image).expect("a well-formed archive should parse"); + + //* When + let file = pfs0.file_by_name("logo"); + + //* Then + assert!(file.is_none()); + } +}