1 package club.wpia.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 club.wpia.gigi.GigiApiException;
20 import club.wpia.gigi.database.DBEnum;
21 import club.wpia.gigi.database.GigiPreparedStatement;
22 import club.wpia.gigi.database.GigiResultSet;
23 import club.wpia.gigi.output.template.Outputable;
24 import club.wpia.gigi.output.template.TranslateCommand;
25 import club.wpia.gigi.pages.account.certs.CertificateRequest;
26 import club.wpia.gigi.util.KeyStorage;
28 public class Certificate implements IdCachable {
30 public enum RevocationType implements DBEnum {
31 USER("user"), SUPPORT("support"), PING_TIMEOUT("ping_timeout");
33 private final String dbName;
35 private RevocationType(String dbName) {
40 public String getDBName() {
45 public enum SANType implements DBEnum {
46 EMAIL("email"), DNS("DNS");
48 private final String opensslName;
50 private SANType(String opensslName) {
51 this.opensslName = opensslName;
54 public String getOpensslName() {
59 public String getDBName() {
64 public static class SubjectAlternateName implements Comparable<SubjectAlternateName> {
70 public SubjectAlternateName(SANType type, String name) {
75 public String getName() {
79 public SANType getType() {
84 public int compareTo(SubjectAlternateName o) {
85 int i = type.compareTo(o.type);
89 return name.compareTo(o.name);
93 public int hashCode() {
96 result = prime * result + ((name == null) ? 0 : name.hashCode());
97 result = prime * result + ((type == null) ? 0 : type.hashCode());
102 public boolean equals(Object obj) {
109 if (getClass() != obj.getClass()) {
112 SubjectAlternateName other = (SubjectAlternateName) obj;
114 if (other.name != null) {
117 } else if ( !name.equals(other.name)) {
120 if (type != other.type) {
128 public enum CSRType {
134 private CertificateOwner owner;
136 private String serial;
140 private String csrName;
142 private String crtName;
144 private String csr = null;
146 private CSRType csrType;
148 private List<SubjectAlternateName> sans;
150 private CertificateProfile profile;
152 private HashMap<String, String> dn;
154 private String dnString;
156 private CACertificate ca;
159 * Creates a new Certificate. WARNING: this is an internal API. Creating
160 * certificates for users must be done using the {@link CertificateRequest}
164 * the owner for whom the certificate should be created.
166 * the acting user that creates the certificate
168 * the distinguished name of the subject of this certificate (as
169 * Map using OpenSSL-Style keys)
171 * the {@link Digest} to sign the certificate with
173 * the CSR/SPKAC-Request containing the public key in question
175 * the type of the csr parameter
177 * the profile under which this certificate is to be issued
179 * additional subject alternative names
180 * @throws GigiApiException
181 * in case the request is malformed or internal errors occur
182 * @throws IOException
183 * when the request cannot be written.
185 public Certificate(CertificateOwner owner, User actor, HashMap<String, String> dn, Digest md, String csr, CSRType csrType, CertificateProfile profile, SubjectAlternateName... sans) throws GigiApiException, IOException {
186 if ( !profile.canBeIssuedBy(owner, actor)) {
187 throw new GigiApiException("You are not allowed to issue these certificates.");
191 if (dn.size() == 0) {
192 throw new GigiApiException("DN must not be empty.");
194 dnString = stringifyDN(dn);
197 this.csrType = csrType;
198 this.profile = profile;
199 this.sans = Arrays.asList(sans);
200 synchronized (Certificate.class) {
202 try (GigiPreparedStatement inserter = new GigiPreparedStatement("INSERT INTO certs SET md=?::`mdType`, csr_type=?::`csrType`, crt_name='', memid=?, profile=?")) {
203 inserter.setString(1, md.toString().toLowerCase());
204 inserter.setString(2, this.csrType.toString());
205 inserter.setInt(3, owner.getId());
206 inserter.setInt(4, profile.getId());
208 id = inserter.lastInsertId();
211 try (GigiPreparedStatement san = new GigiPreparedStatement("INSERT INTO `subjectAlternativeNames` SET `certId`=?, contents=?, type=?::`SANType`")) {
212 for (SubjectAlternateName subjectAlternateName : sans) {
214 san.setString(2, subjectAlternateName.getName());
215 san.setString(3, subjectAlternateName.getType().getOpensslName());
220 try (GigiPreparedStatement insertAVA = new GigiPreparedStatement("INSERT INTO `certAvas` SET `certId`=?, name=?, value=?")) {
221 insertAVA.setInt(1, id);
222 for (Entry<String, String> e : this.dn.entrySet()) {
223 insertAVA.setString(2, e.getKey());
224 insertAVA.setString(3, e.getValue());
228 File csrFile = KeyStorage.locateCsr(id);
229 csrName = csrFile.getPath();
230 try (FileOutputStream fos = new FileOutputStream(csrFile)) {
231 fos.write(this.csr.getBytes("UTF-8"));
233 try (GigiPreparedStatement updater = new GigiPreparedStatement("UPDATE `certs` SET `csr_name`=? WHERE id=?")) {
234 updater.setString(1, csrName);
235 updater.setInt(2, id);
243 private Certificate(GigiResultSet rs) {
244 this.id = rs.getInt("id");
245 dnString = rs.getString("subject");
246 md = Digest.valueOf(rs.getString("md").toUpperCase());
247 csrName = rs.getString("csr_name");
248 crtName = rs.getString("crt_name");
249 owner = CertificateOwner.getById(rs.getInt("memid"));
250 profile = CertificateProfile.getById(rs.getInt("profile"));
251 this.serial = rs.getString("serial");
253 try (GigiPreparedStatement ps2 = new GigiPreparedStatement("SELECT `contents`, `type` FROM `subjectAlternativeNames` WHERE `certId`=?")) {
255 GigiResultSet rs2 = ps2.executeQuery();
256 sans = new LinkedList<>();
258 sans.add(new SubjectAlternateName(SANType.valueOf(rs2.getString("type").toUpperCase()), rs2.getString("contents")));
263 public enum CertificateStatus {
265 * This certificate is not in the database, has no id and only exists as
270 * The certificate has been signed. It is stored in the database.
271 * {@link Certificate#cert()} is valid.
276 * The certificate has been revoked.
281 * If this certificate cannot be updated because an error happened in
286 private final Outputable name;
288 private CertificateStatus(String codename) {
289 this.name = new TranslateCommand(codename);
293 public Outputable getName() {
299 public synchronized CertificateStatus getStatus() {
300 try (GigiPreparedStatement searcher = new GigiPreparedStatement("SELECT crt_name, created, `revoked`, serial, caid FROM certs LEFT JOIN `certsRevoked` ON `certs`.`id` = `certsRevoked`.`id` WHERE `certs`.id=?")) {
301 searcher.setInt(1, id);
302 GigiResultSet rs = searcher.executeQuery();
304 throw new IllegalStateException("Certificate not in Database");
307 crtName = rs.getString(1);
308 serial = rs.getString(4);
309 if (rs.getTimestamp(2) == null) {
310 return CertificateStatus.DRAFT;
312 ca = CACertificate.getById(rs.getInt("caid"));
313 if (rs.getTimestamp(2) != null && rs.getTimestamp(3) == null) {
314 return CertificateStatus.ISSUED;
316 return CertificateStatus.REVOKED;
322 * the date from which on the certificate should be valid. (or
323 * null if it should be valid instantly)
325 * the period for which the date should be valid. (a
326 * <code>yyyy-mm-dd</code> or a "2y" (2 calendar years), "6m" (6
328 * @return A job which can be used to monitor the progress of this task.
329 * @throws IOException
330 * for problems with writing the CSR/SPKAC
331 * @throws GigiApiException
332 * if the period is bogus
334 public Job issue(Date start, String period, User actor) throws IOException, GigiApiException {
335 if (getStatus() != CertificateStatus.DRAFT) {
336 throw new IllegalStateException();
339 return Job.sign(this, start, period);
343 public Job revoke(RevocationType type) {
344 if (getStatus() != CertificateStatus.ISSUED) {
345 throw new IllegalStateException();
347 return Job.revoke(this, type);
351 public CACertificate getParent() {
352 CertificateStatus status = getStatus();
353 if (status != CertificateStatus.REVOKED && status != CertificateStatus.ISSUED) {
354 throw new IllegalStateException(status + " is not wanted here.");
359 public X509Certificate cert() throws IOException, GeneralSecurityException {
360 CertificateStatus status = getStatus();
361 if (status != CertificateStatus.REVOKED && status != CertificateStatus.ISSUED) {
362 throw new IllegalStateException(status + " is not wanted here.");
364 InputStream is = null;
365 X509Certificate crt = null;
367 is = new FileInputStream(crtName);
368 CertificateFactory cf = CertificateFactory.getInstance("X.509");
369 crt = (X509Certificate) cf.generateCertificate(is);
378 public Certificate renew() {
386 public String getSerial() {
392 public String getDistinguishedName() {
396 public Digest getMessageDigest() {
400 public CertificateOwner getOwner() {
404 public List<SubjectAlternateName> getSANs() {
405 return Collections.unmodifiableList(sans);
408 public CertificateProfile getProfile() {
412 private static final String CONCAT = "string_agg(concat('/', `name`, '=', REPLACE(REPLACE(value, '\\\\', '\\\\\\\\'), '/', '\\\\/')), '')";
414 public synchronized static Certificate getBySerial(String serial) {
415 if (serial == null || "".equals(serial)) {
418 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`")) {
419 ps.setString(1, serial);
420 GigiResultSet rs = ps.executeQuery();
424 int id = rs.getInt(1);
425 Certificate c1 = cache.get(id);
429 Certificate certificate = new Certificate(rs);
430 cache.put(certificate);
435 private static ObjectCache<Certificate> cache = new ObjectCache<>();
437 public synchronized static Certificate getById(int id) {
438 Certificate cacheRes = cache.get(id);
439 if (cacheRes != null) {
444 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")) {
446 GigiResultSet rs = ps.executeQuery();
451 Certificate c = new Certificate(rs);
455 } catch (IllegalArgumentException e) {
461 public static String escapeAVA(String value) {
463 return value.replace("\\", "\\\\").replace("/", "\\/");
466 public static String stringifyDN(HashMap<String, String> contents) {
467 StringBuffer res = new StringBuffer();
468 for (Entry<String, String> i : contents.entrySet()) {
469 res.append("/" + i.getKey() + "=");
470 res.append(escapeAVA(i.getValue()));
472 return res.toString();
475 public static HashMap<String, String> buildDN(String... contents) {
476 HashMap<String, String> res = new HashMap<>();
477 for (int i = 0; i + 1 < contents.length; i += 2) {
478 res.put(contents[i], contents[i + 1]);
483 public java.util.Date getRevocationDate() {
484 if (getStatus() == CertificateStatus.REVOKED) {
485 try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT revoked FROM `certsRevoked` WHERE id=?")) {
486 prep.setInt(1, getId());
487 GigiResultSet res = prep.executeQuery();
489 return new java.util.Date(res.getTimestamp("revoked").getTime());
496 public void setLoginEnabled(boolean activate) {
498 if ( !isLoginEnabled()) {
499 try (GigiPreparedStatement prep = new GigiPreparedStatement("INSERT INTO `logincerts` SET `id`=?")) {
505 try (GigiPreparedStatement prep = new GigiPreparedStatement("DELETE FROM `logincerts` WHERE `id`=?")) {
512 public boolean isLoginEnabled() {
513 try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT 1 FROM `logincerts` WHERE `id`=?")) {
515 GigiResultSet res = prep.executeQuery();
520 public static Certificate[] findBySerialPattern(String serial) {
521 try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `id` FROM `certs` WHERE `serial` LIKE ? GROUP BY `id` LIMIT 100", true)) {
522 prep.setString(1, serial);
523 return fetchCertsToArray(prep);
527 public static Certificate[] findBySANPattern(String request, SANType type) {
528 try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `certId` FROM `subjectAlternativeNames` WHERE `contents` LIKE ? and `type`=?::`SANType` GROUP BY `certId` LIMIT 100", true)) {
529 prep.setString(1, request);
530 prep.setEnum(2, type);
531 return fetchCertsToArray(prep);
535 private static Certificate[] fetchCertsToArray(GigiPreparedStatement prep) {
536 GigiResultSet res = prep.executeQuery();
538 Certificate[] certs = new Certificate[res.getRow()];
540 for (int i = 0; res.next(); i++) {
541 certs[i] = Certificate.getById(res.getInt(1));