{  EjbcaWebBean ejbcawebbean= getEjbcaWebBean(request); try{ ejbcawebbean.initialize(request, "/ra_functionallity/create_end_entity"); } catch(Exception e){ throw new java.io.IOException("Authorization Denied");
{  EjbcaWebBean ejbcawebbean= getEjbcaWebBean(request); try{ ejbcawebbean.initialize(request, "/ra_functionallity/create_end_entity"); } catch(Exception e){ throw new java.io.IOException("Authorization Denied"); }  X509Certificate[] certs = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate"); if (certs == null) { throw new ServletException("This servlet requires certificate authentication!"); }  Admin admin = new Admin(certs[0]);  byte[] buffer = pkcs10Bytes(request.getParameter("pkcs10req")); if (buffer == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid request, missing 'pkcs10req'!"); return; }  RAInterfaceBean rabean = getRaBean(request);   PKCS10RequestMessage p10 = new PKCS10RequestMessage(buffer); String dn = p10.getCertificationRequest().getCertificationRequestInfo().getSubject().toString();  String username = request.getParameter("username"); if (username == null || username.trim().length() == 0) { username = dn; }  username = StringTools.strip(username);   username = checkUsername(rabean, username);  UserView newuser = new UserView(); newuser.setUsername(username);  newuser.setSubjectDN(dn); newuser.setTokenType(SecConst.TOKEN_SOFT_BROWSERGEN); newuser.setAdministrator(false); newuser.setKeyRecoverable(false);  String email = CertTools.getPartFromDN(dn, "E"); if (email == null) email = CertTools.getPartFromDN(dn, "EMAILADDRESS"); if (email != null) { newuser.setEmail(email); }  String tmp = null; int eProfileId = SecConst.EMPTY_ENDENTITYPROFILE; if ((tmp = request.getParameter("entityprofile")) != null) { int reqId = rabean.getEndEntityProfileId(tmp); if (reqId == 0) { throw new ServletException("No such end entity profile: " + tmp); } eProfileId = reqId; } newuser.setEndEntityProfileId(eProfileId);  int cProfileId = SecConst.CERTPROFILE_FIXED_ENDUSER; if ((tmp = request.getParameter("certificateprofile")) != null) { CAInterfaceBean cabean = getCaBean(request); int reqId = cabean.getCertificateProfileId(tmp); if (reqId == 0) { throw new ServletException("No such certificate profile: " + tmp); } cProfileId = reqId; } newuser.setCertificateProfileId(cProfileId);  int caid = 0; if ((tmp = request.getParameter("ca")) != null) {  } newuser.setCAId(caid);   String password = request.getParameter("password"); if (password == null) password = ""; newuser.setPassword(password); newuser.setClearTextPassword(false);  try { rabean.addUser(newuser); } catch (Exception e) { throw new ServletException("Error adding user: " + e.toString(), e); }  ISignSessionLocal ss; try { ss = signhome.create(); } catch (CreateException e) { throw new ServletException(e); }  byte[] pkcs7; try { p10.setUsername(username); p10.setPassword(password); IResponseMessage resp = ss.createCertificate(admin, p10, Class.forName("se.anatom.ejbca.protocol.X509ResponseMessage")); X509Certificate cert = CertTools.getCertfromByteArray(resp.getResponseMessage()); pkcs7 = ss.createPKCS7(admin, cert, true); } catch (ClassNotFoundException e) {  throw new ServletException(e); } catch (CertificateEncodingException e) {  throw new ServletException(e); } catch (CertificateException e) {  throw new ServletException(e); } catch (ObjectNotFoundException e) {  throw new ServletException(e); } catch (AuthStatusException e) {   throw new ServletException(e); } catch (AuthLoginException e) {   throw new ServletException(e); } catch (IllegalKeyException e) {  throw new ServletException(e); } catch (SignRequestException e) {  throw new ServletException(e); } catch (SignRequestSignatureException e) {  throw new ServletException(e); } catch (CADoesntExistsException e) {  throw new ServletException(e); }  log.debug("Created certificate (PKCS7) for " + username);  sendNewB64Cert(Base64.encode(pkcs7), response);
  public void doPost(HttpServletRequest request, HttpServletResponse response)    throws IOException, ServletException  {    // Check if authorized    EjbcaWebBean ejbcawebbean= getEjbcaWebBean(request);    try{      ejbcawebbean.initialize(request, "/ra_functionallity/create_end_entity");    } catch(Exception e){       throw new java.io.IOException("Authorization Denied");    }    X509Certificate[] certs = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate");    if (certs == null) {        throw new ServletException("This servlet requires certificate authentication!");    }    Admin admin = new Admin(certs[0]);    byte[] buffer = pkcs10Bytes(request.getParameter("pkcs10req"));    if (buffer == null) {        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid request, missing 'pkcs10req'!");        return;    }    RAInterfaceBean rabean = getRaBean(request);    // Decompose the PKCS#10 request, and create the user.    PKCS10RequestMessage p10 = new PKCS10RequestMessage(buffer);    String dn = p10.getCertificationRequest().getCertificationRequestInfo().getSubject().toString();    String username = request.getParameter("username");    if (username == null || username.trim().length() == 0) {      username = dn;    }    // Strip dangerous chars    username = StringTools.strip(username);    // need null check here?    // Before doing anything else, check if the user name is unique and ok.    username = checkUsername(rabean, username);    UserView newuser = new UserView(ejbcawebbean.getInformationMemory().getCAIdToNameMap());    newuser.setUsername(username);    newuser.setSubjectDN(dn);    newuser.setTokenType(SecConst.TOKEN_SOFT_BROWSERGEN);    newuser.setAdministrator(false);    newuser.setKeyRecoverable(false);    String email = CertTools.getPartFromDN(dn, "E"); // BC says VeriSign    if (email == null) email = CertTools.getPartFromDN(dn, "EMAILADDRESS");    if (email != null) {      newuser.setEmail(email);    }    String tmp = null;    int eProfileId = SecConst.EMPTY_ENDENTITYPROFILE;    if ((tmp = request.getParameter("entityprofile")) != null) {      int reqId = rabean.getEndEntityProfileId(tmp);      if (reqId == 0) {        throw new ServletException("No such end entity profile: " + tmp);      } else {        eProfileId = reqId;      }    }    newuser.setEndEntityProfileId(eProfileId);    int cProfileId = SecConst.CERTPROFILE_FIXED_ENDUSER;    if ((tmp = request.getParameter("certificateprofile")) != null) {      CAInterfaceBean cabean = getCaBean(request);      int reqId = cabean.getCertificateProfileId(tmp);      if (reqId == 0) {        throw new ServletException("No such certificate profile: " + tmp);      } else {        cProfileId = reqId;      }    }    newuser.setCertificateProfileId(cProfileId);    int caid = 0;    if ((tmp = request.getParameter("ca")) != null) {        // TODO: get requested CA to sign with    }    newuser.setCAId(caid);            String password = request.getParameter("password");    if (password == null) password = "";    newuser.setPassword(password);    newuser.setClearTextPassword(false);    try {      rabean.addUser(newuser);    } catch (Exception e) {      throw new ServletException("Error adding user: " + e.toString(), e);    }    ISignSessionLocal ss;    try {      ss = signhome.create();    } catch (CreateException e) {      throw new ServletException(e);    }    byte[] pkcs7;    try {      p10.setUsername(username);      p10.setPassword(password);      IResponseMessage resp = ss.createCertificate(admin, p10, Class.forName("se.anatom.ejbca.protocol.X509ResponseMessage"));      X509Certificate cert = CertTools.getCertfromByteArray(resp.getResponseMessage());      pkcs7 = ss.createPKCS7(admin, cert, true);    } catch (ClassNotFoundException e) {      // Class not found      throw new ServletException(e);    } catch (CertificateEncodingException e) {      // Error in cert      throw new ServletException(e);    } catch (CertificateException e) {      // Error in cert      throw new ServletException(e);    } catch (ObjectNotFoundException e) {      // User not found      throw new ServletException(e);    } catch (AuthStatusException e) {      // Wrong user status, shouldn't really happen.  The user needs to have      // status of NEW, FAILED or INPROCESS.      throw new ServletException(e);    } catch (AuthLoginException e) {      // Wrong username or password, hmm... wasn't the wrong username caught      // in the objectnotfoundexception above... and this shouldn't happen.      throw new ServletException(e);    } catch (IllegalKeyException e) {      // Malformed key (?)      throw new ServletException(e);    } catch (SignRequestException e) {      // Invalid request      throw new ServletException(e);    } catch (SignRequestSignatureException e) {      // Invalid signature in certificate request      throw new ServletException(e);    } catch (CADoesntExistsException e) {      // Reqqested CA does not exist      throw new ServletException(e);    }    log.debug("Created certificate (PKCS7) for " + username);    sendNewB64Cert(Base64.encode(pkcs7), response);  }