blob: 9724ed75a1b9a05f1ceee7fbb8dfdcf2c11f37da [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 */
Andres Morales7d0f0402015-03-19 18:02:55 -070016#include <gatekeeper/gatekeeper.h>
Andres Moralesac808182015-02-26 14:11:04 -080017
Andres Moralesec9fd1d2015-04-20 07:45:50 -070018#include <endian.h>
19
Elliott Hughes3b6f0be2016-09-11 14:58:04 -070020#include <memory>
21
Andres Moralesf436c802015-10-08 12:21:25 -070022#define DAY_IN_MS (1000 * 60 * 60 * 24)
23
Andres Morales7d0f0402015-03-19 18:02:55 -070024namespace gatekeeper {
Andres Moralesac808182015-02-26 14:11:04 -080025
Andres Morales7d0f0402015-03-19 18:02:55 -070026void GateKeeper::Enroll(const EnrollRequest &request, EnrollResponse *response) {
Andres Moralesac808182015-02-26 14:11:04 -080027 if (response == NULL) return;
28
Andres Moralesb2abaa82015-03-03 09:09:18 -080029 if (!request.provided_password.buffer.get()) {
Andres Morales7d0f0402015-03-19 18:02:55 -070030 response->error = ERROR_INVALID;
Andres Moralesac808182015-02-26 14:11:04 -080031 return;
32 }
Andres Moralesb2abaa82015-03-03 09:09:18 -080033
Andres Moralesaedf6052015-05-14 13:10:30 -070034 secure_id_t user_id = 0;// todo: rename to policy
35 uint32_t uid = request.user_id;
Andres Moralesb2abaa82015-03-03 09:09:18 -080036
Andres Moralesedd3e3d2015-03-12 13:30:15 -070037 if (request.password_handle.buffer.get() == NULL) {
38 // Password handle does not match what is stored, generate new SecureID
39 GetRandom(&user_id, sizeof(secure_id_t));
40 } else {
Andres Morales426fcfb2015-04-01 13:33:45 -070041 password_handle_t *pw_handle =
42 reinterpret_cast<password_handle_t *>(request.password_handle.buffer.get());
Andres Moralesaedf6052015-05-14 13:10:30 -070043
Andres Morales48a4f832015-05-29 10:13:31 -070044 if (pw_handle->version > HANDLE_VERSION) {
Andres Morales426fcfb2015-04-01 13:33:45 -070045 response->error = ERROR_INVALID;
46 return;
Andres Moralesedd3e3d2015-03-12 13:30:15 -070047 }
Andres Morales426fcfb2015-04-01 13:33:45 -070048
Andres Morales48a4f832015-05-29 10:13:31 -070049 user_id = pw_handle->user_id;
50
51 uint64_t timestamp = GetMillisecondsSinceBoot();
52
Andres Moralesaedf6052015-05-14 13:10:30 -070053 uint32_t timeout = 0;
Andres Morales48a4f832015-05-29 10:13:31 -070054 bool throttle = (pw_handle->version >= HANDLE_VERSION_THROTTLE);
Andres Moralesaedf6052015-05-14 13:10:30 -070055 if (throttle) {
Andres Morales48a4f832015-05-29 10:13:31 -070056 bool throttle_secure = pw_handle->flags & HANDLE_FLAG_THROTTLE_SECURE;
Andres Moralesaedf6052015-05-14 13:10:30 -070057 failure_record_t record;
Andres Morales48a4f832015-05-29 10:13:31 -070058 if (!GetFailureRecord(uid, user_id, &record, throttle_secure)) {
Andres Moralesaedf6052015-05-14 13:10:30 -070059 response->error = ERROR_UNKNOWN;
60 return;
61 }
62
Andres Morales48a4f832015-05-29 10:13:31 -070063 if (ThrottleRequest(uid, timestamp, &record, throttle_secure, response)) return;
Andres Moralesaedf6052015-05-14 13:10:30 -070064
Andres Morales48a4f832015-05-29 10:13:31 -070065 if (!IncrementFailureRecord(uid, user_id, timestamp, &record, throttle_secure)) {
Andres Moralesaedf6052015-05-14 13:10:30 -070066 response->error = ERROR_UNKNOWN;
67 return;
68 }
69
70 timeout = ComputeRetryTimeout(&record);
71 }
72
73 if (!DoVerify(pw_handle, request.enrolled_password)) {
74 // incorrect old password
75 if (throttle && timeout > 0) {
76 response->SetRetryTimeout(timeout);
77 } else {
78 response->error = ERROR_INVALID;
79 }
80 return;
81 }
Andres Moralesedd3e3d2015-03-12 13:30:15 -070082 }
83
Andres Morales48a4f832015-05-29 10:13:31 -070084 uint64_t flags = 0;
85 if (ClearFailureRecord(uid, user_id, true)) {
86 flags |= HANDLE_FLAG_THROTTLE_SECURE;
87 } else {
88 ClearFailureRecord(uid, user_id, false);
89 }
Andres Moralesaedf6052015-05-14 13:10:30 -070090
Andres Moralesedd3e3d2015-03-12 13:30:15 -070091 salt_t salt;
92 GetRandom(&salt, sizeof(salt));
93
Andres Moralesedd3e3d2015-03-12 13:30:15 -070094 SizedBuffer password_handle;
Andres Moralesaedf6052015-05-14 13:10:30 -070095 if (!CreatePasswordHandle(&password_handle,
Andres Morales48a4f832015-05-29 10:13:31 -070096 salt, user_id, flags, HANDLE_VERSION, request.provided_password.buffer.get(),
Andres Moralesedd3e3d2015-03-12 13:30:15 -070097 request.provided_password.length)) {
Andres Morales7d0f0402015-03-19 18:02:55 -070098 response->error = ERROR_INVALID;
Andres Moralesedd3e3d2015-03-12 13:30:15 -070099 return;
100 }
101
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700102 response->SetEnrolledPasswordHandle(&password_handle);
Andres Moralesac808182015-02-26 14:11:04 -0800103}
104
Andres Morales7d0f0402015-03-19 18:02:55 -0700105void GateKeeper::Verify(const VerifyRequest &request, VerifyResponse *response) {
Andres Moralesac808182015-02-26 14:11:04 -0800106 if (response == NULL) return;
107
Andres Moralesb2abaa82015-03-03 09:09:18 -0800108 if (!request.provided_password.buffer.get() || !request.password_handle.buffer.get()) {
Andres Morales7d0f0402015-03-19 18:02:55 -0700109 response->error = ERROR_INVALID;
Andres Moralesac808182015-02-26 14:11:04 -0800110 return;
111 }
112
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700113 password_handle_t *password_handle = reinterpret_cast<password_handle_t *>(
114 request.password_handle.buffer.get());
Andres Moralesb2abaa82015-03-03 09:09:18 -0800115
Andres Morales48a4f832015-05-29 10:13:31 -0700116 if (password_handle->version > HANDLE_VERSION) {
Andres Morales7d0f0402015-03-19 18:02:55 -0700117 response->error = ERROR_INVALID;
Andres Moralesb2abaa82015-03-03 09:09:18 -0800118 return;
119 }
120
Andres Morales426fcfb2015-04-01 13:33:45 -0700121 secure_id_t user_id = password_handle->user_id;
Andres Morales48a4f832015-05-29 10:13:31 -0700122 secure_id_t authenticator_id = 0;
Andres Moralesaedf6052015-05-14 13:10:30 -0700123 uint32_t uid = request.user_id;
Andres Moralesb2abaa82015-03-03 09:09:18 -0800124
Andres Moralesec9fd1d2015-04-20 07:45:50 -0700125 uint64_t timestamp = GetMillisecondsSinceBoot();
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700126
Andres Moralesaedf6052015-05-14 13:10:30 -0700127 uint32_t timeout = 0;
Andres Morales48a4f832015-05-29 10:13:31 -0700128 bool throttle = (password_handle->version >= HANDLE_VERSION_THROTTLE);
129 bool throttle_secure = password_handle->flags & HANDLE_FLAG_THROTTLE_SECURE;
Andres Moralesaedf6052015-05-14 13:10:30 -0700130 if (throttle) {
131 failure_record_t record;
Andres Morales48a4f832015-05-29 10:13:31 -0700132 if (!GetFailureRecord(uid, user_id, &record, throttle_secure)) {
Andres Moralesaedf6052015-05-14 13:10:30 -0700133 response->error = ERROR_UNKNOWN;
134 return;
135 }
136
Andres Morales48a4f832015-05-29 10:13:31 -0700137 if (ThrottleRequest(uid, timestamp, &record, throttle_secure, response)) return;
Andres Moralesaedf6052015-05-14 13:10:30 -0700138
Andres Morales48a4f832015-05-29 10:13:31 -0700139 if (!IncrementFailureRecord(uid, user_id, timestamp, &record, throttle_secure)) {
Andres Moralesaedf6052015-05-14 13:10:30 -0700140 response->error = ERROR_UNKNOWN;
141 return;
142 }
143
144 timeout = ComputeRetryTimeout(&record);
Andres Morales893fa7f2015-06-02 19:00:03 -0700145 } else {
146 response->request_reenroll = true;
Andres Moralesaedf6052015-05-14 13:10:30 -0700147 }
148
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700149 if (DoVerify(password_handle, request.provided_password)) {
Andres Moralesac808182015-02-26 14:11:04 -0800150 // Signature matches
Elliott Hughes3b6f0be2016-09-11 14:58:04 -0700151 std::unique_ptr<uint8_t> auth_token_buffer;
Andres Morales48a4f832015-05-29 10:13:31 -0700152 uint32_t auth_token_len;
153 MintAuthToken(&auth_token_buffer, &auth_token_len, timestamp,
Andres Morales60343092015-04-09 19:01:10 -0700154 user_id, authenticator_id, request.challenge);
Andres Morales48a4f832015-05-29 10:13:31 -0700155
156 SizedBuffer auth_token(auth_token_len);
157 memcpy(auth_token.buffer.get(), auth_token_buffer.get(), auth_token_len);
Andres Moralesac808182015-02-26 14:11:04 -0800158 response->SetVerificationToken(&auth_token);
Andres Morales48a4f832015-05-29 10:13:31 -0700159 if (throttle) ClearFailureRecord(uid, user_id, throttle_secure);
Andres Moralesac808182015-02-26 14:11:04 -0800160 } else {
Andres Moralesaedf6052015-05-14 13:10:30 -0700161 // compute the new timeout given the incremented record
162 if (throttle && timeout > 0) {
163 response->SetRetryTimeout(timeout);
164 } else {
165 response->error = ERROR_INVALID;
166 }
Andres Moralesac808182015-02-26 14:11:04 -0800167 }
168}
169
Andres Morales7d0f0402015-03-19 18:02:55 -0700170bool GateKeeper::CreatePasswordHandle(SizedBuffer *password_handle_buffer, salt_t salt,
Andres Morales48a4f832015-05-29 10:13:31 -0700171 secure_id_t user_id, uint64_t flags, uint8_t handle_version, const uint8_t *password,
Andres Morales11ed52a2015-03-30 16:47:47 -0700172 uint32_t password_length) {
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700173 password_handle_buffer->buffer.reset(new uint8_t[sizeof(password_handle_t)]);
174 password_handle_buffer->length = sizeof(password_handle_t);
175
176 password_handle_t *password_handle = reinterpret_cast<password_handle_t *>(
177 password_handle_buffer->buffer.get());
Andres Moralesaedf6052015-05-14 13:10:30 -0700178 password_handle->version = handle_version;
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700179 password_handle->salt = salt;
180 password_handle->user_id = user_id;
Andres Morales48a4f832015-05-29 10:13:31 -0700181 password_handle->flags = flags;
Andres Moralesaedf6052015-05-14 13:10:30 -0700182 password_handle->hardware_backed = IsHardwareBacked();
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700183
Andres Morales48a4f832015-05-29 10:13:31 -0700184 uint32_t metadata_length = sizeof(user_id) + sizeof(flags) + sizeof(HANDLE_VERSION);
Alexey Polyudov84f8f9f2016-08-18 13:48:50 -0700185 const size_t to_sign_size = password_length + metadata_length;
Elliott Hughes3b6f0be2016-09-11 14:58:04 -0700186 std::unique_ptr<uint8_t> to_sign(new uint8_t[to_sign_size]);
Alexey Polyudov84f8f9f2016-08-18 13:48:50 -0700187
188 if (to_sign.get() == nullptr) {
189 return false;
190 }
191
192 memcpy(to_sign.get(), password_handle, metadata_length);
193 memcpy(to_sign.get() + metadata_length, password, password_length);
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700194
Andres Moralesf10e2892015-03-23 12:03:56 -0700195 const uint8_t *password_key = NULL;
Andres Morales11ed52a2015-03-30 16:47:47 -0700196 uint32_t password_key_length = 0;
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700197 GetPasswordKey(&password_key, &password_key_length);
198
Andres Moralesf10e2892015-03-23 12:03:56 -0700199 if (!password_key || password_key_length == 0) {
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700200 return false;
201 }
202
203 ComputePasswordSignature(password_handle->signature, sizeof(password_handle->signature),
Alexey Polyudov84f8f9f2016-08-18 13:48:50 -0700204 password_key, password_key_length, to_sign.get(), to_sign_size, salt);
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700205 return true;
206}
207
Andres Morales7d0f0402015-03-19 18:02:55 -0700208bool GateKeeper::DoVerify(const password_handle_t *expected_handle, const SizedBuffer &password) {
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700209 if (!password.buffer.get()) return false;
210
211 SizedBuffer provided_handle;
212 if (!CreatePasswordHandle(&provided_handle, expected_handle->salt, expected_handle->user_id,
Andres Morales48a4f832015-05-29 10:13:31 -0700213 expected_handle->flags, expected_handle->version,
Andres Moralesaedf6052015-05-14 13:10:30 -0700214 password.buffer.get(), password.length)) {
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700215 return false;
216 }
217
Andres Moralesaedf6052015-05-14 13:10:30 -0700218 password_handle_t *generated_handle =
219 reinterpret_cast<password_handle_t *>(provided_handle.buffer.get());
220 return memcmp_s(generated_handle->signature, expected_handle->signature,
221 sizeof(expected_handle->signature)) == 0;
Andres Moralesedd3e3d2015-03-12 13:30:15 -0700222}
223
Elliott Hughes3b6f0be2016-09-11 14:58:04 -0700224void GateKeeper::MintAuthToken(std::unique_ptr<uint8_t> *auth_token, uint32_t *length,
Andres Morales652f0762015-04-13 10:23:20 -0700225 uint64_t timestamp, secure_id_t user_id, secure_id_t authenticator_id,
Andres Morales60343092015-04-09 19:01:10 -0700226 uint64_t challenge) {
Andres Moralesb2abaa82015-03-03 09:09:18 -0800227 if (auth_token == NULL) return;
228
Andres Moralesfa104e12015-04-08 12:36:44 -0700229 hw_auth_token_t *token = new hw_auth_token_t;
Andres Moralesac808182015-02-26 14:11:04 -0800230 SizedBuffer serialized_auth_token;
231
Andres Moralesfa104e12015-04-08 12:36:44 -0700232 token->version = HW_AUTH_TOKEN_VERSION;
Andres Morales60343092015-04-09 19:01:10 -0700233 token->challenge = challenge;
Andres Moralesfa104e12015-04-08 12:36:44 -0700234 token->user_id = user_id;
235 token->authenticator_id = authenticator_id;
236 token->authenticator_type = htonl(HW_AUTH_PASSWORD);
Andres Moralesec9fd1d2015-04-20 07:45:50 -0700237 token->timestamp = htobe64(timestamp);
Andres Moralesac808182015-02-26 14:11:04 -0800238
Andres Moralesf10e2892015-03-23 12:03:56 -0700239 const uint8_t *auth_token_key = NULL;
Andres Morales11ed52a2015-03-30 16:47:47 -0700240 uint32_t key_len = 0;
Andres Morales58bb2462015-04-10 18:27:50 -0700241 if (GetAuthTokenKey(&auth_token_key, &key_len)) {
242 uint32_t hash_len = (uint32_t)((uint8_t *)&token->hmac - (uint8_t *)token);
243 ComputeSignature(token->hmac, sizeof(token->hmac), auth_token_key, key_len,
244 reinterpret_cast<uint8_t *>(token), hash_len);
Andres Morales48a4f832015-05-29 10:13:31 -0700245 delete[] auth_token_key;
Andres Morales58bb2462015-04-10 18:27:50 -0700246 } else {
247 memset(token->hmac, 0, sizeof(token->hmac));
248 }
Andres Moralesac808182015-02-26 14:11:04 -0800249
Andres Moralesfa104e12015-04-08 12:36:44 -0700250 if (length != NULL) *length = sizeof(*token);
Andres Moralesb2abaa82015-03-03 09:09:18 -0800251 auth_token->reset(reinterpret_cast<uint8_t *>(token));
Andres Moralesac808182015-02-26 14:11:04 -0800252}
Andres Moralesb2abaa82015-03-03 09:09:18 -0800253
Andres Moralesf436c802015-10-08 12:21:25 -0700254/*
255 * Calculates the timeout in milliseconds as a function of the failure
256 * counter 'x' as follows:
257 *
258 * [0. 5) -> 0
259 * 5 -> 30
260 * [6, 10) -> 0
261 * [11, 30) -> 30
262 * [30, 140) -> 30 * (2^((x - 30)/10))
263 * [140, inf) -> 1 day
264 *
265 */
Andres Moralesaedf6052015-05-14 13:10:30 -0700266uint32_t GateKeeper::ComputeRetryTimeout(const failure_record_t *record) {
Andres Moralesb6a5cd72015-06-03 17:59:17 -0700267 static const int failure_timeout_ms = 30000;
268 if (record->failure_counter == 0) return 0;
269
Andres Moralesaedf6052015-05-14 13:10:30 -0700270 if (record->failure_counter > 0 && record->failure_counter <= 10) {
271 if (record->failure_counter % 5 == 0) {
Andres Moralesb6a5cd72015-06-03 17:59:17 -0700272 return failure_timeout_ms;
Andres Moralesd14f4722015-12-18 11:57:34 -0800273 } else {
274 return 0;
Andres Moralesaedf6052015-05-14 13:10:30 -0700275 }
Andres Moralesf436c802015-10-08 12:21:25 -0700276 } else if (record->failure_counter < 30) {
Andres Moralesb6a5cd72015-06-03 17:59:17 -0700277 return failure_timeout_ms;
Andres Moralesf436c802015-10-08 12:21:25 -0700278 } else if (record->failure_counter < 140) {
279 return failure_timeout_ms << ((record->failure_counter - 30) / 10);
Andres Moralesaedf6052015-05-14 13:10:30 -0700280 }
Andres Moralesf436c802015-10-08 12:21:25 -0700281
282 return DAY_IN_MS;
Andres Moralesac808182015-02-26 14:11:04 -0800283}
Andres Moralesaedf6052015-05-14 13:10:30 -0700284
Andres Moralesa623e452015-05-27 12:26:59 -0700285bool GateKeeper::ThrottleRequest(uint32_t uid, uint64_t timestamp,
Andres Morales48a4f832015-05-29 10:13:31 -0700286 failure_record_t *record, bool secure, GateKeeperMessage *response) {
Andres Moralesaedf6052015-05-14 13:10:30 -0700287
288 uint64_t last_checked = record->last_checked_timestamp;
289 uint32_t timeout = ComputeRetryTimeout(record);
290
291 if (timeout > 0) {
292 // we have a pending timeout
293 if (timestamp < last_checked + timeout && timestamp > last_checked) {
294 // attempt before timeout expired, return remaining time
295 response->SetRetryTimeout(timeout - (timestamp - last_checked));
296 return true;
297 } else if (timestamp <= last_checked) {
298 // device was rebooted or timer reset, don't count as new failure but
299 // reset timeout
300 record->last_checked_timestamp = timestamp;
Andres Morales48a4f832015-05-29 10:13:31 -0700301 if (!WriteFailureRecord(uid, record, secure)) {
Andres Moralesaedf6052015-05-14 13:10:30 -0700302 response->error = ERROR_UNKNOWN;
303 return true;
304 }
305 response->SetRetryTimeout(timeout);
306 return true;
307 }
308 }
309
310 return false;
311}
312
313bool GateKeeper::IncrementFailureRecord(uint32_t uid, secure_id_t user_id, uint64_t timestamp,
Andres Morales48a4f832015-05-29 10:13:31 -0700314 failure_record_t *record, bool secure) {
Andres Moralesaedf6052015-05-14 13:10:30 -0700315 record->secure_user_id = user_id;
316 record->failure_counter++;
317 record->last_checked_timestamp = timestamp;
318
Andres Morales48a4f832015-05-29 10:13:31 -0700319 return WriteFailureRecord(uid, record, secure);
Andres Moralesaedf6052015-05-14 13:10:30 -0700320}
321} // namespace gatekeeper
322