]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/Certificate.java
Change cert variable names id -> serial for identification.
[gigi.git] / src / org / cacert / gigi / Certificate.java
index c6924132cdc8c0445e3bcbee2e88d34112251b31..378b3a7001fc10e39fc01589eee794ce5ca2356a 100644 (file)
@@ -17,18 +17,43 @@ import org.cacert.gigi.util.KeyStorage;
 
 public class Certificate {
        private int id;
+       private int ownerId;
        private int serial;
        private String dn;
        private String md;
        private String csrName;
        private String crtName;
        private String csr = null;
-       public Certificate(String dn, String md, String csr) {
+
+       public Certificate(int ownerId, String dn, String md, String csr) {
+               this.ownerId = ownerId;
                this.dn = dn;
                this.md = md;
                this.csr = csr;
        }
 
+       public Certificate(int serial) {
+               try {
+                       PreparedStatement ps = DatabaseConnection.getInstance().prepare(
+                               "SELECT id,subject, md, csr_name, crt_name,memid FROM `emailcerts` WHERE serial=?");
+                       ps.setInt(1, serial);
+                       ResultSet rs = ps.executeQuery();
+                       if (!rs.next()) {
+                               throw new IllegalArgumentException("Invalid mid " + serial);
+                       }
+                       this.id = rs.getInt(1);
+                       dn = rs.getString(2);
+                       md = rs.getString(3);
+                       csrName = rs.getString(4);
+                       crtName = rs.getString(5);
+                       ownerId = rs.getInt(6);
+                       this.serial = serial;
+                       rs.close();
+               } catch (SQLException e) {
+                       e.printStackTrace();
+               }
+       }
+
        public enum CertificateStatus {
                /**
                 * This certificate is not in the database, has no id and only exists as
@@ -66,6 +91,7 @@ public class Certificate {
                private CertificateStatus(boolean unstable) {
                        this.unstable = unstable;
                }
+
                /**
                 * Checks, iff this certificate stage will be left by signer actions.
                 * 
@@ -77,14 +103,13 @@ public class Certificate {
                }
 
        }
+
        public CertificateStatus getStatus() throws SQLException {
                if (id == 0) {
                        return CertificateStatus.DRAFT;
                }
-               PreparedStatement searcher = DatabaseConnection
-                               .getInstance()
-                               .prepare(
-                                               "SELECT crt_name, created, revoked, warning FROM emailcerts WHERE id=?");
+               PreparedStatement searcher = DatabaseConnection.getInstance().prepare(
+                       "SELECT crt_name, created, revoked, warning FROM emailcerts WHERE id=?");
                searcher.setInt(1, id);
                ResultSet rs = searcher.executeQuery();
                if (!rs.next()) {
@@ -98,12 +123,10 @@ public class Certificate {
                        return CertificateStatus.SIGNING;
                }
                crtName = rs.getString(1);
-               System.out.println(crtName);
                if (rs.getTime(2) != null && rs.getTime(3) == null) {
                        return CertificateStatus.ISSUED;
                }
-               if (rs.getTime(2) != null
-                               && rs.getString(3).equals("1970-01-01 00:00:00.0")) {
+               if (rs.getTime(2) != null && rs.getString(3).equals("1970-01-01 00:00:00.0")) {
                        return CertificateStatus.BEING_REVOKED;
                }
                return CertificateStatus.REVOKED;
@@ -114,12 +137,11 @@ public class Certificate {
                        if (getStatus() != CertificateStatus.DRAFT) {
                                throw new IllegalStateException();
                        }
-                       PreparedStatement inserter = DatabaseConnection
-                                       .getInstance()
-                                       .prepare(
-                                                       "INSERT INTO emailcerts SET md=?, subject=?, coll_found=0, crt_name=''");
+                       PreparedStatement inserter = DatabaseConnection.getInstance().prepare(
+                               "INSERT INTO emailcerts SET md=?, subject=?, coll_found=0, crt_name='', memid=?");
                        inserter.setString(1, md);
                        inserter.setString(2, dn);
+                       inserter.setInt(3, ownerId);
                        inserter.execute();
                        id = DatabaseConnection.lastInsertId(inserter);
                        File csrFile = KeyStorage.locateCsr(id);
@@ -128,8 +150,8 @@ public class Certificate {
                        fos.write(csr.getBytes());
                        fos.close();
 
-                       PreparedStatement updater = DatabaseConnection.getInstance()
-                                       .prepare("UPDATE emailcerts SET csr_name=? WHERE id=?");
+                       PreparedStatement updater = DatabaseConnection.getInstance().prepare(
+                               "UPDATE emailcerts SET csr_name=? WHERE id=?");
                        updater.setString(1, csrName);
                        updater.setInt(2, id);
                        updater.execute();
@@ -138,6 +160,7 @@ public class Certificate {
                }
 
        }
+
        public boolean waitFor(int max) throws SQLException, InterruptedException {
                long start = System.currentTimeMillis();
                while (getStatus().isUnstable()) {
@@ -148,15 +171,14 @@ public class Certificate {
                }
                return true;
        }
+
        public void revoke() {
                try {
                        if (getStatus() != CertificateStatus.ISSUED) {
                                throw new IllegalStateException();
                        }
-                       PreparedStatement inserter = DatabaseConnection
-                                       .getInstance()
-                                       .prepare(
-                                                       "UPDATE emailcerts SET revoked = '1970-01-01' WHERE id=?");
+                       PreparedStatement inserter = DatabaseConnection.getInstance().prepare(
+                               "UPDATE emailcerts SET revoked = '1970-01-01' WHERE id=?");
                        inserter.setInt(1, id);
                        inserter.execute();
                } catch (SQLException e) {
@@ -165,8 +187,7 @@ public class Certificate {
 
        }
 
-       public X509Certificate cert() throws IOException, GeneralSecurityException,
-                       SQLException {
+       public X509Certificate cert() throws IOException, GeneralSecurityException, SQLException {
                CertificateStatus status = getStatus();
                if (status != CertificateStatus.ISSUED) {
                        throw new IllegalStateException(status + " is not wanted here.");
@@ -184,20 +205,29 @@ public class Certificate {
                }
                return crt;
        }
+
        public Certificate renew() {
                return null;
        }
+
        public int getId() {
                return id;
        }
+
        public int getSerial() {
                return serial;
        }
+
        public String getDistinguishedName() {
                return dn;
        }
+
        public String getMessageDigest() {
                return md;
        }
 
+       public int getOwnerId() {
+               return ownerId;
+       }
+
 }