/** Tests the extension Freshest CRL DP. */
  @Test
  public void testCRLFreshestCRL() throws Exception {
    final String cdpURL = "http://www.ejbca.org/foo/bar.crl";
    final String freshestCdpURL = "http://www.ejbca.org/foo/delta.crl";
    X509CAInfo cainfo = (X509CAInfo) testx509ca.getCAInfo();
    X509CRL x509crl;
    byte[] cFreshestDpDER;

    cainfo.setUseCrlDistributionPointOnCrl(true);
    cainfo.setDefaultCRLDistPoint(cdpURL);
    cainfo.setCADefinedFreshestCRL(freshestCdpURL);
    caSession.editCA(roleMgmgToken, cainfo);
    publishingCrlSessionRemote.forceCRL(roleMgmgToken, testx509ca.getCAId());
    x509crl =
        CertTools.getCRLfromByteArray(crlStoreSession.getLastCRL(cainfo.getSubjectDN(), false));
    cFreshestDpDER = x509crl.getExtensionValue(Extension.freshestCRL.getId());
    assertNotNull("CRL has no Freshest Distribution Point", cFreshestDpDER);

    ASN1InputStream aIn = new ASN1InputStream(new ByteArrayInputStream(cFreshestDpDER));
    ASN1OctetString octs = (ASN1OctetString) aIn.readObject();
    aIn = new ASN1InputStream(new ByteArrayInputStream(octs.getOctets()));
    CRLDistPoint cdp = CRLDistPoint.getInstance((ASN1Sequence) aIn.readObject());
    DistributionPoint[] distpoints = cdp.getDistributionPoints();

    assertEquals("More CRL Freshest distributions points than expected", 1, distpoints.length);
    assertEquals(
        "Freshest CRL distribution point is different",
        freshestCdpURL,
        ((DERIA5String)
                ((GeneralNames) distpoints[0].getDistributionPoint().getName())
                    .getNames()[0].getName())
            .getString());
  }
 /** Test Overflow of CRL Period */
 @Test
 public void testCRLPeriodOverflow() throws Exception {
   log.trace(">test05CRLPeriodOverflow()");
   // Fetch CAInfo and save CRLPeriod
   CAInfo cainfo = testx509ca.getCAInfo();
   long tempCRLPeriod = cainfo.getCRLPeriod();
   X509Certificate cert = createCertWithValidity(1);
   try {
     // Revoke the user
     certificateStoreSession.setRevokeStatus(
         roleMgmgToken, cert, RevokedCertInfo.REVOCATION_REASON_KEYCOMPROMISE, null);
     // Change CRLPeriod
     cainfo.setCRLPeriod(Long.MAX_VALUE);
     caSession.editCA(roleMgmgToken, cainfo);
     // Create new CRL's
     assertTrue(publishingCrlSessionRemote.forceCRL(roleMgmgToken, testx509ca.getCAId()));
     // Verify that status is not archived
     CertificateInfo certinfo =
         certificateStoreSession.getCertificateInfo(CertTools.getFingerprintAsString(cert));
     assertFalse(
         "Non Expired Revoked Certificate was archived",
         certinfo.getStatus() == CertificateConstants.CERT_ARCHIVED);
   } finally {
     internalCertificateStoreSession.removeCertificate(CertTools.getSerialNumber(cert));
     // Restore CRL Period
     cainfo.setCRLPeriod(tempCRLPeriod);
     caSession.editCA(roleMgmgToken, cainfo);
   }
 }
 /** Test error handling of off-line CA during CRL creation. */
 @Test
 public void testCrlCreateSessionErrorHandling() throws Exception {
   CAInfo cainfo = testx509ca.getCAInfo();
   cainfo.setStatus(CAConstants.CA_OFFLINE);
   caSession.editCA(roleMgmgToken, cainfo);
   CA ca = caTestSession.getCA(roleMgmgToken, testx509ca.getCAId());
   assertEquals(CAConstants.CA_OFFLINE, ca.getStatus());
   assertEquals(CAConstants.CA_OFFLINE, ca.getCAInfo().getStatus());
   try {
     publishingCrlSessionRemote.forceCRL(roleMgmgToken, testx509ca.getCAId());
     assertTrue(
         "Trying to generate a CRL for CA with status CA_OFFLINE did not throw the CATokenOfflineException.",
         false);
   } catch (CAOfflineException e) {
     // Expected
   }
   cainfo.setStatus(CAConstants.CA_ACTIVE);
   caSession.editCA(roleMgmgToken, cainfo);
 }
 @Test
 public void testCrlGenerateForAll() throws Exception {
   X509CAInfo cainfo = (X509CAInfo) testx509ca.getCAInfo();
   cainfo.setCRLIssueInterval(1); // Issue very often..
   cainfo.setDeltaCRLPeriod(1); // Issue very often..
   caSession.editCA(roleMgmgToken, cainfo);
   // make sure we have a CRL and delta CRL generated
   publishingCrlSessionRemote.forceCRL(roleMgmgToken, testx509ca.getCAId());
   publishingCrlSessionRemote.forceDeltaCRL(roleMgmgToken, testx509ca.getCAId());
   try {
     // Now wait and test again
     Thread.sleep(1000);
     final X509CRL x509crl =
         CertTools.getCRLfromByteArray(crlStoreSession.getLastCRL(cainfo.getSubjectDN(), false));
     assertTrue(publishingCrlSessionRemote.createCRLs(roleMgmgToken) > 0);
     final X509CRL x509crlAfter =
         CertTools.getCRLfromByteArray(crlStoreSession.getLastCRL(cainfo.getSubjectDN(), false));
     assertTrue(
         "Did not generate a newer CRL.",
         x509crlAfter.getThisUpdate().after(x509crl.getThisUpdate()));
     final X509CRL x509deltaCrl =
         CertTools.getCRLfromByteArray(crlStoreSession.getLastCRL(cainfo.getSubjectDN(), true));
     assertTrue(publishingCrlSessionRemote.createDeltaCRLs(roleMgmgToken) > 0);
     final X509CRL x509deltaCrlAfter =
         CertTools.getCRLfromByteArray(crlStoreSession.getLastCRL(cainfo.getSubjectDN(), true));
     assertTrue(
         "Did not generate a newer Delta CRL.",
         x509deltaCrlAfter.getThisUpdate().after(x509deltaCrl.getThisUpdate()));
     // Try a similar thing when we specify which CA IDs to generate CRLs for
     // Compare CRL numbers instead of Dates, since these CRLs might have been generated the same
     // second as the last ones
     final Collection<Integer> caids = new ArrayList<Integer>();
     caids.add(Integer.valueOf(testx509ca.getCAId()));
     publishingCrlProxySession.createCRLs(roleMgmgToken, caids, 2);
     final X509CRL x509crlAfter2 =
         CertTools.getCRLfromByteArray(crlStoreSession.getLastCRL(cainfo.getSubjectDN(), false));
     assertTrue(
         "Did not generate a newer CRL.",
         CrlExtensions.getCrlNumber(x509crlAfter2).intValue()
             > CrlExtensions.getCrlNumber(x509crlAfter).intValue());
     publishingCrlProxySession.createDeltaCRLs(roleMgmgToken, caids, 2);
     final X509CRL x509deltaCrlAfter2 =
         CertTools.getCRLfromByteArray(crlStoreSession.getLastCRL(cainfo.getSubjectDN(), true));
     assertTrue(
         "Did not generate a newer Delta CRL.",
         CrlExtensions.getCrlNumber(x509deltaCrlAfter2).intValue()
             > CrlExtensions.getCrlNumber(x509deltaCrlAfter).intValue());
   } finally {
     byte[] crl;
     while ((crl = crlStoreSession.getLastCRL(testx509ca.getSubjectDN(), false)) != null) {
       X509CRL x509crl = CertTools.getCRLfromByteArray(crl);
       internalCertificateStoreSession.removeCRL(
           roleMgmgToken, CertTools.getFingerprintAsString(x509crl));
     }
     while ((crl = crlStoreSession.getLastCRL(testx509ca.getSubjectDN(), true)) != null) {
       X509CRL x509crl = CertTools.getCRLfromByteArray(crl);
       internalCertificateStoreSession.removeCRL(
           roleMgmgToken, CertTools.getFingerprintAsString(x509crl));
     }
   }
 }