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

Implement NPDM support #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
513 changes: 451 additions & 62 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ byteorder = "1"
lz4 = "1.23.1"
clap = { version = "2", optional = true }
structopt = { version = "0.2", optional = true }
sha2 = "0.7.1"
sha2 = "0.8.0"
scroll = { version = "0.9.0", optional = true }
serde = "1"
serde_derive = "1"
Expand All @@ -41,6 +41,12 @@ derive_more = "0.13"
cmac = "0.2.0"
blz-nx = { git = "https://github.com/Thog/blz-nx-rs" }
bit_field = "0.10.0"
bincode = "1.1.4"
yasna = { version = "0.3", features = ["num-bigint"] }
pem = "0.6"
rand = "0.7"
rsa = { git = "https://github.com/RustCrypto/RSA", rev = "11500ed" }
hex = "0.3"

[features]
binaries = ["structopt", "cargo_metadata", "scroll", "goblin", "clap"]
43 changes: 42 additions & 1 deletion src/bin/linkle_clap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ enum Opt {
/// Key file to use
#[structopt(parse(from_os_str), short = "k", long = "keyset")]
keyfile: Option<PathBuf>,
},
/// Create an NPDM from a JSON-NPDM formatted file.
#[structopt(name = "npdm")]
Npdm {
/// Sets the input JSON file to use.
#[structopt(parse(from_os_str))]
input_file: PathBuf,

/// Sets the output NPDM file to use.
#[structopt(parse(from_os_str))]
output_file: PathBuf,

/// Use the given file for the ACID section.
#[structopt(long = "use-acid", parse(from_os_str), conflicts_with = "pem_file")]
acid_file: Option<PathBuf>,

/// Sign the ACID section with the given RSA private key.
#[structopt(long = "sign-with", parse(from_os_str))]
pem_file: Option<PathBuf>,
}
}

Expand Down Expand Up @@ -134,7 +153,7 @@ fn create_kip(input_file: &str, npdm_file: &str, output_file: &str) -> Result<()
let mut option = OpenOptions::new();
let output_option = option.write(true).create(true).truncate(true);
output_option.open(output_file)?;

nxo.write_kip1(&mut output_option.open(output_file).map_err(|err| (err, output_file))?, &npdm).map_err(|err| (err, output_file))?;
Ok(())
}
Expand Down Expand Up @@ -193,6 +212,27 @@ fn print_keys(is_dev: bool, key_path: Option<&Path>) -> Result<(), linkle::error
Ok(())
}

fn create_npdm(input_file: &Path, input_acid: Option<&Path>, sign_acid: Option<&Path>, output_file: &Path) -> Result<(), linkle::error::Error> {
use linkle::format::npdm::{NpdmJson, ACIDBehavior};

let npdm = NpdmJson::from_file(&input_file)?;
let mut option = OpenOptions::new();
let output_option = option.write(true).create(true).truncate(true);
let mut out_file = output_option.open(output_file).map_err(|err| (err, output_file))?;
let behavior = if sign_acid.is_some() && input_acid.is_some() {
// They are set as conflicting in clap, so we can't reach here.
unreachable!("Can't pass both sign_acid and input_acid.");
} else if let Some(input_pem) = sign_acid {
ACIDBehavior::Sign { pem_file_path: input_pem }
} else if let Some(input_acid) = input_acid {
ACIDBehavior::Use { acid_file_path: input_acid }
} else {
ACIDBehavior::Empty
};
npdm.into_npdm(&mut out_file, behavior)?;
Ok(())
}

fn to_opt_ref<U: ?Sized, T: AsRef<U>>(s: &Option<T>) -> Option<&U> {
s.as_ref().map(AsRef::as_ref)
}
Expand All @@ -207,6 +247,7 @@ fn process_args(app: &Opt) {
Opt::Nacp { ref input_file, ref output_file } => create_nacp(input_file, output_file),
Opt::Romfs { ref input_directory, ref output_file } => create_romfs(input_directory, output_file),
Opt::Keygen { dev, ref keyfile } => print_keys(*dev, to_opt_ref(keyfile)),
Opt::Npdm { ref input_file, ref pem_file, ref acid_file, ref output_file } => create_npdm(input_file, to_opt_ref(acid_file), to_opt_ref(pem_file), output_file)
};

if let Err(e) = res {
Expand Down
35 changes: 35 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use failure::Backtrace;
use block_modes::BlockModeError;
use failure::Fail;
use derive_more::Display;
use std::borrow::Cow;

#[derive(Debug, Fail, Display)]
pub enum Error {
Expand All @@ -32,6 +33,16 @@ pub enum Error {
RomFsSymlink(PathBuf, Backtrace),
#[display(fmt = "Unknown file type at {}", "_0.display()")]
RomFsFiletype(PathBuf, Backtrace),
#[display(fmt = "Invalid NPDM value for field {}", "_0")]
InvalidNpdmValue(Cow<'static, str>, Backtrace),
#[display(fmt = "Failed to serialize NPDM.")]
BincodeError(#[cause] Box<bincode::ErrorKind>, Backtrace),
#[display(fmt = "Failed to sign NPDM.")]
RsaError(#[cause] rsa::errors::Error, Backtrace),
#[display(fmt = "Failed to sign NPDM, invalid PEM.")]
PemError(#[cause] pem::PemError, Backtrace),
#[display(fmt = "Failed to sign NPDM, invalid PEM.")]
Asn1Error(#[cause] yasna::ASN1Error, Backtrace),
}

impl Error {
Expand Down Expand Up @@ -96,3 +107,27 @@ impl From<(usize, cmac::crypto_mac::MacError)> for Error {
Error::MacError(err, id, Backtrace::new())
}
}

impl From<Box<bincode::ErrorKind>> for Error {
fn from(err: Box<bincode::ErrorKind>) -> Error {
Error::BincodeError(err, Backtrace::new())
}
}

impl From<rsa::errors::Error> for Error {
fn from(err: rsa::errors::Error) -> Error {
Error::RsaError(err, Backtrace::new())
}
}

impl From<pem::PemError> for Error {
fn from(err: pem::PemError) -> Error {
Error::PemError(err, Backtrace::new())
}
}

impl From<yasna::ASN1Error> for Error {
fn from(err: yasna::ASN1Error) -> Error {
Error::Asn1Error(err, Backtrace::new())
}
}
2 changes: 1 addition & 1 deletion src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ pub mod nacp;
pub mod nxo;
pub mod pfs0;
pub mod romfs;
mod npdm;
pub mod npdm;
mod utils;
Loading