diff --git a/.gitignore b/.gitignore index e3d273c..176ffc8 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,6 @@ target # Added by cargo /target + +*.mk +*.mpkb diff --git a/Cargo.lock b/Cargo.lock index 592912f..16c9bf5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2280,12 +2280,13 @@ dependencies = [ [[package]] name = "mtp" version = "0.1.0" -source = "git+https://git.methanium.net/methanium/mtp#44ff1d8781b4645dbff3dce2406155e4d9d43a4c" +source = "git+https://git.methanium.net/methanium/mtp#b96c072a0f87de7828e45cf3dcd44aef4a9e459d" dependencies = [ "mtp-client", "mtp-codec", "mtp-common", "mtp-crypto", + "mtp-files", "mtp-host", "mtp-transport", "mtp-type-map", @@ -2294,7 +2295,7 @@ dependencies = [ [[package]] name = "mtp-client" version = "0.1.0" -source = "git+https://git.methanium.net/methanium/mtp#44ff1d8781b4645dbff3dce2406155e4d9d43a4c" +source = "git+https://git.methanium.net/methanium/mtp#b96c072a0f87de7828e45cf3dcd44aef4a9e459d" dependencies = [ "mtp-codec", "mtp-common", @@ -2307,7 +2308,7 @@ dependencies = [ [[package]] name = "mtp-codec" version = "0.1.0" -source = "git+https://git.methanium.net/methanium/mtp#44ff1d8781b4645dbff3dce2406155e4d9d43a4c" +source = "git+https://git.methanium.net/methanium/mtp#b96c072a0f87de7828e45cf3dcd44aef4a9e459d" dependencies = [ "base64", "byteorder", @@ -2320,7 +2321,7 @@ dependencies = [ [[package]] name = "mtp-common" version = "0.1.0" -source = "git+https://git.methanium.net/methanium/mtp#44ff1d8781b4645dbff3dce2406155e4d9d43a4c" +source = "git+https://git.methanium.net/methanium/mtp#b96c072a0f87de7828e45cf3dcd44aef4a9e459d" dependencies = [ "quinn", "rustls", @@ -2331,7 +2332,7 @@ dependencies = [ [[package]] name = "mtp-crypto" version = "0.1.0" -source = "git+https://git.methanium.net/methanium/mtp#44ff1d8781b4645dbff3dce2406155e4d9d43a4c" +source = "git+https://git.methanium.net/methanium/mtp#b96c072a0f87de7828e45cf3dcd44aef4a9e459d" dependencies = [ "base64", "chacha20poly1305", @@ -2347,10 +2348,19 @@ dependencies = [ "zeroize", ] +[[package]] +name = "mtp-files" +version = "0.1.0" +source = "git+https://git.methanium.net/methanium/mtp#b96c072a0f87de7828e45cf3dcd44aef4a9e459d" +dependencies = [ + "mtp-crypto", + "thiserror 1.0.69", +] + [[package]] name = "mtp-host" version = "0.1.0" -source = "git+https://git.methanium.net/methanium/mtp#44ff1d8781b4645dbff3dce2406155e4d9d43a4c" +source = "git+https://git.methanium.net/methanium/mtp#b96c072a0f87de7828e45cf3dcd44aef4a9e459d" dependencies = [ "mtp-codec", "mtp-common", @@ -2363,7 +2373,7 @@ dependencies = [ [[package]] name = "mtp-transport" version = "0.1.0" -source = "git+https://git.methanium.net/methanium/mtp#44ff1d8781b4645dbff3dce2406155e4d9d43a4c" +source = "git+https://git.methanium.net/methanium/mtp#b96c072a0f87de7828e45cf3dcd44aef4a9e459d" dependencies = [ "log", "mtp-codec", @@ -2377,7 +2387,7 @@ dependencies = [ [[package]] name = "mtp-type-map" version = "0.1.0" -source = "git+https://git.methanium.net/methanium/mtp#44ff1d8781b4645dbff3dce2406155e4d9d43a4c" +source = "git+https://git.methanium.net/methanium/mtp#b96c072a0f87de7828e45cf3dcd44aef4a9e459d" dependencies = [ "serde", "serde_yaml", diff --git a/Cargo.toml b/Cargo.toml index e9a78f4..d3de686 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,8 @@ edition = "2024" [dependencies] mtp = { git = "https://git.methanium.net/methanium/mtp", features = [ "host", - "crypto" + "crypto", + "files", ] } actix-web = { version = "4.12.1", features = ["rustls-0_23"] } @@ -39,4 +40,6 @@ uuid = { version = "1.19.0", features = ["v4"] } x448 = "0.6.0" zip = "6.0.0" thiserror = "2.0.18" -mtp-crypto = { git = "https://git.methanium.net/methanium/mtp", version = "0.1.0", features = ["pqc"] } +mtp-crypto = { git = "https://git.methanium.net/methanium/mtp", version = "0.1.0", features = [ + "pqc", +] } diff --git a/src/main.rs b/src/main.rs index 8672167..2abf3a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,23 +9,32 @@ use crate::transport::omikron_connection; use crate::util::file_util::get_directory; use crate::util::logger::PrintType; use crate::util::logger::startup; -use base64::Engine as _; use dotenv::from_path; -use mtp_crypto::{Keyring, PublicKeyBundle}; +use mtp::files::{load_keyring as load_keyring_file, save_keyring, save_public_key_bundle}; +use mtp_crypto::Keyring; use once_cell::sync::Lazy; use rustls::crypto::aws_lc_rs::default_provider; use std::env; use std::path::Path; -static KEYRING_ENV: Lazy = Lazy::new(|| env::var("KEYRING").unwrap()); -fn load_keyring() -> Keyring { - let bytes = base64::engine::general_purpose::STANDARD - .decode(&*KEYRING_ENV) - .expect("Invalid KEYRING env var: not valid base64"); - Keyring::from_bytes(&bytes).expect("Invalid KEYRING env var: failed to deserialize") +const KEYRING_PATH: &str = "./omega.mk"; + +static KEYRING: Lazy = Lazy::new(|| { + load_keyring_file(KEYRING_PATH).unwrap_or_else(|_| { + let kr = Keyring::generate(); + save_keyring(&kr, KEYRING_PATH).expect("Failed to save generated keyring"); + save_public_key_bundle(&kr.public_key_bundle(), KEYRING_PATH) + .expect("Failed to save generated public key bundle"); + eprintln!("Generated new keyring at {}", KEYRING_PATH); + kr + }) +}); + +pub fn get_keyring() -> &'static Keyring { + &KEYRING } -pub fn get_public_key_bundle() -> PublicKeyBundle { - load_keyring().public_key_bundle() +pub fn load_keyring() -> Keyring { + Keyring::from_bytes(&KEYRING.to_bytes()).unwrap() } #[tokio::main] diff --git a/src/server/api.rs b/src/server/api.rs index 93c594e..ae251cf 100644 --- a/src/server/api.rs +++ b/src/server/api.rs @@ -1,4 +1,4 @@ -use crate::get_public_key_bundle; +use crate::load_keyring; use crate::sql::sql; use crate::sql::sql::{get_by_user_id, get_omikron_by_id}; use crate::sql::user_online_tracker::get_iota_primary_omikron_connection; @@ -192,7 +192,7 @@ pub async fn handle(path: &str, body_string: Option) -> HttpResponse { ["api", "get", "public_key"] => { let mut res = JsonValue::new_object(); res["status"] = "success".into(); - let bundle = get_public_key_bundle(); + let bundle = load_keyring().public_key_bundle(); res["public_key"] = base64::engine::general_purpose::STANDARD .encode(bundle.as_bytes()) .into(); diff --git a/src/transport/omikron_connection.rs b/src/transport/omikron_connection.rs index cb11548..4435fdb 100644 --- a/src/transport/omikron_connection.rs +++ b/src/transport/omikron_connection.rs @@ -71,8 +71,6 @@ pub struct WaitingTask { pub struct OmikronConnection { id: u64, sender: Mutex>, - challenge: RwLock, - pub_key: RwLock>>, pub ping: RwLock, waiting_tasks: DashMap, cleanup_handle: std::sync::Mutex>>, @@ -91,12 +89,10 @@ impl OmikronConnection { // Construction // ------------------------------------------------------------------------- - pub fn new(sender: Sender) -> Arc { + pub fn new(sender: Sender, id: u64) -> Arc { let conn = Arc::new(Self { - id: rand::random(), + id, sender: Mutex::new(Some(sender)), - challenge: RwLock::new(String::new()), - pub_key: RwLock::new(None), ping: RwLock::new(-1), waiting_tasks: DashMap::new(), cleanup_handle: std::sync::Mutex::new(None), @@ -163,11 +159,14 @@ impl OmikronConnection { return Ok(()); } - // Handle ping regardless of auth state + // Handle ping regardless of message type if cv.is_type(CommunicationType::Ping) { return self.handle_ping(cv).await; } - return Ok(()); + + // Authentication is completed by the mtp host before this connection exists. + let omikron_id = self.id as i64; + self.clone().handle_authenticated(cv, omikron_id).await } async fn handle_authenticated( @@ -1054,7 +1053,7 @@ pub async fn get_by_omikron_id( .ok() .map(|(bundle, _ip_address)| bundle) } -pub async fn complete_register(pub_key: PublicKeyBundle, description: Option) -> u64 { +pub async fn complete_register(_pub_key: PublicKeyBundle, _description: Option) -> u64 { 0 } @@ -1097,7 +1096,8 @@ pub async fn start(port: u16) -> Result<(), Box> { while let Ok(Some(mut connection)) = host.accept().await { tokio::spawn(async move { - let conn = OmikronConnection::new(connection.sender); + let conn = OmikronConnection::new(connection.sender, connection.client_id); + omikron_manager::add_omikron(conn.clone()).await; conn.handle(&mut connection.receiver).await; }); }