First cut at supporting trust anchors
[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 class TrustAnchorDialog : Dialog
35 {
36     private static Gdk.Color white = make_color(65535, 65535, 65535);
37
38     private Entry trust_anchor_entry;
39
40     public bool complete = false;
41
42     public TrustAnchorDialog(IdCard idcard, Window parent)
43     {
44         this.set_title(_("Trust Anchor"));
45         this.set_modal(true);
46         this.set_transient_for(parent);
47         this.modify_bg(StateType.NORMAL, white);
48
49         this.add_buttons(_("Connect"), ResponseType.OK,
50                          _("Cancel"), ResponseType.CANCEL);
51
52         this.set_default_response(ResponseType.OK);
53
54         var content_area = this.get_content_area();
55         ((Box) content_area).set_spacing(12);
56         content_area.modify_bg(StateType.NORMAL, white);
57
58         Label dialog_label = new Label("");
59         dialog_label.set_alignment(0, 0);
60
61         string label_markup = _("<span font-weight='heavy'>You are using this identity for the first time with the following trust anchor:</span>");
62
63         dialog_label.set_markup(label_markup);
64         dialog_label.set_line_wrap(true);
65         dialog_label.set_width_chars(60);
66                                                    
67         var user_label = new Label(_("Username: ") + idcard.username);
68         user_label.set_alignment(0, 0.5f);
69
70         var realm_label = new Label(_("Realm: ") + idcard.issuer);
71         realm_label.set_alignment(0, 0.5f);
72
73         var fingerprint_label = new Label(_("SHA-256 fingerprint:"));
74         fingerprint_label.set_alignment(0, 0.5f);
75
76         var footprint = new TextView();
77         //footprint.activates_default = false;
78         // footprint.set_sensitive(false);
79         footprint.set_editable(false);
80         // footprint.set_text(idcard.trust_anchor.server_cert);
81         var buffer = footprint.get_buffer();
82         buffer.set_text(colonize(idcard.trust_anchor.server_cert), -1);
83         footprint.wrap_mode = WrapMode.WORD_CHAR;
84
85         set_atk_relation(fingerprint_label, footprint, Atk.RelationType.LABEL_FOR);
86
87         var footprint_width_constraint = new ScrolledWindow(null, null);
88         footprint_width_constraint.set_policy(PolicyType.NEVER, PolicyType.NEVER);
89         footprint_width_constraint.set_shadow_type(ShadowType.IN);
90         footprint_width_constraint.set_size_request(400, 60);
91         footprint_width_constraint.add_with_viewport(footprint);
92
93         Label confirm_label = new Label(_("Please confirm that this is the correct trust anchor."));
94         confirm_label.set_alignment(0, 0.5f);
95
96         var vbox = new VBox(false, 0);
97         vbox.set_border_width(6);
98         vbox.pack_start(dialog_label, true, true, 12);
99         vbox.pack_start(user_label, true, true, 2);
100         vbox.pack_start(realm_label, true, true, 2);
101         vbox.pack_start(fingerprint_label, true, true, 2);
102         vbox.pack_start(footprint_width_constraint, true, true, 2);
103         vbox.pack_start(confirm_label, true, true, 12);
104
105         ((Container) content_area).add(vbox);
106
107         this.set_border_width(6);
108         this.set_resizable(false);
109
110         this.response.connect(on_response);
111
112         this.show_all();
113     }
114
115     private void on_response(Dialog source, int response_id)
116     {
117         switch (response_id) {
118         case ResponseType.OK:
119             complete = true;
120             break;
121         case ResponseType.CANCEL:
122             complete = true;
123             break;
124         }
125     }
126
127     // Yeah, it doesn't mean "colonize" the way you might think... :-)
128     private static string colonize(string input) {
129         return_if_fail(input.length % 2 == 0);
130
131         string result = "";
132         int i = 0;
133         while (i < input.length) {
134             if (i > 0) {
135                 result += ":";
136             }
137             result += input[i : i + 2];
138             i += 2;
139         }
140         return result;
141     }
142 }