Search in sources :

Example 1 with PasswordProtection

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

the class KeyManagerFactoryTest method test_KeyManagerFactory.

private void test_KeyManagerFactory(KeyManagerFactory kmf) throws Exception {
    assertNotNull(kmf);
    assertNotNull(kmf.getAlgorithm());
    assertNotNull(kmf.getProvider());
    // before init
    try {
        kmf.getKeyManagers();
        fail();
    } catch (IllegalStateException expected) {
    }
    // init with null ManagerFactoryParameters
    try {
        kmf.init(null);
        fail();
    } catch (InvalidAlgorithmParameterException expected) {
    }
    // init with useless ManagerFactoryParameters
    try {
        kmf.init(new UselessManagerFactoryParameters());
        fail();
    } catch (InvalidAlgorithmParameterException expected) {
    }
    // init with KeyStoreBuilderParameters ManagerFactoryParameters
    PasswordProtection pp = new PasswordProtection(getTestKeyStore().storePassword);
    Builder builder = Builder.newInstance(getTestKeyStore().keyStore, pp);
    KeyStoreBuilderParameters ksbp = new KeyStoreBuilderParameters(builder);
    if (supportsManagerFactoryParameters(kmf.getAlgorithm())) {
        kmf.init(ksbp);
        test_KeyManagerFactory_getKeyManagers(kmf, false);
    } else {
        try {
            kmf.init(ksbp);
            fail();
        } catch (InvalidAlgorithmParameterException expected) {
        }
    }
    // init with null for default behavior
    kmf.init(null, null);
    test_KeyManagerFactory_getKeyManagers(kmf, true);
    // init with specific key store and password
    kmf.init(getTestKeyStore().keyStore, getTestKeyStore().storePassword);
    test_KeyManagerFactory_getKeyManagers(kmf, false);
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyStoreBuilderParameters(javax.net.ssl.KeyStoreBuilderParameters) Builder(java.security.KeyStore.Builder) PasswordProtection(java.security.KeyStore.PasswordProtection)

Example 2 with PasswordProtection

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

the class KeyStoreBuilderParametersTest method test_init_List.

public void test_init_List() {
    TestKeyStore testKeyStore1 = TestKeyStore.getClient();
    TestKeyStore testKeyStore2 = TestKeyStore.getServer();
    Builder builder1 = Builder.newInstance(testKeyStore1.keyStore, new PasswordProtection(testKeyStore1.storePassword));
    Builder builder2 = Builder.newInstance(testKeyStore2.keyStore, new PasswordProtection(testKeyStore2.storePassword));
    List list = Arrays.asList(builder1, builder2);
    KeyStoreBuilderParameters ksbp = new KeyStoreBuilderParameters(list);
    assertNotNull(ksbp);
    assertNotNull(ksbp.getParameters());
    assertNotSame(list, ksbp.getParameters());
    assertEquals(2, ksbp.getParameters().size());
    assertSame(builder1, ksbp.getParameters().get(0));
    assertSame(builder2, ksbp.getParameters().get(1));
    // confirm result is not modifiable
    try {
        ksbp.getParameters().set(0, builder2);
        fail();
    } catch (UnsupportedOperationException expected) {
    }
    // confirm result is a copy of original
    list.set(0, builder2);
    assertSame(builder1, ksbp.getParameters().get(0));
}
Also used : TestKeyStore(libcore.java.security.TestKeyStore) KeyStoreBuilderParameters(javax.net.ssl.KeyStoreBuilderParameters) Builder(java.security.KeyStore.Builder) List(java.util.List) PasswordProtection(java.security.KeyStore.PasswordProtection)

Example 3 with PasswordProtection

use of java.security.KeyStore.PasswordProtection in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class CertInstallerHelper method extractCertificate.

private void extractCertificate(String certFile, String password) {
    InputStream in = null;
    final byte[] raw;
    java.security.KeyStore keystore = null;
    try {
        // Read .p12 file from SDCARD and extract with password
        in = new FileInputStream(new File(Environment.getExternalStorageDirectory(), certFile));
        raw = Streams.readFully(in);
        keystore = java.security.KeyStore.getInstance("PKCS12");
        PasswordProtection passwordProtection = new PasswordProtection(password.toCharArray());
        keystore.load(new ByteArrayInputStream(raw), passwordProtection.getPassword());
        // Install certificates and private keys
        Enumeration<String> aliases = keystore.aliases();
        if (!aliases.hasMoreElements()) {
            Assert.fail("key store failed to put in keychain");
        }
        ArrayList<String> aliasesList = Collections.list(aliases);
        // The keystore is initialized for each test case, there will
        // be only one alias in the keystore
        Assert.assertEquals(1, aliasesList.size());
        String alias = aliasesList.get(0);
        java.security.KeyStore.Entry entry = keystore.getEntry(alias, passwordProtection);
        Log.d(TAG, "extracted alias = " + alias + ", entry=" + entry.getClass());
        if (entry instanceof PrivateKeyEntry) {
            Assert.assertTrue(installFrom((PrivateKeyEntry) entry));
        }
    } catch (IOException e) {
        Assert.fail("Failed to read certficate: " + e);
    } catch (KeyStoreException e) {
        Log.e(TAG, "failed to extract certificate" + e);
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "failed to extract certificate" + e);
    } catch (CertificateException e) {
        Log.e(TAG, "failed to extract certificate" + e);
    } catch (UnrecoverableEntryException e) {
        Log.e(TAG, "failed to extract certificate" + e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                Log.e(TAG, "close FileInputStream error: " + e);
            }
        }
    }
}
Also used : ASN1InputStream(com.android.org.bouncycastle.asn1.ASN1InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CertificateException(java.security.cert.CertificateException) DEROctetString(com.android.org.bouncycastle.asn1.DEROctetString) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStore(android.security.KeyStore) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) UnrecoverableEntryException(java.security.UnrecoverableEntryException) File(java.io.File) PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry) PasswordProtection(java.security.KeyStore.PasswordProtection)

Example 4 with PasswordProtection

use of java.security.KeyStore.PasswordProtection in project j2objc by google.

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)

Example 5 with PasswordProtection

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

the class KeyStoreBuilderParametersTest method test_init_Builder.

public void test_init_Builder() {
    TestKeyStore testKeyStore = TestKeyStore.getClient();
    Builder builder = Builder.newInstance(testKeyStore.keyStore, new PasswordProtection(testKeyStore.storePassword));
    KeyStoreBuilderParameters ksbp = new KeyStoreBuilderParameters(builder);
    assertNotNull(ksbp);
    assertNotNull(ksbp.getParameters());
    assertEquals(1, ksbp.getParameters().size());
    assertSame(builder, ksbp.getParameters().get(0));
}
Also used : TestKeyStore(libcore.java.security.TestKeyStore) KeyStoreBuilderParameters(javax.net.ssl.KeyStoreBuilderParameters) Builder(java.security.KeyStore.Builder) PasswordProtection(java.security.KeyStore.PasswordProtection)

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