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