Reorganize a bit

This commit is contained in:
Alex Page 2023-06-18 02:24:11 -04:00
parent fc74b28d84
commit 9402735559
2 changed files with 69 additions and 63 deletions

View file

@ -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<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 {
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<Self> {
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,7 +124,68 @@ 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<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(

View file

@ -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()
}