Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 1 | /* |
| 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 Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 24 | #include <cutils/log.h> |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 25 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 26 | namespace keystore { |
| 27 | |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 28 | // |
| 29 | // Some trivial template wrappers around std algorithms, so they take containers not ranges. |
| 30 | // |
| 31 | template <typename Container, typename Predicate> |
| 32 | typename Container::iterator find_if(Container& container, Predicate pred) { |
| 33 | return std::find_if(container.begin(), container.end(), pred); |
| 34 | } |
| 35 | |
| 36 | template <typename Container, typename Predicate> |
| 37 | typename Container::iterator remove_if(Container& container, Predicate pred) { |
| 38 | return std::remove_if(container.begin(), container.end(), pred); |
| 39 | } |
| 40 | |
| 41 | template <typename Container> typename Container::iterator min_element(Container& container) { |
| 42 | return std::min_element(container.begin(), container.end()); |
| 43 | } |
| 44 | |
| 45 | time_t clock_gettime_raw() { |
| 46 | struct timespec time; |
| 47 | clock_gettime(CLOCK_MONOTONIC_RAW, &time); |
| 48 | return time.tv_sec; |
| 49 | } |
| 50 | |
Shawn Willden | 76f21b2 | 2017-02-17 12:29:42 -0700 | [diff] [blame^] | 51 | void AuthTokenTable::AddAuthenticationToken(hidl_vec<uint8_t> token, HardwareAuthTokenInfo info) { |
| 52 | Entry new_entry(std::move(token), std::move(info), clock_function_()); |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 53 | RemoveEntriesSupersededBy(new_entry); |
| 54 | if (entries_.size() >= max_entries_) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 55 | ALOGW("Auth token table filled up; replacing oldest entry"); |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 56 | *min_element(entries_) = std::move(new_entry); |
| 57 | } else { |
| 58 | entries_.push_back(std::move(new_entry)); |
| 59 | } |
| 60 | } |
| 61 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 62 | inline bool is_secret_key_operation(Algorithm algorithm, KeyPurpose purpose) { |
| 63 | if ((algorithm != Algorithm::RSA && algorithm != Algorithm::EC)) |
Shawn Willden | b2ffa42 | 2015-06-17 12:18:55 -0600 | [diff] [blame] | 64 | return true; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 65 | if (purpose == KeyPurpose::SIGN || purpose == KeyPurpose::DECRYPT) |
Shawn Willden | b2ffa42 | 2015-06-17 12:18:55 -0600 | [diff] [blame] | 66 | return true; |
| 67 | return false; |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 68 | } |
| 69 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 70 | inline 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 Willden | b2ffa42 | 2015-06-17 12:18:55 -0600 | [diff] [blame] | 74 | } |
| 75 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 76 | inline 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 Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | AuthTokenTable::Error AuthTokenTable::FindAuthorization(const AuthorizationSet& key_info, |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 82 | KeyPurpose purpose, uint64_t op_handle, |
Shawn Willden | 76f21b2 | 2017-02-17 12:29:42 -0700 | [diff] [blame^] | 83 | const hidl_vec<uint8_t>** found) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 84 | if (!KeyRequiresAuthentication(key_info, purpose)) return AUTH_NOT_REQUIRED; |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 85 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 86 | auto auth_type = |
| 87 | defaultOr(key_info.GetTagValue(TAG_USER_AUTH_TYPE), HardwareAuthenticatorType::NONE); |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 88 | |
| 89 | std::vector<uint64_t> key_sids; |
| 90 | ExtractSids(key_info, &key_sids); |
| 91 | |
Shawn Willden | b2ffa42 | 2015-06-17 12:18:55 -0600 | [diff] [blame] | 92 | if (KeyRequiresAuthPerOperation(key_info, purpose)) |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 93 | return FindAuthPerOpAuthorization(key_sids, auth_type, op_handle, found); |
| 94 | else |
| 95 | return FindTimedAuthorization(key_sids, auth_type, key_info, found); |
| 96 | } |
| 97 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 98 | AuthTokenTable::Error |
| 99 | AuthTokenTable::FindAuthPerOpAuthorization(const std::vector<uint64_t>& sids, |
| 100 | HardwareAuthenticatorType auth_type, uint64_t op_handle, |
Shawn Willden | 76f21b2 | 2017-02-17 12:29:42 -0700 | [diff] [blame^] | 101 | const hidl_vec<uint8_t>** found) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 102 | if (op_handle == 0) return OP_HANDLE_REQUIRED; |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 103 | |
| 104 | auto matching_op = find_if( |
Shawn Willden | 76f21b2 | 2017-02-17 12:29:42 -0700 | [diff] [blame^] | 105 | entries_, [&](Entry& e) { return e.tokenInfo().challenge == op_handle && !e.completed(); }); |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 106 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 107 | if (matching_op == entries_.end()) return AUTH_TOKEN_NOT_FOUND; |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 108 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 109 | if (!matching_op->SatisfiesAuth(sids, auth_type)) return AUTH_TOKEN_WRONG_SID; |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 110 | |
Shawn Willden | 76f21b2 | 2017-02-17 12:29:42 -0700 | [diff] [blame^] | 111 | *found = &matching_op->token(); |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 112 | return OK; |
| 113 | } |
| 114 | |
| 115 | AuthTokenTable::Error AuthTokenTable::FindTimedAuthorization(const std::vector<uint64_t>& sids, |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 116 | HardwareAuthenticatorType auth_type, |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 117 | const AuthorizationSet& key_info, |
Shawn Willden | 76f21b2 | 2017-02-17 12:29:42 -0700 | [diff] [blame^] | 118 | const hidl_vec<uint8_t>** found) { |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 119 | 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 Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 124 | if (!newest_match) return AUTH_TOKEN_NOT_FOUND; |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 125 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 126 | auto timeout = defaultOr(key_info.GetTagValue(TAG_AUTH_TIMEOUT), 0); |
| 127 | |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 128 | 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 Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 132 | if (key_info.GetTagValue(TAG_ALLOW_WHILE_ON_BODY).isOk()) { |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 133 | 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 Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 139 | newest_match->UpdateLastUse(now); |
Shawn Willden | 76f21b2 | 2017-02-17 12:29:42 -0700 | [diff] [blame^] | 140 | *found = &newest_match->token(); |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 141 | return OK; |
| 142 | } |
| 143 | |
| 144 | void AuthTokenTable::ExtractSids(const AuthorizationSet& key_info, std::vector<uint64_t>* sids) { |
| 145 | assert(sids); |
| 146 | for (auto& param : key_info) |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 147 | if (param.tag == Tag::USER_SECURE_ID) |
| 148 | sids->push_back(authorizationValue(TAG_USER_SECURE_ID, param).value()); |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void AuthTokenTable::RemoveEntriesSupersededBy(const Entry& entry) { |
| 152 | entries_.erase(remove_if(entries_, [&](Entry& e) { return entry.Supersedes(e); }), |
| 153 | entries_.end()); |
| 154 | } |
| 155 | |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 156 | void AuthTokenTable::onDeviceOffBody() { |
| 157 | last_off_body_ = clock_function_(); |
| 158 | } |
| 159 | |
Chad Brubaker | bbc7648 | 2015-04-16 15:16:44 -0700 | [diff] [blame] | 160 | void AuthTokenTable::Clear() { |
| 161 | entries_.clear(); |
| 162 | } |
| 163 | |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 164 | bool AuthTokenTable::IsSupersededBySomeEntry(const Entry& entry) { |
| 165 | return std::any_of(entries_.begin(), entries_.end(), |
| 166 | [&](Entry& e) { return e.Supersedes(entry); }); |
| 167 | } |
| 168 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 169 | void AuthTokenTable::MarkCompleted(const uint64_t op_handle) { |
Shawn Willden | 76f21b2 | 2017-02-17 12:29:42 -0700 | [diff] [blame^] | 170 | auto found = find_if(entries_, [&](Entry& e) { return e.tokenInfo().challenge == op_handle; }); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 171 | if (found == entries_.end()) return; |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 172 | |
| 173 | assert(!IsSupersededBySomeEntry(*found)); |
| 174 | found->mark_completed(); |
| 175 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 176 | if (IsSupersededBySomeEntry(*found)) entries_.erase(found); |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 177 | } |
| 178 | |
Shawn Willden | 76f21b2 | 2017-02-17 12:29:42 -0700 | [diff] [blame^] | 179 | AuthTokenTable::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 Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 183 | |
| 184 | bool AuthTokenTable::Entry::SatisfiesAuth(const std::vector<uint64_t>& sids, |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 185 | HardwareAuthenticatorType auth_type) { |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 186 | for (auto sid : sids) |
Shawn Willden | 76f21b2 | 2017-02-17 12:29:42 -0700 | [diff] [blame^] | 187 | if ((sid == tokenInfo_.authenticatorId) || |
| 188 | (sid == tokenInfo_.userId && (auth_type & tokenInfo_.authenticatorType) != 0)) |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 189 | return true; |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | void AuthTokenTable::Entry::UpdateLastUse(time_t time) { |
| 194 | this->last_use_ = time; |
| 195 | } |
| 196 | |
| 197 | bool AuthTokenTable::Entry::Supersedes(const Entry& entry) const { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 198 | if (!entry.completed()) return false; |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 199 | |
Shawn Willden | 76f21b2 | 2017-02-17 12:29:42 -0700 | [diff] [blame^] | 200 | return (tokenInfo_.userId == entry.tokenInfo_.userId && |
| 201 | tokenInfo_.authenticatorType == entry.tokenInfo_.authenticatorType && |
| 202 | tokenInfo_.timestamp > entry.tokenInfo_.timestamp); |
Shawn Willden | 489dfe1 | 2015-03-17 10:13:27 -0600 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | } // namespace keymaster |