Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -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 | |
Dmitry Dementyev | ed89ea0 | 2018-01-11 13:53:52 -0800 | [diff] [blame] | 17 | package android.security.keystore; |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 21 | import android.app.PendingIntent; |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 22 | import android.content.pm.PackageManager.NameNotFoundException; |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 23 | import android.os.RemoteException; |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 24 | import android.os.ServiceManager; |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 25 | import android.os.ServiceSpecificException; |
Robert Berry | 2bcdad9 | 2018-01-18 12:53:29 +0000 | [diff] [blame] | 26 | import android.util.Log; |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 27 | |
| 28 | import com.android.internal.widget.ILockSettings; |
| 29 | |
| 30 | import java.util.List; |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 31 | import java.util.Map; |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
Robert Berry | 74928a1 | 2018-01-18 17:49:07 +0000 | [diff] [blame] | 34 | * An assistant for generating {@link javax.crypto.SecretKey} instances that can be recovered by |
| 35 | * other Android devices belonging to the user. The exported keychain is protected by the user's |
| 36 | * lock screen. |
| 37 | * |
| 38 | * <p>The RecoveryController must be paired with a recovery agent. The recovery agent is responsible |
| 39 | * for transporting the keychain to remote trusted hardware. This hardware must prevent brute force |
| 40 | * attempts against the user's lock screen by limiting the number of allowed guesses (to, e.g., 10). |
| 41 | * After that number of incorrect guesses, the trusted hardware no longer allows access to the |
| 42 | * key chain. |
| 43 | * |
| 44 | * <p>For now only the recovery agent itself is able to create keys, so it is expected that the |
| 45 | * recovery agent is itself the system app. |
| 46 | * |
| 47 | * <p>A recovery agent requires the privileged permission |
| 48 | * {@code android.Manifest.permission#RECOVER_KEYSTORE}. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 49 | * |
Robert Berry | bbe02ae | 2018-02-20 19:47:43 +0000 | [diff] [blame] | 50 | * @deprecated Use {@link android.security.keystore.recovery.RecoveryController}. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 51 | * @hide |
| 52 | */ |
Robert Berry | 74928a1 | 2018-01-18 17:49:07 +0000 | [diff] [blame] | 53 | public class RecoveryController { |
Robert Berry | 2bcdad9 | 2018-01-18 12:53:29 +0000 | [diff] [blame] | 54 | private static final String TAG = "RecoveryController"; |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 55 | |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 56 | /** Key has been successfully synced. */ |
| 57 | public static final int RECOVERY_STATUS_SYNCED = 0; |
| 58 | /** Waiting for recovery agent to sync the key. */ |
| 59 | public static final int RECOVERY_STATUS_SYNC_IN_PROGRESS = 1; |
| 60 | /** Recovery account is not available. */ |
| 61 | public static final int RECOVERY_STATUS_MISSING_ACCOUNT = 2; |
| 62 | /** Key cannot be synced. */ |
| 63 | public static final int RECOVERY_STATUS_PERMANENT_FAILURE = 3; |
| 64 | |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 65 | /** |
| 66 | * Failed because no snapshot is yet pending to be synced for the user. |
| 67 | * |
| 68 | * @hide |
| 69 | */ |
| 70 | public static final int ERROR_NO_SNAPSHOT_PENDING = 21; |
| 71 | |
| 72 | /** |
| 73 | * Failed due to an error internal to the recovery service. This is unexpected and indicates |
| 74 | * either a problem with the logic in the service, or a problem with a dependency of the |
| 75 | * service (such as AndroidKeyStore). |
| 76 | * |
| 77 | * @hide |
| 78 | */ |
| 79 | public static final int ERROR_SERVICE_INTERNAL_ERROR = 22; |
| 80 | |
| 81 | /** |
| 82 | * Failed because the user does not have a lock screen set. |
| 83 | * |
| 84 | * @hide |
| 85 | */ |
| 86 | public static final int ERROR_INSECURE_USER = 23; |
| 87 | |
| 88 | /** |
| 89 | * Error thrown when attempting to use a recovery session that has since been closed. |
| 90 | * |
| 91 | * @hide |
| 92 | */ |
| 93 | public static final int ERROR_SESSION_EXPIRED = 24; |
| 94 | |
| 95 | /** |
| 96 | * Failed because the provided certificate was not a valid X509 certificate. |
| 97 | * |
| 98 | * @hide |
| 99 | */ |
| 100 | public static final int ERROR_BAD_CERTIFICATE_FORMAT = 25; |
| 101 | |
| 102 | /** |
| 103 | * Error thrown if decryption failed. This might be because the tag is wrong, the key is wrong, |
| 104 | * the data has become corrupted, the data has been tampered with, etc. |
| 105 | * |
| 106 | * @hide |
| 107 | */ |
| 108 | public static final int ERROR_DECRYPTION_FAILED = 26; |
| 109 | |
| 110 | |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 111 | private final ILockSettings mBinder; |
| 112 | |
Robert Berry | 74928a1 | 2018-01-18 17:49:07 +0000 | [diff] [blame] | 113 | private RecoveryController(ILockSettings binder) { |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 114 | mBinder = binder; |
| 115 | } |
| 116 | |
Dmitry Dementyev | ed89ea0 | 2018-01-11 13:53:52 -0800 | [diff] [blame] | 117 | /** |
| 118 | * Gets a new instance of the class. |
| 119 | */ |
Robert Berry | 74928a1 | 2018-01-18 17:49:07 +0000 | [diff] [blame] | 120 | public static RecoveryController getInstance() { |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 121 | ILockSettings lockSettings = |
| 122 | ILockSettings.Stub.asInterface(ServiceManager.getService("lock_settings")); |
Robert Berry | 74928a1 | 2018-01-18 17:49:07 +0000 | [diff] [blame] | 123 | return new RecoveryController(lockSettings); |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
Robert Berry | 74928a1 | 2018-01-18 17:49:07 +0000 | [diff] [blame] | 127 | * Initializes key recovery service for the calling application. RecoveryController |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 128 | * randomly chooses one of the keys from the list and keeps it to use for future key export |
| 129 | * operations. Collection of all keys in the list must be signed by the provided {@code |
| 130 | * rootCertificateAlias}, which must also be present in the list of root certificates |
Robert Berry | 74928a1 | 2018-01-18 17:49:07 +0000 | [diff] [blame] | 131 | * preinstalled on the device. The random selection allows RecoveryController to select |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 132 | * which of a set of remote recovery service devices will be used. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 133 | * |
Robert Berry | 74928a1 | 2018-01-18 17:49:07 +0000 | [diff] [blame] | 134 | * <p>In addition, RecoveryController enforces a delay of three months between |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 135 | * consecutive initialization attempts, to limit the ability of an attacker to often switch |
| 136 | * remote recovery devices and significantly increase number of recovery attempts. |
| 137 | * |
| 138 | * @param rootCertificateAlias alias of a root certificate preinstalled on the device |
| 139 | * @param signedPublicKeyList binary blob a list of X509 certificates and signature |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 140 | * @throws BadCertificateFormatException if the {@code signedPublicKeyList} is in a bad format. |
| 141 | * @throws InternalRecoveryServiceException if an unexpected error occurred in the recovery |
| 142 | * service. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 143 | */ |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 144 | public void initRecoveryService( |
| 145 | @NonNull String rootCertificateAlias, @NonNull byte[] signedPublicKeyList) |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 146 | throws BadCertificateFormatException, InternalRecoveryServiceException { |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 147 | try { |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 148 | mBinder.initRecoveryService(rootCertificateAlias, signedPublicKeyList); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 149 | } catch (RemoteException e) { |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 150 | throw e.rethrowFromSystemServer(); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 151 | } catch (ServiceSpecificException e) { |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 152 | if (e.errorCode == ERROR_BAD_CERTIFICATE_FORMAT) { |
| 153 | throw new BadCertificateFormatException(e.getMessage()); |
| 154 | } |
| 155 | throw wrapUnexpectedServiceSpecificException(e); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 156 | } |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 160 | * Returns data necessary to store all recoverable keys for given account. Key material is |
| 161 | * encrypted with user secret and recovery public key. |
| 162 | * |
| 163 | * @param account specific to Recovery agent. |
| 164 | * @return Data necessary to recover keystore. |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 165 | * @throws InternalRecoveryServiceException if an unexpected error occurred in the recovery |
| 166 | * service. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 167 | */ |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 168 | public @NonNull KeychainSnapshot getRecoveryData(@NonNull byte[] account) |
| 169 | throws InternalRecoveryServiceException { |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 170 | try { |
Dmitry Dementyev | b4fb987 | 2018-01-26 11:49:34 -0800 | [diff] [blame] | 171 | return BackwardsCompat.toLegacyKeychainSnapshot(mBinder.getKeyChainSnapshot()); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 172 | } catch (RemoteException e) { |
| 173 | throw e.rethrowFromSystemServer(); |
| 174 | } catch (ServiceSpecificException e) { |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 175 | if (e.errorCode == ERROR_NO_SNAPSHOT_PENDING) { |
| 176 | return null; |
| 177 | } |
| 178 | throw wrapUnexpectedServiceSpecificException(e); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 179 | } |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 183 | * Sets a listener which notifies recovery agent that new recovery snapshot is available. {@link |
| 184 | * #getRecoveryData} can be used to get the snapshot. Note that every recovery agent can have at |
| 185 | * most one registered listener at any time. |
| 186 | * |
| 187 | * @param intent triggered when new snapshot is available. Unregisters listener if the value is |
| 188 | * {@code null}. |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 189 | * @throws InternalRecoveryServiceException if an unexpected error occurred in the recovery |
| 190 | * service. |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 191 | */ |
| 192 | public void setSnapshotCreatedPendingIntent(@Nullable PendingIntent intent) |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 193 | throws InternalRecoveryServiceException { |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 194 | try { |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 195 | mBinder.setSnapshotCreatedPendingIntent(intent); |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 196 | } catch (RemoteException e) { |
| 197 | throw e.rethrowFromSystemServer(); |
| 198 | } catch (ServiceSpecificException e) { |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 199 | throw wrapUnexpectedServiceSpecificException(e); |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Returns a map from recovery agent accounts to corresponding KeyStore recovery snapshot |
| 205 | * version. Version zero is used, if no snapshots were created for the account. |
| 206 | * |
| 207 | * @return Map from recovery agent accounts to snapshot versions. |
Robert Berry | 5f13870 | 2018-01-17 15:18:05 +0000 | [diff] [blame] | 208 | * @see KeychainSnapshot#getSnapshotVersion |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 209 | * @throws InternalRecoveryServiceException if an unexpected error occurred in the recovery |
| 210 | * service. |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 211 | */ |
| 212 | public @NonNull Map<byte[], Integer> getRecoverySnapshotVersions() |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 213 | throws InternalRecoveryServiceException { |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 214 | try { |
| 215 | // IPC doesn't support generic Maps. |
| 216 | @SuppressWarnings("unchecked") |
| 217 | Map<byte[], Integer> result = |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 218 | (Map<byte[], Integer>) mBinder.getRecoverySnapshotVersions(); |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 219 | return result; |
| 220 | } catch (RemoteException e) { |
| 221 | throw e.rethrowFromSystemServer(); |
| 222 | } catch (ServiceSpecificException e) { |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 223 | throw wrapUnexpectedServiceSpecificException(e); |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | /** |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 228 | * Server parameters used to generate new recovery key blobs. This value will be included in |
Robert Berry | 5f13870 | 2018-01-17 15:18:05 +0000 | [diff] [blame] | 229 | * {@code KeychainSnapshot.getEncryptedRecoveryKeyBlob()}. The same value must be included |
Bo Zhu | 584b923f | 2017-12-22 16:05:15 -0800 | [diff] [blame] | 230 | * in vaultParams {@link #startRecoverySession} |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 231 | * |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 232 | * @param serverParams included in recovery key blob. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 233 | * @see #getRecoveryData |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 234 | * @throws InternalRecoveryServiceException if an unexpected error occurred in the recovery |
| 235 | * service. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 236 | */ |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 237 | public void setServerParams(byte[] serverParams) throws InternalRecoveryServiceException { |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 238 | try { |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 239 | mBinder.setServerParams(serverParams); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 240 | } catch (RemoteException e) { |
| 241 | throw e.rethrowFromSystemServer(); |
| 242 | } catch (ServiceSpecificException e) { |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 243 | throw wrapUnexpectedServiceSpecificException(e); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 244 | } |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /** |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 248 | * Updates recovery status for given keys. It is used to notify keystore that key was |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 249 | * successfully stored on the server or there were an error. Application can check this value |
| 250 | * using {@code getRecoveyStatus}. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 251 | * |
| 252 | * @param packageName Application whose recoverable keys' statuses are to be updated. |
| 253 | * @param aliases List of application-specific key aliases. If the array is empty, updates the |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 254 | * status for all existing recoverable keys. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 255 | * @param status Status specific to recovery agent. |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 256 | * @throws InternalRecoveryServiceException if an unexpected error occurred in the recovery |
| 257 | * service. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 258 | */ |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 259 | public void setRecoveryStatus( |
| 260 | @NonNull String packageName, @Nullable String[] aliases, int status) |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 261 | throws NameNotFoundException, InternalRecoveryServiceException { |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 262 | try { |
Robert Berry | bbe02ae | 2018-02-20 19:47:43 +0000 | [diff] [blame] | 263 | for (String alias : aliases) { |
| 264 | mBinder.setRecoveryStatus(alias, status); |
| 265 | } |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 266 | } catch (RemoteException e) { |
| 267 | throw e.rethrowFromSystemServer(); |
| 268 | } catch (ServiceSpecificException e) { |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 269 | throw wrapUnexpectedServiceSpecificException(e); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 270 | } |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /** |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 274 | * Returns a {@code Map} from Application's KeyStore key aliases to their recovery status. |
| 275 | * Negative status values are reserved for recovery agent specific codes. List of common codes: |
| 276 | * |
| 277 | * <ul> |
| 278 | * <li>{@link #RECOVERY_STATUS_SYNCED} |
| 279 | * <li>{@link #RECOVERY_STATUS_SYNC_IN_PROGRESS} |
| 280 | * <li>{@link #RECOVERY_STATUS_MISSING_ACCOUNT} |
| 281 | * <li>{@link #RECOVERY_STATUS_PERMANENT_FAILURE} |
| 282 | * </ul> |
| 283 | * |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 284 | * @return {@code Map} from KeyStore alias to recovery status. |
| 285 | * @see #setRecoveryStatus |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 286 | * @throws InternalRecoveryServiceException if an unexpected error occurred in the recovery |
| 287 | * service. |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 288 | */ |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 289 | public Map<String, Integer> getRecoveryStatus() throws InternalRecoveryServiceException { |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 290 | try { |
| 291 | // IPC doesn't support generic Maps. |
| 292 | @SuppressWarnings("unchecked") |
| 293 | Map<String, Integer> result = |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 294 | (Map<String, Integer>) mBinder.getRecoveryStatus(/*packageName=*/ null); |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 295 | return result; |
| 296 | } catch (RemoteException e) { |
| 297 | throw e.rethrowFromSystemServer(); |
| 298 | } catch (ServiceSpecificException e) { |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 299 | throw wrapUnexpectedServiceSpecificException(e); |
Dmitry Dementyev | b8b030b | 2017-12-19 11:02:54 -0800 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | /** |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 304 | * Specifies a set of secret types used for end-to-end keystore encryption. Knowing all of them |
| 305 | * is necessary to recover data. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 306 | * |
Robert Berry | 9e1bd36 | 2018-01-17 23:28:45 +0000 | [diff] [blame] | 307 | * @param secretTypes {@link KeychainProtectionParams#TYPE_LOCKSCREEN} or {@link |
| 308 | * KeychainProtectionParams#TYPE_CUSTOM_PASSWORD} |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 309 | * @throws InternalRecoveryServiceException if an unexpected error occurred in the recovery |
| 310 | * service. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 311 | */ |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 312 | public void setRecoverySecretTypes( |
Robert Berry | 9e1bd36 | 2018-01-17 23:28:45 +0000 | [diff] [blame] | 313 | @NonNull @KeychainProtectionParams.UserSecretType int[] secretTypes) |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 314 | throws InternalRecoveryServiceException { |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 315 | try { |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 316 | mBinder.setRecoverySecretTypes(secretTypes); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 317 | } catch (RemoteException e) { |
| 318 | throw e.rethrowFromSystemServer(); |
| 319 | } catch (ServiceSpecificException e) { |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 320 | throw wrapUnexpectedServiceSpecificException(e); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 321 | } |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /** |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 325 | * Defines a set of secret types used for end-to-end keystore encryption. Knowing all of them is |
Robert Berry | 5f13870 | 2018-01-17 15:18:05 +0000 | [diff] [blame] | 326 | * necessary to generate KeychainSnapshot. |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 327 | * |
| 328 | * @return list of recovery secret types |
Robert Berry | 5f13870 | 2018-01-17 15:18:05 +0000 | [diff] [blame] | 329 | * @see KeychainSnapshot |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 330 | * @throws InternalRecoveryServiceException if an unexpected error occurred in the recovery |
| 331 | * service. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 332 | */ |
Robert Berry | 9e1bd36 | 2018-01-17 23:28:45 +0000 | [diff] [blame] | 333 | public @NonNull @KeychainProtectionParams.UserSecretType int[] getRecoverySecretTypes() |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 334 | throws InternalRecoveryServiceException { |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 335 | try { |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 336 | return mBinder.getRecoverySecretTypes(); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 337 | } catch (RemoteException e) { |
| 338 | throw e.rethrowFromSystemServer(); |
| 339 | } catch (ServiceSpecificException e) { |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 340 | throw wrapUnexpectedServiceSpecificException(e); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 341 | } |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Returns a list of recovery secret types, necessary to create a pending recovery snapshot. |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 346 | * When user enters a secret of a pending type {@link #recoverySecretAvailable} should be |
| 347 | * called. |
| 348 | * |
| 349 | * @return list of recovery secret types |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 350 | * @throws InternalRecoveryServiceException if an unexpected error occurred in the recovery |
| 351 | * service. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 352 | */ |
Robert Berry | 5f13870 | 2018-01-17 15:18:05 +0000 | [diff] [blame] | 353 | @NonNull |
Robert Berry | 9e1bd36 | 2018-01-17 23:28:45 +0000 | [diff] [blame] | 354 | public @KeychainProtectionParams.UserSecretType int[] getPendingRecoverySecretTypes() |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 355 | throws InternalRecoveryServiceException { |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 356 | try { |
Dmitry Dementyev | 1429831 | 2018-01-04 15:19:19 -0800 | [diff] [blame] | 357 | return mBinder.getPendingRecoverySecretTypes(); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 358 | } catch (RemoteException e) { |
| 359 | throw e.rethrowFromSystemServer(); |
| 360 | } catch (ServiceSpecificException e) { |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 361 | throw wrapUnexpectedServiceSpecificException(e); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 362 | } |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /** |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 366 | * Initializes recovery session and returns a blob with proof of recovery secrets possession. |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 367 | * The method generates symmetric key for a session, which trusted remote device can use to |
| 368 | * return recovery key. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 369 | * |
Dmitry Dementyev | 7d8c78a | 2018-01-12 19:14:07 -0800 | [diff] [blame] | 370 | * @param verifierPublicKey Encoded {@code java.security.cert.X509Certificate} with Public key |
| 371 | * used to create the recovery blob on the source device. |
| 372 | * Keystore will verify the certificate using root of trust. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 373 | * @param vaultParams Must match the parameters in the corresponding field in the recovery blob. |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 374 | * Used to limit number of guesses. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 375 | * @param vaultChallenge Data passed from server for this recovery session and used to prevent |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 376 | * replay attacks |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 377 | * @param secrets Secrets provided by user, the method only uses type and secret fields. |
Robert Berry | 2bcdad9 | 2018-01-18 12:53:29 +0000 | [diff] [blame] | 378 | * @return The recovery claim. Claim provides a b binary blob with recovery claim. It is |
| 379 | * encrypted with verifierPublicKey and contains a proof of user secrets, session symmetric |
| 380 | * key and parameters necessary to identify the counter with the number of failed recovery |
| 381 | * attempts. |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 382 | * @throws BadCertificateFormatException if the {@code verifierPublicKey} is in an incorrect |
| 383 | * format. |
| 384 | * @throws InternalRecoveryServiceException if an unexpected error occurred in the recovery |
| 385 | * service. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 386 | */ |
Robert Berry | 2bcdad9 | 2018-01-18 12:53:29 +0000 | [diff] [blame] | 387 | @NonNull public RecoveryClaim startRecoverySession( |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 388 | @NonNull byte[] verifierPublicKey, |
| 389 | @NonNull byte[] vaultParams, |
| 390 | @NonNull byte[] vaultChallenge, |
Robert Berry | 9e1bd36 | 2018-01-17 23:28:45 +0000 | [diff] [blame] | 391 | @NonNull List<KeychainProtectionParams> secrets) |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 392 | throws BadCertificateFormatException, InternalRecoveryServiceException { |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 393 | try { |
Robert Berry | 2bcdad9 | 2018-01-18 12:53:29 +0000 | [diff] [blame] | 394 | RecoverySession recoverySession = RecoverySession.newInstance(this); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 395 | byte[] recoveryClaim = |
| 396 | mBinder.startRecoverySession( |
Robert Berry | 2bcdad9 | 2018-01-18 12:53:29 +0000 | [diff] [blame] | 397 | recoverySession.getSessionId(), |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 398 | verifierPublicKey, |
| 399 | vaultParams, |
| 400 | vaultChallenge, |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 401 | BackwardsCompat.fromLegacyKeychainProtectionParams(secrets)); |
Robert Berry | 2bcdad9 | 2018-01-18 12:53:29 +0000 | [diff] [blame] | 402 | return new RecoveryClaim(recoverySession, recoveryClaim); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 403 | } catch (RemoteException e) { |
| 404 | throw e.rethrowFromSystemServer(); |
| 405 | } catch (ServiceSpecificException e) { |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 406 | if (e.errorCode == ERROR_BAD_CERTIFICATE_FORMAT) { |
| 407 | throw new BadCertificateFormatException(e.getMessage()); |
| 408 | } |
| 409 | throw wrapUnexpectedServiceSpecificException(e); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 410 | } |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Imports keys. |
| 415 | * |
Robert Berry | 2bcdad9 | 2018-01-18 12:53:29 +0000 | [diff] [blame] | 416 | * @param session Related recovery session, as originally created by invoking |
| 417 | * {@link #startRecoverySession(byte[], byte[], byte[], List)}. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 418 | * @param recoveryKeyBlob Recovery blob encrypted by symmetric key generated for this session. |
| 419 | * @param applicationKeys Application keys. Key material can be decrypted using recoveryKeyBlob |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 420 | * and session. KeyStore only uses package names from the application info in {@link |
Robert Berry | 5f13870 | 2018-01-17 15:18:05 +0000 | [diff] [blame] | 421 | * WrappedApplicationKey}. Caller is responsibility to perform certificates check. |
Robert Berry | bd4c43c | 2017-12-22 11:35:14 +0000 | [diff] [blame] | 422 | * @return Map from alias to raw key material. |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 423 | * @throws SessionExpiredException if {@code session} has since been closed. |
| 424 | * @throws DecryptionFailedException if unable to decrypt the snapshot. |
| 425 | * @throws InternalRecoveryServiceException if an error occurs internal to the recovery service. |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 426 | */ |
Robert Berry | bd4c43c | 2017-12-22 11:35:14 +0000 | [diff] [blame] | 427 | public Map<String, byte[]> recoverKeys( |
Robert Berry | 2bcdad9 | 2018-01-18 12:53:29 +0000 | [diff] [blame] | 428 | @NonNull RecoverySession session, |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 429 | @NonNull byte[] recoveryKeyBlob, |
Robert Berry | 5f13870 | 2018-01-17 15:18:05 +0000 | [diff] [blame] | 430 | @NonNull List<WrappedApplicationKey> applicationKeys) |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 431 | throws SessionExpiredException, DecryptionFailedException, |
| 432 | InternalRecoveryServiceException { |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 433 | try { |
Robert Berry | bd4c43c | 2017-12-22 11:35:14 +0000 | [diff] [blame] | 434 | return (Map<String, byte[]>) mBinder.recoverKeys( |
Robert Berry | 81ee34b | 2018-01-23 11:59:59 +0000 | [diff] [blame] | 435 | session.getSessionId(), |
| 436 | recoveryKeyBlob, |
| 437 | BackwardsCompat.fromLegacyWrappedApplicationKeys(applicationKeys)); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 438 | } catch (RemoteException e) { |
| 439 | throw e.rethrowFromSystemServer(); |
| 440 | } catch (ServiceSpecificException e) { |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 441 | if (e.errorCode == ERROR_DECRYPTION_FAILED) { |
| 442 | throw new DecryptionFailedException(e.getMessage()); |
| 443 | } |
| 444 | if (e.errorCode == ERROR_SESSION_EXPIRED) { |
| 445 | throw new SessionExpiredException(e.getMessage()); |
| 446 | } |
| 447 | throw wrapUnexpectedServiceSpecificException(e); |
Dmitry Dementyev | 1aa9613 | 2017-12-11 11:33:12 -0800 | [diff] [blame] | 448 | } |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 449 | } |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 450 | |
| 451 | /** |
Robert Berry | 2bcdad9 | 2018-01-18 12:53:29 +0000 | [diff] [blame] | 452 | * Deletes all data associated with {@code session}. Should not be invoked directly but via |
| 453 | * {@link RecoverySession#close()}. |
| 454 | * |
| 455 | * @hide |
| 456 | */ |
| 457 | void closeSession(RecoverySession session) { |
| 458 | try { |
| 459 | mBinder.closeSession(session.getSessionId()); |
| 460 | } catch (RemoteException | ServiceSpecificException e) { |
| 461 | Log.e(TAG, "Unexpected error trying to close session", e); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /** |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 466 | * Generates a key called {@code alias} and loads it into the recoverable key store. Returns the |
| 467 | * raw material of the key. |
| 468 | * |
Dmitry Dementyev | e77a24b | 2018-01-10 12:26:05 -0800 | [diff] [blame] | 469 | * @param alias The key alias. |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 470 | * @throws InternalRecoveryServiceException if an unexpected error occurred in the recovery |
| 471 | * service. |
| 472 | * @throws LockScreenRequiredException if the user has not set a lock screen. This is required |
| 473 | * to generate recoverable keys, as the snapshots are encrypted using a key derived from the |
| 474 | * lock screen. |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 475 | */ |
Dmitry Dementyev | e77a24b | 2018-01-10 12:26:05 -0800 | [diff] [blame] | 476 | public byte[] generateAndStoreKey(@NonNull String alias) |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 477 | throws InternalRecoveryServiceException, LockScreenRequiredException { |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 478 | try { |
| 479 | return mBinder.generateAndStoreKey(alias); |
| 480 | } catch (RemoteException e) { |
| 481 | throw e.rethrowFromSystemServer(); |
| 482 | } catch (ServiceSpecificException e) { |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 483 | if (e.errorCode == ERROR_INSECURE_USER) { |
| 484 | throw new LockScreenRequiredException(e.getMessage()); |
| 485 | } |
| 486 | throw wrapUnexpectedServiceSpecificException(e); |
Robert Berry | cfc990a | 2017-12-22 15:54:30 +0000 | [diff] [blame] | 487 | } |
| 488 | } |
Dmitry Dementyev | e77a24b | 2018-01-10 12:26:05 -0800 | [diff] [blame] | 489 | |
| 490 | /** |
| 491 | * Removes a key called {@code alias} from the recoverable key store. |
| 492 | * |
| 493 | * @param alias The key alias. |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 494 | * @throws InternalRecoveryServiceException if an unexpected error occurred in the recovery |
| 495 | * service. |
Dmitry Dementyev | e77a24b | 2018-01-10 12:26:05 -0800 | [diff] [blame] | 496 | */ |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 497 | public void removeKey(@NonNull String alias) throws InternalRecoveryServiceException { |
Dmitry Dementyev | e77a24b | 2018-01-10 12:26:05 -0800 | [diff] [blame] | 498 | try { |
| 499 | mBinder.removeKey(alias); |
| 500 | } catch (RemoteException e) { |
| 501 | throw e.rethrowFromSystemServer(); |
| 502 | } catch (ServiceSpecificException e) { |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 503 | throw wrapUnexpectedServiceSpecificException(e); |
Dmitry Dementyev | e77a24b | 2018-01-10 12:26:05 -0800 | [diff] [blame] | 504 | } |
| 505 | } |
Robert Berry | a16cd59 | 2018-01-17 14:43:09 +0000 | [diff] [blame] | 506 | |
| 507 | private InternalRecoveryServiceException wrapUnexpectedServiceSpecificException( |
| 508 | ServiceSpecificException e) { |
| 509 | if (e.errorCode == ERROR_SERVICE_INTERNAL_ERROR) { |
| 510 | return new InternalRecoveryServiceException(e.getMessage()); |
| 511 | } |
| 512 | |
| 513 | // Should never happen. If it does, it's a bug, and we need to update how the method that |
| 514 | // called this throws its exceptions. |
| 515 | return new InternalRecoveryServiceException("Unexpected error code for method: " |
| 516 | + e.errorCode, e); |
| 517 | } |
Dmitry Dementyev | 8eaf607 | 2017-12-06 19:05:33 -0800 | [diff] [blame] | 518 | } |