Cleaning up compilation warnings
[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(uchar* inbuf, int inlen, char* outbuf, int outlen);
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     private string _datetime_added = "";
55     public bool user_verified = false;
56
57     private static string fixup (string s) {
58         return (s == null ? "" : s.strip());
59     }
60
61     public TrustAnchor(string ca_cert, string server_cert, string subject, string subject_alt, bool user_verified) {
62         _ca_cert = fixup(ca_cert);
63         _server_cert = fixup(server_cert);
64         _subject = fixup(subject);
65         _subject_alt = fixup(subject_alt);
66         this.user_verified = user_verified;
67
68         // If we're reading from store, this will be overridden (see set_datetime_added)
69         _datetime_added = "";
70     }
71
72     public TrustAnchor.empty() {
73     }
74
75
76     public string ca_cert {
77         get {
78             return _ca_cert;
79         }
80     }
81
82     public string subject {
83         get {
84             return _subject;
85         }
86     }
87
88     public string subject_alt  {
89         get {
90             return _subject_alt;
91         }
92     }
93
94
95     public string server_cert {
96         get {
97             return _server_cert;
98         }
99     }
100
101     public string datetime_added {
102         get {
103             return _datetime_added;
104         }
105     }
106
107     public bool is_empty() {
108         return ca_cert == "" && subject == "" && subject_alt == "" && server_cert == "";
109     }
110
111     public TrustAnchorType get_anchor_type() {
112         return server_cert == "" ? TrustAnchorType.CA_CERT : TrustAnchorType.SERVER_CERT;
113     }
114
115     internal void set_datetime_added(string datetime) {
116         _datetime_added = fixup(datetime);
117     }
118
119     internal static string format_datetime_now() {
120         DateTime now = new DateTime.now_utc();
121         string dt = now.format("%b %d %T %Y %Z");
122         return dt;
123     }
124
125     public int Compare(TrustAnchor other)
126     {
127         if (this.ca_cert != other.ca_cert) {
128             // IdCard.logger.trace("TrustAnchor.Compare: this.ca_cert='%s'; other.ca_cert='%s'".printf(this.ca_cert, other.ca_cert));
129             return 1;
130         }
131         if (this.subject != other.subject) {
132             // IdCard.logger.trace("TrustAnchor.Compare: this.subject='%s'; other.subject='%s'".printf(this.subject, other.subject));
133             return 1;
134         }
135         if (this.subject_alt != other.subject_alt) {
136             // IdCard.logger.trace("TrustAnchor.Compare: this.subject_alt='%s'; other.subject_alt='%s'".printf(this.subject_alt, other.subject_alt));
137             return 1;
138         }
139         if (this.server_cert != other.server_cert) {
140             // IdCard.logger.trace("TrustAnchor.Compare: this.server_cert=%s'; other.server_cert='%s'".printf(this.server_cert, other.server_cert));
141             return 1;
142         }
143
144         // Do not compare the user_verified and datetime_added fields; they are not essential.
145
146         return 0;
147     }
148
149     public string? get_expiration_date(out string? err_out=null)
150     {
151         if (&err_out != null) {
152             err_out = null;
153         }
154
155         if (this.ca_cert == "") {
156             if (&err_out != null) {
157                 err_out = "Trust anchor does not have a ca_certificate";
158                 return null;
159             }
160         }
161
162         string cert = this.ca_cert;
163         cert.chomp();
164
165         uchar[] binary = Base64.decode(cert);
166         IdCard.logger.trace("get_expiration_date: encoded length=%d; decoded length=%d".printf(cert.length, binary.length));
167
168         char buf[64];
169         string err = (string) get_cert_valid_before(binary, binary.length, buf, 64);
170         if (err != "") {
171             IdCard.logger.error(@"get_expiration_date: get_cert_valid_before returned '$err'");
172             if (&err_out != null) {
173                 err_out = err;
174             }
175             return null;
176         }
177             
178         string date = (string) buf;
179         IdCard.logger.trace(@"get_expiration_date: get_cert_valid_before returned '$date'");
180
181         return date;
182     }
183 }
184
185
186 public struct Rule
187 {
188     public string pattern;
189     public string always_confirm;
190     public int Compare(Rule other) {
191         if (this.pattern != other.pattern)
192             return 1;
193         if (this.always_confirm != other.always_confirm)
194             return 1;
195         return 0;
196     }
197 }
198
199 public class IdCard : Object
200 {
201     internal static MoonshotLogger logger = get_logger("IdCard");
202
203     public const string NO_IDENTITY = "No Identity";
204
205     private string _username = "";
206     private string _issuer = "";
207
208     public string display_name { get; set; default = ""; }
209   
210     public string username { 
211         public get {
212             return _username;
213         }
214         public set {
215             _username = value;
216             update_nai();
217         }
218     }
219
220     public string issuer { 
221         public get {
222             return _issuer;
223         }
224         public set {
225             _issuer = value;
226             update_nai();
227         }
228     }
229
230     private void update_nai() {
231         _nai = username + "@" + issuer;
232     }
233
234 #if GNOME_KEYRING
235     private unowned string _password;
236     public string password {
237         get {
238             return (_password!=null) ? _password : "";
239         }
240         set {
241             if (_password != null) {
242                 GnomeKeyring.memory_free((void *)_password);
243                 _password = null;
244             }
245             if (value != null)
246                 _password = GnomeKeyring.memory_strdup(value); 
247         }
248     }
249 #else
250     public string password { get; set; default = null; }
251 #endif
252
253     private Rule[] _rules = new Rule[0];
254     public Rule[] rules {
255         get {return _rules;}
256         internal set {_rules = value ?? new Rule[0] ;}
257     }
258
259     private ArrayList<string> _services = new ArrayList<string>();
260
261     internal ArrayList<string> services {
262          get {return  _services;}
263     }
264
265     // Returns the list of services as a string, using the given separator.
266     internal string get_services_string(string sep) {
267         if (_services.is_empty) {
268             return "";
269         }
270
271         // ArrayList.to_array() seems to be unreliable -- it causes segfaults 
272         // semi-randomly. (Possibly because it returns an unowned ref?)
273         // return string.joinv(sep, _services.to_array());
274         // 
275         // This problem may be related to the one noted elsewhere as the
276         // "Centos vala array property bug".
277
278         string[] svcs = new string[_services.size];
279         for (int i = 0; i < _services.size; i++) {
280             svcs[i] = _services[i];
281         }
282
283         return string.joinv(sep, svcs);
284     }
285
286     internal void update_services(string[] services) {
287         _services.clear();
288
289         // Doesn't exist in older versions of libgee:
290         // _services.add_all_array(services);
291
292         if (services != null) {
293             foreach (string s in services) {
294                 _services.add(s);
295             }
296         }
297     } 
298
299     internal void update_services_from_list(ArrayList<string> services) {
300         if (services == this._services) {
301             // Don't try to update from self.
302             return;
303         }
304
305         _services.clear();
306
307         if (services != null) {
308             _services.add_all(services);
309         }
310     } 
311
312
313     public bool temporary {get; set; default = false; }
314
315     private TrustAnchor _trust_anchor = new TrustAnchor.empty();
316     public TrustAnchor trust_anchor  { 
317         get {
318             return _trust_anchor;
319         }
320     }
321
322     // For use by storage implementations.
323     internal void set_trust_anchor_from_store(TrustAnchor ta) {
324         _trust_anchor = ta;
325     }
326
327     internal void clear_trust_anchor() {
328         _trust_anchor = new TrustAnchor.empty();
329     }
330   
331     public string nai { public get; private set;}
332
333     public bool store_password { get; set; default = false; }
334
335     public bool is_no_identity() 
336     {
337         return (display_name == NO_IDENTITY);
338     }
339
340     public enum DiffFlags {
341         DISPLAY_NAME,
342         USERNAME,
343         PASSWORD,
344         ISSUER,
345         RULES,
346         SERVICES,
347         TRUST_ANCHOR;
348     }
349
350     public int Compare(IdCard other)
351     {
352         int diff = 0;
353         if (this.display_name != other.display_name)
354             diff |= 1 << DiffFlags.DISPLAY_NAME;
355
356         if (this.username != other.username)
357             diff |= 1 << DiffFlags.USERNAME;
358
359         if (this.password != other.password)
360             diff |= 1 << DiffFlags.PASSWORD;
361
362         if (this.issuer != other.issuer)
363             diff |= 1 << DiffFlags.ISSUER;
364
365         if (CompareRules(this.rules, other.rules)!=0)
366             diff |= 1 << DiffFlags.RULES;
367
368         if (CompareStringArrayList(this._services, other._services)!=0)
369             diff |= 1 << DiffFlags.SERVICES;
370
371         if (this.trust_anchor.Compare(other.trust_anchor)!=0)
372             diff |= 1 << DiffFlags.TRUST_ANCHOR;
373
374         // stdout.printf("Diff Flags: %x\n", diff);
375         if (this.display_name == other.display_name && diff != 0) {
376             logger.trace("Compare: Two IDs with display_name '%s', but diff_flags=%0x".printf(this.display_name, diff));
377         }
378         return diff;
379     }
380
381     public static IdCard NewNoIdentity() 
382     { 
383         IdCard card = new IdCard();
384         card.display_name = NO_IDENTITY;
385         return card;
386     }
387
388     ~IdCard() {
389         password = null;
390     }
391
392     internal void add_rule(Rule rule) {
393         _rules += rule;
394     }
395 }
396
397 public int CompareRules(Rule[] a, Rule[] b)
398 {
399     if (a.length != b.length) {
400         return 1;
401     }
402
403     for (int i = 0; i < a.length; i++) {
404         if (a[i].Compare(b[i]) != 0) {
405             return 1;
406         }
407     }
408     return 0;
409 }
410
411 public int CompareStringArrayList(ArrayList<string> a, ArrayList<string> b)
412 {
413     if (a.size != b.size) {
414         return 1;
415     }
416
417     for (int i = 0; i < a.size; i++) {
418         if (a[i] != b[i]) {
419             return 1;
420         }
421     }
422     return 0;
423 }