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