From 84d5ff4c0c51d047aa61403e4d66f37ad95f0e81 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 26 Dec 2023 07:17:20 +1100 Subject: [PATCH] partial implementation of sigchat Account link() --- apps/sigchat/src/account.rs | 61 +++++++++++++++++++++++- apps/sigchat/src/lib.rs | 9 ++-- apps/sigchat/src/manager.rs | 40 +++++++++------- apps/sigchat/src/manager/libsignal.rs | 68 ++++++++++++++++++++------- 4 files changed, 139 insertions(+), 39 deletions(-) diff --git a/apps/sigchat/src/account.rs b/apps/sigchat/src/account.rs index 0549bb68e..a32722ce2 100644 --- a/apps/sigchat/src/account.rs +++ b/apps/sigchat/src/account.rs @@ -1,5 +1,6 @@ mod service_environment; +use crate::manager::libsignal::{DeviceNameUtil, ProvisionMessage}; use pddb::Pddb; use service_environment::ServiceEnvironment; use std::io::{Error, ErrorKind, Read, Write}; @@ -103,7 +104,6 @@ impl Account { Account::read(pddb_dict) } - // retrieves an existing Account from the pddb // // # Arguments @@ -217,6 +217,64 @@ impl Account { Ok(()) } + pub fn link( + &mut self, + device_name: &str, + provisioning_msg: ProvisionMessage, + ) -> Result { + // TODO complete link pre-checks + // Check if this device can be relinked + // check not primary + // check not registered in another environment + // check account state with server + + let aci = provisioning_msg.aci; + self.set(ACI_IDENTITY_PRIVATE_KEY, Some(&aci.djb_private_key.key))?; + self.set(ACI_IDENTITY_PUBLIC_KEY, Some(&aci.djb_identity_key.key))?; + self.set(ACI_SERVICE_ID_KEY, Some(&aci.service_id))?; + self.set(DEVICE_ID_KEY, Some("0"))?; + self.set( + ENCRYPTED_DEVICE_NAME_KEY, + Some(&DeviceNameUtil::encrypt_device_name( + device_name, + aci.djb_private_key, + )), + )?; + self.set(IS_MULTI_DEVICE_KEY, Some(&true.to_string()))?; + self.set(NUMBER_KEY, Some(&provisioning_msg.number))?; + self.set(PASSWORD_KEY, Some("STUB 32 bytes"))?; + self.set(PIN_MASTER_KEY_KEY, Some(&provisioning_msg.master_key))?; + let pni = provisioning_msg.pni; + self.set(PNI_IDENTITY_PRIVATE_KEY, Some(&pni.djb_private_key.key))?; + self.set(PNI_IDENTITY_PUBLIC_KEY, Some(&pni.djb_identity_key.key))?; + self.set(PNI_SERVICE_ID_KEY, Some(&aci.service_id))?; + self.set( + PROFILE_KEY_KEY, + Some( + &provisioning_msg + .profile_key + .unwrap_or_else(|| "STUB 32 bytes".to_string()), + ), + )?; + self.set(REGISTERED_KEY, Some(&false.to_string()))?; + self.set(STORAGE_KEY_KEY, None)?; + self.set(STORE_LAST_RECEIVE_TIMESTAMP_KEY, Some("0"))?; + self.set(STORE_MANIFEST_VERSION_KEY, Some("-1"))?; + self.set(STORE_MANIFEST_KEY, None)?; + + // TODO complete registration setup + // getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress()); + // getSenderKeyStore().deleteAll(); + // trustSelfIdentity(ServiceIdType.ACI); + // trustSelfIdentity(ServiceIdType.PNI); + // aciAccountData.getSessionStore().archiveAllSessions(); + // pniAccountData.getSessionStore().archiveAllSessions(); + // clearAllPreKeys(); + // getKeyValueStore().storeEntry(lastRecipientsRefresh, null); + + Ok(false) + } + #[allow(dead_code)] pub fn number(&self) -> Option<&str> { match &self.number { @@ -239,7 +297,6 @@ impl Account { get(&self.pddb, &self.pddb_dict, key) } - // Sets the value of a pddb_key / field in the Account // // To guarantee consistency, the value is saved to the pddb and, diff --git a/apps/sigchat/src/lib.rs b/apps/sigchat/src/lib.rs index ad7c6b37d..8aa7d81be 100644 --- a/apps/sigchat/src/lib.rs +++ b/apps/sigchat/src/lib.rs @@ -177,7 +177,7 @@ impl<'a> SigChat<'a> { .set_status_text(t!("sigchat.status.probing", locales::LANG)); self.chat.set_busy_state(true); let tls = Tls::new(); - if tls.accessible(host, true) { + if tls.accessible(host, true) { self.chat.set_busy_state(false); true } else { @@ -236,8 +236,11 @@ impl<'a> SigChat<'a> { } } } - Err(_) => { - log::warn!("failed to create new Account in pddb: {}", SIGCHAT_ACCOUNT); + Err(e) => { + log::warn!( + "failed to create new Account in pddb:{} : {e}", + SIGCHAT_ACCOUNT + ); self.modals .show_notification(t!("sigchat.account.failed", locales::LANG), None) .expect("notification failed"); diff --git a/apps/sigchat/src/manager.rs b/apps/sigchat/src/manager.rs index ec8052858..0d1ce43fd 100644 --- a/apps/sigchat/src/manager.rs +++ b/apps/sigchat/src/manager.rs @@ -1,5 +1,5 @@ mod group_permission; -mod libsignal; // stub +pub mod libsignal; // stub mod link_state; mod signal_ws; mod trust_mode; @@ -137,7 +137,7 @@ impl Manager { /// 4. displaying a uri as a qr-code (containing the uuid and pub_key) /// 5. scanning the qr-code with the primary Signal device /// 6. obtaining device registration via the websocket - /// 7. completing the registration processing + /// 7. checking and saving account details /// /// # Arguments /// * `name` - Optionally specify a name to describe this new device (defaults to "xous"). @@ -148,12 +148,13 @@ impl Manager { // insert default values as required let name = name.unwrap_or(DEFAULT_DEVICE_NAME); let host = host.unwrap_or(DEFAULT_HOST_NAME); - let link_err = Error::new(ErrorKind::Other, "failed to link device",); + let link_err = Error::new(ErrorKind::Other, "failed to link device"); match SignalWS::new_provision(&host) { Ok(mut ws) => { log::info!("provisioning websocket established to {host}"); match ws.read() { Ok(Message::Binary(uuid)) => { + log::info!("received Provisioning UUID message from host"); let uuid = libsignal::ProvisioningUuid::decode(uuid).id.clone(); let identity_key_pair = libsignal::generate_identity_key_pair(); let pub_key = identity_key_pair.djb_identity_key.key.clone(); @@ -174,21 +175,27 @@ impl Manager { .expect("qrcode failed"); match ws.read() { Ok(Message::Binary(registration)) => { - log::info!("raw registration ProtoBuffer: {:?}", registration); - log::info!("device name: {name}"); + log::info!("Registration message received from host"); ws.close(); - let provisioning_message = libsignal::ProvisionMessage::decode(identity_key_pair, registration); - log::info!("provisioning_message.number: {}", provisioning_message.number); - log::info!("provisioning_message.aci: {}", provisioning_message.aci); - log::info!("provisioning_message.pni: {}", provisioning_message.pni); - // TODO complete registration - Ok(true) + match self.account.link( + name, + libsignal::ProvisionMessage::decode( + identity_key_pair, + registration, + ), + ) { + Ok(result) => Ok(result), + Err(e) => { + log::warn!("linking error: {e}"); + Ok(false) + } + } } - Ok(_) => { + Ok(_) => { log::warn!("unexpected Provisioning msg"); Err(link_err) } - Err(e) => { + Err(e) => { log::warn!("{e}"); Err(link_err) } @@ -200,17 +207,16 @@ impl Manager { } } } - Ok(_) => { + Ok(_) => { log::warn!("unexpected Provisioning msg."); Err(link_err) } - Err(e) => { + Err(e) => { log::warn!("{e}"); Err(link_err) } } - } - + } Err(e) => { log::info!("failed to connect to server: {}", e); Err(e) diff --git a/apps/sigchat/src/manager/libsignal.rs b/apps/sigchat/src/manager/libsignal.rs index b8e2bb663..5b29e753d 100644 --- a/apps/sigchat/src/manager/libsignal.rs +++ b/apps/sigchat/src/manager/libsignal.rs @@ -1,27 +1,56 @@ // this is a stub for a wrapped libsignal +// org.whispersystems.signalservice.api.util.DeviceNameUtil; +pub struct DeviceNameUtil {} +impl DeviceNameUtil { + pub fn encrypt_device_name(_device_name: &str, _aci_private_key: IdentityKey) -> String { + "STUB".to_string() + } +} + +//////////////////////////////////////////////////////// // use org.whispersystems.signalservice.internal.crypto.PrimaryProvisioningCipher; -pub struct PrimaryProvisioningCipher{} +pub struct PrimaryProvisioningCipher {} impl PrimaryProvisioningCipher { - pub fn new(_stub: Option) -> PrimaryProvisioningCipher { - PrimaryProvisioningCipher {} + pub fn new(_stub: Option) -> PrimaryProvisioningCipher { + PrimaryProvisioningCipher {} } pub fn decrypt(&self, _temp_identity: IdentityKeyPair, bytes: Vec) -> ProvisionMessage { //log::info!("temp_identity: {:?}", temp_identity); log::info!("raw uuid Protocol Buffer: {:?}", bytes); ProvisionMessage { - number : "STUB number".to_string(), - aci : "STUB number".to_string(), - pni : "STUB number".to_string(), - } + number: "STUB number".to_string(), + aci: IdentityKeyPair { + service_id: "STUB".to_string(), + djb_identity_key: IdentityKey { + key: "STUB".to_string(), + }, + djb_private_key: IdentityKey { + key: "STUB".to_string(), + }, + }, + pni: IdentityKeyPair { + service_id: "STUB".to_string(), + djb_identity_key: IdentityKey { + key: "STUB".to_string(), + }, + djb_private_key: IdentityKey { + key: "STUB".to_string(), + }, + }, + master_key: "STUB number".to_string(), + profile_key: Some("STUB number".to_string()), + } } } pub struct ProvisionMessage { pub number: String, - pub aci:String, - pub pni:String, + pub aci: IdentityKeyPair, + pub pni: IdentityKeyPair, + pub master_key: String, + pub profile_key: Option, } impl ProvisionMessage { pub fn decode(temp_identity: IdentityKeyPair, bytes: Vec) -> ProvisionMessage { @@ -38,12 +67,14 @@ impl ProvisionMessage { // optional string uuid = 1; // } pub struct ProvisioningUuid { - pub id : String, + pub id: String, } impl ProvisioningUuid { pub fn decode(bytes: Vec) -> ProvisioningUuid { log::info!("raw uuid Protocol Buffer: {:?}", bytes); - ProvisioningUuid {id : "TODO decode uuid Protocol Buffer".to_string()} + ProvisioningUuid { + id: "TODO decode uuid Protocol Buffer".to_string(), + } } } @@ -51,12 +82,12 @@ impl ProvisioningUuid { // use org.signal.libsignal.protocol.IdentityKey; pub struct IdentityKey { - pub key:String, + pub key: String, } impl IdentityKey { pub fn new(key: String) -> Self { - IdentityKey {key} - } + IdentityKey { key } + } pub fn clone(&self) -> IdentityKey { IdentityKey::new(self.key.clone()) @@ -65,11 +96,11 @@ impl IdentityKey { // use org.signal.libsignal.protocol.IdentityKeyPair; pub struct IdentityKeyPair { + pub service_id: String, pub djb_identity_key: IdentityKey, pub djb_private_key: IdentityKey, } - // use org.signal.libsignal.protocol.ecc.Curve; pub struct Curve {} impl Curve { @@ -94,11 +125,14 @@ impl DjbKeyPair { } } - // https://github.com/AsamK/signal-cli/blob/375bdb79485ec90beb9a154112821a4657740b7a/lib/src/main/java/org/asamk/signal/manager/util/KeyUtils.java#L45-L51 pub fn generate_identity_key_pair() -> IdentityKeyPair { let djb_key_pair = Curve::generate_key_pair(); let djb_identity_key = IdentityKey::new(djb_key_pair.get_public_key().key); let djb_private_key = djb_key_pair.get_private_key(); - IdentityKeyPair { djb_identity_key, djb_private_key } + IdentityKeyPair { + service_id: "STUB".to_string(), + djb_identity_key, + djb_private_key, + } }