read aes container

master
Avril 4 years ago
parent 48f7ed5d9e
commit 5b0c4e7ae0
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -0,0 +1,67 @@
//! AES file container operations
//!
//! Should operate on streams to produce/consume instances of `format::key::aes::AesBody`.
use super::*;
use std::{
path::Path,
};
use tokio::{
io::BufReader,
fs::OpenOptions,
};
/// Read an `AesBody` from a file.
///
/// 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<Path>, passwd: config::op::Password) -> eyre::Result<format::key::aes::AesBody>
{
// password function
let passwd = passwd.into_password()?;
macro_rules! passwdfn {
() => (|salt| passwd.as_ref().map(|string| crypto::password::Password::derive(string, salt)))
}
// read from `path`
use config::op::KeyFormat;
let aesbody = match resolve::find_key_format(&path, false).await
.wrap_err(eyre!("Failed to detect file format for key"))
.with_section(|| format!("{:?}", path.as_ref()).header("Path was"))
.with_suggestion(|| "Are you sure this file is valid?")? {
#[cold] KeyFormat::PEM => unreachable!(),
other => {
let mut file = OpenOptions::new()
.read(true)
.open(path).await
.wrap_err(eyre!("Failed to open file for reading a second time."))
.with_suggestion(|| "Has the file just been/is being modified as we are reading it?")
.with_section(|| format!("{:?}", other).header("File format was successfully detected as"))?;
match other {
KeyFormat::Bin => {
let sh = format::SuperHeader::<format::key::KeyHeader>::read_bytes(&mut file, passwdfn!()).await
.wrap_err(eyre!("Failed to read key super-header"))?;
trace!("Read super {:?}", sh);
let h = format::key::KeyHeader::read_bytes(&mut file, passwdfn!()).await
.wrap_err(eyre!("Failed to read key header"))?;
trace!("Read header {:?}", h);
format::key::aes::AesBody::read_bytes(&mut file, h.body_key()).await
.wrap_err(eyre!("Failed to read key body"))?
},
KeyFormat::Text => {
let mut file = BufReader::new(file);
let sh = format::SuperHeader::<format::key::KeyHeader>::read_text(&mut file, passwdfn!()).await
.wrap_err(eyre!("Failed to read key super-header"))?;
trace!("Read super {:?}", sh);
let h = format::key::KeyHeader::read_text(&mut file, passwdfn!()).await
.wrap_err(eyre!("Failed to read key header"))?;
trace!("Read header {:?}", h);
format::key::aes::AesBody::read_text(&mut file, h.body_key()).await
.wrap_err(eyre!("Failed to read key body"))?
},
#[cold] _ => unreachable!(),
}
},
};
debug!("Read body {:?}", aesbody);
Ok(aesbody)
}

@ -0,0 +1,4 @@
//! File containers reading + writing.
use super::*;
pub mod aes;

@ -123,6 +123,7 @@ impl TryFrom<u8> for KeyHeaderKind
}
/// Header for all keys
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
pub struct KeyHeader
{

@ -17,6 +17,7 @@ use std::{
};
use color_eyre::{
eyre::{
eyre,
self,
WrapErr,
},
@ -99,6 +100,7 @@ mod resolve;
mod args;
mod format;
mod container;
mod work;
pub mod timestamp
@ -191,11 +193,9 @@ async fn work(op: config::Operation) -> Result<(), eyre::Report>
config::Operation::GenerateKey(config::op::GenerateKey::Aes(aes)) => {
let input_aes = match aes.input {
Some((path, passwd)) => {
let passwd = passwd.into_password()?;
// read from `path`
},
_ => None,
_ => (),
};
},
config::Operation::GenerateKey(config::op::GenerateKey::Rsa(rsa)) => {

Loading…
Cancel
Save