Skip to content

Instantly share code, notes, and snippets.

@jmarkoff
Last active June 10, 2020 06:42
Show Gist options
  • Save jmarkoff/44f5a9cab1a881c8b0abc787791add08 to your computer and use it in GitHub Desktop.
Save jmarkoff/44f5a9cab1a881c8b0abc787791add08 to your computer and use it in GitHub Desktop.
/*
* Copyright 2020 The Android Open Source Project
*
* 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.
*/
//packgage com.company.app
import android.content.Context;
import android.content.SharedPreferences;
import android.security.keystore.KeyGenParameterSpec;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.security.crypto.MasterKeys;
import androidx.security.crypto.EncryptedSharedPreferences;
import java.io.IOException;
import java.security.GeneralSecurityException;
/**
* Convenient method to test the Android Keystore before using encryption/decryption. A small number
* OEMs have devices with a bad keystore and KeyStore exceptions will occur.
*
* Requires Jetpack Security - https://developer.android.com/jetpack/androidx/releases/security
*
* Bugs:
*
* https://issuetracker.google.com/issues/147480931
* https://issuetracker.google.com/issues/134417365
* https://issuetracker.google.com/issues/150221071
*
*/
public final class TestKeyStore {
/**
* Test the keystore, encryption and decryption on the device. This is useful to find devices
* that have a bad keystore and encryption should not be used. It is up to the developer to
* decide how to handle when a bad keystore is encountered. We recommend that the device be
* trusted less by your app if possible.
*
* @param keyGenParameterSpec The key encryption scheme
* @return true if the keystore can be relied on, false otherwise
*/
public static boolean trustDeviceKeyStore(@NonNull KeyGenParameterSpec keyGenParameterSpec,
@NonNull Context context) {
try {
String keyAlias = MasterKeys.getOrCreate(keyGenParameterSpec);
SharedPreferences sharedPreferences =
EncryptedSharedPreferences.create("test_keystore", keyAlias,
context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("TestKeyStore", "Testing");
editor.commit();
String value = sharedPreferences.getString("TestKeyStore", "Failed");
if (value.equals("Testing")) {
return true;
}
} catch (GeneralSecurityException ex) {
Log.e(TestKeyStore.class.getSimpleName(),
"SecurityException: Could be a keystore issue, check the error for more "
+ "details message: " + ex.getMessage() + ".\n Stacktrace:\n"
+ ex.getStackTrace().toString());
} catch (IOException ex) {
Log.e(TestKeyStore.class.getSimpleName(),
"IOException: Check to make sure you have enough disk space and that the "
+ "file doesn't exist." + ex.getMessage());
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment