return; } catch (se.anatom.ejbca.ca.exception.IllegalKeyException e) { log.debug("Illegal Key received: "+e.getMessage()); debug.printMessage("Invalid Key in request: "+e.getMessage()); debug.printMessage("Please supply a correct request."); debug.printDebugInfo();
    public void doPost(HttpServletRequest request, HttpServletResponse response)        throws IOException, ServletException {        ServletDebug debug = new ServletDebug(request, response);        boolean usekeyrecovery = false;        try {            String username = request.getParameter("user");            String password = request.getParameter("password");            String keylengthstring = request.getParameter("keylength");			int keylength = 1024;			            int resulttype = 0;            if(request.getParameter("resulttype") != null)              resulttype = Integer.parseInt(request.getParameter("resulttype")); // Indicates if certificate or PKCS7 should be returned on manual PKCS10 request.                        String classid = "clsid:127698e4-e730-4e5c-a2b1-21490a70c8a1\" CODEBASE=\"/CertControl/xenroll.cab#Version=5,131,3659,0";            if ((request.getParameter("classid") != null) &&                    !request.getParameter("classid").equals("")) {                classid = request.getParameter("classid");            }            if (keylengthstring != null) {                keylength = Integer.parseInt(keylengthstring);            }            Admin administrator = new Admin(Admin.TYPE_PUBLIC_WEB_USER, request.getRemoteAddr());            IUserAdminSessionRemote adminsession = useradminhome.create();            IRaAdminSessionRemote raadminsession = raadminhome.create();                        ISignSessionRemote signsession = signsessionhome.create();            RequestHelper helper = new RequestHelper(administrator, debug);            log.debug("Got request for " + username + "/" + password);            debug.print("<h3>username: " + username + "</h3>");            // Check user            int tokentype = SecConst.TOKEN_SOFT_BROWSERGEN;            usekeyrecovery = (raadminsession.loadGlobalConfiguration(administrator)).getEnableKeyRecovery();            UserAdminData data = adminsession.findUser(administrator, username);            if (data == null) {                throw new ObjectNotFoundException();            }            boolean savekeys = data.getKeyRecoverable() && usekeyrecovery;            boolean loadkeys = (data.getStatus() == UserDataLocal.STATUS_KEYRECOVERY) &&                usekeyrecovery;            // get users Token Type.            tokentype = data.getTokenType();            if(tokentype == SecConst.TOKEN_SOFT_P12){              KeyStore ks = generateToken(administrator, username, password, data.getCAId(), keylength, false, loadkeys, savekeys);              sendP12Token(ks, username, password, response);            }            if(tokentype == SecConst.TOKEN_SOFT_JKS){              KeyStore ks = generateToken(administrator, username, password, data.getCAId(), keylength, true, loadkeys, savekeys);              sendJKSToken(ks, username, password, response);            }            if(tokentype == SecConst.TOKEN_SOFT_PEM){              KeyStore ks = generateToken(administrator, username, password, data.getCAId(), keylength, false, loadkeys, savekeys);              sendPEMTokens(ks, username, password, response);            }            if(tokentype == SecConst.TOKEN_SOFT_BROWSERGEN){              // first check if it is a netcsape request,              if (request.getParameter("keygen") != null) {                  byte[] reqBytes=request.getParameter("keygen").getBytes();                  log.debug("Received NS request:"+new String(reqBytes));                  if (reqBytes != null) {                      byte[] certs = helper.nsCertRequest(signsession, reqBytes, username, password);                      RequestHelper.sendNewCertToNSClient(certs, response);                  }              } else if ( (request.getParameter("pkcs10") != null) || (request.getParameter("PKCS10") != null) ) {                  // if not netscape, check if it's IE                  byte[] reqBytes=request.getParameter("pkcs10").getBytes();                  if (reqBytes == null)                      reqBytes=request.getParameter("PKCS10").getBytes();                  log.debug("Received IE request:"+new String(reqBytes));                  if (reqBytes != null) {                      byte[] b64cert=helper.pkcs10CertRequest(signsession, reqBytes, username, password, RequestHelper.ENCODED_PKCS7);                      debug.ieCertFix(b64cert);                      RequestHelper.sendNewCertToIEClient(b64cert, response.getOutputStream(), getServletContext(), getInitParameter("responseTemplate"),classid);                  }              } else if (request.getParameter("pkcs10req") != null && resulttype != 0) {                  // if not IE, check if it's manual request                  byte[] reqBytes=request.getParameter("pkcs10req").getBytes();                  if (reqBytes != null) {                      byte[] b64cert=helper.pkcs10CertRequest(signsession, reqBytes, username, password, resulttype);                      if(resulttype == RequestHelper.ENCODED_PKCS7)                          RequestHelper.sendNewB64Cert(b64cert, response, RequestHelper.BEGIN_PKCS7_WITH_NL, RequestHelper.END_PKCS7_WITH_NL);                      if(resulttype == RequestHelper.ENCODED_CERTIFICATE)                        RequestHelper.sendNewB64Cert(b64cert, response, RequestHelper.BEGIN_CERTIFICATE_WITH_NL, RequestHelper.END_CERTIFICATE_WITH_NL);                  }              }            }        } catch (ObjectNotFoundException oe) {            log.debug("Non existent username!");            debug.printMessage("Non existent username!");            debug.printMessage(                "To generate a certificate a valid username and password must be entered.");            debug.printDebugInfo();            return;        } catch (AuthStatusException ase) {            log.debug("Wrong user status!");            debug.printMessage("Wrong user status!");            if (usekeyrecovery) {                debug.printMessage(                    "To generate a certificate for a user the user must have status new, failed or inprocess.");            } else {                debug.printMessage(                    "To generate a certificate for a user the user must have status new, failed or inprocess.");            }            debug.printDebugInfo();            return;        } catch (AuthLoginException ale) {            log.debug("Wrong password for user!");            debug.printMessage("Wrong username or password!");            debug.printMessage(                "To generate a certificate a valid username and password must be entered.");            debug.printDebugInfo();            return;        } catch (SignRequestException re) {            log.debug("Invalid request!");            debug.printMessage("Invalid request!");            debug.printMessage("Please supply a correct request.");            debug.printDebugInfo();            return;        } catch (SignRequestSignatureException se) {            log.debug("Invalid signature on certificate request!");            debug.printMessage("Invalid signature on certificate request!");            debug.printMessage("Please supply a correctly signed request.");            debug.printDebugInfo();            return;        } catch (java.lang.ArrayIndexOutOfBoundsException ae) {            log.debug("Empty or invalid request received.");            debug.printMessage("Empty or invalid request!");            debug.printMessage("Please supply a correct request.");            debug.printDebugInfo();            return;        } catch (Exception e) {            log.debug(e);            debug.print("<h3>parameter name and values: </h3>");            Enumeration paramNames = request.getParameterNames();            while (paramNames.hasMoreElements()) {                String name = paramNames.nextElement().toString();                String parameter = request.getParameter(name);                debug.print("<h4>" + name + ":</h4>" + parameter + "<br>");            }            debug.takeCareOfException(e);            debug.printDebugInfo();        }    }