1 18 package org.apache.geronimo.ca.helper; 19 20 import java.io.IOException ; 21 import java.io.OutputStream ; 22 import java.math.BigInteger ; 23 import java.security.cert.Certificate ; 24 25 import javax.servlet.ServletException ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 29 import org.apache.geronimo.ca.helper.util.CAHelperUtils; 30 import org.apache.geronimo.management.geronimo.CertificateRequestStore; 31 import org.apache.geronimo.management.geronimo.CertificateStore; 32 33 38 public class DownloadCertificateServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { 39 42 public DownloadCertificateServlet() { 43 super(); 44 } 45 46 49 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 50 doPost(request, response); 51 } 52 53 56 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 57 String type = request.getParameter("type"); 58 String csrId = request.getParameter("csrId"); 59 try { 60 if(type != null && type.equals("ca")){ 61 CertificateStore certStore = CAHelperUtils.getCertificateStore(); 64 Certificate cert = certStore.getCACertificate(); 65 byte[] data = cert.getEncoded(); 66 response.setContentType("application/x-x509-ca-cert"); 68 response.setContentLength(data.length); 69 response.getOutputStream().write(data); 70 } else if(csrId != null){ 71 CertificateRequestStore certReqStore = CAHelperUtils.getCertificateRequestStore(); 74 BigInteger sNo = certReqStore.getSerialNumberForRequest(csrId); 75 if(sNo == null) { 76 throw new Exception ("Either the CSR is yet to be fulfilled or the csrId is invalid. csrId = "+csrId); 78 } 79 CertificateStore certStore = CAHelperUtils.getCertificateStore(); 80 Certificate cert = certStore.getCertificate(sNo); 81 byte[] data = cert.getEncoded(); 82 83 String host = request.getServerName(); 85 int port = CAHelperUtils.getHttpsClientAuthPort(); 86 String contextPath = request.getContextPath(); 87 String link = "https://"+host+":"+port+""+contextPath+"/verifyCertificate.jsp?csrId="+request.getParameter("csrId"); 88 89 response.setContentType("multipart/mixed; boundary=\"BOUNDARY\""); 91 OutputStream out = response.getOutputStream(); 92 out.write("This is a multi-part message in MIME format.\n".getBytes()); 93 94 out.write("--BOUNDARY\n".getBytes()); 96 out.write(("Content-type: application/x-x509-user-cert\n\n").getBytes()); 97 out.write(data); 98 99 out.write("--BOUNDARY\n".getBytes()); 101 out.write("Content-type: text/html\n\n".getBytes()); 102 out.write("<html><body>".getBytes()); 103 out.write("<p>Certificate is downloaded successfully. ".getBytes()); 104 if(port != -1) 105 out.write(("Access <a HREF="+link+">this link</a> to verify.</p>\n").getBytes()); 106 else 107 out.write("No HTTPS client-authentication port is configured to verify.</p>\n".getBytes()); 108 109 out.write(("<a HREF=\""+contextPath+"\"> Back to CA Helper home</a>").getBytes()); 110 out.write("</body></html>".getBytes()); 111 112 out.write("--BOUNDARY--\n".getBytes()); 113 out.flush(); 114 } else { 115 throw new Exception ("Invalid certificate download request."); 117 } 118 } catch (Exception e) { 119 throw new ServletException ("Exception while uploading certificate.", e); 120 } 121 } 122 } 123 | Popular Tags |