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=?, profile=1");
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 // TODO draft to insert SANs
143 PreparedStatement san = DatabaseConnection.getInstance().prepare("INSERT INTO subjectAlternativeNames SET certId=?, contents=?, type=?");
145 san.setString(2, "<address>");
146 san.setString(3, "email");
149 PreparedStatement updater = DatabaseConnection.getInstance().prepare("UPDATE emailcerts SET csr_name=? WHERE id=?");
150 updater.setString(1, csrName);
151 updater.setInt(2, id);
153 return Job.submit(this, JobType.SIGN);
157 public Job revoke() throws SQLException {
158 if (getStatus() != CertificateStatus.ISSUED) {
159 throw new IllegalStateException();
161 return Job.submit(this, JobType.REVOKE);
165 public X509Certificate cert() throws IOException, GeneralSecurityException, SQLException {
166 CertificateStatus status = getStatus();
167 if (status != CertificateStatus.ISSUED) {
168 throw new IllegalStateException(status + " is not wanted here.");
170 InputStream is = null;
171 X509Certificate crt = null;
173 is = new FileInputStream(crtName);
174 CertificateFactory cf = CertificateFactory.getInstance("X.509");
175 crt = (X509Certificate) cf.generateCertificate(is);
184 public Certificate renew() {
192 public String getSerial() {
195 } catch (SQLException e) {
201 public String getDistinguishedName() {
205 public String getMessageDigest() {
209 public int getOwnerId() {
213 public static Certificate getBySerial(String serial) {
216 return new Certificate(serial);
217 } catch (IllegalArgumentException e) {