Example usage for java.security KeyStore aliases

List of usage examples for java.security KeyStore aliases

Introduction

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

Prototype

public final Enumeration<String> aliases() throws KeyStoreException 

Source Link

Document

Lists all the alias names of this keystore.

Usage

From source file:org.lockss.util.TestKeyStoreUtil.java

public void testStore() throws Exception {
    File dir = getTempDir();/*from   w ww . j av  a 2s  .  c o  m*/
    File file = new File(dir, "test.ks");
    Properties p = initProps();
    p.put(KeyStoreUtil.PROP_KEYSTORE_FILE, file.toString());
    assertFalse(file.exists());
    KeyStore ks = KeyStoreUtil.createKeyStore(p);
    assertTrue(file.exists());

    KeyStore ks2 = loadKeyStore(ks.getType(), file, PASSWD);
    List aliases = ListUtil.fromIterator(new EnumerationIterator(ks2.aliases()));
    assertIsomorphic(SetUtil.set("mykey", "mycert"), SetUtil.theSet(aliases));
    assertNotNull(ks2.getCertificate("mycert"));
    assertNull(ks2.getCertificate("foocert"));
    assertEquals("JCEKS", ks2.getType());
}

From source file:be.fedict.hsm.model.KeyStoreLoaderBean.java

private Map<String, PrivateKeyEntry> loadKeys(KeyStoreEntity keyStoreEntity, KeyStore keyStore,
        String keyStorePassword)/*from w  ww.j a  v a  2s .c  o  m*/
        throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableEntryException {
    Enumeration<String> aliases = keyStore.aliases();
    Map<String, PrivateKeyEntry> keyStorePrivateKeys = new HashMap<String, PrivateKeyEntry>();
    while (aliases.hasMoreElements()) {
        String alias = aliases.nextElement();
        PrivateKeyEntry privateKeyEntry;
        if (null != keyStorePassword) {
            privateKeyEntry = (PrivateKeyEntry) keyStore.getEntry(alias,
                    new KeyStore.PasswordProtection(keyStorePassword.toCharArray()));
        } else {
            privateKeyEntry = (PrivateKeyEntry) keyStore.getEntry(alias, null);
        }
        keyStorePrivateKeys.put(alias, privateKeyEntry);
    }
    return keyStorePrivateKeys;
}

From source file:org.lockss.util.TestKeyStoreUtil.java

public void testStoreJks() throws Exception {
    File dir = getTempDir();//from www  .  j a  va2 s  .  c  o m
    File file = new File(dir, "test.ks");
    Properties p = initProps();
    p.put(KeyStoreUtil.PROP_KEYSTORE_FILE, file.toString());
    p.put(KeyStoreUtil.PROP_KEYSTORE_TYPE, "JKS");
    p.put(KeyStoreUtil.PROP_KEYSTORE_PROVIDER, "");
    assertFalse(file.exists());
    KeyStore ks = KeyStoreUtil.createKeyStore(p);
    assertTrue(file.exists());

    KeyStore ks2 = loadKeyStore(ks.getType(), file, PASSWD);
    List aliases = ListUtil.fromIterator(new EnumerationIterator(ks2.aliases()));
    assertIsomorphic(SetUtil.set("mykey", "mycert"), SetUtil.theSet(aliases));
    assertNotNull(ks2.getCertificate("mycert"));
    assertNull(ks2.getCertificate("foocert"));
    assertEquals("JKS", ks2.getType());
}

From source file:eu.europa.esig.dss.x509.KeyStoreCertificateSource.java

public List<CertificateToken> getCertificatesFromKeyStore() {
    List<CertificateToken> list = new ArrayList<CertificateToken>();

    KeyStore keyStore = getKeyStore();
    try {//  ww w. j  av a 2  s  .  com
        Enumeration<String> aliases = keyStore.aliases();
        while (aliases.hasMoreElements()) {
            String alias = aliases.nextElement();
            if (keyStore.isCertificateEntry(alias)) {
                Certificate certificate = keyStore.getCertificate(alias);
                CertificateToken certificateToken = DSSUtils.loadCertificate(certificate.getEncoded());
                list.add(certificateToken);
            }
        }
    } catch (Exception e) {
        logger.error("Unable to retrieve certificates from the keystore : " + e.getMessage(), e);
    }
    return list;
}

From source file:eu.europa.esig.dss.x509.KeyStoreCertificateSource.java

