Search in sources :

Example 16 with CertStore

use of java.security.cert.CertStore in project robovm by robovm.

the class TestUtils method getCollectionCertStoresList.

/**
     * Creates <code>List</code> of <code>CollectionCertStores</code>
     *
     * @return The list created
     *
     * @throws InvalidAlgorithmParameterException
     * @throws NoSuchAlgorithmException
     */
public static List<CertStore> getCollectionCertStoresList() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
    CertStore cs = CertStore.getInstance("Collection", new CollectionCertStoreParameters());
    ArrayList<CertStore> l = new ArrayList<CertStore>();
    if (!l.add(cs)) {
        throw new RuntimeException("Could not create cert stores list");
    }
    return l;
}
Also used : CollectionCertStoreParameters(java.security.cert.CollectionCertStoreParameters) ArrayList(java.util.ArrayList) CertStore(java.security.cert.CertStore)

Example 17 with CertStore

use of java.security.cert.CertStore in project jdk8u_jdk by JetBrains.

the class ForwardBuilder method getCerts.

/**
     * Download Certificates from the given AIA and add them to the
     * specified Collection.
     */
// cs.getCertificates(caSelector) returns a collection of X509Certificate's
// because of the selector, so the cast is safe
@SuppressWarnings("unchecked")
private boolean getCerts(AuthorityInfoAccessExtension aiaExt, Collection<X509Certificate> certs) {
    if (Builder.USE_AIA == false) {
        return false;
    }
    List<AccessDescription> adList = aiaExt.getAccessDescriptions();
    if (adList == null || adList.isEmpty()) {
        return false;
    }
    boolean add = false;
    for (AccessDescription ad : adList) {
        CertStore cs = URICertStore.getInstance(ad);
        if (cs != null) {
            try {
                if (certs.addAll((Collection<X509Certificate>) cs.getCertificates(caSelector))) {
                    add = true;
                    if (!searchAllCertStores) {
                        return true;
                    }
                }
            } catch (CertStoreException cse) {
                if (debug != null) {
                    debug.println("exception getting certs from CertStore:");
                    cse.printStackTrace();
                }
            }
        }
    }
    return add;
}
Also used : AccessDescription(sun.security.x509.AccessDescription) CertStoreException(java.security.cert.CertStoreException) CertStore(java.security.cert.CertStore) X509Certificate(java.security.cert.X509Certificate)

Example 18 with CertStore

use of java.security.cert.CertStore in project jdk8u_jdk by JetBrains.

the class URICertStore method getInstance.

static synchronized CertStore getInstance(URICertStoreParameters params) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    if (debug != null) {
        debug.println("CertStore URI:" + params.uri);
    }
    CertStore ucs = certStoreCache.get(params);
    if (ucs == null) {
        ucs = new UCS(new URICertStore(params), null, "URI", params);
        certStoreCache.put(params, ucs);
    } else {
        if (debug != null) {
            debug.println("URICertStore.getInstance: cache hit");
        }
    }
    return ucs;
}
Also used : CertStore(java.security.cert.CertStore)

Example 19 with CertStore

use of java.security.cert.CertStore in project jdk8u_jdk by JetBrains.

the class Pair method doPrintCert.

