blob: 7987167662dc9bd76be2bac90343fa65a21aa012 [file] [log] [blame]
Andres Morales2d08dce2015-04-03 16:40:15 -07001/*
2 * Copyright (C) 2015 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 */
Andres Morales2d08dce2015-04-03 16:40:15 -070016#define LOG_TAG "gatekeeperd"
17
Pawan Waghc5c0c302023-04-21 22:04:31 +000018#include "gatekeeperd.h"
Andres Morales2d08dce2015-04-03 16:40:15 -070019
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000020#include <endian.h>
Andres Morales6a49c2f2015-04-16 13:16:24 -070021#include <errno.h>
Andres Morales6a49c2f2015-04-16 13:16:24 -070022#include <fcntl.h>
23#include <unistd.h>
Justin Yun68b0ec62017-08-16 18:54:20 +090024#include <memory>
Andres Morales6a49c2f2015-04-16 13:16:24 -070025
Subrahmanyaman355e9772022-07-21 03:51:30 +000026#include <KeyMintUtils.h>
David Anderson97400bd2019-02-15 15:59:39 -080027#include <android-base/logging.h>
28#include <android-base/properties.h>
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000029#include <android/binder_ibinder.h>
30#include <android/binder_manager.h>
Andres Morales2d08dce2015-04-03 16:40:15 -070031#include <binder/IPCThreadState.h>
32#include <binder/IServiceManager.h>
33#include <binder/PermissionCache.h>
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000034#include <gatekeeper/password_handle.h> // for password_handle_t
Andres Morales6a49c2f2015-04-16 13:16:24 -070035#include <hardware/hw_auth_token.h>
David Anderson97400bd2019-02-15 15:59:39 -080036#include <libgsi/libgsi.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080037#include <log/log.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070038#include <utils/String16.h>
Andres Morales2d08dce2015-04-03 16:40:15 -070039
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000040#include <aidl/android/hardware/security/keymint/HardwareAuthToken.h>
41#include <aidl/android/security/authorization/IKeystoreAuthorization.h>
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000042#include <hidl/HidlSupport.h>
Alexey Polyudov275aece2016-10-18 13:59:26 -070043
44using android::sp;
Louis Chang4c66b8a2021-01-18 10:01:12 +000045using android::hardware::Return;
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000046using android::hardware::gatekeeper::V1_0::GatekeeperResponse;
47using android::hardware::gatekeeper::V1_0::GatekeeperStatusCode;
Alexey Polyudov275aece2016-10-18 13:59:26 -070048
Subrahmanyaman355e9772022-07-21 03:51:30 +000049using AidlGatekeeperEnrollResp = aidl::android::hardware::gatekeeper::GatekeeperEnrollResponse;
50using AidlGatekeeperVerifyResp = aidl::android::hardware::gatekeeper::GatekeeperVerifyResponse;
Subrahmanyaman355e9772022-07-21 03:51:30 +000051
Janis Danisevskis3a1eb672019-03-29 11:14:31 -070052using GKResponseCode = ::android::service::gatekeeper::ResponseCode;
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000053using ::aidl::android::hardware::security::keymint::HardwareAuthenticatorType;
54using ::aidl::android::hardware::security::keymint::HardwareAuthToken;
Subrahmanyaman355e9772022-07-21 03:51:30 +000055using ::aidl::android::hardware::security::keymint::km_utils::authToken2AidlVec;
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000056using ::aidl::android::security::authorization::IKeystoreAuthorization;
Janis Danisevskis3a1eb672019-03-29 11:14:31 -070057
Andres Morales2d08dce2015-04-03 16:40:15 -070058namespace android {
59
60static const String16 KEYGUARD_PERMISSION("android.permission.ACCESS_KEYGUARD_SECURE_STORAGE");
61static const String16 DUMP_PERMISSION("android.permission.DUMP");
Subrahmanyaman355e9772022-07-21 03:51:30 +000062constexpr const char gatekeeperServiceName[] = "android.hardware.gatekeeper.IGatekeeper/default";
Andres Morales2d08dce2015-04-03 16:40:15 -070063
Pawan Waghc5c0c302023-04-21 22:04:31 +000064GateKeeperProxy::GateKeeperProxy() {
65 clear_state_if_needed_done = false;
66 hw_device = IGatekeeper::getService();
67 ::ndk::SpAIBinder ks2Binder(AServiceManager_getService(gatekeeperServiceName));
68 aidl_hw_device = AidlIGatekeeper::fromBinder(ks2Binder);
69 is_running_gsi = android::base::GetBoolProperty(android::gsi::kGsiBootedProp, false);
Andres Moralesfef908e2015-07-07 10:28:15 -070070
Pawan Waghc5c0c302023-04-21 22:04:31 +000071 if (!aidl_hw_device && !hw_device) {
72 LOG(ERROR) << "Could not find Gatekeeper device, which makes me very sad.";
73 }
74}
75
76void GateKeeperProxy::store_sid(uint32_t userId, uint64_t sid) {
77 char filename[21];
78 snprintf(filename, sizeof(filename), "%u", userId);
79 int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
80 if (fd < 0) {
81 ALOGE("could not open file: %s: %s", filename, strerror(errno));
82 return;
83 }
84 write(fd, &sid, sizeof(sid));
85 close(fd);
86}
87
88void GateKeeperProxy::clear_state_if_needed() {
89 if (clear_state_if_needed_done) {
90 return;
91 }
92
93 if (mark_cold_boot() && !is_running_gsi) {
94 ALOGI("cold boot: clearing state");
95 if (aidl_hw_device) {
96 aidl_hw_device->deleteAllUsers();
97 } else if (hw_device) {
98 hw_device->deleteAllUsers([](const GatekeeperResponse&) {});
Andres Morales33dfdc72015-05-12 15:37:20 -070099 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700100 }
101
Pawan Waghc5c0c302023-04-21 22:04:31 +0000102 clear_state_if_needed_done = true;
103}
Andres Morales2d08dce2015-04-03 16:40:15 -0700104
Pawan Waghc5c0c302023-04-21 22:04:31 +0000105bool GateKeeperProxy::mark_cold_boot() {
106 const char* filename = ".coldboot";
107 if (access(filename, F_OK) == -1) {
Andres Morales6a49c2f2015-04-16 13:16:24 -0700108 int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
109 if (fd < 0) {
Pawan Waghc5c0c302023-04-21 22:04:31 +0000110 ALOGE("could not open file: %s : %s", filename, strerror(errno));
111 return false;
Andres Morales6a49c2f2015-04-16 13:16:24 -0700112 }
Andres Morales6a49c2f2015-04-16 13:16:24 -0700113 close(fd);
Pawan Waghc5c0c302023-04-21 22:04:31 +0000114 return true;
Andres Morales6a49c2f2015-04-16 13:16:24 -0700115 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000116 return false;
117}
Andres Morales6a49c2f2015-04-16 13:16:24 -0700118
Pawan Waghc5c0c302023-04-21 22:04:31 +0000119void GateKeeperProxy::maybe_store_sid(uint32_t userId, uint64_t sid) {
120 char filename[21];
121 snprintf(filename, sizeof(filename), "%u", userId);
122 if (access(filename, F_OK) == -1) {
123 store_sid(userId, sid);
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700124 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000125}
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700126
Pawan Waghc5c0c302023-04-21 22:04:31 +0000127uint64_t GateKeeperProxy::read_sid(uint32_t userId) {
128 char filename[21];
129 uint64_t sid;
130 snprintf(filename, sizeof(filename), "%u", userId);
131 int fd = open(filename, O_RDONLY);
132 if (fd < 0) return 0;
133 read(fd, &sid, sizeof(sid));
134 close(fd);
135 return sid;
136}
Andres Morales3c2086d2015-06-24 10:21:16 -0700137
Pawan Waghc5c0c302023-04-21 22:04:31 +0000138void GateKeeperProxy::clear_sid(uint32_t userId) {
139 char filename[21];
140 snprintf(filename, sizeof(filename), "%u", userId);
141 if (remove(filename) < 0 && errno != ENOENT) {
142 ALOGE("%s: could not remove file [%s], attempting 0 write", __func__, strerror(errno));
143 store_sid(userId, 0);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700144 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000145}
Andres Morales6a49c2f2015-04-16 13:16:24 -0700146
Pawan Waghd2d320a2023-05-15 22:25:55 +0000147Status GateKeeperProxy::adjust_userId(uint32_t userId, uint32_t* hw_userId) {
Pawan Waghc5c0c302023-04-21 22:04:31 +0000148 static constexpr uint32_t kGsiOffset = 1000000;
Pawan Waghd2d320a2023-05-15 22:25:55 +0000149 if (userId >= kGsiOffset) {
150 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700151 }
Pawan Waghd2d320a2023-05-15 22:25:55 +0000152
153 if ((aidl_hw_device == nullptr) && (hw_device == nullptr)) {
154 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
155 }
156
157 if (is_running_gsi) {
158 *hw_userId = userId + kGsiOffset;
159 return Status::ok();
160 }
161 *hw_userId = userId;
162 return Status::ok();
Pawan Waghc5c0c302023-04-21 22:04:31 +0000163}
David Anderson97400bd2019-02-15 15:59:39 -0800164
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700165#define GK_ERROR *gkResponse = GKResponse::error(), Status::ok()
166
Pawan Waghc5c0c302023-04-21 22:04:31 +0000167Status GateKeeperProxy::enroll(int32_t userId,
168 const std::optional<std::vector<uint8_t>>& currentPasswordHandle,
169 const std::optional<std::vector<uint8_t>>& currentPassword,
170 const std::vector<uint8_t>& desiredPassword,
171 GKResponse* gkResponse) {
172 IPCThreadState* ipc = IPCThreadState::self();
173 const int calling_pid = ipc->getCallingPid();
174 const int calling_uid = ipc->getCallingUid();
175 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
176 return GK_ERROR;
177 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700178
Pawan Waghc5c0c302023-04-21 22:04:31 +0000179 // Make sure to clear any state from before factory reset as soon as a credential is
180 // enrolled (which may happen during device setup).
181 clear_state_if_needed();
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700182
Pawan Waghc5c0c302023-04-21 22:04:31 +0000183 // need a desired password to enroll
184 if (desiredPassword.size() == 0) return GK_ERROR;
Andres Morales33dfdc72015-05-12 15:37:20 -0700185
Pawan Waghc5c0c302023-04-21 22:04:31 +0000186 if (!aidl_hw_device && !hw_device) {
187 LOG(ERROR) << "has no HAL to talk to";
188 return GK_ERROR;
189 }
Andres Morales835d96e2015-06-03 15:06:24 -0700190
Pawan Waghc5c0c302023-04-21 22:04:31 +0000191 android::hardware::hidl_vec<uint8_t> curPwdHandle;
192 android::hardware::hidl_vec<uint8_t> curPwd;
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700193
Pawan Waghc5c0c302023-04-21 22:04:31 +0000194 if (currentPasswordHandle && currentPassword) {
195 if (hw_device) {
196 // Hidl Implementations expects passwordHandle to be in
197 // gatekeeper::password_handle_t format.
198 if (currentPasswordHandle->size() != sizeof(gatekeeper::password_handle_t)) {
199 LOG(INFO) << "Password handle has wrong length";
Subrahmanyaman355e9772022-07-21 03:51:30 +0000200 return GK_ERROR;
201 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000202 }
203 curPwdHandle.setToExternal(const_cast<uint8_t*>(currentPasswordHandle->data()),
204 currentPasswordHandle->size());
205 curPwd.setToExternal(const_cast<uint8_t*>(currentPassword->data()),
206 currentPassword->size());
207 }
208
209 android::hardware::hidl_vec<uint8_t> newPwd;
210 newPwd.setToExternal(const_cast<uint8_t*>(desiredPassword.data()), desiredPassword.size());
211
Pawan Waghd2d320a2023-05-15 22:25:55 +0000212 uint32_t hw_userId = 0;
213 Status result = adjust_userId(userId, &hw_userId);
214 if (!result.isOk()) {
215 return result;
216 }
217
Pawan Waghc5c0c302023-04-21 22:04:31 +0000218 uint64_t secureUserId = 0;
219 if (aidl_hw_device) {
220 // AIDL gatekeeper service
221 AidlGatekeeperEnrollResp rsp;
222 auto result = aidl_hw_device->enroll(hw_userId, curPwdHandle, curPwd, newPwd, &rsp);
223 if (!result.isOk()) {
224 LOG(ERROR) << "enroll transaction failed";
225 return GK_ERROR;
226 }
227 if (rsp.statusCode >= AidlIGatekeeper::STATUS_OK) {
228 *gkResponse = GKResponse::ok({rsp.data.begin(), rsp.data.end()});
229 secureUserId = static_cast<uint64_t>(rsp.secureUserId);
230 } else if (rsp.statusCode == AidlIGatekeeper::ERROR_RETRY_TIMEOUT && rsp.timeoutMs > 0) {
231 *gkResponse = GKResponse::retry(rsp.timeoutMs);
232 } else {
233 *gkResponse = GKResponse::error();
234 }
235 } else if (hw_device) {
236 // HIDL gatekeeper service
237 Return<void> hwRes = hw_device->enroll(
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700238 hw_userId, curPwdHandle, curPwd, newPwd,
239 [&gkResponse](const GatekeeperResponse& rsp) {
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700240 if (rsp.code >= GatekeeperStatusCode::STATUS_OK) {
241 *gkResponse = GKResponse::ok({rsp.data.begin(), rsp.data.end()});
242 } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT &&
243 rsp.timeout > 0) {
244 *gkResponse = GKResponse::retry(rsp.timeout);
245 } else {
246 *gkResponse = GKResponse::error();
Alexey Polyudov275aece2016-10-18 13:59:26 -0700247 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700248 });
Pawan Waghc5c0c302023-04-21 22:04:31 +0000249 if (!hwRes.isOk()) {
250 LOG(ERROR) << "enroll transaction failed";
251 return GK_ERROR;
252 }
253 if (gkResponse->response_code() == GKResponseCode::OK) {
254 if (gkResponse->payload().size() != sizeof(gatekeeper::password_handle_t)) {
255 LOG(ERROR) << "HAL returned password handle of invalid length "
256 << gkResponse->payload().size();
Subrahmanyaman355e9772022-07-21 03:51:30 +0000257 return GK_ERROR;
258 }
Subrahmanyaman355e9772022-07-21 03:51:30 +0000259
Pawan Waghc5c0c302023-04-21 22:04:31 +0000260 const gatekeeper::password_handle_t* handle =
Subrahmanyaman355e9772022-07-21 03:51:30 +0000261 reinterpret_cast<const gatekeeper::password_handle_t*>(
Pawan Waghc5c0c302023-04-21 22:04:31 +0000262 gkResponse->payload().data());
263 secureUserId = handle->user_id;
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700264 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700265 }
266
Pawan Waghc5c0c302023-04-21 22:04:31 +0000267 if (gkResponse->response_code() == GKResponseCode::OK && !gkResponse->should_reenroll()) {
268 store_sid(userId, secureUserId);
269
270 GKResponse verifyResponse;
271 // immediately verify this password so we don't ask the user to enter it again
272 // if they just created it.
273 auto status = verify(userId, gkResponse->payload(), desiredPassword, &verifyResponse);
274 if (!status.isOk() || verifyResponse.response_code() != GKResponseCode::OK) {
275 LOG(ERROR) << "Failed to verify password after enrolling";
276 }
Andres Moralesc828ae82015-04-10 21:03:07 -0700277 }
278
Pawan Waghc5c0c302023-04-21 22:04:31 +0000279 return Status::ok();
280}
281
282Status GateKeeperProxy::verify(int32_t userId, const ::std::vector<uint8_t>& enrolledPasswordHandle,
283 const ::std::vector<uint8_t>& providedPassword,
284 GKResponse* gkResponse) {
285 return verifyChallenge(userId, 0 /* challenge */, enrolledPasswordHandle, providedPassword,
286 gkResponse);
287}
288
289Status GateKeeperProxy::verifyChallenge(int32_t userId, int64_t challenge,
290 const std::vector<uint8_t>& enrolledPasswordHandle,
291 const std::vector<uint8_t>& providedPassword,
292 GKResponse* gkResponse) {
293 IPCThreadState* ipc = IPCThreadState::self();
294 const int calling_pid = ipc->getCallingPid();
295 const int calling_uid = ipc->getCallingUid();
296 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
297 return GK_ERROR;
298 }
299
300 // can't verify if we're missing either param
301 if (enrolledPasswordHandle.size() == 0 || providedPassword.size() == 0) return GK_ERROR;
302
303 if (!aidl_hw_device && !hw_device) {
304 LOG(ERROR) << "has no HAL to talk to";
305 return GK_ERROR;
306 }
307
308 if (hw_device) {
309 // Hidl Implementations expects passwordHandle to be in gatekeeper::password_handle_t
310 if (enrolledPasswordHandle.size() != sizeof(gatekeeper::password_handle_t)) {
311 LOG(INFO) << "Password handle has wrong length";
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700312 return GK_ERROR;
Andres Morales2d08dce2015-04-03 16:40:15 -0700313 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000314 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700315
Pawan Waghd2d320a2023-05-15 22:25:55 +0000316 uint32_t hw_userId = 0;
317 Status result = adjust_userId(userId, &hw_userId);
318 if (!result.isOk()) {
319 return result;
320 }
321
Pawan Waghc5c0c302023-04-21 22:04:31 +0000322 android::hardware::hidl_vec<uint8_t> curPwdHandle;
323 curPwdHandle.setToExternal(const_cast<uint8_t*>(enrolledPasswordHandle.data()),
324 enrolledPasswordHandle.size());
325 android::hardware::hidl_vec<uint8_t> enteredPwd;
326 enteredPwd.setToExternal(const_cast<uint8_t*>(providedPassword.data()),
327 providedPassword.size());
Andres Morales2d08dce2015-04-03 16:40:15 -0700328
Pawan Waghc5c0c302023-04-21 22:04:31 +0000329 uint64_t secureUserId = 0;
330 if (aidl_hw_device) {
331 // AIDL gatekeeper service
332 AidlGatekeeperVerifyResp rsp;
333 auto result = aidl_hw_device->verify(hw_userId, challenge, curPwdHandle, enteredPwd, &rsp);
334 if (!result.isOk()) {
335 LOG(ERROR) << "verify transaction failed";
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700336 return GK_ERROR;
337 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000338 if (rsp.statusCode >= AidlIGatekeeper::STATUS_OK) {
339 secureUserId = rsp.hardwareAuthToken.userId;
340 // Serialize HardwareAuthToken to a vector as hw_auth_token_t.
341 *gkResponse = GKResponse::ok(
342 authToken2AidlVec(rsp.hardwareAuthToken),
343 rsp.statusCode == AidlIGatekeeper::STATUS_REENROLL /* reenroll */);
344 } else if (rsp.statusCode == AidlIGatekeeper::ERROR_RETRY_TIMEOUT) {
345 *gkResponse = GKResponse::retry(rsp.timeoutMs);
346 } else {
347 *gkResponse = GKResponse::error();
Subrahmanyaman355e9772022-07-21 03:51:30 +0000348 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000349 } else if (hw_device) {
350 // HIDL gatekeeper service
351 Return<void> hwRes = hw_device->verify(
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700352 hw_userId, challenge, curPwdHandle, enteredPwd,
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700353 [&gkResponse](const GatekeeperResponse& rsp) {
354 if (rsp.code >= GatekeeperStatusCode::STATUS_OK) {
355 *gkResponse = GKResponse::ok(
Pawan Waghc5c0c302023-04-21 22:04:31 +0000356 {rsp.data.begin(), rsp.data.end()},
357 rsp.code == GatekeeperStatusCode::STATUS_REENROLL /* reenroll */);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700358 } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT) {
359 *gkResponse = GKResponse::retry(rsp.timeout);
360 } else {
361 *gkResponse = GKResponse::error();
Alexey Polyudov275aece2016-10-18 13:59:26 -0700362 }
363 });
Andres Morales835d96e2015-06-03 15:06:24 -0700364
Pawan Waghc5c0c302023-04-21 22:04:31 +0000365 if (!hwRes.isOk()) {
366 LOG(ERROR) << "verify transaction failed";
367 return GK_ERROR;
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700368 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000369 const gatekeeper::password_handle_t* handle =
370 reinterpret_cast<const gatekeeper::password_handle_t*>(
371 enrolledPasswordHandle.data());
372 secureUserId = handle->user_id;
373 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700374
Pawan Waghc5c0c302023-04-21 22:04:31 +0000375 if (gkResponse->response_code() == GKResponseCode::OK) {
376 if (gkResponse->payload().size() != 0) {
377 // try to connect to IKeystoreAuthorization AIDL service first.
378 AIBinder* authzAIBinder = AServiceManager_getService("android.security.authorization");
379 ::ndk::SpAIBinder authzBinder(authzAIBinder);
380 auto authzService = IKeystoreAuthorization::fromBinder(authzBinder);
381 if (authzService) {
382 if (gkResponse->payload().size() != sizeof(hw_auth_token_t)) {
383 LOG(ERROR) << "Incorrect size of AuthToken payload.";
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000384 return GK_ERROR;
Andres Morales835d96e2015-06-03 15:06:24 -0700385 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000386
387 const hw_auth_token_t* hwAuthToken =
388 reinterpret_cast<const hw_auth_token_t*>(gkResponse->payload().data());
389 HardwareAuthToken authToken;
390
391 authToken.timestamp.milliSeconds = betoh64(hwAuthToken->timestamp);
392 authToken.challenge = hwAuthToken->challenge;
393 authToken.userId = hwAuthToken->user_id;
394 authToken.authenticatorId = hwAuthToken->authenticator_id;
395 authToken.authenticatorType = static_cast<HardwareAuthenticatorType>(
396 betoh32(hwAuthToken->authenticator_type));
397 authToken.mac.assign(&hwAuthToken->hmac[0], &hwAuthToken->hmac[32]);
398 auto result = authzService->addAuthToken(authToken);
399 if (!result.isOk()) {
400 LOG(ERROR) << "Failure in sending AuthToken to AuthorizationService.";
401 return GK_ERROR;
402 }
403 } else {
404 LOG(ERROR) << "Cannot deliver auth token. Unable to communicate with "
405 "Keystore.";
406 return GK_ERROR;
Andres Morales835d96e2015-06-03 15:06:24 -0700407 }
Andres Morales33dfdc72015-05-12 15:37:20 -0700408 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700409
Pawan Waghc5c0c302023-04-21 22:04:31 +0000410 maybe_store_sid(userId, secureUserId);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700411 }
412
Pawan Waghc5c0c302023-04-21 22:04:31 +0000413 return Status::ok();
Andres Morales2d08dce2015-04-03 16:40:15 -0700414}
Pawan Waghc5c0c302023-04-21 22:04:31 +0000415
416Status GateKeeperProxy::getSecureUserId(int32_t userId, int64_t* sid) {
417 *sid = read_sid(userId);
418 return Status::ok();
419}
420
421Status GateKeeperProxy::clearSecureUserId(int32_t userId) {
422 IPCThreadState* ipc = IPCThreadState::self();
423 const int calling_pid = ipc->getCallingPid();
424 const int calling_uid = ipc->getCallingUid();
425 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
426 ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
427 return Status::ok();
428 }
429 clear_sid(userId);
430
Pawan Waghd2d320a2023-05-15 22:25:55 +0000431 uint32_t hw_userId = 0;
432 Status result = adjust_userId(userId, &hw_userId);
433 if (!result.isOk()) {
434 return result;
435 }
436
Pawan Waghc5c0c302023-04-21 22:04:31 +0000437 if (aidl_hw_device) {
438 aidl_hw_device->deleteUser(hw_userId);
439 } else if (hw_device) {
440 hw_device->deleteUser(hw_userId, [](const GatekeeperResponse&) {});
441 }
442 return Status::ok();
443}
444
445Status GateKeeperProxy::reportDeviceSetupComplete() {
446 IPCThreadState* ipc = IPCThreadState::self();
447 const int calling_pid = ipc->getCallingPid();
448 const int calling_uid = ipc->getCallingUid();
449 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
450 ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
451 return Status::ok();
452 }
453
454 clear_state_if_needed();
455 return Status::ok();
456}
457
458status_t GateKeeperProxy::dump(int fd, const Vector<String16>&) {
459 IPCThreadState* ipc = IPCThreadState::self();
460 const int pid = ipc->getCallingPid();
461 const int uid = ipc->getCallingUid();
462 if (!PermissionCache::checkPermission(DUMP_PERMISSION, pid, uid)) {
463 return PERMISSION_DENIED;
464 }
465
466 if (aidl_hw_device == nullptr && hw_device == nullptr) {
467 const char* result = "Device not available";
468 write(fd, result, strlen(result) + 1);
469 } else {
470 const char* result = "OK";
471 write(fd, result, strlen(result) + 1);
472 }
473
474 return OK;
475}
476} // namespace android