diff --git a/src/cli.rs b/src/cli.rs index 7b4b3fa..fcbfe6c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -3,9 +3,8 @@ use std::{fs::File, io::BufReader, path::Path}; use anyhow::{anyhow, Result}; use clap::Parser; use openssl::{ - bn::{BigNum, BigNumRef}, - ec::{EcGroup, EcGroupRef, EcPoint, EcPointRef}, - pkey::Private, + bn::BigNum, + ec::{EcGroup, EcPoint}, }; use serde_json::from_reader; @@ -57,59 +56,6 @@ pub struct Options { application_mode: Mode, } -pub fn load_json>(path: P) -> Result { - let file = File::open(path)?; - let reader = BufReader::new(file); - let json = from_reader(reader)?; - Ok(json) -} - -pub fn parse_command_line() -> Result { - 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 { - 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 { options: Options, keys: serde_json::Value, @@ -123,7 +69,10 @@ pub struct Cli { } impl Cli { - pub fn new(options: Options, keys: serde_json::Value) -> Self { + pub fn new() -> Result { + let mut options = Self::parse_command_line(); + let keys = Self::validate_command_line(&mut options)?; + 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 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) = Cli::initialize_elliptic_curve(p, a, b, gx, gy, kx, ky); - Self { + Ok(Self { options, keys, private_key, @@ -175,9 +124,70 @@ impl Cli { e_curve, product_key: None, 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 { + 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>(path: P) -> Result { + let file = File::open(path)?; + let reader = BufReader::new(file); + let json = from_reader(reader)?; + Ok(json) + } + fn initialize_elliptic_curve( p_sel: &str, a_sel: &str, diff --git a/src/main.rs b/src/main.rs index e890108..5e60ce0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,9 +3,5 @@ use anyhow::Result; mod cli; fn main() -> Result<()> { - let mut args = cli::parse_command_line()?; - let keys = cli::validate_command_line(&mut args)?; - let _cli = cli::Cli::new(args, keys); - println!("Hello, world!"); - Ok(()) + cli::Cli::new()?.run() }