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; |
Subrahmanya Manikanta Venkateswarlu Bhamidipati Kameswara Sri | 32e1c70 | 2023-06-13 17:37:29 +0000 | [diff] [blame] | 66 | 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 Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 73 | is_running_gsi = android::base::GetBoolProperty(android::gsi::kGsiBootedProp, false); |
Andres Morales | fef908e | 2015-07-07 10:28:15 -0700 | [diff] [blame] | 74 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 75 | if (!aidl_hw_device && !hw_device) { |
| 76 | LOG(ERROR) << "Could not find Gatekeeper device, which makes me very sad."; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void 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 | |
| 92 | void 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 Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 103 | } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 106 | clear_state_if_needed_done = true; |
| 107 | } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 108 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 109 | bool GateKeeperProxy::mark_cold_boot() { |
| 110 | const char* filename = ".coldboot"; |
| 111 | if (access(filename, F_OK) == -1) { |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 112 | int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR); |
| 113 | if (fd < 0) { |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 114 | ALOGE("could not open file: %s : %s", filename, strerror(errno)); |
| 115 | return false; |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 116 | } |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 117 | close(fd); |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 118 | return true; |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 119 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 120 | return false; |
| 121 | } |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 122 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 123 | void 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 Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 128 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 129 | } |
Adrian Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 130 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 131 | uint64_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 Morales | 3c2086d | 2015-06-24 10:21:16 -0700 | [diff] [blame] | 141 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 142 | void 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 Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 148 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 149 | } |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 150 | |
Pawan Wagh | d2d320a | 2023-05-15 22:25:55 +0000 | [diff] [blame] | 151 | Status GateKeeperProxy::adjust_userId(uint32_t userId, uint32_t* hw_userId) { |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 152 | static constexpr uint32_t kGsiOffset = 1000000; |
Pawan Wagh | d2d320a | 2023-05-15 22:25:55 +0000 | [diff] [blame] | 153 | if (userId >= kGsiOffset) { |
| 154 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 155 | } |
Pawan Wagh | d2d320a | 2023-05-15 22:25:55 +0000 | [diff] [blame] | 156 | |
| 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 Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 167 | } |
David Anderson | 97400bd | 2019-02-15 15:59:39 -0800 | [diff] [blame] | 168 | |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 169 | #define GK_ERROR *gkResponse = GKResponse::error(), Status::ok() |
| 170 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 171 | Status 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 Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 182 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 183 | // 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 Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 186 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 187 | // need a desired password to enroll |
| 188 | if (desiredPassword.size() == 0) return GK_ERROR; |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 189 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 190 | if (!aidl_hw_device && !hw_device) { |
| 191 | LOG(ERROR) << "has no HAL to talk to"; |
| 192 | return GK_ERROR; |
| 193 | } |
Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 194 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 195 | android::hardware::hidl_vec<uint8_t> curPwdHandle; |
| 196 | android::hardware::hidl_vec<uint8_t> curPwd; |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 197 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 198 | 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"; |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 204 | return GK_ERROR; |
| 205 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 206 | } |
| 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 Wagh | d2d320a | 2023-05-15 22:25:55 +0000 | [diff] [blame] | 216 | uint32_t hw_userId = 0; |
| 217 | Status result = adjust_userId(userId, &hw_userId); |
| 218 | if (!result.isOk()) { |
| 219 | return result; |
| 220 | } |
| 221 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 222 | 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 Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 242 | hw_userId, curPwdHandle, curPwd, newPwd, |
| 243 | [&gkResponse](const GatekeeperResponse& rsp) { |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 244 | 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 Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 251 | } |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 252 | }); |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 253 | 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(); |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 261 | return GK_ERROR; |
| 262 | } |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 263 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 264 | const gatekeeper::password_handle_t* handle = |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 265 | reinterpret_cast<const gatekeeper::password_handle_t*>( |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 266 | gkResponse->payload().data()); |
| 267 | secureUserId = handle->user_id; |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 268 | } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 271 | 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 Morales | c828ae8 | 2015-04-10 21:03:07 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 283 | return Status::ok(); |
| 284 | } |
| 285 | |
| 286 | Status 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 | |
| 293 | Status 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 Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 316 | return GK_ERROR; |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 317 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 318 | } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 319 | |
Pawan Wagh | d2d320a | 2023-05-15 22:25:55 +0000 | [diff] [blame] | 320 | uint32_t hw_userId = 0; |
| 321 | Status result = adjust_userId(userId, &hw_userId); |
| 322 | if (!result.isOk()) { |
| 323 | return result; |
| 324 | } |
| 325 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 326 | 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 Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 332 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 333 | 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 Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 340 | return GK_ERROR; |
| 341 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 342 | 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(); |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 352 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 353 | } else if (hw_device) { |
| 354 | // HIDL gatekeeper service |
| 355 | Return<void> hwRes = hw_device->verify( |
Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 356 | hw_userId, challenge, curPwdHandle, enteredPwd, |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 357 | [&gkResponse](const GatekeeperResponse& rsp) { |
| 358 | if (rsp.code >= GatekeeperStatusCode::STATUS_OK) { |
| 359 | *gkResponse = GKResponse::ok( |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 360 | {rsp.data.begin(), rsp.data.end()}, |
| 361 | rsp.code == GatekeeperStatusCode::STATUS_REENROLL /* reenroll */); |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 362 | } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT) { |
| 363 | *gkResponse = GKResponse::retry(rsp.timeout); |
| 364 | } else { |
| 365 | *gkResponse = GKResponse::error(); |
Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 366 | } |
| 367 | }); |
Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 368 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 369 | if (!hwRes.isOk()) { |
| 370 | LOG(ERROR) << "verify transaction failed"; |
| 371 | return GK_ERROR; |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 372 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 373 | const gatekeeper::password_handle_t* handle = |
| 374 | reinterpret_cast<const gatekeeper::password_handle_t*>( |
| 375 | enrolledPasswordHandle.data()); |
| 376 | secureUserId = handle->user_id; |
| 377 | } |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 378 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 379 | 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 Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 388 | return GK_ERROR; |
Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 389 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 390 | |
| 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 Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 411 | } |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 412 | } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 413 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 414 | maybe_store_sid(userId, secureUserId); |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 417 | return Status::ok(); |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 418 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 419 | |
| 420 | Status GateKeeperProxy::getSecureUserId(int32_t userId, int64_t* sid) { |
| 421 | *sid = read_sid(userId); |
| 422 | return Status::ok(); |
| 423 | } |
| 424 | |
| 425 | Status 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 Wagh | d2d320a | 2023-05-15 22:25:55 +0000 | [diff] [blame] | 435 | uint32_t hw_userId = 0; |
| 436 | Status result = adjust_userId(userId, &hw_userId); |
| 437 | if (!result.isOk()) { |
| 438 | return result; |
| 439 | } |
| 440 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 441 | 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 | |
| 449 | Status 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 | |
| 462 | status_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 |