From e6a461341ccf3b952427daf40c973d9914cdb270 Mon Sep 17 00:00:00 2001 From: Chad Brubaker Date: Tue, 10 Feb 2015 21:33:23 -0800 Subject: [PATCH] Add new IKeystoreService methods to KeyStore Add wrappers for all the new IKeystoreService keymaster 0.4 methods to android.security.KeyStore. Change-Id: Icb5500cfffb62d1af326edf326e1b9b67e5cece9 --- keystore/java/android/security/KeyStore.java | 111 +++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/keystore/java/android/security/KeyStore.java b/keystore/java/android/security/KeyStore.java index e753a7c931f2..bfbf02884498 100644 --- a/keystore/java/android/security/KeyStore.java +++ b/keystore/java/android/security/KeyStore.java @@ -18,8 +18,14 @@ package android.security; import com.android.org.conscrypt.NativeCrypto; +import android.os.Binder; +import android.os.IBinder; import android.os.RemoteException; import android.os.ServiceManager; +import android.security.keymaster.ExportResult; +import android.security.keymaster.KeyCharacteristics; +import android.security.keymaster.KeymasterArguments; +import android.security.keymaster.OperationResult; import android.util.Log; import java.util.Locale; @@ -58,6 +64,8 @@ public class KeyStore { private final IKeystoreService mBinder; + private IBinder mToken; + private KeyStore(IKeystoreService binder) { mBinder = binder; } @@ -68,6 +76,13 @@ public class KeyStore { return new KeyStore(keystore); } + private synchronized IBinder getToken() { + if (mToken == null) { + mToken = new Binder(); + } + return mToken; + } + static int getKeyTypeForAlgorithm(String keyType) { if ("RSA".equalsIgnoreCase(keyType)) { return NativeCrypto.EVP_PKEY_RSA; @@ -363,4 +378,100 @@ public class KeyStore { public int getLastError() { return mError; } + + public boolean addRngEntropy(byte[] data) { + try { + return mBinder.addRngEntropy(data) == NO_ERROR; + } catch (RemoteException e) { + Log.w(TAG, "Cannot connect to keystore", e); + return false; + } + } + + public int generateKey(String alias, KeymasterArguments args, int uid, int flags, + KeyCharacteristics outCharacteristics) { + try { + return mBinder.generateKey(alias, args, uid, flags, outCharacteristics); + } catch (RemoteException e) { + Log.w(TAG, "Cannot connect to keystore", e); + return SYSTEM_ERROR; + } + } + + public int generateKey(String alias, KeymasterArguments args, int flags, + KeyCharacteristics outCharacteristics) { + return generateKey(alias, args, UID_SELF, flags, outCharacteristics); + } + + public int getKeyCharacteristics(String alias, byte[] clientId, byte[] appId, + KeyCharacteristics outCharacteristics) { + try { + return mBinder.getKeyCharacteristics(alias, clientId, appId, outCharacteristics); + } catch (RemoteException e) { + Log.w(TAG, "Cannot connect to keystore", e); + return SYSTEM_ERROR; + } + } + + public int importKey(String alias, KeymasterArguments args, int format, byte[] keyData, + int uid, int flags, KeyCharacteristics outCharacteristics) { + try { + return mBinder.importKey(alias, args, format, keyData, uid, flags, + outCharacteristics); + } catch (RemoteException e) { + Log.w(TAG, "Cannot connect to keystore", e); + return SYSTEM_ERROR; + } + } + + public int importKey(String alias, KeymasterArguments args, int format, byte[] keyData, + int flags, KeyCharacteristics outCharacteristics) { + return importKey(alias, args, format, keyData, UID_SELF, flags, outCharacteristics); + } + + public ExportResult exportKey(String alias, int format, byte[] clientId, byte[] appId) { + try { + return mBinder.exportKey(alias, format, clientId, appId); + } catch (RemoteException e) { + Log.w(TAG, "Cannot connect to keystore", e); + return null; + } + } + + public OperationResult begin(String alias, int purpose, boolean pruneable, + KeymasterArguments args, KeymasterArguments outArgs) { + try { + return mBinder.begin(getToken(), alias, purpose, pruneable, args, outArgs); + } catch (RemoteException e) { + Log.w(TAG, "Cannot connect to keystore", e); + return null; + } + } + + public OperationResult update(IBinder token, KeymasterArguments arguments, byte[] input) { + try { + return mBinder.update(token, arguments, input); + } catch (RemoteException e) { + Log.w(TAG, "Cannot connect to keystore", e); + return null; + } + } + + public OperationResult finish(IBinder token, KeymasterArguments arguments, byte[] signature) { + try { + return mBinder.finish(token, arguments, signature); + } catch (RemoteException e) { + Log.w(TAG, "Cannot connect to keystore", e); + return null; + } + } + + public int abort(IBinder token) { + try { + return mBinder.abort(token); + } catch (RemoteException e) { + Log.w(TAG, "Cannot connect to keystore", e); + return SYSTEM_ERROR; + } + } } -- 2.11.0