Search in sources :

Example 6 with IKeyChainService

use of android.security.IKeyChainService in project android_frameworks_base by DirtyUnicorns.

the class DevicePolicyManagerService method installKeyPair.

@Override
public boolean installKeyPair(ComponentName who, byte[] privKey, byte[] cert, byte[] chain, String alias, boolean requestAccess) {
    enforceCanManageInstalledKeys(who);
    final int callingUid = mInjector.binderGetCallingUid();
    final long id = mInjector.binderClearCallingIdentity();
    try {
        final KeyChainConnection keyChainConnection = KeyChain.bindAsUser(mContext, UserHandle.getUserHandleForUid(callingUid));
        try {
            IKeyChainService keyChain = keyChainConnection.getService();
            if (!keyChain.installKeyPair(privKey, cert, chain, alias)) {
                return false;
            }
            if (requestAccess) {
                keyChain.setGrant(callingUid, alias, true);
            }
            return true;
        } catch (RemoteException e) {
            Log.e(LOG_TAG, "Installing certificate", e);
        } finally {
            keyChainConnection.close();
        }
    } catch (InterruptedException e) {
        Log.w(LOG_TAG, "Interrupted while installing certificate", e);
        Thread.currentThread().interrupt();
    } finally {
        mInjector.binderRestoreCallingIdentity(id);
    }
    return false;
}
Also used : IKeyChainService(android.security.IKeyChainService) KeyChainConnection(android.security.KeyChain.KeyChainConnection) RemoteException(android.os.RemoteException)

Example 7 with IKeyChainService

use of android.security.IKeyChainService in project android_packages_apps_Settings by LineageOS.

the class TrustedCredentialsSettings method getX509CertsFromCertHolder.

@Override
public List<X509Certificate> getX509CertsFromCertHolder(CertHolder certHolder) {
    List<X509Certificate> certificates = null;
    try {
        KeyChainConnection keyChainConnection = mKeyChainConnectionByProfileId.get(certHolder.mProfileId);
        IKeyChainService service = keyChainConnection.getService();
        List<String> chain = service.getCaCertificateChainAliases(certHolder.mAlias, true);
        final int n = chain.size();
        certificates = new ArrayList<X509Certificate>(n);
        for (int i = 0; i < n; ++i) {
            byte[] encodedCertificate = service.getEncodedCaCertificate(chain.get(i), true);
            X509Certificate certificate = KeyChain.toCertificate(encodedCertificate);
            certificates.add(certificate);
        }
    } catch (RemoteException ex) {
        Log.e(TAG, "RemoteException while retrieving certificate chain for root " + certHolder.mAlias, ex);
    }
    return certificates;
}
Also used : IKeyChainService(android.security.IKeyChainService) KeyChainConnection(android.security.KeyChain.KeyChainConnection) RemoteException(android.os.RemoteException) X509Certificate(java.security.cert.X509Certificate)

Example 8 with IKeyChainService

use of android.security.IKeyChainService in project platform_frameworks_base by android.

the class DevicePolicyManagerService method installKeyPair.

@Override
public boolean installKeyPair(ComponentName who, byte[] privKey, byte[] cert, byte[] chain, String alias, boolean requestAccess) {
    enforceCanManageInstalledKeys(who);
    final int callingUid = mInjector.binderGetCallingUid();
    final long id = mInjector.binderClearCallingIdentity();
    try {
        final KeyChainConnection keyChainConnection = KeyChain.bindAsUser(mContext, UserHandle.getUserHandleForUid(callingUid));
        try {
            IKeyChainService keyChain = keyChainConnection.getService();
            if (!keyChain.installKeyPair(privKey, cert, chain, alias)) {
                return false;
            }
            if (requestAccess) {
                keyChain.setGrant(callingUid, alias, true);
            }
            return true;
        } catch (RemoteException e) {
            Log.e(LOG_TAG, "Installing certificate", e);
        } finally {
            keyChainConnection.close();
        }
    } catch (InterruptedException e) {
        Log.w(LOG_TAG, "Interrupted while installing certificate", e);
        Thread.currentThread().interrupt();
    } finally {
        mInjector.binderRestoreCallingIdentity(id);
    }
    return false;
}
Also used : IKeyChainService(android.security.IKeyChainService) KeyChainConnection(android.security.KeyChain.KeyChainConnection) RemoteException(android.os.RemoteException)

Example 9 with IKeyChainService

use of android.security.IKeyChainService in project platform_frameworks_base by android.

the class DevicePolicyManagerService method removeKeyPair.

@Override
public boolean removeKeyPair(ComponentName who, String alias) {
    enforceCanManageInstalledKeys(who);
    final UserHandle userHandle = new UserHandle(UserHandle.getCallingUserId());
    final long id = Binder.clearCallingIdentity();
    try {
        final KeyChainConnection keyChainConnection = KeyChain.bindAsUser(mContext, userHandle);
        try {
            IKeyChainService keyChain = keyChainConnection.getService();
            return keyChain.removeKeyPair(alias);
        } catch (RemoteException e) {
            Log.e(LOG_TAG, "Removing keypair", e);
        } finally {
            keyChainConnection.close();
        }
    } catch (InterruptedException e) {
        Log.w(LOG_TAG, "Interrupted while removing keypair", e);
        Thread.currentThread().interrupt();
    } finally {
        Binder.restoreCallingIdentity(id);
    }
    return false;
}
Also used : IKeyChainService(android.security.IKeyChainService) UserHandle(android.os.UserHandle) KeyChainConnection(android.security.KeyChain.KeyChainConnection) RemoteException(android.os.RemoteException)

Example 10 with IKeyChainService

use of android.security.IKeyChainService in project android_packages_apps_Settings by omnirom.

the class TrustedCredentialsSettings method getX509CertsFromCertHolder.

@Override
public List<X509Certificate> getX509CertsFromCertHolder(CertHolder certHolder) {
    List<X509Certificate> certificates = null;
    try {
        KeyChainConnection keyChainConnection = mKeyChainConnectionByProfileId.get(certHolder.mProfileId);
        IKeyChainService service = keyChainConnection.getService();
        List<String> chain = service.getCaCertificateChainAliases(certHolder.mAlias, true);
        final int n = chain.size();
        certificates = new ArrayList<X509Certificate>(n);
        for (int i = 0; i < n; ++i) {
            byte[] encodedCertificate = service.getEncodedCaCertificate(chain.get(i), true);
            X509Certificate certificate = KeyChain.toCertificate(encodedCertificate);
            certificates.add(certificate);
        }
    } catch (RemoteException ex) {
        Log.e(TAG, "RemoteException while retrieving certificate chain for root " + certHolder.mAlias, ex);
    }
    return certificates;
}
Also used : IKeyChainService(android.security.IKeyChainService) KeyChainConnection(android.security.KeyChain.KeyChainConnection) RemoteException(android.os.RemoteException) X509Certificate(java.security.cert.X509Certificate)

Aggregations

RemoteException (android.os.RemoteException)13 IKeyChainService (android.security.IKeyChainService)13 KeyChainConnection (android.security.KeyChain.KeyChainConnection)13 X509Certificate (java.security.cert.X509Certificate)7 UserHandle (android.os.UserHandle)3