[geary/wip/20-cert-pinning: 1/4] Handle untrusted certs when adding a editing account server details



commit 1a2bb280badf15184c21564f770380d6e137fdf4
Author: Michael Gratton <mike vee net>
Date:   Wed Jan 9 13:00:54 2019 +1100

    Handle untrusted certs when adding a editing account server details
    
    Move common code for prompting for cert errors to the account editor,
    use that when validating an account from the server pane.

 src/client/accounts/accounts-editor-add-pane.vala  | 29 ++++--------
 .../accounts/accounts-editor-servers-pane.vala     | 52 +++++++++++++++++++---
 src/client/accounts/accounts-editor.vala           | 35 +++++++++++++++
 3 files changed, 90 insertions(+), 26 deletions(-)
---
diff --git a/src/client/accounts/accounts-editor-add-pane.vala 
b/src/client/accounts/accounts-editor-add-pane.vala
index 5d9f54ae..75789bd1 100644
--- a/src/client/accounts/accounts-editor-add-pane.vala
+++ b/src/client/accounts/accounts-editor-add-pane.vala
@@ -232,7 +232,6 @@ internal class Accounts.EditorAddPane : Gtk.Grid, EditorPane {
                     // Translators: In-app notification label
                     message = _("Check your sending login and password");
                 } catch (GLib.TlsError.BAD_CERTIFICATE err) {
-                    debug("Error validating SMTP certifiate: %s", err.message);
                     // Nothing to do here, since the untrusted host
                     // handler will be dealing with it
                 } catch (GLib.IOError.CANCELLED err) {
@@ -261,6 +260,10 @@ internal class Accounts.EditorAddPane : Gtk.Grid, EditorPane {
                 to_focus = this.email.value;
                 // Translators: In-app notification label
                 message = _("Check your email address and password");
+            } catch (GLib.TlsError.BAD_CERTIFICATE err) {
+                // Nothing to do here, since the untrusted host
+                // handler will be dealing with it
+                debug("Error validating SMTP certifiate: %s", err.message);
             } catch (GLib.Error err) {
                 Geary.ErrorContext context = new Geary.ErrorContext(err);
                 debug("Error validating SMTP service: %s",
@@ -441,29 +444,15 @@ internal class Accounts.EditorAddPane : Gtk.Grid, EditorPane {
                                    Geary.ServiceInformation service,
                                    Geary.Endpoint endpoint,
                                    GLib.TlsConnection cx) {
-        this.editor.certificates.prompt_pin_certificate.begin(
-            this.editor, account, service, endpoint, true, this.op_cancellable,
+        this.editor.prompt_pin_certificate.begin(
+            account, service, endpoint, this.op_cancellable,
             (obj, res) => {
                 try {
-                    this.editor.certificates.prompt_pin_certificate.end(res);
-                } catch (Application.CertificateManagerError.UNTRUSTED err) {
-                    // All good, just drop back into the editor window.
-                    return;
-                } catch (Application.CertificateManagerError.STORE_FAILED err) {
+                    this.editor.prompt_pin_certificate.end(res);
+                } catch (Application.CertificateManagerError err) {
                     // All good, just drop back into the editor
-                    // window. XXX show error info bar rather than a
-                    // notification
-                    this.editor.add_notification(
-                        new InAppNotification(
-                            // Translators: In-app notification label,
-                            // when the app had a problem pinning an
-                            // otherwise untrusted TLS certificate
-                            _("Failed to store certificate")
-                        )
-                    );
+                    // window.
                     return;
-                } catch (Application.CertificateManagerError err) {
-                    debug("Unexptected error pinning cert: %s", err.message);
                 }
 
                 // Kick off another attempt to validate
diff --git a/src/client/accounts/accounts-editor-servers-pane.vala 
b/src/client/accounts/accounts-editor-servers-pane.vala
index 38a4b3c0..db6f2af9 100644
--- a/src/client/accounts/accounts-editor-servers-pane.vala
+++ b/src/client/accounts/accounts-editor-servers-pane.vala
@@ -281,22 +281,34 @@ internal class Accounts.EditorServersPane :
     }
 
     private async bool validate(GLib.Cancellable? cancellable) {
+        // Use a copy here so we can handle any prompting needed
+        // (auth, certs) directly, rather than through the main window
+        Geary.AccountInformation local_account =
+            new Geary.AccountInformation.copy(this.account);
+        local_account.untrusted_host.connect(on_untrusted_host);
+
         string? message = null;
         bool imap_valid = false;
         try {
             yield this.engine.validate_imap(
-                this.account, this.incoming_mutable, cancellable
+                local_account, this.incoming_mutable, cancellable
             );
             imap_valid = true;
         } catch (Geary.ImapError.UNAUTHENTICATED err) {
             debug("Error authenticating IMAP service: %s", err.message);
             // Translators: In-app notification label
             message = _("Check your receiving login and password");
+        } catch (GLib.TlsError.BAD_CERTIFICATE err) {
+            // Nothing to do here, since the untrusted host
+            // handler will be dealing with it
+            debug("Error validating IMAP certifiate: %s", err.message);
         } catch (GLib.IOError.CANCELLED err) {
             // Nothing to do here, someone just cancelled
             debug("IMAP validation was cancelled: %s", err.message);
         } catch (GLib.Error err) {
-            debug("Error validating IMAP service: %s", err.message);
+            Geary.ErrorContext context = new Geary.ErrorContext(err);
+            debug("Error validating IMAP service: %s",
+                  context.format_full_error());
             // Translators: In-app notification label
             message = _("Check your receiving server details");
         }
@@ -306,7 +318,7 @@ internal class Accounts.EditorServersPane :
             debug("Validating SMTP...");
             try {
                 yield this.engine.validate_smtp(
-                    this.account,
+                    local_account,
                     this.outgoing_mutable,
                     this.incoming_mutable.credentials,
                     cancellable
@@ -320,16 +332,24 @@ internal class Accounts.EditorServersPane :
                 this.outgoing_auth.value.source = Geary.Credentials.Requirement.CUSTOM;
                 // Translators: In-app notification label
                 message = _("Check your sending login and password");
+            } catch (GLib.TlsError.BAD_CERTIFICATE err) {
+                // Nothing to do here, since the untrusted host
+                // handler will be dealing with it
+                debug("Error validating SMTP certifiate: %s", err.message);
             } catch (GLib.IOError.CANCELLED err) {
                 // Nothing to do here, someone just cancelled
                 debug("SMTP validation was cancelled: %s", err.message);
             } catch (GLib.Error err) {
-                debug("Error validating SMTP service: %s", err.message);
-                    // Translators: In-app notification label
-                    message = _("Check your sending server details");
+                Geary.ErrorContext context = new Geary.ErrorContext(err);
+                debug("Error validating SMTP service: %s",
+                      context.format_full_error());
+                // Translators: In-app notification label
+                message = _("Check your sending server details");
             }
         }
 
+        local_account.untrusted_host.disconnect(on_untrusted_host);
+
         bool is_valid = imap_valid && smtp_valid;
         debug("Validation complete, is valid: %s", is_valid.to_string());
 
@@ -413,6 +433,26 @@ internal class Accounts.EditorServersPane :
         }
     }
 
+    private void on_untrusted_host(Geary.AccountInformation account,
+                                   Geary.ServiceInformation service,
+                                   Geary.Endpoint endpoint,
+                                   GLib.TlsConnection cx) {
+        this.editor.prompt_pin_certificate.begin(
+            account, service, endpoint, null,
+            (obj, res) => {
+                try {
+                    this.editor.prompt_pin_certificate.end(res);
+                } catch (Application.CertificateManagerError err) {
+                    // All good, just drop back into the editor
+                    // window.
+                    return;
+                }
+
+                // Kick off another attempt to save
+                this.save.begin(null);
+            });
+    }
+
     [GtkCallback]
     private void on_cancel_button_clicked() {
         if (this.is_operation_running) {
diff --git a/src/client/accounts/accounts-editor.vala b/src/client/accounts/accounts-editor.vala
index 1d1a8be6..24f3297f 100644
--- a/src/client/accounts/accounts-editor.vala
+++ b/src/client/accounts/accounts-editor.vala
@@ -175,6 +175,41 @@ public class Accounts.Editor : Gtk.Dialog {
         notification.show();
     }
 
+    /**
+     * Prompts for pinning a certificate using the certificate manager.
+     *
+     * This provides a thing wrapper around {@link
+     * CertificateManager.prompt_pin_certificate} that uses the
+     * account editor as the dialog parent.
+     */
+    internal async void prompt_pin_certificate(Geary.AccountInformation account,
+                                               Geary.ServiceInformation service,
+                                               Geary.Endpoint endpoint,
+                                               GLib.Cancellable? cancellable)
+        throws Application.CertificateManagerError {
+        try {
+            yield this.certificates.prompt_pin_certificate(
+                this, account, service, endpoint, true, cancellable
+            );
+        } catch (Application.CertificateManagerError.UNTRUSTED err) {
+            throw err;
+        } catch (Application.CertificateManagerError.STORE_FAILED err) {
+            // XXX show error info bar rather than a notification?
+            add_notification(
+                new InAppNotification(
+                    // Translators: In-app notification label, when
+                    // the app had a problem pinning an otherwise
+                    // untrusted TLS certificate
+                    _("Failed to store certificate")
+                )
+            );
+            throw err;
+        } catch (Application.CertificateManagerError err) {
+            debug("Unexpected error pinning cert: %s", err.message);
+            throw err;
+        }
+    }
+
     /** Removes an account from the editor. */
     internal void remove_account(Geary.AccountInformation account) {
         this.editor_panes.set_visible_child(this.editor_list_pane);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]