Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 1 | /* |
| 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 Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 16 | #define LOG_TAG "gatekeeperd" |
| 17 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 18 | #include "gatekeeperd.h" |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 19 | |
Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 20 | #include <endian.h> |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 21 | #include <errno.h> |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
| 23 | #include <unistd.h> |
Justin Yun | 68b0ec6 | 2017-08-16 18:54:20 +0900 | [diff] [blame] | 24 | #include <memory> |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 25 | |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 26 | #include <KeyMintUtils.h> |
David Anderson | 97400bd | 2019-02-15 15:59:39 -0800 | [diff] [blame] | 27 | #include <android-base/logging.h> |
| 28 | #include <android-base/properties.h> |
Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 29 | #include <android/binder_ibinder.h> |
| 30 | #include <android/binder_manager.h> |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 31 | #include <binder/IPCThreadState.h> |
| 32 | #include <binder/IServiceManager.h> |
| 33 | #include <binder/PermissionCache.h> |
Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 34 | #include <gatekeeper/password_handle.h> // for password_handle_t |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 35 | #include <hardware/hw_auth_token.h> |
David Anderson | 97400bd | 2019-02-15 15:59:39 -0800 | [diff] [blame] | 36 | #include <libgsi/libgsi.h> |
Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 37 | #include <log/log.h> |
Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 38 | #include <utils/String16.h> |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 39 | |
Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 40 | #include <aidl/android/hardware/security/keymint/HardwareAuthToken.h> |
| 41 | #include <aidl/android/security/authorization/IKeystoreAuthorization.h> |
Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 42 | #include <hidl/HidlSupport.h> |
Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 43 | |
| 44 | using android::sp; |
Louis Chang | 4c66b8a | 2021-01-18 10:01:12 +0000 | [diff] [blame] | 45 | using android::hardware::Return; |
Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 46 | using android::hardware::gatekeeper::V1_0::GatekeeperResponse; |
| 47 | using android::hardware::gatekeeper::V1_0::GatekeeperStatusCode; |
Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 48 | |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 49 | using AidlGatekeeperEnrollResp = aidl::android::hardware::gatekeeper::GatekeeperEnrollResponse; |
| 50 | using AidlGatekeeperVerifyResp = aidl::android::hardware::gatekeeper::GatekeeperVerifyResponse; |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 51 | |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 52 | using GKResponseCode = ::android::service::gatekeeper::ResponseCode; |
Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 53 | using ::aidl::android::hardware::security::keymint::HardwareAuthenticatorType; |
| 54 | using ::aidl::android::hardware::security::keymint::HardwareAuthToken; |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 55 | using ::aidl::android::hardware::security::keymint::km_utils::authToken2AidlVec; |
Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 56 | using ::aidl::android::security::authorization::IKeystoreAuthorization; |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 57 | |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 58 | namespace android { |
| 59 | |
| 60 | static const String16 KEYGUARD_PERMISSION("android.permission.ACCESS_KEYGUARD_SECURE_STORAGE"); |
| 61 | static const String16 DUMP_PERMISSION("android.permission.DUMP"); |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 62 | constexpr const char gatekeeperServiceName[] = "android.hardware.gatekeeper.IGatekeeper/default"; |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 63 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 64 | GateKeeperProxy::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 Morales | fef908e | 2015-07-07 10:28:15 -0700 | [diff] [blame] | 70 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 71 | if (!aidl_hw_device && !hw_device) { |
| 72 | LOG(ERROR) << "Could not find Gatekeeper device, which makes me very sad."; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void 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 | |
| 88 | void 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 Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 99 | } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 102 | clear_state_if_needed_done = true; |
| 103 | } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 104 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 105 | bool GateKeeperProxy::mark_cold_boot() { |
| 106 | const char* filename = ".coldboot"; |
| 107 | if (access(filename, F_OK) == -1) { |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 108 | int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR); |
| 109 | if (fd < 0) { |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 110 | ALOGE("could not open file: %s : %s", filename, strerror(errno)); |
| 111 | return false; |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 112 | } |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 113 | close(fd); |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 114 | return true; |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 115 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 116 | return false; |
| 117 | } |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 118 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 119 | void 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 Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 124 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 125 | } |
Adrian Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 126 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 127 | uint64_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 Morales | 3c2086d | 2015-06-24 10:21:16 -0700 | [diff] [blame] | 137 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 138 | void 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 Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 144 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 145 | } |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 146 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 147 | uint32_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 Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 153 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 154 | return userId; |
| 155 | } |
David Anderson | 97400bd | 2019-02-15 15:59:39 -0800 | [diff] [blame] | 156 | |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 157 | #define GK_ERROR *gkResponse = GKResponse::error(), Status::ok() |
| 158 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 159 | Status 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 Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 170 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 171 | // 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 Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 174 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 175 | // need a desired password to enroll |
| 176 | if (desiredPassword.size() == 0) return GK_ERROR; |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 177 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 178 | if (!aidl_hw_device && !hw_device) { |
| 179 | LOG(ERROR) << "has no HAL to talk to"; |
| 180 | return GK_ERROR; |
| 181 | } |
Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 182 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 183 | android::hardware::hidl_vec<uint8_t> curPwdHandle; |
| 184 | android::hardware::hidl_vec<uint8_t> curPwd; |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 185 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 186 | 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"; |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 192 | return GK_ERROR; |
| 193 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 194 | } |
| 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 Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 225 | hw_userId, curPwdHandle, curPwd, newPwd, |
| 226 | [&gkResponse](const GatekeeperResponse& rsp) { |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 227 | 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 Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 234 | } |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 235 | }); |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 236 | 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(); |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 244 | return GK_ERROR; |
| 245 | } |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 246 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 247 | const gatekeeper::password_handle_t* handle = |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 248 | reinterpret_cast<const gatekeeper::password_handle_t*>( |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 249 | gkResponse->payload().data()); |
| 250 | secureUserId = handle->user_id; |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 251 | } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 254 | 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 Morales | c828ae8 | 2015-04-10 21:03:07 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 266 | return Status::ok(); |
| 267 | } |
| 268 | |
| 269 | Status 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 | |
| 276 | Status 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 Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 299 | return GK_ERROR; |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 300 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 301 | } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 302 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 303 | 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 Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 310 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 311 | 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 Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 318 | return GK_ERROR; |
| 319 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 320 | 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(); |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 330 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 331 | } else if (hw_device) { |
| 332 | // HIDL gatekeeper service |
| 333 | Return<void> hwRes = hw_device->verify( |
Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 334 | hw_userId, challenge, curPwdHandle, enteredPwd, |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 335 | [&gkResponse](const GatekeeperResponse& rsp) { |
| 336 | if (rsp.code >= GatekeeperStatusCode::STATUS_OK) { |
| 337 | *gkResponse = GKResponse::ok( |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 338 | {rsp.data.begin(), rsp.data.end()}, |
| 339 | rsp.code == GatekeeperStatusCode::STATUS_REENROLL /* reenroll */); |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 340 | } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT) { |
| 341 | *gkResponse = GKResponse::retry(rsp.timeout); |
| 342 | } else { |
| 343 | *gkResponse = GKResponse::error(); |
Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 344 | } |
| 345 | }); |
Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 346 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 347 | if (!hwRes.isOk()) { |
| 348 | LOG(ERROR) << "verify transaction failed"; |
| 349 | return GK_ERROR; |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 350 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 351 | const gatekeeper::password_handle_t* handle = |
| 352 | reinterpret_cast<const gatekeeper::password_handle_t*>( |
| 353 | enrolledPasswordHandle.data()); |
| 354 | secureUserId = handle->user_id; |
| 355 | } |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 356 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 357 | 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 Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 366 | return GK_ERROR; |
Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 367 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 368 | |
| 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 Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 389 | } |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 390 | } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 391 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 392 | maybe_store_sid(userId, secureUserId); |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 395 | return Status::ok(); |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 396 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame^] | 397 | |
| 398 | Status GateKeeperProxy::getSecureUserId(int32_t userId, int64_t* sid) { |
| 399 | *sid = read_sid(userId); |
| 400 | return Status::ok(); |
| 401 | } |
| 402 | |
| 403 | Status 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 | |
| 422 | Status 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 | |
| 435 | status_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 |