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 std::vector<CredentialProblem> IsPinNumeric(const std::string& pin) { | |
| 72 std::vector<CredentialProblem> problems; | |
| 73 if (!std::all_of(pin.begin(), pin.end(), ::isdigit)) | |
| 74 problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_CONTAINS_NONDIGIT); | |
| 75 return problems; | |
| 76 } | |
| 77 | |
| 78 // Returns a list of length related problems (if any) for a given |pin| | |
| 79 // given the PIN min/max policies in |profile|. If |out_min_length| and/or | |
| 80 // |out_max_length| are valid pointers, return the profile min/max length as | |
| 81 // well. | |
| 82 std::vector<CredentialProblem> IsPinLengthValid(const std::string& pin, | |
| 83 Profile* profile, | |
| 84 int* out_min_length, | |
| 85 int* out_max_length) { | |
| 86 PrefService* pref_service = profile->GetPrefs(); | |
| 87 int min_length = pref_service->GetInteger(prefs::kPinUnlockMinimumLength); | |
| 88 int max_length = pref_service->GetInteger(prefs::kPinUnlockMaximumLength); | |
| 89 | |
| 90 // Sanitize the policy input. | |
| 91 if (min_length < 1) | |
| 92 min_length = 1; | |
| 93 if (max_length < 0) | |
| 94 max_length = 0; | |
| 95 | |
| 96 // If the maximum length is nonzero and shorter than the minimum length, the | |
| 97 // maximum length is the minimum length. | |
| 98 if (max_length != 0 && max_length < min_length) | |
| 99 max_length = min_length; | |
| 100 | |
| 101 if (out_min_length) | |
| 102 *out_min_length = min_length; | |
| 103 if (out_max_length) | |
| 104 *out_max_length = max_length; | |
| 105 | |
| 106 std::vector<CredentialProblem> problems; | |
| 107 | |
| 108 // Check if the PIN is shorter than the minimum specified length. | |
| 109 if (int{pin.size()} < min_length) | |
| 110 problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_SHORT); | |
| 111 | |
| 112 // If the maximum specified length is zero, there is no maximum length. | |
| 113 // Otherwise check if the PIN is longer than the maximum specified length. | |
| 114 if (max_length != 0 && int{pin.size()} > max_length) | |
| 115 problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_LONG); | |
| 116 | |
| 117 return problems; | |
| 118 } | |
| 119 | |
| 120 // Returns a list of problems regarding the difficulty of the PIN (if any) for | |
| 121 // a given |pin|. | |
| 122 std::vector<CredentialProblem> IsPinDifficultEnough(const std::string& pin) { | |
| 123 std::vector<CredentialProblem> problems; | |
| 124 // If the pin length is |kMinLengthForWeakPin| or less, there is no need to | |
| 125 // check for same character and increasing pin. | |
| 126 if (int{pin.size()} <= kMinLengthForWeakPin) | |
| 127 return problems; | |
| 128 | |
| 129 // Check if it is on the list of most common PINs. | |
| 130 const char** end = | |
| 131 kMostCommonPins + sizeof(kMostCommonPins) / sizeof(kMostCommonPins[0]); | |
|
jdufault
2016/11/08 23:39:56
You should be able to use std::end(kMostCommonPins
sammiequon
2016/11/10 01:54:45
Done.
| |
| 132 bool is_too_common = std::find(kMostCommonPins, end, pin) != end; | |
|
jdufault
2016/11/08 23:39:56
why the too? What about is_too_common => is_common
sammiequon
2016/11/10 01:54:45
Done.
| |
| 133 | |
| 134 // Check for same digits, increasing PIN simutaneously. | |
| 135 bool is_same = true; | |
| 136 bool is_increasing = true; | |
| 137 bool is_decreasing = true; | |
| 138 for (int i = 1; i < int{pin.length()}; ++i) { | |
| 139 const char previous = pin[i - 1]; | |
| 140 const char current = pin[i]; | |
| 141 | |
| 142 is_same = is_same && (current == previous); | |
| 143 is_increasing = is_increasing && (current == previous + 1); | |
| 144 is_decreasing = is_decreasing && (current == previous - 1); | |
| 145 } | |
| 146 | |
| 147 // PIN is considered weak if any of these conditions is met. | |
| 148 if (is_same || is_increasing || is_decreasing || is_too_common) | |
| 149 problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_WEAK); | |
| 150 | |
| 151 return problems; | |
| 152 } | |
| 153 | |
| 154 void AppendToProblemList(std::vector<CredentialProblem>& dest, | |
| 155 const std::vector<CredentialProblem>& src) { | |
| 156 dest.insert(dest.end(), src.begin(), src.end()); | |
| 157 } | |
| 158 | |
| 59 } // namespace | 159 } // namespace |
| 60 | 160 |
| 61 // quickUnlockPrivate.getAvailableModes | 161 // quickUnlockPrivate.getAvailableModes |
| 62 | 162 |
| 63 QuickUnlockPrivateGetAvailableModesFunction:: | 163 QuickUnlockPrivateGetAvailableModesFunction:: |
| 64 QuickUnlockPrivateGetAvailableModesFunction() | 164 QuickUnlockPrivateGetAvailableModesFunction() |
| 65 : chrome_details_(this) {} | 165 : chrome_details_(this) {} |
| 66 | 166 |
| 67 QuickUnlockPrivateGetAvailableModesFunction:: | 167 QuickUnlockPrivateGetAvailableModesFunction:: |
| 68 ~QuickUnlockPrivateGetAvailableModesFunction() {} | 168 ~QuickUnlockPrivateGetAvailableModesFunction() {} |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 86 QuickUnlockPrivateGetActiveModesFunction:: | 186 QuickUnlockPrivateGetActiveModesFunction:: |
| 87 ~QuickUnlockPrivateGetActiveModesFunction() {} | 187 ~QuickUnlockPrivateGetActiveModesFunction() {} |
| 88 | 188 |
| 89 ExtensionFunction::ResponseAction | 189 ExtensionFunction::ResponseAction |
| 90 QuickUnlockPrivateGetActiveModesFunction::Run() { | 190 QuickUnlockPrivateGetActiveModesFunction::Run() { |
| 91 const QuickUnlockModeList modes = | 191 const QuickUnlockModeList modes = |
| 92 ComputeActiveModes(chrome_details_.GetProfile()); | 192 ComputeActiveModes(chrome_details_.GetProfile()); |
| 93 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); | 193 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); |
| 94 } | 194 } |
| 95 | 195 |
| 196 // quickUnlockPrivate.checkCredential | |
| 197 | |
| 198 QuickUnlockPrivateCheckCredentialFunction:: | |
| 199 QuickUnlockPrivateCheckCredentialFunction() | |
| 200 : chrome_details_(this) {} | |
| 201 | |
| 202 QuickUnlockPrivateCheckCredentialFunction:: | |
| 203 ~QuickUnlockPrivateCheckCredentialFunction() {} | |
| 204 | |
| 205 ExtensionFunction::ResponseAction | |
| 206 QuickUnlockPrivateCheckCredentialFunction::Run() { | |
| 207 params_ = CheckCredential::Params::Create(*args_); | |
| 208 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
| 209 | |
| 210 auto result = base::MakeUnique<CredentialCheck>(); | |
| 211 | |
| 212 // Only handles pins for now. | |
| 213 if (params_->mode != QuickUnlockMode::QUICK_UNLOCK_MODE_PIN) | |
| 214 return RespondNow(ArgumentList(CheckCredential::Results::Create(*result))); | |
| 215 | |
| 216 std::string credential = params_->credential; | |
| 217 | |
| 218 Profile* profile = chrome_details_.GetProfile(); | |
| 219 PrefService* pref_service = profile->GetPrefs(); | |
| 220 bool allow_weak = pref_service->GetBoolean(prefs::kPinUnlockAllowWeakPins); | |
| 221 | |
| 222 // Check and return the problems. | |
| 223 std::vector<CredentialProblem> validity_problems = IsPinNumeric(credential); | |
| 224 // Note: This function call writes values to |result->min_length| and | |
| 225 // |result->max_length|. | |
| 226 std::vector<CredentialProblem> length_problems = IsPinLengthValid( | |
| 227 credential, profile, &result->min_length, &result->max_length); | |
| 228 std::vector<CredentialProblem> weak_problems = | |
| 229 IsPinDifficultEnough(credential); | |
| 230 | |
| 231 // Append the results from various checks to the appropriate list in |result|. | |
| 232 // |validity_problems| and |length_problems| are always errors, while | |
| 233 // |weak_problems| are errors or warnings depending on the policy. | |
| 234 AppendToProblemList(result->errors, validity_problems); | |
| 235 AppendToProblemList(result->errors, length_problems); | |
| 236 AppendToProblemList(allow_weak ? result->warnings : result->errors, | |
| 237 weak_problems); | |
| 238 | |
| 239 return RespondNow(ArgumentList(CheckCredential::Results::Create(*result))); | |
| 240 } | |
| 241 | |
| 96 // quickUnlockPrivate.setModes | 242 // quickUnlockPrivate.setModes |
| 97 | 243 |
| 98 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() | 244 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() |
| 99 : chrome_details_(this) {} | 245 : chrome_details_(this) {} |
| 100 | 246 |
| 101 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} | 247 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} |
| 102 | 248 |
| 103 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting( | 249 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting( |
| 104 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator& | 250 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator& |
| 105 allocator) { | 251 allocator) { |
| 106 authenticator_allocator_ = allocator; | 252 authenticator_allocator_ = allocator; |
| 107 } | 253 } |
| 108 | 254 |
| 109 void QuickUnlockPrivateSetModesFunction::SetModesChangedEventHandlerForTesting( | 255 void QuickUnlockPrivateSetModesFunction::SetModesChangedEventHandlerForTesting( |
| 110 const ModesChangedEventHandler& handler) { | 256 const ModesChangedEventHandler& handler) { |
| 111 modes_changed_handler_ = handler; | 257 modes_changed_handler_ = handler; |
| 112 } | 258 } |
| 113 | 259 |
| 114 ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() { | 260 ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() { |
| 115 params_ = SetModes::Params::Create(*args_); | 261 params_ = SetModes::Params::Create(*args_); |
| 116 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 262 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 117 | 263 |
| 118 if (params_->modes.size() != params_->credentials.size()) | 264 if (params_->modes.size() != params_->credentials.size()) |
| 119 return RespondNow(Error(kModesAndCredentialsLengthMismatch)); | 265 return RespondNow(Error(kModesAndCredentialsLengthMismatch)); |
| 120 | 266 |
| 121 if (params_->modes.size() > 1) | 267 if (params_->modes.size() > 1) |
| 122 return RespondNow(Error(kMultipleModesNotSupported)); | 268 return RespondNow(Error(kMultipleModesNotSupported)); |
| 123 | 269 |
| 124 // Verify every credential is numeric. | 270 // Verify every credential is valid based on policies. |
| 125 for (const std::string& credential : params_->credentials) { | 271 bool allow_weak = chrome_details_.GetProfile()->GetPrefs()->GetBoolean( |
| 126 if (!std::all_of(credential.begin(), credential.end(), ::isdigit)) | 272 prefs::kPinUnlockAllowWeakPins); |
| 127 return RespondNow(ArgumentList(SetModes::Results::Create(false))); | 273 for (size_t j = 0; j < params_->modes.size(); ++j) { |
| 274 if (params_->credentials[j].empty()) | |
| 275 continue; | |
| 276 | |
| 277 if (params_->modes[j] == QuickUnlockMode::QUICK_UNLOCK_MODE_PIN) { | |
| 278 if (!IsPinNumeric(params_->credentials[j]).empty()) | |
| 279 return RespondNow(ArgumentList(SetModes::Results::Create(false))); | |
| 280 | |
| 281 if (!IsPinLengthValid(params_->credentials[j], | |
| 282 chrome_details_.GetProfile(), nullptr, nullptr) | |
| 283 .empty()) { | |
| 284 return RespondNow(ArgumentList(SetModes::Results::Create(false))); | |
| 285 } | |
| 286 if (!allow_weak && | |
| 287 !IsPinDifficultEnough(params_->credentials[j]).empty()) { | |
| 288 return RespondNow(ArgumentList(SetModes::Results::Create(false))); | |
| 289 } | |
| 290 } | |
| 128 } | 291 } |
| 129 | 292 |
| 130 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( | 293 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( |
| 131 chrome_details_.GetProfile()); | 294 chrome_details_.GetProfile()); |
| 132 chromeos::UserContext user_context(user->GetAccountId()); | 295 chromeos::UserContext user_context(user->GetAccountId()); |
| 133 user_context.SetKey(chromeos::Key(params_->account_password)); | 296 user_context.SetKey(chromeos::Key(params_->account_password)); |
| 134 | 297 |
| 135 // Lazily allocate the authenticator. We do this here, instead of in the ctor, | 298 // Lazily allocate the authenticator. We do this here, instead of in the ctor, |
| 136 // so that tests can install a fake. | 299 // so that tests can install a fake. |
| 137 if (authenticator_allocator_.is_null()) | 300 if (authenticator_allocator_.is_null()) |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 } | 386 } |
| 224 | 387 |
| 225 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); | 388 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); |
| 226 std::unique_ptr<Event> event( | 389 std::unique_ptr<Event> event( |
| 227 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, | 390 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, |
| 228 OnActiveModesChanged::kEventName, std::move(args))); | 391 OnActiveModesChanged::kEventName, std::move(args))); |
| 229 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event)); | 392 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event)); |
| 230 } | 393 } |
| 231 | 394 |
| 232 } // namespace extensions | 395 } // namespace extensions |
| OLD | NEW |