Search in sources :

Example 36 with KeyStore

use of android.security.KeyStore in project android_packages_apps_Settings by crdroidandroid.

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 37 with KeyStore

use of android.security.KeyStore in project platform_packages_apps_Settings by BlissRoms.

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 38 with KeyStore

use of android.security.KeyStore in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class ConfigDialogFragment method onClick.

@Override
public void onClick(DialogInterface dialogInterface, int button) {
    ConfigDialog dialog = (ConfigDialog) getDialog();
    VpnProfile profile = dialog.getProfile();
    if (button == DialogInterface.BUTTON_POSITIVE) {
        // Possibly throw up a dialog to explain lockdown VPN.
        final boolean shouldLockdown = dialog.isVpnAlwaysOn();
        final boolean shouldConnect = shouldLockdown || !dialog.isEditing();
        final boolean wasLockdown = VpnUtils.isAnyLockdownActive(mContext);
        try {
            final boolean replace = VpnUtils.isVpnActive(mContext);
            if (shouldConnect && !isConnected(profile) && ConfirmLockdownFragment.shouldShow(replace, wasLockdown, shouldLockdown)) {
                final Bundle opts = new Bundle();
                opts.putParcelable(ARG_PROFILE, profile);
                ConfirmLockdownFragment.show(this, replace, /* alwaysOn */
                shouldLockdown, /* from */
                wasLockdown, /* to */
                shouldLockdown, opts);
            } else if (shouldConnect) {
                connect(profile, shouldLockdown);
            } else {
                save(profile, false);
            }
        } catch (RemoteException e) {
            Log.w(TAG, "Failed to check active VPN state. Skipping.", e);
        }
    } else if (button == DialogInterface.BUTTON_NEUTRAL) {
        // Disable profile if connected
        if (!disconnect(profile)) {
            Log.e(TAG, "Failed to disconnect VPN. Leaving profile in keystore.");
            return;
        }
        // Delete from KeyStore
        KeyStore keyStore = KeyStore.getInstance();
        keyStore.delete(Credentials.VPN + profile.key, KeyStore.UID_SELF);
        updateLockdownVpn(false, profile);
    }
    dismiss();
}
Also used : VpnProfile(com.android.internal.net.VpnProfile) Bundle(android.os.Bundle) RemoteException(android.os.RemoteException) KeyStore(android.security.KeyStore)

Aggregations

KeyStore (android.security.KeyStore)38 RemoteException (android.os.RemoteException)14 IOException (java.io.IOException)14 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)12 CertificateException (java.security.cert.CertificateException)12 KeyStoreException (java.security.KeyStoreException)11 Bundle (android.os.Bundle)7 VpnProfile (com.android.internal.net.VpnProfile)7 ASN1InputStream (com.android.org.bouncycastle.asn1.ASN1InputStream)7 DEROctetString (com.android.org.bouncycastle.asn1.DEROctetString)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 File (java.io.File)7 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)7 PasswordProtection (java.security.KeyStore.PasswordProtection)7 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)7 UnrecoverableEntryException (java.security.UnrecoverableEntryException)7 IGateKeeperService (android.service.gatekeeper.IGateKeeperService)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 InvalidKeyException (java.security.InvalidKeyException)5