diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6b39d31 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +.idea/ \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..b485067 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,265 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitness" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57792b99d555ebf109c83169228076f7d997e2b37ba1a653850ccd703ac7bab0" +dependencies = [ + "sysctl", + "thiserror", + "uname", + "winapi", +] + +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "copy_sign_tool" +version = "0.1.0" +dependencies = [ + "bitness", + "fs_extra", + "log", + "thiserror", + "winreg", +] + +[[package]] +name = "fs_extra" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" + +[[package]] +name = "libc" +version = "0.2.147" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" + +[[package]] +name = "log" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" + +[[package]] +name = "proc-macro2" +version = "1.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78803b62cbf1f46fde80d7c0e803111524b9877184cfe7c3033659490ac7a7da" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "573015e8ab27661678357f27dc26460738fd2b6c86e46f386fde94cb5d913105" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "syn" +version = "2.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45c3457aacde3c65315de5031ec191ce46604304d2446e803d71ade03308d970" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sysctl" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225e483f02d0ad107168dc57381a8a40c3aeea6abe47f37506931f861643cfa8" +dependencies = [ + "bitflags", + "byteorder", + "libc", + "thiserror", + "walkdir", +] + +[[package]] +name = "thiserror" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a35fc5b8971143ca348fa6df4f024d4d55264f3468c71ad1c2f365b0a4d58c42" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "463fe12d7993d3b327787537ce8dd4dfa058de32fc2b195ef3cde03dc4771e8f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "uname" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b72f89f0ca32e4db1c04e2a72f5345d59796d4866a1ee0609084569f73683dc8" +dependencies = [ + "libc", +] + +[[package]] +name = "unicode-ident" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" + +[[package]] +name = "walkdir" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36df944cda56c7d8d8b7496af378e6b16de9284591917d307c9b4d313c44e698" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05d4b17490f70499f20b9e791dcf6a299785ce8af4d709018206dc5b4953e95f" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" + +[[package]] +name = "winreg" +version = "0.50.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" +dependencies = [ + "cfg-if", + "windows-sys", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8626edb --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "copy_sign_tool" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +bitness = "0.4.0" +log = "0.4.19" +thiserror = "1.0.40" +winreg = "0.50.0" +fs_extra="1.3.0" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..8bafd79 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,97 @@ +use bitness::{self, Bitness}; +use std::{env, path::{Path, PathBuf}}; + +use winreg::{ + enums::{HKEY_LOCAL_MACHINE, KEY_READ, KEY_WOW64_32KEY}, + RegKey, +}; +use fs_extra::file::copy; +use fs_extra::file::CopyOptions; + +mod utils; + +fn main() { + let path = locate_signtool().unwrap(); + + println!("signtool path ={:?}", path.as_path()); + let options = CopyOptions { + overwrite: true, + buffer_size: 64000, + skip_exist: false, + }; + let exe_file_path = env::current_exe().expect("unable to get path"); + let folder_location = exe_file_path.parent().expect("No parent folder"); + let source_file = folder_location.to_str().unwrap().to_owned() + "\\signtool.exe"; + + + println!("{:?}", source_file); + let result = copy(source_file, path.as_path(), &options); + println!("result = {:?}", result); +} + +// sign code forked from https://github.com/forbjok/rust-codesign +fn locate_signtool() -> utils::Result { + const INSTALLED_ROOTS_REGKEY_PATH: &str = r"SOFTWARE\Microsoft\Windows Kits\Installed Roots"; + const KITS_ROOT_REGVALUE_NAME: &str = r"KitsRoot10"; + + let installed_roots_key_path = Path::new(INSTALLED_ROOTS_REGKEY_PATH); + + // Open 32-bit HKLM "Installed Roots" key + let installed_roots_key = RegKey::predef(HKEY_LOCAL_MACHINE) + .open_subkey_with_flags(installed_roots_key_path, KEY_READ | KEY_WOW64_32KEY) + .map_err(|_| utils::Error::OpenRegistry(INSTALLED_ROOTS_REGKEY_PATH.to_string()))?; + + // Get the Windows SDK root path + let kits_root_10_path: String = installed_roots_key + .get_value(KITS_ROOT_REGVALUE_NAME) + .map_err(|_| utils::Error::GetRegistryValue(KITS_ROOT_REGVALUE_NAME.to_string()))?; + + // Construct Windows SDK bin path + let kits_root_10_bin_path = Path::new(&kits_root_10_path).join("bin"); + + let mut installed_kits: Vec = installed_roots_key + .enum_keys() + /* Report and ignore errors, pass on values. */ + .filter_map(|res| match res { + Ok(v) => Some(v), + Err(_) => None, + }) + .collect(); + + // Sort installed kits + installed_kits.sort(); + + /* Iterate through installed kit version keys in reverse (from newest to oldest), + adding their bin paths to the list. + Windows SDK 10 v10.0.15063.468 and later will have their signtools located there. */ + let mut kit_bin_paths: Vec = installed_kits + .iter() + .rev() + .map(|kit| kits_root_10_bin_path.join(kit)) + .collect(); + + /* Add kits root bin path. + For Windows SDK 10 versions earlier than v10.0.15063.468, signtool will be located there. */ + kit_bin_paths.push(kits_root_10_bin_path); + + // Choose which version of SignTool to use based on OS bitness + let arch_dir = match bitness::os_bitness().expect("failed to get os bitness") { + Bitness::X86_32 => "x86", + Bitness::X86_64 => "x64", + _ => return Err(utils::Error::UnsupportedBitness), + }; + + /* Iterate through all bin paths, checking for existence of a SignTool executable. */ + for kit_bin_path in &kit_bin_paths { + /* Construct SignTool path. */ + let signtool_path = kit_bin_path.join(arch_dir).join("signtool.exe"); + + /* Check if SignTool exists at this location. */ + if signtool_path.exists() { + // SignTool found. Return it. + return Ok(signtool_path); + } + } + + Err(utils::Error::SignToolNotFound) +} \ No newline at end of file diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..d448fd6 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,27 @@ +// Copyright 2016-2019 Cargo-Bundle developers +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +use thiserror::Error as DeriveError; + +/// Errors returned by the bundler. +#[derive(Debug, DeriveError)] +#[non_exhaustive] +pub enum Error { + + #[error("SignTool not found")] + SignToolNotFound, + /// Failed to open Windows registry. + #[error("failed to open registry {0}")] + OpenRegistry(String), + /// Failed to get registry value. + #[error("failed to get {0} value on registry")] + GetRegistryValue(String), + /// Unsupported OS bitness. + #[error("unsupported OS bitness")] + UnsupportedBitness +} + +/// Convenient type alias of Result type. +pub type Result = std::result::Result; \ No newline at end of file