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.DBEnum;
21 import org.cacert.gigi.database.GigiPreparedStatement;
22 import org.cacert.gigi.database.GigiResultSet;
23 import org.cacert.gigi.output.template.Outputable;
24 import org.cacert.gigi.output.template.TranslateCommand;
25 import org.cacert.gigi.util.KeyStorage;
27 public class Certificate implements IdCachable {
29 public enum SANType implements DBEnum {
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 String getDBName() {
48 public static class SubjectAlternateName implements Comparable<SubjectAlternateName> {
54 public SubjectAlternateName(SANType type, String name) {
59 public String getName() {
63 public SANType getType() {
68 public int compareTo(SubjectAlternateName o) {
69 int i = type.compareTo(o.type);
73 return name.compareTo(o.name);
77 public int hashCode() {
80 result = prime * result + ((name == null) ? 0 : name.hashCode());
81 result = prime * result + ((type == null) ? 0 : type.hashCode());
86 public boolean equals(Object obj) {
93 if (getClass() != obj.getClass()) {
96 SubjectAlternateName other = (SubjectAlternateName) obj;
98 if (other.name != null) {
101 } else if ( !name.equals(other.name)) {
104 if (type != other.type) {
112 public enum CSRType {
118 private CertificateOwner owner;
120 private String serial;
124 private String csrName;
126 private String crtName;
128 private String csr = null;
130 private CSRType csrType;
132 private List<SubjectAlternateName> sans;
134 private CertificateProfile profile;
136 private HashMap<String, String> dn;
138 private String dnString;
140 private CACertificate ca;
142 public Certificate(CertificateOwner owner, User actor, HashMap<String, String> dn, Digest md, String csr, CSRType csrType, CertificateProfile profile, SubjectAlternateName... sans) throws GigiApiException, IOException {
143 if ( !profile.canBeIssuedBy(owner, actor)) {
144 throw new GigiApiException("You are not allowed to issue these certificates.");
148 if (dn.size() == 0) {
149 throw new GigiApiException("DN must not be empty.");
151 dnString = stringifyDN(dn);
154 this.csrType = csrType;
155 this.profile = profile;
156 this.sans = Arrays.asList(sans);
157 synchronized (Certificate.class) {
159 try (GigiPreparedStatement inserter = new GigiPreparedStatement("INSERT INTO certs SET md=?::`mdType`, csr_type=?::`csrType`, crt_name='', memid=?, profile=?")) {
160 inserter.setString(1, md.toString().toLowerCase());
161 inserter.setString(2, this.csrType.toString());
162 inserter.setInt(3, owner.getId());
163 inserter.setInt(4, profile.getId());
165 id = inserter.lastInsertId();
168 try (GigiPreparedStatement san = new GigiPreparedStatement("INSERT INTO `subjectAlternativeNames` SET `certId`=?, contents=?, type=?::`SANType`")) {
169 for (SubjectAlternateName subjectAlternateName : sans) {
171 san.setString(2, subjectAlternateName.getName());
172 san.setString(3, subjectAlternateName.getType().getOpensslName());
177 try (GigiPreparedStatement insertAVA = new GigiPreparedStatement("INSERT INTO `certAvas` SET `certId`=?, name=?, value=?")) {
178 insertAVA.setInt(1, id);
179 for (Entry<String, String> e : this.dn.entrySet()) {
180 insertAVA.setString(2, e.getKey());
181 insertAVA.setString(3, e.getValue());
185 File csrFile = KeyStorage.locateCsr(id);
186 csrName = csrFile.getPath();
187 try (FileOutputStream fos = new FileOutputStream(csrFile)) {
188 fos.write(this.csr.getBytes("UTF-8"));
190 try (GigiPreparedStatement updater = new GigiPreparedStatement("UPDATE `certs` SET `csr_name`=? WHERE id=?")) {
191 updater.setString(1, csrName);
192 updater.setInt(2, id);
200 private Certificate(GigiResultSet rs) {
201 this.id = rs.getInt("id");
202 dnString = rs.getString("subject");
203 md = Digest.valueOf(rs.getString("md").toUpperCase());
204 csrName = rs.getString("csr_name");
205 crtName = rs.getString("crt_name");
206 owner = CertificateOwner.getById(rs.getInt("memid"));
207 profile = CertificateProfile.getById(rs.getInt("profile"));
208 this.serial = rs.getString("serial");
210 try (GigiPreparedStatement ps2 = new GigiPreparedStatement("SELECT `contents`, `type` FROM `subjectAlternativeNames` WHERE `certId`=?")) {
212 GigiResultSet rs2 = ps2.executeQuery();
213 sans = new LinkedList<>();
215 sans.add(new SubjectAlternateName(SANType.valueOf(rs2.getString("type").toUpperCase()), rs2.getString("contents")));
220 public enum CertificateStatus {
222 * This certificate is not in the database, has no id and only exists as
227 * The certificate has been signed. It is stored in the database.
228 * {@link Certificate#cert()} is valid.
233 * The certificate has been revoked.
238 * If this certificate cannot be updated because an error happened in
243 private final Outputable name;
245 private CertificateStatus(String codename) {
246 this.name = new TranslateCommand(codename);
250 public Outputable getName() {
256 public synchronized CertificateStatus getStatus() {
257 try (GigiPreparedStatement searcher = new GigiPreparedStatement("SELECT crt_name, created, revoked, serial, caid FROM certs WHERE id=?")) {
258 searcher.setInt(1, id);
259 GigiResultSet rs = searcher.executeQuery();
261 throw new IllegalStateException("Certificate not in Database");
264 crtName = rs.getString(1);
265 serial = rs.getString(4);
266 if (rs.getTimestamp(2) == null) {
267 return CertificateStatus.DRAFT;
269 ca = CACertificate.getById(rs.getInt("caid"));
270 if (rs.getTimestamp(2) != null && rs.getTimestamp(3) == null) {
271 return CertificateStatus.ISSUED;
273 return CertificateStatus.REVOKED;
279 * the date from which on the certificate should be valid. (or
280 * null if it should be valid instantly)
282 * the period for which the date should be valid. (a
283 * <code>yyyy-mm-dd</code> or a "2y" (2 calendar years), "6m" (6
285 * @return A job which can be used to monitor the progress of this task.
286 * @throws IOException
287 * for problems with writing the CSR/SPKAC
288 * @throws GigiApiException
289 * if the period is bogus
291 public Job issue(Date start, String period, User actor) throws IOException, GigiApiException {
292 if (getStatus() != CertificateStatus.DRAFT) {
293 throw new IllegalStateException();
296 return Job.sign(this, start, period);
300 public Job revoke() {
301 if (getStatus() != CertificateStatus.ISSUED) {
302 throw new IllegalStateException();
304 return Job.revoke(this);
308 public CACertificate getParent() {
309 CertificateStatus status = getStatus();
310 if (status != CertificateStatus.REVOKED && status != CertificateStatus.ISSUED) {
311 throw new IllegalStateException(status + " is not wanted here.");
316 public X509Certificate cert() throws IOException, GeneralSecurityException {
317 CertificateStatus status = getStatus();
318 if (status != CertificateStatus.REVOKED && status != CertificateStatus.ISSUED) {
319 throw new IllegalStateException(status + " is not wanted here.");
321 InputStream is = null;
322 X509Certificate crt = null;
324 is = new FileInputStream(crtName);
325 CertificateFactory cf = CertificateFactory.getInstance("X.509");
326 crt = (X509Certificate) cf.generateCertificate(is);
335 public Certificate renew() {
343 public String getSerial() {
349 public String getDistinguishedName() {
353 public Digest getMessageDigest() {
357 public CertificateOwner getOwner() {
361 public List<SubjectAlternateName> getSANs() {
362 return Collections.unmodifiableList(sans);
365 public CertificateProfile getProfile() {
369 public synchronized static Certificate getBySerial(String serial) {
370 if (serial == null || "".equals(serial)) {
373 String concat = "string_agg(concat('/', `name`, '=', REPLACE(REPLACE(value, '\\\\', '\\\\\\\\'), '/', '\\\\/')), '')";
374 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT certs.id, " + concat + " as `subject`, `md`, `csr_name`, `crt_name`,`memid`, `profile`, `certs`.`serial` FROM `certs` LEFT JOIN `certAvas` ON `certAvas`.`certId`=`certs`.`id` WHERE `serial`=? GROUP BY `certs`.`id`")) {
375 ps.setString(1, serial);
376 GigiResultSet rs = ps.executeQuery();
380 int id = rs.getInt(1);
381 Certificate c1 = cache.get(id);
385 Certificate certificate = new Certificate(rs);
386 cache.put(certificate);
391 private static ObjectCache<Certificate> cache = new ObjectCache<>();
393 public synchronized static Certificate getById(int id) {
394 Certificate cacheRes = cache.get(id);
395 if (cacheRes != null) {
400 String concat = "string_agg(concat('/', `name`, '=', REPLACE(REPLACE(value, '\\\\', '\\\\\\\\'), '/', '\\\\/')), '')";
401 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT certs.id, " + concat + " as subject, md, csr_name, crt_name,memid, profile, certs.serial FROM `certs` LEFT JOIN `certAvas` ON `certAvas`.`certId`=certs.id WHERE certs.id=? GROUP BY certs.id")) {
403 GigiResultSet rs = ps.executeQuery();
408 Certificate c = new Certificate(rs);
412 } catch (IllegalArgumentException e) {
418 public static String escapeAVA(String value) {
420 return value.replace("\\", "\\\\").replace("/", "\\/");
423 public static String stringifyDN(HashMap<String, String> contents) {
424 StringBuffer res = new StringBuffer();
425 for (Entry<String, String> i : contents.entrySet()) {
426 res.append("/" + i.getKey() + "=");
427 res.append(escapeAVA(i.getValue()));
429 return res.toString();
432 public static HashMap<String, String> buildDN(String... contents) {
433 HashMap<String, String> res = new HashMap<>();
434 for (int i = 0; i + 1 < contents.length; i += 2) {
435 res.put(contents[i], contents[i + 1]);
440 public java.util.Date getRevocationDate() {
441 if (getStatus() == CertificateStatus.REVOKED) {
442 try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT revoked FROM certs WHERE id=?")) {
443 prep.setInt(1, getId());
444 GigiResultSet res = prep.executeQuery();
446 return new java.util.Date(res.getDate("revoked").getTime());
453 public void setLoginEnabled(boolean activate) {
455 if ( !isLoginEnabled()) {
456 try (GigiPreparedStatement prep = new GigiPreparedStatement("INSERT INTO `logincerts` SET `id`=?")) {
462 try (GigiPreparedStatement prep = new GigiPreparedStatement("DELETE FROM `logincerts` WHERE `id`=?")) {
469 public boolean isLoginEnabled() {
470 try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT 1 FROM `logincerts` WHERE `id`=?")) {
472 GigiResultSet res = prep.executeQuery();
477 public static Certificate[] findBySerialPattern(String serial) {
478 try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `id` FROM `certs` WHERE `serial` LIKE ? GROUP BY `id` LIMIT 100", true)) {
479 prep.setString(1, serial);
480 return fetchCertsToArray(prep);
484 public static Certificate[] findBySANPattern(String request, SANType type) {
485 try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `certId` FROM `subjectAlternativeNames` WHERE `contents` LIKE ? and `type`=?::`SANType` GROUP BY `certId` LIMIT 100", true)) {
486 prep.setString(1, request);
487 prep.setEnum(2, type);
488 return fetchCertsToArray(prep);
492 private static Certificate[] fetchCertsToArray(GigiPreparedStatement prep) {
493 GigiResultSet res = prep.executeQuery();
495 Certificate[] certs = new Certificate[res.getRow()];
497 for (int i = 0; res.next(); i++) {
498 certs[i] = Certificate.getById(res.getInt(1));