Example usage for java.security KeyStore.Entry getClass

List of usage examples for java.security KeyStore.Entry getClass

Introduction

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

Prototype

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

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.apache.nifi.toolkit.tls.standalone.TlsToolkitStandaloneTest.java

private Properties checkHostDirAndReturnNifiProperties(String hostname, String dnPrefix, String dnSuffix,
        X509Certificate rootCert) throws Exception {
    File hostDir = new File(tempDir, hostname);
    Properties nifiProperties = new Properties();
    try (InputStream inputStream = new FileInputStream(
            new File(hostDir, TlsToolkitStandalone.NIFI_PROPERTIES))) {
        nifiProperties.load(inputStream);
    }/*  w  ww . j  ava  2s  . com*/

    String trustStoreType = nifiProperties.getProperty(NiFiProperties.SECURITY_TRUSTSTORE_TYPE);
    assertEquals(KeystoreType.JKS.toString().toLowerCase(), trustStoreType.toLowerCase());
    KeyStore trustStore = KeyStoreUtils.getTrustStore(trustStoreType);
    try (InputStream inputStream = new FileInputStream(new File(hostDir, "truststore." + trustStoreType))) {
        trustStore.load(inputStream,
                nifiProperties.getProperty(NiFiProperties.SECURITY_TRUSTSTORE_PASSWD).toCharArray());
    }

    String trustStoreFilename = BaseCommandLine.TRUSTSTORE + trustStoreType;
    assertEquals("./conf/" + trustStoreFilename,
            nifiProperties.getProperty(NiFiProperties.SECURITY_TRUSTSTORE));

    Certificate certificate = trustStore.getCertificate(TlsToolkitStandalone.NIFI_CERT);
    assertEquals(rootCert, certificate);

    String keyStoreType = nifiProperties.getProperty(NiFiProperties.SECURITY_KEYSTORE_TYPE);
    String keyStoreFilename = BaseCommandLine.KEYSTORE + keyStoreType;
    File keyStoreFile = new File(hostDir, keyStoreFilename);
    assertEquals("./conf/" + keyStoreFilename, nifiProperties.getProperty(NiFiProperties.SECURITY_KEYSTORE));

    KeyStore keyStore = KeyStoreUtils.getKeyStore(keyStoreType);
    char[] keyStorePassword = nifiProperties.getProperty(NiFiProperties.SECURITY_KEYSTORE_PASSWD).toCharArray();
    try (InputStream inputStream = new FileInputStream(keyStoreFile)) {
        keyStore.load(inputStream, keyStorePassword);
    }

    char[] keyPassword = nifiProperties.getProperty(NiFiProperties.SECURITY_KEY_PASSWD).toCharArray();
    if (keyPassword == null || keyPassword.length == 0) {
        keyPassword = keyStorePassword;
    }

    KeyStore.Entry entry = keyStore.getEntry(TlsToolkitStandalone.NIFI_KEY,
            new KeyStore.PasswordProtection(keyPassword));
    assertEquals(KeyStore.PrivateKeyEntry.class, entry.getClass());

    KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) entry;

    Certificate[] certificateChain = privateKeyEntry.getCertificateChain();

    assertEquals(2, certificateChain.length);
    assertEquals(rootCert, certificateChain[1]);
    certificateChain[1].verify(rootCert.getPublicKey());
    certificateChain[0].verify(rootCert.getPublicKey());
    TlsConfig tlsConfig = new TlsConfig();
    tlsConfig.setDnPrefix(dnPrefix);
    tlsConfig.setDnSuffix(dnSuffix);
    assertEquals(tlsConfig.calcDefaultDn(hostname), CertificateUtils
            .convertAbstractX509Certificate(certificateChain[0]).getSubjectX500Principal().getName());
    TlsCertificateAuthorityTest.assertPrivateAndPublicKeyMatch(privateKeyEntry.getPrivateKey(),
            certificateChain[0].getPublicKey());
    return nifiProperties;
}

From source file:org.kuali.rice.ksb.security.admin.service.impl.JavaSecurityManagementServiceImpl.java

public List<KeyStoreEntryDataContainer> getListOfModuleKeyStoreEntries() {
    List<KeyStoreEntryDataContainer> keyStoreEntries = new ArrayList<KeyStoreEntryDataContainer>();
    try {/* w  w w  .  ja v  a 2 s.  c om*/
        KeyStore moduleKeyStore = getModuleKeyStore();

        // List the aliases
        for (Enumeration<String> enumer = moduleKeyStore.aliases(); enumer.hasMoreElements();) {
            String alias = (String) enumer.nextElement();
            KeyStoreEntryDataContainer dataContainer = new KeyStoreEntryDataContainer(alias,
                    moduleKeyStore.getCreationDate(alias));
            KeyStore.PasswordProtection passwordProtection = null;
            if (moduleKeyStore.isKeyEntry(alias)) {
                passwordProtection = new KeyStore.PasswordProtection(getModuleKeyStorePassword().toCharArray());
            }
            KeyStore.Entry entry = moduleKeyStore.getEntry(alias, passwordProtection);
            dataContainer.setType(entry.getClass());
            keyStoreEntries.add(dataContainer);
        }
    } catch (KeyStoreException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } catch (UnrecoverableEntryException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    return keyStoreEntries;
}

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

public boolean isEqual(KeyStore.Entry a, KeyStore.Entry b) {
    if (!a.getClass().equals(b.getClass())) {
        return false;
    }/*from   w w w . jav  a2 s.  c o m*/
    if (a instanceof KeyStore.PrivateKeyEntry) {
        KeyStore.PrivateKeyEntry aKey = (KeyStore.PrivateKeyEntry) a;
        KeyStore.PrivateKeyEntry bKey = (KeyStore.PrivateKeyEntry) b;
        if (!aKey.getPrivateKey().equals(bKey.getPrivateKey())) {
            return false;
        }
        if (!ArrayUtils.isEquals(aKey.getCertificateChain(), bKey.getCertificateChain())) {
            return false;
        }
        return true;
    }

    if (a instanceof KeyStore.TrustedCertificateEntry) {
        KeyStore.TrustedCertificateEntry aCert = (KeyStore.TrustedCertificateEntry) a;
        KeyStore.TrustedCertificateEntry bCert = (KeyStore.TrustedCertificateEntry) b;
        return aCert.getTrustedCertificate().equals(bCert.getTrustedCertificate());
    }

    if (a instanceof KeyStore.SecretKeyEntry) {
        KeyStore.SecretKeyEntry aSecret = (KeyStore.SecretKeyEntry) a;
        KeyStore.SecretKeyEntry bSecret = (KeyStore.SecretKeyEntry) b;
        return (aSecret.getSecretKey().equals(bSecret.getSecretKey()));
    }

    LOG.error("Unrecognized keystore entry " + a.getClass());
    return false;
}