1 package org.cacert.gigi.pages.account.certs;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.net.URLEncoder;
6 import java.security.GeneralSecurityException;
7 import java.security.cert.X509Certificate;
8 import java.util.HashMap;
11 import javax.servlet.ServletOutputStream;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
15 import org.cacert.gigi.dbObjects.CACertificate;
16 import org.cacert.gigi.dbObjects.Certificate;
17 import org.cacert.gigi.localisation.Language;
18 import org.cacert.gigi.output.template.Form;
19 import org.cacert.gigi.output.template.IterableDataset;
20 import org.cacert.gigi.output.template.Template;
21 import org.cacert.gigi.pages.HandlesMixedRequest;
22 import org.cacert.gigi.pages.LoginPage;
23 import org.cacert.gigi.pages.Page;
24 import org.cacert.gigi.util.PEM;
26 public class Certificates extends Page implements HandlesMixedRequest {
28 private Template certDisplay = new Template(Certificates.class.getResource("CertificateDisplay.templ"));
30 public static final String PATH = "/account/certs";
32 static class TrustchainIterable implements IterableDataset {
36 public TrustchainIterable(CACertificate cert) {
41 public boolean next(Language l, Map<String, Object> vars) {
45 vars.put("name", cert.getKeyname());
46 vars.put("link", cert.getLink());
47 if (cert.isSelfsigned()) {
51 cert = cert.getParent();
57 public Certificates() {
58 super("Certificates");
62 public boolean beforeTemplate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
64 String pi = req.getPathInfo().substring(PATH.length());
65 if (pi.length() == 0) {
71 resp.setContentType("application/pkix-cert");
72 if (pi.endsWith(".crt")) {
74 pi = pi.substring(0, pi.length() - 4);
75 } else if (pi.endsWith(".cer")) {
76 if (req.getParameter("install") != null) {
77 resp.setContentType("application/x-x509-user-cert");
80 pi = pi.substring(0, pi.length() - 4);
81 } else if (pi.endsWith(".cer")) {
83 pi = pi.substring(0, pi.length() - 4);
87 Certificate c = Certificate.getBySerial(serial);
88 if (c == null || getUser(req).getId() != c.getOwner().getId()) {
92 X509Certificate cert = c.cert();
96 ServletOutputStream out = resp.getOutputStream();
98 out.println(PEM.encode("CERTIFICATE", cert.getEncoded()));
99 if (req.getParameter("chain") != null) {
100 CACertificate ca = c.getParent();
101 while ( !ca.isSelfsigned()) {
102 out.println(PEM.encode("CERTIFICATE", ca.getCertificate().getEncoded()));
105 if (req.getParameter("noAnchor") == null) {
106 out.println(PEM.encode("CERTIFICATE", ca.getCertificate().getEncoded()));
110 out.write(cert.getEncoded());
112 } catch (IllegalArgumentException e) {
115 } catch (GeneralSecurityException e) {
124 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
125 if (req.getQueryString() != null && !req.getQueryString().equals("") && !req.getQueryString().equals("withRevoked")) {
126 return;// Block actions by get parameters.
128 if ( !req.getPathInfo().equals(PATH)) {
132 Form.getForm(req, CertificateModificationForm.class).submit(resp.getWriter(), req);
137 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
138 PrintWriter out = resp.getWriter();
139 String pi = req.getPathInfo().substring(PATH.length());
140 if (pi.length() != 0) {
141 pi = pi.substring(1);
144 Certificate c = Certificate.getBySerial(serial);
145 if (c == null || LoginPage.getUser(req).getId() != c.getOwner().getId()) {
149 HashMap<String, Object> vars = new HashMap<>();
150 vars.put("serial", URLEncoder.encode(serial, "UTF-8"));
151 vars.put("trustchain", new TrustchainIterable(c.getParent()));
153 vars.put("cert", c.cert());
154 } catch (GeneralSecurityException e) {
157 certDisplay.output(out, getLanguage(req), vars);
162 HashMap<String, Object> vars = new HashMap<String, Object>();
163 new CertificateModificationForm(req, req.getParameter("withRevoked") != null).output(out, getLanguage(req), vars);