From c9e3f549a49bf7ce2ffbcec6f401d47a025b5fab Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Sun, 10 Dec 2023 18:54:31 +1100 Subject: [PATCH] sigchat probe the Signal host server for tls certificates - prompt the user for the Signal host server - probe the host for the tls Certificate Authorities chain of trust - allow the user to trust 1 or more Certificate Authorities --- apps/sigchat/locales/i18n.json | 8 ++++ apps/sigchat/src/api.rs | 1 + apps/sigchat/src/lib.rs | 80 ++++++++++++++++++++++++++++++++-- 3 files changed, 85 insertions(+), 4 deletions(-) diff --git a/apps/sigchat/locales/i18n.json b/apps/sigchat/locales/i18n.json index 2c65ac76d..4e12facc8 100644 --- a/apps/sigchat/locales/i18n.json +++ b/apps/sigchat/locales/i18n.json @@ -3,6 +3,14 @@ "en": "Failed to setup Signal Account", "en-tts": "Failed to setup Signal Account" }, + "sigchat.account.abort": { + "en": "Signal Account setup aborted", + "en-tts": "Signal Account setup aborted" + }, + "sigchat.account.host.name": { + "en": "Please provide the Signal host server name", + "en-tts": "Please provide the Signal host server name" + }, "sigchat.account.link": { "en": "Link to an existing Signal Account", "en-tts": "Link to an existing Signal Account" diff --git a/apps/sigchat/src/api.rs b/apps/sigchat/src/api.rs index bd9e19ae9..aabb47cb6 100644 --- a/apps/sigchat/src/api.rs +++ b/apps/sigchat/src/api.rs @@ -1,5 +1,6 @@ #[allow(dead_code)] pub(crate) const SERVER_NAME_SIGCHAT: &str = "_Signal chat_"; +pub(crate) const DEFAULT_HOST_NAME: &str = "signal.org"; #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] pub enum SigchatOp { diff --git a/apps/sigchat/src/lib.rs b/apps/sigchat/src/lib.rs index 4ccfb8d58..97667ec22 100644 --- a/apps/sigchat/src/lib.rs +++ b/apps/sigchat/src/lib.rs @@ -100,8 +100,32 @@ impl<'a> SigChat<'a> { .expect("failed radiobutton modal"); match self.modals.get_radio_index() { Ok(index) => match index { - 0 => Ok(self.account_link()?), - 1 => Ok(self.account_register()?), + 0 => { + let host = self.name_modal( + DEFAULT_HOST_NAME, + t!("sigchat.account.host.name", locales::LANG), + ); + match self.probe_host(&host) { + true => Ok(self.account_link(&host)?), + false => Err(Error::new( + ErrorKind::Other, + "failed to trust host certificate", + )), + } + } + 1 => { + let host = self.name_modal( + DEFAULT_HOST_NAME, + t!("sigchat.account.host.name", locales::LANG), + ); + match self.probe_host(&host) { + true => Ok(self.account_register(&host)?), + false => Err(Error::new( + ErrorKind::Other, + "failed to trust host certificate", + )), + } + } 2 => { log::info!("account setup aborted"); Err(Error::new(ErrorKind::Other, "account setup aborted")) @@ -121,24 +145,72 @@ impl<'a> SigChat<'a> { } } + /// Probe host for tls Certificate Authority chain of trust + /// + /// # Arguments + /// * `host` - the dns name or ip address of a Signal server + /// + /// # Returns + /// true if at least 1 Certificate Authority is trusted + /// + fn probe_host(&self, host: &str) -> bool { + let tls = Tls::new(); + match tls.probe(host) { + Ok(0) => { + self.modals + .show_notification(t!("sigchat.account.abort", locales::LANG), None) + .expect("abort failed"); + false + } + Ok(_count) => true, + Err(e) => { + self.modals + .show_notification(&format!("{}", e), None) + .expect("qrcode failed"); + false + } + } + } + /// Link this device to an existing Signal Account /// /// Signal allows to link additional devices to your primary device (e.g. Signal-Android). /// Note that currently Signal allows up to three linked devices per primary. /// - pub fn account_link(&self) { + pub fn account_link(&mut self, host: &str) -> Result { self.modals .show_notification(&"sorry - linking is not implemented yet", None) .expect("notification failed"); } + /// Prompt a name from the user + /// + /// # Arguments + /// * `default_name` - A name suggested to the user + /// * `prompt` - A prompt to explain what is being requested + /// + /// # Returns + /// the name provided by the user + /// + fn name_modal(&self, default_name: &str, prompt: &str) -> String { + match self + .modals + .alert_builder(prompt) + .field(Some(default_name.to_string()), None) + .build() + { + Ok(text) => text.content()[0].content.to_string(), + _ => default_name.to_string(), + } + } + /// Register a new Signal Account with this as the primary device. /// /// A Signal Account requires a phone number to receive SMS or incoming calls for registration & validation. /// The phone number must include the country calling code, i.e. the number must start with a "+" sign. /// Warning: this will disable the authentication of your phone as a primary device. /// - pub fn account_register(&mut self) -> Result { + pub fn account_register(&mut self, host: &str) -> Result { log::info!("Attempting to Register a new Signal Account"); self.modals .show_notification(&"sorry - registration is not implemented yet", None)