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 | */ |
| 16 | |
| 17 | #define LOG_TAG "gatekeeperd" |
| 18 | |
| 19 | #include "IGateKeeperService.h" |
| 20 | |
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> |
Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 23 | #include <inttypes.h> |
| 24 | #include <stdint.h> |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 25 | #include <unistd.h> |
Justin Yun | 68b0ec6 | 2017-08-16 18:54:20 +0900 | [diff] [blame] | 26 | #include <memory> |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 27 | |
Dmitry Dementyev | 0dd259c | 2017-11-14 12:53:39 -0800 | [diff] [blame] | 28 | #include <android/security/IKeystoreService.h> |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 29 | #include <binder/IPCThreadState.h> |
| 30 | #include <binder/IServiceManager.h> |
| 31 | #include <binder/PermissionCache.h> |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 32 | #include <gatekeeper/password_handle.h> // for password_handle_t |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 33 | #include <hardware/gatekeeper.h> |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 34 | #include <hardware/hw_auth_token.h> |
Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 35 | #include <keystore/keystore.h> // For error code |
Dmitry Dementyev | 0dd259c | 2017-11-14 12:53:39 -0800 | [diff] [blame] | 36 | #include <keystore/keystore_return_types.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/Log.h> |
| 39 | #include <utils/String16.h> |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 40 | |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 41 | #include "SoftGateKeeperDevice.h" |
| 42 | |
Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 43 | #include <hidl/HidlSupport.h> |
| 44 | #include <android/hardware/gatekeeper/1.0/IGatekeeper.h> |
| 45 | |
| 46 | using android::sp; |
| 47 | using android::hardware::gatekeeper::V1_0::IGatekeeper; |
| 48 | using android::hardware::gatekeeper::V1_0::GatekeeperStatusCode; |
| 49 | using android::hardware::gatekeeper::V1_0::GatekeeperResponse; |
| 50 | using android::hardware::Return; |
| 51 | |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 52 | namespace android { |
| 53 | |
| 54 | static const String16 KEYGUARD_PERMISSION("android.permission.ACCESS_KEYGUARD_SECURE_STORAGE"); |
| 55 | static const String16 DUMP_PERMISSION("android.permission.DUMP"); |
| 56 | |
| 57 | class GateKeeperProxy : public BnGateKeeperService { |
| 58 | public: |
| 59 | GateKeeperProxy() { |
Adrian Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 60 | clear_state_if_needed_done = false; |
Chris Phoenix | a84ce0c | 2017-01-24 13:09:39 -0800 | [diff] [blame] | 61 | hw_device = IGatekeeper::getService(); |
Andres Morales | fef908e | 2015-07-07 10:28:15 -0700 | [diff] [blame] | 62 | |
Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 63 | if (hw_device == nullptr) { |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 64 | ALOGW("falling back to software GateKeeper"); |
| 65 | soft_device.reset(new SoftGateKeeperDevice()); |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 66 | } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | virtual ~GateKeeperProxy() { |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 72 | void store_sid(uint32_t uid, uint64_t sid) { |
| 73 | char filename[21]; |
George Burgess IV | e7aa2b2 | 2016-03-02 14:02:55 -0800 | [diff] [blame] | 74 | snprintf(filename, sizeof(filename), "%u", uid); |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 75 | int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR); |
| 76 | if (fd < 0) { |
Andres Morales | dcb3fbd | 2015-04-17 09:00:28 -0700 | [diff] [blame] | 77 | ALOGE("could not open file: %s: %s", filename, strerror(errno)); |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | write(fd, &sid, sizeof(sid)); |
| 81 | close(fd); |
| 82 | } |
| 83 | |
Adrian Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 84 | void clear_state_if_needed() { |
| 85 | if (clear_state_if_needed_done) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | if (mark_cold_boot()) { |
| 90 | ALOGI("cold boot: clearing state"); |
| 91 | if (hw_device != nullptr) { |
| 92 | hw_device->deleteAllUsers([](const GatekeeperResponse &){}); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | clear_state_if_needed_done = true; |
| 97 | } |
| 98 | |
Andres Morales | 3c2086d | 2015-06-24 10:21:16 -0700 | [diff] [blame] | 99 | bool mark_cold_boot() { |
| 100 | const char *filename = ".coldboot"; |
| 101 | if (access(filename, F_OK) == -1) { |
| 102 | int fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR); |
| 103 | if (fd < 0) { |
| 104 | ALOGE("could not open file: %s : %s", filename, strerror(errno)); |
| 105 | return false; |
| 106 | } |
| 107 | close(fd); |
| 108 | return true; |
| 109 | } |
| 110 | return false; |
| 111 | } |
| 112 | |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 113 | void maybe_store_sid(uint32_t uid, uint64_t sid) { |
| 114 | char filename[21]; |
George Burgess IV | e7aa2b2 | 2016-03-02 14:02:55 -0800 | [diff] [blame] | 115 | snprintf(filename, sizeof(filename), "%u", uid); |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 116 | if (access(filename, F_OK) == -1) { |
| 117 | store_sid(uid, sid); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | uint64_t read_sid(uint32_t uid) { |
| 122 | char filename[21]; |
| 123 | uint64_t sid; |
George Burgess IV | e7aa2b2 | 2016-03-02 14:02:55 -0800 | [diff] [blame] | 124 | snprintf(filename, sizeof(filename), "%u", uid); |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 125 | int fd = open(filename, O_RDONLY); |
| 126 | if (fd < 0) return 0; |
| 127 | read(fd, &sid, sizeof(sid)); |
Andres Morales | 0b0435e | 2015-07-10 09:47:09 -0700 | [diff] [blame] | 128 | close(fd); |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 129 | return sid; |
| 130 | } |
| 131 | |
Andres Morales | dcb3fbd | 2015-04-17 09:00:28 -0700 | [diff] [blame] | 132 | void clear_sid(uint32_t uid) { |
| 133 | char filename[21]; |
George Burgess IV | e7aa2b2 | 2016-03-02 14:02:55 -0800 | [diff] [blame] | 134 | snprintf(filename, sizeof(filename), "%u", uid); |
Andres Morales | dcb3fbd | 2015-04-17 09:00:28 -0700 | [diff] [blame] | 135 | if (remove(filename) < 0) { |
| 136 | ALOGE("%s: could not remove file [%s], attempting 0 write", __func__, strerror(errno)); |
| 137 | store_sid(uid, 0); |
| 138 | } |
| 139 | } |
| 140 | |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 141 | virtual int enroll(uint32_t uid, |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 142 | const uint8_t *current_password_handle, uint32_t current_password_handle_length, |
| 143 | const uint8_t *current_password, uint32_t current_password_length, |
| 144 | const uint8_t *desired_password, uint32_t desired_password_length, |
| 145 | uint8_t **enrolled_password_handle, uint32_t *enrolled_password_handle_length) { |
| 146 | IPCThreadState* ipc = IPCThreadState::self(); |
| 147 | const int calling_pid = ipc->getCallingPid(); |
| 148 | const int calling_uid = ipc->getCallingUid(); |
| 149 | if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) { |
| 150 | return PERMISSION_DENIED; |
| 151 | } |
| 152 | |
Adrian Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 153 | // Make sure to clear any state from before factory reset as soon as a credential is |
| 154 | // enrolled (which may happen during device setup). |
| 155 | clear_state_if_needed(); |
| 156 | |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 157 | // need a desired password to enroll |
| 158 | if (desired_password_length == 0) return -EINVAL; |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 159 | |
| 160 | int ret; |
Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 161 | if (hw_device != nullptr) { |
Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 162 | const gatekeeper::password_handle_t *handle = |
| 163 | reinterpret_cast<const gatekeeper::password_handle_t *>(current_password_handle); |
| 164 | |
Andres Morales | 7f6dcf6 | 2015-06-24 18:40:24 -0700 | [diff] [blame] | 165 | if (handle != NULL && handle->version != 0 && !handle->hardware_backed) { |
Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 166 | // handle is being re-enrolled from a software version. HAL probably won't accept |
| 167 | // the handle as valid, so we nullify it and enroll from scratch |
| 168 | current_password_handle = NULL; |
| 169 | current_password_handle_length = 0; |
| 170 | current_password = NULL; |
| 171 | current_password_length = 0; |
| 172 | } |
| 173 | |
Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 174 | android::hardware::hidl_vec<uint8_t> curPwdHandle; |
| 175 | curPwdHandle.setToExternal(const_cast<uint8_t*>(current_password_handle), |
| 176 | current_password_handle_length); |
| 177 | android::hardware::hidl_vec<uint8_t> curPwd; |
| 178 | curPwd.setToExternal(const_cast<uint8_t*>(current_password), |
| 179 | current_password_length); |
| 180 | android::hardware::hidl_vec<uint8_t> newPwd; |
| 181 | newPwd.setToExternal(const_cast<uint8_t*>(desired_password), |
| 182 | desired_password_length); |
| 183 | |
| 184 | Return<void> hwRes = hw_device->enroll(uid, curPwdHandle, curPwd, newPwd, |
| 185 | [&ret, enrolled_password_handle, enrolled_password_handle_length] |
| 186 | (const GatekeeperResponse &rsp) { |
| 187 | ret = static_cast<int>(rsp.code); // propagate errors |
| 188 | if (rsp.code >= GatekeeperStatusCode::STATUS_OK) { |
| 189 | if (enrolled_password_handle != nullptr && |
| 190 | enrolled_password_handle_length != nullptr) { |
| 191 | *enrolled_password_handle = new uint8_t[rsp.data.size()]; |
| 192 | *enrolled_password_handle_length = rsp.data.size(); |
| 193 | memcpy(*enrolled_password_handle, rsp.data.data(), |
| 194 | *enrolled_password_handle_length); |
| 195 | } |
| 196 | ret = 0; // all success states are reported as 0 |
| 197 | } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT && rsp.timeout > 0) { |
| 198 | ret = rsp.timeout; |
| 199 | } |
| 200 | }); |
Steven Moreland | 8133093 | 2017-01-03 17:01:27 -0800 | [diff] [blame] | 201 | if (!hwRes.isOk()) { |
Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 202 | ALOGE("enroll transaction failed\n"); |
| 203 | ret = -1; |
| 204 | } |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 205 | } else { |
| 206 | ret = soft_device->enroll(uid, |
| 207 | current_password_handle, current_password_handle_length, |
| 208 | current_password, current_password_length, |
| 209 | desired_password, desired_password_length, |
| 210 | enrolled_password_handle, enrolled_password_handle_length); |
| 211 | } |
| 212 | |
Alexey Polyudov | 8c63536 | 2016-09-07 18:51:28 -0700 | [diff] [blame] | 213 | if (ret == GATEKEEPER_RESPONSE_OK && (*enrolled_password_handle == nullptr || |
| 214 | *enrolled_password_handle_length != sizeof(password_handle_t))) { |
| 215 | ret = GATEKEEPER_RESPONSE_ERROR; |
| 216 | ALOGE("HAL: password_handle=%p size_of_handle=%" PRIu32 "\n", |
| 217 | *enrolled_password_handle, *enrolled_password_handle_length); |
| 218 | } |
| 219 | |
| 220 | if (ret == GATEKEEPER_RESPONSE_OK) { |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 221 | gatekeeper::password_handle_t *handle = |
| 222 | reinterpret_cast<gatekeeper::password_handle_t *>(*enrolled_password_handle); |
| 223 | store_sid(uid, handle->user_id); |
Andres Morales | 531e3e8 | 2015-06-01 17:23:04 -0700 | [diff] [blame] | 224 | bool rr; |
| 225 | |
| 226 | // immediately verify this password so we don't ask the user to enter it again |
| 227 | // if they just created it. |
| 228 | verify(uid, *enrolled_password_handle, sizeof(password_handle_t), desired_password, |
| 229 | desired_password_length, &rr); |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 230 | } |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 231 | |
| 232 | return ret; |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 235 | virtual int verify(uint32_t uid, |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 236 | const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length, |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 237 | const uint8_t *provided_password, uint32_t provided_password_length, bool *request_reenroll) { |
Kihyung Lee | d9ad02e | 2018-06-15 12:46:42 +0900 | [diff] [blame] | 238 | uint8_t *auth_token = nullptr; |
Andres Morales | c828ae8 | 2015-04-10 21:03:07 -0700 | [diff] [blame] | 239 | uint32_t auth_token_length; |
Kihyung Lee | d9ad02e | 2018-06-15 12:46:42 +0900 | [diff] [blame] | 240 | int ret = verifyChallenge(uid, 0, enrolled_password_handle, enrolled_password_handle_length, |
Andres Morales | c828ae8 | 2015-04-10 21:03:07 -0700 | [diff] [blame] | 241 | provided_password, provided_password_length, |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 242 | &auth_token, &auth_token_length, request_reenroll); |
Kihyung Lee | d9ad02e | 2018-06-15 12:46:42 +0900 | [diff] [blame] | 243 | delete [] auth_token; |
| 244 | return ret; |
Andres Morales | c828ae8 | 2015-04-10 21:03:07 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 247 | virtual int verifyChallenge(uint32_t uid, uint64_t challenge, |
Andres Morales | c828ae8 | 2015-04-10 21:03:07 -0700 | [diff] [blame] | 248 | const uint8_t *enrolled_password_handle, uint32_t enrolled_password_handle_length, |
| 249 | const uint8_t *provided_password, uint32_t provided_password_length, |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 250 | uint8_t **auth_token, uint32_t *auth_token_length, bool *request_reenroll) { |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 251 | IPCThreadState* ipc = IPCThreadState::self(); |
| 252 | const int calling_pid = ipc->getCallingPid(); |
| 253 | const int calling_uid = ipc->getCallingUid(); |
| 254 | if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) { |
| 255 | return PERMISSION_DENIED; |
| 256 | } |
| 257 | |
| 258 | // can't verify if we're missing either param |
| 259 | if ((enrolled_password_handle_length | provided_password_length) == 0) |
| 260 | return -EINVAL; |
| 261 | |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 262 | int ret; |
Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 263 | if (hw_device != nullptr) { |
Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 264 | const gatekeeper::password_handle_t *handle = |
| 265 | reinterpret_cast<const gatekeeper::password_handle_t *>(enrolled_password_handle); |
Andres Morales | 7f6dcf6 | 2015-06-24 18:40:24 -0700 | [diff] [blame] | 266 | // handle version 0 does not have hardware backed flag, and thus cannot be upgraded to |
| 267 | // a HAL if there was none before |
| 268 | if (handle->version == 0 || handle->hardware_backed) { |
Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 269 | android::hardware::hidl_vec<uint8_t> curPwdHandle; |
| 270 | curPwdHandle.setToExternal(const_cast<uint8_t*>(enrolled_password_handle), |
| 271 | enrolled_password_handle_length); |
| 272 | android::hardware::hidl_vec<uint8_t> enteredPwd; |
| 273 | enteredPwd.setToExternal(const_cast<uint8_t*>(provided_password), |
| 274 | provided_password_length); |
| 275 | Return<void> hwRes = hw_device->verify(uid, challenge, curPwdHandle, enteredPwd, |
| 276 | [&ret, request_reenroll, auth_token, auth_token_length] |
| 277 | (const GatekeeperResponse &rsp) { |
| 278 | ret = static_cast<int>(rsp.code); // propagate errors |
| 279 | if (auth_token != nullptr && auth_token_length != nullptr && |
| 280 | rsp.code >= GatekeeperStatusCode::STATUS_OK) { |
| 281 | *auth_token = new uint8_t[rsp.data.size()]; |
| 282 | *auth_token_length = rsp.data.size(); |
| 283 | memcpy(*auth_token, rsp.data.data(), *auth_token_length); |
| 284 | if (request_reenroll != nullptr) { |
| 285 | *request_reenroll = (rsp.code == GatekeeperStatusCode::STATUS_REENROLL); |
| 286 | } |
| 287 | ret = 0; // all success states are reported as 0 |
| 288 | } else if (rsp.code == GatekeeperStatusCode::ERROR_RETRY_TIMEOUT && |
| 289 | rsp.timeout > 0) { |
| 290 | ret = rsp.timeout; |
| 291 | } |
| 292 | }); |
Steven Moreland | 8133093 | 2017-01-03 17:01:27 -0800 | [diff] [blame] | 293 | if (!hwRes.isOk()) { |
Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 294 | ALOGE("verify transaction failed\n"); |
| 295 | ret = -1; |
| 296 | } |
Andres Morales | 835d96e | 2015-06-03 15:06:24 -0700 | [diff] [blame] | 297 | } else { |
| 298 | // upgrade scenario, a HAL has been added to this device where there was none before |
| 299 | SoftGateKeeperDevice soft_dev; |
| 300 | ret = soft_dev.verify(uid, challenge, |
| 301 | enrolled_password_handle, enrolled_password_handle_length, |
| 302 | provided_password, provided_password_length, auth_token, auth_token_length, |
| 303 | request_reenroll); |
| 304 | |
| 305 | if (ret == 0) { |
| 306 | // success! re-enroll with HAL |
| 307 | *request_reenroll = true; |
| 308 | } |
| 309 | } |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 310 | } else { |
| 311 | ret = soft_device->verify(uid, challenge, |
| 312 | enrolled_password_handle, enrolled_password_handle_length, |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 313 | provided_password, provided_password_length, auth_token, auth_token_length, |
| 314 | request_reenroll); |
Andres Morales | 33dfdc7 | 2015-05-12 15:37:20 -0700 | [diff] [blame] | 315 | } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 316 | |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 317 | if (ret == 0 && *auth_token != NULL && *auth_token_length > 0) { |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 318 | // TODO: cache service? |
| 319 | sp<IServiceManager> sm = defaultServiceManager(); |
| 320 | sp<IBinder> binder = sm->getService(String16("android.security.keystore")); |
Dmitry Dementyev | 0dd259c | 2017-11-14 12:53:39 -0800 | [diff] [blame] | 321 | sp<security::IKeystoreService> service = |
| 322 | interface_cast<security::IKeystoreService>(binder); |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 323 | if (service != NULL) { |
Dmitry Dementyev | 0dd259c | 2017-11-14 12:53:39 -0800 | [diff] [blame] | 324 | std::vector<uint8_t> auth_token_vector(*auth_token, |
| 325 | (*auth_token) + *auth_token_length); |
| 326 | int result = 0; |
Brian Young | 388ff6b | 2018-02-22 23:35:48 +0000 | [diff] [blame] | 327 | auto binder_result = service->addAuthToken(auth_token_vector, &result); |
Dmitry Dementyev | 0dd259c | 2017-11-14 12:53:39 -0800 | [diff] [blame] | 328 | if (!binder_result.isOk() || !keystore::KeyStoreServiceReturnCode(result).isOk()) { |
| 329 | ALOGE("Failure sending auth token to KeyStore: %" PRId32, result); |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 330 | } |
| 331 | } else { |
| 332 | ALOGE("Unable to communicate with KeyStore"); |
| 333 | } |
| 334 | } |
| 335 | |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 336 | if (ret == 0) { |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 337 | maybe_store_sid(uid, reinterpret_cast<const gatekeeper::password_handle_t *>( |
| 338 | enrolled_password_handle)->user_id); |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Andres Morales | ae24292 | 2015-05-18 09:26:19 -0700 | [diff] [blame] | 341 | return ret; |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Pavel Grafov | 9890f89 | 2017-06-28 19:03:58 +0100 | [diff] [blame] | 344 | virtual uint64_t getSecureUserId(uint32_t uid) { return read_sid(uid); } |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 345 | |
Andres Morales | 7c9c3bc | 2015-04-16 15:57:17 -0700 | [diff] [blame] | 346 | virtual void clearSecureUserId(uint32_t uid) { |
| 347 | IPCThreadState* ipc = IPCThreadState::self(); |
| 348 | const int calling_pid = ipc->getCallingPid(); |
| 349 | const int calling_uid = ipc->getCallingUid(); |
| 350 | if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) { |
| 351 | ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid); |
| 352 | return; |
| 353 | } |
Andres Morales | dcb3fbd | 2015-04-17 09:00:28 -0700 | [diff] [blame] | 354 | clear_sid(uid); |
Andres Morales | 3c2086d | 2015-06-24 10:21:16 -0700 | [diff] [blame] | 355 | |
Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 356 | if (hw_device != nullptr) { |
| 357 | hw_device->deleteUser(uid, [] (const GatekeeperResponse &){}); |
Andres Morales | 3c2086d | 2015-06-24 10:21:16 -0700 | [diff] [blame] | 358 | } |
Andres Morales | 7c9c3bc | 2015-04-16 15:57:17 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Adrian Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 361 | virtual void reportDeviceSetupComplete() { |
| 362 | IPCThreadState* ipc = IPCThreadState::self(); |
| 363 | const int calling_pid = ipc->getCallingPid(); |
| 364 | const int calling_uid = ipc->getCallingUid(); |
| 365 | if (!PermissionCache::checkPermission(KEYGUARD_PERMISSION, calling_pid, calling_uid)) { |
| 366 | ALOGE("%s: permission denied for [%d:%d]", __func__, calling_pid, calling_uid); |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | clear_state_if_needed(); |
| 371 | } |
| 372 | |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 373 | virtual status_t dump(int fd, const Vector<String16> &) { |
| 374 | IPCThreadState* ipc = IPCThreadState::self(); |
| 375 | const int pid = ipc->getCallingPid(); |
| 376 | const int uid = ipc->getCallingUid(); |
| 377 | if (!PermissionCache::checkPermission(DUMP_PERMISSION, pid, uid)) { |
| 378 | return PERMISSION_DENIED; |
| 379 | } |
| 380 | |
Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 381 | if (hw_device == NULL) { |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 382 | const char *result = "Device not available"; |
| 383 | write(fd, result, strlen(result) + 1); |
| 384 | } else { |
| 385 | const char *result = "OK"; |
| 386 | write(fd, result, strlen(result) + 1); |
| 387 | } |
| 388 | |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame^] | 389 | return OK; |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | private: |
Alexey Polyudov | 275aece | 2016-10-18 13:59:26 -0700 | [diff] [blame] | 393 | sp<IGatekeeper> hw_device; |
Justin Yun | 68b0ec6 | 2017-08-16 18:54:20 +0900 | [diff] [blame] | 394 | std::unique_ptr<SoftGateKeeperDevice> soft_device; |
Adrian Roos | cb4ed1b | 2017-04-12 13:03:04 -0700 | [diff] [blame] | 395 | |
| 396 | bool clear_state_if_needed_done; |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 397 | }; |
| 398 | }// namespace android |
| 399 | |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 400 | int main(int argc, char* argv[]) { |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 401 | ALOGI("Starting gatekeeperd..."); |
Andres Morales | 6a49c2f | 2015-04-16 13:16:24 -0700 | [diff] [blame] | 402 | if (argc < 2) { |
| 403 | ALOGE("A directory must be specified!"); |
| 404 | return 1; |
| 405 | } |
| 406 | if (chdir(argv[1]) == -1) { |
| 407 | ALOGE("chdir: %s: %s", argv[1], strerror(errno)); |
| 408 | return 1; |
| 409 | } |
| 410 | |
Andres Morales | 2d08dce | 2015-04-03 16:40:15 -0700 | [diff] [blame] | 411 | android::sp<android::IServiceManager> sm = android::defaultServiceManager(); |
| 412 | android::sp<android::GateKeeperProxy> proxy = new android::GateKeeperProxy(); |
| 413 | android::status_t ret = sm->addService( |
| 414 | android::String16("android.service.gatekeeper.IGateKeeperService"), proxy); |
| 415 | if (ret != android::OK) { |
| 416 | ALOGE("Couldn't register binder service!"); |
| 417 | return -1; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * We're the only thread in existence, so we're just going to process |
| 422 | * Binder transaction as a single-threaded program. |
| 423 | */ |
| 424 | android::IPCThreadState::self()->joinThreadPool(); |
| 425 | return 0; |
| 426 | } |