1 package org.cacert.gigi;
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6 import org.cacert.gigi.database.DatabaseConnection;
8 public class Certificate {
16 // created, modified, revoked, expire
17 public enum CertificateStatus {
18 DRAFT(false), BEEING_ISSUED(true), ISSUED(false), BEEING_REVOKED(true), REVOKED(
23 private CertificateStatus(boolean unstable) {
24 this.unstable = unstable;
26 public boolean isUnstable() {
31 public CertificateStatus getStatus() throws SQLException {
33 return CertificateStatus.DRAFT;
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();
40 throw new IllegalStateException("Certificate not in Database");
42 if (rs.getString(2) == null) {
43 return CertificateStatus.BEEING_ISSUED;
45 csrName = rs.getString(1);
46 if (rs.getTime(2) != null && rs.getTime(3) == null) {
47 return CertificateStatus.ISSUED;
49 if (rs.getTime(2) != null
50 && rs.getString(3).equals("1970-01-01 00:00:00.0")) {
51 return CertificateStatus.BEEING_REVOKED;
53 return CertificateStatus.REVOKED;
58 if (getStatus() != CertificateStatus.DRAFT) {
59 throw new IllegalStateException();
61 PreparedStatement inserter = DatabaseConnection
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);
68 id = DatabaseConnection.lastInsertId(inserter);
69 } catch (SQLException e) {
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) {
80 Thread.sleep((long) (2000 + Math.random() * 2000));
84 public void revoke() {
86 if (getStatus() != CertificateStatus.ISSUED) {
87 throw new IllegalStateException();
89 PreparedStatement inserter = DatabaseConnection
92 "UPDATE emailcerts SET revoked = '1970-01-01' WHERE id=?");
93 inserter.setInt(1, id);
95 } catch (SQLException e) {
100 public Certificate renew() {