Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Keeping Android Secrets Secure with Fingerprint...

josh skeen
February 24, 2017

Keeping Android Secrets Secure with Fingerprint Authentication and the Keystore

Getting cryptography right on Android is challenging. There are code examples without much explanation about what is really going on.
In this talk you will learn the details you need to implement a subset of Android security: user authorization.
We will discuss how to implement robust user authorization using the new Fingerprint API and the Android Keystore,
and we will explore how to correctly use the Keystore to keep your application secrets secure.

demo application source here: https://github.com/bignerdranch/android-securebank

josh skeen

February 24, 2017
Tweet

More Decks by josh skeen

Other Decks in Programming

Transcript

  1. New Feature A user should be allowed to sign in

    using their fingerprint, optionally.
  2. Central Problem What should i save locally if i want

    to implement fingerprint authentication, and how can i do it securely ‽
  3. Getting it Wrong! Storing the password itself in the application

    Shared Preferences <map> <string name="username">johnnydoe</string> <string name="password">pizza2017</string> <boolean name="rememberUsername" value="true" /> </map> /data/data/securebankcorp.mobile.client.android/shared_prefs:
  4. Encrypting the password or token, but using keys and encryption

    constants that are defined in application code: apktool -d release.apk Getting it Wrong!
  5. Proposed Solution •Upon a user authenticating, capture the session token

    •Create a SecretKey and store it in the AndroidKeyStore •Use a Cipher w/ SecretKey to encrypt & decrypt the token •Hook it up to work with the Fingerprint API
  6. Creating A Secret Key Android Keystore Make a (secret) Key

    Save the Key (securely!) KeyGenerator Encrypt the Data Cipher SecretKey
  7. AndroidKeyStore •Can store Public Keys, Private Keys, and Certificates •In

    our case, all we’ll need is a Private Key (symmetric) •Does Fancy stuff under the hood (TEE, SE, etc) •Actually a “KeystoreProvider” •Been called other things in previous versions Android Keystore Save the Key (securely!) Encrypt the Data Cipher SecretKey
  8. Prep the Keystore private static final String ANDROID_KEY_STORE = "AndroidKeyStore";

    keystore = KeyStore.getInstance(ANDROID_KEY_STORE);
 keystore.load(null); KeyStoreHelper.java
  9. Next Up: Creating a Secret Key KeyGenerator keyGenerator = KeyGenerator


    .getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE);
 KeyStoreHelper.java
  10. KeyGenParameterSpec KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(DEFAULT_KEY,
 KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
 .setBlockModes(KeyProperties.BLOCK_MODE_CBC)


    .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7); KeyStoreHelper.java Provides Config for how specifically we want to construct the SecretKey
  11. Obtain a Cipher Instance Cipher cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"


    + KeyProperties.BLOCK_MODE_CBC + "/"
 + KeyProperties.ENCRYPTION_PADDING_PKCS7); CryptoHelper.java
  12. Storing the IV (needed later!) private void initEncryptCipher(Cipher cipher) {


    cipher.init(Cipher.ENCRYPT_MODE, keyStoreHelper.getSecretKey());
 sharedPreferencesHelper.saveIV(cipher.getIV());
 } CryptoHelper.java
  13. Putting It All Together: doFinal() public void encrypt(String authTokenFromServer) {


    Cipher cipher = getCipher();
 initEncryptCipher(cipher);
 byte[] bytes = cipher.doFinal(authTokenFromServer.getBytes());
 sharedPreferencesHelper.saveToken(bytes);
 } CryptoHelper.java
  14. Saving the Encrypted Token SharedPreferencesHelper.java public void saveToken(byte[] encryptedToken) {


    String encoded = Base64.encodeString(encryptedToken, Base64.NO_WRAP);
 sharedPreferences .edit() .putString(SHARED_PREFERENCES_ENCRYPTED_TOKEN, encoded).apply();
 }
  15. Problem 2: Fingerprint Authentication FingerprintManager - system service for managing

    fingerprint reader hardware/software interaction CryptoObject - verifies the fingerprint authentication occurred and enables or revokes a Cipher object in ENCRYPT or DECRYPT mode
  16. Fingerprint Sensor Technology • most are using capacitive touch •

    old tech = optical - sort of dangerous! • new tech = ultrasonic
  17. Adding USE_FINGERPRINT Permission requestPermissions(new String[]{Manifest.permission.USE_FINGERPRINT}, REQUEST_USE_FINGERPRINT);
 @Override
 public void onRequestPermissionsResult(int

    requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
 if (requestCode == REQUEST_USE_FINGERPRINT && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
 performFingerprintAuthentication();
 }
 }
 } LoginActivity.java
  18. Fingerprint Auth: Check if Available private void performFingerprintAuthentication() {
 if

    (fingerprintManager.isHardwareDetected() && fingerprintManager.hasEnrolledFingerprints()) {
 //ready to rock. warm up the sensor
 } else {
 //no dice. show an error message, etc
 Timber.e("fingerprint not available");
 }
 } LoginActivity.java
  19. KGPS: Enabling User Auth Required KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec


    .Builder(DEFAULT_KEY, KeyProperties.PURPOSE_ENCRYPT |
 KeyProperties.PURPOSE_DECRYPT)
 .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
 .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
 .setUserAuthenticationRequired(true);
 throws an android.security.KeyStoreException if used w/ cipher and user’s not “authed" KeyStoreHelper.java
  20. Fingerprint Authentication Callbacks private void performFingerprintAuthentication() {
 Cipher cipher =

    cryptoHelper.getCipher();
 cryptoHelper.initDecryptCipher(cipher);
 FingerprintManager.CryptoObject cryptoObject = new FingerprintManager.CryptoObject(cipher);
 fingerprintManager.authenticate(cryptoObject, null, 0, new FingerprintManager.AuthenticationCallback() {
 @Override
 public void onAuthenticationFailed() {
 super.onAuthenticationFailed();
 }
 @Override
 public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
 super.onAuthenticationSucceeded(result);
 //we can now decrypt using our authenticated cipher!
 }
 @Override
 public void onAuthenticationError(int errorCode, CharSequence errString) {
 super.onAuthenticationError(errorCode, errString);
 }
 }, null);
 } LoginActivity.java
  21. Decrypt, using the Authed Cipher @Override
 public void onAuthenticationSucceeded(AuthenticationResult result)

    {
 super.onAuthenticationSucceeded(result); String decryptedToken = cryptoHelper.decrypt(result.getCryptoObject());
 //we can now decrypt using our authenticated cipher
 } LoginActivity.java
  22. Note about Testing •robolectric “shadow object” doesn't work with keystore

    currently •real integration tests (espresso) may be best option if its important
  23. Final Thoughts •Good foundation, though with security there’s never a

    “one size fits all” solution •Additional measures available - Proguard, Dexguard, SafetyNet ( is it “security theater?”) •2FA/MFA •Big Nerd Ranch Security Courses coming soon! (Android & Web) Q3ish 2017
  24. Resources •code example: https://github.com/bignerdranch/securebank •SafetyNet API: https://developer.android.com/training/safetynet/index.html •Big Nerd Ranch

    Security Course: http://www.bignerdranch.com •KeyGenerator : https://developer.android.com/reference/javax/crypto/ KeyGenerator.html •AES explained with stick figures: www.moserware.com/2009/09/stick-figure-guide-to- advanced.html •IDA https://www.hex-rays.com/products/ida/ •FRIDA https://www.frida.re/docs/android/