a4210aba4f62358d2f5506fe9bc6552370f3cbff
[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 ca_hash;
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 ca_hash)
51     {
52         this.parent_app = parent_app;
53         this.userid = userid;
54         this.realm = realm;
55         this.ca_hash = ca_hash;
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         if (card.trust_anchor.server_cert == ca_hash) {
84             logger.trace(@"execute: Fingerprint for $nai matches stored value; returning true.");
85             return_confirmation(true);
86             return false;
87         }
88
89         var dialog = new TrustAnchorDialog(card, userid, realm, ca_hash);
90         var response = dialog.run();
91         dialog.destroy();
92         bool is_confirmed = (response == ResponseType.OK);
93
94         if (is_confirmed) {
95             logger.trace(@"execute: Fingerprint confirmed; updating stored value.");
96
97             card.trust_anchor.update_server_fingerprint(ca_hash);
98             parent_app.model.update_card(card);
99         }            
100
101         return_confirmation(is_confirmed);
102
103         /* This function works as a GSourceFunc, so it can be passed to
104          * the main loop from other threads
105          */
106         return false;
107     }
108
109     private void return_confirmation(bool confirmed) {
110         return_if_fail(callback != null);
111
112         this.confirmed = confirmed;
113         logger.trace(@"return_confirmation: confirmed=$confirmed");
114
115         // Send back the confirmation (we can't directly run the
116         // callback because we may be being called from a 'yield')
117         GLib.Idle.add(
118             () => {
119                 logger.trace("return_confirmation[Idle handler]: invoking callback");
120                 callback(this);
121                 return false;
122             }
123             );
124     }
125 }
126
127
128
129 class TrustAnchorDialog : Dialog
130 {
131     private static Gdk.Color white = make_color(65535, 65535, 65535);
132
133     public bool complete = false;
134
135     public TrustAnchorDialog(IdCard card,
136                              string userid,
137                              string realm,
138                              string ca_hash)
139     {
140         string server_ta_label_text = null;
141
142         this.set_title(_("Trust Anchor"));
143         this.set_modal(true);
144 //        this.set_transient_for(parent);
145         set_bg_color(this);
146
147         this.add_buttons(_("Cancel"), ResponseType.CANCEL,
148                          _("Confirm"), ResponseType.OK);
149
150         this.set_default_response(ResponseType.OK);
151
152         var content_area = this.get_content_area();
153         ((Box) content_area).set_spacing(12);
154         set_bg_color(content_area);
155
156         Label dialog_label = new Label("");
157         dialog_label.set_alignment(0, 0);
158
159         string label_markup;
160         if (card.trust_anchor.server_cert == "") {
161             label_markup = "<span font-weight='heavy'>" + _("You are using this identity for the first time with the following trust anchor:") + "</span>";
162         }
163         else {
164             // The server's fingerprint isn't what we're expecting this server to provide.
165             label_markup = "<span font-weight='heavy'>" + _("WARNING: This connection may not be secure! ")
166             + _("The server's trust anchor does not match the expected trust anchor for this server.")
167             + "</span>";
168
169             server_ta_label_text = _("Server's trust anchor (SHA-256 fingerprint) :");
170         }
171
172         dialog_label.set_markup(label_markup);
173         dialog_label.set_line_wrap(true);
174         dialog_label.set_width_chars(60);
175                                                    
176         var user_label = new Label(_("Username: ") + userid);
177         user_label.set_alignment(0, 0.5f);
178
179         var realm_label = new Label(_("Realm: ") + realm);
180         realm_label.set_alignment(0, 0.5f);
181
182         Label confirm_label = new Label(_("Please confirm that this is the correct trust anchor."));
183         confirm_label.set_alignment(0, 0.5f);
184
185         var trust_anchor_display = make_ta_fingerprint_widget(ca_hash, server_ta_label_text);
186
187         var vbox = new VBox(false, 0);
188         vbox.set_border_width(6);
189         vbox.pack_start(dialog_label, true, true, 12);
190         vbox.pack_start(user_label, true, true, 2);
191         vbox.pack_start(realm_label, true, true, 2);
192         vbox.pack_start(trust_anchor_display, true, true, 0);
193         vbox.pack_start(confirm_label, true, true, 12);
194
195         if (card.trust_anchor.server_cert != "") {
196             var expected_ta_display = make_ta_fingerprint_widget(card.trust_anchor.server_cert, 
197                                                                  _("Expected trust anchor (SHA-256 fingerprint) :"));
198             vbox.pack_start(expected_ta_display, true, true, 0);
199         }
200
201         ((Container) content_area).add(vbox);
202
203         this.set_border_width(6);
204         this.set_resizable(false);
205
206         this.response.connect(on_response);
207
208         this.show_all();
209     }
210
211     private void on_response(Dialog source, int response_id)
212     {
213         switch (response_id) {
214         case ResponseType.OK:
215             complete = true;
216             break;
217         case ResponseType.CANCEL:
218             complete = true;
219             break;
220         }
221     }
222 }