Ram Periathiruvadi | de0ca08 | 2019-03-20 11:16:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 com.android.car.trust; |
| 18 | |
| 19 | import android.app.ActivityManager; |
| 20 | import android.bluetooth.BluetoothAdapter; |
| 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.IntentFilter; |
| 25 | import android.os.UserHandle; |
| 26 | import android.os.UserManager; |
| 27 | import android.service.trust.TrustAgentService; |
| 28 | import android.util.Log; |
| 29 | |
| 30 | import com.android.car.CarLocalServices; |
| 31 | import com.android.car.Utils; |
| 32 | import com.android.car.trust.CarTrustAgentEnrollmentService.CarTrustAgentEnrollmentRequestDelegate; |
| 33 | import com.android.car.trust.CarTrustAgentUnlockService.CarTrustAgentUnlockDelegate; |
| 34 | |
| 35 | /** |
| 36 | * A BluetoothLE (BLE) based {@link TrustAgentService} that uses the escrow token unlock APIs. |
| 37 | * <p> |
| 38 | * This trust agent runs during direct boot and interacts with {@link CarTrustedDeviceService} |
| 39 | * to listen for remote devices to trigger an unlock. |
| 40 | * <p> |
| 41 | * The system {@link com.android.server.trust.TrustManagerService} binds to this agent and uses |
| 42 | * the data it receives from this agent to authorize a user in lieu of the PIN/Pattern/Password |
| 43 | * credentials. |
| 44 | */ |
| 45 | public class CarBleTrustAgent extends TrustAgentService { |
| 46 | private static final String TAG = CarBleTrustAgent.class.getSimpleName(); |
| 47 | private boolean mIsDeviceLocked; |
| 48 | private CarTrustedDeviceService mCarTrustedDeviceService; |
| 49 | private CarTrustAgentEnrollmentService mCarTrustAgentEnrollmentService; |
| 50 | private CarTrustAgentUnlockService mCarTrustAgentUnlockService; |
| 51 | |
| 52 | @Override |
| 53 | public void onCreate() { |
| 54 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 55 | Log.d(TAG, "onCreate()"); |
| 56 | } |
| 57 | super.onCreate(); |
| 58 | // Registering for more granular BLE specific state changes as against Bluetooth state |
| 59 | // changes, helps with reducing latency in getting notified. |
| 60 | IntentFilter intentFilter = new IntentFilter(BluetoothAdapter.ACTION_BLE_STATE_CHANGED); |
| 61 | registerReceiver(mBluetoothBroadcastReceiver, intentFilter); |
| 62 | |
| 63 | // TODO(b/129144535) handle scenarios where CarService crashed. Maybe retrieve this |
| 64 | // every time we need instead of caching. |
| 65 | mCarTrustedDeviceService = CarLocalServices.getService(CarTrustedDeviceService.class); |
| 66 | if (mCarTrustedDeviceService == null) { |
| 67 | Log.e(TAG, "Cannot retrieve the Trusted device Service"); |
| 68 | return; |
| 69 | } |
| 70 | mCarTrustAgentEnrollmentService = |
| 71 | mCarTrustedDeviceService.getCarTrustAgentEnrollmentService(); |
| 72 | setEnrollmentRequestDelegate(); |
| 73 | mCarTrustAgentUnlockService = mCarTrustedDeviceService.getCarTrustAgentUnlockService(); |
| 74 | setUnlockRequestDelegate(); |
| 75 | setManagingTrust(true); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void onDestroy() { |
| 80 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 81 | Log.d(TAG, "Car Trust agent shutting down"); |
| 82 | } |
| 83 | super.onDestroy(); |
| 84 | mCarTrustAgentEnrollmentService = null; |
| 85 | if (mBluetoothBroadcastReceiver != null) { |
| 86 | unregisterReceiver(mBluetoothBroadcastReceiver); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Overriding TrustAgentService methods |
| 91 | @Override |
| 92 | public void onDeviceLocked() { |
| 93 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 94 | Log.d(TAG, "onDeviceLocked Current user: " + ActivityManager.getCurrentUser()); |
| 95 | } |
| 96 | super.onDeviceLocked(); |
| 97 | mIsDeviceLocked = true; |
| 98 | if (BluetoothAdapter.getDefaultAdapter().getState() == BluetoothAdapter.STATE_OFF) { |
| 99 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 100 | Log.d(TAG, "Not starting Unlock Advertising yet, since Bluetooth Adapter is off"); |
| 101 | } |
| 102 | return; |
| 103 | } |
| 104 | if (mCarTrustAgentUnlockService != null) { |
| 105 | mCarTrustAgentUnlockService.startUnlockAdvertising(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void onDeviceUnlocked() { |
| 111 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 112 | Log.d(TAG, "onDeviceUnlocked Current user: " + ActivityManager.getCurrentUser()); |
| 113 | } |
| 114 | super.onDeviceUnlocked(); |
| 115 | mIsDeviceLocked = false; |
| 116 | if (BluetoothAdapter.getDefaultAdapter().getState() == BluetoothAdapter.STATE_OFF) { |
| 117 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 118 | Log.d(TAG, "Not stopping Unlock Advertising, since Bluetooth Adapter is off"); |
| 119 | } |
| 120 | return; |
| 121 | } |
| 122 | if (mCarTrustAgentUnlockService != null) { |
| 123 | mCarTrustAgentUnlockService.stopUnlockAdvertising(); |
| 124 | |
| 125 | } |
Ram Periathiruvadi | 9d94c6b | 2019-03-20 11:16:44 -0700 | [diff] [blame] | 126 | revokeTrust(); |
Ram Periathiruvadi | de0ca08 | 2019-03-20 11:16:44 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void onEscrowTokenRemoved(long handle, boolean successful) { |
Ram Periathiruvadi | 9d94c6b | 2019-03-20 11:16:44 -0700 | [diff] [blame] | 131 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 132 | Log.d(TAG, "onEscrowTokenRemoved handle: " + Long.toHexString(handle)); |
| 133 | } |
| 134 | if (mCarTrustAgentEnrollmentService == null) { |
| 135 | return; |
| 136 | } |
| 137 | if (successful) { |
| 138 | mCarTrustAgentEnrollmentService.onEscrowTokenRemoved(handle, |
| 139 | ActivityManager.getCurrentUser()); |
| 140 | } |
Ram Periathiruvadi | de0ca08 | 2019-03-20 11:16:44 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void onEscrowTokenStateReceived(long handle, int tokenState) { |
| 145 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 146 | Log.d(TAG, "onEscrowTokenStateReceived: " + Long.toHexString(handle) + " state: " |
| 147 | + tokenState); |
| 148 | } |
| 149 | if (mCarTrustAgentEnrollmentService == null) { |
| 150 | return; |
| 151 | } |
| 152 | mCarTrustAgentEnrollmentService.onEscrowTokenActiveStateChanged(handle, |
| 153 | tokenState == TOKEN_STATE_ACTIVE); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void onEscrowTokenAdded(byte[] token, long handle, UserHandle user) { |
| 158 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 159 | Log.d(TAG, "onEscrowTokenAdded handle: " + Long.toHexString(handle) + "token: " |
| 160 | + Utils.byteArrayToHexString(token)); |
| 161 | } |
| 162 | if (mCarTrustAgentEnrollmentService == null) { |
| 163 | return; |
| 164 | } |
| 165 | mCarTrustAgentEnrollmentService.onEscrowTokenAdded(token, handle, user.getIdentifier()); |
| 166 | } |
| 167 | |
| 168 | private void setEnrollmentRequestDelegate() { |
| 169 | if (mCarTrustAgentEnrollmentService == null) { |
| 170 | return; |
| 171 | } |
| 172 | mCarTrustAgentEnrollmentService.setEnrollmentRequestDelegate(mEnrollDelegate); |
| 173 | } |
| 174 | |
| 175 | private void setUnlockRequestDelegate() { |
| 176 | if (mCarTrustAgentUnlockService == null) { |
| 177 | return; |
| 178 | } |
| 179 | mCarTrustAgentUnlockService.setUnlockRequestDelegate(mUnlockDelegate); |
| 180 | } |
| 181 | |
| 182 | private void unlockUserInternally(int uid, byte[] token, long handle) { |
| 183 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 184 | Log.d(TAG, "About to unlock user: " + uid); |
| 185 | UserManager um = (UserManager) getSystemService(Context.USER_SERVICE); |
| 186 | if (um.isUserUnlocked(UserHandle.of(uid))) { |
| 187 | Log.d(TAG, "User currently unlocked"); |
| 188 | } else { |
| 189 | Log.d(TAG, "User currently locked"); |
| 190 | } |
| 191 | } |
| 192 | unlockUserWithToken(handle, token, UserHandle.of(uid)); |
| 193 | grantTrust("Granting trust from escrow token", |
| 194 | 0, FLAG_GRANT_TRUST_DISMISS_KEYGUARD); |
| 195 | } |
| 196 | |
| 197 | private final BroadcastReceiver mBluetoothBroadcastReceiver = new BroadcastReceiver() { |
| 198 | @Override |
| 199 | public void onReceive(Context context, Intent intent) { |
| 200 | if (intent.getAction() != null && BluetoothAdapter.ACTION_BLE_STATE_CHANGED.equals( |
| 201 | intent.getAction())) { |
| 202 | onBluetoothStateChanged(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)); |
| 203 | } |
| 204 | } |
| 205 | }; |
| 206 | |
| 207 | private void onBluetoothStateChanged(int state) { |
| 208 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 209 | Log.d(TAG, "onBluetoothStateChanged: " + state); |
| 210 | } |
| 211 | if (!mIsDeviceLocked) { |
| 212 | return; |
| 213 | } |
| 214 | switch (state) { |
| 215 | case BluetoothAdapter.STATE_BLE_ON: |
| 216 | if (mCarTrustAgentUnlockService != null) { |
| 217 | mCarTrustAgentUnlockService.startUnlockAdvertising(); |
| 218 | } |
| 219 | break; |
| 220 | case BluetoothAdapter.STATE_OFF: |
| 221 | Log.e(TAG, "Bluetooth Adapter Off in lock screen"); |
| 222 | if (mCarTrustedDeviceService != null) { |
| 223 | mCarTrustedDeviceService.cleanupBleService(); |
| 224 | } |
| 225 | break; |
| 226 | default: |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // Implementing Delegates for Enrollment and Unlock. The CarBleTrustAgent acts as the interface |
| 232 | // between the Trust Agent framework and the Car Service. The Car service handles communicating |
| 233 | // with the peer device part and the framework handles the actual authentication. The |
| 234 | // CarBleTrustAgent abstracts these 2 pieces from each other. |
| 235 | /** |
| 236 | * Implementation of the {@link CarTrustAgentEnrollmentRequestDelegate} |
| 237 | */ |
| 238 | private final CarTrustAgentEnrollmentRequestDelegate mEnrollDelegate = |
| 239 | new CarTrustAgentEnrollmentRequestDelegate() { |
| 240 | @Override |
Ram Periathiruvadi | de0ca08 | 2019-03-20 11:16:44 -0700 | [diff] [blame] | 241 | public void addEscrowToken(byte[] token, int uid) { |
| 242 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 243 | Log.d(TAG, |
| 244 | "addEscrowToken. uid: " + uid + " token: " |
| 245 | + Utils.byteArrayToHexString( |
| 246 | token)); |
| 247 | } |
| 248 | CarBleTrustAgent.this.addEscrowToken(token, UserHandle.of(uid)); |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public void removeEscrowToken(long handle, int uid) { |
Ram Periathiruvadi | 9d94c6b | 2019-03-20 11:16:44 -0700 | [diff] [blame] | 253 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 254 | Log.d(TAG, |
| 255 | "removeEscrowToken. uid: " + ActivityManager.getCurrentUser() |
| 256 | + " handle: " + handle); |
| 257 | } |
| 258 | CarBleTrustAgent.this.removeEscrowToken(handle, |
| 259 | UserHandle.of(uid)); |
Ram Periathiruvadi | de0ca08 | 2019-03-20 11:16:44 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public void isEscrowTokenActive(long handle, int uid) { |
| 264 | CarBleTrustAgent.this.isEscrowTokenActive(handle, UserHandle.of(uid)); |
| 265 | } |
| 266 | }; |
| 267 | |
| 268 | /** |
| 269 | * Implementation of the {@link CarTrustAgentUnlockDelegate} |
| 270 | */ |
| 271 | private final CarTrustAgentUnlockDelegate mUnlockDelegate = new CarTrustAgentUnlockDelegate() { |
| 272 | /** |
| 273 | * Pass the user and token credentials to authenticate with the LockSettingsService. |
| 274 | * |
| 275 | * @param user user being authorized |
| 276 | * @param token escrow token for the user |
| 277 | * @param handle the handle corresponding to the escrow token |
| 278 | */ |
| 279 | @Override |
| 280 | public void onUnlockDataReceived(int user, byte[] token, long handle) { |
| 281 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 282 | Log.d(TAG, "onUnlockDataReceived:" + user + " token: " + Long.toHexString( |
| 283 | Utils.bytesToLong(token)) + " handle: " + Long.toHexString(handle)); |
| 284 | } |
| 285 | if (ActivityManager.getCurrentUser() != user) { |
| 286 | // Current behavior is to only authenticate the user we have booted into. |
| 287 | // TODO(b/129029418) Make identification & Auth vs Auth-only a |
| 288 | // configurable option |
| 289 | Log.e(TAG, "Expected User: " + ActivityManager.getCurrentUser() |
| 290 | + " Presented User: " + user); |
| 291 | return; |
| 292 | } else { |
| 293 | unlockUserInternally(user, token, handle); |
| 294 | } |
| 295 | |
| 296 | } |
| 297 | }; |
| 298 | } |