2022-11-23 05:59:54 -05:00
|
|
|
use std::path::{Path, PathBuf};
|
2022-11-25 07:07:04 -05:00
|
|
|
use std::process::Command;
|
2022-11-23 05:59:54 -05:00
|
|
|
|
2022-11-25 09:46:33 -05:00
|
|
|
use anyhow::Result;
|
|
|
|
|
|
|
|
use crate::utils;
|
|
|
|
|
2022-11-25 07:07:04 -05:00
|
|
|
pub struct Signer {
|
|
|
|
pub private_key: PathBuf,
|
|
|
|
pub public_key: PathBuf,
|
2022-11-23 05:59:54 -05:00
|
|
|
}
|
|
|
|
|
2022-11-25 07:07:04 -05:00
|
|
|
impl Signer {
|
|
|
|
pub fn new(public_key: &Path, private_key: &Path) -> Self {
|
2022-11-23 05:59:54 -05:00
|
|
|
Self {
|
2022-11-25 07:07:04 -05:00
|
|
|
public_key: public_key.into(),
|
|
|
|
private_key: private_key.into(),
|
2022-11-23 05:59:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-26 09:32:43 -05:00
|
|
|
pub fn sign_and_copy(&self, from: &Path, to: &Path) -> Result<()> {
|
2022-11-23 05:59:54 -05:00
|
|
|
let args = vec![
|
|
|
|
String::from("--key"),
|
2022-11-25 09:46:33 -05:00
|
|
|
utils::path_to_string(&self.private_key),
|
2022-11-23 05:59:54 -05:00
|
|
|
String::from("--cert"),
|
2022-11-25 09:46:33 -05:00
|
|
|
utils::path_to_string(&self.public_key),
|
2022-11-26 09:32:43 -05:00
|
|
|
utils::path_to_string(from),
|
2022-11-24 21:04:44 -05:00
|
|
|
String::from("--output"),
|
2022-11-26 09:32:43 -05:00
|
|
|
utils::path_to_string(to),
|
2022-11-23 05:59:54 -05:00
|
|
|
];
|
|
|
|
|
2022-11-25 07:07:04 -05:00
|
|
|
let status = Command::new("sbsign").args(&args).status()?;
|
2022-11-23 05:59:54 -05:00
|
|
|
|
|
|
|
if !status.success() {
|
2022-11-25 09:15:58 -05:00
|
|
|
return Err(anyhow::anyhow!(
|
|
|
|
"Failed to sign file using sbsign with args `{:?}`",
|
|
|
|
&args
|
|
|
|
)
|
|
|
|
.into());
|
2022-11-23 05:59:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|