]> WPIA git - gigi.git/blobdiff - src/club/wpia/gigi/dbObjects/CACertificate.java
add: display fingerprint on RootCertPage, TestCACertificate class
[gigi.git] / src / club / wpia / gigi / dbObjects / CACertificate.java
index a1a8f9bd3d54b5e50c1639bcef3b47342c4baa04..6e03fbc4989b56b8c12c55a59ec2da53d14675b6 100644 (file)
@@ -4,10 +4,13 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.security.GeneralSecurityException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.CertificateEncodingException;
 import java.security.cert.CertificateException;
 import java.security.cert.CertificateFactory;
 import java.security.cert.X509Certificate;
 import java.util.ArrayDeque;
+import java.util.Arrays;
 import java.util.Deque;
 import java.util.HashMap;
 
@@ -16,6 +19,7 @@ import javax.security.auth.x500.X500Principal;
 import club.wpia.gigi.database.GigiPreparedStatement;
 import club.wpia.gigi.database.GigiResultSet;
 import club.wpia.gigi.util.ServerConstants;
+import club.wpia.gigi.util.ServerConstants.Host;
 
 public class CACertificate implements IdCachable {
 
@@ -29,6 +33,10 @@ public class CACertificate implements IdCachable {
 
     private final String link;
 
+    private static final CACertificate[] instances;
+
+    private static ObjectCache<CACertificate> myCache = new ObjectCache<>();
+
     private CACertificate(int id) {
         this.id = id;
         int parentRoot;
@@ -77,6 +85,17 @@ public class CACertificate implements IdCachable {
     static {
         try {
             update();
+            try (GigiPreparedStatement q = new GigiPreparedStatement("SELECT `id` FROM `cacerts`", true)) {
+                GigiResultSet res = q.executeQuery();
+                res.last();
+                CACertificate[] certs = new CACertificate[res.getRow()];
+                res.beforeFirst();
+                int i = 0;
+                while (res.next()) {
+                    certs[i++] = getById(res.getInt(1));
+                }
+                instances = certs;
+            }
         } catch (CertificateException e) {
             throw new Error(e);
         } catch (FileNotFoundException e) {
@@ -128,10 +147,10 @@ public class CACertificate implements IdCachable {
                         String link;
                         String keyname = names.get(subj);
                         if ( !keyname.contains("_")) {
-                            link = "https://g2.crt." + ServerConstants.getSuffix() + "/g2/" + keyname + ".crt";
+                            link = "https://" + ServerConstants.getHostNamePortSecure(Host.CRT_REPO) + "/g2/" + keyname + ".crt";
                         } else {
                             String[] parts = keyname.split("_");
-                            link = "https://g2.crt." + ServerConstants.getSuffix() + "/g2/" + parts[1] + "/" + parts[0] + "-" + parts[2] + ".crt";
+                            link = "https://" + ServerConstants.getHostNamePortSecure(Host.CRT_REPO) + "/g2/" + parts[1] + "/" + parts[0] + "-" + parts[2] + ".crt";
 
                         }
                         try (GigiPreparedStatement q2 = new GigiPreparedStatement("INSERT INTO `cacerts` SET `parentRoot`=?, `keyname`=?, `link`=?")) {
@@ -160,8 +179,6 @@ public class CACertificate implements IdCachable {
         return id;
     }
 
-    private static ObjectCache<CACertificate> myCache = new ObjectCache<>();
-
     public String getKeyname() {
         return keyname;
     }
@@ -182,4 +199,11 @@ public class CACertificate implements IdCachable {
         return this == getParent();
     }
 
+    public String getFingerprint(String algorithm) throws CertificateEncodingException, NoSuchAlgorithmException {
+        return Certificate.getFingerprint(cert, algorithm);
+    }
+    
+    public static synchronized CACertificate[] getAll() {
+        return Arrays.copyOf(instances, instances.length);
+    }
 }