In TrustAnchorConfirmationRequest, check for headless mode and return false if finger...
[moonshot-ui.git] / src / moonshot-trust-anchor-dialog.vala
1 /*
2  * Copyright (c) 2011-2016, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31 */
32 using Gtk;
33
34 public delegate void TrustAnchorConfirmationCallback(TrustAnchorConfirmationRequest request);
35
36 public class TrustAnchorConfirmationRequest : GLib.Object {
37     static MoonshotLogger logger = get_logger("TrustAnchorConfirmationRequest");
38
39     IdentityManagerApp parent_app;
40     string userid;
41     string realm;
42     string fingerprint;
43     public bool confirmed = false;
44
45     TrustAnchorConfirmationCallback callback = null;
46
47     public TrustAnchorConfirmationRequest(IdentityManagerApp parent_app,
48                                           string userid,
49                                           string realm,
50                                           string fingerprint)
51     {
52         this.parent_app = parent_app;
53         this.userid = userid;
54         this.realm = realm;
55         this.fingerprint = fingerprint;
56     }
57
58     public void set_callback(owned TrustAnchorConfirmationCallback cb)
59     {
60 //        #if VALA_0_12
61             this.callback = ((owned) cb);
62 //        #else
63 //            this.callback = ((IdCard) => cb(IdCard));
64 //        #endif
65     }
66
67     public bool execute() {
68
69         string nai = userid + "@" + realm;
70         IdCard? card = parent_app.model.find_id_card(nai, parent_app.use_flat_file_store);
71         if (card == null) {
72             logger.warn(@"execute: Could not find ID card for NAI $nai; returning false.");
73             return_confirmation(false);
74             return false;
75         }
76         
77         if (!(card.trust_anchor.is_empty() || card.trust_anchor.get_anchor_type() == TrustAnchor.TrustAnchorType.SERVER_CERT)) {
78             logger.warn(@"execute: Trust anchor type for NAI $nai is not empty or SERVER_CERT; returning true.");
79             return_confirmation(true);
80             return false;
81         }
82
83         logger.trace("execute: expected cert='%s'; fingerprint='%s'".printf(card.trust_anchor.server_cert, fingerprint));
84         if (card.trust_anchor.server_cert == fingerprint) {
85             logger.trace(@"execute: Fingerprint for $nai matches stored value; returning true.");
86             return_confirmation(true);
87             return false;
88         }
89
90         if (parent_app.headless) {
91             logger.trace(@"execute: Running in headless mode; returning false.");
92             return_confirmation(false);
93             return false;
94         }
95
96         var dialog = new TrustAnchorDialog(card, userid, realm, fingerprint);
97         var response = dialog.run();
98         dialog.destroy();
99         bool is_confirmed = (response == ResponseType.OK);
100
101         if (is_confirmed) {
102             logger.trace(@"execute: Fingerprint confirmed; updating stored value.");
103
104             card.trust_anchor.update_server_fingerprint(fingerprint);
105             parent_app.model.update_card(card);
106         }            
107
108         return_confirmation(is_confirmed);
109
110         /* This function works as a GSourceFunc, so it can be passed to
111          * the main loop from other threads
112          */
113         return false;
114     }
115
116     private void return_confirmation(bool confirmed) {
117         return_if_fail(callback != null);
118
119         this.confirmed = confirmed;
120         logger.trace(@"return_confirmation: confirmed=$confirmed");
121
122         // Send back the confirmation (we can't directly run the
123         // callback because we may be being called from a 'yield')
124         GLib.Idle.add(
125             () => {
126                 logger.trace("return_confirmation[Idle handler]: invoking callback");
127                 callback(this);
128                 return false;
129             }
130             );
131     }
132 }
133
134
135
136 class TrustAnchorDialog : Dialog
137 {
138     private static Gdk.Color white = make_color(65535, 65535, 65535);
139
140     public bool complete = false;
141
142     public TrustAnchorDialog(IdCard card,
143                              string userid,
144                              string realm,
145                              string fingerprint)
146     {
147         string server_ta_label_text = _("Server’s trust anchor certificate (SHA-256 fingerprint):");
148
149         this.set_title(_("Trust Anchor"));
150         this.set_modal(true);
151 //        this.set_transient_for(parent);
152         set_bg_color(this);
153
154         this.add_buttons(_("Cancel"), ResponseType.CANCEL,
155                          _("Confirm"), ResponseType.OK);
156
157         this.set_default_response(ResponseType.CANCEL);
158
159         var content_area = this.get_content_area();
160         ((Box) content_area).set_spacing(12);
161         set_bg_color(content_area);
162
163         Label dialog_label = new Label("");
164         dialog_label.set_alignment(0, 0);
165
166         string label_markup;
167         if (card.trust_anchor.server_cert == "") {
168             label_markup = "<span font-weight='heavy'>" 
169             + _("You are using this identity for the first time with the following trust anchor:") + "</span>";
170         }
171         else {
172             // The server's fingerprint isn't what we're expecting this server to provide.
173             label_markup = "<span font-weight='heavy'>" +
174             _("WARNING: The certificate we received for the authentication server for %s").printf(card.issuer)
175             + _(" is different than expected.  Either the server certificate has changed, or an")
176             + _(" attack may be underway.  If you proceed to the wrong server, your login credentials may be compromised.")
177             + "</span>";
178         }
179
180         dialog_label.set_markup(label_markup);
181         dialog_label.set_line_wrap(true);
182         dialog_label.set_width_chars(60);
183                                                    
184         var user_label = new Label(_("Username: ") + userid);
185         user_label.set_alignment(0, 0.5f);
186
187         var realm_label = new Label(_("Realm: ") + realm);
188         realm_label.set_alignment(0, 0.5f);
189
190         string confirm_text = _("\nPlease check with your realm administrator for the correct fingerprint")
191         + _(" for your authentication server.  If it matches the above fingerprint,")
192         + _(" confirm the change.  If not, then cancel.");
193
194         Label confirm_label = new Label(confirm_text);
195         confirm_label.set_alignment(0, 0.5f);
196         confirm_label.set_line_wrap(true);
197         confirm_label.set_width_chars(60);
198
199         var trust_anchor_display = make_ta_fingerprint_widget(fingerprint, server_ta_label_text);
200
201         var vbox = new VBox(false, 0);
202         vbox.set_border_width(6);
203         vbox.pack_start(dialog_label, true, true, 12);
204         vbox.pack_start(user_label, true, true, 2);
205         vbox.pack_start(realm_label, true, true, 2);
206         vbox.pack_start(trust_anchor_display, true, true, 0);
207         vbox.pack_start(confirm_label, true, true, 12);
208
209         ((Container) content_area).add(vbox);
210
211         this.set_border_width(6);
212         this.set_resizable(false);
213
214         this.response.connect(on_response);
215
216         this.show_all();
217     }
218
219     private void on_response(Dialog source, int response_id)
220     {
221         switch (response_id) {
222         case ResponseType.OK:
223             complete = true;
224             break;
225         case ResponseType.CANCEL:
226             complete = true;
227             break;
228         }
229     }
230 }