blob: bdfb7f6f7f1ff80648ff55d036751339f348b7c0 [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;
Subrahmanya Manikanta Venkateswarlu Bhamidipati Kameswara Sri32e1c702023-06-13 17:37:29 +000066 if (AServiceManager_isDeclared(gatekeeperServiceName)) {
67 ::ndk::SpAIBinder ks2Binder(AServiceManager_waitForService(gatekeeperServiceName));
68 aidl_hw_device = AidlIGatekeeper::fromBinder(ks2Binder);
69 }
70 if (!aidl_hw_device) {
71 hw_device = IGatekeeper::getService();
72 }
Pawan Waghc5c0c302023-04-21 22:04:31 +000073 is_running_gsi = android::base::GetBoolProperty(android::gsi::kGsiBootedProp, false);
Andres Moralesfef908e2015-07-07 10:28:15 -070074
Pawan Waghc5c0c302023-04-21 22:04:31 +000075 if (!aidl_hw_device && !hw_device) {
76 LOG(ERROR) << "Could not find Gatekeeper device, which makes me very sad.";
77 }
78}
79
80void GateKeeperProxy::store_sid(uint32_t userId, uint64_t sid) {
81 char filename[21];
82 snprintf(filename, sizeof(filename), "%u", userId);
83 int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
84 if (fd < 0) {
85 ALOGE("could not open file: %s: %s", filename, strerror(errno));
86 return;
87 }
88 write(fd, &sid, sizeof(sid));
89 close(fd);
90}
91
92void GateKeeperProxy::clear_state_if_needed() {
93 if (clear_state_if_needed_done) {
94 return;
95 }
96
97 if (mark_cold_boot() && !is_running_gsi) {
98 ALOGI("cold boot: clearing state");
99 if (aidl_hw_device) {
100 aidl_hw_device->deleteAllUsers();
101 } else if (hw_device) {
102 hw_device->deleteAllUsers([](const GatekeeperResponse&) {});
Andres Morales33dfdc72015-05-12 15:37:20 -0700103 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700104 }
105
Pawan Waghc5c0c302023-04-21 22:04:31 +0000106 clear_state_if_needed_done = true;
107}
Andres Morales2d08dce2015-04-03 16:40:15 -0700108
Pawan Waghc5c0c302023-04-21 22:04:31 +0000109bool GateKeeperProxy::mark_cold_boot() {
110 const char* filename = ".coldboot";
111 if (access(filename, F_OK) == -1) {
Andres Morales6a49c2f2015-04-16 13:16:24 -0700112 int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
113 if (fd < 0) {
Pawan Waghc5c0c302023-04-21 22:04:31 +0000114 ALOGE("could not open file: %s : %s", filename, strerror(errno));
115 return false;
Andres Morales6a49c2f2015-04-16 13:16:24 -0700116 }
Andres Morales6a49c2f2015-04-16 13:16:24 -0700117 close(fd);
Pawan Waghc5c0c302023-04-21 22:04:31 +0000118 return true;
Andres Morales6a49c2f2015-04-16 13:16:24 -0700119 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000120 return false;
121}
Andres Morales6a49c2f2015-04-16 13:16:24 -0700122
Pawan Waghc5c0c302023-04-21 22:04:31 +0000123void GateKeeperProxy::maybe_store_sid(uint32_t userId, uint64_t sid) {
124 char filename[21];
125 snprintf(filename, sizeof(filename), "%u", userId);
126 if (access(filename, F_OK) == -1) {
127 store_sid(userId, sid);
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700128 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000129}
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700130
Pawan Waghc5c0c302023-04-21 22:04:31 +0000131uint64_t GateKeeperProxy::read_sid(uint32_t userId) {
132 char filename[21];
133 uint64_t sid;
134 snprintf(filename, sizeof(filename), "%u", userId);
135 int fd = open(filename, O_RDONLY);
136 if (fd < 0) return 0;
137 read(fd, &sid, sizeof(sid));
138 close(fd);
139 return sid;
140}
Andres Morales3c2086d2015-06-24 10:21:16 -0700141
Pawan Waghc5c0c302023-04-21 22:04:31 +0000142void GateKeeperProxy::clear_sid(uint32_t userId) {
143 char filename[21];
144 snprintf(filename, sizeof(filename), "%u", userId);
145 if (remove(filename) < 0 && errno != ENOENT) {
146 ALOGE("%s: could not remove file [%s], attempting 0 write", __func__, strerror(errno));
147 store_sid(userId, 0);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700148 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000149}
Andres Morales6a49c2f2015-04-16 13:16:24 -0700150
Pawan Waghd2d320a2023-05-15 22:25:55 +0000151Status GateKeeperProxy::adjust_userId(uint32_t userId, uint32_t* hw_userId) {
Pawan Waghc5c0c302023-04-21 22:04:31 +0000152 static constexpr uint32_t kGsiOffset = 1000000;
Pawan Waghd2d320a2023-05-15 22:25:55 +0000153 if (userId >= kGsiOffset) {
154 return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700155 }
Pawan Waghd2d320a2023-05-15 22:25:55 +0000156
157 if ((aidl_hw_device == nullptr) && (hw_device == nullptr)) {
158 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
159 }
160
161 if (is_running_gsi) {
162 *hw_userId = userId + kGsiOffset;
163 return Status::ok();
164 }
165 *hw_userId = userId;
166 return Status::ok();
Pawan Waghc5c0c302023-04-21 22:04:31 +0000167}
David Anderson97400bd2019-02-15 15:59:39 -0800168
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700169#define GK_ERROR *gkResponse = GKResponse::error(), Status::ok()
170
Pawan Waghc5c0c302023-04-21 22:04:31 +0000171Status GateKeeperProxy::enroll(int32_t userId,
172 const std::optional<std::vector<uint8_t>>& currentPasswordHandle,
173 const std::optional<std::vector<uint8_t>>& currentPassword,
174 const std::vector<uint8_t>& desiredPassword,
175 GKResponse* gkResponse) {
176 IPCThreadState* ipc = IPCThreadState::self();
177 const int calling_pid = ipc->getCallingPid();
178 const int calling_uid = ipc->getCallingUid();
179 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
180 return GK_ERROR;
181 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700182
Pawan Waghc5c0c302023-04-21 22:04:31 +0000183 // Make sure to clear any state from before factory reset as soon as a credential is
184 // enrolled (which may happen during device setup).
185 clear_state_if_needed();
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700186
Pawan Waghc5c0c302023-04-21 22:04:31 +0000187 // need a desired password to enroll
188 if (desiredPassword.size() == 0) return GK_ERROR;
Andres Morales33dfdc72015-05-12 15:37:20 -0700189
Pawan Waghc5c0c302023-04-21 22:04:31 +0000190 if (!aidl_hw_device && !hw_device) {
191 LOG(ERROR) << "has no HAL to talk to";
192 return GK_ERROR;
193 }
Andres Morales835d96e2015-06-03 15:06:24 -0700194
Pawan Waghc5c0c302023-04-21 22:04:31 +0000195 android::hardware::hidl_vec<uint8_t> curPwdHandle;
196 android::hardware::hidl_vec<uint8_t> curPwd;
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700197
Pawan Waghc5c0c302023-04-21 22:04:31 +0000198 if (currentPasswordHandle && currentPassword) {
199 if (hw_device) {
200 // Hidl Implementations expects passwordHandle to be in
201 // gatekeeper::password_handle_t format.
202 if (currentPasswordHandle->size() != sizeof(gatekeeper::password_handle_t)) {
203 LOG(INFO) << "Password handle has wrong length";
Subrahmanyaman355e9772022-07-21 03:51:30 +0000204 return GK_ERROR;
205 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000206 }
207 curPwdHandle.setToExternal(const_cast<uint8_t*>(currentPasswordHandle->data()),
208 currentPasswordHandle->size());
209 curPwd.setToExternal(const_cast<uint8_t*>(currentPassword->data()),
210 currentPassword->size());
211 }
212
213 android::hardware::hidl_vec<uint8_t> newPwd;
214 newPwd.setToExternal(const_cast<uint8_t*>(desiredPassword.data()), desiredPassword.size());
215
Pawan Waghd2d320a2023-05-15 22:25:55 +0000216 uint32_t hw_userId = 0;
217 Status result = adjust_userId(userId, &hw_userId);
218 if (!result.isOk()) {
219 return result;
220 }
221
Pawan Waghc5c0c302023-04-21 22:04:31 +0000222 uint64_t secureUserId = 0;
223 if (aidl_hw_device) {
224 // AIDL gatekeeper service
225 AidlGatekeeperEnrollResp rsp;
226 auto result = aidl_hw_device->enroll(hw_userId, curPwdHandle, curPwd, newPwd, &rsp);
227 if (!result.isOk()) {
228 LOG(ERROR) << "enroll transaction failed";
229 return GK_ERROR;
230 }
231 if (rsp.statusCode >= AidlIGatekeeper::STATUS_OK) {
232 *gkResponse = GKResponse::ok({rsp.data.begin(), rsp.data.end()});
233 secureUserId = static_cast<uint64_t>(rsp.secureUserId);
234 } else if (rsp.statusCode == AidlIGatekeeper::ERROR_RETRY_TIMEOUT && rsp.timeoutMs > 0) {
235 *gkResponse = GKResponse::retry(rsp.timeoutMs);
236 } else {
237 *gkResponse = GKResponse::error();
238 }
239 } else if (hw_device) {
240 // HIDL gatekeeper service
241 Return<void> hwRes = hw_device->enroll(
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700242 hw_userId, curPwdHandle, curPwd, newPwd,
243 [&gkResponse](const GatekeeperResponse& rsp) {
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700244 if (rsp.code >= GatekeeperStatusCode::STATUS_OK) {
245 *gkResponse = GKResponse::ok({rsp.data.begin(), rsp.data.end()});
246 } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT &&
247 rsp.timeout > 0) {
248 *gkResponse = GKResponse::retry(rsp.timeout);
249 } else {
250 *gkResponse = GKResponse::error();
Alexey Polyudov275aece2016-10-18 13:59:26 -0700251 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700252 });
Pawan Waghc5c0c302023-04-21 22:04:31 +0000253 if (!hwRes.isOk()) {
254 LOG(ERROR) << "enroll transaction failed";
255 return GK_ERROR;
256 }
257 if (gkResponse->response_code() == GKResponseCode::OK) {
258 if (gkResponse->payload().size() != sizeof(gatekeeper::password_handle_t)) {
259 LOG(ERROR) << "HAL returned password handle of invalid length "
260 << gkResponse->payload().size();
Subrahmanyaman355e9772022-07-21 03:51:30 +0000261 return GK_ERROR;
262 }
Subrahmanyaman355e9772022-07-21 03:51:30 +0000263
Pawan Waghc5c0c302023-04-21 22:04:31 +0000264 const gatekeeper::password_handle_t* handle =
Subrahmanyaman355e9772022-07-21 03:51:30 +0000265 reinterpret_cast<const gatekeeper::password_handle_t*>(
Pawan Waghc5c0c302023-04-21 22:04:31 +0000266 gkResponse->payload().data());
267 secureUserId = handle->user_id;
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700268 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700269 }
270
Pawan Waghc5c0c302023-04-21 22:04:31 +0000271 if (gkResponse->response_code() == GKResponseCode::OK && !gkResponse->should_reenroll()) {
272 store_sid(userId, secureUserId);
273
274 GKResponse verifyResponse;
275 // immediately verify this password so we don't ask the user to enter it again
276 // if they just created it.
277 auto status = verify(userId, gkResponse->payload(), desiredPassword, &verifyResponse);
278 if (!status.isOk() || verifyResponse.response_code() != GKResponseCode::OK) {
279 LOG(ERROR) << "Failed to verify password after enrolling";
280 }
Andres Moralesc828ae82015-04-10 21:03:07 -0700281 }
282
Pawan Waghc5c0c302023-04-21 22:04:31 +0000283 return Status::ok();
284}
285
286Status GateKeeperProxy::verify(int32_t userId, const ::std::vector<uint8_t>& enrolledPasswordHandle,
287 const ::std::vector<uint8_t>& providedPassword,
288 GKResponse* gkResponse) {
289 return verifyChallenge(userId, 0 /* challenge */, enrolledPasswordHandle, providedPassword,
290 gkResponse);
291}
292
293Status GateKeeperProxy::verifyChallenge(int32_t userId, int64_t challenge,
294 const std::vector<uint8_t>& enrolledPasswordHandle,
295 const std::vector<uint8_t>& providedPassword,
296 GKResponse* gkResponse) {
297 IPCThreadState* ipc = IPCThreadState::self();
298 const int calling_pid = ipc->getCallingPid();
299 const int calling_uid = ipc->getCallingUid();
300 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
301 return GK_ERROR;
302 }
303
304 // can't verify if we're missing either param
305 if (enrolledPasswordHandle.size() == 0 || providedPassword.size() == 0) return GK_ERROR;
306
307 if (!aidl_hw_device && !hw_device) {
308 LOG(ERROR) << "has no HAL to talk to";
309 return GK_ERROR;
310 }
311
312 if (hw_device) {
313 // Hidl Implementations expects passwordHandle to be in gatekeeper::password_handle_t
314 if (enrolledPasswordHandle.size() != sizeof(gatekeeper::password_handle_t)) {
315 LOG(INFO) << "Password handle has wrong length";
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700316 return GK_ERROR;
Andres Morales2d08dce2015-04-03 16:40:15 -0700317 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000318 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700319
Pawan Waghd2d320a2023-05-15 22:25:55 +0000320 uint32_t hw_userId = 0;
321 Status result = adjust_userId(userId, &hw_userId);
322 if (!result.isOk()) {
323 return result;
324 }
325
Pawan Waghc5c0c302023-04-21 22:04:31 +0000326 android::hardware::hidl_vec<uint8_t> curPwdHandle;
327 curPwdHandle.setToExternal(const_cast<uint8_t*>(enrolledPasswordHandle.data()),
328 enrolledPasswordHandle.size());
329 android::hardware::hidl_vec<uint8_t> enteredPwd;
330 enteredPwd.setToExternal(const_cast<uint8_t*>(providedPassword.data()),
331 providedPassword.size());
Andres Morales2d08dce2015-04-03 16:40:15 -0700332
Pawan Waghc5c0c302023-04-21 22:04:31 +0000333 uint64_t secureUserId = 0;
334 if (aidl_hw_device) {
335 // AIDL gatekeeper service
336 AidlGatekeeperVerifyResp rsp;
337 auto result = aidl_hw_device->verify(hw_userId, challenge, curPwdHandle, enteredPwd, &rsp);
338 if (!result.isOk()) {
339 LOG(ERROR) << "verify transaction failed";
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700340 return GK_ERROR;
341 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000342 if (rsp.statusCode >= AidlIGatekeeper::STATUS_OK) {
343 secureUserId = rsp.hardwareAuthToken.userId;
344 // Serialize HardwareAuthToken to a vector as hw_auth_token_t.
345 *gkResponse = GKResponse::ok(
346 authToken2AidlVec(rsp.hardwareAuthToken),
347 rsp.statusCode == AidlIGatekeeper::STATUS_REENROLL /* reenroll */);
348 } else if (rsp.statusCode == AidlIGatekeeper::ERROR_RETRY_TIMEOUT) {
349 *gkResponse = GKResponse::retry(rsp.timeoutMs);
350 } else {
351 *gkResponse = GKResponse::error();
Subrahmanyaman355e9772022-07-21 03:51:30 +0000352 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000353 } else if (hw_device) {
354 // HIDL gatekeeper service
355 Return<void> hwRes = hw_device->verify(
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700356 hw_userId, challenge, curPwdHandle, enteredPwd,
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700357 [&gkResponse](const GatekeeperResponse& rsp) {
358 if (rsp.code >= GatekeeperStatusCode::STATUS_OK) {
359 *gkResponse = GKResponse::ok(
Pawan Waghc5c0c302023-04-21 22:04:31 +0000360 {rsp.data.begin(), rsp.data.end()},
361 rsp.code == GatekeeperStatusCode::STATUS_REENROLL /* reenroll */);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700362 } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT) {
363 *gkResponse = GKResponse::retry(rsp.timeout);
364 } else {
365 *gkResponse = GKResponse::error();
Alexey Polyudov275aece2016-10-18 13:59:26 -0700366 }
367 });
Andres Morales835d96e2015-06-03 15:06:24 -0700368
Pawan Waghc5c0c302023-04-21 22:04:31 +0000369 if (!hwRes.isOk()) {
370 LOG(ERROR) << "verify transaction failed";
371 return GK_ERROR;
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700372 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000373 const gatekeeper::password_handle_t* handle =
374 reinterpret_cast<const gatekeeper::password_handle_t*>(
375 enrolledPasswordHandle.data());
376 secureUserId = handle->user_id;
377 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700378
Pawan Waghc5c0c302023-04-21 22:04:31 +0000379 if (gkResponse->response_code() == GKResponseCode::OK) {
380 if (gkResponse->payload().size() != 0) {
381 // try to connect to IKeystoreAuthorization AIDL service first.
382 AIBinder* authzAIBinder = AServiceManager_getService("android.security.authorization");
383 ::ndk::SpAIBinder authzBinder(authzAIBinder);
384 auto authzService = IKeystoreAuthorization::fromBinder(authzBinder);
385 if (authzService) {
386 if (gkResponse->payload().size() != sizeof(hw_auth_token_t)) {
387 LOG(ERROR) << "Incorrect size of AuthToken payload.";
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000388 return GK_ERROR;
Andres Morales835d96e2015-06-03 15:06:24 -0700389 }
Pawan Waghc5c0c302023-04-21 22:04:31 +0000390
391 const hw_auth_token_t* hwAuthToken =
392 reinterpret_cast<const hw_auth_token_t*>(gkResponse->payload().data());
393 HardwareAuthToken authToken;
394
395 authToken.timestamp.milliSeconds = betoh64(hwAuthToken->timestamp);
396 authToken.challenge = hwAuthToken->challenge;
397 authToken.userId = hwAuthToken->user_id;
398 authToken.authenticatorId = hwAuthToken->authenticator_id;
399 authToken.authenticatorType = static_cast<HardwareAuthenticatorType>(
400 betoh32(hwAuthToken->authenticator_type));
401 authToken.mac.assign(&hwAuthToken->hmac[0], &hwAuthToken->hmac[32]);
402 auto result = authzService->addAuthToken(authToken);
403 if (!result.isOk()) {
404 LOG(ERROR) << "Failure in sending AuthToken to AuthorizationService.";
405 return GK_ERROR;
406 }
407 } else {
408 LOG(ERROR) << "Cannot deliver auth token. Unable to communicate with "
409 "Keystore.";
410 return GK_ERROR;
Andres Morales835d96e2015-06-03 15:06:24 -0700411 }
Andres Morales33dfdc72015-05-12 15:37:20 -0700412 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700413
Pawan Waghc5c0c302023-04-21 22:04:31 +0000414 maybe_store_sid(userId, secureUserId);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700415 }
416
Pawan Waghc5c0c302023-04-21 22:04:31 +0000417 return Status::ok();
Andres Morales2d08dce2015-04-03 16:40:15 -0700418}
Pawan Waghc5c0c302023-04-21 22:04:31 +0000419
420Status GateKeeperProxy::getSecureUserId(int32_t userId, int64_t* sid) {
421 *sid = read_sid(userId);
422 return Status::ok();
423}
424
425Status GateKeeperProxy::clearSecureUserId(int32_t userId) {
426 IPCThreadState* ipc = IPCThreadState::self();
427 const int calling_pid = ipc->getCallingPid();
428 const int calling_uid = ipc->getCallingUid();
429 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
430 ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
431 return Status::ok();
432 }
433 clear_sid(userId);
434
Pawan Waghd2d320a2023-05-15 22:25:55 +0000435 uint32_t hw_userId = 0;
436 Status result = adjust_userId(userId, &hw_userId);
437 if (!result.isOk()) {
438 return result;
439 }
440
Pawan Waghc5c0c302023-04-21 22:04:31 +0000441 if (aidl_hw_device) {
442 aidl_hw_device->deleteUser(hw_userId);
443 } else if (hw_device) {
444 hw_device->deleteUser(hw_userId, [](const GatekeeperResponse&) {});
445 }
446 return Status::ok();
447}
448
449Status GateKeeperProxy::reportDeviceSetupComplete() {
450 IPCThreadState* ipc = IPCThreadState::self();
451 const int calling_pid = ipc->getCallingPid();
452 const int calling_uid = ipc->getCallingUid();
453 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
454 ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
455 return Status::ok();
456 }
457
458 clear_state_if_needed();
459 return Status::ok();
460}
461
462status_t GateKeeperProxy::dump(int fd, const Vector<String16>&) {
463 IPCThreadState* ipc = IPCThreadState::self();
464 const int pid = ipc->getCallingPid();
465 const int uid = ipc->getCallingUid();
466 if (!PermissionCache::checkPermission(DUMP_PERMISSION, pid, uid)) {
467 return PERMISSION_DENIED;
468 }
469
470 if (aidl_hw_device == nullptr && hw_device == nullptr) {
471 const char* result = "Device not available";
472 write(fd, result, strlen(result) + 1);
473 } else {
474 const char* result = "OK";
475 write(fd, result, strlen(result) + 1);
476 }
477
478 return OK;
479}
480} // namespace android