[email protected] | 67f92bc3 | 2012-01-26 01:56:19 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/internal_auth.h" |
| 6 | |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 7 | #include <stddef.h> |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 10 | #include <algorithm> |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 11 | #include <limits> |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 12 | #include <memory> |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 13 | |
| 14 | #include "base/base64.h" |
Brett Wilson | 275a137 | 2017-09-01 20:27:54 | [diff] [blame] | 15 | #include "base/containers/circular_deque.h" |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 16 | #include "base/lazy_instance.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 17 | #include "base/macros.h" |
Peter Kasting | 1f01655 | 2019-09-09 22:20:55 | [diff] [blame] | 18 | #include "base/numerics/ranges.h" |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 19 | #include "base/rand_util.h" |
tripta.g | c49d42a | 2017-06-29 11:45:05 | [diff] [blame] | 20 | #include "base/stl_util.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 21 | #include "base/strings/string_number_conversions.h" |
[email protected] | 1988e1c | 2013-02-28 20:27:42 | [diff] [blame] | 22 | #include "base/strings/string_split.h" |
[email protected] | 9c7ddc9 | 2013-06-11 01:40:57 | [diff] [blame] | 23 | #include "base/strings/string_util.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 24 | #include "base/synchronization/lock.h" |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 25 | #include "base/threading/thread_checker.h" |
[email protected] | 0e49848 | 2013-06-28 01:53:43 | [diff] [blame] | 26 | #include "base/time/time.h" |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 27 | #include "base/values.h" |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 28 | #include "crypto/hmac.h" |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | typedef std::map<std::string, std::string> VarValueMap; |
| 33 | |
| 34 | // Size of a tick in microseconds. This determines upper bound for average |
| 35 | // number of passports generated per time unit. This bound equals to |
| 36 | // (kMicrosecondsPerSecond / TickUs) calls per second. |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 37 | const int64_t kTickUs = 10000; |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 38 | |
| 39 | // Verification window size in ticks; that means any passport expires in |
| 40 | // (kVerificationWindowTicks * TickUs / kMicrosecondsPerSecond) seconds. |
| 41 | const int kVerificationWindowTicks = 2000; |
| 42 | |
| 43 | // Generation window determines how well we are able to cope with bursts of |
| 44 | // GeneratePassport calls those exceed upper bound on average speed. |
| 45 | const int kGenerationWindowTicks = 20; |
| 46 | |
| 47 | // Makes no sense to compare other way round. |
mostynb | 3a46e0bf | 2014-12-23 09:02:43 | [diff] [blame] | 48 | static_assert(kGenerationWindowTicks <= kVerificationWindowTicks, |
| 49 | "generation window should not be larger than the verification window"); |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 50 | // We are not optimized for high value of kGenerationWindowTicks. |
mostynb | 3a46e0bf | 2014-12-23 09:02:43 | [diff] [blame] | 51 | static_assert(kGenerationWindowTicks < 30, |
| 52 | "generation window should not be too large"); |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 53 | |
| 54 | // Regenerate key after this number of ticks. |
| 55 | const int kKeyRegenerationSoftTicks = 500000; |
| 56 | // Reject passports if key has not been regenerated in that number of ticks. |
| 57 | const int kKeyRegenerationHardTicks = kKeyRegenerationSoftTicks * 2; |
| 58 | |
| 59 | // Limit for number of accepted var=value pairs. Feel free to bump this limit |
| 60 | // higher once needed. |
| 61 | const size_t kVarsLimit = 16; |
| 62 | |
| 63 | // Limit for length of caller-supplied strings. Feel free to bump this limit |
| 64 | // higher once needed. |
| 65 | const size_t kStringLengthLimit = 512; |
| 66 | |
| 67 | // Character used as a separator for construction of message to take HMAC of. |
| 68 | // It is critical to validate all caller-supplied data (used to construct |
| 69 | // message) to be clear of this separator because it could allow attacks. |
| 70 | const char kItemSeparator = '\n'; |
| 71 | |
| 72 | // Character used for var=value separation. |
| 73 | const char kVarValueSeparator = '='; |
| 74 | |
| 75 | const size_t kKeySizeInBytes = 128 / 8; |
[email protected] | 673266c4 | 2012-12-04 00:50:35 | [diff] [blame] | 76 | const size_t kHMACSizeInBytes = 256 / 8; |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 77 | |
| 78 | // Length of base64 string required to encode given number of raw octets. |
| 79 | #define BASE64_PER_RAW(X) (X > 0 ? ((X - 1) / 3 + 1) * 4 : 0) |
| 80 | |
| 81 | // Size of decimal string representing 64-bit tick. |
| 82 | const size_t kTickStringLength = 20; |
| 83 | |
| 84 | // A passport consists of 2 parts: HMAC and tick. |
| 85 | const size_t kPassportSize = |
| 86 | BASE64_PER_RAW(kHMACSizeInBytes) + kTickStringLength; |
| 87 | |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 88 | int64_t GetCurrentTick() { |
| 89 | int64_t tick = base::Time::Now().ToInternalValue() / kTickUs; |
| 90 | if (tick < kVerificationWindowTicks || tick < kKeyRegenerationHardTicks || |
| 91 | tick > std::numeric_limits<int64_t>::max() - kKeyRegenerationHardTicks) { |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 92 | return 0; |
| 93 | } |
| 94 | return tick; |
| 95 | } |
| 96 | |
| 97 | bool IsDomainSane(const std::string& domain) { |
| 98 | return !domain.empty() && |
| 99 | domain.size() <= kStringLengthLimit && |
[email protected] | 52796541 | 2014-05-07 14:38:26 | [diff] [blame] | 100 | base::IsStringUTF8(domain) && |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 101 | domain.find_first_of(kItemSeparator) == std::string::npos; |
| 102 | } |
| 103 | |
| 104 | bool IsVarSane(const std::string& var) { |
| 105 | static const char kAllowedChars[] = |
| 106 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 107 | "abcdefghijklmnopqrstuvwxyz" |
| 108 | "0123456789" |
| 109 | "_"; |
mostynb | 3a46e0bf | 2014-12-23 09:02:43 | [diff] [blame] | 110 | static_assert( |
| 111 | sizeof(kAllowedChars) == 26 + 26 + 10 + 1 + 1, "some mess with chars"); |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 112 | // We must not allow kItemSeparator in anything used as an input to construct |
| 113 | // message to sign. |
Jan Wilken Dörrie | 0aaef98 | 2019-06-06 18:36:22 | [diff] [blame] | 114 | DCHECK(!base::Contains(kAllowedChars, kItemSeparator)); |
| 115 | DCHECK(!base::Contains(kAllowedChars, kVarValueSeparator)); |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 116 | return !var.empty() && |
| 117 | var.size() <= kStringLengthLimit && |
[email protected] | 52796541 | 2014-05-07 14:38:26 | [diff] [blame] | 118 | base::IsStringASCII(var) && |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 119 | var.find_first_not_of(kAllowedChars) == std::string::npos && |
brettw | b341306 | 2015-06-24 00:39:02 | [diff] [blame] | 120 | !base::IsAsciiDigit(var[0]); |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | bool IsValueSane(const std::string& value) { |
| 124 | return value.size() <= kStringLengthLimit && |
[email protected] | 52796541 | 2014-05-07 14:38:26 | [diff] [blame] | 125 | base::IsStringUTF8(value) && |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 126 | value.find_first_of(kItemSeparator) == std::string::npos; |
| 127 | } |
| 128 | |
| 129 | bool IsVarValueMapSane(const VarValueMap& map) { |
| 130 | if (map.size() > kVarsLimit) |
| 131 | return false; |
jdoerrie | 2f1af51 | 2018-10-03 00:59:37 | [diff] [blame] | 132 | for (auto it = map.begin(); it != map.end(); ++it) { |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 133 | const std::string& var = it->first; |
| 134 | const std::string& value = it->second; |
| 135 | if (!IsVarSane(var) || !IsValueSane(value)) |
| 136 | return false; |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | void ConvertVarValueMapToBlob(const VarValueMap& map, std::string* out) { |
| 142 | out->clear(); |
| 143 | DCHECK(IsVarValueMapSane(map)); |
jdoerrie | 2f1af51 | 2018-10-03 00:59:37 | [diff] [blame] | 144 | for (auto it = map.begin(); it != map.end(); ++it) |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 145 | *out += it->first + kVarValueSeparator + it->second + kItemSeparator; |
| 146 | } |
| 147 | |
[email protected] | 08b14a5 | 2012-07-02 23:30:36 | [diff] [blame] | 148 | void CreatePassport(const std::string& domain, |
| 149 | const VarValueMap& map, |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 150 | int64_t tick, |
[email protected] | 08b14a5 | 2012-07-02 23:30:36 | [diff] [blame] | 151 | const crypto::HMAC* engine, |
| 152 | std::string* out) { |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 153 | DCHECK(engine); |
| 154 | DCHECK(out); |
| 155 | DCHECK(IsDomainSane(domain)); |
| 156 | DCHECK(IsVarValueMapSane(map)); |
| 157 | |
| 158 | out->clear(); |
| 159 | std::string result(kPassportSize, '0'); |
| 160 | |
| 161 | std::string blob; |
| 162 | blob = domain + kItemSeparator; |
| 163 | std::string tmp; |
| 164 | ConvertVarValueMapToBlob(map, &tmp); |
Raul Tambre | fff51b75 | 2019-02-04 13:09:47 | [diff] [blame] | 165 | blob += tmp + kItemSeparator + base::NumberToString(tick); |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 166 | |
| 167 | std::string hmac; |
| 168 | unsigned char* hmac_data = reinterpret_cast<unsigned char*>( |
Brett Wilson | e3c4d1a | 2015-07-07 23:38:09 | [diff] [blame] | 169 | base::WriteInto(&hmac, kHMACSizeInBytes + 1)); |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 170 | if (!engine->Sign(blob, hmac_data, kHMACSizeInBytes)) { |
| 171 | NOTREACHED(); |
| 172 | return; |
| 173 | } |
| 174 | std::string hmac_base64; |
[email protected] | 33fca12 | 2013-12-11 01:48:50 | [diff] [blame] | 175 | base::Base64Encode(hmac, &hmac_base64); |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 176 | if (hmac_base64.size() != BASE64_PER_RAW(kHMACSizeInBytes)) { |
| 177 | NOTREACHED(); |
| 178 | return; |
| 179 | } |
| 180 | DCHECK(hmac_base64.size() < result.size()); |
| 181 | std::copy(hmac_base64.begin(), hmac_base64.end(), result.begin()); |
| 182 | |
Raul Tambre | fff51b75 | 2019-02-04 13:09:47 | [diff] [blame] | 183 | std::string tick_decimal = base::NumberToString(tick); |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 184 | DCHECK(tick_decimal.size() <= kTickStringLength); |
| 185 | std::copy( |
| 186 | tick_decimal.begin(), |
| 187 | tick_decimal.end(), |
| 188 | result.begin() + kPassportSize - tick_decimal.size()); |
| 189 | |
| 190 | out->swap(result); |
| 191 | } |
| 192 | |
| 193 | } // namespace |
| 194 | |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 195 | class InternalAuthVerificationService { |
| 196 | public: |
| 197 | InternalAuthVerificationService() |
| 198 | : key_change_tick_(0), |
| 199 | dark_tick_(0) { |
| 200 | } |
| 201 | |
| 202 | bool VerifyPassport( |
| 203 | const std::string& passport, |
| 204 | const std::string& domain, |
| 205 | const VarValueMap& map) { |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 206 | int64_t current_tick = GetCurrentTick(); |
| 207 | int64_t tick = PreVerifyPassport(passport, domain, current_tick); |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 208 | if (tick == 0) |
| 209 | return false; |
| 210 | if (!IsVarValueMapSane(map)) |
| 211 | return false; |
| 212 | std::string reference_passport; |
| 213 | CreatePassport(domain, map, tick, engine_.get(), &reference_passport); |
| 214 | if (passport != reference_passport) { |
| 215 | // Consider old key. |
| 216 | if (key_change_tick_ + get_verification_window_ticks() < tick) { |
| 217 | return false; |
| 218 | } |
| 219 | if (old_key_.empty() || old_engine_ == NULL) |
| 220 | return false; |
| 221 | CreatePassport(domain, map, tick, old_engine_.get(), &reference_passport); |
| 222 | if (passport != reference_passport) |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | // Record used tick to prevent reuse. |
Brett Wilson | 275a137 | 2017-09-01 20:27:54 | [diff] [blame] | 227 | base::circular_deque<int64_t>::iterator it = |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 228 | std::lower_bound(used_ticks_.begin(), used_ticks_.end(), tick); |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 229 | DCHECK(it == used_ticks_.end() || *it != tick); |
| 230 | used_ticks_.insert(it, tick); |
| 231 | |
| 232 | // Consider pruning |used_ticks_|. |
| 233 | if (used_ticks_.size() > 50) { |
| 234 | dark_tick_ = std::max(dark_tick_, |
| 235 | current_tick - get_verification_window_ticks()); |
| 236 | used_ticks_.erase( |
| 237 | used_ticks_.begin(), |
| 238 | std::lower_bound(used_ticks_.begin(), used_ticks_.end(), |
| 239 | dark_tick_ + 1)); |
| 240 | } |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | void ChangeKey(const std::string& key) { |
| 245 | old_key_.swap(key_); |
| 246 | key_.clear(); |
| 247 | old_engine_.swap(engine_); |
| 248 | engine_.reset(NULL); |
| 249 | |
| 250 | if (key.size() != kKeySizeInBytes) |
| 251 | return; |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 252 | std::unique_ptr<crypto::HMAC> new_engine( |
[email protected] | 6df5b9e | 2011-07-30 05:18:01 | [diff] [blame] | 253 | new crypto::HMAC(crypto::HMAC::SHA256)); |
| 254 | if (!new_engine->Init(key)) |
| 255 | return; |
| 256 | engine_.swap(new_engine); |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 257 | key_ = key; |
| 258 | key_change_tick_ = GetCurrentTick(); |
| 259 | } |
| 260 | |
| 261 | private: |
| 262 | static int get_verification_window_ticks() { |
| 263 | return InternalAuthVerification::get_verification_window_ticks(); |
| 264 | } |
| 265 | |
| 266 | // Returns tick bound to given passport on success or zero on failure. |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 267 | int64_t PreVerifyPassport(const std::string& passport, |
| 268 | const std::string& domain, |
| 269 | int64_t current_tick) { |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 270 | if (passport.size() != kPassportSize || |
[email protected] | 52796541 | 2014-05-07 14:38:26 | [diff] [blame] | 271 | !base::IsStringASCII(passport) || |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 272 | !IsDomainSane(domain) || |
| 273 | current_tick <= dark_tick_ || |
| 274 | current_tick > key_change_tick_ + kKeyRegenerationHardTicks || |
| 275 | key_.empty() || |
| 276 | engine_ == NULL) { |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | // Passport consists of 2 parts: first hmac and then tick. |
| 281 | std::string tick_decimal = |
| 282 | passport.substr(BASE64_PER_RAW(kHMACSizeInBytes)); |
| 283 | DCHECK(tick_decimal.size() == kTickStringLength); |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 284 | int64_t tick = 0; |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 285 | if (!base::StringToInt64(tick_decimal, &tick) || |
| 286 | tick <= dark_tick_ || |
| 287 | tick > key_change_tick_ + kKeyRegenerationHardTicks || |
| 288 | tick < current_tick - get_verification_window_ticks() || |
| 289 | std::binary_search(used_ticks_.begin(), used_ticks_.end(), tick)) { |
| 290 | return 0; |
| 291 | } |
| 292 | return tick; |
| 293 | } |
| 294 | |
| 295 | // Current key. |
| 296 | std::string key_; |
| 297 | |
| 298 | // We keep previous key in order to be able to verify passports during |
| 299 | // regeneration time. Keys are regenerated on a regular basis. |
| 300 | std::string old_key_; |
| 301 | |
| 302 | // Corresponding HMAC engines. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 303 | std::unique_ptr<crypto::HMAC> engine_; |
| 304 | std::unique_ptr<crypto::HMAC> old_engine_; |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 305 | |
| 306 | // Tick at a time of recent key regeneration. |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 307 | int64_t key_change_tick_; |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 308 | |
| 309 | // Keeps track of ticks of successfully verified passports to prevent their |
| 310 | // reuse. Size of this container is kept reasonably low by purging outdated |
| 311 | // ticks. |
Brett Wilson | 275a137 | 2017-09-01 20:27:54 | [diff] [blame] | 312 | base::circular_deque<int64_t> used_ticks_; |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 313 | |
| 314 | // Some ticks before |dark_tick_| were purged from |used_ticks_| container. |
| 315 | // That means that we must not trust any tick less than or equal to dark tick. |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 316 | int64_t dark_tick_; |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 317 | |
| 318 | DISALLOW_COPY_AND_ASSIGN(InternalAuthVerificationService); |
| 319 | }; |
| 320 | |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 321 | namespace { |
| 322 | |
cm.sanchi | 72edd19 | 2017-10-30 09:37:22 | [diff] [blame] | 323 | static base::LazyInstance<InternalAuthVerificationService>::DestructorAtExit |
| 324 | g_verification_service = LAZY_INSTANCE_INITIALIZER; |
[email protected] | 67f92bc3 | 2012-01-26 01:56:19 | [diff] [blame] | 325 | static base::LazyInstance<base::Lock>::Leaky |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 326 | g_verification_service_lock = LAZY_INSTANCE_INITIALIZER; |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 327 | |
| 328 | } // namespace |
| 329 | |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 330 | class InternalAuthGenerationService : public base::ThreadChecker { |
| 331 | public: |
| 332 | InternalAuthGenerationService() : key_regeneration_tick_(0) { |
| 333 | GenerateNewKey(); |
| 334 | } |
| 335 | |
| 336 | void GenerateNewKey() { |
| 337 | DCHECK(CalledOnValidThread()); |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 338 | std::unique_ptr<crypto::HMAC> new_engine( |
| 339 | new crypto::HMAC(crypto::HMAC::SHA256)); |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 340 | std::string key = base::RandBytesAsString(kKeySizeInBytes); |
[email protected] | 6df5b9e | 2011-07-30 05:18:01 | [diff] [blame] | 341 | if (!new_engine->Init(key)) |
| 342 | return; |
| 343 | engine_.swap(new_engine); |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 344 | key_regeneration_tick_ = GetCurrentTick(); |
| 345 | g_verification_service.Get().ChangeKey(key); |
| 346 | std::fill(key.begin(), key.end(), 0); |
| 347 | } |
| 348 | |
| 349 | // Returns zero on failure. |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 350 | int64_t GetUnusedTick(const std::string& domain) { |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 351 | DCHECK(CalledOnValidThread()); |
| 352 | if (engine_ == NULL) { |
| 353 | NOTREACHED(); |
| 354 | return 0; |
| 355 | } |
| 356 | if (!IsDomainSane(domain)) |
| 357 | return 0; |
| 358 | |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 359 | int64_t current_tick = GetCurrentTick(); |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 360 | if (!used_ticks_.empty() && used_ticks_.back() > current_tick) |
| 361 | current_tick = used_ticks_.back(); |
[email protected] | e234b11 | 2011-09-13 10:10:29 | [diff] [blame] | 362 | for (bool first_iteration = true;; first_iteration = false) { |
| 363 | if (current_tick < key_regeneration_tick_ + kKeyRegenerationHardTicks) |
| 364 | break; |
| 365 | if (!first_iteration) |
| 366 | return 0; |
| 367 | GenerateNewKey(); |
| 368 | } |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 369 | |
| 370 | // Forget outdated ticks if any. |
| 371 | used_ticks_.erase( |
| 372 | used_ticks_.begin(), |
| 373 | std::lower_bound(used_ticks_.begin(), used_ticks_.end(), |
| 374 | current_tick - kGenerationWindowTicks + 1)); |
| 375 | DCHECK(used_ticks_.size() <= kGenerationWindowTicks + 0u); |
| 376 | if (used_ticks_.size() >= kGenerationWindowTicks + 0u) { |
| 377 | // Average speed of GeneratePassport calls exceeds limit. |
| 378 | return 0; |
| 379 | } |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 380 | for (int64_t tick = current_tick; |
| 381 | tick > current_tick - kGenerationWindowTicks; --tick) { |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 382 | int idx = static_cast<int>(used_ticks_.size()) - |
| 383 | static_cast<int>(current_tick - tick + 1); |
| 384 | if (idx < 0 || used_ticks_[idx] != tick) { |
Jan Wilken Dörrie | 0aaef98 | 2019-06-06 18:36:22 | [diff] [blame] | 385 | DCHECK(!base::Contains(used_ticks_, tick)); |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 386 | return tick; |
| 387 | } |
| 388 | } |
| 389 | NOTREACHED(); |
| 390 | return 0; |
| 391 | } |
| 392 | |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 393 | std::string GeneratePassport(const std::string& domain, |
| 394 | const VarValueMap& map, |
| 395 | int64_t tick) { |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 396 | DCHECK(CalledOnValidThread()); |
| 397 | if (tick == 0) { |
| 398 | tick = GetUnusedTick(domain); |
| 399 | if (tick == 0) |
| 400 | return std::string(); |
| 401 | } |
| 402 | if (!IsVarValueMapSane(map)) |
| 403 | return std::string(); |
| 404 | |
| 405 | std::string result; |
| 406 | CreatePassport(domain, map, tick, engine_.get(), &result); |
| 407 | used_ticks_.insert( |
| 408 | std::lower_bound(used_ticks_.begin(), used_ticks_.end(), tick), tick); |
| 409 | return result; |
| 410 | } |
| 411 | |
| 412 | private: |
| 413 | static int get_verification_window_ticks() { |
| 414 | return InternalAuthVerification::get_verification_window_ticks(); |
| 415 | } |
| 416 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 417 | std::unique_ptr<crypto::HMAC> engine_; |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 418 | int64_t key_regeneration_tick_; |
Brett Wilson | 275a137 | 2017-09-01 20:27:54 | [diff] [blame] | 419 | base::circular_deque<int64_t> used_ticks_; |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 420 | |
| 421 | DISALLOW_COPY_AND_ASSIGN(InternalAuthGenerationService); |
| 422 | }; |
| 423 | |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 424 | namespace { |
| 425 | |
cm.sanchi | 72edd19 | 2017-10-30 09:37:22 | [diff] [blame] | 426 | static base::LazyInstance<InternalAuthGenerationService>::DestructorAtExit |
| 427 | g_generation_service = LAZY_INSTANCE_INITIALIZER; |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 428 | |
| 429 | } // namespace |
| 430 | |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 431 | // static |
| 432 | bool InternalAuthVerification::VerifyPassport( |
| 433 | const std::string& passport, |
| 434 | const std::string& domain, |
| 435 | const VarValueMap& var_value_map) { |
| 436 | base::AutoLock alk(g_verification_service_lock.Get()); |
| 437 | return g_verification_service.Get().VerifyPassport( |
| 438 | passport, domain, var_value_map); |
| 439 | } |
| 440 | |
| 441 | // static |
| 442 | void InternalAuthVerification::ChangeKey(const std::string& key) { |
| 443 | base::AutoLock alk(g_verification_service_lock.Get()); |
| 444 | g_verification_service.Get().ChangeKey(key); |
brettw | b341306 | 2015-06-24 00:39:02 | [diff] [blame] | 445 | } |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 446 | |
| 447 | // static |
| 448 | int InternalAuthVerification::get_verification_window_ticks() { |
| 449 | int candidate = kVerificationWindowTicks; |
| 450 | if (verification_window_seconds_ > 0) |
| 451 | candidate = verification_window_seconds_ * |
| 452 | base::Time::kMicrosecondsPerSecond / kTickUs; |
Peter Kasting | 1f01655 | 2019-09-09 22:20:55 | [diff] [blame] | 453 | return base::ClampToRange(candidate, 1, kVerificationWindowTicks); |
[email protected] | c6e584c | 2011-05-18 11:58:44 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | int InternalAuthVerification::verification_window_seconds_ = 0; |
| 457 | |
| 458 | // static |
| 459 | std::string InternalAuthGeneration::GeneratePassport( |
| 460 | const std::string& domain, const VarValueMap& var_value_map) { |
| 461 | return g_generation_service.Get().GeneratePassport(domain, var_value_map, 0); |
| 462 | } |
| 463 | |
| 464 | // static |
| 465 | void InternalAuthGeneration::GenerateNewKey() { |
| 466 | g_generation_service.Get().GenerateNewKey(); |
| 467 | } |
| 468 | |