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.HashMap;
15 import java.util.LinkedList;
16 import java.util.List;
17 import java.util.Map.Entry;
19 import org.cacert.gigi.GigiApiException;
20 import org.cacert.gigi.database.DatabaseConnection;
21 import org.cacert.gigi.database.GigiPreparedStatement;
22 import org.cacert.gigi.database.GigiResultSet;
23 import org.cacert.gigi.util.Job;
24 import org.cacert.gigi.util.KeyStorage;
25 import org.cacert.gigi.util.Notary;
27 public class Certificate {
30 EMAIL("email"), DNS("DNS");
32 private final String opensslName;
34 private SANType(String opensslName) {
35 this.opensslName = opensslName;
38 public String getOpensslName() {
43 public static class SubjectAlternateName implements Comparable<SubjectAlternateName> {
49 public SubjectAlternateName(SANType type, String name) {
54 public String getName() {
58 public SANType getType() {
63 public int compareTo(SubjectAlternateName o) {
64 int i = type.compareTo(o.type);
68 return name.compareTo(o.name);
72 public int hashCode() {
75 result = prime * result + ((name == null) ? 0 : name.hashCode());
76 result = prime * result + ((type == null) ? 0 : type.hashCode());
81 public boolean equals(Object obj) {
88 if (getClass() != obj.getClass()) {
91 SubjectAlternateName other = (SubjectAlternateName) obj;
93 if (other.name != null) {
96 } else if ( !name.equals(other.name)) {
99 if (type != other.type) {
107 public enum CSRType {
115 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 private HashMap<String, String> dn;
133 private String dnString;
135 public Certificate(User owner, HashMap<String, String> dn, String md, String csr, CSRType csrType, CertificateProfile profile, SubjectAlternateName... sans) throws GigiApiException {
136 if ( !owner.canIssue(profile)) {
137 throw new GigiApiException("You are not allowed to issue these certificates.");
141 if (dn.size() == 0) {
142 throw new GigiApiException("DN must not be empty");
144 dnString = stringifyDN(dn);
147 this.csrType = csrType;
148 this.profile = profile;
149 this.sans = Arrays.asList(sans);
152 private Certificate(String serial) {
154 String concat = "group_concat(concat('/', `name`, '=', REPLACE(REPLACE(value, '\\\\', '\\\\\\\\'), '/', '\\\\/')))";
155 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT certs.id, " + concat + " as subject, md, csr_name, crt_name,memid, profile FROM `certs` LEFT JOIN certAvas ON certAvas.certid=certs.id WHERE serial=? GROUP BY certs.id");
156 ps.setString(1, serial);
157 GigiResultSet rs = ps.executeQuery();
159 throw new IllegalArgumentException("Invalid mid " + serial);
161 this.id = rs.getInt(1);
162 dnString = rs.getString(2);
163 md = rs.getString(3);
164 csrName = rs.getString(4);
165 crtName = rs.getString(5);
166 owner = User.getById(rs.getInt(6));
167 profile = CertificateProfile.getById(rs.getInt(7));
168 this.serial = serial;
170 GigiPreparedStatement ps2 = DatabaseConnection.getInstance().prepare("SELECT contents, type FROM `subjectAlternativeNames` WHERE certId=?");
172 GigiResultSet rs2 = ps2.executeQuery();
173 sans = new LinkedList<>();
175 sans.add(new SubjectAlternateName(SANType.valueOf(rs2.getString("type").toUpperCase()), rs2.getString("contents")));
182 public enum CertificateStatus {
184 * This certificate is not in the database, has no id and only exists as
189 * The certificate has been signed. It is stored in the database.
190 * {@link Certificate#cert()} is valid.
195 * The certificate has been revoked.
200 * If this certificate cannot be updated because an error happened in
205 private CertificateStatus() {}
209 public CertificateStatus getStatus() {
211 return CertificateStatus.DRAFT;
213 GigiPreparedStatement searcher = DatabaseConnection.getInstance().prepare("SELECT crt_name, created, revoked, serial FROM certs WHERE id=?");
214 searcher.setInt(1, id);
215 GigiResultSet rs = searcher.executeQuery();
217 throw new IllegalStateException("Certificate not in Database");
220 crtName = rs.getString(1);
221 serial = rs.getString(4);
222 if (rs.getTime(2) == null) {
223 return CertificateStatus.DRAFT;
225 if (rs.getTime(2) != null && rs.getTime(3) == null) {
226 return CertificateStatus.ISSUED;
228 return CertificateStatus.REVOKED;
233 * the date from which on the certificate should be valid. (or
234 * null if it should be valid instantly)
236 * the period for which the date should be valid. (a
237 * <code>yyyy-mm-dd</code> or a "2y" (2 calendar years), "6m" (6
239 * @return A job which can be used to monitor the progress of this task.
240 * @throws IOException
241 * for problems with writing the CSR/SPKAC
242 * @throws GigiApiException
243 * if the period is bogus
245 public Job issue(Date start, String period) throws IOException, GigiApiException {
246 if (getStatus() != CertificateStatus.DRAFT) {
247 throw new IllegalStateException();
249 Notary.writeUserAgreement(owner, "CCA", "issue certificate", "", true, 0);
251 GigiPreparedStatement inserter = DatabaseConnection.getInstance().prepare("INSERT INTO certs SET md=?, csr_type=?, crt_name='', memid=?, profile=?");
252 inserter.setString(1, md);
253 inserter.setString(2, csrType.toString());
254 inserter.setInt(3, owner.getId());
255 inserter.setInt(4, profile.getId());
257 id = inserter.lastInsertId();
259 GigiPreparedStatement san = DatabaseConnection.getInstance().prepare("INSERT INTO subjectAlternativeNames SET certId=?, contents=?, type=?");
260 for (SubjectAlternateName subjectAlternateName : sans) {
262 san.setString(2, subjectAlternateName.getName());
263 san.setString(3, subjectAlternateName.getType().getOpensslName());
267 GigiPreparedStatement insertAVA = DatabaseConnection.getInstance().prepare("INSERT certAvas SET certid=?, name=?, value=?");
268 insertAVA.setInt(1, id);
269 for (Entry<String, String> e : dn.entrySet()) {
270 insertAVA.setString(2, e.getKey());
271 insertAVA.setString(3, e.getValue());
274 File csrFile = KeyStorage.locateCsr(id);
275 csrName = csrFile.getPath();
276 FileOutputStream fos = new FileOutputStream(csrFile);
277 fos.write(csr.getBytes());
280 GigiPreparedStatement updater = DatabaseConnection.getInstance().prepare("UPDATE certs SET csr_name=? WHERE id=?");
281 updater.setString(1, csrName);
282 updater.setInt(2, id);
284 return Job.sign(this, start, period);
288 public Job revoke() {
289 if (getStatus() != CertificateStatus.ISSUED) {
290 throw new IllegalStateException();
292 return Job.revoke(this);
296 public X509Certificate cert() throws IOException, GeneralSecurityException {
297 CertificateStatus status = getStatus();
298 if (status != CertificateStatus.ISSUED) {
299 throw new IllegalStateException(status + " is not wanted here.");
301 InputStream is = null;
302 X509Certificate crt = null;
304 is = new FileInputStream(crtName);
305 CertificateFactory cf = CertificateFactory.getInstance("X.509");
306 crt = (X509Certificate) cf.generateCertificate(is);
315 public Certificate renew() {
323 public String getSerial() {
329 public String getDistinguishedName() {
333 public String getMessageDigest() {
337 public User getOwner() {
341 public List<SubjectAlternateName> getSANs() {
342 return Collections.unmodifiableList(sans);
345 public CertificateProfile getProfile() {
349 public static Certificate getBySerial(String serial) {
352 return new Certificate(serial);
353 } catch (IllegalArgumentException e) {
359 public static String escapeAVA(String value) {
361 return value.replace("\\", "\\\\").replace("/", "\\/");
364 public static String stringifyDN(HashMap<String, String> contents) {
365 StringBuffer res = new StringBuffer();
366 for (Entry<String, String> i : contents.entrySet()) {
367 res.append("/" + i.getKey() + "=");
368 res.append(escapeAVA(i.getValue()));
370 return res.toString();
373 public static HashMap<String, String> buildDN(String... contents) {
374 HashMap<String, String> res = new HashMap<>();
375 for (int i = 0; i + 1 < contents.length; i += 2) {
376 res.put(contents[i], contents[i + 1]);