[Fix] WASM

This commit is contained in:
Alois 2026-06-25 19:38:40 +02:00
commit cfc9cebf6a
14 changed files with 461 additions and 204 deletions

View file

@ -8,14 +8,18 @@ pub fn load_client_db(
path: &str,
) -> Result<(Arc<Mutex<HashMap<u64, PublicKeyBundle>>>, Arc<Mutex<u64>>), Box<dyn std::error::Error>>
{
let clients: Arc<Mutex<HashMap<u64, PublicKeyBundle>>> =
Arc::new(Mutex::new(if let Ok(data) = fs::read_to_string(path) {
serde_json::from_str(&data).unwrap_or_default()
} else {
HashMap::new()
}));
let next_id = Arc::new(Mutex::new(
clients.lock().unwrap().keys().max().unwrap_or(&999) + 1,
));
let clients_map = match fs::read_to_string(path) {
Ok(data) => match serde_json::from_str(&data) {
Ok(clients) => clients,
Err(e) => {
eprintln!("Failed to parse {path}; starting with empty client database: {e}");
HashMap::new()
}
},
Err(_) => HashMap::new(),
};
let clients: Arc<Mutex<HashMap<u64, PublicKeyBundle>>> = Arc::new(Mutex::new(clients_map));
let next_value = clients.lock().unwrap().keys().max().unwrap_or(&999) + 1;
let next_id = Arc::new(Mutex::new(next_value));
Ok((clients, next_id))
}

View file

@ -1,5 +1,7 @@
use mtp::codec::{CommunicationType, CommunicationValue, DataType, DataTypeId, DataValue, TypeMap};
use mtp::crypto::{ChaCha20Poly1305, CryptoError, SignatureScheme, SignaturePublicKey, verify_ed25519};
use mtp::crypto::{
ChaCha20Poly1305, CryptoError, SignaturePublicKey, SignatureScheme, verify_ed25519,
};
struct Ed25519Verifier(SignaturePublicKey);
@ -71,6 +73,7 @@ pub fn process_and_respond(
enc_status = format!("EncryptedPayload decrypted OK ({} entries)", entries.len());
}
} else {
eprintln!(" EncryptedPayload decryption failed");
enc_status = String::from("EncryptedPayload: decryption FAILED");
}
}
@ -83,13 +86,14 @@ pub fn process_and_respond(
if dv.verify_into_container(&verifier).is_some() {
if let Some(entries) = dv.as_container() {
println!(" Verified SignedPayload: {:?}", entries);
sig_status =
format!("SignedPayload verified OK ({} entries)", entries.len());
sig_status = format!("SignedPayload verified OK ({} entries)", entries.len());
}
} else {
eprintln!(" SignedPayload verification failed");
sig_status = String::from("SignedPayload: verification FAILED");
}
} else {
eprintln!(" SignedPayload cannot be verified; no client public key available");
sig_status = String::from("SignedPayload: no client public key available");
}
}
@ -99,7 +103,9 @@ pub fn process_and_respond(
if let Some(pk_bundle) = client_pk {
let verifier = Ed25519Verifier(pk_bundle.sig_cl_public_key.clone());
let mut dv = secure.clone();
if dv.decrypt_signed_encrypted_container(&cipher, b"demo-aad").is_some()
if dv
.decrypt_signed_encrypted_container(&cipher, b"demo-aad")
.is_some()
&& dv.verify_into_container(&verifier).is_some()
{
if let Some(entries) = dv.as_container() {
@ -110,9 +116,11 @@ pub fn process_and_respond(
);
}
} else {
eprintln!(" SecurePayload decryption/verification failed");
secure_status = String::from("SecurePayload: decryption/verification FAILED");
}
} else {
eprintln!(" SecurePayload cannot be verified; no client public key available");
secure_status = String::from("SecurePayload: no client public key available");
}
}

View file

@ -1,9 +1,11 @@
use std::fs;
use mtp::crypto::{Ed25519Signer, Keyring, MlDsaSigner};
use mtp::crypto::kem::HybridKem;
use mtp::crypto::{Ed25519Signer, Keyring, MlDsaSigner};
pub fn load_or_generate_host_keys(path: &str) -> Result<(u64, Keyring), Box<dyn std::error::Error>> {
pub fn load_or_generate_host_keys(
path: &str,
) -> Result<(u64, Keyring), Box<dyn std::error::Error>> {
if let Ok(data) = fs::read_to_string(path) {
let json: serde_json::Value = serde_json::from_str(&data)?;
let hid = json["host_id"].as_u64().unwrap_or(1);
@ -29,10 +31,7 @@ pub fn load_or_generate_host_keys(path: &str) -> Result<(u64, Keyring), Box<dyn
pub fn export_host_public_keys(host_keyring: &Keyring) -> Result<(), Box<dyn std::error::Error>> {
let public_key_bundle_hex = hex::encode(host_keyring.public_key_bundle().as_bytes());
fs::write(
"host_public_key_bundle.hex",
&public_key_bundle_hex,
)?;
fs::write("host_public_key_bundle.hex", &public_key_bundle_hex)?;
fs::create_dir_all("web-client/public")?;
fs::write(
"web-client/public/host_public_key_bundle.hex",

View file

@ -40,7 +40,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let clients_for_get = clients.clone();
let get_existing_user = Box::new(move |id: u64| -> Option<mtp::crypto::PublicKeyBundle> {
clients_for_get.lock().unwrap().get(&id).cloned()
let result = clients_for_get.lock().unwrap().get(&id).cloned();
if result.is_some() {
println!("Auth lookup: client ID {id} found");
} else {
eprintln!("Auth lookup: unknown client ID {id}");
}
result
});
let clients_for_register = clients.clone();
@ -52,7 +58,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let id = *nid;
*nid += 1;
db.insert(id, bundle);
std::fs::write(&clients_path, serde_json::to_string_pretty(&*db).unwrap()).ok();
match serde_json::to_string_pretty(&*db) {
Ok(json) => match std::fs::write(&clients_path, json) {
Ok(()) => {}
Err(e) => eprintln!("Failed to persist client database to {clients_path}: {e}"),
},
Err(e) => eprintln!("Failed to serialize client database after registering {id}: {e}"),
}
println!("Registered new client with ID: {}", id);
id
});