]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/account/certs/Certificates.java
UPD: Update the api to work more with "Users" than with their ids.
[gigi.git] / src / org / cacert / gigi / pages / account / certs / Certificates.java
1 package org.cacert.gigi.pages.account.certs;
2
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;
9
10 import javax.servlet.ServletOutputStream;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13
14 import org.cacert.gigi.dbObjects.Certificate;
15 import org.cacert.gigi.dbObjects.User;
16 import org.cacert.gigi.output.CertificateIterable;
17 import org.cacert.gigi.output.template.Template;
18 import org.cacert.gigi.pages.LoginPage;
19 import org.cacert.gigi.pages.Page;
20 import org.cacert.gigi.util.PEM;
21
22 public class Certificates extends Page {
23
24     private Template certDisplay = new Template(Certificates.class.getResource("CertificateDisplay.templ"));
25
26     public static final String PATH = "/account/certs";
27
28     public Certificates() {
29         super("Certificates");
30     }
31
32     @Override
33     public boolean beforeTemplate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
34
35         String pi = req.getPathInfo().substring(PATH.length());
36         if (pi.length() == 0) {
37             return false;
38         }
39         pi = pi.substring(1);
40         boolean crt = false;
41         boolean cer = false;
42         resp.setContentType("application/pkix-cert");
43         if (pi.endsWith(".crt")) {
44             crt = true;
45             pi = pi.substring(0, pi.length() - 4);
46         } else if (pi.endsWith(".cer")) {
47             if (req.getParameter("install") != null) {
48                 resp.setContentType("application/x-x509-user-cert");
49             }
50             cer = true;
51             pi = pi.substring(0, pi.length() - 4);
52         } else if (pi.endsWith(".cer")) {
53             cer = true;
54             pi = pi.substring(0, pi.length() - 4);
55         }
56         String serial = pi;
57         try {
58             Certificate c = Certificate.getBySerial(serial);
59             if (c == null || getUser(req).getId() != c.getOwner().getId()) {
60                 resp.sendError(404);
61                 return true;
62             }
63             X509Certificate cert = c.cert();
64             if ( !crt && !cer) {
65                 return false;
66             }
67             ServletOutputStream out = resp.getOutputStream();
68             if (crt) {
69                 out.println(PEM.encode("CERTIFICATE", cert.getEncoded()));
70             } else if (cer) {
71                 out.write(cert.getEncoded());
72             }
73         } catch (IllegalArgumentException e) {
74             resp.sendError(404);
75             return true;
76         } catch (GeneralSecurityException e) {
77             resp.sendError(404);
78             return true;
79         }
80
81         return true;
82     }
83
84     private Template certTable = new Template(CertificateIterable.class.getResource("CertificateTable.templ"));
85
86     @Override
87     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
88         PrintWriter out = resp.getWriter();
89         String pi = req.getPathInfo().substring(PATH.length());
90         if (pi.length() != 0) {
91             pi = pi.substring(1);
92
93             String serial = pi;
94             Certificate c = Certificate.getBySerial(serial);
95             if (c == null || LoginPage.getUser(req).getId() != c.getOwner().getId()) {
96                 resp.sendError(404);
97                 return;
98             }
99             HashMap<String, Object> vars = new HashMap<>();
100             vars.put("serial", URLEncoder.encode(serial, "UTF-8"));
101             try {
102                 vars.put("cert", c.cert());
103             } catch (GeneralSecurityException e) {
104                 e.printStackTrace();
105             }
106             certDisplay.output(out, getLanguage(req), vars);
107
108             return;
109         }
110
111         HashMap<String, Object> vars = new HashMap<String, Object>();
112         User us = LoginPage.getUser(req);
113         vars.put("certs", new CertificateIterable(us.getCertificates()));
114         certTable.output(out, getLanguage(req), vars);
115     }
116
117 }