Example usage for java.security.cert CertificateFactory generateCertPath

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

Introduction

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

Prototype

public final CertPath generateCertPath(InputStream inStream, String encoding) throws CertificateException 

Source Link

Document

Generates a CertPath object and initializes it with the data read from the InputStream inStream.

Usage

From source file:net.shirayu.android.WlanLogin.MyHttpClient.java

public static X509Certificate readPem(InputStream stream)
        throws CertificateException, NoSuchProviderException, IOException {
    CertPath cp;//from   ww w .j a v  a  2 s .  c  om
    try {
        CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");
        cp = cf.generateCertPath(stream, "PEM");
    } finally {
        stream.close();
    }
    List<? extends Certificate> certs = cp.getCertificates();
    if (certs.size() < 1) {
        throw new CertificateException("Certificate list is empty");
    } else if (certs.size() > 1) {
        throw new CertificateException("Intermediate certificate is not allowed");
    }
    if (certs.get(0) instanceof X509Certificate) {
        X509Certificate cert = (X509Certificate) certs.get(0);
        cert.checkValidity();
        return cert;
    } else {
        throw new CertificateException("Certificate is not X509Certificate");
    }
}

From source file:net.sf.keystore_explorer.crypto.x509.X509CertUtil.java

private static X509Certificate[] loadCertificatesPkiPath(InputStream is) throws CryptoException {
    try {//from w ww  .j  a v  a 2s .  c o  m
        CertificateFactory cf = CertificateFactory.getInstance(X509_CERT_TYPE, BOUNCY_CASTLE.jce());
        CertPath certPath = cf.generateCertPath(is, PKI_PATH_ENCODING);

        List<? extends Certificate> certs = certPath.getCertificates();

        ArrayList<X509Certificate> loadedCerts = new ArrayList<X509Certificate>();

        for (Iterator<? extends Certificate> itr = certs.iterator(); itr.hasNext();) {
            X509Certificate cert = (X509Certificate) itr.next();

            if (cert != null) {
                loadedCerts.add(cert);
            }
        }

        return loadedCerts.toArray(new X509Certificate[loadedCerts.size()]);
    } catch (CertificateException e) {
        throw new CryptoException(res.getString("NoLoadPkiPath.exception.message"), e);
    } catch (NoSuchProviderException e) {
        throw new CryptoException(res.getString("NoLoadPkiPath.exception.message"), e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.lastdaywaiting.example.kalkan.service.SecureManager.java

private Certificate createCerificateByFile(String fileName, String storeDescript) {
    CertPath cp = null;/* w w  w.ja v  a  2 s  .c  o m*/
    try {
        InputStream inputStream = this.getClass().getResourceAsStream(fileName);
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509", providerName);
        cp = certFactory.generateCertPath(inputStream, "PKCS7");
        inputStream.close();
        //IOUtils.closeQuietly(fis);
    } catch (Exception ex) {
        throw new RuntimeException(
                "ORE SIGN: ? ? ? ?   '"
                        + fileName + "' ? " + storeDescript + ".",
                ex);
    }

    List<? extends Certificate> certs = cp.getCertificates();
    if (certs.size() == 1) {
        System.out.println(" ? " + fileName + " ? " + storeDescript);
        return certs.get(0);
    } else {
        throw new RuntimeException("  '" + fileName + "' ? " + storeDescript
                + "    1 ?   " + certs.size());
    }

}

From source file:org.gluu.oxtrust.ldap.service.SSLService.java

private static X509Certificate[] loadCertificatesAsPkiPathEncoded(InputStream is) throws Exception {
    try {/*w w  w  .jav a2  s.c  om*/
        CertificateFactory cf = CertificateFactory.getInstance(X509_CERT_TYPE, SECURITY_PROVIDER_BOUNCY_CASTLE);
        CertPath certPath = cf.generateCertPath(is, PKI_PATH_ENCODING);

        List<? extends Certificate> certs = certPath.getCertificates();

        ArrayList<X509Certificate> loadedCerts = new ArrayList<X509Certificate>();

        for (Iterator<? extends Certificate> itr = certs.iterator(); itr.hasNext();) {
            X509Certificate cert = (X509Certificate) itr.next();

            if (cert != null) {
                loadedCerts.add(cert);
            }
        }

        return loadedCerts.toArray(new X509Certificate[loadedCerts.size()]);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:org.kse.crypto.x509.X509CertUtil.java

private static X509Certificate[] loadCertificatesPkiPath(InputStream is) throws CryptoException {
    try {//  w  w w .j  av  a2s  .co m
        CertificateFactory cf = CertificateFactory.getInstance(X509_CERT_TYPE, BOUNCY_CASTLE.jce());
        CertPath certPath = cf.generateCertPath(is, PKI_PATH_ENCODING);

        List<? extends Certificate> certs = certPath.getCertificates();

        ArrayList<X509Certificate> loadedCerts = new ArrayList<X509Certificate>();

        for (Iterator<? extends Certificate> itr = certs.iterator(); itr.hasNext();) {
            X509Certificate cert = (X509Certificate) itr.next();

            if (cert != null) {
                loadedCerts.add(cert);
            }
        }

        return loadedCerts.toArray(new X509Certificate[loadedCerts.size()]);
    } catch (CertificateException | NoSuchProviderException e) {
        throw new CryptoException(res.getString("NoLoadPkiPath.exception.message"), e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}