Fixed content decryption, created base for real rustii CLI

rustii currently only supports unpacking WADs, with packing support being a work-in-progress.
This commit is contained in:
2025-03-18 20:39:38 -04:00
parent 83dc83d2d6
commit 6ab9993dd9
14 changed files with 406 additions and 50 deletions

View File

@@ -0,0 +1,37 @@
// Sample file for testing rustii library stuff.
use std::fs;
use rustii::title::{tmd, ticket, content, crypto, wad};
fn main() {
let data = fs::read("sm.wad").unwrap();
let wad = wad::WAD::from_bytes(&data).unwrap();
println!("size of tmd: {:?}", wad.tmd().len());
let tmd = tmd::TMD::from_bytes(&wad.tmd()).unwrap();
println!("num content records: {:?}", tmd.content_records.len());
println!("first record data: {:?}", tmd.content_records.first().unwrap());
assert_eq!(wad.tmd(), tmd.to_bytes().unwrap());
let tik = ticket::Ticket::from_bytes(&wad.ticket()).unwrap();
println!("title version from ticket is: {:?}", tik.title_version);
println!("title key (enc): {:?}", tik.title_key);
println!("title key (dec): {:?}", tik.dec_title_key());
assert_eq!(wad.ticket(), tik.to_bytes().unwrap());
let content_region = content::ContentRegion::from_bytes(&wad.content(), tmd.content_records).unwrap();
assert_eq!(wad.content(), content_region.to_bytes().unwrap());
println!("content OK");
let content_dec = content_region.get_content_by_index(0, tik.dec_title_key()).unwrap();
println!("content dec from index: {:?}", content_dec);
let content = content_region.get_enc_content_by_index(0).unwrap();
assert_eq!(content, crypto::encrypt_content(&content_dec, tik.dec_title_key(), 0, content_region.content_records[0].content_size));
println!("content re-encrypted OK");
println!("wad header: {:?}", wad.header);
let repacked = wad.to_bytes().unwrap();
assert_eq!(repacked, data);
println!("wad packed OK");
}

View File

@@ -1,37 +1,45 @@
// Sample file for testing rustii library stuff.
// main.rs from rustii (c) 2025 NinjaCheetah & Contributors
// https://github.com/NinjaCheetah/rustii
//
// Base for the rustii CLI that handles argument parsing and directs execution to the proper module.
use std::fs;
use rustii::title::{tmd, ticket, content, crypto, wad};
mod title;
use clap::{Subcommand, Parser};
use title::wad;
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
#[command(arg_required_else_help = true)]
enum Commands {
/// Pack/unpack/edit a WAD file
Wad {
#[command(subcommand)]
command: Option<wad::Commands>,
},
}
fn main() {
let data = fs::read("sm.wad").unwrap();
let wad = wad::WAD::from_bytes(&data).unwrap();
println!("size of tmd: {:?}", wad.tmd().len());
let tmd = tmd::TMD::from_bytes(&wad.tmd()).unwrap();
println!("num content records: {:?}", tmd.content_records.len());
println!("first record data: {:?}", tmd.content_records.first().unwrap());
assert_eq!(wad.tmd(), tmd.to_bytes().unwrap());
let cli = Cli::parse();
let tik = ticket::Ticket::from_bytes(&wad.ticket()).unwrap();
println!("title version from ticket is: {:?}", tik.title_version);
println!("title key (enc): {:?}", tik.title_key);
println!("title key (dec): {:?}", tik.dec_title_key());
assert_eq!(wad.ticket(), tik.to_bytes().unwrap());
let content_region = content::ContentRegion::from_bytes(&wad.content(), tmd.content_records).unwrap();
assert_eq!(wad.content(), content_region.to_bytes().unwrap());
println!("content OK");
let content_dec = content_region.get_content_by_index(0, tik.dec_title_key()).unwrap();
println!("content dec from index: {:?}", content_dec);
let content = content_region.get_enc_content_by_index(0).unwrap();
assert_eq!(content, crypto::encrypt_content(&content_dec, tik.dec_title_key(), 0, content_region.content_records[0].content_size));
println!("content re-encrypted OK");
println!("wad header: {:?}", wad.header);
let repacked = wad.to_bytes().unwrap();
assert_eq!(repacked, data);
println!("wad packed OK");
}
match &cli.command {
Some(Commands::Wad { command }) => {
match command {
Some(wad::Commands::Pack { input, output}) => {
wad::pack_wad(input, output)
},
Some(wad::Commands::Unpack { input, output }) => {
wad::unpack_wad(input, output)
},
&None => { /* This is handled by clap */}
}
}
None => {}
}
}

View File

@@ -0,0 +1,4 @@
// title/mod.rs from rustii (c) 2025 NinjaCheetah & Contributors
// https://github.com/NinjaCheetah/rustii
pub mod wad;

View File

