| 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 QuickUnlockPasswordConfirmationFrequencyToTimeDelta( |
| 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 QuickUnlockPasswordConfirmationFrequencyToTimeDelta(strong_auth_interval); |
| 86 |
| 87 return TimeSinceLastStrongAuth() < strong_auth_timeout; |
| 67 } | 88 } |
| 68 | 89 |
| 69 base::TimeDelta PinStorage::TimeSinceLastStrongAuth() const { | 90 base::TimeDelta PinStorage::TimeSinceLastStrongAuth() const { |
| 70 DCHECK(!last_strong_auth_.is_null()); | 91 DCHECK(!last_strong_auth_.is_null()); |
| 71 return base::Time::Now() - last_strong_auth_; | 92 return base::Time::Now() - last_strong_auth_; |
| 72 } | 93 } |
| 73 | 94 |
| 74 void PinStorage::AddUnlockAttempt() { | 95 void PinStorage::AddUnlockAttempt() { |
| 75 ++unlock_attempt_count_; | 96 ++unlock_attempt_count_; |
| 76 } | 97 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 100 return pref_service_->GetString(prefs::kQuickUnlockPinSalt); | 121 return pref_service_->GetString(prefs::kQuickUnlockPinSalt); |
| 101 } | 122 } |
| 102 | 123 |
| 103 std::string PinStorage::PinSecret() const { | 124 std::string PinStorage::PinSecret() const { |
| 104 return pref_service_->GetString(prefs::kQuickUnlockPinSecret); | 125 return pref_service_->GetString(prefs::kQuickUnlockPinSecret); |
| 105 } | 126 } |
| 106 | 127 |
| 107 bool PinStorage::IsPinAuthenticationAvailable() const { | 128 bool PinStorage::IsPinAuthenticationAvailable() const { |
| 108 const bool exceeded_unlock_attempts = | 129 const bool exceeded_unlock_attempts = |
| 109 unlock_attempt_count() >= kMaximumUnlockAttempts; | 130 unlock_attempt_count() >= kMaximumUnlockAttempts; |
| 110 const bool has_strong_auth = | |
| 111 HasStrongAuth() && TimeSinceLastStrongAuth() < kStrongAuthTimeout; | |
| 112 | 131 |
| 113 return IsQuickUnlockEnabled() && IsPinSet() && has_strong_auth && | 132 return IsPinUnlockEnabled(pref_service_) && IsPinSet() && HasStrongAuth() && |
| 114 !exceeded_unlock_attempts; | 133 !exceeded_unlock_attempts; |
| 115 } | 134 } |
| 116 | 135 |
| 117 bool PinStorage::TryAuthenticatePin(const std::string& pin) { | 136 bool PinStorage::TryAuthenticatePin(const std::string& pin) { |
| 118 if (!IsPinAuthenticationAvailable()) | 137 if (!IsPinAuthenticationAvailable()) |
| 119 return false; | 138 return false; |
| 120 | 139 |
| 121 AddUnlockAttempt(); | 140 AddUnlockAttempt(); |
| 122 return ComputeSecret(pin, PinSalt()) == PinSecret(); | 141 return ComputeSecret(pin, PinSalt()) == PinSecret(); |
| 123 } | 142 } |
| 124 | 143 |
| 125 } // namespace chromeos | 144 } // namespace chromeos |
| OLD | NEW |