Example usage for java.security KeyStore isCertificateEntry

List of usage examples for java.security KeyStore isCertificateEntry

Introduction

In this page you can find the example usage for java.security KeyStore isCertificateEntry.

Prototype

public final boolean isCertificateEntry(String alias) throws KeyStoreException 

Source Link

Document

Returns true if the entry identified by the given alias was created by a call to setCertificateEntry , or created by a call to setEntry with a TrustedCertificateEntry .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    FileInputStream is = new FileInputStream("yourfile" + ".keystore");
    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    String password = "my-keystore-password";
    keystore.load(is, password.toCharArray());

    Enumeration e = keystore.aliases();
    for (; e.hasMoreElements();) {
        String alias = (String) e.nextElement();

        boolean b = keystore.isKeyEntry(alias);

        b = keystore.isCertificateEntry(alias);
    }//from  ww  w.  java 2s. c o  m
    is.close();
}

From source file:com.cerema.cloud2.lib.common.network.NetworkUtils.java

public static boolean isCertInKnownServersStore(Certificate cert, Context context)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {

    KeyStore knownServers = getKnownServersStore(context);
    Log_OC.d(TAG, "Certificate - HashCode: " + cert.hashCode() + " "
            + Boolean.toString(knownServers.isCertificateEntry(Integer.toString(cert.hashCode()))));
    return knownServers.isCertificateEntry(Integer.toString(cert.hashCode()));
}

From source file:it.geosolutions.sfs.web.Start.java

private static boolean keyStoreContainsCertificate(KeyStore ks, String hostname) throws Exception {
    //          SubjectDnX509PrincipalExtractor ex = new SubjectDnX509PrincipalExtractor();
    Enumeration<String> e = ks.aliases();
    while (e.hasMoreElements()) {
        String alias = e.nextElement();
        if (ks.isCertificateEntry(alias)) {
            Certificate c = ks.getCertificate(alias);
            if (c instanceof X509Certificate) {
                X500Principal p = (X500Principal) ((X509Certificate) c).getSubjectX500Principal();
                if (p.getName().contains(hostname))
                    return true;
            }/*from w  w  w .j  a  v  a  2s . c  om*/
        }
    }
    return false;
}

From source file:net.sf.keystore_explorer.crypto.keystore.KeyStoreUtil.java

/**
 * Is the named entry in the KeyStore a trusted certificate entry?
 *
 * @param alias//from w  w w  .ja  v a 2 s.co  m
 *            Alias
 * @param keyStore
 *            KeyStore
 * @return True if it is, false otherwise
 * @throws KeyStoreException
 *             If there was a problem accessing the KeyStore.
 */
public static boolean isTrustedCertificateEntry(String alias, KeyStore keyStore) throws KeyStoreException {
    return keyStore.isCertificateEntry(alias);
}

From source file:com.cloudbees.jenkins.support.impl.RootCAs.java

public static void getRootCAList(StringWriter writer) {
    KeyStore instance = null;
    try {//  w ww  .j a v a 2  s  .c  om
        instance = KeyStore.getInstance(KeyStore.getDefaultType());
        Enumeration<String> aliases = instance.aliases();
        while (aliases.hasMoreElements()) {
            String s = aliases.nextElement();
            writer.append("========");
            writer.append("Alias: " + s);
            writer.append(instance.getCertificate(s).getPublicKey().toString());
            writer.append("Trusted certificate: " + instance.isCertificateEntry(s));
        }
    } catch (KeyStoreException e) {
        writer.write(Functions.printThrowable(e));
    }
}

From source file:net.sf.jsignpdf.utils.KeyStoreUtils.java

/**
 * Loads certificate names (aliases) from the given keystore
 * /*from  w  w  w  . ja  va  2 s .  co m*/
 * @return array of certificate aliases
 */
