Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 android.security; |
| 18 | |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Brian Young | 9272dab | 2018-02-23 18:04:20 +0000 | [diff] [blame] | 20 | import android.app.ActivityManager; |
Svetoslav | 2dac95d | 2015-04-30 11:30:33 -0700 | [diff] [blame] | 21 | import android.app.ActivityThread; |
| 22 | import android.app.Application; |
Alex Klyubin | 5418393 | 2015-05-08 15:25:48 -0700 | [diff] [blame] | 23 | import android.app.KeyguardManager; |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Kevin Chyn | 5596642 | 2017-10-23 16:08:47 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
Alex Klyubin | 2d7a85c | 2015-04-30 11:43:53 -0700 | [diff] [blame] | 26 | import android.hardware.fingerprint.FingerprintManager; |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 27 | import android.os.Binder; |
| 28 | import android.os.IBinder; |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 29 | import android.os.Process; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 30 | import android.os.RemoteException; |
| 31 | import android.os.ServiceManager; |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 32 | import android.os.UserHandle; |
Janis Danisevskis | 1864c95 | 2018-08-09 11:14:49 -0700 | [diff] [blame] | 33 | import android.security.KeyStoreException; |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 34 | import android.security.keymaster.ExportResult; |
| 35 | import android.security.keymaster.KeyCharacteristics; |
| 36 | import android.security.keymaster.KeymasterArguments; |
Chad Brubaker | 5e73c0e | 2015-03-21 22:46:43 -0700 | [diff] [blame] | 37 | import android.security.keymaster.KeymasterBlob; |
Shawn Willden | 8d8c747 | 2016-02-02 08:27:39 -0700 | [diff] [blame] | 38 | import android.security.keymaster.KeymasterCertificateChain; |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 39 | import android.security.keymaster.KeymasterDefs; |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 40 | import android.security.keymaster.OperationResult; |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 41 | import android.security.keystore.IKeystoreService; |
Alex Klyubin | 3f8d4d8 | 2015-05-13 09:15:00 -0700 | [diff] [blame] | 42 | import android.security.keystore.KeyExpiredException; |
| 43 | import android.security.keystore.KeyNotYetValidException; |
| 44 | import android.security.keystore.KeyPermanentlyInvalidatedException; |
Janis Danisevskis | 1864c95 | 2018-08-09 11:14:49 -0700 | [diff] [blame] | 45 | import android.security.keystore.KeyProperties; |
| 46 | import android.security.keystore.KeyProtection; |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 47 | import android.security.keystore.KeystoreResponse; |
Frank Salim | ea5e038 | 2018-01-23 22:42:29 -0800 | [diff] [blame] | 48 | import android.security.keystore.StrongBoxUnavailableException; |
Alex Klyubin | 3f8d4d8 | 2015-05-13 09:15:00 -0700 | [diff] [blame] | 49 | import android.security.keystore.UserNotAuthenticatedException; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 50 | import android.util.Log; |
Janis Danisevskis | 1864c95 | 2018-08-09 11:14:49 -0700 | [diff] [blame] | 51 | import com.android.org.bouncycastle.asn1.ASN1InputStream; |
| 52 | import com.android.org.bouncycastle.asn1.pkcs.PrivateKeyInfo; |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 53 | import java.math.BigInteger; |
Janis Danisevskis | 1864c95 | 2018-08-09 11:14:49 -0700 | [diff] [blame] | 54 | import java.io.ByteArrayInputStream; |
| 55 | import java.io.IOException; |
Alex Klyubin | ad9ba10 | 2015-04-21 15:17:24 -0700 | [diff] [blame] | 56 | import java.security.InvalidKeyException; |
Rob Barnes | f1a678e | 2018-11-13 15:57:22 -0700 | [diff] [blame] | 57 | import java.util.Arrays; |
Janis Danisevskis | 2b106ad | 2018-11-15 09:27:16 -0800 | [diff] [blame] | 58 | import java.util.Date; |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 59 | import java.util.List; |
Kenny Root | b91773b | 2013-09-05 13:03:16 -0700 | [diff] [blame] | 60 | import java.util.Locale; |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 61 | import java.util.concurrent.CompletableFuture; |
| 62 | import java.util.concurrent.ExecutionException; |
Janis Danisevskis | 1864c95 | 2018-08-09 11:14:49 -0700 | [diff] [blame] | 63 | import sun.security.util.ObjectIdentifier; |
| 64 | import sun.security.x509.AlgorithmId; |
Kenny Root | b91773b | 2013-09-05 13:03:16 -0700 | [diff] [blame] | 65 | |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 66 | /** |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 67 | * @hide This should not be made public in its present form because it |
| 68 | * assumes that private and secret key bytes are available and would |
| 69 | * preclude the use of hardware crypto. |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 70 | */ |
| 71 | public class KeyStore { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 72 | private static final String TAG = "KeyStore"; |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 73 | |
Dmitry Dementyev | efc4311 | 2017-10-27 23:10:28 -0700 | [diff] [blame] | 74 | // ResponseCodes - see system/security/keystore/include/keystore/keystore.h |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 75 | @UnsupportedAppUsage |
Brian Carlstrom | 7e4b1a4 | 2011-06-01 15:29:29 -0700 | [diff] [blame] | 76 | public static final int NO_ERROR = 1; |
| 77 | public static final int LOCKED = 2; |
| 78 | public static final int UNINITIALIZED = 3; |
| 79 | public static final int SYSTEM_ERROR = 4; |
| 80 | public static final int PROTOCOL_ERROR = 5; |
| 81 | public static final int PERMISSION_DENIED = 6; |
| 82 | public static final int KEY_NOT_FOUND = 7; |
| 83 | public static final int VALUE_CORRUPTED = 8; |
| 84 | public static final int UNDEFINED_ACTION = 9; |
| 85 | public static final int WRONG_PASSWORD = 10; |
Janis Danisevskis | d257538 | 2018-10-08 07:56:58 -0700 | [diff] [blame] | 86 | public static final int KEY_ALREADY_EXISTS = 16; |
Eran Messeri | 6169239 | 2018-03-26 16:43:14 +0100 | [diff] [blame] | 87 | public static final int CANNOT_ATTEST_IDS = -66; |
Frank Salim | ea5e038 | 2018-01-23 22:42:29 -0800 | [diff] [blame] | 88 | public static final int HARDWARE_TYPE_UNAVAILABLE = -68; |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 89 | |
Chad Brubaker | 560d653 | 2015-04-24 10:32:18 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Per operation authentication is needed before this operation is valid. |
| 92 | * This is returned from {@link #begin} when begin succeeds but the operation uses |
| 93 | * per-operation authentication and must authenticate before calling {@link #update} or |
| 94 | * {@link #finish}. |
| 95 | */ |
| 96 | public static final int OP_AUTH_NEEDED = 15; |
| 97 | |
Max Bires | b2cc3dc | 2018-11-02 10:50:40 -0700 | [diff] [blame^] | 98 | // Used when a user changes their pin, invalidating old auth bound keys. |
| 99 | public static final int KEY_PERMANENTLY_INVALIDATED = 17; |
| 100 | |
Kenny Root | 2eeda72 | 2013-04-10 11:30:58 -0700 | [diff] [blame] | 101 | // Used for UID field to indicate the calling UID. |
| 102 | public static final int UID_SELF = -1; |
| 103 | |
| 104 | // Flags for "put" "import" and "generate" |
| 105 | public static final int FLAG_NONE = 0; |
Alex Klyubin | 5418393 | 2015-05-08 15:25:48 -0700 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Indicates that this key (or key pair) must be encrypted at rest. This will protect the key |
| 109 | * (or key pair) with the secure lock screen credential (e.g., password, PIN, or pattern). |
| 110 | * |
| 111 | * <p>Note that this requires that the secure lock screen (e.g., password, PIN, pattern) is set |
| 112 | * up, otherwise key (or key pair) generation or import will fail. Moreover, this key (or key |
| 113 | * pair) will be deleted when the secure lock screen is disabled or reset (e.g., by the user or |
| 114 | * a Device Administrator). Finally, this key (or key pair) cannot be used until the user |
| 115 | * unlocks the secure lock screen after boot. |
| 116 | * |
| 117 | * @see KeyguardManager#isDeviceSecure() |
| 118 | */ |
Kenny Root | a3788b0 | 2013-04-10 10:37:55 -0700 | [diff] [blame] | 119 | public static final int FLAG_ENCRYPTED = 1; |
| 120 | |
Rubin Xu | 12b644d | 2017-04-21 19:21:42 +0100 | [diff] [blame] | 121 | /** |
Janis Danisevskis | 0aadf93 | 2017-12-18 17:28:52 -0800 | [diff] [blame] | 122 | * Select Software keymaster device, which as of this writing is the lowest security |
| 123 | * level available on an android device. If neither FLAG_STRONGBOX nor FLAG_SOFTWARE is provided |
| 124 | * A TEE based keymaster implementation is implied. |
| 125 | * |
| 126 | * Need to be in sync with KeyStoreFlag in system/security/keystore/include/keystore/keystore.h |
| 127 | * For historical reasons this corresponds to the KEYSTORE_FLAG_FALLBACK flag. |
| 128 | */ |
| 129 | public static final int FLAG_SOFTWARE = 1 << 1; |
| 130 | |
| 131 | /** |
Rubin Xu | 12b644d | 2017-04-21 19:21:42 +0100 | [diff] [blame] | 132 | * A private flag that's only available to system server to indicate that this key is part of |
| 133 | * device encryption flow so it receives special treatment from keystore. For example this key |
| 134 | * will not be super encrypted, and it will be stored separately under an unique UID instead |
| 135 | * of the caller UID i.e. SYSTEM. |
| 136 | * |
| 137 | * Need to be in sync with KeyStoreFlag in system/security/keystore/include/keystore/keystore.h |
| 138 | */ |
| 139 | public static final int FLAG_CRITICAL_TO_DEVICE_ENCRYPTION = 1 << 3; |
| 140 | |
Janis Danisevskis | 0aadf93 | 2017-12-18 17:28:52 -0800 | [diff] [blame] | 141 | /** |
| 142 | * Select Strongbox keymaster device, which as of this writing the the highest security level |
| 143 | * available an android devices. If neither FLAG_STRONGBOX nor FLAG_SOFTWARE is provided |
| 144 | * A TEE based keymaster implementation is implied. |
| 145 | * |
| 146 | * Need to be in sync with KeyStoreFlag in system/security/keystore/include/keystore/keystore.h |
| 147 | */ |
| 148 | public static final int FLAG_STRONGBOX = 1 << 4; |
| 149 | |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 150 | // States |
Mathew Inwood | efb4816 | 2018-08-01 10:24:49 +0100 | [diff] [blame] | 151 | public enum State { |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 152 | @UnsupportedAppUsage |
Mathew Inwood | efb4816 | 2018-08-01 10:24:49 +0100 | [diff] [blame] | 153 | UNLOCKED, |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 154 | @UnsupportedAppUsage |
Mathew Inwood | efb4816 | 2018-08-01 10:24:49 +0100 | [diff] [blame] | 155 | LOCKED, |
| 156 | UNINITIALIZED |
| 157 | }; |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 158 | |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 159 | private int mError = NO_ERROR; |
| 160 | |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 161 | private final IKeystoreService mBinder; |
Alex Klyubin | 2d7a85c | 2015-04-30 11:43:53 -0700 | [diff] [blame] | 162 | private final Context mContext; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 163 | |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 164 | private IBinder mToken; |
| 165 | |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 166 | private KeyStore(IKeystoreService binder) { |
| 167 | mBinder = binder; |
Alex Klyubin | 3f8d4d8 | 2015-05-13 09:15:00 -0700 | [diff] [blame] | 168 | mContext = getApplicationContext(); |
Alex Klyubin | 2d7a85c | 2015-04-30 11:43:53 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 171 | @UnsupportedAppUsage |
Alex Klyubin | dcdaf87 | 2015-05-13 15:57:09 -0700 | [diff] [blame] | 172 | public static Context getApplicationContext() { |
Alex Klyubin | a99b8b5 | 2015-06-11 13:27:34 -0700 | [diff] [blame] | 173 | Application application = ActivityThread.currentApplication(); |
Alex Klyubin | 2d7a85c | 2015-04-30 11:43:53 -0700 | [diff] [blame] | 174 | if (application == null) { |
| 175 | throw new IllegalStateException( |
Alex Klyubin | a99b8b5 | 2015-06-11 13:27:34 -0700 | [diff] [blame] | 176 | "Failed to obtain application Context from ActivityThread"); |
Alex Klyubin | 2d7a85c | 2015-04-30 11:43:53 -0700 | [diff] [blame] | 177 | } |
| 178 | return application; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 179 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 180 | |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 181 | @UnsupportedAppUsage |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 182 | public static KeyStore getInstance() { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 183 | IKeystoreService keystore = IKeystoreService.Stub.asInterface(ServiceManager |
| 184 | .getService("android.security.keystore")); |
| 185 | return new KeyStore(keystore); |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 186 | } |
| 187 | |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 188 | private synchronized IBinder getToken() { |
| 189 | if (mToken == null) { |
| 190 | mToken = new Binder(); |
| 191 | } |
| 192 | return mToken; |
| 193 | } |
| 194 | |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 195 | @UnsupportedAppUsage |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 196 | public State state(int userId) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 197 | final int ret; |
| 198 | try { |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 199 | ret = mBinder.getState(userId); |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 200 | } catch (RemoteException e) { |
| 201 | Log.w(TAG, "Cannot connect to keystore", e); |
| 202 | throw new AssertionError(e); |
| 203 | } |
| 204 | |
| 205 | switch (ret) { |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 206 | case NO_ERROR: return State.UNLOCKED; |
| 207 | case LOCKED: return State.LOCKED; |
| 208 | case UNINITIALIZED: return State.UNINITIALIZED; |
| 209 | default: throw new AssertionError(mError); |
| 210 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 211 | } |
| 212 | |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 213 | @UnsupportedAppUsage |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 214 | public State state() { |
| 215 | return state(UserHandle.myUserId()); |
| 216 | } |
| 217 | |
Kenny Root | b9594ce | 2013-02-14 10:18:38 -0800 | [diff] [blame] | 218 | public boolean isUnlocked() { |
| 219 | return state() == State.UNLOCKED; |
| 220 | } |
| 221 | |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame] | 222 | public byte[] get(String key, int uid) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 223 | try { |
Dmitry Dementyev | efc4311 | 2017-10-27 23:10:28 -0700 | [diff] [blame] | 224 | key = key != null ? key : ""; |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame] | 225 | return mBinder.get(key, uid); |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 226 | } catch (RemoteException e) { |
| 227 | Log.w(TAG, "Cannot connect to keystore", e); |
| 228 | return null; |
Dmitry Dementyev | efc4311 | 2017-10-27 23:10:28 -0700 | [diff] [blame] | 229 | } catch (android.os.ServiceSpecificException e) { |
| 230 | Log.w(TAG, "KeyStore exception", e); |
| 231 | return null; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 232 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 233 | } |
| 234 | |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 235 | @UnsupportedAppUsage |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame] | 236 | public byte[] get(String key) { |
| 237 | return get(key, UID_SELF); |
| 238 | } |
| 239 | |
Kenny Root | a3788b0 | 2013-04-10 10:37:55 -0700 | [diff] [blame] | 240 | public boolean put(String key, byte[] value, int uid, int flags) { |
Alex Klyubin | 3ceb1a0 | 2015-06-05 15:51:06 -0700 | [diff] [blame] | 241 | return insert(key, value, uid, flags) == NO_ERROR; |
| 242 | } |
| 243 | |
| 244 | public int insert(String key, byte[] value, int uid, int flags) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 245 | try { |
Dmitry Dementyev | efc4311 | 2017-10-27 23:10:28 -0700 | [diff] [blame] | 246 | if (value == null) { |
| 247 | value = new byte[0]; |
| 248 | } |
Janis Danisevskis | d257538 | 2018-10-08 07:56:58 -0700 | [diff] [blame] | 249 | int error = mBinder.insert(key, value, uid, flags); |
| 250 | if (error == KEY_ALREADY_EXISTS) { |
| 251 | mBinder.del(key, uid); |
| 252 | error = mBinder.insert(key, value, uid, flags); |
| 253 | } |
| 254 | return error; |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 255 | } catch (RemoteException e) { |
| 256 | Log.w(TAG, "Cannot connect to keystore", e); |
Alex Klyubin | 3ceb1a0 | 2015-06-05 15:51:06 -0700 | [diff] [blame] | 257 | return SYSTEM_ERROR; |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Janis Danisevskis | 906147c | 2018-11-06 14:14:05 -0800 | [diff] [blame] | 261 | int delete2(String key, int uid) { |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 262 | try { |
Janis Danisevskis | 906147c | 2018-11-06 14:14:05 -0800 | [diff] [blame] | 263 | return mBinder.del(key, uid); |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 264 | } catch (RemoteException e) { |
| 265 | Log.w(TAG, "Cannot connect to keystore", e); |
Janis Danisevskis | 906147c | 2018-11-06 14:14:05 -0800 | [diff] [blame] | 266 | return SYSTEM_ERROR; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 267 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 268 | } |
| 269 | |
Janis Danisevskis | 906147c | 2018-11-06 14:14:05 -0800 | [diff] [blame] | 270 | public boolean delete(String key, int uid) { |
| 271 | int ret = delete2(key, uid); |
| 272 | return ret == NO_ERROR || ret == KEY_NOT_FOUND; |
| 273 | } |
| 274 | |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 275 | @UnsupportedAppUsage |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 276 | public boolean delete(String key) { |
Kenny Root | 2eeda72 | 2013-04-10 11:30:58 -0700 | [diff] [blame] | 277 | return delete(key, UID_SELF); |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | public boolean contains(String key, int uid) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 281 | try { |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 282 | return mBinder.exist(key, uid) == NO_ERROR; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 283 | } catch (RemoteException e) { |
| 284 | Log.w(TAG, "Cannot connect to keystore", e); |
| 285 | return false; |
| 286 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | public boolean contains(String key) { |
Kenny Root | 2eeda72 | 2013-04-10 11:30:58 -0700 | [diff] [blame] | 290 | return contains(key, UID_SELF); |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 291 | } |
| 292 | |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 293 | /** |
| 294 | * List all entries in the keystore for {@code uid} starting with {@code prefix}. |
| 295 | */ |
| 296 | public String[] list(String prefix, int uid) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 297 | try { |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 298 | return mBinder.list(prefix, uid); |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 299 | } catch (RemoteException e) { |
| 300 | Log.w(TAG, "Cannot connect to keystore", e); |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 301 | return null; |
Dmitry Dementyev | efc4311 | 2017-10-27 23:10:28 -0700 | [diff] [blame] | 302 | } catch (android.os.ServiceSpecificException e) { |
| 303 | Log.w(TAG, "KeyStore exception", e); |
| 304 | return null; |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 305 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 306 | } |
| 307 | |
Rob Barnes | f1a678e | 2018-11-13 15:57:22 -0700 | [diff] [blame] | 308 | /** |
| 309 | * List uids of all keys that are auth bound to the current user. |
| 310 | * Only system is allowed to call this method. |
| 311 | */ |
| 312 | @UnsupportedAppUsage |
| 313 | public int[] listUidsOfAuthBoundKeys() { |
| 314 | final int MAX_RESULT_SIZE = 100; |
| 315 | int[] uidsOut = new int[MAX_RESULT_SIZE]; |
| 316 | try { |
| 317 | int rc = mBinder.listUidsOfAuthBoundKeys(uidsOut); |
| 318 | if (rc != NO_ERROR) { |
| 319 | Log.w(TAG, String.format("listUidsOfAuthBoundKeys failed with error code %d", rc)); |
| 320 | return null; |
| 321 | } |
| 322 | } catch (RemoteException e) { |
| 323 | Log.w(TAG, "Cannot connect to keystore", e); |
| 324 | return null; |
| 325 | } catch (android.os.ServiceSpecificException e) { |
| 326 | Log.w(TAG, "KeyStore exception", e); |
| 327 | return null; |
| 328 | } |
| 329 | // Remove any 0 entries |
| 330 | return Arrays.stream(uidsOut).filter(x -> x > 0).toArray(); |
| 331 | } |
| 332 | |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 333 | public String[] list(String prefix) { |
| 334 | return list(prefix, UID_SELF); |
| 335 | } |
| 336 | |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 337 | @UnsupportedAppUsage |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 338 | public boolean reset() { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 339 | try { |
| 340 | return mBinder.reset() == NO_ERROR; |
| 341 | } catch (RemoteException e) { |
| 342 | Log.w(TAG, "Cannot connect to keystore", e); |
| 343 | return false; |
| 344 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 345 | } |
| 346 | |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 347 | /** |
| 348 | * Attempt to lock the keystore for {@code user}. |
| 349 | * |
Brian Young | 36716eb | 2018-02-23 18:04:20 +0000 | [diff] [blame] | 350 | * @param userId Android user to lock. |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 351 | * @return whether {@code user}'s keystore was locked. |
| 352 | */ |
| 353 | public boolean lock(int userId) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 354 | try { |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 355 | return mBinder.lock(userId) == NO_ERROR; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 356 | } catch (RemoteException e) { |
| 357 | Log.w(TAG, "Cannot connect to keystore", e); |
| 358 | return false; |
| 359 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 360 | } |
| 361 | |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 362 | public boolean lock() { |
| 363 | return lock(UserHandle.myUserId()); |
| 364 | } |
| 365 | |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 366 | /** |
| 367 | * Attempt to unlock the keystore for {@code user} with the password {@code password}. |
| 368 | * This is required before keystore entries created with FLAG_ENCRYPTED can be accessed or |
| 369 | * created. |
| 370 | * |
Brian Young | 36716eb | 2018-02-23 18:04:20 +0000 | [diff] [blame] | 371 | * @param userId Android user ID to operate on |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 372 | * @param password user's keystore password. Should be the most recent value passed to |
| 373 | * {@link #onUserPasswordChanged} for the user. |
| 374 | * |
| 375 | * @return whether the keystore was unlocked. |
| 376 | */ |
| 377 | public boolean unlock(int userId, String password) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 378 | try { |
Dmitry Dementyev | efc4311 | 2017-10-27 23:10:28 -0700 | [diff] [blame] | 379 | password = password != null ? password : ""; |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 380 | mError = mBinder.unlock(userId, password); |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 381 | return mError == NO_ERROR; |
| 382 | } catch (RemoteException e) { |
| 383 | Log.w(TAG, "Cannot connect to keystore", e); |
| 384 | return false; |
| 385 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 386 | } |
| 387 | |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 388 | @UnsupportedAppUsage |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 389 | public boolean unlock(String password) { |
| 390 | return unlock(UserHandle.getUserId(Process.myUid()), password); |
| 391 | } |
| 392 | |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 393 | /** |
| 394 | * Check if the keystore for {@code userId} is empty. |
| 395 | */ |
| 396 | public boolean isEmpty(int userId) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 397 | try { |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 398 | return mBinder.isEmpty(userId) != 0; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 399 | } catch (RemoteException e) { |
| 400 | Log.w(TAG, "Cannot connect to keystore", e); |
| 401 | return false; |
| 402 | } |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 405 | @UnsupportedAppUsage |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 406 | public boolean isEmpty() { |
| 407 | return isEmpty(UserHandle.myUserId()); |
| 408 | } |
| 409 | |
Janis Danisevskis | b50e9f6 | 2017-06-08 17:53:34 -0700 | [diff] [blame] | 410 | public String grant(String key, int uid) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 411 | try { |
Janis Danisevskis | b50e9f6 | 2017-06-08 17:53:34 -0700 | [diff] [blame] | 412 | String grantAlias = mBinder.grant(key, uid); |
| 413 | if (grantAlias == "") return null; |
| 414 | return grantAlias; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 415 | } catch (RemoteException e) { |
| 416 | Log.w(TAG, "Cannot connect to keystore", e); |
Janis Danisevskis | b50e9f6 | 2017-06-08 17:53:34 -0700 | [diff] [blame] | 417 | return null; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 418 | } |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | public boolean ungrant(String key, int uid) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 422 | try { |
| 423 | return mBinder.ungrant(key, uid) == NO_ERROR; |
| 424 | } catch (RemoteException e) { |
| 425 | Log.w(TAG, "Cannot connect to keystore", e); |
| 426 | return false; |
Kenny Root | 473c712 | 2012-08-17 21:13:48 -0700 | [diff] [blame] | 427 | } |
Kenny Root | 473c712 | 2012-08-17 21:13:48 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Returns the last modification time of the key in milliseconds since the |
| 432 | * epoch. Will return -1L if the key could not be found or other error. |
| 433 | */ |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame] | 434 | public long getmtime(String key, int uid) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 435 | try { |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame] | 436 | final long millis = mBinder.getmtime(key, uid); |
Kenny Root | e66769a | 2013-02-04 15:49:11 -0800 | [diff] [blame] | 437 | if (millis == -1L) { |
| 438 | return -1L; |
| 439 | } |
| 440 | |
| 441 | return millis * 1000L; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 442 | } catch (RemoteException e) { |
| 443 | Log.w(TAG, "Cannot connect to keystore", e); |
| 444 | return -1L; |
| 445 | } |
Kenny Root | 473c712 | 2012-08-17 21:13:48 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame] | 448 | public long getmtime(String key) { |
| 449 | return getmtime(key, UID_SELF); |
| 450 | } |
| 451 | |
Alex Klyubin | 469cbf5 | 2015-06-04 12:36:27 -0700 | [diff] [blame] | 452 | // TODO: remove this when it's removed from Settings |
Kenny Root | 5cb5cec | 2013-03-29 11:14:17 -0700 | [diff] [blame] | 453 | public boolean isHardwareBacked() { |
Kenny Root | b91773b | 2013-09-05 13:03:16 -0700 | [diff] [blame] | 454 | return isHardwareBacked("RSA"); |
| 455 | } |
| 456 | |
| 457 | public boolean isHardwareBacked(String keyType) { |
Kenny Root | 5cb5cec | 2013-03-29 11:14:17 -0700 | [diff] [blame] | 458 | try { |
Kenny Root | b91773b | 2013-09-05 13:03:16 -0700 | [diff] [blame] | 459 | return mBinder.is_hardware_backed(keyType.toUpperCase(Locale.US)) == NO_ERROR; |
Kenny Root | 5cb5cec | 2013-03-29 11:14:17 -0700 | [diff] [blame] | 460 | } catch (RemoteException e) { |
| 461 | Log.w(TAG, "Cannot connect to keystore", e); |
| 462 | return false; |
| 463 | } |
| 464 | } |
| 465 | |
Kenny Root | d72317a | 2013-04-01 15:59:59 -0700 | [diff] [blame] | 466 | public boolean clearUid(int uid) { |
| 467 | try { |
| 468 | return mBinder.clear_uid(uid) == NO_ERROR; |
| 469 | } catch (RemoteException e) { |
| 470 | Log.w(TAG, "Cannot connect to keystore", e); |
| 471 | return false; |
| 472 | } |
| 473 | } |
| 474 | |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 475 | public int getLastError() { |
| 476 | return mError; |
| 477 | } |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 478 | |
Janis Danisevskis | 0aadf93 | 2017-12-18 17:28:52 -0800 | [diff] [blame] | 479 | public boolean addRngEntropy(byte[] data, int flags) { |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 480 | try { |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 481 | KeystoreResultPromise promise = new KeystoreResultPromise(); |
| 482 | int errorCode = mBinder.addRngEntropy(promise, data, flags); |
| 483 | if (errorCode == NO_ERROR) { |
| 484 | return promise.getFuture().get().getErrorCode() == NO_ERROR; |
| 485 | } else { |
| 486 | return false; |
| 487 | } |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 488 | } catch (RemoteException e) { |
| 489 | Log.w(TAG, "Cannot connect to keystore", e); |
| 490 | return false; |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 491 | } catch (ExecutionException | InterruptedException e) { |
| 492 | Log.e(TAG, "AddRngEntropy completed with exception", e); |
| 493 | return false; |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 497 | private class KeyCharacteristicsCallbackResult { |
| 498 | private KeystoreResponse keystoreResponse; |
| 499 | private KeyCharacteristics keyCharacteristics; |
| 500 | |
| 501 | public KeyCharacteristicsCallbackResult(KeystoreResponse keystoreResponse, |
| 502 | KeyCharacteristics keyCharacteristics) { |
| 503 | this.keystoreResponse = keystoreResponse; |
| 504 | this.keyCharacteristics = keyCharacteristics; |
| 505 | } |
| 506 | |
| 507 | public KeystoreResponse getKeystoreResponse() { |
| 508 | return keystoreResponse; |
| 509 | } |
| 510 | |
| 511 | public void setKeystoreResponse(KeystoreResponse keystoreResponse) { |
| 512 | this.keystoreResponse = keystoreResponse; |
| 513 | } |
| 514 | |
| 515 | public KeyCharacteristics getKeyCharacteristics() { |
| 516 | return keyCharacteristics; |
| 517 | } |
| 518 | |
| 519 | public void setKeyCharacteristics(KeyCharacteristics keyCharacteristics) { |
| 520 | this.keyCharacteristics = keyCharacteristics; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | private class KeyCharacteristicsPromise |
| 525 | extends android.security.keystore.IKeystoreKeyCharacteristicsCallback.Stub { |
| 526 | final private CompletableFuture<KeyCharacteristicsCallbackResult> future = |
| 527 | new CompletableFuture<KeyCharacteristicsCallbackResult>(); |
| 528 | @Override |
| 529 | public void onFinished(KeystoreResponse keystoreResponse, |
| 530 | KeyCharacteristics keyCharacteristics) |
| 531 | throws android.os.RemoteException { |
| 532 | future.complete( |
| 533 | new KeyCharacteristicsCallbackResult(keystoreResponse, keyCharacteristics)); |
| 534 | } |
| 535 | public final CompletableFuture<KeyCharacteristicsCallbackResult> getFuture() { |
| 536 | return future; |
| 537 | } |
| 538 | }; |
| 539 | |
| 540 | private int generateKeyInternal(String alias, KeymasterArguments args, byte[] entropy, int uid, |
| 541 | int flags, KeyCharacteristics outCharacteristics) |
| 542 | throws RemoteException, ExecutionException, InterruptedException { |
| 543 | KeyCharacteristicsPromise promise = new KeyCharacteristicsPromise(); |
| 544 | int error = mBinder.generateKey(promise, alias, args, entropy, uid, flags); |
| 545 | if (error != NO_ERROR) { |
| 546 | Log.e(TAG, "generateKeyInternal failed on request " + error); |
| 547 | return error; |
| 548 | } |
| 549 | |
| 550 | KeyCharacteristicsCallbackResult result = promise.getFuture().get(); |
| 551 | error = result.getKeystoreResponse().getErrorCode(); |
| 552 | if (error != NO_ERROR) { |
| 553 | Log.e(TAG, "generateKeyInternal failed on response " + error); |
| 554 | return error; |
| 555 | } |
| 556 | KeyCharacteristics characteristics = result.getKeyCharacteristics(); |
| 557 | if (characteristics == null) { |
| 558 | Log.e(TAG, "generateKeyInternal got empty key cheractariestics " + error); |
| 559 | return SYSTEM_ERROR; |
| 560 | } |
| 561 | outCharacteristics.shallowCopyFrom(characteristics); |
| 562 | return NO_ERROR; |
| 563 | } |
| 564 | |
Chad Brubaker | dae79e5 | 2015-03-27 14:28:35 -0700 | [diff] [blame] | 565 | public int generateKey(String alias, KeymasterArguments args, byte[] entropy, int uid, |
| 566 | int flags, KeyCharacteristics outCharacteristics) { |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 567 | try { |
Dmitry Dementyev | efc4311 | 2017-10-27 23:10:28 -0700 | [diff] [blame] | 568 | entropy = entropy != null ? entropy : new byte[0]; |
| 569 | args = args != null ? args : new KeymasterArguments(); |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 570 | int error = generateKeyInternal(alias, args, entropy, uid, flags, outCharacteristics); |
Janis Danisevskis | d257538 | 2018-10-08 07:56:58 -0700 | [diff] [blame] | 571 | if (error == KEY_ALREADY_EXISTS) { |
| 572 | mBinder.del(alias, uid); |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 573 | error = generateKeyInternal(alias, args, entropy, uid, flags, outCharacteristics); |
Janis Danisevskis | d257538 | 2018-10-08 07:56:58 -0700 | [diff] [blame] | 574 | } |
| 575 | return error; |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 576 | } catch (RemoteException e) { |
| 577 | Log.w(TAG, "Cannot connect to keystore", e); |
| 578 | return SYSTEM_ERROR; |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 579 | } catch (ExecutionException | InterruptedException e) { |
| 580 | Log.e(TAG, "generateKey completed with exception", e); |
| 581 | return SYSTEM_ERROR; |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
Chad Brubaker | dae79e5 | 2015-03-27 14:28:35 -0700 | [diff] [blame] | 585 | public int generateKey(String alias, KeymasterArguments args, byte[] entropy, int flags, |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 586 | KeyCharacteristics outCharacteristics) { |
Chad Brubaker | dae79e5 | 2015-03-27 14:28:35 -0700 | [diff] [blame] | 587 | return generateKey(alias, args, entropy, UID_SELF, flags, outCharacteristics); |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 588 | } |
| 589 | |
Chad Brubaker | 5e73c0e | 2015-03-21 22:46:43 -0700 | [diff] [blame] | 590 | public int getKeyCharacteristics(String alias, KeymasterBlob clientId, KeymasterBlob appId, |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame] | 591 | int uid, KeyCharacteristics outCharacteristics) { |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 592 | try { |
Dmitry Dementyev | efc4311 | 2017-10-27 23:10:28 -0700 | [diff] [blame] | 593 | clientId = clientId != null ? clientId : new KeymasterBlob(new byte[0]); |
| 594 | appId = appId != null ? appId : new KeymasterBlob(new byte[0]); |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 595 | KeyCharacteristicsPromise promise = new KeyCharacteristicsPromise(); |
| 596 | int error = mBinder.getKeyCharacteristics(promise, alias, clientId, appId, uid); |
| 597 | if (error != NO_ERROR) return error; |
| 598 | |
| 599 | KeyCharacteristicsCallbackResult result = promise.getFuture().get(); |
| 600 | error = result.getKeystoreResponse().getErrorCode(); |
| 601 | if (error != NO_ERROR) return error; |
| 602 | |
| 603 | KeyCharacteristics characteristics = result.getKeyCharacteristics(); |
| 604 | if (characteristics == null) return SYSTEM_ERROR; |
| 605 | outCharacteristics.shallowCopyFrom(characteristics); |
| 606 | return NO_ERROR; |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 607 | } catch (RemoteException e) { |
| 608 | Log.w(TAG, "Cannot connect to keystore", e); |
| 609 | return SYSTEM_ERROR; |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 610 | } catch (ExecutionException | InterruptedException e) { |
| 611 | Log.e(TAG, "GetKeyCharacteristics completed with exception", e); |
| 612 | return SYSTEM_ERROR; |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame] | 616 | public int getKeyCharacteristics(String alias, KeymasterBlob clientId, KeymasterBlob appId, |
| 617 | KeyCharacteristics outCharacteristics) { |
| 618 | return getKeyCharacteristics(alias, clientId, appId, UID_SELF, outCharacteristics); |
| 619 | } |
| 620 | |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 621 | private int importKeyInternal(String alias, KeymasterArguments args, int format, byte[] keyData, |
| 622 | int uid, int flags, KeyCharacteristics outCharacteristics) |
| 623 | throws RemoteException, ExecutionException, InterruptedException { |
| 624 | KeyCharacteristicsPromise promise = new KeyCharacteristicsPromise(); |
| 625 | int error = mBinder.importKey(promise, alias, args, format, keyData, uid, flags); |
| 626 | if (error != NO_ERROR) return error; |
| 627 | |
| 628 | KeyCharacteristicsCallbackResult result = promise.getFuture().get(); |
| 629 | error = result.getKeystoreResponse().getErrorCode(); |
| 630 | if (error != NO_ERROR) return error; |
| 631 | |
| 632 | KeyCharacteristics characteristics = result.getKeyCharacteristics(); |
| 633 | if (characteristics == null) return SYSTEM_ERROR; |
| 634 | outCharacteristics.shallowCopyFrom(characteristics); |
| 635 | return NO_ERROR; |
| 636 | } |
| 637 | |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 638 | public int importKey(String alias, KeymasterArguments args, int format, byte[] keyData, |
| 639 | int uid, int flags, KeyCharacteristics outCharacteristics) { |
| 640 | try { |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 641 | int error = importKeyInternal(alias, args, format, keyData, uid, flags, |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 642 | outCharacteristics); |
Janis Danisevskis | d257538 | 2018-10-08 07:56:58 -0700 | [diff] [blame] | 643 | if (error == KEY_ALREADY_EXISTS) { |
| 644 | mBinder.del(alias, uid); |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 645 | error = importKeyInternal(alias, args, format, keyData, uid, flags, |
Janis Danisevskis | d257538 | 2018-10-08 07:56:58 -0700 | [diff] [blame] | 646 | outCharacteristics); |
| 647 | } |
| 648 | return error; |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 649 | } catch (RemoteException e) { |
| 650 | Log.w(TAG, "Cannot connect to keystore", e); |
| 651 | return SYSTEM_ERROR; |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 652 | } catch (ExecutionException | InterruptedException e) { |
| 653 | Log.e(TAG, "ImportKey completed with exception", e); |
| 654 | return SYSTEM_ERROR; |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
| 658 | public int importKey(String alias, KeymasterArguments args, int format, byte[] keyData, |
| 659 | int flags, KeyCharacteristics outCharacteristics) { |
| 660 | return importKey(alias, args, format, keyData, UID_SELF, flags, outCharacteristics); |
| 661 | } |
| 662 | |
Janis Danisevskis | 1864c95 | 2018-08-09 11:14:49 -0700 | [diff] [blame] | 663 | private String getAlgorithmFromPKCS8(byte[] keyData) { |
| 664 | try { |
| 665 | final ASN1InputStream bIn = new ASN1InputStream(new ByteArrayInputStream(keyData)); |
| 666 | final PrivateKeyInfo pki = PrivateKeyInfo.getInstance(bIn.readObject()); |
| 667 | final String algOid = pki.getPrivateKeyAlgorithm().getAlgorithm().getId(); |
| 668 | return new AlgorithmId(new ObjectIdentifier(algOid)).getName(); |
| 669 | } catch (IOException e) { |
| 670 | Log.e(TAG, "getAlgorithmFromPKCS8 Failed to parse key data"); |
| 671 | Log.e(TAG, Log.getStackTraceString(e)); |
| 672 | return null; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | private KeymasterArguments makeLegacyArguments(String algorithm) { |
| 677 | KeymasterArguments args = new KeymasterArguments(); |
| 678 | args.addEnum(KeymasterDefs.KM_TAG_ALGORITHM, |
| 679 | KeyProperties.KeyAlgorithm.toKeymasterAsymmetricKeyAlgorithm(algorithm)); |
| 680 | args.addEnum(KeymasterDefs.KM_TAG_PURPOSE, KeymasterDefs.KM_PURPOSE_SIGN); |
| 681 | args.addEnum(KeymasterDefs.KM_TAG_PURPOSE, KeymasterDefs.KM_PURPOSE_VERIFY); |
| 682 | args.addEnum(KeymasterDefs.KM_TAG_PURPOSE, KeymasterDefs.KM_PURPOSE_ENCRYPT); |
| 683 | args.addEnum(KeymasterDefs.KM_TAG_PURPOSE, KeymasterDefs.KM_PURPOSE_DECRYPT); |
| 684 | args.addEnum(KeymasterDefs.KM_TAG_PADDING, KeymasterDefs.KM_PAD_NONE); |
| 685 | if (algorithm.equalsIgnoreCase(KeyProperties.KEY_ALGORITHM_RSA)) { |
| 686 | args.addEnum(KeymasterDefs.KM_TAG_PADDING, KeymasterDefs.KM_PAD_RSA_OAEP); |
| 687 | args.addEnum(KeymasterDefs.KM_TAG_PADDING, KeymasterDefs.KM_PAD_RSA_PKCS1_1_5_ENCRYPT); |
| 688 | args.addEnum(KeymasterDefs.KM_TAG_PADDING, KeymasterDefs.KM_PAD_RSA_PKCS1_1_5_SIGN); |
| 689 | args.addEnum(KeymasterDefs.KM_TAG_PADDING, KeymasterDefs.KM_PAD_RSA_PSS); |
| 690 | } |
| 691 | args.addEnum(KeymasterDefs.KM_TAG_DIGEST, KeymasterDefs.KM_DIGEST_NONE); |
| 692 | args.addEnum(KeymasterDefs.KM_TAG_DIGEST, KeymasterDefs.KM_DIGEST_MD5); |
| 693 | args.addEnum(KeymasterDefs.KM_TAG_DIGEST, KeymasterDefs.KM_DIGEST_SHA1); |
| 694 | args.addEnum(KeymasterDefs.KM_TAG_DIGEST, KeymasterDefs.KM_DIGEST_SHA_2_224); |
| 695 | args.addEnum(KeymasterDefs.KM_TAG_DIGEST, KeymasterDefs.KM_DIGEST_SHA_2_256); |
| 696 | args.addEnum(KeymasterDefs.KM_TAG_DIGEST, KeymasterDefs.KM_DIGEST_SHA_2_384); |
| 697 | args.addEnum(KeymasterDefs.KM_TAG_DIGEST, KeymasterDefs.KM_DIGEST_SHA_2_512); |
| 698 | args.addBoolean(KeymasterDefs.KM_TAG_NO_AUTH_REQUIRED); |
Janis Danisevskis | 2b106ad | 2018-11-15 09:27:16 -0800 | [diff] [blame] | 699 | args.addDate(KeymasterDefs.KM_TAG_ORIGINATION_EXPIRE_DATETIME, new Date(Long.MAX_VALUE)); |
| 700 | args.addDate(KeymasterDefs.KM_TAG_USAGE_EXPIRE_DATETIME, new Date(Long.MAX_VALUE)); |
| 701 | args.addDate(KeymasterDefs.KM_TAG_ACTIVE_DATETIME, new Date(0)); |
Janis Danisevskis | 1864c95 | 2018-08-09 11:14:49 -0700 | [diff] [blame] | 702 | return args; |
| 703 | } |
| 704 | |
| 705 | public boolean importKey(String alias, byte[] keyData, int uid, int flags) { |
| 706 | String algorithm = getAlgorithmFromPKCS8(keyData); |
| 707 | if (algorithm == null) return false; |
| 708 | KeymasterArguments args = makeLegacyArguments(algorithm); |
| 709 | KeyCharacteristics out = new KeyCharacteristics(); |
| 710 | int result = importKey(alias, args, KeymasterDefs.KM_KEY_FORMAT_PKCS8, keyData, uid, |
| 711 | flags, out); |
| 712 | if (result != NO_ERROR) { |
| 713 | Log.e(TAG, Log.getStackTraceString( |
| 714 | new KeyStoreException(result, "legacy key import failed"))); |
| 715 | return false; |
| 716 | } |
| 717 | return true; |
| 718 | } |
| 719 | |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 720 | private int importWrappedKeyInternal(String wrappedKeyAlias, byte[] wrappedKey, |
| 721 | String wrappingKeyAlias, |
| 722 | byte[] maskingKey, KeymasterArguments args, long rootSid, long fingerprintSid, |
| 723 | KeyCharacteristics outCharacteristics) |
| 724 | throws RemoteException, ExecutionException, InterruptedException { |
| 725 | KeyCharacteristicsPromise promise = new KeyCharacteristicsPromise(); |
| 726 | int error = mBinder.importWrappedKey(promise, wrappedKeyAlias, wrappedKey, wrappingKeyAlias, |
| 727 | maskingKey, args, rootSid, fingerprintSid); |
| 728 | if (error != NO_ERROR) return error; |
| 729 | |
| 730 | KeyCharacteristicsCallbackResult result = promise.getFuture().get(); |
| 731 | error = result.getKeystoreResponse().getErrorCode(); |
| 732 | if (error != NO_ERROR) return error; |
| 733 | |
| 734 | KeyCharacteristics characteristics = result.getKeyCharacteristics(); |
| 735 | if (characteristics == null) return SYSTEM_ERROR; |
| 736 | outCharacteristics.shallowCopyFrom(characteristics); |
| 737 | return NO_ERROR; |
| 738 | } |
| 739 | |
Frank Salim | 21d9c1d | 2017-12-19 22:38:09 -0800 | [diff] [blame] | 740 | public int importWrappedKey(String wrappedKeyAlias, byte[] wrappedKey, |
| 741 | String wrappingKeyAlias, |
| 742 | byte[] maskingKey, KeymasterArguments args, long rootSid, long fingerprintSid, int uid, |
| 743 | KeyCharacteristics outCharacteristics) { |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 744 | // TODO b/119217337 uid parameter gets silently ignored. |
Frank Salim | 21d9c1d | 2017-12-19 22:38:09 -0800 | [diff] [blame] | 745 | try { |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 746 | int error = importWrappedKeyInternal(wrappedKeyAlias, wrappedKey, wrappingKeyAlias, |
Frank Salim | 21d9c1d | 2017-12-19 22:38:09 -0800 | [diff] [blame] | 747 | maskingKey, args, rootSid, fingerprintSid, outCharacteristics); |
Janis Danisevskis | d257538 | 2018-10-08 07:56:58 -0700 | [diff] [blame] | 748 | if (error == KEY_ALREADY_EXISTS) { |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 749 | mBinder.del(wrappedKeyAlias, UID_SELF); |
| 750 | error = importWrappedKeyInternal(wrappedKeyAlias, wrappedKey, wrappingKeyAlias, |
Janis Danisevskis | d257538 | 2018-10-08 07:56:58 -0700 | [diff] [blame] | 751 | maskingKey, args, rootSid, fingerprintSid, outCharacteristics); |
| 752 | } |
| 753 | return error; |
Frank Salim | 21d9c1d | 2017-12-19 22:38:09 -0800 | [diff] [blame] | 754 | } catch (RemoteException e) { |
| 755 | Log.w(TAG, "Cannot connect to keystore", e); |
| 756 | return SYSTEM_ERROR; |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 757 | } catch (ExecutionException | InterruptedException e) { |
| 758 | Log.e(TAG, "ImportWrappedKey completed with exception", e); |
| 759 | return SYSTEM_ERROR; |
Frank Salim | 21d9c1d | 2017-12-19 22:38:09 -0800 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 763 | private class ExportKeyPromise |
| 764 | extends android.security.keystore.IKeystoreExportKeyCallback.Stub { |
| 765 | final private CompletableFuture<ExportResult> future = new CompletableFuture<ExportResult>(); |
| 766 | @Override |
| 767 | public void onFinished(ExportResult exportKeyResult) throws android.os.RemoteException { |
| 768 | future.complete(exportKeyResult); |
| 769 | } |
| 770 | public final CompletableFuture<ExportResult> getFuture() { |
| 771 | return future; |
| 772 | } |
| 773 | }; |
| 774 | |
Chad Brubaker | 5e73c0e | 2015-03-21 22:46:43 -0700 | [diff] [blame] | 775 | public ExportResult exportKey(String alias, int format, KeymasterBlob clientId, |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame] | 776 | KeymasterBlob appId, int uid) { |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 777 | try { |
Dmitry Dementyev | efc4311 | 2017-10-27 23:10:28 -0700 | [diff] [blame] | 778 | clientId = clientId != null ? clientId : new KeymasterBlob(new byte[0]); |
| 779 | appId = appId != null ? appId : new KeymasterBlob(new byte[0]); |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 780 | ExportKeyPromise promise = new ExportKeyPromise(); |
| 781 | int error = mBinder.exportKey(promise, alias, format, clientId, appId, uid); |
| 782 | if (error == NO_ERROR) { |
| 783 | return promise.getFuture().get(); |
| 784 | } else { |
| 785 | return new ExportResult(error); |
| 786 | } |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame] | 787 | } catch (RemoteException e) { |
| 788 | Log.w(TAG, "Cannot connect to keystore", e); |
| 789 | return null; |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 790 | } catch (ExecutionException | InterruptedException e) { |
| 791 | Log.e(TAG, "ExportKey completed with exception", e); |
| 792 | return null; |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | public ExportResult exportKey(String alias, int format, KeymasterBlob clientId, |
| 796 | KeymasterBlob appId) { |
| 797 | return exportKey(alias, format, clientId, appId, UID_SELF); |
| 798 | } |
| 799 | |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 800 | private class OperationPromise |
| 801 | extends android.security.keystore.IKeystoreOperationResultCallback.Stub { |
| 802 | final private CompletableFuture<OperationResult> future = new CompletableFuture<OperationResult>(); |
| 803 | @Override |
| 804 | public void onFinished(OperationResult operationResult) throws android.os.RemoteException { |
| 805 | future.complete(operationResult); |
| 806 | } |
| 807 | public final CompletableFuture<OperationResult> getFuture() { |
| 808 | return future; |
| 809 | } |
| 810 | }; |
| 811 | |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame] | 812 | public OperationResult begin(String alias, int purpose, boolean pruneable, |
| 813 | KeymasterArguments args, byte[] entropy, int uid) { |
| 814 | try { |
Dmitry Dementyev | efc4311 | 2017-10-27 23:10:28 -0700 | [diff] [blame] | 815 | args = args != null ? args : new KeymasterArguments(); |
| 816 | entropy = entropy != null ? entropy : new byte[0]; |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 817 | OperationPromise promise = new OperationPromise(); |
| 818 | int errorCode = mBinder.begin(promise, getToken(), alias, purpose, pruneable, args, |
| 819 | entropy, uid); |
| 820 | if (errorCode == NO_ERROR) { |
| 821 | return promise.getFuture().get(); |
| 822 | } else { |
| 823 | return new OperationResult(errorCode); |
| 824 | } |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 825 | } catch (RemoteException e) { |
| 826 | Log.w(TAG, "Cannot connect to keystore", e); |
| 827 | return null; |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 828 | } catch (ExecutionException | InterruptedException e) { |
| 829 | Log.e(TAG, "Begin completed with exception", e); |
| 830 | return null; |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | |
| 834 | public OperationResult begin(String alias, int purpose, boolean pruneable, |
Chad Brubaker | 966486e | 2015-06-01 12:57:06 -0700 | [diff] [blame] | 835 | KeymasterArguments args, byte[] entropy) { |
Dmitry Dementyev | efc4311 | 2017-10-27 23:10:28 -0700 | [diff] [blame] | 836 | entropy = entropy != null ? entropy : new byte[0]; |
| 837 | args = args != null ? args : new KeymasterArguments(); |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame] | 838 | return begin(alias, purpose, pruneable, args, entropy, UID_SELF); |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | public OperationResult update(IBinder token, KeymasterArguments arguments, byte[] input) { |
| 842 | try { |
Dmitry Dementyev | efc4311 | 2017-10-27 23:10:28 -0700 | [diff] [blame] | 843 | arguments = arguments != null ? arguments : new KeymasterArguments(); |
| 844 | input = input != null ? input : new byte[0]; |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 845 | OperationPromise promise = new OperationPromise(); |
| 846 | int errorCode = mBinder.update(promise, token, arguments, input); |
| 847 | if (errorCode == NO_ERROR) { |
| 848 | return promise.getFuture().get(); |
| 849 | } else { |
| 850 | return new OperationResult(errorCode); |
| 851 | } |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 852 | } catch (RemoteException e) { |
| 853 | Log.w(TAG, "Cannot connect to keystore", e); |
| 854 | return null; |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 855 | } catch (ExecutionException | InterruptedException e) { |
| 856 | Log.e(TAG, "Update completed with exception", e); |
| 857 | return null; |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | |
Chad Brubaker | 8a07701 | 2015-05-29 12:32:51 -0700 | [diff] [blame] | 861 | public OperationResult finish(IBinder token, KeymasterArguments arguments, byte[] signature, |
| 862 | byte[] entropy) { |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 863 | try { |
Dmitry Dementyev | efc4311 | 2017-10-27 23:10:28 -0700 | [diff] [blame] | 864 | arguments = arguments != null ? arguments : new KeymasterArguments(); |
| 865 | entropy = entropy != null ? entropy : new byte[0]; |
| 866 | signature = signature != null ? signature : new byte[0]; |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 867 | OperationPromise promise = new OperationPromise(); |
| 868 | int errorCode = mBinder.finish(promise, token, arguments, signature, entropy); |
| 869 | if (errorCode == NO_ERROR) { |
| 870 | return promise.getFuture().get(); |
| 871 | } else { |
| 872 | return new OperationResult(errorCode); |
| 873 | } |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 874 | } catch (RemoteException e) { |
| 875 | Log.w(TAG, "Cannot connect to keystore", e); |
| 876 | return null; |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 877 | } catch (ExecutionException | InterruptedException e) { |
| 878 | Log.e(TAG, "Finish completed with exception", e); |
| 879 | return null; |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
Chad Brubaker | 8a07701 | 2015-05-29 12:32:51 -0700 | [diff] [blame] | 883 | public OperationResult finish(IBinder token, KeymasterArguments arguments, byte[] signature) { |
| 884 | return finish(token, arguments, signature, null); |
| 885 | } |
| 886 | |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 887 | private class KeystoreResultPromise |
| 888 | extends android.security.keystore.IKeystoreResponseCallback.Stub { |
| 889 | final private CompletableFuture<KeystoreResponse> future = new CompletableFuture<KeystoreResponse>(); |
| 890 | @Override |
| 891 | public void onFinished(KeystoreResponse keystoreResponse) throws android.os.RemoteException { |
| 892 | future.complete(keystoreResponse); |
| 893 | } |
| 894 | public final CompletableFuture<KeystoreResponse> getFuture() { |
| 895 | return future; |
| 896 | } |
| 897 | }; |
| 898 | |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 899 | public int abort(IBinder token) { |
| 900 | try { |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 901 | KeystoreResultPromise promise = new KeystoreResultPromise(); |
| 902 | int errorCode = mBinder.abort(promise, token); |
| 903 | if (errorCode == NO_ERROR) { |
| 904 | return promise.getFuture().get().getErrorCode(); |
| 905 | } else { |
| 906 | return errorCode; |
| 907 | } |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 908 | } catch (RemoteException e) { |
| 909 | Log.w(TAG, "Cannot connect to keystore", e); |
| 910 | return SYSTEM_ERROR; |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 911 | } catch (ExecutionException | InterruptedException e) { |
| 912 | Log.e(TAG, "Abort completed with exception", e); |
| 913 | return SYSTEM_ERROR; |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 914 | } |
| 915 | } |
Chad Brubaker | 5654b36 | 2015-03-17 16:59:52 -0700 | [diff] [blame] | 916 | |
| 917 | /** |
Chad Brubaker | 5654b36 | 2015-03-17 16:59:52 -0700 | [diff] [blame] | 918 | * Add an authentication record to the keystore authorization table. |
| 919 | * |
| 920 | * @param authToken The packed bytes of a hw_auth_token_t to be provided to keymaster. |
| 921 | * @return {@code KeyStore.NO_ERROR} on success, otherwise an error value corresponding to |
| 922 | * a {@code KeymasterDefs.KM_ERROR_} value or {@code KeyStore} ResponseCode. |
| 923 | */ |
Brian Young | da82e2c | 2018-02-22 23:36:34 +0000 | [diff] [blame] | 924 | public int addAuthToken(byte[] authToken) { |
Chad Brubaker | 5654b36 | 2015-03-17 16:59:52 -0700 | [diff] [blame] | 925 | try { |
Brian Young | da82e2c | 2018-02-22 23:36:34 +0000 | [diff] [blame] | 926 | return mBinder.addAuthToken(authToken); |
Chad Brubaker | 5654b36 | 2015-03-17 16:59:52 -0700 | [diff] [blame] | 927 | } catch (RemoteException e) { |
| 928 | Log.w(TAG, "Cannot connect to keystore", e); |
| 929 | return SYSTEM_ERROR; |
| 930 | } |
| 931 | } |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 932 | |
Alex Klyubin | ad9ba10 | 2015-04-21 15:17:24 -0700 | [diff] [blame] | 933 | /** |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 934 | * Notify keystore that a user's password has changed. |
| 935 | * |
| 936 | * @param userId the user whose password changed. |
| 937 | * @param newPassword the new password or "" if the password was removed. |
| 938 | */ |
| 939 | public boolean onUserPasswordChanged(int userId, String newPassword) { |
| 940 | // Parcel.cpp doesn't support deserializing null strings and treats them as "". Make that |
| 941 | // explicit here. |
| 942 | if (newPassword == null) { |
| 943 | newPassword = ""; |
| 944 | } |
| 945 | try { |
| 946 | return mBinder.onUserPasswordChanged(userId, newPassword) == NO_ERROR; |
| 947 | } catch (RemoteException e) { |
| 948 | Log.w(TAG, "Cannot connect to keystore", e); |
| 949 | return false; |
| 950 | } |
| 951 | } |
| 952 | |
Chad Brubaker | 83ce095 | 2015-05-12 13:00:02 -0700 | [diff] [blame] | 953 | /** |
| 954 | * Notify keystore that a user was added. |
| 955 | * |
| 956 | * @param userId the new user. |
| 957 | * @param parentId the parent of the new user, or -1 if the user has no parent. If parentId is |
| 958 | * specified then the new user's keystore will be intialized with the same secure lockscreen |
| 959 | * password as the parent. |
| 960 | */ |
| 961 | public void onUserAdded(int userId, int parentId) { |
| 962 | try { |
| 963 | mBinder.onUserAdded(userId, parentId); |
| 964 | } catch (RemoteException e) { |
| 965 | Log.w(TAG, "Cannot connect to keystore", e); |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | /** |
| 970 | * Notify keystore that a user was added. |
| 971 | * |
| 972 | * @param userId the new user. |
| 973 | */ |
| 974 | public void onUserAdded(int userId) { |
| 975 | onUserAdded(userId, -1); |
| 976 | } |
| 977 | |
| 978 | /** |
| 979 | * Notify keystore that a user was removed. |
| 980 | * |
| 981 | * @param userId the removed user. |
| 982 | */ |
| 983 | public void onUserRemoved(int userId) { |
| 984 | try { |
| 985 | mBinder.onUserRemoved(userId); |
| 986 | } catch (RemoteException e) { |
| 987 | Log.w(TAG, "Cannot connect to keystore", e); |
| 988 | } |
| 989 | } |
| 990 | |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 991 | public boolean onUserPasswordChanged(String newPassword) { |
| 992 | return onUserPasswordChanged(UserHandle.getUserId(Process.myUid()), newPassword); |
| 993 | } |
| 994 | |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 995 | private class KeyAttestationCallbackResult { |
| 996 | private KeystoreResponse keystoreResponse; |
| 997 | private KeymasterCertificateChain certificateChain; |
| 998 | |
| 999 | public KeyAttestationCallbackResult(KeystoreResponse keystoreResponse, |
| 1000 | KeymasterCertificateChain certificateChain) { |
| 1001 | this.keystoreResponse = keystoreResponse; |
| 1002 | this.certificateChain = certificateChain; |
| 1003 | } |
| 1004 | |
| 1005 | public KeystoreResponse getKeystoreResponse() { |
| 1006 | return keystoreResponse; |
| 1007 | } |
| 1008 | |
| 1009 | public void setKeystoreResponse(KeystoreResponse keystoreResponse) { |
| 1010 | this.keystoreResponse = keystoreResponse; |
| 1011 | } |
| 1012 | |
| 1013 | public KeymasterCertificateChain getCertificateChain() { |
| 1014 | return certificateChain; |
| 1015 | } |
| 1016 | |
| 1017 | public void setCertificateChain(KeymasterCertificateChain certificateChain) { |
| 1018 | this.certificateChain = certificateChain; |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | private class CertificateChainPromise |
| 1023 | extends android.security.keystore.IKeystoreCertificateChainCallback.Stub { |
| 1024 | final private CompletableFuture<KeyAttestationCallbackResult> future = new CompletableFuture<KeyAttestationCallbackResult>(); |
| 1025 | @Override |
| 1026 | public void onFinished(KeystoreResponse keystoreResponse, |
| 1027 | KeymasterCertificateChain certificateChain) throws android.os.RemoteException { |
| 1028 | future.complete(new KeyAttestationCallbackResult(keystoreResponse, certificateChain)); |
| 1029 | } |
| 1030 | public final CompletableFuture<KeyAttestationCallbackResult> getFuture() { |
| 1031 | return future; |
| 1032 | } |
| 1033 | }; |
| 1034 | |
| 1035 | |
Shawn Willden | 8d8c747 | 2016-02-02 08:27:39 -0700 | [diff] [blame] | 1036 | public int attestKey( |
| 1037 | String alias, KeymasterArguments params, KeymasterCertificateChain outChain) { |
| 1038 | try { |
Dmitry Dementyev | efc4311 | 2017-10-27 23:10:28 -0700 | [diff] [blame] | 1039 | if (params == null) { |
| 1040 | params = new KeymasterArguments(); |
| 1041 | } |
| 1042 | if (outChain == null) { |
| 1043 | outChain = new KeymasterCertificateChain(); |
| 1044 | } |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 1045 | CertificateChainPromise promise = new CertificateChainPromise(); |
| 1046 | int error = mBinder.attestKey(promise, alias, params); |
| 1047 | if (error != NO_ERROR) return error; |
| 1048 | KeyAttestationCallbackResult result = promise.getFuture().get(); |
| 1049 | error = result.getKeystoreResponse().getErrorCode(); |
| 1050 | if (error == NO_ERROR) { |
| 1051 | outChain.shallowCopyFrom(result.getCertificateChain()); |
| 1052 | } |
| 1053 | return error; |
Shawn Willden | 8d8c747 | 2016-02-02 08:27:39 -0700 | [diff] [blame] | 1054 | } catch (RemoteException e) { |
| 1055 | Log.w(TAG, "Cannot connect to keystore", e); |
| 1056 | return SYSTEM_ERROR; |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 1057 | } catch (ExecutionException | InterruptedException e) { |
| 1058 | Log.e(TAG, "AttestKey completed with exception", e); |
| 1059 | return SYSTEM_ERROR; |
Shawn Willden | 8d8c747 | 2016-02-02 08:27:39 -0700 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | |
Bartosz Fabianowski | 237f4b36 | 2017-04-24 13:57:46 +0200 | [diff] [blame] | 1063 | public int attestDeviceIds(KeymasterArguments params, KeymasterCertificateChain outChain) { |
| 1064 | try { |
Dmitry Dementyev | efc4311 | 2017-10-27 23:10:28 -0700 | [diff] [blame] | 1065 | if (params == null) { |
| 1066 | params = new KeymasterArguments(); |
| 1067 | } |
| 1068 | if (outChain == null) { |
| 1069 | outChain = new KeymasterCertificateChain(); |
| 1070 | } |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 1071 | CertificateChainPromise promise = new CertificateChainPromise(); |
| 1072 | int error = mBinder.attestDeviceIds(promise, params); |
| 1073 | if (error != NO_ERROR) return error; |
| 1074 | KeyAttestationCallbackResult result = promise.getFuture().get(); |
| 1075 | error = result.getKeystoreResponse().getErrorCode(); |
| 1076 | if (error == NO_ERROR) { |
| 1077 | outChain.shallowCopyFrom(result.getCertificateChain()); |
| 1078 | } |
| 1079 | return error; |
Bartosz Fabianowski | 237f4b36 | 2017-04-24 13:57:46 +0200 | [diff] [blame] | 1080 | } catch (RemoteException e) { |
| 1081 | Log.w(TAG, "Cannot connect to keystore", e); |
| 1082 | return SYSTEM_ERROR; |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 1083 | } catch (ExecutionException | InterruptedException e) { |
| 1084 | Log.e(TAG, "AttestDevicdeIds completed with exception", e); |
| 1085 | return SYSTEM_ERROR; |
Bartosz Fabianowski | 237f4b36 | 2017-04-24 13:57:46 +0200 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | |
Tucker Sylvestro | b32aae2 | 2016-06-23 17:23:33 -0400 | [diff] [blame] | 1089 | /** |
| 1090 | * Notify keystore that the device went off-body. |
| 1091 | */ |
| 1092 | public void onDeviceOffBody() { |
| 1093 | try { |
| 1094 | mBinder.onDeviceOffBody(); |
| 1095 | } catch (RemoteException e) { |
| 1096 | Log.w(TAG, "Cannot connect to keystore", e); |
| 1097 | } |
| 1098 | } |
Shawn Willden | 8d8c747 | 2016-02-02 08:27:39 -0700 | [diff] [blame] | 1099 | |
Janis Danisevskis | 7dacad8 | 2018-01-24 15:12:11 -0800 | [diff] [blame] | 1100 | // Keep in sync with confirmationui/1.0/types.hal. |
| 1101 | public static final int CONFIRMATIONUI_OK = 0; |
| 1102 | public static final int CONFIRMATIONUI_CANCELED = 1; |
| 1103 | public static final int CONFIRMATIONUI_ABORTED = 2; |
| 1104 | public static final int CONFIRMATIONUI_OPERATION_PENDING = 3; |
| 1105 | public static final int CONFIRMATIONUI_IGNORED = 4; |
| 1106 | public static final int CONFIRMATIONUI_SYSTEM_ERROR = 5; |
| 1107 | public static final int CONFIRMATIONUI_UNIMPLEMENTED = 6; |
| 1108 | public static final int CONFIRMATIONUI_UNEXPECTED = 7; |
| 1109 | public static final int CONFIRMATIONUI_UIERROR = 0x10000; |
| 1110 | public static final int CONFIRMATIONUI_UIERROR_MISSING_GLYPH = 0x10001; |
| 1111 | public static final int CONFIRMATIONUI_UIERROR_MESSAGE_TOO_LONG = 0x10002; |
| 1112 | public static final int CONFIRMATIONUI_UIERROR_MALFORMED_UTF8_ENCODING = 0x10003; |
| 1113 | |
| 1114 | /** |
| 1115 | * Requests keystore call into the confirmationui HAL to display a prompt. |
| 1116 | * |
| 1117 | * @param listener the binder to use for callbacks. |
| 1118 | * @param promptText the prompt to display. |
| 1119 | * @param extraData extra data / nonce from application. |
| 1120 | * @param locale the locale as a BCP 47 langauge tag. |
| 1121 | * @param uiOptionsAsFlags the UI options to use, as flags. |
| 1122 | * @return one of the {@code CONFIRMATIONUI_*} constants, for |
| 1123 | * example {@code KeyStore.CONFIRMATIONUI_OK}. |
| 1124 | */ |
| 1125 | public int presentConfirmationPrompt(IBinder listener, String promptText, byte[] extraData, |
| 1126 | String locale, int uiOptionsAsFlags) { |
| 1127 | try { |
| 1128 | return mBinder.presentConfirmationPrompt(listener, promptText, extraData, locale, |
| 1129 | uiOptionsAsFlags); |
| 1130 | } catch (RemoteException e) { |
| 1131 | Log.w(TAG, "Cannot connect to keystore", e); |
| 1132 | return CONFIRMATIONUI_SYSTEM_ERROR; |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | /** |
| 1137 | * Requests keystore call into the confirmationui HAL to cancel displaying a prompt. |
| 1138 | * |
| 1139 | * @param listener the binder passed to the {@link #presentConfirmationPrompt} method. |
| 1140 | * @return one of the {@code CONFIRMATIONUI_*} constants, for |
| 1141 | * example {@code KeyStore.CONFIRMATIONUI_OK}. |
| 1142 | */ |
| 1143 | public int cancelConfirmationPrompt(IBinder listener) { |
| 1144 | try { |
| 1145 | return mBinder.cancelConfirmationPrompt(listener); |
| 1146 | } catch (RemoteException e) { |
| 1147 | Log.w(TAG, "Cannot connect to keystore", e); |
| 1148 | return CONFIRMATIONUI_SYSTEM_ERROR; |
| 1149 | } |
| 1150 | } |
| 1151 | |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 1152 | /** |
David Zeuthen | bbb7f65 | 2018-02-26 11:04:18 -0500 | [diff] [blame] | 1153 | * Requests keystore to check if the confirmationui HAL is available. |
| 1154 | * |
| 1155 | * @return whether the confirmationUI HAL is available. |
| 1156 | */ |
| 1157 | public boolean isConfirmationPromptSupported() { |
| 1158 | try { |
| 1159 | return mBinder.isConfirmationPromptSupported(); |
| 1160 | } catch (RemoteException e) { |
| 1161 | Log.w(TAG, "Cannot connect to keystore", e); |
| 1162 | return false; |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | /** |
Alex Klyubin | ad9ba10 | 2015-04-21 15:17:24 -0700 | [diff] [blame] | 1167 | * Returns a {@link KeyStoreException} corresponding to the provided keystore/keymaster error |
| 1168 | * code. |
| 1169 | */ |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 1170 | @UnsupportedAppUsage |
Alex Klyubin | dcdaf87 | 2015-05-13 15:57:09 -0700 | [diff] [blame] | 1171 | public static KeyStoreException getKeyStoreException(int errorCode) { |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 1172 | if (errorCode > 0) { |
| 1173 | // KeyStore layer error |
| 1174 | switch (errorCode) { |
| 1175 | case NO_ERROR: |
| 1176 | return new KeyStoreException(errorCode, "OK"); |
| 1177 | case LOCKED: |
Alex Klyubin | 5418393 | 2015-05-08 15:25:48 -0700 | [diff] [blame] | 1178 | return new KeyStoreException(errorCode, "User authentication required"); |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 1179 | case UNINITIALIZED: |
| 1180 | return new KeyStoreException(errorCode, "Keystore not initialized"); |
| 1181 | case SYSTEM_ERROR: |
| 1182 | return new KeyStoreException(errorCode, "System error"); |
| 1183 | case PERMISSION_DENIED: |
| 1184 | return new KeyStoreException(errorCode, "Permission denied"); |
| 1185 | case KEY_NOT_FOUND: |
| 1186 | return new KeyStoreException(errorCode, "Key not found"); |
| 1187 | case VALUE_CORRUPTED: |
| 1188 | return new KeyStoreException(errorCode, "Key blob corrupted"); |
Alex Klyubin | 058de02 | 2015-04-29 17:32:00 -0700 | [diff] [blame] | 1189 | case OP_AUTH_NEEDED: |
| 1190 | return new KeyStoreException(errorCode, "Operation requires authorization"); |
Max Bires | b2cc3dc | 2018-11-02 10:50:40 -0700 | [diff] [blame^] | 1191 | case KEY_PERMANENTLY_INVALIDATED: |
| 1192 | return new KeyStoreException(errorCode, "Key permanently invalidated"); |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 1193 | default: |
| 1194 | return new KeyStoreException(errorCode, String.valueOf(errorCode)); |
| 1195 | } |
| 1196 | } else { |
| 1197 | // Keymaster layer error |
| 1198 | switch (errorCode) { |
| 1199 | case KeymasterDefs.KM_ERROR_INVALID_AUTHORIZATION_TIMEOUT: |
| 1200 | // The name of this parameter significantly differs between Keymaster and |
| 1201 | // framework APIs. Use the framework wording to make life easier for developers. |
| 1202 | return new KeyStoreException(errorCode, |
| 1203 | "Invalid user authentication validity duration"); |
| 1204 | default: |
| 1205 | return new KeyStoreException(errorCode, |
| 1206 | KeymasterDefs.getErrorMessage(errorCode)); |
| 1207 | } |
| 1208 | } |
| 1209 | } |
| 1210 | |
Alex Klyubin | ad9ba10 | 2015-04-21 15:17:24 -0700 | [diff] [blame] | 1211 | /** |
| 1212 | * Returns an {@link InvalidKeyException} corresponding to the provided |
| 1213 | * {@link KeyStoreException}. |
| 1214 | */ |
Alex Klyubin | dcdaf87 | 2015-05-13 15:57:09 -0700 | [diff] [blame] | 1215 | public InvalidKeyException getInvalidKeyException( |
Alex Klyubin | 3876b1b | 2015-09-09 14:55:03 -0700 | [diff] [blame] | 1216 | String keystoreKeyAlias, int uid, KeyStoreException e) { |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 1217 | switch (e.getErrorCode()) { |
Brian Young | da82e2c | 2018-02-22 23:36:34 +0000 | [diff] [blame] | 1218 | case LOCKED: |
Alex Klyubin | 5418393 | 2015-05-08 15:25:48 -0700 | [diff] [blame] | 1219 | return new UserNotAuthenticatedException(); |
Brian Young | da82e2c | 2018-02-22 23:36:34 +0000 | [diff] [blame] | 1220 | case KeymasterDefs.KM_ERROR_KEY_EXPIRED: |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 1221 | return new KeyExpiredException(); |
Brian Young | da82e2c | 2018-02-22 23:36:34 +0000 | [diff] [blame] | 1222 | case KeymasterDefs.KM_ERROR_KEY_NOT_YET_VALID: |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 1223 | return new KeyNotYetValidException(); |
Brian Young | da82e2c | 2018-02-22 23:36:34 +0000 | [diff] [blame] | 1224 | case KeymasterDefs.KM_ERROR_KEY_USER_NOT_AUTHENTICATED: |
| 1225 | case OP_AUTH_NEEDED: |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 1226 | { |
| 1227 | // We now need to determine whether the key/operation can become usable if user |
| 1228 | // authentication is performed, or whether it can never become usable again. |
| 1229 | // User authentication requirements are contained in the key's characteristics. We |
| 1230 | // need to check whether these requirements can be be satisfied by asking the user |
| 1231 | // to authenticate. |
| 1232 | KeyCharacteristics keyCharacteristics = new KeyCharacteristics(); |
| 1233 | int getKeyCharacteristicsErrorCode = |
Alex Klyubin | 3876b1b | 2015-09-09 14:55:03 -0700 | [diff] [blame] | 1234 | getKeyCharacteristics(keystoreKeyAlias, null, null, uid, |
| 1235 | keyCharacteristics); |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 1236 | if (getKeyCharacteristicsErrorCode != NO_ERROR) { |
| 1237 | return new InvalidKeyException( |
| 1238 | "Failed to obtained key characteristics", |
| 1239 | getKeyStoreException(getKeyCharacteristicsErrorCode)); |
| 1240 | } |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 1241 | List<BigInteger> keySids = |
| 1242 | keyCharacteristics.getUnsignedLongs(KeymasterDefs.KM_TAG_USER_SECURE_ID); |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 1243 | if (keySids.isEmpty()) { |
| 1244 | // Key is not bound to any SIDs -- no amount of authentication will help here. |
| 1245 | return new KeyPermanentlyInvalidatedException(); |
| 1246 | } |
| 1247 | long rootSid = GateKeeper.getSecureUserId(); |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 1248 | if ((rootSid != 0) && (keySids.contains(KeymasterArguments.toUint64(rootSid)))) { |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 1249 | // One of the key's SIDs is the current root SID -- user can be authenticated |
| 1250 | // against that SID. |
| 1251 | return new UserNotAuthenticatedException(); |
| 1252 | } |
| 1253 | |
| 1254 | long fingerprintOnlySid = getFingerprintOnlySid(); |
| 1255 | if ((fingerprintOnlySid != 0) |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 1256 | && (keySids.contains(KeymasterArguments.toUint64(fingerprintOnlySid)))) { |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 1257 | // One of the key's SIDs is the current fingerprint SID -- user can be |
| 1258 | // authenticated against that SID. |
| 1259 | return new UserNotAuthenticatedException(); |
| 1260 | } |
| 1261 | |
| 1262 | // None of the key's SIDs can ever be authenticated |
| 1263 | return new KeyPermanentlyInvalidatedException(); |
| 1264 | } |
Brian Young | da82e2c | 2018-02-22 23:36:34 +0000 | [diff] [blame] | 1265 | case UNINITIALIZED: |
Janis Danisevskis | d07d338 | 2017-09-01 14:45:16 -0700 | [diff] [blame] | 1266 | return new KeyPermanentlyInvalidatedException(); |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 1267 | default: |
Alex Klyubin | ad9ba10 | 2015-04-21 15:17:24 -0700 | [diff] [blame] | 1268 | return new InvalidKeyException("Keystore operation failed", e); |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 1269 | } |
| 1270 | } |
| 1271 | |
Alex Klyubin | 2d7a85c | 2015-04-30 11:43:53 -0700 | [diff] [blame] | 1272 | private long getFingerprintOnlySid() { |
Kevin Chyn | 5596642 | 2017-10-23 16:08:47 -0700 | [diff] [blame] | 1273 | final PackageManager packageManager = mContext.getPackageManager(); |
| 1274 | if (!packageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) { |
| 1275 | return 0; |
| 1276 | } |
Alex Klyubin | a99b8b5 | 2015-06-11 13:27:34 -0700 | [diff] [blame] | 1277 | FingerprintManager fingerprintManager = mContext.getSystemService(FingerprintManager.class); |
Alex Klyubin | 2d7a85c | 2015-04-30 11:43:53 -0700 | [diff] [blame] | 1278 | if (fingerprintManager == null) { |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 1279 | return 0; |
| 1280 | } |
| 1281 | |
Alex Klyubin | a99b8b5 | 2015-06-11 13:27:34 -0700 | [diff] [blame] | 1282 | // TODO: Restore USE_FINGERPRINT permission check in |
| 1283 | // FingerprintManager.getAuthenticatorId once the ID is no longer needed here. |
Alex Klyubin | 2d7a85c | 2015-04-30 11:43:53 -0700 | [diff] [blame] | 1284 | return fingerprintManager.getAuthenticatorId(); |
Svetoslav | 2dac95d | 2015-04-30 11:30:33 -0700 | [diff] [blame] | 1285 | } |
| 1286 | |
Alex Klyubin | ad9ba10 | 2015-04-21 15:17:24 -0700 | [diff] [blame] | 1287 | /** |
| 1288 | * Returns an {@link InvalidKeyException} corresponding to the provided keystore/keymaster error |
| 1289 | * code. |
| 1290 | */ |
Alex Klyubin | 3876b1b | 2015-09-09 14:55:03 -0700 | [diff] [blame] | 1291 | public InvalidKeyException getInvalidKeyException(String keystoreKeyAlias, int uid, |
| 1292 | int errorCode) { |
| 1293 | return getInvalidKeyException(keystoreKeyAlias, uid, getKeyStoreException(errorCode)); |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 1294 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 1295 | } |