Example usage for java.security.cert CertificateExpiredException CertificateExpiredException

List of usage examples for java.security.cert CertificateExpiredException CertificateExpiredException

Introduction

In this page you can find the example usage for java.security.cert CertificateExpiredException CertificateExpiredException.

Prototype

public CertificateExpiredException(String message) 

Source Link

Document

Constructs a CertificateExpiredException with the specified detail message.

Usage

From source file:org.cesecore.util.CertTools.java

/**
 * Checks that the given date is within the certificate's validity period. In other words, this determines whether the certificate would be valid
 * at the given date/time./* w ww .ja  v  a 2  s  .c om*/
 * 
 * This utility class is only a helper to get the same behavior as the standard java.security.cert API regardless if using X.509 or CV
 * Certificate.
 * 
 * @param cert certificate to verify, if null the method returns immediately, null does not have a validity to check.
 * @param date the Date to check against to see if this certificate is valid at that date/time.
 * @throws NoSuchFieldException
 * @throws CertificateExpiredException - if the certificate has expired with respect to the date supplied.
 * @throws CertificateNotYetValidException - if the certificate is not yet valid with respect to the date supplied.
 * @see java.security.cert.X509Certificate#checkValidity(Date)
 */
public static void checkValidity(final Certificate cert, final Date date)
        throws CertificateExpiredException, CertificateNotYetValidException {
    if (cert != null) {
        if (cert instanceof X509Certificate) {
            final X509Certificate xcert = (X509Certificate) cert;
            xcert.checkValidity(date);
        } else if (StringUtils.equals(cert.getType(), "CVC")) {
            final CardVerifiableCertificate cvccert = (CardVerifiableCertificate) cert;
            try {
                final Date start = cvccert.getCVCertificate().getCertificateBody().getValidFrom();
                final Date end = cvccert.getCVCertificate().getCertificateBody().getValidTo();
                if (start.after(date)) {
                    String msg = "Certificate startDate '" + start + "' is after check date '" + date + "'";
                    if (log.isTraceEnabled()) {
                        log.trace(msg);
                    }
                    throw new CertificateNotYetValidException(msg);
                }
                if (end.before(date)) {
                    final String msg = "Certificate endDate '" + end + "' is before check date '" + date + "'";
                    if (log.isTraceEnabled()) {
                        log.trace(msg);
                    }
                    throw new CertificateExpiredException(msg);
                }
            } catch (NoSuchFieldException e) {
                log.error("NoSuchFieldException: ", e);
            }
        }
    }
}

From source file:org.ejbca.util.CertTools.java

/**
 * Checks that the given date is within the certificate's validity period. 
 * In other words, this determines whether the certificate would be valid at the given date/time.
 * /*from   w w  w .ja  v  a2 s .  co  m*/
 * This utility class is only a helper to get the same behavior as the standard java.security.cert API regardless if using X.509 or CV Certificate.
 *
 * @param cert certificate to verify, if null the method returns immediately, null does not have a validity to check.
 * @param date the Date to check against to see if this certificate is valid at that date/time.
 * @throws NoSuchFieldException 
 * @throws CertificateExpiredException - if the certificate has expired with respect to the date supplied. 
  * @throws CertificateNotYetValidException - if the certificate is not yet valid with respect to the date supplied.
  * @see java.security.cert.X509Certificate#checkValidity(Date)
 */
public static void checkValidity(Certificate cert, Date date)
        throws CertificateExpiredException, CertificateNotYetValidException {
    if (cert != null) {
        if (cert instanceof X509Certificate) {
            X509Certificate xcert = (X509Certificate) cert;
            xcert.checkValidity(date);
        } else if (StringUtils.equals(cert.getType(), "CVC")) {
            CardVerifiableCertificate cvccert = (CardVerifiableCertificate) cert;
            try {
                Date start = cvccert.getCVCertificate().getCertificateBody().getValidFrom();
                Date end = cvccert.getCVCertificate().getCertificateBody().getValidTo();
                if (start.after(date)) {
                    String msg = "Certificate startDate '" + start + "' is after check date '" + date + "'";
                    if (log.isTraceEnabled()) {
                        log.trace(msg);
                    }
                    throw new CertificateNotYetValidException(msg);
                }
                if (end.before(date)) {
                    String msg = "Certificate endDate '" + end + "' is before check date '" + date + "'";
                    if (log.isTraceEnabled()) {
                        log.trace(msg);
                    }
                    throw new CertificateExpiredException(msg);
                }
            } catch (NoSuchFieldException e) {
                log.error("NoSuchFieldException: ", e);
            }
        }
    }
}