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.Job;
17 import org.cacert.gigi.util.Job.JobType;
18 import org.cacert.gigi.util.KeyStorage;
19 import org.cacert.gigi.util.Notary;
21 public class Certificate {
31 private String serial;
37 private String csrName;
39 private String crtName;
41 private String csr = null;
43 private CSRType csrType;
45 public Certificate(int ownerId, String dn, String md, String csr, CSRType csrType) {
46 this.ownerId = ownerId;
50 this.csrType = csrType;
53 private Certificate(String serial) {
55 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id,subject, md, csr_name, crt_name,memid FROM `emailcerts` WHERE serial=?");
56 ps.setString(1, serial);
57 ResultSet rs = ps.executeQuery();
59 throw new IllegalArgumentException("Invalid mid " + serial);
61 this.id = rs.getInt(1);
64 csrName = rs.getString(4);
65 crtName = rs.getString(5);
66 ownerId = rs.getInt(6);
69 } catch (SQLException e) {
74 public enum CertificateStatus {
76 * This certificate is not in the database, has no id and only exists as
81 * The certificate has been signed. It is stored in the database.
82 * {@link Certificate#cert()} is valid.
87 * The certificate has been revoked.
92 * If this certificate cannot be updated because an error happened in
97 private CertificateStatus() {}
101 public CertificateStatus getStatus() throws SQLException {
103 return CertificateStatus.DRAFT;
105 PreparedStatement searcher = DatabaseConnection.getInstance().prepare("SELECT crt_name, created, revoked, serial FROM emailcerts WHERE id=?");
106 searcher.setInt(1, id);
107 ResultSet rs = searcher.executeQuery();
109 throw new IllegalStateException("Certificate not in Database");
112 crtName = rs.getString(1);
113 serial = rs.getString(4);
114 if (rs.getTime(2) == null) {
115 return CertificateStatus.DRAFT;
117 if (rs.getTime(2) != null && rs.getTime(3) == null) {
118 return CertificateStatus.ISSUED;
120 return CertificateStatus.REVOKED;
123 public Job issue() throws IOException, SQLException {
124 if (getStatus() != CertificateStatus.DRAFT) {
125 throw new IllegalStateException();
127 Notary.writeUserAgreement(ownerId, "CCA", "issue certificate", "", true, 0);
129 PreparedStatement inserter = DatabaseConnection.getInstance().prepare("INSERT INTO emailcerts SET md=?, subject=?, csr_type=?, crt_name='', memid=?");
130 inserter.setString(1, md);
131 inserter.setString(2, dn);
132 inserter.setString(3, csrType.toString());
133 inserter.setInt(4, ownerId);
135 id = DatabaseConnection.lastInsertId(inserter);
136 File csrFile = KeyStorage.locateCsr(id);
137 csrName = csrFile.getPath();
138 FileOutputStream fos = new FileOutputStream(csrFile);
139 fos.write(csr.getBytes());
142 PreparedStatement updater = DatabaseConnection.getInstance().prepare("UPDATE emailcerts SET csr_name=? WHERE id=?");
143 updater.setString(1, csrName);
144 updater.setInt(2, id);
146 return Job.submit(this, JobType.SIGN);
150 public Job revoke() throws SQLException {
151 if (getStatus() != CertificateStatus.ISSUED) {
152 throw new IllegalStateException();
154 return Job.submit(this, JobType.REVOKE);
158 public X509Certificate cert() throws IOException, GeneralSecurityException, SQLException {
159 CertificateStatus status = getStatus();
160 if (status != CertificateStatus.ISSUED) {
161 throw new IllegalStateException(status + " is not wanted here.");
163 InputStream is = null;
164 X509Certificate crt = null;
166 is = new FileInputStream(crtName);
167 CertificateFactory cf = CertificateFactory.getInstance("X.509");
168 crt = (X509Certificate) cf.generateCertificate(is);
177 public Certificate renew() {
185 public String getSerial() {
188 } catch (SQLException e) {
194 public String getDistinguishedName() {
198 public String getMessageDigest() {
202 public int getOwnerId() {
206 public static Certificate getBySerial(String serial) {
209 return new Certificate(serial);
210 } catch (IllegalArgumentException e) {