]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Certificate.java
Tighten Certificate Class (fix Status enums names)
[gigi.git] / src / org / cacert / gigi / Certificate.java
1 package org.cacert.gigi;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.security.GeneralSecurityException;
9 import java.security.cert.CertificateFactory;
10 import java.security.cert.X509Certificate;
11 import java.sql.PreparedStatement;
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14
15 import org.cacert.gigi.database.DatabaseConnection;
16 import org.cacert.gigi.util.KeyStorage;
17
18 public class Certificate {
19         private int id;
20         private int serial;
21         private String dn;
22         private String md;
23         private String csrName;
24         private String crtName;
25         private String csr = null;
26         public Certificate(String dn, String md, String csr) {
27                 this.dn = dn;
28                 this.md = md;
29                 this.csr = csr;
30         }
31
32         public enum CertificateStatus {
33                 DRAFT(false), BEING_ISSUED(true), ISSUED(false), BEING_REVOKED(true), REVOKED(
34                                 false);
35
36                 private boolean unstable;
37
38                 private CertificateStatus(boolean unstable) {
39                         this.unstable = unstable;
40                 }
41                 public boolean isUnstable() {
42                         return unstable;
43                 }
44
45         }
46         public CertificateStatus getStatus() throws SQLException {
47                 if (id == 0) {
48                         return CertificateStatus.DRAFT;
49                 }
50                 PreparedStatement searcher = DatabaseConnection.getInstance().prepare(
51                                 "SELECT crt_name, created, revoked FROM emailcerts WHERE id=?");
52                 searcher.setInt(1, id);
53                 ResultSet rs = searcher.executeQuery();
54                 if (!rs.next()) {
55                         throw new IllegalStateException("Certificate not in Database");
56                 }
57                 if (rs.getString(2) == null) {
58                         return CertificateStatus.BEING_ISSUED;
59                 }
60                 crtName = rs.getString(1);
61                 System.out.println(crtName);
62                 if (rs.getTime(2) != null && rs.getTime(3) == null) {
63                         return CertificateStatus.ISSUED;
64                 }
65                 if (rs.getTime(2) != null
66                                 && rs.getString(3).equals("1970-01-01 00:00:00.0")) {
67                         return CertificateStatus.BEING_REVOKED;
68                 }
69                 return CertificateStatus.REVOKED;
70         }
71
72         public void issue() throws IOException {
73                 try {
74                         if (getStatus() != CertificateStatus.DRAFT) {
75                                 throw new IllegalStateException();
76                         }
77                         PreparedStatement inserter = DatabaseConnection
78                                         .getInstance()
79                                         .prepare(
80                                                         "INSERT INTO emailcerts SET md=?, subject=?, coll_found=0, crt_name=''");
81                         inserter.setString(1, md);
82                         inserter.setString(2, dn);
83                         inserter.execute();
84                         id = DatabaseConnection.lastInsertId(inserter);
85                         File csrFile = KeyStorage.locateCsr(id);
86                         csrName = csrFile.getPath();
87                         FileOutputStream fos = new FileOutputStream(csrFile);
88                         fos.write(csr.getBytes());
89                         fos.close();
90
91                         PreparedStatement updater = DatabaseConnection.getInstance()
92                                         .prepare("UPDATE emailcerts SET csr_name=? WHERE id=?");
93                         updater.setString(1, csrName);
94                         updater.setInt(2, id);
95                         updater.execute();
96                 } catch (SQLException e) {
97                         e.printStackTrace();
98                 }
99
100         }
101         public boolean waitFor(int max) throws SQLException, InterruptedException {
102                 long start = System.currentTimeMillis();
103                 while (getStatus().isUnstable()) {
104                         if (max != 0 && System.currentTimeMillis() - start > max) {
105                                 return false;
106                         }
107                         Thread.sleep((long) (2000 + Math.random() * 2000));
108                 }
109                 return true;
110         }
111         public void revoke() {
112                 try {
113                         if (getStatus() != CertificateStatus.ISSUED) {
114                                 throw new IllegalStateException();
115                         }
116                         PreparedStatement inserter = DatabaseConnection
117                                         .getInstance()
118                                         .prepare(
119                                                         "UPDATE emailcerts SET revoked = '1970-01-01' WHERE id=?");
120                         inserter.setInt(1, id);
121                         inserter.execute();
122                 } catch (SQLException e) {
123                         e.printStackTrace();
124                 }
125
126         }
127
128         public X509Certificate cert() throws IOException, GeneralSecurityException,
129                         SQLException {
130                 CertificateStatus status = getStatus();
131                 if (status != CertificateStatus.ISSUED) {
132                         throw new IllegalStateException(status + " is not wanted here.");
133                 }
134                 InputStream is = null;
135                 X509Certificate crt = null;
136                 try {
137                         is = new FileInputStream(crtName);
138                         CertificateFactory cf = CertificateFactory.getInstance("X.509");
139                         crt = (X509Certificate) cf.generateCertificate(is);
140                 } finally {
141                         if (is != null) {
142                                 is.close();
143                         }
144                 }
145                 return crt;
146         }
147         public Certificate renew() {
148                 return null;
149         }
150         public int getId() {
151                 return id;
152         }
153         public int getSerial() {
154                 return serial;
155         }
156         public String getDistinguishedName() {
157                 return dn;
158         }
159         public String getMessageDigest() {
160                 return md;
161         }
162
163 }