Chromium Code Reviews| 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 std::string ComputeSecret(std::string pin, std::string salt) { | |
|
achuithb
2016/05/16 23:05:10
Input params should be const std::string&. You mig
jdufault
2016/05/17 19:58:26
Converted input params to references.
Is there a
achuithb
2016/05/17 20:13:43
It saves a copy, and it's generally preferred for
| |
| 22 Key key(pin); | |
| 23 key.Transform(Key::KEY_TYPE_SALTED_PBKDF2_AES256_1234, salt); | |
| 24 return key.GetSecret(); | |
| 25 } | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 // static | |
| 30 const base::TimeDelta PinStorage::kStrongAuthTimeout = | |
| 31 base::TimeDelta::FromHours(24); | |
| 32 | |
| 33 // static | |
| 34 void PinStorage::RegisterProfilePrefs( | |
| 35 user_prefs::PrefRegistrySyncable* registry) { | |
| 36 registry->RegisterStringPref(prefs::kQuickUnlockPinSalt, "", | |
| 37 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | |
| 38 registry->RegisterStringPref(prefs::kQuickUnlockPinSecret, "", | |
| 39 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | |
| 40 } | |
| 41 | |
| 42 PinStorage::PinStorage(PrefService* pref_service) | |
| 43 : pref_service_(pref_service) {} | |
| 44 | |
| 45 PinStorage::~PinStorage() {} | |
| 46 | |
| 47 void PinStorage::MarkStrongAuth() { | |
| 48 last_strong_auth_ = base::Time::Now(); | |
| 49 ResetUnlockAttemptCount(); | |
| 50 } | |
| 51 | |
| 52 bool PinStorage::HasStrongAuth() const { | |
| 53 return !last_strong_auth_.is_null(); | |
| 54 } | |
| 55 | |
| 56 base::TimeDelta PinStorage::TimeSinceLastStrongAuth() const { | |
| 57 DCHECK(!last_strong_auth_.is_null()); | |
| 58 return base::Time::Now() - last_strong_auth_; | |
| 59 } | |
| 60 | |
| 61 void PinStorage::AddUnlockAttempt() { | |
| 62 ++attempt_count_; | |
| 63 } | |
| 64 | |
| 65 void PinStorage::ResetUnlockAttemptCount() { | |
| 66 attempt_count_ = 0; | |
| 67 } | |
| 68 | |
| 69 int PinStorage::UnlockAttemptCount() const { | |
| 70 return attempt_count_; | |
| 71 } | |
|
stevenjb
2016/05/16 22:19:08
This could be attempt_count() inlined in the heade
jdufault
2016/05/17 19:58:25
Done.
| |
| 72 | |
| 73 bool PinStorage::IsPinSet() const { | |
| 74 return !PinSalt().empty() && !PinSecret().empty(); | |
| 75 } | |
| 76 | |
| 77 void PinStorage::SetPin(const std::string& pin) { | |
| 78 // Generate random salt. It needs to be base64 encoded because the pref | |
| 79 // service requires a UTF8 string. | |
| 80 std::string salt; | |
| 81 crypto::RandBytes(base::WriteInto(&salt, kSaltByteSize + 1), kSaltByteSize); | |
|
achuithb
2016/05/16 23:05:10
nit: Also make salt generation an anonymous functi
jdufault
2016/05/17 19:58:26
Done.
| |
| 82 base::Base64Encode(salt, &salt); | |
| 83 DCHECK(!salt.empty()); | |
| 84 | |
| 85 const std::string secret = ComputeSecret(pin, salt); | |
| 86 | |
| 87 pref_service_->SetString(prefs::kQuickUnlockPinSalt, salt); | |
| 88 pref_service_->SetString(prefs::kQuickUnlockPinSecret, secret); | |
| 89 } | |
| 90 | |
| 91 void PinStorage::RemovePin() { | |
| 92 pref_service_->SetString(prefs::kQuickUnlockPinSalt, ""); | |
| 93 pref_service_->SetString(prefs::kQuickUnlockPinSecret, ""); | |
| 94 } | |
| 95 | |
| 96 std::string PinStorage::PinSalt() const { | |
| 97 return pref_service_->GetString(prefs::kQuickUnlockPinSalt); | |
| 98 } | |
| 99 | |
| 100 std::string PinStorage::PinSecret() const { | |
| 101 return pref_service_->GetString(prefs::kQuickUnlockPinSecret); | |
| 102 } | |
| 103 | |
| 104 bool PinStorage::IsPinAuthenticationAvailable() const { | |
| 105 return IsPinSet() && UnlockAttemptCount() < kMaximumUnlockAttempts && | |
| 106 HasStrongAuth() && TimeSinceLastStrongAuth() < kStrongAuthTimeout; | |
| 107 } | |
| 108 | |
| 109 bool PinStorage::TryAuthenticatePin(const std::string& pin) { | |
| 110 if (!IsPinAuthenticationAvailable()) | |
| 111 return false; | |
| 112 | |
| 113 AddUnlockAttempt(); | |
| 114 | |
| 115 const std::string secret = ComputeSecret(pin, PinSalt()); | |
| 116 return secret == PinSecret(); | |
|
achuithb
2016/05/16 23:05:10
Inline ComputeSecret? (assuming you don't use the
jdufault
2016/05/17 19:58:26
Done. I initially did this, but thought it was a b
| |
| 117 } | |
| 118 | |
| 119 } // namespace chromeos | |
| OLD | NEW |