summaryrefslogtreecommitdiffstats
blob: 227ea8919e40b9184171571b185d89a0aeec5066 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.car.trust;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGattServer;
import android.bluetooth.BluetoothGattServerCallback;
import android.bluetooth.BluetoothManager;
import android.car.trust.ICarTrustAgentBleService;
import android.car.trust.ICarTrustAgentTokenRequestDelegate;
import android.car.trust.ICarTrustAgentUnlockCallback;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserManager;
import android.service.trust.TrustAgentService;
import android.util.Log;

import java.util.concurrent.TimeUnit;

/**
 * A BluetoothLE (BLE) based {@link TrustAgentService} that uses the escrow token unlock APIs. </p>
 *
 * This trust agent runs during direct boot and binds to a BLE service that listens for remote
 * devices to trigger an unlock. <p/>
 *
 * The permissions for this agent must be enabled as priv-app permissions for it to start.
 */
public class CarBleTrustAgent extends TrustAgentService {

    private static final String TAG = CarBleTrustAgent.class.getSimpleName();

    private static final long TRUST_DURATION_MS = TimeUnit.MINUTES.toMicros(5);
    private static final long BLE_RETRY_MS = TimeUnit.SECONDS.toMillis(1);

    private Handler mHandler;
    private BluetoothManager mBluetoothManager;
    private ICarTrustAgentBleService mCarTrustAgentBleService;
    private boolean mCarTrustAgentBleServiceBound;

    private final ICarTrustAgentUnlockCallback mUnlockCallback =
            new ICarTrustAgentUnlockCallback.Stub() {
        @Override
        public void onUnlockDataReceived(byte[] token, long handle) {
            UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
            // TODO(b/77854782): get the actual user to unlock by token
            UserHandle userHandle = getForegroundUserHandle();

            Log.d(TAG, "About to unlock user. Handle: " + handle
                    + " Time: " + System.currentTimeMillis());
            unlockUserWithToken(handle, token, userHandle);

            Log.d(TAG, "Attempted to unlock user, is user unlocked: "
                    + um.isUserUnlocked(userHandle)
                    + " Time: " + System.currentTimeMillis());
            setManagingTrust(true);

            if (um.isUserUnlocked(userHandle)) {
                Log.d(TAG, getString(R.string.trust_granted_explanation));
                grantTrust("Granting trust from escrow token",
                        TRUST_DURATION_MS, FLAG_GRANT_TRUST_DISMISS_KEYGUARD);
            }
        }
    };

    private final ICarTrustAgentTokenRequestDelegate mTokenRequestDelegate =
            new ICarTrustAgentTokenRequestDelegate.Stub() {
        @Override
        public void revokeTrust() {
            CarBleTrustAgent.this.revokeTrust();
        }

        @Override
        public void addEscrowToken(byte[] token, int uid) {
            CarBleTrustAgent.this.addEscrowToken(token, UserHandle.of(uid));
        }

        @Override
        public void removeEscrowToken(long handle, int uid) {
            CarBleTrustAgent.this.removeEscrowToken(handle, UserHandle.of(uid));
        }

        @Override
        public void isEscrowTokenActive(long handle, int uid) {
            CarBleTrustAgent.this.isEscrowTokenActive(handle, UserHandle.of(uid));
        }
    };