private void doPrintCert(final PrintStream out) throws Exception {
    if (jarfile != null) {
        JarFile jf = new JarFile(jarfile, true);
        Enumeration<JarEntry> entries = jf.entries();
        Set<CodeSigner> ss = new HashSet<>();
        byte[] buffer = new byte[8192];
        int pos = 0;
        while (entries.hasMoreElements()) {
            JarEntry je = entries.nextElement();
            try (InputStream is = jf.getInputStream(je)) {
                while (is.read(buffer) != -1) {
                // we just read. this will throw a SecurityException
                // if a signature/digest check fails. This also
                // populate the signers
                }
            }
            CodeSigner[] signers = je.getCodeSigners();
            if (signers != null) {
                for (CodeSigner signer : signers) {
                    if (!ss.contains(signer)) {
                        ss.add(signer);
                        out.printf(rb.getString("Signer.d."), ++pos);
                        out.println();
                        out.println();
                        out.println(rb.getString("Signature."));
                        out.println();
                        for (Certificate cert : signer.getSignerCertPath().getCertificates()) {
                            X509Certificate x = (X509Certificate) cert;
                            if (rfc) {
                                out.println(rb.getString("Certificate.owner.") + x.getSubjectDN() + "\n");
                                dumpCert(x, out);
                            } else {
                                printX509Cert(x, out);
                            }
                            out.println();
                        }
                        Timestamp ts = signer.getTimestamp();
                        if (ts != null) {
                            out.println(rb.getString("Timestamp."));
                            out.println();
                            for (Certificate cert : ts.getSignerCertPath().getCertificates()) {
                                X509Certificate x = (X509Certificate) cert;
                                if (rfc) {
                                    out.println(rb.getString("Certificate.owner.") + x.getSubjectDN() + "\n");
                                    dumpCert(x, out);
                                } else {
                                    printX509Cert(x, out);
                                }
                                out.println();
                            }
                        }
                    }
                }
            }
        }
        jf.close();
        if (ss.isEmpty()) {
            out.println(rb.getString("Not.a.signed.jar.file"));
        }
    } else if (sslserver != null) {
        // Lazily load SSLCertStoreHelper if present
        CertStoreHelper helper = CertStoreHelper.getInstance("SSLServer");
        CertStore cs = helper.getCertStore(new URI("https://" + sslserver));
        Collection<? extends Certificate> chain;
        try {
            chain = cs.getCertificates(null);
            if (chain.isEmpty()) {
                // even if the URL connection is successful.
                throw new Exception(rb.getString("No.certificate.from.the.SSL.server"));
            }
        } catch (CertStoreException cse) {
            if (cse.getCause() instanceof IOException) {
                throw new Exception(rb.getString("No.certificate.from.the.SSL.server"), cse.getCause());
            } else {
                throw cse;
            }
        }
        int i = 0;
        for (Certificate cert : chain) {
            try {
                if (rfc) {
                    dumpCert(cert, out);
                } else {
                    out.println("Certificate #" + i++);
                    out.println("====================================");
                    printX509Cert((X509Certificate) cert, out);
                    out.println();
                }
            } catch (Exception e) {
                if (debug) {
                    e.printStackTrace();
                }
            }
        }
    } else {
        if (filename != null) {
            try (FileInputStream inStream = new FileInputStream(filename)) {
                printCertFromStream(inStream, out);
            }
        } else {
            printCertFromStream(System.in, out);
        }
    }
}
Also used : CertStoreException(java.security.cert.CertStoreException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Timestamp(java.security.Timestamp) URI(java.net.URI) X509Certificate(java.security.cert.X509Certificate) KeyStoreException(java.security.KeyStoreException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) CertStoreException(java.security.cert.CertStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) CertificateException(java.security.cert.CertificateException) CertStoreHelper(sun.security.provider.certpath.CertStoreHelper) CertStore(java.security.cert.CertStore) CodeSigner(java.security.CodeSigner) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 20 with CertStore

use of java.security.cert.CertStore in project Payara by payara.

the class BaseContainerCallbackHandler method processCertStore.

private void processCertStore(CertStoreCallback certStoreCallback) {
    if (_logger.isLoggable(Level.FINE)) {
        _logger.log(Level.FINE, "JMAC: In CertStoreCallback Processor");
    }
    KeyStore certStore = sslUtils.getMergedTrustStore();
    if (certStore == null) {
        // should never happen
        certStoreCallback.setCertStore(null);
    }
    List<Certificate> list = new ArrayList<Certificate>();
    CollectionCertStoreParameters ccsp;
    try {
        if (certStore != null) {
            Enumeration enu = certStore.aliases();
            while (enu.hasMoreElements()) {
                String alias = (String) enu.nextElement();
                if (certStore.isCertificateEntry(alias)) {
                    try {
                        Certificate cert = certStore.getCertificate(alias);
                        list.add(cert);
                    } catch (KeyStoreException kse) {
                        // ignore and move to next
                        if (_logger.isLoggable(Level.FINE)) {
                            _logger.log(Level.FINE, "JMAC: Cannot retrieve" + "certificate for alias " + alias);
                        }
                    }
                }
            }
        }
        ccsp = new CollectionCertStoreParameters(list);
        CertStore certstore = CertStore.getInstance("Collection", ccsp);
        certStoreCallback.setCertStore(certstore);
    } catch (KeyStoreException kse) {
        if (_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE, "JMAC:  Cannot determine truststore aliases", kse);
        }
    } catch (InvalidAlgorithmParameterException iape) {
        if (_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE, "JMAC:  Cannot instantiate CertStore", iape);
        }
    } catch (NoSuchAlgorithmException nsape) {
        if (_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE, "JMAC:  Cannot instantiate CertStore", nsape);
        }
    }
}
Also used : CollectionCertStoreParameters(java.security.cert.CollectionCertStoreParameters) Enumeration(java.util.Enumeration) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ArrayList(java.util.ArrayList) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStore(java.security.KeyStore) CertStore(java.security.cert.CertStore) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Aggregations

CertStore (java.security.cert.CertStore)40 X509Certificate (java.security.cert.X509Certificate)19 CollectionCertStoreParameters (java.security.cert.CollectionCertStoreParameters)15 ArrayList (java.util.ArrayList)13 CertStoreException (java.security.cert.CertStoreException)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)10 X509CertSelector (java.security.cert.X509CertSelector)10 PKIXBuilderParameters (java.security.cert.PKIXBuilderParameters)9 NoSuchProviderException (java.security.NoSuchProviderException)8 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)7 CertStoreParameters (java.security.cert.CertStoreParameters)7 HashSet (java.util.HashSet)7 IOException (java.io.IOException)6 Iterator (java.util.Iterator)6 KeyStoreException (java.security.KeyStoreException)5 CertPathBuilder (java.security.cert.CertPathBuilder)5 Certificate (java.security.cert.Certificate)5 CertificateException (java.security.cert.CertificateException)5 TrustAnchor (java.security.cert.TrustAnchor)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4