start resolve

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

11
Cargo.lock generated

@ -835,6 +835,7 @@ dependencies = [
"once_cell",
"pin-project",
"recolored",
"rpassword",
"rustc_version",
"serde",
"serde_cbor",
@ -927,6 +928,16 @@ version = "0.6.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8"
[[package]]
name = "rpassword"
version = "5.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d755237fc0f99d98641540e66abac8bc46a0652f19148ac9e21de2da06b326c9"
dependencies = [
"libc",
"winapi 0.3.9",
]
[[package]]
name = "rustc-demangle"
version = "0.1.16"

@ -41,6 +41,7 @@ async-trait = "0.1.40"
hex-literal = "0.3.1"
once_cell = "1.4.1"
getrandom = "0.2.0"
rpassword = "5.0.0"
[build-dependencies]
rustc_version = "0.2"

@ -0,0 +1 @@
resolve::find_key_format::check_pem()

@ -1,6 +1,3 @@
use libc::{
c_void,
};
/// Copy slice of bytes only
///
/// # Notes
@ -9,7 +6,7 @@ pub fn copy_slice(dst: &mut [u8], src: &[u8]) -> usize
{
let sz = std::cmp::min(dst.len(),src.len());
unsafe {
libc::memcpy(&mut dst[0] as *mut u8 as *mut c_void, &src[0] as *const u8 as *const c_void, sz);
std::ptr::copy(&src[0] as *const u8, &mut dst[0] as *mut u8, sz);
}
sz
}
@ -22,7 +19,7 @@ pub fn move_slice(dst: &mut [u8], src: &[u8]) -> usize
{
let sz = std::cmp::min(dst.len(),src.len());
unsafe {
libc::memmove(&mut dst[0] as *mut u8 as *mut c_void, &src[0] as *const u8 as *const c_void, sz);
std::ptr::copy(&src[0] as *const u8, &mut dst[0] as *mut u8, sz);
}
sz
}

@ -113,3 +113,27 @@ pub enum KeyKind
RsaPrivate,
Aes,
}
pub use op::Password;
impl Password
{
/// Consume into real password
#[instrument]
pub fn into_password(self) -> eyre::Result<Option<String>>
{
Ok(match self {
Self::No => None,
Self::Yes => Some(read_password()?),
Self::Specific(passwd) => Some(passwd),
})
}
}
/// Read password from stdin
#[instrument(err)]
fn read_password() -> eyre::Result<String>
{
rpassword::read_password()
.wrap_err(eyre!("Failed to read password from stdin"))
}

@ -189,7 +189,14 @@ async fn work(op: config::Operation) -> Result<(), eyre::Report>
match op {
config::Operation::Help => args::usage(),
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)) => {
@ -205,8 +212,8 @@ async fn main() -> Result<(), eyre::Report> {
install_tracing();
color_eyre::install()?;
fuck().await?;
return Ok(());
//fuck().await?;
//return Ok(());
trace!("Parsing args");
let args = args::parse_args().await?;

@ -6,7 +6,84 @@ use std::{
},
error,
fmt,
io,
};
use tokio::{
prelude::*,
fs::{
OpenOptions,
File,
},
io::{
BufReader,
},
};
use config::op::KeyFormat;
/// Find the format (Binary, Text, PEM) of this key file.
///
/// # Note
/// This isn't an infallible test as it only parses parts of headers, if it detects a type it doesn't guarantee that the file *actually* is that type. It is just a hint or best-guess.
#[instrument(err, skip(path), fields(path = ?path.as_ref()))]
pub async fn find_key_format(path: impl AsRef<Path>, can_pem: bool) -> eyre::Result<KeyFormat>
{
async fn check_bin(file: &mut File) -> Result<bool, Error>
{
let mut buffer = [0u8; 4];
file.read_exact(&mut buffer[..]).await?;
Ok(&buffer[..] == format::RAE_HEADER_BIT)
}
async fn check_text(file: &mut File) -> Result<bool, Error>
{
let mut buffer = [0u8; 4];
file.read_exact(&mut buffer[..]).await?;
if &buffer[..] != b"--- " {
return Ok(false);
}
file.read_exact(&mut buffer[..]).await?;
Ok(&buffer[..] == format::RAE_HEADER_BIT)
}
async fn check_pem(file: &mut File) -> Result<bool, Error>
{
todo!("I don't remember the PEM format right now")
}
let mut file = {
let path = path.as_ref();
OpenOptions::new()
.read(true)
.open(path).await
.wrap_err(eyre::eyre!("Failed to open file to check"))
.with_section(|| format!("{:?}", path).header("Path was"))?
};
macro_rules! reset {
() => {
file.seek(std::io::SeekFrom::Start(0)).await?
}
}
if can_pem {
if check_pem(&mut file).await
.with_note(|| "With check for PEM")? {
return Ok(KeyFormat::PEM);
} else {
reset!();
}
}
if check_bin(&mut file).await
.with_note(|| "With check for binary")? {
return Ok(KeyFormat::Bin);
} else {
reset!();
}
if check_text(&mut file).await
.with_note(|| "With check for text")? {
return Ok(KeyFormat::Text);
}
return Err(Error::UnknownFormat)?;
}
pub async fn find_file_mode<P: AsRef<Path>>(path: P) -> Result<config::op::Mode, Error>
{
@ -26,7 +103,9 @@ pub async fn find_key_mode<P: AsRef<Path>>(path: P) -> Result<config::KeyKind, E
#[non_exhaustive]
pub enum Error
{
Unknown
UnknownFormat,
IO(io::Error),
Unknown,
}
impl error::Error for Error{}
@ -39,3 +118,11 @@ impl fmt::Display for Error
}
}
}
impl From<io::Error> for Error
{
#[inline] fn from(from: io::Error) -> Self
{
Self::IO(from)
}
}

@ -1,2 +1,3 @@
//! Generate aes key operations
use super::*;

Loading…
Cancel
Save