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