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 | d2d320a | 2023-05-15 22:25:55 +0000 | [diff] [blame^] | 147 | Status GateKeeperProxy::adjust_userId(uint32_t userId, uint32_t* hw_userId) { |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 148 | static constexpr uint32_t kGsiOffset = 1000000; |
Pawan Wagh | d2d320a | 2023-05-15 22:25:55 +0000 | [diff] [blame^] | 149 | if (userId >= kGsiOffset) { |
| 150 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 151 | } |
Pawan Wagh | d2d320a | 2023-05-15 22:25:55 +0000 | [diff] [blame^] | 152 | |
| 153 | if ((aidl_hw_device == nullptr) && (hw_device == nullptr)) { |
| 154 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 155 | } |
| 156 | |
| 157 | if (is_running_gsi) { |
| 158 | *hw_userId = userId + kGsiOffset; |
| 159 | return Status::ok(); |
| 160 | } |
| 161 | *hw_userId = userId; |
| 162 | return Status::ok(); |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 163 | } |
David Anderson | 97400bd | 2019-02-15 15:59:39 -0800 | [diff] [blame] | 164 | |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 165 | #define GK_ERROR *gkResponse = GKResponse::error(), Status::ok() |
| 166 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 167 | Status GateKeeperProxy::enroll(int32_t userId, |
| 168 | const std::optional<std::vector<uint8_t>>& currentPasswordHandle, |
| 169 | const std::optional<std::vector<uint8_t>>& currentPassword, |
| 170 | const std::vector<uint8_t>& desiredPassword, |
| 171 | GKResponse* gkResponse) { |
| 172 | IPCThreadState* ipc = IPCThreadState::self(); |
| 173 | const int calling_pid = ipc->getCallingPid(); |
| 174 | const int calling_uid = ipc->getCallingUid(); |
| 175 | if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) { |
| 176 | return GK_ERROR; |
| 177 | } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 178 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 179 | // Make sure to clear any state from before factory reset as soon as a credential is |
| 180 | // enrolled (which may happen during device setup). |
| 181 | clear_state_if_needed(); |
Adrian Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 182 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 183 | // need a desired password to enroll |
| 184 | if (desiredPassword.size() == 0) return GK_ERROR; |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 185 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 186 | if (!aidl_hw_device && !hw_device) { |
| 187 | LOG(ERROR) << "has no HAL to talk to"; |
| 188 | return GK_ERROR; |
| 189 | } |
Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 190 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 191 | android::hardware::hidl_vec<uint8_t> curPwdHandle; |
| 192 | android::hardware::hidl_vec<uint8_t> curPwd; |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 193 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 194 | if (currentPasswordHandle && currentPassword) { |
| 195 | if (hw_device) { |
| 196 | // Hidl Implementations expects passwordHandle to be in |
| 197 | // gatekeeper::password_handle_t format. |
| 198 | if (currentPasswordHandle->size() != sizeof(gatekeeper::password_handle_t)) { |
| 199 | LOG(INFO) << "Password handle has wrong length"; |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 200 | return GK_ERROR; |
| 201 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 202 | } |
| 203 | curPwdHandle.setToExternal(const_cast<uint8_t*>(currentPasswordHandle->data()), |
| 204 | currentPasswordHandle->size()); |
| 205 | curPwd.setToExternal(const_cast<uint8_t*>(currentPassword->data()), |
| 206 | currentPassword->size()); |
| 207 | } |
| 208 | |
| 209 | android::hardware::hidl_vec<uint8_t> newPwd; |
| 210 | newPwd.setToExternal(const_cast<uint8_t*>(desiredPassword.data()), desiredPassword.size()); |
| 211 | |
Pawan Wagh | d2d320a | 2023-05-15 22:25:55 +0000 | [diff] [blame^] | 212 | uint32_t hw_userId = 0; |
| 213 | Status result = adjust_userId(userId, &hw_userId); |
| 214 | if (!result.isOk()) { |
| 215 | return result; |
| 216 | } |
| 217 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 218 | 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 && rsp.timeoutMs > 0) { |
| 231 | *gkResponse = GKResponse::retry(rsp.timeoutMs); |
| 232 | } else { |
| 233 | *gkResponse = GKResponse::error(); |
| 234 | } |
| 235 | } else if (hw_device) { |
| 236 | // HIDL gatekeeper service |
| 237 | Return<void> hwRes = hw_device->enroll( |
Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 238 | hw_userId, curPwdHandle, curPwd, newPwd, |
| 239 | [&gkResponse](const GatekeeperResponse& rsp) { |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 240 | if (rsp.code >= GatekeeperStatusCode::STATUS_OK) { |
| 241 | *gkResponse = GKResponse::ok({rsp.data.begin(), rsp.data.end()}); |
| 242 | } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT && |
| 243 | rsp.timeout > 0) { |
| 244 | *gkResponse = GKResponse::retry(rsp.timeout); |
| 245 | } else { |
| 246 | *gkResponse = GKResponse::error(); |
Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 247 | } |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 248 | }); |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 249 | if (!hwRes.isOk()) { |
| 250 | LOG(ERROR) << "enroll transaction failed"; |
| 251 | return GK_ERROR; |
| 252 | } |
| 253 | if (gkResponse->response_code() == GKResponseCode::OK) { |
| 254 | if (gkResponse->payload().size() != sizeof(gatekeeper::password_handle_t)) { |
| 255 | LOG(ERROR) << "HAL returned password handle of invalid length " |
| 256 | << gkResponse->payload().size(); |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 257 | return GK_ERROR; |
| 258 | } |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 259 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 260 | const gatekeeper::password_handle_t* handle = |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 261 | reinterpret_cast<const gatekeeper::password_handle_t*>( |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 262 | gkResponse->payload().data()); |
| 263 | secureUserId = handle->user_id; |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 264 | } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 267 | if (gkResponse->response_code() == GKResponseCode::OK && !gkResponse->should_reenroll()) { |
| 268 | store_sid(userId, secureUserId); |
| 269 | |
| 270 | GKResponse verifyResponse; |
| 271 | // immediately verify this password so we don't ask the user to enter it again |
| 272 | // if they just created it. |
| 273 | auto status = verify(userId, gkResponse->payload(), desiredPassword, &verifyResponse); |
| 274 | if (!status.isOk() || verifyResponse.response_code() != GKResponseCode::OK) { |
| 275 | LOG(ERROR) << "Failed to verify password after enrolling"; |
| 276 | } |
Andres Morales | c828ae8 | 2015-04-10 21:03:07 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 279 | return Status::ok(); |
| 280 | } |
| 281 | |
| 282 | Status GateKeeperProxy::verify(int32_t userId, const ::std::vector<uint8_t>& enrolledPasswordHandle, |
| 283 | const ::std::vector<uint8_t>& providedPassword, |
| 284 | GKResponse* gkResponse) { |
| 285 | return verifyChallenge(userId, 0 /* challenge */, enrolledPasswordHandle, providedPassword, |
| 286 | gkResponse); |
| 287 | } |
| 288 | |
| 289 | Status GateKeeperProxy::verifyChallenge(int32_t userId, int64_t challenge, |
| 290 | const std::vector<uint8_t>& enrolledPasswordHandle, |
| 291 | const std::vector<uint8_t>& providedPassword, |
| 292 | GKResponse* gkResponse) { |
| 293 | 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)) { |
| 297 | return GK_ERROR; |
| 298 | } |
| 299 | |
| 300 | // can't verify if we're missing either param |
| 301 | if (enrolledPasswordHandle.size() == 0 || providedPassword.size() == 0) return GK_ERROR; |
| 302 | |
| 303 | if (!aidl_hw_device && !hw_device) { |
| 304 | LOG(ERROR) << "has no HAL to talk to"; |
| 305 | return GK_ERROR; |
| 306 | } |
| 307 | |
| 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"; |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 312 | return GK_ERROR; |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 313 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 314 | } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 315 | |
Pawan Wagh | d2d320a | 2023-05-15 22:25:55 +0000 | [diff] [blame^] | 316 | uint32_t hw_userId = 0; |
| 317 | Status result = adjust_userId(userId, &hw_userId); |
| 318 | if (!result.isOk()) { |
| 319 | return result; |
| 320 | } |
| 321 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 322 | android::hardware::hidl_vec<uint8_t> curPwdHandle; |
| 323 | curPwdHandle.setToExternal(const_cast<uint8_t*>(enrolledPasswordHandle.data()), |
| 324 | enrolledPasswordHandle.size()); |
| 325 | android::hardware::hidl_vec<uint8_t> enteredPwd; |
| 326 | enteredPwd.setToExternal(const_cast<uint8_t*>(providedPassword.data()), |
| 327 | providedPassword.size()); |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 328 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 329 | uint64_t secureUserId = 0; |
| 330 | if (aidl_hw_device) { |
| 331 | // AIDL gatekeeper service |
| 332 | AidlGatekeeperVerifyResp rsp; |
| 333 | auto result = aidl_hw_device->verify(hw_userId, challenge, curPwdHandle, enteredPwd, &rsp); |
| 334 | if (!result.isOk()) { |
| 335 | LOG(ERROR) << "verify transaction failed"; |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 336 | return GK_ERROR; |
| 337 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 338 | if (rsp.statusCode >= AidlIGatekeeper::STATUS_OK) { |
| 339 | secureUserId = rsp.hardwareAuthToken.userId; |
| 340 | // Serialize HardwareAuthToken to a vector as hw_auth_token_t. |
| 341 | *gkResponse = GKResponse::ok( |
| 342 | authToken2AidlVec(rsp.hardwareAuthToken), |
| 343 | rsp.statusCode == AidlIGatekeeper::STATUS_REENROLL /* reenroll */); |
| 344 | } else if (rsp.statusCode == AidlIGatekeeper::ERROR_RETRY_TIMEOUT) { |
| 345 | *gkResponse = GKResponse::retry(rsp.timeoutMs); |
| 346 | } else { |
| 347 | *gkResponse = GKResponse::error(); |
Subrahmanyaman | 355e977 | 2022-07-21 03:51:30 +0000 | [diff] [blame] | 348 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 349 | } else if (hw_device) { |
| 350 | // HIDL gatekeeper service |
| 351 | Return<void> hwRes = hw_device->verify( |
Janis Danisevskis | 0a738d9 | 2020-09-23 17:00:20 -0700 | [diff] [blame] | 352 | hw_userId, challenge, curPwdHandle, enteredPwd, |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 353 | [&gkResponse](const GatekeeperResponse& rsp) { |
| 354 | if (rsp.code >= GatekeeperStatusCode::STATUS_OK) { |
| 355 | *gkResponse = GKResponse::ok( |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 356 | {rsp.data.begin(), rsp.data.end()}, |
| 357 | rsp.code == GatekeeperStatusCode::STATUS_REENROLL /* reenroll */); |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 358 | } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT) { |
| 359 | *gkResponse = GKResponse::retry(rsp.timeout); |
| 360 | } else { |
| 361 | *gkResponse = GKResponse::error(); |
Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 362 | } |
| 363 | }); |
Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 364 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 365 | if (!hwRes.isOk()) { |
| 366 | LOG(ERROR) << "verify transaction failed"; |
| 367 | return GK_ERROR; |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 368 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 369 | const gatekeeper::password_handle_t* handle = |
| 370 | reinterpret_cast<const gatekeeper::password_handle_t*>( |
| 371 | enrolledPasswordHandle.data()); |
| 372 | secureUserId = handle->user_id; |
| 373 | } |
Janis Danisevskis | 3a1eb67 | 2019-03-29 11:14:31 -0700 | [diff] [blame] | 374 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 375 | if (gkResponse->response_code() == GKResponseCode::OK) { |
| 376 | if (gkResponse->payload().size() != 0) { |
| 377 | // try to connect to IKeystoreAuthorization AIDL service first. |
| 378 | AIBinder* authzAIBinder = AServiceManager_getService("android.security.authorization"); |
| 379 | ::ndk::SpAIBinder authzBinder(authzAIBinder); |
| 380 | auto authzService = IKeystoreAuthorization::fromBinder(authzBinder); |
| 381 | if (authzService) { |
| 382 | if (gkResponse->payload().size() != sizeof(hw_auth_token_t)) { |
| 383 | LOG(ERROR) << "Incorrect size of AuthToken payload."; |
Hasini Gunasinghe | 6fd5603 | 2020-12-08 21:08:13 +0000 | [diff] [blame] | 384 | return GK_ERROR; |
Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 385 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 386 | |
| 387 | const hw_auth_token_t* hwAuthToken = |
| 388 | reinterpret_cast<const hw_auth_token_t*>(gkResponse->payload().data()); |
| 389 | HardwareAuthToken authToken; |
| 390 | |
| 391 | authToken.timestamp.milliSeconds = betoh64(hwAuthToken->timestamp); |
| 392 | authToken.challenge = hwAuthToken->challenge; |
| 393 | authToken.userId = hwAuthToken->user_id; |
| 394 | authToken.authenticatorId = hwAuthToken->authenticator_id; |
| 395 | authToken.authenticatorType = static_cast<HardwareAuthenticatorType>( |
| 396 | betoh32(hwAuthToken->authenticator_type)); |
| 397 | authToken.mac.assign(&hwAuthToken->hmac[0], &hwAuthToken->hmac[32]); |
| 398 | auto result = authzService->addAuthToken(authToken); |
| 399 | if (!result.isOk()) { |
| 400 | LOG(ERROR) << "Failure in sending AuthToken to AuthorizationService."; |
| 401 | return GK_ERROR; |
| 402 | } |
| 403 | } else { |
| 404 | LOG(ERROR) << "Cannot deliver auth token. Unable to communicate with " |
| 405 | "Keystore."; |
| 406 | return GK_ERROR; |
Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 407 | } |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 408 | } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 409 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 410 | maybe_store_sid(userId, secureUserId); |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 413 | return Status::ok(); |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 414 | } |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 415 | |
| 416 | Status GateKeeperProxy::getSecureUserId(int32_t userId, int64_t* sid) { |
| 417 | *sid = read_sid(userId); |
| 418 | return Status::ok(); |
| 419 | } |
| 420 | |
| 421 | Status GateKeeperProxy::clearSecureUserId(int32_t userId) { |
| 422 | IPCThreadState* ipc = IPCThreadState::self(); |
| 423 | const int calling_pid = ipc->getCallingPid(); |
| 424 | const int calling_uid = ipc->getCallingUid(); |
| 425 | if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) { |
| 426 | ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid); |
| 427 | return Status::ok(); |
| 428 | } |
| 429 | clear_sid(userId); |
| 430 | |
Pawan Wagh | d2d320a | 2023-05-15 22:25:55 +0000 | [diff] [blame^] | 431 | uint32_t hw_userId = 0; |
| 432 | Status result = adjust_userId(userId, &hw_userId); |
| 433 | if (!result.isOk()) { |
| 434 | return result; |
| 435 | } |
| 436 | |
Pawan Wagh | c5c0c30 | 2023-04-21 22:04:31 +0000 | [diff] [blame] | 437 | if (aidl_hw_device) { |
| 438 | aidl_hw_device->deleteUser(hw_userId); |
| 439 | } else if (hw_device) { |
| 440 | hw_device->deleteUser(hw_userId, [](const GatekeeperResponse&) {}); |
| 441 | } |
| 442 | return Status::ok(); |
| 443 | } |
| 444 | |
| 445 | Status GateKeeperProxy::reportDeviceSetupComplete() { |
| 446 | IPCThreadState* ipc = IPCThreadState::self(); |
| 447 | const int calling_pid = ipc->getCallingPid(); |
| 448 | const int calling_uid = ipc->getCallingUid(); |
| 449 | if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) { |
| 450 | ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid); |
| 451 | return Status::ok(); |
| 452 | } |
| 453 | |
| 454 | clear_state_if_needed(); |
| 455 | return Status::ok(); |
| 456 | } |
| 457 | |
| 458 | status_t GateKeeperProxy::dump(int fd, const Vector<String16>&) { |
| 459 | IPCThreadState* ipc = IPCThreadState::self(); |
| 460 | const int pid = ipc->getCallingPid(); |
| 461 | const int uid = ipc->getCallingUid(); |
| 462 | if (!PermissionCache::checkPermission(DUMP_PERMISSION, pid, uid)) { |
| 463 | return PERMISSION_DENIED; |
| 464 | } |
| 465 | |
| 466 | if (aidl_hw_device == nullptr && hw_device == nullptr) { |
| 467 | const char* result = "Device not available"; |
| 468 | write(fd, result, strlen(result) + 1); |
| 469 | } else { |
| 470 | const char* result = "OK"; |
| 471 | write(fd, result, strlen(result) + 1); |
| 472 | } |
| 473 | |
| 474 | return OK; |
| 475 | } |
| 476 | } // namespace android |