First pass at supporting trust anchors in IdCard dialog
[moonshot-ui.git] / src / moonshot-id.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
33 using Gee;
34
35 public class TrustAnchor : Object
36 {
37     public enum TrustAnchorType {
38         CA_CERT,
39         SERVER_CERT
40     }
41  
42     private string _ca_cert = "";
43     private string _subject = "";
44     private string _subject_alt = "";
45     private string _server_cert = "";
46
47     public string ca_cert {
48         get {
49             return _ca_cert;
50         }
51         set {
52             _ca_cert = (value ?? "");
53         }
54     }
55
56     public string subject {
57         get {
58             return _subject;
59         }
60         set {
61             _subject = (value ?? "");
62         }
63     }
64
65     public string subject_alt  {
66         get {
67             return _subject_alt;
68         }
69         set {
70             _subject_alt = (value ?? "");
71         }
72     }
73
74
75     public string server_cert {
76         get {
77             return _server_cert;
78         }
79         set {
80             _server_cert = (value ?? "");
81         }
82     }
83
84     public bool is_empty() {
85         return ca_cert == "" && subject == "" && subject_alt == "" && server_cert == "";
86     }
87
88     public TrustAnchorType get_anchor_type() {
89         return server_cert == "" ? TrustAnchorType.CA_CERT : TrustAnchorType.SERVER_CERT;
90     }
91
92     public int Compare(TrustAnchor other)
93     {
94         if (this.ca_cert != other.ca_cert)
95             return 1;
96         if (this.subject != other.subject)
97             return 1;
98         if (this.subject_alt != other.subject_alt)
99             return 1;
100         if (this.server_cert != other.server_cert)
101             return 1;
102         return 0;
103     }
104
105     public string? get_expiration_date()
106     {
107         if (this.ca_cert == "") {
108             return null;
109         }
110
111         //!!TODO read expiration date
112         return "";
113     }
114 }
115
116 public struct Rule
117 {
118     public string pattern;
119     public string always_confirm;
120     public int Compare(Rule other) {
121         if (this.pattern != other.pattern)
122             return 1;
123         if (this.always_confirm != other.always_confirm)
124             return 1;
125         return 0;
126     }
127 }
128
129 public class IdCard : Object
130 {
131     static MoonshotLogger logger = get_logger("IdCard");
132
133     public const string NO_IDENTITY = "No Identity";
134
135     private string _nai;
136   
137     public string display_name { get; set; default = ""; }
138   
139     public string username { get; set; default = ""; }
140 #if GNOME_KEYRING
141     private unowned string _password;
142     public string password {
143         get {
144             return (_password!=null) ? _password : "";
145         }
146         set {
147             if (_password != null) {
148                 GnomeKeyring.memory_free((void *)_password);
149                 _password = null;
150             }
151             if (value != null)
152                 _password = GnomeKeyring.memory_strdup(value); 
153         }
154     }
155 #else
156     public string password { get; set; default = null; }
157 #endif
158
159     public string issuer { get; set; default = ""; }
160   
161     private Rule[] _rules = new Rule[0];
162     public Rule[] rules {
163         get {return _rules;}
164         internal set {_rules = value ?? new Rule[0] ;}
165     }
166
167     private ArrayList<string> _services = new ArrayList<string>();
168
169     internal ArrayList<string> services {
170          get {return  _services;}
171     }
172
173     // Returns the list of services as a string, using the given separator.
174     internal string get_services_string(string sep) {
175         if (_services.is_empty) {
176             return "";
177         }
178
179         // ArrayList.to_array() seems to be unreliable -- it causes segfaults 
180         // semi-randomly. (Possibly because it returns an unowned ref?)
181         // return string.joinv(sep, _services.to_array());
182         // 
183         // This problem may be related to the one noted elsewhere as the
184         // "Centos vala array property bug".
185
186         string[] svcs = new string[_services.size];
187         for (int i = 0; i < _services.size; i++) {
188             svcs[i] = _services[i];
189         }
190
191         return string.joinv(sep, svcs);
192     }
193
194     internal void update_services(string[] services) {
195         _services.clear();
196
197         // Doesn't exist in older versions of libgee:
198         // _services.add_all_array(services);
199
200         if (services != null) {
201             foreach (string s in services) {
202                 _services.add(s);
203             }
204         }
205     } 
206
207     internal void update_services_from_list(ArrayList<string> services) {
208         if (services == this._services) {
209             // Don't try to update from self.
210             return;
211         }
212
213         _services.clear();
214
215         if (services != null) {
216             _services.add_all(services);
217         }
218     } 
219
220
221     public bool temporary {get; set; default = false; }
222
223     public TrustAnchor trust_anchor  { get; set; default = new TrustAnchor (); }
224   
225     public unowned string nai { get {  _nai = username + "@" + issuer; return _nai;}}
226
227     public bool store_password { get; set; default = false; }
228
229     public bool is_no_identity() 
230     {
231         return (display_name == NO_IDENTITY);
232     }
233
234     public enum DiffFlags {
235         DISPLAY_NAME,
236         USERNAME,
237         PASSWORD,
238         ISSUER,
239         RULES,
240         SERVICES,
241         TRUST_ANCHOR;
242     }
243
244     public int Compare(IdCard other)
245     {
246         int diff = 0;
247         if (this.display_name != other.display_name)
248             diff |= 1 << DiffFlags.DISPLAY_NAME;
249
250         if (this.username != other.username)
251             diff |= 1 << DiffFlags.USERNAME;
252
253         if (this.password != other.password)
254             diff |= 1 << DiffFlags.PASSWORD;
255
256         if (this.issuer != other.issuer)
257             diff |= 1 << DiffFlags.ISSUER;
258
259         if (CompareRules(this.rules, other.rules)!=0)
260             diff |= 1 << DiffFlags.RULES;
261
262         if (CompareStringArrayList(this._services, other._services)!=0)
263             diff |= 1 << DiffFlags.SERVICES;
264
265         if (this.trust_anchor.Compare(other.trust_anchor)!=0)
266             diff |= 1 << DiffFlags.TRUST_ANCHOR;
267
268         // stdout.printf("Diff Flags: %x\n", diff);
269         return diff;
270     }
271
272     public static IdCard NewNoIdentity() 
273     { 
274         IdCard card = new IdCard();
275         card.display_name = NO_IDENTITY;
276         return card;
277     }
278
279     ~IdCard() {
280         password = null;
281     }
282
283     internal void add_rule(Rule rule) {
284         _rules += rule;
285     }
286 }
287
288 public int CompareRules(Rule[] a, Rule[] b)
289 {
290     if (a.length != b.length) {
291         return 1;
292     }
293
294     for (int i = 0; i < a.length; i++) {
295         if (a[i].Compare(b[i]) != 0) {
296             return 1;
297         }
298     }
299     return 0;
300 }
301
302 public int CompareStringArrayList(ArrayList<string> a, ArrayList<string> b)
303 {
304     if (a.size != b.size) {
305         return 1;
306     }
307
308     for (int i = 0; i < a.size; i++) {
309         if (a[i] != b[i]) {
310             return 1;
311         }
312     }
313     return 0;
314 }