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

📄 localcertificatestoresessionbean.java

📁 用来生成java证书
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     * <p/>     * <code>     * ...     * ICertificateStoreSessionRemote itf = ...     * Collection certs = itf.findCertificatesByType(adm,     * CertificateDataBean.CERTTYPE_SUBCA     * + CERTTYPE_ROOTCA,     * null);     * ...     * </code>     * </li>     * </ol>     *     * @param admin     * @param issuerDN get all certificates issued by a specific issuer.     *                 If <tt>null</tt> or empty return certificates regardless of     *                 the issuer.     * @param type     CERTTYPE_* types from CertificateDataBean     * @return Collection Collection of X509Certificate, never <tt>null</tt>     * @ejb.interface-method     */    public Collection findCertificatesByType(Admin admin, int type, String issuerDN) {        return CertificateDataUtil.findCertificatesByType(admin, type, issuerDN, certHome, adapter);    } // findCertificatesByType    /**     * Set the status of certificates of given dn to revoked.     *     * @param admin      Administrator performing the operation     * @param username   the username of user to revoke certificates.     * @param publishers and array of publiserids (Integer) of publishers to revoke the certificate in.     * @param reason     the reason of the revokation. (One of the RevokedCertInfo.REVOKATION_REASON     *                   constants.)     * @ejb.transaction type="Required"     * @ejb.interface-method     */    public void setRevokeStatus(Admin admin, String username, Collection publishers, int reason) {        debug(">setRevokeStatus(),  username=" + username);        // Strip dangerous chars        username = StringTools.strip(username);        try {            Collection certs = findCertificatesByUsername(admin, username);            // Revoke all certs            if (!certs.isEmpty()) {                Iterator j = certs.iterator();                while (j.hasNext()) {        	        setRevokeStatus(admin, (Certificate) j.next(), publishers, reason);                }            }        } catch (FinderException e) {        	String msg = intres.getLocalizedMessage("store.errorfindcertuser", username);            	            getLogSession().log(admin, admin.getCaId(), LogConstants.MODULE_CA, new java.util.Date(), null, null, LogConstants.EVENT_ERROR_REVOKEDCERT, msg);            throw new EJBException(e);        }        debug("<setRevokeStatus(),  username=" + username);    } // setRevokeStatus    /**     * Set the status of certificate with  given serno to revoked.     *     * @param admin      Administrator performing the operation     * @param serno      the serno of certificate to revoke.     * @param publishers and array of publiserids (Integer) of publishers to revoke the certificate in.     * @param reason     the reason of the revokation. (One of the RevokedCertInfo.REVOKATION_REASON constants.)     * @ejb.transaction type="Required"     * @ejb.interface-method     */    public void setRevokeStatus(Admin admin, String issuerdn, BigInteger serno, Collection publishers, int reason) {        debug(">setRevokeStatus(),  issuerdn=" + issuerdn + ", serno=" + serno.toString(16));        Certificate certificate = null;        try {            certificate = (Certificate) this.findCertificateByIssuerAndSerno(admin, issuerdn, serno);	        setRevokeStatus(admin, certificate, publishers, reason);        } catch (FinderException e) {        	String msg = intres.getLocalizedMessage("store.errorfindcertserno", serno.toString(16));            	            getLogSession().log(admin, issuerdn.hashCode(), LogConstants.MODULE_CA, new java.util.Date(), null, null, LogConstants.EVENT_ERROR_REVOKEDCERT, msg);            throw new EJBException(e);        }        debug("<setRevokeStatus(),  issuerdn=" + issuerdn + ", serno=" + serno.toString(16));    } // setRevokeStatus    /**     * Helper method to set the status of certificate to revoked or active.     *     * @param admin      Administrator performing the operation     * @param certificate the certificate to revoke or activate.     * @param publishers and array of publiserids (Integer) of publishers to revoke/re-publish the certificate in.     * @param reason     the reason of the revokation. (One of the RevokedCertInfo.REVOKATION_REASON constants.)     * @throws FinderException      */    private void setRevokeStatus(Admin admin, Certificate certificate, Collection publishers, int reason) throws FinderException {    	if (certificate == null) {    		return;    	}    	debug(">setRevokeStatus(Certificate),  issuerdn=" + CertTools.getIssuerDN(certificate) + ", serno=" + CertTools.getSerialNumberAsString(certificate));    	CertificateDataPK revpk = new CertificateDataPK();    	revpk.fingerprint = CertTools.getFingerprintAsString(certificate);    	CertificateDataLocal rev = certHome.findByPrimaryKey(revpk);     	String username = rev.getUsername();    	String serialNo = CertTools.getSerialNumber(certificate).toString(16); // for logging    	if ( (rev.getStatus() != CertificateDataBean.CERT_REVOKED)     			&& (reason != RevokedCertInfo.NOT_REVOKED) && (reason != RevokedCertInfo.REVOKATION_REASON_REMOVEFROMCRL) ) {    		rev.setStatus(CertificateDataBean.CERT_REVOKED);    		rev.setRevocationDate(new Date());    		rev.setRevocationReason(reason);            	      		String msg = intres.getLocalizedMessage("store.revokedcert", new Integer(reason));            	    		getLogSession().log(admin, certificate, LogConstants.MODULE_CA, new java.util.Date(), null, certificate, LogConstants.EVENT_INFO_REVOKEDCERT, msg);    		// Revoke in all related publishers    		if (publishers != null) {    			getPublisherSession().revokeCertificate(admin, publishers, certificate, username, reason);    		}            	      	} else if ( ((reason == RevokedCertInfo.NOT_REVOKED) || (reason == RevokedCertInfo.REVOKATION_REASON_REMOVEFROMCRL))     			&& (rev.getRevocationReason() == RevokedCertInfo.REVOKATION_REASON_CERTIFICATEHOLD) ) {    		// Only allow unrevocation if the certificate is revoked and the revocation reason is CERTIFICATE_HOLD    		rev.setStatus(CertificateDataBean.CERT_ACTIVE);    		rev.setRevocationDate(null);    		rev.setRevocationReason(RevokedCertInfo.NOT_REVOKED);    		// Republish the certificate if possible    		// If it is not possible, only log error but continue the operation of not revoking the certificate    		try {    			CertReqHistory certreqhist = getCertReqHistory(admin, CertTools.getSerialNumber(certificate), CertTools.getIssuerDN(certificate));    			if(certreqhist == null){    				throw new Exception("Unrevoked cert:" + serialNo + " reason: " + reason + " Must not be republished.");    			}    			UserDataVO userdata = certreqhist.getUserDataVO();    			if ( userdata == null ){    				throw new Exception("Unrevoked cert:" + serialNo + " reason: " + reason + " Could not be republished, there ane no UserData in History.");    			}    			CertificateProfile certprofile = getCertificateProfile(admin, userdata.getCertificateProfileId());    			if(certprofile == null){    				throw new Exception("Unrevoked cert:" + serialNo + " reason: " + reason + " Could not be republished, can't find certificate profile.");      			}    			CertificateInfo certinfo = getCertificateInfo(admin, CertTools.getFingerprintAsString(certificate));    			if(certprofile.getPublisherList().size() <= 0){    				throw new Exception("Unrevoked cert:" + serialNo + " reason: " + reason + " Could not be republished, there are no publishers defined.");    			}    			boolean published = publishersession.storeCertificate(admin, certprofile.getPublisherList(), certificate, certreqhist.getUserDataVO().getUsername(), certreqhist.getUserDataVO().getPassword(),    					certinfo.getCAFingerprint(), certinfo.getStatus() , certinfo.getType(), certinfo.getRevocationDate().getTime(), certinfo.getRevocationReason(), certreqhist.getUserDataVO().getExtendedinformation());    			if ( !published ) {    				throw new Exception("Unrevoked cert:" + serialNo + " reason: " + reason + " Could not be republished.");    			}                	      			String msg = intres.getLocalizedMessage("store.republishunrevokedcert", new Integer(reason));            	    			getLogSession().log(admin, CertTools.getIssuerDN(certificate).hashCode(), LogConstants.MODULE_CA, new java.util.Date(), null, certificate, LogConstants.EVENT_INFO_NOTIFICATION, msg);    		} catch (Exception ex) {    			// We catch the exception thrown above, to log the message, but it is only informational, so we dont re-throw anything    			getLogSession().log(admin, CertTools.getIssuerDN(certificate).hashCode(), LogConstants.MODULE_CA, new java.util.Date(), null, certificate, LogConstants.EVENT_INFO_NOTIFICATION, ex.getMessage());    		}    	} else {    		String msg = intres.getLocalizedMessage("store.ignorerevoke", serialNo, new Integer(rev.getStatus()), new Integer(reason));            	    		getLogSession().log(admin, CertTools.getIssuerDN(certificate).hashCode(), LogConstants.MODULE_CA, new java.util.Date(), null, certificate, LogConstants.EVENT_INFO_NOTIFICATION, msg);    	}    	// Update database protection    	if (protect) {    		CertificateInfo entry = new CertificateInfo(rev.getFingerprint(), rev.getCaFingerprint(), rev.getSerialNumber(), rev.getIssuerDN(), rev.getSubjectDN(), rev.getStatus(), rev.getType(), rev.getExpireDate(), rev.getRevocationDate(), rev.getRevocationReason());    		TableProtectSessionLocal protect;    		try {    			protect = protecthome.create();    			protect.protect(admin, entry);            	    		} catch (CreateException e) {    			String msg = intres.getLocalizedMessage("protect.errorcreatesession");            	    			error(msg, e);    		}    	}    	debug("<setRevokeStatus(),  issuerdn=" + CertTools.getIssuerDN(certificate) + ", serno=" + CertTools.getSerialNumber(certificate).toString(16));    } // setRevokeStatus    /**     * Revokes a certificate (already revoked by the CA), in the database     *     * @param cert       The DER coded Certificate that has been revoked.     * @param publishers and array of publiserids (Integer) of publishers to revoke the certificate in.     * @ejb.transaction type="Required"     * @ejb.interface-method     */    public void revokeCertificate(Admin admin, Certificate cert, Collection publishers, int reason) {        if (cert instanceof X509Certificate) {            setRevokeStatus(admin, CertTools.getIssuerDN(cert), CertTools.getSerialNumber(cert), publishers, reason);        }    } //revokeCertificate    /**     * Method revoking all certificates generated by the specified issuerdn. Sets revokedate to current time.     * Should only be called by CAAdminBean when a CA is about to be revoked.     *     * @param admin    the administrator performing the event.     * @param issuerdn the dn of CA about to be revoked     * @param reason   the reason of revokation.     * @ejb.transaction type="Required"     * @ejb.interface-method     */    public void revokeAllCertByCA(Admin admin, String issuerdn, int reason) {        Connection con = null;        PreparedStatement ps = null;        PreparedStatement ps2 = null;        int temprevoked = 0;        int revoked = 0;        String bcdn = CertTools.stringToBCDNString(issuerdn);        final String firstsqlstatement = "UPDATE CertificateData SET status=?" +                " WHERE issuerDN=? AND status = ? ";        final String secondsqlstatement = "UPDATE CertificateData SET status=?, revocationDate=?, revocationReason=?" +                " WHERE issuerDN=? AND status <> ?";        long currentdate = new Date().getTime();        try {            // First SQL statement, changing all temporaty revoked certificates to permanently revoked certificates            con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE);            ps = con.prepareStatement(firstsqlstatement);            ps.setInt(1, CertificateDataBean.CERT_REVOKED); // first statusfield            ps.setString(2, bcdn); // issuerdn field            ps.setInt(3, CertificateDataBean.CERT_TEMP_REVOKED); // second statusfield            temprevoked = ps.executeUpdate();            // Second SQL statement, revoking all non revoked certificates.            ps2 = con.prepareStatement(secondsqlstatement);            ps2.setInt(1, CertificateDataBean.CERT_REVOKED); // first statusfield            ps2.setLong(2, currentdate); // revokedate field            ps2.setInt(3, reason); // revokation reason            ps2.setString(4, bcdn); // issuer dn            ps2.setInt(5, CertificateDataBean.CERT_REVOKED); // second statusfield            revoked = ps2.executeUpdate();    		String msg = intres.getLocalizedMessage("store.revokedallbyca", issuerdn, new Integer(revoked + temprevoked), new Integer(reason));            	            getLogSession().log(admin, bcdn.hashCode(), LogConstants.MODULE_CA, new java.util.Date(), null, null, LogConstants.EVENT_INFO_REVOKEDCERT, msg);        } catch (Exception e) {    		String msg = intres.getLocalizedMessage("store.errorrevokeallbyca", issuerdn);            	            getLogSession().log(admin, bcdn.hashCode(), LogConstants.MODULE_CA, new java.util.Date(), null, null, LogConstants.EVENT_ERROR_REVOKEDCERT, msg, e);            throw new EJBException(e);        } finally {            JDBCUtil.close(con, ps, null);            JDBCUtil.close(ps2);        }    } // revokeAllCertByCA    /**     * Method that checks if a users all certificates have been revoked.     *     * @param admin    Administrator performing the operation     * @param username the username to check for.     * @return returns true if all certificates are revoked.     * @ejb.interface-method     */    public boolean checkIfAllRevoked(Admin admin, String username) {        boolean returnval = true;        Certificate certificate = null;        // Strip dangerous chars        username = StringTools.strip(username);        try {            Collection certs = findCertificatesByUsername(admin, username);            // Revoke all certs            if (!certs.isEmpty()) {                Iterator j = certs.iterator();                while (j.hasNext()) {                    CertificateDataPK revpk = new CertificateDataPK();                    certificate = (Certificate) j.next();                    revpk.fingerprint = CertTools.getFingerprintAsString(certificate);                    CertificateDataLocal rev = certHome.findByPrimaryKey(revpk);                    if (protect) {                		CertificateInfo entry = new CertificateInfo(rev.getFingerprint(), rev.getCaFingerprint(), rev.getSerialNumber(), rev.getIssuerDN(), rev.getSubjectDN(), rev.getStatus(), rev.getType(), rev.getExpireDate(), rev.getRevocationDate(), rev.getRevocationReason());                    	TableProtectSessionLocal protect;                    	try {                    		protect = protecthome.create();                    		// The verify method will log failed verifies itself                    		TableVerifyResult res = protect.verify(entry);                    		if (res.getResultCode() != TableVerifyResult.VERIFY_SUCCESS) {                    			//error("Verify failed, but we go on anyway.");                    		}                    	} catch (CreateException e) {                        	String msg = intres.getLocalizedMessage("protect.errorcreatesession");            	                    		error(msg, e);                    	}                    }                    if (rev.getStatus() != CertificateDataBean.CERT_REVOKED) {                        returnval = false;                    }                }            }        } catch (FinderException e) {            throw new EJBException(e);        }        return returnval;    }    /**

⌨️ 快捷键说明

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