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 extern char* get_cert_valid_before(uchar* inbuf, int inlen, char* outbuf, int outlen);
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
42 private static const string CERT_HEADER = "-----BEGIN CERTIFICATE-----";
43 private static const string CERT_FOOTER = "-----END CERTIFICATE-----";
45 public enum TrustAnchorType {
50 private string _ca_cert = "";
51 private string _subject = "";
52 private string _subject_alt = "";
53 private string _server_cert = "";
54 private string _datetime_added = "";
55 public bool user_verified = false;
57 private static string fixup (string s) {
58 return (s == null ? "" : s.strip());
61 public TrustAnchor(string ca_cert, string server_cert, string subject, string subject_alt, bool user_verified) {
62 _ca_cert = fixup(ca_cert);
63 _server_cert = fixup(server_cert);
64 _subject = fixup(subject);
65 _subject_alt = fixup(subject_alt);
66 this.user_verified = user_verified;
68 // If we're reading from store, this will be overridden (see set_datetime_added)
72 public TrustAnchor.empty() {
76 public string ca_cert {
82 public string subject {
88 public string subject_alt {
95 public string server_cert {
101 public string datetime_added {
103 return _datetime_added;
107 public bool is_empty() {
108 return ca_cert == "" && subject == "" && subject_alt == "" && server_cert == "";
111 public TrustAnchorType get_anchor_type() {
112 return server_cert == "" ? TrustAnchorType.CA_CERT : TrustAnchorType.SERVER_CERT;
115 internal void set_datetime_added(string datetime) {
116 _datetime_added = fixup(datetime);
119 internal static string format_datetime_now() {
120 DateTime now = new DateTime.now_utc();
121 string dt = now.format("%b %d %T %Y %Z");
125 public int Compare(TrustAnchor other)
127 if (this.ca_cert != other.ca_cert) {
128 // IdCard.logger.trace("TrustAnchor.Compare: this.ca_cert='%s'; other.ca_cert='%s'".printf(this.ca_cert, other.ca_cert));
131 if (this.subject != other.subject) {
132 // IdCard.logger.trace("TrustAnchor.Compare: this.subject='%s'; other.subject='%s'".printf(this.subject, other.subject));
135 if (this.subject_alt != other.subject_alt) {
136 // IdCard.logger.trace("TrustAnchor.Compare: this.subject_alt='%s'; other.subject_alt='%s'".printf(this.subject_alt, other.subject_alt));
139 if (this.server_cert != other.server_cert) {
140 // IdCard.logger.trace("TrustAnchor.Compare: this.server_cert=%s'; other.server_cert='%s'".printf(this.server_cert, other.server_cert));
144 // Do not compare the user_verified and datetime_added fields; they are not essential.
149 public string? get_expiration_date(out string? err_out=null)
151 if (this.ca_cert == "") {
152 if (&err_out != null) {
153 err_out = "Trust anchor does not have a ca_certificate";
158 string cert = this.ca_cert;
161 uchar[] binary = Base64.decode(cert);
162 IdCard.logger.trace("get_expiration_date: encoded length=%d; decoded length=%d".printf(cert.length, binary.length));
165 string err = (string) get_cert_valid_before(binary, binary.length, buf, 64);
167 IdCard.logger.error(@"get_expiration_date: get_cert_valid_before returned '$err'");
168 if (&err_out != null) {
174 string date = (string) buf;
175 IdCard.logger.trace(@"get_expiration_date: get_cert_valid_before returned '$date'");
184 public string pattern;
185 public string always_confirm;
186 public int Compare(Rule other) {
187 if (this.pattern != other.pattern)
189 if (this.always_confirm != other.always_confirm)
195 public class IdCard : Object
197 internal static MoonshotLogger logger = get_logger("IdCard");
199 public const string NO_IDENTITY = "No Identity";
201 private string _username = "";
202 private string _issuer = "";
204 public string display_name { get; set; default = ""; }
206 public string username {
216 public string issuer {
226 private void update_nai() {
227 _nai = username + "@" + issuer;
231 private unowned string _password;
232 public string password {
234 return (_password!=null) ? _password : "";
237 if (_password != null) {
238 GnomeKeyring.memory_free((void *)_password);
242 _password = GnomeKeyring.memory_strdup(value);
246 public string password { get; set; default = null; }
249 private Rule[] _rules = new Rule[0];
250 public Rule[] rules {
252 internal set {_rules = value ?? new Rule[0] ;}
255 private ArrayList<string> _services = new ArrayList<string>();
257 internal ArrayList<string> services {
258 get {return _services;}
261 // Returns the list of services as a string, using the given separator.
262 internal string get_services_string(string sep) {
263 if (_services.is_empty) {
267 // ArrayList.to_array() seems to be unreliable -- it causes segfaults
268 // semi-randomly. (Possibly because it returns an unowned ref?)
269 // return string.joinv(sep, _services.to_array());
271 // This problem may be related to the one noted elsewhere as the
272 // "Centos vala array property bug".
274 string[] svcs = new string[_services.size];
275 for (int i = 0; i < _services.size; i++) {
276 svcs[i] = _services[i];
279 return string.joinv(sep, svcs);
282 internal void update_services(string[] services) {
285 // Doesn't exist in older versions of libgee:
286 // _services.add_all_array(services);
288 if (services != null) {
289 foreach (string s in services) {
295 internal void update_services_from_list(ArrayList<string> services) {
296 if (services == this._services) {
297 // Don't try to update from self.
303 if (services != null) {
304 _services.add_all(services);
309 public bool temporary {get; set; default = false; }
311 private TrustAnchor _trust_anchor = new TrustAnchor.empty();
312 public TrustAnchor trust_anchor {
314 return _trust_anchor;
318 // For use by storage implementations.
319 internal void set_trust_anchor_from_store(TrustAnchor ta) {
323 internal void clear_trust_anchor() {
324 _trust_anchor = new TrustAnchor.empty();
327 public string nai { public get; private set;}
329 public bool store_password { get; set; default = false; }
331 public bool is_no_identity()
333 return (display_name == NO_IDENTITY);
336 public enum DiffFlags {
346 public int Compare(IdCard other)
349 if (this.display_name != other.display_name)
350 diff |= 1 << DiffFlags.DISPLAY_NAME;
352 if (this.username != other.username)
353 diff |= 1 << DiffFlags.USERNAME;
355 if (this.password != other.password)
356 diff |= 1 << DiffFlags.PASSWORD;
358 if (this.issuer != other.issuer)
359 diff |= 1 << DiffFlags.ISSUER;
361 if (CompareRules(this.rules, other.rules)!=0)
362 diff |= 1 << DiffFlags.RULES;
364 if (CompareStringArrayList(this._services, other._services)!=0)
365 diff |= 1 << DiffFlags.SERVICES;
367 if (this.trust_anchor.Compare(other.trust_anchor)!=0)
368 diff |= 1 << DiffFlags.TRUST_ANCHOR;
370 // stdout.printf("Diff Flags: %x\n", diff);
371 if (this.display_name == other.display_name && diff != 0) {
372 logger.trace("Compare: Two IDs with display_name '%s', but diff_flags=%0x".printf(this.display_name, diff));
377 public static IdCard NewNoIdentity()
379 IdCard card = new IdCard();
380 card.display_name = NO_IDENTITY;
388 internal void add_rule(Rule rule) {
393 public int CompareRules(Rule[] a, Rule[] b)
395 if (a.length != b.length) {
399 for (int i = 0; i < a.length; i++) {
400 if (a[i].Compare(b[i]) != 0) {
407 public int CompareStringArrayList(ArrayList<string> a, ArrayList<string> b)
409 if (a.size != b.size) {
413 for (int i = 0; i < a.size; i++) {