1 package org.cacert.gigi;
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;
15 import org.cacert.gigi.database.DatabaseConnection;
16 import org.cacert.gigi.util.KeyStorage;
18 public class Certificate {
23 private String csrName;
24 private String crtName;
25 private String csr = null;
26 public Certificate(String dn, String md, String csr) {
32 public Certificate(int id) {
34 PreparedStatement ps = DatabaseConnection
37 "SELECT subject, md, csr_name, crt_name FROM `emailcerts` WHERE id=?");
39 ResultSet rs = ps.executeQuery();
41 throw new IllegalArgumentException("Invalid mid " + id);
46 csrName = rs.getString(3);
47 crtName = rs.getString(4);
49 } catch (SQLException e) {
54 public enum CertificateStatus {
56 * This certificate is not in the database, has no id and only exists as
61 * The certificate has been written to the database and is waiting for
62 * the signer to sign it.
66 * The certificate has been signed. It is stored in the database.
67 * {@link Certificate#cert()} is valid.
71 * The cetrificate is about to be revoked by the signer bot.
76 * The certificate has been revoked.
81 * If this certificate cannot be updated because an error happened in
86 private boolean unstable;
88 private CertificateStatus(boolean unstable) {
89 this.unstable = unstable;
92 * Checks, iff this certificate stage will be left by signer actions.
94 * @return True, iff this certificate stage will be left by signer
97 public boolean isUnstable() {
102 public CertificateStatus getStatus() throws SQLException {
104 return CertificateStatus.DRAFT;
106 PreparedStatement searcher = DatabaseConnection
109 "SELECT crt_name, created, revoked, warning FROM emailcerts WHERE id=?");
110 searcher.setInt(1, id);
111 ResultSet rs = searcher.executeQuery();
113 throw new IllegalStateException("Certificate not in Database");
115 if (rs.getInt(4) >= 3) {
116 return CertificateStatus.ERROR;
119 if (rs.getString(2) == null) {
120 return CertificateStatus.SIGNING;
122 crtName = rs.getString(1);
123 System.out.println(crtName);
124 if (rs.getTime(2) != null && rs.getTime(3) == null) {
125 return CertificateStatus.ISSUED;
127 if (rs.getTime(2) != null
128 && rs.getString(3).equals("1970-01-01 00:00:00.0")) {
129 return CertificateStatus.BEING_REVOKED;
131 return CertificateStatus.REVOKED;
134 public void issue() throws IOException {
136 if (getStatus() != CertificateStatus.DRAFT) {
137 throw new IllegalStateException();
139 PreparedStatement inserter = DatabaseConnection
142 "INSERT INTO emailcerts SET md=?, subject=?, coll_found=0, crt_name=''");
143 inserter.setString(1, md);
144 inserter.setString(2, dn);
146 id = DatabaseConnection.lastInsertId(inserter);
147 File csrFile = KeyStorage.locateCsr(id);
148 csrName = csrFile.getPath();
149 FileOutputStream fos = new FileOutputStream(csrFile);
150 fos.write(csr.getBytes());
153 PreparedStatement updater = DatabaseConnection.getInstance()
154 .prepare("UPDATE emailcerts SET csr_name=? WHERE id=?");
155 updater.setString(1, csrName);
156 updater.setInt(2, id);
158 } catch (SQLException e) {
163 public boolean waitFor(int max) throws SQLException, InterruptedException {
164 long start = System.currentTimeMillis();
165 while (getStatus().isUnstable()) {
166 if (max != 0 && System.currentTimeMillis() - start > max) {
169 Thread.sleep((long) (2000 + Math.random() * 2000));
173 public void revoke() {
175 if (getStatus() != CertificateStatus.ISSUED) {
176 throw new IllegalStateException();
178 PreparedStatement inserter = DatabaseConnection
181 "UPDATE emailcerts SET revoked = '1970-01-01' WHERE id=?");
182 inserter.setInt(1, id);
184 } catch (SQLException e) {
190 public X509Certificate cert() throws IOException, GeneralSecurityException,
192 CertificateStatus status = getStatus();
193 if (status != CertificateStatus.ISSUED) {
194 throw new IllegalStateException(status + " is not wanted here.");
196 InputStream is = null;
197 X509Certificate crt = null;
199 is = new FileInputStream(crtName);
200 CertificateFactory cf = CertificateFactory.getInstance("X.509");
201 crt = (X509Certificate) cf.generateCertificate(is);
209 public Certificate renew() {
215 public int getSerial() {
218 public String getDistinguishedName() {
221 public String getMessageDigest() {