1 13 14 package org.ejbca.core.ejb.ca.store; 15 16 import java.math.BigInteger ; 17 import java.security.cert.Certificate ; 18 import java.security.cert.X509Certificate ; 19 import java.sql.Connection ; 20 import java.sql.PreparedStatement ; 21 import java.sql.ResultSet ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Date ; 25 import java.util.Iterator ; 26 27 import javax.ejb.CreateException ; 28 import javax.ejb.EJBException ; 29 import javax.ejb.FinderException ; 30 31 import org.apache.log4j.Logger; 32 import org.ejbca.core.ejb.JNDINames; 33 import org.ejbca.core.ejb.protect.TableProtectSessionLocal; 34 import org.ejbca.core.ejb.protect.TableProtectSessionLocalHome; 35 import org.ejbca.core.model.InternalResources; 36 import org.ejbca.core.model.ca.crl.RevokedCertInfo; 37 import org.ejbca.core.model.ca.store.CertificateInfo; 38 import org.ejbca.core.model.log.Admin; 39 import org.ejbca.core.model.log.LogEntry; 40 import org.ejbca.core.model.protect.TableVerifyResult; 41 import org.ejbca.util.CertTools; 42 import org.ejbca.util.JDBCUtil; 43 import org.ejbca.util.StringTools; 44 45 51 public class CertificateDataUtil { 52 53 private static final InternalResources intres = InternalResources.getInstance(); 54 55 public interface Adapter { 56 void debug( String s ); 57 void error( String s ); 58 void error( String s, Exception e ); 59 Logger getLogger(); 60 void log(Admin admin, int caid, int module, Date time, String username, 61 X509Certificate certificate, int event, String comment); 62 } 63 public static Certificate findCertificateByFingerprint(Admin admin, String fingerprint, 64 CertificateDataLocalHome certHome, 65 Adapter adapter) { 66 adapter.debug(">findCertificateByFingerprint()"); 67 Certificate ret = null; 68 69 try { 70 CertificateDataLocal res = certHome.findByPrimaryKey(new CertificateDataPK(fingerprint)); 71 ret = res.getCertificate(); 72 adapter.debug("<findCertificateByFingerprint()"); 73 } catch (FinderException fe) { 74 } catch (Exception e) { 76 adapter.getLogger().error("Error finding certificate with fp: " + fingerprint); 77 throw new EJBException (e); 78 } 79 return ret; 80 } 82 public static Certificate findCertificateByIssuerAndSerno(Admin admin, String issuerDN, BigInteger serno, CertificateDataLocalHome certHome, Adapter adapter) { 83 if (adapter.getLogger().isDebugEnabled()) { 84 adapter.debug(">findCertificateByIssuerAndSerno(), dn:" + issuerDN + ", serno=" + serno); 85 } 86 String dn = CertTools.stringToBCDNString(issuerDN); 88 dn = StringTools.strip(dn); 89 if (adapter.getLogger().isDebugEnabled()) { 90 adapter.debug("Looking for cert with (transformed)DN: " + dn); 91 } 92 try { 93 Collection coll = certHome.findByIssuerDNSerialNumber(dn, serno.toString()); 94 Certificate ret = null; 95 if (coll != null) { 96 if (coll.size() > 1) 97 adapter.log(admin, issuerDN.hashCode(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_DATABASE, "Error in database, more than one certificate has the same Issuer : " + issuerDN + " and serialnumber " 98 + serno.toString(16) + "."); 99 Iterator iter = coll.iterator(); 100 if (iter.hasNext()) { 101 ret = ((CertificateDataLocal) iter.next()).getCertificate(); 102 } 103 } 104 if (adapter.getLogger().isDebugEnabled()) { 105 adapter.debug("<findCertificateByIssuerAndSerno(), dn:" + issuerDN + ", serno=" + serno); 106 } 107 return ret; 108 } catch (Exception fe) { 109 throw new EJBException (fe); 110 } 111 } 113 public static Collection findCertificatesByType(Admin admin, int type, String issuerDN, 114 CertificateDataLocalHome certHome, 115 Adapter adapter) { 116 adapter.debug(">findCertificatesByType()"); 117 if (null == admin 118 || type <= 0 119 || type > CertificateDataBean.CERTTYPE_SUBCA + CertificateDataBean.CERTTYPE_ENDENTITY + CertificateDataBean.CERTTYPE_ROOTCA) { 120 throw new IllegalArgumentException (); 121 } 122 StringBuffer ctypes = new StringBuffer (); 123 if ((type & CertificateDataBean.CERTTYPE_SUBCA) > 0) { 124 ctypes.append(CertificateDataBean.CERTTYPE_SUBCA); 125 } 126 if ((type & CertificateDataBean.CERTTYPE_ENDENTITY) > 0) { 127 if (ctypes.length() > 0) { 128 ctypes.append(", "); 129 } 130 ctypes.append(CertificateDataBean.CERTTYPE_ENDENTITY); 131 } 132 if ((type & CertificateDataBean.CERTTYPE_ROOTCA) > 0) { 133 if (ctypes.length() > 0) { 134 ctypes.append(", "); 135 } 136 ctypes.append(CertificateDataBean.CERTTYPE_ROOTCA); 137 } 138 139 Connection con = null; 140 PreparedStatement ps = null; 141 ResultSet result = null; 142 try { 143 ArrayList vect; 144 StringBuffer stmt = new StringBuffer ("SELECT DISTINCT fingerprint FROM CertificateData WHERE status = "+CertificateDataBean.CERT_ACTIVE+" AND "); 146 stmt.append(" type IN ("); 147 stmt.append(ctypes.toString()); 148 stmt.append(')'); 149 if (null != issuerDN && issuerDN.length() > 0) { 150 String dn = CertTools.stringToBCDNString(issuerDN); 151 dn = StringTools.strip(dn); 152 if (adapter.getLogger().isDebugEnabled()) { 153 adapter.debug("findCertificatesByType() : Looking for cert with (transformed)DN: " + dn); 154 } 155 stmt.append(" AND issuerDN = '"); 156 stmt.append(dn); 157 stmt.append('\''); 158 } 159 if (adapter.getLogger().isDebugEnabled()) { 160 adapter.debug("findCertificatesByType() : executing SQL statement\n" 161 + stmt.toString()); 162 } 163 con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE); 164 ps = con.prepareStatement(stmt.toString()); 165 result = ps.executeQuery(); 166 167 vect = new ArrayList (); 168 while (result.next()) { 169 Certificate cert = findCertificateByFingerprint(admin, result.getString(1), 170 certHome, adapter); 171 if (cert != null) { 172 vect.add(cert); 173 } 174 } 175 176 adapter.debug("<findCertificatesByType()"); 177 return vect; 178 } catch (Exception e) { 179 throw new EJBException (e); 180 } finally { 181 JDBCUtil.close(con, ps, result); 182 } 183 } 185 static public RevokedCertInfo isRevoked(Admin admin, String issuerDN, BigInteger serno, 186 CertificateDataLocalHome certHome, TableProtectSessionLocalHome protectHome, Adapter adapter) { 187 if (adapter.getLogger().isDebugEnabled()) { 188 adapter.debug(">isRevoked(), dn:" + issuerDN + ", serno=" + serno.toString(16)); 189 } 190 String dn = CertTools.stringToBCDNString(issuerDN); 192 193 try { 194 Collection coll = certHome.findByIssuerDNSerialNumber(dn, serno.toString()); 195 if (coll != null) { 196 if (coll.size() > 1) { 197 String msg = intres.getLocalizedMessage("store.errorseveralissuerserno", issuerDN, serno.toString(16)); 198 adapter.log(admin, issuerDN.hashCode(), LogEntry.MODULE_CA, new java.util.Date (), 199 null, null, LogEntry.EVENT_ERROR_DATABASE, msg); 200 } 201 Iterator iter = coll.iterator(); 202 if (iter.hasNext()) { 203 RevokedCertInfo revinfo = null; 204 CertificateDataLocal data = (CertificateDataLocal) iter.next(); 205 if (protectHome != null) { 206 CertificateInfo entry = new CertificateInfo(data.getFingerprint(), data.getCaFingerprint(), data.getSerialNumber(), data.getIssuerDN(), data.getSubjectDN(), data.getStatus(), data.getType(), data.getExpireDate(), data.getRevocationDate(), data.getRevocationReason()); 207 TableProtectSessionLocal protect; 208 try { 209 protect = protectHome.create(); 210 TableVerifyResult res = protect.verify(entry); 212 if (res.getResultCode() != TableVerifyResult.VERIFY_SUCCESS) { 213 } 215 } catch (CreateException e) { 216 String msg = intres.getLocalizedMessage("protect.errorcreatesession"); 217 adapter.error(msg, e); 218 } 219 } 220 revinfo = new RevokedCertInfo(serno, new Date (data.getRevocationDate()), data.getRevocationReason()); 221 if (data.getStatus() != CertificateDataBean.CERT_REVOKED) { 223 revinfo.setReason(RevokedCertInfo.NOT_REVOKED); 224 } 225 if (adapter.getLogger().isDebugEnabled()) { 226 adapter.debug("<isRevoked() returned " + ((data.getStatus() == CertificateDataBean.CERT_REVOKED) ? "yes" : "no")); 227 } 228 return revinfo; 229 } 230 } 231 if (adapter.getLogger().isDebugEnabled()) { 232 adapter.debug("<isRevoked() did not find certificate with dn "+dn+" and serno "+serno.toString(16)); 233 } 234 } catch (Exception e) { 235 throw new EJBException (e); 236 } 237 return null; 238 } } 240 | Popular Tags |