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/extensions/quick_unlock_private/quick_unlock_p rivate_api.h" | 5 #include "chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_p rivate_api.h" |
| 6 | 6 |
| 7 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage.h" | 7 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage.h" |
| 8 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage_factory.h" | 8 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage_factory.h" |
| 9 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 9 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 10 #include "chrome/common/pref_names.h" | |
| 10 #include "chromeos/login/auth/extended_authenticator.h" | 11 #include "chromeos/login/auth/extended_authenticator.h" |
| 11 #include "chromeos/login/auth/user_context.h" | 12 #include "chromeos/login/auth/user_context.h" |
| 13 #include "components/prefs/pref_service.h" | |
| 12 #include "extensions/browser/event_router.h" | 14 #include "extensions/browser/event_router.h" |
| 13 | 15 |
| 14 namespace extensions { | 16 namespace extensions { |
| 15 | 17 |
| 16 namespace quick_unlock_private = api::quick_unlock_private; | 18 namespace quick_unlock_private = api::quick_unlock_private; |
| 17 namespace SetModes = quick_unlock_private::SetModes; | 19 namespace SetModes = quick_unlock_private::SetModes; |
| 18 namespace GetActiveModes = quick_unlock_private::GetActiveModes; | 20 namespace GetActiveModes = quick_unlock_private::GetActiveModes; |
| 21 namespace CheckCredential = quick_unlock_private::CheckCredential; | |
| 19 namespace GetAvailableModes = quick_unlock_private::GetAvailableModes; | 22 namespace GetAvailableModes = quick_unlock_private::GetAvailableModes; |
| 20 namespace OnActiveModesChanged = quick_unlock_private::OnActiveModesChanged; | 23 namespace OnActiveModesChanged = quick_unlock_private::OnActiveModesChanged; |
| 24 using CredentialProblem = quick_unlock_private::CredentialProblem; | |
| 25 using CredentialCheck = quick_unlock_private::CredentialCheck; | |
| 21 using QuickUnlockMode = quick_unlock_private::QuickUnlockMode; | 26 using QuickUnlockMode = quick_unlock_private::QuickUnlockMode; |
| 22 using QuickUnlockModeList = std::vector<QuickUnlockMode>; | 27 using QuickUnlockModeList = std::vector<QuickUnlockMode>; |
| 23 | 28 |
| 24 namespace { | 29 namespace { |
| 25 | 30 |
| 26 const char kModesAndCredentialsLengthMismatch[] = | 31 const char kModesAndCredentialsLengthMismatch[] = |
| 27 "|modes| and |credentials| must have the same number of elements"; | 32 "|modes| and |credentials| must have the same number of elements"; |
| 28 const char kMultipleModesNotSupported[] = | 33 const char kMultipleModesNotSupported[] = |
| 29 "At most one quick unlock mode can be active."; | 34 "At most one quick unlock mode can be active."; |
| 35 // PINs greater in length than |kMinLengthForWeakPin| will be checked for | |
| 36 // weakness. | |
| 37 const int kMinLengthForWeakPin = 2; | |
| 38 // A list of the top-10 most commmonly used PINs. This list is taken from | |
| 39 // www.datagenetics.com/blog/september32012/. | |
| 40 const char* kMostCommonPins[] = {"1234", "1111", "0000", "1212", "7777", | |
| 41 "1004", "2000", "4444", "2222", "6969"}; | |
| 30 | 42 |
| 31 // Returns the active set of quick unlock modes. | 43 // Returns the active set of quick unlock modes. |
| 32 QuickUnlockModeList ComputeActiveModes(Profile* profile) { | 44 QuickUnlockModeList ComputeActiveModes(Profile* profile) { |
| 33 QuickUnlockModeList modes; | 45 QuickUnlockModeList modes; |
| 34 | 46 |
| 35 chromeos::PinStorage* pin_storage = | 47 chromeos::PinStorage* pin_storage = |
| 36 chromeos::PinStorageFactory::GetForProfile(profile); | 48 chromeos::PinStorageFactory::GetForProfile(profile); |
| 37 if (pin_storage && pin_storage->IsPinSet()) | 49 if (pin_storage && pin_storage->IsPinSet()) |
| 38 modes.push_back(quick_unlock_private::QUICK_UNLOCK_MODE_PIN); | 50 modes.push_back(quick_unlock_private::QUICK_UNLOCK_MODE_PIN); |
| 39 | 51 |
| 40 return modes; | 52 return modes; |
| 41 } | 53 } |
| 42 | 54 |
| 43 // Returns true if |a| and |b| contain the same elements. The elements do not | 55 // Returns true if |a| and |b| contain the same elements. The elements do not |
| 44 // need to be in the same order. | 56 // need to be in the same order. |
| 45 bool AreModesEqual(const QuickUnlockModeList& a, const QuickUnlockModeList& b) { | 57 bool AreModesEqual(const QuickUnlockModeList& a, const QuickUnlockModeList& b) { |
| 46 if (a.size() != b.size()) | 58 if (a.size() != b.size()) |
| 47 return false; | 59 return false; |
| 48 | 60 |
| 49 // This is a slow comparison algorithm, but the number of entries in |a| and | 61 // This is a slow comparison algorithm, but the number of entries in |a| and |
| 50 // |b| will always be very low (0-3 items) so it doesn't matter. | 62 // |b| will always be very low (0-3 items) so it doesn't matter. |
| 51 for (size_t i = 0; i < a.size(); ++i) { | 63 for (size_t i = 0; i < a.size(); ++i) { |
| 52 if (std::find(b.begin(), b.end(), a[i]) == b.end()) | 64 if (std::find(b.begin(), b.end(), a[i]) == b.end()) |
| 53 return false; | 65 return false; |
| 54 } | 66 } |
| 55 | 67 |
| 56 return true; | 68 return true; |
| 57 } | 69 } |
| 58 | 70 |
| 71 bool IsPinNumeric(const std::string& pin) { | |
| 72 return std::all_of(pin.begin(), pin.end(), ::isdigit); | |
| 73 } | |
| 74 | |
| 75 // Checks whether a given |pin| is valid given the PIN min/max policies in | |
| 76 // |pref_service|. If |out_min_length| and/or |out_max_length| are valid | |
| 77 // pointers, return the profile min/max length as well. If |length_problem| is | |
| 78 // a valid pointer return the type of problem. | |
| 79 bool IsPinLengthValid(const std::string& pin, | |
| 80 PrefService* pref_service, | |
| 81 int* out_min_length, | |
| 82 int* out_max_length, | |
| 83 CredentialProblem* length_problem) { | |
| 84 int min_length = pref_service->GetInteger(prefs::kPinUnlockMinimumLength); | |
| 85 int max_length = pref_service->GetInteger(prefs::kPinUnlockMaximumLength); | |
| 86 | |
| 87 // Sanitize the policy input. | |
| 88 if (min_length < 1) | |
| 89 min_length = 1; | |
| 90 if (max_length < 0) | |
| 91 max_length = 0; | |
| 92 | |
| 93 // If the maximum length is nonzero and shorter than the minimum length, the | |
| 94 // maximum length is the minimum length. | |
| 95 if (max_length != 0 && max_length < min_length) | |
| 96 max_length = min_length; | |
| 97 | |
| 98 if (out_min_length) | |
| 99 *out_min_length = min_length; | |
| 100 if (out_max_length) | |
| 101 *out_max_length = max_length; | |
| 102 | |
| 103 // Check if the PIN is shorter than the minimum specified length. | |
| 104 if (static_cast<int>(pin.size()) < min_length) { | |
| 105 *length_problem = CredentialProblem::CREDENTIAL_PROBLEM_TOO_SHORT; | |
|
Devlin
2016/11/29 21:35:17
need to check this for null (here and on line 112)
sammiequon
2016/11/30 00:42:50
Done.
| |
| 106 return false; | |
| 107 } | |
| 108 | |
| 109 // If the maximum specified length is zero, there is no maximum length. | |
| 110 // Otherwise check if the PIN is longer than the maximum specified length. | |
| 111 if (max_length != 0 && static_cast<int>(pin.size()) > max_length) { | |
| 112 *length_problem = CredentialProblem::CREDENTIAL_PROBLEM_TOO_LONG; | |
| 113 return false; | |
| 114 } | |
| 115 | |
| 116 return true; | |
| 117 } | |
| 118 | |
| 119 // Checks if a given |pin| is weak or not. A PIN is considered weak if it: | |
| 120 // a) has all the same digits | |
| 121 // b) each digit is one larger than the previous digit | |
| 122 // c) each digit is one smaller than the previous digit | |
| 123 // d) is on the top ten of this list; | |
| 124 // www.datagenetics.com/blog/september32012/ | |
| 125 // Note: A 9 followed by a 0 is not considered increasing, and a 0 followed by | |
| 126 // a 9 is not considered decreasing. | |
| 127 bool IsPinDifficultEnough(const std::string& pin) { | |
| 128 // If the pin length is |kMinLengthForWeakPin| or less, there is no need to | |
| 129 // check for same character and increasing pin. | |
| 130 if (static_cast<int>(pin.size()) <= kMinLengthForWeakPin) | |
| 131 return true; | |
| 132 | |
| 133 // Check if it is on the list of most common PINs. | |
| 134 bool is_common = std::find(kMostCommonPins, std::end(kMostCommonPins), pin) != | |
| 135 std::end(kMostCommonPins); | |
|
Devlin
2016/11/29 21:35:17
nit: just return here:
if (std::find(...) != std::
sammiequon
2016/11/30 00:42:50
Done.
| |
| 136 | |
| 137 // Check for same digits, increasing PIN simutaneously. | |
| 138 bool is_same = true; | |
| 139 // TODO(sammiequon): Should longer PINs (5+) be still subjected to this? | |
| 140 bool is_increasing = true; | |
| 141 bool is_decreasing = true; | |
| 142 for (int i = 1; i < int{pin.length()}; ++i) { | |
| 143 const char previous = pin[i - 1]; | |
| 144 const char current = pin[i]; | |
| 145 | |
| 146 is_same = is_same && (current == previous); | |
| 147 is_increasing = is_increasing && (current == previous + 1); | |
| 148 is_decreasing = is_decreasing && (current == previous - 1); | |
| 149 } | |
| 150 | |
| 151 // PIN is considered weak if any of these conditions is met. | |
| 152 if (is_same || is_increasing || is_decreasing || is_common) | |
| 153 return false; | |
| 154 | |
| 155 return true; | |
| 156 } | |
| 157 | |
| 59 } // namespace | 158 } // namespace |
| 60 | 159 |
| 61 // quickUnlockPrivate.getAvailableModes | 160 // quickUnlockPrivate.getAvailableModes |
| 62 | 161 |
| 63 QuickUnlockPrivateGetAvailableModesFunction:: | 162 QuickUnlockPrivateGetAvailableModesFunction:: |
| 64 QuickUnlockPrivateGetAvailableModesFunction() | 163 QuickUnlockPrivateGetAvailableModesFunction() |
| 65 : chrome_details_(this) {} | 164 : chrome_details_(this) {} |
| 66 | 165 |
| 67 QuickUnlockPrivateGetAvailableModesFunction:: | 166 QuickUnlockPrivateGetAvailableModesFunction:: |
| 68 ~QuickUnlockPrivateGetAvailableModesFunction() {} | 167 ~QuickUnlockPrivateGetAvailableModesFunction() {} |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 86 QuickUnlockPrivateGetActiveModesFunction:: | 185 QuickUnlockPrivateGetActiveModesFunction:: |
| 87 ~QuickUnlockPrivateGetActiveModesFunction() {} | 186 ~QuickUnlockPrivateGetActiveModesFunction() {} |
| 88 | 187 |
| 89 ExtensionFunction::ResponseAction | 188 ExtensionFunction::ResponseAction |
| 90 QuickUnlockPrivateGetActiveModesFunction::Run() { | 189 QuickUnlockPrivateGetActiveModesFunction::Run() { |
| 91 const QuickUnlockModeList modes = | 190 const QuickUnlockModeList modes = |
| 92 ComputeActiveModes(chrome_details_.GetProfile()); | 191 ComputeActiveModes(chrome_details_.GetProfile()); |
| 93 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); | 192 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); |
| 94 } | 193 } |
| 95 | 194 |
| 195 // quickUnlockPrivate.checkCredential | |
| 196 | |
| 197 QuickUnlockPrivateCheckCredentialFunction:: | |
| 198 QuickUnlockPrivateCheckCredentialFunction() | |
| 199 : chrome_details_(this) {} | |
|
Devlin
2016/11/29 21:35:17
no longer needed, right?
sammiequon
2016/11/30 00:42:50
Right.
| |
| 200 | |
| 201 QuickUnlockPrivateCheckCredentialFunction:: | |
| 202 ~QuickUnlockPrivateCheckCredentialFunction() {} | |
| 203 | |
| 204 ExtensionFunction::ResponseAction | |
| 205 QuickUnlockPrivateCheckCredentialFunction::Run() { | |
| 206 params_ = CheckCredential::Params::Create(*args_); | |
|
Devlin
2016/11/29 21:35:17
given params is only used here, let's have it be l
sammiequon
2016/11/30 00:42:50
Done.
| |
| 207 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
|
Devlin
2016/11/29 21:35:17
nit: params_.get() is redundant (unique_ptr has a
sammiequon
2016/11/30 00:42:50
Done.
| |
| 208 | |
| 209 auto result = base::MakeUnique<CredentialCheck>(); | |
| 210 | |
| 211 // Only handles pins for now. | |
| 212 if (params_->mode != QuickUnlockMode::QUICK_UNLOCK_MODE_PIN) | |
| 213 return RespondNow(ArgumentList(CheckCredential::Results::Create(*result))); | |
| 214 | |
| 215 std::string credential = params_->credential; | |
|
Devlin
2016/11/29 21:35:17
const std::string&
sammiequon
2016/11/30 00:42:50
Done.
| |
| 216 | |
| 217 Profile* profile = Profile::FromBrowserContext(browser_context()); | |
| 218 PrefService* pref_service = profile->GetPrefs(); | |
| 219 bool allow_weak = pref_service->GetBoolean(prefs::kPinUnlockAllowWeakPins); | |
| 220 | |
| 221 // Check and return the problems. | |
| 222 if (!IsPinNumeric(credential)) { | |
| 223 result->errors.push_back( | |
| 224 CredentialProblem::CREDENTIAL_PROBLEM_CONTAINS_NONDIGIT); | |
| 225 } | |
| 226 | |
| 227 CredentialProblem length_problem = CredentialProblem::CREDENTIAL_PROBLEM_NONE; | |
| 228 // Note: This function call writes values to |result->min_length|, | |
| 229 // |result->max_length| and |length_problem|. | |
| 230 if (!IsPinLengthValid(credential, pref_service, &result->min_length, | |
| 231 &result->max_length, &length_problem)) { | |
| 232 if (length_problem != CredentialProblem::CREDENTIAL_PROBLEM_NONE) | |
|
Devlin
2016/11/29 21:35:17
When would we return false to IsPinLengthValid(),
sammiequon
2016/11/30 00:42:50
Done.
| |
| 233 result->errors.push_back(length_problem); | |
| 234 } | |
| 235 | |
| 236 if (!IsPinDifficultEnough(credential)) { | |
| 237 if (allow_weak) { | |
| 238 result->warnings.push_back( | |
| 239 CredentialProblem::CREDENTIAL_PROBLEM_TOO_WEAK); | |
| 240 } else { | |
| 241 result->errors.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_WEAK); | |
| 242 } | |
| 243 } | |
| 244 | |
| 245 return RespondNow(ArgumentList(CheckCredential::Results::Create(*result))); | |
| 246 } | |
| 247 | |
| 96 // quickUnlockPrivate.setModes | 248 // quickUnlockPrivate.setModes |
| 97 | 249 |
| 98 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() | 250 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() |
| 99 : chrome_details_(this) {} | 251 : chrome_details_(this) {} |
| 100 | 252 |
| 101 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} | 253 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} |
| 102 | 254 |
| 103 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting( | 255 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting( |
| 104 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator& | 256 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator& |
| 105 allocator) { | 257 allocator) { |
| 106 authenticator_allocator_ = allocator; | 258 authenticator_allocator_ = allocator; |
| 107 } | 259 } |
| 108 | 260 |
| 109 void QuickUnlockPrivateSetModesFunction::SetModesChangedEventHandlerForTesting( | 261 void QuickUnlockPrivateSetModesFunction::SetModesChangedEventHandlerForTesting( |
| 110 const ModesChangedEventHandler& handler) { | 262 const ModesChangedEventHandler& handler) { |
| 111 modes_changed_handler_ = handler; | 263 modes_changed_handler_ = handler; |
| 112 } | 264 } |
| 113 | 265 |
| 114 ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() { | 266 ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() { |
| 115 params_ = SetModes::Params::Create(*args_); | 267 params_ = SetModes::Params::Create(*args_); |
| 116 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 268 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 117 | 269 |
| 118 if (params_->modes.size() != params_->credentials.size()) | 270 if (params_->modes.size() != params_->credentials.size()) |
| 119 return RespondNow(Error(kModesAndCredentialsLengthMismatch)); | 271 return RespondNow(Error(kModesAndCredentialsLengthMismatch)); |
| 120 | 272 |
| 121 if (params_->modes.size() > 1) | 273 if (params_->modes.size() > 1) |
| 122 return RespondNow(Error(kMultipleModesNotSupported)); | 274 return RespondNow(Error(kMultipleModesNotSupported)); |
| 123 | 275 |
| 124 // Verify every credential is numeric. | 276 // Verify every credential is valid based on policies. |
| 125 for (const std::string& credential : params_->credentials) { | 277 bool allow_weak = Profile::FromBrowserContext(browser_context()) |
|
Devlin
2016/11/29 21:35:17
nit: cache Profile so we can re-use it on line 290
sammiequon
2016/11/30 00:42:50
Done.
| |
| 126 if (!std::all_of(credential.begin(), credential.end(), ::isdigit)) | 278 ->GetPrefs() |
| 127 return RespondNow(ArgumentList(SetModes::Results::Create(false))); | 279 ->GetBoolean(prefs::kPinUnlockAllowWeakPins); |
| 280 for (size_t j = 0; j < params_->modes.size(); ++j) { | |
|
Devlin
2016/11/29 21:35:17
This loop seems unnecessary given the check on lin
sammiequon
2016/11/30 00:42:50
Yes, there are other modes planned to be added lat
| |
| 281 if (params_->credentials[j].empty()) | |
| 282 continue; | |
| 283 | |
| 284 if (params_->modes[j] == QuickUnlockMode::QUICK_UNLOCK_MODE_PIN) { | |
| 285 if (!IsPinNumeric(params_->credentials[j])) | |
| 286 return RespondNow(ArgumentList(SetModes::Results::Create(false))); | |
| 287 | |
| 288 if (!IsPinLengthValid( | |
| 289 params_->credentials[j], | |
| 290 Profile::FromBrowserContext(browser_context())->GetPrefs(), | |
| 291 nullptr, nullptr, nullptr)) { | |
| 292 return RespondNow(ArgumentList(SetModes::Results::Create(false))); | |
| 293 } | |
| 294 if (!allow_weak && !IsPinDifficultEnough(params_->credentials[j])) { | |
| 295 return RespondNow(ArgumentList(SetModes::Results::Create(false))); | |
| 296 } | |
| 297 } | |
| 128 } | 298 } |
| 129 | 299 |
| 130 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( | 300 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( |
| 131 chrome_details_.GetProfile()); | 301 chrome_details_.GetProfile()); |
| 132 chromeos::UserContext user_context(user->GetAccountId()); | 302 chromeos::UserContext user_context(user->GetAccountId()); |
| 133 user_context.SetKey(chromeos::Key(params_->account_password)); | 303 user_context.SetKey(chromeos::Key(params_->account_password)); |
| 134 | 304 |
| 135 // Lazily allocate the authenticator. We do this here, instead of in the ctor, | 305 // Lazily allocate the authenticator. We do this here, instead of in the ctor, |
| 136 // so that tests can install a fake. | 306 // so that tests can install a fake. |
| 137 if (authenticator_allocator_.is_null()) | 307 if (authenticator_allocator_.is_null()) |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 } | 393 } |
| 224 | 394 |
| 225 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); | 395 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); |
| 226 std::unique_ptr<Event> event( | 396 std::unique_ptr<Event> event( |
| 227 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, | 397 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, |
| 228 OnActiveModesChanged::kEventName, std::move(args))); | 398 OnActiveModesChanged::kEventName, std::move(args))); |
| 229 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event)); | 399 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event)); |
| 230 } | 400 } |
| 231 | 401 |
| 232 } // namespace extensions | 402 } // namespace extensions |
| OLD | NEW |