Example usage for java.security KeyStore getClass

List of usage examples for java.security KeyStore getClass

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

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

/**
 * For WINDOWS-MY keystore fixes problem with non-unique aliases
 * //from w w  w  . j  a  va  2 s .co m
 * @param keyStore
 */
@SuppressWarnings("unchecked")
private static void fixAliases(final KeyStore keyStore) {
    Field field;
    KeyStoreSpi keyStoreVeritable;
    final Set<String> tmpAliases = new HashSet<String>();
    try {
        field = keyStore.getClass().getDeclaredField("keyStoreSpi");
        field.setAccessible(true);
        keyStoreVeritable = (KeyStoreSpi) field.get(keyStore);

        if ("sun.security.mscapi.KeyStore$MY".equals(keyStoreVeritable.getClass().getName())) {
            Collection<Object> entries;
            String alias, hashCode;
            X509Certificate[] certificates;

            field = keyStoreVeritable.getClass().getEnclosingClass().getDeclaredField("entries");
            field.setAccessible(true);
            entries = (Collection<Object>) field.get(keyStoreVeritable);

            for (Object entry : entries) {
                field = entry.getClass().getDeclaredField("certChain");
                field.setAccessible(true);
                certificates = (X509Certificate[]) field.get(entry);

                hashCode = Integer.toString(certificates[0].hashCode());

                field = entry.getClass().getDeclaredField("alias");
                field.setAccessible(true);
                alias = (String) field.get(entry);
                String tmpAlias = alias;
                int i = 0;
                while (tmpAliases.contains(tmpAlias)) {
                    i++;
                    tmpAlias = alias + "-" + i;
                }
                tmpAliases.add(tmpAlias);
                if (!alias.equals(hashCode)) {
                    field.set(entry, tmpAlias);
                }
            }
        }
    } catch (Exception exception) {
        // nothing to do here
    }
}

From source file:net.java.sip.communicator.impl.certificate.CertificateServiceImpl.java

/**
 * Appends an index number to the alias of each entry in the KeyStore.
 * //from ww w .  ja v a2  s.c  om
 * The Windows TrustStore might contain multiple entries with the same
 * "Friendly Name", which is directly used as the "Alias" for the KeyStore.
 * As all operations of the KeyStore operate with these non-unique names,
 * PKIX path building could fail and in the end lead to certificate warnings
 * for perfectly valid certificates.
 * 
 * @throws Exception when the aliases could not be renamed.
 */
private static int keyStoreAppendIndex(KeyStore ks) throws Exception {
    Field keyStoreSpiField = ks.getClass().getDeclaredField("keyStoreSpi");
    keyStoreSpiField.setAccessible(true);
    KeyStoreSpi keyStoreSpi = (KeyStoreSpi) keyStoreSpiField.get(ks);

    if ("sun.security.mscapi.KeyStore$ROOT".equals(keyStoreSpi.getClass().getName())) {
        Field entriesField = keyStoreSpi.getClass().getEnclosingClass().getDeclaredField("entries");
        entriesField.setAccessible(true);
        Collection<?> entries = (Collection<?>) entriesField.get(keyStoreSpi);

        int i = 0;
        for (Object entry : entries) {
            Field aliasField = entry.getClass().getDeclaredField("alias");
            aliasField.setAccessible(true);
            String alias = (String) aliasField.get(entry);
            aliasField.set(entry, alias.concat("_").concat(Integer.toString(i++)));
        }

        return i;
    }

    return -1;
}