|
@@ -3,7 +3,7 @@
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
-use crate::bundle::common::CommandExt;
|
|
|
+use crate::{bundle::common::CommandExt, Settings};
|
|
|
use bitness::{self, Bitness};
|
|
|
use log::{debug, info};
|
|
|
use std::{
|
|
@@ -90,18 +90,11 @@ fn locate_signtool() -> crate::Result<PathBuf> {
|
|
|
Err(crate::Error::SignToolNotFound)
|
|
|
}
|
|
|
|
|
|
-pub fn sign<P: AsRef<Path>>(path: P, params: &SignParams) -> crate::Result<()> {
|
|
|
- // Convert path to string reference, as we need to pass it as a command-line parameter to signtool
|
|
|
- let path_str = path.as_ref().to_str().unwrap();
|
|
|
-
|
|
|
- info!(action = "Signing"; "{} with identity \"{}\"", path_str, params.certificate_thumbprint);
|
|
|
-
|
|
|
+pub fn sign_command(path: &str, params: &SignParams) -> crate::Result<(Command, PathBuf)> {
|
|
|
// Construct SignTool command
|
|
|
let signtool = locate_signtool()?;
|
|
|
|
|
|
- debug!("Running signtool {:?}", signtool);
|
|
|
-
|
|
|
- let mut cmd = Command::new(signtool);
|
|
|
+ let mut cmd = Command::new(&signtool);
|
|
|
cmd.arg("sign");
|
|
|
cmd.args(["/fd", ¶ms.digest_algorithm]);
|
|
|
cmd.args(["/sha1", ¶ms.certificate_thumbprint]);
|
|
@@ -116,7 +109,18 @@ pub fn sign<P: AsRef<Path>>(path: P, params: &SignParams) -> crate::Result<()> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- cmd.arg(path_str);
|
|
|
+ cmd.arg(path);
|
|
|
+
|
|
|
+ Ok((cmd, signtool))
|
|
|
+}
|
|
|
+
|
|
|
+pub fn sign<P: AsRef<Path>>(path: P, params: &SignParams) -> crate::Result<()> {
|
|
|
+ let path_str = path.as_ref().to_str().unwrap();
|
|
|
+
|
|
|
+ info!(action = "Signing"; "{} with identity \"{}\"", path_str, params.certificate_thumbprint);
|
|
|
+
|
|
|
+ let (mut cmd, signtool) = sign_command(path_str, params)?;
|
|
|
+ debug!("Running signtool {:?}", signtool);
|
|
|
|
|
|
// Execute SignTool command
|
|
|
let output = cmd.output_ok()?;
|
|
@@ -126,3 +130,39 @@ pub fn sign<P: AsRef<Path>>(path: P, params: &SignParams) -> crate::Result<()> {
|
|
|
|
|
|
Ok(())
|
|
|
}
|
|
|
+
|
|
|
+impl Settings {
|
|
|
+ pub(crate) fn can_sign(&self) -> bool {
|
|
|
+ self.windows().certificate_thumbprint.is_some()
|
|
|
+ }
|
|
|
+ pub(crate) fn sign_params(&self) -> SignParams {
|
|
|
+ SignParams {
|
|
|
+ product_name: self.product_name().into(),
|
|
|
+ digest_algorithm: self
|
|
|
+ .windows()
|
|
|
+ .digest_algorithm
|
|
|
+ .as_ref()
|
|
|
+ .map(|algorithm| algorithm.to_string())
|
|
|
+ .unwrap_or_else(|| "sha256".to_string()),
|
|
|
+ certificate_thumbprint: self
|
|
|
+ .windows()
|
|
|
+ .certificate_thumbprint
|
|
|
+ .clone()
|
|
|
+ .unwrap_or_default(),
|
|
|
+ timestamp_url: self
|
|
|
+ .windows()
|
|
|
+ .timestamp_url
|
|
|
+ .as_ref()
|
|
|
+ .map(|url| url.to_string()),
|
|
|
+ tsp: self.windows().tsp,
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+pub fn try_sign(file_path: &std::path::PathBuf, settings: &Settings) -> crate::Result<()> {
|
|
|
+ if settings.can_sign() {
|
|
|
+ info!(action = "Signing"; "{}", tauri_utils::display_path(file_path));
|
|
|
+ sign(file_path, &settings.sign_params())?;
|
|
|
+ }
|
|
|
+ Ok(())
|
|
|
+}
|