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;
14 import java.util.Arrays;
15 import java.util.Collections;
16 import java.util.LinkedList;
17 import java.util.List;
19 import org.cacert.gigi.database.DatabaseConnection;
20 import org.cacert.gigi.util.Job;
21 import org.cacert.gigi.util.Job.JobType;
22 import org.cacert.gigi.util.KeyStorage;
23 import org.cacert.gigi.util.Notary;
25 public class Certificate {
28 EMAIL("email"), DNS("DNS");
30 private final String opensslName;
32 private SANType(String opensslName) {
33 this.opensslName = opensslName;
36 public String getOpensslName() {
41 public static class SubjectAlternateName {
47 public SubjectAlternateName(SANType type, String name) {
52 public String getName() {
56 public SANType getType() {
70 private String serial;
76 private String csrName;
78 private String crtName;
80 private String csr = null;
82 private CSRType csrType;
84 private List<SubjectAlternateName> sans;
86 private CertificateProfile profile;
88 public Certificate(int ownerId, String dn, String md, String csr, CSRType csrType, CertificateProfile profile, SubjectAlternateName... sans) {
89 this.ownerId = ownerId;
93 this.csrType = csrType;
94 this.profile = profile;
95 this.sans = Arrays.asList(sans);
98 private Certificate(String serial) {
100 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id,subject, md, csr_name, crt_name,memid, profile FROM `certs` WHERE serial=?");
101 ps.setString(1, serial);
102 ResultSet rs = ps.executeQuery();
104 throw new IllegalArgumentException("Invalid mid " + serial);
106 this.id = rs.getInt(1);
107 dn = rs.getString(2);
108 md = rs.getString(3);
109 csrName = rs.getString(4);
110 crtName = rs.getString(5);
111 ownerId = rs.getInt(6);
112 profile = CertificateProfile.getById(rs.getInt(7));
113 this.serial = serial;
115 PreparedStatement ps2 = DatabaseConnection.getInstance().prepare("SELECT contents, type FROM `subjectAlternativeNames` WHERE certId=?");
117 ResultSet rs2 = ps2.executeQuery();
118 sans = new LinkedList<>();
120 sans.add(new SubjectAlternateName(SANType.valueOf(rs2.getString("type").toUpperCase()), rs2.getString("contents")));
125 } catch (SQLException e) {
130 public enum CertificateStatus {
132 * This certificate is not in the database, has no id and only exists as
137 * The certificate has been signed. It is stored in the database.
138 * {@link Certificate#cert()} is valid.
143 * The certificate has been revoked.
148 * If this certificate cannot be updated because an error happened in
153 private CertificateStatus() {}
157 public CertificateStatus getStatus() throws SQLException {
159 return CertificateStatus.DRAFT;
161 PreparedStatement searcher = DatabaseConnection.getInstance().prepare("SELECT crt_name, created, revoked, serial FROM certs WHERE id=?");
162 searcher.setInt(1, id);
163 ResultSet rs = searcher.executeQuery();
165 throw new IllegalStateException("Certificate not in Database");
168 crtName = rs.getString(1);
169 serial = rs.getString(4);
170 if (rs.getTime(2) == null) {
171 return CertificateStatus.DRAFT;
173 if (rs.getTime(2) != null && rs.getTime(3) == null) {
174 return CertificateStatus.ISSUED;
176 return CertificateStatus.REVOKED;
179 public Job issue() throws IOException, SQLException {
180 if (getStatus() != CertificateStatus.DRAFT) {
181 throw new IllegalStateException();
183 Notary.writeUserAgreement(ownerId, "CCA", "issue certificate", "", true, 0);
185 PreparedStatement inserter = DatabaseConnection.getInstance().prepare("INSERT INTO certs SET md=?, subject=?, csr_type=?, crt_name='', memid=?, profile=?");
186 inserter.setString(1, md);
187 inserter.setString(2, dn);
188 inserter.setString(3, csrType.toString());
189 inserter.setInt(4, ownerId);
190 inserter.setInt(5, profile.getId());
192 id = DatabaseConnection.lastInsertId(inserter);
193 File csrFile = KeyStorage.locateCsr(id);
194 csrName = csrFile.getPath();
195 FileOutputStream fos = new FileOutputStream(csrFile);
196 fos.write(csr.getBytes());
199 // TODO draft to insert SANs
200 PreparedStatement san = DatabaseConnection.getInstance().prepare("INSERT INTO subjectAlternativeNames SET certId=?, contents=?, type=?");
201 for (SubjectAlternateName subjectAlternateName : sans) {
203 san.setString(2, subjectAlternateName.getName());
204 san.setString(3, subjectAlternateName.getType().getOpensslName());
208 PreparedStatement updater = DatabaseConnection.getInstance().prepare("UPDATE certs SET csr_name=? WHERE id=?");
209 updater.setString(1, csrName);
210 updater.setInt(2, id);
212 return Job.submit(this, JobType.SIGN);
216 public Job revoke() throws SQLException {
217 if (getStatus() != CertificateStatus.ISSUED) {
218 throw new IllegalStateException();
220 return Job.submit(this, JobType.REVOKE);
224 public X509Certificate cert() throws IOException, GeneralSecurityException, SQLException {
225 CertificateStatus status = getStatus();
226 if (status != CertificateStatus.ISSUED) {
227 throw new IllegalStateException(status + " is not wanted here.");
229 InputStream is = null;
230 X509Certificate crt = null;
232 is = new FileInputStream(crtName);
233 CertificateFactory cf = CertificateFactory.getInstance("X.509");
234 crt = (X509Certificate) cf.generateCertificate(is);
243 public Certificate renew() {
251 public String getSerial() {
254 } catch (SQLException e) {
260 public String getDistinguishedName() {
264 public String getMessageDigest() {
268 public int getOwnerId() {
272 public List<SubjectAlternateName> getSANs() {
273 return Collections.unmodifiableList(sans);
276 public CertificateProfile getProfile() {
280 public static Certificate getBySerial(String serial) {
283 return new Certificate(serial);
284 } catch (IllegalArgumentException e) {