Search in sources :

Example 6 with PasswordProtection

use of java.security.KeyStore.PasswordProtection in project robovm by robovm.

the class TestKeyStore method privateKey.

/**
     * Return the only private key in a keystore for the given
     * algorithms. Throws IllegalStateException if there are are more
     * or less than one.
     */
public static PrivateKeyEntry privateKey(KeyStore keyStore, char[] keyPassword, String keyAlgorithm, String signatureAlgorithm) {
    try {
        PrivateKeyEntry found = null;
        PasswordProtection password = new PasswordProtection(keyPassword);
        for (String alias : Collections.list(keyStore.aliases())) {
            if (!keyStore.entryInstanceOf(alias, PrivateKeyEntry.class)) {
                continue;
            }
            PrivateKeyEntry privateKey = (PrivateKeyEntry) keyStore.getEntry(alias, password);
            if (!privateKey.getPrivateKey().getAlgorithm().equals(keyAlgorithm)) {
                continue;
            }
            X509Certificate certificate = (X509Certificate) privateKey.getCertificate();
            if (!certificate.getSigAlgName().contains(signatureAlgorithm)) {
                continue;
            }
            if (found != null) {
                throw new IllegalStateException("KeyStore has more than one private key for " + " keyAlgorithm: " + keyAlgorithm + " signatureAlgorithm: " + signatureAlgorithm + "\nfirst: " + found.getPrivateKey() + "\nsecond: " + privateKey.getPrivateKey());
            }
            found = privateKey;
        }
        if (found == null) {
            throw new IllegalStateException("KeyStore contained no private key for " + " keyAlgorithm: " + keyAlgorithm + " signatureAlgorithm: " + signatureAlgorithm);
        }
        return found;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : DEROctetString(com.android.org.bouncycastle.asn1.DEROctetString) PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry) PasswordProtection(java.security.KeyStore.PasswordProtection) X509Certificate(java.security.cert.X509Certificate) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) UnknownHostException(java.net.UnknownHostException)

Example 7 with PasswordProtection

use of java.security.KeyStore.PasswordProtection in project robovm by robovm.

the class KeyStoreTest method testKeyStoreCreate.

public void testKeyStoreCreate() {
    KeyStore keyStore = null;
    try {
        keyStore = KeyStore.getInstance(algorithmName);
    } catch (KeyStoreException e) {
        fail(e.getMessage());
    }
    try {
        keyStore.load(null, "the secret password".toCharArray());
    } catch (NoSuchAlgorithmException e) {
        fail(e.getMessage());
    } catch (CertificateException e) {
        fail(e.getMessage());
    } catch (IOException e) {
        fail(e.getMessage());
    }
    CertificateFactory certificateFactory = null;
    try {
        certificateFactory = CertificateFactory.getInstance("X.509");
    } catch (CertificateException e) {
        fail(e.getMessage());
    }
    Certificate certificate = null;
    try {
        certificate = certificateFactory.generateCertificate(new ByteArrayInputStream(encodedCertificate.getBytes()));
    } catch (CertificateException e) {
        fail(e.getMessage());
    }
    KeyPairGenerator generator = null;
    try {
        generator = KeyPairGenerator.getInstance(certificate.getPublicKey().getAlgorithm());
    } catch (NoSuchAlgorithmException e) {
        fail(e.getMessage());
    }
    KeyPair keyPair = generator.generateKeyPair();
    PrivateKeyEntry privateKeyEntry = new PrivateKeyEntry(keyPair.getPrivate(), new Certificate[] { certificate });
    try {
        keyStore.setEntry("aPrivateKey", privateKeyEntry, new PasswordProtection("the key password".toCharArray()));
    } catch (KeyStoreException e) {
        fail(e.getMessage());
    }
    try {
        assertTrue(keyStore.containsAlias("aPrivateKey"));
    } catch (KeyStoreException e) {
        fail(e.getMessage());
    }
    try {
        PrivateKeyEntry entry = (PrivateKeyEntry) keyStore.getEntry("aPrivateKey", new PasswordProtection("the key password".toCharArray()));
        PrivateKey privateKey = entry.getPrivateKey();
        assertEquals(keyPair.getPrivate(), privateKey);
    } catch (NoSuchAlgorithmException e) {
        fail(e.getMessage());
    } catch (UnrecoverableEntryException e) {
        fail(e.getMessage());
    } catch (KeyStoreException e) {
        fail(e.getMessage());
    }
    try {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        keyStore.store(stream, "the keystore password".toCharArray());
        assertTrue("keystore not written", stream.size() > 0);
    } catch (KeyStoreException e) {
        fail(e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        fail(e.getMessage());
    } catch (CertificateException e) {
        fail(e.getMessage());
    } catch (IOException e) {
        fail(e.getMessage());
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyPairGenerator(java.security.KeyPairGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeyStore(java.security.KeyStore) CertificateFactory(java.security.cert.CertificateFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) UnrecoverableEntryException(java.security.UnrecoverableEntryException) PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry) PasswordProtection(java.security.KeyStore.PasswordProtection) Certificate(java.security.cert.Certificate)

Aggregations

PasswordProtection (java.security.KeyStore.PasswordProtection)7 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)4 KeyStoreException (java.security.KeyStoreException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 Builder (java.security.KeyStore.Builder)3 UnrecoverableEntryException (java.security.UnrecoverableEntryException)3 CertificateException (java.security.cert.CertificateException)3 KeyStoreBuilderParameters (javax.net.ssl.KeyStoreBuilderParameters)3 DEROctetString (com.android.org.bouncycastle.asn1.DEROctetString)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 KeyPair (java.security.KeyPair)2 KeyPairGenerator (java.security.KeyPairGenerator)2 KeyStore (java.security.KeyStore)2 PrivateKey (java.security.PrivateKey)2 Certificate (java.security.cert.Certificate)2 CertificateFactory (java.security.cert.CertificateFactory)2 TestKeyStore (libcore.java.security.TestKeyStore)2 KeyStore (android.security.KeyStore)1