Example usage for java.security.cert CertificateNotYetValidException getMessage

List of usage examples for java.security.cert CertificateNotYetValidException getMessage

Introduction

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

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:org.cesecore.certificates.ca.CaSessionBean.java

/**
  * Checks if the CA certificate has expired (or is not yet valid) since last check.
  * Logs an info message first time that the CA certificate has expired, or every time when not yet valid.
  * //from w ww. ja  v  a2s .  c  om
  * @return the true if the CA is expired
  */
private boolean hasCAExpiredNow(final CA ca) {
    boolean expired = false;
    // Check that CA hasn't expired.
    try {
        CertTools.checkValidity(ca.getCACertificate(), new Date());
    } catch (CertificateExpiredException cee) {
        // Signers Certificate has expired, we want to make sure that the
        // status in the database is correctly EXPIRED for this CA
        // Don't set external CAs to expired though, because they should always be treated as external CAs
        if (ca.getStatus() != CAConstants.CA_EXPIRED && ca.getStatus() != CAConstants.CA_EXTERNAL) {
            log.info(intres.getLocalizedMessage("caadmin.caexpired", ca.getSubjectDN()) + " "
                    + cee.getMessage());
            expired = true;
        }
    } catch (CertificateNotYetValidException e) {
        // Signers Certificate is not yet valid.
        log.warn(intres.getLocalizedMessage("caadmin.canotyetvalid", ca.getSubjectDN()) + " " + e.getMessage());
    }
    return expired;
}

From source file:org.cesecore.certificates.ocsp.OcspResponseGeneratorSessionBean.java

/**
 * Checks the signature on an OCSP request. Does not check for revocation of the signer certificate
 * //from  w  w w .  ja  v a2s .c  om
 * @param clientRemoteAddr The IP address or host name of the remote client that sent the request, can be null.
 * @param req The signed OCSPReq
 * @return X509Certificate which is the certificate that signed the OCSP request
 * @throws SignRequestSignatureException if signature verification fail, or if the signing certificate is not authorized
 * @throws SignRequestException if there is no signature on the OCSPReq
 * @throws OCSPException if the request can not be parsed to retrieve certificates
 * @throws NoSuchProviderException if the BC provider is not installed
 * @throws CertificateException if the certificate can not be parsed
 * @throws NoSuchAlgorithmException if the certificate contains an unsupported algorithm
 * @throws InvalidKeyException if the certificate, or CA key is invalid
 */
private X509Certificate checkRequestSignature(String clientRemoteAddr, OCSPReq req) throws SignRequestException,
        SignRequestSignatureException, CertificateException, NoSuchAlgorithmException {
    X509Certificate signercert = null;
    // Get all certificates embedded in the request (probably a certificate chain)
    try {
        final X509CertificateHolder[] certs = req.getCerts();
        String signerSubjectDn = null;
        // We must find a certificate to verify the signature with...
        boolean verifyOK = false;
        for (int i = 0; i < certs.length; i++) {
            final X509Certificate certificate = certificateConverter.getCertificate(certs[i]);
            try {
                if (req.isSignatureValid(
                        new JcaContentVerifierProviderBuilder().build(certificate.getPublicKey()))) {
                    signercert = certificate; // if the request signature verifies by this certificate, this is the signer cert 
                    signerSubjectDn = CertTools.getSubjectDN(signercert);
                    log.info(intres.getLocalizedMessage("ocsp.infosigner", signerSubjectDn));
                    verifyOK = true;
                    // Check that the signer certificate can be verified by one of the CA-certificates that we answer for
                    final X509Certificate signerca = CaCertificateCache.INSTANCE
                            .findLatestBySubjectDN(HashID.getFromIssuerDN(signercert));
                    if (signerca != null) {
                        try {
                            signercert.verify(signerca.getPublicKey());
                            final Date now = new Date();
                            if (log.isDebugEnabled()) {
                                log.debug("Checking validity. Now: " + now + ", signerNotAfter: "
                                        + signercert.getNotAfter());
                            }
                            try {
                                // Check validity of the request signing certificate
                                CertTools.checkValidity(signercert, now);
                            } catch (CertificateNotYetValidException e) {
                                log.info(intres.getLocalizedMessage("ocsp.infosigner.certnotyetvalid",
                                        signerSubjectDn, CertTools.getIssuerDN(signercert), e.getMessage()));
                                verifyOK = false;
                            } catch (CertificateExpiredException e) {
                                log.info(intres.getLocalizedMessage("ocsp.infosigner.certexpired",
                                        signerSubjectDn, CertTools.getIssuerDN(signercert), e.getMessage()));
                                verifyOK = false;
                            }
                            try {
                                // Check validity of the CA certificate
                                CertTools.checkValidity(signerca, now);
                            } catch (CertificateNotYetValidException e) {
                                log.info(intres.getLocalizedMessage("ocsp.infosigner.certnotyetvalid",
                                        CertTools.getSubjectDN(signerca), CertTools.getIssuerDN(signerca),
                                        e.getMessage()));
                                verifyOK = false;
                            } catch (CertificateExpiredException e) {
                                log.info(intres.getLocalizedMessage("ocsp.infosigner.certexpired",
                                        CertTools.getSubjectDN(signerca), CertTools.getIssuerDN(signerca),
                                        e.getMessage()));
                                verifyOK = false;
                            }
                        } catch (SignatureException e) {
                            log.info(intres.getLocalizedMessage("ocsp.infosigner.invalidcertsignature",
                                    signerSubjectDn, CertTools.getIssuerDN(signercert), e.getMessage()));
                            verifyOK = false;
                        } catch (InvalidKeyException e) {
                            log.info(intres.getLocalizedMessage("ocsp.infosigner.invalidcertsignature",
                                    signerSubjectDn, CertTools.getIssuerDN(signercert), e.getMessage()));
                            verifyOK = false;
                        }
                    } else {
                        log.info(intres.getLocalizedMessage("ocsp.infosigner.nocacert", signerSubjectDn,
                                CertTools.getIssuerDN(signercert)));
                        verifyOK = false;
                    }
                    break;
                }
            } catch (OperatorCreationException e) {
                // Very fatal error
                throw new EJBException("Can not create Jca content signer: ", e);
            }
        }
        if (!verifyOK) {
            if (signerSubjectDn == null && certs.length > 0) {
                signerSubjectDn = CertTools.getSubjectDN(certificateConverter.getCertificate(certs[0]));
            }
            String errMsg = intres.getLocalizedMessage("ocsp.errorinvalidsignature", signerSubjectDn);
            log.info(errMsg);
            throw new SignRequestSignatureException(errMsg);
        }
    } catch (OCSPException e) {
        throw new CryptoProviderException("BouncyCastle was not initialized properly.", e);
    } catch (NoSuchProviderException e) {
        throw new CryptoProviderException("BouncyCastle was not found as a provider.", e);
    }
    return signercert;
}