1 13 14 package org.ejbca.core.ejb.ca.crl; 15 16 import java.math.BigInteger ; 17 import java.security.cert.X509CRL ; 18 import java.util.Collection ; 19 import java.util.Date ; 20 import java.util.Iterator ; 21 import java.util.Vector ; 22 23 import javax.ejb.CreateException ; 24 import javax.ejb.EJBException ; 25 26 import org.ejbca.core.ejb.BaseSessionBean; 27 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionLocal; 28 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionLocalHome; 29 import org.ejbca.core.ejb.ca.sign.ISignSessionLocal; 30 import org.ejbca.core.ejb.ca.sign.ISignSessionLocalHome; 31 import org.ejbca.core.ejb.ca.store.CertificateDataBean; 32 import org.ejbca.core.ejb.ca.store.CertificateDataLocal; 33 import org.ejbca.core.ejb.ca.store.CertificateDataLocalHome; 34 import org.ejbca.core.ejb.ca.store.CertificateDataPK; 35 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionLocal; 36 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionLocalHome; 37 import org.ejbca.core.ejb.log.ILogSessionLocal; 38 import org.ejbca.core.ejb.log.ILogSessionLocalHome; 39 import org.ejbca.core.model.InternalResources; 40 import org.ejbca.core.model.SecConst; 41 import org.ejbca.core.model.ca.caadmin.CADoesntExistsException; 42 import org.ejbca.core.model.ca.caadmin.CAInfo; 43 import org.ejbca.core.model.ca.caadmin.X509CAInfo; 44 import org.ejbca.core.model.ca.catoken.CATokenOfflineException; 45 import org.ejbca.core.model.ca.crl.RevokedCertInfo; 46 import org.ejbca.core.model.ca.store.CRLInfo; 47 import org.ejbca.core.model.log.Admin; 48 import org.ejbca.core.model.log.LogEntry; 49 import org.ejbca.util.CertTools; 50 51 52 129 public class CreateCRLSessionBean extends BaseSessionBean { 130 131 132 private static final InternalResources intres = InternalResources.getInstance(); 133 134 135 private ICertificateStoreSessionLocalHome storeHome = null; 136 137 138 private CertificateDataLocalHome certHome = null; 139 140 141 private ISignSessionLocalHome signHome = null; 142 143 144 private ICAAdminSessionLocalHome caadminHome = null; 145 146 147 private ILogSessionLocal logsession; 148 149 150 153 public void ejbCreate () throws CreateException { 154 caadminHome = (ICAAdminSessionLocalHome)getLocator().getLocalHome(ICAAdminSessionLocalHome.COMP_NAME); 155 storeHome = (ICertificateStoreSessionLocalHome)getLocator().getLocalHome(ICertificateStoreSessionLocalHome.COMP_NAME); 156 certHome = (CertificateDataLocalHome)getLocator().getLocalHome(CertificateDataLocalHome.COMP_NAME); 157 signHome = (ISignSessionLocalHome)getLocator().getLocalHome(ISignSessionLocalHome.COMP_NAME); 158 ILogSessionLocalHome logsessionhome = (ILogSessionLocalHome) getLocator().getLocalHome(ILogSessionLocalHome.COMP_NAME); 159 logsession = logsessionhome.create(); 160 } 161 162 172 public void run(Admin admin, String issuerdn) throws CATokenOfflineException { 173 debug(">run()"); 174 int caid = issuerdn.hashCode(); 175 try { 176 ICAAdminSessionLocal caadmin = caadminHome.create(); 177 ICertificateStoreSessionLocal store = storeHome.create(); 178 179 CAInfo cainfo = caadmin.getCAInfo(admin, caid); 180 if (cainfo == null) { 181 throw new CADoesntExistsException("CA not found: "+issuerdn); 182 } 183 int crlperiod = cainfo.getCRLPeriod(); 184 Collection revcerts = store.listRevokedCertificates(admin, issuerdn); 186 debug("Found "+revcerts.size()+" revoked certificates."); 187 188 Date now = new Date (); 190 now.setTime(now.getTime() - (crlperiod * 60 * 60 * 1000)); 192 Vector certs = new Vector (); 193 Iterator iter = revcerts.iterator(); 194 while (iter.hasNext()) { 195 CertificateDataPK pk = new CertificateDataPK((String )iter.next()); 196 CertificateDataLocal data = certHome.findByPrimaryKey(pk); 197 if ( (data.getStatus() == CertificateDataBean.CERT_REVOKED) && 200 (data.getExpireDate() < now.getTime()) ) 201 { 202 data.setStatus(CertificateDataBean.CERT_ARCHIVED); 203 } else 204 { 205 if (data.getRevocationDate() == -1) 206 data.setRevocationDate((new Date ()).getTime()); 207 RevokedCertInfo certinfo = new RevokedCertInfo(new BigInteger (data.getSerialNumber()),new Date (data.getRevocationDate()), data.getRevocationReason()); 208 certs.add(certinfo); 209 } 210 } 211 ISignSessionLocal sign = signHome.create(); 212 byte[] crlBytes = sign.createCRL(admin, caid, certs); 213 String msg = intres.getLocalizedMessage("createcrl.createdcrl", cainfo.getName(), cainfo.getSubjectDN()); 215 log.info(msg); 216 if (log.isDebugEnabled()) { 217 X509CRL crl = CertTools.getCRLfromByteArray(crlBytes); 218 debug("Created CRL with expire date: "+crl.getNextUpdate()); 219 } 223 224 } catch (CATokenOfflineException e) { 225 throw e; 226 } catch (Exception e) { 227 String msg = intres.getLocalizedMessage("createcrl.errorcreate", new Integer (caid)); 228 log.error(msg, e); 229 logsession.log(admin, caid, LogEntry.MODULE_CA, new java.util.Date (),null, null, LogEntry.EVENT_ERROR_CREATECRL, msg, e); 230 throw new EJBException (e); 231 } 232 debug("<run()"); 233 } 234 235 236 246 public int createCRLs(Admin admin) { 247 return createCRLs(admin, 0); 248 } 249 250 266 public int createCRLs(Admin admin, long addtocrloverlaptime) { 267 int createdcrls = 0; 268 try { 269 Date currenttime = new Date (); 270 ICAAdminSessionLocal caadmin = caadminHome.create(); 271 ICertificateStoreSessionLocal store = storeHome.create(); 272 273 Iterator iter = caadmin.getAvailableCAs(admin).iterator(); 274 while(iter.hasNext()){ 275 int caid = ((Integer ) iter.next()).intValue(); 276 log.debug("createCRLs for caid: "+caid); 277 try { 278 CAInfo cainfo = caadmin.getCAInfo(admin, caid); 279 if (cainfo instanceof X509CAInfo) { 280 if (cainfo.getStatus() == SecConst.CA_OFFLINE ) { 281 String msg = intres.getLocalizedMessage("createcrl.caoffline", cainfo.getName(), new Integer (caid)); 282 log.error(msg); 283 logsession.log(admin, caid, LogEntry.MODULE_CA, new java.util.Date (),null, null, LogEntry.EVENT_ERROR_CREATECRL, msg); 284 } else { 285 try { 286 if (log.isDebugEnabled()) { 287 log.debug("Checking to see if CA '"+cainfo.getName()+"' needs CRL generation."); 288 } 289 CRLInfo crlinfo = store.getLastCRLInfo(admin,cainfo.getSubjectDN()); 290 if (log.isDebugEnabled()) { 291 if (crlinfo == null) { 292 log.debug("Crlinfo was null"); 293 } else { 294 log.debug("Read crlinfo for CA: "+cainfo.getName()+", lastNumber="+crlinfo.getLastCRLNumber()+", expireDate="+crlinfo.getExpireDate()); 295 } 296 } 297 int crlissueinterval = cainfo.getCRLIssueInterval(); 298 if (log.isDebugEnabled()) { 299 log.debug("crlissueinterval="+crlissueinterval); 300 log.debug("crloverlaptime="+cainfo.getCRLOverlapTime()); 301 } 302 long overlap = (cainfo.getCRLOverlapTime() * 60 * 1000) + addtocrloverlaptime; long nextUpdate = 0; if (crlinfo != null) { 305 nextUpdate = crlinfo.getExpireDate().getTime(); if (crlissueinterval > 0) { 310 long crlissueintervalmillisec = ((long)crlissueinterval) * 60 * 60 * 1000; 311 if (log.isDebugEnabled()) { 312 log.debug("crlissueinterval milliseconds: "+crlissueintervalmillisec); 313 } 314 long u = crlinfo.getCreateDate().getTime() + (crlissueintervalmillisec); 315 if ((u + overlap) < nextUpdate) { 318 nextUpdate = u; 319 overlap = 0; 321 } 322 } 323 log.debug("Calculated nextUpdate to "+nextUpdate); 324 } else { 325 String msg = intres.getLocalizedMessage("createcrl.crlinfonull"); 326 log.info(msg); 327 } 328 if ((currenttime.getTime() + overlap) >= nextUpdate) { 329 if (log.isDebugEnabled()) { 330 log.debug("Creating CRL for CA, because:"+currenttime.getTime()+overlap+" >= "+nextUpdate); 331 } 332 this.run(admin, cainfo.getSubjectDN()); 333 createdcrls++; 334 } 335 336 } catch (CATokenOfflineException e) { 337 String msg = intres.getLocalizedMessage("createcrl.caoffline", cainfo.getName(), new Integer (caid)); 338 log.error(msg); 339 logsession.log(admin, caid, LogEntry.MODULE_CA, new java.util.Date (),null, null, LogEntry.EVENT_ERROR_CREATECRL, msg); 340 } 341 } 342 } 343 } catch(Exception e) { 344 String msg = intres.getLocalizedMessage("createcrl.generalerror", new Integer (caid)); 345 error(msg, e); 346 logsession.log(admin, caid, LogEntry.MODULE_CA, new java.util.Date (),null, null, LogEntry.EVENT_ERROR_CREATECRL,msg,e); 347 if (e instanceof EJBException ) { 348 throw (EJBException )e; 349 } 350 throw new EJBException (e); 351 } 352 } 353 } catch (Exception e) { 354 String msg = intres.getLocalizedMessage("createcrl.erroravailcas"); 355 error(msg, e); 356 logsession.log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (),null, null, LogEntry.EVENT_ERROR_CREATECRL,msg,e); 357 if (e instanceof EJBException ) { 358 throw (EJBException )e; 359 } 360 throw new EJBException (e); 361 } 362 363 return createdcrls; 364 } 365 366 } 367 368 | Popular Tags |