]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Certificate.java
Implement a basic certificate class.
[gigi.git] / src / org / cacert / gigi / Certificate.java
1 package org.cacert.gigi;
2
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6 import org.cacert.gigi.database.DatabaseConnection;
7
8 public class Certificate {
9         int id;
10         int serial;
11         String dn;
12         String md;
13         String csrName;
14         String crtName;
15
16         // created, modified, revoked, expire
17         public enum CertificateStatus {
18                 DRAFT(false), BEEING_ISSUED(true), ISSUED(false), BEEING_REVOKED(true), REVOKED(
19                                 false);
20
21                 boolean unstable;
22
23                 private CertificateStatus(boolean unstable) {
24                         this.unstable = unstable;
25                 }
26                 public boolean isUnstable() {
27                         return unstable;
28                 }
29
30         }
31         public CertificateStatus getStatus() throws SQLException {
32                 if (id == 0) {
33                         return CertificateStatus.DRAFT;
34                 }
35                 PreparedStatement searcher = DatabaseConnection.getInstance().prepare(
36                                 "SELECT csr_name, created, revoked FROM emailcerts WHERE id=?");
37                 searcher.setInt(1, id);
38                 ResultSet rs = searcher.executeQuery();
39                 if (!rs.next()) {
40                         throw new IllegalStateException("Certificate not in Database");
41                 }
42                 if (rs.getString(2) == null) {
43                         return CertificateStatus.BEEING_ISSUED;
44                 }
45                 csrName = rs.getString(1);
46                 if (rs.getTime(2) != null && rs.getTime(3) == null) {
47                         return CertificateStatus.ISSUED;
48                 }
49                 if (rs.getTime(2) != null
50                                 && rs.getString(3).equals("1970-01-01 00:00:00.0")) {
51                         return CertificateStatus.BEEING_REVOKED;
52                 }
53                 return CertificateStatus.REVOKED;
54         }
55
56         public void issue() {
57                 try {
58                         if (getStatus() != CertificateStatus.DRAFT) {
59                                 throw new IllegalStateException();
60                         }
61                         PreparedStatement inserter = DatabaseConnection
62                                         .getInstance()
63                                         .prepare(
64                                                         "INSERT INTO emailcerts SET csr_name =?, md=?, subject='a', coll_found=0, crt_name=''");
65                         inserter.setString(1, csrName);
66                         inserter.setString(2, md);
67                         inserter.execute();
68                         id = DatabaseConnection.lastInsertId(inserter);
69                 } catch (SQLException e) {
70                         e.printStackTrace();
71                 }
72
73         }
74         public boolean waitFor(int max) throws SQLException, InterruptedException {
75                 long start = System.currentTimeMillis();
76                 while (getStatus().isUnstable()) {
77                         if (max != 0 && System.currentTimeMillis() - start > max) {
78                                 return false;
79                         }
80                         Thread.sleep((long) (2000 + Math.random() * 2000));
81                 }
82                 return true;
83         }
84         public void revoke() {
85                 try {
86                         if (getStatus() != CertificateStatus.ISSUED) {
87                                 throw new IllegalStateException();
88                         }
89                         PreparedStatement inserter = DatabaseConnection
90                                         .getInstance()
91                                         .prepare(
92                                                         "UPDATE emailcerts SET revoked = '1970-01-01' WHERE id=?");
93                         inserter.setInt(1, id);
94                         inserter.execute();
95                 } catch (SQLException e) {
96                         e.printStackTrace();
97                 }
98
99         }
100         public Certificate renew() {
101                 return null;
102         }
103
104 }