+ public static String escapeAVA(String value) {
+
+ return value.replace("\\", "\\\\").replace("/", "\\/");
+ }
+
+ public static String stringifyDN(HashMap<String, String> contents) {
+ StringBuffer res = new StringBuffer();
+ for (Entry<String, String> i : contents.entrySet()) {
+ res.append("/" + i.getKey() + "=");
+ res.append(escapeAVA(i.getValue()));
+ }
+ return res.toString();
+ }
+
+ public static HashMap<String, String> buildDN(String... contents) {
+ HashMap<String, String> res = new HashMap<>();
+ for (int i = 0; i + 1 < contents.length; i += 2) {
+ res.put(contents[i], contents[i + 1]);
+ }
+ return res;
+ }
+
+ public java.util.Date getRevocationDate() {
+ if (getStatus() == CertificateStatus.REVOKED) {
+ try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT revoked FROM certs WHERE id=?")) {
+ prep.setInt(1, getId());
+ GigiResultSet res = prep.executeQuery();
+ if (res.next()) {
+ return new java.util.Date(res.getDate("revoked").getTime());
+ }
+ }
+ }
+ return null;
+ }
+
+ public void setLoginEnabled(boolean activate) {
+ if (activate) {
+ if ( !isLoginEnabled()) {
+ try (GigiPreparedStatement prep = new GigiPreparedStatement("INSERT INTO `logincerts` SET `id`=?")) {
+ prep.setInt(1, id);
+ prep.execute();
+ }
+ }
+ } else {
+ try (GigiPreparedStatement prep = new GigiPreparedStatement("DELETE FROM `logincerts` WHERE `id`=?")) {
+ prep.setInt(1, id);
+ prep.execute();
+ }
+ }
+ }
+
+ public boolean isLoginEnabled() {
+ try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT 1 FROM `logincerts` WHERE `id`=?")) {
+ prep.setInt(1, id);
+ GigiResultSet res = prep.executeQuery();
+ return res.next();
+ }
+ }
+
+ public static Certificate[] findBySerialPattern(String serial) {
+ try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `id` FROM `certs` WHERE `serial` LIKE ? GROUP BY `id` LIMIT 100", true)) {
+ prep.setString(1, serial);
+ return fetchCertsToArray(prep);
+ }
+ }
+
+ public static Certificate[] findBySANPattern(String request, SANType type) {
+ try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `certId` FROM `subjectAlternativeNames` WHERE `contents` LIKE ? and `type`=?::`SANType` GROUP BY `certId` LIMIT 100", true)) {
+ prep.setString(1, request);
+ prep.setString(2, type.getOpensslName());
+ return fetchCertsToArray(prep);
+ }
+ }
+
+ private static Certificate[] fetchCertsToArray(GigiPreparedStatement prep) {
+ GigiResultSet res = prep.executeQuery();
+ res.last();
+ Certificate[] certs = new Certificate[res.getRow()];
+ res.beforeFirst();
+ for (int i = 0; res.next(); i++) {
+ certs[i] = Certificate.getById(res.getInt(1));
+ }
+ return certs;
+ }