From 807fab34a291a819308d29ece20861bb54fb02fd Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 30 Oct 2020 12:21:35 +0000 Subject: [PATCH] container read AES key --- src/config.rs | 8 ++++---- src/container/aes.rs | 4 ++-- src/main.rs | 9 +++++++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/config.rs b/src/config.rs index ed5df3c..e257917 100644 --- a/src/config.rs +++ b/src/config.rs @@ -120,11 +120,11 @@ impl Password { /// Consume into real password #[instrument] - pub fn into_password(self) -> eyre::Result> + pub fn into_password(self, prompt: impl AsRef) -> eyre::Result> { Ok(match self { Self::No => None, - Self::Yes => Some(read_password()?), + Self::Yes => Some(read_password(prompt.as_ref())?), Self::Specific(passwd) => Some(passwd), }) } @@ -132,8 +132,8 @@ impl Password /// Read password from stdin #[instrument(err)] -fn read_password() -> eyre::Result +fn read_password(prompt: &str) -> eyre::Result { - rpassword::read_password() + rpassword::prompt_password_stderr(prompt) .wrap_err(eyre!("Failed to read password from stdin")) } diff --git a/src/container/aes.rs b/src/container/aes.rs index aad1b82..bc0bfc2 100644 --- a/src/container/aes.rs +++ b/src/container/aes.rs @@ -14,10 +14,10 @@ use tokio::{ /// /// Detect the container type if possible and then decode the AES key. Returning it as `format::key::aes::AesBody`. #[instrument(skip(path), err, fields(path = ?path.as_ref()))] -pub async fn read_aes_container(path: impl AsRef, passwd: config::op::Password) -> eyre::Result +pub async fn read_aes_container(path: impl AsRef, passwd: (config::op::Password, Option<&str>)) -> eyre::Result { // password function - let passwd = passwd.into_password()?; + let passwd = passwd.0.into_password(passwd.1.unwrap_or("Enter password for input key: "))?; macro_rules! passwdfn { () => (|salt| passwd.as_ref().map(|string| crypto::password::Password::derive(string, salt))) } diff --git a/src/main.rs b/src/main.rs index 25f4745..6c788a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -191,12 +191,17 @@ async fn work(op: config::Operation) -> Result<(), eyre::Report> match op { config::Operation::Help => args::usage(), config::Operation::GenerateKey(config::op::GenerateKey::Aes(aes)) => { + // read input aes key if provided let input_aes = match aes.input { Some((path, passwd)) => { - + Some(container::aes::read_aes_container(&path, (passwd, None)).await + .wrap_err(eyre!("Failed to read input AES container from file")) + .with_section(|| format!("{:?}", path).header("Path was"))?) }, - _ => (), + _ => None, }; + //TODO: Generate new key + //TODO: Save new key to file, in provided format, with password if needed }, config::Operation::GenerateKey(config::op::GenerateKey::Rsa(rsa)) => {