1 13 14 package org.ejbca.ui.web.admin.cainterface; 15 16 import java.io.ByteArrayOutputStream ; 17 import java.io.IOException ; 18 import java.security.cert.Certificate ; 19 20 import javax.ejb.EJBException ; 21 import javax.servlet.ServletConfig ; 22 import javax.servlet.ServletException ; 23 import javax.servlet.http.HttpServlet ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 27 import org.apache.log4j.Logger; 28 import org.bouncycastle.asn1.DEROutputStream; 29 import org.bouncycastle.jce.PKCS10CertificationRequest; 30 import org.ejbca.core.ejb.ServiceLocator; 31 import org.ejbca.core.ejb.ca.sign.ISignSessionLocal; 32 import org.ejbca.core.ejb.ca.sign.ISignSessionLocalHome; 33 import org.ejbca.core.model.InternalResources; 34 import org.ejbca.ui.web.RequestHelper; 35 import org.ejbca.ui.web.admin.configuration.EjbcaWebBean; 36 import org.ejbca.ui.web.pub.ServletUtils; 37 38 57 public class CACertReqServlet extends HttpServlet { 58 59 private static final Logger log = Logger.getLogger(CACertReqServlet.class); 60 61 private static final InternalResources intres = InternalResources.getInstance(); 62 63 private static final String COMMAND_PROPERTY_NAME = "cmd"; 64 private static final String COMMAND_CERTREQ = "certreq"; 65 private static final String COMMAND_CERT = "cert"; 66 private static final String COMMAND_CERTPKCS7 = "certpkcs7"; 67 68 private ISignSessionLocal signsession = null; 69 70 private synchronized ISignSessionLocal getSignSession(){ 71 if(signsession == null){ 72 try { 73 ISignSessionLocalHome signhome = (ISignSessionLocalHome)ServiceLocator.getInstance().getLocalHome(ISignSessionLocalHome.COMP_NAME); 74 signsession = signhome.create(); 75 }catch(Exception e){ 76 throw new EJBException (e); 77 } 78 } 79 return signsession; 80 } 81 82 83 public void init(ServletConfig config) throws ServletException { 84 super.init(config); 85 } 86 87 public void doPost(HttpServletRequest req, HttpServletResponse res) 88 throws IOException , ServletException { 89 log.debug(">doPost()"); 90 doGet(req, res); 91 log.debug("<doPost()"); 92 } 94 public void doGet(HttpServletRequest req, HttpServletResponse res) throws java.io.IOException , ServletException { 95 log.debug(">doGet()"); 96 97 EjbcaWebBean ejbcawebbean= (org.ejbca.ui.web.admin.configuration.EjbcaWebBean) 99 req.getSession().getAttribute("ejbcawebbean"); 100 if ( ejbcawebbean == null ){ 101 try { 102 ejbcawebbean = (org.ejbca.ui.web.admin.configuration.EjbcaWebBean) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "org.ejbca.ui.web.admin.configuration.EjbcaWebBean"); 103 } catch (ClassNotFoundException exc) { 104 throw new ServletException (exc.getMessage()); 105 }catch (Exception exc) { 106 throw new ServletException (" Cannot create bean of class "+"org.ejbca.ui.web.admin.configuration.EjbcaWebBean", exc); 107 } 108 req.getSession().setAttribute("ejbcawebbean", ejbcawebbean); 109 } 110 111 CAInterfaceBean cabean= (org.ejbca.ui.web.admin.cainterface.CAInterfaceBean) 113 req.getSession().getAttribute("cabean"); 114 if ( cabean == null ){ 115 try { 116 cabean = (org.ejbca.ui.web.admin.cainterface.CAInterfaceBean) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "org.ejbca.ui.web.admin.cainterface.CAInterfaceBean"); 117 } catch (ClassNotFoundException exc) { 118 throw new ServletException (exc.getMessage()); 119 }catch (Exception exc) { 120 throw new ServletException (" Cannot create bean of class "+"org.ejbca.ui.web.admin.cainterface.CAInterfaceBean", exc); 121 } 122 req.getSession().setAttribute("cabean", cabean); 123 } 124 125 126 try{ 127 ejbcawebbean.initialize(req, "/super_administrator"); 128 } catch(Exception e){ 129 throw new java.io.IOException ("Authorization Denied"); 130 } 131 132 try{ 133 cabean.initialize(req, ejbcawebbean); 134 } catch(Exception e){ 135 throw new java.io.IOException ("Error initializing CACertReqServlet"); 136 } 137 138 139 String command; 140 String remoteAddr = req.getRemoteAddr(); 142 RequestHelper.setDefaultCharacterEncoding(req); 143 command = req.getParameter(COMMAND_PROPERTY_NAME); 144 if (command == null) 145 command = ""; 146 if (command.equalsIgnoreCase(COMMAND_CERTREQ)) { 147 try { 148 149 PKCS10CertificationRequest pkcs10request = cabean.getPKCS10RequestData(); 150 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 151 DEROutputStream dOut = new DEROutputStream(bOut); 152 dOut.writeObject(pkcs10request); 153 dOut.close(); 154 byte[] b64certreq = org.ejbca.util.Base64.encode(bOut.toByteArray()); 155 String out = "-----BEGIN CERTIFICATE REQUEST-----\n"; 156 out += new String (b64certreq); 157 out += "\n-----END CERTIFICATE REQUEST-----\n"; 158 ServletUtils.removeCacheHeaders(res); 160 String filename = "pkcs10certificaterequest.pem"; 161 res.setHeader("Content-disposition", "attachment; filename=" + filename); 162 res.setContentType("application/octet-stream"); 163 res.setContentLength(out.length()); 164 res.getOutputStream().write(out.getBytes()); 165 String iMsg = intres.getLocalizedMessage("certreq.sentlatestcertreq", remoteAddr); 166 log.info(iMsg); 167 } catch (Exception e) { 168 String errMsg = intres.getLocalizedMessage("certreq.errorsendlatestcertreq", remoteAddr); 169 log.error(errMsg, e); 170 res.sendError(HttpServletResponse.SC_NOT_FOUND, errMsg); 171 return; 172 } 173 } 174 if (command.equalsIgnoreCase(COMMAND_CERT)) { 175 try { 176 Certificate cert = cabean.getProcessedCertificate(); 177 byte[] b64cert = org.ejbca.util.Base64.encode(cert.getEncoded()); 178 RequestHelper.sendNewB64Cert(b64cert, res, RequestHelper.BEGIN_CERTIFICATE_WITH_NL, RequestHelper.END_CERTIFICATE_WITH_NL); 179 } catch (Exception e) { 180 String errMsg = intres.getLocalizedMessage("certreq.errorsendcert", remoteAddr, e.getMessage()); 181 log.error(errMsg, e); 182 res.sendError(HttpServletResponse.SC_NOT_FOUND, errMsg); 183 return; 184 } 185 } 186 if (command.equalsIgnoreCase(COMMAND_CERTPKCS7)) { 187 try { 188 Certificate cert = cabean.getProcessedCertificate(); 189 byte[] pkcs7 = getSignSession().createPKCS7(ejbcawebbean.getAdminObject(), cert, true); 190 byte[] b64cert = org.ejbca.util.Base64.encode(pkcs7); 191 RequestHelper.sendNewB64Cert(b64cert, res, RequestHelper.BEGIN_PKCS7_WITH_NL, RequestHelper.END_PKCS7_WITH_NL); 192 } catch (Exception e) { 193 String errMsg = intres.getLocalizedMessage("certreq.errorsendcert", remoteAddr, e.getMessage()); 194 log.error(errMsg, e); 195 res.sendError(HttpServletResponse.SC_NOT_FOUND, errMsg); 196 return; 197 } 198 } 199 200 201 202 203 } 205 } 206 | Popular Tags |