Decode the CA Certificate from binary, not from PEM format.
[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
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
134         uchar[] binary = Base64.decode(cert);
135         IdCard.logger.trace("get_expiration_date: encoded length=%d; decoded length=%d".printf(cert.length, binary.length));
136
137         char buf[64];
138         string err = (string) get_cert_valid_before(binary, binary.length, buf, 64);
139         if (err != "") {
140             IdCard.logger.error(@"get_expiration_date: get_cert_valid_before returned '$err'");
141             if (&err_out != null) {
142                 err_out = err;
143             }
144             return null;
145         }
146             
147         string date = (string) buf;
148         IdCard.logger.trace(@"get_expiration_date: get_cert_valid_before returned '$date'");
149
150         return date;
151     }
152 }
153
154
155 public struct Rule
156 {
157     public string pattern;
158     public string always_confirm;
159     public int Compare(Rule other) {
160         if (this.pattern != other.pattern)
161             return 1;
162         if (this.always_confirm != other.always_confirm)
163             return 1;
164         return 0;
165     }
166 }
167
168 public class IdCard : Object
169 {
170     internal static MoonshotLogger logger = get_logger("IdCard");
171
172     public const string NO_IDENTITY = "No Identity";
173
174     private string _nai;
175   
176     public string display_name { get; set; default = ""; }
177   
178     public string username { get; set; default = ""; }
179 #if GNOME_KEYRING
180     private unowned string _password;
181     public string password {
182         get {
183             return (_password!=null) ? _password : "";
184         }
185         set {
186             if (_password != null) {
187                 GnomeKeyring.memory_free((void *)_password);
188                 _password = null;
189             }
190             if (value != null)
191                 _password = GnomeKeyring.memory_strdup(value); 
192         }
193     }
194 #else
195     public string password { get; set; default = null; }
196 #endif
197
198     public string issuer { get; set; default = ""; }
199   
200     private Rule[] _rules = new Rule[0];
201     public Rule[] rules {
202         get {return _rules;}
203         internal set {_rules = value ?? new Rule[0] ;}
204     }
205
206     private ArrayList<string> _services = new ArrayList<string>();
207
208     internal ArrayList<string> services {
209          get {return  _services;}
210     }
211
212     // Returns the list of services as a string, using the given separator.
213     internal string get_services_string(string sep) {
214         if (_services.is_empty) {
215             return "";
216         }
217
218         // ArrayList.to_array() seems to be unreliable -- it causes segfaults 
219         // semi-randomly. (Possibly because it returns an unowned ref?)
220         // return string.joinv(sep, _services.to_array());
221         // 
222         // This problem may be related to the one noted elsewhere as the
223         // "Centos vala array property bug".
224
225         string[] svcs = new string[_services.size];
226         for (int i = 0; i < _services.size; i++) {
227             svcs[i] = _services[i];
228         }
229
230         return string.joinv(sep, svcs);
231     }
232
233     internal void update_services(string[] services) {
234         _services.clear();
235
236         // Doesn't exist in older versions of libgee:
237         // _services.add_all_array(services);
238
239         if (services != null) {
240             foreach (string s in services) {
241                 _services.add(s);
242             }
243         }
244     } 
245
246     internal void update_services_from_list(ArrayList<string> services) {
247         if (services == this._services) {
248             // Don't try to update from self.
249             return;
250         }
251
252         _services.clear();
253
254         if (services != null) {
255             _services.add_all(services);
256         }
257     } 
258
259
260     public bool temporary {get; set; default = false; }
261
262     private TrustAnchor _trust_anchor = new TrustAnchor.empty();
263     public TrustAnchor trust_anchor  { 
264         get {
265             return _trust_anchor;
266         }
267     }
268
269     // For use by storage implementations.
270     internal void set_trust_anchor_from_store(TrustAnchor ta) {
271         _trust_anchor = ta;
272     }
273
274     internal void clear_trust_anchor() {
275         _trust_anchor = new TrustAnchor.empty();
276     }
277   
278     public unowned string nai { get {  _nai = username + "@" + issuer; return _nai;}}
279
280     public bool store_password { get; set; default = false; }
281
282     public bool is_no_identity() 
283     {
284         return (display_name == NO_IDENTITY);
285     }
286
287     public enum DiffFlags {
288         DISPLAY_NAME,
289         USERNAME,
290         PASSWORD,
291         ISSUER,
292         RULES,
293         SERVICES,
294         TRUST_ANCHOR;
295     }
296
297     public int Compare(IdCard other)
298     {
299         int diff = 0;
300         if (this.display_name != other.display_name)
301             diff |= 1 << DiffFlags.DISPLAY_NAME;
302
303         if (this.username != other.username)
304             diff |= 1 << DiffFlags.USERNAME;
305
306         if (this.password != other.password)
307             diff |= 1 << DiffFlags.PASSWORD;
308
309         if (this.issuer != other.issuer)
310             diff |= 1 << DiffFlags.ISSUER;
311
312         if (CompareRules(this.rules, other.rules)!=0)
313             diff |= 1 << DiffFlags.RULES;
314
315         if (CompareStringArrayList(this._services, other._services)!=0)
316             diff |= 1 << DiffFlags.SERVICES;
317
318         if (this.trust_anchor.Compare(other.trust_anchor)!=0)
319             diff |= 1 << DiffFlags.TRUST_ANCHOR;
320
321         // stdout.printf("Diff Flags: %x\n", diff);
322         if (this.display_name == other.display_name && diff != 0) {
323             logger.trace("Compare: Two IDs with display_name '%s', but diff_flags=%0x".printf(this.display_name, diff));
324         }
325         return diff;
326     }
327
328     public static IdCard NewNoIdentity() 
329     { 
330         IdCard card = new IdCard();
331         card.display_name = NO_IDENTITY;
332         return card;
333     }
334
335     ~IdCard() {
336         password = null;
337     }
338
339     internal void add_rule(Rule rule) {
340         _rules += rule;
341     }
342 }
343
344 public int CompareRules(Rule[] a, Rule[] b)
345 {
346     if (a.length != b.length) {
347         return 1;
348     }
349
350     for (int i = 0; i < a.length; i++) {
351         if (a[i].Compare(b[i]) != 0) {
352             return 1;
353         }
354     }
355     return 0;
356 }
357
358 public int CompareStringArrayList(ArrayList<string> a, ArrayList<string> b)
359 {
360     if (a.size != b.size) {
361         return 1;
362     }
363
364     for (int i = 0; i < a.size; i++) {
365         if (a[i] != b[i]) {
366             return 1;
367         }
368     }
369     return 0;
370 }