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