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;
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;
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) {
return id;
}
- private static ObjectCache<CACertificate> myCache = new ObjectCache<>();
-
public String getKeyname() {
return keyname;
}
return this == getParent();
}
+ public static synchronized CACertificate[] getAll() {
+ return Arrays.copyOf(instances, instances.length);
+ }
+
}
package club.wpia.gigi.pages;
import java.io.IOException;
+import java.io.PrintWriter;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import club.wpia.gigi.dbObjects.CACertificate;
+import club.wpia.gigi.localisation.Language;
+import club.wpia.gigi.output.template.Outputable;
+import club.wpia.gigi.util.HTMLEncoder;
import club.wpia.gigi.util.PEM;
public class RootCertPage extends Page {
- private Certificate root;
+ private final Certificate root;
+
+ private final CACertificate[] cs;
+
+ private final OutputableCertificate rootP;
+
+ private class OutputableCertificate implements Outputable {
+
+ private final CACertificate target;
+
+ private final OutputableCertificate[] children;
+
+ public OutputableCertificate(CACertificate c) {
+ target = c;
+ LinkedList<OutputableCertificate> children = new LinkedList<>();
+ for (CACertificate c0 : cs) {
+ if (c0.getParent() == c && c0 != c) {
+ children.add(new OutputableCertificate(c0));
+ }
+ }
+
+ Collections.sort(children, new Comparator<OutputableCertificate>() {
+
+ @Override
+ public int compare(OutputableCertificate o1, OutputableCertificate o2) {
+ return o1.target.getKeyname().compareTo(o2.target.getKeyname());
+ }
+ });
+ this.children = children.toArray(new OutputableCertificate[children.size()]);
+ }
+
+ @Override
+ public void output(PrintWriter out, Language l, Map<String, Object> vars) {
+ out.println("<a href='" + HTMLEncoder.encodeHTML(target.getLink()) + "'>");
+ out.println(HTMLEncoder.encodeHTML(target.getKeyname()));
+ out.println("</a>");
+ out.println(HTMLEncoder.encodeHTML(target.getCertificate().getSubjectX500Principal().toString()));
+ out.println("<ul>");
+ for (OutputableCertificate c : children) {
+ out.print("<li>");
+ c.output(out, l, vars);
+ out.print("</li>");
+ }
+ out.println("</ul>");
+ }
+
+ }
public RootCertPage(KeyStore ks) {
super("Root Certificates");
try {
root = ks.getCertificate("root");
} catch (KeyStoreException e) {
- e.printStackTrace();
+ throw new Error(e);
+ }
+ cs = CACertificate.getAll();
+ CACertificate rootC = null;
+ for (CACertificate c : cs) {
+ if (c.isSelfsigned()) {
+ rootC = c;
+ break;
+ }
+ }
+ if (rootC == null) {
+ throw new Error();
}
+ rootP = new OutputableCertificate(rootC);
}
@Override
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
- getDefaultTemplate().output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
+ HashMap<String, Object> map = new HashMap<String, Object>();
+ map.put("root", rootP);
+ getDefaultTemplate().output(resp.getWriter(), getLanguage(req), map);
}