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 = std::end(kMostCommonPins); |
| 131 bool is_common = std::find(kMostCommonPins, end, pin) != end; |
| 132 |
| 133 // Check for same digits, increasing PIN simutaneously. |
| 134 bool is_same = true; |
| 135 bool is_increasing = true; |
| 136 bool is_decreasing = true; |
| 137 for (int i = 1; i < int{pin.length()}; ++i) { |
| 138 const char previous = pin[i - 1]; |
| 139 const char current = pin[i]; |
| 140 |
| 141 is_same = is_same && (current == previous); |
| 142 is_increasing = is_increasing && (current == previous + 1); |
| 143 is_decreasing = is_decreasing && (current == previous - 1); |
| 144 } |
| 145 |
| 146 // PIN is considered weak if any of these conditions is met. |
| 147 if (is_same || is_increasing || is_decreasing || is_common) |
| 148 problems.push_back(CredentialProblem::CREDENTIAL_PROBLEM_TOO_WEAK); |
| 149 |
| 150 return problems; |
| 151 } |
| 152 |
| 153 void AppendToProblemList(std::vector<CredentialProblem>& dest, |
| 154 const std::vector<CredentialProblem>& src) { |
| 155 dest.insert(dest.end(), src.begin(), src.end()); |
| 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) {} |
| 200 |
| 201 QuickUnlockPrivateCheckCredentialFunction:: |
| 202 ~QuickUnlockPrivateCheckCredentialFunction() {} |
| 203 |
| 204 ExtensionFunction::ResponseAction |
| 205 QuickUnlockPrivateCheckCredentialFunction::Run() { |
| 206 params_ = CheckCredential::Params::Create(*args_); |
| 207 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 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; |
| 216 |
| 217 Profile* profile = chrome_details_.GetProfile(); |
| 218 PrefService* pref_service = profile->GetPrefs(); |
| 219 bool allow_weak = pref_service->GetBoolean(prefs::kPinUnlockAllowWeakPins); |
| 220 |
| 221 // Check and return the problems. |
| 222 std::vector<CredentialProblem> validity_problems = IsPinNumeric(credential); |
| 223 // Note: This function call writes values to |result->min_length| and |
| 224 // |result->max_length|. |
| 225 std::vector<CredentialProblem> length_problems = IsPinLengthValid( |
| 226 credential, profile, &result->min_length, &result->max_length); |
| 227 std::vector<CredentialProblem> weak_problems = |
| 228 IsPinDifficultEnough(credential); |
| 229 |
| 230 // Append the results from various checks to the appropriate list in |result|. |
| 231 // |validity_problems| and |length_problems| are always errors, while |
| 232 // |weak_problems| are errors or warnings depending on the policy. |
| 233 AppendToProblemList(result->errors, validity_problems); |
| 234 AppendToProblemList(result->errors, length_problems); |
| 235 AppendToProblemList(allow_weak ? result->warnings : result->errors, |
| 236 weak_problems); |
| 237 |
| 238 return RespondNow(ArgumentList(CheckCredential::Results::Create(*result))); |
| 239 } |
| 240 |
96 // quickUnlockPrivate.setModes | 241 // quickUnlockPrivate.setModes |
97 | 242 |
98 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() | 243 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() |
99 : chrome_details_(this) {} | 244 : chrome_details_(this) {} |
100 | 245 |
101 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} | 246 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} |
102 | 247 |
103 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting( | 248 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting( |
104 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator& | 249 const QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator& |
105 allocator) { | 250 allocator) { |
106 authenticator_allocator_ = allocator; | 251 authenticator_allocator_ = allocator; |
107 } | 252 } |
108 | 253 |
109 void QuickUnlockPrivateSetModesFunction::SetModesChangedEventHandlerForTesting( | 254 void QuickUnlockPrivateSetModesFunction::SetModesChangedEventHandlerForTesting( |
110 const ModesChangedEventHandler& handler) { | 255 const ModesChangedEventHandler& handler) { |
111 modes_changed_handler_ = handler; | 256 modes_changed_handler_ = handler; |
112 } | 257 } |
113 | 258 |
114 ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() { | 259 ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() { |
115 params_ = SetModes::Params::Create(*args_); | 260 params_ = SetModes::Params::Create(*args_); |
116 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 261 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
117 | 262 |
118 if (params_->modes.size() != params_->credentials.size()) | 263 if (params_->modes.size() != params_->credentials.size()) |
119 return RespondNow(Error(kModesAndCredentialsLengthMismatch)); | 264 return RespondNow(Error(kModesAndCredentialsLengthMismatch)); |
120 | 265 |
121 if (params_->modes.size() > 1) | 266 if (params_->modes.size() > 1) |
122 return RespondNow(Error(kMultipleModesNotSupported)); | 267 return RespondNow(Error(kMultipleModesNotSupported)); |
123 | 268 |
124 // Verify every credential is numeric. | 269 // Verify every credential is valid based on policies. |
125 for (const std::string& credential : params_->credentials) { | 270 bool allow_weak = chrome_details_.GetProfile()->GetPrefs()->GetBoolean( |
126 if (!std::all_of(credential.begin(), credential.end(), ::isdigit)) | 271 prefs::kPinUnlockAllowWeakPins); |
127 return RespondNow(ArgumentList(SetModes::Results::Create(false))); | 272 for (size_t j = 0; j < params_->modes.size(); ++j) { |
| 273 if (params_->credentials[j].empty()) |
| 274 continue; |
| 275 |
| 276 if (params_->modes[j] == QuickUnlockMode::QUICK_UNLOCK_MODE_PIN) { |
| 277 if (!IsPinNumeric(params_->credentials[j]).empty()) |
| 278 return RespondNow(ArgumentList(SetModes::Results::Create(false))); |
| 279 |
| 280 if (!IsPinLengthValid(params_->credentials[j], |
| 281 chrome_details_.GetProfile(), nullptr, nullptr) |
| 282 .empty()) { |
| 283 return RespondNow(ArgumentList(SetModes::Results::Create(false))); |
| 284 } |
| 285 if (!allow_weak && |
| 286 !IsPinDifficultEnough(params_->credentials[j]).empty()) { |
| 287 return RespondNow(ArgumentList(SetModes::Results::Create(false))); |
| 288 } |
| 289 } |
128 } | 290 } |
129 | 291 |
130 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( | 292 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( |
131 chrome_details_.GetProfile()); | 293 chrome_details_.GetProfile()); |
132 chromeos::UserContext user_context(user->GetAccountId()); | 294 chromeos::UserContext user_context(user->GetAccountId()); |
133 user_context.SetKey(chromeos::Key(params_->account_password)); | 295 user_context.SetKey(chromeos::Key(params_->account_password)); |
134 | 296 |
135 // Lazily allocate the authenticator. We do this here, instead of in the ctor, | 297 // Lazily allocate the authenticator. We do this here, instead of in the ctor, |
136 // so that tests can install a fake. | 298 // so that tests can install a fake. |
137 if (authenticator_allocator_.is_null()) | 299 if (authenticator_allocator_.is_null()) |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 } | 385 } |
224 | 386 |
225 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); | 387 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); |
226 std::unique_ptr<Event> event( | 388 std::unique_ptr<Event> event( |
227 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, | 389 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, |
228 OnActiveModesChanged::kEventName, std::move(args))); | 390 OnActiveModesChanged::kEventName, std::move(args))); |
229 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event)); | 391 EventRouter::Get(browser_context())->BroadcastEvent(std::move(event)); |
230 } | 392 } |
231 | 393 |
232 } // namespace extensions | 394 } // namespace extensions |
OLD | NEW |