From 61ef5b92bbe15f50d1eb344e23cc9795cb666af5 Mon Sep 17 00:00:00 2001 From: user Date: Tue, 5 Dec 2023 14:50:13 -0500 Subject: [PATCH] fix: add deprecated keygen command This allows users to use the old keygen command, while being informed about its deprecation. --- rosenpass/src/cli.rs | 59 ++++++++++++++++++++++++++++++++++++++----- rosenpass/src/main.rs | 3 ++- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/rosenpass/src/cli.rs b/rosenpass/src/cli.rs index dc6b596..86cee92 100644 --- a/rosenpass/src/cli.rs +++ b/rosenpass/src/cli.rs @@ -87,6 +87,15 @@ pub enum Cli { force: bool, }, + /// Deprecated - use gen-keys instead + #[allow(rustdoc::broken_intra_doc_links)] + #[allow(rustdoc::invalid_html_tags)] + Keygen { + // NOTE yes, the legacy keygen argument initially really accepted "privet-key", not "secret-key"! + /// public-key private-key + args: Vec, + }, + /// Validate a configuration Validate { config_files: Vec }, @@ -119,6 +128,40 @@ impl Cli { config::Rosenpass::example_config().store(config_file)?; } + // Deprecated - use gen-keys instead + Keygen { args } => { + log::warn!("The 'keygen' command is deprecated. Please use the 'gen-keys' command instead."); + + let mut public_key: Option = None; + let mut secret_key: Option = None; + + // Manual arg parsing, since clap wants to prefix flags with "--" + let mut args = args.into_iter(); + loop { + match (args.next().as_ref().map(String::as_str), args.next()) { + (Some("private-key"), Some(opt)) | (Some("secret-key"), Some(opt)) => { + secret_key = Some(opt.into()); + } + (Some("public-key"), Some(opt)) => { + public_key = Some(opt.into()); + } + (Some(flag), _) => { + bail!("Unknown option `{}`", flag); + } + (_, _) => break, + }; + } + + if secret_key.is_none() { + bail!("private-key is required"); + } + if public_key.is_none() { + bail!("public-key is required"); + } + + generate_and_save_keypair(secret_key.unwrap(), public_key.unwrap())?; + } + GenKeys { config_file, public_key, @@ -160,12 +203,7 @@ impl Cli { } // generate the keys and store them in files - let mut ssk = crate::protocol::SSk::random(); - let mut spk = crate::protocol::SPk::random(); - StaticKem::keygen(ssk.secret_mut(), spk.secret_mut())?; - - ssk.store_secret(skf)?; - spk.store_secret(pkf)?; + generate_and_save_keypair(skf, pkf)?; } ExchangeConfig { config_file } => { @@ -246,3 +284,12 @@ impl Cli { srv.event_loop() } } + +/// generate secret and public keys, store in files according to the paths passed as arguments +fn generate_and_save_keypair(secret_key: PathBuf, public_key: PathBuf) -> anyhow::Result<()> { + let mut ssk = crate::protocol::SSk::random(); + let mut spk = crate::protocol::SPk::random(); + StaticKem::keygen(ssk.secret_mut(), spk.secret_mut())?; + ssk.store_secret(secret_key)?; + spk.store_secret(public_key) +} diff --git a/rosenpass/src/main.rs b/rosenpass/src/main.rs index c6c9d84..e4db49e 100644 --- a/rosenpass/src/main.rs +++ b/rosenpass/src/main.rs @@ -5,7 +5,8 @@ use std::process::exit; /// Catches errors, prints them through the logger, then exits pub fn main() { - env_logger::init(); + // default to displaying warning and error log messages only + env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn")).init(); let res = attempt!({ rosenpass_sodium::init()?;