1 package org.cacert.gigi.dbObjects;
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;
12 import java.util.Arrays;
13 import java.util.Collections;
14 import java.util.LinkedList;
15 import java.util.List;
17 import org.cacert.gigi.GigiApiException;
18 import org.cacert.gigi.database.DatabaseConnection;
19 import org.cacert.gigi.database.GigiPreparedStatement;
20 import org.cacert.gigi.database.GigiResultSet;
21 import org.cacert.gigi.util.Job;
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 implements Comparable<SubjectAlternateName> {
47 public SubjectAlternateName(SANType type, String name) {
52 public String getName() {
56 public SANType getType() {
61 public int compareTo(SubjectAlternateName o) {
62 int i = type.compareTo(o.type);
66 return name.compareTo(o.name);
70 public int hashCode() {
73 result = prime * result + ((name == null) ? 0 : name.hashCode());
74 result = prime * result + ((type == null) ? 0 : type.hashCode());
79 public boolean equals(Object obj) {
86 if (getClass() != obj.getClass()) {
89 SubjectAlternateName other = (SubjectAlternateName) obj;
91 if (other.name != null) {
94 } else if ( !name.equals(other.name)) {
97 if (type != other.type) {
105 public enum CSRType {
113 private String serial;
119 private String csrName;
121 private String crtName;
123 private String csr = null;
125 private CSRType csrType;
127 private List<SubjectAlternateName> sans;
129 private CertificateProfile profile;
131 public Certificate(User owner, String dn, String md, String csr, CSRType csrType, CertificateProfile profile, SubjectAlternateName... sans) throws GigiApiException {
132 if ( !owner.canIssue(profile)) {
133 throw new GigiApiException("You are not allowed to issue these certificates.");
139 this.csrType = csrType;
140 this.profile = profile;
141 this.sans = Arrays.asList(sans);
144 private Certificate(String serial) {
145 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id,subject, md, csr_name, crt_name,memid, profile FROM `certs` WHERE serial=?");
146 ps.setString(1, serial);
147 GigiResultSet rs = ps.executeQuery();
149 throw new IllegalArgumentException("Invalid mid " + serial);
151 this.id = rs.getInt(1);
152 dn = rs.getString(2);
153 md = rs.getString(3);
154 csrName = rs.getString(4);
155 crtName = rs.getString(5);
156 owner = User.getById(rs.getInt(6));
157 profile = CertificateProfile.getById(rs.getInt(7));
158 this.serial = serial;
160 GigiPreparedStatement ps2 = DatabaseConnection.getInstance().prepare("SELECT contents, type FROM `subjectAlternativeNames` WHERE certId=?");
162 GigiResultSet rs2 = ps2.executeQuery();
163 sans = new LinkedList<>();
165 sans.add(new SubjectAlternateName(SANType.valueOf(rs2.getString("type").toUpperCase()), rs2.getString("contents")));
172 public enum CertificateStatus {
174 * This certificate is not in the database, has no id and only exists as
179 * The certificate has been signed. It is stored in the database.
180 * {@link Certificate#cert()} is valid.
185 * The certificate has been revoked.
190 * If this certificate cannot be updated because an error happened in
195 private CertificateStatus() {}
199 public CertificateStatus getStatus() {
201 return CertificateStatus.DRAFT;
203 GigiPreparedStatement searcher = DatabaseConnection.getInstance().prepare("SELECT crt_name, created, revoked, serial FROM certs WHERE id=?");
204 searcher.setInt(1, id);
205 GigiResultSet rs = searcher.executeQuery();
207 throw new IllegalStateException("Certificate not in Database");
210 crtName = rs.getString(1);
211 serial = rs.getString(4);
212 if (rs.getTime(2) == null) {
213 return CertificateStatus.DRAFT;
215 if (rs.getTime(2) != null && rs.getTime(3) == null) {
216 return CertificateStatus.ISSUED;
218 return CertificateStatus.REVOKED;
223 * the date from which on the certificate should be valid. (or
224 * null if it should be valid instantly)
226 * the period for which the date should be valid. (a
227 * <code>yyyy-mm-dd</code> or a "2y" (2 calendar years), "6m" (6
229 * @return A job which can be used to monitor the progress of this task.
230 * @throws IOException
231 * for problems with writing the CSR/SPKAC
232 * @throws GigiApiException
233 * if the period is bogus
235 public Job issue(Date start, String period) throws IOException, GigiApiException {
236 if (getStatus() != CertificateStatus.DRAFT) {
237 throw new IllegalStateException();
239 Notary.writeUserAgreement(owner, "CCA", "issue certificate", "", true, 0);
241 GigiPreparedStatement inserter = DatabaseConnection.getInstance().prepare("INSERT INTO certs SET md=?, subject=?, csr_type=?, crt_name='', memid=?, profile=?");
242 inserter.setString(1, md);
243 inserter.setString(2, dn);
244 inserter.setString(3, csrType.toString());
245 inserter.setInt(4, owner.getId());
246 inserter.setInt(5, profile.getId());
248 id = inserter.lastInsertId();
249 File csrFile = KeyStorage.locateCsr(id);
250 csrName = csrFile.getPath();
251 FileOutputStream fos = new FileOutputStream(csrFile);
252 fos.write(csr.getBytes());
255 // TODO draft to insert SANs
256 GigiPreparedStatement san = DatabaseConnection.getInstance().prepare("INSERT INTO subjectAlternativeNames SET certId=?, contents=?, type=?");
257 for (SubjectAlternateName subjectAlternateName : sans) {
259 san.setString(2, subjectAlternateName.getName());
260 san.setString(3, subjectAlternateName.getType().getOpensslName());
264 GigiPreparedStatement updater = DatabaseConnection.getInstance().prepare("UPDATE certs SET csr_name=? WHERE id=?");
265 updater.setString(1, csrName);
266 updater.setInt(2, id);
268 return Job.sign(this, start, period);
272 public Job revoke() {
273 if (getStatus() != CertificateStatus.ISSUED) {
274 throw new IllegalStateException();
276 return Job.revoke(this);
280 public X509Certificate cert() throws IOException, GeneralSecurityException {
281 CertificateStatus status = getStatus();
282 if (status != CertificateStatus.ISSUED) {
283 throw new IllegalStateException(status + " is not wanted here.");
285 InputStream is = null;
286 X509Certificate crt = null;
288 is = new FileInputStream(crtName);
289 CertificateFactory cf = CertificateFactory.getInstance("X.509");
290 crt = (X509Certificate) cf.generateCertificate(is);
299 public Certificate renew() {
307 public String getSerial() {
313 public String getDistinguishedName() {
317 public String getMessageDigest() {
321 public User getOwner() {
325 public List<SubjectAlternateName> getSANs() {
326 return Collections.unmodifiableList(sans);
329 public CertificateProfile getProfile() {
333 public static Certificate getBySerial(String serial) {
336 return new Certificate(serial);
337 } catch (IllegalArgumentException e) {