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