diff --git a/src/acns/tor/run.rs b/src/acns/tor/run.rs index de96e11..a308952 100644 --- a/src/acns/tor/run.rs +++ b/src/acns/tor/run.rs @@ -62,7 +62,7 @@ mod tests { #[test] fn test_run_tor() { - match TorRunner::run(TorrcGenerator::new().with_socks_port(9055), "./torrc", "/usr/sbin/tor") { + match TorRunner::run(TorrcGenerator::new().with_socks_port(9055), "./torrc", "/usr/sbin/tor", ".") { Ok(runner) => { sleep(Duration::new(5, 0)); assert!(remove_file("./torrc").is_ok()); diff --git a/src/acns/tor/validation.rs b/src/acns/tor/validation.rs index 30280d6..2dd576b 100644 --- a/src/acns/tor/validation.rs +++ b/src/acns/tor/validation.rs @@ -1,3 +1,8 @@ +use crate::acns::ACNError; +use crate::acns::ACNError::AuthenticationError; +use ed25519_dalek::{PublicKey, PUBLIC_KEY_LENGTH}; +use sha3::Digest; + /// check if the given hostname is a valid onion v3 address pub fn validate_hostname(hostname: &str) -> bool { match base32::decode(base32::Alphabet::RFC4648 { padding: false }, hostname) { @@ -6,8 +11,13 @@ pub fn validate_hostname(hostname: &str) -> bool { } } -use ed25519_dalek::{PublicKey, PUBLIC_KEY_LENGTH}; -use sha3::Digest; +/// convert a tor onionv3 hostname to a public key +pub fn hostname_to_public_key(hostname: &str) -> Result { + match base32::decode(base32::Alphabet::RFC4648 { padding: false }, hostname) { + Some(hostname_bytes) => Ok(PublicKey::from_bytes(hostname_bytes.split_at(PUBLIC_KEY_LENGTH).0).unwrap_or_default()), + _ => Err(AuthenticationError(String::from("error decoing hostname"))), + } +} /// convert the given public key to a tor onion v3 hostname pub fn public_key_to_hostname(public_key: &PublicKey) -> String { @@ -26,3 +36,18 @@ pub fn public_key_to_hostname(public_key: &PublicKey) -> String { buf[34] = 0x03; base32::encode(base32::Alphabet::RFC4648 { padding: false }, &buf).to_ascii_lowercase() } + +#[cfg(test)] +mod tests { + use crate::acns::tor::validation::{hostname_to_public_key, validate_hostname}; + use crate::primitives::identity::Identity; + use std::process::id; + + #[test] + fn test_validation() { + let identity = Identity::initialize_ephemeral_identity(); + let hostname = identity.hostname(); + assert!(validate_hostname(hostname.as_str())); + assert_eq!(hostname_to_public_key(hostname.as_str()).unwrap(), identity.public_key()) + } +}