public static String[] getCertAliases(KeyStore tmpKs) {
    if (tmpKs == null)
        return null;
    final List<String> tmpResult = new ArrayList<String>();
    try {
        final Enumeration<String> tmpAliases = tmpKs.aliases();
        while (tmpAliases.hasMoreElements()) {
            final String tmpAlias = tmpAliases.nextElement();
            if (tmpKs.isCertificateEntry(tmpAlias)) {
                tmpResult.add(tmpAlias);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return tmpResult.toArray(new String[tmpResult.size()]);
}

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

/**
 * Check whether or not a trusted certificate in the supplied KeyStore
 * matches the supplied X.509 certificate.
 *
 * @param cert//from   w  ww.jav  a2 s . c om
 *            The certificate
 * @param keyStore
 *            The KeyStore
 * @return The alias of the matching certificate in the KeyStore or null if
 *         there is no match
 * @throws CryptoException
 *             If there is a problem establishing trust
 */
public static String matchCertificate(KeyStore keyStore, X509Certificate cert) throws CryptoException {
    try {
        for (Enumeration aliases = keyStore.aliases(); aliases.hasMoreElements();) {
            String alias = (String) aliases.nextElement();
            if (keyStore.isCertificateEntry(alias)) {
                X509Certificate compCert = X509CertUtil.convertCertificate(keyStore.getCertificate(alias));

                if (cert.equals(compCert)) {
                    return alias;
                }
            }
        }
        return null;
    } catch (KeyStoreException ex) {
        throw new CryptoException(res.getString("NoMatchCertificate.exception.message"), ex);
    }
}

From source file:net.link.util.common.KeyUtils.java

public static ImmutableMap<String, X509Certificate> getCertificates(KeyStore keyStore,
        Predicate<String> ignoreAlias) {

    Enumeration<String> aliases;
    try {/* w ww  . j a  v  a  2 s .  c o  m*/
        aliases = keyStore.aliases();
    } catch (KeyStoreException e) {
        throw new InternalInconsistencyException("could not enumerate aliases", e);
    }

    ImmutableMap.Builder<String, X509Certificate> certificates = ImmutableMap.builder();
    while (aliases.hasMoreElements()) {
        String alias = aliases.nextElement();
        if (ignoreAlias != null && ignoreAlias.apply(alias))
            continue;

        try {
            if (keyStore.isCertificateEntry(alias))
                certificates.put(alias, (X509Certificate) keyStore.getCertificate(alias));
        } catch (KeyStoreException e) {
            throw new InternalInconsistencyException(
                    String.format("error retrieving certificate, alias=%s", alias), e);
        }
    }

    return certificates.build();
}

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

private static List<X509Certificate> extractCertificates(KeyStore keyStore) throws CryptoException {
    try {/*from   ww  w  .  j av  a2s  . c o m*/
        List<X509Certificate> certs = new ArrayList<X509Certificate>();

        for (Enumeration aliases = keyStore.aliases(); aliases.hasMoreElements();) {
            String alias = (String) aliases.nextElement();

            if (keyStore.isCertificateEntry(alias)) {
                certs.add(X509CertUtil.convertCertificate(keyStore.getCertificate(alias)));
            }
        }

        return certs;
    } catch (KeyStoreException ex) {
        throw new CryptoException(res.getString("NoExtractCertificates.exception.message"), ex);
    }
}

From source file:com.vmware.identity.idm.server.ClientCertTestUtils.java

/**
 * @return selfsigned valid cert//from   w w w  .j ava2 s  . com
 * @throws KeyStoreException
 */
public X509Certificate[] getValidCert() throws KeyStoreException {
    KeyStore ks = loadKeyStore(clientStoreName, storePass);
    if (!ks.isCertificateEntry(validCertAlias)) {
        throw new KeyStoreException("Cert not in the store");
    }
    X509Certificate leaf = (X509Certificate) ks.getCertificate(validCertAlias);
    X509Certificate[] certs = { leaf };
    return certs;
}