public List<CertificateToken> populate() {
    List<CertificateToken> list = new ArrayList<CertificateToken>();
    try {// www  .  j av  a 2 s  .  com
        KeyStore keyStore = getKeyStore();
        Enumeration<String> aliases = keyStore.aliases();
        while (aliases.hasMoreElements()) {
            String alias = aliases.nextElement();
            final Certificate certificate = keyStore.getCertificate(alias);
            if (certificate != null) {
                X509Certificate x509Certificate = (X509Certificate) certificate;
                logger.debug("Alias " + alias + " Cert " + x509Certificate.getSubjectDN());

                CertificateToken certToken = certPool.getInstance(new CertificateToken(x509Certificate),
                        CertificateSourceType.OTHER);
                list.add(certToken);
            }
            Certificate[] certificateChain = keyStore.getCertificateChain(alias);
            if (certificateChain != null) {
                for (Certificate chainCert : certificateChain) {
                    logger.debug("Alias " + alias + " Cert " + ((X509Certificate) chainCert).getSubjectDN());
                    CertificateToken certToken = certPool.getInstance(
                            new CertificateToken((X509Certificate) chainCert),
                            CertificateSourceType.OCSP_RESPONSE);
                    if (!list.contains(certToken)) {
                        list.add(certToken);
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new DSSEncodingException(MSG.CERTIFICATE_CANNOT_BE_READ, e);
    }
    return list;
}

From source file:org.apache.directory.studio.connection.core.StudioKeyStoreManager.java

/**
 * Gets the certificates contained in the key store.
 * /*from w w w . j av  a 2  s . c  o  m*/
 * @return the certificates
 */
public X509Certificate[] getCertificates() throws CertificateException {
    try {
        List<X509Certificate> certificateList = new ArrayList<X509Certificate>();
        KeyStore keyStore = getKeyStore();
        Enumeration<String> aliases = keyStore.aliases();
        while (aliases.hasMoreElements()) {
            String alias = aliases.nextElement();
            Certificate certificate = keyStore.getCertificate(alias);
            if (certificate instanceof X509Certificate) {
                certificateList.add((X509Certificate) certificate);
            }
        }
        return certificateList.toArray(new X509Certificate[0]);
    } catch (KeyStoreException e) {
        throw new CertificateException(Messages.StudioKeyStoreManager_CantReadTrustStore, e);
    }
}

From source file:org.wso2.identity.integration.common.clients.KeyStoreAdminClient.java

public boolean isPrivateKeyStore(byte[] content, String password, String type) throws Exception {
    try {/*from w  w  w.  java  2s  .com*/
        boolean isPrivateStore = false;
        ByteArrayInputStream stream = new ByteArrayInputStream(content);
        KeyStore store = KeyStore.getInstance(type);
        store.load(stream, password.toCharArray());
        Enumeration<String> aliases = store.aliases();
        while (aliases.hasMoreElements()) {
            String value = aliases.nextElement();
            if (store.isKeyEntry(value)) {
                isPrivateStore = true;
                break;
            }
        }
        return isPrivateStore;
    } catch (Exception e) {
        log.error("Error in checking private key store.", e);
        throw new Exception("Error in checking private key store.");
    }
}

From source file:org.sipfoundry.sipxconfig.cert.JavaKeyStore.java

/**
 * Compare entries from one keystore with another
 *
 * @return false if they differ// ww w.  j  ava 2 s. c o  m
 */
public boolean isEqual(InputStream bStream) {
    try {
        KeyStore b = KeyStore.getInstance(m_type);
        b.load(bStream, m_password);
        Set<String> bAliases = toSet(b.aliases());
        Set<String> aAliases = toSet(m_store.aliases());
        KeyStore.PasswordProtection keyPass = new KeyStore.PasswordProtection(m_password);
        if (!aAliases.equals(bAliases)) {
            return false;
        }

        for (String alias : aAliases) {

            // weird, when not using password, cert require null and keys
            // require the password given to the filestore.
            KeyStore.PasswordProtection password = keyPass;
            if (m_store.entryInstanceOf(alias, KeyStore.TrustedCertificateEntry.class)) {
                password = null;
            }

            Entry aEntry = m_store.getEntry(alias, password);
            Entry bEntry = b.getEntry(alias, password);
            if (!isEqual(aEntry, bEntry)) {
                return false;
            }
        }

        return true;
    } catch (Exception e) {
        LOG.error("Could not read store", e);
        return false;
    }
}

From source file:com.gnut3ll4.android.basicandroidkeystore.MainActivity.java

public ArrayList<String> getAliases()
        throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException {
    KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");

    keyStore.load(null, null);/*  ww  w .  j a  va2  s  .  c  o  m*/

    return Collections.list(keyStore.aliases());
}

From source file:org.apache.ws.security.components.crypto.Merlin.java

private static String createKeyStoreErrorMessage(KeyStore keystore) throws KeyStoreException {
    Enumeration<String> aliases = keystore.aliases();
    StringBuilder sb = new StringBuilder(keystore.size() * 7);
    boolean firstAlias = true;
    while (aliases.hasMoreElements()) {
        if (!firstAlias) {
            sb.append(", ");
        }/*ww  w .  java2  s. c  o m*/
        sb.append(aliases.nextElement());
        firstAlias = false;
    }
    String msg = " in keystore of type [" + keystore.getType() + "] from provider [" + keystore.getProvider()
            + "] with size [" + keystore.size() + "] and aliases: {" + sb.toString() + "}";
    return msg;
}