    private final ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "CarTrustAgentBleService connected");
            mCarTrustAgentBleServiceBound = true;
            mCarTrustAgentBleService = ICarTrustAgentBleService.Stub.asInterface(service);
            try {
                mCarTrustAgentBleService.registerUnlockCallback(mUnlockCallback);
                mCarTrustAgentBleService.setTokenRequestDelegate(mTokenRequestDelegate);
                maybeStartBleUnlockService();
            } catch (RemoteException e) {
                Log.e(TAG, "Error registerUnlockCallback", e);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            if (mCarTrustAgentBleService != null) {
                try {
                    mCarTrustAgentBleService.unregisterUnlockCallback(mUnlockCallback);
                    mCarTrustAgentBleService.setTokenRequestDelegate(null);
                } catch (RemoteException e) {
                    Log.e(TAG, "Error unregisterUnlockCallback", e);
                }
                mCarTrustAgentBleService = null;
                mCarTrustAgentBleServiceBound = false;
            }
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();

        Log.d(TAG, "Bluetooth trust agent starting up");
        mHandler = new Handler();
        mBluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);

        // If the user is already unlocked, don't bother starting the BLE service.
        UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
        if (!um.isUserUnlocked(getForegroundUserHandle())) {
            Log.d(TAG, "User locked, will now bind CarTrustAgentBleService");
            Intent intent = new Intent(this, CarTrustAgentBleService.class);
            bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
        } else {
            setManagingTrust(true);
        }
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "Car Trust agent shutting down");
        mHandler.removeCallbacks(null);

        // Unbind the service to avoid leaks from BLE stack.
        if (mCarTrustAgentBleServiceBound) {
            unbindService(mServiceConnection);
        }
        super.onDestroy();
    }

    @Override
    public void onDeviceLocked() {
        super.onDeviceLocked();
        if (mCarTrustAgentBleServiceBound) {
            try {
                // Only one BLE advertising is allowed, ensure enrolment advertising is stopped
                // before start unlock advertising.
                mCarTrustAgentBleService.stopEnrolmentAdvertising();
                mCarTrustAgentBleService.startUnlockAdvertising();
            } catch (RemoteException e) {
                Log.e(TAG, "Error startUnlockAdvertising", e);
            }
        }
    }

    @Override
    public void onDeviceUnlocked() {
        super.onDeviceUnlocked();
        if (mCarTrustAgentBleServiceBound) {
            try {
                // Only one BLE advertising is allowed, ensure unlock advertising is stopped.
                mCarTrustAgentBleService.stopUnlockAdvertising();
            } catch (RemoteException e) {
                Log.e(TAG, "Error stopUnlockAdvertising", e);
            }
        }
    }

    private void maybeStartBleUnlockService() {
        Log.d(TAG, "Trying to open a Ble GATT server");
        BluetoothGattServer gattServer = mBluetoothManager.openGattServer(
                this, new BluetoothGattServerCallback() {
            @Override
            public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
                super.onConnectionStateChange(device, status, newState);
            }
        });

        // The BLE stack is started up before the trust agent service, however Gatt capabilities
        // might not be ready just yet. Keep trying until a GattServer can open up before proceeding
        // to start the rest of the BLE services.
        if (gattServer == null) {
            Log.e(TAG, "Gatt not available, will try again...in " + BLE_RETRY_MS + "ms");
            mHandler.postDelayed(this::maybeStartBleUnlockService, BLE_RETRY_MS);
        } else {
            mHandler.removeCallbacks(null);
            gattServer.close();
            Log.d(TAG, "GATT available, starting up UnlockService");
            try {
                mCarTrustAgentBleService.startUnlockAdvertising();
            } catch (RemoteException e) {
                Log.e(TAG, "Error startUnlockAdvertising", e);
            }
        }
    }

    @Override
    public void onEscrowTokenRemoved(long handle, boolean successful) {
        if (mCarTrustAgentBleServiceBound) {
            try {
                mCarTrustAgentBleService.onEscrowTokenRemoved(handle, successful);
                Log.v(TAG, "Callback onEscrowTokenRemoved");
            } catch (RemoteException e) {
                Log.e(TAG, "Error callback onEscrowTokenRemoved", e);
            }
        }
    }

    @Override
    public void onEscrowTokenStateReceived(long handle, int tokenState) {
        boolean isActive = tokenState == TOKEN_STATE_ACTIVE;
        if (mCarTrustAgentBleServiceBound) {
            try {
                mCarTrustAgentBleService.onEscrowTokenActiveStateChanged(handle, isActive);
                Log.v(TAG, "Callback onEscrowTokenActiveStateChanged");
            } catch (RemoteException e) {
                Log.e(TAG, "Error callback onEscrowTokenActiveStateChanged", e);
            }
        }
    }

    @Override
    public void onEscrowTokenAdded(byte[] token, long handle, UserHandle user) {
        if (mCarTrustAgentBleServiceBound) {
            try {
                mCarTrustAgentBleService.onEscrowTokenAdded(token, handle, user.getIdentifier());
                Log.v(TAG, "Callback onEscrowTokenAdded");
            } catch (RemoteException e) {
                Log.e(TAG, "Error callback onEscrowTokenAdded", e);
            }
        }
    }

    /**
     * TODO(b/77854782): return the {@link UserHandle} of foreground user.
     * CarBleTrustAgent itself runs as user-0
     */
    private UserHandle getForegroundUserHandle() {
        Log.d(TAG, "getForegroundUserHandle for " + UserHandle.myUserId());
        return UserHandle.of(UserHandle.myUserId());
    }
}