Add validate command line option

This commit is contained in:
Alex Page 2023-06-22 02:59:19 -04:00
parent 78b45cf929
commit e001afdaed
3 changed files with 83 additions and 15 deletions

View file

@ -9,18 +9,22 @@ use openssl::{
};
use serde_json::{from_reader, from_str};
use crate::{bink1998, bink2002, confid, crypto::initialize_elliptic_curve};
use crate::{
bink1998, bink2002, confid, crypto::initialize_elliptic_curve, key::P_KEY_CHARSET, PK_LENGTH,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mode {
Bink1998,
Bink2002,
Bink1998Generate,
Bink2002Generate,
ConfirmationId,
Bink1998Validate,
Bink2002Validate,
}
impl Default for Mode {
fn default() -> Self {
Self::Bink1998
Self::Bink1998Generate
}
}
@ -55,6 +59,10 @@ pub struct Options {
#[arg(short = 'c', long = "channel", default_value = "640")]
channel_id: u32,
/// Product key to validate signature
#[arg(short = 'V', long = "validate")]
key_to_check: Option<String>,
#[clap(skip)]
application_mode: Mode,
}
@ -165,8 +173,16 @@ impl Cli {
let bink_id = u32::from_str_radix(&options.binkid, 16)?;
if options.key_to_check.is_some() {
options.application_mode = Mode::Bink1998Validate;
}
if bink_id >= 0x40 {
options.application_mode = Mode::Bink2002;
if options.key_to_check.is_some() {
options.application_mode = Mode::Bink2002Validate;
} else {
options.application_mode = Mode::Bink2002Generate;
}
}
if options.channel_id > 999 {
@ -187,13 +203,15 @@ impl Cli {
pub fn run(&mut self) -> Result<()> {
match self.options.application_mode {
Mode::Bink1998 => self.bink1998(),
Mode::Bink2002 => self.bink2002(),
Mode::Bink1998Generate => self.bink1998_generate(),
Mode::Bink2002Generate => self.bink2002_generate(),
Mode::ConfirmationId => self.confirmation_id(),
Mode::Bink1998Validate => self.bink1998_validate(),
Mode::Bink2002Validate => self.bink2002_validate(),
}
}
fn bink1998(&mut self) -> Result<()> {
fn bink1998_generate(&mut self) -> Result<()> {
let mut n_raw = self.options.channel_id * 1_000_000; // <- change
let mut bn_rand = BigNum::new()?;
@ -230,7 +248,7 @@ impl Cli {
Ok(())
}
fn bink2002(&mut self) -> Result<()> {
fn bink2002_generate(&mut self) -> Result<()> {
let p_channel_id = self.options.channel_id;
if self.options.verbose {
@ -267,6 +285,34 @@ impl Cli {
Ok(())
}
fn bink1998_validate(&mut self) -> Result<()> {
let Ok(key) = Self::strip_key(self.options.key_to_check.as_ref().unwrap()) else {
return Err(anyhow!("Product key is in an incorrect format!"));
};
Self::print_key(&key);
if !bink1998::verify(&self.e_curve, &self.gen_point, &self.pub_point, &key)? {
return Err(anyhow!("Product key is invalid! Wrong BINK ID?"));
}
println!("Key validated successfully!");
Ok(())
}
fn bink2002_validate(&mut self) -> Result<()> {
let Ok(key) = Self::strip_key(self.options.key_to_check.as_ref().unwrap()) else {
return Err(anyhow!("Product key is in an incorrect format!"));
};
Self::print_key(&key);
if !bink2002::verify(&self.e_curve, &self.gen_point, &self.pub_point, &key)? {
return Err(anyhow!("Product key is invalid! Wrong BINK ID?"));
}
println!("Key validated successfully!");
Ok(())
}
fn confirmation_id(&mut self) -> Result<()> {
if let Some(instid) = &self.options.instid {
let confirmation_id = confid::generate(instid)?;
@ -276,7 +322,7 @@ impl Cli {
}
fn print_key(pk: &str) {
assert_eq!(pk.len(), 25);
assert!(pk.len() >= PK_LENGTH);
println!(
"{}",
pk.chars()
@ -290,4 +336,23 @@ impl Cli {
})
);
}
fn strip_key(in_key: &str) -> Result<String> {
let out_key: String = in_key
.chars()
.filter_map(|c| {
let c = c.to_ascii_uppercase();
if P_KEY_CHARSET.into_iter().any(|x| x == c) {
Some(c)
} else {
None
}
})
.collect();
if out_key.len() == PK_LENGTH {
Ok(out_key)
} else {
Err(anyhow!("Invalid key length"))
}
}
}