use crate::command_runner::CommandRunner; use crate::symbols::Symbol; use async_trait::async_trait; use nonzero_ext::nonzero; use std::error::Error; use std::num::NonZeroU32; use std::path::Path; #[derive(Debug)] pub struct Key { file_path: P, command_runner: C, bits: NonZeroU32, } impl Key { pub const fn new(command_runner: C, file_path: P) -> Self { Self { file_path, command_runner, bits: nonzero!(4096u32), // FIXME: Policy } } } #[async_trait(?Send)] impl> Symbol for Key { async fn target_reached(&self) -> Result> { if !self.file_path.as_ref().exists() { return Ok(false); } let stdout = self .command_runner .get_output( "openssl", args![ "rsa", "-in", self.file_path.as_ref(), "-noout", "-check", "-text", ], ) .await?; Ok( stdout.ends_with(b"RSA key ok\n") && stdout.starts_with(format!("RSA Private-Key: ({} bit, 2 primes)\n", self.bits).as_ref()), ) } async fn execute(&self) -> Result<(), Box> { self .command_runner .run_successfully( "openssl", args![ "genrsa", "-out", self.file_path.as_ref(), self.bits.to_string(), ], ) .await } } #[cfg(test)] mod test {}