blob: 86d65de5479e0acffbbd1b3ecbf2bd46112d1f41 [file] [log] [blame]
Shawn Willden489dfe12015-03-17 10:13:27 -06001/*
2 * Copyright (C) 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
17#include <memory>
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070018#include <mutex>
Shawn Willden489dfe12015-03-17 10:13:27 -060019#include <vector>
20
Shawn Willden0329a822017-12-04 13:55:14 -070021#include <keystore/keymaster_types.h>
Shawn Willden489dfe12015-03-17 10:13:27 -060022
Shawn Willden6507c272016-01-05 22:51:48 -070023#ifndef KEYSTORE_AUTH_TOKEN_TABLE_H_
24#define KEYSTORE_AUTH_TOKEN_TABLE_H_
Shawn Willden489dfe12015-03-17 10:13:27 -060025
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010026namespace keystore {
27
Shawn Willden0329a822017-12-04 13:55:14 -070028using keymaster::HardwareAuthToken;
Shawn Willden489dfe12015-03-17 10:13:27 -060029
30namespace test {
31class AuthTokenTableTest;
32} // namespace test
33
34time_t clock_gettime_raw();
35
36/**
37 * AuthTokenTable manages a set of received authorization tokens and can provide the appropriate
38 * token for authorizing a key operation.
39 *
40 * To keep the table from growing without bound, superseded entries are removed when possible, and
41 * least recently used entries are automatically pruned when when the table exceeds a size limit,
42 * which is expected to be relatively small, since the implementation uses a linear search.
43 */
44class AuthTokenTable {
45 public:
Chih-Hung Hsiehd7791be2016-07-12 11:58:02 -070046 explicit AuthTokenTable(size_t max_entries = 32, time_t (*clock_function)() = clock_gettime_raw)
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010047 : max_entries_(max_entries), last_off_body_(clock_function()),
48 clock_function_(clock_function) {}
Shawn Willden489dfe12015-03-17 10:13:27 -060049
50 enum Error {
51 OK,
52 AUTH_NOT_REQUIRED = -1,
53 AUTH_TOKEN_EXPIRED = -2, // Found a matching token, but it's too old.
54 AUTH_TOKEN_WRONG_SID = -3, // Found a token with the right challenge, but wrong SID. This
55 // most likely indicates that the authenticator was updated
56 // (e.g. new fingerprint enrolled).
57 OP_HANDLE_REQUIRED = -4, // The key requires auth per use but op_handle was zero.
58 AUTH_TOKEN_NOT_FOUND = -5,
59 };
60
61 /**
Shawn Willdenbb22a6c2017-12-06 19:35:28 -070062 * Add an authorization token to the table.
Shawn Willden489dfe12015-03-17 10:13:27 -060063 */
Shawn Willden0329a822017-12-04 13:55:14 -070064 void AddAuthenticationToken(HardwareAuthToken&& auth_token);
Shawn Willden489dfe12015-03-17 10:13:27 -060065
66 /**
67 * Find an authorization token that authorizes the operation specified by \p operation_handle on
68 * a key with the characteristics specified in \p key_info.
69 *
70 * This method is O(n * m), where n is the number of KM_TAG_USER_SECURE_ID entries in key_info
71 * and m is the number of entries in the table. It could be made better, but n and m should
72 * always be small.
73 *
74 * The table retains ownership of the returned object.
75 */
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070076 std::tuple<Error, HardwareAuthToken> FindAuthorization(const AuthorizationSet& key_info,
77 KeyPurpose purpose, uint64_t op_handle);
Shawn Willden489dfe12015-03-17 10:13:27 -060078
David Zeuthenab3e5652019-10-28 13:32:48 -040079 std::tuple<Error, HardwareAuthToken>
80 FindAuthorizationForCredstore(uint64_t challenge, uint64_t secureUserId,
81 int64_t authTokenMaxAgeMillis);
82
Shawn Willden489dfe12015-03-17 10:13:27 -060083 /**
Shawn Willden489dfe12015-03-17 10:13:27 -060084 * Mark operation completed. This allows tokens associated with the specified operation to be
85 * superseded by new tokens.
86 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010087 void MarkCompleted(const uint64_t op_handle);
Shawn Willden489dfe12015-03-17 10:13:27 -060088
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -040089 /**
90 * Update the last_off_body_ timestamp so that tokens which remain authorized only so long as
91 * the device stays on body can be revoked.
92 */
93 void onDeviceOffBody();
94
Chad Brubakerbbc76482015-04-16 15:16:44 -070095 void Clear();
96
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070097 /**
98 * This function shall only be used for testing.
99 *
100 * BEWARE: Since the auth token table can be accessed
101 * concurrently, the size may be out dated as soon as it returns.
102 */
103 size_t size() const;
Shawn Willden489dfe12015-03-17 10:13:27 -0600104
105 private:
106 friend class AuthTokenTableTest;
107
108 class Entry {
109 public:
Shawn Willden0329a822017-12-04 13:55:14 -0700110 Entry(HardwareAuthToken&& token, time_t current_time);
Chih-Hung Hsiehf73b1982018-09-25 12:03:21 -0700111 Entry(Entry&& entry) noexcept { *this = std::move(entry); }
Shawn Willden489dfe12015-03-17 10:13:27 -0600112
Chih-Hung Hsiehf73b1982018-09-25 12:03:21 -0700113 void operator=(Entry&& rhs) noexcept {
Shawn Willden489dfe12015-03-17 10:13:27 -0600114 token_ = std::move(rhs.token_);
115 time_received_ = rhs.time_received_;
116 last_use_ = rhs.last_use_;
117 operation_completed_ = rhs.operation_completed_;
118 }
119
120 bool operator<(const Entry& rhs) const { return last_use_ < rhs.last_use_; }
121
122 void UpdateLastUse(time_t time);
123
124 bool Supersedes(const Entry& entry) const;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100125 bool SatisfiesAuth(const std::vector<uint64_t>& sids, HardwareAuthenticatorType auth_type);
Shawn Willden489dfe12015-03-17 10:13:27 -0600126
Rubin Xubfb01d92017-10-23 17:04:25 +0100127 bool is_newer_than(const Entry* entry) const {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100128 if (!entry) return true;
Shawn Willden0329a822017-12-04 13:55:14 -0700129 uint64_t ts = token_.timestamp;
130 uint64_t other_ts = entry->token_.timestamp;
Rubin Xubfb01d92017-10-23 17:04:25 +0100131 // Normally comparing timestamp_host_order alone is sufficient, but here is an
132 // additional hack to compare time_received value for some devices where their auth
133 // tokens contain fixed timestamp (due to the a stuck secure RTC on them)
134 return (ts > other_ts) ||
135 ((ts == other_ts) && (time_received_ > entry->time_received_));
Shawn Willden489dfe12015-03-17 10:13:27 -0600136 }
137
138 void mark_completed() { operation_completed_ = true; }
139
Shawn Willden0329a822017-12-04 13:55:14 -0700140 const HardwareAuthToken& token() const & { return token_; }
Shawn Willden489dfe12015-03-17 10:13:27 -0600141 time_t time_received() const { return time_received_; }
142 bool completed() const { return operation_completed_; }
Shawn Willden489dfe12015-03-17 10:13:27 -0600143
144 private:
Shawn Willden0329a822017-12-04 13:55:14 -0700145 bool SatisfiesAuth(uint64_t sid, HardwareAuthenticatorType auth_type) const {
146 return (sid == token_.userId || sid == token_.authenticatorId) &&
147 (auth_type & token_.authenticatorType) != 0;
148 }
149
150 HardwareAuthToken token_;
Shawn Willden489dfe12015-03-17 10:13:27 -0600151 time_t time_received_;
152 time_t last_use_;
153 bool operation_completed_;
154 };
155
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700156 std::tuple<Error, HardwareAuthToken>
157 FindAuthPerOpAuthorization(const std::vector<uint64_t>& sids,
158 HardwareAuthenticatorType auth_type, uint64_t op_handle);
159 std::tuple<Error, HardwareAuthToken> FindTimedAuthorization(const std::vector<uint64_t>& sids,
160 HardwareAuthenticatorType auth_type,
161 const AuthorizationSet& key_info);
Shawn Willden489dfe12015-03-17 10:13:27 -0600162 void ExtractSids(const AuthorizationSet& key_info, std::vector<uint64_t>* sids);
163 void RemoveEntriesSupersededBy(const Entry& entry);
164 bool IsSupersededBySomeEntry(const Entry& entry);
165
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700166 /**
167 * Guards the entries_ vector against concurrent modification. All public facing methods
168 * reading of modifying the vector must grab this mutex.
169 */
170 mutable std::mutex entries_mutex_;
Shawn Willden489dfe12015-03-17 10:13:27 -0600171 std::vector<Entry> entries_;
172 size_t max_entries_;
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400173 time_t last_off_body_;
Shawn Willden489dfe12015-03-17 10:13:27 -0600174 time_t (*clock_function_)();
175};
176
Shawn Willden0329a822017-12-04 13:55:14 -0700177} // namespace keystore
Shawn Willden489dfe12015-03-17 10:13:27 -0600178
Shawn Willden6507c272016-01-05 22:51:48 -0700179#endif // KEYSTORE_AUTH_TOKEN_TABLE_H_