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

📄 localcertificatestoresessionbean.java

📁 JAVA做的J2EE下CA认证系统 基于EJB开发
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
// Make sure this is really a BigInteger passed in as (untrusted param)                    BigInteger serno = (BigInteger) iter.next();                    sb.append(serno.toString());                    sb.append("'");                }            }            /*             * to save the repeating if-statement in the above             * Closure not to add ', ' as the first characters             * in the StringBuffer we remove the two chars here :)             */            sb.delete(0, ", ".length());            con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE);            ps = con.prepareStatement("SELECT DISTINCT fingerprint"                    + " FROM CertificateData WHERE"                    + " issuerDN = ?"                    + " AND serialNumber IN (" + sb.toString() + ")");            ps.setString(1, dn);            result = ps.executeQuery();            vect = new ArrayList();            while (result.next()) {                Certificate cert = findCertificateByFingerprint(admin, result.getString(1));                if (cert != null) {                    vect.add(cert);                }            }            debug("<findCertificateByIssuerAndSernos()");            return vect;        } catch (Exception fe) {            throw new EJBException(fe);        } finally {            JDBCUtil.close(con, ps, result);        }    } // findCertificateByIssuerAndSernos    /**     * Finds certificate(s) for a given serialnumber.     *     * @param admin Administrator performing the operation     * @param serno the serialnumber of the certificate(s) that will be retrieved     * @return Certificate or null if none found.     * @ejb.interface-method     */    public Collection findCertificatesBySerno(Admin admin, BigInteger serno) {        debug(">findCertificateBySerno(),  serno=" + serno);        try {            Collection coll = certHome.findBySerialNumber(serno.toString());            ArrayList ret = new ArrayList();            if (coll != null) {                Iterator iter = coll.iterator();                while (iter.hasNext()) {                    ret.add(((CertificateDataLocal) iter.next()).getCertificate());                }            }            debug("<findCertificateBySerno(), serno=" + serno);            return ret;        } catch (javax.ejb.FinderException fe) {            throw new EJBException(fe);        }    } // findCertificateBySerno    /**     * Finds username for a given certificate serial number.     *     * @param admin Administrator performing the operation     * @param serno the serialnumber of the certificate to find username for.     * @return username or null if none found.     * @ejb.interface-method     */    public String findUsernameByCertSerno(Admin admin, BigInteger serno, String issuerdn) {        debug(">findUsernameByCertSerno(), serno: " + serno + ", issuerdn: " + issuerdn);        String dn = CertTools.stringToBCDNString(issuerdn);        try {            Collection coll = certHome.findByIssuerDNSerialNumber(dn, serno.toString());            String ret = null;            if (coll != null) {                Iterator iter = coll.iterator();                while (iter.hasNext()) {                    ret = ((CertificateDataLocal) iter.next()).getUsername();                }            }            debug("<findUsernameByCertSerno(), ret=" + ret);            return ret;        } catch (javax.ejb.FinderException fe) {            throw new EJBException(fe);        }    } // findUsernameByCertSerno    /**     * Finds certificate(s) for a given usernaem.     *     * @param admin    Administrator performing the operation     * @param username the usernaem of the certificate(s) that will be retrieved     * @return Collection of Certificates (in no specified order) or null if none found.     * @ejb.interface-method     */    public Collection findCertificatesByUsername(Admin admin, String username) {        debug(">findCertificateBySerno(),  username=" + username);        try {            // Strip dangerous chars            username = StringTools.strip(username);            Collection coll = certHome.findByUsername(username);            ArrayList ret = new ArrayList();            if (coll != null) {                Iterator iter = coll.iterator();                while (iter.hasNext()) {                    ret.add(((CertificateDataLocal) iter.next()).getCertificate());                }            }            debug("<findCertificateBySerno(), username=" + username);            return ret;        } catch (javax.ejb.FinderException fe) {            throw new EJBException(fe);        }    } // findCertificateByUsername    /**     * @ejb.interface-method     */    public CertificateInfo getCertificateInfo(Admin admin, String fingerprint) {        debug(">getCertificateInfo()");        CertificateInfo ret = null;        try {            CertificateDataLocal res = certHome.findByPrimaryKey(new CertificateDataPK(fingerprint));            ret = new CertificateInfo(res.getFingerprint(), res.getCAFingerprint(), res.getSerialNumber(), res.getIssuerDN(), res.getSubjectDN(),                    res.getStatus(), res.getType(), res.getExpireDate(), res.getRevocationDate(), res.getRevocationReason());            debug("<getCertificateInfo()");        } catch (FinderException fe) {            // Return null;        } catch (Exception e) {            log.error("Error finding certificate with fp: " + fingerprint);            throw new EJBException(e);        }        return ret;    } // getCertificateInfo    /**     * @ejb.interface-method     */    public Certificate findCertificateByFingerprint(Admin admin, String fingerprint) {        debug(">findCertificateByFingerprint()");        Certificate ret = null;        try {            CertificateDataLocal res = certHome.findByPrimaryKey(new CertificateDataPK(fingerprint));            ret = res.getCertificate();            debug("<findCertificateByFingerprint()");        } catch (FinderException fe) {            // Return null;        } catch (Exception e) {            log.error("Error finding certificate with fp: " + fingerprint);            throw new EJBException(e);        }        return ret;    } // findCertificateByFingerprint    /**     * Lists all active (status = 20) certificates of a specific type and if     * given from a specific issuer.     * <p/>     * The type is the bitwise OR value of the types listed     * int {@link se.anatom.ejbca.ca.store.CertificateDataBean}:<br>     * <ul>     * <li><tt>CERTTYPE_ENDENTITY</tt><br>     * An user or machine certificate, which identifies a subject.     * </li>     * <li><tt>CERTTYPE_CA</tt><br>     * A CA certificate which is <b>not</b> a root CA.     * </li>     * <li><tt>CERTTYPE_ROOTCA</tt><br>     * A Root CA certificate.     * </li>     * </ul>     * <p/>     * Usage examples:<br>     * <ol>     * <li>Get all root CA certificates     * <p/>     * <code>     * ...     * ICertificateStoreSessionRemote itf = ...     * Collection certs = itf.findCertificatesByType(adm,     * CertificateDataBean.CERTTYPE_ROOTCA,     * null);     * ...     * </code>     * </li>     * <li>Get all subordinate CA certificates for a specific     * Root CA. It is assumed that the <tt>subjectDN</tt> of the     * Root CA certificate is located in the variable <tt>issuer</tt>.     * <p/>     * <code>     * ...     * ICertificateStoreSessionRemote itf = ...     * Certficate rootCA = ...     * String issuer = rootCA.getSubjectDN();     * Collection certs = itf.findCertificatesByType(adm,     * CertificateDataBean.CERTTYPE_SUBCA,     * issuer);     * ...     * </code>     * </li>     * <li>Get <b>all</b> CA certificates.     * <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) {        debug(">findCertificatesByType()");        if (null == admin                || type <= 0                || type > CertificateDataBean.CERTTYPE_SUBCA + CertificateDataBean.CERTTYPE_ENDENTITY + CertificateDataBean.CERTTYPE_ROOTCA) {            throw new IllegalArgumentException();        }        StringBuffer ctypes = new StringBuffer();        if ((type & CertificateDataBean.CERTTYPE_SUBCA) > 0) {            ctypes.append(CertificateDataBean.CERTTYPE_SUBCA);        }        if ((type & CertificateDataBean.CERTTYPE_ENDENTITY) > 0) {            if (ctypes.length() > 0) {                ctypes.append(", ");            }            ctypes.append(CertificateDataBean.CERTTYPE_ENDENTITY);        }        if ((type & CertificateDataBean.CERTTYPE_ROOTCA) > 0) {            if (ctypes.length() > 0) {                ctypes.append(", ");            }            ctypes.append(CertificateDataBean.CERTTYPE_ROOTCA);        }        Connection con = null;        PreparedStatement ps = null;        ResultSet result = null;        try {            ArrayList vect;// Status 20 = CertificateDataBean.CERT_ACTIVE            StringBuffer stmt = new StringBuffer("SELECT DISTINCT fingerprint FROM CertificateData WHERE status = 20 AND ");            stmt.append(" type IN (");            stmt.append(ctypes.toString());            stmt.append(')');            if (null != issuerDN && issuerDN.length() > 0) {                String dn = CertTools.stringToBCDNString(issuerDN);                dn = StringTools.strip(dn);                if (log.isDebugEnabled()) {                    debug("findCertificatesByType() : Looking for cert with (transformed)DN: " + dn);                }                stmt.append(" AND issuerDN = '");                stmt.append(dn);                stmt.append('\'');            }            if (log.isDebugEnabled()) {                debug("findCertificatesByType() : executing SQL statement\n"                        + stmt.toString());            }            con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE);            ps = con.prepareStatement(stmt.toString());            result = ps.executeQuery();            vect = new ArrayList();            while (result.next()) {                Certificate cert = findCertificateByFingerprint(admin, result.getString(1));                if (cert != null) {                    vect.add(cert);                }            }            debug("<findCertificatesByType()");            return vect;        } catch (Exception e) {            throw new EJBException(e);        } finally {            JDBCUtil.close(con, ps, result);        }    } // findCertificatesByType

⌨️ 快捷键说明

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