partial implementation of sigchat Account link()

This commit is contained in:
nworbnhoj 2023-12-26 07:17:20 +11:00
parent 68b2c55460
commit 84d5ff4c0c
4 changed files with 139 additions and 39 deletions

View file

@ -1,5 +1,6 @@
mod service_environment; mod service_environment;
use crate::manager::libsignal::{DeviceNameUtil, ProvisionMessage};
use pddb::Pddb; use pddb::Pddb;
use service_environment::ServiceEnvironment; use service_environment::ServiceEnvironment;
use std::io::{Error, ErrorKind, Read, Write}; use std::io::{Error, ErrorKind, Read, Write};
@ -103,7 +104,6 @@ impl Account {
Account::read(pddb_dict) Account::read(pddb_dict)
} }
// retrieves an existing Account from the pddb // retrieves an existing Account from the pddb
// //
// # Arguments // # Arguments
@ -217,6 +217,64 @@ impl Account {
Ok(()) Ok(())
} }
pub fn link(
&mut self,
device_name: &str,
provisioning_msg: ProvisionMessage,
) -> Result<bool, Error> {
// 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)] #[allow(dead_code)]
pub fn number(&self) -> Option<&str> { pub fn number(&self) -> Option<&str> {
match &self.number { match &self.number {
@ -239,7 +297,6 @@ impl Account {
get(&self.pddb, &self.pddb_dict, key) get(&self.pddb, &self.pddb_dict, key)
} }
// Sets the value of a pddb_key / field in the Account // Sets the value of a pddb_key / field in the Account
// //
// To guarantee consistency, the value is saved to the pddb and, // To guarantee consistency, the value is saved to the pddb and,

View file

@ -236,8 +236,11 @@ impl<'a> SigChat<'a> {
} }
} }
} }
Err(_) => { Err(e) => {
log::warn!("failed to create new Account in pddb: {}", SIGCHAT_ACCOUNT); log::warn!(
"failed to create new Account in pddb:{} : {e}",
SIGCHAT_ACCOUNT
);
self.modals self.modals
.show_notification(t!("sigchat.account.failed", locales::LANG), None) .show_notification(t!("sigchat.account.failed", locales::LANG), None)
.expect("notification failed"); .expect("notification failed");

View file

@ -1,5 +1,5 @@
mod group_permission; mod group_permission;
mod libsignal; // stub pub mod libsignal; // stub
mod link_state; mod link_state;
mod signal_ws; mod signal_ws;
mod trust_mode; mod trust_mode;
@ -137,7 +137,7 @@ impl Manager {
/// 4. displaying a uri as a qr-code (containing the uuid and pub_key) /// 4. displaying a uri as a qr-code (containing the uuid and pub_key)
/// 5. scanning the qr-code with the primary Signal device /// 5. scanning the qr-code with the primary Signal device
/// 6. obtaining device registration via the websocket /// 6. obtaining device registration via the websocket
/// 7. completing the registration processing /// 7. checking and saving account details
/// ///
/// # Arguments /// # Arguments
/// * `name` - Optionally specify a name to describe this new device (defaults to "xous"). /// * `name` - Optionally specify a name to describe this new device (defaults to "xous").
@ -148,12 +148,13 @@ impl Manager {
// insert default values as required // insert default values as required
let name = name.unwrap_or(DEFAULT_DEVICE_NAME); let name = name.unwrap_or(DEFAULT_DEVICE_NAME);
let host = host.unwrap_or(DEFAULT_HOST_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) { match SignalWS::new_provision(&host) {
Ok(mut ws) => { Ok(mut ws) => {
log::info!("provisioning websocket established to {host}"); log::info!("provisioning websocket established to {host}");
match ws.read() { match ws.read() {
Ok(Message::Binary(uuid)) => { Ok(Message::Binary(uuid)) => {
log::info!("received Provisioning UUID message from host");
let uuid = libsignal::ProvisioningUuid::decode(uuid).id.clone(); let uuid = libsignal::ProvisioningUuid::decode(uuid).id.clone();
let identity_key_pair = libsignal::generate_identity_key_pair(); let identity_key_pair = libsignal::generate_identity_key_pair();
let pub_key = identity_key_pair.djb_identity_key.key.clone(); let pub_key = identity_key_pair.djb_identity_key.key.clone();
@ -174,15 +175,21 @@ impl Manager {
.expect("qrcode failed"); .expect("qrcode failed");
match ws.read() { match ws.read() {
Ok(Message::Binary(registration)) => { Ok(Message::Binary(registration)) => {
log::info!("raw registration ProtoBuffer: {:?}", registration); log::info!("Registration message received from host");
log::info!("device name: {name}");
ws.close(); ws.close();
let provisioning_message = libsignal::ProvisionMessage::decode(identity_key_pair, registration); match self.account.link(
log::info!("provisioning_message.number: {}", provisioning_message.number); name,
log::info!("provisioning_message.aci: {}", provisioning_message.aci); libsignal::ProvisionMessage::decode(
log::info!("provisioning_message.pni: {}", provisioning_message.pni); identity_key_pair,
// TODO complete registration registration,
Ok(true) ),
) {
Ok(result) => Ok(result),
Err(e) => {
log::warn!("linking error: {e}");
Ok(false)
}
}
} }
Ok(_) => { Ok(_) => {
log::warn!("unexpected Provisioning msg"); log::warn!("unexpected Provisioning msg");
@ -210,7 +217,6 @@ impl Manager {
} }
} }
} }
Err(e) => { Err(e) => {
log::info!("failed to connect to server: {}", e); log::info!("failed to connect to server: {}", e);
Err(e) Err(e)

View file

@ -1,27 +1,56 @@
// this is a stub for a wrapped libsignal // 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; // use org.whispersystems.signalservice.internal.crypto.PrimaryProvisioningCipher;
pub struct PrimaryProvisioningCipher{} pub struct PrimaryProvisioningCipher {}
impl PrimaryProvisioningCipher { impl PrimaryProvisioningCipher {
pub fn new(_stub: Option<String>) -> PrimaryProvisioningCipher { pub fn new(_stub: Option<String>) -> PrimaryProvisioningCipher {
PrimaryProvisioningCipher {} PrimaryProvisioningCipher {}
} }
pub fn decrypt(&self, _temp_identity: IdentityKeyPair, bytes: Vec<u8>) -> ProvisionMessage { pub fn decrypt(&self, _temp_identity: IdentityKeyPair, bytes: Vec<u8>) -> ProvisionMessage {
//log::info!("temp_identity: {:?}", temp_identity); //log::info!("temp_identity: {:?}", temp_identity);
log::info!("raw uuid Protocol Buffer: {:?}", bytes); log::info!("raw uuid Protocol Buffer: {:?}", bytes);
ProvisionMessage { ProvisionMessage {
number : "STUB number".to_string(), number: "STUB number".to_string(),
aci : "STUB number".to_string(), aci: IdentityKeyPair {
pni : "STUB number".to_string(), 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 struct ProvisionMessage {
pub number: String, pub number: String,
pub aci:String, pub aci: IdentityKeyPair,
pub pni:String, pub pni: IdentityKeyPair,
pub master_key: String,
pub profile_key: Option<String>,
} }
impl ProvisionMessage { impl ProvisionMessage {
pub fn decode(temp_identity: IdentityKeyPair, bytes: Vec<u8>) -> ProvisionMessage { pub fn decode(temp_identity: IdentityKeyPair, bytes: Vec<u8>) -> ProvisionMessage {
@ -38,12 +67,14 @@ impl ProvisionMessage {
// optional string uuid = 1; // optional string uuid = 1;
// } // }
pub struct ProvisioningUuid { pub struct ProvisioningUuid {
pub id : String, pub id: String,
} }
impl ProvisioningUuid { impl ProvisioningUuid {
pub fn decode(bytes: Vec<u8>) -> ProvisioningUuid { pub fn decode(bytes: Vec<u8>) -> ProvisioningUuid {
log::info!("raw uuid Protocol Buffer: {:?}", bytes); 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,11 +82,11 @@ impl ProvisioningUuid {
// use org.signal.libsignal.protocol.IdentityKey; // use org.signal.libsignal.protocol.IdentityKey;
pub struct IdentityKey { pub struct IdentityKey {
pub key:String, pub key: String,
} }
impl IdentityKey { impl IdentityKey {
pub fn new(key: String) -> Self { pub fn new(key: String) -> Self {
IdentityKey {key} IdentityKey { key }
} }
pub fn clone(&self) -> IdentityKey { pub fn clone(&self) -> IdentityKey {
@ -65,11 +96,11 @@ impl IdentityKey {
// use org.signal.libsignal.protocol.IdentityKeyPair; // use org.signal.libsignal.protocol.IdentityKeyPair;
pub struct IdentityKeyPair { pub struct IdentityKeyPair {
pub service_id: String,
pub djb_identity_key: IdentityKey, pub djb_identity_key: IdentityKey,
pub djb_private_key: IdentityKey, pub djb_private_key: IdentityKey,
} }
// use org.signal.libsignal.protocol.ecc.Curve; // use org.signal.libsignal.protocol.ecc.Curve;
pub struct Curve {} pub struct Curve {}
impl 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 // 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 { pub fn generate_identity_key_pair() -> IdentityKeyPair {
let djb_key_pair = Curve::generate_key_pair(); let djb_key_pair = Curve::generate_key_pair();
let djb_identity_key = IdentityKey::new(djb_key_pair.get_public_key().key); let djb_identity_key = IdentityKey::new(djb_key_pair.get_public_key().key);
let djb_private_key = djb_key_pair.get_private_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,
}
} }