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