]> WPIA git - gigi.git/commitdiff
Implement a basic certificate class.
authorFelix Dörre <felix@dogcraft.de>
Tue, 8 Jul 2014 17:14:55 +0000 (19:14 +0200)
committerFelix Dörre <felix@dogcraft.de>
Tue, 8 Jul 2014 20:49:39 +0000 (22:49 +0200)
src/org/cacert/gigi/Certificate.java [new file with mode: 0644]

diff --git a/src/org/cacert/gigi/Certificate.java b/src/org/cacert/gigi/Certificate.java
new file mode 100644 (file)
index 0000000..43e6b87
--- /dev/null
@@ -0,0 +1,104 @@
+package org.cacert.gigi;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import org.cacert.gigi.database.DatabaseConnection;
+
+public class Certificate {
+       int id;
+       int serial;
+       String dn;
+       String md;
+       String csrName;
+       String crtName;
+
+       // created, modified, revoked, expire
+       public enum CertificateStatus {
+               DRAFT(false), BEEING_ISSUED(true), ISSUED(false), BEEING_REVOKED(true), REVOKED(
+                               false);
+
+               boolean unstable;
+
+               private CertificateStatus(boolean unstable) {
+                       this.unstable = unstable;
+               }
+               public boolean isUnstable() {
+                       return unstable;
+               }
+
+       }
+       public CertificateStatus getStatus() throws SQLException {
+               if (id == 0) {
+                       return CertificateStatus.DRAFT;
+               }
+               PreparedStatement searcher = DatabaseConnection.getInstance().prepare(
+                               "SELECT csr_name, created, revoked FROM emailcerts WHERE id=?");
+               searcher.setInt(1, id);
+               ResultSet rs = searcher.executeQuery();
+               if (!rs.next()) {
+                       throw new IllegalStateException("Certificate not in Database");
+               }
+               if (rs.getString(2) == null) {
+                       return CertificateStatus.BEEING_ISSUED;
+               }
+               csrName = rs.getString(1);
+               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")) {
+                       return CertificateStatus.BEEING_REVOKED;
+               }
+               return CertificateStatus.REVOKED;
+       }
+
+       public void issue() {
+               try {
+                       if (getStatus() != CertificateStatus.DRAFT) {
+                               throw new IllegalStateException();
+                       }
+                       PreparedStatement inserter = DatabaseConnection
+                                       .getInstance()
+                                       .prepare(
+                                                       "INSERT INTO emailcerts SET csr_name =?, md=?, subject='a', coll_found=0, crt_name=''");
+                       inserter.setString(1, csrName);
+                       inserter.setString(2, md);
+                       inserter.execute();
+                       id = DatabaseConnection.lastInsertId(inserter);
+               } catch (SQLException e) {
+                       e.printStackTrace();
+               }
+
+       }
+       public boolean waitFor(int max) throws SQLException, InterruptedException {
+               long start = System.currentTimeMillis();
+               while (getStatus().isUnstable()) {
+                       if (max != 0 && System.currentTimeMillis() - start > max) {
+                               return false;
+                       }
+                       Thread.sleep((long) (2000 + Math.random() * 2000));
+               }
+               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=?");
+                       inserter.setInt(1, id);
+                       inserter.execute();
+               } catch (SQLException e) {
+                       e.printStackTrace();
+               }
+
+       }
+       public Certificate renew() {
+               return null;
+       }
+
+}