Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage.h" | 5 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "chrome/browser/chromeos/login/quick_unlock/quick_unlock_utils.h" | 9 #include "chrome/browser/chromeos/login/quick_unlock/quick_unlock_utils.h" |
| 10 #include "chrome/common/pref_names.h" | 10 #include "chrome/common/pref_names.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 return salt; | 30 return salt; |
| 31 } | 31 } |
| 32 | 32 |
| 33 // Computes the hash for |pin| and |salt|. | 33 // Computes the hash for |pin| and |salt|. |
| 34 std::string ComputeSecret(const std::string& pin, const std::string& salt) { | 34 std::string ComputeSecret(const std::string& pin, const std::string& salt) { |
| 35 Key key(pin); | 35 Key key(pin); |
| 36 key.Transform(Key::KEY_TYPE_SALTED_PBKDF2_AES256_1234, salt); | 36 key.Transform(Key::KEY_TYPE_SALTED_PBKDF2_AES256_1234, salt); |
| 37 return key.GetSecret(); | 37 return key.GetSecret(); |
| 38 } | 38 } |
| 39 | 39 |
| 40 base::TimeDelta QuickUnlockPasswordConfirmationFrequencyFrequencyToTimeDelta( | |
|
pastarmovj
2016/11/04 10:33:03
nit: Do you need the word Frequency twice in this
sammiequon
2016/11/06 17:52:52
Done.
| |
| 41 QuickUnlockPasswordConfirmationFrequency frequency) { | |
| 42 switch (frequency) { | |
| 43 case QuickUnlockPasswordConfirmationFrequency::SIX_HOURS: | |
| 44 return base::TimeDelta::FromHours(6); | |
| 45 case QuickUnlockPasswordConfirmationFrequency::TWELVE_HOURS: | |
| 46 return base::TimeDelta::FromHours(12); | |
| 47 case QuickUnlockPasswordConfirmationFrequency::DAY: | |
| 48 return base::TimeDelta::FromDays(1); | |
| 49 case QuickUnlockPasswordConfirmationFrequency::WEEK: | |
| 50 return base::TimeDelta::FromDays(7); | |
| 51 } | |
| 52 NOTREACHED(); | |
| 53 return base::TimeDelta(); | |
| 54 } | |
| 55 | |
| 40 } // namespace | 56 } // namespace |
| 41 | 57 |
| 42 // static | 58 // static |
| 43 const base::TimeDelta PinStorage::kStrongAuthTimeout = | |
| 44 base::TimeDelta::FromHours(24); | |
| 45 | |
| 46 // static | |
| 47 void PinStorage::RegisterProfilePrefs( | 59 void PinStorage::RegisterProfilePrefs( |
| 48 user_prefs::PrefRegistrySyncable* registry) { | 60 user_prefs::PrefRegistrySyncable* registry) { |
| 49 registry->RegisterStringPref(prefs::kQuickUnlockPinSalt, "", | 61 registry->RegisterStringPref(prefs::kQuickUnlockPinSalt, "", |
| 50 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 62 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 51 registry->RegisterStringPref(prefs::kQuickUnlockPinSecret, "", | 63 registry->RegisterStringPref(prefs::kQuickUnlockPinSecret, "", |
| 52 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 64 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 53 } | 65 } |
| 54 | 66 |
| 55 PinStorage::PinStorage(PrefService* pref_service) | 67 PinStorage::PinStorage(PrefService* pref_service) |
| 56 : pref_service_(pref_service) {} | 68 : pref_service_(pref_service) {} |
| 57 | 69 |
| 58 PinStorage::~PinStorage() {} | 70 PinStorage::~PinStorage() {} |
| 59 | 71 |
| 60 void PinStorage::MarkStrongAuth() { | 72 void PinStorage::MarkStrongAuth() { |
| 61 last_strong_auth_ = base::Time::Now(); | 73 last_strong_auth_ = base::Time::Now(); |
| 62 ResetUnlockAttemptCount(); | 74 ResetUnlockAttemptCount(); |
| 63 } | 75 } |
| 64 | 76 |
| 65 bool PinStorage::HasStrongAuth() const { | 77 bool PinStorage::HasStrongAuth() const { |
| 66 return !last_strong_auth_.is_null(); | 78 if (last_strong_auth_.is_null()) |
| 79 return false; | |
| 80 | |
| 81 QuickUnlockPasswordConfirmationFrequency strong_auth_interval = | |
| 82 static_cast<QuickUnlockPasswordConfirmationFrequency>( | |
| 83 pref_service_->GetInteger(prefs::kQuickUnlockTimeout)); | |
| 84 base::TimeDelta strong_auth_timeout = | |
| 85 QuickUnlockPasswordConfirmationFrequencyFrequencyToTimeDelta( | |
| 86 strong_auth_interval); | |
| 87 | |
| 88 return TimeSinceLastStrongAuth() < strong_auth_timeout; | |
| 67 } | 89 } |
| 68 | 90 |
| 69 base::TimeDelta PinStorage::TimeSinceLastStrongAuth() const { | 91 base::TimeDelta PinStorage::TimeSinceLastStrongAuth() const { |
| 70 DCHECK(!last_strong_auth_.is_null()); | 92 DCHECK(!last_strong_auth_.is_null()); |
| 71 return base::Time::Now() - last_strong_auth_; | 93 return base::Time::Now() - last_strong_auth_; |
| 72 } | 94 } |
| 73 | 95 |
| 74 void PinStorage::AddUnlockAttempt() { | 96 void PinStorage::AddUnlockAttempt() { |
| 75 ++unlock_attempt_count_; | 97 ++unlock_attempt_count_; |
| 76 } | 98 } |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 100 return pref_service_->GetString(prefs::kQuickUnlockPinSalt); | 122 return pref_service_->GetString(prefs::kQuickUnlockPinSalt); |
| 101 } | 123 } |
| 102 | 124 |
| 103 std::string PinStorage::PinSecret() const { | 125 std::string PinStorage::PinSecret() const { |
| 104 return pref_service_->GetString(prefs::kQuickUnlockPinSecret); | 126 return pref_service_->GetString(prefs::kQuickUnlockPinSecret); |
| 105 } | 127 } |
| 106 | 128 |
| 107 bool PinStorage::IsPinAuthenticationAvailable() const { | 129 bool PinStorage::IsPinAuthenticationAvailable() const { |
| 108 const bool exceeded_unlock_attempts = | 130 const bool exceeded_unlock_attempts = |
| 109 unlock_attempt_count() >= kMaximumUnlockAttempts; | 131 unlock_attempt_count() >= kMaximumUnlockAttempts; |
| 110 const bool has_strong_auth = | |
| 111 HasStrongAuth() && TimeSinceLastStrongAuth() < kStrongAuthTimeout; | |
| 112 | 132 |
| 113 return IsQuickUnlockEnabled() && IsPinSet() && has_strong_auth && | 133 return IsPinUnlockEnabled(pref_service_) && IsPinSet() && HasStrongAuth() && |
| 114 !exceeded_unlock_attempts; | 134 !exceeded_unlock_attempts; |
| 115 } | 135 } |
| 116 | 136 |
| 117 bool PinStorage::TryAuthenticatePin(const std::string& pin) { | 137 bool PinStorage::TryAuthenticatePin(const std::string& pin) { |
| 118 if (!IsPinAuthenticationAvailable()) | 138 if (!IsPinAuthenticationAvailable()) |
| 119 return false; | 139 return false; |
| 120 | 140 |
| 121 AddUnlockAttempt(); | 141 AddUnlockAttempt(); |
| 122 return ComputeSecret(pin, PinSalt()) == PinSecret(); | 142 return ComputeSecret(pin, PinSalt()) == PinSecret(); |
| 123 } | 143 } |
| 124 | 144 |
| 125 } // namespace chromeos | 145 } // namespace chromeos |
| OLD | NEW |