Fixed compilation error and error handling.
[moonshot-ui.git] / src / moonshot-id.vala
index 77afd4e..46fd088 100644 (file)
 
 using Gee;
 
+extern char* get_cert_valid_before(char* cert, int certlen, char* datebuf, int buflen);
+
+
 public class TrustAnchor : Object
 {
-    public static const string TYPE_CA_CERT = _("CA Certificate");
-    public static const string TYPE_ENTERPRISE = _("Enterprise provisioned");
+    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 = "";
@@ -84,8 +91,8 @@ public class TrustAnchor : Object
         return ca_cert == "" && subject == "" && subject_alt == "" && server_cert == "";
     }
 
-    public string get_anchor_type() {
-        return server_cert == "" ? TYPE_CA_CERT : TYPE_ENTERPRISE;
+    public TrustAnchorType get_anchor_type() {
+        return server_cert == "" ? TrustAnchorType.CA_CERT : TrustAnchorType.SERVER_CERT;
     }
 
     public int Compare(TrustAnchor other)
@@ -100,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;
@@ -117,7 +162,7 @@ public struct Rule
 
 public class IdCard : Object
 {
-    static MoonshotLogger logger = get_logger("IdCard");
+    internal static MoonshotLogger logger = get_logger("IdCard");
 
     public const string NO_IDENTITY = "No Identity";