Reorganize a bit
This commit is contained in:
parent
fc74b28d84
commit
9402735559
2 changed files with 69 additions and 63 deletions
126
src/cli.rs
126
src/cli.rs
|
@ -3,9 +3,8 @@ use std::{fs::File, io::BufReader, path::Path};
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use openssl::{
|
use openssl::{
|
||||||
bn::{BigNum, BigNumRef},
|
bn::BigNum,
|
||||||
ec::{EcGroup, EcGroupRef, EcPoint, EcPointRef},
|
ec::{EcGroup, EcPoint},
|
||||||
pkey::Private,
|
|
||||||
};
|
};
|
||||||
use serde_json::from_reader;
|
use serde_json::from_reader;
|
||||||
|
|
||||||
|
@ -57,59 +56,6 @@ pub struct Options {
|
||||||
application_mode: Mode,
|
application_mode: Mode,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_json<P: AsRef<Path>>(path: P) -> Result<serde_json::Value> {
|
|
||||||
let file = File::open(path)?;
|
|
||||||
let reader = BufReader::new(file);
|
|
||||||
let json = from_reader(reader)?;
|
|
||||||
Ok(json)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse_command_line() -> Result<Options> {
|
|
||||||
let mut args = Options::parse();
|
|
||||||
if args.instid.is_some() {
|
|
||||||
args.application_mode = Mode::ConfirmationId;
|
|
||||||
}
|
|
||||||
Ok(args)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn validate_command_line(options: &mut Options) -> Result<serde_json::Value> {
|
|
||||||
if options.verbose {
|
|
||||||
println!("Loading keys file {}", options.keys_filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
let keys = load_json(&options.keys_filename)?;
|
|
||||||
|
|
||||||
if options.verbose {
|
|
||||||
println!("Loaded keys from {} successfully", options.keys_filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
if options.list {
|
|
||||||
let products = keys["Products"].as_object().ok_or(anyhow!(
|
|
||||||
"`Products` object not found in {}",
|
|
||||||
options.keys_filename
|
|
||||||
))?;
|
|
||||||
for (key, value) in products.iter() {
|
|
||||||
println!("{}: {}", key, value["BINK"]);
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("\n\n** Please note: any BINK ID other than 2E is considered experimental at this time **\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
let bink_id = u32::from_str_radix(&options.binkid, 16)?;
|
|
||||||
|
|
||||||
if bink_id >= 0x40 {
|
|
||||||
options.application_mode = Mode::Bink2002;
|
|
||||||
}
|
|
||||||
|
|
||||||
if options.channel_id > 999 {
|
|
||||||
return Err(anyhow!(
|
|
||||||
"Refusing to create a key with a Channel ID greater than 999"
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(keys)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Cli {
|
pub struct Cli {
|
||||||
options: Options,
|
options: Options,
|
||||||
keys: serde_json::Value,
|
keys: serde_json::Value,
|
||||||
|
@ -123,7 +69,10 @@ pub struct Cli {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Cli {
|
impl Cli {
|
||||||
pub fn new(options: Options, keys: serde_json::Value) -> Self {
|
pub fn new() -> Result<Self> {
|
||||||
|
let mut options = Self::parse_command_line();
|
||||||
|
let keys = Self::validate_command_line(&mut options)?;
|
||||||
|
|
||||||
let bink = &keys["BINK"][&options.binkid];
|
let bink = &keys["BINK"][&options.binkid];
|
||||||
// We cannot produce a valid key without knowing the private key k. The reason for this is that
|
// We cannot produce a valid key without knowing the private key k. The reason for this is that
|
||||||
// we need the result of the function K(x; y) = kG(x; y).
|
// we need the result of the function K(x; y) = kG(x; y).
|
||||||
|
@ -165,7 +114,7 @@ impl Cli {
|
||||||
let (e_curve, gen_point, pub_point) =
|
let (e_curve, gen_point, pub_point) =
|
||||||
Cli::initialize_elliptic_curve(p, a, b, gx, gy, kx, ky);
|
Cli::initialize_elliptic_curve(p, a, b, gx, gy, kx, ky);
|
||||||
|
|
||||||
Self {
|
Ok(Self {
|
||||||
options,
|
options,
|
||||||
keys,
|
keys,
|
||||||
private_key,
|
private_key,
|
||||||
|
@ -175,9 +124,70 @@ impl Cli {
|
||||||
e_curve,
|
e_curve,
|
||||||
product_key: None,
|
product_key: None,
|
||||||
count: 0,
|
count: 0,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(&mut self) -> Result<()> {
|
||||||
|
match self.options.application_mode {
|
||||||
|
Mode::Bink1998 => todo!(),
|
||||||
|
Mode::Bink2002 => todo!(),
|
||||||
|
Mode::ConfirmationId => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_command_line() -> Options {
|
||||||
|
let mut args = Options::parse();
|
||||||
|
if args.instid.is_some() {
|
||||||
|
args.application_mode = Mode::ConfirmationId;
|
||||||
|
}
|
||||||
|
args
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_command_line(options: &mut Options) -> Result<serde_json::Value> {
|
||||||
|
if options.verbose {
|
||||||
|
println!("Loading keys file {}", options.keys_filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
let keys = Self::load_json(&options.keys_filename)?;
|
||||||
|
|
||||||
|
if options.verbose {
|
||||||
|
println!("Loaded keys from {} successfully", options.keys_filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
if options.list {
|
||||||
|
let products = keys["Products"].as_object().ok_or(anyhow!(
|
||||||
|
"`Products` object not found in {}",
|
||||||
|
options.keys_filename
|
||||||
|
))?;
|
||||||
|
for (key, value) in products.iter() {
|
||||||
|
println!("{}: {}", key, value["BINK"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("\n\n** Please note: any BINK ID other than 2E is considered experimental at this time **\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
let bink_id = u32::from_str_radix(&options.binkid, 16)?;
|
||||||
|
|
||||||
|
if bink_id >= 0x40 {
|
||||||
|
options.application_mode = Mode::Bink2002;
|
||||||
|
}
|
||||||
|
|
||||||
|
if options.channel_id > 999 {
|
||||||
|
return Err(anyhow!(
|
||||||
|
"Refusing to create a key with a Channel ID greater than 999"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(keys)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_json<P: AsRef<Path>>(path: P) -> Result<serde_json::Value> {
|
||||||
|
let file = File::open(path)?;
|
||||||
|
let reader = BufReader::new(file);
|
||||||
|
let json = from_reader(reader)?;
|
||||||
|
Ok(json)
|
||||||
|
}
|
||||||
|
|
||||||
fn initialize_elliptic_curve(
|
fn initialize_elliptic_curve(
|
||||||
p_sel: &str,
|
p_sel: &str,
|
||||||
a_sel: &str,
|
a_sel: &str,
|
||||||
|
|
|
@ -3,9 +3,5 @@ use anyhow::Result;
|
||||||
mod cli;
|
mod cli;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let mut args = cli::parse_command_line()?;
|
cli::Cli::new()?.run()
|
||||||
let keys = cli::validate_command_line(&mut args)?;
|
|
||||||
let _cli = cli::Cli::new(args, keys);
|
|
||||||
println!("Hello, world!");
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue