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