• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
16 #include <gatekeeper/UniquePtr.h>
17 #include <gatekeeper/gatekeeper.h>
18 
19 #include <endian.h>
20 
21 #define DAY_IN_MS (1000 * 60 * 60 * 24)
22 
23 namespace gatekeeper {
24 
Enroll(const EnrollRequest & request,EnrollResponse * response)25 void GateKeeper::Enroll(const EnrollRequest &request, EnrollResponse *response) {
26     if (response == NULL) return;
27 
28     if (!request.provided_password.buffer.get()) {
29         response->error = ERROR_INVALID;
30         return;
31     }
32 
33     secure_id_t user_id = 0;// todo: rename to policy
34     uint32_t uid = request.user_id;
35 
36     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 {
40         if (request.password_handle.length < sizeof(password_handle_t)) {
41             response->error = ERROR_INVALID;
42             return;
43         }
44         password_handle_t *pw_handle =
45             reinterpret_cast<password_handle_t *>(request.password_handle.buffer.get());
46 
47         if (pw_handle->version > HANDLE_VERSION) {
48             response->error = ERROR_INVALID;
49             return;
50         }
51 
52         user_id = pw_handle->user_id;
53 
54         uint64_t timestamp = GetMillisecondsSinceBoot();
55 
56         uint32_t timeout = 0;
57         bool throttle = (pw_handle->version >= HANDLE_VERSION_THROTTLE);
58         if (throttle) {
59             bool throttle_secure = pw_handle->flags & HANDLE_FLAG_THROTTLE_SECURE;
60             failure_record_t record;
61             if (!GetFailureRecord(uid, user_id, &record, throttle_secure)) {
62                 response->error = ERROR_UNKNOWN;
63                 return;
64             }
65 
66             if (ThrottleRequest(uid, timestamp, &record, throttle_secure, response)) return;
67 
68             if (!IncrementFailureRecord(uid, user_id, timestamp, &record, throttle_secure)) {
69                 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         }
85     }
86 
87     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     }
93 
94     salt_t salt;
95     GetRandom(&salt, sizeof(salt));
96 
97     SizedBuffer password_handle;
98     if (!CreatePasswordHandle(&password_handle,
99             salt, user_id, flags, HANDLE_VERSION, request.provided_password.buffer.get(),
100             request.provided_password.length)) {
101         response->error = ERROR_INVALID;
102         return;
103     }
104 
105     response->SetEnrolledPasswordHandle(&password_handle);
106 }
107 
Verify(const VerifyRequest & request,VerifyResponse * response)108 void GateKeeper::Verify(const VerifyRequest &request, VerifyResponse *response) {
109     if (response == NULL) return;
110 
111     if (!request.provided_password.buffer.get() || !request.password_handle.buffer.get()) {
112         response->error = ERROR_INVALID;
113         return;
114     }
115 
116     if (request.password_handle.length < sizeof(password_handle_t)) {
117         response->error = ERROR_INVALID;
118         return;
119     }
120 
121     password_handle_t *password_handle = reinterpret_cast<password_handle_t *>(
122             request.password_handle.buffer.get());
123 
124     if (password_handle->version > HANDLE_VERSION) {
125         response->error = ERROR_INVALID;
126         return;
127     }
128 
129     secure_id_t user_id = password_handle->user_id;
130     secure_id_t authenticator_id = 0;
131     uint32_t uid = request.user_id;
132 
133     uint64_t timestamp = GetMillisecondsSinceBoot();
134 
135     uint32_t timeout = 0;
136     bool throttle = (password_handle->version >= HANDLE_VERSION_THROTTLE);
137     bool throttle_secure = password_handle->flags & HANDLE_FLAG_THROTTLE_SECURE;
138     if (throttle) {
139         failure_record_t record;
140         if (!GetFailureRecord(uid, user_id, &record, throttle_secure)) {
141             response->error = ERROR_UNKNOWN;
142             return;
143         }
144 
145         if (ThrottleRequest(uid, timestamp, &record, throttle_secure, response)) return;
146 
147         if (!IncrementFailureRecord(uid, user_id, timestamp, &record, throttle_secure)) {
148             response->error = ERROR_UNKNOWN;
149             return;
150         }
151 
152         timeout = ComputeRetryTimeout(&record);
153     } else {
154         response->request_reenroll = true;
155     }
156 
157     if (DoVerify(password_handle, request.provided_password)) {
158         // Signature matches
159         UniquePtr<uint8_t> auth_token_buffer;
160         uint32_t auth_token_len;
161         MintAuthToken(&auth_token_buffer, &auth_token_len, timestamp,
162                 user_id, authenticator_id, request.challenge);
163 
164         SizedBuffer auth_token(auth_token_len);
165         memcpy(auth_token.buffer.get(), auth_token_buffer.get(), auth_token_len);
166         response->SetVerificationToken(&auth_token);
167         if (throttle) ClearFailureRecord(uid, user_id, throttle_secure);
168     } else {
169         // 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         }
175     }
176 }
177 
CreatePasswordHandle(SizedBuffer * password_handle_buffer,salt_t salt,secure_id_t user_id,uint64_t flags,uint8_t handle_version,const uint8_t * password,uint32_t password_length)178 bool GateKeeper::CreatePasswordHandle(SizedBuffer *password_handle_buffer, salt_t salt,
179         secure_id_t user_id, uint64_t flags, uint8_t handle_version, const uint8_t *password,
180         uint32_t password_length) {
181     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());
186     password_handle->version = handle_version;
187     password_handle->salt = salt;
188     password_handle->user_id = user_id;
189     password_handle->flags = flags;
190     password_handle->hardware_backed = IsHardwareBacked();
191 
192     uint32_t metadata_length = sizeof(user_id) + sizeof(flags) + sizeof(HANDLE_VERSION);
193     const size_t to_sign_size = password_length + metadata_length;
194     UniquePtr<uint8_t[]> to_sign(new uint8_t[to_sign_size]);
195 
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);
202 
203     const uint8_t *password_key = NULL;
204     uint32_t password_key_length = 0;
205     GetPasswordKey(&password_key, &password_key_length);
206 
207     if (!password_key || password_key_length == 0) {
208         return false;
209     }
210 
211     ComputePasswordSignature(password_handle->signature, sizeof(password_handle->signature),
212             password_key, password_key_length, to_sign.get(), to_sign_size, salt);
213     return true;
214 }
215 
DoVerify(const password_handle_t * expected_handle,const SizedBuffer & password)216 bool GateKeeper::DoVerify(const password_handle_t *expected_handle, const SizedBuffer &password) {
217     if (!password.buffer.get()) return false;
218 
219     SizedBuffer provided_handle;
220     if (!CreatePasswordHandle(&provided_handle, expected_handle->salt, expected_handle->user_id,
221             expected_handle->flags, expected_handle->version,
222             password.buffer.get(), password.length)) {
223         return false;
224     }
225 
226     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;
230 }
231 
MintAuthToken(UniquePtr<uint8_t> * auth_token,uint32_t * length,uint64_t timestamp,secure_id_t user_id,secure_id_t authenticator_id,uint64_t challenge)232 void GateKeeper::MintAuthToken(UniquePtr<uint8_t> *auth_token, uint32_t *length,
233         uint64_t timestamp, secure_id_t user_id, secure_id_t authenticator_id,
234         uint64_t challenge) {
235     if (auth_token == NULL) return;
236 
237     hw_auth_token_t *token = new hw_auth_token_t;
238     SizedBuffer serialized_auth_token;
239 
240     token->version = HW_AUTH_TOKEN_VERSION;
241     token->challenge = challenge;
242     token->user_id = user_id;
243     token->authenticator_id = authenticator_id;
244     token->authenticator_type = htobe32(HW_AUTH_PASSWORD);
245     token->timestamp = htobe64(timestamp);
246 
247     const uint8_t *auth_token_key = NULL;
248     uint32_t key_len = 0;
249     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     }
256 
257     if (length != NULL) *length = sizeof(*token);
258     auth_token->reset(reinterpret_cast<uint8_t *>(token));
259 }
260 
261 /*
262  * Calculates the timeout in milliseconds as a function of the failure
263  * counter 'x' as follows:
264  *
265  * [0, 4] -> 0
266  * 5 -> 30
267  * [6, 10] -> 0
268  * [11, 29] -> 30
269  * [30, 139] -> 30 * (2^((x - 30)/10))
270  * [140, inf) -> 1 day
271  *
272  */
ComputeRetryTimeout(const failure_record_t * record)273 uint32_t GateKeeper::ComputeRetryTimeout(const failure_record_t *record) {
274     static const int failure_timeout_ms = 30000;
275     if (record->failure_counter == 0) return 0;
276 
277     if (record->failure_counter > 0 && record->failure_counter <= 10) {
278         if (record->failure_counter % 5 == 0) {
279             return failure_timeout_ms;
280         }  else {
281             return 0;
282         }
283     } else if (record->failure_counter < 30) {
284         return failure_timeout_ms;
285     } else if (record->failure_counter < 140) {
286         return failure_timeout_ms << ((record->failure_counter - 30) / 10);
287     }
288 
289     return DAY_IN_MS;
290 }
291 
ThrottleRequest(uint32_t uid,uint64_t timestamp,failure_record_t * record,bool secure,GateKeeperMessage * response)292 bool GateKeeper::ThrottleRequest(uint32_t uid, uint64_t timestamp,
293         failure_record_t *record, bool secure, GateKeeperMessage *response) {
294 
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;
308             if (!WriteFailureRecord(uid, record, secure)) {
309                 response->error = ERROR_UNKNOWN;
310                 return true;
311             }
312             response->SetRetryTimeout(timeout);
313             return true;
314         }
315     }
316 
317     return false;
318 }
319 
IncrementFailureRecord(uint32_t uid,secure_id_t user_id,uint64_t timestamp,failure_record_t * record,bool secure)320 bool GateKeeper::IncrementFailureRecord(uint32_t uid, secure_id_t user_id, uint64_t timestamp,
321             failure_record_t *record, bool secure) {
322     record->secure_user_id = user_id;
323     record->failure_counter++;
324     record->last_checked_timestamp = timestamp;
325 
326     return WriteFailureRecord(uid, record, secure);
327 }
328 } // namespace gatekeeper
329 
330