Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(281)

Side by Side Diff: chrome/browser/password_manager/password_manager_util_win.cc

Issue 34393007: [Win] Add option to reauthenticate the OS user before revealing passwords. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@password
Patch Set: rebase, and fix issue with last_changed return value Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 // windows.h must be first otherwise Win8 SDK breaks.
6 #include <windows.h>
7 #include <wincred.h>
8 #include <LM.h>
9
10 // SECURITY_WIN32 must be defined in order to get
11 // EXTENDED_NAME_FORMAT enumeration.
12 #define SECURITY_WIN32 1
13 #include <security.h>
14 #undef SECURITY_WIN32
15
16 #include "chrome/browser/password_manager/password_manager_util.h"
17
18 #include "base/prefs/pref_registry_simple.h"
19 #include "base/prefs/pref_service.h"
20 #include "base/safe_numerics.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "base/time/time.h"
23 #include "chrome/browser/browser_process.h"
24 #include "chrome/browser/password_manager/password_manager.h"
25 #include "chrome/common/pref_names.h"
26 #include "content/public/browser/render_view_host.h"
27 #include "content/public/browser/render_widget_host_view.h"
28 #include "content/public/browser/web_contents.h"
29 #include "grit/chromium_strings.h"
30 #include "grit/generated_resources.h"
31 #include "ui/base/l10n/l10n_util.h"
32
33 #if defined(USE_AURA)
34 #include "ui/aura/root_window.h"
35 #include "ui/aura/window.h"
36 #endif
37
38 // static
39 void PasswordManager::RegisterLocalPrefs(PrefRegistrySimple* registry) {
40 registry->RegisterInt64Pref(prefs::kOsPasswordLastChanged, 0);
41 registry->RegisterBooleanPref(prefs::kOsPasswordBlank, false);
42 }
43
44 namespace password_manager_util {
45
46 const unsigned kMaxPasswordRetries = 3;
47
48 const unsigned kCredUiDefaultFlags =
49 CREDUI_FLAGS_GENERIC_CREDENTIALS |
50 CREDUI_FLAGS_EXCLUDE_CERTIFICATES |
51 CREDUI_FLAGS_KEEP_USERNAME |
52 CREDUI_FLAGS_ALWAYS_SHOW_UI |
53 CREDUI_FLAGS_DO_NOT_PERSIST;
54
55 static int64 GetPasswordLastChanged(WCHAR* username) {
56 LPUSER_INFO_1 user_info = NULL;
57 DWORD age = 0;
58
59 NET_API_STATUS ret = NetUserGetInfo(NULL, username, 1, (LPBYTE*) &user_info);
60
61 if (ret == NERR_Success) {
62 // Returns seconds since last password change.
63 age = user_info->usri1_password_age;
64 NetApiBufferFree(user_info);
65 } else {
66 return -1;
67 }
68
69 base::Time changed = base::Time::Now() - base::TimeDelta::FromSeconds(age);
70
71 return changed.ToInternalValue();
72 }
73
74 static bool CheckBlankPassword(WCHAR* username) {
75 PrefService* local_state = g_browser_process->local_state();
76 int64 last_changed = GetPasswordLastChanged(username);
77 bool need_recheck = true;
78 bool blank_password = false;
79
80 // If we cannot determine when the password was last changed
81 // then assume the password is not blank
82 if (last_changed == -1)
83 return false;
84
85 if (local_state->HasPrefPath(prefs::kOsPasswordBlank)) {
86 blank_password = local_state->GetBoolean(prefs::kOsPasswordBlank);
Bernhard Bauer 2013/12/03 09:34:04 It's fine to call this method even if the preferen
Will Harris 2013/12/03 21:16:57 Done.
87 if (local_state->HasPrefPath(prefs::kOsPasswordLastChanged)) {
88 int64 pref_last_changed =
89 local_state->GetInt64(prefs::kOsPasswordLastChanged);
90 if (pref_last_changed > 0 &&
91 last_changed <= pref_last_changed) {
92 need_recheck = false;
93 }
94 }
95 }
96
97 if (need_recheck) {
98 HANDLE handle = INVALID_HANDLE_VALUE;
99
100 // Attempt to login using blank password.
101 DWORD logon_result = LogonUser(username,
102 L".",
103 L"",
104 LOGON32_LOGON_NETWORK,
105 LOGON32_PROVIDER_DEFAULT,
106 &handle);
107
108 // Win XP and later return ERROR_ACCOUNT_RESTRICTION for blank password.
109 if (logon_result)
110 CloseHandle(handle);
111
112 // In the case the password is blank, then LogonUser returns a failure,
113 // handle is INVALID_HANDLE_VALUE, and GetLastError() is
114 // ERROR_ACCOUNT_RESTRICTION.
115 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms681385
116 blank_password = (logon_result ||
117 GetLastError() == ERROR_ACCOUNT_RESTRICTION);
118 }
119
120 // Account for clock skew between pulling the password age and
121 // writing to the preferences by adding a small skew factor here.
122 last_changed += base::Time::kMicrosecondsPerSecond;
123
124 // Save the blank password status for later.
125 local_state->SetBoolean(prefs::kOsPasswordBlank, blank_password);
126 local_state->SetInt64(prefs::kOsPasswordLastChanged, last_changed);
127
128 return blank_password;
129 }
130
131 bool AuthenticateUser(content::WebContents* web_contents) {
132 bool retval = false;
133 CREDUI_INFO cui = {};
134 WCHAR username[CREDUI_MAX_USERNAME_LENGTH+1] = {};
135 WCHAR displayname[CREDUI_MAX_USERNAME_LENGTH+1] = {};
136 WCHAR password[CREDUI_MAX_PASSWORD_LENGTH+1] = {};
137 DWORD username_length = CREDUI_MAX_USERNAME_LENGTH;
138 std::wstring product_name =
139 UTF16ToWide(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
140 std::wstring password_prompt =
141 UTF16ToWide(l10n_util::GetStringUTF16(
142 IDS_PASSWORDS_PAGE_AUTHENTICATION_PROMPT));
143 HANDLE handle = INVALID_HANDLE_VALUE;
144 int tries = 0;
145 bool use_displayname = false;
146 bool use_principalname = false;
147 DWORD logon_result = 0;
148
149 // On a domain, we obtain the User Principal Name
150 // for domain authentication.
151 if (GetUserNameEx(NameUserPrincipal, username, &username_length)) {
152 use_principalname = true;
153 } else {
154 username_length = CREDUI_MAX_USERNAME_LENGTH;
155 // Otherwise, we're a workstation, use the plain local username.
156 if (!GetUserName(username, &username_length)) {
157 DLOG(ERROR) << "Unable to obtain username " << GetLastError();
158 return false;
159 } else {
160 // As we are on a workstation, it's possible the user
161 // has no password, so check here.
162 if (CheckBlankPassword(username))
163 return true;
164 }
165 }
166
167 // Try and obtain a friendly display name.
168 username_length = CREDUI_MAX_USERNAME_LENGTH;
169 if (GetUserNameEx(NameDisplay, displayname, &username_length))
170 use_displayname = true;
171
172 cui.cbSize = sizeof(CREDUI_INFO);
173 cui.hwndParent = NULL;
174
175 gfx::NativeView view = web_contents->GetRenderViewHost()->
176 GetView()->GetNativeView();
177 #if defined(USE_AURA)
178 cui.hwndParent = view->GetDispatcher()->host()->GetAcceleratedWidget();
179 #else
180 cui.hwndParent = view;
181 #endif
182
183 cui.pszMessageText = password_prompt.c_str();
184 cui.pszCaptionText = product_name.c_str();
185
186 cui.hbmBanner = NULL;
187 BOOL save_password = FALSE;
188 DWORD credErr = NO_ERROR;
189
190 do {
191 tries++;
192
193 // TODO(wfh) Make sure we support smart cards here.
194 credErr = CredUIPromptForCredentials(
195 &cui,
196 product_name.c_str(),
197 NULL,
198 0,
199 use_displayname ? displayname : username,
200 CREDUI_MAX_USERNAME_LENGTH+1,
201 password,
202 CREDUI_MAX_PASSWORD_LENGTH+1,
203 &save_password,
204 kCredUiDefaultFlags |
205 (tries > 1 ? CREDUI_FLAGS_INCORRECT_PASSWORD : 0));
206
207 if (credErr == NO_ERROR) {
208 logon_result = LogonUser(username,
209 use_principalname ? NULL : L".",
210 password,
211 LOGON32_LOGON_NETWORK,
212 LOGON32_PROVIDER_DEFAULT,
213 &handle);
214 if (logon_result) {
215 retval = true;
216 CloseHandle(handle);
217 } else {
218 if (GetLastError() == ERROR_ACCOUNT_RESTRICTION &&
219 wcslen(password) == 0) {
220 // Password is blank, so permit.
221 retval = true;
222 } else {
223 DLOG(WARNING) << "Unable to authenticate " << GetLastError();
224 }
225 }
226 SecureZeroMemory(password, sizeof(password));
227 }
228 } while (credErr == NO_ERROR &&
229 (retval == false && tries < kMaxPasswordRetries));
230 return retval;
231 }
232
233 } // namespace password_manager_util
OLDNEW
« no previous file with comments | « chrome/browser/password_manager/password_manager_util_stub.cc ('k') | chrome/browser/prefs/browser_prefs.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698