blob: 909749152ec56b5b699d8dd5830b241557d5d22a [file] [log] [blame]
Andres Moralesac808182015-02-26 14:11:04 -08001/*
2 * Copyright 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 */
Jiyong Park9414ec02017-08-09 21:10:16 +090016#include <gatekeeper/UniquePtr.h>
Andres Morales7d0f0402015-03-19 18:02:55 -070017#include <gatekeeper/gatekeeper.h>
Andres Moralesac808182015-02-26 14:11:04 -080018
Andres Moralesec9fd1d2015-04-20 07:45:50 -070019#include <endian.h>
20
Andres Moralesf436c802015-10-08 12:21:25 -070021#define DAY_IN_MS (1000 * 60 * 60 * 24)
22
Andres Morales7d0f0402015-03-19 18:02:55 -070023namespace gatekeeper {
Andres Moralesac808182015-02-26 14:11:04 -080024
Andres Morales7d0f0402015-03-19 18:02:55 -070025void GateKeeper::Enroll(const EnrollRequest &request, EnrollResponse *response) {
Andres Moralesac808182015-02-26 14:11:04 -080026 if (response == NULL) return;
27
Andres Moralesb2abaa82015-03-03 09:09:18 -080028 if (!request.provided_password.buffer.get()) {
Andres Morales7d0f0402015-03-19 18:02:55 -070029 response->error = ERROR_INVALID;
Andres Moralesac808182015-02-26 14:11:04 -080030 return;
31 }
Andres Moralesb2abaa82015-03-03 09:09:18 -080032
Andres Moralesaedf6052015-05-14 13:10:30 -070033 secure_id_t user_id = 0;// todo: rename to policy
34 uint32_t uid = request.user_id;
Andres Moralesb2abaa82015-03-03 09:09:18 -080035
Andres Moralesedd3e3d2015-03-12 13:30:15 -070036 if (request.password_handle.buffer.get() == NULL) {
37 // Password handle does not match what is stored, generate new SecureID
38 GetRandom(&user_id, sizeof(secure_id_t));
39 } else {
Matthew Maurera8f97092019-03-28 13:43:44 -070040 if (request.password_handle.length < sizeof(password_handle_t)) {
41 response->error = ERROR_INVALID;
42 return;
43 }
Andres Morales426fcfb2015-04-01 13:33:45 -070044 password_handle_t *pw_handle =
45 reinterpret_cast<password_handle_t *>(request.password_handle.buffer.get());
Andres Moralesaedf6052015-05-14 13:10:30 -070046
Andres Morales48a4f832015-05-29 10:13:31 -070047 if (pw_handle->version > HANDLE_VERSION) {
Andres Morales426fcfb2015-04-01 13:33:45 -070048 response->error = ERROR_INVALID;
49 return;
Andres Moralesedd3e3d2015-03-12 13:30:15 -070050 }
Andres Morales426fcfb2015-04-01 13:33:45 -070051
Andres Morales48a4f832015-05-29 10:13:31 -070052 user_id = pw_handle->user_id;
53
54 uint64_t timestamp = GetMillisecondsSinceBoot();
55
Andres Moralesaedf6052015-05-14 13:10:30 -070056 uint32_t timeout = 0;
Andres Morales48a4f832015-05-29 10:13:31 -070057 bool throttle = (pw_handle->version >= HANDLE_VERSION_THROTTLE);
Andres Moralesaedf6052015-05-14 13:10:30 -070058 if (throttle) {
Andres Morales48a4f832015-05-29 10:13:31 -070059 bool throttle_secure = pw_handle->flags & HANDLE_FLAG_THROTTLE_SECURE;
Andres Moralesaedf6052015-05-14 13:10:30 -070060 failure_record_t record;
Andres Morales48a4f832015-05-29 10:13:31 -070061 if (!GetFailureRecord(uid, user_id, &record, throttle_secure)) {
Andres Moralesaedf6052015-05-14 13:10:30 -070062 response->error = ERROR_UNKNOWN;
63 return;
64 }
65
Andres Morales48a4f832015-05-29 10:13:31 -070066 if (ThrottleRequest(uid, timestamp, &record, throttle_secure, response)) return;
Andres Moralesaedf6052015-05-14 13:10:30 -070067
Andres Morales48a4f832015-05-29 10:13:31 -070068 if (!IncrementFailureRecord(uid, user_id, timestamp, &record, throttle_secure)) {
Andres Moralesaedf6052015-05-14 13:10:30 -070069 response->error = ERROR_UNKNOWN;
70 return;
71 }
72
73 timeout = ComputeRetryTimeout(&record);
74 }
75
76 if (!DoVerify(pw_handle, request.enrolled_password)) {
77 // incorrect old password
78 if (throttle && timeout > 0) {
79 response->SetRetryTimeout(timeout);
80 } else {
81 response->error = ERROR_INVALID;
82 }
83 return;
84 }
Andres Moralesedd3e3d2015-03-12 13:30:15 -070085 }
86
Andres Morales48a4f832015-05-29 10:13:31 -070087 uint64_t flags = 0;
88 if (ClearFailureRecord(uid, user_id, true)) {
89 flags |= HANDLE_FLAG_THROTTLE_SECURE;
90 } else {
91 ClearFailureRecord(uid, user_id, false);
92 }
Andres Moralesaedf6052015-05-14 13:10:30 -070093
Andres Moralesedd3e3d2015-03-12 13:30:15 -070094 salt_t salt;
95 GetRandom(&salt, sizeof(salt));
96
Andres Moralesedd3e3d2015-03-12 13:30:15 -070097 SizedBuffer password_handle;
Andres Moralesaedf6052015-05-14 13:10:30 -070098 if (!CreatePasswordHandle(&password_handle,
Andres Morales48a4f832015-05-29 10:13:31 -070099 salt, user_id, flags, HANDLE_VERSION, request.provided_password.buffer.get(),
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700100 request.provided_password.length)) {
Andres Morales7d0f0402015-03-19 18:02:55 -0700101 response->error = ERROR_INVALID;
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700102 return;
103 }
104
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700105 response->SetEnrolledPasswordHandle(&password_handle);
Andres Moralesac808182015-02-26 14:11:04 -0800106}
107
Andres Morales7d0f0402015-03-19 18:02:55 -0700108void GateKeeper::Verify(const VerifyRequest &request, VerifyResponse *response) {
Andres Moralesac808182015-02-26 14:11:04 -0800109 if (response == NULL) return;
110
Andres Moralesb2abaa82015-03-03 09:09:18 -0800111 if (!request.provided_password.buffer.get() || !request.password_handle.buffer.get()) {
Andres Morales7d0f0402015-03-19 18:02:55 -0700112 response->error = ERROR_INVALID;
Andres Moralesac808182015-02-26 14:11:04 -0800113 return;
114 }
115
Matthew Maurera8f97092019-03-28 13:43:44 -0700116 if (request.password_handle.length < sizeof(password_handle_t)) {
117 response->error = ERROR_INVALID;
118 return;
119 }
120
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700121 password_handle_t *password_handle = reinterpret_cast<password_handle_t *>(
122 request.password_handle.buffer.get());
Andres Moralesb2abaa82015-03-03 09:09:18 -0800123
Andres Morales48a4f832015-05-29 10:13:31 -0700124 if (password_handle->version > HANDLE_VERSION) {
Andres Morales7d0f0402015-03-19 18:02:55 -0700125 response->error = ERROR_INVALID;
Andres Moralesb2abaa82015-03-03 09:09:18 -0800126 return;
127 }
128
Andres Morales426fcfb2015-04-01 13:33:45 -0700129 secure_id_t user_id = password_handle->user_id;
Andres Morales48a4f832015-05-29 10:13:31 -0700130 secure_id_t authenticator_id = 0;
Andres Moralesaedf6052015-05-14 13:10:30 -0700131 uint32_t uid = request.user_id;
Andres Moralesb2abaa82015-03-03 09:09:18 -0800132
Andres Moralesec9fd1d2015-04-20 07:45:50 -0700133 uint64_t timestamp = GetMillisecondsSinceBoot();
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700134
Andres Moralesaedf6052015-05-14 13:10:30 -0700135 uint32_t timeout = 0;
Andres Morales48a4f832015-05-29 10:13:31 -0700136 bool throttle = (password_handle->version >= HANDLE_VERSION_THROTTLE);
137 bool throttle_secure = password_handle->flags & HANDLE_FLAG_THROTTLE_SECURE;
Andres Moralesaedf6052015-05-14 13:10:30 -0700138 if (throttle) {
139 failure_record_t record;
Andres Morales48a4f832015-05-29 10:13:31 -0700140 if (!GetFailureRecord(uid, user_id, &record, throttle_secure)) {
Andres Moralesaedf6052015-05-14 13:10:30 -0700141 response->error = ERROR_UNKNOWN;
142 return;
143 }
144
Andres Morales48a4f832015-05-29 10:13:31 -0700145 if (ThrottleRequest(uid, timestamp, &record, throttle_secure, response)) return;
Andres Moralesaedf6052015-05-14 13:10:30 -0700146
Andres Morales48a4f832015-05-29 10:13:31 -0700147 if (!IncrementFailureRecord(uid, user_id, timestamp, &record, throttle_secure)) {
Andres Moralesaedf6052015-05-14 13:10:30 -0700148 response->error = ERROR_UNKNOWN;
149 return;
150 }
151
152 timeout = ComputeRetryTimeout(&record);
Andres Morales893fa7f2015-06-02 19:00:03 -0700153 } else {
154 response->request_reenroll = true;
Andres Moralesaedf6052015-05-14 13:10:30 -0700155 }
156
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700157 if (DoVerify(password_handle, request.provided_password)) {
Andres Moralesac808182015-02-26 14:11:04 -0800158 // Signature matches
Shawn Willdenf73a0102016-09-13 17:39:27 +0000159 UniquePtr<uint8_t> auth_token_buffer;
Andres Morales48a4f832015-05-29 10:13:31 -0700160 uint32_t auth_token_len;
161 MintAuthToken(&auth_token_buffer, &auth_token_len, timestamp,
Andres Morales60343092015-04-09 19:01:10 -0700162 user_id, authenticator_id, request.challenge);
Andres Morales48a4f832015-05-29 10:13:31 -0700163
164 SizedBuffer auth_token(auth_token_len);
165 memcpy(auth_token.buffer.get(), auth_token_buffer.get(), auth_token_len);
Andres Moralesac808182015-02-26 14:11:04 -0800166 response->SetVerificationToken(&auth_token);
Andres Morales48a4f832015-05-29 10:13:31 -0700167 if (throttle) ClearFailureRecord(uid, user_id, throttle_secure);
Andres Moralesac808182015-02-26 14:11:04 -0800168 } else {
Andres Moralesaedf6052015-05-14 13:10:30 -0700169 // compute the new timeout given the incremented record
170 if (throttle && timeout > 0) {
171 response->SetRetryTimeout(timeout);
172 } else {
173 response->error = ERROR_INVALID;
174 }
Andres Moralesac808182015-02-26 14:11:04 -0800175 }
176}
177
Andres Morales7d0f0402015-03-19 18:02:55 -0700178bool GateKeeper::CreatePasswordHandle(SizedBuffer *password_handle_buffer, salt_t salt,
Andres Morales48a4f832015-05-29 10:13:31 -0700179 secure_id_t user_id, uint64_t flags, uint8_t handle_version, const uint8_t *password,
Andres Morales11ed52a2015-03-30 16:47:47 -0700180 uint32_t password_length) {
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700181 password_handle_buffer->buffer.reset(new uint8_t[sizeof(password_handle_t)]);
182 password_handle_buffer->length = sizeof(password_handle_t);
183
184 password_handle_t *password_handle = reinterpret_cast<password_handle_t *>(
185 password_handle_buffer->buffer.get());
Andres Moralesaedf6052015-05-14 13:10:30 -0700186 password_handle->version = handle_version;
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700187 password_handle->salt = salt;
188 password_handle->user_id = user_id;
Andres Morales48a4f832015-05-29 10:13:31 -0700189 password_handle->flags = flags;
Andres Moralesaedf6052015-05-14 13:10:30 -0700190 password_handle->hardware_backed = IsHardwareBacked();
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700191
Andres Morales48a4f832015-05-29 10:13:31 -0700192 uint32_t metadata_length = sizeof(user_id) + sizeof(flags) + sizeof(HANDLE_VERSION);
Alexey Polyudov84f8f9f2016-08-18 13:48:50 -0700193 const size_t to_sign_size = password_length + metadata_length;
George Burgess IVcfa8a412017-08-29 14:13:20 -0700194 UniquePtr<uint8_t[]> to_sign(new uint8_t[to_sign_size]);
Alexey Polyudov84f8f9f2016-08-18 13:48:50 -0700195
196 if (to_sign.get() == nullptr) {
197 return false;
198 }
199
200 memcpy(to_sign.get(), password_handle, metadata_length);
201 memcpy(to_sign.get() + metadata_length, password, password_length);
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700202
Andres Moralesf10e2892015-03-23 12:03:56 -0700203 const uint8_t *password_key = NULL;
Andres Morales11ed52a2015-03-30 16:47:47 -0700204 uint32_t password_key_length = 0;
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700205 GetPasswordKey(&password_key, &password_key_length);
206
Andres Moralesf10e2892015-03-23 12:03:56 -0700207 if (!password_key || password_key_length == 0) {
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700208 return false;
209 }
210
211 ComputePasswordSignature(password_handle->signature, sizeof(password_handle->signature),
Alexey Polyudov84f8f9f2016-08-18 13:48:50 -0700212 password_key, password_key_length, to_sign.get(), to_sign_size, salt);
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700213 return true;
214}
215
Andres Morales7d0f0402015-03-19 18:02:55 -0700216bool GateKeeper::DoVerify(const password_handle_t *expected_handle, const SizedBuffer &password) {
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700217 if (!password.buffer.get()) return false;
218
219 SizedBuffer provided_handle;
220 if (!CreatePasswordHandle(&provided_handle, expected_handle->salt, expected_handle->user_id,
Andres Morales48a4f832015-05-29 10:13:31 -0700221 expected_handle->flags, expected_handle->version,
Andres Moralesaedf6052015-05-14 13:10:30 -0700222 password.buffer.get(), password.length)) {
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700223 return false;
224 }
225
Andres Moralesaedf6052015-05-14 13:10:30 -0700226 password_handle_t *generated_handle =
227 reinterpret_cast<password_handle_t *>(provided_handle.buffer.get());
228 return memcmp_s(generated_handle->signature, expected_handle->signature,
229 sizeof(expected_handle->signature)) == 0;
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700230}
231
Shawn Willdenf73a0102016-09-13 17:39:27 +0000232void GateKeeper::MintAuthToken(UniquePtr<uint8_t> *auth_token, uint32_t *length,
Andres Morales652f0762015-04-13 10:23:20 -0700233 uint64_t timestamp, secure_id_t user_id, secure_id_t authenticator_id,
Andres Morales60343092015-04-09 19:01:10 -0700234 uint64_t challenge) {
Andres Moralesb2abaa82015-03-03 09:09:18 -0800235 if (auth_token == NULL) return;
236
Andres Moralesfa104e12015-04-08 12:36:44 -0700237 hw_auth_token_t *token = new hw_auth_token_t;
Andres Moralesac808182015-02-26 14:11:04 -0800238 SizedBuffer serialized_auth_token;
239
Andres Moralesfa104e12015-04-08 12:36:44 -0700240 token->version = HW_AUTH_TOKEN_VERSION;
Andres Morales60343092015-04-09 19:01:10 -0700241 token->challenge = challenge;
Andres Moralesfa104e12015-04-08 12:36:44 -0700242 token->user_id = user_id;
243 token->authenticator_id = authenticator_id;
Nick Bray27748e22018-10-31 10:39:41 -0700244 token->authenticator_type = htobe32(HW_AUTH_PASSWORD);
Andres Moralesec9fd1d2015-04-20 07:45:50 -0700245 token->timestamp = htobe64(timestamp);
Andres Moralesac808182015-02-26 14:11:04 -0800246
Andres Moralesf10e2892015-03-23 12:03:56 -0700247 const uint8_t *auth_token_key = NULL;
Andres Morales11ed52a2015-03-30 16:47:47 -0700248 uint32_t key_len = 0;
Andres Morales58bb2462015-04-10 18:27:50 -0700249 if (GetAuthTokenKey(&auth_token_key, &key_len)) {
250 uint32_t hash_len = (uint32_t)((uint8_t *)&token->hmac - (uint8_t *)token);
251 ComputeSignature(token->hmac, sizeof(token->hmac), auth_token_key, key_len,
252 reinterpret_cast<uint8_t *>(token), hash_len);
253 } else {
254 memset(token->hmac, 0, sizeof(token->hmac));
255 }
Andres Moralesac808182015-02-26 14:11:04 -0800256
Andres Moralesfa104e12015-04-08 12:36:44 -0700257 if (length != NULL) *length = sizeof(*token);
Andres Moralesb2abaa82015-03-03 09:09:18 -0800258 auth_token->reset(reinterpret_cast<uint8_t *>(token));
Andres Moralesac808182015-02-26 14:11:04 -0800259}
Andres Moralesb2abaa82015-03-03 09:09:18 -0800260
Andres Moralesf436c802015-10-08 12:21:25 -0700261/*
262 * Calculates the timeout in milliseconds as a function of the failure
263 * counter 'x' as follows:
264 *
Paul Crowley8fbcf802018-06-15 15:03:33 -0700265 * [0, 4] -> 0
Andres Moralesf436c802015-10-08 12:21:25 -0700266 * 5 -> 30
Paul Crowley8fbcf802018-06-15 15:03:33 -0700267 * [6, 10] -> 0
268 * [11, 29] -> 30
269 * [30, 139] -> 30 * (2^((x - 30)/10))
Andres Moralesf436c802015-10-08 12:21:25 -0700270 * [140, inf) -> 1 day
271 *
272 */
Andres Moralesaedf6052015-05-14 13:10:30 -0700273uint32_t GateKeeper::ComputeRetryTimeout(const failure_record_t *record) {
Andres Moralesb6a5cd72015-06-03 17:59:17 -0700274 static const int failure_timeout_ms = 30000;
275 if (record->failure_counter == 0) return 0;
276
Andres Moralesaedf6052015-05-14 13:10:30 -0700277 if (record->failure_counter > 0 && record->failure_counter <= 10) {
278 if (record->failure_counter % 5 == 0) {
Andres Moralesb6a5cd72015-06-03 17:59:17 -0700279 return failure_timeout_ms;
Andres Moralesd14f4722015-12-18 11:57:34 -0800280 } else {
281 return 0;
Andres Moralesaedf6052015-05-14 13:10:30 -0700282 }
Andres Moralesf436c802015-10-08 12:21:25 -0700283 } else if (record->failure_counter < 30) {
Andres Moralesb6a5cd72015-06-03 17:59:17 -0700284 return failure_timeout_ms;
Andres Moralesf436c802015-10-08 12:21:25 -0700285 } else if (record->failure_counter < 140) {
286 return failure_timeout_ms << ((record->failure_counter - 30) / 10);
Andres Moralesaedf6052015-05-14 13:10:30 -0700287 }
Andres Moralesf436c802015-10-08 12:21:25 -0700288
289 return DAY_IN_MS;
Andres Moralesac808182015-02-26 14:11:04 -0800290}
Andres Moralesaedf6052015-05-14 13:10:30 -0700291
Andres Moralesa623e452015-05-27 12:26:59 -0700292bool GateKeeper::ThrottleRequest(uint32_t uid, uint64_t timestamp,
Andres Morales48a4f832015-05-29 10:13:31 -0700293 failure_record_t *record, bool secure, GateKeeperMessage *response) {
Andres Moralesaedf6052015-05-14 13:10:30 -0700294
295 uint64_t last_checked = record->last_checked_timestamp;
296 uint32_t timeout = ComputeRetryTimeout(record);
297
298 if (timeout > 0) {
299 // we have a pending timeout
300 if (timestamp < last_checked + timeout && timestamp > last_checked) {
301 // attempt before timeout expired, return remaining time
302 response->SetRetryTimeout(timeout - (timestamp - last_checked));
303 return true;
304 } else if (timestamp <= last_checked) {
305 // device was rebooted or timer reset, don't count as new failure but
306 // reset timeout
307 record->last_checked_timestamp = timestamp;
Andres Morales48a4f832015-05-29 10:13:31 -0700308 if (!WriteFailureRecord(uid, record, secure)) {
Andres Moralesaedf6052015-05-14 13:10:30 -0700309 response->error = ERROR_UNKNOWN;
310 return true;
311 }
312 response->SetRetryTimeout(timeout);
313 return true;
314 }
315 }
316
317 return false;
318}
319
320bool GateKeeper::IncrementFailureRecord(uint32_t uid, secure_id_t user_id, uint64_t timestamp,
Andres Morales48a4f832015-05-29 10:13:31 -0700321 failure_record_t *record, bool secure) {
Andres Moralesaedf6052015-05-14 13:10:30 -0700322 record->secure_user_id = user_id;
323 record->failure_counter++;
324 record->last_checked_timestamp = timestamp;
325
Andres Morales48a4f832015-05-29 10:13:31 -0700326 return WriteFailureRecord(uid, record, secure);
Andres Moralesaedf6052015-05-14 13:10:30 -0700327}
328} // namespace gatekeeper
329