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