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 enum TrustAnchorType {
42 private string _ca_cert = "";
43 private string _subject = "";
44 private string _subject_alt = "";
45 private string _server_cert = "";
47 public string ca_cert {
52 _ca_cert = (value ?? "");
56 public string subject {
61 _subject = (value ?? "");
65 public string subject_alt {
70 _subject_alt = (value ?? "");
75 public string server_cert {
80 _server_cert = (value ?? "");
84 public bool is_empty() {
85 return ca_cert == "" && subject == "" && subject_alt == "" && server_cert == "";
88 public TrustAnchorType get_anchor_type() {
89 return server_cert == "" ? TrustAnchorType.CA_CERT : TrustAnchorType.SERVER_CERT;
92 public int Compare(TrustAnchor other)
94 if (this.ca_cert != other.ca_cert)
96 if (this.subject != other.subject)
98 if (this.subject_alt != other.subject_alt)
100 if (this.server_cert != other.server_cert)
108 public string pattern;
109 public string always_confirm;
110 public int Compare(Rule other) {
111 if (this.pattern != other.pattern)
113 if (this.always_confirm != other.always_confirm)
119 public class IdCard : Object
121 static MoonshotLogger logger = get_logger("IdCard");
123 public const string NO_IDENTITY = "No Identity";
127 public string display_name { get; set; default = ""; }
129 public string username { get; set; default = ""; }
131 private unowned string _password;
132 public string password {
134 return (_password!=null) ? _password : "";
137 if (_password != null) {
138 GnomeKeyring.memory_free((void *)_password);
142 _password = GnomeKeyring.memory_strdup(value);
146 public string password { get; set; default = null; }
149 public string issuer { get; set; default = ""; }
151 private Rule[] _rules = new Rule[0];
152 public Rule[] rules {
154 internal set {_rules = value ?? new Rule[0] ;}
157 private ArrayList<string> _services = new ArrayList<string>();
159 internal ArrayList<string> services {
160 get {return _services;}
163 // Returns the list of services as a string, using the given separator.
164 internal string get_services_string(string sep) {
165 if (_services.is_empty) {
169 // ArrayList.to_array() seems to be unreliable -- it causes segfaults
170 // semi-randomly. (Possibly because it returns an unowned ref?)
171 // return string.joinv(sep, _services.to_array());
173 // This problem may be related to the one noted elsewhere as the
174 // "Centos vala array property bug".
176 string[] svcs = new string[_services.size];
177 for (int i = 0; i < _services.size; i++) {
178 svcs[i] = _services[i];
181 return string.joinv(sep, svcs);
184 internal void update_services(string[] services) {
187 // Doesn't exist in older versions of libgee:
188 // _services.add_all_array(services);
190 if (services != null) {
191 foreach (string s in services) {
197 internal void update_services_from_list(ArrayList<string> services) {
198 if (services == this._services) {
199 // Don't try to update from self.
205 if (services != null) {
206 _services.add_all(services);
211 public bool temporary {get; set; default = false; }
213 public TrustAnchor trust_anchor { get; set; default = new TrustAnchor (); }
215 public unowned string nai { get { _nai = username + "@" + issuer; return _nai;}}
217 public bool store_password { get; set; default = false; }
219 public bool is_no_identity()
221 return (display_name == NO_IDENTITY);
224 public enum DiffFlags {
234 public int Compare(IdCard other)
237 if (this.display_name != other.display_name)
238 diff |= 1 << DiffFlags.DISPLAY_NAME;
240 if (this.username != other.username)
241 diff |= 1 << DiffFlags.USERNAME;
243 if (this.password != other.password)
244 diff |= 1 << DiffFlags.PASSWORD;
246 if (this.issuer != other.issuer)
247 diff |= 1 << DiffFlags.ISSUER;
249 if (CompareRules(this.rules, other.rules)!=0)
250 diff |= 1 << DiffFlags.RULES;
252 if (CompareStringArrayList(this._services, other._services)!=0)
253 diff |= 1 << DiffFlags.SERVICES;
255 if (this.trust_anchor.Compare(other.trust_anchor)!=0)
256 diff |= 1 << DiffFlags.TRUST_ANCHOR;
258 // stdout.printf("Diff Flags: %x\n", diff);
262 public static IdCard NewNoIdentity()
264 IdCard card = new IdCard();
265 card.display_name = NO_IDENTITY;
273 internal void add_rule(Rule rule) {
278 public int CompareRules(Rule[] a, Rule[] b)
280 if (a.length != b.length) {
284 for (int i = 0; i < a.length; i++) {
285 if (a[i].Compare(b[i]) != 0) {
292 public int CompareStringArrayList(ArrayList<string> a, ArrayList<string> b)
294 if (a.size != b.size) {
298 for (int i = 0; i < a.size; i++) {