2 * Copyright (c) 2011-2016, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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
35 public class TrustAnchor : Object
37 public static const string TYPE_CA_CERT = _("CA Certificate");
38 public static const string TYPE_ENTERPRISE = _("Enterprise provisioned");
41 private string _ca_cert = "";
42 private string _subject = "";
43 private string _subject_alt = "";
44 private string _server_cert = "";
46 public string ca_cert {
51 _ca_cert = (value ?? "");
55 public string subject {
60 _subject = (value ?? "");
64 public string subject_alt {
69 _subject_alt = (value ?? "");
74 public string server_cert {
79 _server_cert = (value ?? "");
83 public bool is_empty() {
84 return ca_cert == "" && subject == "" && subject_alt == "" && server_cert == "";
87 public string get_anchor_type() {
88 return server_cert == "" ? TYPE_CA_CERT : TYPE_ENTERPRISE;
91 public int Compare(TrustAnchor other)
93 if (this.ca_cert != other.ca_cert)
95 if (this.subject != other.subject)
97 if (this.subject_alt != other.subject_alt)
99 if (this.server_cert != other.server_cert)
107 public string pattern;
108 public string always_confirm;
109 public int Compare(Rule other) {
110 if (this.pattern != other.pattern)
112 if (this.always_confirm != other.always_confirm)
118 public class IdCard : Object
120 static MoonshotLogger logger = get_logger("IdCard");
122 public const string NO_IDENTITY = "No Identity";
126 public string display_name { get; set; default = ""; }
128 public string username { get; set; default = ""; }
130 private unowned string _password;
131 public string password {
133 return (_password!=null) ? _password : "";
136 if (_password != null) {
137 GnomeKeyring.memory_free((void *)_password);
141 _password = GnomeKeyring.memory_strdup(value);
145 public string password { get; set; default = null; }
148 public string issuer { get; set; default = ""; }
150 private Rule[] _rules = new Rule[0];
151 public Rule[] rules {
153 internal set {_rules = value ?? new Rule[0] ;}
156 private ArrayList<string> _services = new ArrayList<string>();
158 internal ArrayList<string> services {
159 get {return _services;}
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) {
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());
172 // This problem may be related to the one noted elsewhere as the
173 // "Centos vala array property bug".
175 string[] svcs = new string[_services.size];
176 for (int i = 0; i < _services.size; i++) {
177 svcs[i] = _services[i];
180 return string.joinv(sep, svcs);
183 internal void update_services(string[] services) {
186 // Doesn't exist in older versions of libgee:
187 // _services.add_all_array(services);
189 if (services != null) {
190 foreach (string s in services) {
196 internal void update_services_from_list(ArrayList<string> services) {
197 if (services == this._services) {
198 // Don't try to update from self.
204 if (services != null) {
205 _services.add_all(services);
210 public bool temporary {get; set; default = false; }
212 public TrustAnchor trust_anchor { get; set; default = new TrustAnchor (); }
214 public unowned string nai { get { _nai = username + "@" + issuer; return _nai;}}
216 public bool store_password { get; set; default = false; }
218 public bool is_no_identity()
220 return (display_name == NO_IDENTITY);
223 public enum DiffFlags {
233 public int Compare(IdCard other)
236 if (this.display_name != other.display_name)
237 diff |= 1 << DiffFlags.DISPLAY_NAME;
239 if (this.username != other.username)
240 diff |= 1 << DiffFlags.USERNAME;
242 if (this.password != other.password)
243 diff |= 1 << DiffFlags.PASSWORD;
245 if (this.issuer != other.issuer)
246 diff |= 1 << DiffFlags.ISSUER;
248 if (CompareRules(this.rules, other.rules)!=0)
249 diff |= 1 << DiffFlags.RULES;
251 if (CompareStringArrayList(this._services, other._services)!=0)
252 diff |= 1 << DiffFlags.SERVICES;
254 if (this.trust_anchor.Compare(other.trust_anchor)!=0)
255 diff |= 1 << DiffFlags.TRUST_ANCHOR;
257 // stdout.printf("Diff Flags: %x\n", diff);
261 public static IdCard NewNoIdentity()
263 IdCard card = new IdCard();
264 card.display_name = NO_IDENTITY;
272 internal void add_rule(Rule rule) {
277 public int CompareRules(Rule[] a, Rule[] b)
279 if (a.length != b.length) {
283 for (int i = 0; i < a.length; i++) {
284 if (a[i].Compare(b[i]) != 0) {
291 public int CompareStringArrayList(ArrayList<string> a, ArrayList<string> b)
293 if (a.size != b.size) {
297 for (int i = 0; i < a.size; i++) {