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