📄 localcertificatestoresessionbean.java
字号:
/************************************************************************* * * * EJBCA: The OpenSource Certificate Authority * * * * This software is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or any later version. * * * * See terms of license at gnu.org. * * * *************************************************************************/ package se.anatom.ejbca.ca.store;import java.math.BigInteger;import java.security.cert.Certificate;import java.security.cert.X509CRL;import java.security.cert.X509Certificate;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Collection;import java.util.Date;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Random;import javax.ejb.CreateException;import javax.ejb.EJBException;import javax.ejb.FinderException;import javax.naming.NamingException;import javax.sql.DataSource;import se.anatom.ejbca.BaseSessionBean;import se.anatom.ejbca.SecConst;import se.anatom.ejbca.authorization.AuthorizationDeniedException;import se.anatom.ejbca.authorization.IAuthorizationSessionLocal;import se.anatom.ejbca.authorization.IAuthorizationSessionLocalHome;import se.anatom.ejbca.ca.crl.RevokedCertInfo;import se.anatom.ejbca.ca.exception.CertificateProfileExistsException;import se.anatom.ejbca.ca.publisher.IPublisherSessionLocal;import se.anatom.ejbca.ca.publisher.IPublisherSessionLocalHome;import se.anatom.ejbca.ca.store.certificateprofiles.*;import se.anatom.ejbca.log.Admin;import se.anatom.ejbca.log.ILogSessionLocal;import se.anatom.ejbca.log.ILogSessionLocalHome;import se.anatom.ejbca.log.LogEntry;import se.anatom.ejbca.util.CertTools;import se.anatom.ejbca.util.StringTools;/** * Stores certificate and CRL in the local database using Certificate and CRL Entity Beans. * Uses JNDI name for datasource as defined in env 'Datasource' in ejb-jar.xml. * * @version $Id: LocalCertificateStoreSessionBean.java,v 1.70 2004/06/02 08:29:43 herrvendil Exp $ */public class LocalCertificateStoreSessionBean extends BaseSessionBean { /** Var holding JNDI name of datasource */ private String dataSource = ""; /** The home interface of Certificate entity bean */ private CertificateDataLocalHome certHome = null; /** The home interface of Certificate Type entity bean */ private CertificateProfileDataLocalHome certprofilehome = null; /** The home interface of CRL entity bean */ private CRLDataLocalHome crlHome = null; /** The local interface of the log session bean */ private ILogSessionLocal logsession = null; /** The local interface of the authorization session bean */ private IAuthorizationSessionLocal authorizationsession = null; /** The local interface of the publisher session bean */ private IPublisherSessionLocal publishersession = null; /** * Default create for SessionBean without any creation Arguments. * * @throws CreateException if bean instance can't be created */ public void ejbCreate() throws CreateException { debug(">ejbCreate()"); dataSource = (String)lookup("java:comp/env/DataSource", java.lang.String.class); debug("DataSource=" + dataSource); crlHome = (CRLDataLocalHome)lookup("java:comp/env/ejb/CRLDataLocal"); certHome = (CertificateDataLocalHome)lookup("java:comp/env/ejb/CertificateDataLocal"); certprofilehome = (CertificateProfileDataLocalHome)lookup("java:comp/env/ejb/CertificateProfileDataLocal"); debug("<ejbCreate()"); } /** Gets connection to Datasource used for manual SQL searches * @return Connection */ private Connection getConnection() throws SQLException, NamingException { DataSource ds = (DataSource)getInitialContext().lookup(dataSource); return ds.getConnection(); } //getConnection /** Gets connection to log session bean */ private ILogSessionLocal getLogSession() { if(logsession == null){ try{ ILogSessionLocalHome logsessionhome = (ILogSessionLocalHome) lookup("java:comp/env/ejb/LogSessionLocal",ILogSessionLocalHome.class); logsession = logsessionhome.create(); }catch(Exception e){ throw new EJBException(e); } } return logsession; } //getLogSession /** Gets connection to authorization session bean * @return Connection */ private IAuthorizationSessionLocal getAuthorizationSession() { if(authorizationsession == null){ try{ IAuthorizationSessionLocalHome authorizationsessionhome = (IAuthorizationSessionLocalHome) lookup("java:comp/env/ejb/AuthorizationSessionLocal",IAuthorizationSessionLocalHome.class); authorizationsession = authorizationsessionhome.create(); }catch(Exception e){ throw new EJBException(e); } } return authorizationsession; } //getAuthorizationSession /** Gets connection to publisher session bean * @return Connection */ private IPublisherSessionLocal getPublisherSession() { if(publishersession == null){ try{ IPublisherSessionLocalHome publishersessionhome = (IPublisherSessionLocalHome) lookup("java:comp/env/ejb/PublisherSessionLocal",IPublisherSessionLocalHome.class); publishersession = publishersessionhome.create(); }catch(Exception e){ throw new EJBException(e); } } return publishersession; } //getPublisherSession /** * Implements ICertificateStoreSession::storeCertificate. Implements a mechanism that uses * Certificate Entity Bean. * * @param admin DOCUMENT ME! * @param incert DOCUMENT ME! * @param username DOCUMENT ME! * @param cafp DOCUMENT ME! * @param status DOCUMENT ME! * @param type DOCUMENT ME! * * @return DOCUMENT ME! */ public boolean storeCertificate(Admin admin, Certificate incert, String username, String cafp, int status, int type) { debug(">storeCertificate(" + cafp + ", " + status + ", " + type + ")"); try { // Strip dangerous chars username = StringTools.strip(username); X509Certificate cert = (X509Certificate) incert; CertificateDataPK pk = new CertificateDataPK(); pk.fingerprint = CertTools.getFingerprintAsString(cert); getLogSession().log(admin, cert, LogEntry.MODULE_CA, new java.util.Date(), username, (X509Certificate) incert, LogEntry.EVENT_INFO_STORECERTIFICATE,""); CertificateDataLocal data1=null; data1 = certHome.create(cert); data1.setUsername(username); data1.setCAFingerprint(cafp); data1.setStatus(status); data1.setType(type); } catch (Exception e) { getLogSession().log(admin, (X509Certificate) incert, LogEntry.MODULE_CA, new java.util.Date(), username, (X509Certificate) incert, LogEntry.EVENT_ERROR_STORECERTIFICATE,""); throw new EJBException(e); } debug("<storeCertificate()"); return true; } // storeCertificate /** * Implements ICertificateStoreSession::storeCRL. Implements a mechanism that uses CRL Entity * Bean. * * @param admin DOCUMENT ME! * @param incrl DOCUMENT ME! * @param cafp DOCUMENT ME! * @param number DOCUMENT ME! * * @return DOCUMENT ME! */ public boolean storeCRL(Admin admin, byte[] incrl, String cafp, int number) { debug(">storeCRL(" + cafp + ", " + number + ")"); try { X509CRL crl = CertTools.getCRLfromByteArray(incrl); CRLDataLocal data1 = crlHome.create(crl, number); data1.setCAFingerprint(cafp); getLogSession().log(admin, crl.getIssuerDN().toString().hashCode(), LogEntry.MODULE_CA, new java.util.Date(), null, null, LogEntry.EVENT_INFO_STORECRL,"Number : " + number + " Fingerprint : " + CertTools.getFingerprintAsString(crl) + "."); } catch (Exception e) { getLogSession().log(admin, ILogSessionLocal.INTERNALCAID, LogEntry.MODULE_CA, new java.util.Date(), null, null, LogEntry.EVENT_ERROR_STORECRL,"Number : " + number + "."); throw new EJBException(e); } debug("<storeCRL()"); return true; } // storeCRL /** * Implements ICertificateStoreSession::listAlLCertificates. Uses select directly from * datasource. * * @param admin DOCUMENT ME! * * @return DOCUMENT ME! */ public Collection listAllCertificates(Admin admin, String issuerdn) { debug(">listAllCertificates()"); Connection con = null; PreparedStatement ps = null; ResultSet result = null; String dn=CertTools.stringToBCDNString(issuerdn); dn = StringTools.strip(dn); try { con = getConnection(); ps = con.prepareStatement("select fingerprint from CertificateData where issuerDN=? ORDER BY expireDate DESC"); ps.setString(1, dn); result = ps.executeQuery(); ArrayList vect = new ArrayList(); while (result.next()) { vect.add(result.getString(1)); } debug("<listAllCertificates()"); return vect; } catch (Exception e) { throw new EJBException(e); } finally { try { if (result != null) result.close(); if (ps != null) ps.close(); if (con!= null) con.close(); } catch(SQLException se) { error("Error cleaning up: ", se); } } } // listAllCertificates /** * Implements ICertificateStoreSession::listRevokedCertificates. Uses select directly from * datasource. * * @param admin DOCUMENT ME! * * @return DOCUMENT ME! */ public Collection listRevokedCertificates(Admin admin, String issuerdn) { debug(">listRevokedCertificates()"); Connection con = null; PreparedStatement ps = null; ResultSet result = null; String dn=CertTools.stringToBCDNString(issuerdn); dn = StringTools.strip(dn); try { // TODO: // This should only list a few thousend certificates at a time, in case there // are really many revoked certificates after some time... con = getConnection(); ps = con.prepareStatement("select fingerprint from CertificateData where status=? and issuerDN=? ORDER BY expireDate DESC"); ps.setInt(1, CertificateData.CERT_REVOKED); ps.setString(2, dn); result = ps.executeQuery(); ArrayList vect = new ArrayList(); while (result.next()) { vect.add(result.getString(1)); } debug("<listRevokedCertificates()"); return vect; } catch (Exception e) { throw new EJBException(e); } finally { try { if (result != null) result.close(); if (ps != null) ps.close(); if (con != null) con.close(); } catch (SQLException se) { error("Error cleaning up: ", se); } } } // listRevokedCertificates /** * Implements ICertificateStoreSession::findCertificatesBySubjectAndIssuer. * * @param admin DOCUMENT ME! * @param subjectDN DOCUMENT ME! * @param issuerDN DOCUMENT ME! * * @return DOCUMENT ME! */ public Collection findCertificatesBySubjectAndIssuer(Admin admin, String subjectDN, String issuerDN) { debug(">findCertificatesBySubjectAndIssuer(), dn='"+subjectDN+"' and issuer='"+issuerDN+"'"); // First make a DN in our well-known format String dn = CertTools.stringToBCDNString(subjectDN); dn = StringTools.strip(dn); String issuerdn = CertTools.stringToBCDNString(issuerDN); issuerdn = StringTools.strip(issuerdn); debug("Looking for cert with (transformed)DN: " + dn); try { Collection coll = certHome.findBySubjectDNAndIssuerDN(dn, issuerdn); Collection ret = new ArrayList(); if (coll != null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -