Fixed compilation error and error handling.
[moonshot-ui.git] / src / moonshot-id.vala
index 2dc9ac2..46fd088 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2014, JANET(UK)
+ * Copyright (c) 2011-2016, JANET(UK)
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
 */
+
+using Gee;
+
+extern char* get_cert_valid_before(char* cert, int certlen, char* datebuf, int buflen);
+
+
 public class TrustAnchor : Object
 {
-    public string ca_cert {get; set; default = "";}
-    public string subject {get; set; default = "";}
-    public string subject_alt  {get; set; default = "";}
-    public string server_cert  {get; set; default = "";}
+    private static const string CERT_HEADER = "-----BEGIN CERTIFICATE-----";
+    private static const string CERT_FOOTER = "-----END CERTIFICATE-----";
+
+    public enum TrustAnchorType {
+        CA_CERT,
+        SERVER_CERT
+    }
+    private string _ca_cert = "";
+    private string _subject = "";
+    private string _subject_alt = "";
+    private string _server_cert = "";
+
+    public string ca_cert {
+        get {
+            return _ca_cert;
+        }
+        set {
+            _ca_cert = (value ?? "");
+        }
+    }
+
+    public string subject {
+        get {
+            return _subject;
+        }
+        set {
+            _subject = (value ?? "");
+        }
+    }
+
+    public string subject_alt  {
+        get {
+            return _subject_alt;
+        }
+        set {
+            _subject_alt = (value ?? "");
+        }
+    }
+
+
+    public string server_cert {
+        get {
+            return _server_cert;
+        }
+        set {
+            _server_cert = (value ?? "");
+        }
+    }
+
+    public bool is_empty() {
+        return ca_cert == "" && subject == "" && subject_alt == "" && server_cert == "";
+    }
+
+    public TrustAnchorType get_anchor_type() {
+        return server_cert == "" ? TrustAnchorType.CA_CERT : TrustAnchorType.SERVER_CERT;
+    }
+
     public int Compare(TrustAnchor other)
     {
         if (this.ca_cert != other.ca_cert)
@@ -47,8 +107,46 @@ public class TrustAnchor : Object
             return 1;
         return 0;
     }
+
+    public string? get_expiration_date(out string? err_out=null)
+    {
+        if (this.ca_cert == "") {
+            if (&err_out != null) {
+                err_out = "Trust anchor does not have a ca_certificate";
+                return null;
+            }
+        }
+
+        string cert = this.ca_cert;
+        cert.chomp();
+        if (cert.substring(0, CERT_HEADER.length) != CERT_HEADER) {
+            cert = CERT_HEADER + "\n" + cert;
+        }
+        if (cert.substring(0, -CERT_FOOTER.length) != CERT_FOOTER) {
+            cert += "\n" + CERT_FOOTER;
+        }
+        cert += "\n";
+
+        IdCard.logger.trace(@"get_expiration_date: Sending " + cert);
+
+        char buf[64];
+        string err = (string) get_cert_valid_before(cert, cert.length, buf, 64);
+        if (err != "") {
+            IdCard.logger.error(@"get_expiration_date: get_cert_valid_before returned '$err'");
+            if (&err_out != null) {
+                err_out = err;
+            }
+            return null;
+        }
+            
+        string date = (string) buf;
+        IdCard.logger.trace(@"get_expiration_date: get_cert_valid_before returned '$date'");
+
+        return date;
+    }
 }
 
+
 public struct Rule
 {
     public string pattern;
@@ -64,6 +162,8 @@ public struct Rule
 
 public class IdCard : Object
 {
+    internal static MoonshotLogger logger = get_logger("IdCard");
+
     public const string NO_IDENTITY = "No Identity";
 
     private string _nai;
@@ -92,18 +192,66 @@ public class IdCard : Object
 
     public string issuer { get; set; default = ""; }
   
-    private Rule[] _rules = {};
+    private Rule[] _rules = new Rule[0];
     public Rule[] rules {
         get {return _rules;}
-        internal set {_rules = value ?? {};}
+        internal set {_rules = value ?? new Rule[0] ;}
     }
 
-    private string[] _services = {};
-    public string[] services {
-        get {return _services;}
-        internal set {_services = value ?? {};}
+    private ArrayList<string> _services = new ArrayList<string>();
+
+    internal ArrayList<string> services {
+         get {return  _services;}
     }
 
+    // Returns the list of services as a string, using the given separator.
+    internal string get_services_string(string sep) {
+        if (_services.is_empty) {
+            return "";
+        }
+
+        // ArrayList.to_array() seems to be unreliable -- it causes segfaults 
+        // semi-randomly. (Possibly because it returns an unowned ref?)
+        // return string.joinv(sep, _services.to_array());
+        // 
+        // This problem may be related to the one noted elsewhere as the
+        // "Centos vala array property bug".
+
+        string[] svcs = new string[_services.size];
+        for (int i = 0; i < _services.size; i++) {
+            svcs[i] = _services[i];
+        }
+
+        return string.joinv(sep, svcs);
+    }
+
+    internal void update_services(string[] services) {
+        _services.clear();
+
+        // Doesn't exist in older versions of libgee:
+        // _services.add_all_array(services);
+
+        if (services != null) {
+            foreach (string s in services) {
+                _services.add(s);
+            }
+        }
+    } 
+
+    internal void update_services_from_list(ArrayList<string> services) {
+        if (services == this._services) {
+            // Don't try to update from self.
+            return;
+        }
+
+        _services.clear();
+
+        if (services != null) {
+            _services.add_all(services);
+        }
+    } 
+
+
     public bool temporary {get; set; default = false; }
 
     public TrustAnchor trust_anchor  { get; set; default = new TrustAnchor (); }
@@ -112,7 +260,7 @@ public class IdCard : Object
 
     public bool store_password { get; set; default = false; }
 
-    public bool IsNoIdentity() 
+    public bool is_no_identity() 
     {
         return (display_name == NO_IDENTITY);
     }
@@ -145,7 +293,7 @@ public class IdCard : Object
         if (CompareRules(this.rules, other.rules)!=0)
             diff |= 1 << DiffFlags.RULES;
 
-        if (CompareStringArray(this.services, other.services)!=0)
+        if (CompareStringArrayList(this._services, other._services)!=0)
             diff |= 1 << DiffFlags.SERVICES;
 
         if (this.trust_anchor.Compare(other.trust_anchor)!=0)
@@ -169,10 +317,6 @@ public class IdCard : Object
     internal void add_rule(Rule rule) {
         _rules += rule;
     }
-
-    internal void add_service(string service) {
-        _services += service;
-    }
 }
 
 public int CompareRules(Rule[] a, Rule[] b)
@@ -189,13 +333,13 @@ public int CompareRules(Rule[] a, Rule[] b)
     return 0;
 }
 
-public int CompareStringArray(string[] a, string [] b)
+public int CompareStringArrayList(ArrayList<string> a, ArrayList<string> b)
 {
-    if (a.length != b.length) {
+    if (a.size != b.size) {
         return 1;
     }
 
-    for (int i = 0; i < a.length; i++) {
+    for (int i = 0; i < a.size; i++) {
         if (a[i] != b[i]) {
             return 1;
         }