⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 caadminsessionbean.java

📁 JAVA做的J2EE下CA认证系统 基于EJB开发
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
            }catch(Exception e){                throw new EJBException(e);            }        }        return signsession;    } //getSignSession    /**     * @ejb.interface-method     * @jboss.method-attributes transaction-timeout="900"     */    public void createCA(Admin admin, CAInfo cainfo) throws CAExistsException, AuthorizationDeniedException, CATokenOfflineException, CATokenAuthenticationFailedException{    	int castatus = SecConst.CA_OFFLINE;        // Check that administrat has superadminsitrator rights.        try{            getAuthorizationSession().isAuthorizedNoLog(admin,"/super_administrator");        }catch(AuthorizationDeniedException ade){            getLogSession().log (admin, admin.getCaId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,"Administrator isn't authorized to create CA",ade);            throw new AuthorizationDeniedException("Administrator not authorized to create CA");        }                // Check that CA doesn't already exists        try{            int caid = cainfo.getCAId();            if(caid >=0 && caid <= CAInfo.SPECIALCAIDBORDER){                getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CACREATED,"CA already exists.");                throw new CAExistsException();            }            cadatahome.findByPrimaryKey(new Integer(caid));            getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CACREATED,"CA already exists.");            throw new CAExistsException();        }catch(javax.ejb.FinderException fe) {}        try{            cadatahome.findByName(cainfo.getName());            getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CACREATED,"CA name already exists.");            throw new CAExistsException("CA Name already exists");        }catch(javax.ejb.FinderException fe) {}        // Create CAToken        CAToken catoken = null;        CATokenInfo catokeninfo = cainfo.getCATokenInfo();        if(catokeninfo instanceof SoftCATokenInfo){            try{                catoken = new SoftCAToken();                ((SoftCAToken) catoken).generateKeys(catokeninfo);            }catch(Exception e){                getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CACREATED,"Error when creating CA token.",e);                throw new EJBException(e);            }        }        if(catokeninfo instanceof HardCATokenInfo){        	catoken = new HardCATokenContainer();        	((HardCATokenContainer) catoken).updateCATokenInfo(catokeninfo);        	try{        		catoken.activate(((HardCATokenInfo) catokeninfo).getAuthenticationCode());        	}catch(CATokenAuthenticationFailedException ctaf){        		getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CACREATED,"Error when creating hard CA token. Authorization code was wrong.",ctaf);        		throw ctaf;        	}catch(CATokenOfflineException ctoe){        		getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CACREATED,"Error when creating hard CA token. CA token is offline.",ctoe);        		throw ctoe;        	}        }        // Create CA        CA ca = null;        if(cainfo instanceof X509CAInfo){            X509CAInfo x509cainfo = (X509CAInfo) cainfo;            // Create X509CA            ca = new X509CA((X509CAInfo) cainfo);            X509CA x509ca = (X509CA) ca;            ca.setCAToken(catoken);            // Create Certificate Chain            Collection certificatechain = null;            // getCertificateProfile            CertificateProfile certprofile = getCertificateStoreSession().getCertificateProfile(admin,cainfo.getCertificateProfileId());            if(x509cainfo.getPolicyId() != null){              certprofile.setUseCertificatePolicies(true);              certprofile.setCertificatePolicyId(x509cainfo.getPolicyId());            }else{              if(certprofile.getUseCertificatePolicies())                x509ca.setPolicyId(certprofile.getCertificatePolicyId());            }            if(cainfo.getSignedBy() == CAInfo.SELFSIGNED){              try{                // create selfsigned certificate                Certificate cacertificate = null;                log.debug("CAAdminSessionBean : " + cainfo.getSubjectDN());                UserDataVO cadata = new UserDataVO("nobody", cainfo.getSubjectDN(), cainfo.getSubjectDN().hashCode(), x509cainfo.getSubjectAltName(), null,                                                      0,0,0,  cainfo.getCertificateProfileId(), null, null, 0, 0, null);                                cacertificate = ca.generateCertificate(cadata, catoken.getPublicKey(SecConst.CAKEYPURPOSE_CERTSIGN),-1, cainfo.getValidity(), certprofile);                log.debug("CAAdminSessionBean : " + ((X509Certificate) cacertificate).getSubjectDN().toString());                // Build Certificate Chain                certificatechain = new ArrayList();                certificatechain.add(cacertificate);                // set status to active                castatus = SecConst.CA_ACTIVE;              }catch(CATokenOfflineException e){              	getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CACREATED,"Couldn't Create Root CA.",e);            	throw e;              }catch(Exception fe){                 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CACREATED,"Couldn't Create Root CA.",fe);                 throw new EJBException(fe);              }            }            if(cainfo.getSignedBy() == CAInfo.SIGNEDBYEXTERNALCA){				certificatechain = new ArrayList();                // set status to waiting certificate response.				castatus = SecConst.CA_WAITING_CERTIFICATE_RESPONSE;            }            if(cainfo.getSignedBy() > CAInfo.SPECIALCAIDBORDER || cainfo.getSignedBy() < 0){                // Create CA signed by other internal CA.            	try{            		CADataLocal signcadata = cadatahome.findByPrimaryKey(new Integer(cainfo.getSignedBy()));            		CA signca = signcadata.getCA();            		//Check that the signer is valid            		checkSignerValidity(admin, signcadata);            		// Create cacertificate            		Certificate cacertificate = null;            		UserDataVO cadata = new UserDataVO("nobody", cainfo.getSubjectDN(), cainfo.getSubjectDN().hashCode(), x509cainfo.getSubjectAltName(), null,            				0, 0, 0, cainfo.getCertificateProfileId(),null, null, 0, 0, null);            		            		cacertificate = signca.generateCertificate(cadata, catoken.getPublicKey(SecConst.CAKEYPURPOSE_CERTSIGN), -1, cainfo.getValidity(), certprofile);            		// Build Certificate Chain            		Collection rootcachain = signca.getCertificateChain();            		certificatechain = new ArrayList();            		certificatechain.add(cacertificate);            		certificatechain.addAll(rootcachain);            		// set status to active            		castatus = SecConst.CA_ACTIVE;            	}catch(CATokenOfflineException e){            		getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CACREATED,"Couldn't Create Sub CA.",e);            		throw e;            	}catch(Exception fe){            		getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CACREATED,"Couldn't Create Sub CA.",fe);            		throw new EJBException(fe);            	}            }            // Set Certificate Chain            x509ca.setCertificateChain(certificatechain);        }        //	Publish CA certificates.           int certtype = CertificateDataBean.CERTTYPE_SUBCA;           if(ca.getSignedBy() == CAInfo.SELFSIGNED)          	  certtype = CertificateDataBean.CERTTYPE_ROOTCA;           getSignSession().publishCACertificate(admin, ca.getCertificateChain(), ca.getCRLPublishers(), certtype);		     if(castatus ==SecConst.CA_ACTIVE){		     	// activate External CA Services		     	Iterator iter = cainfo.getExtendedCAServiceInfos().iterator();		     	while(iter.hasNext()){		     	  ExtendedCAServiceInfo info = (ExtendedCAServiceInfo) iter.next();		     	  if(info instanceof OCSPCAServiceInfo){		     	  	try{		     	  	  ca.initExternalService(OCSPCAService.TYPE, ca);		     	  	  ArrayList ocspcertificate = new ArrayList();		     	  	  ocspcertificate.add(((OCSPCAServiceInfo) ca.getExtendedCAServiceInfo(OCSPCAService.TYPE)).getOCSPSignerCertificatePath().get(0));					  getSignSession().publishCACertificate(admin, ocspcertificate, ca.getCRLPublishers(), CertificateDataBean.CERTTYPE_ENDENTITY);				    }catch(Exception fe){					  getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CACREATED,"Couldn't Create ExternalCAService.",fe);					  throw new EJBException(fe);				    }		     	  }		     	}		     }        // Store CA in database.       try{            cadatahome.create(cainfo.getSubjectDN(), cainfo.getName(), castatus, ca);              if(castatus == SecConst.CA_ACTIVE){                //  create initial CRL                this.getCRLCreateSession().run(admin,cainfo.getSubjectDN());              }            getLogSession().log(admin, ca.getCAId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_INFO_CACREATED,"CA created successfully, status: " + castatus);        }catch(javax.ejb.CreateException e){            getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CACREATED,"Error when trying to create CA.");            throw new EJBException(e);        }    } // createCA    /**     * @ejb.interface-method     */    public void editCA(Admin admin, CAInfo cainfo) throws AuthorizationDeniedException{        boolean ocsprenewcert = false;        // Check authorization        try{            getAuthorizationSession().isAuthorizedNoLog(admin,"/super_administrator");        }catch(AuthorizationDeniedException e){            getLogSession().log(admin, cainfo.getCAId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,"Administrator isn't authorized to edit CA",e);            throw new AuthorizationDeniedException("Not authorized to edit CA with caid = " + cainfo.getCAId());        }        // Check if OCSP Certificate is about to be renewed.        Iterator iter = cainfo.getExtendedCAServiceInfos().iterator();        while(iter.hasNext()){          Object next = iter.next();          if(next instanceof OCSPCAServiceInfo)            ocsprenewcert = ((OCSPCAServiceInfo) next).getRenewFlag();        }        // Get CA from database        try{            CADataLocal cadata = cadatahome.findByPrimaryKey(new Integer(cainfo.getCAId()));            CA ca = cadata.getCA();            // Update CA values            ca.updateCA(cainfo);            // Store CA in database            cadata.setCA(ca);            // If OCSP Certificate renew, publish the new one.            if(ocsprenewcert){              X509Certificate ocspcert = (X509Certificate) ((OCSPCAServiceInfo)                                         ca.getExtendedCAServiceInfo(ExtendedCAServiceInfo.TYPE_OCSPEXTENDEDSERVICE))                                         .getOCSPSignerCertificatePath().get(0);			  ArrayList ocspcertificate = new ArrayList();              ocspcertificate.add(ocspcert);              getSignSession().publishCACertificate(admin, ocspcertificate, ca.getCRLPublishers(), CertificateDataBean.CERTTYPE_ENDENTITY);            }            // Log Action            getLogSession().log(admin, cainfo.getCAId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_INFO_CAEDITED,"");        }catch(Exception fe) {            getLogSession().log(admin, cainfo.getCAId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,"Couldn't Edit CA.",fe);            throw new EJBException(fe);        }    } // editCA    /**     * @ejb.interface-method     */    public void removeCA(Admin admin, int caid) throws AuthorizationDeniedException{        // check authorization        try{            getAuthorizationSession().isAuthorizedNoLog(admin,"/super_administrator");        }catch(AuthorizationDeniedException e){            getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,"Administrator isn't authorized to remove CA",e);            throw new AuthorizationDeniedException("Not authorized to remove CA with caid = " + caid);        }        // Get CA from database        try{            CADataLocal cadata = cadatahome.findByPrimaryKey(new Integer(caid));            // Remove CA            cadata.remove();            // Remove an eventual CA token from the token registry            HardCATokenManager.instance().addCAToken(caid, null);            getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_INFO_CAEDITED,"CA Removed");        }catch(Exception e) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -