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; |
Kenny Root | a398598 | 2013-08-16 14:03:29 -0700 | [diff] [blame] | 22 | |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Alex Klyubin | 2d7a85c | 2015-04-30 11:43:53 -0700 | [diff] [blame] | 24 | import android.hardware.fingerprint.FingerprintManager; |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 25 | import android.os.Binder; |
| 26 | import android.os.IBinder; |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 27 | import android.os.Process; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 28 | import android.os.RemoteException; |
| 29 | import android.os.ServiceManager; |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 30 | import android.os.UserHandle; |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 31 | import android.security.keymaster.ExportResult; |
| 32 | import android.security.keymaster.KeyCharacteristics; |
| 33 | import android.security.keymaster.KeymasterArguments; |
Chad Brubaker | 5e73c0e | 2015-03-21 22:46:43 -0700 | [diff] [blame] | 34 | import android.security.keymaster.KeymasterBlob; |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 35 | import android.security.keymaster.KeymasterDefs; |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 36 | import android.security.keymaster.OperationResult; |
Alex Klyubin | 3f8d4d8 | 2015-05-13 09:15:00 -0700 | [diff] [blame] | 37 | import android.security.keystore.KeyExpiredException; |
| 38 | import android.security.keystore.KeyNotYetValidException; |
| 39 | import android.security.keystore.KeyPermanentlyInvalidatedException; |
Alex Klyubin | 3f8d4d8 | 2015-05-13 09:15:00 -0700 | [diff] [blame] | 40 | import android.security.keystore.UserNotAuthenticatedException; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 41 | import android.util.Log; |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 42 | |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 43 | import java.math.BigInteger; |
Alex Klyubin | ad9ba10 | 2015-04-21 15:17:24 -0700 | [diff] [blame] | 44 | import java.security.InvalidKeyException; |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 45 | import java.util.List; |
Kenny Root | b91773b | 2013-09-05 13:03:16 -0700 | [diff] [blame] | 46 | import java.util.Locale; |
| 47 | |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 48 | /** |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 49 | * @hide This should not be made public in its present form because it |
| 50 | * assumes that private and secret key bytes are available and would |
| 51 | * preclude the use of hardware crypto. |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 52 | */ |
| 53 | public class KeyStore { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 54 | private static final String TAG = "KeyStore"; |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 55 | |
| 56 | // ResponseCodes |
Brian Carlstrom | 7e4b1a4 | 2011-06-01 15:29:29 -0700 | [diff] [blame] | 57 | public static final int NO_ERROR = 1; |
| 58 | public static final int LOCKED = 2; |
| 59 | public static final int UNINITIALIZED = 3; |
| 60 | public static final int SYSTEM_ERROR = 4; |
| 61 | public static final int PROTOCOL_ERROR = 5; |
| 62 | public static final int PERMISSION_DENIED = 6; |
| 63 | public static final int KEY_NOT_FOUND = 7; |
| 64 | public static final int VALUE_CORRUPTED = 8; |
| 65 | public static final int UNDEFINED_ACTION = 9; |
| 66 | public static final int WRONG_PASSWORD = 10; |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 67 | |
Chad Brubaker | 560d653 | 2015-04-24 10:32:18 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Per operation authentication is needed before this operation is valid. |
| 70 | * This is returned from {@link #begin} when begin succeeds but the operation uses |
| 71 | * per-operation authentication and must authenticate before calling {@link #update} or |
| 72 | * {@link #finish}. |
| 73 | */ |
| 74 | public static final int OP_AUTH_NEEDED = 15; |
| 75 | |
Kenny Root | 2eeda72 | 2013-04-10 11:30:58 -0700 | [diff] [blame] | 76 | // Used for UID field to indicate the calling UID. |
| 77 | public static final int UID_SELF = -1; |
| 78 | |
| 79 | // Flags for "put" "import" and "generate" |
| 80 | public static final int FLAG_NONE = 0; |
Alex Klyubin | 5418393 | 2015-05-08 15:25:48 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Indicates that this key (or key pair) must be encrypted at rest. This will protect the key |
| 84 | * (or key pair) with the secure lock screen credential (e.g., password, PIN, or pattern). |
| 85 | * |
| 86 | * <p>Note that this requires that the secure lock screen (e.g., password, PIN, pattern) is set |
| 87 | * up, otherwise key (or key pair) generation or import will fail. Moreover, this key (or key |
| 88 | * pair) will be deleted when the secure lock screen is disabled or reset (e.g., by the user or |
| 89 | * a Device Administrator). Finally, this key (or key pair) cannot be used until the user |
| 90 | * unlocks the secure lock screen after boot. |
| 91 | * |
| 92 | * @see KeyguardManager#isDeviceSecure() |
| 93 | */ |
Kenny Root | a3788b0 | 2013-04-10 10:37:55 -0700 | [diff] [blame] | 94 | public static final int FLAG_ENCRYPTED = 1; |
| 95 | |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 96 | // States |
| 97 | public enum State { UNLOCKED, LOCKED, UNINITIALIZED }; |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 98 | |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 99 | private int mError = NO_ERROR; |
| 100 | |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 101 | private final IKeystoreService mBinder; |
Alex Klyubin | 2d7a85c | 2015-04-30 11:43:53 -0700 | [diff] [blame] | 102 | private final Context mContext; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 103 | |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 104 | private IBinder mToken; |
| 105 | |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 106 | private KeyStore(IKeystoreService binder) { |
| 107 | mBinder = binder; |
Alex Klyubin | 3f8d4d8 | 2015-05-13 09:15:00 -0700 | [diff] [blame] | 108 | mContext = getApplicationContext(); |
Alex Klyubin | 2d7a85c | 2015-04-30 11:43:53 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Alex Klyubin | dcdaf87 | 2015-05-13 15:57:09 -0700 | [diff] [blame] | 111 | public static Context getApplicationContext() { |
Alex Klyubin | a99b8b5 | 2015-06-11 13:27:34 -0700 | [diff] [blame] | 112 | Application application = ActivityThread.currentApplication(); |
Alex Klyubin | 2d7a85c | 2015-04-30 11:43:53 -0700 | [diff] [blame] | 113 | if (application == null) { |
| 114 | throw new IllegalStateException( |
Alex Klyubin | a99b8b5 | 2015-06-11 13:27:34 -0700 | [diff] [blame] | 115 | "Failed to obtain application Context from ActivityThread"); |
Alex Klyubin | 2d7a85c | 2015-04-30 11:43:53 -0700 | [diff] [blame] | 116 | } |
| 117 | return application; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 118 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 119 | |
| 120 | public static KeyStore getInstance() { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 121 | IKeystoreService keystore = IKeystoreService.Stub.asInterface(ServiceManager |
| 122 | .getService("android.security.keystore")); |
| 123 | return new KeyStore(keystore); |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 124 | } |
| 125 | |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 126 | private synchronized IBinder getToken() { |
| 127 | if (mToken == null) { |
| 128 | mToken = new Binder(); |
| 129 | } |
| 130 | return mToken; |
| 131 | } |
| 132 | |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 133 | public State state(int userId) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 134 | final int ret; |
| 135 | try { |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 136 | ret = mBinder.getState(userId); |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 137 | } catch (RemoteException e) { |
| 138 | Log.w(TAG, "Cannot connect to keystore", e); |
| 139 | throw new AssertionError(e); |
| 140 | } |
| 141 | |
| 142 | switch (ret) { |
Brian Carlstrom | 5cfee3f | 2011-05-31 01:00:15 -0700 | [diff] [blame] | 143 | case NO_ERROR: return State.UNLOCKED; |
| 144 | case LOCKED: return State.LOCKED; |
| 145 | case UNINITIALIZED: return State.UNINITIALIZED; |
| 146 | default: throw new AssertionError(mError); |
| 147 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 148 | } |
| 149 | |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 150 | public State state() { |
| 151 | return state(UserHandle.myUserId()); |
| 152 | } |
| 153 | |
Kenny Root | b9594ce | 2013-02-14 10:18:38 -0800 | [diff] [blame] | 154 | public boolean isUnlocked() { |
| 155 | return state() == State.UNLOCKED; |
| 156 | } |
| 157 | |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame^] | 158 | public byte[] get(String key, int uid) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 159 | try { |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame^] | 160 | return mBinder.get(key, uid); |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 161 | } catch (RemoteException e) { |
| 162 | Log.w(TAG, "Cannot connect to keystore", e); |
| 163 | return null; |
| 164 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 165 | } |
| 166 | |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame^] | 167 | public byte[] get(String key) { |
| 168 | return get(key, UID_SELF); |
| 169 | } |
| 170 | |
Kenny Root | a3788b0 | 2013-04-10 10:37:55 -0700 | [diff] [blame] | 171 | public boolean put(String key, byte[] value, int uid, int flags) { |
Alex Klyubin | 3ceb1a0 | 2015-06-05 15:51:06 -0700 | [diff] [blame] | 172 | return insert(key, value, uid, flags) == NO_ERROR; |
| 173 | } |
| 174 | |
| 175 | public int insert(String key, byte[] value, int uid, int flags) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 176 | try { |
Alex Klyubin | 3ceb1a0 | 2015-06-05 15:51:06 -0700 | [diff] [blame] | 177 | return mBinder.insert(key, value, uid, flags); |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 178 | } catch (RemoteException e) { |
| 179 | Log.w(TAG, "Cannot connect to keystore", e); |
Alex Klyubin | 3ceb1a0 | 2015-06-05 15:51:06 -0700 | [diff] [blame] | 180 | return SYSTEM_ERROR; |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 184 | public boolean delete(String key, int uid) { |
| 185 | try { |
| 186 | return mBinder.del(key, uid) == NO_ERROR; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 187 | } catch (RemoteException e) { |
| 188 | Log.w(TAG, "Cannot connect to keystore", e); |
| 189 | return false; |
| 190 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | public boolean delete(String key) { |
Kenny Root | 2eeda72 | 2013-04-10 11:30:58 -0700 | [diff] [blame] | 194 | return delete(key, UID_SELF); |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | public boolean contains(String key, int uid) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 198 | try { |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 199 | return mBinder.exist(key, uid) == NO_ERROR; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 200 | } catch (RemoteException e) { |
| 201 | Log.w(TAG, "Cannot connect to keystore", e); |
| 202 | return false; |
| 203 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | public boolean contains(String key) { |
Kenny Root | 2eeda72 | 2013-04-10 11:30:58 -0700 | [diff] [blame] | 207 | return contains(key, UID_SELF); |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 208 | } |
| 209 | |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 210 | /** |
| 211 | * List all entries in the keystore for {@code uid} starting with {@code prefix}. |
| 212 | */ |
| 213 | public String[] list(String prefix, int uid) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 214 | try { |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 215 | return mBinder.list(prefix, uid); |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 216 | } catch (RemoteException e) { |
| 217 | Log.w(TAG, "Cannot connect to keystore", e); |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 218 | return null; |
| 219 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 220 | } |
| 221 | |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 222 | public String[] list(String prefix) { |
| 223 | return list(prefix, UID_SELF); |
| 224 | } |
| 225 | |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 226 | public boolean reset() { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 227 | try { |
| 228 | return mBinder.reset() == NO_ERROR; |
| 229 | } catch (RemoteException e) { |
| 230 | Log.w(TAG, "Cannot connect to keystore", e); |
| 231 | return false; |
| 232 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 233 | } |
| 234 | |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 235 | /** |
| 236 | * Attempt to lock the keystore for {@code user}. |
| 237 | * |
| 238 | * @param user Android user to lock. |
| 239 | * @return whether {@code user}'s keystore was locked. |
| 240 | */ |
| 241 | public boolean lock(int userId) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 242 | try { |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 243 | return mBinder.lock(userId) == NO_ERROR; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 244 | } catch (RemoteException e) { |
| 245 | Log.w(TAG, "Cannot connect to keystore", e); |
| 246 | return false; |
| 247 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 248 | } |
| 249 | |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 250 | public boolean lock() { |
| 251 | return lock(UserHandle.myUserId()); |
| 252 | } |
| 253 | |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 254 | /** |
| 255 | * Attempt to unlock the keystore for {@code user} with the password {@code password}. |
| 256 | * This is required before keystore entries created with FLAG_ENCRYPTED can be accessed or |
| 257 | * created. |
| 258 | * |
| 259 | * @param user Android user ID to operate on |
| 260 | * @param password user's keystore password. Should be the most recent value passed to |
| 261 | * {@link #onUserPasswordChanged} for the user. |
| 262 | * |
| 263 | * @return whether the keystore was unlocked. |
| 264 | */ |
| 265 | public boolean unlock(int userId, String password) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 266 | try { |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 267 | mError = mBinder.unlock(userId, password); |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 268 | return mError == NO_ERROR; |
| 269 | } catch (RemoteException e) { |
| 270 | Log.w(TAG, "Cannot connect to keystore", e); |
| 271 | return false; |
| 272 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 273 | } |
| 274 | |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 275 | public boolean unlock(String password) { |
| 276 | return unlock(UserHandle.getUserId(Process.myUid()), password); |
| 277 | } |
| 278 | |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 279 | /** |
| 280 | * Check if the keystore for {@code userId} is empty. |
| 281 | */ |
| 282 | public boolean isEmpty(int userId) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 283 | try { |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 284 | return mBinder.isEmpty(userId) != 0; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 285 | } catch (RemoteException e) { |
| 286 | Log.w(TAG, "Cannot connect to keystore", e); |
| 287 | return false; |
| 288 | } |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Chad Brubaker | e35d49f | 2015-05-12 15:19:52 -0700 | [diff] [blame] | 291 | public boolean isEmpty() { |
| 292 | return isEmpty(UserHandle.myUserId()); |
| 293 | } |
| 294 | |
Kenny Root | a398598 | 2013-08-16 14:03:29 -0700 | [diff] [blame] | 295 | public boolean generate(String key, int uid, int keyType, int keySize, int flags, |
| 296 | byte[][] args) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 297 | try { |
Chad Brubaker | 7a2c973 | 2015-01-13 17:41:22 -0800 | [diff] [blame] | 298 | return mBinder.generate(key, uid, keyType, keySize, flags, |
| 299 | new KeystoreArguments(args)) == NO_ERROR; |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 300 | } catch (RemoteException e) { |
| 301 | Log.w(TAG, "Cannot connect to keystore", e); |
| 302 | return false; |
| 303 | } |
| 304 | } |
| 305 | |
Kenny Root | a3788b0 | 2013-04-10 10:37:55 -0700 | [diff] [blame] | 306 | public boolean importKey(String keyName, byte[] key, int uid, int flags) { |
Kenny Root | 78ad849 | 2013-02-13 17:02:57 -0800 | [diff] [blame] | 307 | try { |
Kenny Root | a3788b0 | 2013-04-10 10:37:55 -0700 | [diff] [blame] | 308 | return mBinder.import_key(keyName, key, uid, flags) == NO_ERROR; |
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); |
| 311 | return false; |
| 312 | } |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 315 | public byte[] sign(String key, byte[] data) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 316 | try { |
| 317 | return mBinder.sign(key, data); |
| 318 | } catch (RemoteException e) { |
| 319 | Log.w(TAG, "Cannot connect to keystore", e); |
| 320 | return null; |
| 321 | } |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | public boolean verify(String key, byte[] data, byte[] signature) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 325 | try { |
| 326 | return mBinder.verify(key, data, signature) == NO_ERROR; |
| 327 | } catch (RemoteException e) { |
| 328 | Log.w(TAG, "Cannot connect to keystore", e); |
| 329 | return false; |
| 330 | } |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | public boolean grant(String key, int uid) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 334 | try { |
| 335 | return mBinder.grant(key, uid) == NO_ERROR; |
| 336 | } catch (RemoteException e) { |
| 337 | Log.w(TAG, "Cannot connect to keystore", e); |
| 338 | return false; |
| 339 | } |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | public boolean ungrant(String key, int uid) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 343 | try { |
| 344 | return mBinder.ungrant(key, uid) == NO_ERROR; |
| 345 | } catch (RemoteException e) { |
| 346 | Log.w(TAG, "Cannot connect to keystore", e); |
| 347 | return false; |
Kenny Root | 473c712 | 2012-08-17 21:13:48 -0700 | [diff] [blame] | 348 | } |
Kenny Root | 473c712 | 2012-08-17 21:13:48 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Returns the last modification time of the key in milliseconds since the |
| 353 | * epoch. Will return -1L if the key could not be found or other error. |
| 354 | */ |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame^] | 355 | public long getmtime(String key, int uid) { |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 356 | try { |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame^] | 357 | final long millis = mBinder.getmtime(key, uid); |
Kenny Root | e66769a | 2013-02-04 15:49:11 -0800 | [diff] [blame] | 358 | if (millis == -1L) { |
| 359 | return -1L; |
| 360 | } |
| 361 | |
| 362 | return millis * 1000L; |
Kenny Root | 6b77645 | 2012-11-02 15:40:32 -0700 | [diff] [blame] | 363 | } catch (RemoteException e) { |
| 364 | Log.w(TAG, "Cannot connect to keystore", e); |
| 365 | return -1L; |
| 366 | } |
Kenny Root | 473c712 | 2012-08-17 21:13:48 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame^] | 369 | public long getmtime(String key) { |
| 370 | return getmtime(key, UID_SELF); |
| 371 | } |
| 372 | |
Kenny Root | 5f1d965 | 2013-03-21 14:21:50 -0700 | [diff] [blame] | 373 | public boolean duplicate(String srcKey, int srcUid, String destKey, int destUid) { |
Kenny Root | bd79419 | 2013-03-20 11:36:50 -0700 | [diff] [blame] | 374 | try { |
Kenny Root | 5f1d965 | 2013-03-21 14:21:50 -0700 | [diff] [blame] | 375 | return mBinder.duplicate(srcKey, srcUid, destKey, destUid) == NO_ERROR; |
Kenny Root | bd79419 | 2013-03-20 11:36:50 -0700 | [diff] [blame] | 376 | } catch (RemoteException e) { |
| 377 | Log.w(TAG, "Cannot connect to keystore", e); |
| 378 | return false; |
| 379 | } |
| 380 | } |
| 381 | |
Alex Klyubin | 469cbf5 | 2015-06-04 12:36:27 -0700 | [diff] [blame] | 382 | // TODO: remove this when it's removed from Settings |
Kenny Root | 5cb5cec | 2013-03-29 11:14:17 -0700 | [diff] [blame] | 383 | public boolean isHardwareBacked() { |
Kenny Root | b91773b | 2013-09-05 13:03:16 -0700 | [diff] [blame] | 384 | return isHardwareBacked("RSA"); |
| 385 | } |
| 386 | |
| 387 | public boolean isHardwareBacked(String keyType) { |
Kenny Root | 5cb5cec | 2013-03-29 11:14:17 -0700 | [diff] [blame] | 388 | try { |
Kenny Root | b91773b | 2013-09-05 13:03:16 -0700 | [diff] [blame] | 389 | return mBinder.is_hardware_backed(keyType.toUpperCase(Locale.US)) == NO_ERROR; |
Kenny Root | 5cb5cec | 2013-03-29 11:14:17 -0700 | [diff] [blame] | 390 | } catch (RemoteException e) { |
| 391 | Log.w(TAG, "Cannot connect to keystore", e); |
| 392 | return false; |
| 393 | } |
| 394 | } |
| 395 | |
Kenny Root | d72317a | 2013-04-01 15:59:59 -0700 | [diff] [blame] | 396 | public boolean clearUid(int uid) { |
| 397 | try { |
| 398 | return mBinder.clear_uid(uid) == NO_ERROR; |
| 399 | } catch (RemoteException e) { |
| 400 | Log.w(TAG, "Cannot connect to keystore", e); |
| 401 | return false; |
| 402 | } |
| 403 | } |
| 404 | |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 405 | public int getLastError() { |
| 406 | return mError; |
| 407 | } |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 408 | |
| 409 | public boolean addRngEntropy(byte[] data) { |
| 410 | try { |
| 411 | return mBinder.addRngEntropy(data) == NO_ERROR; |
| 412 | } catch (RemoteException e) { |
| 413 | Log.w(TAG, "Cannot connect to keystore", e); |
| 414 | return false; |
| 415 | } |
| 416 | } |
| 417 | |
Chad Brubaker | dae79e5 | 2015-03-27 14:28:35 -0700 | [diff] [blame] | 418 | public int generateKey(String alias, KeymasterArguments args, byte[] entropy, int uid, |
| 419 | int flags, KeyCharacteristics outCharacteristics) { |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 420 | try { |
Chad Brubaker | dae79e5 | 2015-03-27 14:28:35 -0700 | [diff] [blame] | 421 | return mBinder.generateKey(alias, args, entropy, uid, flags, outCharacteristics); |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 422 | } catch (RemoteException e) { |
| 423 | Log.w(TAG, "Cannot connect to keystore", e); |
| 424 | return SYSTEM_ERROR; |
| 425 | } |
| 426 | } |
| 427 | |
Chad Brubaker | dae79e5 | 2015-03-27 14:28:35 -0700 | [diff] [blame] | 428 | public int generateKey(String alias, KeymasterArguments args, byte[] entropy, int flags, |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 429 | KeyCharacteristics outCharacteristics) { |
Chad Brubaker | dae79e5 | 2015-03-27 14:28:35 -0700 | [diff] [blame] | 430 | return generateKey(alias, args, entropy, UID_SELF, flags, outCharacteristics); |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 431 | } |
| 432 | |
Chad Brubaker | 5e73c0e | 2015-03-21 22:46:43 -0700 | [diff] [blame] | 433 | public int getKeyCharacteristics(String alias, KeymasterBlob clientId, KeymasterBlob appId, |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame^] | 434 | int uid, KeyCharacteristics outCharacteristics) { |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 435 | try { |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame^] | 436 | return mBinder.getKeyCharacteristics(alias, clientId, appId, uid, outCharacteristics); |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 437 | } catch (RemoteException e) { |
| 438 | Log.w(TAG, "Cannot connect to keystore", e); |
| 439 | return SYSTEM_ERROR; |
| 440 | } |
| 441 | } |
| 442 | |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame^] | 443 | public int getKeyCharacteristics(String alias, KeymasterBlob clientId, KeymasterBlob appId, |
| 444 | KeyCharacteristics outCharacteristics) { |
| 445 | return getKeyCharacteristics(alias, clientId, appId, UID_SELF, outCharacteristics); |
| 446 | } |
| 447 | |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 448 | public int importKey(String alias, KeymasterArguments args, int format, byte[] keyData, |
| 449 | int uid, int flags, KeyCharacteristics outCharacteristics) { |
| 450 | try { |
| 451 | return mBinder.importKey(alias, args, format, keyData, uid, flags, |
| 452 | outCharacteristics); |
| 453 | } catch (RemoteException e) { |
| 454 | Log.w(TAG, "Cannot connect to keystore", e); |
| 455 | return SYSTEM_ERROR; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | public int importKey(String alias, KeymasterArguments args, int format, byte[] keyData, |
| 460 | int flags, KeyCharacteristics outCharacteristics) { |
| 461 | return importKey(alias, args, format, keyData, UID_SELF, flags, outCharacteristics); |
| 462 | } |
| 463 | |
Chad Brubaker | 5e73c0e | 2015-03-21 22:46:43 -0700 | [diff] [blame] | 464 | public ExportResult exportKey(String alias, int format, KeymasterBlob clientId, |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame^] | 465 | KeymasterBlob appId, int uid) { |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 466 | try { |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame^] | 467 | return mBinder.exportKey(alias, format, clientId, appId, uid); |
| 468 | } catch (RemoteException e) { |
| 469 | Log.w(TAG, "Cannot connect to keystore", e); |
| 470 | return null; |
| 471 | } |
| 472 | } |
| 473 | public ExportResult exportKey(String alias, int format, KeymasterBlob clientId, |
| 474 | KeymasterBlob appId) { |
| 475 | return exportKey(alias, format, clientId, appId, UID_SELF); |
| 476 | } |
| 477 | |
| 478 | public OperationResult begin(String alias, int purpose, boolean pruneable, |
| 479 | KeymasterArguments args, byte[] entropy, int uid) { |
| 480 | try { |
| 481 | return mBinder.begin(getToken(), alias, purpose, pruneable, args, entropy, uid); |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 482 | } catch (RemoteException e) { |
| 483 | Log.w(TAG, "Cannot connect to keystore", e); |
| 484 | return null; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | public OperationResult begin(String alias, int purpose, boolean pruneable, |
Chad Brubaker | 966486e | 2015-06-01 12:57:06 -0700 | [diff] [blame] | 489 | KeymasterArguments args, byte[] entropy) { |
Chad Brubaker | 5bbf048 | 2015-09-09 14:53:52 -0700 | [diff] [blame^] | 490 | return begin(alias, purpose, pruneable, args, entropy, UID_SELF); |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | public OperationResult update(IBinder token, KeymasterArguments arguments, byte[] input) { |
| 494 | try { |
| 495 | return mBinder.update(token, arguments, input); |
| 496 | } catch (RemoteException e) { |
| 497 | Log.w(TAG, "Cannot connect to keystore", e); |
| 498 | return null; |
| 499 | } |
| 500 | } |
| 501 | |
Chad Brubaker | 8a07701 | 2015-05-29 12:32:51 -0700 | [diff] [blame] | 502 | public OperationResult finish(IBinder token, KeymasterArguments arguments, byte[] signature, |
| 503 | byte[] entropy) { |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 504 | try { |
Chad Brubaker | 8a07701 | 2015-05-29 12:32:51 -0700 | [diff] [blame] | 505 | return mBinder.finish(token, arguments, signature, entropy); |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 506 | } catch (RemoteException e) { |
| 507 | Log.w(TAG, "Cannot connect to keystore", e); |
| 508 | return null; |
| 509 | } |
| 510 | } |
| 511 | |
Chad Brubaker | 8a07701 | 2015-05-29 12:32:51 -0700 | [diff] [blame] | 512 | public OperationResult finish(IBinder token, KeymasterArguments arguments, byte[] signature) { |
| 513 | return finish(token, arguments, signature, null); |
| 514 | } |
| 515 | |
Chad Brubaker | e6a46134 | 2015-02-10 21:33:23 -0800 | [diff] [blame] | 516 | public int abort(IBinder token) { |
| 517 | try { |
| 518 | return mBinder.abort(token); |
| 519 | } catch (RemoteException e) { |
| 520 | Log.w(TAG, "Cannot connect to keystore", e); |
| 521 | return SYSTEM_ERROR; |
| 522 | } |
| 523 | } |
Chad Brubaker | 5654b36 | 2015-03-17 16:59:52 -0700 | [diff] [blame] | 524 | |
| 525 | /** |
| 526 | * Check if the operation referenced by {@code token} is currently authorized. |
| 527 | * |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 528 | * @param token An operation token returned by a call to |
| 529 | * {@link #begin(String, int, boolean, KeymasterArguments, byte[], KeymasterArguments) begin}. |
Chad Brubaker | 5654b36 | 2015-03-17 16:59:52 -0700 | [diff] [blame] | 530 | */ |
| 531 | public boolean isOperationAuthorized(IBinder token) { |
| 532 | try { |
| 533 | return mBinder.isOperationAuthorized(token); |
| 534 | } catch (RemoteException e) { |
| 535 | Log.w(TAG, "Cannot connect to keystore", e); |
| 536 | return false; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Add an authentication record to the keystore authorization table. |
| 542 | * |
| 543 | * @param authToken The packed bytes of a hw_auth_token_t to be provided to keymaster. |
| 544 | * @return {@code KeyStore.NO_ERROR} on success, otherwise an error value corresponding to |
| 545 | * a {@code KeymasterDefs.KM_ERROR_} value or {@code KeyStore} ResponseCode. |
| 546 | */ |
| 547 | public int addAuthToken(byte[] authToken) { |
| 548 | try { |
| 549 | return mBinder.addAuthToken(authToken); |
| 550 | } catch (RemoteException e) { |
| 551 | Log.w(TAG, "Cannot connect to keystore", e); |
| 552 | return SYSTEM_ERROR; |
| 553 | } |
| 554 | } |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 555 | |
Alex Klyubin | ad9ba10 | 2015-04-21 15:17:24 -0700 | [diff] [blame] | 556 | /** |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 557 | * Notify keystore that a user's password has changed. |
| 558 | * |
| 559 | * @param userId the user whose password changed. |
| 560 | * @param newPassword the new password or "" if the password was removed. |
| 561 | */ |
| 562 | public boolean onUserPasswordChanged(int userId, String newPassword) { |
| 563 | // Parcel.cpp doesn't support deserializing null strings and treats them as "". Make that |
| 564 | // explicit here. |
| 565 | if (newPassword == null) { |
| 566 | newPassword = ""; |
| 567 | } |
| 568 | try { |
| 569 | return mBinder.onUserPasswordChanged(userId, newPassword) == NO_ERROR; |
| 570 | } catch (RemoteException e) { |
| 571 | Log.w(TAG, "Cannot connect to keystore", e); |
| 572 | return false; |
| 573 | } |
| 574 | } |
| 575 | |
Chad Brubaker | 83ce095 | 2015-05-12 13:00:02 -0700 | [diff] [blame] | 576 | /** |
| 577 | * Notify keystore that a user was added. |
| 578 | * |
| 579 | * @param userId the new user. |
| 580 | * @param parentId the parent of the new user, or -1 if the user has no parent. If parentId is |
| 581 | * specified then the new user's keystore will be intialized with the same secure lockscreen |
| 582 | * password as the parent. |
| 583 | */ |
| 584 | public void onUserAdded(int userId, int parentId) { |
| 585 | try { |
| 586 | mBinder.onUserAdded(userId, parentId); |
| 587 | } catch (RemoteException e) { |
| 588 | Log.w(TAG, "Cannot connect to keystore", e); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Notify keystore that a user was added. |
| 594 | * |
| 595 | * @param userId the new user. |
| 596 | */ |
| 597 | public void onUserAdded(int userId) { |
| 598 | onUserAdded(userId, -1); |
| 599 | } |
| 600 | |
| 601 | /** |
| 602 | * Notify keystore that a user was removed. |
| 603 | * |
| 604 | * @param userId the removed user. |
| 605 | */ |
| 606 | public void onUserRemoved(int userId) { |
| 607 | try { |
| 608 | mBinder.onUserRemoved(userId); |
| 609 | } catch (RemoteException e) { |
| 610 | Log.w(TAG, "Cannot connect to keystore", e); |
| 611 | } |
| 612 | } |
| 613 | |
Chad Brubaker | a91a850 | 2015-05-07 10:02:22 -0700 | [diff] [blame] | 614 | public boolean onUserPasswordChanged(String newPassword) { |
| 615 | return onUserPasswordChanged(UserHandle.getUserId(Process.myUid()), newPassword); |
| 616 | } |
| 617 | |
| 618 | /** |
Alex Klyubin | ad9ba10 | 2015-04-21 15:17:24 -0700 | [diff] [blame] | 619 | * Returns a {@link KeyStoreException} corresponding to the provided keystore/keymaster error |
| 620 | * code. |
| 621 | */ |
Alex Klyubin | dcdaf87 | 2015-05-13 15:57:09 -0700 | [diff] [blame] | 622 | public static KeyStoreException getKeyStoreException(int errorCode) { |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 623 | if (errorCode > 0) { |
| 624 | // KeyStore layer error |
| 625 | switch (errorCode) { |
| 626 | case NO_ERROR: |
| 627 | return new KeyStoreException(errorCode, "OK"); |
| 628 | case LOCKED: |
Alex Klyubin | 5418393 | 2015-05-08 15:25:48 -0700 | [diff] [blame] | 629 | return new KeyStoreException(errorCode, "User authentication required"); |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 630 | case UNINITIALIZED: |
| 631 | return new KeyStoreException(errorCode, "Keystore not initialized"); |
| 632 | case SYSTEM_ERROR: |
| 633 | return new KeyStoreException(errorCode, "System error"); |
| 634 | case PERMISSION_DENIED: |
| 635 | return new KeyStoreException(errorCode, "Permission denied"); |
| 636 | case KEY_NOT_FOUND: |
| 637 | return new KeyStoreException(errorCode, "Key not found"); |
| 638 | case VALUE_CORRUPTED: |
| 639 | return new KeyStoreException(errorCode, "Key blob corrupted"); |
Alex Klyubin | 058de02 | 2015-04-29 17:32:00 -0700 | [diff] [blame] | 640 | case OP_AUTH_NEEDED: |
| 641 | return new KeyStoreException(errorCode, "Operation requires authorization"); |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 642 | default: |
| 643 | return new KeyStoreException(errorCode, String.valueOf(errorCode)); |
| 644 | } |
| 645 | } else { |
| 646 | // Keymaster layer error |
| 647 | switch (errorCode) { |
| 648 | case KeymasterDefs.KM_ERROR_INVALID_AUTHORIZATION_TIMEOUT: |
| 649 | // The name of this parameter significantly differs between Keymaster and |
| 650 | // framework APIs. Use the framework wording to make life easier for developers. |
| 651 | return new KeyStoreException(errorCode, |
| 652 | "Invalid user authentication validity duration"); |
| 653 | default: |
| 654 | return new KeyStoreException(errorCode, |
| 655 | KeymasterDefs.getErrorMessage(errorCode)); |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
Alex Klyubin | ad9ba10 | 2015-04-21 15:17:24 -0700 | [diff] [blame] | 660 | /** |
| 661 | * Returns an {@link InvalidKeyException} corresponding to the provided |
| 662 | * {@link KeyStoreException}. |
| 663 | */ |
Alex Klyubin | dcdaf87 | 2015-05-13 15:57:09 -0700 | [diff] [blame] | 664 | public InvalidKeyException getInvalidKeyException( |
| 665 | String keystoreKeyAlias, KeyStoreException e) { |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 666 | switch (e.getErrorCode()) { |
Alex Klyubin | 5418393 | 2015-05-08 15:25:48 -0700 | [diff] [blame] | 667 | case LOCKED: |
| 668 | return new UserNotAuthenticatedException(); |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 669 | case KeymasterDefs.KM_ERROR_KEY_EXPIRED: |
| 670 | return new KeyExpiredException(); |
| 671 | case KeymasterDefs.KM_ERROR_KEY_NOT_YET_VALID: |
| 672 | return new KeyNotYetValidException(); |
| 673 | case KeymasterDefs.KM_ERROR_KEY_USER_NOT_AUTHENTICATED: |
Alex Klyubin | 058de02 | 2015-04-29 17:32:00 -0700 | [diff] [blame] | 674 | case OP_AUTH_NEEDED: |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 675 | { |
| 676 | // We now need to determine whether the key/operation can become usable if user |
| 677 | // authentication is performed, or whether it can never become usable again. |
| 678 | // User authentication requirements are contained in the key's characteristics. We |
| 679 | // need to check whether these requirements can be be satisfied by asking the user |
| 680 | // to authenticate. |
| 681 | KeyCharacteristics keyCharacteristics = new KeyCharacteristics(); |
| 682 | int getKeyCharacteristicsErrorCode = |
| 683 | getKeyCharacteristics(keystoreKeyAlias, null, null, keyCharacteristics); |
| 684 | if (getKeyCharacteristicsErrorCode != NO_ERROR) { |
| 685 | return new InvalidKeyException( |
| 686 | "Failed to obtained key characteristics", |
| 687 | getKeyStoreException(getKeyCharacteristicsErrorCode)); |
| 688 | } |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 689 | List<BigInteger> keySids = |
| 690 | keyCharacteristics.getUnsignedLongs(KeymasterDefs.KM_TAG_USER_SECURE_ID); |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 691 | if (keySids.isEmpty()) { |
| 692 | // Key is not bound to any SIDs -- no amount of authentication will help here. |
| 693 | return new KeyPermanentlyInvalidatedException(); |
| 694 | } |
| 695 | long rootSid = GateKeeper.getSecureUserId(); |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 696 | if ((rootSid != 0) && (keySids.contains(KeymasterArguments.toUint64(rootSid)))) { |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 697 | // One of the key's SIDs is the current root SID -- user can be authenticated |
| 698 | // against that SID. |
| 699 | return new UserNotAuthenticatedException(); |
| 700 | } |
| 701 | |
| 702 | long fingerprintOnlySid = getFingerprintOnlySid(); |
| 703 | if ((fingerprintOnlySid != 0) |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 704 | && (keySids.contains(KeymasterArguments.toUint64(fingerprintOnlySid)))) { |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 705 | // One of the key's SIDs is the current fingerprint SID -- user can be |
| 706 | // authenticated against that SID. |
| 707 | return new UserNotAuthenticatedException(); |
| 708 | } |
| 709 | |
| 710 | // None of the key's SIDs can ever be authenticated |
| 711 | return new KeyPermanentlyInvalidatedException(); |
| 712 | } |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 713 | default: |
Alex Klyubin | ad9ba10 | 2015-04-21 15:17:24 -0700 | [diff] [blame] | 714 | return new InvalidKeyException("Keystore operation failed", e); |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
Alex Klyubin | 2d7a85c | 2015-04-30 11:43:53 -0700 | [diff] [blame] | 718 | private long getFingerprintOnlySid() { |
Alex Klyubin | a99b8b5 | 2015-06-11 13:27:34 -0700 | [diff] [blame] | 719 | FingerprintManager fingerprintManager = mContext.getSystemService(FingerprintManager.class); |
Alex Klyubin | 2d7a85c | 2015-04-30 11:43:53 -0700 | [diff] [blame] | 720 | if (fingerprintManager == null) { |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 721 | return 0; |
| 722 | } |
| 723 | |
Alex Klyubin | a99b8b5 | 2015-06-11 13:27:34 -0700 | [diff] [blame] | 724 | // TODO: Restore USE_FINGERPRINT permission check in |
| 725 | // FingerprintManager.getAuthenticatorId once the ID is no longer needed here. |
Alex Klyubin | 2d7a85c | 2015-04-30 11:43:53 -0700 | [diff] [blame] | 726 | return fingerprintManager.getAuthenticatorId(); |
Svetoslav | 2dac95d | 2015-04-30 11:30:33 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Alex Klyubin | ad9ba10 | 2015-04-21 15:17:24 -0700 | [diff] [blame] | 729 | /** |
| 730 | * Returns an {@link InvalidKeyException} corresponding to the provided keystore/keymaster error |
| 731 | * code. |
| 732 | */ |
Alex Klyubin | dcdaf87 | 2015-05-13 15:57:09 -0700 | [diff] [blame] | 733 | public InvalidKeyException getInvalidKeyException(String keystoreKeyAlias, int errorCode) { |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 734 | return getInvalidKeyException(keystoreKeyAlias, getKeyStoreException(errorCode)); |
Alex Klyubin | b4834ae | 2015-04-02 15:53:46 -0700 | [diff] [blame] | 735 | } |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 736 | } |