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 {
51 private string _ca_cert = "";
52 private string _subject = "";
53 private string _subject_alt = "";
54 private string _server_cert = "";
55 private string _datetime_added = "";
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) {
62 _ca_cert = fixup(ca_cert);
63 _server_cert = fixup(server_cert);
64 _subject = fixup(subject);
65 _subject_alt = fixup(subject_alt);
67 // If we're reading from store, this will be overridden (see set_datetime_added)
71 public TrustAnchor.empty() {
75 public string ca_cert {
81 public string subject {
87 public string subject_alt {
94 public string server_cert {
100 public string datetime_added {
102 return _datetime_added;
106 public bool is_empty() {
107 return ca_cert == "" && server_cert == "";
110 public TrustAnchorType get_anchor_type() {
111 return (server_cert != "" ? TrustAnchorType.SERVER_CERT
112 : (ca_cert != "" ? TrustAnchorType.CA_CERT : TrustAnchorType.EMPTY));
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 internal void update_server_fingerprint(string fingerprint) {
126 this._server_cert = fingerprint;
127 string ta_datetime_added = TrustAnchor.format_datetime_now();
128 this.set_datetime_added(ta_datetime_added);
131 public int Compare(TrustAnchor other)
133 if (this.ca_cert != other.ca_cert) {
134 // IdCard.logger.trace("TrustAnchor.Compare: this.ca_cert='%s'; other.ca_cert='%s'".printf(this.ca_cert, other.ca_cert));
137 if (this.subject != other.subject) {
138 // IdCard.logger.trace("TrustAnchor.Compare: this.subject='%s'; other.subject='%s'".printf(this.subject, other.subject));
141 if (this.subject_alt != other.subject_alt) {
142 // IdCard.logger.trace("TrustAnchor.Compare: this.subject_alt='%s'; other.subject_alt='%s'".printf(this.subject_alt, other.subject_alt));
145 if (this.server_cert != other.server_cert) {
146 // IdCard.logger.trace("TrustAnchor.Compare: this.server_cert=%s'; other.server_cert='%s'".printf(this.server_cert, other.server_cert));
150 // Do not compare the datetime_added fields; it's not essential.
155 public string? get_expiration_date(out string? err_out=null)
157 if (&err_out != null) {
161 if (this.ca_cert == "") {
162 if (&err_out != null) {
163 err_out = "Trust anchor does not have a ca_certificate";
168 string cert = this.ca_cert;
171 uchar[] binary = Base64.decode(cert);
172 IdCard.logger.trace("get_expiration_date: encoded length=%d; decoded length=%d".printf(cert.length, binary.length));
175 string err = (string) get_cert_valid_before(binary, binary.length, buf, 64);
177 IdCard.logger.error(@"get_expiration_date: get_cert_valid_before returned '$err'");
178 if (&err_out != null) {
184 string date = (string) buf;
185 IdCard.logger.trace(@"get_expiration_date: get_cert_valid_before returned '$date'");
194 public string pattern;
195 public string always_confirm;
196 public int Compare(Rule other) {
197 if (this.pattern != other.pattern)
199 if (this.always_confirm != other.always_confirm)
205 public class IdCard : Object
207 internal static MoonshotLogger logger = get_logger("IdCard");
209 public const string NO_IDENTITY = "No Identity";
211 private string _username = "";
212 private string _issuer = "";
214 public string display_name { get; set; default = ""; }
216 public string username {
226 public string issuer {
236 private void update_nai() {
237 _nai = username + "@" + issuer;
241 private unowned string _password;
242 public string password {
244 return (_password!=null) ? _password : "";
247 if (_password != null) {
248 GnomeKeyring.memory_free((void *)_password);
252 _password = GnomeKeyring.memory_strdup(value);
256 public string password { get; set; default = null; }
259 private Rule[] _rules = new Rule[0];
260 public Rule[] rules {
262 internal set {_rules = value ?? new Rule[0] ;}
265 private ArrayList<string> _services = new ArrayList<string>();
267 internal ArrayList<string> services {
268 get {return _services;}
271 // Returns the list of services as a string, using the given separator.
272 internal string get_services_string(string sep) {
273 if (_services.is_empty) {
277 // ArrayList.to_array() seems to be unreliable -- it causes segfaults
278 // semi-randomly. (Possibly because it returns an unowned ref?)
279 // return string.joinv(sep, _services.to_array());
281 // This problem may be related to the one noted elsewhere as the
282 // "Centos vala array property bug".
284 string[] svcs = new string[_services.size];
285 for (int i = 0; i < _services.size; i++) {
286 svcs[i] = _services[i];
289 return string.joinv(sep, svcs);
292 internal void update_services(string[] services) {
295 // Doesn't exist in older versions of libgee:
296 // _services.add_all_array(services);
298 if (services != null) {
299 foreach (string s in services) {
305 internal void update_services_from_list(ArrayList<string> services) {
306 if (services == this._services) {
307 // Don't try to update from self.
313 if (services != null) {
314 _services.add_all(services);
319 public bool temporary {get; set; default = false; }
321 private TrustAnchor _trust_anchor = new TrustAnchor.empty();
322 public TrustAnchor trust_anchor {
324 return _trust_anchor;
328 // For use by storage implementations.
329 internal void set_trust_anchor_from_store(TrustAnchor ta) {
333 internal void clear_trust_anchor() {
334 _trust_anchor = new TrustAnchor.empty();
337 public string nai { public get; private set;}
339 public bool store_password { get; set; default = false; }
341 // uuid is currently used only for debugging. Must be unique, even between cards with same nai and display name.
343 public get {return _uuid;}
345 private string _uuid = generate_uuid();
347 internal static string generate_uuid() {
348 uint32 rand1 = Random.next_int();
349 uint32 rand2 = Random.next_int();
350 return "%08X.%08X::%s".printf(rand1, rand2, TrustAnchor.format_datetime_now());
353 public bool is_no_identity()
355 return (display_name == NO_IDENTITY);
358 public enum DiffFlags {
368 public int Compare(IdCard other)
371 if (this.display_name != other.display_name)
372 diff |= 1 << DiffFlags.DISPLAY_NAME;
374 if (this.username != other.username)
375 diff |= 1 << DiffFlags.USERNAME;
377 if (this.password != other.password)
378 diff |= 1 << DiffFlags.PASSWORD;
380 if (this.issuer != other.issuer)
381 diff |= 1 << DiffFlags.ISSUER;
383 if (CompareRules(this.rules, other.rules)!=0)
384 diff |= 1 << DiffFlags.RULES;
386 if (CompareStringArrayList(this._services, other._services)!=0)
387 diff |= 1 << DiffFlags.SERVICES;
389 if (this.trust_anchor.Compare(other.trust_anchor)!=0)
390 diff |= 1 << DiffFlags.TRUST_ANCHOR;
392 // stdout.printf("Diff Flags: %x\n", diff);
393 if (this.display_name == other.display_name && diff != 0) {
394 logger.trace("Compare: Two IDs with display_name '%s', but diff_flags=%0x".printf(this.display_name, diff));
399 public static IdCard NewNoIdentity()
401 IdCard card = new IdCard();
402 card.display_name = NO_IDENTITY;
410 internal void add_rule(Rule rule) {
415 public int CompareRules(Rule[] a, Rule[] b)
417 if (a.length != b.length) {
421 for (int i = 0; i < a.length; i++) {
422 if (a[i].Compare(b[i]) != 0) {
429 public int CompareStringArrayList(ArrayList<string> a, ArrayList<string> b)
431 if (a.size != b.size) {
435 for (int i = 0; i < a.size; i++) {