blob: 3f476cd84f13b15dc300a3ed066fa755f5af8f35 [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 "auth_token_table.h"
18
19#include <assert.h>
20#include <time.h>
21
22#include <algorithm>
23
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010024#include <cutils/log.h>
Shawn Willden489dfe12015-03-17 10:13:27 -060025
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010026namespace keystore {
27
Shawn Willdend3ed3a22017-03-28 00:39:16 +000028template <typename IntType, uint32_t byteOrder> struct choose_hton;
29
30template <typename IntType> struct choose_hton<IntType, __ORDER_LITTLE_ENDIAN__> {
31 inline static IntType hton(const IntType& value) {
32 IntType result = 0;
33 const unsigned char* inbytes = reinterpret_cast<const unsigned char*>(&value);
34 unsigned char* outbytes = reinterpret_cast<unsigned char*>(&result);
35 for (int i = sizeof(IntType) - 1; i >= 0; --i) {
36 *(outbytes++) = inbytes[i];
37 }
38 return result;
39 }
40};
41
42template <typename IntType> struct choose_hton<IntType, __ORDER_BIG_ENDIAN__> {
43 inline static IntType hton(const IntType& value) { return value; }
44};
45
46template <typename IntType> inline IntType hton(const IntType& value) {
47 return choose_hton<IntType, __BYTE_ORDER__>::hton(value);
48}
49
50template <typename IntType> inline IntType ntoh(const IntType& value) {
51 // same operation and hton
52 return choose_hton<IntType, __BYTE_ORDER__>::hton(value);
53}
54
Shawn Willden489dfe12015-03-17 10:13:27 -060055//
56// Some trivial template wrappers around std algorithms, so they take containers not ranges.
57//
58template <typename Container, typename Predicate>
59typename Container::iterator find_if(Container& container, Predicate pred) {
60 return std::find_if(container.begin(), container.end(), pred);
61}
62
63template <typename Container, typename Predicate>
64typename Container::iterator remove_if(Container& container, Predicate pred) {
65 return std::remove_if(container.begin(), container.end(), pred);
66}
67
68template <typename Container> typename Container::iterator min_element(Container& container) {
69 return std::min_element(container.begin(), container.end());
70}
71
72time_t clock_gettime_raw() {
73 struct timespec time;
74 clock_gettime(CLOCK_MONOTONIC_RAW, &time);
75 return time.tv_sec;
76}
77
Shawn Willdend3ed3a22017-03-28 00:39:16 +000078void AuthTokenTable::AddAuthenticationToken(const HardwareAuthToken* auth_token) {
79 Entry new_entry(auth_token, clock_function_());
Shawn Willden489dfe12015-03-17 10:13:27 -060080 RemoveEntriesSupersededBy(new_entry);
81 if (entries_.size() >= max_entries_) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010082 ALOGW("Auth token table filled up; replacing oldest entry");
Shawn Willden489dfe12015-03-17 10:13:27 -060083 *min_element(entries_) = std::move(new_entry);
84 } else {
85 entries_.push_back(std::move(new_entry));
86 }
87}
88
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010089inline bool is_secret_key_operation(Algorithm algorithm, KeyPurpose purpose) {
90 if ((algorithm != Algorithm::RSA && algorithm != Algorithm::EC))
Shawn Willdenb2ffa422015-06-17 12:18:55 -060091 return true;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010092 if (purpose == KeyPurpose::SIGN || purpose == KeyPurpose::DECRYPT)
Shawn Willdenb2ffa422015-06-17 12:18:55 -060093 return true;
94 return false;
Shawn Willden489dfe12015-03-17 10:13:27 -060095}
96
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010097inline bool KeyRequiresAuthentication(const AuthorizationSet& key_info, KeyPurpose purpose) {
98 auto algorithm = defaultOr(key_info.GetTagValue(TAG_ALGORITHM), Algorithm::AES);
99 return is_secret_key_operation(algorithm, purpose) &&
100 key_info.find(Tag::NO_AUTH_REQUIRED) == -1;
Shawn Willdenb2ffa422015-06-17 12:18:55 -0600101}
102
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100103inline bool KeyRequiresAuthPerOperation(const AuthorizationSet& key_info, KeyPurpose purpose) {
104 auto algorithm = defaultOr(key_info.GetTagValue(TAG_ALGORITHM), Algorithm::AES);
105 return is_secret_key_operation(algorithm, purpose) && key_info.find(Tag::AUTH_TIMEOUT) == -1;
Shawn Willden489dfe12015-03-17 10:13:27 -0600106}
107
108AuthTokenTable::Error AuthTokenTable::FindAuthorization(const AuthorizationSet& key_info,
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100109 KeyPurpose purpose, uint64_t op_handle,
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000110 const HardwareAuthToken** found) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100111 if (!KeyRequiresAuthentication(key_info, purpose)) return AUTH_NOT_REQUIRED;
Shawn Willden489dfe12015-03-17 10:13:27 -0600112
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100113 auto auth_type =
114 defaultOr(key_info.GetTagValue(TAG_USER_AUTH_TYPE), HardwareAuthenticatorType::NONE);
Shawn Willden489dfe12015-03-17 10:13:27 -0600115
116 std::vector<uint64_t> key_sids;
117 ExtractSids(key_info, &key_sids);
118
Shawn Willdenb2ffa422015-06-17 12:18:55 -0600119 if (KeyRequiresAuthPerOperation(key_info, purpose))
Shawn Willden489dfe12015-03-17 10:13:27 -0600120 return FindAuthPerOpAuthorization(key_sids, auth_type, op_handle, found);
121 else
122 return FindTimedAuthorization(key_sids, auth_type, key_info, found);
123}
124
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100125AuthTokenTable::Error
126AuthTokenTable::FindAuthPerOpAuthorization(const std::vector<uint64_t>& sids,
127 HardwareAuthenticatorType auth_type, uint64_t op_handle,
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000128 const HardwareAuthToken** found) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100129 if (op_handle == 0) return OP_HANDLE_REQUIRED;
Shawn Willden489dfe12015-03-17 10:13:27 -0600130
131 auto matching_op = find_if(
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000132 entries_, [&](Entry& e) { return e.token()->challenge == op_handle && !e.completed(); });
Shawn Willden489dfe12015-03-17 10:13:27 -0600133
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100134 if (matching_op == entries_.end()) return AUTH_TOKEN_NOT_FOUND;
Shawn Willden489dfe12015-03-17 10:13:27 -0600135
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100136 if (!matching_op->SatisfiesAuth(sids, auth_type)) return AUTH_TOKEN_WRONG_SID;
Shawn Willden489dfe12015-03-17 10:13:27 -0600137
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000138 *found = matching_op->token();
Shawn Willden489dfe12015-03-17 10:13:27 -0600139 return OK;
140}
141
142AuthTokenTable::Error AuthTokenTable::FindTimedAuthorization(const std::vector<uint64_t>& sids,
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100143 HardwareAuthenticatorType auth_type,
Shawn Willden489dfe12015-03-17 10:13:27 -0600144 const AuthorizationSet& key_info,
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000145 const HardwareAuthToken** found) {
Shawn Willden489dfe12015-03-17 10:13:27 -0600146 Entry* newest_match = NULL;
147 for (auto& entry : entries_)
148 if (entry.SatisfiesAuth(sids, auth_type) && entry.is_newer_than(newest_match))
149 newest_match = &entry;
150
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100151 if (!newest_match) return AUTH_TOKEN_NOT_FOUND;
Shawn Willden489dfe12015-03-17 10:13:27 -0600152
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100153 auto timeout = defaultOr(key_info.GetTagValue(TAG_AUTH_TIMEOUT), 0);
154
Shawn Willden489dfe12015-03-17 10:13:27 -0600155 time_t now = clock_function_();
156 if (static_cast<int64_t>(newest_match->time_received()) + timeout < static_cast<int64_t>(now))
157 return AUTH_TOKEN_EXPIRED;
158
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100159 if (key_info.GetTagValue(TAG_ALLOW_WHILE_ON_BODY).isOk()) {
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400160 if (static_cast<int64_t>(newest_match->time_received()) <
161 static_cast<int64_t>(last_off_body_)) {
162 return AUTH_TOKEN_EXPIRED;
163 }
164 }
165
Shawn Willden489dfe12015-03-17 10:13:27 -0600166 newest_match->UpdateLastUse(now);
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000167 *found = newest_match->token();
Shawn Willden489dfe12015-03-17 10:13:27 -0600168 return OK;
169}
170
171void AuthTokenTable::ExtractSids(const AuthorizationSet& key_info, std::vector<uint64_t>* sids) {
172 assert(sids);
173 for (auto& param : key_info)
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100174 if (param.tag == Tag::USER_SECURE_ID)
175 sids->push_back(authorizationValue(TAG_USER_SECURE_ID, param).value());
Shawn Willden489dfe12015-03-17 10:13:27 -0600176}
177
178void AuthTokenTable::RemoveEntriesSupersededBy(const Entry& entry) {
179 entries_.erase(remove_if(entries_, [&](Entry& e) { return entry.Supersedes(e); }),
180 entries_.end());
181}
182
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400183void AuthTokenTable::onDeviceOffBody() {
184 last_off_body_ = clock_function_();
185}
186
Chad Brubakerbbc76482015-04-16 15:16:44 -0700187void AuthTokenTable::Clear() {
188 entries_.clear();
189}
190
Shawn Willden489dfe12015-03-17 10:13:27 -0600191bool AuthTokenTable::IsSupersededBySomeEntry(const Entry& entry) {
192 return std::any_of(entries_.begin(), entries_.end(),
193 [&](Entry& e) { return e.Supersedes(entry); });
194}
195
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100196void AuthTokenTable::MarkCompleted(const uint64_t op_handle) {
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000197 auto found = find_if(entries_, [&](Entry& e) { return e.token()->challenge == op_handle; });
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100198 if (found == entries_.end()) return;
Shawn Willden489dfe12015-03-17 10:13:27 -0600199
200 assert(!IsSupersededBySomeEntry(*found));
201 found->mark_completed();
202
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100203 if (IsSupersededBySomeEntry(*found)) entries_.erase(found);
Shawn Willden489dfe12015-03-17 10:13:27 -0600204}
205
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000206AuthTokenTable::Entry::Entry(const HardwareAuthToken* token, time_t current_time)
207 : token_(token), time_received_(current_time), last_use_(current_time),
208 operation_completed_(token_->challenge == 0) {}
209
210uint32_t AuthTokenTable::Entry::timestamp_host_order() const {
211 return ntoh(token_->timestamp);
212}
213
214HardwareAuthenticatorType AuthTokenTable::Entry::authenticator_type() const {
215 HardwareAuthenticatorType result = static_cast<HardwareAuthenticatorType>(
216 ntoh(static_cast<uint32_t>(token_->authenticatorType)));
217 return result;
218}
Shawn Willden489dfe12015-03-17 10:13:27 -0600219
220bool AuthTokenTable::Entry::SatisfiesAuth(const std::vector<uint64_t>& sids,
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100221 HardwareAuthenticatorType auth_type) {
Shawn Willden489dfe12015-03-17 10:13:27 -0600222 for (auto sid : sids)
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000223 if ((sid == token_->authenticatorId) ||
224 (sid == token_->userId && (auth_type & authenticator_type()) != 0))
Shawn Willden489dfe12015-03-17 10:13:27 -0600225 return true;
226 return false;
227}
228
229void AuthTokenTable::Entry::UpdateLastUse(time_t time) {
230 this->last_use_ = time;
231}
232
233bool AuthTokenTable::Entry::Supersedes(const Entry& entry) const {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100234 if (!entry.completed()) return false;
Shawn Willden489dfe12015-03-17 10:13:27 -0600235
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000236 return (token_->userId == entry.token_->userId &&
237 token_->authenticatorType == entry.token_->authenticatorType &&
238 token_->authenticatorType == entry.token_->authenticatorType &&
239 timestamp_host_order() > entry.timestamp_host_order());
Shawn Willden489dfe12015-03-17 10:13:27 -0600240}
241
242} // namespace keymaster