@@ -0,0 +1,59 @@
// title/wad.rs from rustii (c) 2025 NinjaCheetah & Contributors
// https://github.com/NinjaCheetah/rustii
//
// Code for WAD-related commands in the rustii CLI.
use clap::Subcommand;
use std::{str, fs};
use std::path::Path;
use rustii::title::{tmd, ticket, wad, content};
#[derive(Subcommand)]
#[command(arg_required_else_help = true)]
pub enum Commands {
/// Pack a directory into a WAD file
Pack {
input: String,
output: String
},
/// Unpack a WAD file into a directory
Unpack {
input: String,
output: String
}
}
pub fn pack_wad(input: &str, output: &str) {
print!("packing");
}
pub fn unpack_wad(input: &str, output: &str) {
let wad_file = fs::read(input).expect("could not read WAD");
let wad = wad::WAD::from_bytes(&wad_file).expect("could not parse WAD");
let tmd = tmd::TMD::from_bytes(&wad.tmd()).expect("could not parse TMD");
let tik = ticket::Ticket::from_bytes(&wad.ticket()).expect("could not parse Ticket");
let cert_data = &wad.cert_chain();
let meta_data = &wad.meta();
// Create output directory if it doesn't exist.
if !Path::new(output).exists() {
fs::create_dir(output).expect("could not create output directory");
}
let out_path = Path::new(output);
// Write out all WAD components.
let tmd_file_name = format!("{}.tmd", hex::encode(tmd.title_id));
fs::write(Path::join(out_path, tmd_file_name), tmd.to_bytes().unwrap()).expect("could not write TMD file");
let ticket_file_name = format!("{}.tik", hex::encode(tmd.title_id));
fs::write(Path::join(out_path, ticket_file_name), tik.to_bytes().unwrap()).expect("could not write Ticket file");
let cert_file_name = format!("{}.cert", hex::encode(tmd.title_id));
fs::write(Path::join(out_path, cert_file_name), cert_data).expect("could not write Cert file");
let meta_file_name = format!("{}.footer", hex::encode(tmd.title_id));
fs::write(Path::join(out_path, meta_file_name), meta_data).expect("could not write footer file");
// Iterate over contents, decrypt them, and write them out.
let content_region = content::ContentRegion::from_bytes(&wad.content(), tmd.content_records).unwrap();
for i in 0..tmd.num_contents {
let content_file_name = format!("{:08X}.app", content_region.content_records[i as usize].index);
let dec_content = content_region.get_content_by_index(i as usize, tik.dec_title_key()).unwrap();
fs::write(Path::join(out_path, content_file_name), dec_content).unwrap();
}
println!("WAD file unpacked!");
}

View File

@@ -1,5 +1,5 @@
// lib.rs from rustii-lib (c) 2025 NinjaCheetah & Contributors
// https://github.com/NinjaCheetah/rustii-lib
// lib.rs from rustii (c) 2025 NinjaCheetah & Contributors
// https://github.com/NinjaCheetah/rustii
pub mod title;

View File

@@ -1,5 +1,5 @@
// title/commonkeys.rs from rustii-lib (c) 2025 NinjaCheetah & Contributors
// https://github.com/NinjaCheetah/rustii-lib
// https://github.com/NinjaCheetah/rustii
const COMMON_KEY: &str = "ebe42a225e8593e448d9c5457381aaf7";
const KOREAN_KEY: &str = "63b82bb4f4614e2e13f2fefbba4c9b7e";

View File

@@ -1,5 +1,5 @@
// title/content.rs from rustii-lib (c) 2025 NinjaCheetah & Contributors
// https://github.com/NinjaCheetah/rustii-lib
// title/content.rs from rustii (c) 2025 NinjaCheetah & Contributors
// https://github.com/NinjaCheetah/rustii
//
// Implements content parsing and editing.
@@ -95,7 +95,8 @@ impl ContentRegion {
pub fn get_content_by_index(&self, index: usize, title_key: [u8; 16]) -> Result<Vec<u8>, ContentError> {
let content = self.get_enc_content_by_index(index)?;
// Verify the hash of the decrypted content against its record.
let content_dec = decrypt_content(&content, title_key, self.content_records[index].index);
let mut content_dec = decrypt_content(&content, title_key, self.content_records[index].index);
content_dec.resize(self.content_records[index].content_size as usize, 0);
let mut hasher = Sha1::new();
hasher.update(content_dec.clone());
let result = hasher.finalize();

View File

@@ -1,5 +1,5 @@
// title/crypto.rs from rustii-lib (c) 2025 NinjaCheetah & Contributors
// https://github.com/NinjaCheetah/rustii-lib
// title/crypto.rs from rustii (c) 2025 NinjaCheetah & Contributors
// https://github.com/NinjaCheetah/rustii
//
// Implements the common crypto functions required to handle Wii content encryption.

View File

@@ -1,5 +1,5 @@
// title/mod.rs from rustii-lib (c) 2025 NinjaCheetah & Contributors
// https://github.com/NinjaCheetah/rustii-lib
// title/mod.rs from rustii (c) 2025 NinjaCheetah & Contributors
// https://github.com/NinjaCheetah/rustii
pub mod commonkeys;
pub mod content;

View File

@@ -1,5 +1,5 @@
// title/tik.rs from rustii-lib (c) 2025 NinjaCheetah & Contributors
// https://github.com/NinjaCheetah/rustii-lib
// title/tik.rs from rustii (c) 2025 NinjaCheetah & Contributors
// https://github.com/NinjaCheetah/rustii
//
// Implements the structures and methods required for Ticket parsing and editing.

View File

@@ -1,5 +1,5 @@
// title/tmd.rs from rustii-lib (c) 2025 NinjaCheetah & Contributors
// https://github.com/NinjaCheetah/rustii-lib
// title/tmd.rs from rustii (c) 2025 NinjaCheetah & Contributors
// https://github.com/NinjaCheetah/rustii
//
// Implements the structures and methods required for TMD parsing and editing.

View File

@@ -1,5 +1,5 @@
// title/wad.rs from rustii-lib (c) 2025 NinjaCheetah & Contributors
// https://github.com/NinjaCheetah/rustii-lib
// title/wad.rs from rustii (c) 2025 NinjaCheetah & Contributors
// https://github.com/NinjaCheetah/rustii
//
// Implements the structures and methods required for WAD parsing and editing.
@@ -141,7 +141,7 @@ impl WAD {
};
Ok(wad)
}
pub fn to_bytes(&self) -> Result<Vec<u8>, WADError> {
let mut buf = Vec::new();
buf.write_u32::<BigEndian>(self.header.header_size).map_err(WADError::IOError)?;