]> WPIA git - gigi.git/blobdiff - src/club/wpia/gigi/dbObjects/Certificate.java
add: text-attachments for certificates
[gigi.git] / src / club / wpia / gigi / dbObjects / Certificate.java
index 8447fd73e39ea04819a7f092d61a227ea669bc60..d5679029c474d51b819049fe0e4083e419375ac3 100644 (file)
@@ -14,6 +14,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map.Entry;
 
 import club.wpia.gigi.GigiApiException;
@@ -28,7 +29,7 @@ import club.wpia.gigi.util.KeyStorage;
 public class Certificate implements IdCachable {
 
     public enum RevocationType implements DBEnum {
-        USER("user"), SUPPORT("support"), PING_TIMEOUT("ping_timeout");
+        USER("user"), SUPPORT("support"), PING_TIMEOUT("ping_timeout"), KEY_COMPROMISE("key_compromise");
 
         private final String dbName;
 
@@ -40,6 +41,19 @@ public class Certificate implements IdCachable {
         public String getDBName() {
             return dbName;
         }
+
+        public static RevocationType fromString(String s) {
+            return valueOf(s.toUpperCase(Locale.ENGLISH));
+        }
+    }
+
+    public enum AttachmentType implements DBEnum {
+        CSR, CRT;
+
+        @Override
+        public String getDBName() {
+            return toString();
+        }
     }
 
     public enum SANType implements DBEnum {
@@ -345,7 +359,13 @@ public class Certificate implements IdCachable {
             throw new IllegalStateException();
         }
         return Job.revoke(this, type);
+    }
 
+    public Job revoke(String challenge, String signature, String message) {
+        if (getStatus() != CertificateStatus.ISSUED) {
+            throw new IllegalStateException();
+        }
+        return Job.revoke(this, challenge, signature, message);
     }
 
     public CACertificate getParent() {
@@ -542,4 +562,35 @@ public class Certificate implements IdCachable {
         }
         return certs;
     }
+
+    public void addAttachment(AttachmentType tp, String data) throws GigiApiException {
+        if (getAttachment(tp) != null) {
+            throw new GigiApiException("Cannot override attachment");
+        }
+        if (data == null) {
+            throw new GigiApiException("Attachment must not be null");
+        }
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `certificateAttachment` SET `certid`=?, `type`=?::`certificateAttachmentType`, `content`=?")) {
+            ps.setInt(1, getId());
+            ps.setEnum(2, tp);
+            ps.setString(3, data);
+            ps.execute();
+        }
+    }
+
+    public String getAttachment(AttachmentType tp) throws GigiApiException {
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `content` FROM `certificateAttachment` WHERE `certid`=? AND `type`=?::`certificateAttachmentType`")) {
+            ps.setInt(1, getId());
+            ps.setEnum(2, tp);
+            GigiResultSet rs = ps.executeQuery();
+            if ( !rs.next()) {
+                return null;
+            }
+            String s = rs.getString(1);
+            if (rs.next()) {
+                throw new GigiApiException("Invalid database state");
+            }
+            return s;
+        }
+    }
 }