Example usage for java.security.cert CertificateFactory getInstance

List of usage examples for java.security.cert CertificateFactory getInstance

Introduction

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

Prototype

public static final CertificateFactory getInstance(String type, Provider provider) throws CertificateException 

Source Link

Document

Returns a certificate factory object for the specified certificate type.

Usage

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

public static CertificateFactory getCertificateFactory(String provider) {
    String prov = provider;/*from  www.j  a va2 s  .c o m*/
    if (provider == null) {
        prov = "BC";
    }
    if (StringUtils.equals(prov, "BC")) {
        installBCProviderIfNotAvailable();
    }
    try {
        return CertificateFactory.getInstance("X.509", prov);
    } catch (NoSuchProviderException nspe) {
        log.error("NoSuchProvider: ", nspe);
    } catch (CertificateException ce) {
        log.error("CertificateException: ", ce);
    }
    return null;
}

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

/** Returns a CertificateFactory that can be used to create certificates from byte arrays and such.
 * @param provider Security provider that should be used to create certificates, default BC is null is passed.
 * @return CertificateFactory/*from  w ww .  j ava 2  s  . c om*/
 */
public static CertificateFactory getCertificateFactory(final String provider) {
    final String prov;
    if (provider == null) {
        prov = "BC";
    } else {
        prov = provider;
    }
    if ("BC".equals(prov)) {
        CryptoProviderTools.installBCProviderIfNotAvailable();
    }
    try {
        return CertificateFactory.getInstance("X.509", prov);
    } catch (NoSuchProviderException nspe) {
        log.error("NoSuchProvider: ", nspe);
    } catch (CertificateException ce) {
        log.error("CertificateException: ", ce);
    }
    return null;
}

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

/**
 * Check the certificate with CA certificate.
 *
 * @param certificate cert to verify/*from   ww w  .  j a  v a 2s.  c o m*/
 * @param caCertPath collection of X509Certificate
 * @return true if verified OK
 * @throws Exception if verification failed
 */
public static boolean verify(Certificate certificate, Collection<Certificate> caCertPath) throws Exception {
    try {
        ArrayList<Certificate> certlist = new ArrayList<Certificate>();
        // Create CertPath
        certlist.add(certificate);
        // Add other certs...         
        CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");
        java.security.cert.CertPath cp = cf.generateCertPath(certlist);
        // Create TrustAnchor. Since EJBCA use BouncyCastle provider, we assume
        // certificate already in correct order
        X509Certificate[] cac = (X509Certificate[]) caCertPath.toArray(new X509Certificate[] {});
        java.security.cert.TrustAnchor anchor = new java.security.cert.TrustAnchor(cac[0], null);
        // Set the PKIX parameters
        java.security.cert.PKIXParameters params = new java.security.cert.PKIXParameters(
                java.util.Collections.singleton(anchor));
        params.setRevocationEnabled(false);
        java.security.cert.CertPathValidator cpv = java.security.cert.CertPathValidator.getInstance("PKIX",
                "BC");
        java.security.cert.PKIXCertPathValidatorResult result = (java.security.cert.PKIXCertPathValidatorResult) cpv
                .validate(cp, params);
        if (log.isDebugEnabled()) {
            log.debug("Certificate verify result: " + result.toString());
        }
    } catch (java.security.cert.CertPathValidatorException cpve) {
        throw new Exception(
                "Invalid certificate or certificate not issued by specified CA: " + cpve.getMessage());
    } catch (Exception e) {
        throw new Exception("Error checking certificate chain: " + e.getMessage());
    }
    return true;
}

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

/**
 * Check the certificate with CA certificate.
 * /* www.  ja va  2s . c  o  m*/
 * @param certificate cert to verify
 * @param caCertChain collection of X509Certificate
 * @return true if verified OK
 * @throws Exception if verification failed
 */
public static boolean verify(Certificate certificate, Collection<Certificate> caCertChain) throws Exception {
    try {
        ArrayList<Certificate> certlist = new ArrayList<Certificate>();
        // Create CertPath
        certlist.add(certificate);
        // Add other certs...
        CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");
        java.security.cert.CertPath cp = cf.generateCertPath(certlist);

        // Create TrustAnchor. Since EJBCA use BouncyCastle provider, we assume
        // certificate already in correct order
        X509Certificate[] cac = (X509Certificate[]) caCertChain.toArray(new X509Certificate[] {});
        java.security.cert.TrustAnchor anchor = new java.security.cert.TrustAnchor(cac[0], null);
        // Set the PKIX parameters
        java.security.cert.PKIXParameters params = new java.security.cert.PKIXParameters(
                java.util.Collections.singleton(anchor));

        params.setRevocationEnabled(false);
        java.security.cert.CertPathValidator cpv = java.security.cert.CertPathValidator.getInstance("PKIX",
                "BC");
        java.security.cert.PKIXCertPathValidatorResult result = (java.security.cert.PKIXCertPathValidatorResult) cpv
                .validate(cp, params);
        if (log.isDebugEnabled()) {
            log.debug("Certificate verify result: " + result.toString());
        }
    } catch (java.security.cert.CertPathValidatorException cpve) {
        throw new Exception(
                "Invalid certificate or certificate not issued by specified CA: " + cpve.getMessage());
    } catch (Exception e) {
        throw new Exception("Error checking certificate chain: " + e.getMessage());
    }
    return true;
}