blob: e5241b5842a787dc7488dda9aecabf928dd30a0a [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 Waghc5c0c302023-04-21 22:04:31 +0000147uint32_t GateKeeperProxy::adjust_userId(uint32_t userId) {
148 static constexpr uint32_t kGsiOffset = 1000000;
149 CHECK(userId < kGsiOffset);
150 CHECK((aidl_hw_device != nullptr) || (hw_device != nullptr));
151 if (is_running_gsi) {
152 return userId + kGsiOffset;
Andres Morales6a49c2f2015-04-16 13:16:24 -0700153 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000154 return userId;
155}
David Anderson97400bd2019-02-15 15:59:39 -0800156
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700157#define GK_ERROR *gkResponse = GKResponse::error(), Status::ok()
158
Pawan Waghc5c0c302023-04-21 22:04:31 +0000159Status GateKeeperProxy::enroll(int32_t userId,
160 const std::optional<std::vector<uint8_t>>& currentPasswordHandle,
161 const std::optional<std::vector<uint8_t>>& currentPassword,
162 const std::vector<uint8_t>& desiredPassword,
163 GKResponse* gkResponse) {
164 IPCThreadState* ipc = IPCThreadState::self();
165 const int calling_pid = ipc->getCallingPid();
166 const int calling_uid = ipc->getCallingUid();
167 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
168 return GK_ERROR;
169 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700170
Pawan Waghc5c0c302023-04-21 22:04:31 +0000171 // Make sure to clear any state from before factory reset as soon as a credential is
172 // enrolled (which may happen during device setup).
173 clear_state_if_needed();
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700174
Pawan Waghc5c0c302023-04-21 22:04:31 +0000175 // need a desired password to enroll
176 if (desiredPassword.size() == 0) return GK_ERROR;
Andres Morales33dfdc72015-05-12 15:37:20 -0700177
Pawan Waghc5c0c302023-04-21 22:04:31 +0000178 if (!aidl_hw_device && !hw_device) {
179 LOG(ERROR) << "has no HAL to talk to";
180 return GK_ERROR;
181 }
Andres Morales835d96e2015-06-03 15:06:24 -0700182
Pawan Waghc5c0c302023-04-21 22:04:31 +0000183 android::hardware::hidl_vec<uint8_t> curPwdHandle;
184 android::hardware::hidl_vec<uint8_t> curPwd;
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700185
Pawan Waghc5c0c302023-04-21 22:04:31 +0000186 if (currentPasswordHandle && currentPassword) {
187 if (hw_device) {
188 // Hidl Implementations expects passwordHandle to be in
189 // gatekeeper::password_handle_t format.
190 if (currentPasswordHandle->size() != sizeof(gatekeeper::password_handle_t)) {
191 LOG(INFO) << "Password handle has wrong length";
Subrahmanyaman355e9772022-07-21 03:51:30 +0000192 return GK_ERROR;
193 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000194 }
195 curPwdHandle.setToExternal(const_cast<uint8_t*>(currentPasswordHandle->data()),
196 currentPasswordHandle->size());
197 curPwd.setToExternal(const_cast<uint8_t*>(currentPassword->data()),
198 currentPassword->size());
199 }
200
201 android::hardware::hidl_vec<uint8_t> newPwd;
202 newPwd.setToExternal(const_cast<uint8_t*>(desiredPassword.data()), desiredPassword.size());
203
204 uint32_t hw_userId = adjust_userId(userId);
205 uint64_t secureUserId = 0;
206 if (aidl_hw_device) {
207 // AIDL gatekeeper service
208 AidlGatekeeperEnrollResp rsp;
209 auto result = aidl_hw_device->enroll(hw_userId, curPwdHandle, curPwd, newPwd, &rsp);
210 if (!result.isOk()) {
211 LOG(ERROR) << "enroll transaction failed";
212 return GK_ERROR;
213 }
214 if (rsp.statusCode >= AidlIGatekeeper::STATUS_OK) {
215 *gkResponse = GKResponse::ok({rsp.data.begin(), rsp.data.end()});
216 secureUserId = static_cast<uint64_t>(rsp.secureUserId);
217 } else if (rsp.statusCode == AidlIGatekeeper::ERROR_RETRY_TIMEOUT && rsp.timeoutMs > 0) {
218 *gkResponse = GKResponse::retry(rsp.timeoutMs);
219 } else {
220 *gkResponse = GKResponse::error();
221 }
222 } else if (hw_device) {
223 // HIDL gatekeeper service
224 Return<void> hwRes = hw_device->enroll(
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700225 hw_userId, curPwdHandle, curPwd, newPwd,
226 [&gkResponse](const GatekeeperResponse& rsp) {
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700227 if (rsp.code >= GatekeeperStatusCode::STATUS_OK) {
228 *gkResponse = GKResponse::ok({rsp.data.begin(), rsp.data.end()});
229 } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT &&
230 rsp.timeout > 0) {
231 *gkResponse = GKResponse::retry(rsp.timeout);
232 } else {
233 *gkResponse = GKResponse::error();
Alexey Polyudov275aece2016-10-18 13:59:26 -0700234 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700235 });
Pawan Waghc5c0c302023-04-21 22:04:31 +0000236 if (!hwRes.isOk()) {
237 LOG(ERROR) << "enroll transaction failed";
238 return GK_ERROR;
239 }
240 if (gkResponse->response_code() == GKResponseCode::OK) {
241 if (gkResponse->payload().size() != sizeof(gatekeeper::password_handle_t)) {
242 LOG(ERROR) << "HAL returned password handle of invalid length "
243 << gkResponse->payload().size();
Subrahmanyaman355e9772022-07-21 03:51:30 +0000244 return GK_ERROR;
245 }
Subrahmanyaman355e9772022-07-21 03:51:30 +0000246
Pawan Waghc5c0c302023-04-21 22:04:31 +0000247 const gatekeeper::password_handle_t* handle =
Subrahmanyaman355e9772022-07-21 03:51:30 +0000248 reinterpret_cast<const gatekeeper::password_handle_t*>(
Pawan Waghc5c0c302023-04-21 22:04:31 +0000249 gkResponse->payload().data());
250 secureUserId = handle->user_id;
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700251 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700252 }
253
Pawan Waghc5c0c302023-04-21 22:04:31 +0000254 if (gkResponse->response_code() == GKResponseCode::OK && !gkResponse->should_reenroll()) {
255 store_sid(userId, secureUserId);
256
257 GKResponse verifyResponse;
258 // immediately verify this password so we don't ask the user to enter it again
259 // if they just created it.
260 auto status = verify(userId, gkResponse->payload(), desiredPassword, &verifyResponse);
261 if (!status.isOk() || verifyResponse.response_code() != GKResponseCode::OK) {
262 LOG(ERROR) << "Failed to verify password after enrolling";
263 }
Andres Moralesc828ae82015-04-10 21:03:07 -0700264 }
265
Pawan Waghc5c0c302023-04-21 22:04:31 +0000266 return Status::ok();
267}
268
269Status GateKeeperProxy::verify(int32_t userId, const ::std::vector<uint8_t>& enrolledPasswordHandle,
270 const ::std::vector<uint8_t>& providedPassword,
271 GKResponse* gkResponse) {
272 return verifyChallenge(userId, 0 /* challenge */, enrolledPasswordHandle, providedPassword,
273 gkResponse);
274}
275
276Status GateKeeperProxy::verifyChallenge(int32_t userId, int64_t challenge,
277 const std::vector<uint8_t>& enrolledPasswordHandle,
278 const std::vector<uint8_t>& providedPassword,
279 GKResponse* gkResponse) {
280 IPCThreadState* ipc = IPCThreadState::self();
281 const int calling_pid = ipc->getCallingPid();
282 const int calling_uid = ipc->getCallingUid();
283 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
284 return GK_ERROR;
285 }
286
287 // can't verify if we're missing either param
288 if (enrolledPasswordHandle.size() == 0 || providedPassword.size() == 0) return GK_ERROR;
289
290 if (!aidl_hw_device && !hw_device) {
291 LOG(ERROR) << "has no HAL to talk to";
292 return GK_ERROR;
293 }
294
295 if (hw_device) {
296 // Hidl Implementations expects passwordHandle to be in gatekeeper::password_handle_t
297 if (enrolledPasswordHandle.size() != sizeof(gatekeeper::password_handle_t)) {
298 LOG(INFO) << "Password handle has wrong length";
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700299 return GK_ERROR;
Andres Morales2d08dce2015-04-03 16:40:15 -0700300 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000301 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700302
Pawan Waghc5c0c302023-04-21 22:04:31 +0000303 uint32_t hw_userId = adjust_userId(userId);
304 android::hardware::hidl_vec<uint8_t> curPwdHandle;
305 curPwdHandle.setToExternal(const_cast<uint8_t*>(enrolledPasswordHandle.data()),
306 enrolledPasswordHandle.size());
307 android::hardware::hidl_vec<uint8_t> enteredPwd;
308 enteredPwd.setToExternal(const_cast<uint8_t*>(providedPassword.data()),
309 providedPassword.size());
Andres Morales2d08dce2015-04-03 16:40:15 -0700310
Pawan Waghc5c0c302023-04-21 22:04:31 +0000311 uint64_t secureUserId = 0;
312 if (aidl_hw_device) {
313 // AIDL gatekeeper service
314 AidlGatekeeperVerifyResp rsp;
315 auto result = aidl_hw_device->verify(hw_userId, challenge, curPwdHandle, enteredPwd, &rsp);
316 if (!result.isOk()) {
317 LOG(ERROR) << "verify transaction failed";
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700318 return GK_ERROR;
319 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000320 if (rsp.statusCode >= AidlIGatekeeper::STATUS_OK) {
321 secureUserId = rsp.hardwareAuthToken.userId;
322 // Serialize HardwareAuthToken to a vector as hw_auth_token_t.
323 *gkResponse = GKResponse::ok(
324 authToken2AidlVec(rsp.hardwareAuthToken),
325 rsp.statusCode == AidlIGatekeeper::STATUS_REENROLL /* reenroll */);
326 } else if (rsp.statusCode == AidlIGatekeeper::ERROR_RETRY_TIMEOUT) {
327 *gkResponse = GKResponse::retry(rsp.timeoutMs);
328 } else {
329 *gkResponse = GKResponse::error();
Subrahmanyaman355e9772022-07-21 03:51:30 +0000330 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000331 } else if (hw_device) {
332 // HIDL gatekeeper service
333 Return<void> hwRes = hw_device->verify(
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700334 hw_userId, challenge, curPwdHandle, enteredPwd,
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700335 [&gkResponse](const GatekeeperResponse& rsp) {
336 if (rsp.code >= GatekeeperStatusCode::STATUS_OK) {
337 *gkResponse = GKResponse::ok(
Pawan Waghc5c0c302023-04-21 22:04:31 +0000338 {rsp.data.begin(), rsp.data.end()},
339 rsp.code == GatekeeperStatusCode::STATUS_REENROLL /* reenroll */);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700340 } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT) {
341 *gkResponse = GKResponse::retry(rsp.timeout);
342 } else {
343 *gkResponse = GKResponse::error();
Alexey Polyudov275aece2016-10-18 13:59:26 -0700344 }
345 });
Andres Morales835d96e2015-06-03 15:06:24 -0700346
Pawan Waghc5c0c302023-04-21 22:04:31 +0000347 if (!hwRes.isOk()) {
348 LOG(ERROR) << "verify transaction failed";
349 return GK_ERROR;
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700350 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000351 const gatekeeper::password_handle_t* handle =
352 reinterpret_cast<const gatekeeper::password_handle_t*>(
353 enrolledPasswordHandle.data());
354 secureUserId = handle->user_id;
355 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700356
Pawan Waghc5c0c302023-04-21 22:04:31 +0000357 if (gkResponse->response_code() == GKResponseCode::OK) {
358 if (gkResponse->payload().size() != 0) {
359 // try to connect to IKeystoreAuthorization AIDL service first.
360 AIBinder* authzAIBinder = AServiceManager_getService("android.security.authorization");
361 ::ndk::SpAIBinder authzBinder(authzAIBinder);
362 auto authzService = IKeystoreAuthorization::fromBinder(authzBinder);
363 if (authzService) {
364 if (gkResponse->payload().size() != sizeof(hw_auth_token_t)) {
365 LOG(ERROR) << "Incorrect size of AuthToken payload.";
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000366 return GK_ERROR;
Andres Morales835d96e2015-06-03 15:06:24 -0700367 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000368
369 const hw_auth_token_t* hwAuthToken =
370 reinterpret_cast<const hw_auth_token_t*>(gkResponse->payload().data());
371 HardwareAuthToken authToken;
372
373 authToken.timestamp.milliSeconds = betoh64(hwAuthToken->timestamp);
374 authToken.challenge = hwAuthToken->challenge;
375 authToken.userId = hwAuthToken->user_id;
376 authToken.authenticatorId = hwAuthToken->authenticator_id;
377 authToken.authenticatorType = static_cast<HardwareAuthenticatorType>(
378 betoh32(hwAuthToken->authenticator_type));
379 authToken.mac.assign(&hwAuthToken->hmac[0], &hwAuthToken->hmac[32]);
380 auto result = authzService->addAuthToken(authToken);
381 if (!result.isOk()) {
382 LOG(ERROR) << "Failure in sending AuthToken to AuthorizationService.";
383 return GK_ERROR;
384 }
385 } else {
386 LOG(ERROR) << "Cannot deliver auth token. Unable to communicate with "
387 "Keystore.";
388 return GK_ERROR;
Andres Morales835d96e2015-06-03 15:06:24 -0700389 }
Andres Morales33dfdc72015-05-12 15:37:20 -0700390 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700391
Pawan Waghc5c0c302023-04-21 22:04:31 +0000392 maybe_store_sid(userId, secureUserId);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700393 }
394
Pawan Waghc5c0c302023-04-21 22:04:31 +0000395 return Status::ok();
Andres Morales2d08dce2015-04-03 16:40:15 -0700396}
Pawan Waghc5c0c302023-04-21 22:04:31 +0000397
398Status GateKeeperProxy::getSecureUserId(int32_t userId, int64_t* sid) {
399 *sid = read_sid(userId);
400 return Status::ok();
401}
402
403Status GateKeeperProxy::clearSecureUserId(int32_t userId) {
404 IPCThreadState* ipc = IPCThreadState::self();
405 const int calling_pid = ipc->getCallingPid();
406 const int calling_uid = ipc->getCallingUid();
407 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
408 ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
409 return Status::ok();
410 }
411 clear_sid(userId);
412
413 uint32_t hw_userId = adjust_userId(userId);
414 if (aidl_hw_device) {
415 aidl_hw_device->deleteUser(hw_userId);
416 } else if (hw_device) {
417 hw_device->deleteUser(hw_userId, [](const GatekeeperResponse&) {});
418 }
419 return Status::ok();
420}
421
422Status GateKeeperProxy::reportDeviceSetupComplete() {
423 IPCThreadState* ipc = IPCThreadState::self();
424 const int calling_pid = ipc->getCallingPid();
425 const int calling_uid = ipc->getCallingUid();
426 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
427 ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
428 return Status::ok();
429 }
430
431 clear_state_if_needed();
432 return Status::ok();
433}
434
435status_t GateKeeperProxy::dump(int fd, const Vector<String16>&) {
436 IPCThreadState* ipc = IPCThreadState::self();
437 const int pid = ipc->getCallingPid();
438 const int uid = ipc->getCallingUid();
439 if (!PermissionCache::checkPermission(DUMP_PERMISSION, pid, uid)) {
440 return PERMISSION_DENIED;
441 }
442
443 if (aidl_hw_device == nullptr && hw_device == nullptr) {
444 const char* result = "Device not available";
445 write(fd, result, strlen(result) + 1);
446 } else {
447 const char* result = "OK";
448 write(fd, result, strlen(result) + 1);
449 }
450
451 return OK;
452}
453} // namespace android