gatekeeper.cpp 11 KB

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