Implemented "Clear Trust Anchor" button in Edit Identity 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 extern char* get_cert_valid_before(char* cert, int certlen, char* datebuf, int buflen);
36
37
38 // A TrustAnchor object can be imported or installed via the API, but cannot
39 // be modified by the user, other than being cleared. Hence the fields are read-only.
40 public class TrustAnchor : Object
41 {
42     private static const string CERT_HEADER = "-----BEGIN CERTIFICATE-----";
43     private static const string CERT_FOOTER = "-----END CERTIFICATE-----";
44
45     public enum TrustAnchorType {
46         CA_CERT,
47         SERVER_CERT
48     }
49  
50     private string _ca_cert = "";
51     private string _subject = "";
52     private string _subject_alt = "";
53     private string _server_cert = "";
54
55     public bool user_verified = false;
56
57     public TrustAnchor(string ca_cert, string server_cert, string subject, string subject_alt, bool user_verified) {
58         _ca_cert = ca_cert;
59         _server_cert = server_cert;
60         _subject = subject;
61         _subject_alt = subject_alt;
62         this.user_verified = user_verified;
63     }
64
65     public TrustAnchor.empty() {
66         _ca_cert = "";
67         _server_cert = "";
68         _subject = "";
69         _subject_alt = "";
70         this.user_verified = false;
71     }
72
73
74     public string ca_cert {
75         get {
76             return _ca_cert;
77         }
78     }
79
80     public string subject {
81         get {
82             return _subject;
83         }
84     }
85
86     public string subject_alt  {
87         get {
88             return _subject_alt;
89         }
90     }
91
92
93     public string server_cert {
94         get {
95             return _server_cert;
96         }
97     }
98
99     public bool is_empty() {
100         return ca_cert == "" && subject == "" && subject_alt == "" && server_cert == "";
101     }
102
103     public TrustAnchorType get_anchor_type() {
104         return server_cert == "" ? TrustAnchorType.CA_CERT : TrustAnchorType.SERVER_CERT;
105     }
106
107     public int Compare(TrustAnchor other)
108     {
109         if (this.ca_cert != other.ca_cert)
110             return 1;
111         if (this.subject != other.subject)
112             return 1;
113         if (this.subject_alt != other.subject_alt)
114             return 1;
115         if (this.server_cert != other.server_cert)
116             return 1;
117         if (this.user_verified != other.user_verified)
118             return 1;
119         return 0;
120     }
121
122     public string? get_expiration_date(out string? err_out=null)
123     {
124         if (this.ca_cert == "") {
125             if (&err_out != null) {
126                 err_out = "Trust anchor does not have a ca_certificate";
127                 return null;
128             }
129         }
130
131         string cert = this.ca_cert;
132         cert.chomp();
133         if (cert.substring(0, CERT_HEADER.length) != CERT_HEADER) {
134             cert = CERT_HEADER + "\n" + cert;
135         }
136         if (cert.substring(0, -CERT_FOOTER.length) != CERT_FOOTER) {
137             cert += "\n" + CERT_FOOTER;
138         }
139         cert += "\n";
140
141         IdCard.logger.trace(@"get_expiration_date: Sending " + cert);
142
143         char buf[64];
144         string err = (string) get_cert_valid_before(cert, cert.length, buf, 64);
145         if (err != "") {
146             IdCard.logger.error(@"get_expiration_date: get_cert_valid_before returned '$err'");
147             if (&err_out != null) {
148                 err_out = err;
149             }
150             return null;
151         }
152             
153         string date = (string) buf;
154         IdCard.logger.trace(@"get_expiration_date: get_cert_valid_before returned '$date'");
155
156         return date;
157     }
158 }
159
160
161 public struct Rule
162 {
163     public string pattern;
164     public string always_confirm;
165     public int Compare(Rule other) {
166         if (this.pattern != other.pattern)
167             return 1;
168         if (this.always_confirm != other.always_confirm)
169             return 1;
170         return 0;
171     }
172 }
173
174 public class IdCard : Object
175 {
176     internal static MoonshotLogger logger = get_logger("IdCard");
177
178     public const string NO_IDENTITY = "No Identity";
179
180     private string _nai;
181   
182     public string display_name { get; set; default = ""; }
183   
184     public string username { get; set; default = ""; }
185 #if GNOME_KEYRING
186     private unowned string _password;
187     public string password {
188         get {
189             return (_password!=null) ? _password : "";
190         }
191         set {
192             if (_password != null) {
193                 GnomeKeyring.memory_free((void *)_password);
194                 _password = null;
195             }
196             if (value != null)
197                 _password = GnomeKeyring.memory_strdup(value); 
198         }
199     }
200 #else
201     public string password { get; set; default = null; }
202 #endif
203
204     public string issuer { get; set; default = ""; }
205   
206     private Rule[] _rules = new Rule[0];
207     public Rule[] rules {
208         get {return _rules;}
209         internal set {_rules = value ?? new Rule[0] ;}
210     }
211
212     private ArrayList<string> _services = new ArrayList<string>();
213
214     internal ArrayList<string> services {
215          get {return  _services;}
216     }
217
218     // Returns the list of services as a string, using the given separator.
219     internal string get_services_string(string sep) {
220         if (_services.is_empty) {
221             return "";
222         }
223
224         // ArrayList.to_array() seems to be unreliable -- it causes segfaults 
225         // semi-randomly. (Possibly because it returns an unowned ref?)
226         // return string.joinv(sep, _services.to_array());
227         // 
228         // This problem may be related to the one noted elsewhere as the
229         // "Centos vala array property bug".
230
231         string[] svcs = new string[_services.size];
232         for (int i = 0; i < _services.size; i++) {
233             svcs[i] = _services[i];
234         }
235
236         return string.joinv(sep, svcs);
237     }
238
239     internal void update_services(string[] services) {
240         _services.clear();
241
242         // Doesn't exist in older versions of libgee:
243         // _services.add_all_array(services);
244
245         if (services != null) {
246             foreach (string s in services) {
247                 _services.add(s);
248             }
249         }
250     } 
251
252     internal void update_services_from_list(ArrayList<string> services) {
253         if (services == this._services) {
254             // Don't try to update from self.
255             return;
256         }
257
258         _services.clear();
259
260         if (services != null) {
261             _services.add_all(services);
262         }
263     } 
264
265
266     public bool temporary {get; set; default = false; }
267
268     private TrustAnchor _trust_anchor = new TrustAnchor.empty();
269     public TrustAnchor trust_anchor  { 
270         get {
271             return _trust_anchor;
272         }
273     }
274
275     // For use by storage implementations.
276     internal void set_trust_anchor_from_store(TrustAnchor ta) {
277         _trust_anchor = ta;
278     }
279
280     internal void clear_trust_anchor() {
281         _trust_anchor = new TrustAnchor.empty();
282     }
283   
284     public unowned string nai { get {  _nai = username + "@" + issuer; return _nai;}}
285
286     public bool store_password { get; set; default = false; }
287
288     public bool is_no_identity() 
289     {
290         return (display_name == NO_IDENTITY);
291     }
292
293     public enum DiffFlags {
294         DISPLAY_NAME,
295         USERNAME,
296         PASSWORD,
297         ISSUER,
298         RULES,
299         SERVICES,
300         TRUST_ANCHOR;
301     }
302
303     public int Compare(IdCard other)
304     {
305         int diff = 0;
306         if (this.display_name != other.display_name)
307             diff |= 1 << DiffFlags.DISPLAY_NAME;
308
309         if (this.username != other.username)
310             diff |= 1 << DiffFlags.USERNAME;
311
312         if (this.password != other.password)
313             diff |= 1 << DiffFlags.PASSWORD;
314
315         if (this.issuer != other.issuer)
316             diff |= 1 << DiffFlags.ISSUER;
317
318         if (CompareRules(this.rules, other.rules)!=0)
319             diff |= 1 << DiffFlags.RULES;
320
321         if (CompareStringArrayList(this._services, other._services)!=0)
322             diff |= 1 << DiffFlags.SERVICES;
323
324         if (this.trust_anchor.Compare(other.trust_anchor)!=0)
325             diff |= 1 << DiffFlags.TRUST_ANCHOR;
326
327         // stdout.printf("Diff Flags: %x\n", diff);
328         if (this.display_name == other.display_name && diff != 0) {
329             logger.trace("Compare: Two IDs with display_name '%s', but diff_flags=%0x".printf(this.display_name, diff));
330         }
331         return diff;
332     }
333
334     public static IdCard NewNoIdentity() 
335     { 
336         IdCard card = new IdCard();
337         card.display_name = NO_IDENTITY;
338         return card;
339     }
340
341     ~IdCard() {
342         password = null;
343     }
344
345     internal void add_rule(Rule rule) {
346         _rules += rule;
347     }
348 }
349
350 public int CompareRules(Rule[] a, Rule[] b)
351 {
352     if (a.length != b.length) {
353         return 1;
354     }
355
356     for (int i = 0; i < a.length; i++) {
357         if (a[i].Compare(b[i]) != 0) {
358             return 1;
359         }
360     }
361     return 0;
362 }
363
364 public int CompareStringArrayList(ArrayList<string> a, ArrayList<string> b)
365 {
366     if (a.size != b.size) {
367         return 1;
368     }
369
370     for (int i = 0; i < a.size; i++) {
371         if (a[i] != b[i]) {
372             return 1;
373         }
374     }
375     return 0;
376 }