2023-10-18 09:28:12 +00:00
|
|
|
use std::{
|
|
|
|
env,
|
|
|
|
io::{stdin, stdout},
|
|
|
|
path::PathBuf,
|
|
|
|
process::ExitCode,
|
|
|
|
str::FromStr,
|
|
|
|
};
|
|
|
|
|
2023-10-19 13:53:59 +00:00
|
|
|
use keyfork_shard::{combine, discover_certs, parse_messages, openpgp::Cert};
|
2023-10-18 09:28:12 +00:00
|
|
|
|
|
|
|
type Result<T, E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
|
|
|
|
|
|
|
|
fn validate(threshold: &str, key_discovery: &str) -> Result<(u8, Vec<Cert>)> {
|
|
|
|
let threshold = u8::from_str(threshold)?;
|
|
|
|
let key_discovery = PathBuf::from(key_discovery);
|
|
|
|
|
|
|
|
// Verify path exists
|
|
|
|
std::fs::metadata(&key_discovery)?;
|
|
|
|
|
|
|
|
// Load certs from path
|
|
|
|
let certs = discover_certs(key_discovery)?;
|
|
|
|
|
|
|
|
Ok((threshold, certs))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run() -> Result<()> {
|
|
|
|
let mut args = env::args();
|
|
|
|
let program_name = args.next().expect("program name");
|
|
|
|
let args = args.collect::<Vec<_>>();
|
|
|
|
let (threshold, cert_list) = match args.as_slice() {
|
|
|
|
[threshold, key_discovery] => validate(threshold, key_discovery)?,
|
2023-10-19 13:53:59 +00:00
|
|
|
_ => panic!("Usage: {program_name} threshold key_discovery"),
|
2023-10-18 09:28:12 +00:00
|
|
|
};
|
|
|
|
|
2023-10-19 13:53:59 +00:00
|
|
|
let mut encrypted_messages = parse_messages(stdin())?;
|
2023-10-18 09:28:12 +00:00
|
|
|
|
|
|
|
let encrypted_metadata = encrypted_messages
|
|
|
|
.pop_front()
|
|
|
|
.expect("any pgp encrypted message");
|
|
|
|
|
|
|
|
combine(
|
|
|
|
threshold,
|
|
|
|
cert_list,
|
|
|
|
encrypted_metadata,
|
|
|
|
encrypted_messages.into(),
|
|
|
|
stdout(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> ExitCode {
|
|
|
|
let result = run();
|
|
|
|
if let Err(e) = result {
|
|
|
|
eprintln!("Error: {e}");
|
|
|
|
return ExitCode::FAILURE;
|
|
|
|
}
|
|
|
|
ExitCode::SUCCESS
|
|
|
|
}
|