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 <UniquePtr.h> 17 #include <gatekeeper/gatekeeper.h> 18 19 #include <endian.h> 20 21 namespace gatekeeper { 22 23 void GateKeeper::Enroll(const EnrollRequest &request, EnrollResponse *response) { 24 if (response == NULL) return; 25 26 if (!request.provided_password.buffer.get()) { 27 response->error = ERROR_INVALID; 28 return; 29 } 30 31 secure_id_t user_id = 0;// todo: rename to policy 32 uint32_t uid = request.user_id; 33 34 if (request.password_handle.buffer.get() == NULL) { 35 // Password handle does not match what is stored, generate new SecureID 36 GetRandom(&user_id, sizeof(secure_id_t)); 37 } else { 38 password_handle_t *pw_handle = 39 reinterpret_cast<password_handle_t *>(request.password_handle.buffer.get()); 40 41 if (pw_handle->version > HANDLE_VERSION) { 42 response->error = ERROR_INVALID; 43 return; 44 } 45 46 user_id = pw_handle->user_id; 47 48 uint64_t timestamp = GetMillisecondsSinceBoot(); 49 50 uint32_t timeout = 0; 51 bool throttle = (pw_handle->version >= HANDLE_VERSION_THROTTLE); 52 if (throttle) { 53 bool throttle_secure = pw_handle->flags & HANDLE_FLAG_THROTTLE_SECURE; 54 failure_record_t record; 55 if (!GetFailureRecord(uid, user_id, &record, throttle_secure)) { 56 response->error = ERROR_UNKNOWN; 57 return; 58 } 59 60 if (ThrottleRequest(uid, timestamp, &record, throttle_secure, response)) return; 61 62 if (!IncrementFailureRecord(uid, user_id, timestamp, &record, throttle_secure)) { 63 response->error = ERROR_UNKNOWN; 64 return; 65 } 66 67 timeout = ComputeRetryTimeout(&record); 68 } 69 70 if (!DoVerify(pw_handle, request.enrolled_password)) { 71 // incorrect old password 72 if (throttle && timeout > 0) { 73 response->SetRetryTimeout(timeout); 74 } else { 75 response->error = ERROR_INVALID; 76 } 77 return; 78 } 79 } 80 81 uint64_t flags = 0; 82 if (ClearFailureRecord(uid, user_id, true)) { 83 flags |= HANDLE_FLAG_THROTTLE_SECURE; 84 } else { 85 ClearFailureRecord(uid, user_id, false); 86 } 87 88 salt_t salt; 89 GetRandom(&salt, sizeof(salt)); 90 91 SizedBuffer password_handle; 92 if (!CreatePasswordHandle(&password_handle, 93 salt, user_id, flags, HANDLE_VERSION, request.provided_password.buffer.get(), 94 request.provided_password.length)) { 95 response->error = ERROR_INVALID; 96 return; 97 } 98 99 response->SetEnrolledPasswordHandle(&password_handle); 100 } 101 102 void GateKeeper::Verify(const VerifyRequest &request, VerifyResponse *response) { 103 if (response == NULL) return; 104 105 if (!request.provided_password.buffer.get() || !request.password_handle.buffer.get()) { 106 response->error = ERROR_INVALID; 107 return; 108 } 109 110 password_handle_t *password_handle = reinterpret_cast<password_handle_t *>( 111 request.password_handle.buffer.get()); 112 113 if (password_handle->version > HANDLE_VERSION) { 114 response->error = ERROR_INVALID; 115 return; 116 } 117 118 secure_id_t user_id = password_handle->user_id; 119 secure_id_t authenticator_id = 0; 120 uint32_t uid = request.user_id; 121 122 uint64_t timestamp = GetMillisecondsSinceBoot(); 123 124 uint32_t timeout = 0; 125 bool throttle = (password_handle->version >= HANDLE_VERSION_THROTTLE); 126 bool throttle_secure = password_handle->flags & HANDLE_FLAG_THROTTLE_SECURE; 127 if (throttle) { 128 failure_record_t record; 129 if (!GetFailureRecord(uid, user_id, &record, throttle_secure)) { 130 response->error = ERROR_UNKNOWN; 131 return; 132 } 133 134 if (ThrottleRequest(uid, timestamp, &record, throttle_secure, response)) return; 135 136 if (!IncrementFailureRecord(uid, user_id, timestamp, &record, throttle_secure)) { 137 response->error = ERROR_UNKNOWN; 138 return; 139 } 140 141 timeout = ComputeRetryTimeout(&record); 142 } else { 143 response->request_reenroll = true; 144 } 145 146 if (DoVerify(password_handle, request.provided_password)) { 147 // Signature matches 148 UniquePtr<uint8_t> auth_token_buffer; 149 uint32_t auth_token_len; 150 MintAuthToken(&auth_token_buffer, &auth_token_len, timestamp, 151 user_id, authenticator_id, request.challenge); 152 153 SizedBuffer auth_token(auth_token_len); 154 memcpy(auth_token.buffer.get(), auth_token_buffer.get(), auth_token_len); 155 response->SetVerificationToken(&auth_token); 156 if (throttle) ClearFailureRecord(uid, user_id, throttle_secure); 157 } else { 158 // compute the new timeout given the incremented record 159 if (throttle && timeout > 0) { 160 response->SetRetryTimeout(timeout); 161 } else { 162 response->error = ERROR_INVALID; 163 } 164 } 165 } 166 167 bool GateKeeper::CreatePasswordHandle(SizedBuffer *password_handle_buffer, salt_t salt, 168 secure_id_t user_id, uint64_t flags, uint8_t handle_version, const uint8_t *password, 169 uint32_t password_length) { 170 password_handle_buffer->buffer.reset(new uint8_t[sizeof(password_handle_t)]); 171 password_handle_buffer->length = sizeof(password_handle_t); 172 173 password_handle_t *password_handle = reinterpret_cast<password_handle_t *>( 174 password_handle_buffer->buffer.get()); 175 password_handle->version = handle_version; 176 password_handle->salt = salt; 177 password_handle->user_id = user_id; 178 password_handle->flags = flags; 179 password_handle->hardware_backed = IsHardwareBacked(); 180 181 uint32_t metadata_length = sizeof(user_id) + sizeof(flags) + sizeof(HANDLE_VERSION); 182 uint8_t to_sign[password_length + metadata_length]; 183 memcpy(to_sign, password_handle, metadata_length); 184 memcpy(to_sign + metadata_length, password, password_length); 185 186 const uint8_t *password_key = NULL; 187 uint32_t password_key_length = 0; 188 GetPasswordKey(&password_key, &password_key_length); 189 190 if (!password_key || password_key_length == 0) { 191 return false; 192 } 193 194 ComputePasswordSignature(password_handle->signature, sizeof(password_handle->signature), 195 password_key, password_key_length, to_sign, sizeof(to_sign), salt); 196 return true; 197 } 198 199 bool GateKeeper::DoVerify(const password_handle_t *expected_handle, const SizedBuffer &password) { 200 if (!password.buffer.get()) return false; 201 202 SizedBuffer provided_handle; 203 if (!CreatePasswordHandle(&provided_handle, expected_handle->salt, expected_handle->user_id, 204 expected_handle->flags, expected_handle->version, 205 password.buffer.get(), password.length)) { 206 return false; 207 } 208 209 password_handle_t *generated_handle = 210 reinterpret_cast<password_handle_t *>(provided_handle.buffer.get()); 211 return memcmp_s(generated_handle->signature, expected_handle->signature, 212 sizeof(expected_handle->signature)) == 0; 213 } 214 215 void GateKeeper::MintAuthToken(UniquePtr<uint8_t> *auth_token, uint32_t *length, 216 uint64_t timestamp, secure_id_t user_id, secure_id_t authenticator_id, 217 uint64_t challenge) { 218 if (auth_token == NULL) return; 219 220 hw_auth_token_t *token = new hw_auth_token_t; 221 SizedBuffer serialized_auth_token; 222 223 token->version = HW_AUTH_TOKEN_VERSION; 224 token->challenge = challenge; 225 token->user_id = user_id; 226 token->authenticator_id = authenticator_id; 227 token->authenticator_type = htonl(HW_AUTH_PASSWORD); 228 token->timestamp = htobe64(timestamp); 229 230 const uint8_t *auth_token_key = NULL; 231 uint32_t key_len = 0; 232 if (GetAuthTokenKey(&auth_token_key, &key_len)) { 233 uint32_t hash_len = (uint32_t)((uint8_t *)&token->hmac - (uint8_t *)token); 234 ComputeSignature(token->hmac, sizeof(token->hmac), auth_token_key, key_len, 235 reinterpret_cast<uint8_t *>(token), hash_len); 236 delete[] auth_token_key; 237 } else { 238 memset(token->hmac, 0, sizeof(token->hmac)); 239 } 240 241 if (length != NULL) *length = sizeof(*token); 242 auth_token->reset(reinterpret_cast<uint8_t *>(token)); 243 } 244 245 uint32_t GateKeeper::ComputeRetryTimeout(const failure_record_t *record) { 246 if (record->failure_counter > 0 && record->failure_counter <= 10) { 247 if (record->failure_counter % 5 == 0) { 248 return 30000; 249 } 250 } else { 251 return 30000; 252 } 253 return 0; 254 } 255 256 bool GateKeeper::ThrottleRequest(uint32_t uid, uint64_t timestamp, 257 failure_record_t *record, bool secure, GateKeeperMessage *response) { 258 259 uint64_t last_checked = record->last_checked_timestamp; 260 uint32_t timeout = ComputeRetryTimeout(record); 261 262 if (timeout > 0) { 263 // we have a pending timeout 264 if (timestamp < last_checked + timeout && timestamp > last_checked) { 265 // attempt before timeout expired, return remaining time 266 response->SetRetryTimeout(timeout - (timestamp - last_checked)); 267 return true; 268 } else if (timestamp <= last_checked) { 269 // device was rebooted or timer reset, don't count as new failure but 270 // reset timeout 271 record->last_checked_timestamp = timestamp; 272 if (!WriteFailureRecord(uid, record, secure)) { 273 response->error = ERROR_UNKNOWN; 274 return true; 275 } 276 response->SetRetryTimeout(timeout); 277 return true; 278 } 279 } 280 281 return false; 282 } 283 284 bool GateKeeper::IncrementFailureRecord(uint32_t uid, secure_id_t user_id, uint64_t timestamp, 285 failure_record_t *record, bool secure) { 286 record->secure_user_id = user_id; 287 record->failure_counter++; 288 record->last_checked_timestamp = timestamp; 289 290 return WriteFailureRecord(uid, record, secure); 291 } 292 } // namespace gatekeeper 293 294