| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage.h" |
| 6 |
| 7 #include "base/base64.h" |
| 8 #include "base/strings/string_util.h" |
| 9 #include "chrome/common/pref_names.h" |
| 10 #include "chromeos/login/auth/key.h" |
| 11 #include "components/pref_registry/pref_registry_syncable.h" |
| 12 #include "components/prefs/pref_service.h" |
| 13 #include "crypto/random.h" |
| 14 |
| 15 namespace chromeos { |
| 16 |
| 17 namespace { |
| 18 |
| 19 const int kSaltByteSize = 16; |
| 20 |
| 21 // Returns a new salt of length |kSaltByteSize|. |
| 22 std::string CreateSalt() { |
| 23 // The salt needs to be base64 encoded because the pref service requires a |
| 24 // UTF8 string. |
| 25 std::string salt; |
| 26 crypto::RandBytes(base::WriteInto(&salt, kSaltByteSize + 1), kSaltByteSize); |
| 27 base::Base64Encode(salt, &salt); |
| 28 DCHECK(!salt.empty()); |
| 29 return salt; |
| 30 } |
| 31 |
| 32 // Computes the hash for |pin| and |salt|. |
| 33 std::string ComputeSecret(const std::string& pin, const std::string& salt) { |
| 34 Key key(pin); |
| 35 key.Transform(Key::KEY_TYPE_SALTED_PBKDF2_AES256_1234, salt); |
| 36 return key.GetSecret(); |
| 37 } |
| 38 |
| 39 } // namespace |
| 40 |
| 41 // static |
| 42 const base::TimeDelta PinStorage::kStrongAuthTimeout = |
| 43 base::TimeDelta::FromHours(24); |
| 44 |
| 45 // static |
| 46 void PinStorage::RegisterProfilePrefs( |
| 47 user_prefs::PrefRegistrySyncable* registry) { |
| 48 registry->RegisterStringPref(prefs::kQuickUnlockPinSalt, "", |
| 49 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 50 registry->RegisterStringPref(prefs::kQuickUnlockPinSecret, "", |
| 51 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 52 } |
| 53 |
| 54 PinStorage::PinStorage(PrefService* pref_service) |
| 55 : pref_service_(pref_service) {} |
| 56 |
| 57 PinStorage::~PinStorage() {} |
| 58 |
| 59 void PinStorage::MarkStrongAuth() { |
| 60 last_strong_auth_ = base::Time::Now(); |
| 61 ResetUnlockAttemptCount(); |
| 62 } |
| 63 |
| 64 bool PinStorage::HasStrongAuth() const { |
| 65 return !last_strong_auth_.is_null(); |
| 66 } |
| 67 |
| 68 base::TimeDelta PinStorage::TimeSinceLastStrongAuth() const { |
| 69 DCHECK(!last_strong_auth_.is_null()); |
| 70 return base::Time::Now() - last_strong_auth_; |
| 71 } |
| 72 |
| 73 void PinStorage::AddUnlockAttempt() { |
| 74 ++unlock_attempt_count_; |
| 75 } |
| 76 |
| 77 void PinStorage::ResetUnlockAttemptCount() { |
| 78 unlock_attempt_count_ = 0; |
| 79 } |
| 80 |
| 81 bool PinStorage::IsPinSet() const { |
| 82 return !PinSalt().empty() && !PinSecret().empty(); |
| 83 } |
| 84 |
| 85 void PinStorage::SetPin(const std::string& pin) { |
| 86 const std::string salt = CreateSalt(); |
| 87 const std::string secret = ComputeSecret(pin, salt); |
| 88 |
| 89 pref_service_->SetString(prefs::kQuickUnlockPinSalt, salt); |
| 90 pref_service_->SetString(prefs::kQuickUnlockPinSecret, secret); |
| 91 } |
| 92 |
| 93 void PinStorage::RemovePin() { |
| 94 pref_service_->SetString(prefs::kQuickUnlockPinSalt, ""); |
| 95 pref_service_->SetString(prefs::kQuickUnlockPinSecret, ""); |
| 96 } |
| 97 |
| 98 std::string PinStorage::PinSalt() const { |
| 99 return pref_service_->GetString(prefs::kQuickUnlockPinSalt); |
| 100 } |
| 101 |
| 102 std::string PinStorage::PinSecret() const { |
| 103 return pref_service_->GetString(prefs::kQuickUnlockPinSecret); |
| 104 } |
| 105 |
| 106 bool PinStorage::IsPinAuthenticationAvailable() const { |
| 107 return IsPinSet() && unlock_attempt_count() < kMaximumUnlockAttempts && |
| 108 HasStrongAuth() && TimeSinceLastStrongAuth() < kStrongAuthTimeout; |
| 109 } |
| 110 |
| 111 bool PinStorage::TryAuthenticatePin(const std::string& pin) { |
| 112 if (!IsPinAuthenticationAvailable()) |
| 113 return false; |
| 114 |
| 115 AddUnlockAttempt(); |
| 116 return ComputeSecret(pin, PinSalt()) == PinSecret(); |
| 117 } |
| 118 |
| 119 } // namespace chromeos |
| OLD | NEW |