From 2ed9b9b276c4df477cfd6c749f9d4deeed209a0d Mon Sep 17 00:00:00 2001 From: Janis Danisevskis Date: Tue, 20 Oct 2020 09:30:51 -0700 Subject: [PATCH] Keystore 2.0 SPI: Make Recoverable keystore tolerate the Keystore 2.0 SPI This patch adds support for using the Legacy Keystore provider when the Keystore 2.0 provider is installed. This is the first step towards the transition to Keystore 2.0. Installation of the new provider can be triggered by setting the platform property ro.android.security.keystore2.enable=true. Bug: 171305545 Test: None Change-Id: I3825f40558b4c25cb64caa25c0029cea76333c44 --- .../KeyStoreProxyImpl.java | 23 +++++++++++++++---- .../PlatformKeyManager.java | 4 +--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/services/core/java/com/android/server/locksettings/recoverablekeystore/KeyStoreProxyImpl.java b/services/core/java/com/android/server/locksettings/recoverablekeystore/KeyStoreProxyImpl.java index 285e722886c28..9857fb637b596 100644 --- a/services/core/java/com/android/server/locksettings/recoverablekeystore/KeyStoreProxyImpl.java +++ b/services/core/java/com/android/server/locksettings/recoverablekeystore/KeyStoreProxyImpl.java @@ -16,23 +16,38 @@ package com.android.server.locksettings.recoverablekeystore; +import android.security.keystore2.AndroidKeyStoreProvider; + import java.io.IOException; -import java.security.cert.CertificateException; import java.security.Key; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; -import java.security.NoSuchAlgorithmException; import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; /** * Implementation of {@link KeyStoreProxy} that delegates all method calls to the {@link KeyStore}. */ public class KeyStoreProxyImpl implements KeyStoreProxy { - private static final String ANDROID_KEY_STORE_PROVIDER = "AndroidKeyStore"; private final KeyStore mKeyStore; + /** + * TODO This function redirects keystore access to the legacy keystore during a transitional + * phase during which not all calling code has been adjusted to use Keystore 2.0. + * This can be reverted to a constant of "AndroidKeyStore" when b/171305684 is complete. + * The specific bug for this component is b/171305545. + */ + static String androidKeystoreProviderName() { + if (AndroidKeyStoreProvider.isInstalled()) { + return "AndroidKeyStoreLegacy"; + } else { + return "AndroidKeyStore"; + } + + } + /** * A new instance, delegating to {@code keyStore}. */ @@ -69,7 +84,7 @@ public class KeyStoreProxyImpl implements KeyStoreProxy { * @throws KeyStoreException if there was a problem getting or initializing the key store. */ public static KeyStore getAndLoadAndroidKeyStore() throws KeyStoreException { - KeyStore keyStore = KeyStore.getInstance(ANDROID_KEY_STORE_PROVIDER); + KeyStore keyStore = KeyStore.getInstance(androidKeystoreProviderName()); try { keyStore.load(/*param=*/ null); } catch (CertificateException | IOException | NoSuchAlgorithmException e) { diff --git a/services/core/java/com/android/server/locksettings/recoverablekeystore/PlatformKeyManager.java b/services/core/java/com/android/server/locksettings/recoverablekeystore/PlatformKeyManager.java index 0761cde825b66..569b7098bb6cd 100644 --- a/services/core/java/com/android/server/locksettings/recoverablekeystore/PlatformKeyManager.java +++ b/services/core/java/com/android/server/locksettings/recoverablekeystore/PlatformKeyManager.java @@ -86,8 +86,6 @@ public class PlatformKeyManager { private final KeyStoreProxy mKeyStore; private final RecoverableKeyStoreDb mDatabase; - private static final String ANDROID_KEY_STORE_PROVIDER = "AndroidKeyStore"; - /** * A new instance operating on behalf of {@code userId}, storing its prefs in the location * defined by {@code context}. @@ -486,7 +484,7 @@ public class PlatformKeyManager { * @throws KeyStoreException if there was a problem getting or initializing the key store. */ private static KeyStore getAndLoadAndroidKeyStore() throws KeyStoreException { - KeyStore keyStore = KeyStore.getInstance(ANDROID_KEY_STORE_PROVIDER); + KeyStore keyStore = KeyStore.getInstance(KeyStoreProxyImpl.androidKeystoreProviderName()); try { keyStore.load(/*param=*/ null); } catch (CertificateException | IOException | NoSuchAlgorithmException e) {