1 package org.ejbca.ui.web.admin.cainterface; 2 3 import java.io.IOException ; 4 5 import javax.servlet.ServletConfig ; 6 import javax.servlet.ServletException ; 7 import javax.servlet.http.HttpServlet ; 8 import javax.servlet.http.HttpServletRequest ; 9 import javax.servlet.http.HttpServletResponse ; 10 11 import org.apache.log4j.Logger; 12 import org.ejbca.core.model.authorization.AvailableAccessRules; 13 import org.ejbca.ui.web.RequestHelper; 14 import org.ejbca.ui.web.admin.configuration.EjbcaWebBean; 15 import org.ejbca.ui.web.pub.ServletUtils; 16 17 28 public class CAExportServlet extends HttpServlet { 29 private static final Logger log = Logger.getLogger(CAExportServlet.class); 30 private static final long serialVersionUID = 378499368926058906L; 31 public static final String HIDDEN_CANAME = "hiddencaname"; 32 public static final String TEXTFIELD_EXPORTCA_PASSWORD = "textfieldexportcapassword"; 33 34 37 public void init(ServletConfig config) throws ServletException { 38 super.init(config); 39 } 41 48 public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException , ServletException { 49 log.debug(">doPost()"); 50 doGet(req, res); 51 log.debug("<doPost()"); 52 } 54 61 public void doGet(HttpServletRequest req, HttpServletResponse res) throws java.io.IOException , ServletException { 62 log.debug(">doGet()"); 63 EjbcaWebBean ejbcawebbean= (org.ejbca.ui.web.admin.configuration.EjbcaWebBean) req.getSession().getAttribute("ejbcawebbean"); 65 if ( ejbcawebbean == null ) { 66 try { 67 ejbcawebbean = (org.ejbca.ui.web.admin.configuration.EjbcaWebBean) java.beans.Beans.instantiate(this.getClass().getClassLoader(), org.ejbca.ui.web.admin.configuration.EjbcaWebBean.class.getName()); 68 } catch (ClassNotFoundException e) { 69 throw new ServletException (e.getMessage()); 70 } catch (Exception e) { 71 throw new ServletException ("Cannot create bean of class org.ejbca.ui.web.admin.configuration.EjbcaWebBean", e); 72 } 73 req.getSession().setAttribute("ejbcawebbean", ejbcawebbean); 74 } 75 try{ 76 ejbcawebbean.initialize(req, AvailableAccessRules.ROLE_SUPERADMINISTRATOR); 77 } catch(Exception e) { 78 throw new java.io.IOException ("Authorization Denied"); 79 } 80 RequestHelper.setDefaultCharacterEncoding(req); 81 String caname = req.getParameter(HIDDEN_CANAME); 82 String capassword = req.getParameter(TEXTFIELD_EXPORTCA_PASSWORD); 83 javax.naming.Context ictx = null; 84 log.debug("Got request from "+req.getRemoteAddr()+" to export "+caname); 85 try{ 86 byte[] keystorebytes = null; 87 if (ictx == null) { 88 ictx = org.ejbca.core.ejb.InitialContextBuilder.getInstance().getInitialContext(); 89 } 90 org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionHome home = (org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(ictx.lookup("CAAdminSession"), org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionHome.class ); 91 org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionRemote caadminsession = home.create(); 92 if ( !caadminsession.isKeyStorePassword(ejbcawebbean.getAdminObject(), capassword) ) { 93 throw new IllegalArgumentException ("Keystore password does not match user-supplied password."); 94 } 95 keystorebytes = caadminsession.exportCAKeyStore(ejbcawebbean.getAdminObject(), caname, capassword.toCharArray(), capassword.toCharArray(), "SignatureKeyAlias", "EncryptionKeyAlias"); 96 ServletUtils.removeCacheHeaders(res); res.setContentType("application/octet-stream"); 98 res.setHeader("Cache-Control", "no-cache"); 99 res.setContentLength(keystorebytes.length); 100 res.setHeader("Content-Disposition", "attachment;filename=\"" + caname + ".p12\""); 101 res.getOutputStream().write(keystorebytes); 102 } catch(Exception e) { 103 res.setContentType("text/plain"); 104 res.sendError( HttpServletResponse.SC_BAD_REQUEST, e.getMessage() ); 105 } 106 } } | Popular Tags |