From 06409279f4ddd343eea1f0222bfc8deb4b46fb7d Mon Sep 17 00:00:00 2001 From: Paolo Asperti Date: Thu, 14 Jul 2022 15:59:39 +0200 Subject: [PATCH 1/3] rustdesk-utils --- .github/workflows/build.yaml | 3 +- Cargo.toml | 4 ++ src/utils.rs | 106 +++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 src/utils.rs diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 632310d..b3e82cf 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -66,6 +66,7 @@ jobs: path: | target/${{ matrix.job.target }}/release/hbbr target/${{ matrix.job.target }}/release/hbbs + target/${{ matrix.job.target }}/release/rustdesk-utils if-no-files-found: error # github (draft) release with all binaries @@ -102,7 +103,7 @@ jobs: path: i386 - name: Rename files - run: for arch in amd64 arm64v8 armv7 i386 ; do for b in hbbr hbbs ; do mv -v ${arch}/${b} ${arch}/${b}-${arch} ; done ; done + run: for arch in amd64 arm64v8 armv7 i386 ; do for b in hbbr hbbs rustdesk-utils ; do mv -v ${arch}/${b} ${arch}/${b}-${arch} ; done ; done - name: Create Release uses: softprops/action-gh-release@v1 diff --git a/Cargo.toml b/Cargo.toml index 5ace1a4..d652980 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,10 @@ default-run = "hbbs" name = "hbbr" path = "src/hbbr.rs" +[[bin]] +name = "rustdesk-utils" +path = "src/utils.rs" + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..8b32b4d --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,106 @@ +use sodiumoxide::crypto::sign; +use std::str; +use std::env; +use std::process; + +fn print_help() { + println!("Usage:"); + println!(" rustdesk-util [command]\n"); + println!("Available Commands:"); + println!(" genkeypair Generate a new keypair"); + println!(" validatekeypair [public key] [secret key] Validate an existing keypair"); + process::exit(0x0001); +} + +fn error_then_help(msg: &str) { + println!("ERROR: {}\n", msg); + print_help(); +} + +fn gen_keypair() { + let (pk, sk) = sign::gen_keypair(); + let public_key = base64::encode(pk); + let secret_key = base64::encode(sk); + println!("Public Key: {public_key}"); + println!("Secret Key: {secret_key}"); +} + +fn validate_keypair(pk: &str, sk: &str) { + let sk1 = base64::decode(&sk); + match sk1 { + Ok(_) => {}, + Err(_) => { + println!("Invalid secret key"); + process::exit(0x0001); + }, + } + let sk1 = sk1.unwrap(); + + let secret_key = sign::SecretKey::from_slice(sk1.as_slice()); + match secret_key { + Some(_) => {}, + None => { + println!("Invalid Secret key"); + process::exit(0x0001); + }, + } + let secret_key = secret_key.unwrap(); + + let pk1 = base64::decode(&pk); + match pk1 { + Ok(_) => {}, + Err(_) => { + println!("Invalid public key"); + process::exit(0x0001); + }, + } + let pk1 = pk1.unwrap(); + + let public_key = sign::PublicKey::from_slice(pk1.as_slice()); + match public_key { + Some(_) => {}, + None => { + println!("Invalid Public key"); + process::exit(0x0001); + }, + } + let public_key = public_key.unwrap(); + + let random_data_to_test = b"This is meh."; + let signed_data = sign::sign(random_data_to_test, &secret_key); + let verified_data = sign::verify(&signed_data, &public_key); + match verified_data { + Ok(_) => {}, + Err(_) => { + println!("Key pair is INVALID"); + process::exit(0x0001); + }, + } + let verified_data = verified_data.unwrap(); + + if random_data_to_test == &verified_data[..] { + println!("Key pair is VALID"); + } else { + println!("Key pair is INVALID"); + process::exit(0x0001); + } +} + +fn main() { + let args: Vec<_> = env::args().collect(); + if args.len() <= 1 { + print_help(); + } + + let command = args[1].to_lowercase(); + match command.as_str() { + "genkeypair" => gen_keypair(), + "validatekeypair" => { + if args.len() <= 3 { + error_then_help("You must supply both the public and the secret key"); + } + validate_keypair(args[2].as_str(),args[3].as_str()); + }, + _=>print_help(), + } +} \ No newline at end of file From 913de8515e26dda0e340358ce5017e822ecf016b Mon Sep 17 00:00:00 2001 From: Paolo Asperti Date: Fri, 15 Jul 2022 21:39:19 +0200 Subject: [PATCH 2/3] just a pass of 'cargo fmt' --- src/utils.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 8b32b4d..30ad081 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,7 +1,7 @@ use sodiumoxide::crypto::sign; -use std::str; use std::env; use std::process; +use std::str; fn print_help() { println!("Usage:"); @@ -28,41 +28,41 @@ fn gen_keypair() { fn validate_keypair(pk: &str, sk: &str) { let sk1 = base64::decode(&sk); match sk1 { - Ok(_) => {}, + Ok(_) => {} Err(_) => { println!("Invalid secret key"); process::exit(0x0001); - }, + } } let sk1 = sk1.unwrap(); let secret_key = sign::SecretKey::from_slice(sk1.as_slice()); match secret_key { - Some(_) => {}, + Some(_) => {} None => { println!("Invalid Secret key"); process::exit(0x0001); - }, + } } let secret_key = secret_key.unwrap(); let pk1 = base64::decode(&pk); match pk1 { - Ok(_) => {}, + Ok(_) => {} Err(_) => { println!("Invalid public key"); process::exit(0x0001); - }, + } } let pk1 = pk1.unwrap(); let public_key = sign::PublicKey::from_slice(pk1.as_slice()); match public_key { - Some(_) => {}, + Some(_) => {} None => { println!("Invalid Public key"); process::exit(0x0001); - }, + } } let public_key = public_key.unwrap(); @@ -70,11 +70,11 @@ fn validate_keypair(pk: &str, sk: &str) { let signed_data = sign::sign(random_data_to_test, &secret_key); let verified_data = sign::verify(&signed_data, &public_key); match verified_data { - Ok(_) => {}, + Ok(_) => {} Err(_) => { println!("Key pair is INVALID"); process::exit(0x0001); - }, + } } let verified_data = verified_data.unwrap(); @@ -99,8 +99,8 @@ fn main() { if args.len() <= 3 { error_then_help("You must supply both the public and the secret key"); } - validate_keypair(args[2].as_str(),args[3].as_str()); - }, - _=>print_help(), + validate_keypair(args[2].as_str(), args[3].as_str()); + } + _ => print_help(), } -} \ No newline at end of file +} From 26549d7e7ea9b82d9bcaf68cc30762fd76075fb9 Mon Sep 17 00:00:00 2001 From: Paolo Asperti Date: Thu, 21 Jul 2022 11:07:16 +0200 Subject: [PATCH 3/3] better error management --- src/utils.rs | 71 +++++++++++++++++++++------------------------------- 1 file changed, 29 insertions(+), 42 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 30ad081..7eb7a58 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,14 +1,17 @@ +use hbb_common::{bail, ResultType}; use sodiumoxide::crypto::sign; use std::env; use std::process; use std::str; fn print_help() { - println!("Usage:"); - println!(" rustdesk-util [command]\n"); - println!("Available Commands:"); - println!(" genkeypair Generate a new keypair"); - println!(" validatekeypair [public key] [secret key] Validate an existing keypair"); + println!( + "Usage: + rustdesk-util [command]\n +Available Commands: + genkeypair Generate a new keypair + validatekeypair [public key] [secret key] Validate an existing keypair" + ); process::exit(0x0001); } @@ -25,65 +28,44 @@ fn gen_keypair() { println!("Secret Key: {secret_key}"); } -fn validate_keypair(pk: &str, sk: &str) { +fn validate_keypair(pk: &str, sk: &str) -> ResultType<()> { let sk1 = base64::decode(&sk); - match sk1 { - Ok(_) => {} - Err(_) => { - println!("Invalid secret key"); - process::exit(0x0001); - } + if sk1.is_err() { + bail!("Invalid secret key"); } let sk1 = sk1.unwrap(); let secret_key = sign::SecretKey::from_slice(sk1.as_slice()); - match secret_key { - Some(_) => {} - None => { - println!("Invalid Secret key"); - process::exit(0x0001); - } + if secret_key.is_none() { + bail!("Invalid Secret key"); } let secret_key = secret_key.unwrap(); let pk1 = base64::decode(&pk); - match pk1 { - Ok(_) => {} - Err(_) => { - println!("Invalid public key"); - process::exit(0x0001); - } + if pk1.is_err() { + bail!("Invalid public key"); } let pk1 = pk1.unwrap(); let public_key = sign::PublicKey::from_slice(pk1.as_slice()); - match public_key { - Some(_) => {} - None => { - println!("Invalid Public key"); - process::exit(0x0001); - } + if public_key.is_none() { + bail!("Invalid Public key"); } let public_key = public_key.unwrap(); let random_data_to_test = b"This is meh."; let signed_data = sign::sign(random_data_to_test, &secret_key); let verified_data = sign::verify(&signed_data, &public_key); - match verified_data { - Ok(_) => {} - Err(_) => { - println!("Key pair is INVALID"); - process::exit(0x0001); - } + if verified_data.is_err() { + bail!("Key pair is INVALID"); } let verified_data = verified_data.unwrap(); - if random_data_to_test == &verified_data[..] { - println!("Key pair is VALID"); - } else { - println!("Key pair is INVALID"); - process::exit(0x0001); + if random_data_to_test != &verified_data[..] { + bail!("Key pair is INVALID"); } + + Ok(()) } fn main() { @@ -99,7 +81,12 @@ fn main() { if args.len() <= 3 { error_then_help("You must supply both the public and the secret key"); } - validate_keypair(args[2].as_str(), args[3].as_str()); + let res = validate_keypair(args[2].as_str(), args[3].as_str()); + if let Err(e) = res { + println!("{}", e); + process::exit(0x0001); + } + println!("Key pair is VALID"); } _ => print_help(), }