Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Implement NCA decryption, take 2 #68

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 74 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ clap = { version = "4.0.32", optional = true, features = ["cargo"] }
structopt = { version = "0.3", optional = true }
sha2 = "0.10.6"
scroll = { version = "0.11.0", optional = true }
serde = "1"
serde_derive = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
cargo_metadata = { version = "0.15.2", optional = true }
semver = { version = "1.0.16", optional = true }
Expand All @@ -41,12 +40,18 @@ derive_more = "0.99"
blz-nx = "1.0"
bit_field = "0.10"
cargo-toml2 = { version = "1.3.2", optional = true }
binrw = "0.10.0"
pretty-hex = "0.3.0"
hex = "0.4.3"


cipher = "0.4.3"
digest = "0.10.6"
ctr = "0.9.2"
aes = "0.8.2"
cmac = "0.7.1"

xts-mode = "0.5.1"

[features]
binaries = ["structopt", "cargo_metadata", "semver", "scroll", "goblin", "clap", "cargo-toml2"]
106 changes: 101 additions & 5 deletions src/bin/linkle_clap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,44 @@ enum Opt {
#[structopt(long = "console-unique")]
show_console_unique: bool,
},
/// Extract NCA
#[structopt(name = "nca_extract")]
NcaExtract {
/// Sets the input file to use.
#[structopt(parse(from_os_str))]
input_file: PathBuf,

/// Sets the output file to extract the header to.
#[structopt(parse(from_os_str), long = "header-json")]
header_file: Option<PathBuf>,

/// Sets the output file to extract the section0 to.
#[structopt(parse(from_os_str), long = "section0")]
section0_file: Option<PathBuf>,

/// Sets the output file to extract the section1 to.
#[structopt(parse(from_os_str), long = "section1")]
section1_file: Option<PathBuf>,

/// Sets the output file to extract the section2 to.
#[structopt(parse(from_os_str), long = "section2")]
section2_file: Option<PathBuf>,

/// Sets the output file to extract the section3 to.
#[structopt(parse(from_os_str), long = "section3")]
section3_file: Option<PathBuf>,

/// Sets the title key to use (if the NCA has RightsId crypto).
title_key: Option<String>,

/// Use development keys instead of retail
#[structopt(short = "d", long = "dev")]
dev: bool,

/// Keyfile
#[structopt(parse(from_os_str), short = "k", long = "keyset")]
keyfile: Option<PathBuf>,
},
}

fn create_nxo(
Expand Down Expand Up @@ -241,17 +279,54 @@ fn print_keys(
console_unique: bool,
minimal: bool,
) -> Result<(), linkle::error::Error> {
let keys = if is_dev {
linkle::pki::Keys::new_dev(key_path).unwrap()
} else {
linkle::pki::Keys::new_retail(key_path).unwrap()
};
let keys = linkle::pki::Keys::new(key_path, is_dev)?;

keys.write(&mut std::io::stdout(), console_unique, minimal)
.unwrap();
Ok(())
}

fn extract_nca(
input_file: &Path,
is_dev: bool,
key_path: Option<&Path>,
title_key: Option<&str>,
output_header_json: Option<&Path>,
output_section0: Option<&Path>,
output_section1: Option<&Path>,
output_section2: Option<&Path>,
output_section3: Option<&Path>,
) -> Result<(), linkle::error::Error> {
let keys = linkle::pki::Keys::new(key_path, is_dev)?;
let title_key = title_key.map(linkle::pki::parse_title_key).transpose()?;
let nca = linkle::format::nca::Nca::from_file(&keys, File::open(input_file)?, title_key)?;
if let Some(output_header_json) = output_header_json {
let mut output_header_json = File::create(output_header_json)?;
serde_json::to_writer_pretty(&mut output_header_json, &nca.info())?;
}
if let Some(output_section0) = output_section0 {
let mut output_section0 = File::create(output_section0)?;
let mut section = nca.raw_section(0).unwrap();
std::io::copy(&mut section, &mut output_section0)?;
}
if let Some(output_section1) = output_section1 {
let mut output_section1 = File::create(output_section1)?;
let mut section = nca.raw_section(1).unwrap();
std::io::copy(&mut section, &mut output_section1)?;
}
if let Some(output_section2) = output_section2 {
let mut output_section2 = File::create(output_section2)?;
let mut section = nca.raw_section(2).unwrap();
std::io::copy(&mut section, &mut output_section2)?;
}
if let Some(output_section3) = output_section3 {
let mut output_section3 = File::create(output_section3)?;
let mut section = nca.raw_section(3).unwrap();
std::io::copy(&mut section, &mut output_section3)?;
}
Ok(())
}

fn to_opt_ref<U: ?Sized, T: AsRef<U>>(s: &Option<T>) -> Option<&U> {
s.as_ref().map(AsRef::as_ref)
}
Expand Down Expand Up @@ -303,6 +378,27 @@ fn process_args(app: &Opt) {
show_console_unique,
minimal,
} => print_keys(*dev, to_opt_ref(keyfile), *show_console_unique, *minimal),
Opt::NcaExtract {
ref input_file,
ref header_file,
ref section0_file,
ref section1_file,
ref section2_file,
ref section3_file,
title_key,
dev,
ref keyfile,
} => extract_nca(
input_file,
*dev,
to_opt_ref(keyfile),
to_opt_ref(title_key),
to_opt_ref(header_file),
to_opt_ref(section0_file),
to_opt_ref(section1_file),
to_opt_ref(section2_file),
to_opt_ref(section3_file),
),
};

if let Err(e) = res {
Expand Down
19 changes: 19 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::format::nca::RightsId;
use crate::pki::KeyName;
use snafu::Snafu;
use snafu::{Backtrace, GenerateImplicitData};
use std::io;
Expand Down Expand Up @@ -57,6 +59,23 @@ pub enum Error {
error: PathBuf,
backtrace: Backtrace,
},
#[snafu(display("Missing key {:?}. Make sure your keyfile is complete", key_name))]
MissingKey {
key_name: KeyName,
backtrace: Backtrace,
},
#[snafu(display("Missing titlekey for {}. Make sure you have provided it", rights_id))]
MissingTitleKey {
rights_id: RightsId,
backtrace: Backtrace,
},
#[snafu(display("Failed to parse NCA. Make sure your {} key is correct.", key_name))]
NcaParse {
key_name: &'static str,
backtrace: Backtrace,
},
#[snafu(display("Missing section {}.", index))]
MissingSection { index: usize, backtrace: Backtrace },
}

impl Error {
Expand Down
1 change: 1 addition & 0 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod nacp;
pub mod nca;
mod npdm;
pub mod nxo;
pub mod pfs0;
Expand Down
2 changes: 1 addition & 1 deletion src/format/nacp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::format::utils;
use byteorder::{LittleEndian, WriteBytesExt};
use serde_derive::{Deserialize, Serialize};
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::Write;

Expand Down
Loading