Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.locksettings.recoverablekeystore; |
| 18 | |
Robert Berry | d9f11a9 | 2018-02-26 16:37:06 +0000 | [diff] [blame] | 19 | import static android.security.keystore.recovery.RecoveryController.ERROR_BAD_CERTIFICATE_FORMAT; |
| 20 | import static android.security.keystore.recovery.RecoveryController.ERROR_DECRYPTION_FAILED; |
| 21 | import static android.security.keystore.recovery.RecoveryController.ERROR_INSECURE_USER; |
Bo Zhu | 2c8e538 | 2018-02-26 15:54:25 -0800 | [diff] [blame] | 22 | import static android.security.keystore.recovery.RecoveryController.ERROR_INVALID_KEY_FORMAT; |
Bo Zhu | 7f414d9 | 2018-02-28 09:28:19 -0800 | [diff] [blame] | 23 | import static android.security.keystore.recovery.RecoveryController.ERROR_INVALID_CERTIFICATE; |
Robert Berry | d9f11a9 | 2018-02-26 16:37:06 +0000 | [diff] [blame] | 24 | import static android.security.keystore.recovery.RecoveryController.ERROR_NO_SNAPSHOT_PENDING; |
| 25 | import static android.security.keystore.recovery.RecoveryController.ERROR_SERVICE_INTERNAL_ERROR; |
| 26 | import static android.security.keystore.recovery.RecoveryController.ERROR_SESSION_EXPIRED; |
Robert Berry | 97e5558 | 2018-01-05 12:43:13 +0000 | [diff] [blame] | 27 | |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 28 | import android.Manifest; |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 29 | import android.annotation.NonNull; |
| 30 | import android.annotation.Nullable; |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 31 | import android.app.PendingIntent; |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 32 | import android.content.Context; |
| 33 | import android.os.Binder; |
Robert Berry | 4a5c87d | 2018-03-19 18:00:46 +0000 | [diff] [blame] | 34 | import android.os.Process; |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 35 | import android.os.RemoteException; |
| 36 | import android.os.ServiceSpecificException; |
| 37 | import android.os.UserHandle; |
Dmitry Dementyev | 0916e7c | 2018-01-23 13:02:08 -0800 | [diff] [blame] | 38 | import android.security.keystore.recovery.KeyChainProtectionParams; |
| 39 | import android.security.keystore.recovery.KeyChainSnapshot; |
Bo Zhu | 7c1972f | 2018-02-22 21:43:52 -0800 | [diff] [blame] | 40 | import android.security.keystore.recovery.RecoveryCertPath; |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 41 | import android.security.keystore.recovery.RecoveryController; |
Bo Zhu | b31ab67 | 2018-03-20 22:44:18 -0700 | [diff] [blame] | 42 | import android.security.keystore.recovery.TrustedRootCertificates; |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 43 | import android.security.keystore.recovery.WrappedApplicationKey; |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 44 | import android.security.KeyStore; |
Robert Berry | 4a5c87d | 2018-03-19 18:00:46 +0000 | [diff] [blame] | 45 | import android.util.ArrayMap; |
Robert Berry | 4a534ec | 2017-12-21 15:44:02 +0000 | [diff] [blame] | 46 | import android.util.Log; |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 47 | |
| 48 | import com.android.internal.annotations.VisibleForTesting; |
Bo Zhu | 31a40c0 | 2018-01-24 17:40:29 -0800 | [diff] [blame] | 49 | import com.android.internal.util.HexDump; |
Dmitry Dementyev | 1e6a9dc | 2018-03-21 13:52:00 -0700 | [diff] [blame] | 50 | import com.android.internal.util.Preconditions; |
Bo Zhu | 7ce4ea5 | 2018-02-27 23:52:19 -0800 | [diff] [blame] | 51 | import com.android.server.locksettings.recoverablekeystore.certificate.CertUtils; |
Bo Zhu | 7f414d9 | 2018-02-28 09:28:19 -0800 | [diff] [blame] | 52 | import com.android.server.locksettings.recoverablekeystore.certificate.SigXml; |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 53 | import com.android.server.locksettings.recoverablekeystore.storage.ApplicationKeyStorage; |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 54 | import com.android.server.locksettings.recoverablekeystore.certificate.CertParsingException; |
| 55 | import com.android.server.locksettings.recoverablekeystore.certificate.CertValidationException; |
| 56 | import com.android.server.locksettings.recoverablekeystore.certificate.CertXml; |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 57 | import com.android.server.locksettings.recoverablekeystore.storage.RecoverableKeyStoreDb; |
| 58 | import com.android.server.locksettings.recoverablekeystore.storage.RecoverySessionStorage; |
Robert Berry | bd086f1 | 2017-12-27 13:29:39 +0000 | [diff] [blame] | 59 | import com.android.server.locksettings.recoverablekeystore.storage.RecoverySnapshotStorage; |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 60 | |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 61 | import java.security.InvalidKeyException; |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 62 | import java.security.KeyFactory; |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 63 | import java.security.KeyStoreException; |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 64 | import java.security.NoSuchAlgorithmException; |
| 65 | import java.security.PublicKey; |
Bo Zhu | 8d6861e | 2018-03-21 20:45:09 -0700 | [diff] [blame] | 66 | import java.security.SecureRandom; |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 67 | import java.security.UnrecoverableKeyException; |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 68 | import java.security.cert.CertPath; |
| 69 | import java.security.cert.CertificateEncodingException; |
Bo Zhu | 7c1972f | 2018-02-22 21:43:52 -0800 | [diff] [blame] | 70 | import java.security.cert.CertificateException; |
Bo Zhu | b31ab67 | 2018-03-20 22:44:18 -0700 | [diff] [blame] | 71 | import java.security.cert.X509Certificate; |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 72 | import java.security.spec.InvalidKeySpecException; |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 73 | import java.security.spec.X509EncodedKeySpec; |
Bo Zhu | def7ffd | 2018-01-05 14:50:52 -0800 | [diff] [blame] | 74 | import java.util.Arrays; |
Robert Berry | bd4c43c | 2017-12-22 11:35:14 +0000 | [diff] [blame] | 75 | import java.util.HashMap; |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 76 | import java.util.List; |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 77 | import java.util.Locale; |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 78 | import java.util.Map; |
Robert Berry | 4a534ec | 2017-12-21 15:44:02 +0000 | [diff] [blame] | 79 | import java.util.concurrent.ExecutorService; |
| 80 | import java.util.concurrent.Executors; |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 81 | |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 82 | import javax.crypto.AEADBadTagException; |
| 83 | |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 84 | /** |
Robert Berry | 74928a1 | 2018-01-18 17:49:07 +0000 | [diff] [blame] | 85 | * Class with {@link RecoveryController} API implementation and internal methods to interact |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 86 | * with {@code LockSettingsService}. |
| 87 | * |
| 88 | * @hide |
| 89 | */ |
| 90 | public class RecoverableKeyStoreManager { |
Robert Berry | 4a534ec | 2017-12-21 15:44:02 +0000 | [diff] [blame] | 91 | private static final String TAG = "RecoverableKeyStoreMgr"; |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 92 | |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 93 | private static RecoverableKeyStoreManager mInstance; |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 94 | |
| 95 | private final Context mContext; |
| 96 | private final RecoverableKeyStoreDb mDatabase; |
| 97 | private final RecoverySessionStorage mRecoverySessionStorage; |
Robert Berry | 4a534ec | 2017-12-21 15:44:02 +0000 | [diff] [blame] | 98 | private final ExecutorService mExecutorService; |
Robert Berry | 9104404 | 2017-12-27 12:05:58 +0000 | [diff] [blame] | 99 | private final RecoverySnapshotListenersStorage mListenersStorage; |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 100 | private final RecoverableKeyGenerator mRecoverableKeyGenerator; |
Robert Berry | bd086f1 | 2017-12-27 13:29:39 +0000 | [diff] [blame] | 101 | private final RecoverySnapshotStorage mSnapshotStorage; |
Bo Zhu | 3462c83 | 2018-01-04 22:42:36 -0800 | [diff] [blame] | 102 | private final PlatformKeyManager mPlatformKeyManager; |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 103 | private final KeyStore mKeyStore; |
| 104 | private final ApplicationKeyStorage mApplicationKeyStorage; |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * Returns a new or existing instance. |
| 108 | * |
| 109 | * @hide |
| 110 | */ |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 111 | public static synchronized RecoverableKeyStoreManager |
| 112 | getInstance(Context context, KeyStore keystore) { |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 113 | if (mInstance == null) { |
Bo Zhu | 3462c83 | 2018-01-04 22:42:36 -0800 | [diff] [blame] | 114 | RecoverableKeyStoreDb db = RecoverableKeyStoreDb.newInstance(context); |
| 115 | PlatformKeyManager platformKeyManager; |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 116 | ApplicationKeyStorage applicationKeyStorage; |
Bo Zhu | 3462c83 | 2018-01-04 22:42:36 -0800 | [diff] [blame] | 117 | try { |
| 118 | platformKeyManager = PlatformKeyManager.getInstance(context, db); |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 119 | applicationKeyStorage = ApplicationKeyStorage.getInstance(keystore); |
Bo Zhu | 3462c83 | 2018-01-04 22:42:36 -0800 | [diff] [blame] | 120 | } catch (NoSuchAlgorithmException e) { |
| 121 | // Impossible: all algorithms must be supported by AOSP |
| 122 | throw new RuntimeException(e); |
| 123 | } catch (KeyStoreException e) { |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 124 | throw new ServiceSpecificException(ERROR_SERVICE_INTERNAL_ERROR, e.getMessage()); |
Bo Zhu | 3462c83 | 2018-01-04 22:42:36 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 127 | mInstance = new RecoverableKeyStoreManager( |
Bo Zhu | 3462c83 | 2018-01-04 22:42:36 -0800 | [diff] [blame] | 128 | context.getApplicationContext(), |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 129 | keystore, |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 130 | db, |
Robert Berry | 4a534ec | 2017-12-21 15:44:02 +0000 | [diff] [blame] | 131 | new RecoverySessionStorage(), |
Dmitry Dementyev | 3b17c63 | 2017-12-21 17:30:48 -0800 | [diff] [blame] | 132 | Executors.newSingleThreadExecutor(), |
Robert Berry | 9104404 | 2017-12-27 12:05:58 +0000 | [diff] [blame] | 133 | new RecoverySnapshotStorage(), |
Bo Zhu | 3462c83 | 2018-01-04 22:42:36 -0800 | [diff] [blame] | 134 | new RecoverySnapshotListenersStorage(), |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 135 | platformKeyManager, |
| 136 | applicationKeyStorage); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 137 | } |
| 138 | return mInstance; |
| 139 | } |
| 140 | |
| 141 | @VisibleForTesting |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 142 | RecoverableKeyStoreManager( |
| 143 | Context context, |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 144 | KeyStore keystore, |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 145 | RecoverableKeyStoreDb recoverableKeyStoreDb, |
Robert Berry | 4a534ec | 2017-12-21 15:44:02 +0000 | [diff] [blame] | 146 | RecoverySessionStorage recoverySessionStorage, |
Dmitry Dementyev | 3b17c63 | 2017-12-21 17:30:48 -0800 | [diff] [blame] | 147 | ExecutorService executorService, |
Robert Berry | 9104404 | 2017-12-27 12:05:58 +0000 | [diff] [blame] | 148 | RecoverySnapshotStorage snapshotStorage, |
Bo Zhu | 3462c83 | 2018-01-04 22:42:36 -0800 | [diff] [blame] | 149 | RecoverySnapshotListenersStorage listenersStorage, |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 150 | PlatformKeyManager platformKeyManager, |
| 151 | ApplicationKeyStorage applicationKeyStorage) { |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 152 | mContext = context; |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 153 | mKeyStore = keystore; |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 154 | mDatabase = recoverableKeyStoreDb; |
| 155 | mRecoverySessionStorage = recoverySessionStorage; |
Robert Berry | 4a534ec | 2017-12-21 15:44:02 +0000 | [diff] [blame] | 156 | mExecutorService = executorService; |
Dmitry Dementyev | 3b17c63 | 2017-12-21 17:30:48 -0800 | [diff] [blame] | 157 | mListenersStorage = listenersStorage; |
Robert Berry | bd086f1 | 2017-12-27 13:29:39 +0000 | [diff] [blame] | 158 | mSnapshotStorage = snapshotStorage; |
Bo Zhu | 3462c83 | 2018-01-04 22:42:36 -0800 | [diff] [blame] | 159 | mPlatformKeyManager = platformKeyManager; |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 160 | mApplicationKeyStorage = applicationKeyStorage; |
Bo Zhu | 3462c83 | 2018-01-04 22:42:36 -0800 | [diff] [blame] | 161 | |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 162 | try { |
| 163 | mRecoverableKeyGenerator = RecoverableKeyGenerator.newInstance(mDatabase); |
| 164 | } catch (NoSuchAlgorithmException e) { |
Robert Berry | 97e5558 | 2018-01-05 12:43:13 +0000 | [diff] [blame] | 165 | Log.wtf(TAG, "AES keygen algorithm not available. AOSP must support this.", e); |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 166 | throw new ServiceSpecificException(ERROR_SERVICE_INTERNAL_ERROR, e.getMessage()); |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 167 | } |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Bo Zhu | 7f414d9 | 2018-02-28 09:28:19 -0800 | [diff] [blame] | 170 | /** |
| 171 | * @deprecated Use {@link #initRecoveryServiceWithSigFile(String, byte[], byte[])} instead. |
| 172 | */ |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 173 | public void initRecoveryService( |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 174 | @NonNull String rootCertificateAlias, @NonNull byte[] recoveryServiceCertFile) |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 175 | throws RemoteException { |
| 176 | checkRecoverKeyStorePermission(); |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 177 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 40dadb0 | 2018-01-10 18:03:37 -0800 | [diff] [blame] | 178 | int uid = Binder.getCallingUid(); |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 179 | rootCertificateAlias = replaceEmptyValueWithSecureDefault(rootCertificateAlias); |
| 180 | |
| 181 | // Always set active alias to the argument of the last call to initRecoveryService method, |
| 182 | // even if cert file is incorrect. |
| 183 | String activeRootAlias = mDatabase.getActiveRootOfTrust(userId, uid); |
| 184 | if (activeRootAlias == null) { |
| 185 | Log.d(TAG, "Root of trust for recovery agent + " + uid |
| 186 | + " is assigned for the first time to " + rootCertificateAlias); |
| 187 | mDatabase.setActiveRootOfTrust(userId, uid, rootCertificateAlias); |
| 188 | } else if (!activeRootAlias.equals(rootCertificateAlias)) { |
| 189 | Log.i(TAG, "Root of trust for recovery agent " + uid + " is changed to " |
| 190 | + rootCertificateAlias + " from " + activeRootAlias); |
| 191 | mDatabase.setActiveRootOfTrust(userId, uid, rootCertificateAlias); |
| 192 | } |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 193 | |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 194 | CertXml certXml; |
| 195 | try { |
| 196 | certXml = CertXml.parse(recoveryServiceCertFile); |
| 197 | } catch (CertParsingException e) { |
| 198 | // TODO: Do not use raw key bytes anymore once the other components are updated |
Bo Zhu | ed00d25 | 2018-02-14 11:50:57 -0800 | [diff] [blame] | 199 | Log.d(TAG, "Failed to parse the input as a cert file: " + HexDump.toHexString( |
| 200 | recoveryServiceCertFile)); |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 201 | PublicKey publicKey = parseEcPublicKey(recoveryServiceCertFile); |
| 202 | if (mDatabase.setRecoveryServicePublicKey(userId, uid, publicKey) > 0) { |
| 203 | mDatabase.setShouldCreateSnapshot(userId, uid, true); |
| 204 | } |
Bo Zhu | ed00d25 | 2018-02-14 11:50:57 -0800 | [diff] [blame] | 205 | Log.d(TAG, "Successfully set the input as the raw public key"); |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 206 | return; |
| 207 | } |
| 208 | |
| 209 | // Check serial number |
| 210 | long newSerial = certXml.getSerial(); |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 211 | Long oldSerial = mDatabase.getRecoveryServiceCertSerial(userId, uid, rootCertificateAlias); |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 212 | if (oldSerial != null && oldSerial >= newSerial) { |
| 213 | if (oldSerial == newSerial) { |
| 214 | Log.i(TAG, "The cert file serial number is the same, so skip updating."); |
| 215 | } else { |
| 216 | Log.e(TAG, "The cert file serial number is older than the one in database."); |
| 217 | } |
| 218 | return; |
| 219 | } |
| 220 | Log.i(TAG, "Updating the certificate with the new serial number " + newSerial); |
| 221 | |
Bo Zhu | b31ab67 | 2018-03-20 22:44:18 -0700 | [diff] [blame] | 222 | // Randomly choose and validate an endpoint certificate from the list |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 223 | CertPath certPath; |
Bo Zhu | b31ab67 | 2018-03-20 22:44:18 -0700 | [diff] [blame] | 224 | X509Certificate rootCert = getRootCertificate(rootCertificateAlias); |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 225 | try { |
| 226 | Log.d(TAG, "Getting and validating a random endpoint certificate"); |
Bo Zhu | b31ab67 | 2018-03-20 22:44:18 -0700 | [diff] [blame] | 227 | certPath = certXml.getRandomEndpointCert(rootCert); |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 228 | } catch (CertValidationException e) { |
| 229 | Log.e(TAG, "Invalid endpoint cert", e); |
| 230 | throw new ServiceSpecificException( |
Bo Zhu | 7f414d9 | 2018-02-28 09:28:19 -0800 | [diff] [blame] | 231 | ERROR_INVALID_CERTIFICATE, "Failed to validate certificate."); |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 232 | } |
Bo Zhu | b31ab67 | 2018-03-20 22:44:18 -0700 | [diff] [blame] | 233 | |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 234 | boolean wasInitialized = mDatabase.getRecoveryServiceCertPath(userId, uid, |
| 235 | rootCertificateAlias) != null; |
Robert Berry | e8edf97 | 2018-03-27 11:45:11 +0100 | [diff] [blame] | 236 | |
Bo Zhu | b31ab67 | 2018-03-20 22:44:18 -0700 | [diff] [blame] | 237 | // Save the chosen and validated certificate into database |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 238 | try { |
| 239 | Log.d(TAG, "Saving the randomly chosen endpoint certificate to database"); |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 240 | if (mDatabase.setRecoveryServiceCertPath(userId, uid, rootCertificateAlias, |
| 241 | certPath) > 0) { |
| 242 | mDatabase.setRecoveryServiceCertSerial(userId, uid, rootCertificateAlias, |
| 243 | newSerial); |
Robert Berry | e8edf97 | 2018-03-27 11:45:11 +0100 | [diff] [blame] | 244 | if (wasInitialized) { |
| 245 | Log.i(TAG, "This is a certificate change. Snapshot pending."); |
| 246 | mDatabase.setShouldCreateSnapshot(userId, uid, true); |
| 247 | } |
Bo Zhu | 8d6861e | 2018-03-21 20:45:09 -0700 | [diff] [blame] | 248 | mDatabase.setCounterId(userId, uid, new SecureRandom().nextLong()); |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 249 | } |
| 250 | } catch (CertificateEncodingException e) { |
| 251 | Log.e(TAG, "Failed to encode CertPath", e); |
| 252 | throw new ServiceSpecificException( |
| 253 | ERROR_BAD_CERTIFICATE_FORMAT, "Failed to encode CertPath."); |
| 254 | } |
| 255 | } |
| 256 | |
Bo Zhu | 7f414d9 | 2018-02-28 09:28:19 -0800 | [diff] [blame] | 257 | /** |
| 258 | * Initializes the recovery service with the two files {@code recoveryServiceCertFile} and |
| 259 | * {@code recoveryServiceSigFile}. |
| 260 | * |
| 261 | * @param rootCertificateAlias the alias for the root certificate that is used for validating |
| 262 | * the recovery service certificates. |
| 263 | * @param recoveryServiceCertFile the content of the XML file containing a list of certificates |
| 264 | * for the recovery service. |
| 265 | * @param recoveryServiceSigFile the content of the XML file containing the public-key signature |
| 266 | * over the entire content of {@code recoveryServiceCertFile}. |
| 267 | */ |
| 268 | public void initRecoveryServiceWithSigFile( |
| 269 | @NonNull String rootCertificateAlias, @NonNull byte[] recoveryServiceCertFile, |
| 270 | @NonNull byte[] recoveryServiceSigFile) |
| 271 | throws RemoteException { |
Dmitry Dementyev | a5945d5 | 2018-03-23 12:10:09 -0700 | [diff] [blame] | 272 | checkRecoverKeyStorePermission(); |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 273 | rootCertificateAlias = replaceEmptyValueWithSecureDefault(rootCertificateAlias); |
Dmitry Dementyev | 1e6a9dc | 2018-03-21 13:52:00 -0700 | [diff] [blame] | 274 | Preconditions.checkNotNull(recoveryServiceCertFile, "recoveryServiceCertFile is null"); |
| 275 | Preconditions.checkNotNull(recoveryServiceSigFile, "recoveryServiceSigFile is null"); |
Bo Zhu | 7f414d9 | 2018-02-28 09:28:19 -0800 | [diff] [blame] | 276 | |
| 277 | SigXml sigXml; |
| 278 | try { |
| 279 | sigXml = SigXml.parse(recoveryServiceSigFile); |
| 280 | } catch (CertParsingException e) { |
| 281 | Log.d(TAG, "Failed to parse the sig file: " + HexDump.toHexString( |
| 282 | recoveryServiceSigFile)); |
| 283 | throw new ServiceSpecificException( |
| 284 | ERROR_BAD_CERTIFICATE_FORMAT, "Failed to parse the sig file."); |
| 285 | } |
| 286 | |
Bo Zhu | b31ab67 | 2018-03-20 22:44:18 -0700 | [diff] [blame] | 287 | X509Certificate rootCert = getRootCertificate(rootCertificateAlias); |
Bo Zhu | 7f414d9 | 2018-02-28 09:28:19 -0800 | [diff] [blame] | 288 | try { |
Bo Zhu | b31ab67 | 2018-03-20 22:44:18 -0700 | [diff] [blame] | 289 | sigXml.verifyFileSignature(rootCert, recoveryServiceCertFile); |
Bo Zhu | 7f414d9 | 2018-02-28 09:28:19 -0800 | [diff] [blame] | 290 | } catch (CertValidationException e) { |
| 291 | Log.d(TAG, "The signature over the cert file is invalid." |
| 292 | + " Cert: " + HexDump.toHexString(recoveryServiceCertFile) |
| 293 | + " Sig: " + HexDump.toHexString(recoveryServiceSigFile)); |
| 294 | throw new ServiceSpecificException( |
| 295 | ERROR_INVALID_CERTIFICATE, "The signature over the cert file is invalid."); |
| 296 | } |
| 297 | |
| 298 | initRecoveryService(rootCertificateAlias, recoveryServiceCertFile); |
| 299 | } |
| 300 | |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 301 | private PublicKey parseEcPublicKey(@NonNull byte[] bytes) throws ServiceSpecificException { |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 302 | try { |
| 303 | KeyFactory kf = KeyFactory.getInstance("EC"); |
Bo Zhu | 14d993d | 2018-02-03 21:38:48 -0800 | [diff] [blame] | 304 | X509EncodedKeySpec pkSpec = new X509EncodedKeySpec(bytes); |
| 305 | return kf.generatePublic(pkSpec); |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 306 | } catch (NoSuchAlgorithmException e) { |
Robert Berry | 97e5558 | 2018-01-05 12:43:13 +0000 | [diff] [blame] | 307 | Log.wtf(TAG, "EC algorithm not available. AOSP must support this.", e); |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 308 | throw new ServiceSpecificException(ERROR_SERVICE_INTERNAL_ERROR, e.getMessage()); |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 309 | } catch (InvalidKeySpecException e) { |
Robert Berry | 97e5558 | 2018-01-05 12:43:13 +0000 | [diff] [blame] | 310 | throw new ServiceSpecificException( |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 311 | ERROR_BAD_CERTIFICATE_FORMAT, "Not a valid X509 certificate."); |
Bo Zhu | 5b81fa6 | 2017-12-21 14:36:11 -0800 | [diff] [blame] | 312 | } |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Gets all data necessary to recover application keys on new device. |
| 317 | * |
Dmitry Dementyev | 1e6a9dc | 2018-03-21 13:52:00 -0700 | [diff] [blame] | 318 | * @return KeyChain Snapshot. |
| 319 | * @throws ServiceSpecificException if no snapshot is pending. |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 320 | * @hide |
| 321 | */ |
Dmitry Dementyev | 1e6a9dc | 2018-03-21 13:52:00 -0700 | [diff] [blame] | 322 | public @NonNull KeyChainSnapshot getKeyChainSnapshot() |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 323 | throws RemoteException { |
| 324 | checkRecoverKeyStorePermission(); |
Dmitry Dementyev | 77183ef | 2018-01-05 15:46:00 -0800 | [diff] [blame] | 325 | int uid = Binder.getCallingUid(); |
Dmitry Dementyev | 0916e7c | 2018-01-23 13:02:08 -0800 | [diff] [blame] | 326 | KeyChainSnapshot snapshot = mSnapshotStorage.get(uid); |
Robert Berry | bd086f1 | 2017-12-27 13:29:39 +0000 | [diff] [blame] | 327 | if (snapshot == null) { |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 328 | throw new ServiceSpecificException(ERROR_NO_SNAPSHOT_PENDING); |
Robert Berry | bd086f1 | 2017-12-27 13:29:39 +0000 | [diff] [blame] | 329 | } |
| 330 | return snapshot; |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 333 | public void setSnapshotCreatedPendingIntent(@Nullable PendingIntent intent) |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 334 | throws RemoteException { |
| 335 | checkRecoverKeyStorePermission(); |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 336 | int uid = Binder.getCallingUid(); |
| 337 | mListenersStorage.setSnapshotListener(uid, intent); |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Robert Berry | 8f9038c | 2018-03-26 11:36:40 +0100 | [diff] [blame] | 340 | /** |
| 341 | * Set the server params for the user's key chain. This is used to uniquely identify a key |
| 342 | * chain. Along with the counter ID, it is used to uniquely identify an instance of a vault. |
| 343 | */ |
Dmitry Dementyev | 1e6a9dc | 2018-03-21 13:52:00 -0700 | [diff] [blame] | 344 | public void setServerParams(@NonNull byte[] serverParams) throws RemoteException { |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 345 | checkRecoverKeyStorePermission(); |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 346 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 40dadb0 | 2018-01-10 18:03:37 -0800 | [diff] [blame] | 347 | int uid = Binder.getCallingUid(); |
Robert Berry | 8f9038c | 2018-03-26 11:36:40 +0100 | [diff] [blame] | 348 | |
| 349 | byte[] currentServerParams = mDatabase.getServerParams(userId, uid); |
| 350 | |
| 351 | if (Arrays.equals(serverParams, currentServerParams)) { |
| 352 | Log.v(TAG, "Not updating server params - same as old value."); |
| 353 | return; |
Dmitry Dementyev | 40dadb0 | 2018-01-10 18:03:37 -0800 | [diff] [blame] | 354 | } |
Robert Berry | 8f9038c | 2018-03-26 11:36:40 +0100 | [diff] [blame] | 355 | |
| 356 | long updatedRows = mDatabase.setServerParams(userId, uid, serverParams); |
| 357 | if (updatedRows < 1) { |
| 358 | throw new ServiceSpecificException( |
| 359 | ERROR_SERVICE_INTERNAL_ERROR, "Database failure trying to set server params."); |
| 360 | } |
| 361 | |
| 362 | if (currentServerParams == null) { |
| 363 | Log.i(TAG, "Initialized server params."); |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | Log.i(TAG, "Updated server params. Snapshot pending."); |
| 368 | mDatabase.setShouldCreateSnapshot(userId, uid, true); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Dmitry Dementyev | ad88471 | 2017-12-20 12:38:36 -0800 | [diff] [blame] | 371 | /** |
Robert Berry | bbe02ae | 2018-02-20 19:47:43 +0000 | [diff] [blame] | 372 | * Sets the recovery status of key with {@code alias} to {@code status}. |
Dmitry Dementyev | ad88471 | 2017-12-20 12:38:36 -0800 | [diff] [blame] | 373 | */ |
Dmitry Dementyev | 1e6a9dc | 2018-03-21 13:52:00 -0700 | [diff] [blame] | 374 | public void setRecoveryStatus(@NonNull String alias, int status) throws RemoteException { |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 375 | checkRecoverKeyStorePermission(); |
Dmitry Dementyev | 1e6a9dc | 2018-03-21 13:52:00 -0700 | [diff] [blame] | 376 | Preconditions.checkNotNull(alias, "alias is null"); |
Robert Berry | bbe02ae | 2018-02-20 19:47:43 +0000 | [diff] [blame] | 377 | mDatabase.setRecoveryStatus(Binder.getCallingUid(), alias, status); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /** |
Robert Berry | 56f06b4 | 2018-02-23 13:31:32 +0000 | [diff] [blame] | 381 | * Returns recovery statuses for all keys belonging to the calling uid. |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 382 | * |
Robert Berry | 56f06b4 | 2018-02-23 13:31:32 +0000 | [diff] [blame] | 383 | * @return {@link Map} from key alias to recovery status. Recovery status is one of |
| 384 | * {@link RecoveryController#RECOVERY_STATUS_SYNCED}, |
| 385 | * {@link RecoveryController#RECOVERY_STATUS_SYNC_IN_PROGRESS} or |
| 386 | * {@link RecoveryController#RECOVERY_STATUS_PERMANENT_FAILURE}. |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 387 | */ |
Robert Berry | 56f06b4 | 2018-02-23 13:31:32 +0000 | [diff] [blame] | 388 | public @NonNull Map<String, Integer> getRecoveryStatus() throws RemoteException { |
Dmitry Dementyev | a5945d5 | 2018-03-23 12:10:09 -0700 | [diff] [blame] | 389 | checkRecoverKeyStorePermission(); |
Dmitry Dementyev | ad88471 | 2017-12-20 12:38:36 -0800 | [diff] [blame] | 390 | return mDatabase.getStatusForAllKeys(Binder.getCallingUid()); |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /** |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 394 | * Sets recovery secrets list used by all recovery agents for given {@code userId} |
| 395 | * |
| 396 | * @hide |
| 397 | */ |
| 398 | public void setRecoverySecretTypes( |
Dmitry Dementyev | 0916e7c | 2018-01-23 13:02:08 -0800 | [diff] [blame] | 399 | @NonNull @KeyChainProtectionParams.UserSecretType int[] secretTypes) |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 400 | throws RemoteException { |
| 401 | checkRecoverKeyStorePermission(); |
Dmitry Dementyev | 1e6a9dc | 2018-03-21 13:52:00 -0700 | [diff] [blame] | 402 | Preconditions.checkNotNull(secretTypes, "secretTypes is null"); |
Dmitry Dementyev | 40dadb0 | 2018-01-10 18:03:37 -0800 | [diff] [blame] | 403 | int userId = UserHandle.getCallingUserId(); |
| 404 | int uid = Binder.getCallingUid(); |
Robert Berry | 5a1acefb | 2018-03-26 14:41:30 +0100 | [diff] [blame] | 405 | |
| 406 | int[] currentSecretTypes = mDatabase.getRecoverySecretTypes(userId, uid); |
| 407 | if (Arrays.equals(secretTypes, currentSecretTypes)) { |
| 408 | Log.v(TAG, "Not updating secret types - same as old value."); |
| 409 | return; |
Dmitry Dementyev | 40dadb0 | 2018-01-10 18:03:37 -0800 | [diff] [blame] | 410 | } |
Robert Berry | 5a1acefb | 2018-03-26 14:41:30 +0100 | [diff] [blame] | 411 | |
| 412 | long updatedRows = mDatabase.setRecoverySecretTypes(userId, uid, secretTypes); |
| 413 | if (updatedRows < 1) { |
| 414 | throw new ServiceSpecificException(ERROR_SERVICE_INTERNAL_ERROR, |
| 415 | "Database error trying to set secret types."); |
| 416 | } |
| 417 | |
| 418 | if (currentSecretTypes.length == 0) { |
| 419 | Log.i(TAG, "Initialized secret types."); |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | Log.i(TAG, "Updated secret types. Snapshot pending."); |
| 424 | mDatabase.setShouldCreateSnapshot(userId, uid, true); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Gets secret types necessary to create Recovery Data. |
| 429 | * |
| 430 | * @return secret types |
| 431 | * @hide |
| 432 | */ |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 433 | public @NonNull int[] getRecoverySecretTypes() throws RemoteException { |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 434 | checkRecoverKeyStorePermission(); |
Dmitry Dementyev | bdfdf53 | 2017-12-27 11:58:45 -0800 | [diff] [blame] | 435 | return mDatabase.getRecoverySecretTypes(UserHandle.getCallingUserId(), |
| 436 | Binder.getCallingUid()); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /** |
Bo Zhu | 7c1972f | 2018-02-22 21:43:52 -0800 | [diff] [blame] | 440 | * Initializes recovery session given the X509-encoded public key of the recovery service. |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 441 | * |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 442 | * @param sessionId A unique ID to identify the recovery session. |
| 443 | * @param verifierPublicKey X509-encoded public key. |
| 444 | * @param vaultParams Additional params associated with vault. |
| 445 | * @param vaultChallenge Challenge issued by vault service. |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 446 | * @param secrets Lock-screen hashes. For now only a single secret is supported. |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 447 | * @return Encrypted bytes of recovery claim. This can then be issued to the vault service. |
Bo Zhu | f23c203 | 2018-03-21 22:46:55 -0700 | [diff] [blame] | 448 | * @deprecated Use {@link #startRecoverySessionWithCertPath(String, String, RecoveryCertPath, |
| 449 | * byte[], byte[], List)} instead. |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 450 | * |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 451 | * @hide |
| 452 | */ |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 453 | public @NonNull byte[] startRecoverySession( |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 454 | @NonNull String sessionId, |
| 455 | @NonNull byte[] verifierPublicKey, |
| 456 | @NonNull byte[] vaultParams, |
| 457 | @NonNull byte[] vaultChallenge, |
Dmitry Dementyev | 0916e7c | 2018-01-23 13:02:08 -0800 | [diff] [blame] | 458 | @NonNull List<KeyChainProtectionParams> secrets) |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 459 | throws RemoteException { |
| 460 | checkRecoverKeyStorePermission(); |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 461 | int uid = Binder.getCallingUid(); |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 462 | |
| 463 | if (secrets.size() != 1) { |
Robert Berry | 9e1bd36 | 2018-01-17 23:28:45 +0000 | [diff] [blame] | 464 | throw new UnsupportedOperationException( |
Dmitry Dementyev | 0916e7c | 2018-01-23 13:02:08 -0800 | [diff] [blame] | 465 | "Only a single KeyChainProtectionParams is supported"); |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Bo Zhu | def7ffd | 2018-01-05 14:50:52 -0800 | [diff] [blame] | 468 | PublicKey publicKey; |
| 469 | try { |
| 470 | publicKey = KeySyncUtils.deserializePublicKey(verifierPublicKey); |
Bo Zhu | def7ffd | 2018-01-05 14:50:52 -0800 | [diff] [blame] | 471 | } catch (InvalidKeySpecException e) { |
Bo Zhu | 7c1972f | 2018-02-22 21:43:52 -0800 | [diff] [blame] | 472 | throw new ServiceSpecificException(ERROR_BAD_CERTIFICATE_FORMAT, |
| 473 | "Not a valid X509 key"); |
Bo Zhu | def7ffd | 2018-01-05 14:50:52 -0800 | [diff] [blame] | 474 | } |
| 475 | // The raw public key bytes contained in vaultParams must match the ones given in |
| 476 | // verifierPublicKey; otherwise, the user secret may be decrypted by a key that is not owned |
| 477 | // by the original recovery service. |
| 478 | if (!publicKeysMatch(publicKey, vaultParams)) { |
Bo Zhu | 7f414d9 | 2018-02-28 09:28:19 -0800 | [diff] [blame] | 479 | throw new ServiceSpecificException(ERROR_INVALID_CERTIFICATE, |
Bo Zhu | def7ffd | 2018-01-05 14:50:52 -0800 | [diff] [blame] | 480 | "The public keys given in verifierPublicKey and vaultParams do not match."); |
| 481 | } |
| 482 | |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 483 | byte[] keyClaimant = KeySyncUtils.generateKeyClaimant(); |
| 484 | byte[] kfHash = secrets.get(0).getSecret(); |
| 485 | mRecoverySessionStorage.add( |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 486 | uid, |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 487 | new RecoverySessionStorage.Entry(sessionId, kfHash, keyClaimant, vaultParams)); |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 488 | |
Bo Zhu | f23c203 | 2018-03-21 22:46:55 -0700 | [diff] [blame] | 489 | Log.i(TAG, "Received VaultParams for recovery: " + HexDump.toHexString(vaultParams)); |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 490 | try { |
| 491 | byte[] thmKfHash = KeySyncUtils.calculateThmKfHash(kfHash); |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 492 | return KeySyncUtils.encryptRecoveryClaim( |
| 493 | publicKey, |
| 494 | vaultParams, |
| 495 | vaultChallenge, |
| 496 | thmKfHash, |
| 497 | keyClaimant); |
| 498 | } catch (NoSuchAlgorithmException e) { |
Robert Berry | 97e5558 | 2018-01-05 12:43:13 +0000 | [diff] [blame] | 499 | Log.wtf(TAG, "SecureBox algorithm missing. AOSP must support this.", e); |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 500 | throw new ServiceSpecificException(ERROR_SERVICE_INTERNAL_ERROR, e.getMessage()); |
Bo Zhu | def7ffd | 2018-01-05 14:50:52 -0800 | [diff] [blame] | 501 | } catch (InvalidKeyException e) { |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 502 | throw new ServiceSpecificException(ERROR_BAD_CERTIFICATE_FORMAT, e.getMessage()); |
Robert Berry | e16fa98 | 2017-12-20 15:59:37 +0000 | [diff] [blame] | 503 | } |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 504 | } |
| 505 | |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 506 | /** |
Bo Zhu | 7c1972f | 2018-02-22 21:43:52 -0800 | [diff] [blame] | 507 | * Initializes recovery session given the certificate path of the recovery service. |
| 508 | * |
| 509 | * @param sessionId A unique ID to identify the recovery session. |
| 510 | * @param verifierCertPath The certificate path of the recovery service. |
| 511 | * @param vaultParams Additional params associated with vault. |
| 512 | * @param vaultChallenge Challenge issued by vault service. |
| 513 | * @param secrets Lock-screen hashes. For now only a single secret is supported. |
| 514 | * @return Encrypted bytes of recovery claim. This can then be issued to the vault service. |
| 515 | * |
| 516 | * @hide |
| 517 | */ |
| 518 | public @NonNull byte[] startRecoverySessionWithCertPath( |
| 519 | @NonNull String sessionId, |
Bo Zhu | b31ab67 | 2018-03-20 22:44:18 -0700 | [diff] [blame] | 520 | @NonNull String rootCertificateAlias, |
Bo Zhu | 7c1972f | 2018-02-22 21:43:52 -0800 | [diff] [blame] | 521 | @NonNull RecoveryCertPath verifierCertPath, |
| 522 | @NonNull byte[] vaultParams, |
| 523 | @NonNull byte[] vaultChallenge, |
| 524 | @NonNull List<KeyChainProtectionParams> secrets) |
| 525 | throws RemoteException { |
| 526 | checkRecoverKeyStorePermission(); |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 527 | rootCertificateAlias = replaceEmptyValueWithSecureDefault(rootCertificateAlias); |
Dmitry Dementyev | 1e6a9dc | 2018-03-21 13:52:00 -0700 | [diff] [blame] | 528 | Preconditions.checkNotNull(sessionId, "invalid session"); |
| 529 | Preconditions.checkNotNull(verifierCertPath, "verifierCertPath is null"); |
| 530 | Preconditions.checkNotNull(vaultParams, "vaultParams is null"); |
| 531 | Preconditions.checkNotNull(vaultChallenge, "vaultChallenge is null"); |
| 532 | Preconditions.checkNotNull(secrets, "secrets is null"); |
Bo Zhu | 7c1972f | 2018-02-22 21:43:52 -0800 | [diff] [blame] | 533 | CertPath certPath; |
| 534 | try { |
| 535 | certPath = verifierCertPath.getCertPath(); |
| 536 | } catch (CertificateException e) { |
| 537 | throw new ServiceSpecificException(ERROR_BAD_CERTIFICATE_FORMAT, |
| 538 | "Failed decode the certificate path"); |
| 539 | } |
| 540 | |
Bo Zhu | 7ce4ea5 | 2018-02-27 23:52:19 -0800 | [diff] [blame] | 541 | try { |
Bo Zhu | b31ab67 | 2018-03-20 22:44:18 -0700 | [diff] [blame] | 542 | CertUtils.validateCertPath(getRootCertificate(rootCertificateAlias), certPath); |
Bo Zhu | 7ce4ea5 | 2018-02-27 23:52:19 -0800 | [diff] [blame] | 543 | } catch (CertValidationException e) { |
| 544 | Log.e(TAG, "Failed to validate the given cert path", e); |
Bo Zhu | b31ab67 | 2018-03-20 22:44:18 -0700 | [diff] [blame] | 545 | throw new ServiceSpecificException(ERROR_INVALID_CERTIFICATE, e.getMessage()); |
Bo Zhu | 7c1972f | 2018-02-22 21:43:52 -0800 | [diff] [blame] | 546 | } |
Bo Zhu | 7ce4ea5 | 2018-02-27 23:52:19 -0800 | [diff] [blame] | 547 | |
Bo Zhu | 7c1972f | 2018-02-22 21:43:52 -0800 | [diff] [blame] | 548 | byte[] verifierPublicKey = certPath.getCertificates().get(0).getPublicKey().getEncoded(); |
| 549 | if (verifierPublicKey == null) { |
| 550 | Log.e(TAG, "Failed to encode verifierPublicKey"); |
| 551 | throw new ServiceSpecificException(ERROR_BAD_CERTIFICATE_FORMAT, |
| 552 | "Failed to encode verifierPublicKey"); |
| 553 | } |
| 554 | |
| 555 | return startRecoverySession( |
| 556 | sessionId, verifierPublicKey, vaultParams, vaultChallenge, secrets); |
| 557 | } |
| 558 | |
| 559 | /** |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 560 | * Invoked by a recovery agent after a successful recovery claim is sent to the remote vault |
| 561 | * service. |
| 562 | * |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 563 | * @param sessionId The session ID used to generate the claim. See |
Bo Zhu | def7ffd | 2018-01-05 14:50:52 -0800 | [diff] [blame] | 564 | * {@link #startRecoverySession(String, byte[], byte[], byte[], List)}. |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 565 | * @param encryptedRecoveryKey The encrypted recovery key blob returned by the remote vault |
| 566 | * service. |
| 567 | * @param applicationKeys The encrypted key blobs returned by the remote vault service. These |
| 568 | * were wrapped with the recovery key. |
Robert Berry | bd4c43c | 2017-12-22 11:35:14 +0000 | [diff] [blame] | 569 | * @return Map from alias to raw key material. |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 570 | * @throws RemoteException if an error occurred recovering the keys. |
| 571 | */ |
Dmitry Dementyev | 4da14e0 | 2018-03-23 15:18:33 -0700 | [diff] [blame] | 572 | public @NonNull Map<String, byte[]> recoverKeys( |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 573 | @NonNull String sessionId, |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 574 | @NonNull byte[] encryptedRecoveryKey, |
Robert Berry | 5f13870 | 2018-01-17 15:18:05 +0000 | [diff] [blame] | 575 | @NonNull List<WrappedApplicationKey> applicationKeys) |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 576 | throws RemoteException { |
| 577 | checkRecoverKeyStorePermission(); |
Dmitry Dementyev | 1e6a9dc | 2018-03-21 13:52:00 -0700 | [diff] [blame] | 578 | Preconditions.checkNotNull(sessionId, "invalid session"); |
| 579 | Preconditions.checkNotNull(encryptedRecoveryKey, "encryptedRecoveryKey is null"); |
| 580 | Preconditions.checkNotNull(applicationKeys, "encryptedRecoveryKey is null"); |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 581 | int uid = Binder.getCallingUid(); |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 582 | RecoverySessionStorage.Entry sessionEntry = mRecoverySessionStorage.get(uid, sessionId); |
| 583 | if (sessionEntry == null) { |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 584 | throw new ServiceSpecificException(ERROR_SESSION_EXPIRED, |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 585 | String.format(Locale.US, |
| 586 | "Application uid=%d does not have pending session '%s'", uid, sessionId)); |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | try { |
| 590 | byte[] recoveryKey = decryptRecoveryKey(sessionEntry, encryptedRecoveryKey); |
Robert Berry | bd4c43c | 2017-12-22 11:35:14 +0000 | [diff] [blame] | 591 | return recoverApplicationKeys(recoveryKey, applicationKeys); |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 592 | } finally { |
| 593 | sessionEntry.destroy(); |
| 594 | mRecoverySessionStorage.remove(uid); |
| 595 | } |
| 596 | } |
| 597 | |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 598 | /** |
Robert Berry | 4a5c87d | 2018-03-19 18:00:46 +0000 | [diff] [blame] | 599 | * Invoked by a recovery agent after a successful recovery claim is sent to the remote vault |
| 600 | * service. |
| 601 | * |
| 602 | * @param sessionId The session ID used to generate the claim. See |
| 603 | * {@link #startRecoverySession(String, byte[], byte[], byte[], List)}. |
| 604 | * @param encryptedRecoveryKey The encrypted recovery key blob returned by the remote vault |
| 605 | * service. |
| 606 | * @param applicationKeys The encrypted key blobs returned by the remote vault service. These |
| 607 | * were wrapped with the recovery key. |
| 608 | * @throws RemoteException if an error occurred recovering the keys. |
| 609 | */ |
Dmitry Dementyev | fd4ae0b | 2018-03-23 11:06:24 -0700 | [diff] [blame] | 610 | public @NonNull Map<String, String> recoverKeyChainSnapshot( |
Robert Berry | 4a5c87d | 2018-03-19 18:00:46 +0000 | [diff] [blame] | 611 | @NonNull String sessionId, |
| 612 | @NonNull byte[] encryptedRecoveryKey, |
| 613 | @NonNull List<WrappedApplicationKey> applicationKeys) throws RemoteException { |
| 614 | checkRecoverKeyStorePermission(); |
| 615 | int userId = UserHandle.getCallingUserId(); |
| 616 | int uid = Binder.getCallingUid(); |
| 617 | RecoverySessionStorage.Entry sessionEntry = mRecoverySessionStorage.get(uid, sessionId); |
| 618 | if (sessionEntry == null) { |
| 619 | throw new ServiceSpecificException(ERROR_SESSION_EXPIRED, |
| 620 | String.format(Locale.US, |
| 621 | "Application uid=%d does not have pending session '%s'", |
| 622 | uid, |
| 623 | sessionId)); |
| 624 | } |
| 625 | |
| 626 | try { |
| 627 | byte[] recoveryKey = decryptRecoveryKey(sessionEntry, encryptedRecoveryKey); |
| 628 | Map<String, byte[]> keysByAlias = recoverApplicationKeys(recoveryKey, applicationKeys); |
| 629 | return importKeyMaterials(userId, uid, keysByAlias); |
| 630 | } catch (KeyStoreException e) { |
| 631 | throw new ServiceSpecificException(ERROR_SERVICE_INTERNAL_ERROR, e.getMessage()); |
| 632 | } finally { |
| 633 | sessionEntry.destroy(); |
| 634 | mRecoverySessionStorage.remove(uid); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | /** |
| 639 | * Imports the key materials, returning a map from alias to grant alias for the calling user. |
| 640 | * |
| 641 | * @param userId The calling user ID. |
| 642 | * @param uid The calling uid. |
| 643 | * @param keysByAlias The key materials, keyed by alias. |
| 644 | * @throws KeyStoreException if an error occurs importing the key or getting the grant. |
| 645 | */ |
Dmitry Dementyev | fd4ae0b | 2018-03-23 11:06:24 -0700 | [diff] [blame] | 646 | private @NonNull Map<String, String> importKeyMaterials( |
Robert Berry | 4a5c87d | 2018-03-19 18:00:46 +0000 | [diff] [blame] | 647 | int userId, int uid, Map<String, byte[]> keysByAlias) throws KeyStoreException { |
| 648 | ArrayMap<String, String> grantAliasesByAlias = new ArrayMap<>(keysByAlias.size()); |
| 649 | for (String alias : keysByAlias.keySet()) { |
| 650 | mApplicationKeyStorage.setSymmetricKeyEntry(userId, uid, alias, keysByAlias.get(alias)); |
| 651 | String grantAlias = getAlias(userId, uid, alias); |
| 652 | Log.i(TAG, String.format(Locale.US, "Import %s -> %s", alias, grantAlias)); |
| 653 | grantAliasesByAlias.put(alias, grantAlias); |
| 654 | } |
| 655 | return grantAliasesByAlias; |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * Returns an alias for the key. |
| 660 | * |
| 661 | * @param userId The user ID of the calling process. |
| 662 | * @param uid The uid of the calling process. |
| 663 | * @param alias The alias of the key. |
| 664 | * @return The alias in the calling process's keystore. |
| 665 | */ |
Dmitry Dementyev | 4da14e0 | 2018-03-23 15:18:33 -0700 | [diff] [blame] | 666 | private @Nullable String getAlias(int userId, int uid, String alias) { |
Robert Berry | 4a5c87d | 2018-03-19 18:00:46 +0000 | [diff] [blame] | 667 | return mApplicationKeyStorage.getGrantAlias(userId, uid, alias); |
| 668 | } |
| 669 | |
| 670 | /** |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 671 | * Deprecated |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 672 | * Generates a key named {@code alias} in the recoverable store for the calling uid. Then |
| 673 | * returns the raw key material. |
| 674 | * |
| 675 | * <p>TODO: Once AndroidKeyStore has added move api, do not return raw bytes. |
| 676 | * |
Bo Zhu | 2c8e538 | 2018-02-26 15:54:25 -0800 | [diff] [blame] | 677 | * @deprecated |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 678 | * @hide |
| 679 | */ |
| 680 | public byte[] generateAndStoreKey(@NonNull String alias) throws RemoteException { |
Dmitry Dementyev | a5945d5 | 2018-03-23 12:10:09 -0700 | [diff] [blame] | 681 | checkRecoverKeyStorePermission(); |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 682 | int uid = Binder.getCallingUid(); |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 683 | int userId = UserHandle.getCallingUserId(); |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 684 | |
Bo Zhu | 3462c83 | 2018-01-04 22:42:36 -0800 | [diff] [blame] | 685 | PlatformEncryptionKey encryptionKey; |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 686 | try { |
Bo Zhu | 3462c83 | 2018-01-04 22:42:36 -0800 | [diff] [blame] | 687 | encryptionKey = mPlatformKeyManager.getEncryptKey(userId); |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 688 | } catch (NoSuchAlgorithmException e) { |
| 689 | // Impossible: all algorithms must be supported by AOSP |
| 690 | throw new RuntimeException(e); |
| 691 | } catch (KeyStoreException | UnrecoverableKeyException e) { |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 692 | throw new ServiceSpecificException(ERROR_SERVICE_INTERNAL_ERROR, e.getMessage()); |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 693 | } catch (InsecureUserException e) { |
| 694 | throw new ServiceSpecificException(ERROR_INSECURE_USER, e.getMessage()); |
| 695 | } |
| 696 | |
| 697 | try { |
| 698 | return mRecoverableKeyGenerator.generateAndStoreKey(encryptionKey, userId, uid, alias); |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 699 | } catch (KeyStoreException | InvalidKeyException | RecoverableKeyStorageException e) { |
| 700 | throw new ServiceSpecificException(ERROR_SERVICE_INTERNAL_ERROR, e.getMessage()); |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
Robert Berry | 2bcdad9 | 2018-01-18 12:53:29 +0000 | [diff] [blame] | 704 | /** |
| 705 | * Destroys the session with the given {@code sessionId}. |
| 706 | */ |
| 707 | public void closeSession(@NonNull String sessionId) throws RemoteException { |
Dmitry Dementyev | 1e6a9dc | 2018-03-21 13:52:00 -0700 | [diff] [blame] | 708 | checkRecoverKeyStorePermission(); |
| 709 | Preconditions.checkNotNull(sessionId, "invalid session"); |
Robert Berry | 2bcdad9 | 2018-01-18 12:53:29 +0000 | [diff] [blame] | 710 | mRecoverySessionStorage.remove(Binder.getCallingUid(), sessionId); |
| 711 | } |
| 712 | |
Robert Berry | 5daccec | 2018-01-06 19:16:25 +0000 | [diff] [blame] | 713 | public void removeKey(@NonNull String alias) throws RemoteException { |
Dmitry Dementyev | a5945d5 | 2018-03-23 12:10:09 -0700 | [diff] [blame] | 714 | checkRecoverKeyStorePermission(); |
Dmitry Dementyev | 1e6a9dc | 2018-03-21 13:52:00 -0700 | [diff] [blame] | 715 | Preconditions.checkNotNull(alias, "alias is null"); |
Dmitry Dementyev | 77183ef | 2018-01-05 15:46:00 -0800 | [diff] [blame] | 716 | int uid = Binder.getCallingUid(); |
| 717 | int userId = UserHandle.getCallingUserId(); |
| 718 | |
| 719 | boolean wasRemoved = mDatabase.removeKey(uid, alias); |
| 720 | if (wasRemoved) { |
| 721 | mDatabase.setShouldCreateSnapshot(userId, uid, true); |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 722 | mApplicationKeyStorage.deleteEntry(userId, uid, alias); |
Dmitry Dementyev | 77183ef | 2018-01-05 15:46:00 -0800 | [diff] [blame] | 723 | } |
Robert Berry | 5daccec | 2018-01-06 19:16:25 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 726 | /** |
| 727 | * Generates a key named {@code alias} in caller's namespace. |
| 728 | * The key is stored in system service keystore namespace. |
| 729 | * |
| 730 | * @return grant alias, which caller can use to access the key. |
| 731 | */ |
Robert Berry | a3b9947 | 2018-02-23 15:59:02 +0000 | [diff] [blame] | 732 | public String generateKey(@NonNull String alias) throws RemoteException { |
Dmitry Dementyev | a5945d5 | 2018-03-23 12:10:09 -0700 | [diff] [blame] | 733 | checkRecoverKeyStorePermission(); |
Dmitry Dementyev | 1e6a9dc | 2018-03-21 13:52:00 -0700 | [diff] [blame] | 734 | Preconditions.checkNotNull(alias, "alias is null"); |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 735 | int uid = Binder.getCallingUid(); |
| 736 | int userId = UserHandle.getCallingUserId(); |
| 737 | |
| 738 | PlatformEncryptionKey encryptionKey; |
| 739 | try { |
| 740 | encryptionKey = mPlatformKeyManager.getEncryptKey(userId); |
| 741 | } catch (NoSuchAlgorithmException e) { |
| 742 | // Impossible: all algorithms must be supported by AOSP |
| 743 | throw new RuntimeException(e); |
| 744 | } catch (KeyStoreException | UnrecoverableKeyException e) { |
| 745 | throw new ServiceSpecificException(ERROR_SERVICE_INTERNAL_ERROR, e.getMessage()); |
| 746 | } catch (InsecureUserException e) { |
| 747 | throw new ServiceSpecificException(ERROR_INSECURE_USER, e.getMessage()); |
| 748 | } |
| 749 | |
| 750 | try { |
| 751 | byte[] secretKey = |
| 752 | mRecoverableKeyGenerator.generateAndStoreKey(encryptionKey, userId, uid, alias); |
| 753 | mApplicationKeyStorage.setSymmetricKeyEntry(userId, uid, alias, secretKey); |
Robert Berry | 4a5c87d | 2018-03-19 18:00:46 +0000 | [diff] [blame] | 754 | return getAlias(userId, uid, alias); |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 755 | } catch (KeyStoreException | InvalidKeyException | RecoverableKeyStorageException e) { |
| 756 | throw new ServiceSpecificException(ERROR_SERVICE_INTERNAL_ERROR, e.getMessage()); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | /** |
Bo Zhu | 2c8e538 | 2018-02-26 15:54:25 -0800 | [diff] [blame] | 761 | * Imports a 256-bit AES-GCM key named {@code alias}. The key is stored in system service |
| 762 | * keystore namespace. |
| 763 | * |
| 764 | * @param alias the alias provided by caller as a reference to the key. |
| 765 | * @param keyBytes the raw bytes of the 256-bit AES key. |
| 766 | * @return grant alias, which caller can use to access the key. |
| 767 | * @throws RemoteException if the given key is invalid or some internal errors occur. |
| 768 | * |
| 769 | * @hide |
| 770 | */ |
Dmitry Dementyev | 4da14e0 | 2018-03-23 15:18:33 -0700 | [diff] [blame] | 771 | public @Nullable String importKey(@NonNull String alias, @NonNull byte[] keyBytes) |
Bo Zhu | 2c8e538 | 2018-02-26 15:54:25 -0800 | [diff] [blame] | 772 | throws RemoteException { |
Dmitry Dementyev | a5945d5 | 2018-03-23 12:10:09 -0700 | [diff] [blame] | 773 | checkRecoverKeyStorePermission(); |
Dmitry Dementyev | 1e6a9dc | 2018-03-21 13:52:00 -0700 | [diff] [blame] | 774 | Preconditions.checkNotNull(alias, "alias is null"); |
| 775 | Preconditions.checkNotNull(keyBytes, "keyBytes is null"); |
| 776 | if (keyBytes.length != RecoverableKeyGenerator.KEY_SIZE_BITS / Byte.SIZE) { |
Bo Zhu | 2c8e538 | 2018-02-26 15:54:25 -0800 | [diff] [blame] | 777 | Log.e(TAG, "The given key for import doesn't have the required length " |
| 778 | + RecoverableKeyGenerator.KEY_SIZE_BITS); |
| 779 | throw new ServiceSpecificException(ERROR_INVALID_KEY_FORMAT, |
| 780 | "The given key does not contain " + RecoverableKeyGenerator.KEY_SIZE_BITS |
| 781 | + " bits."); |
| 782 | } |
| 783 | |
| 784 | int uid = Binder.getCallingUid(); |
| 785 | int userId = UserHandle.getCallingUserId(); |
| 786 | |
| 787 | // TODO: Refactor RecoverableKeyGenerator to wrap the PlatformKey logic |
| 788 | |
| 789 | PlatformEncryptionKey encryptionKey; |
| 790 | try { |
| 791 | encryptionKey = mPlatformKeyManager.getEncryptKey(userId); |
| 792 | } catch (NoSuchAlgorithmException e) { |
| 793 | // Impossible: all algorithms must be supported by AOSP |
| 794 | throw new RuntimeException(e); |
| 795 | } catch (KeyStoreException | UnrecoverableKeyException e) { |
| 796 | throw new ServiceSpecificException(ERROR_SERVICE_INTERNAL_ERROR, e.getMessage()); |
| 797 | } catch (InsecureUserException e) { |
| 798 | throw new ServiceSpecificException(ERROR_INSECURE_USER, e.getMessage()); |
| 799 | } |
| 800 | |
| 801 | try { |
| 802 | // Wrap the key by the platform key and store the wrapped key locally |
| 803 | mRecoverableKeyGenerator.importKey(encryptionKey, userId, uid, alias, keyBytes); |
| 804 | |
| 805 | // Import the key to Android KeyStore and get grant |
| 806 | mApplicationKeyStorage.setSymmetricKeyEntry(userId, uid, alias, keyBytes); |
Robert Berry | 4a5c87d | 2018-03-19 18:00:46 +0000 | [diff] [blame] | 807 | return getAlias(userId, uid, alias); |
Bo Zhu | 2c8e538 | 2018-02-26 15:54:25 -0800 | [diff] [blame] | 808 | } catch (KeyStoreException | InvalidKeyException | RecoverableKeyStorageException e) { |
| 809 | throw new ServiceSpecificException(ERROR_SERVICE_INTERNAL_ERROR, e.getMessage()); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | /** |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 814 | * Gets a key named {@code alias} in caller's namespace. |
| 815 | * |
| 816 | * @return grant alias, which caller can use to access the key. |
| 817 | */ |
Dmitry Dementyev | 4da14e0 | 2018-03-23 15:18:33 -0700 | [diff] [blame] | 818 | public @Nullable String getKey(@NonNull String alias) throws RemoteException { |
Dmitry Dementyev | a5945d5 | 2018-03-23 12:10:09 -0700 | [diff] [blame] | 819 | checkRecoverKeyStorePermission(); |
Dmitry Dementyev | 1e6a9dc | 2018-03-21 13:52:00 -0700 | [diff] [blame] | 820 | Preconditions.checkNotNull(alias, "alias is null"); |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 821 | int uid = Binder.getCallingUid(); |
| 822 | int userId = UserHandle.getCallingUserId(); |
Robert Berry | 4a5c87d | 2018-03-19 18:00:46 +0000 | [diff] [blame] | 823 | return getAlias(userId, uid, alias); |
Dmitry Dementyev | 29b9de5 | 2018-01-31 16:09:32 -0800 | [diff] [blame] | 824 | } |
| 825 | |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 826 | private byte[] decryptRecoveryKey( |
| 827 | RecoverySessionStorage.Entry sessionEntry, byte[] encryptedClaimResponse) |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 828 | throws RemoteException, ServiceSpecificException { |
Bo Zhu | 31a40c0 | 2018-01-24 17:40:29 -0800 | [diff] [blame] | 829 | byte[] locallyEncryptedKey; |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 830 | try { |
Bo Zhu | 31a40c0 | 2018-01-24 17:40:29 -0800 | [diff] [blame] | 831 | locallyEncryptedKey = KeySyncUtils.decryptRecoveryClaimResponse( |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 832 | sessionEntry.getKeyClaimant(), |
| 833 | sessionEntry.getVaultParams(), |
| 834 | encryptedClaimResponse); |
Bo Zhu | 31a40c0 | 2018-01-24 17:40:29 -0800 | [diff] [blame] | 835 | } catch (InvalidKeyException e) { |
| 836 | Log.e(TAG, "Got InvalidKeyException during decrypting recovery claim response", e); |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 837 | throw new ServiceSpecificException(ERROR_DECRYPTION_FAILED, |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 838 | "Failed to decrypt recovery key " + e.getMessage()); |
Bo Zhu | 31a40c0 | 2018-01-24 17:40:29 -0800 | [diff] [blame] | 839 | } catch (AEADBadTagException e) { |
| 840 | Log.e(TAG, "Got AEADBadTagException during decrypting recovery claim response", e); |
Bo Zhu | 31a40c0 | 2018-01-24 17:40:29 -0800 | [diff] [blame] | 841 | throw new ServiceSpecificException(ERROR_DECRYPTION_FAILED, |
| 842 | "Failed to decrypt recovery key " + e.getMessage()); |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 843 | } catch (NoSuchAlgorithmException e) { |
| 844 | // Should never happen: all the algorithms used are required by AOSP implementations |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 845 | throw new ServiceSpecificException(ERROR_SERVICE_INTERNAL_ERROR, e.getMessage()); |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 846 | } |
Bo Zhu | 31a40c0 | 2018-01-24 17:40:29 -0800 | [diff] [blame] | 847 | |
| 848 | try { |
Bo Zhu | 9d05bfb | 2018-01-29 17:22:03 -0800 | [diff] [blame] | 849 | return KeySyncUtils.decryptRecoveryKey(sessionEntry.getLskfHash(), locallyEncryptedKey); |
| 850 | } catch (InvalidKeyException e) { |
| 851 | Log.e(TAG, "Got InvalidKeyException during decrypting recovery key", e); |
Bo Zhu | 31a40c0 | 2018-01-24 17:40:29 -0800 | [diff] [blame] | 852 | throw new ServiceSpecificException(ERROR_DECRYPTION_FAILED, |
| 853 | "Failed to decrypt recovery key " + e.getMessage()); |
| 854 | } catch (AEADBadTagException e) { |
| 855 | Log.e(TAG, "Got AEADBadTagException during decrypting recovery key", e); |
Bo Zhu | 31a40c0 | 2018-01-24 17:40:29 -0800 | [diff] [blame] | 856 | throw new ServiceSpecificException(ERROR_DECRYPTION_FAILED, |
| 857 | "Failed to decrypt recovery key " + e.getMessage()); |
| 858 | } catch (NoSuchAlgorithmException e) { |
| 859 | // Should never happen: all the algorithms used are required by AOSP implementations |
| 860 | throw new ServiceSpecificException(ERROR_SERVICE_INTERNAL_ERROR, e.getMessage()); |
| 861 | } |
| 862 | } |
| 863 | |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 864 | /** |
| 865 | * Uses {@code recoveryKey} to decrypt {@code applicationKeys}. |
| 866 | * |
Robert Berry | bd4c43c | 2017-12-22 11:35:14 +0000 | [diff] [blame] | 867 | * @return Map from alias to raw key material. |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 868 | * @throws RemoteException if an error occurred decrypting the keys. |
| 869 | */ |
Dmitry Dementyev | 4da14e0 | 2018-03-23 15:18:33 -0700 | [diff] [blame] | 870 | private @NonNull Map<String, byte[]> recoverApplicationKeys( |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 871 | @NonNull byte[] recoveryKey, |
Robert Berry | 5f13870 | 2018-01-17 15:18:05 +0000 | [diff] [blame] | 872 | @NonNull List<WrappedApplicationKey> applicationKeys) throws RemoteException { |
Robert Berry | bd4c43c | 2017-12-22 11:35:14 +0000 | [diff] [blame] | 873 | HashMap<String, byte[]> keyMaterialByAlias = new HashMap<>(); |
Robert Berry | 5f13870 | 2018-01-17 15:18:05 +0000 | [diff] [blame] | 874 | for (WrappedApplicationKey applicationKey : applicationKeys) { |
Dmitry Dementyev | 07c76555 | 2018-01-08 17:31:59 -0800 | [diff] [blame] | 875 | String alias = applicationKey.getAlias(); |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 876 | byte[] encryptedKeyMaterial = applicationKey.getEncryptedKeyMaterial(); |
| 877 | |
| 878 | try { |
Robert Berry | bd4c43c | 2017-12-22 11:35:14 +0000 | [diff] [blame] | 879 | byte[] keyMaterial = |
| 880 | KeySyncUtils.decryptApplicationKey(recoveryKey, encryptedKeyMaterial); |
| 881 | keyMaterialByAlias.put(alias, keyMaterial); |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 882 | } catch (NoSuchAlgorithmException e) { |
Robert Berry | 97e5558 | 2018-01-05 12:43:13 +0000 | [diff] [blame] | 883 | Log.wtf(TAG, "Missing SecureBox algorithm. AOSP required to support this.", e); |
| 884 | throw new ServiceSpecificException( |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 885 | ERROR_SERVICE_INTERNAL_ERROR, e.getMessage()); |
Bo Zhu | a9a04e2 | 2018-01-26 11:38:11 -0800 | [diff] [blame] | 886 | } catch (InvalidKeyException e) { |
Bo Zhu | a9a04e2 | 2018-01-26 11:38:11 -0800 | [diff] [blame] | 887 | Log.e(TAG, "Got InvalidKeyException during decrypting application key with alias: " |
| 888 | + alias, e); |
Bo Zhu | a9a04e2 | 2018-01-26 11:38:11 -0800 | [diff] [blame] | 889 | throw new ServiceSpecificException(ERROR_DECRYPTION_FAILED, |
| 890 | "Failed to recover key with alias '" + alias + "': " + e.getMessage()); |
| 891 | } catch (AEADBadTagException e) { |
Bo Zhu | a9a04e2 | 2018-01-26 11:38:11 -0800 | [diff] [blame] | 892 | Log.e(TAG, "Got AEADBadTagException during decrypting application key with alias: " |
| 893 | + alias, e); |
Bo Zhu | 4857cb5 | 2018-02-06 14:34:48 -0800 | [diff] [blame] | 894 | // Ignore the exception to continue to recover the other application keys. |
Robert Berry | b9a220b | 2017-12-21 12:41:01 +0000 | [diff] [blame] | 895 | } |
| 896 | } |
Bo Zhu | ae0682d | 2018-02-13 10:23:39 -0800 | [diff] [blame] | 897 | if (!applicationKeys.isEmpty() && keyMaterialByAlias.isEmpty()) { |
Bo Zhu | 4857cb5 | 2018-02-06 14:34:48 -0800 | [diff] [blame] | 898 | Log.e(TAG, "Failed to recover any of the application keys."); |
| 899 | throw new ServiceSpecificException(ERROR_DECRYPTION_FAILED, |
| 900 | "Failed to recover any of the application keys."); |
| 901 | } |
Robert Berry | bd4c43c | 2017-12-22 11:35:14 +0000 | [diff] [blame] | 902 | return keyMaterialByAlias; |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 903 | } |
| 904 | |
Dmitry Dementyev | 6a509e4 | 2017-12-19 14:47:26 -0800 | [diff] [blame] | 905 | /** |
| 906 | * This function can only be used inside LockSettingsService. |
| 907 | * |
Robert Berry | 9104404 | 2017-12-27 12:05:58 +0000 | [diff] [blame] | 908 | * @param storedHashType from {@code CredentialHash} |
Dmitry Dementyev | 6a509e4 | 2017-12-19 14:47:26 -0800 | [diff] [blame] | 909 | * @param credential - unencrypted String. Password length should be at most 16 symbols {@code |
| 910 | * mPasswordMaxLength} |
| 911 | * @param userId for user who just unlocked the device. |
| 912 | * @hide |
| 913 | */ |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 914 | public void lockScreenSecretAvailable( |
Dmitry Dementyev | 6a509e4 | 2017-12-19 14:47:26 -0800 | [diff] [blame] | 915 | int storedHashType, @NonNull String credential, int userId) { |
Robert Berry | 4a534ec | 2017-12-21 15:44:02 +0000 | [diff] [blame] | 916 | // So as not to block the critical path unlocking the phone, defer to another thread. |
| 917 | try { |
| 918 | mExecutorService.execute(KeySyncTask.newInstance( |
Robert Berry | 9104404 | 2017-12-27 12:05:58 +0000 | [diff] [blame] | 919 | mContext, |
| 920 | mDatabase, |
| 921 | mSnapshotStorage, |
| 922 | mListenersStorage, |
| 923 | userId, |
| 924 | storedHashType, |
Dmitry Dementyev | 77183ef | 2018-01-05 15:46:00 -0800 | [diff] [blame] | 925 | credential, |
| 926 | /*credentialUpdated=*/ false)); |
Robert Berry | 4a534ec | 2017-12-21 15:44:02 +0000 | [diff] [blame] | 927 | } catch (NoSuchAlgorithmException e) { |
| 928 | Log.wtf(TAG, "Should never happen - algorithm unavailable for KeySync", e); |
| 929 | } catch (KeyStoreException e) { |
| 930 | Log.e(TAG, "Key store error encountered during recoverable key sync", e); |
| 931 | } catch (InsecureUserException e) { |
| 932 | Log.wtf(TAG, "Impossible - insecure user, but user just entered lock screen", e); |
Dmitry Dementyev | 6a509e4 | 2017-12-19 14:47:26 -0800 | [diff] [blame] | 933 | } |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 934 | } |
| 935 | |
Dmitry Dementyev | 77183ef | 2018-01-05 15:46:00 -0800 | [diff] [blame] | 936 | /** |
| 937 | * This function can only be used inside LockSettingsService. |
Dmitry Dementyev | 6e16724 | 2018-01-25 15:29:50 -0800 | [diff] [blame] | 938 | * |
Dmitry Dementyev | 77183ef | 2018-01-05 15:46:00 -0800 | [diff] [blame] | 939 | * @param storedHashType from {@code CredentialHash} |
| 940 | * @param credential - unencrypted String |
| 941 | * @param userId for the user whose lock screen credentials were changed. |
| 942 | * @hide |
| 943 | */ |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 944 | public void lockScreenSecretChanged( |
Dmitry Dementyev | 77183ef | 2018-01-05 15:46:00 -0800 | [diff] [blame] | 945 | int storedHashType, |
Dmitry Dementyev | 6a509e4 | 2017-12-19 14:47:26 -0800 | [diff] [blame] | 946 | @Nullable String credential, |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 947 | int userId) { |
Dmitry Dementyev | 77183ef | 2018-01-05 15:46:00 -0800 | [diff] [blame] | 948 | // So as not to block the critical path unlocking the phone, defer to another thread. |
| 949 | try { |
| 950 | mExecutorService.execute(KeySyncTask.newInstance( |
| 951 | mContext, |
| 952 | mDatabase, |
| 953 | mSnapshotStorage, |
| 954 | mListenersStorage, |
| 955 | userId, |
| 956 | storedHashType, |
| 957 | credential, |
| 958 | /*credentialUpdated=*/ true)); |
| 959 | } catch (NoSuchAlgorithmException e) { |
| 960 | Log.wtf(TAG, "Should never happen - algorithm unavailable for KeySync", e); |
| 961 | } catch (KeyStoreException e) { |
| 962 | Log.e(TAG, "Key store error encountered during recoverable key sync", e); |
| 963 | } catch (InsecureUserException e) { |
Dmitry Dementyev | 6e16724 | 2018-01-25 15:29:50 -0800 | [diff] [blame] | 964 | Log.e(TAG, "InsecureUserException during lock screen secret update", e); |
Dmitry Dementyev | 77183ef | 2018-01-05 15:46:00 -0800 | [diff] [blame] | 965 | } |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 966 | } |
| 967 | |
Bo Zhu | b31ab67 | 2018-03-20 22:44:18 -0700 | [diff] [blame] | 968 | private X509Certificate getRootCertificate(String rootCertificateAlias) throws RemoteException { |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 969 | rootCertificateAlias = replaceEmptyValueWithSecureDefault(rootCertificateAlias); |
Bo Zhu | b31ab67 | 2018-03-20 22:44:18 -0700 | [diff] [blame] | 970 | X509Certificate rootCertificate = |
| 971 | TrustedRootCertificates.getRootCertificate(rootCertificateAlias); |
| 972 | if (rootCertificate == null) { |
| 973 | throw new ServiceSpecificException( |
| 974 | ERROR_INVALID_CERTIFICATE, "The provided root certificate alias is invalid"); |
| 975 | } |
| 976 | return rootCertificate; |
| 977 | } |
| 978 | |
Dmitry Dementyev | f34fc7e | 2018-03-26 17:31:29 -0700 | [diff] [blame^] | 979 | private @NonNull String replaceEmptyValueWithSecureDefault( |
| 980 | @Nullable String rootCertificateAlias) { |
| 981 | if (rootCertificateAlias == null || rootCertificateAlias.isEmpty()) { |
| 982 | Log.e(TAG, "rootCertificateAlias is null or empty"); |
| 983 | // Use the default Google Key Vault Service CA certificate if the alias is not provided |
| 984 | rootCertificateAlias = TrustedRootCertificates.GOOGLE_CLOUD_KEY_VAULT_SERVICE_V1_ALIAS; |
| 985 | } |
| 986 | return rootCertificateAlias; |
| 987 | } |
| 988 | |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 989 | private void checkRecoverKeyStorePermission() { |
| 990 | mContext.enforceCallingOrSelfPermission( |
Dmitry Dementyev | ed89ea0 | 2018-01-11 13:53:52 -0800 | [diff] [blame] | 991 | Manifest.permission.RECOVER_KEYSTORE, |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 992 | "Caller " + Binder.getCallingUid() + " doesn't have RecoverKeyStore permission."); |
| 993 | } |
Bo Zhu | def7ffd | 2018-01-05 14:50:52 -0800 | [diff] [blame] | 994 | |
| 995 | private boolean publicKeysMatch(PublicKey publicKey, byte[] vaultParams) { |
| 996 | byte[] encodedPublicKey = SecureBox.encodePublicKey(publicKey); |
| 997 | return Arrays.equals(encodedPublicKey, Arrays.copyOf(vaultParams, encodedPublicKey.length)); |
| 998 | } |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 999 | } |