blob: e54febef42676fd1ffe529788b0f64260170245f [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
Rubin Xuce99f582017-10-12 10:50:11 +010017#define LOG_TAG "keystore"
18
Shawn Willden489dfe12015-03-17 10:13:27 -060019#include "auth_token_table.h"
20
21#include <assert.h>
22#include <time.h>
23
24#include <algorithm>
25
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010026#include <cutils/log.h>
Shawn Willden489dfe12015-03-17 10:13:27 -060027
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010028namespace keystore {
29
Shawn Willdend3ed3a22017-03-28 00:39:16 +000030template <typename IntType, uint32_t byteOrder> struct choose_hton;
31
32template <typename IntType> struct choose_hton<IntType, __ORDER_LITTLE_ENDIAN__> {
33 inline static IntType hton(const IntType& value) {
34 IntType result = 0;
35 const unsigned char* inbytes = reinterpret_cast<const unsigned char*>(&value);
36 unsigned char* outbytes = reinterpret_cast<unsigned char*>(&result);
37 for (int i = sizeof(IntType) - 1; i >= 0; --i) {
38 *(outbytes++) = inbytes[i];
39 }
40 return result;
41 }
42};
43
44template <typename IntType> struct choose_hton<IntType, __ORDER_BIG_ENDIAN__> {
45 inline static IntType hton(const IntType& value) { return value; }
46};
47
48template <typename IntType> inline IntType hton(const IntType& value) {
49 return choose_hton<IntType, __BYTE_ORDER__>::hton(value);
50}
51
52template <typename IntType> inline IntType ntoh(const IntType& value) {
53 // same operation and hton
54 return choose_hton<IntType, __BYTE_ORDER__>::hton(value);
55}
56
Shawn Willden489dfe12015-03-17 10:13:27 -060057//
58// Some trivial template wrappers around std algorithms, so they take containers not ranges.
59//
60template <typename Container, typename Predicate>
61typename Container::iterator find_if(Container& container, Predicate pred) {
62 return std::find_if(container.begin(), container.end(), pred);
63}
64
65template <typename Container, typename Predicate>
66typename Container::iterator remove_if(Container& container, Predicate pred) {
67 return std::remove_if(container.begin(), container.end(), pred);
68}
69
70template <typename Container> typename Container::iterator min_element(Container& container) {
71 return std::min_element(container.begin(), container.end());
72}
73
74time_t clock_gettime_raw() {
75 struct timespec time;
76 clock_gettime(CLOCK_MONOTONIC_RAW, &time);
77 return time.tv_sec;
78}
79
Janis Danisevskis8f737ad2017-11-21 12:30:15 -080080void AuthTokenTable::AddAuthenticationToken(std::unique_ptr<const HardwareAuthToken>&& auth_token) {
81 Entry new_entry(std::move(auth_token), clock_function_());
Rubin Xuce99f582017-10-12 10:50:11 +010082 //STOPSHIP: debug only, to be removed
83 ALOGD("AddAuthenticationToken: timestamp = %llu (%llu), time_received = %lld",
84 static_cast<unsigned long long>(new_entry.timestamp_host_order()),
Janis Danisevskis8f737ad2017-11-21 12:30:15 -080085 static_cast<unsigned long long>(new_entry.token().timestamp),
Rubin Xuce99f582017-10-12 10:50:11 +010086 static_cast<long long>(new_entry.time_received()));
87
Shawn Willden489dfe12015-03-17 10:13:27 -060088 RemoveEntriesSupersededBy(new_entry);
89 if (entries_.size() >= max_entries_) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010090 ALOGW("Auth token table filled up; replacing oldest entry");
Shawn Willden489dfe12015-03-17 10:13:27 -060091 *min_element(entries_) = std::move(new_entry);
92 } else {
93 entries_.push_back(std::move(new_entry));
94 }
95}
96
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010097inline bool is_secret_key_operation(Algorithm algorithm, KeyPurpose purpose) {
98 if ((algorithm != Algorithm::RSA && algorithm != Algorithm::EC))
Shawn Willdenb2ffa422015-06-17 12:18:55 -060099 return true;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100100 if (purpose == KeyPurpose::SIGN || purpose == KeyPurpose::DECRYPT)
Shawn Willdenb2ffa422015-06-17 12:18:55 -0600101 return true;
102 return false;
Shawn Willden489dfe12015-03-17 10:13:27 -0600103}
104
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100105inline bool KeyRequiresAuthentication(const AuthorizationSet& key_info, KeyPurpose purpose) {
106 auto algorithm = defaultOr(key_info.GetTagValue(TAG_ALGORITHM), Algorithm::AES);
107 return is_secret_key_operation(algorithm, purpose) &&
108 key_info.find(Tag::NO_AUTH_REQUIRED) == -1;
Shawn Willdenb2ffa422015-06-17 12:18:55 -0600109}
110
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100111inline bool KeyRequiresAuthPerOperation(const AuthorizationSet& key_info, KeyPurpose purpose) {
112 auto algorithm = defaultOr(key_info.GetTagValue(TAG_ALGORITHM), Algorithm::AES);
113 return is_secret_key_operation(algorithm, purpose) && key_info.find(Tag::AUTH_TIMEOUT) == -1;
Shawn Willden489dfe12015-03-17 10:13:27 -0600114}
115
116AuthTokenTable::Error AuthTokenTable::FindAuthorization(const AuthorizationSet& key_info,
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100117 KeyPurpose purpose, uint64_t op_handle,
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000118 const HardwareAuthToken** found) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100119 if (!KeyRequiresAuthentication(key_info, purpose)) return AUTH_NOT_REQUIRED;
Shawn Willden489dfe12015-03-17 10:13:27 -0600120
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100121 auto auth_type =
122 defaultOr(key_info.GetTagValue(TAG_USER_AUTH_TYPE), HardwareAuthenticatorType::NONE);
Shawn Willden489dfe12015-03-17 10:13:27 -0600123
124 std::vector<uint64_t> key_sids;
125 ExtractSids(key_info, &key_sids);
126
Shawn Willdenb2ffa422015-06-17 12:18:55 -0600127 if (KeyRequiresAuthPerOperation(key_info, purpose))
Shawn Willden489dfe12015-03-17 10:13:27 -0600128 return FindAuthPerOpAuthorization(key_sids, auth_type, op_handle, found);
129 else
130 return FindTimedAuthorization(key_sids, auth_type, key_info, found);
131}
132
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100133AuthTokenTable::Error
134AuthTokenTable::FindAuthPerOpAuthorization(const std::vector<uint64_t>& sids,
135 HardwareAuthenticatorType auth_type, uint64_t op_handle,
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000136 const HardwareAuthToken** found) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100137 if (op_handle == 0) return OP_HANDLE_REQUIRED;
Shawn Willden489dfe12015-03-17 10:13:27 -0600138
139 auto matching_op = find_if(
Janis Danisevskis8f737ad2017-11-21 12:30:15 -0800140 entries_, [&](Entry& e) { return e.token().challenge == op_handle && !e.completed(); });
Shawn Willden489dfe12015-03-17 10:13:27 -0600141
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100142 if (matching_op == entries_.end()) return AUTH_TOKEN_NOT_FOUND;
Shawn Willden489dfe12015-03-17 10:13:27 -0600143
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100144 if (!matching_op->SatisfiesAuth(sids, auth_type)) return AUTH_TOKEN_WRONG_SID;
Shawn Willden489dfe12015-03-17 10:13:27 -0600145
Janis Danisevskis8f737ad2017-11-21 12:30:15 -0800146 *found = &matching_op->token();
Shawn Willden489dfe12015-03-17 10:13:27 -0600147 return OK;
148}
149
150AuthTokenTable::Error AuthTokenTable::FindTimedAuthorization(const std::vector<uint64_t>& sids,
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100151 HardwareAuthenticatorType auth_type,
Shawn Willden489dfe12015-03-17 10:13:27 -0600152 const AuthorizationSet& key_info,
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000153 const HardwareAuthToken** found) {
Shawn Willden489dfe12015-03-17 10:13:27 -0600154 Entry* newest_match = NULL;
155 for (auto& entry : entries_)
156 if (entry.SatisfiesAuth(sids, auth_type) && entry.is_newer_than(newest_match))
157 newest_match = &entry;
158
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100159 if (!newest_match) return AUTH_TOKEN_NOT_FOUND;
Shawn Willden489dfe12015-03-17 10:13:27 -0600160
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100161 auto timeout = defaultOr(key_info.GetTagValue(TAG_AUTH_TIMEOUT), 0);
162
Shawn Willden489dfe12015-03-17 10:13:27 -0600163 time_t now = clock_function_();
164 if (static_cast<int64_t>(newest_match->time_received()) + timeout < static_cast<int64_t>(now))
165 return AUTH_TOKEN_EXPIRED;
166
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100167 if (key_info.GetTagValue(TAG_ALLOW_WHILE_ON_BODY).isOk()) {
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400168 if (static_cast<int64_t>(newest_match->time_received()) <
169 static_cast<int64_t>(last_off_body_)) {
170 return AUTH_TOKEN_EXPIRED;
171 }
172 }
173
Shawn Willden489dfe12015-03-17 10:13:27 -0600174 newest_match->UpdateLastUse(now);
Janis Danisevskis8f737ad2017-11-21 12:30:15 -0800175 *found = &newest_match->token();
Shawn Willden489dfe12015-03-17 10:13:27 -0600176 return OK;
177}
178
179void AuthTokenTable::ExtractSids(const AuthorizationSet& key_info, std::vector<uint64_t>* sids) {
180 assert(sids);
181 for (auto& param : key_info)
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100182 if (param.tag == Tag::USER_SECURE_ID)
183 sids->push_back(authorizationValue(TAG_USER_SECURE_ID, param).value());
Shawn Willden489dfe12015-03-17 10:13:27 -0600184}
185
186void AuthTokenTable::RemoveEntriesSupersededBy(const Entry& entry) {
187 entries_.erase(remove_if(entries_, [&](Entry& e) { return entry.Supersedes(e); }),
188 entries_.end());
189}
190
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400191void AuthTokenTable::onDeviceOffBody() {
192 last_off_body_ = clock_function_();
193}
194
Chad Brubakerbbc76482015-04-16 15:16:44 -0700195void AuthTokenTable::Clear() {
196 entries_.clear();
197}
198
Shawn Willden489dfe12015-03-17 10:13:27 -0600199bool AuthTokenTable::IsSupersededBySomeEntry(const Entry& entry) {
200 return std::any_of(entries_.begin(), entries_.end(),
201 [&](Entry& e) { return e.Supersedes(entry); });
202}
203
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100204void AuthTokenTable::MarkCompleted(const uint64_t op_handle) {
Janis Danisevskis8f737ad2017-11-21 12:30:15 -0800205 auto found = find_if(entries_, [&](Entry& e) { return e.token().challenge == op_handle; });
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100206 if (found == entries_.end()) return;
Shawn Willden489dfe12015-03-17 10:13:27 -0600207
208 assert(!IsSupersededBySomeEntry(*found));
209 found->mark_completed();
210
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100211 if (IsSupersededBySomeEntry(*found)) entries_.erase(found);
Shawn Willden489dfe12015-03-17 10:13:27 -0600212}
213
Janis Danisevskis8f737ad2017-11-21 12:30:15 -0800214AuthTokenTable::Entry::Entry(std::unique_ptr<const HardwareAuthToken>&& token, time_t current_time)
215 : token_(std::move(token)), time_received_(current_time), last_use_(current_time),
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000216 operation_completed_(token_->challenge == 0) {}
217
Rubin Xuce99f582017-10-12 10:50:11 +0100218uint64_t AuthTokenTable::Entry::timestamp_host_order() const {
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000219 return ntoh(token_->timestamp);
220}
221
222HardwareAuthenticatorType AuthTokenTable::Entry::authenticator_type() const {
223 HardwareAuthenticatorType result = static_cast<HardwareAuthenticatorType>(
224 ntoh(static_cast<uint32_t>(token_->authenticatorType)));
225 return result;
226}
Shawn Willden489dfe12015-03-17 10:13:27 -0600227
228bool AuthTokenTable::Entry::SatisfiesAuth(const std::vector<uint64_t>& sids,
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100229 HardwareAuthenticatorType auth_type) {
Shawn Willden489dfe12015-03-17 10:13:27 -0600230 for (auto sid : sids)
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000231 if ((sid == token_->authenticatorId) ||
232 (sid == token_->userId && (auth_type & authenticator_type()) != 0))
Shawn Willden489dfe12015-03-17 10:13:27 -0600233 return true;
234 return false;
235}
236
237void AuthTokenTable::Entry::UpdateLastUse(time_t time) {
238 this->last_use_ = time;
239}
240
241bool AuthTokenTable::Entry::Supersedes(const Entry& entry) const {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100242 if (!entry.completed()) return false;
Shawn Willden489dfe12015-03-17 10:13:27 -0600243
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000244 return (token_->userId == entry.token_->userId &&
245 token_->authenticatorType == entry.token_->authenticatorType &&
Shawn Willden0143be72017-06-20 17:27:55 -0600246 token_->authenticatorId == entry.token_->authenticatorId &&
Rubin Xubfb01d92017-10-23 17:04:25 +0100247 is_newer_than(&entry));
Shawn Willden489dfe12015-03-17 10:13:27 -0600248}
249
250} // namespace keymaster