1/* Copyright 2016 Software Freedom Conservancy Inc.
2 *
3 * This software is licensed under the GNU Lesser General Public License
4 * (version 2.1 or later).  See the COPYING file in this distribution.
5 */
6
7public class CertificateWarningDialog {
8    public enum Result {
9        DONT_TRUST,
10        TRUST,
11        ALWAYS_TRUST
12    }
13
14    private const string BULLET = "• ";
15
16    private Gtk.Dialog dialog;
17
18    public CertificateWarningDialog(Gtk.Window? parent,
19                                    Geary.AccountInformation account,
20                                    Geary.ServiceInformation service,
21                                    Geary.Endpoint endpoint,
22                                    bool is_validation) {
23        Gtk.Builder builder = GioUtil.create_builder("certificate_warning_dialog.glade");
24
25        dialog = (Gtk.Dialog) builder.get_object("CertificateWarningDialog");
26        dialog.transient_for = parent;
27        dialog.modal = true;
28
29        Gtk.Label title_label = (Gtk.Label) builder.get_object("untrusted_connection_label");
30        Gtk.Label top_label = (Gtk.Label) builder.get_object("top_label");
31        Gtk.Label warnings_label = (Gtk.Label) builder.get_object("warnings_label");
32        Gtk.Label trust_label = (Gtk.Label) builder.get_object("trust_label");
33        Gtk.Label dont_trust_label = (Gtk.Label) builder.get_object("dont_trust_label");
34        Gtk.Label contact_label = (Gtk.Label) builder.get_object("contact_label");
35
36        title_label.label = _("Untrusted Connection: %s").printf(account.display_name);
37
38        top_label.label = _("The identity of the %s mail server at %s:%u could not be verified.").printf(
39            service.protocol.to_value(), service.host, service.port);
40
41        warnings_label.label = generate_warning_list(
42            endpoint.tls_validation_warnings
43        );
44        warnings_label.use_markup = true;
45
46        trust_label.label =
47            "<b>"
48            +_("Selecting “Trust This Server” or “Always Trust This Server” may cause your username and password to be transmitted insecurely.")
49            + "</b>";
50        trust_label.use_markup = true;
51
52        if (is_validation) {
53            // could be a new or existing account
54            dont_trust_label.label =
55                "<b>"
56                + _("Selecting “Don’t Trust This Server” will cause Geary not to access this server.")
57                + "</b> "
58                + _("Geary will not add or update this email account.");
59        } else {
60            // a registered account
61            dont_trust_label.label =
62                "<b>"
63                + _("Selecting “Don’t Trust This Server” will cause Geary to stop accessing this account.")
64                + "</b> ";
65        }
66        dont_trust_label.use_markup = true;
67
68        contact_label.label =
69            _("Contact your system administrator or email service provider if you have any question about these issues.");
70    }
71
72    private static string generate_warning_list(TlsCertificateFlags warnings) {
73        StringBuilder builder = new StringBuilder();
74
75        if ((warnings & TlsCertificateFlags.UNKNOWN_CA) != 0)
76            builder.append(BULLET + _("The server’s certificate is not signed by a known authority") + "\n");
77
78        if ((warnings & TlsCertificateFlags.BAD_IDENTITY) != 0)
79            builder.append(BULLET + _("The server’s identity does not match the identity in the certificate") + "\n");
80
81        if ((warnings & TlsCertificateFlags.EXPIRED) != 0)
82            builder.append(BULLET + _("The server’s certificate has expired") + "\n");
83
84        if ((warnings & TlsCertificateFlags.NOT_ACTIVATED) != 0)
85            builder.append(BULLET + _("The server’s certificate has not been activated") + "\n");
86
87        if ((warnings & TlsCertificateFlags.REVOKED) != 0)
88            builder.append(BULLET + _("The server’s certificate has been revoked and is now invalid") + "\n");
89
90        if ((warnings & TlsCertificateFlags.INSECURE) != 0)
91            builder.append(BULLET + _("The server’s certificate is considered insecure") + "\n");
92
93        if ((warnings & TlsCertificateFlags.GENERIC_ERROR) != 0)
94            builder.append(BULLET + _("An error has occurred processing the server’s certificate") + "\n");
95
96        return builder.str;
97    }
98
99    public Result run() {
100        dialog.show_all();
101        int response = dialog.run();
102        dialog.destroy();
103
104        // these values are defined in the Glade file
105        switch (response) {
106            case 1:
107                return Result.TRUST;
108
109            case 2:
110                return Result.ALWAYS_TRUST;
111
112            default:
113                return Result.DONT_TRUST;
114        }
115    }
116}
117
118