blob: 328121a71b1403901b7b27d1e18980a1056db6ed [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 Willden489dfe12015-03-17 10:13:27 -060028//
29// Some trivial template wrappers around std algorithms, so they take containers not ranges.
30//
31template <typename Container, typename Predicate>
32typename Container::iterator find_if(Container& container, Predicate pred) {
33 return std::find_if(container.begin(), container.end(), pred);
34}
35
36template <typename Container, typename Predicate>
37typename Container::iterator remove_if(Container& container, Predicate pred) {
38 return std::remove_if(container.begin(), container.end(), pred);
39}
40
41template <typename Container> typename Container::iterator min_element(Container& container) {
42 return std::min_element(container.begin(), container.end());
43}
44
45time_t clock_gettime_raw() {
46 struct timespec time;
47 clock_gettime(CLOCK_MONOTONIC_RAW, &time);
48 return time.tv_sec;
49}
50
Shawn Willden76f21b22017-02-17 12:29:42 -070051void AuthTokenTable::AddAuthenticationToken(hidl_vec<uint8_t> token, HardwareAuthTokenInfo info) {
52 Entry new_entry(std::move(token), std::move(info), clock_function_());
Shawn Willden489dfe12015-03-17 10:13:27 -060053 RemoveEntriesSupersededBy(new_entry);
54 if (entries_.size() >= max_entries_) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010055 ALOGW("Auth token table filled up; replacing oldest entry");
Shawn Willden489dfe12015-03-17 10:13:27 -060056 *min_element(entries_) = std::move(new_entry);
57 } else {
58 entries_.push_back(std::move(new_entry));
59 }
60}
61
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010062inline bool is_secret_key_operation(Algorithm algorithm, KeyPurpose purpose) {
63 if ((algorithm != Algorithm::RSA && algorithm != Algorithm::EC))
Shawn Willdenb2ffa422015-06-17 12:18:55 -060064 return true;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010065 if (purpose == KeyPurpose::SIGN || purpose == KeyPurpose::DECRYPT)
Shawn Willdenb2ffa422015-06-17 12:18:55 -060066 return true;
67 return false;
Shawn Willden489dfe12015-03-17 10:13:27 -060068}
69
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010070inline bool KeyRequiresAuthentication(const AuthorizationSet& key_info, KeyPurpose purpose) {
71 auto algorithm = defaultOr(key_info.GetTagValue(TAG_ALGORITHM), Algorithm::AES);
72 return is_secret_key_operation(algorithm, purpose) &&
73 key_info.find(Tag::NO_AUTH_REQUIRED) == -1;
Shawn Willdenb2ffa422015-06-17 12:18:55 -060074}
75
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010076inline bool KeyRequiresAuthPerOperation(const AuthorizationSet& key_info, KeyPurpose purpose) {
77 auto algorithm = defaultOr(key_info.GetTagValue(TAG_ALGORITHM), Algorithm::AES);
78 return is_secret_key_operation(algorithm, purpose) && key_info.find(Tag::AUTH_TIMEOUT) == -1;
Shawn Willden489dfe12015-03-17 10:13:27 -060079}
80
81AuthTokenTable::Error AuthTokenTable::FindAuthorization(const AuthorizationSet& key_info,
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010082 KeyPurpose purpose, uint64_t op_handle,
Shawn Willden76f21b22017-02-17 12:29:42 -070083 const hidl_vec<uint8_t>** found) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010084 if (!KeyRequiresAuthentication(key_info, purpose)) return AUTH_NOT_REQUIRED;
Shawn Willden489dfe12015-03-17 10:13:27 -060085
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010086 auto auth_type =
87 defaultOr(key_info.GetTagValue(TAG_USER_AUTH_TYPE), HardwareAuthenticatorType::NONE);
Shawn Willden489dfe12015-03-17 10:13:27 -060088
89 std::vector<uint64_t> key_sids;
90 ExtractSids(key_info, &key_sids);
91
Shawn Willdenb2ffa422015-06-17 12:18:55 -060092 if (KeyRequiresAuthPerOperation(key_info, purpose))
Shawn Willden489dfe12015-03-17 10:13:27 -060093 return FindAuthPerOpAuthorization(key_sids, auth_type, op_handle, found);
94 else
95 return FindTimedAuthorization(key_sids, auth_type, key_info, found);
96}
97
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010098AuthTokenTable::Error
99AuthTokenTable::FindAuthPerOpAuthorization(const std::vector<uint64_t>& sids,
100 HardwareAuthenticatorType auth_type, uint64_t op_handle,
Shawn Willden76f21b22017-02-17 12:29:42 -0700101 const hidl_vec<uint8_t>** found) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100102 if (op_handle == 0) return OP_HANDLE_REQUIRED;
Shawn Willden489dfe12015-03-17 10:13:27 -0600103
104 auto matching_op = find_if(
Shawn Willden76f21b22017-02-17 12:29:42 -0700105 entries_, [&](Entry& e) { return e.tokenInfo().challenge == op_handle && !e.completed(); });
Shawn Willden489dfe12015-03-17 10:13:27 -0600106
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100107 if (matching_op == entries_.end()) return AUTH_TOKEN_NOT_FOUND;
Shawn Willden489dfe12015-03-17 10:13:27 -0600108
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100109 if (!matching_op->SatisfiesAuth(sids, auth_type)) return AUTH_TOKEN_WRONG_SID;
Shawn Willden489dfe12015-03-17 10:13:27 -0600110
Shawn Willden76f21b22017-02-17 12:29:42 -0700111 *found = &matching_op->token();
Shawn Willden489dfe12015-03-17 10:13:27 -0600112 return OK;
113}
114
115AuthTokenTable::Error AuthTokenTable::FindTimedAuthorization(const std::vector<uint64_t>& sids,
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100116 HardwareAuthenticatorType auth_type,
Shawn Willden489dfe12015-03-17 10:13:27 -0600117 const AuthorizationSet& key_info,
Shawn Willden76f21b22017-02-17 12:29:42 -0700118 const hidl_vec<uint8_t>** found) {
Shawn Willden489dfe12015-03-17 10:13:27 -0600119 Entry* newest_match = NULL;
120 for (auto& entry : entries_)
121 if (entry.SatisfiesAuth(sids, auth_type) && entry.is_newer_than(newest_match))
122 newest_match = &entry;
123
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100124 if (!newest_match) return AUTH_TOKEN_NOT_FOUND;
Shawn Willden489dfe12015-03-17 10:13:27 -0600125
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100126 auto timeout = defaultOr(key_info.GetTagValue(TAG_AUTH_TIMEOUT), 0);
127
Shawn Willden489dfe12015-03-17 10:13:27 -0600128 time_t now = clock_function_();
129 if (static_cast<int64_t>(newest_match->time_received()) + timeout < static_cast<int64_t>(now))
130 return AUTH_TOKEN_EXPIRED;
131
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100132 if (key_info.GetTagValue(TAG_ALLOW_WHILE_ON_BODY).isOk()) {
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400133 if (static_cast<int64_t>(newest_match->time_received()) <
134 static_cast<int64_t>(last_off_body_)) {
135 return AUTH_TOKEN_EXPIRED;
136 }
137 }
138
Shawn Willden489dfe12015-03-17 10:13:27 -0600139 newest_match->UpdateLastUse(now);
Shawn Willden76f21b22017-02-17 12:29:42 -0700140 *found = &newest_match->token();
Shawn Willden489dfe12015-03-17 10:13:27 -0600141 return OK;
142}
143
144void AuthTokenTable::ExtractSids(const AuthorizationSet& key_info, std::vector<uint64_t>* sids) {
145 assert(sids);
146 for (auto& param : key_info)
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100147 if (param.tag == Tag::USER_SECURE_ID)
148 sids->push_back(authorizationValue(TAG_USER_SECURE_ID, param).value());
Shawn Willden489dfe12015-03-17 10:13:27 -0600149}
150
151void AuthTokenTable::RemoveEntriesSupersededBy(const Entry& entry) {
152 entries_.erase(remove_if(entries_, [&](Entry& e) { return entry.Supersedes(e); }),
153 entries_.end());
154}
155
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400156void AuthTokenTable::onDeviceOffBody() {
157 last_off_body_ = clock_function_();
158}
159
Chad Brubakerbbc76482015-04-16 15:16:44 -0700160void AuthTokenTable::Clear() {
161 entries_.clear();
162}
163
Shawn Willden489dfe12015-03-17 10:13:27 -0600164bool AuthTokenTable::IsSupersededBySomeEntry(const Entry& entry) {
165 return std::any_of(entries_.begin(), entries_.end(),
166 [&](Entry& e) { return e.Supersedes(entry); });
167}
168
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100169void AuthTokenTable::MarkCompleted(const uint64_t op_handle) {
Shawn Willden76f21b22017-02-17 12:29:42 -0700170 auto found = find_if(entries_, [&](Entry& e) { return e.tokenInfo().challenge == op_handle; });
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100171 if (found == entries_.end()) return;
Shawn Willden489dfe12015-03-17 10:13:27 -0600172
173 assert(!IsSupersededBySomeEntry(*found));
174 found->mark_completed();
175
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100176 if (IsSupersededBySomeEntry(*found)) entries_.erase(found);
Shawn Willden489dfe12015-03-17 10:13:27 -0600177}
178
Shawn Willden76f21b22017-02-17 12:29:42 -0700179AuthTokenTable::Entry::Entry(hidl_vec<uint8_t>&& token, HardwareAuthTokenInfo&& tokenInfo,
180 time_t current_time)
181 : token_(std::move(token)), tokenInfo_(std::move(tokenInfo)), time_received_(current_time),
182 last_use_(current_time), operation_completed_(tokenInfo_.challenge == 0) {}
Shawn Willden489dfe12015-03-17 10:13:27 -0600183
184bool AuthTokenTable::Entry::SatisfiesAuth(const std::vector<uint64_t>& sids,
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100185 HardwareAuthenticatorType auth_type) {
Shawn Willden489dfe12015-03-17 10:13:27 -0600186 for (auto sid : sids)
Shawn Willden76f21b22017-02-17 12:29:42 -0700187 if ((sid == tokenInfo_.authenticatorId) ||
188 (sid == tokenInfo_.userId && (auth_type & tokenInfo_.authenticatorType) != 0))
Shawn Willden489dfe12015-03-17 10:13:27 -0600189 return true;
190 return false;
191}
192
193void AuthTokenTable::Entry::UpdateLastUse(time_t time) {
194 this->last_use_ = time;
195}
196
197bool AuthTokenTable::Entry::Supersedes(const Entry& entry) const {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100198 if (!entry.completed()) return false;
Shawn Willden489dfe12015-03-17 10:13:27 -0600199
Shawn Willden76f21b22017-02-17 12:29:42 -0700200 return (tokenInfo_.userId == entry.tokenInfo_.userId &&
201 tokenInfo_.authenticatorType == entry.tokenInfo_.authenticatorType &&
202 tokenInfo_.timestamp > entry.tokenInfo_.timestamp);
Shawn Willden489dfe12015-03-17 10:13:27 -0600203}
204
205} // namespace keymaster