blob: 76fcd55d671ff1cf277779c96eb2fe23cff49902 [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 */
16
17#define LOG_TAG "gatekeeperd"
18
Janis Danisevskis3a1eb672019-03-29 11:14:31 -070019#include <android/service/gatekeeper/BnGateKeeperService.h>
20#include <gatekeeper/GateKeeperResponse.h>
Andres Morales2d08dce2015-04-03 16:40:15 -070021
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000022#include <endian.h>
Andres Morales6a49c2f2015-04-16 13:16:24 -070023#include <errno.h>
Andres Morales6a49c2f2015-04-16 13:16:24 -070024#include <fcntl.h>
25#include <unistd.h>
Justin Yun68b0ec62017-08-16 18:54:20 +090026#include <memory>
Andres Morales6a49c2f2015-04-16 13:16:24 -070027
Subrahmanyaman355e9772022-07-21 03:51:30 +000028#include <KeyMintUtils.h>
David Anderson97400bd2019-02-15 15:59:39 -080029#include <android-base/logging.h>
30#include <android-base/properties.h>
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000031#include <android/binder_ibinder.h>
32#include <android/binder_manager.h>
Andres Morales2d08dce2015-04-03 16:40:15 -070033#include <binder/IPCThreadState.h>
34#include <binder/IServiceManager.h>
35#include <binder/PermissionCache.h>
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000036#include <gatekeeper/password_handle.h> // for password_handle_t
Andres Morales6a49c2f2015-04-16 13:16:24 -070037#include <hardware/hw_auth_token.h>
David Anderson97400bd2019-02-15 15:59:39 -080038#include <libgsi/libgsi.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080039#include <log/log.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070040#include <utils/String16.h>
Andres Morales2d08dce2015-04-03 16:40:15 -070041
Subrahmanyaman355e9772022-07-21 03:51:30 +000042#include <aidl/android/hardware/gatekeeper/IGatekeeper.h>
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000043#include <aidl/android/hardware/security/keymint/HardwareAuthToken.h>
44#include <aidl/android/security/authorization/IKeystoreAuthorization.h>
Louis Chang4c66b8a2021-01-18 10:01:12 +000045#include <android/hardware/gatekeeper/1.0/IGatekeeper.h>
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000046#include <hidl/HidlSupport.h>
Alexey Polyudov275aece2016-10-18 13:59:26 -070047
48using android::sp;
Louis Chang4c66b8a2021-01-18 10:01:12 +000049using android::hardware::Return;
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000050using android::hardware::gatekeeper::V1_0::GatekeeperResponse;
51using android::hardware::gatekeeper::V1_0::GatekeeperStatusCode;
52using android::hardware::gatekeeper::V1_0::IGatekeeper;
Alexey Polyudov275aece2016-10-18 13:59:26 -070053
Subrahmanyaman355e9772022-07-21 03:51:30 +000054using AidlGatekeeperEnrollResp = aidl::android::hardware::gatekeeper::GatekeeperEnrollResponse;
55using AidlGatekeeperVerifyResp = aidl::android::hardware::gatekeeper::GatekeeperVerifyResponse;
56using AidlIGatekeeper = aidl::android::hardware::gatekeeper::IGatekeeper;
57
Janis Danisevskis3a1eb672019-03-29 11:14:31 -070058using ::android::binder::Status;
59using ::android::service::gatekeeper::BnGateKeeperService;
60using GKResponse = ::android::service::gatekeeper::GateKeeperResponse;
61using GKResponseCode = ::android::service::gatekeeper::ResponseCode;
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000062using ::aidl::android::hardware::security::keymint::HardwareAuthenticatorType;
63using ::aidl::android::hardware::security::keymint::HardwareAuthToken;
Subrahmanyaman355e9772022-07-21 03:51:30 +000064using ::aidl::android::hardware::security::keymint::km_utils::authToken2AidlVec;
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000065using ::aidl::android::security::authorization::IKeystoreAuthorization;
Janis Danisevskis3a1eb672019-03-29 11:14:31 -070066
Andres Morales2d08dce2015-04-03 16:40:15 -070067namespace android {
68
69static const String16 KEYGUARD_PERMISSION("android.permission.ACCESS_KEYGUARD_SECURE_STORAGE");
70static const String16 DUMP_PERMISSION("android.permission.DUMP");
Subrahmanyaman355e9772022-07-21 03:51:30 +000071constexpr const char gatekeeperServiceName[] = "android.hardware.gatekeeper.IGatekeeper/default";
Andres Morales2d08dce2015-04-03 16:40:15 -070072
73class GateKeeperProxy : public BnGateKeeperService {
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000074 public:
Andres Morales2d08dce2015-04-03 16:40:15 -070075 GateKeeperProxy() {
Adrian Rooscb4ed1b2017-04-12 13:03:04 -070076 clear_state_if_needed_done = false;
Chris Phoenixa84ce0c2017-01-24 13:09:39 -080077 hw_device = IGatekeeper::getService();
Subrahmanyaman355e9772022-07-21 03:51:30 +000078 ::ndk::SpAIBinder ks2Binder(AServiceManager_getService(gatekeeperServiceName));
79 aidl_hw_device = AidlIGatekeeper::fromBinder(ks2Binder);
David Anderson97400bd2019-02-15 15:59:39 -080080 is_running_gsi = android::base::GetBoolProperty(android::gsi::kGsiBootedProp, false);
Andres Moralesfef908e2015-07-07 10:28:15 -070081
Subrahmanyaman355e9772022-07-21 03:51:30 +000082 if (!aidl_hw_device && !hw_device) {
Janis Danisevskis3a1eb672019-03-29 11:14:31 -070083 LOG(ERROR) << "Could not find Gatekeeper device, which makes me very sad.";
Andres Morales33dfdc72015-05-12 15:37:20 -070084 }
Andres Morales2d08dce2015-04-03 16:40:15 -070085 }
86
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +000087 virtual ~GateKeeperProxy() {}
Andres Morales2d08dce2015-04-03 16:40:15 -070088
Janis Danisevskis0a738d92020-09-23 17:00:20 -070089 void store_sid(uint32_t userId, uint64_t sid) {
Andres Morales6a49c2f2015-04-16 13:16:24 -070090 char filename[21];
Janis Danisevskis0a738d92020-09-23 17:00:20 -070091 snprintf(filename, sizeof(filename), "%u", userId);
Andres Morales6a49c2f2015-04-16 13:16:24 -070092 int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
93 if (fd < 0) {
Andres Moralesdcb3fbd2015-04-17 09:00:28 -070094 ALOGE("could not open file: %s: %s", filename, strerror(errno));
Andres Morales6a49c2f2015-04-16 13:16:24 -070095 return;
96 }
97 write(fd, &sid, sizeof(sid));
98 close(fd);
99 }
100
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700101 void clear_state_if_needed() {
102 if (clear_state_if_needed_done) {
103 return;
104 }
105
David Anderson97400bd2019-02-15 15:59:39 -0800106 if (mark_cold_boot() && !is_running_gsi) {
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700107 ALOGI("cold boot: clearing state");
Subrahmanyaman355e9772022-07-21 03:51:30 +0000108 if (aidl_hw_device) {
109 aidl_hw_device->deleteAllUsers();
110 } else if (hw_device) {
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000111 hw_device->deleteAllUsers([](const GatekeeperResponse&) {});
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700112 }
113 }
114
115 clear_state_if_needed_done = true;
116 }
117
Andres Morales3c2086d2015-06-24 10:21:16 -0700118 bool mark_cold_boot() {
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000119 const char* filename = ".coldboot";
Andres Morales3c2086d2015-06-24 10:21:16 -0700120 if (access(filename, F_OK) == -1) {
121 int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
122 if (fd < 0) {
123 ALOGE("could not open file: %s : %s", filename, strerror(errno));
124 return false;
125 }
126 close(fd);
127 return true;
128 }
129 return false;
130 }
131
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700132 void maybe_store_sid(uint32_t userId, uint64_t sid) {
Andres Morales6a49c2f2015-04-16 13:16:24 -0700133 char filename[21];
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700134 snprintf(filename, sizeof(filename), "%u", userId);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700135 if (access(filename, F_OK) == -1) {
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700136 store_sid(userId, sid);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700137 }
138 }
139
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700140 uint64_t read_sid(uint32_t userId) {
Andres Morales6a49c2f2015-04-16 13:16:24 -0700141 char filename[21];
142 uint64_t sid;
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700143 snprintf(filename, sizeof(filename), "%u", userId);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700144 int fd = open(filename, O_RDONLY);
145 if (fd < 0) return 0;
146 read(fd, &sid, sizeof(sid));
Andres Morales0b0435e2015-07-10 09:47:09 -0700147 close(fd);
Andres Morales6a49c2f2015-04-16 13:16:24 -0700148 return sid;
149 }
150
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700151 void clear_sid(uint32_t userId) {
Andres Moralesdcb3fbd2015-04-17 09:00:28 -0700152 char filename[21];
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700153 snprintf(filename, sizeof(filename), "%u", userId);
Andres Moralesdcb3fbd2015-04-17 09:00:28 -0700154 if (remove(filename) < 0) {
155 ALOGE("%s: could not remove file [%s], attempting 0 write", __func__, strerror(errno));
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700156 store_sid(userId, 0);
Andres Moralesdcb3fbd2015-04-17 09:00:28 -0700157 }
158 }
159
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700160 // This should only be called on userIds being passed to the GateKeeper HAL. It ensures that
David Anderson97400bd2019-02-15 15:59:39 -0800161 // secure storage shared across a GSI image and a host image will not overlap.
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700162 uint32_t adjust_userId(uint32_t userId) {
David Anderson97400bd2019-02-15 15:59:39 -0800163 static constexpr uint32_t kGsiOffset = 1000000;
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700164 CHECK(userId < kGsiOffset);
Subrahmanyaman355e9772022-07-21 03:51:30 +0000165 CHECK((aidl_hw_device != nullptr) || (hw_device != nullptr));
David Anderson97400bd2019-02-15 15:59:39 -0800166 if (is_running_gsi) {
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700167 return userId + kGsiOffset;
David Anderson97400bd2019-02-15 15:59:39 -0800168 }
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700169 return userId;
David Anderson97400bd2019-02-15 15:59:39 -0800170 }
171
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700172#define GK_ERROR *gkResponse = GKResponse::error(), Status::ok()
173
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700174 Status enroll(int32_t userId, const std::optional<std::vector<uint8_t>>& currentPasswordHandle,
Jooyung Han57110a42020-01-23 13:28:36 +0900175 const std::optional<std::vector<uint8_t>>& currentPassword,
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700176 const std::vector<uint8_t>& desiredPassword, GKResponse* gkResponse) override {
Andres Morales2d08dce2015-04-03 16:40:15 -0700177 IPCThreadState* ipc = IPCThreadState::self();
178 const int calling_pid = ipc->getCallingPid();
179 const int calling_uid = ipc->getCallingUid();
180 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700181 return GK_ERROR;
Andres Morales2d08dce2015-04-03 16:40:15 -0700182 }
183
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700184 // Make sure to clear any state from before factory reset as soon as a credential is
185 // enrolled (which may happen during device setup).
186 clear_state_if_needed();
187
Andres Morales2d08dce2015-04-03 16:40:15 -0700188 // need a desired password to enroll
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700189 if (desiredPassword.size() == 0) return GK_ERROR;
Andres Morales33dfdc72015-05-12 15:37:20 -0700190
Subrahmanyaman355e9772022-07-21 03:51:30 +0000191 if (!aidl_hw_device && !hw_device) {
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700192 LOG(ERROR) << "has no HAL to talk to";
193 return GK_ERROR;
194 }
Andres Morales835d96e2015-06-03 15:06:24 -0700195
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700196 android::hardware::hidl_vec<uint8_t> curPwdHandle;
197 android::hardware::hidl_vec<uint8_t> curPwd;
198
199 if (currentPasswordHandle && currentPassword) {
Subrahmanyaman355e9772022-07-21 03:51:30 +0000200 if (hw_device) {
201 // Hidl Implementations expects passwordHandle to be in
202 // gatekeeper::password_handle_t format.
203 if (currentPasswordHandle->size() != sizeof(gatekeeper::password_handle_t)) {
204 LOG(INFO) << "Password handle has wrong length";
205 return GK_ERROR;
206 }
Andres Morales835d96e2015-06-03 15:06:24 -0700207 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700208 curPwdHandle.setToExternal(const_cast<uint8_t*>(currentPasswordHandle->data()),
209 currentPasswordHandle->size());
210 curPwd.setToExternal(const_cast<uint8_t*>(currentPassword->data()),
211 currentPassword->size());
212 }
Andres Morales835d96e2015-06-03 15:06:24 -0700213
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700214 android::hardware::hidl_vec<uint8_t> newPwd;
215 newPwd.setToExternal(const_cast<uint8_t*>(desiredPassword.data()), desiredPassword.size());
Alexey Polyudov275aece2016-10-18 13:59:26 -0700216
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700217 uint32_t hw_userId = adjust_userId(userId);
Subrahmanyaman355e9772022-07-21 03:51:30 +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 &&
231 rsp.timeoutMs > 0) {
232 *gkResponse = GKResponse::retry(rsp.timeoutMs);
233 } else {
234 *gkResponse = GKResponse::error();
235 }
236 } else if (hw_device) {
237 // HIDL gatekeeper service
238 Return<void> hwRes = hw_device->enroll(
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700239 hw_userId, curPwdHandle, curPwd, newPwd,
240 [&gkResponse](const GatekeeperResponse& rsp) {
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700241 if (rsp.code >= GatekeeperStatusCode::STATUS_OK) {
242 *gkResponse = GKResponse::ok({rsp.data.begin(), rsp.data.end()});
243 } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT &&
244 rsp.timeout > 0) {
245 *gkResponse = GKResponse::retry(rsp.timeout);
246 } else {
247 *gkResponse = GKResponse::error();
Alexey Polyudov275aece2016-10-18 13:59:26 -0700248 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700249 });
Subrahmanyaman355e9772022-07-21 03:51:30 +0000250 if (!hwRes.isOk()) {
251 LOG(ERROR) << "enroll transaction failed";
252 return GK_ERROR;
253 }
254 if (gkResponse->response_code() == GKResponseCode::OK) {
255 if (gkResponse->payload().size() != sizeof(gatekeeper::password_handle_t)) {
256 LOG(ERROR) << "HAL returned password handle of invalid length "
257 << gkResponse->payload().size();
258 return GK_ERROR;
259 }
260
261 const gatekeeper::password_handle_t* handle =
262 reinterpret_cast<const gatekeeper::password_handle_t*>(
263 gkResponse->payload().data());
264 secureUserId = handle->user_id;
265 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700266 }
267
268 if (gkResponse->response_code() == GKResponseCode::OK && !gkResponse->should_reenroll()) {
Subrahmanyaman355e9772022-07-21 03:51:30 +0000269 store_sid(userId, secureUserId);
Andres Morales531e3e82015-06-01 17:23:04 -0700270
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700271 GKResponse verifyResponse;
Andres Morales531e3e82015-06-01 17:23:04 -0700272 // immediately verify this password so we don't ask the user to enter it again
273 // if they just created it.
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700274 auto status = verify(userId, gkResponse->payload(), desiredPassword, &verifyResponse);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700275 if (!status.isOk() || verifyResponse.response_code() != GKResponseCode::OK) {
276 LOG(ERROR) << "Failed to verify password after enrolling";
277 }
Andres Morales6a49c2f2015-04-16 13:16:24 -0700278 }
Andres Moralesae242922015-05-18 09:26:19 -0700279
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700280 return Status::ok();
Andres Morales2d08dce2015-04-03 16:40:15 -0700281 }
282
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700283 Status verify(int32_t userId, const ::std::vector<uint8_t>& enrolledPasswordHandle,
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700284 const ::std::vector<uint8_t>& providedPassword, GKResponse* gkResponse) override {
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700285 return verifyChallenge(userId, 0 /* challenge */, enrolledPasswordHandle, providedPassword,
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700286 gkResponse);
Andres Moralesc828ae82015-04-10 21:03:07 -0700287 }
288
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700289 Status verifyChallenge(int32_t userId, int64_t challenge,
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700290 const std::vector<uint8_t>& enrolledPasswordHandle,
291 const std::vector<uint8_t>& providedPassword,
292 GKResponse* gkResponse) override {
Andres Morales2d08dce2015-04-03 16:40:15 -0700293 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)) {
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700297 return GK_ERROR;
Andres Morales2d08dce2015-04-03 16:40:15 -0700298 }
299
300 // can't verify if we're missing either param
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700301 if (enrolledPasswordHandle.size() == 0 || providedPassword.size() == 0) return GK_ERROR;
Andres Morales2d08dce2015-04-03 16:40:15 -0700302
Subrahmanyaman355e9772022-07-21 03:51:30 +0000303 if (!aidl_hw_device && !hw_device) {
304 LOG(ERROR) << "has no HAL to talk to";
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700305 return GK_ERROR;
306 }
Subrahmanyaman355e9772022-07-21 03:51:30 +0000307
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";
312 return GK_ERROR;
313 }
314 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700315
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700316 uint32_t hw_userId = adjust_userId(userId);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700317 android::hardware::hidl_vec<uint8_t> curPwdHandle;
318 curPwdHandle.setToExternal(const_cast<uint8_t*>(enrolledPasswordHandle.data()),
319 enrolledPasswordHandle.size());
320 android::hardware::hidl_vec<uint8_t> enteredPwd;
321 enteredPwd.setToExternal(const_cast<uint8_t*>(providedPassword.data()),
322 providedPassword.size());
323
Subrahmanyaman355e9772022-07-21 03:51:30 +0000324 uint64_t secureUserId = 0;
325 if (aidl_hw_device) {
326 // AIDL gatekeeper service
327 AidlGatekeeperVerifyResp rsp;
328 auto result =
329 aidl_hw_device->verify(hw_userId, challenge, curPwdHandle, enteredPwd, &rsp);
330 if (!result.isOk()) {
331 LOG(ERROR) << "verify transaction failed";
332 return GK_ERROR;
333 }
334 if (rsp.statusCode >= AidlIGatekeeper::STATUS_OK) {
335 secureUserId = rsp.hardwareAuthToken.userId;
336 // Serialize HardwareAuthToken to a vector as hw_auth_token_t.
337 *gkResponse = GKResponse::ok(authToken2AidlVec(rsp.hardwareAuthToken),
338 rsp.statusCode ==
339 AidlIGatekeeper::STATUS_REENROLL /* reenroll */);
340 } else if (rsp.statusCode == AidlIGatekeeper::ERROR_RETRY_TIMEOUT) {
341 *gkResponse = GKResponse::retry(rsp.timeoutMs);
342 } else {
343 *gkResponse = GKResponse::error();
344 }
345 } else if (hw_device) {
346 // HIDL gatekeeper service
347 Return<void> hwRes = hw_device->verify(
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700348 hw_userId, challenge, curPwdHandle, enteredPwd,
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700349 [&gkResponse](const GatekeeperResponse& rsp) {
350 if (rsp.code >= GatekeeperStatusCode::STATUS_OK) {
351 *gkResponse = GKResponse::ok(
Subrahmanyaman355e9772022-07-21 03:51:30 +0000352 {rsp.data.begin(), rsp.data.end()},
353 rsp.code == GatekeeperStatusCode::STATUS_REENROLL /* reenroll */);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700354 } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT) {
355 *gkResponse = GKResponse::retry(rsp.timeout);
356 } else {
357 *gkResponse = GKResponse::error();
Alexey Polyudov275aece2016-10-18 13:59:26 -0700358 }
359 });
Andres Morales835d96e2015-06-03 15:06:24 -0700360
Subrahmanyaman355e9772022-07-21 03:51:30 +0000361 if (!hwRes.isOk()) {
362 LOG(ERROR) << "verify transaction failed";
363 return GK_ERROR;
364 }
365 const gatekeeper::password_handle_t* handle =
366 reinterpret_cast<const gatekeeper::password_handle_t*>(
367 enrolledPasswordHandle.data());
368 secureUserId = handle->user_id;
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700369 }
370
371 if (gkResponse->response_code() == GKResponseCode::OK) {
372 if (gkResponse->payload().size() != 0) {
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000373 // try to connect to IKeystoreAuthorization AIDL service first.
374 AIBinder* authzAIBinder =
Janis Danisevskis36ac55f2021-03-10 11:19:37 -0800375 AServiceManager_getService("android.security.authorization");
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000376 ::ndk::SpAIBinder authzBinder(authzAIBinder);
377 auto authzService = IKeystoreAuthorization::fromBinder(authzBinder);
378 if (authzService) {
379 if (gkResponse->payload().size() != sizeof(hw_auth_token_t)) {
380 LOG(ERROR) << "Incorrect size of AuthToken payload.";
381 return GK_ERROR;
382 }
383
384 const hw_auth_token_t* hwAuthToken =
385 reinterpret_cast<const hw_auth_token_t*>(gkResponse->payload().data());
386 HardwareAuthToken authToken;
387
388 authToken.timestamp.milliSeconds = betoh64(hwAuthToken->timestamp);
389 authToken.challenge = hwAuthToken->challenge;
Janis Danisevskis8e0035c2021-02-11 23:16:58 -0800390 authToken.userId = hwAuthToken->user_id;
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000391 authToken.authenticatorId = hwAuthToken->authenticator_id;
392 authToken.authenticatorType = static_cast<HardwareAuthenticatorType>(
393 betoh32(hwAuthToken->authenticator_type));
394 authToken.mac.assign(&hwAuthToken->hmac[0], &hwAuthToken->hmac[32]);
395 auto result = authzService->addAuthToken(authToken);
396 if (!result.isOk()) {
397 LOG(ERROR) << "Failure in sending AuthToken to AuthorizationService.";
398 return GK_ERROR;
399 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700400 } else {
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000401 LOG(ERROR) << "Cannot deliver auth token. Unable to communicate with "
402 "Keystore.";
403 return GK_ERROR;
Andres Morales835d96e2015-06-03 15:06:24 -0700404 }
405 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700406
Subrahmanyaman355e9772022-07-21 03:51:30 +0000407 maybe_store_sid(userId, secureUserId);
Andres Morales33dfdc72015-05-12 15:37:20 -0700408 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700409
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700410 return Status::ok();
Andres Morales6a49c2f2015-04-16 13:16:24 -0700411 }
412
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700413 Status getSecureUserId(int32_t userId, int64_t* sid) override {
414 *sid = read_sid(userId);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700415 return Status::ok();
416 }
Andres Morales2d08dce2015-04-03 16:40:15 -0700417
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700418 Status clearSecureUserId(int32_t userId) override {
Andres Morales7c9c3bc2015-04-16 15:57:17 -0700419 IPCThreadState* ipc = IPCThreadState::self();
420 const int calling_pid = ipc->getCallingPid();
421 const int calling_uid = ipc->getCallingUid();
422 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
423 ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700424 return Status::ok();
Andres Morales7c9c3bc2015-04-16 15:57:17 -0700425 }
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700426 clear_sid(userId);
Andres Morales3c2086d2015-06-24 10:21:16 -0700427
Subrahmanyaman355e9772022-07-21 03:51:30 +0000428 uint32_t hw_userId = adjust_userId(userId);
429 if (aidl_hw_device) {
430 aidl_hw_device->deleteUser(hw_userId);
431 } else if (hw_device) {
Janis Danisevskis0a738d92020-09-23 17:00:20 -0700432 hw_device->deleteUser(hw_userId, [](const GatekeeperResponse&) {});
Andres Morales3c2086d2015-06-24 10:21:16 -0700433 }
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700434 return Status::ok();
Andres Morales7c9c3bc2015-04-16 15:57:17 -0700435 }
436
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700437 Status reportDeviceSetupComplete() override {
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700438 IPCThreadState* ipc = IPCThreadState::self();
439 const int calling_pid = ipc->getCallingPid();
440 const int calling_uid = ipc->getCallingUid();
441 if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) {
442 ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid);
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700443 return Status::ok();
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700444 }
445
446 clear_state_if_needed();
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700447 return Status::ok();
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700448 }
449
Janis Danisevskis3a1eb672019-03-29 11:14:31 -0700450 status_t dump(int fd, const Vector<String16>&) override {
Andres Morales2d08dce2015-04-03 16:40:15 -0700451 IPCThreadState* ipc = IPCThreadState::self();
452 const int pid = ipc->getCallingPid();
453 const int uid = ipc->getCallingUid();
454 if (!PermissionCache::checkPermission(DUMP_PERMISSION, pid, uid)) {
455 return PERMISSION_DENIED;
456 }
457
Subrahmanyaman355e9772022-07-21 03:51:30 +0000458 if (aidl_hw_device == nullptr && hw_device == nullptr) {
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000459 const char* result = "Device not available";
Andres Morales2d08dce2015-04-03 16:40:15 -0700460 write(fd, result, strlen(result) + 1);
461 } else {
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000462 const char* result = "OK";
Andres Morales2d08dce2015-04-03 16:40:15 -0700463 write(fd, result, strlen(result) + 1);
464 }
465
Elliott Hughes643268f2018-10-08 11:10:11 -0700466 return OK;
Andres Morales2d08dce2015-04-03 16:40:15 -0700467 }
468
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000469 private:
Subrahmanyaman355e9772022-07-21 03:51:30 +0000470 // AIDL gatekeeper service.
471 std::shared_ptr<AidlIGatekeeper> aidl_hw_device;
472 // HIDL gatekeeper service.
Alexey Polyudov275aece2016-10-18 13:59:26 -0700473 sp<IGatekeeper> hw_device;
Adrian Rooscb4ed1b2017-04-12 13:03:04 -0700474
475 bool clear_state_if_needed_done;
David Anderson97400bd2019-02-15 15:59:39 -0800476 bool is_running_gsi;
Andres Morales2d08dce2015-04-03 16:40:15 -0700477};
Hasini Gunasinghe6fd56032020-12-08 21:08:13 +0000478} // namespace android
Andres Morales2d08dce2015-04-03 16:40:15 -0700479
Andres Morales6a49c2f2015-04-16 13:16:24 -0700480int main(int argc, char* argv[]) {
Andres Morales2d08dce2015-04-03 16:40:15 -0700481 ALOGI("Starting gatekeeperd...");
Andres Morales6a49c2f2015-04-16 13:16:24 -0700482 if (argc < 2) {
483 ALOGE("A directory must be specified!");
484 return 1;
485 }
486 if (chdir(argv[1]) == -1) {
487 ALOGE("chdir: %s: %s", argv[1], strerror(errno));
488 return 1;
489 }
490
Andres Morales2d08dce2015-04-03 16:40:15 -0700491 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
492 android::sp<android::GateKeeperProxy> proxy = new android::GateKeeperProxy();
Subrahmanyaman355e9772022-07-21 03:51:30 +0000493 android::status_t ret =
494 sm->addService(android::String16("android.service.gatekeeper.IGateKeeperService"), proxy);
Andres Morales2d08dce2015-04-03 16:40:15 -0700495 if (ret != android::OK) {
496 ALOGE("Couldn't register binder service!");
497 return -1;
498 }
499
500 /*
501 * We're the only thread in existence, so we're just going to process
502 * Binder transaction as a single-threaded program.
503 */
504 android::IPCThreadState::self()->joinThreadPool();
505 return 0;
506}