OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/importer/nss_decryptor.h" | 5 #include "chrome/browser/importer/nss_decryptor.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/string_split.h" | 12 #include "base/string_split.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "sql/connection.h" | 15 #include "sql/connection.h" |
16 #include "sql/statement.h" | 16 #include "sql/statement.h" |
17 #include "webkit/glue/password_form.h" | 17 #include "webkit/forms/password_form.h" |
18 | 18 |
19 #if defined(USE_NSS) | 19 #if defined(USE_NSS) |
20 #include <pk11pub.h> | 20 #include <pk11pub.h> |
21 #include <pk11sdr.h> | 21 #include <pk11sdr.h> |
22 #endif // defined(USE_NSS) | 22 #endif // defined(USE_NSS) |
23 | 23 |
24 // This method is based on some Firefox code in | 24 // This method is based on some Firefox code in |
25 // security/manager/ssl/src/nsSDR.cpp | 25 // security/manager/ssl/src/nsSDR.cpp |
26 // The license block is: | 26 // The license block is: |
27 | 27 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 103 |
104 return UTF8ToUTF16(plain); | 104 return UTF8ToUTF16(plain); |
105 } | 105 } |
106 | 106 |
107 // There are three versions of password files. They store saved user | 107 // There are three versions of password files. They store saved user |
108 // names and passwords. | 108 // names and passwords. |
109 // References: | 109 // References: |
110 // http://kb.mozillazine.org/Signons.txt | 110 // http://kb.mozillazine.org/Signons.txt |
111 // http://kb.mozillazine.org/Signons2.txt | 111 // http://kb.mozillazine.org/Signons2.txt |
112 // http://kb.mozillazine.org/Signons3.txt | 112 // http://kb.mozillazine.org/Signons3.txt |
113 void NSSDecryptor::ParseSignons(const std::string& content, | 113 void NSSDecryptor::ParseSignons( |
114 std::vector<webkit_glue::PasswordForm>* forms) { | 114 const std::string& content, |
| 115 std::vector<webkit::forms::PasswordForm>* forms) { |
115 forms->clear(); | 116 forms->clear(); |
116 | 117 |
117 // Splits the file content into lines. | 118 // Splits the file content into lines. |
118 std::vector<std::string> lines; | 119 std::vector<std::string> lines; |
119 base::SplitString(content, '\n', &lines); | 120 base::SplitString(content, '\n', &lines); |
120 | 121 |
121 // The first line is the file version. We skip the unknown versions. | 122 // The first line is the file version. We skip the unknown versions. |
122 if (lines.empty()) | 123 if (lines.empty()) |
123 return; | 124 return; |
124 int version; | 125 int version; |
125 if (lines[0] == "#2c") | 126 if (lines[0] == "#2c") |
126 version = 1; | 127 version = 1; |
127 else if (lines[0] == "#2d") | 128 else if (lines[0] == "#2d") |
128 version = 2; | 129 version = 2; |
129 else if (lines[0] == "#2e") | 130 else if (lines[0] == "#2e") |
130 version = 3; | 131 version = 3; |
131 else | 132 else |
132 return; | 133 return; |
133 | 134 |
134 GURL::Replacements rep; | 135 GURL::Replacements rep; |
135 rep.ClearQuery(); | 136 rep.ClearQuery(); |
136 rep.ClearRef(); | 137 rep.ClearRef(); |
137 rep.ClearUsername(); | 138 rep.ClearUsername(); |
138 rep.ClearPassword(); | 139 rep.ClearPassword(); |
139 | 140 |
140 // Reads never-saved list. Domains are stored one per line. | 141 // Reads never-saved list. Domains are stored one per line. |
141 size_t i; | 142 size_t i; |
142 for (i = 1; i < lines.size() && lines[i].compare(".") != 0; ++i) { | 143 for (i = 1; i < lines.size() && lines[i].compare(".") != 0; ++i) { |
143 webkit_glue::PasswordForm form; | 144 webkit::forms::PasswordForm form; |
144 form.origin = GURL(lines[i]).ReplaceComponents(rep); | 145 form.origin = GURL(lines[i]).ReplaceComponents(rep); |
145 form.signon_realm = form.origin.GetOrigin().spec(); | 146 form.signon_realm = form.origin.GetOrigin().spec(); |
146 form.blacklisted_by_user = true; | 147 form.blacklisted_by_user = true; |
147 forms->push_back(form); | 148 forms->push_back(form); |
148 } | 149 } |
149 ++i; | 150 ++i; |
150 | 151 |
151 // Reads saved passwords. The information is stored in blocks | 152 // Reads saved passwords. The information is stored in blocks |
152 // seperated by lines that only contain a dot. We find a block | 153 // seperated by lines that only contain a dot. We find a block |
153 // by the seperator and parse them one by one. | 154 // by the seperator and parse them one by one. |
154 while (i < lines.size()) { | 155 while (i < lines.size()) { |
155 size_t begin = i; | 156 size_t begin = i; |
156 size_t end = i + 1; | 157 size_t end = i + 1; |
157 while (end < lines.size() && lines[end].compare(".") != 0) | 158 while (end < lines.size() && lines[end].compare(".") != 0) |
158 ++end; | 159 ++end; |
159 i = end + 1; | 160 i = end + 1; |
160 | 161 |
161 // A block has at least five lines. | 162 // A block has at least five lines. |
162 if (end - begin < 5) | 163 if (end - begin < 5) |
163 continue; | 164 continue; |
164 | 165 |
165 webkit_glue::PasswordForm form; | 166 webkit::forms::PasswordForm form; |
166 | 167 |
167 // The first line is the site URL. | 168 // The first line is the site URL. |
168 // For HTTP authentication logins, the URL may contain http realm, | 169 // For HTTP authentication logins, the URL may contain http realm, |
169 // which will be in bracket: | 170 // which will be in bracket: |
170 // sitename:8080 (realm) | 171 // sitename:8080 (realm) |
171 GURL url; | 172 GURL url; |
172 std::string realm; | 173 std::string realm; |
173 const char kRealmBracketBegin[] = " ("; | 174 const char kRealmBracketBegin[] = " ("; |
174 const char kRealmBracketEnd[] = ")"; | 175 const char kRealmBracketEnd[] = ")"; |
175 if (lines[begin].find(kRealmBracketBegin) != std::string::npos) { | 176 if (lines[begin].find(kRealmBracketBegin) != std::string::npos) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 if (version == 3) { | 226 if (version == 3) { |
226 ++begin; | 227 ++begin; |
227 } | 228 } |
228 | 229 |
229 forms->push_back(form); | 230 forms->push_back(form); |
230 } | 231 } |
231 } | 232 } |
232 } | 233 } |
233 | 234 |
234 bool NSSDecryptor::ReadAndParseSignons(const FilePath& sqlite_file, | 235 bool NSSDecryptor::ReadAndParseSignons(const FilePath& sqlite_file, |
235 std::vector<webkit_glue::PasswordForm>* forms) { | 236 std::vector<webkit::forms::PasswordForm>* forms) { |
236 sql::Connection db; | 237 sql::Connection db; |
237 if (!db.Open(sqlite_file)) | 238 if (!db.Open(sqlite_file)) |
238 return false; | 239 return false; |
239 | 240 |
240 const char* query = "SELECT hostname FROM moz_disabledHosts"; | 241 const char* query = "SELECT hostname FROM moz_disabledHosts"; |
241 sql::Statement s(db.GetUniqueStatement(query)); | 242 sql::Statement s(db.GetUniqueStatement(query)); |
242 if (!s) | 243 if (!s) |
243 return false; | 244 return false; |
244 | 245 |
245 GURL::Replacements rep; | 246 GURL::Replacements rep; |
246 rep.ClearQuery(); | 247 rep.ClearQuery(); |
247 rep.ClearRef(); | 248 rep.ClearRef(); |
248 rep.ClearUsername(); | 249 rep.ClearUsername(); |
249 rep.ClearPassword(); | 250 rep.ClearPassword(); |
250 // Read domains for which passwords are never saved. | 251 // Read domains for which passwords are never saved. |
251 while (s.Step()) { | 252 while (s.Step()) { |
252 webkit_glue::PasswordForm form; | 253 webkit::forms::PasswordForm form; |
253 form.origin = GURL(s.ColumnString(0)).ReplaceComponents(rep); | 254 form.origin = GURL(s.ColumnString(0)).ReplaceComponents(rep); |
254 form.signon_realm = form.origin.GetOrigin().spec(); | 255 form.signon_realm = form.origin.GetOrigin().spec(); |
255 form.blacklisted_by_user = true; | 256 form.blacklisted_by_user = true; |
256 forms->push_back(form); | 257 forms->push_back(form); |
257 } | 258 } |
258 | 259 |
259 const char* query2 = "SELECT hostname, httpRealm, formSubmitURL, " | 260 const char* query2 = "SELECT hostname, httpRealm, formSubmitURL, " |
260 "usernameField, passwordField, encryptedUsername, " | 261 "usernameField, passwordField, encryptedUsername, " |
261 "encryptedPassword FROM moz_logins"; | 262 "encryptedPassword FROM moz_logins"; |
262 | 263 |
(...skipping 10 matching lines...) Expand all Loading... |
273 if (host.find("://") == std::string::npos) | 274 if (host.find("://") == std::string::npos) |
274 host = "http://" + host; | 275 host = "http://" + host; |
275 url = GURL(host); | 276 url = GURL(host); |
276 } else { | 277 } else { |
277 url = GURL(s2.ColumnString(0)); | 278 url = GURL(s2.ColumnString(0)); |
278 } | 279 } |
279 // Skip this row if the URL is not valid. | 280 // Skip this row if the URL is not valid. |
280 if (!url.is_valid()) | 281 if (!url.is_valid()) |
281 continue; | 282 continue; |
282 | 283 |
283 webkit_glue::PasswordForm form; | 284 webkit::forms::PasswordForm form; |
284 form.origin = url.ReplaceComponents(rep); | 285 form.origin = url.ReplaceComponents(rep); |
285 form.signon_realm = form.origin.GetOrigin().spec(); | 286 form.signon_realm = form.origin.GetOrigin().spec(); |
286 if (!realm.empty()) | 287 if (!realm.empty()) |
287 form.signon_realm += realm; | 288 form.signon_realm += realm; |
288 form.ssl_valid = form.origin.SchemeIsSecure(); | 289 form.ssl_valid = form.origin.SchemeIsSecure(); |
289 // The user name, password and action. | 290 // The user name, password and action. |
290 form.username_element = s2.ColumnString16(3); | 291 form.username_element = s2.ColumnString16(3); |
291 form.username_value = Decrypt(s2.ColumnString(5)); | 292 form.username_value = Decrypt(s2.ColumnString(5)); |
292 form.password_element = s2.ColumnString16(4); | 293 form.password_element = s2.ColumnString16(4); |
293 form.password_value = Decrypt(s2.ColumnString(6)); | 294 form.password_value = Decrypt(s2.ColumnString(6)); |
294 form.action = GURL(s2.ColumnString(2)).ReplaceComponents(rep); | 295 form.action = GURL(s2.ColumnString(2)).ReplaceComponents(rep); |
295 forms->push_back(form); | 296 forms->push_back(form); |
296 } | 297 } |
297 return true; | 298 return true; |
298 } | 299 } |
OLD | NEW |