diff options
author | Alex Klyubin <klyubin@google.com> | 2015-04-24 18:35:37 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2015-04-24 18:35:37 +0000 |
commit | 9716b081a6174823d8fb49576e62382e953a1c98 (patch) | |
tree | c6578bf847ccc6175d0293e07d6767a7fdab30c7 /keystore | |
parent | a298a96af77f0cb638b4e2be2ab762d0183ee813 (diff) | |
parent | edf5454a14cc53de92eaf1ef15fcfb21d8caccca (diff) | |
download | frameworks_base-9716b081a6174823d8fb49576e62382e953a1c98.zip frameworks_base-9716b081a6174823d8fb49576e62382e953a1c98.tar.gz frameworks_base-9716b081a6174823d8fb49576e62382e953a1c98.tar.bz2 |
am edf5454a: am 3167fb4b: am 71223ebe: Merge "Reset AndroidKeyStore Mac and Cipher state when init fails."
* commit 'edf5454a14cc53de92eaf1ef15fcfb21d8caccca':
Reset AndroidKeyStore Mac and Cipher state when init fails.
Diffstat (limited to 'keystore')
-rw-r--r-- | keystore/java/android/security/KeyStoreCipherSpi.java | 49 | ||||
-rw-r--r-- | keystore/java/android/security/KeyStoreHmacSpi.java | 52 |
2 files changed, 79 insertions, 22 deletions
diff --git a/keystore/java/android/security/KeyStoreCipherSpi.java b/keystore/java/android/security/KeyStoreCipherSpi.java index 37e00b2..1f8d8ec 100644 --- a/keystore/java/android/security/KeyStoreCipherSpi.java +++ b/keystore/java/android/security/KeyStoreCipherSpi.java @@ -152,29 +152,58 @@ public abstract class KeyStoreCipherSpi extends CipherSpi implements KeyStoreCry @Override protected void engineInit(int opmode, Key key, SecureRandom random) throws InvalidKeyException { - init(opmode, key, random); - initAlgorithmSpecificParameters(); - ensureKeystoreOperationInitialized(); + resetAll(); + + boolean success = false; + try { + init(opmode, key, random); + initAlgorithmSpecificParameters(); + ensureKeystoreOperationInitialized(); + success = true; + } finally { + if (!success) { + resetAll(); + } + } } @Override protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { - init(opmode, key, random); - initAlgorithmSpecificParameters(params); - ensureKeystoreOperationInitialized(); + resetAll(); + + boolean success = false; + try { + init(opmode, key, random); + initAlgorithmSpecificParameters(params); + ensureKeystoreOperationInitialized(); + success = true; + } finally { + if (!success) { + resetAll(); + } + } } @Override protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { - init(opmode, key, random); - initAlgorithmSpecificParameters(params); - ensureKeystoreOperationInitialized(); + resetAll(); + + boolean success = false; + try { + init(opmode, key, random); + initAlgorithmSpecificParameters(params); + ensureKeystoreOperationInitialized(); + success = true; + } finally { + if (!success) { + resetAll(); + } + } } private void init(int opmode, Key key, SecureRandom random) throws InvalidKeyException { - resetAll(); if (!(key instanceof KeyStoreSecretKey)) { throw new InvalidKeyException( "Unsupported key: " + ((key != null) ? key.getClass().getName() : "null")); diff --git a/keystore/java/android/security/KeyStoreHmacSpi.java b/keystore/java/android/security/KeyStoreHmacSpi.java index a19bbda..f8b6fef 100644 --- a/keystore/java/android/security/KeyStoreHmacSpi.java +++ b/keystore/java/android/security/KeyStoreHmacSpi.java @@ -69,9 +69,10 @@ public abstract class KeyStoreHmacSpi extends MacSpi implements KeyStoreCryptoOp private final int mKeymasterDigest; private final int mMacSizeBytes; - private String mKeyAliasInKeyStore; + // Fields below are populated by engineInit and should be preserved after engineDoFinal. + private KeyStoreSecretKey mKey; - // The fields below are reset by the engineReset operation. + // Fields below are reset when engineDoFinal succeeds. private KeyStoreCryptoOperationChunkedStreamer mChunkedStreamer; private IBinder mOperationToken; private Long mOperationHandle; @@ -89,28 +90,49 @@ public abstract class KeyStoreHmacSpi extends MacSpi implements KeyStoreCryptoOp @Override protected void engineInit(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException { + resetAll(); + + boolean success = false; + try { + init(key, params); + ensureKeystoreOperationInitialized(); + success = true; + } finally { + if (!success) { + resetAll(); + } + } + } + + private void init(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, + InvalidAlgorithmParameterException { if (key == null) { throw new InvalidKeyException("key == null"); } else if (!(key instanceof KeyStoreSecretKey)) { throw new InvalidKeyException( "Only Android KeyStore secret keys supported. Key: " + key); } + mKey = (KeyStoreSecretKey) key; if (params != null) { throw new InvalidAlgorithmParameterException( "Unsupported algorithm parameters: " + params); } - mKeyAliasInKeyStore = ((KeyStoreSecretKey) key).getAlias(); - if (mKeyAliasInKeyStore == null) { - throw new InvalidKeyException("Key's KeyStore alias not known"); + } + + private void resetAll() { + mKey = null; + IBinder operationToken = mOperationToken; + if (operationToken != null) { + mOperationToken = null; + mKeyStore.abort(operationToken); } - engineReset(); - ensureKeystoreOperationInitialized(); + mOperationHandle = null; + mChunkedStreamer = null; } - @Override - protected void engineReset() { + private void resetWhilePreservingInitState() { IBinder operationToken = mOperationToken; if (operationToken != null) { mOperationToken = null; @@ -120,11 +142,16 @@ public abstract class KeyStoreHmacSpi extends MacSpi implements KeyStoreCryptoOp mChunkedStreamer = null; } + @Override + protected void engineReset() { + resetWhilePreservingInitState(); + } + private void ensureKeystoreOperationInitialized() { if (mChunkedStreamer != null) { return; } - if (mKeyAliasInKeyStore == null) { + if (mKey == null) { throw new IllegalStateException("Not initialized"); } @@ -132,7 +159,8 @@ public abstract class KeyStoreHmacSpi extends MacSpi implements KeyStoreCryptoOp keymasterArgs.addInt(KeymasterDefs.KM_TAG_ALGORITHM, KeymasterDefs.KM_ALGORITHM_HMAC); keymasterArgs.addInt(KeymasterDefs.KM_TAG_DIGEST, mKeymasterDigest); - OperationResult opResult = mKeyStore.begin(mKeyAliasInKeyStore, + OperationResult opResult = mKeyStore.begin( + mKey.getAlias(), KeymasterDefs.KM_PURPOSE_SIGN, true, keymasterArgs, @@ -184,7 +212,7 @@ public abstract class KeyStoreHmacSpi extends MacSpi implements KeyStoreCryptoOp throw KeyStore.getCryptoOperationException(e); } - engineReset(); + resetWhilePreservingInitState(); return result; } |