Skip to content

Commit

Permalink
fix: use der format and return pubk
Browse files Browse the repository at this point in the history
  • Loading branch information
BalazsSevecsek committed Dec 16, 2024
1 parent 2f42c47 commit 2906b45
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 18 deletions.
167 changes: 167 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
32 changes: 25 additions & 7 deletions src-tauri/src/app_in_memory_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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<Ed25519KeyPair> {
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<String> {
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")]
Expand Down
Loading

0 comments on commit 2906b45

Please sign in to comment.