diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 764a4a70a..893eb278f 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -491,6 +491,12 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "base16ct" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c7f02d4ea65f2c1853089ffd8d2787bdbc63de2f0d29dedbcf8ccdfa0ccd4cf" + [[package]] name = "base58-monero" version = "2.0.0" @@ -1102,6 +1108,12 @@ dependencies = [ "toml 0.8.19", ] +[[package]] +name = "const-oid" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" + [[package]] name = "constant_time_eq" version = "0.3.1" @@ -1295,6 +1307,18 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" +[[package]] +name = "crypto-bigint" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" +dependencies = [ + "generic-array", + "rand_core 0.6.4", + "subtle", + "zeroize", +] + [[package]] name = "crypto-common" version = "0.1.6" @@ -1447,6 +1471,16 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da692b8d1080ea3045efaab14434d40468c3d8657e42abddfffca87b428f4c1b" +[[package]] +name = "der" +version = "0.7.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f55bf8e7b65898637379c1b74eb1551107c8294ed26d855ceb9fd1a09cfc9bc0" +dependencies = [ + "const-oid", + "zeroize", +] + [[package]] name = "deranged" version = "0.3.11" @@ -1733,12 +1767,51 @@ version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125" +[[package]] +name = "ecdsa" +version = "0.16.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee27f32b5c5292967d2d4a9d7f1e0b0aed2c15daded5a60300e4abb9d8020bca" +dependencies = [ + "der", + "elliptic-curve", + "signature", +] + +[[package]] +name = "ed25519" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53" +dependencies = [ + "pkcs8", + "signature", +] + [[package]] name = "either" version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +[[package]] +name = "elliptic-curve" +version = "0.13.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6043086bf7973472e0c7dff2142ea0b680d30e18d9cc40f267efbf222bd47" +dependencies = [ + "base16ct", + "crypto-bigint", + "digest", + "ff", + "generic-array", + "group", + "rand_core 0.6.4", + "sec1", + "subtle", + "zeroize", +] + [[package]] name = "embed-resource" version = "2.5.1" @@ -2277,6 +2350,7 @@ checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" dependencies = [ "typenum", "version_check", + "zeroize", ] [[package]] @@ -4595,6 +4669,27 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "p256" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9863ad85fa8f4460f9c48cb909d38a0d689dba1f6f6988a5e3e0d31071bcd4b" +dependencies = [ + "ecdsa", + "elliptic-curve", +] + +[[package]] +name = "p384" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70786f51bcc69f6a4c0360e063a4cac5419ef7c5cd5b3c99ad70f3be5ba79209" +dependencies = [ + "ecdsa", + "elliptic-curve", + "primeorder", +] + [[package]] name = "pango" version = "0.18.3" @@ -4945,6 +5040,16 @@ dependencies = [ "futures-io", ] +[[package]] +name = "pkcs8" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" +dependencies = [ + "der", + "spki", +] + [[package]] name = "pkg-config" version = "0.3.31" @@ -5068,6 +5173,15 @@ dependencies = [ "syn 2.0.90", ] +[[package]] +name = "primeorder" +version = "0.13.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "353e1ca18966c16d9deb1c69278edbc5f194139612772bd9537af60ac231e1e6" +dependencies = [ + "elliptic-curve", +] + [[package]] name = "primitive-types" version = "0.12.2" @@ -5643,6 +5757,25 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "ring-compat" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccce7bae150b815f0811db41b8312fcb74bffa4cab9cee5429ee00f356dd5bd4" +dependencies = [ + "aead", + "digest", + "ecdsa", + "ed25519", + "generic-array", + "p256", + "p384", + "pkcs8", + "rand_core 0.6.4", + "ring", + "signature", +] + [[package]] name = "roff" version = "0.2.2" @@ -5847,6 +5980,19 @@ dependencies = [ "syn 2.0.90", ] +[[package]] +name = "sec1" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" +dependencies = [ + "base16ct", + "der", + "generic-array", + "subtle", + "zeroize", +] + [[package]] name = "security-framework" version = "2.11.1" @@ -6269,6 +6415,15 @@ dependencies = [ "libc", ] +[[package]] +name = "signature" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" +dependencies = [ + "rand_core 0.6.4", +] + [[package]] name = "simd-adler32" version = "0.3.7" @@ -6421,6 +6576,16 @@ version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +[[package]] +name = "spki" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" +dependencies = [ + "base64ct", + "der", +] + [[package]] name = "stable_deref_trait" version = "1.2.0" @@ -6753,6 +6918,7 @@ dependencies = [ "base64 0.22.1", "blake2", "chrono", + "der", "device_query", "dirs 5.0.1", "dunce", @@ -6780,6 +6946,7 @@ dependencies = [ "regex", "reqwest", "ring", + "ring-compat", "sanitize-filename", "semver", "serde", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 10164a54e..3ab4b0364 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -92,6 +92,8 @@ hex = "0.4.3" # temporary fix for openssl openssl = { version = "0.10", features = ["vendored"] } +ring-compat = "0.8.0" +der = "0.7.9" [target.'cfg(windows)'.dependencies] winreg = "0.52.0" diff --git a/src-tauri/src/app_in_memory_config.rs b/src-tauri/src/app_in_memory_config.rs index 2d4048892..9a1072d83 100644 --- a/src-tauri/src/app_in_memory_config.rs +++ b/src-tauri/src/app_in_memory_config.rs @@ -21,8 +21,9 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use anyhow::anyhow; -use base64::prelude::*; -use ring::signature::Ed25519KeyPair; +use der::{self, asn1::BitString, oid::ObjectIdentifier, Encode}; +use ring::signature::{Ed25519KeyPair, KeyPair}; +use ring_compat::pkcs8::{spki::AlgorithmIdentifier, SubjectPublicKeyInfo}; use serde::{Deserialize, Serialize}; #[cfg(feature = "airdrop-env")] @@ -78,20 +79,37 @@ impl Default for AppInMemoryConfig { const AIRDROP_WEBSOCKET_CRYPTO_KEY: &str = match option_env!("AIRDROP_WEBSOCKET_CRYPTO_KEY") { Some(value) => value, - None => "4d43344341514177425159444b325677424349454943443235436e576b454f5a796833346a5479566c36484f4e396d4e31594248354374536f2f6439414f3145", + None => "302e020100300506032b65700422042030f9f3e8ba7cac3d648b059f2fd5c5a6394caab46bdbb002e6989c883137b799", }; pub fn get_websocket_key() -> anyhow::Result { let decoded_str = hex::decode(AIRDROP_WEBSOCKET_CRYPTO_KEY)?; - let utf8_str = String::from_utf8(decoded_str)?; - let key_bytes = BASE64_STANDARD.decode(utf8_str)?; - - match Ed25519KeyPair::from_pkcs8_maybe_unchecked(&key_bytes) { + match Ed25519KeyPair::from_pkcs8_maybe_unchecked(&decoded_str) { Ok(key) => Ok(key), Err(e) => Err(anyhow!(e.to_string())), } } +pub fn get_der_encode_pub_key(key_pair: &Ed25519KeyPair) -> anyhow::Result { + let pub_key_bytes = key_pair.public_key().as_ref(); + + let algorithm_identifier: AlgorithmIdentifier<()> = AlgorithmIdentifier { + oid: ObjectIdentifier::new("1.3.101.112").map_err(|e| anyhow::anyhow!(e.to_string()))?, + parameters: None, // No parameters for Ed25519 + }; + + let subject_public_key = + BitString::from_bytes(pub_key_bytes).map_err(|e| anyhow::anyhow!(e.to_string()))?; + + let spki = SubjectPublicKeyInfo { + algorithm: algorithm_identifier, + subject_public_key, + }; + + let der_encoded = spki.to_der().map_err(|e| anyhow::anyhow!(e.to_string()))?; + Ok(hex::encode(der_encoded)) +} + impl AppInMemoryConfig { pub fn init() -> Self { #[cfg(feature = "airdrop-env")] diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 8382f9c1d..941812334 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -21,7 +21,9 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use crate::app_config::{AppConfig, GpuThreads}; -use crate::app_in_memory_config::{get_websocket_key, AirdropInMemoryConfig}; +use crate::app_in_memory_config::{ + get_der_encode_pub_key, get_websocket_key, AirdropInMemoryConfig, +}; use crate::auto_launcher::AutoLauncher; use crate::binaries::{Binaries, BinaryResolver}; use crate::credential_manager::{CredentialError, CredentialManager}; @@ -39,6 +41,7 @@ use crate::utils::shutdown_utils::stop_all_processes; use crate::wallet_adapter::{TransactionInfo, WalletBalance}; use crate::wallet_manager::WalletManagerError; use crate::{setup_inner, UniverseAppState, APPLICATION_FOLDER_ID}; + use base64::prelude::*; use keyring::Entry; use log::{debug, error, info, warn}; @@ -135,6 +138,7 @@ pub struct CpuMinerConnectionStatus { #[serde(rename_all = "camelCase")] pub struct SignWsDataResponse { signature: String, + pub_key: String, } #[tauri::command] @@ -1056,7 +1060,7 @@ pub async fn set_cpu_mining_enabled<'r>( } #[tauri::command] -pub async fn sign_ws_data(data_base64: String) -> Result { +pub async fn sign_ws_data(data: String) -> Result { let key: ring::signature::Ed25519KeyPair = get_websocket_key().map_err(|e| { warn!(target: LOG_TARGET, "error ws key handling:{:?}", @@ -1064,17 +1068,19 @@ pub async fn sign_ws_data(data_base64: String) -> Result { @@ -48,11 +49,15 @@ export const useWebsocket = () => { userId, }; try { - const transformedPayload = btoa(JSON.stringify(payload)); + const transformedPayload = `${payload.version},${payload.network},${payload.appId},${payload.userId},${payload.isMining},${payload.blockHeight}`; const signatureData = (await invoke('sign_ws_data', { - dataBase64: transformedPayload, + data: transformedPayload, })) as SignData; - socket.emit(MINING_EVENT_NAME, { data: payload, signature: signatureData.signature }); + await socket.emit(MINING_EVENT_NAME, { + data: payload, + signature: signatureData.signature, + pubKey: signatureData.pubKey, + }); } catch (e) { console.error(e); }