blob: fe427bd0a1a7c12d8b13e5453258d3959bf2f4e5 [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 Park002c8662017-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 {
Andres Morales426fcfb2015-04-01 13:33:45 -070040 password_handle_t *pw_handle =
41 reinterpret_cast<password_handle_t *>(request.password_handle.buffer.get());
Andres Moralesaedf6052015-05-14 13:10:30 -070042
Andres Morales48a4f832015-05-29 10:13:31 -070043 if (pw_handle->version > HANDLE_VERSION) {
Andres Morales426fcfb2015-04-01 13:33:45 -070044 response->error = ERROR_INVALID;
45 return;
Andres Moralesedd3e3d2015-03-12 13:30:15 -070046 }
Andres Morales426fcfb2015-04-01 13:33:45 -070047
Andres Morales48a4f832015-05-29 10:13:31 -070048 user_id = pw_handle->user_id;
49
50 uint64_t timestamp = GetMillisecondsSinceBoot();
51
Andres Moralesaedf6052015-05-14 13:10:30 -070052 uint32_t timeout = 0;
Andres Morales48a4f832015-05-29 10:13:31 -070053 bool throttle = (pw_handle->version >= HANDLE_VERSION_THROTTLE);
Andres Moralesaedf6052015-05-14 13:10:30 -070054 if (throttle) {
Andres Morales48a4f832015-05-29 10:13:31 -070055 bool throttle_secure = pw_handle->flags & HANDLE_FLAG_THROTTLE_SECURE;
Andres Moralesaedf6052015-05-14 13:10:30 -070056 failure_record_t record;
Andres Morales48a4f832015-05-29 10:13:31 -070057 if (!GetFailureRecord(uid, user_id, &record, throttle_secure)) {
Andres Moralesaedf6052015-05-14 13:10:30 -070058 response->error = ERROR_UNKNOWN;
59 return;
60 }
61
Andres Morales48a4f832015-05-29 10:13:31 -070062 if (ThrottleRequest(uid, timestamp, &record, throttle_secure, response)) return;
Andres Moralesaedf6052015-05-14 13:10:30 -070063
Andres Morales48a4f832015-05-29 10:13:31 -070064 if (!IncrementFailureRecord(uid, user_id, timestamp, &record, throttle_secure)) {
Andres Moralesaedf6052015-05-14 13:10:30 -070065 response->error = ERROR_UNKNOWN;
66 return;
67 }
68
69 timeout = ComputeRetryTimeout(&record);
70 }
71
72 if (!DoVerify(pw_handle, request.enrolled_password)) {
73 // incorrect old password
74 if (throttle && timeout > 0) {
75 response->SetRetryTimeout(timeout);
76 } else {
77 response->error = ERROR_INVALID;
78 }
79 return;
80 }
Andres Moralesedd3e3d2015-03-12 13:30:15 -070081 }
82
Andres Morales48a4f832015-05-29 10:13:31 -070083 uint64_t flags = 0;
84 if (ClearFailureRecord(uid, user_id, true)) {
85 flags |= HANDLE_FLAG_THROTTLE_SECURE;
86 } else {
87 ClearFailureRecord(uid, user_id, false);
88 }
Andres Moralesaedf6052015-05-14 13:10:30 -070089
Andres Moralesedd3e3d2015-03-12 13:30:15 -070090 salt_t salt;
91 GetRandom(&salt, sizeof(salt));
92
Andres Moralesedd3e3d2015-03-12 13:30:15 -070093 SizedBuffer password_handle;
Andres Moralesaedf6052015-05-14 13:10:30 -070094 if (!CreatePasswordHandle(&password_handle,
Andres Morales48a4f832015-05-29 10:13:31 -070095 salt, user_id, flags, HANDLE_VERSION, request.provided_password.buffer.get(),
Andres Moralesedd3e3d2015-03-12 13:30:15 -070096 request.provided_password.length)) {
Andres Morales7d0f0402015-03-19 18:02:55 -070097 response->error = ERROR_INVALID;
Andres Moralesedd3e3d2015-03-12 13:30:15 -070098 return;
99 }
100
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700101 response->SetEnrolledPasswordHandle(&password_handle);
Andres Moralesac808182015-02-26 14:11:04 -0800102}
103
Andres Morales7d0f0402015-03-19 18:02:55 -0700104void GateKeeper::Verify(const VerifyRequest &request, VerifyResponse *response) {
Andres Moralesac808182015-02-26 14:11:04 -0800105 if (response == NULL) return;
106
Andres Moralesb2abaa82015-03-03 09:09:18 -0800107 if (!request.provided_password.buffer.get() || !request.password_handle.buffer.get()) {
Andres Morales7d0f0402015-03-19 18:02:55 -0700108 response->error = ERROR_INVALID;
Andres Moralesac808182015-02-26 14:11:04 -0800109 return;
110 }
111
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700112 password_handle_t *password_handle = reinterpret_cast<password_handle_t *>(
113 request.password_handle.buffer.get());
Andres Moralesb2abaa82015-03-03 09:09:18 -0800114
Andres Morales48a4f832015-05-29 10:13:31 -0700115 if (password_handle->version > HANDLE_VERSION) {
Andres Morales7d0f0402015-03-19 18:02:55 -0700116 response->error = ERROR_INVALID;
Andres Moralesb2abaa82015-03-03 09:09:18 -0800117 return;
118 }
119
Andres Morales426fcfb2015-04-01 13:33:45 -0700120 secure_id_t user_id = password_handle->user_id;
Andres Morales48a4f832015-05-29 10:13:31 -0700121 secure_id_t authenticator_id = 0;
Andres Moralesaedf6052015-05-14 13:10:30 -0700122 uint32_t uid = request.user_id;
Andres Moralesb2abaa82015-03-03 09:09:18 -0800123
Andres Moralesec9fd1d2015-04-20 07:45:50 -0700124 uint64_t timestamp = GetMillisecondsSinceBoot();
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700125
Andres Moralesaedf6052015-05-14 13:10:30 -0700126 uint32_t timeout = 0;
Andres Morales48a4f832015-05-29 10:13:31 -0700127 bool throttle = (password_handle->version >= HANDLE_VERSION_THROTTLE);
128 bool throttle_secure = password_handle->flags & HANDLE_FLAG_THROTTLE_SECURE;
Andres Moralesaedf6052015-05-14 13:10:30 -0700129 if (throttle) {
130 failure_record_t record;
Andres Morales48a4f832015-05-29 10:13:31 -0700131 if (!GetFailureRecord(uid, user_id, &record, throttle_secure)) {
Andres Moralesaedf6052015-05-14 13:10:30 -0700132 response->error = ERROR_UNKNOWN;
133 return;
134 }
135
Andres Morales48a4f832015-05-29 10:13:31 -0700136 if (ThrottleRequest(uid, timestamp, &record, throttle_secure, response)) return;
Andres Moralesaedf6052015-05-14 13:10:30 -0700137
Andres Morales48a4f832015-05-29 10:13:31 -0700138 if (!IncrementFailureRecord(uid, user_id, timestamp, &record, throttle_secure)) {
Andres Moralesaedf6052015-05-14 13:10:30 -0700139 response->error = ERROR_UNKNOWN;
140 return;
141 }
142
143 timeout = ComputeRetryTimeout(&record);
Andres Morales893fa7f2015-06-02 19:00:03 -0700144 } else {
145 response->request_reenroll = true;
Andres Moralesaedf6052015-05-14 13:10:30 -0700146 }
147
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700148 if (DoVerify(password_handle, request.provided_password)) {
Andres Moralesac808182015-02-26 14:11:04 -0800149 // Signature matches
Shawn Willdenf73a0102016-09-13 17:39:27 +0000150 UniquePtr<uint8_t> auth_token_buffer;
Andres Morales48a4f832015-05-29 10:13:31 -0700151 uint32_t auth_token_len;
152 MintAuthToken(&auth_token_buffer, &auth_token_len, timestamp,
Andres Morales60343092015-04-09 19:01:10 -0700153 user_id, authenticator_id, request.challenge);
Andres Morales48a4f832015-05-29 10:13:31 -0700154
155 SizedBuffer auth_token(auth_token_len);
156 memcpy(auth_token.buffer.get(), auth_token_buffer.get(), auth_token_len);
Andres Moralesac808182015-02-26 14:11:04 -0800157 response->SetVerificationToken(&auth_token);
Andres Morales48a4f832015-05-29 10:13:31 -0700158 if (throttle) ClearFailureRecord(uid, user_id, throttle_secure);
Andres Moralesac808182015-02-26 14:11:04 -0800159 } else {
Andres Moralesaedf6052015-05-14 13:10:30 -0700160 // compute the new timeout given the incremented record
161 if (throttle && timeout > 0) {
162 response->SetRetryTimeout(timeout);
163 } else {
164 response->error = ERROR_INVALID;
165 }
Andres Moralesac808182015-02-26 14:11:04 -0800166 }
167}
168
Andres Morales7d0f0402015-03-19 18:02:55 -0700169bool GateKeeper::CreatePasswordHandle(SizedBuffer *password_handle_buffer, salt_t salt,
Andres Morales48a4f832015-05-29 10:13:31 -0700170 secure_id_t user_id, uint64_t flags, uint8_t handle_version, const uint8_t *password,
Andres Morales11ed52a2015-03-30 16:47:47 -0700171 uint32_t password_length) {
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700172 password_handle_buffer->buffer.reset(new uint8_t[sizeof(password_handle_t)]);
173 password_handle_buffer->length = sizeof(password_handle_t);
174
175 password_handle_t *password_handle = reinterpret_cast<password_handle_t *>(
176 password_handle_buffer->buffer.get());
Andres Moralesaedf6052015-05-14 13:10:30 -0700177 password_handle->version = handle_version;
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700178 password_handle->salt = salt;
179 password_handle->user_id = user_id;
Andres Morales48a4f832015-05-29 10:13:31 -0700180 password_handle->flags = flags;
Andres Moralesaedf6052015-05-14 13:10:30 -0700181 password_handle->hardware_backed = IsHardwareBacked();
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700182
Andres Morales48a4f832015-05-29 10:13:31 -0700183 uint32_t metadata_length = sizeof(user_id) + sizeof(flags) + sizeof(HANDLE_VERSION);
Alexey Polyudov84f8f9f2016-08-18 13:48:50 -0700184 const size_t to_sign_size = password_length + metadata_length;
George Burgess IVcfa8a412017-08-29 14:13:20 -0700185 UniquePtr<uint8_t[]> to_sign(new uint8_t[to_sign_size]);
Alexey Polyudov84f8f9f2016-08-18 13:48:50 -0700186
187 if (to_sign.get() == nullptr) {
188 return false;
189 }
190
191 memcpy(to_sign.get(), password_handle, metadata_length);
192 memcpy(to_sign.get() + metadata_length, password, password_length);
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700193
Andres Moralesf10e2892015-03-23 12:03:56 -0700194 const uint8_t *password_key = NULL;
Andres Morales11ed52a2015-03-30 16:47:47 -0700195 uint32_t password_key_length = 0;
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700196 GetPasswordKey(&password_key, &password_key_length);
197
Andres Moralesf10e2892015-03-23 12:03:56 -0700198 if (!password_key || password_key_length == 0) {
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700199 return false;
200 }
201
202 ComputePasswordSignature(password_handle->signature, sizeof(password_handle->signature),
Alexey Polyudov84f8f9f2016-08-18 13:48:50 -0700203 password_key, password_key_length, to_sign.get(), to_sign_size, salt);
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700204 return true;
205}
206
Andres Morales7d0f0402015-03-19 18:02:55 -0700207bool GateKeeper::DoVerify(const password_handle_t *expected_handle, const SizedBuffer &password) {
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700208 if (!password.buffer.get()) return false;
209
210 SizedBuffer provided_handle;
211 if (!CreatePasswordHandle(&provided_handle, expected_handle->salt, expected_handle->user_id,
Andres Morales48a4f832015-05-29 10:13:31 -0700212 expected_handle->flags, expected_handle->version,
Andres Moralesaedf6052015-05-14 13:10:30 -0700213 password.buffer.get(), password.length)) {
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700214 return false;
215 }
216
Andres Moralesaedf6052015-05-14 13:10:30 -0700217 password_handle_t *generated_handle =
218 reinterpret_cast<password_handle_t *>(provided_handle.buffer.get());
219 return memcmp_s(generated_handle->signature, expected_handle->signature,
220 sizeof(expected_handle->signature)) == 0;
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700221}
222
Shawn Willdenf73a0102016-09-13 17:39:27 +0000223void GateKeeper::MintAuthToken(UniquePtr<uint8_t> *auth_token, uint32_t *length,
Andres Morales652f0762015-04-13 10:23:20 -0700224 uint64_t timestamp, secure_id_t user_id, secure_id_t authenticator_id,
Andres Morales60343092015-04-09 19:01:10 -0700225 uint64_t challenge) {
Andres Moralesb2abaa82015-03-03 09:09:18 -0800226 if (auth_token == NULL) return;
227
Andres Moralesfa104e12015-04-08 12:36:44 -0700228 hw_auth_token_t *token = new hw_auth_token_t;
Andres Moralesac808182015-02-26 14:11:04 -0800229 SizedBuffer serialized_auth_token;
230
Andres Moralesfa104e12015-04-08 12:36:44 -0700231 token->version = HW_AUTH_TOKEN_VERSION;
Andres Morales60343092015-04-09 19:01:10 -0700232 token->challenge = challenge;
Andres Moralesfa104e12015-04-08 12:36:44 -0700233 token->user_id = user_id;
234 token->authenticator_id = authenticator_id;
235 token->authenticator_type = htonl(HW_AUTH_PASSWORD);
Andres Moralesec9fd1d2015-04-20 07:45:50 -0700236 token->timestamp = htobe64(timestamp);
Andres Moralesac808182015-02-26 14:11:04 -0800237
Andres Moralesf10e2892015-03-23 12:03:56 -0700238 const uint8_t *auth_token_key = NULL;
Andres Morales11ed52a2015-03-30 16:47:47 -0700239 uint32_t key_len = 0;
Andres Morales58bb2462015-04-10 18:27:50 -0700240 if (GetAuthTokenKey(&auth_token_key, &key_len)) {
241 uint32_t hash_len = (uint32_t)((uint8_t *)&token->hmac - (uint8_t *)token);
242 ComputeSignature(token->hmac, sizeof(token->hmac), auth_token_key, key_len,
243 reinterpret_cast<uint8_t *>(token), hash_len);
244 } else {
245 memset(token->hmac, 0, sizeof(token->hmac));
246 }
Andres Moralesac808182015-02-26 14:11:04 -0800247
Andres Moralesfa104e12015-04-08 12:36:44 -0700248 if (length != NULL) *length = sizeof(*token);
Andres Moralesb2abaa82015-03-03 09:09:18 -0800249 auth_token->reset(reinterpret_cast<uint8_t *>(token));
Andres Moralesac808182015-02-26 14:11:04 -0800250}
Andres Moralesb2abaa82015-03-03 09:09:18 -0800251
Andres Moralesf436c802015-10-08 12:21:25 -0700252/*
253 * Calculates the timeout in milliseconds as a function of the failure
254 * counter 'x' as follows:
255 *
256 * [0. 5) -> 0
257 * 5 -> 30
258 * [6, 10) -> 0
259 * [11, 30) -> 30
260 * [30, 140) -> 30 * (2^((x - 30)/10))
261 * [140, inf) -> 1 day
262 *
263 */
Andres Moralesaedf6052015-05-14 13:10:30 -0700264uint32_t GateKeeper::ComputeRetryTimeout(const failure_record_t *record) {
Andres Moralesb6a5cd72015-06-03 17:59:17 -0700265 static const int failure_timeout_ms = 30000;
266 if (record->failure_counter == 0) return 0;
267
Andres Moralesaedf6052015-05-14 13:10:30 -0700268 if (record->failure_counter > 0 && record->failure_counter <= 10) {
269 if (record->failure_counter % 5 == 0) {
Andres Moralesb6a5cd72015-06-03 17:59:17 -0700270 return failure_timeout_ms;
Andres Moralesd14f4722015-12-18 11:57:34 -0800271 } else {
272 return 0;
Andres Moralesaedf6052015-05-14 13:10:30 -0700273 }
Andres Moralesf436c802015-10-08 12:21:25 -0700274 } else if (record->failure_counter < 30) {
Andres Moralesb6a5cd72015-06-03 17:59:17 -0700275 return failure_timeout_ms;
Andres Moralesf436c802015-10-08 12:21:25 -0700276 } else if (record->failure_counter < 140) {
277 return failure_timeout_ms << ((record->failure_counter - 30) / 10);
Andres Moralesaedf6052015-05-14 13:10:30 -0700278 }
Andres Moralesf436c802015-10-08 12:21:25 -0700279
280 return DAY_IN_MS;
Andres Moralesac808182015-02-26 14:11:04 -0800281}
Andres Moralesaedf6052015-05-14 13:10:30 -0700282
Andres Moralesa623e452015-05-27 12:26:59 -0700283bool GateKeeper::ThrottleRequest(uint32_t uid, uint64_t timestamp,
Andres Morales48a4f832015-05-29 10:13:31 -0700284 failure_record_t *record, bool secure, GateKeeperMessage *response) {
Andres Moralesaedf6052015-05-14 13:10:30 -0700285
286 uint64_t last_checked = record->last_checked_timestamp;
287 uint32_t timeout = ComputeRetryTimeout(record);
288
289 if (timeout > 0) {
290 // we have a pending timeout
291 if (timestamp < last_checked + timeout && timestamp > last_checked) {
292 // attempt before timeout expired, return remaining time
293 response->SetRetryTimeout(timeout - (timestamp - last_checked));
294 return true;
295 } else if (timestamp <= last_checked) {
296 // device was rebooted or timer reset, don't count as new failure but
297 // reset timeout
298 record->last_checked_timestamp = timestamp;
Andres Morales48a4f832015-05-29 10:13:31 -0700299 if (!WriteFailureRecord(uid, record, secure)) {
Andres Moralesaedf6052015-05-14 13:10:30 -0700300 response->error = ERROR_UNKNOWN;
301 return true;
302 }
303 response->SetRetryTimeout(timeout);
304 return true;
305 }
306 }
307
308 return false;
309}
310
311bool GateKeeper::IncrementFailureRecord(uint32_t uid, secure_id_t user_id, uint64_t timestamp,
Andres Morales48a4f832015-05-29 10:13:31 -0700312 failure_record_t *record, bool secure) {
Andres Moralesaedf6052015-05-14 13:10:30 -0700313 record->secure_user_id = user_id;
314 record->failure_counter++;
315 record->last_checked_timestamp = timestamp;
316
Andres Morales48a4f832015-05-29 10:13:31 -0700317 return WriteFailureRecord(uid, record, secure);
Andres Moralesaedf6052015-05-14 13:10:30 -0700318}
319} // namespace gatekeeper
320