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 PasswordConfirmationFrequencyFrequencyToTimeDelta( | |
| 41 PasswordConfirmationFrequency frequency) { | |
| 42 base::TimeDelta time_delta; | |
| 43 switch (frequency) { | |
| 44 case PasswordConfirmationFrequency::SIX_HOURS: | |
| 45 time_delta = base::TimeDelta::FromHours(6); | |
|
jdufault
2016/10/21 19:03:45
Return the value directly and remove the |time_del
sammiequon
2016/10/21 23:49:25
Done.
| |
| 46 break; | |
| 47 case PasswordConfirmationFrequency::TWELVE_HOURS: | |
| 48 time_delta = base::TimeDelta::FromHours(12); | |
| 49 break; | |
| 50 case PasswordConfirmationFrequency::DAY: | |
| 51 time_delta = base::TimeDelta::FromDays(1); | |
| 52 break; | |
| 53 case PasswordConfirmationFrequency::WEEK: | |
| 54 time_delta = base::TimeDelta::FromDays(7); | |
| 55 break; | |
| 56 default: | |
| 57 NOTREACHED(); | |
| 58 } | |
| 59 return time_delta; | |
| 60 } | |
| 61 | |
| 40 } // namespace | 62 } // namespace |
| 41 | 63 |
| 42 // static | 64 // static |
| 43 const base::TimeDelta PinStorage::kStrongAuthTimeout = | |
| 44 base::TimeDelta::FromHours(24); | |
| 45 | |
| 46 // static | |
| 47 void PinStorage::RegisterProfilePrefs( | 65 void PinStorage::RegisterProfilePrefs( |
| 48 user_prefs::PrefRegistrySyncable* registry) { | 66 user_prefs::PrefRegistrySyncable* registry) { |
| 49 registry->RegisterStringPref(prefs::kQuickUnlockPinSalt, "", | 67 registry->RegisterStringPref(prefs::kQuickUnlockPinSalt, "", |
| 50 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 68 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 51 registry->RegisterStringPref(prefs::kQuickUnlockPinSecret, "", | 69 registry->RegisterStringPref(prefs::kQuickUnlockPinSecret, "", |
| 52 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 70 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 53 } | 71 } |
| 54 | 72 |
| 55 PinStorage::PinStorage(PrefService* pref_service) | 73 PinStorage::PinStorage(PrefService* pref_service) |
| 56 : pref_service_(pref_service) {} | 74 : pref_service_(pref_service) {} |
| 57 | 75 |
| 58 PinStorage::~PinStorage() {} | 76 PinStorage::~PinStorage() {} |
| 59 | 77 |
| 60 void PinStorage::MarkStrongAuth() { | 78 void PinStorage::MarkStrongAuth() { |
| 61 last_strong_auth_ = base::Time::Now(); | 79 last_strong_auth_ = base::Time::Now(); |
| 62 ResetUnlockAttemptCount(); | 80 ResetUnlockAttemptCount(); |
| 63 } | 81 } |
| 64 | 82 |
| 65 bool PinStorage::HasStrongAuth() const { | 83 bool PinStorage::HasStrongAuth() const { |
| 66 return !last_strong_auth_.is_null(); | 84 return !last_strong_auth_.is_null(); |
| 67 } | 85 } |
| 68 | 86 |
| 87 bool PinStorage::NeedsStrongAuth() const { | |
| 88 PasswordConfirmationFrequency strong_auth_interval = | |
| 89 static_cast<PasswordConfirmationFrequency>( | |
| 90 pref_service_->GetInteger(prefs::kQuickUnlockTimeout)); | |
| 91 base::TimeDelta strong_auth_timeout = | |
| 92 PasswordConfirmationFrequencyFrequencyToTimeDelta(strong_auth_interval); | |
| 93 ; | |
|
jdufault
2016/10/21 19:03:44
Remove extra ;
sammiequon
2016/10/21 23:49:25
Done.
| |
| 94 | |
| 95 return TimeSinceLastStrongAuth() > strong_auth_timeout; | |
| 96 } | |
| 97 | |
| 69 base::TimeDelta PinStorage::TimeSinceLastStrongAuth() const { | 98 base::TimeDelta PinStorage::TimeSinceLastStrongAuth() const { |
| 70 DCHECK(!last_strong_auth_.is_null()); | 99 DCHECK(!last_strong_auth_.is_null()); |
| 71 return base::Time::Now() - last_strong_auth_; | 100 return base::Time::Now() - last_strong_auth_; |
| 72 } | 101 } |
| 73 | 102 |
| 74 void PinStorage::AddUnlockAttempt() { | 103 void PinStorage::AddUnlockAttempt() { |
| 75 ++unlock_attempt_count_; | 104 ++unlock_attempt_count_; |
| 76 } | 105 } |
| 77 | 106 |
| 78 void PinStorage::ResetUnlockAttemptCount() { | 107 void PinStorage::ResetUnlockAttemptCount() { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 89 | 118 |
| 90 pref_service_->SetString(prefs::kQuickUnlockPinSalt, salt); | 119 pref_service_->SetString(prefs::kQuickUnlockPinSalt, salt); |
| 91 pref_service_->SetString(prefs::kQuickUnlockPinSecret, secret); | 120 pref_service_->SetString(prefs::kQuickUnlockPinSecret, secret); |
| 92 } | 121 } |
| 93 | 122 |
| 94 void PinStorage::RemovePin() { | 123 void PinStorage::RemovePin() { |
| 95 pref_service_->SetString(prefs::kQuickUnlockPinSalt, ""); | 124 pref_service_->SetString(prefs::kQuickUnlockPinSalt, ""); |
| 96 pref_service_->SetString(prefs::kQuickUnlockPinSecret, ""); | 125 pref_service_->SetString(prefs::kQuickUnlockPinSecret, ""); |
| 97 } | 126 } |
| 98 | 127 |
| 128 bool PinStorage::IsPinUnlockEnabled() const { | |
| 129 const base::ListValue* screen_lock_whitelist = | |
| 130 pref_service_->GetList(prefs::kQuickUnlockModeWhitelist); | |
| 131 auto all_value = base::StringValue("all"); | |
|
jdufault
2016/10/21 19:03:45
base::StringValue all_value("all")?
sammiequon
2016/10/21 23:49:25
Done.
| |
| 132 auto pin_value = base::StringValue("pin"); | |
| 133 return screen_lock_whitelist->Find(all_value) != | |
| 134 screen_lock_whitelist->end() || | |
| 135 screen_lock_whitelist->Find(pin_value) != screen_lock_whitelist->end(); | |
| 136 } | |
| 137 | |
| 99 std::string PinStorage::PinSalt() const { | 138 std::string PinStorage::PinSalt() const { |
| 100 return pref_service_->GetString(prefs::kQuickUnlockPinSalt); | 139 return pref_service_->GetString(prefs::kQuickUnlockPinSalt); |
| 101 } | 140 } |
| 102 | 141 |
| 103 std::string PinStorage::PinSecret() const { | 142 std::string PinStorage::PinSecret() const { |
| 104 return pref_service_->GetString(prefs::kQuickUnlockPinSecret); | 143 return pref_service_->GetString(prefs::kQuickUnlockPinSecret); |
| 105 } | 144 } |
| 106 | 145 |
| 107 bool PinStorage::IsPinAuthenticationAvailable() const { | 146 bool PinStorage::IsPinAuthenticationAvailable() const { |
| 108 const bool exceeded_unlock_attempts = | 147 const bool exceeded_unlock_attempts = |
| 109 unlock_attempt_count() >= kMaximumUnlockAttempts; | 148 unlock_attempt_count() >= kMaximumUnlockAttempts; |
| 110 const bool has_strong_auth = | 149 const bool has_strong_auth = HasStrongAuth() && !NeedsStrongAuth(); |
| 111 HasStrongAuth() && TimeSinceLastStrongAuth() < kStrongAuthTimeout; | |
| 112 | 150 |
| 113 return IsQuickUnlockEnabled() && IsPinSet() && has_strong_auth && | 151 return IsPinUnlockEnabled() && IsQuickUnlockEnabled() && IsPinSet() && |
| 114 !exceeded_unlock_attempts; | 152 has_strong_auth && !exceeded_unlock_attempts; |
| 115 } | 153 } |
| 116 | 154 |
| 117 bool PinStorage::TryAuthenticatePin(const std::string& pin) { | 155 bool PinStorage::TryAuthenticatePin(const std::string& pin) { |
| 118 if (!IsPinAuthenticationAvailable()) | 156 if (!IsPinAuthenticationAvailable()) |
| 119 return false; | 157 return false; |
| 120 | 158 |
| 121 AddUnlockAttempt(); | 159 AddUnlockAttempt(); |
| 122 return ComputeSecret(pin, PinSalt()) == PinSecret(); | 160 return ComputeSecret(pin, PinSalt()) == PinSecret(); |
| 123 } | 161 } |
| 124 | 162 |
| 125 } // namespace chromeos | 163 } // namespace chromeos |
| OLD | NEW |