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