-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OAEP support enabled only on API 23+ devices with associated testing …
…changes.
- Loading branch information
Adam Newman
committed
Sep 22, 2016
1 parent
88ec791
commit 8bc37ad
Showing
12 changed files
with
367 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...c/androidTest/java/com/bottlerocketstudios/vault/keys/storage/TestVaultUpgrade22To23.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (c) 2016. Bottle Rocket LLC | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.bottlerocketstudios.vault.keys.storage; | ||
|
||
import android.os.Build; | ||
import android.test.AndroidTestCase; | ||
import android.util.Log; | ||
|
||
import com.bottlerocketstudios.vault.EncryptionConstants; | ||
import com.bottlerocketstudios.vault.keys.generator.Aes256RandomKeyFactory; | ||
import com.bottlerocketstudios.vault.salt.PrngSaltGenerator; | ||
|
||
import java.security.GeneralSecurityException; | ||
import java.util.Arrays; | ||
|
||
import javax.crypto.SecretKey; | ||
|
||
/** | ||
* Test transition to version 18 from a pre-18 device if it receives an OS upgrade. | ||
*/ | ||
public class TestVaultUpgrade22To23 extends AndroidTestCase { | ||
private static final String TAG = TestVaultUpgrade22To23.class.getSimpleName(); | ||
|
||
private static final String KEY_FILE_NAME = "upgrade22to23KeyFile"; | ||
private static final String KEY_ALIAS_1 = "upgrade22to23KeyAlias"; | ||
private static final int KEY_INDEX_1 = 1232734; | ||
private static final String PRESHARED_SECRET_1 = "a;sdlfkja;asdfasds21222e;l2ihjl9jl9dj9"; | ||
|
||
public void testUpgrade() { | ||
assertTrue("This test will not pass below API 23", Build.VERSION.SDK_INT >= Build.VERSION_CODES.M); | ||
try { | ||
SecretKey originalKey = Aes256RandomKeyFactory.createKey(); | ||
KeyStorage keyStorageOld = getKeyStorage(Build.VERSION_CODES.LOLLIPOP_MR1); | ||
assertEquals("Incorrect KeyStorageType", KeyStorageType.ANDROID_KEYSTORE, keyStorageOld.getKeyStorageType()); | ||
keyStorageOld.clearKey(getContext()); | ||
keyStorageOld.saveKey(getContext(), originalKey); | ||
|
||
SecretKey originalReadKey = keyStorageOld.loadKey(getContext()); | ||
assertNotNull("Key was null after creation and read from old storage.", originalReadKey); | ||
assertTrue("Keys were not identical after creation and read from old storage", Arrays.equals(originalKey.getEncoded(), originalReadKey.getEncoded())); | ||
|
||
KeyStorage keyStorageNew = getKeyStorage(Build.VERSION_CODES.M); | ||
assertEquals("Incorrect KeyStorageType", KeyStorageType.ANDROID_KEYSTORE, keyStorageNew.getKeyStorageType()); | ||
SecretKey upgradedKey = keyStorageNew.loadKey(getContext()); | ||
assertNotNull("Key was null after upgrade.", upgradedKey); | ||
assertTrue("Keys were not identical after upgrade", Arrays.equals(originalKey.getEncoded(), upgradedKey.getEncoded())); | ||
|
||
KeyStorage keyStorageRead = getKeyStorage(Build.VERSION_CODES.M); | ||
assertEquals("Incorrect KeyStorageType", KeyStorageType.ANDROID_KEYSTORE, keyStorageRead.getKeyStorageType()); | ||
SecretKey upgradedReadKey = keyStorageRead.loadKey(getContext()); | ||
assertNotNull("Key was null after upgrade and read from storage.", upgradedReadKey); | ||
assertTrue("Keys were not identical after upgrade and read from storage", Arrays.equals(originalKey.getEncoded(), upgradedReadKey.getEncoded())); | ||
|
||
} catch (GeneralSecurityException e) { | ||
Log.e(TAG, "Caught java.security.GeneralSecurityException", e); | ||
assertTrue("Exception when creating keystores", false); | ||
} | ||
|
||
} | ||
|
||
private KeyStorage getKeyStorage(int sdkInt) throws GeneralSecurityException { | ||
return CompatSharedPrefKeyStorageFactory.createKeyStorage( | ||
getContext(), | ||
sdkInt, | ||
KEY_FILE_NAME, | ||
KEY_ALIAS_1, | ||
KEY_INDEX_1, | ||
EncryptionConstants.AES_CIPHER, | ||
PRESHARED_SECRET_1, | ||
new PrngSaltGenerator()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
.../main/java/com/bottlerocketstudios/vault/keys/storage/hardware/AndroidKeystoreTester.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright (c) 2016. Bottle Rocket LLC | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.bottlerocketstudios.vault.keys.storage.hardware; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.os.Build; | ||
import android.util.Log; | ||
|
||
import com.bottlerocketstudios.vault.keys.storage.AndroidKeystoreTestState; | ||
import com.bottlerocketstudios.vault.keys.wrapper.AbstractAndroidKeystoreSecretKeyWrapper; | ||
|
||
import java.security.GeneralSecurityException; | ||
|
||
/** | ||
* Created on 9/21/16. | ||
*/ | ||
public abstract class AndroidKeystoreTester { | ||
private static final String TAG = AndroidKeystoreTester.class.getSimpleName(); | ||
|
||
protected static final String PREF_COMPAT_FACTORY_ANDROID_KEYSTORE_TEST_STATE_ROOT = "androidKeystoreTestState."; | ||
|
||
private final Context mContext; | ||
private final String mKeystoreAlias; | ||
private final int mCurrentSdkInt; | ||
private final String mTestKeystoreAlias; | ||
|
||
public AndroidKeystoreTester(Context context, String keystoreAlias, int currentSdkInt) { | ||
mContext = context; | ||
mKeystoreAlias = keystoreAlias; | ||
mTestKeystoreAlias = keystoreAlias + "___TEST___"; | ||
mCurrentSdkInt = currentSdkInt; | ||
} | ||
|
||
public boolean canUseAndroidKeystore(SharedPreferences sharedPreferences) { | ||
AndroidKeystoreTestState androidKeystoreTestState = readAndroidKeystoreTestState(sharedPreferences); | ||
if (AndroidKeystoreTestState.UNTESTED.equals(androidKeystoreTestState)) { | ||
androidKeystoreTestState = performAndroidKeystoreTest(); | ||
writeAndroidKeystoreTestState(sharedPreferences, androidKeystoreTestState); | ||
} | ||
return AndroidKeystoreTestState.PASS.equals(androidKeystoreTestState); | ||
} | ||
|
||
private AndroidKeystoreTestState performAndroidKeystoreTest() { | ||
AndroidKeystoreTestState androidKeystoreTestState = AndroidKeystoreTestState.FAIL; | ||
if (mCurrentSdkInt >= Build.VERSION_CODES.JELLY_BEAN_MR2) { | ||
AbstractAndroidKeystoreSecretKeyWrapper androidKeystoreSecretKeyWrapper = null; | ||
try { | ||
androidKeystoreSecretKeyWrapper = createKeystoreSecretKeyWrapper(mContext, mTestKeystoreAlias); | ||
androidKeystoreTestState = androidKeystoreSecretKeyWrapper.testKey() ? AndroidKeystoreTestState.PASS : AndroidKeystoreTestState.FAIL; | ||
} catch (Throwable t) { | ||
Log.e(TAG, "Caught an exception while creating the AndroidKeystoreSecretKeyWrapper", t); | ||
androidKeystoreTestState = AndroidKeystoreTestState.FAIL; | ||
} finally { | ||
if (androidKeystoreSecretKeyWrapper != null) { | ||
try { | ||
androidKeystoreSecretKeyWrapper.clearKey(mContext); | ||
} catch (Throwable t) { | ||
Log.e(TAG, "Caught an exception while cleaning up the AndroidKeystoreSecretKeyWrapper", t); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (AndroidKeystoreTestState.FAIL.equals(androidKeystoreTestState)) { | ||
Log.w(TAG, "This device failed the AndroidKeystoreSecretKeyWrapper test."); | ||
} | ||
return androidKeystoreTestState; | ||
} | ||
|
||
private void writeAndroidKeystoreTestState(SharedPreferences sharedPreferences, AndroidKeystoreTestState androidKeystoreTestState) { | ||
sharedPreferences.edit() | ||
.putString(getAndroidKeystoreTestStateSharedPreferenceKey(mKeystoreAlias), androidKeystoreTestState.toString()) | ||
.apply(); | ||
} | ||
|
||
protected AndroidKeystoreTestState readAndroidKeystoreTestState(SharedPreferences sharedPreferences) { | ||
String prefValue = sharedPreferences.getString(getAndroidKeystoreTestStateSharedPreferenceKey(mKeystoreAlias), AndroidKeystoreTestState.UNTESTED.toString()); | ||
return parseAndroidKeystoreTestState(prefValue); | ||
} | ||
|
||
private AndroidKeystoreTestState parseAndroidKeystoreTestState(String prefValue) { | ||
AndroidKeystoreTestState androidKeystoreTestState; | ||
try { | ||
androidKeystoreTestState = AndroidKeystoreTestState.valueOf(prefValue); | ||
} catch (IllegalArgumentException e) { | ||
Log.e(TAG, "Failed to parse previous test state"); | ||
androidKeystoreTestState = AndroidKeystoreTestState.UNTESTED; | ||
} | ||
return androidKeystoreTestState; | ||
} | ||
|
||
protected abstract String getAndroidKeystoreTestStateSharedPreferenceKey(String keystoreAlias); | ||
|
||
protected abstract AbstractAndroidKeystoreSecretKeyWrapper createKeystoreSecretKeyWrapper(Context context, String testKeystoreAlias) throws GeneralSecurityException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...java/com/bottlerocketstudios/vault/keys/storage/hardware/LegacyAndroidKeystoreTester.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (c) 2016. Bottle Rocket LLC | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.bottlerocketstudios.vault.keys.storage.hardware; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
|
||
import com.bottlerocketstudios.vault.keys.storage.AndroidKeystoreTestState; | ||
import com.bottlerocketstudios.vault.keys.wrapper.AbstractAndroidKeystoreSecretKeyWrapper; | ||
|
||
import java.security.GeneralSecurityException; | ||
|
||
/** | ||
* Created on 9/21/16. | ||
*/ | ||
public class LegacyAndroidKeystoreTester extends AndroidKeystoreTester { | ||
|
||
public LegacyAndroidKeystoreTester(Context context, String keystoreAlias, int currentSdkInt) { | ||
super(context, keystoreAlias, currentSdkInt); | ||
} | ||
|
||
@Override | ||
protected String getAndroidKeystoreTestStateSharedPreferenceKey(String keystoreAlias) { | ||
return PREF_COMPAT_FACTORY_ANDROID_KEYSTORE_TEST_STATE_ROOT + keystoreAlias; | ||
} | ||
|
||
@Override | ||
protected AbstractAndroidKeystoreSecretKeyWrapper createKeystoreSecretKeyWrapper(Context context, String testKeystoreAlias) throws GeneralSecurityException { | ||
throw new UnsupportedOperationException("Tests should not be performed. This class is provided to read legacy test status."); | ||
} | ||
|
||
public static boolean hasAlreadyPassedTest(Context context, String keystoreAlias, int currentSdkInt, SharedPreferences sharedPreferences) { | ||
LegacyAndroidKeystoreTester tester = new LegacyAndroidKeystoreTester(context, keystoreAlias, currentSdkInt); | ||
return AndroidKeystoreTestState.PASS.equals(tester.readAndroidKeystoreTestState(sharedPreferences)); | ||
} | ||
} |
Oops, something went wrong.